From b6b5becf359370c7f32d8419bcf8f50a47fbe933 Mon Sep 17 00:00:00 2001 From: Moritz Röhrich Date: Thu, 7 Jan 2021 17:40:20 +0100 Subject: new toy: base32 Add new toy `base32`. Add tests for `base32`. base32 is added by adapting the base64 encode/decode function to also do base32 encoding/decoding. Then their respective main functions set up the global parameter `n` to be the number of bits used in the encoding (5 for base32 and 6 for base64) and `align` to align the result to a certain length via padding. These are deliberately kept as parameters to enable future expansion for other bases easily. --- lib/lib.c | 12 ++++++++++++ lib/lib.h | 1 + 2 files changed, 13 insertions(+) (limited to 'lib') diff --git a/lib/lib.c b/lib/lib.c index dfa4499f..1aa9b80d 100644 --- a/lib/lib.c +++ b/lib/lib.c @@ -870,6 +870,18 @@ void base64_init(char *p) *(p++) = '/'; } +// Init base32 table + +void base32_init(char *p) +{ + int i; + + for (i = 'A'; i != '8'; i++) { + if (i == 'Z'+1) i = '2'; + *(p++) = i; + } +} + int yesno(int def) { return fyesno(stdin, def); diff --git a/lib/lib.h b/lib/lib.h index 431ce5ad..1ba59ee3 100644 --- a/lib/lib.h +++ b/lib/lib.h @@ -255,6 +255,7 @@ void delete_tempfile(int fdin, int fdout, char **tempname); void replace_tempfile(int fdin, int fdout, char **tempname); void crc_init(unsigned int *crc_table, int little_endian); void base64_init(char *p); +void base32_init(char *p); int yesno(int def); int fyesno(FILE *fp, int def); int qstrcmp(const void *a, const void *b); -- cgit v1.2.3