| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #ifndef UTIL_BASE64_H | ||
| 2 | #define UTIL_BASE64_H | ||
| 3 | |||
| 4 | #include <stddef.h> | ||
| 5 | #include <stdint.h> | ||
| 6 | #include "macros.h" | ||
| 7 | |||
| 8 | extern const uint8_t base64_decode_table[80]; | ||
| 9 | extern const char base64_encode_table[64]; | ||
| 10 | |||
| 11 | enum { | ||
| 12 | BASE64_PADDING = 1 << 6, // Return value for padding bytes (=) | ||
| 13 | BASE64_INVALID = 1 << 7, // Return value for invalid bytes ([^A-Za-z0-9+/=]) | ||
| 14 | }; | ||
| 15 | |||
| 16 | // Decodes a single, base64 digit and returns a numerical value between 0-63, | ||
| 17 | // or one of the special enum values above | ||
| 18 | 16402 | static inline unsigned int base64_decode(unsigned char c) | |
| 19 | { | ||
| 20 | 16402 | c -= '+'; // Lookup table starts at '+' | |
| 21 |
2/2✓ Branch 2 → 3 taken 16217 times.
✓ Branch 2 → 4 taken 185 times.
|
16402 | return base64_decode_table[(c < sizeof(base64_decode_table)) ? c : 1]; |
| 22 | } | ||
| 23 | |||
| 24 | // Like base64_decode(), but implemented in a way that's more amenable to | ||
| 25 | // constant propagation when `c` is known at compile-time | ||
| 26 | 256 | static inline unsigned int base64_decode_branchy(unsigned char c) | |
| 27 | { | ||
| 28 | 282 | return (c >= 'A' && c <= 'Z') ? c - 'A' | |
| 29 |
10/10✓ Branch 2 → 3 taken 26 times.
✓ Branch 2 → 4 taken 230 times.
✓ Branch 4 → 5 taken 26 times.
✓ Branch 4 → 6 taken 204 times.
✓ Branch 8 → 9 taken 193 times.
✓ Branch 8 → 12 taken 1 time.
✓ Branch 9 → 10 taken 192 times.
✓ Branch 9 → 12 taken 1 time.
✓ Branch 10 → 11 taken 191 times.
✓ Branch 10 → 12 taken 1 time.
|
450 | : (c >= 'a' && c <= 'z') ? (c - 'a') + 26 |
| 30 |
2/2✓ Branch 6 → 7 taken 10 times.
✓ Branch 6 → 8 taken 194 times.
|
204 | : (c >= '0' && c <= '9') ? (c - '0') + 52 |
| 31 | : (c == '+') ? 62 | ||
| 32 | : (c == '/') ? 63 | ||
| 33 | : (c == '=') ? BASE64_PADDING | ||
| 34 | : BASE64_INVALID; | ||
| 35 | } | ||
| 36 | |||
| 37 | size_t base64_encode_block(const char *in, size_t ilen, char *out, size_t olen) NONNULL_ARGS; | ||
| 38 | void base64_encode_final(const char *in, size_t ilen, char out[static 4]) NONNULL_ARGS; | ||
| 39 | |||
| 40 | #endif | ||
| 41 |