From bcfe1fc406e917a03121eef0acd84909d8687dd6 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jos=C3=A9=20Fonseca?= Date: Thu, 1 Sep 2011 12:16:49 +0100 Subject: [PATCH] Always write snappy chunk lengths in little endian. --- trace_snappyfile.cpp | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) 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; } -- 2.7.4