Coverage Report

Created: 2025-08-26 07:08

/src/PROJ/curl/lib/curlx/base64.c
Line
Count
Source (jump to first uncovered line)
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
/* The last 2 #include files should be in this order */
34
#ifdef BUILDING_LIBCURL
35
#include "../curl_memory.h"
36
#endif
37
#include "../memdebug.h"
38
39
/* ---- Base64 Encoding/Decoding Table --- */
40
const char Curl_base64encdec[]=
41
  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
42
43
/* The Base 64 encoding with a URL and filename safe alphabet, RFC 4648
44
   section 5 */
45
static const char base64url[]=
46
  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
47
48
static const unsigned char decodetable[] =
49
{ 62, 255, 255, 255, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, 255, 255,
50
  255, 255, 255, 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
51
  17, 18, 19, 20, 21, 22, 23, 24, 25, 255, 255, 255, 255, 255, 255, 26, 27, 28,
52
  29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
53
  48, 49, 50, 51 };
54
/*
55
 * curlx_base64_decode()
56
 *
57
 * Given a base64 null-terminated string at src, decode it and return a
58
 * pointer in *outptr to a newly allocated memory area holding decoded data.
59
 * Size of decoded data is returned in variable pointed by outlen.
60
 *
61
 * Returns CURLE_OK on success, otherwise specific error code. Function
62
 * output shall not be considered valid unless CURLE_OK is returned.
63
 *
64
 * When decoded data length is 0, returns NULL in *outptr.
65
 *
66
 * @unittest: 1302
67
 */
68
CURLcode curlx_base64_decode(const char *src,
69
                             unsigned char **outptr, size_t *outlen)
70
0
{
71
0
  size_t srclen = 0;
72
0
  size_t padding = 0;
73
0
  size_t i;
74
0
  size_t numQuantums;
75
0
  size_t fullQuantums;
76
0
  size_t rawlen = 0;
77
0
  unsigned char *pos;
78
0
  unsigned char *newstr;
79
0
  unsigned char lookup[256];
80
81
0
  *outptr = NULL;
82
0
  *outlen = 0;
83
0
  srclen = strlen(src);
84
85
  /* Check the length of the input string is valid */
86
0
  if(!srclen || srclen % 4)
87
0
    return CURLE_BAD_CONTENT_ENCODING;
88
89
  /* srclen is at least 4 here */
90
0
  while(src[srclen - 1 - padding] == '=') {
91
    /* count padding characters */
92
0
    padding++;
93
    /* A maximum of two = padding characters is allowed */
94
0
    if(padding > 2)
95
0
      return CURLE_BAD_CONTENT_ENCODING;
96
0
  }
97
98
  /* Calculate the number of quantums */
99
0
  numQuantums = srclen / 4;
100
0
  fullQuantums = numQuantums - (padding ? 1 : 0);
101
102
  /* Calculate the size of the decoded string */
103
0
  rawlen = (numQuantums * 3) - padding;
104
105
  /* Allocate our buffer including room for a null-terminator */
106
0
  newstr = malloc(rawlen + 1);
107
0
  if(!newstr)
108
0
    return CURLE_OUT_OF_MEMORY;
109
110
0
  pos = newstr;
111
112
0
  memset(lookup, 0xff, sizeof(lookup));
113
0
  memcpy(&lookup['+'], decodetable, sizeof(decodetable));
114
115
  /* Decode the complete quantums first */
116
0
  for(i = 0; i < fullQuantums; i++) {
117
0
    unsigned char val;
118
0
    unsigned int x = 0;
119
0
    int j;
120
121
0
    for(j = 0; j < 4; j++) {
122
0
      val = lookup[(unsigned char)*src++];
123
0
      if(val == 0xff) /* bad symbol */
124
0
        goto bad;
125
0
      x = (x << 6) | val;
126
0
    }
127
0
    pos[2] = x & 0xff;
128
0
    pos[1] = (x >> 8) & 0xff;
129
0
    pos[0] = (x >> 16) & 0xff;
130
0
    pos += 3;
131
0
  }
132
0
  if(padding) {
133
    /* this means either 8 or 16 bits output */
134
0
    unsigned char val;
135
0
    unsigned int x = 0;
136
0
    int j;
137
0
    size_t padc = 0;
138
0
    for(j = 0; j < 4; j++) {
139
0
      if(*src == '=') {
140
0
        x <<= 6;
141
0
        src++;
142
0
        if(++padc > padding)
143
          /* this is a badly placed '=' symbol! */
144
0
          goto bad;
145
0
      }
146
0
      else {
147
0
        val = lookup[(unsigned char)*src++];
148
0
        if(val == 0xff) /* bad symbol */
149
0
          goto bad;
150
0
        x = (x << 6) | val;
151
0
      }
152
0
    }
153
0
    if(padding == 1)
154
0
      pos[1] = (x >> 8) & 0xff;
155
0
    pos[0] = (x >> 16) & 0xff;
156
0
    pos += 3 - padding;
157
0
  }
158
159
  /* Zero terminate */
160
0
  *pos = '\0';
161
162
  /* Return the decoded data */
163
0
  *outptr = newstr;
164
0
  *outlen = rawlen;
165
166
0
  return CURLE_OK;
167
0
bad:
168
0
  free(newstr);
169
0
  return CURLE_BAD_CONTENT_ENCODING;
170
0
}
171
172
static CURLcode base64_encode(const char *table64,
173
                              unsigned char padbyte,
174
                              const char *inputbuff, size_t insize,
175
                              char **outptr, size_t *outlen)
