Repository navigation
CReader
#include <certpp/asn1/reader.hpp> --- certpp::asn1
Sequentially reads ASN.1 tag-length-values from an IStream (or an in-memory span), pairing CDecoder's content-level decode*() methods with stream/cursor management.
Constructed from an IStream, a CReader keeps that stream and pulls from it lazily: it reads more bytes into an internal buffer only when a read actually needs data it doesn't already have buffered, rather than reading the whole stream up front (data written to the stream after construction, but before it's needed, is still visible -- CReader never assumes it has already seen everything the stream will ever produce). CDecoder's decoders need a contiguous in-memory span regardless (and BER/CER's indefinite-length form requires scanning ahead), so growing that buffer is unavoidable; it just happens on demand instead of eagerly. Constructed from a span directly (e.g. a parent element's content, for descending into a SEQUENCE/SET -- see readSequence()/readSet()/readConstructed()), there's no stream to grow from, so it simply aliases the span. Because of the owned, on-demand- reallocated buffer a stream-backed CReader has, it is move-only: copying would leave the copy's cursor pointing into the source's buffer instead of its own.
Every typed read*() method reads and decodes the next element as a specific, plain UNIVERSAL, primitive tag -- if the next element's tag doesn't match (including a constructed value, since none of CDecoder's content-level decoders reassemble constructed/segmented BER/CER strings) or its content fails to decode, the read fails and the cursor is left exactly where it was, so the caller can try a different read or treat the field as absent (e.g. an OPTIONAL/DEFAULT X.509 field). For IMPLICIT-tagged or otherwise non-universal-tagged content, use the lower-level readNextElement() and call CDecoder's content-level decode*() directly on the returned content.
Constructs an empty CReader (atEnd() is immediately true).
Constructs a CReader that reads from an in-memory span directly (aliased, not copied).
-
content--- The span to read from. -
ruleSet--- The ASN.1 encoding rule set to use.
Constructs a CReader that reads from an IStream, from its current position onward. No data is read from stream yet at this point -- see the class comment.
-
stream--- The stream to read from. -
ruleSet--- The ASN.1 encoding rule set to use.
Checks whether the reader is at the end of its content: nothing left buffered, and (for a stream-backed reader) the stream itself reports no more data either.
Returns the not-yet-consumed remainder of what's currently buffered. For a stream-backed reader, this does not include data the stream could still produce but that hasn't been read into the buffer yet.
Reads the next encoded ASN.1 value, advancing the cursor past it. The low-level primitive every other read*() method builds on; use this directly for IMPLICIT-tagged or CHOICE content the typed methods can't recognize by their plain universal tag.
-
outTag--- The output tag of the encoded value. -
outArea--- The output span containing the encoded value's content.
Returns: True if a value was successfully read; false on a parse error or if the reader was already at the end (the cursor is left unchanged either way).
Reads the next element expecting a constructed value, returning a nested CReader over its content for the caller to descend into. Unlike the typed read*() methods, this accepts any tag class/value as long as the value is constructed -- the caller checks outTag themselves (e.g. to distinguish a context-specific [n] EXPLICIT wrapper).
-
outTag--- The output tag of the constructed value. -
outReader--- A nested reader over the constructed value's content.
Returns: True on success; otherwise, false (cursor unchanged).
Reads the next element expecting a SEQUENCE (universal, constructed), returning a nested CReader over its content for the caller to iterate.
-
outReader--- A nested reader over the SEQUENCE's content.
Returns: True on success; otherwise, false (cursor unchanged).
Reads the next element expecting a SET (universal, constructed), returning a nested CReader over its content for the caller to iterate. Doesn't re-validate CER/DER's canonical child ordering; that's an encoder-side (CWriter-side) concern.
-
outReader--- A nested reader over the SET's content.
Returns: True on success; otherwise, false (cursor unchanged).
Reads the next element expecting a BOOLEAN (universal, primitive).
-
outValue--- The decoded value.
Returns: True on success; otherwise, false (cursor unchanged).
Reads the next element expecting an INTEGER (universal, primitive).
-
outValue--- The decoded value.
Returns: True on success; otherwise, false (cursor unchanged).
Reads the next element expecting an ENUMERATED (universal, primitive).
-
outValue--- The decoded value.
Returns: True on success; otherwise, false (cursor unchanged).
Reads the next element expecting a NULL (universal, primitive).
Returns: True on success; otherwise, false (cursor unchanged).
Reads the next element expecting an OCTET STRING (universal, primitive). Rejects a constructed (segmented) encoding, since decodeOctetString() doesn't reassemble one.
-
outValue--- The decoded value (aliases into the reader's own buffer/span).
Returns: True on success; otherwise, false (cursor unchanged).
Reads the next element expecting an OCTET STRING (universal, primitive) into an owning COctet, copying the content rather than aliasing it.
-
outValue--- The decoded value.
Returns: True on success; otherwise, false (cursor unchanged).
Reads the next element expecting a BIT STRING (universal, primitive). Rejects a constructed (segmented) encoding, since decodeBitString() doesn't reassemble one.
-
outBits--- The packed bit data octets (aliases into the reader's own buffer/span). -
outUnusedBits--- The number of unused (padding) bits in the last octet of outBits.
Returns: True on success; otherwise, false (cursor unchanged).
Reads the next element expecting a BIT STRING (universal, primitive) into an owning COctet, copying the packed bit data rather than aliasing it.
-
outBits--- The decoded packed bit data. -
outUnusedBits--- The number of unused (padding) bits in the last octet of outBits.
Returns: True on success; otherwise, false (cursor unchanged).
Reads the next element expecting an OBJECT IDENTIFIER (universal, primitive).
-
outArcs--- The destination for the decoded arc values. -
outArcCount--- The number of arcs written to outArcs.
Returns: True on success; otherwise, false (cursor unchanged).
Reads the next element expecting an OBJECT IDENTIFIER (universal, primitive) directly into its dotted-decimal text form; see CDecoder::decodeOidString(). @tparam TChar The destination TString's character type (char or wchar_t).
-
outValue--- The decoded dotted-decimal text.
Returns: True on success; otherwise, false (cursor unchanged).
Reads the next element expecting the given character string kind (universal, primitive); see CDecoder::decodeText() for which kinds get their character set validated.
-
kind--- The expected universal string tag. -
outText--- The decoded text (aliases into the reader's own buffer/span).
Returns: True on success; otherwise, false (cursor unchanged).
Reads the next element expecting the given character string kind (universal, primitive) directly into a TString; see CDecoder::decodeString(). @tparam TChar The destination TString's character type (char or wchar_t).
-
kind--- The expected universal string tag. -
outValue--- The decoded string.
Returns: True on success; otherwise, false (cursor unchanged).
Reads the next element expecting a UTCTime (universal, primitive).
-
outTime--- The decoded time.
Returns: True on success; otherwise, false (cursor unchanged).
Reads the next element expecting a GeneralizedTime (universal, primitive).
-
outTime--- The decoded time.
Returns: True on success; otherwise, false (cursor unchanged).
Reads the next element expecting a CDistinguishedName (universal, constructed SEQUENCE); see CDecoder::decodeDistinguishedName() for the exact structure expected.
-
outValue--- The decoded distinguished name.
Returns: True on success; otherwise, false (cursor unchanged).
Descends into a DER SEQUENCE read from a stream. A failed typed read leaves the cursor exactly where it was, which is what makes probing an OPTIONAL or DEFAULT field safe: try the type the field would have, and if it is not there, read on as if nothing had happened.
// given: const IStreamPtr& der
CReader reader(der, EAENC_DER);
CReader members;
if (!reader.readSequence(members)) {
return;
}
int64_t version = 0;
if (!members.readInteger(version)) {
version = 1; // absent: the cursor still points at the next field
}
CWideString label;
if (!members.readString(EAUTAG_STRING_UTF8, label)) {
return;
}
while (!members.atEnd()) {
CTag tag;
SReadOnlyByteSpan content;
if (!members.readNextElement(tag, content)) {
return;
}
}Reference generated from certpp/asn1/reader.hpp at commit 0834b29 (v1.0.1). The header is authoritative --- if this page disagrees with it, the header is right and this page needs regenerating.