Archive.cpp 8.4 KB

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