| 1 |
/* |
| 2 |
* SPDX-License-Identifier: ISC |
| 3 |
* |
| 4 |
* Copyright (c) 1996, 1998 by Internet Software Consortium. |
| 5 |
* |
| 6 |
* Permission to use, copy, modify, and distribute this software for any |
| 7 |
* purpose with or without fee is hereby granted, provided that the above |
| 8 |
* copyright notice and this permission notice appear in all copies. |
| 9 |
* |
| 10 |
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS |
| 11 |
* ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES |
| 12 |
* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE |
| 13 |
* CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL |
| 14 |
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 15 |
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS |
| 16 |
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS |
| 17 |
* SOFTWARE. |
| 18 |
*/ |
| 19 |
|
| 20 |
/* |
| 21 |
* Portions Copyright (c) 1995 by International Business Machines, Inc. |
| 22 |
* |
| 23 |
* International Business Machines, Inc. (hereinafter called IBM) grants |
| 24 |
* permission under its copyrights to use, copy, modify, and distribute this |
| 25 |
* Software with or without fee, provided that the above copyright notice and |
| 26 |
* all paragraphs of this notice appear in all copies, and that the name of IBM |
| 27 |
* not be used in connection with the marketing of any product incorporating |
| 28 |
* the Software or modifications thereof, without specific, written prior |
| 29 |
* permission. |
| 30 |
* |
| 31 |
* To the extent it has a right to do so, IBM grants an immunity from suit |
| 32 |
* under its patents, if any, for the use, sale or manufacture of products to |
| 33 |
* the extent that such products are used for performing Domain Name System |
| 34 |
* dynamic updates in TCP/IP networks by means of the Software. No immunity is |
| 35 |
* granted for any product per se or for any other function of any product. |
| 36 |
* |
| 37 |
* THE SOFTWARE IS PROVIDED "AS IS", AND IBM DISCLAIMS ALL WARRANTIES, |
| 38 |
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A |
| 39 |
* PARTICULAR PURPOSE. IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL, |
| 40 |
* DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER ARISING |
| 41 |
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, EVEN |
| 42 |
* IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES. |
| 43 |
*/ |
| 44 |
|
| 45 |
#include "config.h" |
| 46 |
|
| 47 |
#include "cockpitbase64.h" |
| 48 |
|
| 49 |
#include <assert.h> |
| 50 |
#include <ctype.h> |
| 51 |
#include <limits.h> |
| 52 |
#include <stdlib.h> |
| 53 |
#include <string.h> |
| 54 |
|
| 55 |
static const char Base64[] = |
| 56 |
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
| 57 |
|
| 58 |
static const char Pad64 = '='; |
| 59 |
|
| 60 |
/* skips all whitespace anywhere. |
| 61 |
converts characters, four at a time, starting at (or after) |
| 62 |
src from base - 64 numbers into three 8 bit bytes in the target area. |
| 63 |
it returns the number of data bytes stored at the target, or -1 on error. |
| 64 |
*/ |
| 65 |
|
| 66 |
ssize_t |
| 67 |
cockpit_base64_pton (const char *src, |
| 68 |
size_t length, |
| 69 |
unsigned char *target, |
| 70 |
size_t targsize) |
| 71 |
{ |
| 72 |
int tarindex, state, ch; |
| 73 |
const char *pos; |
| 74 |
const char *end; |
| 75 |
|
| 76 |
state = 0; |
| 77 |
tarindex = 0; |
| 78 |
end = src + length; |
| 79 |
|
| 80 |
/* We can't rely on the null terminator */ |
| 81 |
#define next_char(src, end) \ |
| 82 |
(((src) == (end)) ? '\0': *(src)++) |
| 83 |
|
| 84 |
while ((ch = next_char (src, end)) != '\0') |
| 85 |
{ |
| 86 |
if (isspace ((unsigned char) ch)) /* Skip whitespace anywhere. */ |
| 87 |
continue; |
| 88 |
|
| 89 |
if (ch == Pad64) |
| 90 |
break; |
| 91 |
|
| 92 |
pos = strchr (Base64, ch); |
| 93 |
if (pos == 0) /* A non-base64 character */ |
| 94 |
return (-1); |
| 95 |
|
| 96 |
switch (state) |
| 97 |
{ |
| 98 |
case 0: |
| 99 |
if (target) |
| 100 |
{ |
| 101 |
if ((size_t)tarindex >= targsize) |
| 102 |
return (-1); |
| 103 |
target[tarindex] = (pos - Base64) << 2; |
| 104 |
} |
| 105 |
state = 1; |
| 106 |
break; |
| 107 |
case 1: |
| 108 |
if (target) |
| 109 |
{ |
| 110 |
if ((size_t) tarindex + 1 >= targsize) |
| 111 |
return (-1); |
| 112 |
target[tarindex] |= (pos - Base64) >> 4; |
| 113 |
target[tarindex + 1] = ((pos - Base64) & 0x0f) << 4; |
| 114 |
} |
| 115 |
tarindex++; |
| 116 |
state = 2; |
| 117 |
break; |
| 118 |
case 2: |
| 119 |
if (target) |
| 120 |
{ |
| 121 |
if ((size_t) tarindex + 1 >= targsize) |
| 122 |
return (-1); |
| 123 |
target[tarindex] |= (pos - Base64) >> 2; |
| 124 |
target[tarindex + 1] = ((pos - Base64) & 0x03) << 6; |
| 125 |
} |
| 126 |
tarindex++; |
| 127 |
state = 3; |
| 128 |
break; |
| 129 |
case 3: |
| 130 |
if (target) |
| 131 |
{ |
| 132 |
if ((size_t) tarindex >= targsize) |
| 133 |
return (-1); |
| 134 |
target[tarindex] |= (pos - Base64); |
| 135 |
} |
| 136 |
tarindex++; |
| 137 |
state = 0; |
| 138 |
break; |
| 139 |
default: |
| 140 |
abort(); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
/* |
| 145 |
* We are done decoding Base-64 chars. Let's see if we ended |
| 146 |
* on a byte boundary, and/or with erroneous trailing characters. |
| 147 |
*/ |
| 148 |
|
| 149 |
if (ch == Pad64) |
| 150 |
{ /* We got a pad char. */ |
| 151 |
ch = next_char (src, end); /* Skip it, get next. */ |
| 152 |
switch (state) |
| 153 |
{ |
| 154 |
case 0: /* Invalid = in first position */ |
| 155 |
case 1: /* Invalid = in second position */ |
| 156 |
return (-1); |
| 157 |
|
| 158 |
case 2: /* Valid, means one byte of info */ |
| 159 |
/* Skip any number of spaces. */ |
| 160 |
for ((void) NULL; ch != '\0'; ch = next_char (src, end)) |
| 161 |
{ |
| 162 |
if (!isspace((unsigned char) ch)) |
| 163 |
break; |
| 164 |
} |
| 165 |
/* Make sure there is another trailing = sign. */ |
| 166 |
if (ch != Pad64) |
| 167 |
return (-1); |
| 168 |
ch = next_char (src, end); /* Skip the = */ |
| 169 |
/* Fall through to "single trailing =" case. */ |
| 170 |
/* FALLTHROUGH */ |
| 171 |
|
| 172 |
case 3: /* Valid, means two bytes of info */ |
| 173 |
/* |
| 174 |
* We know this char is an =. Is there anything but |
| 175 |
* whitespace after it? |
| 176 |
*/ |
| 177 |
for ((void)NULL; src != end; ch = next_char (src, end)) |
| 178 |
{ |
| 179 |
if (!isspace((unsigned char) ch)) |
| 180 |
return (-1); |
| 181 |
} |
| 182 |
|
| 183 |
/* |
| 184 |
* Now make sure for cases 2 and 3 that the "extra" |
| 185 |
* bits that slopped past the last full byte were |
| 186 |
* zeros. If we don't check them, they become a |
| 187 |
* subliminal channel. |
| 188 |
*/ |
| 189 |
if (target && target[tarindex] != 0) |
| 190 |
return (-1); |
| 191 |
} |
| 192 |
} |
| 193 |
else |
| 194 |
{ |
| 195 |
/* |
| 196 |
* We ended by seeing the end of the string. Make sure we |
| 197 |
* have no partial bytes lying around. |
| 198 |
*/ |
| 199 |
if (state != 0) |
| 200 |
return (-1); |
| 201 |
} |
| 202 |
|
| 203 |
return (tarindex); |
| 204 |
} |
| 205 |
|
| 206 |
ssize_t |
| 207 |
cockpit_base64_ntop (const unsigned char *src, |
| 208 |
size_t srclength, |
| 209 |
char *target, |
| 210 |
size_t targsize) |
| 211 |
{ |
| 212 |
size_t len = 0; |
| 213 |
unsigned char input[3]; |
| 214 |
unsigned char output[4]; |
| 215 |
size_t i; |
| 216 |
|
| 217 |
while (srclength > 0) |
| 218 |
{ |
| 219 |
if (srclength >= 3) |
| 220 |
{ |
| 221 |
input[0] = *src++; |
| 222 |
input[1] = *src++; |
| 223 |
input[2] = *src++; |
| 224 |
srclength -= 3; |
| 225 |
|
| 226 |
output[0] = input[0] >> 2; |
| 227 |
output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4); |
| 228 |
output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6); |
| 229 |
output[3] = input[2] & 0x3f; |
| 230 |
|
| 231 |
} |
| 232 |
else |
| 233 |
{ |
| 234 |
/* srclength 1 or 2: Get what's left. */ |
| 235 |
input[0] = input[1] = input[2] = '\0'; |
| 236 |
for (i = 0; i < srclength; i++) |
| 237 |
input[i] = *src++; |
| 238 |
|
| 239 |
output[0] = input[0] >> 2; |
| 240 |
output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4); |
| 241 |
if (srclength == 1) |
| 242 |
output[2] = 255; |
| 243 |
else |
| 244 |
output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6); |
| 245 |
output[3] = 255; |
| 246 |
|
| 247 |
srclength = 0; |
| 248 |
} |
| 249 |
|
| 250 |
for (i = 0; i < 4; i++) |
| 251 |
{ |
| 252 |
assert(output[i] == 255 || output[i] < 64); |
| 253 |
assert (len + 1 < targsize); |
| 254 |
|
| 255 |
if (output[i] == 255) |
| 256 |
target[len++] = Pad64; |
| 257 |
else |
| 258 |
target[len++] = Base64[output[i]]; |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
assert (len < targsize); |
| 263 |
target[len] = '\0'; /* Returned value doesn't count \0. */ |
| 264 |
return len; |
| 265 |
} |