Coverage Report

Created: 2025-12-14 06:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/lib/curlx/base64.c
Line
Count
Source
1
/***************************************************************************
2
 *                                  _   _ ____  _
3
 *  Project                     ___| | | |  _ \| |
4
 *                             / __| | | | |_) | |
5
 *                            | (__| |_| |  _ <| |___
6
 *                             \___|\___/|_| \_\_____|
7
 *
8
 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9
 *
10
 * This software is licensed as described in the file COPYING, which
11
 * you should have received as part of this distribution. The terms
12
 * are also available at https://curl.se/docs/copyright.html.
13
 *
14
 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15
 * copies of the Software, and permit persons to whom the Software is
16
 * furnished to do so, under the terms of the COPYING file.
17
 *
18
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19
 * KIND, either express or implied.
20
 *
21
 * SPDX-License-Identifier: curl
22
 *
23
 ***************************************************************************/
24
25
/* Base64 encoding/decoding */
26
27
#include "../curl_setup.h"
28
29
#include <curl/curl.h>
30
#include "warnless.h"
31
#include "base64.h"
32
33
/* ---- Base64 Encoding/Decoding Table --- */
34
const char Curl_base64encdec[] =
35
  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
36
37
/* The Base 64 encoding with a URL and filename safe alphabet, RFC 4648
38
   section 5 */
39
static const char base64url[] =
40
  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
41
42
static const unsigned char decodetable[] = {
43
  62,  255, 255, 255, 63,  52,  53, 54, 55, 56, 57, 58, 59, 60, 61, 255,
44
  255, 255, 255, 255, 255, 255, 0,  1,  2,  3,  4,  5,  6,  7,  8,  9,
45
  10,  11,  12,  13,  14,  15,  16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
46
  255, 255, 255, 255, 255, 255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
47
  36,  37,  38,  39,  40,  41,  42, 43, 44, 45, 46, 47, 48, 49, 50, 51
48
};
49
/*
50
 * curlx_base64_decode()
51
 *
52
 * Given a base64 null-terminated string at src, decode it and return a
53
 * pointer in *outptr to a newly allocated memory area holding decoded data.
54
 * Size of decoded data is returned in variable pointed by outlen.
55
 *
56
 * Returns CURLE_OK on success, otherwise specific error code. Function
57
 * output shall not be considered valid unless CURLE_OK is returned.
58
 *
59
 * When decoded data length is 0, returns NULL in *outptr.
60
 *
61
 * @unittest: 1302
62
 */
63
CURLcode curlx_base64_decode(const char *src,
64
                             uint8_t **outptr, size_t *outlen)
65
4.43k
{
66
4.43k
  size_t srclen = 0;
67
4.43k
  size_t padding = 0;
68
4.43k
  size_t i;
69
4.43k
  size_t numQuantums;
70
4.43k
  size_t fullQuantums;
71
4.43k
  size_t rawlen = 0;
72
4.43k
  unsigned char *pos;
73
4.43k
  unsigned char *newstr;
74
4.43k
  unsigned char lookup[256];
75
76
4.43k
  *outptr = NULL;
77
4.43k
  *outlen = 0;
78
4.43k
  srclen = strlen(src);
79
80
  /* Check the length of the input string is valid */
81
4.43k
  if(!srclen || srclen % 4)
82
2.03k
    return CURLE_BAD_CONTENT_ENCODING;
83
84
  /* srclen is at least 4 here */
85
4.05k
  while(src[srclen - 1 - padding] == '=') {
86
    /* count padding characters */
87
1.69k
    padding++;
88
    /* A maximum of two = padding characters is allowed */
89
1.69k
    if(padding > 2)
90
35
      return CURLE_BAD_CONTENT_ENCODING;
91
1.69k
  }
92
93
  /* Calculate the number of quantums */
94
2.35k
  numQuantums = srclen / 4;
95
2.35k
  fullQuantums = numQuantums - (padding ? 1 : 0);
96
97
  /* Calculate the size of the decoded string */
98
2.35k
  rawlen = (numQuantums * 3) - padding;
99
100
  /* Allocate our buffer including room for a null-terminator */
101
2.35k
  newstr = curlx_malloc(rawlen + 1);
102
2.35k
  if(!newstr)
103
0
    return CURLE_OUT_OF_MEMORY;
104
105
2.35k
  pos = newstr;
106
107
2.35k
  memset(lookup, 0xff, sizeof(lookup));
108
2.35k
  memcpy(&lookup['+'], decodetable, sizeof(decodetable));
109
110
  /* Decode the complete quantums first */
111
7.59k
  for(i = 0; i < fullQuantums; i++) {
112
6.45k
    unsigned char val;
113
6.45k
    unsigned int x = 0;
114
6.45k
    int j;
115
116
28.5k
    for(j = 0; j < 4; j++) {
117
23.3k
      val = lookup[(unsigned char)*src++];
118
23.3k
      if(val == 0xff) /* bad symbol */
119
1.22k
        goto bad;
120
22.1k
      x = (x << 6) | val;
121
22.1k
    }
122
5.23k
    pos[2] = x & 0xff;
123
5.23k
    pos[1] = (x >> 8) & 0xff;
124
5.23k
    pos[0] = (x >> 16) & 0xff;
125
5.23k
    pos += 3;
126
5.23k
  }
127
1.13k
  if(padding) {
128
    /* this means either 8 or 16 bits output */
129
870
    unsigned char val;
130
870
    unsigned int x = 0;
131
870
    int j;
132
870
    size_t padc = 0;
133
2.28k
    for(j = 0; j < 4; j++) {
134
2.18k
      if(*src == '=') {
135
372
        x <<= 6;
136
372
        src++;
137
372
        if(++padc > padding)
138
          /* this is a badly placed '=' symbol! */
139
107
          goto bad;
140
372
      }
141
1.81k
      else {
142
1.81k
        val = lookup[(unsigned char)*src++];
143
1.81k
        if(val == 0xff) /* bad symbol */
144
662
          goto bad;
145
1.15k
        x = (x << 6) | val;
146
1.15k
      }
147
2.18k
    }
148
101
    if(padding == 1)
149
83
      pos[1] = (x >> 8) & 0xff;
150
101
    pos[0] = (x >> 16) & 0xff;
151
101
    pos += 3 - padding;
152
101
  }
153
154
  /* Zero terminate */
155
366
  *pos = '\0';
156
157
  /* Return the decoded data */
158
366
  *outptr = newstr;
159
366
  *outlen = rawlen;
160
161
366
  return CURLE_OK;
162
1.99k
bad:
163
1.99k
  curlx_free(newstr);
164
1.99k
  return CURLE_BAD_CONTENT_ENCODING;
165
1.13k
}
166
167
static CURLcode base64_encode(const char *table64,
168
                              uint8_t padbyte,
169
                              const uint8_t *inputbuff, size_t insize,
170
                              char **outptr, size_t *outlen)
