#include #include #include #include #include #include class ByteBuffer { public: std::vector buff; unsigned int readpos; unsigned int writepos; // For bit reading unsigned int bit_readpos; ByteBuffer() { this->writepos = 0; this->readpos = 0; this->bit_readpos = 0; } ByteBuffer(unsigned char* data, unsigned int length) { this->writepos = 0; this->readpos = 0; this->bit_readpos = 0; this->Write(data, length); } ByteBuffer(int fill_size, char c) { this->buff.assign(fill_size, c); this->writepos = 0; this->readpos = 0; } ~ByteBuffer() { } void empty() { this->buff.clear(); this->writepos = 0; this->readpos = 0; this->bit_readpos = 0; } unsigned char* getBytes() { return &this->buff.at(0); } // Write Functions void Write(unsigned char* data, unsigned int length) { if (this->writepos + length > this->buff.size()) { // Resize the array to accomodate this->buff.resize(this->buff.size() + length, 0x00); } for(int i = 0; i < length; i++) { this->buff[this->writepos] = data[i]; this->writepos++; } } void WriteByte(int b) { unsigned char byte[1]; byte[0] = (char)b; this->Write(byte, 1); } void WriteShort(int s) { unsigned char sBytes[2]; sBytes[0] = (unsigned char)((s << 24) >> 24); sBytes[1] = (unsigned char)((s << 16) >> 24); this->Write(sBytes, 2); } void WriteLong(int i) { unsigned char iBytes[4]; iBytes[3] = (unsigned char)(i >> 24); iBytes[2] = (unsigned char)((i << 8) >> 24); iBytes[1] = (unsigned char)((i << 16) >> 24); iBytes[0] = (unsigned char)((i << 24) >> 24); this->Write(iBytes, 4); } void WriteString(std::string str) { this->Write((unsigned char *)str.c_str(), str.length()); if (str[str.length() - 1] != 0x0A) this->WriteByte(0); } // Read Functions unsigned char* Read(unsigned char* &buffer, int offset, unsigned int length = 0) { this->readpos += offset; if (!length) { return &this->buff.at(this->readpos); } memcpy(buffer, &this->buff.at(this->readpos), length); this->readpos += length; return buffer; } int ReadByte() { unsigned char buf; if (this->readpos >= this->buff.size()) { return -1; } buf = this->buff.at(this->readpos); this->readpos += 1; return (int)buf; } bool ReadBit() { // Get the byte that has this bit and the offset to it //int byte = this->buff.at(floor(double(this->bit_readpos / 8))); //unsigned short offset = this->bit_readpos % 8;# if (this->bit_readpos == 8) { this->readpos++; this->bit_readpos = 0; } int byte = this->buff.at(this->readpos); bool bit = ((byte >> this->bit_readpos) & 0x01); this->bit_readpos++; return bit; } int ReadBits(unsigned int length = 8) { int byte = 0; for(int i = 0; i < length; i++) { byte += ReadBit() << i; } return byte; } int ReadShort() { int s; s = (ReadByte() + (ReadByte() << 8)); return s; } int ReadLong() { int i; i = ReadByte() + (ReadByte() << 8) + (ReadByte() << 16) + (ReadByte() << 24); return i; } float ReadBitFloat() { union { unsigned char b[4]; float f; int l; } dat; dat.b[0] = this->ReadBits(8); dat.b[1] = this->ReadBits(8); dat.b[2] = this->ReadBits(8); dat.b[3] = this->ReadBits(8); return dat.f; } std::string ReadBitString() { char str[1024]; int l = 0; do { char c = this->ReadBits(8); if (c == -1 || c == 0) break; str[l] = c; l++; } while (l < sizeof(str)-1); str[l] = 0; return str; } float ReadFloat() { union { unsigned char b[4]; float f; int l; } dat; dat.b[0] = this->ReadByte(); dat.b[1] = this->ReadByte(); dat.b[2] = this->ReadByte(); dat.b[3] = this->ReadByte(); return dat.f; } std::string ReadString() { char str[1024]; int l = 0; do { char c = ReadByte(); if (c == -1 || c == 0) break; str[l] = c; l++; } while (l < sizeof(str)-1); str[l] = 0; return str; } void print_hex() { unsigned int offset = 0; std::string output; std::cout << "Offset 0 1 2 3 4 5 6 7 8 9 A B C D E F " << std::endl; for (unsigned int i = 0; i < buff.size(); i++) { if (i % 16 == 0) { // Output Text std::cout << " " << output << std::endl; // Output Offset std::cout << std::setfill('0') << std::setw(8) << std::hex << offset << " "; // Clear Output output.clear(); //std::cout << "Size: " << buff.size() << " On: " << i << std::endl; } // Output Hex Value std::cout << " " << std::setfill('0') << std::setw(2) << std::hex << (int)buff[i]; if (buff[i] >= 32 && buff[i] <= 126) { output += buff[i]; } else { output += "."; } offset += 16; } if (output.length() <= 16) { for (int i = 0; i < 16 - output.length(); i++) std::cout << " "; std::cout << " " << output << "\n"; } std::cout << std::dec << std::endl; } };