aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNRK <nrk@disroot.org>2022-03-18 17:03:34 +0600
committerHiltjo Posthuma <hiltjo@codemadness.org>2022-03-18 12:20:27 +0100
commitef0551932fb162f907b40185d2f48c3b497708ee (patch)
tree7051a47cfe413c345ca707d3550be21e00081556
parentaf3bb68add1c40d19d0dee382009e21b0870a38f (diff)
downloadst-patched-ef0551932fb162f907b40185d2f48c3b497708ee.tar.bz2
st-patched-ef0551932fb162f907b40185d2f48c3b497708ee.tar.xz
st-patched-ef0551932fb162f907b40185d2f48c3b497708ee.zip
base64_digits: reduce scope, implicit zero, +1 size
the array is not accessed outside of base64dec() so it makes sense to limit it's scope to the related function. the static-storage duration of the array is kept intact. this also removes unnecessary explicit zeroing from the start and end of the array. anything that wasn't explicitly zero-ed will now be implicitly zero-ed instead. the validity of the new array can be easily confirmed via running this trivial loop: for (int i = 0; i < 255; ++i) assert(base64_digits[i] == base64_digits_old[i]); lastly, as pointed out by Roberto, the array needs to have 256 elements in order to able access it as any unsigned char as an index; the previous array had 255. however, this array will only be accessed at indexes which are isprint() || '=' (see `base64dec_getc()`), so reducing the size of the array to the highest printable ascii char (127 AFAIK) + 1 might also be a valid strategy.
-rw-r--r--st.c22
1 files changed, 7 insertions, 15 deletions
diff --git a/st.c b/st.c
index 1307fdf..f43cfd3 100644
--- a/st.c
+++ b/st.c
@@ -349,21 +349,6 @@ utf8validate(Rune *u, size_t i)
349 return i; 349 return i;
350} 350}
351 351
352static const char base64_digits[] = {
353 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
354 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 62, 0, 0, 0,
355 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 0, 0, 0, -1, 0, 0, 0, 0, 1,
356 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
357 22, 23, 24, 25, 0, 0, 0, 0, 0, 0, 26, 27, 28, 29, 30, 31, 32, 33, 34,
358 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 0,
359 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
360 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
361 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
362 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
363 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
364 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
365};
366
367char 352char
368base64dec_getc(const char **src) 353base64dec_getc(const char **src)
369{ 354{
@@ -377,6 +362,13 @@ base64dec(const char *src)
377{ 362{
378 size_t in_len = strlen(src); 363 size_t in_len = strlen(src);
379 char *result, *dst; 364 char *result, *dst;
365 static const char base64_digits[256] = {
366 [43] = 62, 0, 0, 0, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,
367 0, 0, 0, -1, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
368 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 0, 0, 0, 0,
369 0, 0, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
370 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51
371 };
380 372
381 if (in_len % 4) 373 if (in_len % 4)
382 in_len += 4 - (in_len % 4); 374 in_len += 4 - (in_len % 4);