171
26.0k
{
172
26.0k
  char *output;
173
26.0k
  char *base64data;
174
26.0k
  const unsigned char *in = (const unsigned char *)inputbuff;
175
176
26.0k
  *outptr = NULL;
177
26.0k
  *outlen = 0;
178
179
26.0k
  if(!insize)
180
0
    return CURLE_OK;
181
182
  /* safety precaution */
183
26.0k
  DEBUGASSERT(insize <= CURL_MAX_BASE64_INPUT);
184
26.0k
  if(insize > CURL_MAX_BASE64_INPUT)
185
0
    return CURLE_TOO_LARGE;
186
187
26.0k
  base64data = output = curlx_malloc((insize + 2) / 3 * 4 + 1);
188
26.0k
  if(!output)
189
0
    return CURLE_OUT_OF_MEMORY;
190
191
6.06M
  while(insize >= 3) {
192
6.03M
    *output++ = table64[in[0] >> 2];
193
6.03M
    *output++ = table64[((in[0] & 0x03) << 4) | (in[1] >> 4)];
194
6.03M
    *output++ = table64[((in[1] & 0x0F) << 2) | ((in[2] & 0xC0) >> 6)];
195
6.03M
    *output++ = table64[in[2] & 0x3F];
196
6.03M
    insize -= 3;
197
6.03M
    in += 3;
198
6.03M
  }
199
26.0k
  if(insize) {
200
    /* this is only one or two bytes now */
201
20.5k
    *output++ = table64[in[0] >> 2];
202
20.5k
    if(insize == 1) {
203
7.60k
      *output++ = table64[((in[0] & 0x03) << 4)];
204
7.60k
      if(padbyte) {
205
7.60k
        *output++ = padbyte;
206
7.60k
        *output++ = padbyte;
207
7.60k
      }
208
7.60k
    }
209
12.9k
    else {
210
      /* insize == 2 */
211
12.9k
      *output++ = table64[((in[0] & 0x03) << 4) | ((in[1] & 0xF0) >> 4)];
212
12.9k
      *output++ = table64[((in[1] & 0x0F) << 2)];
213
12.9k
      if(padbyte)
214
12.9k
        *output++ = padbyte;
215
12.9k
    }
216
20.5k
  }
217
218
  /* Zero terminate */
219
26.0k
  *output = '\0';
220
221
  /* Return the pointer to the new data (allocated memory) */
222
26.0k
  *outptr = base64data;
223
224
  /* Return the length of the new data */
225
26.0k
  *outlen = (size_t)(output - base64data);
226
227
26.0k
  return CURLE_OK;
228
26.0k
}
229
230
/*
231
 * curlx_base64_encode()
232
 *
233
 * Given a pointer to an input buffer and an input size, encode it and
234
 * return a pointer in *outptr to a newly allocated memory area holding
235
 * encoded data. Size of encoded data is returned in variable pointed by
236
 * outlen.
237
 *
238
 * Returns CURLE_OK on success, otherwise specific error code. Function
239
 * output shall not be considered valid unless CURLE_OK is returned.
240
 *
241
 * @unittest: 1302
242
 */
243
CURLcode curlx_base64_encode(const uint8_t *inputbuff, size_t insize,
244
                             char **outptr, size_t *outlen)
245
25.6k
{
246
25.6k
  return base64_encode(Curl_base64encdec, '=',
247
25.6k
                       inputbuff, insize, outptr, outlen);
248
25.6k
}
249
250
/*
251
 * curlx_base64url_encode()
252
 *
253
 * Given a pointer to an input buffer and an input size, encode it and
254
 * return a pointer in *outptr to a newly allocated memory area holding
255
 * encoded data. Size of encoded data is returned in variable pointed by
256
 * outlen.
257
 *
258
 * Input length of 0 indicates input buffer holds a null-terminated string.
259
 *
260
 * Returns CURLE_OK on success, otherwise specific error code. Function
261
 * output shall not be considered valid unless CURLE_OK is returned.
262
 *
263
 * @unittest: 1302
264
 */
265
CURLcode curlx_base64url_encode(const uint8_t *inputbuff, size_t insize,
266
                                char **outptr, size_t *outlen)
267
365
{
268
365
  return base64_encode(base64url, 0, inputbuff, insize, outptr, outlen);
269
365
}