blob: 4f7f5f2c85f9b3ec116045201b462d489f886912 [file] [log] [blame]
apatrick@chromium.org8963ec22012-07-09 22:34:06 +00001//
2// Copyright (c) 2012 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// BinaryStream.h: Provides binary serialization of simple types.
8
9#ifndef LIBGLESV2_BINARYSTREAM_H_
10#define LIBGLESV2_BINARYSTREAM_H_
11
apatrick@chromium.org8963ec22012-07-09 22:34:06 +000012#include "common/angleutils.h"
Jamie Madill1b2a8f92014-05-14 13:09:39 -040013#include "common/mathutil.h"
apatrick@chromium.org8963ec22012-07-09 22:34:06 +000014
Geoff Lang0b7eef72014-06-12 14:10:47 -040015#include <cstddef>
16#include <string>
17#include <vector>
Brandon Joneseb994362014-09-24 10:27:28 -070018#include <stdint.h>
Geoff Lang0b7eef72014-06-12 14:10:47 -040019
apatrick@chromium.org8963ec22012-07-09 22:34:06 +000020namespace gl
21{
22
23class BinaryInputStream
24{
25 public:
26 BinaryInputStream(const void *data, size_t length)
27 {
28 mError = false;
29 mOffset = 0;
Brandon Joneseb994362014-09-24 10:27:28 -070030 mData = static_cast<const uint8_t*>(data);
apatrick@chromium.org8963ec22012-07-09 22:34:06 +000031 mLength = length;
32 }
33
Jamie Madill1b2a8f92014-05-14 13:09:39 -040034 // readInt will generate an error for bool types
35 template <class IntT>
36 IntT readInt()
apatrick@chromium.org8963ec22012-07-09 22:34:06 +000037 {
Jamie Madill1b2a8f92014-05-14 13:09:39 -040038 int value;
39 read(&value);
40 return static_cast<IntT>(value);
apatrick@chromium.org8963ec22012-07-09 22:34:06 +000041 }
42
Jamie Madill1b2a8f92014-05-14 13:09:39 -040043 template <class IntT>
44 void readInt(IntT *outValue)
apatrick@chromium.org8963ec22012-07-09 22:34:06 +000045 {
Jamie Madill1b2a8f92014-05-14 13:09:39 -040046 *outValue = readInt<IntT>();
apatrick@chromium.org8963ec22012-07-09 22:34:06 +000047 }
48
Jamie Madill1b2a8f92014-05-14 13:09:39 -040049 bool readBool()
50 {
51 int value;
52 read(&value);
53 return (value > 0);
54 }
55
56 void readBool(bool *outValue)
57 {
58 *outValue = readBool();
59 }
60
61 void readBytes(unsigned char outArray[], size_t count)
62 {
63 read<unsigned char>(outArray, count);
64 }
65
66 std::string readString()
67 {
68 std::string outString;
69 readString(&outString);
70 return outString;
71 }
72
73 void readString(std::string *v)
apatrick@chromium.org8963ec22012-07-09 22:34:06 +000074 {
75 size_t length;
Jamie Madill1b2a8f92014-05-14 13:09:39 -040076 readInt(&length);
apatrick@chromium.org8963ec22012-07-09 22:34:06 +000077
78 if (mError)
79 {
80 return;
81 }
82
83 if (mOffset + length > mLength)
84 {
85 mError = true;
86 return;
87 }
88
Brandon Joneseb994362014-09-24 10:27:28 -070089 v->assign(reinterpret_cast<const char *>(mData) + mOffset, length);
apatrick@chromium.org8963ec22012-07-09 22:34:06 +000090 mOffset += length;
91 }
92
apatrick@chromium.org60dafe82012-09-05 22:26:10 +000093 void skip(size_t length)
94 {
95 if (mOffset + length > mLength)
96 {
97 mError = true;
98 return;
99 }
100
101 mOffset += length;
102 }
103
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +0000104 size_t offset() const
105 {
106 return mOffset;
107 }
108
apatrick@chromium.org8963ec22012-07-09 22:34:06 +0000109 bool error() const
110 {
111 return mError;
112 }
113
114 bool endOfStream() const
115 {
116 return mOffset == mLength;
117 }
118
Brandon Joneseb994362014-09-24 10:27:28 -0700119 const uint8_t *data()
120 {
121 return mData;
122 }
123
apatrick@chromium.org8963ec22012-07-09 22:34:06 +0000124 private:
125 DISALLOW_COPY_AND_ASSIGN(BinaryInputStream);
126 bool mError;
127 size_t mOffset;
Brandon Joneseb994362014-09-24 10:27:28 -0700128 const uint8_t *mData;
apatrick@chromium.org8963ec22012-07-09 22:34:06 +0000129 size_t mLength;
Jamie Madill1b2a8f92014-05-14 13:09:39 -0400130
131 template <typename T>
132 void read(T *v, size_t num)
133 {
134 META_ASSERT(std::is_fundamental<T>::value);
135
136 size_t length = num * sizeof(T);
137
138 if (mOffset + length > mLength)
139 {
140 mError = true;
141 return;
142 }
143
144 memcpy(v, mData + mOffset, length);
145 mOffset += length;
146 }
147
148 template <typename T>
149 void read(T *v)
150 {
151 read(v, 1);
152 }
153
apatrick@chromium.org8963ec22012-07-09 22:34:06 +0000154};
155
156class BinaryOutputStream
157{
158 public:
159 BinaryOutputStream()
160 {
161 }
162
Jamie Madill1b2a8f92014-05-14 13:09:39 -0400163 // writeInt also handles bool types
164 template <class IntT>
165 void writeInt(IntT param)
apatrick@chromium.org8963ec22012-07-09 22:34:06 +0000166 {
Jamie Madill1b2a8f92014-05-14 13:09:39 -0400167 ASSERT(rx::IsIntegerCastSafe<int>(param));
168 int intValue = static_cast<int>(param);
169 write(&intValue, 1);
Geoff Lang11c3b302014-05-13 22:33:31 +0000170 }
171
Jamie Madill1b2a8f92014-05-14 13:09:39 -0400172 void writeString(const std::string &v)
Geoff Lang11c3b302014-05-13 22:33:31 +0000173 {
Jamie Madill1b2a8f92014-05-14 13:09:39 -0400174 writeInt(v.length());
175 write(v.c_str(), v.length());
apatrick@chromium.org8963ec22012-07-09 22:34:06 +0000176 }
177
Ehsan Akhgariaa7e1662014-07-05 21:13:11 -0400178 void writeBytes(const unsigned char *bytes, size_t count)
apatrick@chromium.org8963ec22012-07-09 22:34:06 +0000179 {
Jamie Madill1b2a8f92014-05-14 13:09:39 -0400180 write(bytes, count);
apatrick@chromium.org8963ec22012-07-09 22:34:06 +0000181 }
182
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +0000183 size_t length() const
apatrick@chromium.org8963ec22012-07-09 22:34:06 +0000184 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +0000185 return mData.size();
186 }
apatrick@chromium.org8963ec22012-07-09 22:34:06 +0000187
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +0000188 const void* data() const
189 {
190 return mData.size() ? &mData[0] : NULL;
apatrick@chromium.org8963ec22012-07-09 22:34:06 +0000191 }
192
193 private:
194 DISALLOW_COPY_AND_ASSIGN(BinaryOutputStream);
195 std::vector<char> mData;
Jamie Madill1b2a8f92014-05-14 13:09:39 -0400196
197 template <typename T>
198 void write(const T *v, size_t num)
199 {
200 META_ASSERT(std::is_fundamental<T>::value);
201 const char *asBytes = reinterpret_cast<const char*>(v);
202 mData.insert(mData.end(), asBytes, asBytes + num * sizeof(T));
203 }
204
apatrick@chromium.org8963ec22012-07-09 22:34:06 +0000205};
206}
207
208#endif // LIBGLESV2_BINARYSTREAM_H_