aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xtests/base64.test24
-rw-r--r--toys/other/base64.c8
2 files changed, 27 insertions, 5 deletions
diff --git a/tests/base64.test b/tests/base64.test
new file mode 100755
index 00000000..2058198b
--- /dev/null
+++ b/tests/base64.test
@@ -0,0 +1,24 @@
+#!/bin/bash
+
+[ -f testing.sh ] && . testing.sh
+
+#testing "name" "command" "result" "infile" "stdin"
+
+testing "simple" "base64" "c2ltcGxlCg==\n" "" "simple\n"
+testing "file" "base64 input" "c2ltcGxlCg==\n" "simple\n" ""
+testing "simple -d" "base64 -d" "simple\n" "" "c2ltcGxlCg==\n"
+testing "simple -d" "base64 -d input" "simple\n" "c2ltcGxlCg==" ""
+testing "default wrap" "base64" \
+ "V2UndmUgcmVwbGFjZWQgdGhlIGRpbGl0aGl1bSB0aGV5IG5vcm1hbGx5IHVzZSB3aXRoIEZvbGdl\ncidzIENyeXN0YWxzLg==\n" \
+ "" "We've replaced the dilithium they normally use with Folger's Crystals."
+testing "multiline -d " "base64 -d" \
+ "We've replaced the dilithium they normally use with Folger's Crystals." "" \
+ "V2UndmUgcmVwbGFjZWQgdGhlIGRpbGl0aGl1bSB0aGV5IG5vcm1hbGx5IHVzZSB3aXRoIEZvbGdl\ncidzIENyeXN0YWxzLg==\n"
+
+testing "-w" "base64 -w 10" \
+ "TWFyY2hpbm\ncgdG8gdGhl\nIGJlYXQgb2\nYgYSBkaWZm\nZXJlbnQga2\nV0dGxlIG9m\nIGZpc2guCg\n==\n" \
+ "" "Marching to the beat of a different kettle of fish.\n"
+
+testing "-w0" "base64 -w0 input" \
+ "VmlraW5ncz8gVGhlcmUgYWluJ3Qgbm8gdmlraW5ncyBoZXJlLiBKdXN0IHVzIGhvbmVzdCBmYXJtZXJzLiBUaGUgdG93biB3YXMgYnVybmluZywgdGhlIHZpbGxhZ2VycyB3ZXJlIGRlYWQuIFRoZXkgZGlkbid0IG5lZWQgdGhvc2Ugc2hlZXAgYW55d2F5LiBUaGF0J3Mgb3VyIHN0b3J5IGFuZCB3ZSdyZSBzdGlja2luZyB0byBpdC4K" \
+ "Vikings? There ain't no vikings here. Just us honest farmers. The town was burning, the villagers were dead. They didn't need those sheep anyway. That's our story and we're sticking to it.\n" ""
diff --git a/toys/other/base64.c b/toys/other/base64.c
index 234ca1c3..33155bc5 100644
--- a/toys/other/base64.c
+++ b/toys/other/base64.c
@@ -4,7 +4,7 @@
*
* No standard
-USE_BASE64(NEWTOY(base64, "diw#<1[!dw]", TOYFLAG_USR|TOYFLAG_BIN))
+USE_BASE64(NEWTOY(base64, "diw#<0=76[!dw]", TOYFLAG_USR|TOYFLAG_BIN))
config BASE64
bool "base64"
@@ -16,7 +16,7 @@ config BASE64
-d decode
-i ignore non-alphabetic characters
- -w wrap output at COLUMNS (default 76)
+ -w wrap output at COLUMNS (default 76 or 0 for no wrap)
*/
#define FOR_base64
@@ -32,7 +32,7 @@ static void wraputchar(int c, int *x)
{
putchar(c);
TT.total++;
- if (++*x == TT.columns) {
+ if (TT.columns && ++*x == TT.columns) {
*x = 0;
xputc('\n');
};
@@ -89,8 +89,6 @@ static void do_base64(int fd, char *name)
void base64_main(void)
{
- if (!TT.columns) TT.columns = 76;
-
base64_init(toybuf);
loopfiles(toys.optargs, do_base64);
}