Fix proto_stream to support 32-bit platforms. (#427)

In proto_stream.cc, size_t is only correct on 64-bit platforms. Changing it to uint64 adds support for 32-bit platforms. This fixes #426.
master
lanyaye 2017-07-25 21:59:57 +08:00 committed by Wolfgang Hess
parent ea7c39b6f0
commit 56dbad15e3
1 changed files with 6 additions and 6 deletions

View File

@ -22,20 +22,20 @@ namespace io {
namespace { namespace {
// First eight bytes to identify our proto stream format. // First eight bytes to identify our proto stream format.
const size_t kMagic = 0x7b1d1f7b5bf501db; const uint64 kMagic = 0x7b1d1f7b5bf501db;
void WriteSizeAsLittleEndian(size_t size, std::ostream* out) { void WriteSizeAsLittleEndian(uint64 size, std::ostream* out) {
for (int i = 0; i != 8; ++i) { for (int i = 0; i != 8; ++i) {
out->put(size & 0xff); out->put(size & 0xff);
size >>= 8; size >>= 8;
} }
} }
bool ReadSizeAsLittleEndian(std::istream* in, size_t* size) { bool ReadSizeAsLittleEndian(std::istream* in, uint64* size) {
*size = 0; *size = 0;
for (int i = 0; i != 8; ++i) { for (int i = 0; i != 8; ++i) {
*size >>= 8; *size >>= 8;
*size += static_cast<size_t>(in->get()) << 56; *size += static_cast<uint64>(in->get()) << 56;
} }
return !in->fail(); return !in->fail();
} }
@ -63,7 +63,7 @@ bool ProtoStreamWriter::Close() {
ProtoStreamReader::ProtoStreamReader(const string& filename) ProtoStreamReader::ProtoStreamReader(const string& filename)
: in_(filename, std::ios::in | std::ios::binary) { : in_(filename, std::ios::in | std::ios::binary) {
size_t magic; uint64 magic;
if (!ReadSizeAsLittleEndian(&in_, &magic) || magic != kMagic) { if (!ReadSizeAsLittleEndian(&in_, &magic) || magic != kMagic) {
in_.setstate(std::ios::failbit); in_.setstate(std::ios::failbit);
} }
@ -72,7 +72,7 @@ ProtoStreamReader::ProtoStreamReader(const string& filename)
ProtoStreamReader::~ProtoStreamReader() {} ProtoStreamReader::~ProtoStreamReader() {}
bool ProtoStreamReader::Read(string* decompressed_data) { bool ProtoStreamReader::Read(string* decompressed_data) {
size_t compressed_size; uint64 compressed_size;
if (!ReadSizeAsLittleEndian(&in_, &compressed_size)) { if (!ReadSizeAsLittleEndian(&in_, &compressed_size)) {
return false; return false;
} }