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" #include "commit.h"
#define ANGLE_MAJOR_VERSION 1 #define ANGLE_MAJOR_VERSION 2
#define ANGLE_MINOR_VERSION 3 #define ANGLE_MINOR_VERSION 1
#define ANGLE_STRINGIFY(x) #x #define ANGLE_STRINGIFY(x) #x
#define ANGLE_MACRO_STRINGIFY(x) ANGLE_STRINGIFY(x) #define ANGLE_MACRO_STRINGIFY(x) ANGLE_STRINGIFY(x)
......
...@@ -25,39 +25,50 @@ class BinaryInputStream ...@@ -25,39 +25,50 @@ class BinaryInputStream
mLength = length; mLength = length;
} }
template <typename T> // readInt will generate an error for bool types
void read(T *v, size_t num) template <class IntT>
IntT readInt()
{ {
union int value;
{ read(&value);
T dummy; // Compilation error for non-trivial types return static_cast<IntT>(value);
} dummy; }
(void) dummy;
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; int value;
return; read(&value);
} *outValue = (value > 0);
}
memcpy(v, mData + mOffset, length); void readBytes(unsigned char outArray[], size_t count)
mOffset += length; {
read<unsigned char>(outArray, count);
} }
template <typename T> std::string readString()
void read(T * v)
{ {
read(v, 1); std::string outString;
readString(&outString);
return outString;
} }
void read(std::string *v) void readString(std::string *v)
{ {
size_t length; size_t length;
read(&length); read(&length);
...@@ -109,40 +120,65 @@ class BinaryInputStream ...@@ -109,40 +120,65 @@ class BinaryInputStream
size_t mOffset; size_t mOffset;
const char *mData; const char *mData;
size_t mLength; size_t mLength;
};
class BinaryOutputStream
{
public:
BinaryOutputStream()
{
}
template <typename T> template <typename T>
void write(const T *v, size_t num) void read(T *v, size_t num)
{ {
union union
{ {
T dummy; // Compilation error for non-trivial types T dummy; // Compilation error for non-POD types
} dummy; } dummy;
(void) dummy; (void)dummy;
const char *asBytes = reinterpret_cast<const char*>(v); if (mError)
mData.insert(mData.end(), asBytes, asBytes + num * sizeof(T)); {
return;
}
size_t length = num * sizeof(T);
if (mOffset + length > mLength)
{
mError = true;
return;
}
memcpy(v, mData + mOffset, length);
mOffset += length;
} }
template <typename T> 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); write(&v, 1);
} }
void write(const std::string &v) void writeString(const std::string &v)
{ {
size_t length = v.length(); writeInt(v.length());
write(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 size_t length() const
...@@ -158,6 +194,20 @@ class BinaryOutputStream ...@@ -158,6 +194,20 @@ class BinaryOutputStream
private: private:
DISALLOW_COPY_AND_ASSIGN(BinaryOutputStream); DISALLOW_COPY_AND_ASSIGN(BinaryOutputStream);
std::vector<char> mData; 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