123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368 |
- #ifdef _WIN32
- #include "stdafx.h"
- #endif
- #include "ByteBuffer.h"
- #ifdef BB_USE_NS
- namespace bb {
- #endif
- /**
- * ByteBuffer constructor
- * Reserves specified size in internal vector
- *
- * @param size Size (in bytes) of space to preallocate internally. Default is set in DEFAULT_SIZE
- */
- ByteBuffer::ByteBuffer(uint32_t size) {
- buf.reserve(size);
- clear();
- #ifdef BB_UTILITY
- name = "";
- #endif
- }
- /**
- * ByteBuffer constructor
- * Consume an entire uint8_t array of length len in the ByteBuffer
- *
- * @param arr uint8_t array of data (should be of length len)
- * @param size Size of space to allocate
- */
- ByteBuffer::ByteBuffer(uint8_t* arr, uint32_t size) {
- // If the provided array is NULL, allocate a blank buffer of the provided size
- if (arr == NULL) {
- buf.reserve(size);
- clear();
- } else { // Consume the provided array
- buf.reserve(size);
- clear();
- putBytes(arr, size);
- }
- #ifdef BB_UTILITY
- name = "";
- #endif
- }
- /**
- * Bytes Remaining
- * Returns the number of bytes from the current read position till the end of the buffer
- *
- * @return Number of bytes from rpos to the end (size())
- */
- uint32_t ByteBuffer::bytesRemaining() {
- return size() - rpos;
- }
- /**
- * Clear
- * Clears out all data from the internal vector (original preallocated size remains), resets the positions to 0
- */
- void ByteBuffer::clear() {
- rpos = 0;
- wpos = 0;
- buf.clear();
- }
- /**
- * Clone
- * Allocate an exact copy of the ByteBuffer on the heap and return a pointer
- *
- * @return A pointer to the newly cloned ByteBuffer. NULL if no more memory available
- */
- ByteBuffer* ByteBuffer::clone() {
- ByteBuffer* ret = new ByteBuffer(buf.size());
- // Copy data
- for (uint32_t i = 0; i < buf.size(); i++) {
- ret->put((uint8_t) get(i));
- }
- // Reset positions
- ret->setReadPos(0);
- ret->setWritePos(0);
- return ret;
- }
- /**
- * Equals, test for data equivilancy
- * Compare this ByteBuffer to another by looking at each byte in the internal buffers and making sure they are the same
- *
- * @param other A pointer to a ByteBuffer to compare to this one
- * @return True if the internal buffers match. False if otherwise
- */
- bool ByteBuffer::equals(ByteBuffer* other) {
- // If sizes aren't equal, they can't be equal
- if (size() != other->size())
- return false;
- // Compare byte by byte
- uint32_t len = size();
- for (uint32_t i = 0; i < len; i++) {
- if ((uint8_t) get(i) != (uint8_t) other->get(i))
- return false;
- }
- return true;
- }
- /**
- * Resize
- * Reallocates memory for the internal buffer of size newSize. Read and write positions will also be reset
- *
- * @param newSize The amount of memory to allocate
- */
- void ByteBuffer::resize(uint32_t newSize) {
- buf.resize(newSize);
- rpos = 0;
- wpos = 0;
- }
- /**
- * Size
- * Returns the size of the internal buffer...not necessarily the length of bytes used as data!
- *
- * @return size of the internal buffer
- */
- uint32_t ByteBuffer::size() {
- return buf.size();
- }
- // Replacement
- /**
- * Replace
- * Replace occurance of a particular uint8_t, key, with the uint8_t rep
- *
- * @param key uint8_t to find for replacement
- * @param rep uint8_t to replace the found key with
- * @param start Index to start from. By default, start is 0
- * @param firstOccuranceOnly If true, only replace the first occurance of the key. If false, replace all occurances. False by default
- */
- void ByteBuffer::replace(uint8_t key, uint8_t rep, uint32_t start, bool firstOccuranceOnly) {
- uint32_t len = buf.size();
- for (uint32_t i = start; i < len; i++) {
- uint8_t data = read<uint8_t>(i);
- // Wasn't actually found, bounds of buffer were exceeded
- if ((key != 0) && (data == 0))
- break;
- // Key was found in array, perform replacement
- if (data == key) {
- buf[i] = rep;
- if (firstOccuranceOnly)
- return;
- }
- }
- }
- // Read Functions
- uint8_t ByteBuffer::peek() const {
- return read<uint8_t>(rpos);
- }
- uint8_t ByteBuffer::get() const {
- return read<uint8_t>();
- }
- uint8_t ByteBuffer::get(uint32_t index) const {
- return read<uint8_t>(index);
- }
- void ByteBuffer::getBytes(uint8_t* buf, uint32_t len) const {
- for (uint32_t i = 0; i < len; i++) {
- buf[i] = read<uint8_t>();
- }
- }
- char ByteBuffer::getChar() const {
- return read<char>();
- }
- char ByteBuffer::getChar(uint32_t index) const {
- return read<char>(index);
- }
- double ByteBuffer::getDouble() const {
- return read<double>();
- }
- double ByteBuffer::getDouble(uint32_t index) const {
- return read<double>(index);
- }
- float ByteBuffer::getFloat() const {
- return read<float>();
- }
- float ByteBuffer::getFloat(uint32_t index) const {
- return read<float>(index);
- }
- uint32_t ByteBuffer::getInt() const {
- return read<uint32_t>();
- }
- uint32_t ByteBuffer::getInt(uint32_t index) const {
- return read<uint32_t>(index);
- }
- uint64_t ByteBuffer::getLong() const {
- return read<uint64_t>();
- }
- uint64_t ByteBuffer::getLong(uint32_t index) const {
- return read<uint64_t>(index);
- }
- uint16_t ByteBuffer::getShort() const {
- return read<uint16_t>();
- }
- uint16_t ByteBuffer::getShort(uint32_t index) const {
- return read<uint16_t>(index);
- }
- // Write Functions
- void ByteBuffer::put(ByteBuffer* src) {
- uint32_t len = src->size();
- for (uint32_t i = 0; i < len; i++)
- append<uint8_t>(src->get(i));
- }
- void ByteBuffer::put(uint8_t b) {
- append<uint8_t>(b);
- }
- void ByteBuffer::put(uint8_t b, uint32_t index) {
- insert<uint8_t>(b, index);
- }
- void ByteBuffer::putBytes(uint8_t* b, uint32_t len) {
- // Insert the data one byte at a time into the internal buffer at position i+starting index
- for (uint32_t i = 0; i < len; i++)
- append<uint8_t>(b[i]);
- }
- void ByteBuffer::putBytes(uint8_t* b, uint32_t len, uint32_t index) {
- wpos = index;
- // Insert the data one byte at a time into the internal buffer at position i+starting index
- for (uint32_t i = 0; i < len; i++)
- append<uint8_t>(b[i]);
- }
- void ByteBuffer::putChar(char value) {
- append<char>(value);
- }
- void ByteBuffer::putChar(char value, uint32_t index) {
- insert<char>(value, index);
- }
- void ByteBuffer::putDouble(double value) {
- append<double>(value);
- }
- void ByteBuffer::putDouble(double value, uint32_t index) {
- insert<double>(value, index);
- }
- void ByteBuffer::putFloat(float value) {
- append<float>(value);
- }
- void ByteBuffer::putFloat(float value, uint32_t index) {
- insert<float>(value, index);
- }
- void ByteBuffer::putInt(uint32_t value) {
- append<uint32_t>(value);
- }
- void ByteBuffer::putInt(uint32_t value, uint32_t index) {
- insert<uint32_t>(value, index);
- }
- void ByteBuffer::putLong(uint64_t value) {
- append<uint64_t>(value);
- }
- void ByteBuffer::putLong(uint64_t value, uint32_t index) {
- insert<uint64_t>(value, index);
- }
- void ByteBuffer::putShort(uint16_t value) {
- append<uint16_t>(value);
- }
- void ByteBuffer::putShort(uint16_t value, uint32_t index) {
- insert<uint16_t>(value, index);
- }
- // Utility Functions
- #ifdef BB_UTILITY
- void ByteBuffer::setName(std::string n) {
- name = n;
- }
- std::string ByteBuffer::getName() {
- return name;
- }
- void ByteBuffer::printInfo() {
- uint32_t length = buf.size();
- std::cout << "ByteBuffer " << name.c_str() << " Length: " << length << ". Info Print" << std::endl;
- }
- void ByteBuffer::printAH() {
- uint32_t length = buf.size();
- std::cout << "ByteBuffer " << name.c_str() << " Length: " << length << ". ASCII & Hex Print" << std::endl;
- for (uint32_t i = 0; i < length; i++) {
- std::printf("0x%02x ", buf[i]);
- }
- std::printf("\n");
- for (uint32_t i = 0; i < length; i++) {
- std::printf("%c ", buf[i]);
- }
- std::printf("\n");
- }
- void ByteBuffer::printAscii() {
- uint32_t length = buf.size();
- std::cout << "ByteBuffer " << name.c_str() << " Length: " << length << ". ASCII Print" << std::endl;
- for (uint32_t i = 0; i < length; i++) {
- std::printf("%c ", buf[i]);
- }
- std::printf("\n");
- }
- void ByteBuffer::printHex() {
- uint32_t length = buf.size();
- std::cout << "ByteBuffer " << name.c_str() << " Length: " << length << ". Hex Print" << std::endl;
- for (uint32_t i = 0; i < length; i++) {
- std::printf("0x%02x ", buf[i]);
- }
- std::printf("\n");
- }
- void ByteBuffer::printPosition() {
- uint32_t length = buf.size();
- std::cout << "ByteBuffer " << name.c_str() << " Length: " << length << " Read Pos: " << rpos << ". Write Pos: "
- << wpos << std::endl;
- }
- #ifdef BB_USE_NS
- }
- #endif
- #endif
|