TSDuck v3.40-4034
MPEG Transport Stream Toolkit
|
General-purpose memory buffer with bit access. More...
#include <tsBuffer.h>
Public Member Functions | |
Buffer (Buffer &&other) | |
Move constructor. | |
Buffer (const Buffer &other) | |
Copy constructor. | |
Buffer (const void *data, size_t size) | |
Constructor using a read-only external memory area which must remain valid as long as the Buffer object is used and not reset. | |
Buffer (size_t size=DEFAULT_SIZE) | |
Default constructor. | |
Buffer (void *data, size_t size, bool read_only=false) | |
Constructor using an external memory area which must remain valid as long as the Buffer object is used and not reset. | |
~Buffer () | |
Destructor. | |
bool | backBits (size_t bits) |
Skip read bits backward. | |
bool | backBytes (size_t bytes) |
Skip read bytes backward. | |
bool | canRead () const |
Check if we can still read from the buffer. | |
bool | canReadBits (size_t bits) const |
Check if we can read at least the specified number of bits from the buffer. | |
bool | canReadBytes (size_t bytes) const |
Check if we can read at least the specified number of bytes from the buffer. | |
bool | canWrite () const |
Check if we can still write in the buffer. | |
bool | canWriteBits (size_t bits) const |
Check if we can write at least the specified number of bits in the buffer. | |
bool | canWriteBytes (size_t bytes) const |
Check if we can write at least the specified number of bytes in the buffer. | |
size_t | capacity () const |
Get the maximum buffer size in bytes. | |
void | clearError () |
Clear all error states. | |
void | clearReadError () |
Clear the read error state. | |
void | clearUserError () |
Clear the user-generated error state. | |
void | clearWriteError () |
Clear the write error state. | |
const uint8_t * | currentReadAddress () const |
Get starting address of current read data (ignoring bit offset inside first byte to read). | |
size_t | currentReadBitOffset () const |
Get current read bit offset from the beginning of the buffer. | |
size_t | currentReadByteOffset () const |
Get current read byte index (ignoring bit offset inside bytes). | |
size_t | currentWriteBitOffset () const |
Get current write bit offset from the beginning of the buffer. | |
size_t | currentWriteByteOffset () const |
Get current write byte index (ignoring bit offset inside bytes). | |
const uint8_t * | data () const |
Get the current base address of the buffer. | |
bool | dropState (size_t level=NPOS) |
Drop the last saved state of the read/write streams from the stack of saved states. | |
bool | endOfRead () const |
Check end of read stream. | |
bool | endOfWrite () const |
Check end of write stream. | |
bool | error () const |
Check if there was any kind of error. | |
bool | externalMemory () const |
Check if the buffer is linked to some external memory area. | |
template<typename INT > requires std::integral<INT> | |
bool | getBCD (INT &value, size_t bcd_count) |
Read the next 4*n bits as a Binary Coded Decimal (BCD) value and advance the read pointer. | |
template<typename INT > requires std::integral<INT> | |
INT | getBCD (size_t bcd_count) |
Read the next 4*n bits as a Binary Coded Decimal (BCD) value and advance the read pointer. | |
uint8_t | getBit () |
Read the next bit and advance the read pointer. | |
template<class Rep , class Period > | |
void | getBits (cn::duration< Rep, Period > &value, size_t bits) |
Read the next n bits as a std::chrono::duration value and advance the read pointer. | |
template<typename INT > requires std::integral<INT> | |
void | getBits (INT &value, size_t bits) |
Read the next n bits as an integer value and advance the read pointer. | |
template<typename INT > requires std::integral<INT> | |
INT | getBits (size_t bits) |
Read the next n bits as an integer value and advance the read pointer. | |
template<typename INT > requires std::integral<INT> | |
void | getBits (std::optional< INT > &value, size_t bits) |
Read the next n bits as an integer value and advance the read pointer. | |
bool | getBool () |
Read the next bit as a boolean and advance the read pointer. | |
void | getBytes (ByteBlock &bb, size_t bytes=NPOS) |
Get bulk bytes from the buffer. | |
ByteBlock | getBytes (size_t bytes=NPOS) |
Get bulk bytes from the buffer. | |
size_t | getBytes (uint8_t *buffer, size_t bytes) |
Get bulk bytes from the buffer. | |
size_t | getBytesAppend (ByteBlock &bb, size_t bytes=NPOS) |
Get bulk bytes from the buffer. | |
ieee_float32_t | getFloat32 () |
Read the next 32 bits as an IEEE float value and advance the read pointer. | |
ieee_float64_t | getFloat64 () |
Read the next 64 bits as an IEEE float value and advance the read pointer. | |
int16_t | getInt16 () |
Read the next 16 bits as a signed integer value and advance the read pointer. | |
int32_t | getInt24 () |
Read the next 24 bits as a signed integer value and advance the read pointer. | |
int32_t | getInt32 () |
Read the next 32 bits as a signed integer value and advance the read pointer. | |
int64_t | getInt40 () |
Read the next 40 bits as a signed integer value and advance the read pointer. | |
int64_t | getInt48 () |
Read the next 48 bits as a signed integer value and advance the read pointer. | |
int64_t | getInt64 () |
Read the next 64 bits as a signed integer value and advance the read pointer. | |
int8_t | getInt8 () |
Read the next 8 bits as a signed integer value and advance the read pointer. | |
uint16_t | getUInt16 () |
Read the next 16 bits as an unsigned integer value and advance the read pointer. | |
uint32_t | getUInt24 () |
Read the next 24 bits as an unsigned integer value and advance the read pointer. | |
uint32_t | getUInt32 () |
Read the next 32 bits as an unsigned integer value and advance the read pointer. | |
uint64_t | getUInt40 () |
Read the next 40 bits as an unsigned integer value and advance the read pointer. | |
uint64_t | getUInt48 () |
Read the next 48 bits as an unsigned integer value and advance the read pointer. | |
uint64_t | getUInt64 () |
Read the next 64 bits as an unsigned integer value and advance the read pointer. | |
uint8_t | getUInt8 () |
Read the next 8 bits as an unsigned integer value and advance the read pointer. | |
UString | getUTF16 (size_t bytes=NPOS) |
Get a UTF-16 string. | |
bool | getUTF16 (UString &result, size_t bytes=NPOS) |
Get a UTF-16 string. | |
UString | getUTF16WithLength (size_t length_bits=8) |
Get a UTF-16 string (preceded by its length). | |
bool | getUTF16WithLength (UString &result, size_t length_bits=8) |
Get a UTF-16 string (preceded by its length). | |
UString | getUTF8 (size_t bytes=NPOS) |
Get a UTF-8 string. | |
bool | getUTF8 (UString &result, size_t bytes=NPOS) |
Get a UTF-8 string. | |
UString | getUTF8WithLength (size_t length_bits=8) |
Get a UTF-8 string (preceded by its length). | |
bool | getUTF8WithLength (UString &result, size_t length_bits=8) |
Get a UTF-8 string (preceded by its length). | |
bool | internalMemory () const |
Check if the buffer uses some internal private memory buffer. | |
bool | isBigEndian () const |
Check if read/write operations of integers use big endian representation. | |
bool | isLittleEndian () const |
Check if read/write operations of integers use little endian representation. | |
bool | isNativeEndian () const |
Check if read/write operations of integers use the native endian representation. | |
bool | isValid () const |
Check if the buffer is valid and contains some memory. | |
Buffer & | operator= (Buffer &&other) |
Move-assignment operator. | |
Buffer & | operator= (const Buffer &other) |
Assignment operator. | |
bool | popState (size_t level=NPOS) |
Pop the current state of the read/write streams from the stack of saved states and perform appropriate actions. | |
size_t | pushedLevels () const |
Get the current number of pushed states of the read/write streams. | |
size_t | pushReadSize (size_t size) |
Temporary reduce the readable size of the buffer. | |
size_t | pushReadSizeFromLength (size_t length_bits) |
Temporary reduce the readable size of the buffer using a length field from the stream. | |
size_t | pushState () |
Push the current state of the read/write streams on a stack of saved states. | |
size_t | pushWriteSequenceWithLeadingLength (size_t length_bits) |
Start a write sequence with a leading length field. | |
size_t | pushWriteSize (size_t size) |
Temporary reduce the writable size of the buffer. | |
template<typename INT > requires std::integral<INT> | |
bool | putBCD (INT value, size_t bcd_count) |
Put the next 4*n bits as a Binary Coded Decimal (BCD) value and advance the write pointer. | |
bool | putBit (uint8_t bit) |
Write the next bit and advance the write pointer. | |
template<class Rep , class Period > | |
bool | putBits (cn::duration< Rep, Period > value, size_t bits) |
Put the next n bits from a std::chrono::duration value and advance the write pointer. | |
template<typename INT > requires std::integral<INT> | |
bool | putBits (const std::optional< INT > &value, size_t bits) |
Put the next n bits from an integer value and advance the write pointer. | |
template<typename INT > requires std::integral<INT> | |
bool | putBits (INT value, size_t bits) |
Put the next n bits from an integer value and advance the write pointer. | |
size_t | putBytes (const ByteBlock &bb, size_t start=0, size_t count=NPOS) |
Put bulk bytes in the buffer. | |
size_t | putBytes (const uint8_t *buffer, size_t bytes) |
Put bytes in the buffer. | |
bool | putFixedUTF16 (const UString &str, size_t size, uint16_t pad=0, size_t start=0, size_t count=NPOS) |
Put a string using UTF-16 format with a fixed binary size (truncate or pad). | |
bool | putFixedUTF8 (const UString &str, size_t size, uint8_t pad=0, size_t start=0, size_t count=NPOS) |
Put a string using UTF-8 format with a fixed binary size (truncate or pad). | |
bool | putFloat32 (ieee_float32_t f) |
Write a 32-bit IEEE float value and advance the write pointer. | |
bool | putFloat64 (ieee_float64_t f) |
Write a 64-bit IEEE float value and advance the write pointer. | |
bool | putInt16 (int16_t i) |
Write a 16-bit signed integer value and advance the write pointer. | |
bool | putInt24 (int32_t i) |
Write a 24-bit signed integer value and advance the write pointer. | |
bool | putInt32 (int32_t i) |
Write a 32-bit signed integer value and advance the write pointer. | |
bool | putInt40 (int64_t i) |
Write a 40-bit signed integer value and advance the write pointer. | |
bool | putInt48 (int64_t i) |
Write a 48-bit signed integer value and advance the write pointer. | |
bool | putInt64 (int64_t i) |
Write a 64-bit signed integer value and advance the write pointer. | |
bool | putInt8 (int8_t i) |
Write an 8-bit signed integer value and advance the write pointer. | |
size_t | putPartialUTF16 (const UString &str, size_t start=0, size_t count=NPOS) |
Put a partial string using UTF-8 format. | |
size_t | putPartialUTF16WithLength (const UString &str, size_t start=0, size_t count=NPOS, size_t length_bits=8) |
Put a partial string (preceded by its length) using UTF-16 format. | |
size_t | putPartialUTF8 (const UString &str, size_t start=0, size_t count=NPOS) |
Put a partial string using UTF-8 format. | |
size_t | putPartialUTF8WithLength (const UString &str, size_t start=0, size_t count=NPOS, size_t length_bits=8) |
Put a partial string (preceded by its length) using UTF-8 format. | |
bool | putReserved (size_t bits) |
Serialize the number of reserved '1' bits. | |
bool | putReservedZero (size_t bits) |
Serialize the number of reserved '0' bits. | |
bool | putUInt16 (uint16_t i) |
Write a 16-bit unsigned integer value and advance the write pointer. | |
bool | putUInt24 (uint32_t i) |
Write a 24-bit unsigned integer value and advance the write pointer. | |
bool | putUInt32 (uint32_t i) |
Write a 32-bit unsigned integer value and advance the write pointer. | |
bool | putUInt40 (uint64_t i) |
Write a 40-bit unsigned integer value and advance the write pointer. | |
bool | putUInt48 (uint64_t i) |
Write a 48-bit unsigned integer value and advance the write pointer. | |
bool | putUInt64 (uint64_t i) |
Write a 64-bit unsigned integer value and advance the write pointer. | |
bool | putUInt8 (uint8_t i) |
Write an 8-bit unsigned integer value and advance the write pointer. | |
bool | putUTF16 (const UString &str, size_t start=0, size_t count=NPOS) |
Put a string using UTF-16 format. | |
bool | putUTF16WithLength (const UString &str, size_t start=0, size_t count=NPOS, size_t length_bits=8) |
Put a string (preceded by its length) using UTF-16 format. | |
bool | putUTF8 (const UString &str, size_t start=0, size_t count=NPOS) |
Put a string using UTF-8 format. | |
bool | putUTF8WithLength (const UString &str, size_t start=0, size_t count=NPOS, size_t length_bits=8) |
Put a string (preceded by its length) using UTF-8 format. | |
bool | readError () const |
Check if there was a read error. | |
bool | readIsByteAligned () const |
Check if the current read bit pointer is on a byte boundary. | |
bool | readOnly () const |
Check if the buffer is read-only. | |
bool | readRealignByte () |
Align the read pointer to the next byte boundary if not already aligned. | |
bool | readSeek (size_t byte, size_t bit=0) |
Reset reading at the specified offset in the buffer. | |
size_t | remainingReadBits () const |
Get number of remaining bits to read. | |
size_t | remainingReadBytes () const |
Get number of remaining bytes to read (ignoring bit offset inside bytes). | |
size_t | remainingWriteBits () const |
Get number of remaining bits to write. | |
size_t | remainingWriteBytes () const |
Get number of remaining bytes to write (ignoring bit offset inside bytes). | |
bool | reservedBitsError () const |
Check if there were "reserved bits errors". | |
UString | reservedBitsErrorString (size_t base_offset=0, const UString &margin=UString()) |
Return a string describing the "reserved bits errors". | |
void | reset (const void *data, size_t size) |
Reset the buffer using a read-only external memory area which must remain valid as long as the Buffer object is used and not reset. | |
void | reset (size_t size) |
Reset the buffer, remove link to any external memory, reallocate an internal buffer if necessary. | |
void | reset (void *data, size_t size, bool read_only=false) |
Reset the buffer using an external memory area which must remain valid as long as the Buffer object is used and not reset. | |
bool | resize (size_t size, bool reallocate) |
Change the usable size of the buffer. | |
void | setBigEndian () |
Specify that read/write operations of integers should use big endian representation. | |
void | setLittleEndian () |
Specify that read/write operations of integers should use little endian representation. | |
void | setNativeEndian () |
Specify that read/write operations of integers should use the native endian representation. | |
void | setUserError () |
Set the user-generated error state. | |
size_t | size () const |
Get the current buffer size in bytes. | |
bool | skipBits (size_t bits) |
Skip read bits forward. | |
bool | skipBytes (size_t bytes) |
Skip read bytes forward (ignoring bit offset inside bytes). | |
bool | skipReservedBits (size_t bits, int expected=1) |
Skip read reserved bits forward. | |
size_t | swapState () |
Swap the current state of the read/write streams with the one on top of the stack of saved states. | |
void | switchEndian () |
Switch the endianness of read/write operations of integers. | |
UString | tryGetASCII (size_t bytes=NPOS) |
Try to get an ASCII string. | |
bool | tryGetASCII (UString &result, size_t bytes=NPOS) |
Try to get an ASCII string. | |
bool | userError () const |
Check if there was a user-generated error. | |
bool | writeError () const |
Check if there was a write error. | |
bool | writeIsByteAligned () const |
Check if the current write bit pointer is on a byte boundary. | |
bool | writeRealignByte (int stuffing=0) |
Align the write pointer to the next byte boundary if not already aligned. | |
bool | writeSeek (size_t byte, size_t bit, uint8_t stuffing) |
Reset writing at the specified offset in the buffer and trash forward memory. | |
bool | writeSeek (size_t byte, size_t bit=0) |
Reset writing at the specified offset in the buffer. | |
Static Public Member Functions | |
static UString | ReservedBitsErrorString (std::vector< size_t > &errors, size_t base_offset=0, const UString &margin=UString()) |
This static method returns a string describing "reserved bits errors". | |
Protected Member Functions | |
uint8_t * | currentWriteAddress () const |
Get starting address of current write area (ignoring bit offset inside first byte to read). | |
void | setReadError () |
Set the read error state (reserved to subclasses). | |
void | setWriteError () |
Set the write error state (reserved to subclasses). | |
General-purpose memory buffer with bit access.
A buffer has the following properties:
In a read/write buffer, both read and write pointers initially point to the start of the buffer. Then, the read pointer always remains behind the write pointer. In other words, we can read only what was previously written. The application cannot write beyond the current maximum buffer size and cannot read beyond the current write pointer.
In a read-only buffer, the write pointer always points to the end of the buffer and cannot be changed.
Read and write pointers are composed of a byte offset from the beginning of the buffer and a bit offset (0 to 7) in this byte. In big endian mode (the default), bit 0 is the most significant bit (msb) and bit 7 is the least significant bit (lsb). In little endian mode, bit 0 is the lsb and bit 7 is the msb.
It is possible to read and write integer values of any number of bits, starting at any bit offset. Best performances are, of course, achieved on 8, 16, 32 and 64-bit integers starting at a byte boundary (bit offset 0).
The two read-error and write-error states are independent. They are most commonly set when trying to read or write past the write pointer or end of buffer, respectively. When some multi-byte data cannot be read or written, the corresponding error is set and the read or write pointer is left unmodified (no partial read or write).
Once the read error is set, all subsequent read operations will fail until the read error state is explicitly cleared. The same principle applies to write error state and write operations.
For example, consider that the remaining number of bytes to read is 2. Trying to read a 32-bit data fails and the read error is set. Attempting to read an 8-bit or 16-bit data will then fail because the read error is set, even though there are enough bytes to read.
Note: The principles of the C++ class ts::Buffer were freely inspired by the Java class java.nio.ByteBuffer
. There are differences between the two but the main principles are similar.
ts::Buffer::Buffer | ( | size_t | size = DEFAULT_SIZE | ) |
Default constructor.
The read and write index are at the beginning of the buffer. So, initially, there is nothing to read and the entire buffer to write.
[in] | size | Initial internal size in bytes of the buffer. |
ts::Buffer::Buffer | ( | void * | data, |
size_t | size, | ||
bool | read_only = false |
||
) |
Constructor using an external memory area which must remain valid as long as the Buffer object is used and not reset.
When read_only is true, the read index is at the beginning of the buffer and the write index is at the end of the buffer. When read_only is false, the read and write index are both at the beginning of the buffer.
[in] | data | Address of data area to use as memory buffer. |
[in] | size | Size in bytes of the data area. |
[in] | read_only | The buffer is read-only. |
ts::Buffer::Buffer | ( | const void * | data, |
size_t | size | ||
) |
Constructor using a read-only external memory area which must remain valid as long as the Buffer object is used and not reset.
The read index is at the beginning of the buffer and the write index is at the end of the buffer.
[in] | data | Address of data area to use as memory buffer. |
[in] | size | Size in bytes of the data area. |
ts::Buffer::Buffer | ( | const Buffer & | other | ) |
Copy constructor.
[in] | other | Other instance to copy. |
ts::Buffer::Buffer | ( | Buffer && | other | ) |
Move constructor.
[in,out] | other | Other instance to move. Reset and invalidated on return. |
Assignment operator.
[in] | other | Other instance to copy. |
Move-assignment operator.
[in,out] | other | Other instance to move. Reset and invalidated on return. |
bool ts::Buffer::isValid | ( | ) | const |
Check if the buffer is valid and contains some memory.
|
inline |
Check if the buffer is read-only.
|
inline |
Check if the buffer uses some internal private memory buffer.
|
inline |
Check if the buffer is linked to some external memory area.
|
inline |
Get the maximum buffer size in bytes.
|
inline |
Get the current buffer size in bytes.
|
inline |
Get the current base address of the buffer.
|
inline |
Specify that read/write operations of integers should use big endian representation.
The endianness of the buffer is not changed by the various reset() operations.
|
inline |
Specify that read/write operations of integers should use little endian representation.
The endianness of the buffer is not changed by the various reset() operations.
|
inline |
Specify that read/write operations of integers should use the native endian representation.
The endianness of the buffer is not changed by the various reset() operations.
|
inline |
Switch the endianness of read/write operations of integers.
The endianness of the buffer is not changed by the various reset() operations.
|
inline |
Check if read/write operations of integers use big endian representation.
|
inline |
Check if read/write operations of integers use little endian representation.
|
inline |
Check if read/write operations of integers use the native endian representation.
void ts::Buffer::reset | ( | size_t | size | ) |
Reset the buffer, remove link to any external memory, reallocate an internal buffer if necessary.
The read and write index are at the beginning of the buffer. So, initially, there is nothing to read and the entire buffer to write.
[in] | size | Internal size in bytes of the buffer. If an internal buffer already exists and is larger than the requested size, it is not shrunk. |
void ts::Buffer::reset | ( | void * | data, |
size_t | size, | ||
bool | read_only = false |
||
) |
Reset the buffer using an external memory area which must remain valid as long as the Buffer object is used and not reset.
When read_only is true, the read index is at the beginning of the buffer and the write index is at the end of the buffer. When read_only is false, the read and write index are both at the beginning of the buffer.
[in] | data | Address of data area to use as memory buffer. |
[in] | size | Size in bytes of the data area. |
[in] | read_only | The buffer is read-only. |
void ts::Buffer::reset | ( | const void * | data, |
size_t | size | ||
) |
Reset the buffer using a read-only external memory area which must remain valid as long as the Buffer object is used and not reset.
The read index is at the beginning of the buffer and the write index is at the end of the buffer.
[in] | data | Address of data area to use as memory buffer. |
[in] | size | Size in bytes of the data area. |
|
inline |
Check if there was a read error.
|
inline |
Check if there was a write error.
|
inline |
Check if there was a user-generated error.
|
inline |
Check if there was any kind of error.
|
inline |
Set the user-generated error state.
This can be used to indicate an application error such as invalid data format for instance.
bool ts::Buffer::readSeek | ( | size_t | byte, |
size_t | bit = 0 |
||
) |
Reset reading at the specified offset in the buffer.
Seeking past the write pointer moves the read pointer to the write pointer and generates a read error.
[in] | byte | Index of next byte to read. |
[in] | bit | Offset of next bit to read in this byte. |
bool ts::Buffer::writeSeek | ( | size_t | byte, |
size_t | bit = 0 |
||
) |
Reset writing at the specified offset in the buffer.
Seeking backward beyond the read pointer moves the write pointer to the read pointer and generates a write error. Seeking forward past the end of buffer moves the write pointer to the end of the buffer and generates a write error.
[in] | byte | Index of next byte to write. |
[in] | bit | Offset of next bit to write in this byte. |
bool ts::Buffer::writeSeek | ( | size_t | byte, |
size_t | bit, | ||
uint8_t | stuffing | ||
) |
Reset writing at the specified offset in the buffer and trash forward memory.
Seeking backward beyond the read pointer moves the write pointer to the read pointer and generates a write error. Seeking forward past the end of buffer moves the write pointer to the end of the buffer and generates a write error.
[in] | byte | Index of next byte to write. |
[in] | bit | Offset of next bit to write in this byte. |
[in] | stuffing | When seeking forward, byte value to write in skipped bytes. |
|
inline |
Check if the current read bit pointer is on a byte boundary.
|
inline |
Check if the current write bit pointer is on a byte boundary.
bool ts::Buffer::readRealignByte | ( | ) |
Align the read pointer to the next byte boundary if not already aligned.
Skip any bit in a partially read byte.
bool ts::Buffer::writeRealignByte | ( | int | stuffing = 0 | ) |
Align the write pointer to the next byte boundary if not already aligned.
Fill bits in a partially written byte with a know value.
[in] | stuffing | Bit value (must be 0 or 1) to write in skipped bits. |
bool ts::Buffer::skipBytes | ( | size_t | bytes | ) |
Skip read bytes forward (ignoring bit offset inside bytes).
[in] | bytes | Number of bytes to skip. |
bool ts::Buffer::skipBits | ( | size_t | bits | ) |
Skip read bits forward.
[in] | bits | Number of bits to skip. |
bool ts::Buffer::skipReservedBits | ( | size_t | bits, |
int | expected = 1 |
||
) |
Skip read reserved bits forward.
[in] | bits | Number of reserved bits to skip. |
[in] | expected | Expected value of each reserved bit. Must be 0 or 1, default is 1. For each reserved bit which does not have the expected value, a "reserved bit error" is logged. |
|
inline |
Check if there were "reserved bits errors".
|
inline |
Return a string describing the "reserved bits errors".
[in] | base_offset | Artificial base offset in bytes which is used to describe the placement of each bit error. When the Buffer instance describes the payload of a structure, specify the structure header size as base_offset in order to display each bit error relatively to the complete structure. |
[in] | margin | Prepend that string to each line. |
|
static |
This static method returns a string describing "reserved bits errors".
[in,out] | errors | A vector of error description. Each value is made of byte offset || bit offset (3 bits) || expected bit value (1 bit). The vector is sorted first. |
[in] | base_offset | Artificial base offset in bytes which is used to describe the placement of each bit error. When the Buffer instance describes the payload of a structure, specify the structure header size as base_offset in order to display each bit error relatively to the complete structure. |
[in] | margin | Prepend that string to each line. |
bool ts::Buffer::backBytes | ( | size_t | bytes | ) |
Skip read bytes backward.
[in] | bytes | Number of bytes to skip back. |
bool ts::Buffer::backBits | ( | size_t | bits | ) |
Skip read bits backward.
[in] | bits | Number of bits to skip back. |
|
inline |
Get starting address of current read data (ignoring bit offset inside first byte to read).
|
inline |
Get current read byte index (ignoring bit offset inside bytes).
|
inline |
Get current read bit offset from the beginning of the buffer.
|
inline |
Get current write byte index (ignoring bit offset inside bytes).
|
inline |
Get current write bit offset from the beginning of the buffer.
size_t ts::Buffer::remainingReadBytes | ( | ) | const |
Get number of remaining bytes to read (ignoring bit offset inside bytes).
size_t ts::Buffer::remainingReadBits | ( | ) | const |
Get number of remaining bits to read.
size_t ts::Buffer::remainingWriteBytes | ( | ) | const |
Get number of remaining bytes to write (ignoring bit offset inside bytes).
size_t ts::Buffer::remainingWriteBits | ( | ) | const |
Get number of remaining bits to write.
|
inline |
Check end of read stream.
|
inline |
Check end of write stream.
|
inline |
Check if we can still read from the buffer.
|
inline |
Check if we can read at least the specified number of bytes from the buffer.
[in] | bytes | Number of bytes. |
|
inline |
Check if we can read at least the specified number of bits from the buffer.
[in] | bits | Number of bits. |
|
inline |
Check if we can still write in the buffer.
|
inline |
Check if we can write at least the specified number of bytes in the buffer.
[in] | bytes | Number of bytes. |
|
inline |
Check if we can write at least the specified number of bits in the buffer.
[in] | bits | Number of bits. |
size_t ts::Buffer::pushState | ( | ) |
Push the current state of the read/write streams on a stack of saved states.
There is an internal stack of read/write states. It is possible to save the current state of the buffer, try to do some operations and then either restore (pop) the previous state if the attempted operations failed or drop the saved state and continue with the new state.
size_t ts::Buffer::pushReadSize | ( | size_t | size | ) |
Temporary reduce the readable size of the buffer.
The previous state is pushed to the internal stack of state and can be restored later. Saving the readable size temporarily changes the write pointer and sets the buffer as read only. When the state is restored using popState(), the previous readable size (write pointer) and read-only indicator are restored. The read pointer is set to the end of previous readable size.
[in] | size | New readable size in bytes of the buffer. In some cases, the final granted size can be different. The final value is bounded by the current read and write pointers. |
size_t ts::Buffer::pushReadSizeFromLength | ( | size_t | length_bits | ) |
Temporary reduce the readable size of the buffer using a length field from the stream.
An integer value is read from the stream (given the value size in bits). The read pointer must then be byte-aligned. Finally, pushReadSize() is called so that the remaining number of bytes to read is the length value that was just read.
[in] | length_bits | Size in bits of the length field to read. |
size_t ts::Buffer::pushWriteSize | ( | size_t | size | ) |
Temporary reduce the writable size of the buffer.
The previous state is pushed to the internal stack of state and can be restored later. Saving the writable size temporarily changes the end of buffer. When the state is restored using popState(), the previous end of buffer is restored. The read and write pointers are not restored (everything that was read or written in the meantime remain valid).
[in] | size | New writable size in bytes of the buffer. In some cases, the final granted size can be different. The final value is bounded by the current write pointer and end of buffer. |
size_t ts::Buffer::pushWriteSequenceWithLeadingLength | ( | size_t | length_bits | ) |
Start a write sequence with a leading length field.
The current state is pushed and the specified number of bits are skipped in the write field. The write stream must then be byte-aligned or an error is generated. Writing data can be continued by the application. When popState() is called, the size in bytes starting after the length field is then written in the length field.
[in] | length_bits | Size in bits of the length field to write. Must be in the range 1 to 64. |
size_t ts::Buffer::swapState | ( | ) |
Swap the current state of the read/write streams with the one on top of the stack of saved states.
The previous state must have been fully saved using pushState() only, not any other push method such as pushReadSize() or pushWriteSize(). Otherwise, the buffer is set in read and write error state.
As a result, the previously saved state is restored and the current state (just before restoring the saved state) is pushed. If there was no saved state, the current state is unchanged but still saved. So it is always safe to assume that the current state was saved.
bool ts::Buffer::popState | ( | size_t | level = NPOS | ) |
Pop the current state of the read/write streams from the stack of saved states and perform appropriate actions.
The new state depends on which method was used to push the previous state.
[in] | level | Saved level to restore. The default is NPOS, meaning the last saved state. Another inner level can be specified, in which case all outer levels are also popped. |
bool ts::Buffer::dropState | ( | size_t | level = NPOS | ) |
Drop the last saved state of the read/write streams from the stack of saved states.
[in] | level | Saved level to drop. The default is NPOS, meaning the last saved state. Another inner level can be specified, in which case the specified level and all outer levels are dropped. |
|
inline |
Get the current number of pushed states of the read/write streams.
bool ts::Buffer::resize | ( | size_t | size, |
bool | reallocate | ||
) |
Change the usable size of the buffer.
[in] | size | New usable size in bytes of the buffer. In some cases, the final granted size can be different:
|
[in] | reallocate | If true and the buffer is internally allocated, then reallocate the internal buffer to the final accepted size. |
uint8_t ts::Buffer::getBit | ( | ) |
Read the next bit and advance the read pointer.
|
inline |
Read the next bit as a boolean and advance the read pointer.
bool ts::Buffer::putBit | ( | uint8_t | bit | ) |
Write the next bit and advance the write pointer.
[in] | bit | The bit value (0 or 1). |
Read the next n bits as an integer value and advance the read pointer.
INT | An integer type for the result. |
[in] | bits | Number of bits to read. |
|
inline |
Read the next n bits as an integer value and advance the read pointer.
INT | An integer type for the result. |
[out] | value | The value of the next bits. |
[in] | bits | Number of bits to read. |
void ts::Buffer::getBits | ( | std::optional< INT > & | value, |
size_t | bits | ||
) |
Read the next n bits as an integer value and advance the read pointer.
INT | An integer type for the result. |
[out] | value | The value of the next bits as a std::optional instance. If no integer can be read, the std::optional is unset. |
[in] | bits | Number of bits to read. |
void ts::Buffer::getBits | ( | cn::duration< Rep, Period > & | value, |
size_t | bits | ||
) |
Read the next n bits as a std::chrono::duration value and advance the read pointer.
[out] | value | The value of the next bits. |
[in] | bits | Number of bits to read. |
bool ts::Buffer::putBits | ( | INT | value, |
size_t | bits | ||
) |
|
inline |
Put the next n bits from an integer value and advance the write pointer.
INT | An integer type. |
[in] | value | Integer value to write as a std::optional instance. If value is unset, nothing is written. |
[in] | bits | Number of bits to write. |
|
inline |
Put the next n bits from a std::chrono::duration value and advance the write pointer.
[in] | value | Integer value to write. |
[in] | bits | Number of bits to write. |
bool ts::Buffer::putReserved | ( | size_t | bits | ) |
Serialize the number of reserved '1' bits.
[in] | bits | Number of reserved '1' bits to write. |
|
inline |
Serialize the number of reserved '0' bits.
[in] | bits | Number of reserved '0' bits to write. |
size_t ts::Buffer::getBytes | ( | uint8_t * | buffer, |
size_t | bytes | ||
) |
Get bulk bytes from the buffer.
The bit aligment is ignored, reading starts at the current read byte pointer, even if a few bits were already read from that byte.
[out] | buffer | Address of the buffer receiving the read bytes. |
[in] | bytes | Number of bytes to read. |
Get bulk bytes from the buffer.
The bit aligment is ignored, reading starts at the current read byte pointer, even if a few bits were already read from that byte.
[in] | bytes | Number of bytes to read. If specified as NPOS, return all remaining bytes. |
Get bulk bytes from the buffer.
The bit aligment is ignored, reading starts at the current read byte pointer, even if a few bits were already read from that byte.
[out] | bb | Byte block receiving the read bytes. |
[in] | bytes | Number of bytes to read. If specified as NPOS, return all remaining bytes. |
Get bulk bytes from the buffer.
The bit aligment is ignored, reading starts at the current read byte pointer, even if a few bits were already read from that byte.
[in,out] | bb | Byte block receiving the read bytes. The read data are appended to bb. |
[in] | bytes | Number of bytes to read. If specified as NPOS, return all remaining bytes. |
size_t ts::Buffer::putBytes | ( | const uint8_t * | buffer, |
size_t | bytes | ||
) |
Put bytes in the buffer.
[in] | buffer | Address of the data to write. |
[in] | bytes | Number of bytes to write. |
Put bulk bytes in the buffer.
The bit aligment is ignored, writing starts at the current write byte pointer, even if a few bits were already written in that byte.
[in] | bb | Byte block containing the data to write. |
[in] | start | Start in index in bb. |
[in] | count | Number of bytes to write. |
|
inline |
Read the next 8 bits as an unsigned integer value and advance the read pointer.
Set the read error flag if there are not enough bits to read.
|
inline |
Read the next 16 bits as an unsigned integer value and advance the read pointer.
Set the read error flag if there are not enough bits to read.
|
inline |
Read the next 24 bits as an unsigned integer value and advance the read pointer.
Set the read error flag if there are not enough bits to read.
|
inline |
Read the next 32 bits as an unsigned integer value and advance the read pointer.
Set the read error flag if there are not enough bits to read.
|
inline |
Read the next 40 bits as an unsigned integer value and advance the read pointer.
Set the read error flag if there are not enough bits to read.
|
inline |
Read the next 48 bits as an unsigned integer value and advance the read pointer.
Set the read error flag if there are not enough bits to read.
|
inline |
Read the next 64 bits as an unsigned integer value and advance the read pointer.
Set the read error flag if there are not enough bits to read.
|
inline |
Read the next 8 bits as a signed integer value and advance the read pointer.
Set the read error flag if there are not enough bits to read.
|
inline |
Read the next 16 bits as a signed integer value and advance the read pointer.
Set the read error flag if there are not enough bits to read.
|
inline |
Read the next 24 bits as a signed integer value and advance the read pointer.
Set the read error flag if there are not enough bits to read.
|
inline |
Read the next 32 bits as a signed integer value and advance the read pointer.
Set the read error flag if there are not enough bits to read.
|
inline |
Read the next 40 bits as a signed integer value and advance the read pointer.
Set the read error flag if there are not enough bits to read.
|
inline |
Read the next 48 bits as a signed integer value and advance the read pointer.
Set the read error flag if there are not enough bits to read.
|
inline |
Read the next 64 bits as a signed integer value and advance the read pointer.
Set the read error flag if there are not enough bits to read.
|
inline |
Read the next 32 bits as an IEEE float value and advance the read pointer.
Set the read error flag if there are not enough bits to read.
|
inline |
Read the next 64 bits as an IEEE float value and advance the read pointer.
Set the read error flag if there are not enough bits to read.
|
inline |
Write an 8-bit unsigned integer value and advance the write pointer.
[in] | i | 8-bit unsigned integer value to write. |
|
inline |
Write a 16-bit unsigned integer value and advance the write pointer.
[in] | i | 16-bit unsigned integer value to write. |
|
inline |
Write a 24-bit unsigned integer value and advance the write pointer.
[in] | i | 24-bit unsigned integer value to write. |
|
inline |
Write a 32-bit unsigned integer value and advance the write pointer.
[in] | i | 32-bit unsigned integer value to write. |
|
inline |
Write a 40-bit unsigned integer value and advance the write pointer.
[in] | i | 40-bit unsigned integer value to write. |
|
inline |
Write a 48-bit unsigned integer value and advance the write pointer.
[in] | i | 48-bit unsigned integer value to write. |
|
inline |
Write a 64-bit unsigned integer value and advance the write pointer.
[in] | i | 64-bit unsigned integer value to write. |
|
inline |
Write an 8-bit signed integer value and advance the write pointer.
[in] | i | 8-bit signed integer value to write. |
|
inline |
Write a 16-bit signed integer value and advance the write pointer.
[in] | i | 16-bit signed integer value to write. |
|
inline |
Write a 24-bit signed integer value and advance the write pointer.
[in] | i | 24-bit signed integer value to write. |
|
inline |
Write a 32-bit signed integer value and advance the write pointer.
[in] | i | 32-bit signed integer value to write. |
|
inline |
Write a 40-bit signed integer value and advance the write pointer.
[in] | i | 40-bit signed integer value to write. |
|
inline |
Write a 48-bit signed integer value and advance the write pointer.
[in] | i | 48-bit signed integer value to write. |
|
inline |
Write a 64-bit signed integer value and advance the write pointer.
[in] | i | 64-bit signed integer value to write. |
|
inline |
Write a 32-bit IEEE float value and advance the write pointer.
[in] | f | 32-bit IEEE float value to write. |
|
inline |
Write a 64-bit IEEE float value and advance the write pointer.
[in] | f | 32-bit IEEE float value to write. |
Read the next 4*n bits as a Binary Coded Decimal (BCD) value and advance the read pointer.
If an invalid BCD digit is found, the read error state of the buffer is set after reading all BCD digits.
INT | An integer type. |
[in] | bcd_count | Number of BCD digits (bcd_count * 4 bits). |
bool ts::Buffer::getBCD | ( | INT & | value, |
size_t | bcd_count | ||
) |
Read the next 4*n bits as a Binary Coded Decimal (BCD) value and advance the read pointer.
If an invalid BCD digit is found, the read error state of the buffer is set after reading all BCD digits.
INT | An integer type. |
[out] | value | The decoded BCD value or zero in case of error. |
[in] | bcd_count | Number of BCD digits (bcd_count * 4 bits). |
bool ts::Buffer::putBCD | ( | INT | value, |
size_t | bcd_count | ||
) |
Put the next 4*n bits as a Binary Coded Decimal (BCD) value and advance the write pointer.
INT | An integer type. |
[in] | value | Integer value to write. |
[in] | bcd_count | Number of BCD digits (bcd_count * 4 bits). |
Try to get an ASCII string.
The read-pointer must be byte-aligned. If all bytes are valid ASCII characters (optionally zero-padded), the corresponding string is returned and the read pointer is moved. If the corresponding area is not a valid ASCII string (optionally zero-padded), do not move the read pointer and return an empty string.
[out] | result | Returned ASCII string. If the binary area is zero-padded, the trailing zeroes are not included in the string. This means that result can be shorter than bytes. |
[in] | bytes | Size in bytes of the string. If specified as NPOS (the default), read up to the end of the buffer. If different from NPOS, the exact number of bytes must be available or a read error is generated. |
Try to get an ASCII string.
The read-pointer must be byte-aligned. If all bytes are valid ASCII characters (optionally zero-padded), the corresponding string is returned and the read pointer is moved. If the corresponding area is not a valid ASCII string (optionally zero-padded), do not move the read pointer and return an empty string.
[in] | bytes | Size in bytes of the string. If specified as NPOS (the default), read up to the end of the buffer. If different from NPOS, the exact number of bytes must be available or a read error is generated. |
Get a UTF-8 string.
The read-pointer must be byte-aligned.
[out] | result | Returned decoded string. |
[in] | bytes | Size in bytes of the encoded UTF-8 string. If specified as NPOS (the default), read up to the end of the buffer. If different from NPOS, the exact number of bytes must be available or a read error is generated. |
Get a UTF-8 string.
The read-pointer must be byte-aligned.
[in] | bytes | Size in bytes of the encoded UTF-8 string. If specified as NPOS (the default), read up to the end of the buffer. If different from NPOS, the exact number of bytes must be available or a read error is generated. |
|
inline |
Get a UTF-8 string (preceded by its length).
The read-pointer must be byte-aligned after reading the length-field. The specified number of bytes must be available or a read error is generated.
[out] | result | Returned decoded string. |
[in] | length_bits | Size in bits in the length field. |
|
inline |
Get a UTF-8 string (preceded by its length).
The read-pointer must be byte-aligned after reading the length-field. The specified number of bytes must be available or a read error is generated.
[in] | length_bits | Size in bits in the length field. |
Get a UTF-16 string.
The read-pointer must be byte-aligned.
[out] | result | Returned decoded string. |
[in] | bytes | Size in bytes of the encoded UTF-16 string. If specified as NPOS (the default), read up to the end of the buffer. If different from NPOS, the exact number of bytes must be available or a read error is generated. If bytes is an odd value, the last byte is skipped and ignored. |
Get a UTF-16 string.
The read-pointer must be byte-aligned.
[in] | bytes | Size in bytes of the encoded UTF-16 string. If specified as NPOS (the default), read up to the end of the buffer. If different from NPOS, the exact number of bytes must be available or a read error is generated. If bytes is an odd value, the last byte is skipped and ignored. |
|
inline |
Get a UTF-16 string (preceded by its length).
The read-pointer must be byte-aligned after reading the length-field. The specified number of bytes must be available or a read error is generated. If the extracted length is an odd value, the last byte is skipped and ignored.
[out] | result | Returned decoded string. |
[in] | length_bits | Size in bits in the length field. |
|
inline |
Get a UTF-16 string (preceded by its length).
The read-pointer must be byte-aligned after reading the length-field. The specified number of bytes must be available or a read error is generated. If the extracted length is an odd value, the last byte is skipped and ignored.
[in] | length_bits | Size in bits in the length field. |
Put a string using UTF-8 format.
The write-pointer must be byte-aligned. Generate a write error when the buffer is full before writing the complete string.
[in] | str | The UTF-16 string to encode. |
[in] | start | Starting offset to convert in this UTF-16 string. |
[in] | count | Maximum number of characters to convert. |
|
inline |
Put a string using UTF-8 format with a fixed binary size (truncate or pad).
The write-pointer must be byte-aligned. Generate a write error when the buffer is full before writing the complete string.
[in] | str | The UTF-16 string to encode. |
[in] | size | Fixed size in bytes to fill. If str cannot be fully serialized, it is truncated. |
[in] | pad | It str does not fill size bytes, pad the remaining bytes with this value. |
[in] | start | Starting offset to convert in this UTF-16 string. |
[in] | count | Maximum number of characters to convert. |
|
inline |
Put a partial string using UTF-8 format.
The write-pointer must be byte-aligned. Stop either when this string is serialized or when the buffer is full, whichever comes first. Do not generate a write error when the buffer is full.
[in] | str | The UTF-16 string to encode. |
[in] | start | Starting offset to convert in this UTF-16 string. |
[in] | count | Maximum number of characters to convert. |
|
inline |
Put a string (preceded by its length) using UTF-8 format.
The write-pointer must be byte-aligned after writing the length-field. Generate a write error when the buffer is full before writing the complete string.
[in] | str | The UTF-16 string to encode. |
[in] | start | Starting offset to convert in this UTF-16 string. |
[in] | count | Maximum number of characters to convert. |
[in] | length_bits | Size in bits in the length field. |
|
inline |
Put a partial string (preceded by its length) using UTF-8 format.
The write-pointer must be byte-aligned after writing the length-field. Do not generate a write error when the buffer is full.
[in] | str | The UTF-16 string to encode. |
[in] | start | Starting offset to convert in this UTF-16 string. |
[in] | count | Maximum number of characters to convert. |
[in] | length_bits | Size in bits in the length field. |
Put a string using UTF-16 format.
The write-pointer must be byte-aligned. Generate a write error when the buffer is full before writing the complete string.
[in] | str | The UTF-16 string to encode. |
[in] | start | Starting offset to convert in this UTF-16 string. |
[in] | count | Maximum number of characters to convert. |
|
inline |
Put a string using UTF-16 format with a fixed binary size (truncate or pad).
The write-pointer must be byte-aligned. Generate a write error when the buffer is full before writing the complete string.
[in] | str | The UTF-16 string to encode. |
[in] | size | Fixed size in bytes to fill. If str cannot be fully serialized, it is truncated. |
[in] | pad | It str does not fill size bytes, serialize the remaining UTF-16 characters with this value. If size has an odd value, the last byte is padded with the least significant byte of pad. |
[in] | start | Starting offset to convert in this UTF-16 string. |
[in] | count | Maximum number of characters to convert. |
|
inline |
Put a partial string using UTF-8 format.
The write-pointer must be byte-aligned. Stop either when this string is serialized or when the buffer is full, whichever comes first. Do not generate a write error when the buffer is full.
[in] | str | The UTF-16 string to encode. |
[in] | start | Starting offset to convert in this UTF-16 string. |
[in] | count | Maximum number of characters to convert. |
|
inline |
Put a string (preceded by its length) using UTF-16 format.
The write-pointer must be byte-aligned after writing the length-field. Generate a write error when the buffer is full before writing the complete string.
[in] | str | The UTF-16 string to encode. |
[in] | start | Starting offset to convert in this UTF-16 string. |
[in] | count | Maximum number of characters to convert. |
[in] | length_bits | Size in bits in the length field. |
|
inline |
Put a partial string (preceded by its length) using UTF-16 format.
The write-pointer must be byte-aligned after writing the length-field. Do not generate a write error when the buffer is full.
[in] | str | The UTF-16 string to encode. |
[in] | start | Starting offset to convert in this UTF-16 string. |
[in] | count | Maximum number of characters to convert. |
[in] | length_bits | Size in bits in the length field. |
|
inlineprotected |
Get starting address of current write area (ignoring bit offset inside first byte to read).
This operation is reserved to subclasses. Applications can only get a read-only pointer to the current read area (see currentReadAddress()).