176
0
{
177
0
  char *output;
178
0
  char *base64data;
179
0
  const unsigned char *in = (const unsigned char *)inputbuff;
180
181
0
  *outptr = NULL;
182
0
  *outlen = 0;
183
184
0
  if(!insize)
185
0
    insize = strlen(inputbuff);
186
187
#if SIZEOF_SIZE_T == 4
188
  if(insize > UINT_MAX/4)
189
    return CURLE_OUT_OF_MEMORY;
190
#endif
191
192
0
  base64data = output = malloc((insize + 2) / 3 * 4 + 1);
193
0
  if(!output)
194
0
    return CURLE_OUT_OF_MEMORY;
195
196
0
  while(insize >= 3) {
197
0
    *output++ = table64[ in[0] >> 2 ];
198
0
    *output++ = table64[ ((in[0] & 0x03) << 4) | (in[1] >> 4) ];
199
0
    *output++ = table64[ ((in[1] & 0x0F) << 2) | ((in[2] & 0xC0) >> 6) ];
200
0
    *output++ = table64[ in[2] & 0x3F ];
201
0
    insize -= 3;
202
0
    in += 3;
203
0
  }
204
0
  if(insize) {
205
    /* this is only one or two bytes now */
206
0
    *output++ = table64[ in[0] >> 2 ];
207
0
    if(insize == 1) {
208
0
      *output++ = table64[ ((in[0] & 0x03) << 4) ];
209
0
      if(padbyte) {
210
0
        *output++ = padbyte;
211
0
        *output++ = padbyte;
212
0
      }
213
0
    }
214
0
    else {
215
      /* insize == 2 */
216
0
      *output++ = table64[ ((in[0] & 0x03) << 4) | ((in[1] & 0xF0) >> 4) ];
217
0
      *output++ = table64[ ((in[1] & 0x0F) << 2) ];
218
0
      if(padbyte)
219
0
        *output++ = padbyte;
220
0
    }
221
0
  }
222
223
  /* Zero terminate */
224
0
  *output = '\0';
225
226
  /* Return the pointer to the new data (allocated memory) */
227
0
  *outptr = base64data;
228
229
  /* Return the length of the new data */
230
0
  *outlen = (size_t)(output - base64data);
231
232
0
  return CURLE_OK;
233
0
}
234
235
/*
236
 * curlx_base64_encode()
237
 *
238
 * Given a pointer to an input buffer and an input size, encode it and
239
 * return a pointer in *outptr to a newly allocated memory area holding
240
 * encoded data. Size of encoded data is returned in variable pointed by
241
 * outlen.
242
 *
243
 * Input length of 0 indicates input buffer holds a null-terminated string.
244
 *
245
 * Returns CURLE_OK on success, otherwise specific error code. Function
246
 * output shall not be considered valid unless CURLE_OK is returned.
247
 *
248
 * @unittest: 1302
249
 */
250
CURLcode curlx_base64_encode(const char *inputbuff, size_t insize,
251
                             char **outptr, size_t *outlen)
252
0
{
253
0
  return base64_encode(Curl_base64encdec, '=',
254
0
                       inputbuff, insize, outptr, outlen);
255
0
}
256
257
/*
258
 * curlx_base64url_encode()
259
 *
260
 * Given a pointer to an input buffer and an input size, encode it and
261
 * return a pointer in *outptr to a newly allocated memory area holding
262
 * encoded data. Size of encoded data is returned in variable pointed by
263
 * outlen.
264
 *
265
 * Input length of 0 indicates input buffer holds a null-terminated string.
266
 *
267
 * Returns CURLE_OK on success, otherwise specific error code. Function
268
 * output shall not be considered valid unless CURLE_OK is returned.
269
 *
270
 * @unittest: 1302
271
 */
272
CURLcode curlx_base64url_encode(const char *inputbuff, size_t insize,
273
                                char **outptr, size_t *outlen)
274
0
{
275
0
  return base64_encode(base64url, 0, inputbuff, insize, outptr, outlen);
276
0
}