|
|
@@ -76,7 +76,9 @@ Archive::Archive(const char *filepath)
|
|
|
// Null pointer exception trigger
|
|
|
if (filepath == nullptr)
|
|
|
{
|
|
|
+#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
|
|
|
@@ -88,7 +90,9 @@ Archive::Archive(const char *filepath)
|
|
|
// Again another null pointer trigger
|
|
|
if (file == nullptr || file == NULL)
|
|
|
{
|
|
|
+#ifdef WRPG_EXCEPTIONS
|
|
|
throw PIAF::PIAFException("%s: Missing file : %s", __FILE__, filepath);
|
|
|
+#endif
|
|
|
}
|
|
|
|
|
|
// Loading stuff happens NOW
|
|
|
@@ -99,8 +103,10 @@ Archive::Archive(const char *filepath)
|
|
|
// File to small exception trigger
|
|
|
if (filesize < 32)
|
|
|
{
|
|
|
+#ifdef WRPG_EXCEPTIONS
|
|
|
throw PIAF::PIAFException("%s: File too small (%s): %d", __FILE__, filepath,
|
|
|
filesize);
|
|
|
+#endif
|
|
|
}
|
|
|
|
|
|
// Tempoary buffer to contain the header.
|
|
|
@@ -108,35 +114,43 @@ Archive::Archive(const char *filepath)
|
|
|
// Read the headers and trigger exceptions on errors
|
|
|
if (fread(header_container, sizeof(char), 32, file) != 32)
|
|
|
{
|
|
|
+#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
|
|
|
- // fprintf(stderr, "Bad header magic word\n");
|
|
|
+// TODO throw bad header
|
|
|
+// fprintf(stderr, "Bad header magic word\n");
|
|
|
+#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
|
|
|
- // fprintf(stderr, "Bad header checksum : %x != %x\n", expected_checksum,
|
|
|
- // calculated_checksum);
|
|
|
+// TODO throw bad checksum
|
|
|
+// fprintf(stderr, "Bad header checksum : %x != %x\n", expected_checksum,
|
|
|
+// calculated_checksum);
|
|
|
+#ifdef WRPG_EXCEPTIONS
|
|
|
throw PIAF::PIAFException("%s: Bad checksum : %s", __FILE__, filepath);
|
|
|
+#endif
|
|
|
}
|
|
|
|
|
|
// TODO : version checking
|
|
|
version = read_big_endian_value<uint32_t>(&header_container[16]);
|
|
|
if (version != ARCHIVE_VERSION)
|
|
|
{
|
|
|
- // std::exception up;
|
|
|
- // throw up; // haha
|
|
|
+// std::exception up;
|
|
|
+// throw up; // haha
|
|
|
+#ifdef WRPG_EXCEPTIONS
|
|
|
throw PIAF::PIAFException("%s: Wrong(%s) : %08x is not supported by %08x",
|
|
|
__FILE__, filepath, version, ARCHIVE_VERSION);
|
|
|
+#endif
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -151,9 +165,11 @@ 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);
|
|
|
+// fprintf(stderr, "Bad data size : expected %u, got %lld\n", data_size,
|
|
|
+// calculated_data_size);
|
|
|
+#ifdef WRPG_EXCEPTIONS
|
|
|
throw PIAF::PIAFException("Data size mismatch", __LINE__, filepath);
|
|
|
+#endif
|
|
|
}
|
|
|
// Check if there are files to manage.
|
|
|
if (nb_files != 0)
|
|
|
@@ -166,14 +182,18 @@ Archive::Archive(const char *filepath)
|
|
|
fseek(file, 32, SEEK_SET);
|
|
|
if (fread(file_entry_data, sizeof(char), 24 * nb_files, file) < 24 * nb_files)
|
|
|
{
|
|
|
+#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");
|
|
|
+// fprintf(stderr, "Bad filetable checksum\n");
|
|
|
+#ifdef WRPG_EXCEPTIONS
|
|
|
throw PIAF::PIAFException("Bad Filetable checksum", __LINE__, filepath);
|
|
|
+#endif
|
|
|
}
|
|
|
// Create the filetable.
|
|
|
entries = new File[nb_files];
|
|
|
@@ -248,8 +268,10 @@ File Archive::get(const char *filename)
|
|
|
if (fread(data, sizeof(uint8_t), entries[index].file_size, file) !=
|
|
|
entries[index].file_size)
|
|
|
{
|
|
|
+#ifdef WRPG_EXCEPTIONS
|
|
|
throw PIAF::PIAFException("%s: couldn't load %s from an archive.",
|
|
|
filename);
|
|
|
+#endif
|
|
|
}
|
|
|
else
|
|
|
{
|