From: José Fonseca Date: Thu, 1 Sep 2011 11:16:49 +0000 (+0100) Subject: Always write snappy chunk lengths in little endian. X-Git-Url: https://git.cworth.org/git?a=commitdiff_plain;h=bcfe1fc406e917a03121eef0acd84909d8687dd6;p=apitrace Always write snappy chunk lengths in little endian. --- diff --git a/trace_snappyfile.cpp b/trace_snappyfile.cpp index f801685..c161b9d 100644 --- a/trace_snappyfile.cpp +++ b/trace_snappyfile.cpp @@ -30,7 +30,6 @@ #include #include -#include using namespace Trace; @@ -45,7 +44,7 @@ using namespace Trace; * The file is composed of a number of chunks, they are: * chunk { * uint32 - specifying the length of the compressed data - * compressed data + * compressed data, in little endian * } * File can contain any number of such chunks. * The default size of an uncompressed chunk is specified in @@ -232,14 +231,27 @@ void SnappyFile::createCache(size_t size) void SnappyFile::writeCompressedLength(size_t length) { - uint32_t value = length; - assert(value == length); - m_stream.write((const char*)&value, sizeof value); + unsigned char buf[4]; + buf[0] = length & 0xff; length >>= 8; + buf[1] = length & 0xff; length >>= 8; + buf[2] = length & 0xff; length >>= 8; + buf[3] = length & 0xff; length >>= 8; + assert(length == 0); + m_stream.write((const char *)buf, sizeof buf); } size_t SnappyFile::readCompressedLength() { - uint32_t length = 0; - m_stream.read((char*)&length, sizeof length); + unsigned char buf[4]; + size_t length; + m_stream.read((char *)buf, sizeof buf); + if (m_stream.fail()) { + length = 0; + } else { + length = (size_t)buf[0]; + length |= ((size_t)buf[1] << 8); + length |= ((size_t)buf[2] << 16); + length |= ((size_t)buf[3] << 24); + } return length; }