2 This file is part of systemd.
4 Copyright 2010 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
25 #include "alloc-util.h"
26 #include "hexdecoct.h"
28 #include "string-util.h"
35 int unoctchar(char c) {
37 if (c >= '0' && c <= '7')
44 return '0' + (x % 10);
47 int undecchar(char c) {
49 if (c >= '0' && c <= '9')
56 static const char table[16] = "0123456789abcdef";
61 int unhexchar(char c) {
63 if (c >= '0' && c <= '9')
66 if (c >= 'a' && c <= 'f')
69 if (c >= 'A' && c <= 'F')
75 char *hexmem(const void *p, size_t l) {
79 z = r = new(char, l * 2 + 1);
83 for (x = p; x < (const uint8_t*) p + l; x++) {
84 *(z++) = hexchar(*x >> 4);
85 *(z++) = hexchar(*x & 15);
92 int unhexmem(const char *p, size_t l, void **mem, size_t *len) {
93 _cleanup_free_ uint8_t *r = NULL;
101 if (l == (size_t) -1)
107 z = r = malloc((l + 1) / 2 + 1);
111 for (x = p; x < p + l; x += 2) {
122 *(z++) = (uint8_t) a << 4 | (uint8_t) b;
134 /* https://tools.ietf.org/html/rfc4648#section-6
135 * Notice that base32hex differs from base32 in the alphabet it uses.
136 * The distinction is that the base32hex representation preserves the
137 * order of the underlying data when compared as bytestrings, this is
138 * useful when representing NSEC3 hashes, as one can then verify the
139 * order of hashes directly from their representation. */
140 char base32hexchar(int x) {
141 static const char table[32] = "0123456789"
142 "ABCDEFGHIJKLMNOPQRSTUV";
144 return table[x & 31];
147 int unbase32hexchar(char c) {
150 if (c >= '0' && c <= '9')
153 offset = '9' - '0' + 1;
155 if (c >= 'A' && c <= 'V')
156 return c - 'A' + offset;
161 char *base32hexmem(const void *p, size_t l, bool padding) {
169 /* five input bytes makes eight output bytes, padding is added so we must round up */
170 len = 8 * (l + 4) / 5;
172 /* same, but round down as there is no padding */
191 z = r = malloc(len + 1);
195 for (x = p; x < (const uint8_t*) p + (l / 5) * 5; x += 5) {
196 /* x[0] == XXXXXXXX; x[1] == YYYYYYYY; x[2] == ZZZZZZZZ
197 x[3] == QQQQQQQQ; x[4] == WWWWWWWW */
198 *(z++) = base32hexchar(x[0] >> 3); /* 000XXXXX */
199 *(z++) = base32hexchar((x[0] & 7) << 2 | x[1] >> 6); /* 000XXXYY */
200 *(z++) = base32hexchar((x[1] & 63) >> 1); /* 000YYYYY */
201 *(z++) = base32hexchar((x[1] & 1) << 4 | x[2] >> 4); /* 000YZZZZ */
202 *(z++) = base32hexchar((x[2] & 15) << 1 | x[3] >> 7); /* 000ZZZZQ */
203 *(z++) = base32hexchar((x[3] & 127) >> 2); /* 000QQQQQ */
204 *(z++) = base32hexchar((x[3] & 3) << 3 | x[4] >> 5); /* 000QQWWW */
205 *(z++) = base32hexchar((x[4] & 31)); /* 000WWWWW */
210 *(z++) = base32hexchar(x[0] >> 3); /* 000XXXXX */
211 *(z++) = base32hexchar((x[0] & 7) << 2 | x[1] >> 6); /* 000XXXYY */
212 *(z++) = base32hexchar((x[1] & 63) >> 1); /* 000YYYYY */
213 *(z++) = base32hexchar((x[1] & 1) << 4 | x[2] >> 4); /* 000YZZZZ */
214 *(z++) = base32hexchar((x[2] & 15) << 1 | x[3] >> 7); /* 000ZZZZQ */
215 *(z++) = base32hexchar((x[3] & 127) >> 2); /* 000QQQQQ */
216 *(z++) = base32hexchar((x[3] & 3) << 3); /* 000QQ000 */
223 *(z++) = base32hexchar(x[0] >> 3); /* 000XXXXX */
224 *(z++) = base32hexchar((x[0] & 7) << 2 | x[1] >> 6); /* 000XXXYY */
225 *(z++) = base32hexchar((x[1] & 63) >> 1); /* 000YYYYY */
226 *(z++) = base32hexchar((x[1] & 1) << 4 | x[2] >> 4); /* 000YZZZZ */
227 *(z++) = base32hexchar((x[2] & 15) << 1); /* 000ZZZZ0 */
237 *(z++) = base32hexchar(x[0] >> 3); /* 000XXXXX */
238 *(z++) = base32hexchar((x[0] & 7) << 2 | x[1] >> 6); /* 000XXXYY */
239 *(z++) = base32hexchar((x[1] & 63) >> 1); /* 000YYYYY */
240 *(z++) = base32hexchar((x[1] & 1) << 4); /* 000Y0000 */
251 *(z++) = base32hexchar(x[0] >> 3); /* 000XXXXX */
252 *(z++) = base32hexchar((x[0] & 7) << 2); /* 000XXX00 */
269 int unbase32hexmem(const char *p, size_t l, bool padding, void **mem, size_t *_len) {
270 _cleanup_free_ uint8_t *r = NULL;
271 int a, b, c, d, e, f, g, h;
281 if (l == (size_t) -1)
284 /* padding ensures any base32hex input has input divisible by 8 */
285 if (padding && l % 8 != 0)
289 /* strip the padding */
290 while (l > 0 && p[l - 1] == '=' && pad < 7) {
296 /* a group of eight input bytes needs five output bytes, in case of
297 padding we need to add some extra bytes */
319 z = r = malloc(len + 1);
323 for (x = p; x < p + (l / 8) * 8; x += 8) {
324 /* a == 000XXXXX; b == 000YYYYY; c == 000ZZZZZ; d == 000WWWWW
325 e == 000SSSSS; f == 000QQQQQ; g == 000VVVVV; h == 000RRRRR */
326 a = unbase32hexchar(x[0]);
330 b = unbase32hexchar(x[1]);
334 c = unbase32hexchar(x[2]);
338 d = unbase32hexchar(x[3]);
342 e = unbase32hexchar(x[4]);
346 f = unbase32hexchar(x[5]);
350 g = unbase32hexchar(x[6]);
354 h = unbase32hexchar(x[7]);
358 *(z++) = (uint8_t) a << 3 | (uint8_t) b >> 2; /* XXXXXYYY */
359 *(z++) = (uint8_t) b << 6 | (uint8_t) c << 1 | (uint8_t) d >> 4; /* YYZZZZZW */
360 *(z++) = (uint8_t) d << 4 | (uint8_t) e >> 1; /* WWWWSSSS */
361 *(z++) = (uint8_t) e << 7 | (uint8_t) f << 2 | (uint8_t) g >> 3; /* SQQQQQVV */
362 *(z++) = (uint8_t) g << 5 | (uint8_t) h; /* VVVRRRRR */
367 a = unbase32hexchar(x[0]);
371 b = unbase32hexchar(x[1]);
375 c = unbase32hexchar(x[2]);
379 d = unbase32hexchar(x[3]);
383 e = unbase32hexchar(x[4]);
387 f = unbase32hexchar(x[5]);
391 g = unbase32hexchar(x[6]);
399 *(z++) = (uint8_t) a << 3 | (uint8_t) b >> 2; /* XXXXXYYY */
400 *(z++) = (uint8_t) b << 6 | (uint8_t) c << 1 | (uint8_t) d >> 4; /* YYZZZZZW */
401 *(z++) = (uint8_t) d << 4 | (uint8_t) e >> 1; /* WWWWSSSS */
402 *(z++) = (uint8_t) e << 7 | (uint8_t) f << 2 | (uint8_t) g >> 3; /* SQQQQQVV */
406 a = unbase32hexchar(x[0]);
410 b = unbase32hexchar(x[1]);
414 c = unbase32hexchar(x[2]);
418 d = unbase32hexchar(x[3]);
422 e = unbase32hexchar(x[4]);
430 *(z++) = (uint8_t) a << 3 | (uint8_t) b >> 2; /* XXXXXYYY */
431 *(z++) = (uint8_t) b << 6 | (uint8_t) c << 1 | (uint8_t) d >> 4; /* YYZZZZZW */
432 *(z++) = (uint8_t) d << 4 | (uint8_t) e >> 1; /* WWWWSSSS */
436 a = unbase32hexchar(x[0]);
440 b = unbase32hexchar(x[1]);
444 c = unbase32hexchar(x[2]);
448 d = unbase32hexchar(x[3]);
456 *(z++) = (uint8_t) a << 3 | (uint8_t) b >> 2; /* XXXXXYYY */
457 *(z++) = (uint8_t) b << 6 | (uint8_t) c << 1 | (uint8_t) d >> 4; /* YYZZZZZW */
461 a = unbase32hexchar(x[0]);
465 b = unbase32hexchar(x[1]);
473 *(z++) = (uint8_t) a << 3 | (uint8_t) b >> 2; /* XXXXXYYY */
491 /* https://tools.ietf.org/html/rfc4648#section-4 */
492 char base64char(int x) {
493 static const char table[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
494 "abcdefghijklmnopqrstuvwxyz"
496 return table[x & 63];
499 int unbase64char(char c) {
502 if (c >= 'A' && c <= 'Z')
505 offset = 'Z' - 'A' + 1;
507 if (c >= 'a' && c <= 'z')
508 return c - 'a' + offset;
510 offset += 'z' - 'a' + 1;
512 if (c >= '0' && c <= '9')
513 return c - '0' + offset;
515 offset += '9' - '0' + 1;
528 ssize_t base64mem(const void *p, size_t l, char **out) {
535 /* three input bytes makes four output bytes, padding is added so we must round up */
536 z = r = malloc(4 * (l + 2) / 3 + 1);
540 for (x = p; x < (const uint8_t*) p + (l / 3) * 3; x += 3) {
541 /* x[0] == XXXXXXXX; x[1] == YYYYYYYY; x[2] == ZZZZZZZZ */
542 *(z++) = base64char(x[0] >> 2); /* 00XXXXXX */
543 *(z++) = base64char((x[0] & 3) << 4 | x[1] >> 4); /* 00XXYYYY */
544 *(z++) = base64char((x[1] & 15) << 2 | x[2] >> 6); /* 00YYYYZZ */
545 *(z++) = base64char(x[2] & 63); /* 00ZZZZZZ */
550 *(z++) = base64char(x[0] >> 2); /* 00XXXXXX */
551 *(z++) = base64char((x[0] & 3) << 4 | x[1] >> 4); /* 00XXYYYY */
552 *(z++) = base64char((x[1] & 15) << 2); /* 00YYYY00 */
557 *(z++) = base64char(x[0] >> 2); /* 00XXXXXX */
558 *(z++) = base64char((x[0] & 3) << 4); /* 00XX0000 */
570 static int base64_append_width(
571 char **prefix, int plen,
572 const char *sep, int indent,
573 const void *p, size_t l,
576 _cleanup_free_ char *x = NULL;
578 ssize_t slen, len, avail;
581 len = base64mem(p, l, &x);
585 lines = (len + width - 1) / width;
587 slen = strlen_ptr(sep);
588 t = realloc(*prefix, plen + 1 + slen + (indent + width + 1) * lines);
592 memcpy_safe(t + plen, sep, slen);
594 for (line = 0, s = t + plen + slen, avail = len; line < lines; line++) {
595 int act = MIN(width, avail);
597 if (line > 0 || sep) {
598 memset(s, ' ', indent);
602 memcpy(s, x + width * line, act);
604 *(s++) = line < lines - 1 ? '\n' : '\0';
614 char **prefix, int plen,
615 const void *p, size_t l,
616 int indent, int width) {
618 if (plen > width / 2 || plen + indent > width)
619 /* leave indent on the left, keep last column free */
620 return base64_append_width(prefix, plen, "\n", indent, p, l, width - indent - 1);
622 /* leave plen on the left, keep last column free */
623 return base64_append_width(prefix, plen, NULL, plen, p, l, width - plen - 1);
626 int unbase64mem(const char *p, size_t l, void **mem, size_t *_len) {
627 _cleanup_free_ uint8_t *r = NULL;
637 if (l == (size_t) -1)
640 /* padding ensures any base63 input has input divisible by 4 */
644 /* strip the padding */
645 if (l > 0 && p[l - 1] == '=')
647 if (l > 0 && p[l - 1] == '=')
650 /* a group of four input bytes needs three output bytes, in case of
651 padding we need to add two or three extra bytes */
652 len = (l / 4) * 3 + (l % 4 ? (l % 4) - 1 : 0);
654 z = r = malloc(len + 1);
658 for (x = p; x < p + (l / 4) * 4; x += 4) {
659 /* a == 00XXXXXX; b == 00YYYYYY; c == 00ZZZZZZ; d == 00WWWWWW */
660 a = unbase64char(x[0]);
664 b = unbase64char(x[1]);
668 c = unbase64char(x[2]);
672 d = unbase64char(x[3]);
676 *(z++) = (uint8_t) a << 2 | (uint8_t) b >> 4; /* XXXXXXYY */
677 *(z++) = (uint8_t) b << 4 | (uint8_t) c >> 2; /* YYYYZZZZ */
678 *(z++) = (uint8_t) c << 6 | (uint8_t) d; /* ZZWWWWWW */
684 a = unbase64char(x[0]);
688 b = unbase64char(x[1]);
692 c = unbase64char(x[2]);
700 *(z++) = (uint8_t) a << 2 | (uint8_t) b >> 4; /* XXXXXXYY */
701 *(z++) = (uint8_t) b << 4 | (uint8_t) c >> 2; /* YYYYZZZZ */
705 a = unbase64char(x[0]);
709 b = unbase64char(x[1]);
717 *(z++) = (uint8_t) a << 2 | (uint8_t) (b >> 4); /* XXXXXXYY */
736 void hexdump(FILE *f, const void *p, size_t s) {
737 const uint8_t *b = p;
745 fprintf(f, "%04x ", n);
747 for (i = 0; i < 16; i++) {
752 fprintf(f, "%02x ", b[i]);
760 for (i = 0; i < 16; i++) {
765 fputc(isprint(b[i]) ? (char) b[i] : '.', f);