2019-06-03 09:56:50 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <Core/Types.h>
|
2019-06-17 03:27:42 +00:00
|
|
|
#include <Common/BitHelpers.h>
|
2019-11-29 12:29:32 +00:00
|
|
|
#include <Common/Exception.h>
|
|
|
|
|
|
|
|
#include <string.h>
|
2019-06-03 09:56:50 +00:00
|
|
|
|
2019-06-26 20:32:28 +00:00
|
|
|
#if defined(__OpenBSD__) || defined(__FreeBSD__)
|
|
|
|
# include <sys/endian.h>
|
2019-06-27 15:00:03 +00:00
|
|
|
#elif defined(__APPLE__)
|
|
|
|
# include <libkern/OSByteOrder.h>
|
|
|
|
|
|
|
|
# define htobe64(x) OSSwapHostToBigInt64(x)
|
|
|
|
# define be64toh(x) OSSwapBigToHostInt64(x)
|
2019-06-26 20:32:28 +00:00
|
|
|
#endif
|
|
|
|
|
2019-11-29 12:29:32 +00:00
|
|
|
|
2019-06-03 09:56:50 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2019-11-29 12:29:32 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int CANNOT_WRITE_AFTER_END_OF_BUFFER;
|
|
|
|
extern const int ATTEMPT_TO_READ_AFTER_EOF;
|
|
|
|
}
|
|
|
|
|
2019-06-13 14:04:38 +00:00
|
|
|
/** Reads data from underlying ReadBuffer bit by bit, max 64 bits at once.
|
2019-06-03 09:56:50 +00:00
|
|
|
*
|
|
|
|
* reads MSB bits first, imagine that you have a data:
|
2019-06-13 14:04:38 +00:00
|
|
|
* 11110000 10101010 00100100 11111110
|
2019-06-03 09:56:50 +00:00
|
|
|
*
|
|
|
|
* Given that r is BitReader created with a ReadBuffer that reads from data above:
|
|
|
|
* r.readBits(3) => 0b111
|
|
|
|
* r.readBit() => 0b1
|
|
|
|
* r.readBits(8) => 0b1010 // 4 leading zero-bits are not shown
|
|
|
|
* r.readBit() => 0b1
|
|
|
|
* r.readBit() => 0b0
|
2019-06-12 17:12:08 +00:00
|
|
|
* r.readBits(15) => 0b10001001001111111
|
2019-06-13 14:04:38 +00:00
|
|
|
* r.readBit() => 0b0
|
2019-06-03 09:56:50 +00:00
|
|
|
**/
|
|
|
|
|
|
|
|
class BitReader
|
|
|
|
{
|
2019-12-17 09:33:14 +00:00
|
|
|
using BufferType = unsigned __int128;
|
2019-11-29 12:29:32 +00:00
|
|
|
|
|
|
|
const char * source_begin;
|
|
|
|
const char * source_current;
|
|
|
|
const char * source_end;
|
2019-06-03 09:56:50 +00:00
|
|
|
|
2019-11-29 12:29:32 +00:00
|
|
|
BufferType bits_buffer;
|
2019-06-03 09:56:50 +00:00
|
|
|
UInt8 bits_count;
|
|
|
|
|
|
|
|
public:
|
2019-11-29 12:29:32 +00:00
|
|
|
BitReader(const char * begin, size_t size)
|
|
|
|
: source_begin(begin),
|
|
|
|
source_current(begin),
|
|
|
|
source_end(begin + size),
|
2019-06-13 14:04:38 +00:00
|
|
|
bits_buffer(0),
|
|
|
|
bits_count(0)
|
|
|
|
{}
|
|
|
|
|
|
|
|
~BitReader()
|
|
|
|
{}
|
|
|
|
|
2019-12-17 09:33:14 +00:00
|
|
|
// reads bits_to_read high-bits from bits_buffer
|
|
|
|
inline UInt64 readBits(UInt8 bits_to_read)
|
2019-06-13 14:04:38 +00:00
|
|
|
{
|
2019-12-17 09:33:14 +00:00
|
|
|
if (bits_to_read > bits_count)
|
2019-12-18 11:53:52 +00:00
|
|
|
fillBitBuffer();
|
2019-06-13 14:04:38 +00:00
|
|
|
|
2019-12-18 11:53:52 +00:00
|
|
|
return getBitsFromBitBuffer<CONSUME>(bits_to_read);
|
2019-06-13 14:04:38 +00:00
|
|
|
}
|
|
|
|
|
2019-12-17 09:33:14 +00:00
|
|
|
inline UInt8 peekByte()
|
2019-06-13 14:04:38 +00:00
|
|
|
{
|
2019-12-17 09:33:14 +00:00
|
|
|
if (bits_count < 8)
|
2019-12-18 11:53:52 +00:00
|
|
|
fillBitBuffer();
|
2019-11-29 12:29:32 +00:00
|
|
|
|
2019-12-18 11:53:52 +00:00
|
|
|
return getBitsFromBitBuffer<PEEK>(8);
|
2019-06-13 14:04:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline UInt8 readBit()
|
|
|
|
{
|
|
|
|
return static_cast<UInt8>(readBits(1));
|
|
|
|
}
|
|
|
|
|
2019-12-18 11:53:52 +00:00
|
|
|
// skip bits from bits_buffer
|
|
|
|
inline void skipBufferedBits(UInt8 bits)
|
|
|
|
{
|
|
|
|
bits_buffer <<= bits;
|
|
|
|
bits_count -= bits;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-06-13 14:04:38 +00:00
|
|
|
inline bool eof() const
|
|
|
|
{
|
2019-11-29 12:29:32 +00:00
|
|
|
return bits_count == 0 && source_current >= source_end;
|
|
|
|
}
|
|
|
|
|
2019-12-17 09:33:14 +00:00
|
|
|
// number of bits that was already read by clients with readBits()
|
2019-11-29 12:29:32 +00:00
|
|
|
inline UInt64 count() const
|
|
|
|
{
|
2019-12-17 09:33:14 +00:00
|
|
|
return (source_current - source_begin) * 8 - bits_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline UInt64 remaining() const
|
|
|
|
{
|
|
|
|
return (source_end - source_current) * 8 + bits_count;
|
2019-06-13 14:04:38 +00:00
|
|
|
}
|
2019-06-03 09:56:50 +00:00
|
|
|
|
|
|
|
private:
|
2019-12-18 11:53:52 +00:00
|
|
|
enum GetBitsMode {CONSUME, PEEK};
|
|
|
|
// read data from internal buffer, if it has not enough bits, result is undefined.
|
|
|
|
template <GetBitsMode mode>
|
|
|
|
inline UInt64 getBitsFromBitBuffer(UInt8 bits_to_read)
|
|
|
|
{
|
|
|
|
// push down the high-bits
|
|
|
|
const UInt64 result = static_cast<UInt64>(bits_buffer >> (sizeof(bits_buffer) * 8 - bits_to_read));
|
|
|
|
|
|
|
|
if constexpr (mode == CONSUME)
|
|
|
|
{
|
|
|
|
// 'erase' high-bits that were have read
|
|
|
|
skipBufferedBits(bits_to_read);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Fills internal bits_buffer with data from source, reads at most 64 bits
|
|
|
|
size_t fillBitBuffer()
|
2019-06-13 14:04:38 +00:00
|
|
|
{
|
2019-11-29 12:29:32 +00:00
|
|
|
const size_t available = source_end - source_current;
|
2019-12-18 11:53:52 +00:00
|
|
|
const auto bytes_to_read = std::min<size_t>(64 / 8, available);
|
2019-11-29 12:29:32 +00:00
|
|
|
if (available == 0)
|
|
|
|
{
|
2019-12-18 11:53:52 +00:00
|
|
|
if (bytes_to_read == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
throw Exception("Buffer is empty, but requested to read "
|
|
|
|
+ std::to_string(bytes_to_read) + " more bytes.",
|
|
|
|
ErrorCodes::ATTEMPT_TO_READ_AFTER_EOF);
|
2019-11-29 12:29:32 +00:00
|
|
|
}
|
|
|
|
|
2019-12-17 09:33:14 +00:00
|
|
|
UInt64 tmp_buffer = 0;
|
|
|
|
memcpy(&tmp_buffer, source_current, bytes_to_read);
|
|
|
|
source_current += bytes_to_read;
|
2019-11-29 12:29:32 +00:00
|
|
|
|
2019-12-17 09:33:14 +00:00
|
|
|
tmp_buffer = be64toh(tmp_buffer);
|
2019-11-29 12:29:32 +00:00
|
|
|
|
2019-12-17 09:33:14 +00:00
|
|
|
bits_buffer |= BufferType(tmp_buffer) << ((sizeof(BufferType) - sizeof(tmp_buffer)) * 8 - bits_count);
|
|
|
|
bits_count += static_cast<UInt8>(bytes_to_read) * 8;
|
2019-06-17 03:27:42 +00:00
|
|
|
|
2019-12-17 09:33:14 +00:00
|
|
|
return bytes_to_read;
|
2019-06-13 14:04:38 +00:00
|
|
|
}
|
2019-06-03 09:56:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class BitWriter
|
|
|
|
{
|
2019-12-18 11:53:52 +00:00
|
|
|
using BufferType = unsigned __int128;
|
2019-06-03 09:56:50 +00:00
|
|
|
|
2019-11-29 12:29:32 +00:00
|
|
|
char * dest_begin;
|
|
|
|
char * dest_current;
|
|
|
|
char * dest_end;
|
|
|
|
|
|
|
|
BufferType bits_buffer;
|
2019-06-03 09:56:50 +00:00
|
|
|
UInt8 bits_count;
|
|
|
|
|
2019-06-13 14:04:38 +00:00
|
|
|
static constexpr UInt8 BIT_BUFFER_SIZE = sizeof(bits_buffer) * 8;
|
2019-06-03 09:56:50 +00:00
|
|
|
|
2019-06-13 14:04:38 +00:00
|
|
|
public:
|
2019-11-29 12:29:32 +00:00
|
|
|
BitWriter(char * begin, size_t size)
|
|
|
|
: dest_begin(begin),
|
|
|
|
dest_current(begin),
|
|
|
|
dest_end(begin + size),
|
2019-06-13 14:04:38 +00:00
|
|
|
bits_buffer(0),
|
|
|
|
bits_count(0)
|
|
|
|
{}
|
|
|
|
|
|
|
|
~BitWriter()
|
|
|
|
{
|
|
|
|
flush();
|
|
|
|
}
|
|
|
|
|
2019-12-18 11:53:52 +00:00
|
|
|
// write `bits_to_write` low-bits of `value` to the buffer
|
|
|
|
inline void writeBits(UInt8 bits_to_write, UInt64 value)
|
2019-06-13 14:04:38 +00:00
|
|
|
{
|
2019-12-18 11:53:52 +00:00
|
|
|
UInt32 capacity = BIT_BUFFER_SIZE - bits_count;
|
|
|
|
if (capacity < bits_to_write)
|
2019-06-13 14:04:38 +00:00
|
|
|
{
|
|
|
|
doFlush();
|
2019-12-18 11:53:52 +00:00
|
|
|
capacity = BIT_BUFFER_SIZE - bits_count;
|
2019-06-13 14:04:38 +00:00
|
|
|
}
|
2019-12-18 11:53:52 +00:00
|
|
|
|
|
|
|
// write low bits of value as high bits of bits_buffer
|
|
|
|
const UInt64 mask = maskLowBits<UInt64>(bits_to_write);
|
|
|
|
BufferType v = value & mask;
|
|
|
|
v <<= capacity - bits_to_write;
|
|
|
|
|
|
|
|
bits_buffer |= v;
|
|
|
|
bits_count += bits_to_write;
|
2019-06-13 14:04:38 +00:00
|
|
|
}
|
|
|
|
|
2019-12-18 11:53:52 +00:00
|
|
|
// flush contents of bits_buffer to the dest_current, partial bytes are completed with zeroes.
|
2019-06-13 14:04:38 +00:00
|
|
|
inline void flush()
|
|
|
|
{
|
2019-12-18 11:53:52 +00:00
|
|
|
bits_count = (bits_count + 8 - 1) & ~(8 - 1); // align UP to 8-bytes, so doFlush will write ALL data from bits_buffer
|
|
|
|
while (bits_count != 0)
|
2019-06-13 14:04:38 +00:00
|
|
|
doFlush();
|
|
|
|
}
|
2019-06-03 09:56:50 +00:00
|
|
|
|
2019-11-29 12:29:32 +00:00
|
|
|
inline UInt64 count() const
|
|
|
|
{
|
|
|
|
return (dest_current - dest_begin) * 8 + bits_count;
|
|
|
|
}
|
|
|
|
|
2019-06-03 09:56:50 +00:00
|
|
|
private:
|
2019-06-13 14:04:38 +00:00
|
|
|
void doFlush()
|
|
|
|
{
|
2019-12-18 11:53:52 +00:00
|
|
|
// write whole bytes to the dest_current, leaving partial bits in bits_buffer
|
2019-11-29 12:29:32 +00:00
|
|
|
const size_t available = dest_end - dest_current;
|
2019-12-18 11:53:52 +00:00
|
|
|
const size_t to_write = std::min<size_t>(sizeof(UInt64), bits_count / 8); // align to 8-bit boundary
|
2019-11-29 12:29:32 +00:00
|
|
|
|
|
|
|
if (available < to_write)
|
|
|
|
{
|
|
|
|
throw Exception("Can not write past end of buffer. Space available "
|
|
|
|
+ std::to_string(available) + " bytes, required to write: "
|
|
|
|
+ std::to_string(to_write) + ".",
|
|
|
|
ErrorCodes::CANNOT_WRITE_AFTER_END_OF_BUFFER);
|
|
|
|
}
|
|
|
|
|
2019-12-18 11:53:52 +00:00
|
|
|
const auto tmp_buffer = htobe64(static_cast<UInt64>(bits_buffer >> (sizeof(bits_buffer) - sizeof(UInt64)) * 8));
|
|
|
|
memcpy(dest_current, &tmp_buffer, to_write);
|
2019-11-29 12:29:32 +00:00
|
|
|
dest_current += to_write;
|
2019-06-13 14:04:38 +00:00
|
|
|
|
2019-12-18 11:53:52 +00:00
|
|
|
bits_buffer <<= to_write * 8;
|
|
|
|
bits_count -= to_write * 8;
|
2019-06-13 14:04:38 +00:00
|
|
|
}
|
2019-06-03 09:56:50 +00:00
|
|
|
};
|
|
|
|
|
2019-06-13 14:04:38 +00:00
|
|
|
}
|