From: Zack Rusin Date: Thu, 25 Aug 2011 02:16:02 +0000 (-0400) Subject: Fix writing/reading compressed length of the chunks. X-Git-Url: https://git.cworth.org/git?a=commitdiff_plain;h=bfa8b255d1fdfe68f595c189e75eb44a312c8567;p=apitrace Fix writing/reading compressed length of the chunks. Spotted by Jose. --- diff --git a/trace_snappyfile.cpp b/trace_snappyfile.cpp index 457f7e9..45cd95a 100644 --- a/trace_snappyfile.cpp +++ b/trace_snappyfile.cpp @@ -145,7 +145,7 @@ void SnappyFile::flushCache() ::snappy::RawCompress(m_cache, SNAPPY_CHUNK_SIZE - freeCacheSize(), m_compressedCache, &compressedLength); - m_stream << compressedLength; + writeCompressedLength(compressedLength); m_stream.write(m_compressedCache, compressedLength); m_cachePtr = m_cache; } else if (m_mode == File::Read) { @@ -153,7 +153,7 @@ void SnappyFile::flushCache() return; //assert(m_cachePtr == m_cache + m_cacheSize); size_t compressedLength; - m_stream >> compressedLength; + compressedLength = readCompressedLength(); m_stream.read((char*)m_compressedCache, compressedLength); ::snappy::GetUncompressedLength(m_compressedCache, compressedLength, &m_cacheSize); @@ -171,3 +171,15 @@ void SnappyFile::createCache(size_t size) m_cachePtr = m_cache; m_cacheSize = size; } + +void SnappyFile::writeCompressedLength(size_t value) +{ + m_stream.write((char*)&value, sizeof value); +} + +size_t SnappyFile::readCompressedLength() +{ + size_t len; + m_stream.read((char*)&len, sizeof len); + return len; +} diff --git a/trace_snappyfile.hpp b/trace_snappyfile.hpp index 5489069..0cafa98 100644 --- a/trace_snappyfile.hpp +++ b/trace_snappyfile.hpp @@ -42,6 +42,8 @@ private: } void flushCache(); void createCache(size_t size); + void writeCompressedLength(size_t num); + size_t readCompressedLength(); private: std::fstream m_stream; char *m_cache;