mirror of
https://github.com/mfontanini/libtins
synced 2026-01-23 02:35:57 +01:00
Move memory helpers implementation into a source file
This commit is contained in:
@@ -34,13 +34,15 @@
|
||||
#include <cstring>
|
||||
#include <vector>
|
||||
#include "exceptions.h"
|
||||
#include "ip_address.h"
|
||||
#include "ipv6_address.h"
|
||||
#include "hw_address.h"
|
||||
#include "endianness.h"
|
||||
|
||||
namespace Tins {
|
||||
|
||||
class IPv4Address;
|
||||
class IPv6Address;
|
||||
template <size_t n>
|
||||
class HWAddress;
|
||||
|
||||
/**
|
||||
* \cond
|
||||
*/
|
||||
@@ -66,25 +68,8 @@ void write_value(uint8_t* buffer, const T& value) {
|
||||
|
||||
class InputMemoryStream {
|
||||
public:
|
||||
InputMemoryStream(const uint8_t* buffer, size_t total_sz)
|
||||
: buffer_(buffer), size_(total_sz) {
|
||||
}
|
||||
|
||||
InputMemoryStream(const std::vector<uint8_t>& data)
|
||||
: buffer_(&data[0]), size_(data.size()) {
|
||||
}
|
||||
|
||||
void skip(size_t size) {
|
||||
if (TINS_UNLIKELY(size > size_)) {
|
||||
throw malformed_packet();
|
||||
}
|
||||
buffer_ += size;
|
||||
size_ -= size;
|
||||
}
|
||||
|
||||
bool can_read(size_t byte_count) const {
|
||||
return TINS_LIKELY(size_ >= byte_count);
|
||||
}
|
||||
InputMemoryStream(const uint8_t* buffer, size_t total_sz);
|
||||
InputMemoryStream(const std::vector<uint8_t>& data);
|
||||
|
||||
template <typename T>
|
||||
T read() {
|
||||
@@ -112,58 +97,17 @@ public:
|
||||
skip(sizeof(value));
|
||||
}
|
||||
|
||||
void read(std::vector<uint8_t>& value, size_t count) {
|
||||
if (!can_read(count)) {
|
||||
throw malformed_packet();
|
||||
}
|
||||
value.assign(pointer(), pointer() + count);
|
||||
skip(count);
|
||||
}
|
||||
|
||||
void read(IPv4Address& address) {
|
||||
address = IPv4Address(read<uint32_t>());
|
||||
}
|
||||
|
||||
void read(IPv6Address& address) {
|
||||
if (!can_read(IPv6Address::address_size)) {
|
||||
throw malformed_packet();
|
||||
}
|
||||
address = pointer();
|
||||
skip(IPv6Address::address_size);
|
||||
}
|
||||
|
||||
template <size_t n>
|
||||
void read(HWAddress<n>& address) {
|
||||
if (!can_read(HWAddress<n>::address_size)) {
|
||||
throw malformed_packet();
|
||||
}
|
||||
address = pointer();
|
||||
skip(HWAddress<n>::address_size);
|
||||
}
|
||||
|
||||
void read(void* output_buffer, size_t output_buffer_size) {
|
||||
if (!can_read(output_buffer_size)) {
|
||||
throw malformed_packet();
|
||||
}
|
||||
read_data(buffer_, (uint8_t*)output_buffer, output_buffer_size);
|
||||
skip(output_buffer_size);
|
||||
}
|
||||
|
||||
const uint8_t* pointer() const {
|
||||
return buffer_;
|
||||
}
|
||||
|
||||
size_t size() const {
|
||||
return size_;
|
||||
}
|
||||
|
||||
void size(size_t new_size) {
|
||||
size_ = new_size;
|
||||
}
|
||||
|
||||
operator bool() const {
|
||||
return size_ > 0;
|
||||
}
|
||||
void skip(size_t size);
|
||||
bool can_read(size_t byte_count) const;
|
||||
void read(std::vector<uint8_t>& value, size_t count);
|
||||
void read(HWAddress<6>& address);
|
||||
void read(IPv4Address& address);
|
||||
void read(IPv6Address& address);
|
||||
void read(void* output_buffer, size_t output_buffer_size);
|
||||
const uint8_t* pointer() const;
|
||||
size_t size() const;
|
||||
void size(size_t new_size);
|
||||
operator bool() const;
|
||||
private:
|
||||
const uint8_t* buffer_;
|
||||
size_t size_;
|
||||
@@ -171,21 +115,8 @@ private:
|
||||
|
||||
class OutputMemoryStream {
|
||||
public:
|
||||
OutputMemoryStream(uint8_t* buffer, size_t total_sz)
|
||||
: buffer_(buffer), size_(total_sz) {
|
||||
}
|
||||
|
||||
OutputMemoryStream(std::vector<uint8_t>& buffer)
|
||||
: buffer_(&buffer[0]), size_(buffer.size()) {
|
||||
}
|
||||
|
||||
void skip(size_t size) {
|
||||
if (TINS_UNLIKELY(size > size_)) {
|
||||
throw malformed_packet();
|
||||
}
|
||||
buffer_ += size;
|
||||
size_ -= size;
|
||||
}
|
||||
OutputMemoryStream(uint8_t* buffer, size_t total_sz);
|
||||
OutputMemoryStream(std::vector<uint8_t>& buffer);
|
||||
|
||||
template <typename T>
|
||||
void write(const T& value) {
|
||||
@@ -216,38 +147,14 @@ public:
|
||||
skip(length);
|
||||
}
|
||||
|
||||
void write(const uint8_t* ptr, size_t length) {
|
||||
write(ptr, ptr + length);
|
||||
}
|
||||
|
||||
void write(const IPv4Address& address) {
|
||||
write(static_cast<uint32_t>(address));
|
||||
}
|
||||
|
||||
void write(const IPv6Address& address) {
|
||||
write(address.begin(), address.end());
|
||||
}
|
||||
|
||||
template <size_t n>
|
||||
void write(const HWAddress<n>& address) {
|
||||
write(address.begin(), address.end());
|
||||
}
|
||||
|
||||
void fill(size_t size, uint8_t value) {
|
||||
if (TINS_UNLIKELY(size_ < size)) {
|
||||
throw serialization_error();
|
||||
}
|
||||
std::memset(buffer_, value, size);
|
||||
skip(size);
|
||||
}
|
||||
|
||||
uint8_t* pointer() {
|
||||
return buffer_;
|
||||
}
|
||||
|
||||
size_t size() const {
|
||||
return size_;
|
||||
}
|
||||
void skip(size_t size);
|
||||
void write(const uint8_t* ptr, size_t length);
|
||||
void write(const HWAddress<6>& address);
|
||||
void write(const IPv4Address& address);
|
||||
void write(const IPv6Address& address);
|
||||
void fill(size_t size, uint8_t value);
|
||||
uint8_t* pointer();
|
||||
size_t size() const;
|
||||
private:
|
||||
uint8_t* buffer_;
|
||||
size_t size_;
|
||||
|
||||
Reference in New Issue
Block a user