99 lines
2.0 KiB
C++
99 lines
2.0 KiB
C++
#include "buffer.h"
|
|
#include <iostream>
|
|
|
|
Buffer::Buffer()
|
|
{
|
|
buffer = nullptr;
|
|
size = 0;
|
|
taken = 0;
|
|
}
|
|
|
|
Buffer::Buffer(size_t size)
|
|
{
|
|
this->size = size;
|
|
this->buffer = new uint8_t[size];
|
|
}
|
|
|
|
Buffer::~Buffer()
|
|
{
|
|
if (buffer) delete[] buffer;
|
|
}
|
|
|
|
bool Buffer::resize(size_t new_size)
|
|
{
|
|
if (size >= new_size) return true;
|
|
uint8_t* new_buffer = (uint8_t*)realloc(buffer, new_size);
|
|
if (!new_buffer)
|
|
{
|
|
printf_s("Error resizing buffer\n");
|
|
return false;
|
|
}
|
|
buffer = new_buffer;
|
|
size = new_size;
|
|
return true;
|
|
}
|
|
|
|
int Buffer::add_end(uint8_t* data, size_t data_size)
|
|
{
|
|
if (taken + data_size > size)
|
|
if (!resize(size + data_size)) return -1;
|
|
|
|
memcpy_s(buffer + taken, size - taken, data, data_size);
|
|
taken += data_size;
|
|
return taken - data_size;
|
|
}
|
|
|
|
int Buffer::add_middle(uint8_t* data, size_t data_size, size_t index)
|
|
{
|
|
if (taken + data_size > size)
|
|
if (!resize(size + data_size)) return -1;
|
|
memmove_s(buffer + index + data_size, size - index - data_size, buffer + index, taken - index);
|
|
memcpy_s(buffer + index, size - index, data, data_size);
|
|
taken += data_size;
|
|
return index;
|
|
}
|
|
|
|
void Buffer::remove_fast(uint8_t* data, size_t data_size)
|
|
{
|
|
int64_t index = data - buffer;
|
|
if (index < 0 || index > taken || index + data_size > taken)
|
|
{
|
|
printf_s("Error removing from buffer\n");
|
|
return;
|
|
}
|
|
memmove_s(buffer + index, size - index, buffer + index + data_size, taken - index - data_size);
|
|
taken -= data_size;
|
|
}
|
|
|
|
void Buffer::remove(size_t index, size_t data_size)
|
|
{
|
|
if (index + data_size > taken)
|
|
{
|
|
printf_s("Error removing from buffer\n");
|
|
return;
|
|
}
|
|
memmove_s(buffer + index, size - index, buffer + index + data_size, taken - index - data_size);
|
|
taken -= data_size;
|
|
}
|
|
|
|
size_t Buffer::find(uint8_t* data, size_t data_size)
|
|
{
|
|
for (size_t i = 0; i < taken; i++)
|
|
if (memcmp(buffer + i, data, data_size) == 0)
|
|
return i;
|
|
|
|
return -1;
|
|
}
|
|
|
|
void Buffer::remove(uint8_t* data, size_t data_size)
|
|
{
|
|
size_t index = find(data, data_size);
|
|
if (index == -1)
|
|
{
|
|
printf_s("Error removing from buffer\n");
|
|
return;
|
|
}
|
|
remove(index, data_size);
|
|
}
|
|
|