mirror of
https://github.com/tumic0/GPXSee.git
synced 2024-10-07 15:23:22 +02:00
84 lines
1.3 KiB
C++
84 lines
1.3 KiB
C++
|
#include "bitstream.h"
|
||
|
|
||
|
|
||
|
bool BitStream1::read(int bits, quint32 &val)
|
||
|
{
|
||
|
val = 0;
|
||
|
|
||
|
for (int pos = 0; pos < bits; ) {
|
||
|
if (!_remaining) {
|
||
|
if (!_length || !_file.readUInt8(_hdl, _data))
|
||
|
return false;
|
||
|
_remaining = 8;
|
||
|
_length--;
|
||
|
}
|
||
|
|
||
|
quint32 get = bits - pos;
|
||
|
if (get >= _remaining) {
|
||
|
val |= _data << pos;
|
||
|
pos += _remaining;
|
||
|
_remaining = 0;
|
||
|
} else {
|
||
|
quint32 mask = (1<<get) - 1;
|
||
|
val |= (_data & mask)<<pos;
|
||
|
_data >>= get;
|
||
|
_remaining -= get;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
bool BitStream1::flush()
|
||
|
{
|
||
|
if (_length && !_file.seek(_hdl, _hdl.pos + _length))
|
||
|
return false;
|
||
|
|
||
|
_length = 0;
|
||
|
_remaining = 0;
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
|
||
|
bool BitStream4::read(int bits, quint32 &val)
|
||
|
{
|
||
|
if (bits <= 32 - (int)(_used + _unused)) {
|
||
|
val = bits ? (_data << _used) >> (32 - bits) : 0;
|
||
|
_used += bits;
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
quint32 old = 0;
|
||
|
if (_used < 32) {
|
||
|
old = (_data << _used) >> (32 - bits);
|
||
|
bits = (bits - 32) + _used;
|
||
|
}
|
||
|
_used = bits;
|
||
|
|
||
|
quint32 bytes = qMin(_length, (quint32)4);
|
||
|
|
||
|
if (!_file.readVUInt32SW(_hdl, bytes, _data))
|
||
|
return false;
|
||
|
|
||
|
_length -= bytes;
|
||
|
_unused = (4 - bytes) * 8;
|
||
|
_data <<= _unused;
|
||
|
|
||
|
val = _data >> (32 - _used) | old;
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
bool BitStream4::flush()
|
||
|
{
|
||
|
if (_length && !_file.seek(_hdl, _hdl.pos + _length))
|
||
|
return false;
|
||
|
|
||
|
_length = 0;
|
||
|
_unused = 32;
|
||
|
|
||
|
return true;
|
||
|
}
|