int UncompressString(const char * a_Data, size_t a_Length, AString & a_Uncompressed, size_t a_UncompressedSize) { // HACK: We're assuming that AString returns its internal buffer in its data() call and we're overwriting that buffer! // It saves us one allocation and one memcpy of the entire compressed data // It may not work on some STL implementations! (Confirmed working on MSVC 2008 & 2010) a_Uncompressed.resize(a_UncompressedSize); uLongf UncompressedSize = static_cast<uLongf>(a_UncompressedSize); // On some architectures the uLongf is different in size to int, that may be the cause of the -5 error int errorcode = uncompress(reinterpret_cast<Bytef *>(const_cast<char *>(a_Uncompressed.data())), &UncompressedSize, reinterpret_cast<const Bytef *>(a_Data), static_cast<uLong>(a_Length)); if (errorcode != Z_OK) { return errorcode; } a_Uncompressed.resize(UncompressedSize); return Z_OK; }
AString cFile::Read(size_t a_NumBytes) { ASSERT(IsOpen()); if (!IsOpen()) { return AString(); } // HACK: This depends on the knowledge that AString::data() returns the internal buffer, rather than a copy of it. AString res; res.resize(a_NumBytes); auto newSize = fread(const_cast<char *>(res.data()), 1, a_NumBytes, m_File); res.resize(newSize); return res; }
int CompressString(const char * a_Data, size_t a_Length, AString & a_Compressed, int a_Factor) { uLongf CompressedSize = compressBound(static_cast<uLong>(a_Length)); // HACK: We're assuming that AString returns its internal buffer in its data() call and we're overwriting that buffer! // It saves us one allocation and one memcpy of the entire compressed data // It may not work on some STL implementations! (Confirmed working on MSVC 2008 & 2010) a_Compressed.resize(CompressedSize); int errorcode = compress2(reinterpret_cast<Bytef *>(const_cast<char *>(a_Compressed.data())), &CompressedSize, reinterpret_cast<const Bytef *>(a_Data), static_cast<uLong>(a_Length), a_Factor); if (errorcode != Z_OK) { return errorcode; } a_Compressed.resize(CompressedSize); return Z_OK; }
int cProtocol132::ParseItem(cItem & a_Item) { HANDLE_PACKET_READ(ReadBEShort, short, ItemType); if (ItemType <= -1) { a_Item.Empty(); return PARSE_OK; } a_Item.m_ItemType = ItemType; HANDLE_PACKET_READ(ReadChar, char, ItemCount); HANDLE_PACKET_READ(ReadBEShort, short, ItemDamage); a_Item.m_ItemCount = ItemCount; a_Item.m_ItemDamage = ItemDamage; if (ItemCount <= 0) { a_Item.Empty(); } HANDLE_PACKET_READ(ReadBEShort, short, MetadataLength); if (MetadataLength <= 0) { return PARSE_OK; } // Read the metadata AString Metadata; Metadata.resize((size_t)MetadataLength); if (!m_ReceivedData.ReadBuf((void *)Metadata.data(), (size_t)MetadataLength)) { return PARSE_INCOMPLETE; } return ParseItemMetadata(a_Item, Metadata); }