Coverage Report

Created: 2024-05-04 12:45

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