/usr/include/o3dgc/o3dgcBinaryStream.h is in libo3dgc-dev 0~20131011-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 | /*
Copyright (c) 2013 Khaled Mammou - Advanced Micro Devices, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#pragma once
#ifndef O3DGC_BINARY_STREAM_H
#define O3DGC_BINARY_STREAM_H
#include "o3dgcCommon.h"
#include "o3dgcVector.h"
namespace o3dgc
{
const unsigned long O3DGC_BINARY_STREAM_DEFAULT_SIZE = 4096;
const unsigned long O3DGC_BINARY_STREAM_BITS_PER_SYMBOL0 = 7;
const unsigned long O3DGC_BINARY_STREAM_MAX_SYMBOL0 = (1 << O3DGC_BINARY_STREAM_BITS_PER_SYMBOL0) - 1;
const unsigned long O3DGC_BINARY_STREAM_BITS_PER_SYMBOL1 = 6;
const unsigned long O3DGC_BINARY_STREAM_MAX_SYMBOL1 = (1 << O3DGC_BINARY_STREAM_BITS_PER_SYMBOL1) - 1;
const unsigned long O3DGC_BINARY_STREAM_NUM_SYMBOLS_UINT32 = (32+O3DGC_BINARY_STREAM_BITS_PER_SYMBOL0-1) /
O3DGC_BINARY_STREAM_BITS_PER_SYMBOL0;
//!
class BinaryStream
{
public:
//! Constructor.
BinaryStream(unsigned long size = O3DGC_BINARY_STREAM_DEFAULT_SIZE)
{
m_endianness = SystemEndianness();
m_stream.Allocate(size);
};
//! Destructor.
~BinaryStream(void){};
void WriteFloat32(float value, O3DGCStreamType streamType)
{
if (streamType == O3DGC_STREAM_TYPE_ASCII)
{
WriteFloat32ASCII(value);
}
else
{
WriteFloat32Bin(value);
}
}
void WriteUInt32(unsigned long position, unsigned long value, O3DGCStreamType streamType)
{
if (streamType == O3DGC_STREAM_TYPE_ASCII)
{
WriteUInt32ASCII(position, value);
}
else
{
WriteUInt32Bin(position, value);
}
}
void WriteUInt32(unsigned long value, O3DGCStreamType streamType)
{
if (streamType == O3DGC_STREAM_TYPE_ASCII)
{
WriteUInt32ASCII(value);
}
else
{
WriteUInt32Bin(value);
}
}
void WriteUChar(unsigned int position, unsigned char value, O3DGCStreamType streamType)
{
if (streamType == O3DGC_STREAM_TYPE_ASCII)
{
WriteUInt32ASCII(position, value);
}
else
{
WriteUInt32Bin(position, value);
}
}
void WriteUChar(unsigned char value, O3DGCStreamType streamType)
{
if (streamType == O3DGC_STREAM_TYPE_ASCII)
{
WriteUCharASCII(value);
}
else
{
WriteUChar8Bin(value);
}
}
float ReadFloat32(unsigned long & position, O3DGCStreamType streamType) const
{
float value;
if (streamType == O3DGC_STREAM_TYPE_ASCII)
{
value = ReadFloat32ASCII(position);
}
else
{
value = ReadFloat32Bin(position);
}
return value;
}
unsigned long ReadUInt32(unsigned long & position, O3DGCStreamType streamType) const
{
unsigned long value;
if (streamType == O3DGC_STREAM_TYPE_ASCII)
{
value = ReadUInt32ASCII(position);
}
else
{
value = ReadUInt32Bin(position);
}
return value;
}
unsigned char ReadUChar(unsigned long & position, O3DGCStreamType streamType) const
{
unsigned char value;
if (streamType == O3DGC_STREAM_TYPE_ASCII)
{
value = ReadUCharASCII(position);
}
else
{
value = ReadUChar8Bin(position);
}
return value;
}
void WriteFloat32Bin(unsigned long position, float value)
{
assert(position < m_stream.GetSize() - 4);
unsigned char * ptr = (unsigned char *) (&value);
if (m_endianness == O3DGC_BIG_ENDIAN)
{
m_stream[position++] = ptr[3];
m_stream[position++] = ptr[2];
m_stream[position++] = ptr[1];
m_stream[position ] = ptr[0];
}
else
{
m_stream[position++] = ptr[0];
m_stream[position++] = ptr[1];
m_stream[position++] = ptr[2];
m_stream[position ] = ptr[3];
}
}
void WriteFloat32Bin(float value)
{
unsigned char * ptr = (unsigned char *) (&value);
if (m_endianness == O3DGC_BIG_ENDIAN)
{
m_stream.PushBack(ptr[3]);
m_stream.PushBack(ptr[2]);
m_stream.PushBack(ptr[1]);
m_stream.PushBack(ptr[0]);
}
else
{
m_stream.PushBack(ptr[0]);
m_stream.PushBack(ptr[1]);
m_stream.PushBack(ptr[2]);
m_stream.PushBack(ptr[3]);
}
}
void WriteUInt32Bin(unsigned long position, unsigned long value)
{
assert(position < m_stream.GetSize() - 4);
unsigned char * ptr = (unsigned char *) (&value);
if (m_endianness == O3DGC_BIG_ENDIAN)
{
m_stream[position++] = ptr[3];
m_stream[position++] = ptr[2];
m_stream[position++] = ptr[1];
m_stream[position ] = ptr[0];
}
else
{
m_stream[position++] = ptr[0];
m_stream[position++] = ptr[1];
m_stream[position++] = ptr[2];
m_stream[position ] = ptr[3];
}
}
void WriteUInt32Bin(unsigned long value)
{
unsigned char * ptr = (unsigned char *) (&value);
if (m_endianness == O3DGC_BIG_ENDIAN)
{
m_stream.PushBack(ptr[3]);
m_stream.PushBack(ptr[2]);
m_stream.PushBack(ptr[1]);
m_stream.PushBack(ptr[0]);
}
else
{
m_stream.PushBack(ptr[0]);
m_stream.PushBack(ptr[1]);
m_stream.PushBack(ptr[2]);
m_stream.PushBack(ptr[3]);
}
}
void WriteUChar8Bin(unsigned int position, unsigned char value)
{
m_stream[position] = value;
}
void WriteUChar8Bin(unsigned char value)
{
m_stream.PushBack(value);
}
float ReadFloat32Bin(unsigned long & position) const
{
unsigned long value = ReadUInt32Bin(position);
float fvalue = *((float *)(&value));
return fvalue;
}
unsigned long ReadUInt32Bin(unsigned long & position) const
{
assert(position < m_stream.GetSize() - 4);
unsigned long value = 0;
if (m_endianness == O3DGC_BIG_ENDIAN)
{
value += (m_stream[position++]<<24);
value += (m_stream[position++]<<16);
value += (m_stream[position++]<<8);
value += (m_stream[position++]);
}
else
{
value += (m_stream[position++]);
value += (m_stream[position++]<<8);
value += (m_stream[position++]<<16);
value += (m_stream[position++]<<24);
}
return value;
}
unsigned char ReadUChar8Bin(unsigned long & position) const
{
return m_stream[position++];
}
void WriteFloat32ASCII(float value)
{
unsigned long uiValue = *((unsigned long *)(&value));
WriteUInt32ASCII(uiValue);
}
void WriteUInt32ASCII(unsigned long position, unsigned long value)
{
assert(position < m_stream.GetSize() - O3DGC_BINARY_STREAM_NUM_SYMBOLS_UINT32);
unsigned long value0 = value;
for(unsigned long i = 0; i < O3DGC_BINARY_STREAM_NUM_SYMBOLS_UINT32; ++i)
{
m_stream[position++] = (value0 & O3DGC_BINARY_STREAM_MAX_SYMBOL0);
value0 >>= O3DGC_BINARY_STREAM_BITS_PER_SYMBOL0;
}
}
void WriteUInt32ASCII(unsigned long value)
{
for(unsigned long i = 0; i < O3DGC_BINARY_STREAM_NUM_SYMBOLS_UINT32; ++i)
{
m_stream.PushBack(value & O3DGC_BINARY_STREAM_MAX_SYMBOL0);
value >>= O3DGC_BINARY_STREAM_BITS_PER_SYMBOL0;
}
}
void WriteIntASCII(long value)
{
WriteUIntASCII(IntToUInt(value));
}
void WriteUIntASCII(unsigned long value)
{
if (value >= O3DGC_BINARY_STREAM_MAX_SYMBOL0)
{
m_stream.PushBack(O3DGC_BINARY_STREAM_MAX_SYMBOL0);
value -= O3DGC_BINARY_STREAM_MAX_SYMBOL0;
unsigned char a, b;
do
{
a = ((value & O3DGC_BINARY_STREAM_MAX_SYMBOL1) << 1);
b = ( (value >>= O3DGC_BINARY_STREAM_BITS_PER_SYMBOL1) > 0);
a += b;
m_stream.PushBack(a);
} while (b);
}
else
{
m_stream.PushBack((unsigned char) value);
}
}
void WriteUCharASCII(unsigned char value)
{
assert(value <= O3DGC_BINARY_STREAM_MAX_SYMBOL0);
m_stream.PushBack(value);
}
float ReadFloat32ASCII(unsigned long & position) const
{
unsigned long value = ReadUInt32ASCII(position);
float fvalue = *((float *)(&value));
return fvalue;
}
unsigned long ReadUInt32ASCII(unsigned long & position) const
{
assert(position < m_stream.GetSize() - O3DGC_BINARY_STREAM_NUM_SYMBOLS_UINT32);
unsigned long value = 0;
unsigned long shift = 0;
for(unsigned long i = 0; i < O3DGC_BINARY_STREAM_NUM_SYMBOLS_UINT32; ++i)
{
value += (m_stream[position++] << shift);
shift += O3DGC_BINARY_STREAM_BITS_PER_SYMBOL0;
}
return value;
}
long ReadIntASCII(unsigned long & position) const
{
return UIntToInt(ReadUIntASCII(position));
}
unsigned long ReadUIntASCII(unsigned long & position) const
{
unsigned long value = m_stream[position++];
if (value == O3DGC_BINARY_STREAM_MAX_SYMBOL0)
{
long x;
unsigned long i = 0;
do
{
x = m_stream[position++];
value += ( (x>>1) << i);
i += O3DGC_BINARY_STREAM_BITS_PER_SYMBOL1;
} while (x & 1);
}
return value;
}
unsigned char ReadUCharASCII(unsigned long & position) const
{
return m_stream[position++];
}
O3DGCErrorCode Save(const char * const fileName)
{
FILE * fout = fopen(fileName, "wb");
if (!fout)
{
return O3DGC_ERROR_CREATE_FILE;
}
fwrite(m_stream.GetBuffer(), 1, m_stream.GetSize(), fout);
fclose(fout);
return O3DGC_OK;
}
O3DGCErrorCode Load(const char * const fileName)
{
FILE * fin = fopen(fileName, "rb");
if (!fin)
{
return O3DGC_ERROR_OPEN_FILE;
}
fseek(fin, 0, SEEK_END);
unsigned long size = ftell(fin);
m_stream.Allocate(size);
rewind(fin);
unsigned int nread = (unsigned int) fread((void *) m_stream.GetBuffer(), 1, size, fin);
m_stream.SetSize(size);
if (nread != size)
{
return O3DGC_ERROR_READ_FILE;
}
fclose(fin);
return O3DGC_OK;
}
O3DGCErrorCode LoadFromBuffer(unsigned char * buffer, unsigned long bufferSize)
{
m_stream.Allocate(bufferSize);
memcpy(m_stream.GetBuffer(), buffer, bufferSize);
m_stream.SetSize(bufferSize);
return O3DGC_OK;
}
unsigned long GetSize() const
{
return m_stream.GetSize();
}
const unsigned char * const GetBuffer(unsigned long position) const
{
return m_stream.GetBuffer() + position;
}
unsigned char * const GetBuffer(unsigned long position)
{
return (m_stream.GetBuffer() + position);
}
unsigned char * const GetBuffer()
{
return m_stream.GetBuffer();
}
void GetBuffer(unsigned long position, unsigned char * & buffer) const
{
buffer = (unsigned char *) (m_stream.GetBuffer() + position); // fix me: ugly!
}
void SetSize(unsigned long size)
{
m_stream.SetSize(size);
};
void Allocate(unsigned long size)
{
m_stream.Allocate(size);
}
private:
Vector<unsigned char> m_stream;
O3DGCEndianness m_endianness;
};
}
#endif // O3DGC_BINARY_STREAM_H
|