Debian Codegraph @

CorsixTH/Src/iso_fs.cpp

raw
1 /*
2 Copyright (c) 2010 Peter "Corsix" Cawley
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in
6 the Software without restriction, including without limitation the rights to
7 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8 of the Software, and to permit persons to whom the Software is furnished to do
9 so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 SOFTWARE.
21 */
22
23 #include "iso_fs.h"
24
25 #include <algorithm>
26 #include <cstdarg>
27 #include <cstdio>
28 #include <cstring>
29 #include <exception>
30 #include <iterator>
31 #include <memory>
32 #include <stdexcept>
33 #include <utility>
34 #include <vector>
35
36 #include "th.h"
37
38 namespace {
39
40 enum iso_volume_descriptor_type : uint8_t {
41 vdt_primary_volume = 0x01,
42 // Other type numbers are either reserved for future use, or are not
43 // interesting to us.
44 vdt_terminator = 0xFF,
45 };
46
47 /// Flag values for directory table entries. The flag itself is a bitmask.
48 enum iso_dir_ent_flag : uint8_t {
49 def_hidden = 0x01,
50 def_directory = 0x02,
51 def_multi_extent = 0x80,
52 };
53
54 /// Offset to the 32bit sector of the file data
55 /// from the start of the file entry.
56 constexpr ptrdiff_t file_sector_offset = 2;
57
58 /// Offset to the 32bit length of the file data
59 /// from the start of the file entry
60 constexpr ptrdiff_t file_data_length_offset = 10;
61
62 /// The offset of the file flags (e.g. directory vs file)
63 /// from the start of the file entry.
64 constexpr ptrdiff_t file_flags_offset = 25;
65
66 /// The offset of the byte that stores the length of the filename
67 /// from the start of the file entry.
68 constexpr ptrdiff_t filename_length_offset = 32;
69
70 /// The offset of the start of the filename or directory identifier
71 /// from the start of the file entry.
72 constexpr ptrdiff_t filename_offset = 33;
73
74 /// The minimum valid size of a valid file entry.
75 /// Accounts for all fixed header value offsets and even number padding.
76 constexpr uint8_t minimum_file_entry_size = 34;
77
78 /// Formal depth limit in spec is 8. We allows for loose implementations.
79 constexpr int max_directory_depth = 16;
80
81 /// Reasonably unique name of a file from Theme Hospital that can be used to
82 /// indicate that we've loaded the right directory.
83 constexpr const char* vblk_0_filename = "VBLK-0.TAB";
84
85 /// Offset of the sector size from the primary volume descriptor
86 constexpr size_t sector_size_offset = 128;
87
88 /// Offset of the root directory entry from the primary volume descriptor
89 constexpr ptrdiff_t root_directory_offset = 156;
90
91 /// The root directory entry is a fixed size.
92 constexpr size_t root_directory_entry_size = 34;
93
94 /// ISO 9660 has a 32kb reserve area at the beginning of the file formal
95 /// e.g. boot information.
96 constexpr uint32_t first_filesystem_sector = 16;
97
98 /// Finds the length of the file name within a file identifier.
99 /// The file identifier is `filename;file id`.
100 void trim_file_id(const uint8_t* sIdent, uint8_t& iLength) {
101 for (uint8_t i = 0; i < iLength; ++i) {
102 if (sIdent[i] == ';') {
103 iLength = i;
104 return;
105 }
106 }
107 }
108
109 /// Convert character to filename normalized format conforming to ISO filename
110 /// limitations. All letters are converted to upper case, and `_` to `-`.
111 char normalise(char c) {
112 if (c == '_') {
113 return '-';
114 } else if ('a' <= c && c <= 'z') {
115 return static_cast<char>(c - 'a' + 'A');
116 } else {
117 return c;
118 }
119 }
120
121 /// Convert length bytes from the start pointer to a normalized filename
122 /// string. All ASCII letters are converted to upper case, and `_` to `-`.
123 std::string normalise(const uint8_t* start, size_t length) {
124 std::string ret;
125 const uint8_t* p = start;
126 for (size_t i = 0; i < length; i++) {
127 ret.push_back(normalise(static_cast<char>(*p)));
128 ++p;
129 }
130 return ret;
131 }
132
133 /// Convert c string to normalized filename string. All ASCII letters are
134 /// converted to upper case, and `_` to `-`.
135 std::string normalise(const char* str) {
136 std::string ret;
137 const char* p = str;
138 while (*p != '\0') {
139 ret.push_back(normalise(*p));
140 ++p;
141 }
142 return ret;
143 }
144
145 /// A file entry from the directory table
146 class iso_file_entry {
147 public:
148 /// Construct dummy entry.
149 iso_file_entry() = default;
150
151 /// Construct entry from the given memory location.
152 /// The first byte is the size of the entry. Other useful headers are read
153 /// from their offsets from that location.
154 ///
155 /// \param b Pointer to first byte of file entry in directory table.
156 explicit iso_file_entry(const uint8_t* b) {
157 uint8_t size = *b;
158 if (size < minimum_file_entry_size) {
159 throw std::runtime_error("size specified for file entry is too small.");
160 }
161
162 uint8_t filename_length = b[filename_length_offset];
163 if (filename_length + filename_offset > size) {
164 throw std::runtime_error("size specified for file entry is too small.");
165 }
166 trim_file_id(b + filename_offset, filename_length);
167 filename = normalise(b + filename_offset, filename_length);
168
169 data_sector = bytes_to_uint32_le(b + file_sector_offset);
170 data_length = bytes_to_uint32_le(b + file_data_length_offset);
171 flags = b[file_flags_offset];
172 }
173
174 /// Logical location of the data for this file in the ISO image.
175 uint32_t data_sector{};
176
177 /// The length of the data for this file.
178 uint32_t data_length{};
179
180 /// Flags that indicate whether this entry is a file or directory, along
181 /// with other properties.
182 ///
183 /// \see iso_dir_ent_flag
184 uint8_t flags{};
185
186 /// The filename of this entry.
187 std::string filename;
188 };
189
190 /**
191 * Input iterator (forward only, read only) for an ISO 9660 directory table
192 * stored in a byte buffer.
193 */
194 class iso_directory_iterator final {
195 using iterator_category = std::input_iterator_tag;
196 using value_type = const iso_file_entry;
197 using difference_type = ptrdiff_t;
198 using pointer = const iso_file_entry*;
199 using reference = const iso_file_entry&;
200
201 public:
202 iso_directory_iterator() = delete;
203
204 /**
205 * Initialize an iterator for the directory table in the memory region
206 * defined by by begin and end. This iterator is aware of its container
207 * and will throw an exception if an attempt is made to access it out of
208 * range.
209 *
210 * \param begin pointer to the first byte of the directory table.
211 * \param end pointer to the first byte following the directory table.
212 */
213 iso_directory_iterator(const uint8_t* begin, const uint8_t* end) {
214 directory_ptr = begin;
215 end_ptr = end;
216 if (directory_ptr >= end_ptr) {
217 // dummy value, not accessible.
218 entry = iso_file_entry();
219 } else {
220 entry = iso_file_entry(begin);
221 }
222 }
223
224 /**
225 * Copy the given iso_directory_iterator
226 */
227 iso_directory_iterator(iso_directory_iterator& it) {
228 directory_ptr = it.directory_ptr;
229 end_ptr = it.end_ptr;
230 entry = it.entry;
231 }
232
233 /**
234 * Move the given iso_directory_iterator
235 */
236 iso_directory_iterator(iso_directory_iterator&& it) noexcept {
237 directory_ptr = it.directory_ptr;
238 end_ptr = it.end_ptr;
239 entry = std::move(it.entry);
240 it.directory_ptr = nullptr;
241 it.end_ptr = nullptr;
242 it.entry = iso_file_entry();
243 }
244
245 ~iso_directory_iterator() = default;
246
247 /**
248 * Determine whether two iso_directory_iterators point to the same table
249 * entry.
250 */
251 bool operator==(const iso_directory_iterator& rhs) const {
252 return (this->directory_ptr == rhs.directory_ptr);
253 }
254
255 /**
256 * Determine whether to iso_directory_iterators do not point to the same
257 * table entry.
258 */
259 bool operator!=(const iso_directory_iterator& rhs) const {
260 return !((*this) == rhs);
261 }
262
263 /**
264 * Get the file entry pointed to by the iterator.
265 */
266 reference operator*() const {
267 if (directory_ptr >= end_ptr) {
268 throw std::out_of_range("iso directory iterator is past end of input");
269 }
270 return entry;
271 }
272
273 /**
274 * Assign this iterator the value of another iterator by copy
275 */
276 iso_directory_iterator& operator=(iso_directory_iterator& rhs) = default;
277
278 /**
279 * Assign this iterator the value of another iterator by move
280 */
281 iso_directory_iterator& operator=(iso_directory_iterator&& rhs) noexcept {
282 directory_ptr = rhs.directory_ptr;
283 end_ptr = rhs.end_ptr;
284 entry = std::move(rhs.entry);
285 rhs.directory_ptr = nullptr;
286 rhs.end_ptr = nullptr;
287 rhs.entry = {};
288 return *this;
289 }
290
291 /**
292 * Advance this iterator to the next file entry in the directory table,
293 * returning the result.
294 * In cases where advancing the iterator would read past the end of the
295 * directory table, an exception is thrown and the iterator is not
296 * advanced.
297 */
298 iso_directory_iterator& operator++() {
299 if (directory_ptr >= end_ptr) {
300 throw std::out_of_range(
301 "Cannot advance iso directory iterator past end of input");
302 }
303
304 const uint8_t* new_dir_ptr = directory_ptr + *directory_ptr;
305 while (new_dir_ptr < end_ptr && *new_dir_ptr == 0) {
306 ++new_dir_ptr;
307 }
308
309 // Catch a malformed directory entry where the size would extend past
310 // the end of the table.
311 if (new_dir_ptr < end_ptr && new_dir_ptr + *new_dir_ptr > end_ptr) {
312 throw std::runtime_error(
313 "The last directory entry was larger than the defined "
314 "table region.");
315 }
316
317 if (new_dir_ptr < end_ptr) {
318 entry = iso_file_entry(new_dir_ptr);
319 } else {
320 entry = iso_file_entry();
321 }
322 directory_ptr = new_dir_ptr;
323 return *this;
324 }
325
326 /**
327 * Advance this iterator to the next file entry in the directory table,
328 * returning a copy of the old iterator.
329 */
330 iso_directory_iterator operator++(int) {
331 iso_directory_iterator old(*this);
332 ++(*this);
333 return old;
334 }
335
336 private:
337 /// Pointer to the current entry.
338 const uint8_t* directory_ptr;
339
340 /// Pointer to the end of the directory table.
341 const uint8_t* end_ptr;
342
343 /// Current entry.
344 iso_file_entry entry;
345 };
346
347 } // namespace
348
349 iso_filesystem::iso_filesystem(const char* path, char pathSeparator)
350 : path_seperator(pathSeparator),
351 raw_file(std::fopen(path, "rb"), &std::fclose) {
352 if (!raw_file) {
353 throw std::runtime_error("Failed to open ISO file");
354 }
355
356 // The first 16 sectors are reserved for bootable media.
357 // Volume descriptor records follow this, with one record per sector.
358 for (uint32_t iSector = first_filesystem_sector; seek_to_sector(iSector);
359 ++iSector) {
360 uint8_t aBuffer[root_directory_offset + root_directory_entry_size];
361 if (!read_data(sizeof(aBuffer), aBuffer)) {
362 break;
363 }
364 // CD001 is a standard identifier, \x01 is a version number
365 if (std::memcmp(aBuffer + 1, "CD001\x01", 6) == 0) {
366 if (aBuffer[0] == vdt_primary_volume) {
367 sector_size = bytes_to_uint16_le(aBuffer + sector_size_offset);
368 find_hosp_directory(aBuffer + root_directory_offset,
369 root_directory_entry_size, 0);
370 if (files.empty()) {
371 throw(
372 std::runtime_error("Could not find Theme Hospital data "
373 "directory."));
374 }
375 return;
376 } else if (aBuffer[0] == vdt_terminator) {
377 break;
378 }
379 }
380 }
381 throw(std::runtime_error("Could not find primary volume descriptor."));
382 }
383
384 bool iso_filesystem::file_metadata_less(const file_metadata& lhs,
385 const file_metadata& rhs) {
386 return lhs.path < rhs.path;
387 }
388
389 int iso_filesystem::find_hosp_directory(const uint8_t* pDirEnt,
390 const uint32_t dirEntsSize, int level) {
391 // Sanity check
392 // Apart from at the root level, directory record arrays must take up whole
393 // sectors, whose sizes are powers of two and at least 2048.
394 // The formal limit on directory depth is 8, so hitting 16 is insane.
395 if ((level != 0 && (dirEntsSize & (min_sector_size - 1)) != 0) ||
396 level > max_directory_depth)
397 return 0;
398
399 std::unique_ptr<uint8_t[]> pBuffer(nullptr);
400 uint32_t iBufferSize = 0;
401 iso_directory_iterator dir_iter(pDirEnt, pDirEnt + dirEntsSize);
402 iso_directory_iterator end_iter(pDirEnt + dirEntsSize, pDirEnt + dirEntsSize);
403 for (; dir_iter != end_iter; ++dir_iter) {
404 const iso_file_entry& ent = *dir_iter;
405 if (ent.flags & def_directory) {
406 // The names "\x00" and "\x01" are used for the current directory and
407 // the parent directory respectively. We only want to visit these
408 // when at the root level.
409 if (level == 0 || !(ent.filename == std::string(1, '\x00') ||
410 ent.filename == std::string(1, '\x01'))) {
411 if (ent.data_length > iBufferSize) {
412 iBufferSize = ent.data_length;
413 pBuffer = std::make_unique<uint8_t[]>(iBufferSize);
414 }
415 if (seek_to_sector(ent.data_sector) &&
416 read_data(ent.data_length, pBuffer.get())) {
417 int iFoundLevel =
418 find_hosp_directory(pBuffer.get(), ent.data_length, level + 1);
419 if (iFoundLevel != 0) {
420 if (iFoundLevel == 2) {
421 build_file_lookup_table(ent.data_sector, ent.data_length,
422 std::string(""));
423 }
424 return iFoundLevel + 1;
425 }
426 }
427 }
428 } else {
429 // Look for VBLK-0.TAB to serve as indication that we've found the
430 // Theme Hospital data.
431 if (ent.filename == vblk_0_filename) {
432 return 1;
433 }
434 }
435 }
436
437 return 0;
438 }
439
440 void iso_filesystem::build_file_lookup_table(const uint32_t iSector,
441 const uint32_t dirEntsSize,
442 const std::string_view prefix) {
443 // Sanity check
444 // Apart from at the root level, directory record arrays must take up whole
445 // sectors, whose sizes are powers of two and at least 2048.
446 // Path lengths shouldn't exceed 256 either (or at least not for the files
447 // which we're interested in).
448 if ((!prefix.empty() && (dirEntsSize & 0x7FF)) || (prefix.size() > 256))
449 return;
450
451 uint8_t* pBuffer = new uint8_t[dirEntsSize];
452 if (!seek_to_sector(iSector) || !read_data(dirEntsSize, pBuffer)) {
453 delete[] pBuffer;
454 return;
455 }
456
457 uint8_t* pDirEnt = pBuffer;
458 iso_directory_iterator dir_iter(pDirEnt, pDirEnt + dirEntsSize);
459 iso_directory_iterator end_iter(pDirEnt + dirEntsSize, pDirEnt + dirEntsSize);
460 for (; dir_iter != end_iter; ++dir_iter) {
461 const iso_file_entry& ent = *dir_iter;
462 std::string path;
463 if (prefix.empty()) {
464 path = ent.filename;
465 } else {
466 path += prefix;
467 path += path_seperator;
468 path += ent.filename;
469 }
470
471 if (ent.flags & def_directory) {
472 // None of the directories which we're interested in have length 1.
473 // This also avoids the dummy "current" and "parent" directories.
474 if (ent.filename.size() > 1) {
475 build_file_lookup_table(ent.data_sector, ent.data_length, path);
476 }
477 } else {
478 file_metadata file{};
479 file.path = std::move(path);
480 file.sector = ent.data_sector;
481 file.size = ent.data_length;
482 files.push_back(file);
483 }
484 }
485 delete[] pBuffer;
486
487 if (prefix.empty()) {
488 // The lookup table will be ordered by the underlying ordering of the
489 // disk. we want it sorted by the path for ease of lookup.
490 std::sort(files.begin(), files.end(), file_metadata_less);
491 }
492 }
493
494 void iso_filesystem::visit_directory_files(
495 const char* sPath, void (*fnCallback)(void*, const char*, const char*),
496 void* pCallbackData) const {
497 std::string normalised_path = normalise(sPath);
498
499 // Inefficient (better would be to binary search for first and last files
500 // which begin with sPath), but who cares - this isn't called often
501 for (const file_metadata& file : files) {
502 if (normalised_path.size() < file.path.size() &&
503 std::equal(normalised_path.begin(), normalised_path.end(),
504 file.path.begin())) {
505 size_t filename_pos = normalised_path.size();
506 if (file.path.at(normalised_path.size()) == path_seperator) {
507 ++filename_pos;
508 }
509 std::string filename(file.path.substr(filename_pos));
510
511 if (filename.find(path_seperator) == filename.npos) {
512 fnCallback(pCallbackData, filename.c_str(), file.path.c_str());
513 }
514 }
515 }
516 }
517
518 iso_filesystem::file_handle iso_filesystem::find_file(const char* sPath) const {
519 std::string normalised_path = normalise(sPath);
520
521 // Standard binary search over sorted list of files
522 int iLower = 0;
523 int iUpper = static_cast<int>(files.size());
524 while (iLower != iUpper) {
525 int iMid = (iLower + iUpper) / 2;
526 int iComp = normalised_path.compare(files[iMid].path);
527 if (iComp == 0) {
528 return iMid + 1;
529 } else if (iComp < 0) {
530 iUpper = iMid;
531 } else {
532 iLower = iMid + 1;
533 }
534 }
535 return 0;
536 }
537
538 uint32_t iso_filesystem::get_file_start(file_handle iFile) const {
539 if (iFile <= 0 || static_cast<size_t>(iFile) > files.size())
540 return 0;
541 else
542 return files[iFile - 1].sector * sector_size;
543 }
544
545 uint32_t iso_filesystem::get_file_size(file_handle iFile) const {
546 if (iFile <= 0 || static_cast<size_t>(iFile) > files.size())
547 return 0;
548 else
549 return files[iFile - 1].size;
550 }
551
552 bool iso_filesystem::get_file_data(file_handle iFile, uint8_t* pBuffer) {
553 if (iFile <= 0 || static_cast<size_t>(iFile) > files.size()) {
554 set_error("Invalid file handle.");
555 return false;
556 } else {
557 return seek_to_sector(files[iFile - 1].sector) &&
558 read_data(files[iFile - 1].size, pBuffer);
559 }
560 }
561
562 std::string_view iso_filesystem::get_error() const { return error; }
563
564 bool iso_filesystem::seek_to_sector(uint32_t iSector) {
565 int res = std::fseek(raw_file.get(), sector_size * static_cast<long>(iSector),
566 SEEK_SET);
567 if (res == 0) {
568 return true;
569 } else {
570 set_error("Unable to seek to sector %i.", static_cast<int>(iSector));
571 return false;
572 }
573 }
574
575 bool iso_filesystem::read_data(uint32_t iByteCount, uint8_t* pBuffer) {
576 if (std::fread(pBuffer, 1, iByteCount, raw_file.get()) == iByteCount)
577 return true;
578 else {
579 set_error("Unable to read %i bytes.", static_cast<int>(iByteCount));
580 return false;
581 }
582 }
583
584 void iso_filesystem::set_error(const char* sFormat, ...) {
585 // Replace this implementation with std::format when adopting C++20
586
587 char err[1024];
588 va_list a;
589 va_start(a, sFormat);
590 // The valist test is buggy in this environment
591 // (https://bugs.llvm.org/show_bug.cgi?id=41311)
592 // NOLINTNEXTLINE(clang-analyzer-valist.Uninitialized)
593 std::vsnprintf(err, 1024, sFormat, a);
594 va_end(a);
595
596 error = err;
597 }