dte test coverage


Directory: ./
File: src/util/base64.h
Date: 2025-05-08 15:05:54
Exec Total Coverage
Lines: 13 13 100.0%
Functions: 2 2 100.0%
Branches: 12 12 100.0%

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 13038 static inline unsigned int base64_decode(unsigned char c)
19 {
20 13038 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
2/2
✓ Branch 0 (2→3) taken 26 times.
✓ Branch 1 (2→4) taken 230 times.
256 if (c >= 'A' && c <= 'Z') {
28 26 return c - 'A';
29 }
30
2/2
✓ Branch 0 (4→5) taken 26 times.
✓ Branch 1 (4→6) taken 204 times.
230 if (c >= 'a' && c <= 'z') {
31 26 return (c - 'a') + 26;
32 }
33
2/2
✓ Branch 0 (6→7) taken 10 times.
✓ Branch 1 (6→8) taken 194 times.
204 if (c >= '0' && c <= '9') {
34 10 return (c - '0') + 52;
35 }
36
2/2
✓ Branch 0 (8→9) taken 193 times.
✓ Branch 1 (8→12) taken 1 times.
194 if (c == '+') {
37 return 62;
38 }
39
2/2
✓ Branch 0 (9→10) taken 192 times.
✓ Branch 1 (9→12) taken 1 times.
193 if (c == '/') {
40 return 63;
41 }
42
2/2
✓ Branch 0 (10→11) taken 1 times.
✓ Branch 1 (10→12) taken 191 times.
192 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