SatCat5
satcat5::cbor::CborReader Class Reference

Detailed Description

Creates an ephemeral reader for the Concise Binary Object Representation (CBOR, IETF RFC8949), holding underlying objects and providing more concise member functions.

This reader wraps the QCBOR library into a more familiar interface that performs stack buffer allocation and io::Readable handling automatically. QCBOR decode requires a working buffer to hold the in-progress object, which should be allocated by a child class, typically cbor::ListReaderStatic or cbor::MapReaderStatic.

Most users should be able to hit target functionality with usage of cbor::ListReader cbor::MapReader or other future wrappers. However, the QCBOR decode context cbor is a public member variable that may be used for calls to QCBORDecode_* functions for complex use-cases outside the scope of provided wrapper classes.

Usage:

  • Create a CborReader object, which copies from any io::Readable sink and calls read_finalize(). If no io::Readable is passed, the given buffer is assumed to be populated with a CBOR payload.
  • Use member functions of a child class such as cbor::ListReader or cbor::MapReader to read a valid CBOR list or map, respectively.
  • For any edge cases, use QCBORDecode_* functions with the given QCBOR context cbor.

Most users should instantiate ListReaderStatic or MapReaderStatic instead of this to perform all stack buffer allocation.

See also
satcat5::cbor::ListReader, satcat5::cbor::MapReader

Definition at line 465 of file io_cbor.h.

#include <io_cbor.h>

Inheritance diagram for satcat5::cbor::CborReader:
[legend]

Public Member Functions

bool ok () const
 Check if any errors have been encountered yet during decoding.
 
QCBORError get_error () const
 Get the QCBOR decoding error, if any.
 
void close_list () const
 Resume parsing at the end of a nested list. More...
 
void close_map () const
 Resume parsing at the end of a nested dictionary. More...
 
bool copy_item (QCBOREncodeContext *dst)
 Copy the next CBOR item to the specified destination. More...
 
unsigned copy_all (QCBOREncodeContext *dst)
 Copy all remaining CBOR item(s) to the specified destination. More...
 

Public Attributes

QCBORDecodeContext *const cbor
 QCBOR context pointer.
 

Static Public Attributes

static const int ERR_NOT_FOUND = -1
 Key not found.
 
static const int ERR_OVERFLOW = -2
 Overflowed user array.
 
static const int ERR_BAD_TYPE = -3
 Non-matching type.
 
static const int ERR_QCBOR_INT = -4
 Misc QCBOR error.
 

Protected Member Functions

 CborReader (satcat5::io::Readable *src, QCBORDecodeContext *decode, u8 *buff, unsigned size)
 Opens a QCBOR Decode Context from an io::Readable. More...
 
constexpr CborReader (QCBORDecodeContext *decode)
 Create a CborReader from an existing decoder context.
 
 CborReader (const CborReader &other)=delete
 
CborReaderoperator= (const CborReader &other)=delete
 
int get_array_internal (satcat5::io::Writeable &dst, u8 qcbor_type, u8 type_size) const
 Internal function for shared array logic. More...
 
unsigned peek_integer_len (const u8 *rdptr) const
 Predict the length of the next integer value. More...
 

Protected Attributes

QCBORItem m_item
 QCBOR Decoder item.
 

Constructor & Destructor Documentation

◆ CborReader()

satcat5::cbor::CborReader::CborReader ( satcat5::io::Readable src,
QCBORDecodeContext *  decode,
u8 *  buff,
unsigned  size 
)
protected

Opens a QCBOR Decode Context from an io::Readable.

Constructor requires child class to provide a working buffer.

Parameters
srcReadable source to decode, or NULL if buffers are already populated.
decodePointer to an uninitialized QCBOR encoder object.
buffBacking buffer for QCBOR.
sizeBacking buffer size for QCBOR.

Definition at line 179 of file io_cbor.cc.

Member Function Documentation

◆ close_list()

void satcat5::cbor::CborReader::close_list ( ) const
inline

Resume parsing at the end of a nested list.

See also
ListReader::open_list, MapReader::open_list.

Definition at line 477 of file io_cbor.h.

◆ close_map()

void satcat5::cbor::CborReader::close_map ( ) const
inline

Resume parsing at the end of a nested dictionary.

See also
ListReader::open_map, MapReader::open_map.

Definition at line 482 of file io_cbor.h.

◆ copy_all()

unsigned satcat5::cbor::CborReader::copy_all ( QCBOREncodeContext *  dst)

Copy all remaining CBOR item(s) to the specified destination.

Returns
The number of items copied.

Definition at line 227 of file io_cbor.cc.

◆ copy_item()

bool satcat5::cbor::CborReader::copy_item ( QCBOREncodeContext *  dst)

Copy the next CBOR item to the specified destination.

If the next item is a nested data structure, this copies the entire data structure to the destination object.

Returns
True if an item was copied successfully.

Definition at line 194 of file io_cbor.cc.

◆ get_array_internal()

int satcat5::cbor::CborReader::get_array_internal ( satcat5::io::Writeable dst,
u8  qcbor_type,
u8  type_size 
) const
protected

Internal function for shared array logic.

This function is called by all other "get_array" variants.

Definition at line 520 of file io_cbor.cc.

◆ peek_integer_len()

unsigned satcat5::cbor::CborReader::peek_integer_len ( const u8 *  rdptr) const
protected

Predict the length of the next integer value.

Standard integers may be 1, 2, 3, 5, or 9 bytes.

Returns
Length in bytes, or zero on error.

Definition at line 237 of file io_cbor.cc.


The documentation for this class was generated from the following files: