|
|
@@ -77,7 +77,9 @@ Archive::Archive(const char *filepath)
|
|
|
if (filepath == nullptr)
|
|
|
{
|
|
|
Logger::error("%s: Null path given", __FILE__);
|
|
|
- // throw PIAF::PIAFException("%s: Null path given", __FILE__);
|
|
|
+#ifdef WRPG_EXCEPTIONS
|
|
|
+ throw PIAF::PIAFException("%s: Null path given", __FILE__);
|
|
|
+#endif
|
|
|
}
|
|
|
// Solves the absolute path for given relative path.
|
|
|
// Must be needed in targets like Ndless as it doesn't support environment
|
|
|
@@ -90,7 +92,9 @@ Archive::Archive(const char *filepath)
|
|
|
if (file == nullptr || file == NULL)
|
|
|
{
|
|
|
Logger::error("%s: Missing file : %s", __FILE__, filepath);
|
|
|
- // throw PIAF::PIAFException("%s: Missing file : %s", __FILE__, filepath);
|
|
|
+#ifdef WRPG_EXCEPTIONS
|
|
|
+ throw PIAF::PIAFException("%s: Missing file : %s", __FILE__, filepath);
|
|
|
+#endif
|
|
|
}
|
|
|
|
|
|
// Loading stuff happens NOW
|
|
|
@@ -102,8 +106,10 @@ Archive::Archive(const char *filepath)
|
|
|
if (filesize < 32)
|
|
|
{
|
|
|
Logger::error("%s: File too small (%s): %d", __FILE__, filepath, filesize);
|
|
|
- // throw PIAF::PIAFException("%s: File too small (%s): %d", __FILE__, filepath,
|
|
|
- // filesize);
|
|
|
+#ifdef WRPG_EXCEPTIONS
|
|
|
+ throw PIAF::PIAFException("%s: File too small (%s): %d", __FILE__, filepath,
|
|
|
+ filesize);
|
|
|
+#endif
|
|
|
}
|
|
|
|
|
|
// Tempoary buffer to contain the header.
|
|
|
@@ -112,25 +118,29 @@ Archive::Archive(const char *filepath)
|
|
|
if (fread(header_container, sizeof(char), 32, file) != 32)
|
|
|
{
|
|
|
Logger::error("%s: Errorneous header : %s", __FILE__, filepath);
|
|
|
- // throw PIAF::PIAFException("%s: Errorneous header : %s", __FILE__, filepath);
|
|
|
- }
|
|
|
+#ifdef WRPG_EXCEPTIONS
|
|
|
+ throw PIAF::PIAFException("%s: Errorneous header : %s", __FILE__, filepath);
|
|
|
+#endif
|
|
|
+ }
|
|
|
// Check if the magic cookie is the same.
|
|
|
// It's a first way to detect if the file is correctly an archive.
|
|
|
if (strncmp(header_container, "WRPGPIAF", 8) != 0)
|
|
|
{
|
|
|
// TODO throw bad header
|
|
|
Logger::error("%s: Magic cookie mismatch : %s", __FILE__, filepath);
|
|
|
- // throw PIAF::PIAFException("%s: Magic cookie mismatch : %s", __FILE__,
|
|
|
- // filepath);
|
|
|
+#ifdef WRPG_EXCEPTIONS
|
|
|
+ throw PIAF::PIAFException("%s: Magic cookie mismatch : %s", __FILE__, filepath);
|
|
|
+#endif
|
|
|
}
|
|
|
// Checksum time! Let's check if the header hasn"t been altered.
|
|
|
uint32_t expected_checksum = read_big_endian_value<uint32_t>(&header_container[8]);
|
|
|
uint32_t calculated_checksum = crc32(0L, (unsigned char *) &header_container[16], 16);
|
|
|
if (expected_checksum != calculated_checksum)
|
|
|
{
|
|
|
- // TODO throw bad checksum
|
|
|
Logger::error("%s: Bad checksum : %s", __FILE__, filepath);
|
|
|
- // throw PIAF::PIAFException("%s: Bad checksum : %s", __FILE__, filepath);
|
|
|
+#ifdef WRPG_EXCEPTIONS
|
|
|
+ throw PIAF::PIAFException("%s: Bad checksum : %s", __FILE__, filepath);
|
|
|
+#endif
|
|
|
}
|
|
|
|
|
|
// TODO : version checking
|
|
|
@@ -141,8 +151,10 @@ Archive::Archive(const char *filepath)
|
|
|
// throw up; // haha
|
|
|
Logger::error("%s: Wrong(%s) : %08x is not supported by %08x", __FILE__, filepath,
|
|
|
version, ARCHIVE_VERSION);
|
|
|
- // throw PIAF::PIAFException("%s: Wrong(%s) : %08x is not supported by %08x",
|
|
|
- // __FILE__, filepath, version, ARCHIVE_VERSION);
|
|
|
+#ifdef WRPG_EXCEPTIONS
|
|
|
+ throw PIAF::PIAFException("%s: Wrong(%s) : %08x is not supported by %08x",
|
|
|
+ __FILE__, filepath, version, ARCHIVE_VERSION);
|
|
|
+#endif
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -157,10 +169,10 @@ Archive::Archive(const char *filepath)
|
|
|
uint64_t calculated_data_size = filesize - 32 - 24 * nb_files;
|
|
|
if (data_size != calculated_data_size)
|
|
|
{
|
|
|
- // fprintf(stderr, "Bad data size : expected %u, got %lld\n", data_size,
|
|
|
- // calculated_data_size);
|
|
|
Logger::error("Data size mismatch", __LINE__, filepath);
|
|
|
- // throw PIAF::PIAFException("Data size mismatch", __LINE__, filepath);
|
|
|
+#ifdef WRPG_EXCEPTIONS
|
|
|
+ throw PIAF::PIAFException("Data size mismatch", __LINE__, filepath);
|
|
|
+#endif
|
|
|
}
|
|
|
// Check if there are files to manage.
|
|
|
if (nb_files != 0)
|
|
|
@@ -174,16 +186,18 @@ Archive::Archive(const char *filepath)
|
|
|
if (fread(file_entry_data, sizeof(char), 24 * nb_files, file) < 24 * nb_files)
|
|
|
{
|
|
|
Logger::error("Can't read file entry data", __LINE__, filepath);
|
|
|
- // throw PIAF::PIAFException("Can't read file entry data", __LINE__,
|
|
|
- // filepath);
|
|
|
+#ifdef WRPG_EXCEPTIONS
|
|
|
+ throw PIAF::PIAFException("Can't read file entry data", __LINE__, filepath);
|
|
|
+#endif
|
|
|
}
|
|
|
// Compare and trigger an exception if the checksum doesn't match.
|
|
|
if (expected_filetable_checksum !=
|
|
|
crc32(0L, (unsigned char *) file_entry_data, 24 * nb_files))
|
|
|
{
|
|
|
- // fprintf(stderr, "Bad filetable checksum\n");
|
|
|
Logger::error("Bad Filetable checksum", __LINE__, filepath);
|
|
|
- // throw PIAF::PIAFException("Bad Filetable checksum", __LINE__, filepath);
|
|
|
+#ifdef WRPG_EXCEPTIONS
|
|
|
+ throw PIAF::PIAFException("Bad Filetable checksum", __LINE__, filepath);
|
|
|
+#endif
|
|
|
}
|
|
|
// Create the filetable.
|
|
|
entries = new File[nb_files];
|
|
|
@@ -258,8 +272,10 @@ File Archive::get(const char *filename)
|
|
|
if (fread(data, sizeof(uint8_t), entries[index].file_size, file) !=
|
|
|
entries[index].file_size)
|
|
|
{
|
|
|
- // throw PIAF::PIAFException("%s: couldn't load %s from an archive.",
|
|
|
- // filename);
|
|
|
+#ifdef WRPG_EXCEPTIONS
|
|
|
+ throw PIAF::PIAFException("%s: couldn't load %s from an archive.",
|
|
|
+ filename);
|
|
|
+#endif
|
|
|
}
|
|
|
else
|
|
|
{
|