Coverage Report

Created: 2026-01-24 06:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/bind9/lib/isc/base64.c
Line
Count
Source
1
/*
2
 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3
 *
4
 * SPDX-License-Identifier: MPL-2.0
5
 *
6
 * This Source Code Form is subject to the terms of the Mozilla Public
7
 * License, v. 2.0. If a copy of the MPL was not distributed with this
8
 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
9
 *
10
 * See the COPYRIGHT file distributed with this work for additional
11
 * information regarding copyright ownership.
12
 */
13
14
/*! \file */
15
16
#include <stdbool.h>
17
18
#include <isc/base64.h>
19
#include <isc/buffer.h>
20
#include <isc/lex.h>
21
#include <isc/string.h>
22
#include <isc/util.h>
23
24
/*@{*/
25
/*!
26
 * These static functions are also present in lib/dns/rdata.c.  I'm not
27
 * sure where they should go. -- bwelling
28
 */
29
static isc_result_t
30
str_totext(const char *source, isc_buffer_t *target);
31
32
static isc_result_t
33
mem_tobuffer(isc_buffer_t *target, void *base, unsigned int length);
34
35
static const char base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvw"
36
           "xyz0123456789+/=";
37
/*@}*/
38
39
isc_result_t
40
isc_base64_totext(isc_region_t *source, int wordlength, const char *wordbreak,
41
39.1k
      isc_buffer_t *target) {
42
39.1k
  char buf[5];
43
39.1k
  unsigned int loops = 0;
44
45
39.1k
  if (wordlength < 4) {
46
6.50k
    wordlength = 4;
47
6.50k
  }
48
49
39.1k
  memset(buf, 0, sizeof(buf));
50
844k
  while (source->length > 2) {
51
805k
    buf[0] = base64[(source->base[0] >> 2) & 0x3f];
52
805k
    buf[1] = base64[((source->base[0] << 4) & 0x30) |
53
805k
        ((source->base[1] >> 4) & 0x0f)];
54
805k
    buf[2] = base64[((source->base[1] << 2) & 0x3c) |
55
805k
        ((source->base[2] >> 6) & 0x03)];
56
805k
    buf[3] = base64[source->base[2] & 0x3f];
57
805k
    RETERR(str_totext(buf, target));
58
805k
    isc_region_consume(source, 3);
59
60
805k
    loops++;
61
805k
    if (source->length != 0 && (int)((loops + 1) * 4) >= wordlength)
62
102k
    {
63
102k
      loops = 0;
64
102k
      RETERR(str_totext(wordbreak, target));
65
102k
    }
66
805k
  }
67
39.1k
  if (source->length == 2) {
68
8.22k
    buf[0] = base64[(source->base[0] >> 2) & 0x3f];
69
8.22k
    buf[1] = base64[((source->base[0] << 4) & 0x30) |
70
8.22k
        ((source->base[1] >> 4) & 0x0f)];
71
8.22k
    buf[2] = base64[((source->base[1] << 2) & 0x3c)];
72
8.22k
    buf[3] = '=';
73
8.22k
    RETERR(str_totext(buf, target));
74
8.22k
    isc_region_consume(source, 2);
75
30.9k
  } else if (source->length == 1) {
76
20.8k
    buf[0] = base64[(source->base[0] >> 2) & 0x3f];
77
20.8k
    buf[1] = base64[((source->base[0] << 4) & 0x30)];
78
20.8k
    buf[2] = buf[3] = '=';
79
20.8k
    RETERR(str_totext(buf, target));
80
20.8k
    isc_region_consume(source, 1);
81
20.8k
  }
82
39.1k
  return ISC_R_SUCCESS;
83
39.1k
}
84
85
/*%
86
 * State of a base64 decoding process in progress.
87
 */
