Coverage Report

Created: 2026-07-16 06:46

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/lighttpd1.4/src/base64.c
Line
Count
Source
1
#include "first.h"
2
3
#include "base64.h"
4
5
/* reverse mapping:
6
 * >= 0: base64 value
7
 * -1: invalid character
8
 * -2: skip character (whitespace/control)
9
 * -3: padding
10
 */
11
12
/* BASE64_STANDARD: "A-Z a-z 0-9 + /" maps to 0-63, pad with "=" */
13
static const char base64_standard_table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
14
static const signed char base64_standard_reverse_table[] = {
15
/*   0   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F */
16
  -1, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, /* 0x00 - 0x0F */
17
  -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, /* 0x10 - 0x1F */
18
  -2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, /* 0x20 - 0x2F */
19
  52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -3, -1, -1, /* 0x30 - 0x3F */
20
  -1,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, /* 0x40 - 0x4F */
21
  15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, /* 0x50 - 0x5F */
22
  -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, /* 0x60 - 0x6F */
23
  41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1, /* 0x70 - 0x7F */
24
};
25
26
/* BASE64_URL: "A-Z a-z 0-9 - _" maps to 0-63, pad with "=" */
27
static const char base64_url_table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_=";
28
static const signed char base64_url_reverse_table[] = {
29
/*   0   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F */
30
  -1, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, /* 0x00 - 0x0F */
31
  -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, /* 0x10 - 0x1F */
32
  -2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, /* 0x20 - 0x2F */
33
  52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -3, -1, -1, /* 0x30 - 0x3F */
34
  -1,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, /* 0x40 - 0x4F */
35
  15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, 63, /* 0x50 - 0x5F */
36
  -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, /* 0x60 - 0x6F */
37
  41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1, /* 0x70 - 0x7F */
38
};
39
40
133
size_t li_base64_dec(unsigned char * const result, const size_t out_length, const char * const in, const size_t in_length, const base64_charset charset) {
41
133
    const unsigned char *un = (const unsigned char *)in;
42
133
    const unsigned char * const end = un + in_length;
43
133
    const signed char * const base64_reverse_table = (charset)
44
133
      ? base64_url_reverse_table                     /* BASE64_URL */
45
133
      : base64_standard_reverse_table;               /* BASE64_STANDARD */
46
47
133
    int_fast32_t ch = 0;
48
133
    int_fast32_t out4 = 0;
49
133
    size_t i = 0;
50
133
    size_t out_pos = 0;
51
839k
    for (; un < end; ++un) {
52
839k
        ch = (*un < 128) ? base64_reverse_table[*un] : -1;
53
839k
        if (__builtin_expect( (ch < 0), 0)) {
54
            /* skip formatted base64; skip whitespace ('\r' '\n' '\t' ' ')*/
55
876
            if (-2 == ch) /*(loose check; skip ' ', all ctrls not \127 or \0)*/
56
802
                continue; /* skip character */
57
74
            break;
58
876
        }
59
60
838k
        out4 = (out4 << 6) | ch;
61
838k
        if ((++i & 3) == 0) {
62
209k
            result[out_pos]   = (out4 >> 16) & 0xFF;
63
209k
            result[out_pos+1] = (out4 >>  8) & 0xFF;
64
209k
            result[out_pos+2] = (out4      ) & 0xFF;
65
209k
            out_pos += 3;
66
209k
            out4 = 0;
67
209k
        }
68
838k
    }
69
70
    /* permit base64 string ending with pad chars (ch == -3); not checking
71
     * for one or two pad chars + optional whitespace reaches in_length) */
72
    /* permit base64 string truncated before in_length (*un == '\0') */
73
133
    switch (un == end || ch == -3 || *un != '\0' ? (i & 3) : 1) {
74
14
      case 3:
75
14
        result[out_pos++] = (out4 >> 10);
76
14
        out4 <<= 2;
77
14
        __attribute_fallthrough__
78
34
      case 2:
79
34
        result[out_pos++] = (out4 >> 4) & 0xFF;
80
34
        __attribute_fallthrough__
81
106
      case 0:
82
106
        force_assert(out_pos <= out_length);
83
106
        return out_pos;
84
27
      case 1: /* pad char or str end can only come after 2+ base64 chars */
85
27
      default:
86
27
        return 0; /* invalid character, abort */
87
133
    }
88
133
}
89
90
79
size_t li_base64_enc(char * const restrict out, const size_t out_length, const unsigned char * const restrict in, const size_t in_length, const base64_charset charset, const int pad) {
91
79
  size_t i;
92
79
  size_t out_pos = 0;
93
79
  uint_fast32_t v;
94
79
  const char* const base64_table = (charset)
95
79
    ? base64_url_table             /* BASE64_URL */
96
79
    : base64_standard_table;       /* BASE64_STANDARD */
97
79
  const char padchar = base64_table[64]; /* padding char */
98
99
  /* check overflows */
100
  /* 1073741823 is max num full_tuples to avoid overflow in uint32_t;
101
   * and (1 GB - 1) is ridiculously large for base64-encoded data */
102
79
  force_assert(in_length <= 3221225469); /* (3221225469+2) / 3 * 4 < UINT32_MAX */
103
79
  force_assert((in_length+2)/3*4 <= out_length);
104
105
2.95M
  for (i = 2; i < in_length; i += 3) {
106
2.95M
    v = (in[i-2] << 16) | (in[i-1] << 8) | in[i];
107
2.95M
    out[out_pos+0] = base64_table[(v >> 18) & 0x3f];
108
2.95M
    out[out_pos+1] = base64_table[(v >> 12) & 0x3f];
109
2.95M
    out[out_pos+2] = base64_table[(v >>  6) & 0x3f];
110
2.95M
    out[out_pos+3] = base64_table[(v      ) & 0x3f];
111
2.95M
    out_pos += 4;
112
2.95M
  }
113
114
79
  switch (in_length - (i-2)) {
115
23
  case 0:
116
23
  default:
117
23
    break;
118
37
  case 1:
119
37
    {
120
      /* pretend in[i-1] = in[i] = 0, don't write last two (out_pos+3, out_pos+2) characters */
121
37
      v = (in[i-2] << 4);
122
37
      out[out_pos+0] = base64_table[(v >> 6) & 0x3f];
123
37
      out[out_pos+1] = base64_table[(v     ) & 0x3f];
124
37
      if (pad) {
125
37
        out[out_pos+2] = out[out_pos+3] = padchar;
126
37
        out_pos += 4;
127
37
      }
128
0
      else
129
0
        out_pos += 2;
130
37
    }
131
37
    break;
132
19
  case 2:
133
19
    {
134
      /* pretend in[i] = 0, don't write last (out_pos+3) character */
135
19
      v = (in[i-2] << 10) | (in[i-1] << 2);
136
19
      out[out_pos+0] = base64_table[(v >> 12) & 0x3f];
137
19
      out[out_pos+1] = base64_table[(v >>  6) & 0x3f];
138
19
      out[out_pos+2] = base64_table[(v      ) & 0x3f];
139
19
      if (pad) {
140
19
        out[out_pos+3] = padchar;
141
19
        out_pos += 4;
142
19
      }
143
0
      else
144
0
        out_pos += 3;
145
19
    }
146
19
    break;
147
79
  }
148
149
79
  return out_pos;
150
79
}
151
152
153
79
char* buffer_append_base64_enc(buffer *out, const unsigned char* in, size_t in_length, base64_charset charset, int pad) {
154
79
    const size_t reserve = (in_length+2)/3*4;
155
79
    char * const result = buffer_string_prepare_append(out, reserve);
156
79
    const size_t out_pos =
157
79
      li_base64_enc(result, reserve, in, in_length, charset, pad);
158
159
79
    buffer_commit(out, out_pos);
160
161
79
    return result;
162
79
}
163
164
165
133
unsigned char* buffer_append_base64_decode(buffer *out, const char* in, size_t in_length, base64_charset charset) {
166
133
    const size_t reserve = 3*(in_length/4) + 3;
167
133
    unsigned char * const result = (unsigned char *)
168
133
      buffer_string_prepare_append(out, reserve);
169
133
    const size_t out_pos =
170
133
      li_base64_dec(result, reserve, in, in_length, charset);
171
172
133
    buffer_commit(out, out_pos);
173
174
133
    return (out_pos || !in_length) ? result : NULL;
175
133
}