CppBinaryStream is a C++ library that provides functionality for working with binary streams.
The main component of the API is the BinaryStream
class. This class provides functions for reading and writing binary data to a buffer. The following functions are available for writing data:
The byte order is specified by the big_endian
parameter.
If False
then the byte order is little endian.
write(value, big_endian = true)
: Writes a value to the buffer.writeFloat(value, big_endian = true)
: Writes a floating-point value to the buffer.writeString(value, big_endian = true)
: Writes a string to the buffer, preceded by a length value of typeT
.writeStringVarInt(value)
: Writes a string to the buffer, preceded by a varint32 length value.writeVarInt(value)
: Writes a varint value to the buffer.writeZigZag(value)
: Writes a zigzag-encoded integer to the buffer.writePadding(value, size)
: Writes a padding value to the buffer.writeBit(value, skip = false)
: Writes a single bit to the buffer. Theskip
parameter determines whether to skip to the next byte after writing the bit.writeOptional(value)
: Writes an optional value to the buffer.writeBits(value, size, big_endian = true)
: Writes a specified number of bits from a value to the buffer, starting from the most significant bit. The byte order is specified by thebig_endian
parameter.
The following functions are available for reading data:
read<T>(big_endian = true)
: Reads a value of typeT
from the buffer.readFloat(big_endian = true)
: Reads a floating-point value from the buffer.readString<T>(big_endian = true)
: Reads a string from the buffer, preceded by a length value of typeT
.readStringVarInt()
: Reads a string from the buffer, preceded by a varint32 length value.readVarInt<T>()
: Reads a varint value of typeT
from the buffer.readZigZag<T>()
: Reads a zigzag-encoded integer of typeT
from the buffer.readPadding(value, size)
: Reads a padding value from the buffer.readBit(skip = false)
: Reads a single bit from the buffer. Theskip
parameter determines whether to skip to the next byte after reading the bit.readOptional(value)
: Reads an optional value from the buffer.readBits<T>(size, big_endian = true)
: Reads a specified number of bits from the buffer, starting from the most significant bit.
The library uses CMake
as the build system. To build the library and the tests, follow these steps:
- Clone the repository
- Run
cmake .
- Run
cmake --build .
The library will be built as a shared library, and the tests will be built as an executable(see below).
The library comes with a set of tests to ensure its correctness. To build the tests using CMake, you can pass the -DBINARY_STREAM_COMPILE_TESTS=ON
flag.
cmake . -DBINARY_STREAM_COMPILE_TESTS=ON
If you find any issues or have suggestions for improvement, please open an issue or submit a pull request.
This project is licensed under the GNU General Public License v3.0.