#include "StdAfx.h" #include "T2Archive.h" T2Archive::T2Archive() { mFile = NULL; m8 = 0; mIsReadable = false; mIsWritable = false; mBuffer = NULL; mBufferLen = 0; mBuffer = (char *) malloc(0x10000); mPtr = mBuffer; } /*virtual*/ T2Archive::~T2Archive() { if (mFile) Close(); if (mBuffer) free(mBuffer); } /*virtual*/ BOOL T2Archive::OpenAsReadMode(const char* path) { if (mFile) Close(); mFile = new CFile; CFileException exc; BOOL success = mFile->Open(path, CFile::shareDenyWrite, &exc); if (!success) { delete mFile; mFile = NULL; } else { mIsReadable = true; mIsWritable = false; mPtr = mBuffer; } return success; } /*virtual*/ BOOL T2Archive::OpenAsWriteMode(const char* path) { if (mFile) Close(); mFile = new CFile; BOOL success = mFile->Open(path, CFile::modeCreate | CFile::modeWrite | CFile::shareDenyRead); if (!success) { delete mFile; mFile = NULL; } else { mIsReadable = false; mIsWritable = true; } return success; } /*virtual*/ BOOL T2Archive::Close() { #line 93 _ASSERT(mFile); if (mBuffer && mBufferLen > 0 && mIsWritable) { mFile->Write(mBuffer, mBufferLen); mBufferLen = 0; } mFile->Close(); delete mFile; mFile = NULL; mIsReadable = false; mIsWritable = false; return true; } /*virtual*/ int T2Archive::Read(void* buffer, int size) { #line 111 _ASSERT(mIsReadable); _ASSERT(size <= 0x10000); int readAmount; if (mBuffer) { if (mBufferLen < size) { memmove(mBuffer, mPtr, mBufferLen); mPtr = mBuffer; mBufferLen += mFile->Read(mBuffer + mBufferLen, 0x10000 - mBufferLen); #line 122 _ASSERT(mBufferLen >= size); } memcpy(buffer, mPtr, size); mPtr += size; mBufferLen -= size; readAmount = size; } else { readAmount = mFile->Read(buffer, size); #line 132 _ASSERT(readAmount == size); } return readAmount; } /*virtual*/ int T2Archive::Write(const void* buffer, int size) { #line 139 _ASSERT(mIsWritable); _ASSERT(size <= 0x10000); if (mBuffer) { if (mBufferLen + size > 0x10000) { mFile->Write(mBuffer, mBufferLen); mBufferLen = 0; } memcpy(mBuffer + mBufferLen, buffer, size); mBufferLen += size; } else { mFile->Write(buffer, size); } return size; } /*virtual*/ void T2Archive::BeginCompressedStream() { } /*virtual*/ void T2Archive::EndCompressedStream() { } BOOL T2Archive::operator>>(unsigned char& v) { if (Read(&v, 1) != 1) return false; return true; } BOOL T2Archive::operator<<(unsigned char v) { if (Write(&v, 1) != 1) return false; return true; } BOOL T2Archive::operator>>(char& v) { if (Read(&v, 1) != 1) return false; return true; } BOOL T2Archive::operator<<(char v) { if (Write(&v, 1) != 1) return false; return true; } BOOL T2Archive::operator>>(int& v) { if (Read(&v, 4) != 4) return false; return true; } BOOL T2Archive::operator<<(int v) { if (Write(&v, 4) != 4) return false; return true; } BOOL T2Archive::operator>>(short& v) { if (Read(&v, 2) != 2) return false; return true; } BOOL T2Archive::operator<<(short v) { if (Write(&v, 2) != 2) return false; return true; } BOOL T2Archive::operator>>(unsigned int& v) { if (Read(&v, 4) != 4) return false; return true; } BOOL T2Archive::operator<<(unsigned int v) { if (Write(&v, 4) != 4) return false; return true; } BOOL T2Archive::operator>>(unsigned long& v) { if (Read(&v, 4) != 4) return false; return true; } BOOL T2Archive::operator<<(unsigned long v) { if (Write(&v, 4) != 4) return false; return true; } BOOL T2Archive::operator>>(unsigned short& v) { if (Read(&v, 2) != 2) return false; return true; } BOOL T2Archive::operator<<(unsigned short v) { if (Write(&v, 2) != 2) return false; return true; } BOOL T2Archive::operator>>(POINT& v) { if (Read(&v, sizeof(v)) != sizeof(v)) return false; return true; } BOOL T2Archive::operator<<(POINT v) { if (Write(&v, sizeof(v)) != sizeof(v)) return false; return true; } BOOL T2Archive::operator>>(RECT& v) { if (Read(&v, sizeof(v)) != sizeof(v)) return false; return true; } BOOL T2Archive::operator<<(RECT v) { if (Write(&v, sizeof(v)) != sizeof(v)) return false; return true; } BOOL T2Archive::operator>>(CString& v) { int maxLen = 16; char buf[17]; v = ""; int amountRead = Read(buf, maxLen); while (strlen(buf) == amountRead) { buf[amountRead] = 0; v += buf; if (amountRead < maxLen) return false; amountRead = Read(buf, maxLen); } int len = strlen(buf); mFile->Seek(len - amountRead + 1, CFile::current); v += buf; return true; } BOOL T2Archive::operator<<(CString& v) { int len = v.GetLength() + 1; if (Write(v, len) != len) return false; return true; } BOOL T2Archive::operator>>(char* v) { int maxLen = 16; char buf[17]; v = ""; // BUG!! int amountRead = Read(buf, maxLen); while (strlen(buf) == amountRead) { buf[amountRead] = 0; strcat(v, buf); if (amountRead < maxLen) return false; amountRead = Read(buf, maxLen); } int len = strlen(buf); mFile->Seek(len - amountRead + 1, CFile::current); strcat(v, buf); return true; } BOOL T2Archive::operator<<(const char* v) { int len = strlen(v) + 1; if (Write(v, len) != len) return false; return true; } BOOL T2Archive::ReadPStr(char* v) { unsigned char len; Read(&len, 1); memset(v, 0, len + 1); Read(v, len); return true; } BOOL T2Archive::ReadPStr(CString& v) { unsigned char len; Read(&len, 1); char *buf = (char *) malloc(len + 1); memset(buf, 0, len + 1); Read(buf, len); v = buf; free(buf); return true; } BOOL T2Archive::WritePStr(const char* v) { int len = strlen(v) + 1; if (len > 255) return false; unsigned char len8 = len; Write(&len8, 1); Write(v, strlen(v) + 1); return true; } /*virtual*/ BOOL T2Archive::Skip(unsigned long v) { int bufSize; BOOL result = true; if (v > 1000) bufSize = 1000; else bufSize = v; void *buf = malloc(bufSize); while (v > 0) { int howMuch = Read(buf, bufSize); v -= howMuch; if (howMuch < bufSize && v > 0) { result = false; break; } } free(buf); return result; } static short swap16(short v); BOOL T2Archive::ReadSRect(RECT& v) { struct { short top, left, bottom, right; } r; Read(&r, sizeof(r)); v.top = swap16(r.top); v.left = swap16(r.left); v.bottom = swap16(r.bottom); v.right = swap16(r.right); return true; } static short swap16(short v) { union { unsigned char b[2]; short s; } work; unsigned char tmp; work.s = v; tmp = work.b[0]; work.b[0] = work.b[1]; work.b[1] = tmp; return work.s; } BOOL T2Archive::WriteSRect(const RECT& v) { struct { short top, left, bottom, right; } r; r.top = swap16(v.top); r.left = swap16(v.left); r.bottom = swap16(v.bottom); r.right = swap16(v.right); Write(&r, sizeof(r)); return true; }