| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <ctype.h> |
| 4 |
#include <stdio.h> |
| 5 |
|
| 6 |
#include "alloc-util.h" |
| 7 |
#include "hexdecoct.h" |
| 8 |
#include "memory-util.h" |
| 9 |
#include "string-util.h" |
| 10 |
|
| 11 |
char octchar(int x) { |
| 12 |
return '0' + (x & 7); |
| 13 |
} |
| 14 |
|
| 15 |
int unoctchar(char c) { |
| 16 |
|
| 17 |
if (c >= '0' && c <= '7') |
| 18 |
return c - '0'; |
| 19 |
|
| 20 |
return -EINVAL; |
| 21 |
} |
| 22 |
|
| 23 |
char decchar(int x) { |
| 24 |
return '0' + (x % 10); |
| 25 |
} |
| 26 |
|
| 27 |
int undecchar(char c) { |
| 28 |
|
| 29 |
if (c >= '0' && c <= '9') |
| 30 |
return c - '0'; |
| 31 |
|
| 32 |
return -EINVAL; |
| 33 |
} |
| 34 |
|
| 35 |
char hexchar(int x) { |
| 36 |
const char *table = LOWERCASE_HEXDIGITS; |
| 37 |
return table[x & 15]; |
| 38 |
} |
| 39 |
|
| 40 |
int unhexchar(char c) { |
| 41 |
|
| 42 |
if (c >= '0' && c <= '9') |
| 43 |
return c - '0'; |
| 44 |
|
| 45 |
if (c >= 'a' && c <= 'f') |
| 46 |
return c - 'a' + 10; |
| 47 |
|
| 48 |
if (c >= 'A' && c <= 'F') |
| 49 |
return c - 'A' + 10; |
| 50 |
|
| 51 |
return -EINVAL; |
| 52 |
} |
| 53 |
|
| 54 |
char* hexmem(const void *p, size_t l) { |
| 55 |
const uint8_t *x; |
| 56 |
char *r, *z; |
| 57 |
|
| 58 |
assert(p || l == 0); |
| 59 |
|
| 60 |
z = r = new(char, l * 2 + 1); |
| 61 |
if (!r) |
| 62 |
return NULL; |
| 63 |
|
| 64 |
for (x = p; x && x < (const uint8_t*) p + l; x++) { |
| 65 |
*(z++) = hexchar(*x >> 4); |
| 66 |
*(z++) = hexchar(*x & 15); |
| 67 |
} |
| 68 |
|
| 69 |
*z = 0; |
| 70 |
return r; |
| 71 |
} |
| 72 |
|
| 73 |
static int unhex_next(const char **p, size_t *l) { |
| 74 |
int r; |
| 75 |
|
| 76 |
assert(p); |
| 77 |
assert(l); |
| 78 |
|
| 79 |
/* Find the next non-whitespace character, and decode it. We |
| 80 |
* greedily skip all preceding and all following whitespace. */ |
| 81 |
|
| 82 |
for (;;) { |
| 83 |
if (*l == 0) |
| 84 |
return -EPIPE; |
| 85 |
|
| 86 |
if (!strchr(WHITESPACE, **p)) |
| 87 |
break; |
| 88 |
|
| 89 |
/* Skip leading whitespace */ |
| 90 |
(*p)++, (*l)--; |
| 91 |
} |
| 92 |
|
| 93 |
r = unhexchar(**p); |
| 94 |
if (r < 0) |
| 95 |
return r; |
| 96 |
|
| 97 |
for (;;) { |
| 98 |
(*p)++, (*l)--; |
| 99 |
|
| 100 |
if (*l == 0 || !strchr(WHITESPACE, **p)) |
| 101 |
break; |
| 102 |
|
| 103 |
/* Skip following whitespace */ |
| 104 |
} |
| 105 |
|
| 106 |
return r; |
| 107 |
} |
| 108 |
|
| 109 |
int unhexmem_full( |
| 110 |
const char *p, |
| 111 |
size_t l, |
| 112 |
bool secure, |
| 113 |
void **ret_data, |
| 114 |
size_t *ret_size) { |
| 115 |
|
| 116 |
_cleanup_free_ uint8_t *buf = NULL; |
| 117 |
size_t buf_size; |
| 118 |
const char *x; |
| 119 |
uint8_t *z; |
| 120 |
|
| 121 |
assert(p || l == 0); |
| 122 |
|
| 123 |
if (l == SIZE_MAX) |
| 124 |
l = strlen(p); |
| 125 |
|
| 126 |
/* Note that the calculation of memory size is an upper boundary, as we ignore whitespace while decoding */ |
| 127 |
buf_size = (l + 1) / 2 + 1; |
| 128 |
buf = malloc(buf_size); |
| 129 |
if (!buf) |
| 130 |
return -ENOMEM; |
| 131 |
|
| 132 |
CLEANUP_ERASE_PTR(secure ? &buf : NULL, buf_size); |
| 133 |
|
| 134 |
for (x = p, z = buf;;) { |
| 135 |
int a, b; |
| 136 |
|
| 137 |
a = unhex_next(&x, &l); |
| 138 |
if (a == -EPIPE) /* End of string */ |
| 139 |
break; |
| 140 |
if (a < 0) |
| 141 |
return a; |
| 142 |
|
| 143 |
b = unhex_next(&x, &l); |
| 144 |
if (b < 0) |
| 145 |
return b; |
| 146 |
|
| 147 |
*(z++) = (uint8_t) a << 4 | (uint8_t) b; |
| 148 |
} |
| 149 |
|
| 150 |
*z = 0; |
| 151 |
|
| 152 |
if (ret_size) |
| 153 |
*ret_size = (size_t) (z - buf); |
| 154 |
if (ret_data) |
| 155 |
*ret_data = TAKE_PTR(buf); |
| 156 |
|
| 157 |
return 0; |
| 158 |
} |
| 159 |
|
| 160 |
/* https://tools.ietf.org/html/rfc4648#section-6 |
| 161 |
* Notice that base32hex differs from base32 in the alphabet it uses. |
| 162 |
* The distinction is that the base32hex representation preserves the |
| 163 |
* order of the underlying data when compared as bytestrings, this is |
| 164 |
* useful when representing NSEC3 hashes, as one can then verify the |
| 165 |
* order of hashes directly from their representation. */ |
| 166 |
char base32hexchar(int x) { |
| 167 |
const char *table = DIGITS "ABCDEFGHIJKLMNOPQRSTUV"; |
| 168 |
return table[x & 31]; |
| 169 |
} |
| 170 |
|
| 171 |
int unbase32hexchar(char c) { |
| 172 |
unsigned offset; |
| 173 |
|
| 174 |
if (c >= '0' && c <= '9') |
| 175 |
return c - '0'; |
| 176 |
|
| 177 |
offset = '9' - '0' + 1; |
| 178 |
|
| 179 |
if (c >= 'A' && c <= 'V') |
| 180 |
return c - 'A' + offset; |
| 181 |
|
| 182 |
return -EINVAL; |
| 183 |
} |
| 184 |
|
| 185 |
char* base32hexmem(const void *p, size_t l, bool padding) { |
| 186 |
char *r, *z; |
| 187 |
const uint8_t *x; |
| 188 |
size_t len; |
| 189 |
|
| 190 |
assert(p || l == 0); |
| 191 |
|
| 192 |
if (padding) |
| 193 |
/* five input bytes makes eight output bytes, padding is added so we must round up */ |
| 194 |
len = 8 * (l + 4) / 5; |
| 195 |
else { |
| 196 |
/* same, but round down as there is no padding */ |
| 197 |
len = 8 * l / 5; |
| 198 |
|
| 199 |
switch (l % 5) { |
| 200 |
case 4: |
| 201 |
len += 7; |
| 202 |
break; |
| 203 |
case 3: |
| 204 |
len += 5; |
| 205 |
break; |
| 206 |
case 2: |
| 207 |
len += 4; |
| 208 |
break; |
| 209 |
case 1: |
| 210 |
len += 2; |
| 211 |
break; |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
z = r = malloc(len + 1); |
| 216 |
if (!r) |
| 217 |
return NULL; |
| 218 |
|
| 219 |
for (x = p; x < (const uint8_t*) p + (l / 5) * 5; x += 5) { |
| 220 |
/* x[0] == XXXXXXXX; x[1] == YYYYYYYY; x[2] == ZZZZZZZZ |
| 221 |
* x[3] == QQQQQQQQ; x[4] == WWWWWWWW */ |
| 222 |
*(z++) = base32hexchar(x[0] >> 3); /* 000XXXXX */ |
| 223 |
*(z++) = base32hexchar((x[0] & 7) << 2 | x[1] >> 6); /* 000XXXYY */ |
| 224 |
*(z++) = base32hexchar((x[1] & 63) >> 1); /* 000YYYYY */ |
| 225 |
*(z++) = base32hexchar((x[1] & 1) << 4 | x[2] >> 4); /* 000YZZZZ */ |
| 226 |
*(z++) = base32hexchar((x[2] & 15) << 1 | x[3] >> 7); /* 000ZZZZQ */ |
| 227 |
*(z++) = base32hexchar((x[3] & 127) >> 2); /* 000QQQQQ */ |
| 228 |
*(z++) = base32hexchar((x[3] & 3) << 3 | x[4] >> 5); /* 000QQWWW */ |
| 229 |
*(z++) = base32hexchar((x[4] & 31)); /* 000WWWWW */ |
| 230 |
} |
| 231 |
|
| 232 |
switch (l % 5) { |
| 233 |
case 4: |
| 234 |
*(z++) = base32hexchar(x[0] >> 3); /* 000XXXXX */ |
| 235 |
*(z++) = base32hexchar((x[0] & 7) << 2 | x[1] >> 6); /* 000XXXYY */ |
| 236 |
*(z++) = base32hexchar((x[1] & 63) >> 1); /* 000YYYYY */ |
| 237 |
*(z++) = base32hexchar((x[1] & 1) << 4 | x[2] >> 4); /* 000YZZZZ */ |
| 238 |
*(z++) = base32hexchar((x[2] & 15) << 1 | x[3] >> 7); /* 000ZZZZQ */ |
| 239 |
*(z++) = base32hexchar((x[3] & 127) >> 2); /* 000QQQQQ */ |
| 240 |
*(z++) = base32hexchar((x[3] & 3) << 3); /* 000QQ000 */ |
| 241 |
if (padding) |
| 242 |
*(z++) = '='; |
| 243 |
|
| 244 |
break; |
| 245 |
|
| 246 |
case 3: |
| 247 |
*(z++) = base32hexchar(x[0] >> 3); /* 000XXXXX */ |
| 248 |
*(z++) = base32hexchar((x[0] & 7) << 2 | x[1] >> 6); /* 000XXXYY */ |
| 249 |
*(z++) = base32hexchar((x[1] & 63) >> 1); /* 000YYYYY */ |
| 250 |
*(z++) = base32hexchar((x[1] & 1) << 4 | x[2] >> 4); /* 000YZZZZ */ |
| 251 |
*(z++) = base32hexchar((x[2] & 15) << 1); /* 000ZZZZ0 */ |
| 252 |
if (padding) { |
| 253 |
*(z++) = '='; |
| 254 |
*(z++) = '='; |
| 255 |
*(z++) = '='; |
| 256 |
} |
| 257 |
|
| 258 |
break; |
| 259 |
|
| 260 |
case 2: |
| 261 |
*(z++) = base32hexchar(x[0] >> 3); /* 000XXXXX */ |
| 262 |
*(z++) = base32hexchar((x[0] & 7) << 2 | x[1] >> 6); /* 000XXXYY */ |
| 263 |
*(z++) = base32hexchar((x[1] & 63) >> 1); /* 000YYYYY */ |
| 264 |
*(z++) = base32hexchar((x[1] & 1) << 4); /* 000Y0000 */ |
| 265 |
if (padding) { |
| 266 |
*(z++) = '='; |
| 267 |
*(z++) = '='; |
| 268 |
*(z++) = '='; |
| 269 |
*(z++) = '='; |
| 270 |
} |
| 271 |
|
| 272 |
break; |
| 273 |
|
| 274 |
case 1: |
| 275 |
*(z++) = base32hexchar(x[0] >> 3); /* 000XXXXX */ |
| 276 |
*(z++) = base32hexchar((x[0] & 7) << 2); /* 000XXX00 */ |
| 277 |
if (padding) { |
| 278 |
*(z++) = '='; |
| 279 |
*(z++) = '='; |
| 280 |
*(z++) = '='; |
| 281 |
*(z++) = '='; |
| 282 |
*(z++) = '='; |
| 283 |
*(z++) = '='; |
| 284 |
} |
| 285 |
|
| 286 |
break; |
| 287 |
} |
| 288 |
|
| 289 |
*z = 0; |
| 290 |
return r; |
| 291 |
} |
| 292 |
|
| 293 |
int unbase32hexmem(const char *p, size_t l, bool padding, void **mem, size_t *_len) { |
| 294 |
_cleanup_free_ uint8_t *r = NULL; |
| 295 |
int a, b, c, d, e, f, g, h; |
| 296 |
uint8_t *z; |
| 297 |
const char *x; |
| 298 |
size_t len; |
| 299 |
unsigned pad = 0; |
| 300 |
|
| 301 |
assert(p || l == 0); |
| 302 |
assert(mem); |
| 303 |
assert(_len); |
| 304 |
|
| 305 |
if (l == SIZE_MAX) |
| 306 |
l = strlen(p); |
| 307 |
|
| 308 |
/* padding ensures any base32hex input has input divisible by 8 */ |
| 309 |
if (padding && l % 8 != 0) |
| 310 |
return -EINVAL; |
| 311 |
|
| 312 |
if (padding) { |
| 313 |
/* strip the padding */ |
| 314 |
while (l > 0 && p[l - 1] == '=' && pad < 7) { |
| 315 |
pad++; |
| 316 |
l--; |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
/* a group of eight input bytes needs five output bytes, in case of |
| 321 |
* padding we need to add some extra bytes */ |
| 322 |
len = (l / 8) * 5; |
| 323 |
|
| 324 |
switch (l % 8) { |
| 325 |
case 7: |
| 326 |
len += 4; |
| 327 |
break; |
| 328 |
case 5: |
| 329 |
len += 3; |
| 330 |
break; |
| 331 |
case 4: |
| 332 |
len += 2; |
| 333 |
break; |
| 334 |
case 2: |
| 335 |
len += 1; |
| 336 |
break; |
| 337 |
case 0: |
| 338 |
break; |
| 339 |
default: |
| 340 |
return -EINVAL; |
| 341 |
} |
| 342 |
|
| 343 |
z = r = malloc(len + 1); |
| 344 |
if (!r) |
| 345 |
return -ENOMEM; |
| 346 |
|
| 347 |
for (x = p; x < p + (l / 8) * 8; x += 8) { |
| 348 |
/* a == 000XXXXX; b == 000YYYYY; c == 000ZZZZZ; d == 000WWWWW |
| 349 |
* e == 000SSSSS; f == 000QQQQQ; g == 000VVVVV; h == 000RRRRR */ |
| 350 |
a = unbase32hexchar(x[0]); |
| 351 |
if (a < 0) |
| 352 |
return -EINVAL; |
| 353 |
|
| 354 |
b = unbase32hexchar(x[1]); |
| 355 |
if (b < 0) |
| 356 |
return -EINVAL; |
| 357 |
|
| 358 |
c = unbase32hexchar(x[2]); |
| 359 |
if (c < 0) |
| 360 |
return -EINVAL; |
| 361 |
|
| 362 |
d = unbase32hexchar(x[3]); |
| 363 |
if (d < 0) |
| 364 |
return -EINVAL; |
| 365 |
|
| 366 |
e = unbase32hexchar(x[4]); |
| 367 |
if (e < 0) |
| 368 |
return -EINVAL; |
| 369 |
|
| 370 |
f = unbase32hexchar(x[5]); |
| 371 |
if (f < 0) |
| 372 |
return -EINVAL; |
| 373 |
|
| 374 |
g = unbase32hexchar(x[6]); |
| 375 |
if (g < 0) |
| 376 |
return -EINVAL; |
| 377 |
|
| 378 |
h = unbase32hexchar(x[7]); |
| 379 |
if (h < 0) |
| 380 |
return -EINVAL; |
| 381 |
|
| 382 |
*(z++) = (uint8_t) a << 3 | (uint8_t) b >> 2; /* XXXXXYYY */ |
| 383 |
*(z++) = (uint8_t) b << 6 | (uint8_t) c << 1 | (uint8_t) d >> 4; /* YYZZZZZW */ |
| 384 |
*(z++) = (uint8_t) d << 4 | (uint8_t) e >> 1; /* WWWWSSSS */ |
| 385 |
*(z++) = (uint8_t) e << 7 | (uint8_t) f << 2 | (uint8_t) g >> 3; /* SQQQQQVV */ |
| 386 |
*(z++) = (uint8_t) g << 5 | (uint8_t) h; /* VVVRRRRR */ |
| 387 |
} |
| 388 |
|
| 389 |
switch (l % 8) { |
| 390 |
case 7: |
| 391 |
a = unbase32hexchar(x[0]); |
| 392 |
if (a < 0) |
| 393 |
return -EINVAL; |
| 394 |
|
| 395 |
b = unbase32hexchar(x[1]); |
| 396 |
if (b < 0) |
| 397 |
return -EINVAL; |
| 398 |
|
| 399 |
c = unbase32hexchar(x[2]); |
| 400 |
if (c < 0) |
| 401 |
return -EINVAL; |
| 402 |
|
| 403 |
d = unbase32hexchar(x[3]); |
| 404 |
if (d < 0) |
| 405 |
return -EINVAL; |
| 406 |
|
| 407 |
e = unbase32hexchar(x[4]); |
| 408 |
if (e < 0) |
| 409 |
return -EINVAL; |
| 410 |
|
| 411 |
f = unbase32hexchar(x[5]); |
| 412 |
if (f < 0) |
| 413 |
return -EINVAL; |
| 414 |
|
| 415 |
g = unbase32hexchar(x[6]); |
| 416 |
if (g < 0) |
| 417 |
return -EINVAL; |
| 418 |
|
| 419 |
/* g == 000VV000 */ |
| 420 |
if (g & 7) |
| 421 |
return -EINVAL; |
| 422 |
|
| 423 |
*(z++) = (uint8_t) a << 3 | (uint8_t) b >> 2; /* XXXXXYYY */ |
| 424 |
*(z++) = (uint8_t) b << 6 | (uint8_t) c << 1 | (uint8_t) d >> 4; /* YYZZZZZW */ |
| 425 |
*(z++) = (uint8_t) d << 4 | (uint8_t) e >> 1; /* WWWWSSSS */ |
| 426 |
*(z++) = (uint8_t) e << 7 | (uint8_t) f << 2 | (uint8_t) g >> 3; /* SQQQQQVV */ |
| 427 |
|
| 428 |
break; |
| 429 |
case 5: |
| 430 |
a = unbase32hexchar(x[0]); |
| 431 |
if (a < 0) |
| 432 |
return -EINVAL; |
| 433 |
|
| 434 |
b = unbase32hexchar(x[1]); |
| 435 |
if (b < 0) |
| 436 |
return -EINVAL; |
| 437 |
|
| 438 |
c = unbase32hexchar(x[2]); |
| 439 |
if (c < 0) |
| 440 |
return -EINVAL; |
| 441 |
|
| 442 |
d = unbase32hexchar(x[3]); |
| 443 |
if (d < 0) |
| 444 |
return -EINVAL; |
| 445 |
|
| 446 |
e = unbase32hexchar(x[4]); |
| 447 |
if (e < 0) |
| 448 |
return -EINVAL; |
| 449 |
|
| 450 |
/* e == 000SSSS0 */ |
| 451 |
if (e & 1) |
| 452 |
return -EINVAL; |
| 453 |
|
| 454 |
*(z++) = (uint8_t) a << 3 | (uint8_t) b >> 2; /* XXXXXYYY */ |
| 455 |
*(z++) = (uint8_t) b << 6 | (uint8_t) c << 1 | (uint8_t) d >> 4; /* YYZZZZZW */ |
| 456 |
*(z++) = (uint8_t) d << 4 | (uint8_t) e >> 1; /* WWWWSSSS */ |
| 457 |
|
| 458 |
break; |
| 459 |
case 4: |
| 460 |
a = unbase32hexchar(x[0]); |
| 461 |
if (a < 0) |
| 462 |
return -EINVAL; |
| 463 |
|
| 464 |
b = unbase32hexchar(x[1]); |
| 465 |
if (b < 0) |
| 466 |
return -EINVAL; |
| 467 |
|
| 468 |
c = unbase32hexchar(x[2]); |
| 469 |
if (c < 0) |
| 470 |
return -EINVAL; |
| 471 |
|
| 472 |
d = unbase32hexchar(x[3]); |
| 473 |
if (d < 0) |
| 474 |
return -EINVAL; |
| 475 |
|
| 476 |
/* d == 000W0000 */ |
| 477 |
if (d & 15) |
| 478 |
return -EINVAL; |
| 479 |
|
| 480 |
*(z++) = (uint8_t) a << 3 | (uint8_t) b >> 2; /* XXXXXYYY */ |
| 481 |
*(z++) = (uint8_t) b << 6 | (uint8_t) c << 1 | (uint8_t) d >> 4; /* YYZZZZZW */ |
| 482 |
|
| 483 |
break; |
| 484 |
case 2: |
| 485 |
a = unbase32hexchar(x[0]); |
| 486 |
if (a < 0) |
| 487 |
return -EINVAL; |
| 488 |
|
| 489 |
b = unbase32hexchar(x[1]); |
| 490 |
if (b < 0) |
| 491 |
return -EINVAL; |
| 492 |
|
| 493 |
/* b == 000YYY00 */ |
| 494 |
if (b & 3) |
| 495 |
return -EINVAL; |
| 496 |
|
| 497 |
*(z++) = (uint8_t) a << 3 | (uint8_t) b >> 2; /* XXXXXYYY */ |
| 498 |
|
| 499 |
break; |
| 500 |
case 0: |
| 501 |
break; |
| 502 |
default: |
| 503 |
return -EINVAL; |
| 504 |
} |
| 505 |
|
| 506 |
*z = 0; |
| 507 |
|
| 508 |
*mem = TAKE_PTR(r); |
| 509 |
*_len = len; |
| 510 |
|
| 511 |
return 0; |
| 512 |
} |
| 513 |
|
| 514 |
/* https://tools.ietf.org/html/rfc4648#section-4 */ |
| 515 |
char base64char(int x) { |
| 516 |
const char *table = UPPERCASE_LETTERS LOWERCASE_LETTERS DIGITS "+/"; |
| 517 |
return table[x & 63]; |
| 518 |
} |
| 519 |
|
| 520 |
/* This is almost base64char(), but not entirely, as it uses the "url and filename safe" alphabet, |
| 521 |
* since we don't want "/" appear in interface names (since interfaces appear in sysfs as filenames). |
| 522 |
* See section #5 of RFC 4648. */ |
| 523 |
char urlsafe_base64char(int x) { |
| 524 |
const char *table = UPPERCASE_LETTERS LOWERCASE_LETTERS DIGITS "-_"; |
| 525 |
return table[x & 63]; |
| 526 |
} |
| 527 |
|
| 528 |
int unbase64char(char c) { |
| 529 |
unsigned offset; |
| 530 |
|
| 531 |
if (c >= 'A' && c <= 'Z') |
| 532 |
return c - 'A'; |
| 533 |
|
| 534 |
offset = 'Z' - 'A' + 1; |
| 535 |
|
| 536 |
if (c >= 'a' && c <= 'z') |
| 537 |
return c - 'a' + offset; |
| 538 |
|
| 539 |
offset += 'z' - 'a' + 1; |
| 540 |
|
| 541 |
if (c >= '0' && c <= '9') |
| 542 |
return c - '0' + offset; |
| 543 |
|
| 544 |
offset += '9' - '0' + 1; |
| 545 |
|
| 546 |
if (IN_SET(c, '+', '-')) /* Support both the regular and the URL safe character set (see above) */ |
| 547 |
return offset; |
| 548 |
|
| 549 |
offset++; |
| 550 |
|
| 551 |
if (IN_SET(c, '/', '_')) /* ditto */ |
| 552 |
return offset; |
| 553 |
|
| 554 |
return -EINVAL; |
| 555 |
} |
| 556 |
|
| 557 |
static void maybe_line_break(char **x, char *start, size_t line_break) { |
| 558 |
size_t n; |
| 559 |
|
| 560 |
assert(x); |
| 561 |
assert(*x); |
| 562 |
assert(start); |
| 563 |
assert(*x >= start); |
| 564 |
|
| 565 |
if (line_break == SIZE_MAX) |
| 566 |
return; |
| 567 |
|
| 568 |
n = *x - start; |
| 569 |
|
| 570 |
if (n % (line_break + 1) == line_break) |
| 571 |
*((*x)++) = '\n'; |
| 572 |
} |
| 573 |
|
| 574 |
ssize_t base64mem_full( |
| 575 |
const void *p, |
| 576 |
size_t l, |
| 577 |
size_t line_break, |
| 578 |
char **ret) { |
| 579 |
|
| 580 |
const uint8_t *x; |
| 581 |
char *b, *z; |
| 582 |
size_t m; |
| 583 |
|
| 584 |
assert(p || l == 0); |
| 585 |
assert(line_break > 0); |
| 586 |
assert(ret); |
| 587 |
|
| 588 |
/* three input bytes makes four output bytes, padding is added so we must round up */ |
| 589 |
m = 4 * (l + 2) / 3 + 1; |
| 590 |
if (line_break != SIZE_MAX) |
| 591 |
m += m / line_break; |
| 592 |
|
| 593 |
z = b = malloc(m); |
| 594 |
if (!b) |
| 595 |
return -ENOMEM; |
| 596 |
|
| 597 |
for (x = p; x && x < (const uint8_t*) p + (l / 3) * 3; x += 3) { |
| 598 |
/* x[0] == XXXXXXXX; x[1] == YYYYYYYY; x[2] == ZZZZZZZZ */ |
| 599 |
maybe_line_break(&z, b, line_break); |
| 600 |
*(z++) = base64char(x[0] >> 2); /* 00XXXXXX */ |
| 601 |
maybe_line_break(&z, b, line_break); |
| 602 |
*(z++) = base64char((x[0] & 3) << 4 | x[1] >> 4); /* 00XXYYYY */ |
| 603 |
maybe_line_break(&z, b, line_break); |
| 604 |
*(z++) = base64char((x[1] & 15) << 2 | x[2] >> 6); /* 00YYYYZZ */ |
| 605 |
maybe_line_break(&z, b, line_break); |
| 606 |
*(z++) = base64char(x[2] & 63); /* 00ZZZZZZ */ |
| 607 |
} |
| 608 |
|
| 609 |
switch (l % 3) { |
| 610 |
case 2: |
| 611 |
maybe_line_break(&z, b, line_break); |
| 612 |
*(z++) = base64char(x[0] >> 2); /* 00XXXXXX */ |
| 613 |
maybe_line_break(&z, b, line_break); |
| 614 |
*(z++) = base64char((x[0] & 3) << 4 | x[1] >> 4); /* 00XXYYYY */ |
| 615 |
maybe_line_break(&z, b, line_break); |
| 616 |
*(z++) = base64char((x[1] & 15) << 2); /* 00YYYY00 */ |
| 617 |
maybe_line_break(&z, b, line_break); |
| 618 |
*(z++) = '='; |
| 619 |
break; |
| 620 |
|
| 621 |
case 1: |
| 622 |
maybe_line_break(&z, b, line_break); |
| 623 |
*(z++) = base64char(x[0] >> 2); /* 00XXXXXX */ |
| 624 |
maybe_line_break(&z, b, line_break); |
| 625 |
*(z++) = base64char((x[0] & 3) << 4); /* 00XX0000 */ |
| 626 |
maybe_line_break(&z, b, line_break); |
| 627 |
*(z++) = '='; |
| 628 |
maybe_line_break(&z, b, line_break); |
| 629 |
*(z++) = '='; |
| 630 |
break; |
| 631 |
} |
| 632 |
|
| 633 |
*z = 0; |
| 634 |
*ret = b; |
| 635 |
|
| 636 |
assert(z >= b); /* Let static analyzers know that the answer is non-negative. */ |
| 637 |
return z - b; |
| 638 |
} |
| 639 |
|
| 640 |
static ssize_t base64_append_width( |
| 641 |
char **prefix, |
| 642 |
size_t plen, |
| 643 |
char sep, |
| 644 |
size_t indent, |
| 645 |
const void *p, |
| 646 |
size_t l, |
| 647 |
size_t width) { |
| 648 |
|
| 649 |
_cleanup_free_ char *x = NULL; |
| 650 |
char *t, *s; |
| 651 |
size_t lines; |
| 652 |
ssize_t len; |
| 653 |
|
| 654 |
assert(prefix); |
| 655 |
assert(*prefix || plen == 0); |
| 656 |
assert(p || l == 0); |
| 657 |
|
| 658 |
len = base64mem(p, l, &x); |
| 659 |
if (len < 0) |
| 660 |
return len; |
| 661 |
if (len == 0) |
| 662 |
return plen; |
| 663 |
|
| 664 |
lines = DIV_ROUND_UP(len, width); |
| 665 |
|
| 666 |
if (plen >= SSIZE_MAX - 1 - 1 || |
| 667 |
lines > (SSIZE_MAX - plen - 1 - 1) / (indent + width + 1)) |
| 668 |
return -ENOMEM; |
| 669 |
|
| 670 |
t = realloc(*prefix, plen + 1 + 1 + (indent + width + 1) * lines); |
| 671 |
if (!t) |
| 672 |
return -ENOMEM; |
| 673 |
|
| 674 |
s = t + plen; |
| 675 |
for (size_t line = 0; line < lines; line++) { |
| 676 |
size_t act = MIN(width, (size_t) len); |
| 677 |
|
| 678 |
if (line > 0) |
| 679 |
sep = '\n'; |
| 680 |
|
| 681 |
if (s > t) { |
| 682 |
*s++ = sep; |
| 683 |
if (sep == '\n') |
| 684 |
s = mempset(s, ' ', indent); |
| 685 |
} |
| 686 |
|
| 687 |
s = mempcpy(s, x + width * line, act); |
| 688 |
len -= act; |
| 689 |
} |
| 690 |
assert(len == 0); |
| 691 |
|
| 692 |
*s = '\0'; |
| 693 |
*prefix = t; |
| 694 |
return s - t; |
| 695 |
} |
| 696 |
|
| 697 |
ssize_t base64_append( |
| 698 |
char **prefix, |
| 699 |
size_t plen, |
| 700 |
const void *p, |
| 701 |
size_t l, |
| 702 |
size_t indent, |
| 703 |
size_t width) { |
| 704 |
|
| 705 |
if (plen > width / 2 || plen + indent > width) |
| 706 |
/* leave indent on the left, keep last column free */ |
| 707 |
return base64_append_width(prefix, plen, '\n', indent, p, l, width - indent); |
| 708 |
else |
| 709 |
/* leave plen on the left, keep last column free */ |
| 710 |
return base64_append_width(prefix, plen, ' ', plen + 1, p, l, width - plen - 1); |
| 711 |
} |
| 712 |
|
| 713 |
static int unbase64_next(const char **p, size_t *l) { |
| 714 |
int ret; |
| 715 |
|
| 716 |
assert(p); |
| 717 |
assert(l); |
| 718 |
|
| 719 |
/* Find the next non-whitespace character, and decode it. If we find padding, we return it as INT_MAX. We |
| 720 |
* greedily skip all preceding and all following whitespace. */ |
| 721 |
|
| 722 |
for (;;) { |
| 723 |
if (*l == 0) |
| 724 |
return -EPIPE; |
| 725 |
|
| 726 |
if (!strchr(WHITESPACE, **p)) |
| 727 |
break; |
| 728 |
|
| 729 |
/* Skip leading whitespace */ |
| 730 |
(*p)++, (*l)--; |
| 731 |
} |
| 732 |
|
| 733 |
if (**p == '=') |
| 734 |
ret = INT_MAX; /* return padding as INT_MAX */ |
| 735 |
else { |
| 736 |
ret = unbase64char(**p); |
| 737 |
if (ret < 0) |
| 738 |
return ret; |
| 739 |
} |
| 740 |
|
| 741 |
for (;;) { |
| 742 |
(*p)++, (*l)--; |
| 743 |
|
| 744 |
if (*l == 0) |
| 745 |
break; |
| 746 |
if (!strchr(WHITESPACE, **p)) |
| 747 |
break; |
| 748 |
|
| 749 |
/* Skip following whitespace */ |
| 750 |
} |
| 751 |
|
| 752 |
return ret; |
| 753 |
} |
| 754 |
|
| 755 |
int unbase64mem_full( |
| 756 |
const char *p, |
| 757 |
size_t l, |
| 758 |
bool secure, |
| 759 |
void **ret_data, |
| 760 |
size_t *ret_size) { |
| 761 |
|
| 762 |
_cleanup_free_ uint8_t *buf = NULL; |
| 763 |
const char *x; |
| 764 |
uint8_t *z; |
| 765 |
size_t len; |
| 766 |
|
| 767 |
assert(p || l == 0); |
| 768 |
|
| 769 |
if (l == SIZE_MAX) |
| 770 |
l = strlen(p); |
| 771 |
|
| 772 |
/* A group of four input bytes needs three output bytes, in case of padding we need to add two or three extra |
| 773 |
* bytes. Note that this calculation is an upper boundary, as we ignore whitespace while decoding */ |
| 774 |
len = (l / 4) * 3 + (l % 4 != 0 ? (l % 4) - 1 : 0); |
| 775 |
|
| 776 |
buf = malloc(len + 1); |
| 777 |
if (!buf) |
| 778 |
return -ENOMEM; |
| 779 |
|
| 780 |
CLEANUP_ERASE_PTR(secure ? &buf : NULL, len); |
| 781 |
|
| 782 |
for (x = p, z = buf;;) { |
| 783 |
int a, b, c, d; /* a == 00XXXXXX; b == 00YYYYYY; c == 00ZZZZZZ; d == 00WWWWWW */ |
| 784 |
|
| 785 |
a = unbase64_next(&x, &l); |
| 786 |
if (a == -EPIPE) /* End of string */ |
| 787 |
break; |
| 788 |
if (a < 0) |
| 789 |
return a; |
| 790 |
if (a == INT_MAX) /* Padding is not allowed at the beginning of a 4ch block */ |
| 791 |
return -EINVAL; |
| 792 |
|
| 793 |
b = unbase64_next(&x, &l); |
| 794 |
if (b < 0) |
| 795 |
return b; |
| 796 |
if (b == INT_MAX) /* Padding is not allowed at the second character of a 4ch block either */ |
| 797 |
return -EINVAL; |
| 798 |
|
| 799 |
c = unbase64_next(&x, &l); |
| 800 |
if (c < 0) |
| 801 |
return c; |
| 802 |
|
| 803 |
d = unbase64_next(&x, &l); |
| 804 |
if (d < 0) |
| 805 |
return d; |
| 806 |
|
| 807 |
if (c == INT_MAX) { /* Padding at the third character */ |
| 808 |
|
| 809 |
if (d != INT_MAX) /* If the third character is padding, the fourth must be too */ |
| 810 |
return -EINVAL; |
| 811 |
|
| 812 |
/* b == 00YY0000 */ |
| 813 |
if (b & 15) |
| 814 |
return -EINVAL; |
| 815 |
|
| 816 |
if (l > 0) /* Trailing rubbish? */ |
| 817 |
return -ENAMETOOLONG; |
| 818 |
|
| 819 |
*(z++) = (uint8_t) a << 2 | (uint8_t) (b >> 4); /* XXXXXXYY */ |
| 820 |
break; |
| 821 |
} |
| 822 |
|
| 823 |
if (d == INT_MAX) { |
| 824 |
/* c == 00ZZZZ00 */ |
| 825 |
if (c & 3) |
| 826 |
return -EINVAL; |
| 827 |
|
| 828 |
if (l > 0) /* Trailing rubbish? */ |
| 829 |
return -ENAMETOOLONG; |
| 830 |
|
| 831 |
*(z++) = (uint8_t) a << 2 | (uint8_t) b >> 4; /* XXXXXXYY */ |
| 832 |
*(z++) = (uint8_t) b << 4 | (uint8_t) c >> 2; /* YYYYZZZZ */ |
| 833 |
break; |
| 834 |
} |
| 835 |
|
| 836 |
*(z++) = (uint8_t) a << 2 | (uint8_t) b >> 4; /* XXXXXXYY */ |
| 837 |
*(z++) = (uint8_t) b << 4 | (uint8_t) c >> 2; /* YYYYZZZZ */ |
| 838 |
*(z++) = (uint8_t) c << 6 | (uint8_t) d; /* ZZWWWWWW */ |
| 839 |
} |
| 840 |
|
| 841 |
*z = 0; |
| 842 |
|
| 843 |
assert((size_t) (z - buf) <= len); |
| 844 |
|
| 845 |
if (ret_size) |
| 846 |
*ret_size = (size_t) (z - buf); |
| 847 |
if (ret_data) |
| 848 |
*ret_data = TAKE_PTR(buf); |
| 849 |
|
| 850 |
return 0; |
| 851 |
} |
| 852 |
|
| 853 |
void hexdump(FILE *f, const void *p, size_t s) { |
| 854 |
const uint8_t *b = p; |
| 855 |
unsigned n = 0; |
| 856 |
|
| 857 |
assert(b || s == 0); |
| 858 |
|
| 859 |
if (s == SIZE_MAX) |
| 860 |
s = strlen(p); |
| 861 |
|
| 862 |
if (!f) |
| 863 |
f = stdout; |
| 864 |
|
| 865 |
while (s > 0) { |
| 866 |
size_t i; |
| 867 |
|
| 868 |
fprintf(f, "%04x ", n); |
| 869 |
|
| 870 |
for (i = 0; i < 16; i++) { |
| 871 |
|
| 872 |
if (i >= s) |
| 873 |
fputs(" ", f); |
| 874 |
else |
| 875 |
fprintf(f, "%02x ", b[i]); |
| 876 |
|
| 877 |
if (i == 7) |
| 878 |
fputc(' ', f); |
| 879 |
} |
| 880 |
|
| 881 |
fputc(' ', f); |
| 882 |
|
| 883 |
for (i = 0; i < 16; i++) { |
| 884 |
|
| 885 |
if (i >= s) |
| 886 |
fputc(' ', f); |
| 887 |
else |
| 888 |
fputc(isprint(b[i]) ? (char) b[i] : '.', f); |
| 889 |
} |
| 890 |
|
| 891 |
fputc('\n', f); |
| 892 |
|
| 893 |
if (s < 16) |
| 894 |
break; |
| 895 |
|
| 896 |
n += 16; |
| 897 |
b += 16; |
| 898 |
s -= 16; |
| 899 |
} |
| 900 |
} |