Archive.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. try
  65. {
  66. #if NSPIRE
  67. Interrupts::off();
  68. #endif
  69. // Null pointer exception trigger
  70. if (filepath == nullptr)
  71. {
  72. throw PIAF::PIAFException("%s: Null path given", __FILE__);
  73. }
  74. // Solves the absolute path for given relative path.
  75. // Must be needed in targets like Ndless as it doesn't support environment
  76. // vars and thus PATH.
  77. std::unique_ptr<char> real_filename(Quirks::solve_absolute_path(filepath));
  78. // Open the archive
  79. file = fopen(real_filename.get(), "rb");
  80. // Again another null pointer trigger
  81. if (file == nullptr || file == NULL)
  82. {
  83. throw PIAF::PIAFException("%s: Missing file : %s", __FILE__, filepath);
  84. }
  85. // Loading stuff happens NOW
  86. // Checking if the file is long enough to have a header
  87. fseek(file, 0L, SEEK_END);
  88. uint64_t filesize = ftell(file);
  89. fseek(file, 0L, SEEK_SET);
  90. // File to small exception trigger
  91. if (filesize < 32)
  92. {
  93. throw PIAF::PIAFException("%s: File too small (%s): %d", __FILE__, filepath, filesize);
  94. }
  95. // Tempoary buffer to contain the header.
  96. char header_container[32] = {0};
  97. // Read the headers and trigger exceptions on errors
  98. if (fread(header_container, sizeof(char), 32, file) != 32)
  99. {
  100. throw PIAF::PIAFException("%s: Errorneous header : %s", __FILE__, filepath);
  101. }
  102. // Check if the magic cookie is the same.
  103. // It's a first way to detect if the file is correctly an archive.
  104. if (strncmp(header_container, "WRPGPIAF", 8) != 0)
  105. {
  106. // TODO throw bad header
  107. // fprintf(stderr, "Bad header magic word\n");
  108. throw PIAF::PIAFException("%s: Magic cookie mismatch : %s", __FILE__, filepath);
  109. }
  110. // Checksum time! Let's check if the header hasn"t been altered.
  111. uint32_t expected_checksum = read_big_endian_value<uint32_t>(&header_container[8]);
  112. uint32_t calculated_checksum = crc32(0L, (unsigned char *) &header_container[16], 16);
  113. if (expected_checksum != calculated_checksum)
  114. {
  115. // TODO throw bad checksum
  116. // fprintf(stderr, "Bad header checksum : %x != %x\n", expected_checksum,
  117. // calculated_checksum);
  118. throw PIAF::PIAFException("%s: Bad checksum : %s", __FILE__, filepath);
  119. }
  120. // TODO : version checking
  121. version = read_big_endian_value<uint32_t>(&header_container[16]);
  122. if (version != ARCHIVE_VERSION)
  123. {
  124. // std::exception up;
  125. // throw up; // haha
  126. throw PIAF::PIAFException("%s: Wrong(%s) : %08x is not supported by %08x", __FILE__, filepath, version, ARCHIVE_VERSION);
  127. }
  128. // At this point, the archive header looks unaltered and we finally can parse
  129. // and load the header.
  130. // Read the archive's number of files
  131. nb_files = read_big_endian_value<uint32_t>(&header_container[20]);
  132. // printf("nb_files : %u\n", nb_files);
  133. data_size = read_big_endian_value<uint32_t>(&header_container[24]);
  134. uint64_t calculated_data_size = filesize - 32 - 24 * nb_files;
  135. if (data_size != calculated_data_size)
  136. {
  137. // T0D0 : throw wrong size exception
  138. // fprintf(stderr, "Bad data size : expected %u, got %lld\n", data_size,
  139. // calculated_data_size);
  140. throw PIAF::PIAFException("Data size mismatch", __LINE__, filepath);
  141. }
  142. // Check if there are files to manage.
  143. if (nb_files != 0)
  144. {
  145. // So, the file table is not empty. let's check if it's
  146. // not altered.
  147. uint32_t expected_filetable_checksum =
  148. read_big_endian_value<uint32_t>(&header_container[12]);
  149. char *file_entry_data = new char[24 * nb_files];
  150. fseek(file, 32, SEEK_SET);
  151. fread(file_entry_data, sizeof(char), 24 * nb_files, file);
  152. // Compare and trigger an exception if the checksum doesn't match.
  153. if (expected_filetable_checksum !=
  154. crc32(0L, (unsigned char *) file_entry_data, 24 * nb_files))
  155. {
  156. // TODO : checksum exception
  157. // fprintf(stderr, "Bad filetable checksum\n");
  158. throw PIAF::PIAFException("Bad Filetable checksum", __LINE__, filepath);
  159. }
  160. // Create the filetable.
  161. entries = new File[nb_files];
  162. // Parse and story the filetable.
  163. files_data = new uint8_t *[nb_files];
  164. files_loaded = new bool[nb_files];
  165. files_data_offset = new uint32_t[nb_files];
  166. for (unsigned i = 0; i < nb_files; i++)
  167. {
  168. files_data[i] = nullptr;
  169. files_loaded[i] = false;
  170. files_data_offset[i] = 0;
  171. }
  172. load_file_table(entries, files_data_offset, file_entry_data, nb_files);
  173. delete[] file_entry_data;
  174. }
  175. #if NSPIRE
  176. Interrupts::init();
  177. #endif
  178. } catch(PIAF::PIAFException &e)
  179. {
  180. throw e;
  181. }
  182. }
  183. Archive::~Archive()
  184. {
  185. if (file != nullptr)
  186. fclose(file);
  187. if (entries != nullptr)
  188. delete[] entries;
  189. if (files_data != nullptr)
  190. {
  191. for (unsigned i = 0; i < nb_files; i++)
  192. {
  193. if (files_data[i] != nullptr)
  194. {
  195. delete[] files_data[i];
  196. }
  197. }
  198. }
  199. delete[] files_data;
  200. delete[] files_loaded;
  201. delete[] files_data_offset;
  202. }
  203. bool Archive::has(const char *filename)
  204. {
  205. for (unsigned index = 0; index < nb_files; index++)
  206. if (strncmp(filename, entries[index].filename, 8) == 0)
  207. return true;
  208. return false;
  209. }
  210. /**
  211. * Returns the stored file's data from giving a filename.
  212. */
  213. File Archive::get(const char *filename)
  214. {
  215. for (unsigned index = 0; index < nb_files; index++)
  216. {
  217. if (strncmp(filename, entries[index].filename, 8) == 0)
  218. {
  219. // On demand load
  220. if (!files_loaded[index])
  221. {
  222. #if NSPIRE
  223. Interrupts::off();
  224. #endif
  225. uint8_t *data = new uint8_t[entries[index].file_size];
  226. fseek(file, files_data_offset[index] + 32 + 24 * nb_files, SEEK_SET);
  227. if (fread(data, sizeof(uint8_t), entries[index].file_size, file) !=
  228. entries[index].file_size)
  229. {
  230. // throw loading error
  231. }
  232. else
  233. {
  234. files_data[index] = data;
  235. entries[index].data = data;
  236. }
  237. files_loaded[index] = true;
  238. #if NSPIRE
  239. Interrupts::init();
  240. #endif
  241. }
  242. return entries[index];
  243. }
  244. }
  245. return File();
  246. // throw not found exception
  247. }
  248. File::File(uint8_t *data) : data(data)
  249. {
  250. }
  251. File::File() : data(nullptr)
  252. {
  253. }
  254. File::~File()
  255. {
  256. }
  257. const uint8_t *File::get()
  258. {
  259. return data;
  260. }