1
0
mirror of https://github.com/danog/libtgvoip.git synced 2024-12-02 09:37:52 +01:00
libtgvoip/tools/Buffers.cpp

263 lines
6.0 KiB
C++
Raw Normal View History

2020-01-22 12:43:51 +01:00
//
// libtgvoip is free and unencumbered public domain software.
// For more information, see http://unlicense.org or the UNLICENSE file
// you should have received with this source code distribution.
//
#include "tools/Buffers.h"
#include <assert.h>
#include <string.h>
#include <exception>
#include <stdexcept>
2020-03-10 19:36:27 +01:00
#include <cstdlib>
2020-01-22 12:43:51 +01:00
#include "tools/logging.h"
using namespace tgvoip;
#pragma mark - BufferInputStream
BufferInputStream::BufferInputStream(const unsigned char *data, const size_t _length) : buffer(data), length(_length)
{
}
2020-03-16 16:07:13 +01:00
BufferInputStream::BufferInputStream(const Buffer &buffer) : buffer(*buffer), length(buffer.Length())
2020-01-22 12:43:51 +01:00
{
}
2020-03-16 16:07:13 +01:00
void BufferInputStream::Seek(size_t offset) const
2020-01-22 12:43:51 +01:00
{
if (offset > length)
{
throw std::out_of_range("Not enough bytes in buffer");
}
this->offset = offset;
}
size_t BufferInputStream::GetLength() const
{
return length;
}
size_t BufferInputStream::GetOffset() const
{
return offset;
}
size_t BufferInputStream::Remaining() const
{
return length - offset;
}
2020-03-16 16:07:13 +01:00
unsigned char BufferInputStream::ReadByte() const
2020-01-22 12:43:51 +01:00
{
EnsureEnoughRemaining(1);
return (unsigned char)buffer[offset++];
}
2020-03-16 16:07:13 +01:00
int32_t BufferInputStream::ReadInt32() const
2020-01-22 12:43:51 +01:00
{
EnsureEnoughRemaining(4);
int32_t res = ((int32_t)buffer[offset] & 0xFF) |
(((int32_t)buffer[offset + 1] & 0xFF) << 8) |
(((int32_t)buffer[offset + 2] & 0xFF) << 16) |
(((int32_t)buffer[offset + 3] & 0xFF) << 24);
offset += 4;
return res;
}
2020-03-16 16:07:13 +01:00
int64_t BufferInputStream::ReadInt64() const
2020-01-22 12:43:51 +01:00
{
EnsureEnoughRemaining(8);
int64_t res = ((int64_t)buffer[offset] & 0xFF) |
(((int64_t)buffer[offset + 1] & 0xFF) << 8) |
(((int64_t)buffer[offset + 2] & 0xFF) << 16) |
(((int64_t)buffer[offset + 3] & 0xFF) << 24) |
(((int64_t)buffer[offset + 4] & 0xFF) << 32) |
(((int64_t)buffer[offset + 5] & 0xFF) << 40) |
(((int64_t)buffer[offset + 6] & 0xFF) << 48) |
(((int64_t)buffer[offset + 7] & 0xFF) << 56);
offset += 8;
return res;
}
2020-03-16 16:07:13 +01:00
int16_t BufferInputStream::ReadInt16() const
2020-01-22 12:43:51 +01:00
{
EnsureEnoughRemaining(2);
int16_t res = (uint16_t)buffer[offset] | ((uint16_t)buffer[offset + 1] << 8);
offset += 2;
return res;
}
2020-03-16 16:07:13 +01:00
uint32_t BufferInputStream::ReadTlLength() const
2020-01-22 12:43:51 +01:00
{
unsigned char l = ReadByte();
if (l < 254)
return l;
assert(length - offset >= 3);
EnsureEnoughRemaining(3);
2020-01-23 18:06:11 +01:00
uint32_t res = ((uint32_t)buffer[offset] & 0xFF) |
2020-01-25 16:04:52 +01:00
(((uint32_t)buffer[offset + 1] & 0xFF) << 8) |
(((uint32_t)buffer[offset + 2] & 0xFF) << 16);
2020-01-22 12:43:51 +01:00
offset += 3;
return res;
}
2020-03-16 16:07:13 +01:00
void BufferInputStream::ReadBytes(unsigned char *to, size_t count) const
2020-01-22 12:43:51 +01:00
{
EnsureEnoughRemaining(count);
memcpy(to, buffer + offset, count);
offset += count;
}
2020-03-16 16:07:13 +01:00
void BufferInputStream::ReadBytes(Buffer &to) const
2020-01-22 12:43:51 +01:00
{
ReadBytes(*to, to.Length());
}
2020-03-16 16:07:13 +01:00
BufferInputStream BufferInputStream::GetPartBuffer(size_t length, bool advance) const
2020-01-22 12:43:51 +01:00
{
EnsureEnoughRemaining(length);
BufferInputStream s = BufferInputStream(buffer + offset, length);
if (advance)
offset += length;
return s;
}
2020-03-16 16:07:13 +01:00
const unsigned char *BufferInputStream::GetRawBuffer() const
{
return buffer + offset;
}
2020-01-22 12:43:51 +01:00
2020-03-16 16:07:13 +01:00
void BufferInputStream::EnsureEnoughRemaining(size_t need) const
2020-01-22 12:43:51 +01:00
{
if (length - offset < need)
{
throw std::out_of_range("Not enough bytes in buffer");
}
}
#pragma mark - BufferOutputStream
2020-01-25 16:13:16 +01:00
BufferOutputStream::BufferOutputStream(size_t size_)
: size(size_),
bufferProvided(false)
2020-01-22 12:43:51 +01:00
{
2020-03-10 19:36:27 +01:00
buffer = reinterpret_cast<unsigned char *>(std::malloc(size_));
2020-01-25 16:13:16 +01:00
if (!buffer)
throw std::bad_alloc();
bufferProvided = false;
2020-01-22 12:43:51 +01:00
}
2020-01-25 16:13:16 +01:00
BufferOutputStream::BufferOutputStream(unsigned char *buffer_, size_t size_)
: buffer(buffer_),
size(size_),
bufferProvided(true)
2020-01-22 12:43:51 +01:00
{
}
BufferOutputStream::~BufferOutputStream()
{
2020-01-25 16:13:16 +01:00
if (!bufferProvided && buffer)
free(buffer);
2020-01-22 12:43:51 +01:00
}
void BufferOutputStream::WriteByte(unsigned char byte)
{
this->ExpandBufferIfNeeded(1);
buffer[offset++] = byte;
}
void BufferOutputStream::WriteInt32(int32_t i)
{
this->ExpandBufferIfNeeded(4);
buffer[offset + 3] = (unsigned char)((i >> 24) & 0xFF);
buffer[offset + 2] = (unsigned char)((i >> 16) & 0xFF);
buffer[offset + 1] = (unsigned char)((i >> 8) & 0xFF);
buffer[offset] = (unsigned char)(i & 0xFF);
offset += 4;
}
void BufferOutputStream::WriteInt64(int64_t i)
{
this->ExpandBufferIfNeeded(8);
buffer[offset + 7] = (unsigned char)((i >> 56) & 0xFF);
buffer[offset + 6] = (unsigned char)((i >> 48) & 0xFF);
buffer[offset + 5] = (unsigned char)((i >> 40) & 0xFF);
buffer[offset + 4] = (unsigned char)((i >> 32) & 0xFF);
buffer[offset + 3] = (unsigned char)((i >> 24) & 0xFF);
buffer[offset + 2] = (unsigned char)((i >> 16) & 0xFF);
buffer[offset + 1] = (unsigned char)((i >> 8) & 0xFF);
buffer[offset] = (unsigned char)(i & 0xFF);
offset += 8;
}
void BufferOutputStream::WriteInt16(int16_t i)
{
this->ExpandBufferIfNeeded(2);
buffer[offset + 1] = (unsigned char)((i >> 8) & 0xFF);
buffer[offset] = (unsigned char)(i & 0xFF);
offset += 2;
}
void BufferOutputStream::WriteBytes(const unsigned char *bytes, size_t count)
{
this->ExpandBufferIfNeeded(count);
memcpy(buffer + offset, bytes, count);
offset += count;
}
void BufferOutputStream::WriteBytes(const Buffer &buffer)
{
WriteBytes(*buffer, buffer.Length());
}
void BufferOutputStream::WriteBytes(const Buffer &buffer, size_t offset, size_t count)
{
if (offset + count > buffer.Length())
throw std::out_of_range("offset out of buffer bounds");
WriteBytes(*buffer + offset, count);
}
unsigned char *BufferOutputStream::GetBuffer()
{
return buffer;
}
size_t BufferOutputStream::GetLength()
{
return offset;
}
void BufferOutputStream::ExpandBufferIfNeeded(size_t need)
{
if (offset + need > size)
{
if (bufferProvided)
{
throw std::out_of_range("buffer overflow");
}
2020-03-10 19:36:27 +01:00
size += std::max(need, size_t{1024});
2020-03-16 16:07:13 +01:00
unsigned char *newBuffer = reinterpret_cast<unsigned char *>(std::realloc(buffer, size));
2020-03-10 19:36:27 +01:00
if (!newBuffer)
2020-01-22 12:43:51 +01:00
{
2020-03-10 19:36:27 +01:00
std::free(buffer);
buffer = nullptr;
size = 0;
2020-01-22 12:43:51 +01:00
throw std::bad_alloc();
2020-03-10 19:36:27 +01:00
}
buffer = newBuffer;
2020-01-22 12:43:51 +01:00
}
}
void BufferOutputStream::Reset()
{
offset = 0;
}
void BufferOutputStream::Rewind(size_t numBytes)
{
if (numBytes > offset)
throw std::out_of_range("buffer underflow");
offset -= numBytes;
}