Commit 1b2a8f96 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: I9473cc7e119592fce336aa47881839543e337b69 Reviewed-on: https://chromium-review.googlesource.com/199633Reviewed-by: 's avatarGeoff Lang <geofflang@chromium.org> Tested-by: 's avatarJamie Madill <jmadill@chromium.org>
parent 43b00426
......@@ -535,6 +535,12 @@ inline bool IsUnsignedMultiplicationSafe(T lhs, T rhs)
return (lhs == T(0) || rhs == T(0) || (rhs <= std::numeric_limits<T>::max() / lhs));
}
template <class SmallIntT, class BigIntT>
inline bool IsIntegerCastSafe(BigIntT bigValue)
{
return (static_cast<BigIntT>(static_cast<SmallIntT>(bigValue)) == bigValue);
}
}
#endif // LIBGLESV2_MATHUTIL_H_
#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)
......
......@@ -10,6 +10,7 @@
#define LIBGLESV2_BINARYSTREAM_H_
#include "common/angleutils.h"
#include "common/mathutil.h"
namespace gl
{
......@@ -25,42 +26,49 @@ 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)
{
return;
}
template <class IntT>
void readInt(IntT *outValue)
{
*outValue = readInt<IntT>();
}
size_t length = num * sizeof(T);
bool readBool()
{
int value;
read(&value);
return (value > 0);
}
if (mOffset + length > mLength)
{
mError = true;
return;
}
void readBool(bool *outValue)
{
*outValue = readBool();
}
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);
readInt(&length);
if (mError)
{
......@@ -109,6 +117,30 @@ class BinaryInputStream
size_t mOffset;
const char *mData;
size_t mLength;
template <typename T>
void read(T *v, size_t num)
{
META_ASSERT(std::is_fundamental<T>::value);
size_t length = num * sizeof(T);
if (mOffset + length > mLength)
{
mError = true;
return;
}
memcpy(v, mData + mOffset, length);
mOffset += length;
}
template <typename T>
void read(T *v)
{
read(v, 1);
}
};
class BinaryOutputStream
......@@ -118,31 +150,24 @@ class BinaryOutputStream
{
}
template <typename T>
void write(const T *v, size_t num)
// writeInt also handles bool types
template <class IntT>
void writeInt(IntT param)
{
union
{
T dummy; // Compilation error for non-trivial types
} dummy;
(void) dummy;
const char *asBytes = reinterpret_cast<const char*>(v);
mData.insert(mData.end(), asBytes, asBytes + num * sizeof(T));
ASSERT(rx::IsIntegerCastSafe<int>(param));
int intValue = static_cast<int>(param);
write(&intValue, 1);
}
template <typename T>
void write(const T &v)
void writeString(const std::string &v)
{
write(&v, 1);
writeInt(v.length());
write(v.c_str(), v.length());
}
void write(const std::string &v)
void writeBytes(unsigned char *bytes, size_t count)
{
size_t length = v.length();
write(length);
write(v.c_str(), length);
write(bytes, count);
}
size_t length() const
......@@ -158,6 +183,15 @@ class BinaryOutputStream
private:
DISALLOW_COPY_AND_ASSIGN(BinaryOutputStream);
std::vector<char> mData;
template <typename T>
void write(const T *v, size_t num)
{
META_ASSERT(std::is_fundamental<T>::value);
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