|
ProtoCore v0.0.1
Deterministic, zero-heap network stack for embedded targets
|
Base64 encoder/decoder implementation. More...
Go to the source code of this file.
Functions | |
| void | pc_base64_encode (const uint8_t *src, size_t src_len, char *dst) |
Encode src_len bytes of src as Base64. | |
| size_t | pc_base64_decode (const char *src, uint8_t *dst, size_t dst_cap) |
| Decode a null-terminated Base64 string. | |
| size_t | pc_base64url_encode (const uint8_t *src, size_t src_len, char *dst) |
Encode src_len bytes as base64url (RFC 4648 section 5): '-' / '_' in place of '+' / '/', and no '=' padding. | |
| size_t | pc_base64url_decode (const char *src, size_t src_len, uint8_t *dst, size_t dst_cap) |
Decode src_len characters of base64url (RFC 4648 section 5, '-'/'_' alphabet; an '=' ends the input). | |
Base64 encoder/decoder implementation.
Encode is a single portable software codec (RFC 4648) on every target: it is byte-identical to mbedTLS's but ~20x faster on the ESP32-S3 (docs/FEATURE_PERFORMANCE.md section 2), and it only ever encodes public data here (the SHA-1 in the WebSocket accept), so a data-dependent table lookup is fine.
Decode is one portable constant-time codec on every target (no mbedTLS delegation, no separate native path). It is the only base64 path that touches a secret - the Basic-auth credential (RFC 7617) and the JWT / JWS segments (RFC 7515, via base64url) - so the character -> value mapping is evaluated with branchless arithmetic range masks: no data-dependent branch and no data-indexed table, so neither the timing nor the cache footprint depends on the secret bytes. Padding / length handling may branch (the plaintext length mod 3 is public, not a secret). This both removes the mbedTLS base64 dependency and closes the timing gap on the base64url (JWT) path, which was previously a plain branchy software decoder.
Definition in file base64.cpp.
| void pc_base64_encode | ( | const uint8_t * | src, |
| size_t | src_len, | ||
| char * | dst | ||
| ) |
Encode src_len bytes of src as Base64.
Writes a null-terminated string into dst. dst must be at least ((src_len + 2) / 3) * 4 + 1 bytes.
| src | Input bytes. |
| src_len | Number of input bytes. |
| dst | Output buffer (null-terminated Base64 string). |
Definition at line 29 of file base64.cpp.
Referenced by pc_base64url_encode().
| size_t pc_base64_decode | ( | const char * | src, |
| uint8_t * | dst, | ||
| size_t | dst_cap | ||
| ) |
Decode a null-terminated Base64 string.
Writes decoded bytes into dst, never writing more than dst_cap bytes. Returns the number of decoded bytes, or 0 on invalid input or if the decoded output would exceed dst_cap (the write is bounded - no overflow). The caller must leave room for any terminator it adds afterward (pass a capacity one less than the buffer size if it will null-terminate at the returned length).
| src | Null-terminated Base64 input string. |
| dst | Output byte buffer. |
| dst_cap | Capacity of dst in bytes. |
dst, or 0 on error / overflow. Definition at line 217 of file base64.cpp.
| size_t pc_base64url_encode | ( | const uint8_t * | src, |
| size_t | src_len, | ||
| char * | dst | ||
| ) |
Encode src_len bytes as base64url (RFC 4648 section 5): '-' / '_' in place of '+' / '/', and no '=' padding.
Writes a null-terminated string into dst, which must hold at least ((src_len + 2) / 3) * 4 + 1 bytes (the same as pc_base64_encode; the URL form is never longer).
Definition at line 304 of file base64.cpp.
References pc_base64_encode().
| size_t pc_base64url_decode | ( | const char * | src, |
| size_t | src_len, | ||
| uint8_t * | dst, | ||
| size_t | dst_cap | ||
| ) |
Decode src_len characters of base64url (RFC 4648 section 5, '-'/'_' alphabet; an '=' ends the input).
Strict: the standard '+'/'/' characters are rejected so a JWS/JWT segment is decoded as base64url only (RFC 7515), never as a mixed alphabet. Streaming decoder - no padding or buffer length restriction. Writes at most dst_cap bytes; returns the number written, or 0 on an invalid character or if the output would exceed dst_cap (the write is bounded - no overflow).
Definition at line 333 of file base64.cpp.