Archive.cpp 8.5 KB

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