From cb18b7c82a17f5ab85ff7512de20553309264960 Mon Sep 17 00:00:00 2001 From: Frank Zago Date: Sun, 11 Oct 2009 16:05:40 +0000 Subject: [PATCH] Rewrote CSndHandler::CSndHandler and CVidHandler::CVidHandler to get rid of readNormalNr. --- hch/CSndHandler.cpp | 72 ++++++++++++++------------------------------- hch/CSndHandler.h | 15 +++++++++- 2 files changed, 36 insertions(+), 51 deletions(-) diff --git a/hch/CSndHandler.cpp b/hch/CSndHandler.cpp index 418010398..4db235250 100644 --- a/hch/CSndHandler.cpp +++ b/hch/CSndHandler.cpp @@ -2,6 +2,7 @@ #include #include "CSndHandler.h" #include +#include /* * CSndHandler.cpp, part of VCMI engine @@ -38,12 +39,6 @@ CMediaHandler::CMediaHandler(std::string fname) } } -// Reads a 4 byte integer. Format on file is little endian. -unsigned int CMediaHandler::readNormalNr (const unsigned char *p) -{ - return p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24); -} - void CMediaHandler::extract(int index, std::string dstfile) //saves selected file { std::ofstream out(dstfile.c_str(),std::ios_base::binary); @@ -128,37 +123,24 @@ const char * CMediaHandler::extract (std::string srcName, int &size) CSndHandler::CSndHandler(std::string fname) : CMediaHandler(fname) { const unsigned char *data = (const unsigned char *)mfile->data(); + unsigned int numFiles = SDL_SwapLE32(*(Uint32 *)&data[0]); + struct soundEntry *se = (struct soundEntry *)&data[4]; - unsigned int numFiles = readNormalNr(&data[0]); - - for (unsigned int i=0; ifilename; + entry.name += '.'; + p = se->filename; + while(*p) p++; p++; + entry.name += p; - while(*p) - { - entry.name += *p; - p++; - } - - // Read offset and size - p = &data[4+48*i+40]; - entry.offset = readNormalNr(p); - - p += 4; - entry.size = readNormalNr(p); + entry.offset = SDL_SwapLE32(se->offset); + entry.size = SDL_SwapLE32(se->size); entries.push_back(entry); fimap[entry.name] = i; @@ -168,34 +150,24 @@ CSndHandler::CSndHandler(std::string fname) : CMediaHandler(fname) CVidHandler::CVidHandler(std::string fname) : CMediaHandler(fname) { const unsigned char *data = (const unsigned char *)mfile->data(); + unsigned int numFiles = SDL_SwapLE32(*(Uint32 *)&data[0]); + struct videoEntry *ve = (struct videoEntry *)&data[4]; - unsigned int numFiles = readNormalNr(&data[0]); - - for (unsigned int i=0; ifilename; + entry.offset = SDL_SwapLE32(ve->offset); // There is no size, so check where the next file is if (i == numFiles - 1) { entry.size = mfile->size() - entry.offset; } else { - p = &data[4+44*(i+1)+40]; - int next_offset = readNormalNr(p); - entry.size = next_offset - entry.offset; - } + struct videoEntry *ve_next = ve+1; + + entry.size = SDL_SwapLE32(ve_next->offset) - entry.offset; + } entries.push_back(entry); fimap[entry.name] = i; diff --git a/hch/CSndHandler.h b/hch/CSndHandler.h index a5294c616..d4beb46bd 100644 --- a/hch/CSndHandler.h +++ b/hch/CSndHandler.h @@ -4,6 +4,7 @@ #include #include #include +#include /* @@ -30,6 +31,19 @@ struct MemberFile int length; }; +struct soundEntry +{ + char filename[40]; + Uint32 offset; /* lettle endian */ + Uint32 size; /* lettle endian */ +}; + +struct videoEntry +{ + char filename[40]; + Uint32 offset; /* lettle endian */ +}; + class CMediaHandler { protected: @@ -40,7 +54,6 @@ protected: unsigned int offset; }; - inline unsigned int readNormalNr (const unsigned char *p); boost::iostreams::mapped_file_source *mfile; public: