master
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 ***************************************************************************/
25/* Base64 encoding/decoding */
27#include "curl_setup.h"
29#include "curlx/base64.h"
31/* ---- Base64 Encoding/Decoding Table --- */
32const char curlx_base64encdec[] =
33 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
35/* The Base 64 encoding with a URL and filename safe alphabet, RFC 4648
36 section 5 */
37static const char base64url[] =
38 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
40static const unsigned char decodetable[256] = {
41 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
42 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
43 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
44 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
45 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
46 0xff, 0xff, 0xff, 62, 0xff, 0xff, 0xff, 63,
47 52, 53, 54, 55, 56, 57, 58, 59,
48 60, 61, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
49 0xff, 0, 1, 2, 3, 4, 5, 6,
50 7, 8, 9, 10, 11, 12, 13, 14,
51 15, 16, 17, 18, 19, 20, 21, 22,
52 23, 24, 25, 0xff, 0xff, 0xff, 0xff, 0xff,
53 0xff, 26, 27, 28, 29, 30, 31, 32,
54 33, 34, 35, 36, 37, 38, 39, 40,
55 41, 42, 43, 44, 45, 46, 47, 48,
56 49, 50, 51, 0xff, 0xff, 0xff, 0xff, 0xff,
57 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
58 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
59 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
60 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
61 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
62 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
63 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
64 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
65 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
66 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
67 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
68 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
69 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
70 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
71 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
72 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
73};
74/*
75 * curlx_base64_decode()
76 *
77 * Given a base64 null-terminated string at src, decode it and return a
78 * pointer in *outptr to a newly allocated memory area holding decoded data.
79 * Size of decoded data is returned in variable pointed by outlen.
80 *
81 * Returns CURLE_OK on success, otherwise specific error code. Function
82 * output shall not be considered valid unless CURLE_OK is returned.
83 *
84 * When decoded data length is 0, returns NULL in *outptr.
85 *
86 * @unittest: 1302
87 */
88CURLcode curlx_base64_decode(const char *src,
89 uint8_t **outptr, size_t *outlen)
90{
91 size_t srclen = 0;
92 size_t padding = 0;
93 size_t i;
94 size_t numQuantums;
95 size_t fullQuantums;
96 size_t rawlen = 0;
97 unsigned char *pos;
98 unsigned char *newstr;
100 *outptr = NULL;
101 *outlen = 0;
102 srclen = strlen(src);
104 /* Check the length of the input string is valid */
105 if(!srclen || srclen % 4)
106 return CURLE_BAD_CONTENT_ENCODING;
108 /* srclen is at least 4 here */
109 while(src[srclen - 1 - padding] == '=') {
110 /* count padding characters */
111 padding++;
112 /* A maximum of two = padding characters is allowed */
113 if(padding > 2)
114 return CURLE_BAD_CONTENT_ENCODING;
115 }
117 /* Calculate the number of quantums */
118 numQuantums = srclen / 4;
119 fullQuantums = numQuantums - (padding ? 1 : 0);
121 /* Calculate the size of the decoded string */
122 rawlen = (numQuantums * 3) - padding;
124 /* Allocate our buffer including room for a null-terminator */
125 newstr = curlx_malloc(rawlen + 1);
126 if(!newstr)
127 return CURLE_OUT_OF_MEMORY;
129 pos = newstr;
131 /* Decode the complete quantums first */
132 for(i = 0; i < fullQuantums; i++) {
133 unsigned char v0 = decodetable[(unsigned char)src[0]];
134 unsigned char v1 = decodetable[(unsigned char)src[1]];
135 unsigned char v2 = decodetable[(unsigned char)src[2]];
136 unsigned char v3 = decodetable[(unsigned char)src[3]];
137 if((v0 | v1 | v2 | v3) & 0x80)
138 goto bad;
139 pos[0] = (unsigned char)((v0 << 2) | (v1 >> 4));
140 pos[1] = (unsigned char)((v1 << 4) | (v2 >> 2));
141 pos[2] = (unsigned char)((v2 << 6) | v3);
142 pos += 3;
143 src += 4;
144 }
145 if(padding) {
146 /* this means either 8 or 16 bits output */
147 unsigned char val;
148 unsigned int x = 0;
149 int j;
150 size_t padc = 0;
151 for(j = 0; j < 4; j++) {
152 if(*src == '=') {
153 x <<= 6;
154 src++;
155 if(++padc > padding)
156 /* this is a badly placed '=' symbol! */
157 goto bad;
158 }
159 else {
160 val = decodetable[(unsigned char)*src++];
161 if(val == 0xff) /* bad symbol */
162 goto bad;
163 x = (x << 6) | val;
164 }
165 }
166 if(padding == 1)
167 pos[1] = (unsigned char)((x >> 8) & 0xff);
168 pos[0] = (unsigned char)((x >> 16) & 0xff);
169 pos += 3 - padding;
170 }
172 /* null-terminate */
173 *pos = '\0';
175 /* Return the decoded data */
176 *outptr = newstr;
177 *outlen = rawlen;
179 return CURLE_OK;
180bad:
181 curlx_free(newstr);
182 return CURLE_BAD_CONTENT_ENCODING;
183}
185static CURLcode base64_encode(const char *table64,
186 uint8_t padbyte,
187 const uint8_t *inputbuff, size_t insize,
188 char **outptr, size_t *outlen)
189{
190 char *output;
191 char *base64data;
192 const unsigned char *in = (const unsigned char *)inputbuff;
194 *outptr = NULL;
195 *outlen = 0;
197 if(!insize)
198 return CURLE_OK;
200 /* safety precaution */
201 DEBUGASSERT(insize <= CURL_MAX_BASE64_INPUT);
202 if(insize > CURL_MAX_BASE64_INPUT)
203 return CURLE_TOO_LARGE;
205 base64data = output = curlx_malloc(((insize + 2) / 3 * 4) + 1);
206 if(!output)
207 return CURLE_OUT_OF_MEMORY;
209 while(insize >= 3) {
210 /* Load all three input bytes before storing output, avoiding reloads when
211 the compiler cannot rule out aliasing. Extract four 6-bit indices from
212 the packed value; byte-wise loads avoid alignment and endian issues. */
213 uint32_t bits = ((uint32_t)in[0] << 16) |
214 ((uint32_t)in[1] << 8) | in[2];
215 *output++ = table64[bits >> 18];
216 *output++ = table64[(bits >> 12) & 0x3F];
217 *output++ = table64[(bits >> 6) & 0x3F];
218 *output++ = table64[bits & 0x3F];
219 insize -= 3;
220 in += 3;
221 }
222 if(insize) {
223 /* this is only one or two bytes now */
224 *output++ = table64[in[0] >> 2];
225 if(insize == 1) {
226 *output++ = table64[((in[0] & 0x03) << 4)];
227 if(padbyte) {
228 *output++ = padbyte;
229 *output++ = padbyte;
230 }
231 }
232 else {
233 /* insize == 2 */
234 *output++ = table64[((in[0] & 0x03) << 4) | ((in[1] & 0xF0) >> 4)];
235 *output++ = table64[((in[1] & 0x0F) << 2)];
236 if(padbyte)
237 *output++ = padbyte;
238 }
239 }
241 /* null-terminate */
242 *output = '\0';
244 /* Return the pointer to the new data (allocated memory) */
245 *outptr = base64data;
247 /* Return the length of the new data */
248 *outlen = (size_t)(output - base64data);
250 return CURLE_OK;
251}
253/*
254 * curlx_base64_encode()
255 *
256 * Given a pointer to an input buffer and an input size, encode it and
257 * return a pointer in *outptr to a newly allocated memory area holding
258 * encoded data. Size of encoded data is returned in variable pointed by
259 * outlen.
260 *
261 * Returns CURLE_OK on success, otherwise specific error code. Function
262 * output shall not be considered valid unless CURLE_OK is returned.
263 *
264 * @unittest: 1302
265 */
266CURLcode curlx_base64_encode(const uint8_t *inputbuff, size_t insize,
267 char **outptr, size_t *outlen)
268{
269 return base64_encode(curlx_base64encdec, '=',
270 inputbuff, insize, outptr, outlen);
271}
273/*
274 * curlx_base64url_encode()
275 *
276 * Given a pointer to an input buffer and an input size, encode it and
277 * return a pointer in *outptr to a newly allocated memory area holding
278 * encoded data. Size of encoded data is returned in variable pointed by
279 * outlen.
280 *
281 * Input length of 0 indicates input buffer holds a null-terminated string.
282 *
283 * Returns CURLE_OK on success, otherwise specific error code. Function
284 * output shall not be considered valid unless CURLE_OK is returned.
285 *
286 * @unittest: 1302
287 */
288CURLcode curlx_base64url_encode(const uint8_t *inputbuff, size_t insize,
289 char **outptr, size_t *outlen)
290{
291 return base64_encode(base64url, 0, inputbuff, insize, outptr, outlen);
292}
Diagnostics4 git calls · 199 ms
CallStatusSizeTime0199 ms
discover200152 B2 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 git.response.bytes=152 git.server.agent=gitty/1.0 git.server.object_format=sha1 git.server.caps=agent ls-refs fetch object-format git.server.fetch=shallow filter sideband-all git.protocol.version=2 git.pkt.frames=8 git.pkt.data=6 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=152 git.pkt.payload.max=34
ls-refs200149 B · hit2 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 git.response.bytes=149 git.response.cache=HIT git.response.duration_ms=2 git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=149 git.pkt.payload.max=78 git.ls_refs.count=2 git.ls_refs.head=refs/heads/master
fetchblob:none · deepen 10200287 KiB · hit4 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=4 git.response.bytes=293470 git.response.cache=HIT git.response.duration_ms=4 git.fetch.filter=blob:none git.fetch.filter.requested=tree:0 git.fetch.filter.fallback=blob:none git.fetch.filter.learned=false git.fetch.wants=31c911efe5652725e930f4d2e60c9060e5f6646d git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=10 git.pkt.data=8 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=293470 git.pkt.payload.max=65516 git.sideband.band1.bytes=293354 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=87 git.pack.bytes=293354 git.pack.ofs_delta=0 git.pack.ref_delta=1 git.pack.chain.max=1 git.pack.trailer=6a646bcda6740bab42e321b51998b52ea15146c0 git.pack.full=86Open pack map
fetch2002.9 KiB193 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=193 git.response.bytes=2935 git.response.cache=MISS git.response.duration_ms=193 git.fetch.wants=c0d842fef2daf72fe11592e9fbafba00cb98bdd8 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=2935 git.pkt.payload.max=2914 git.sideband.band1.bytes=2913 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/curl.git
- Ref
- refs/heads/master
- Commit
- 31c911efe5652725e930f4d2e60c9060e5f6646d
- cf-ray
- a5e2fbdf3c43cf81
- Request
- b6b19d7a…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format