aboutsummaryrefslogtreecommitdiff
path: root/toys
diff options
context:
space:
mode:
authorMoritz Röhrich <moritz@ildefons.de>2021-01-07 17:40:20 +0100
committerRob Landley <rob@landley.net>2021-01-07 16:10:47 -0600
commitb6b5becf359370c7f32d8419bcf8f50a47fbe933 (patch)
tree4926ca2fe9180730dd9adfcb464d16c8d4ba2ffc /toys
parent824de078e4bdc752da9559cc64df627028310cc3 (diff)
downloadtoybox-b6b5becf359370c7f32d8419bcf8f50a47fbe933.tar.gz
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.
Diffstat (limited to 'toys')
-rw-r--r--toys/other/base64.c46
1 files changed, 37 insertions, 9 deletions
diff --git a/toys/other/base64.c b/toys/other/base64.c
index ef7854a1..25e37d41 100644
--- a/toys/other/base64.c
+++ b/toys/other/base64.c
@@ -5,6 +5,7 @@
* No standard
USE_BASE64(NEWTOY(base64, "diw#<0=76[!dw]", TOYFLAG_USR|TOYFLAG_BIN))
+USE_BASE32(NEWTOY(base32, "diw#<0=76[!dw]", TOYFLAG_USR|TOYFLAG_BIN))
config BASE64
bool "base64"
@@ -17,6 +18,18 @@ config BASE64
-d Decode
-i Ignore non-alphabetic characters
-w Wrap output at COLUMNS (default 76 or 0 for no wrap)
+
+config BASE32
+ bool "base32"
+ default y
+ help
+ usage: base32 [-di] [-w COLUMNS] [FILE...]
+
+ Encode or decode in base32.
+
+ -d Decode
+ -i Ignore non-alphabetic characters
+ -w Wrap output at COLUMNS (default 76 or 0 for no wrap)
*/
#define FOR_base64
@@ -24,8 +37,9 @@ config BASE64
GLOBALS(
long w;
-
unsigned total;
+ unsigned n; // number of bits used in encoding. 5 for base32, 6 for base64
+ unsigned align; // number of bits to align to
)
static void wraputchar(int c, int *x)
@@ -38,7 +52,7 @@ static void wraputchar(int c, int *x)
};
}
-static void do_base64(int fd, char *name)
+static void do_base_n(int fd, char *name)
{
int out = 0, bits = 0, x = 0, i, len;
char *buf = toybuf+128;
@@ -49,8 +63,8 @@ static void do_base64(int fd, char *name)
// If no more data, flush buffer
if (!(len = xread(fd, buf, sizeof(toybuf)-128))) {
if (!FLAG(d)) {
- if (bits) wraputchar(toybuf[out<<(6-bits)], &x);
- while (TT.total&3) wraputchar('=', &x);
+ if (bits) wraputchar(toybuf[out<<(TT.n-bits)], &x);
+ while (TT.total&TT.align) wraputchar('=', &x);
if (x) xputc('\n');
}
@@ -62,8 +76,8 @@ static void do_base64(int fd, char *name)
if (buf[i] == '=') return;
if ((x = stridx(toybuf, buf[i])) != -1) {
- out = (out<<6) + x;
- bits += 6;
+ out = (out<<TT.n) + x;
+ bits += TT.n;
if (bits >= 8) {
putchar(out >> (bits -= 8));
out &= (1<<bits)-1;
@@ -78,8 +92,8 @@ static void do_base64(int fd, char *name)
} else {
out = (out<<8) + buf[i];
bits += 8;
- while (bits >= 6) {
- wraputchar(toybuf[out >> (bits -= 6)], &x);
+ while (bits >= TT.n) {
+ wraputchar(toybuf[out >> (bits -= TT.n)], &x);
out &= (1<<bits)-1;
}
}
@@ -89,6 +103,20 @@ static void do_base64(int fd, char *name)
void base64_main(void)
{
+ TT.n = 6;
+ TT.align = 3;
base64_init(toybuf);
- loopfiles(toys.optargs, do_base64);
+ loopfiles(toys.optargs, do_base_n);
+}
+
+#define CLEANUP_base64
+#define FOR_base32
+#include "generated/flags.h"
+
+void base32_main(void)
+{
+ TT.n = 5;
+ TT.align = 7;
+ base32_init(toybuf);
+ loopfiles(toys.optargs, do_base_n);
}