From 43231ae554d0dacbcc32e2d0fe574c9d0e183258 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Tue, 13 Aug 2024 08:35:24 -0700 Subject: [PATCH] Avoid need for base64_init and extra table MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Simplify the code by assuming C99 initializers. * src/list.c (base_64_digits): Remove. (base64_map): Now a constant. Now has its (old value + 1) % 65, as that’s the only easy portable way to do it with a static initializer (even on platforms where CHAR_BIT != 8); all uses changed. (base64_init): Remove; only use removed. (from_header): Adjust to new values in base64_map. * src/list.c (base_64_digits): Remove; no longer needed. (base64_map): Now const, initialized statically, and with invalid entries being 0 not 64, and with valid entries being 1 greater than before. --- src/list.c | 51 +++++++++++++++++++++++++-------------------------- 1 file changed, 25 insertions(+), 26 deletions(-) diff --git a/src/list.c b/src/list.c index 04005092..0026378f 100644 --- a/src/list.c +++ b/src/list.c @@ -51,29 +51,30 @@ static uid_t uid_from_header (const char *buf, size_t size); static intmax_t from_header (const char *, size_t, const char *, intmax_t, uintmax_t, bool, bool); -/* Base 64 digits; see Internet RFC 2045 Table 1. */ -static char const base_64_digits[64] = -{ - 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', - 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', - 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', - 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', - '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' +/* Table of base-64 digit values + 1, indexed by unsigned chars. + See Internet RFC 2045 Table 1. + Zero entries are for unsigned chars that are not base-64 digits. */ +static char const base64_map[UCHAR_MAX + 1] = { + ['A'] = 0 + 1, ['B'] = 1 + 1, ['C'] = 2 + 1, ['D'] = 3 + 1, + ['E'] = 4 + 1, ['F'] = 5 + 1, ['G'] = 6 + 1, ['H'] = 7 + 1, + ['I'] = 8 + 1, ['J'] = 9 + 1, ['K'] = 10 + 1, ['L'] = 11 + 1, + ['M'] = 12 + 1, ['N'] = 13 + 1, ['O'] = 14 + 1, ['P'] = 15 + 1, + ['Q'] = 16 + 1, ['R'] = 17 + 1, ['S'] = 18 + 1, ['T'] = 19 + 1, + ['U'] = 20 + 1, ['V'] = 21 + 1, ['W'] = 22 + 1, ['X'] = 23 + 1, + ['Y'] = 24 + 1, ['Z'] = 25 + 1, + ['a'] = 26 + 1, ['b'] = 27 + 1, ['c'] = 28 + 1, ['d'] = 29 + 1, + ['e'] = 30 + 1, ['f'] = 31 + 1, ['g'] = 32 + 1, ['h'] = 33 + 1, + ['i'] = 34 + 1, ['j'] = 35 + 1, ['k'] = 36 + 1, ['l'] = 37 + 1, + ['m'] = 38 + 1, ['n'] = 39 + 1, ['o'] = 40 + 1, ['p'] = 41 + 1, + ['q'] = 42 + 1, ['r'] = 43 + 1, ['s'] = 44 + 1, ['t'] = 45 + 1, + ['u'] = 46 + 1, ['v'] = 47 + 1, ['w'] = 48 + 1, ['x'] = 49 + 1, + ['y'] = 50 + 1, ['z'] = 51 + 1, + ['0'] = 52 + 1, ['1'] = 53 + 1, ['2'] = 54 + 1, ['3'] = 55 + 1, + ['4'] = 56 + 1, ['5'] = 57 + 1, ['6'] = 58 + 1, ['7'] = 59 + 1, + ['8'] = 60 + 1, ['9'] = 61 + 1, + ['+'] = 62 + 1, ['/'] = 63 + 1, }; -/* Table of base-64 digit values indexed by unsigned chars. - The value is 64 for unsigned chars that are not base-64 digits. */ -static char base64_map[UCHAR_MAX + 1]; - -static void -base64_init (void) -{ - int i; - memset (base64_map, 64, sizeof base64_map); - for (i = 0; i < 64; i++) - base64_map[(int) base_64_digits[i]] = i; -} - static char * decode_xform (char *file_name, void *data) { @@ -171,7 +172,6 @@ read_and (void (*do_something) (void)) enum read_header prev_status; struct timespec mtime; - base64_init (); name_gather (); open_archive (ACCESS_READ); @@ -843,8 +843,8 @@ from_header (char const *where0, size_t digs, char const *type, while (where != lim) { unsigned char uc = *where; - int dig = base64_map[uc]; - if (64 <= dig) + char dig = base64_map[uc]; + if (dig <= 0) break; if (ckd_mul (&value, value, 64)) { @@ -854,7 +854,7 @@ from_header (char const *where0, size_t digs, char const *type, quote_mem (where0, digs), type)); return -1; } - value |= dig; + value |= dig - 1; where++; } } @@ -1427,7 +1427,6 @@ skim_member (bool must_copy) void test_archive_label (void) { - base64_init (); name_gather (); open_archive (ACCESS_READ); -- 2.47.2