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[256]; | ||
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 | 11428 | static inline unsigned int base64_decode(unsigned char c) | |
19 | { | ||
20 | 11428 | return base64_decode_table[c]; | |
21 | } | ||
22 | |||
23 | // Like base64_decode(), but implemented in a way that's more amenable to | ||
24 | // constant propagation when `c` is known at compile-time | ||
25 | 256 | static inline unsigned int base64_decode_branchy(unsigned char c) | |
26 | { | ||
27 |
7/7✓ Branch 0 taken 26 times.
✓ Branch 1 taken 26 times.
✓ Branch 2 taken 10 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 191 times.
|
256 | if (c >= 'A' && c <= 'Z') { |
28 | 26 | return c - 'A'; | |
29 | } | ||
30 | if (c >= 'a' && c <= 'z') { | ||
31 | 26 | return (c - 'a') + 26; | |
32 | } | ||
33 | if (c >= '0' && c <= '9') { | ||
34 | 10 | return (c - '0') + 52; | |
35 | } | ||
36 | if (c == '+') { | ||
37 | 1 | return 62; | |
38 | } | ||
39 | if (c == '/') { | ||
40 | 1 | return 63; | |
41 | } | ||
42 | if (c == '=') { | ||
43 | 1 | return BASE64_PADDING; | |
44 | } | ||
45 | return BASE64_INVALID; | ||
46 | } | ||
47 | |||
48 | size_t base64_encode_block(const char *in, size_t ilen, char *out, size_t olen) NONNULL_ARGS; | ||
49 | void base64_encode_final(const char *in, size_t ilen, char out[4]) NONNULL_ARGS; | ||
50 | |||
51 | #endif | ||
52 |