Archive.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. #include <cstring>
  2. #include <cstdio>
  3. #include <memory>
  4. #include <zlib.h>
  5. #if NSPIRE
  6. #include "../../platform/nspire/Interrupts.h"
  7. #endif
  8. #include "Archive.h"
  9. #include "Quirks.h"
  10. #include "utility/misc.h"
  11. using tinystl::string;
  12. using WalrusRPG::PIAF::ARCHIVE_VERSION;
  13. using WalrusRPG::PIAF::Archive;
  14. using WalrusRPG::PIAF::File;
  15. using WalrusRPG::PIAF::FileType;
  16. using WalrusRPG::PIAF::CompressionType;
  17. using namespace WalrusRPG::PIAF;
  18. #if NSPIRE
  19. using namespace Nspire;
  20. #endif
  21. namespace
  22. {
  23. // Must get a pointer on the file table.
  24. /**
  25. * Reads the file table from given data pointer to an empty FileEntry
  26. * array long enough and a given number of files to load.
  27. * The pointer must directly access the file entry region of the archive.
  28. */
  29. void load_file_table(File *entries, uint32_t *data_offsets, char *data,
  30. uint32_t nb_files)
  31. {
  32. for (unsigned index = 0; index < nb_files; index++)
  33. {
  34. // Offsetting the data pointer to the current file entry.
  35. char *current_entry_data = &data[index * 24];
  36. // Copying the filename to the FileEntry
  37. memcpy(entries[index].filename, current_entry_data, 8);
  38. // Making sure there is a terminating \0 character in the filename.
  39. entries[index].filename[8] = '\0';
  40. // /!\ Parsing a value to an enumeration.
  41. // Parses the file type and store it.
  42. entries[index].file_type =
  43. (FileType) read_big_endian_value<uint32_t>(&current_entry_data[8]);
  44. // /!\ Parsing a value to an enumeration.
  45. // Parses the compression type and store it.
  46. entries[index].compression_type =
  47. (CompressionType) read_big_endian_value<uint32_t>(
  48. &current_entry_data[12]);
  49. // Parsign the file size and storing it.
  50. entries[index].file_size =
  51. read_big_endian_value<uint32_t>(&current_entry_data[16]);
  52. // Reading the file's data position in the archive data section.
  53. data_offsets[index] =
  54. read_big_endian_value<uint32_t>(&current_entry_data[20]);
  55. }
  56. }
  57. }
  58. Archive::Archive(string &filepath) : Archive(filepath.c_str())
  59. {
  60. }
  61. Archive::Archive(const char *filepath)
  62. : file(nullptr), entries(nullptr), files_data(nullptr), files_loaded(nullptr)
  63. {
  64. #if NSPIRE
  65. Interrupts::off();
  66. #endif
  67. // Null pointer exception trigger
  68. if (filepath == nullptr)
  69. {
  70. throw PIAF::PIAFException("%s: Null path given", __FILE__);
  71. }
  72. // Solves the absolute path for given relative path.
  73. // Must be needed in targets like Ndless as it doesn't support environment
  74. // vars and thus PATH.
  75. std::unique_ptr<char> real_filename(Quirks::solve_absolute_path(filepath));
  76. // Open the archive
  77. file = fopen(real_filename.get(), "rb");
  78. // Again another null pointer trigger
  79. if (file == nullptr || file == NULL)
  80. {
  81. throw PIAF::PIAFException("%s: Missing file : %s", __FILE__, filepath);
  82. }
  83. // Loading stuff happens NOW
  84. // Checking if the file is long enough to have a header
  85. fseek(file, 0L, SEEK_END);
  86. uint64_t filesize = ftell(file);
  87. fseek(file, 0L, SEEK_SET);
  88. // File to small exception trigger
  89. if (filesize < 32)
  90. {
  91. throw PIAF::PIAFException("%s: File too small (%s): %d", __FILE__, filepath, filesize);
  92. }
  93. // Tempoary buffer to contain the header.
  94. char header_container[32] = {0};
  95. // Read the headers and trigger exceptions on errors
  96. if (fread(header_container, sizeof(char), 32, file) != 32)
  97. {
  98. throw PIAF::PIAFException("%s: Errorneous header : %s", __FILE__, filepath);
  99. }
  100. // Check if the magic cookie is the same.
  101. // It's a first way to detect if the file is correctly an archive.
  102. if (strncmp(header_container, "WRPGPIAF", 8) != 0)
  103. {
  104. // TODO throw bad header
  105. // fprintf(stderr, "Bad header magic word\n");
  106. throw PIAF::PIAFException("%s: Magic cookie mismatch : %s", __FILE__, filepath);
  107. }
  108. // Checksum time! Let's check if the header hasn"t been altered.
  109. uint32_t expected_checksum = read_big_endian_value<uint32_t>(&header_container[8]);
  110. uint32_t calculated_checksum = crc32(0L, (unsigned char *) &header_container[16], 16);
  111. if (expected_checksum != calculated_checksum)
  112. {
  113. // TODO throw bad checksum
  114. // fprintf(stderr, "Bad header checksum : %x != %x\n", expected_checksum,
  115. // calculated_checksum);
  116. throw PIAF::PIAFException("%s: Bad checksum : %s", __FILE__, filepath);
  117. }
  118. // TODO : version checking
  119. version = read_big_endian_value<uint32_t>(&header_container[16]);
  120. if (version != ARCHIVE_VERSION)
  121. {
  122. // std::exception up;
  123. // throw up; // haha
  124. throw PIAF::PIAFException("%s: Wrong(%s) : %08x is not supported by %08x", __FILE__, filepath, version, ARCHIVE_VERSION);
  125. }
  126. // At this point, the archive header looks unaltered and we finally can parse
  127. // and load the header.
  128. // Read the archive's number of files
  129. nb_files = read_big_endian_value<uint32_t>(&header_container[20]);
  130. // printf("nb_files : %u\n", nb_files);
  131. data_size = read_big_endian_value<uint32_t>(&header_container[24]);
  132. uint64_t calculated_data_size = filesize - 32 - 24 * nb_files;
  133. if (data_size != calculated_data_size)
  134. {
  135. // T0D0 : throw wrong size exception
  136. // fprintf(stderr, "Bad data size : expected %u, got %lld\n", data_size,
  137. // calculated_data_size);
  138. throw PIAF::PIAFException("Data size mismatch", __LINE__, filepath);
  139. }
  140. // Check if there are files to manage.
  141. if (nb_files != 0)
  142. {
  143. // So, the file table is not empty. let's check if it's
  144. // not altered.
  145. uint32_t expected_filetable_checksum =
  146. read_big_endian_value<uint32_t>(&header_container[12]);
  147. char *file_entry_data = new char[24 * nb_files];
  148. fseek(file, 32, SEEK_SET);
  149. fread(file_entry_data, sizeof(char), 24 * nb_files, file);
  150. // Compare and trigger an exception if the checksum doesn't match.
  151. if (expected_filetable_checksum !=
  152. crc32(0L, (unsigned char *) file_entry_data, 24 * nb_files))
  153. {
  154. // TODO : checksum exception
  155. // fprintf(stderr, "Bad filetable checksum\n");
  156. throw PIAF::PIAFException("Bad Filetable checksum", __LINE__, filepath);
  157. }
  158. // Create the filetable.
  159. entries = new File[nb_files];
  160. // Parse and story the filetable.
  161. files_data = new uint8_t *[nb_files];
  162. files_loaded = new bool[nb_files];
  163. files_data_offset = new uint32_t[nb_files];
  164. for (unsigned i = 0; i < nb_files; i++)
  165. {
  166. files_data[i] = nullptr;
  167. files_loaded[i] = false;
  168. files_data_offset[i] = 0;
  169. }
  170. load_file_table(entries, files_data_offset, file_entry_data, nb_files);
  171. delete[] file_entry_data;
  172. }
  173. #if NSPIRE
  174. Interrupts::init();
  175. #endif
  176. }
  177. Archive::~Archive()
  178. {
  179. if (file != nullptr)
  180. fclose(file);
  181. if (entries != nullptr)
  182. delete[] entries;
  183. if (files_data != nullptr)
  184. {
  185. for (unsigned i = 0; i < nb_files; i++)
  186. {
  187. if (files_data[i] != nullptr)
  188. {
  189. delete[] files_data[i];
  190. }
  191. }
  192. }
  193. delete[] files_data;
  194. delete[] files_loaded;
  195. delete[] files_data_offset;
  196. }
  197. bool Archive::has(const char *filename)
  198. {
  199. for (unsigned index = 0; index < nb_files; index++)
  200. if (strncmp(filename, entries[index].filename, 8) == 0)
  201. return true;
  202. return false;
  203. }
  204. /**
  205. * Returns the stored file's data from giving a filename.
  206. */
  207. File Archive::get(const char *filename)
  208. {
  209. for (unsigned index = 0; index < nb_files; index++)
  210. {
  211. if (strncmp(filename, entries[index].filename, 8) == 0)
  212. {
  213. // On demand load
  214. if (!files_loaded[index])
  215. {
  216. #if NSPIRE
  217. Interrupts::off();
  218. #endif
  219. uint8_t *data = new uint8_t[entries[index].file_size];
  220. fseek(file, files_data_offset[index] + 32 + 24 * nb_files, SEEK_SET);
  221. if (fread(data, sizeof(uint8_t), entries[index].file_size, file) !=
  222. entries[index].file_size)
  223. {
  224. throw PIAF::PIAFException("%s: couldn't load %s from an archive.", filename);
  225. }
  226. else
  227. {
  228. files_data[index] = data;
  229. entries[index].data = data;
  230. }
  231. files_loaded[index] = true;
  232. #if NSPIRE
  233. Interrupts::init();
  234. #endif
  235. }
  236. return entries[index];
  237. }
  238. }
  239. return File();
  240. }
  241. File::File(uint8_t *data) : data(data)
  242. {
  243. }
  244. File::File() : data(nullptr)
  245. {
  246. }
  247. File::~File()
  248. {
  249. }
  250. const uint8_t *File::get()
  251. {
  252. return data;
  253. }