88
typedef struct {
89
  int length;       /*%< Desired length of binary data or -1 */
90
  isc_buffer_t *target; /*%< Buffer for resulting binary data */
91
  int digits;       /*%< Number of buffered base64 digits */
92
  bool seen_end;        /*%< True if "=" end marker seen */
93
  int val[4];
94
} base64_decode_ctx_t;
95
96
static void
97
91.6k
base64_decode_init(base64_decode_ctx_t *ctx, int length, isc_buffer_t *target) {
98
91.6k
  ctx->digits = 0;
99
91.6k
  ctx->seen_end = false;
100
91.6k
  ctx->length = length;
101
91.6k
  ctx->target = target;
102
91.6k
}
103
104
static isc_result_t
105
1.82M
base64_decode_char(base64_decode_ctx_t *ctx, int c) {
106
1.82M
  const char *s;
107
108
1.82M
  if (ctx->seen_end) {
109
6
    return ISC_R_BADBASE64;
110
6
  }
111
1.82M
  if ((s = strchr(base64, c)) == NULL) {
112
69
    return ISC_R_BADBASE64;
113
69
  }
114
1.82M
  ctx->val[ctx->digits++] = (int)(s - base64);
115
1.82M
  if (ctx->digits == 4) {
116
456k
    int n;
117
456k
    unsigned char buf[3];
118
456k
    if (ctx->val[0] == 64 || ctx->val[1] == 64) {
119
5
      return ISC_R_BADBASE64;
120
5
    }
121
456k
    if (ctx->val[2] == 64 && ctx->val[3] != 64) {
122
11
      return ISC_R_BADBASE64;
123
11
    }
124
    /*
125
     * Check that bits that should be zero are.
126
     */
127
456k
    if (ctx->val[2] == 64 && (ctx->val[1] & 0xf) != 0) {
128
2
      return ISC_R_BADBASE64;
129
2
    }
130
    /*
131
     * We don't need to test for ctx->val[2] != 64 as
132
     * the bottom two bits of 64 are zero.
133
     */
134
456k
    if (ctx->val[3] == 64 && (ctx->val[2] & 0x3) != 0) {
135
2
      return ISC_R_BADBASE64;
136
2
    }
137
456k
    n = (ctx->val[2] == 64) ? 1 : (ctx->val[3] == 64) ? 2 : 3;
138
456k
    if (n != 3) {
139
58.8k
      ctx->seen_end = true;
140
58.8k
      if (ctx->val[2] == 64) {
141
1.13k
        ctx->val[2] = 0;
142
1.13k
      }
143
58.8k
      if (ctx->val[3] == 64) {
144
58.8k
        ctx->val[3] = 0;
145
58.8k
      }
146
58.8k
    }
147
456k
    buf[0] = (ctx->val[0] << 2) | (ctx->val[1] >> 4);
148
456k
    buf[1] = (ctx->val[1] << 4) | (ctx->val[2] >> 2);
149
456k
    buf[2] = (ctx->val[2] << 6) | (ctx->val[3]);
150
456k
    RETERR(mem_tobuffer(ctx->target, buf, n));
151
456k
    if (ctx->length >= 0) {
152
417
      if (n > ctx->length) {
153
2
        return ISC_R_BADBASE64;
154
415
      } else {
155
415
        ctx->length -= n;
156
415
      }
157
417
    }
158
456k
    ctx->digits = 0;
159
456k
  }
160
1.82M
  return ISC_R_SUCCESS;
161
1.82M
}
162
163
static isc_result_t
164
91.4k
base64_decode_finish(base64_decode_ctx_t *ctx) {
165
91.4k
  if (ctx->length > 0) {
166
12
    return ISC_R_UNEXPECTEDEND;
167
12
  }
168
91.4k
  if (ctx->digits != 0) {
169
96
    return ISC_R_BADBASE64;
170
96
  }
171
91.3k
  return ISC_R_SUCCESS;
172
91.4k
}
173
174
isc_result_t
175
88.8k
isc_base64_tobuffer(isc_lex_t *lexer, isc_buffer_t *target, int length) {
176
88.8k
  unsigned int before, after;
177
88.8k
  base64_decode_ctx_t ctx;
178
88.8k
  isc_textregion_t *tr;
179
88.8k
  isc_token_t token;
180
88.8k
  bool eol;
181
182
88.8k
  REQUIRE(length >= -2);
183
184
88.8k
  base64_decode_init(&ctx, length, target);
185
186
88.8k
  before = isc_buffer_usedlength(target);
187
257k
  while (!ctx.seen_end && (ctx.length != 0)) {
188
199k
    unsigned int i;
189
190
199k
    if (length > 0) {
191
277
      eol = false;
192
199k
    } else {
193
199k
      eol = true;
194
199k
    }
195
199k
    RETERR(isc_lex_getmastertoken(lexer, &token,
196
199k
                isc_tokentype_string, eol));
197
199k
    if (token.type != isc_tokentype_string) {
198
30.2k
      break;
199
30.2k
    }
200
169k
    tr = &token.value.as_textregion;
201
1.44M
    for (i = 0; i < tr->length; i++) {
202
1.27M
      RETERR(base64_decode_char(&ctx, tr->base[i]));
203
1.27M
    }
204
169k
  }
205
88.6k
  after = isc_buffer_usedlength(target);
206
88.6k
  if (ctx.length < 0 && !ctx.seen_end) {
207
30.2k
    isc_lex_ungettoken(lexer, &token);
208
30.2k
  }
209
88.6k
  RETERR(base64_decode_finish(&ctx));
210
88.6k
  if (length == -2 && before == after) {
211
74
    return ISC_R_UNEXPECTEDEND;
212
74
  }
213
88.5k
  return ISC_R_SUCCESS;
214
88.6k
}
215
216
isc_result_t
217
2.75k
isc_base64_decodestring(const char *cstr, isc_buffer_t *target) {
218
2.75k
  base64_decode_ctx_t ctx;
219
220
2.75k
  base64_decode_init(&ctx, -1, target);
221
548k
  for (;;) {
222
548k
    int c = *cstr++;
223
548k
    if (c == '\0') {
224
2.72k
      break;
225
2.72k
    }
226
546k
    if (c == ' ' || c == '\t' || c == '\n' || c == '\r') {
227
1.47k
      continue;
228
1.47k
    }
229
544k
    RETERR(base64_decode_char(&ctx, c));
230
544k
  }
231
2.72k
  RETERR(base64_decode_finish(&ctx));
232
2.70k
  return ISC_R_SUCCESS;
233
2.72k
}
234
235
static isc_result_t
236
936k
str_totext(const char *source, isc_buffer_t *target) {
237
936k
  unsigned int l;
238
936k
  isc_region_t region;
239
240
936k
  isc_buffer_availableregion(target, &region);
241
936k
  l = strlen(source);
242
243
936k
  if (l > region.length) {
244
0
    return ISC_R_NOSPACE;
245
0
  }
246
247
936k
  memmove(region.base, source, l);
248
936k
  isc_buffer_add(target, l);
249
936k
  return ISC_R_SUCCESS;
250
936k
}
251
252
static isc_result_t
253
456k
mem_tobuffer(isc_buffer_t *target, void *base, unsigned int length) {
254
456k
  isc_region_t tr;
255
256
456k
  isc_buffer_availableregion(target, &tr);
257
456k
  if (length > tr.length) {
258
3
    return ISC_R_NOSPACE;
259
3
  }
260
456k
  memmove(tr.base, base, length);
261
456k
  isc_buffer_add(target, length);
262
456k
  return ISC_R_SUCCESS;
263
456k
}