ENCODEX provides support for encoding, decoding, and parsing to and from multiple encoding schemes.

Authors
Definitions
ASN.1 (Abstract Syntax Notation One)
A data encoding format common to cryptography in which data is represented as a series of one or more tag, size, data groups. The most commonly occurring serialization format for ASN.1 is DER, which stands for Distinguished Encoding Rules.

Base 64
A data encoding format also common to cryptography in which data is expressed in six (6) bit groups of data (sextets) which are then mapped to one of 64 plaintext characters (A-Z,a-z,+/) and padding = characters. Base 64 is the encoding format used by PEM keyfiles.

BPP (Bits-Per-Pixel)
A data encoding format in which a bytewise data value that can be expressed within 4 bits or less is compressed such that multiple values fit within the same byte. This discards any higher, unused bits and makes the data smaller.

Octet Encoding
The standard encoding format in which each byte expresses 8 bits of information.

Enumerations
enum CRYPTX_ASN1_TYPES {ASN1_RESVD = 0, ASN1_BOOLEAN, ASN1_INTEGER, ASN1_BITSTRING, ASN1_OCTETSTRING, ASN1_NULL, ASN1_OBJECTID, ASN1_OBJECTDESC, ASN1_INSTANCE, ASN1_REAL, ASN1_ENUMERATED, ASN1_EMBEDDEDPDV, ASN1_UTF8STRING, ASN1_RELATIVEOID, ASN1_SEQUENCE = 16, ASN1_SET, ASN1_NUMERICSTRING, ASN1_PRINTABLESTRING, ASN1_TELETEXSTRING, ASN1_VIDEOTEXSTRING, ASN1_IA5STRING, ASN1_UTCTIME, ASN1_GENERALIZEDTIME, ASN1_GRAPHICSTRING, ASN1_VISIBLESTRING, ASN1_GENERALSTRING, ASN1_UNIVERSALSTRING, ASN1_CHARSTRING, ASN1_BMPSTRING};

Defines possible ASN.1 basic tag types.

enum CRYPTX_ASN1_CLASSES {
   ASN1_UNIVERSAL,
   ASN1_APPLICATION,
   ASN1_CONTEXTSPEC,
   ASN1_PRIVATE
};

Defines possible class types for ASN.1-encoded objects.

enum CRYPTX_ASN1_FORMS {
   ASN1_PRIMITIVE,
   ASN1_CONSTRUCTED
};

Defines possible forms for ASN.1-encoded objects.

Structs
struct cryptx_asn1_obj {...};

Defines a structure for the decoding of ASN.1-encoded objects.

Functions
size_t cryptx_asn1_decode(
   void *asn1_data, size_t len,
   struct cryptx_asn1_obj* objs,
   size_t iter_count);

Decodes an ASN.1 encoded data stream, returning object metadata into an array of cryptx_asn1_obj structs.
asn1_data: Pointer to ASN.1-encoded data.
len: Length of the data.
objs: Pointer to an array of cryptx_asn1_obj structs to decode objects into.
iter_count: Maximum number of ASN.1 objects to decode before returning
output: The number of objects successfully decoded.
The function will automatically recurse for any object with the ASN1_CONSTRUCTED bit set.
An object that starts with ASN1_RESVD (0x00) is assumed to have a padding byte prepended. That byte is skipped by the parser prior to the start of decoding.
In some keyfile formats, there is a BIT STRING object that is a constructed type but does not have the ASN1_CONSTRUCTED bit set. You will need to call this function again on that object to further parse it.
Object sizes greater than 24 bits (3 bytes) are unsupported due to device limitations. Encountering an unsupported size will cause the parser to quit, returning any objects it has already decoded.

size_t cryptx_base64_encode(void* dest, const void* src, size_t len);

Encodes an octet-encoded byte stream into a sextet encoded byte stream.
dest: Pointer to buffer to write sextet-encoded (output) data to.
src: Pointer to data to encode.
len: Length of data to encode.
output: The length of the output encoded data.

size_t cryptx_base64_decode(void* dest, const void* src, size_t len);

Decodes a sextet encoded byte stream and returns octet-encoded bytes.
dest: Pointer to buffer to write decoded (output) data to.
src: Pointer to data to decode.
len: Length of data to decode.
output: The length of the output decoded data.

bool cryptx_bpp_encode(void* dest, const void* src, size_t len, uint8_t bpp);

Encodes an octet-encoded byte stream into bits-per-pixel encoding.
dest: Pointer to buffer to write bpp-encoded data to.
src: Pointer to data to encode/compress.
len: Length of the encoded (output) data stream.
bpp: Number of bits-per-pixel to encode into.

bool cryptx_bpp_decode(void* dest, const void* src, size_t len, uint8_t bpp);

Decodes a bpp-encoded byte stream into octet-encoded bytes.
dest: Pointer to buffer to write decoded data to.
src: Pointer to data to decode/decompress.
len: Length of the encoded (input) data stream.
bpp: Number of bits-per-pixel to decode from.