| 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 |
#ifndef CORSIX_TH_ISO_FS_H_ |
| 24 |
#define CORSIX_TH_ISO_FS_H_ |
| 25 |
|
| 26 |
#include "config.h" |
| 27 |
|
| 28 |
#include <cstdio> |
| 29 |
#include <memory> |
| 30 |
#include <string> |
| 31 |
#include <string_view> |
| 32 |
#include <vector> |
| 33 |
|
| 34 |
//! Layer for reading Theme Hospital files out of an .iso disk image |
| 35 |
/*! |
| 36 |
An instance of this class can read files which contain an ISO 9660 file |
| 37 |
system (as described at http://alumnus.caltech.edu/~pje/iso9660.html and |
| 38 |
http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-119.pdf) |
| 39 |
which are typically called iso disk images. Once given a disk image, it |
| 40 |
searches for the Theme Hospital data files, and can then be used to read |
| 41 |
these data files. |
| 42 |
*/ |
| 43 |
class iso_filesystem { |
| 44 |
public: |
| 45 |
/// Sector sizes can vary, but they must be powers of two, and the minimum |
| 46 |
/// size is 2048. |
| 47 |
static constexpr size_t min_sector_size = 2048; |
| 48 |
|
| 49 |
//! Load an .iso disk image and search for Theme Hospital data files |
| 50 |
/*! |
| 51 |
\param path Path to the .iso disk image to load |
| 52 |
\param pathSeparator The character to be used between components in file |
| 53 |
paths. Defaults to '/'. |
| 54 |
\throws std::runtime_error if the file could not be opened |
| 55 |
*/ |
| 56 |
explicit iso_filesystem(const char* path, char pathSeparator = '/'); |
| 57 |
~iso_filesystem() = default; |
| 58 |
|
| 59 |
//! Get the reason for the most recent failure |
| 60 |
/*! |
| 61 |
Can be called after get_file_data() return false. |
| 62 |
*/ |
| 63 |
std::string_view get_error() const; |
| 64 |
|
| 65 |
using file_handle = int; |
| 66 |
|
| 67 |
//! Find a file in the loaded .iso disk image |
| 68 |
/*! |
| 69 |
If (and only if) the given file could not be found, then isHandleGood() |
| 70 |
will return false on the returned handle. |
| 71 |
*/ |
| 72 |
file_handle find_file(const char* sPath) const; |
| 73 |
|
| 74 |
//! Iterate all files of the .iso disk image within a given directory |
| 75 |
/*! |
| 76 |
\param sPath The directory to iterate |
| 77 |
\param fnCallback The function to be called for each file. The first |
| 78 |
parameter to this function is pCallbackData. The second is the name |
| 79 |
of a file which is in sPath. |
| 80 |
\param pCallbackData Opaque value to be called to fnCallback. |
| 81 |
*/ |
| 82 |
void visit_directory_files(const char* sPath, |
| 83 |
void (*fnCallback)(void*, const char*, |
| 84 |
const char*), |
| 85 |
void* pCallbackData) const; |
| 86 |
|
| 87 |
//! Test if a file handle from find_file() is good or is invalid |
| 88 |
static inline bool is_handle_good(file_handle x) { return x != 0; } |
| 89 |
|
| 90 |
//! Get the byte offset of the start of the file in the loaded .iso |
| 91 |
/*! |
| 92 |
\param iFile A file handle returned by find_file() |
| 93 |
*/ |
| 94 |
uint32_t get_file_start(file_handle iFile) const; |
| 95 |
|
| 96 |
//! Get the size (in bytes) of a file in the loaded .iso disk image |
| 97 |
/*! |
| 98 |
\param iFile A file handle returned by find_file() |
| 99 |
*/ |
| 100 |
uint32_t get_file_size(file_handle iFile) const; |
| 101 |
|
| 102 |
//! Get the contents of a file in the loaded .iso disk image |
| 103 |
/*! |
| 104 |
\param iFile A file handle returned by find_file() |
| 105 |
\param pBuffer The buffer to place the resulting data in |
| 106 |
\return true on success, false on failure - call getError() for reason |
| 107 |
*/ |
| 108 |
bool get_file_data(file_handle iFile, uint8_t* pBuffer); |
| 109 |
|
| 110 |
private: |
| 111 |
struct file_metadata { |
| 112 |
std::string path; |
| 113 |
uint32_t sector; |
| 114 |
uint32_t size; |
| 115 |
}; |
| 116 |
|
| 117 |
std::unique_ptr<std::FILE, int (*)(std::FILE*)> raw_file; |
| 118 |
std::string error{}; |
| 119 |
std::vector<file_metadata> files; |
| 120 |
long sector_size{min_sector_size}; |
| 121 |
char path_seperator; |
| 122 |
|
| 123 |
//! Set the last error, printf-style |
| 124 |
void set_error(const char* sFormat, ...); |
| 125 |
|
| 126 |
//! Seek to a logical sector of the disk image |
| 127 |
bool seek_to_sector(uint32_t iSector); |
| 128 |
|
| 129 |
//! Read data from the disk image |
| 130 |
bool read_data(uint32_t iByteCount, uint8_t* pBuffer); |
| 131 |
|
| 132 |
//! Scan the given array of directory entries for a Theme Hospital file |
| 133 |
/*! |
| 134 |
\param pDirEnt Pointer to a padded array of ISO 9660 directory entries. |
| 135 |
\param dirEntsSize The number of bytes in the directory entry array. |
| 136 |
\param level The recursion level (used to prevent infinite loops upon |
| 137 |
maliciously-formed .iso disk images). |
| 138 |
\return 0 if no Theme Hospital files were found. 1 if the given array |
| 139 |
contains a Theme Hospital data file. 2 if the given array is the |
| 140 |
top-level Theme Hospital data directory. Other values otherwise. |
| 141 |
*/ |
| 142 |
int find_hosp_directory(const uint8_t* pDirEnt, const uint32_t dirEntsSize, |
| 143 |
int level); |
| 144 |
|
| 145 |
//! Build the list of Theme Hospital data files |
| 146 |
/*! |
| 147 |
\param iSector The ordinal of a logical sector containing a padded |
| 148 |
array of ISO 9660 directory entries. |
| 149 |
\param dirEntsSize The number of bytes in the directory entry array. |
| 150 |
\param prefix The path name to prepend to filenames in the directory. |
| 151 |
*/ |
| 152 |
void build_file_lookup_table(uint32_t iSector, uint32_t dirEntsSize, |
| 153 |
std::string_view prefix); |
| 154 |
|
| 155 |
//! std:less like implementation for file_metadata. Based on the path. |
| 156 |
static bool file_metadata_less(const file_metadata& lhs, |
| 157 |
const file_metadata& rhs); |
| 158 |
}; |
| 159 |
|
| 160 |
#endif |