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
|
/*
* Embedded Linux library
* Copyright (C) 2015 Intel Corporation
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdint.h>
#include "utf8.h"
#include "base64.h"
#include "private.h"
#include "useful.h"
#include <stdio.h>
LIB_EXPORT uint8_t *l_base64_decode(const char *in, size_t in_len,
size_t *n_written)
{
const char *ptr, *in_end = in + in_len;
const char *base64_end = NULL;
uint8_t *out_buf, *out;
int base64_len = 0, pad_len = 0;
uint16_t reg = 0;
for (ptr = in; ptr < in_end; ptr++) {
if (l_ascii_isspace(*ptr))
/* Whitespace */
continue;
else if (*ptr == '=') {
/* Final padding */
if (!pad_len)
base64_end = ptr;
pad_len++;
} else if (!pad_len && (l_ascii_isalnum(*ptr) || *ptr == '+' ||
*ptr == '/'))
/* Base64 character */
base64_len++;
else
/* Bad character */
return NULL;
}
if (ptr != in_end)
return NULL;
if ((base64_len & 3) == 1)
/* Invalid length */
return NULL;
if (pad_len != align_len(base64_len, 4) - base64_len)
return NULL;
/* No padding */
if (!base64_end)
base64_end = ptr;
*n_written = base64_len * 3 / 4;
out_buf = l_malloc(*n_written);
out = out_buf;
base64_len = 0;
for (ptr = in; ptr < base64_end; ptr++) {
if (l_ascii_isspace(*ptr))
/* Whitespace */
continue;
/* Base64 character */
reg <<= 6;
if (l_ascii_isupper(*ptr))
reg |= *ptr - 'A' + 0;
else if (l_ascii_islower(*ptr))
reg |= *ptr - 'a' + 26;
else if (l_ascii_isdigit(*ptr))
reg |= *ptr - '0' + 52;
else if (*ptr == '+')
reg |= 62;
else if (*ptr == '/')
reg |= 63;
if ((base64_len & 3) == 1)
*out++ = reg >> 4;
else if ((base64_len & 3) == 2)
*out++ = reg >> 2;
else if ((base64_len & 3) == 3)
*out++ = reg >> 0;
base64_len++;
}
return out_buf;
}
LIB_EXPORT char *l_base64_encode(const uint8_t *in, size_t in_len, int columns)
{
const uint8_t *in_end = in + in_len;
char *out_buf, *out;
size_t out_len;
uint32_t reg;
uint8_t idx;
int i, pad = 4;
int col = 0;
/* For simplicity allow multiples of 4 only */
if (columns & 3)
return NULL;
out_len = (in_len + 2) / 3 * 4;
if (columns && out_len)
out_len += (out_len - 4) / columns;
out_buf = l_malloc(out_len + 1);
out = out_buf;
while (in < in_end) {
reg = *in++ << 16;
if (in < in_end)
reg |= *in++ << 8;
else
pad--;
if (in < in_end)
reg |= *in++ << 0;
else
pad--;
if (columns && col == columns) {
*out++ = '\n';
col = 0;
}
col += 4;
for (i = 0; i < pad; i++) {
idx = (reg >> 18) & 63;
reg <<= 6;
if (idx < 26)
*out++ = idx + 'A';
else if (idx < 52)
*out++ = idx - 26 + 'a';
else if (idx < 62)
*out++ = idx - 52 + '0';
else
*out++ = (idx == 62) ? '+' : '/';
}
}
for (; pad < 4; pad++)
*out++ = '=';
*out = '\0';
return out_buf;
}
|