Skip to content

Commit

Permalink
Always dump the bytes when an unknown chunk is skipped. An unknown ch…
Browse files Browse the repository at this point in the history
…unk is always a bug.
  • Loading branch information
Ghabry committed Mar 15, 2021
1 parent cd7710d commit f09fe70
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 44 deletions.
22 changes: 2 additions & 20 deletions src/lcf/reader_lcf.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,6 @@
#include "lcf/reader_util.h"
#include "lcf/encoder.h"

/*
* Calls SkipDebug() instead of Skip() for debug builds.
*/
#ifdef _DEBUG
#define Skip(x) SkipDebug(x, __FILE__)
#endif

namespace lcf {

/**
Expand Down Expand Up @@ -189,27 +182,16 @@ class LcfReader {
*/
int Peek();

#ifdef _DEBUG
/**
* The skip-function for debug builds.
* Same as Skip() but also dumps the content of the
* skipped chunk to stderr.
*
* @param chunk_info chunk that will be skipped.
* @param srclife name of the calling cpp-file.
*/
void SkipDebug(const struct LcfReader::Chunk& chunk_info, const char* srcfile);
#else
/**
* Skips a chunk (seeks chunk_info.length bytes from
* the current stream position).
* In debug builds, dumps the content of the
* skipped chunk to stderr.
*
* @param chunk_info chunk that will be skipped.
* @param where name of the caller that caused the skip, for finding unknown chunks
*/
void Skip(const struct LcfReader::Chunk& chunk_info);
#endif
void Skip(const struct LcfReader::Chunk& chunk_info, const char* where);

/**
* Encodes a string to UTF-8 using the set encoding
Expand Down
2 changes: 1 addition & 1 deletion src/ldb_equipment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ void RawStruct<rpg::Equipment>::ReadLcf(rpg::Equipment& ref, LcfReader& stream,
chunk_info.ID = 0x33;
chunk_info.length = length;

stream.Skip(chunk_info);
stream.Skip(chunk_info, "Equipment");

return;
}
Expand Down
23 changes: 3 additions & 20 deletions src/reader_lcf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,22 +258,10 @@ int LcfReader::Peek() {
return stream.peek();
}

#ifdef _DEBUG
void LcfReader::SkipDebug(const struct LcfReader::Chunk& chunk_info, const char* srcfile) {
// Dump the Chunk Data in Debug Mode
#ifdef _WIN32
const char* srcfilename = strrchr(srcfile, '\\');
#else
const char* srcfilename = strrchr(srcfile, '/');
#endif
if (srcfilename == NULL) {
srcfilename = srcfile;
} else {
srcfilename++;
}
void LcfReader::Skip(const struct LcfReader::Chunk& chunk_info, const char* where) {
fprintf(stderr, "Skipped Chunk %02X (%" PRIu32 " byte) in lcf at %" PRIX32 " (%s)\n",
chunk_info.ID, chunk_info.length, Tell(),
srcfilename);
chunk_info.ID, chunk_info.length, Tell(), where);

for (uint32_t i = 0; i < chunk_info.length; ++i) {
uint8_t byte;
LcfReader::Read(byte);
Expand All @@ -287,11 +275,6 @@ void LcfReader::SkipDebug(const struct LcfReader::Chunk& chunk_info, const char*
}
fprintf(stderr, "\n");
}
#else
void LcfReader::Skip(const struct LcfReader::Chunk& chunk_info) {
Seek((size_t)chunk_info.length, FromCurrent);
}
#endif

void LcfReader::SetError(const char* fmt, ...) {
va_list args;
Expand Down
6 changes: 3 additions & 3 deletions src/reader_struct_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,13 @@ void Struct<S>::ReadLcf(S& obj, LcfReader& stream) {
it->second->ReadLcf(obj, stream, chunk_info.length);
const uint32_t bytes_read = stream.Tell() - off;
if (bytes_read != chunk_info.length) {
fprintf(stderr, "Warning: Corrupted Chunk 0x%02" PRIx32 " (size: %" PRIu32 ", pos: 0x%" PRIx32 "): %s : Read %" PRIu32 " bytes! Reseting...\n",
chunk_info.ID, chunk_info.length, off, it->second->name, bytes_read);
fprintf(stderr, "%s: Corrupted Chunk 0x%02" PRIx32 " (size: %" PRIu32 ", pos: 0x%" PRIx32 "): %s : Read %" PRIu32 " bytes! Reseting...\n",
Struct<S>::name, chunk_info.ID, chunk_info.length, off, it->second->name, bytes_read);
stream.Seek(off + chunk_info.length);
}
}
else {
stream.Skip(chunk_info);
stream.Skip(chunk_info, Struct<S>::name);
}
}
}
Expand Down

0 comments on commit f09fe70

Please sign in to comment.