Commit 26198851 by Jamie Madill

Fix binary serialization to use explicit size and type.

Loading program binaries across different architectures would cause runtime errors due to use of size_t. Also fix the ANGLE major and minor version that were clobbered in an earlier commit. BUG=angle:647 BUG=371435 Change-Id: If2ad4c8fc246cc9ff0b4d95ba87b7d1ed109a064 Reviewed-on: https://chromium-review.googlesource.com/199102Reviewed-by: 's avatarNicolas Capens <nicolascapens@chromium.org> Tested-by: 's avatarJamie Madill <jmadill@chromium.org>
parent 2c27db6f
#include "commit.h"
#define ANGLE_MAJOR_VERSION 1
#define ANGLE_MINOR_VERSION 3
#define ANGLE_MAJOR_VERSION 2
#define ANGLE_MINOR_VERSION 1
#define ANGLE_STRINGIFY(x) #x
#define ANGLE_MACRO_STRINGIFY(x) ANGLE_STRINGIFY(x)
......
......@@ -25,39 +25,50 @@ class BinaryInputStream
mLength = length;
}
template <typename T>
void read(T *v, size_t num)
// readInt will generate an error for bool types
template <class IntT>
IntT readInt()
{
union
{
T dummy; // Compilation error for non-trivial types
} dummy;
(void) dummy;
int value;
read(&value);
return static_cast<IntT>(value);
}
if (mError)
template <class IntT>
void readInt(IntT *outValue)
{
return;
int value;
read(&value);
*outValue = static_cast<IntT>(value);
}
size_t length = num * sizeof(T);
bool readBool()
{
int value;
read(&value);
return (value > 0);
}
if (mOffset + length > mLength)
void readBool(bool *outValue)
{
mError = true;
return;
int value;
read(&value);
*outValue = (value > 0);
}
memcpy(v, mData + mOffset, length);
mOffset += length;
void readBytes(unsigned char outArray[], size_t count)
{
read<unsigned char>(outArray, count);
}
template <typename T>
void read(T * v)
std::string readString()
{
read(v, 1);
std::string outString;
readString(&outString);
return outString;
}
void read(std::string *v)
void readString(std::string *v)
{
size_t length;
read(&length);
......@@ -109,40 +120,65 @@ class BinaryInputStream
size_t mOffset;
const char *mData;
size_t mLength;
};
class BinaryOutputStream
{
public:
BinaryOutputStream()
{
}
template <typename T>
void write(const T *v, size_t num)
void read(T *v, size_t num)
{
union
{
T dummy; // Compilation error for non-trivial types
T dummy; // Compilation error for non-POD types
} dummy;
(void) dummy;
(void)dummy;
const char *asBytes = reinterpret_cast<const char*>(v);
mData.insert(mData.end(), asBytes, asBytes + num * sizeof(T));
if (mError)
{
return;
}
size_t length = num * sizeof(T);
if (mOffset + length > mLength)
{
mError = true;
return;
}
memcpy(v, mData + mOffset, length);
mOffset += length;
}
template <typename T>
void write(const T &v)
void read(T * v)
{
read(v, 1);
}
};
class BinaryOutputStream
{
public:
BinaryOutputStream()
{
}
// writeInt also handles bool types
template <class IntT>
void writeInt(IntT v)
{
int intValue = static_cast<int>(v);
write(&v, 1);
}
void write(const std::string &v)
void writeString(const std::string &v)
{
size_t length = v.length();
write(length);
writeInt(v.length());
write(v.c_str(), v.length());
}
write(v.c_str(), length);
void writeBytes(unsigned char *bytes, size_t count)
{
write(bytes, count);
}
size_t length() const
......@@ -158,6 +194,20 @@ class BinaryOutputStream
private:
DISALLOW_COPY_AND_ASSIGN(BinaryOutputStream);
std::vector<char> mData;
template <typename T>
void write(const T *v, size_t num)
{
union
{
T dummy; // Compilation error for non-POD types
} dummy;
(void) dummy;
const char *asBytes = reinterpret_cast<const char*>(v);
mData.insert(mData.end(), asBytes, asBytes + num * sizeof(T));
}
};
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment