1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
|
/*
* Copyright (c) 2012 Tim Ruehsen
* Copyright (c) 2015-2024 Free Software Foundation, Inc.
*
* This file is part of libwget.
*
* Libwget is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Libwget is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with libwget. If not, see <https://www.gnu.org/licenses/>.
*
*
* base64 conversion routines
*
* Changelog
* 21.12.2012 Tim Ruehsen created
*
*/
#include <config.h>
#include <stddef.h>
#include <stdarg.h>
#include <wget.h>
#include "private.h"
/**
* \file
* \brief Base64 functions
* \defgroup libwget-base64 Base64 functions
* @{
*
* This is a collection base64 encoding/decoding functions used in Wget2.
*/
static const unsigned char base64_2_bin[256] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 62, 0, 62, 0, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 0, 0, 0, 0, 0, 0,
0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 0, 0, 0, 0, 63,
0, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
static bool WGET_GCC_CONST isbase64(char c)
{
// isalnum(c) does not work for all locales
return base64_2_bin[(unsigned char) c] != 0;
// return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '+' || c == '/' || c == '-' || c == '_';
}
/**
* \param[in] src String to be checked
* \return 1 if \p src is a base64 string, 0 otherwise
*
* Checks whether \p src is a base64 string.
* Returns 0 if \p src is NULL.
*/
bool wget_base64_is_string(const char *src)
{
if (src) {
while (isbase64(*src)) src++;
if (!*src || (*src == '=' && src[1]) || (*src == '=' && src[1] == '=' && src[2]))
return 1;
}
return 0;
}
/**
* \param[out] dst Output buffer
* \param[in] src Base64 string to be decoded
* \param[in] n Length of \p src
* \return Number of bytes written into \p dst
*
* Decodes \p n bytes of the base64 string \p src.
* The decoded bytes are written into \p dst and are 0-terminated.
*
* The size of \p dst has to be at minimum ((\p n + 3) / 4) * 3 + 1 bytes.
*/
size_t wget_base64_decode(char *dst, const char *src, size_t n)
{
const unsigned char *usrc = (const unsigned char *)src;
char *old = dst;
int extra;
// trim '=' at the end
while (n > 0 && !isbase64(src[n - 1]))
n--;
extra = n & 3;
for (n /= 4; n > 0; n--, usrc += 4) {
*dst++ = (char)(base64_2_bin[usrc[0]] << 2 | base64_2_bin[usrc[1]] >> 4);
*dst++ = (char)((base64_2_bin[usrc[1]]&0x0F) << 4 | base64_2_bin[usrc[2]] >> 2);
*dst++ = (char)((base64_2_bin[usrc[2]]&0x03) << 6 | base64_2_bin[usrc[3]]);
}
switch (extra) {
case 1:
// this should not happen
*dst++ = (char) (base64_2_bin[usrc[0]] << 2);
break;
case 2:
*dst++ = (char)(base64_2_bin[usrc[0]] << 2 | base64_2_bin[usrc[1]] >> 4);
*dst = (char)((base64_2_bin[usrc[1]]&0x0F) << 4);
if (*dst) dst++;
break;
case 3:
*dst++ = (char)(base64_2_bin[usrc[0]] << 2 | base64_2_bin[usrc[1]] >> 4);
*dst++ = (char)((base64_2_bin[usrc[1]]&0x0F) << 4 | base64_2_bin[usrc[2]] >> 2);
*dst = (char)((base64_2_bin[usrc[2]]&0x03) << 6);
if (*dst) dst++;
break;
default: // 0: ignore
break;
}
*dst = 0;
return (size_t) (dst - old);
}
/**
* \param[in] src Base64 string to be decoded
* \param[in] n Length of \p src
* \param[out] outlen Length of returned string, may be NULL.
* \return Decoded bytes, zero terminated. Returns NULL if memory allocation failed.
*
* Decodes \p n bytes of the base64 string \p src.
* The decoded bytes are returned in an allocated buffer.
*
* You should free() the returned string when not needed any more.
*/
char *wget_base64_decode_alloc(const char *src, size_t n, size_t *outlen)
{
char *dst = wget_malloc(wget_base64_get_decoded_length(n));
if (!dst)
return NULL;
size_t _outlen = wget_base64_decode(dst, src, n);
if (outlen)
*outlen = _outlen;
return dst;
}
#define WGET_BASE64_URLENCODE 1
static size_t base64_encode(char *dst, const char *src, size_t n, int flags)
{
static const char base64unsafe[64] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static const char base64urlsafe[64] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
const char *base64 = (flags & WGET_BASE64_URLENCODE) ? base64urlsafe : base64unsafe;
const unsigned char *usrc = (const unsigned char *)src;
char *start = dst;
int extra = n % 3;
// convert line by line
for (n /= 3; n > 0; n--, usrc += 3) {
*dst++ = base64[usrc[0] >> 2];
*dst++ = base64[((usrc[0]&3) << 4) | (usrc[1] >> 4)];
*dst++ = base64[((usrc[1]&15) << 2) | (usrc[2] >> 6)];
*dst++ = base64[usrc[2]&0x3f];
}
// special case
if (extra == 1) {
*dst++ = base64[usrc[0] >> 2];
*dst++ = base64[(usrc[0]&3) << 4];
*dst++ = '=';
*dst++ = '=';
} else if (extra == 2) {
*dst++ = base64[usrc[0] >> 2];
*dst++ = base64[((usrc[0]&3) << 4) | (usrc[1] >> 4)];
*dst++ = base64[((usrc[1]&15) << 2)];
*dst++ = '=';
}
*dst = 0;
return (size_t) (dst - start);
}
/**
* \param[out] dst Base64 output string
* \param[in] src Input buffer
* \param[in] n Number of bytes to be encoded
* \return Length of output string \p dst
*
* Encodes \p n bytes from \p src into a base64 string.
* The encoded string is written into \p dst (0-terminated).
*
* The length of \p dst has to be at minimum ((\p n + 2) / 3) * 4 + 1 bytes.
*/
size_t wget_base64_encode(char *dst, const char *src, size_t n)
{
return base64_encode(dst, src, n, 0);
}
/**
* \param[out] dst Base64 output string (URL and filename safe)
* \param[in] src Input buffer
* \param[in] n Number of bytes to be encoded
* \return Length of output string \p dst
*
* Encodes \p n bytes from \p src into a base64 URL and filename safe string (see RFC 4648, 5.).
* The encoded string is written into \p dst (0-terminated).
*
* The length of \p dst has to be at minimum ((\p n + 2) / 3) * 4 + 1 bytes.
*/
size_t wget_base64_urlencode(char *dst, const char *src, size_t n)
{
return base64_encode(dst, src, n, WGET_BASE64_URLENCODE);
}
/**
* \param[in] src Input buffer
* \param[in] n Number of bytes to be encoded
* \return Base64 encoded string or NULL if memory allocation failed
*
* Encodes \p n bytes from input buffer \p src.
* The encoded string is returned in an allocated buffer.
*
* You should free() the returned string when not needed any more.
*/
char *wget_base64_encode_alloc(const char *src, size_t n)
{
char *dst = wget_malloc(wget_base64_get_encoded_length(n));
if (dst)
wget_base64_encode(dst, src, n);
return dst;
}
/**
* \param[in] fmt Printf-like format string
* \param[in] args Argument list
* \return Base64 encoded string
*
* Encodes the string constructed by \p fmt and \p args.
* The encoded string is returned in an allocated buffer.
*
* You should free() the returned string when not needed any more.
*/
char *wget_base64_encode_vprintf_alloc(const char *fmt, va_list args)
{
char *data = NULL;
size_t n;
n = wget_vasprintf(&data, fmt, args);
if (data) {
char *dst = wget_base64_encode_alloc(data, n);
xfree(data);
return dst;
}
return NULL;
}
/**
* \param[in] fmt Printf-like format string
* \param[in] ... Argument list
* \return Base64 encoded string
*
* Encodes the string constructed by \p fmt and the arguments.
* The encoded string is returned in an allocated buffer.
*
* You should free() the returned string when not needed any more.
*/
char *wget_base64_encode_printf_alloc(const char *fmt, ...)
{
char *dst;
va_list args;
va_start(args, fmt);
dst = wget_base64_encode_vprintf_alloc(fmt, args);
va_end(args);
return dst;
}
/**
* \fn static inline size_t wget_base64_get_decoded_length(size_t len)
* \param[in] len Length of base64 sequence
* \return Number of decoded bytes plus one (for 0-byte termination)
*
* Calculate the number of bytes needed for decoding a base64 sequence with length \p len.
*/
size_t wget_base64_get_decoded_length(size_t len)
{
return ((len + 3) / 4) * 3 + 1;
}
/**
* \fn static inline size_t wget_base64_get_encoded_length(size_t len)
* \param[in] len Number of (un-encoded) bytes
* \return Length of base64 encoding plus one (for 0-byte termination)
*
* Calculate the number of bytes needed for base64 encoding a byte sequence with length \p len,
* including the padding and 0-termination bytes.
*/
size_t wget_base64_get_encoded_length(size_t len)
{
return ((len + 2) / 3) * 4 + 1;
}
/**@}*/
|