From a256e152851c4fa89635a5739f66d31913aaae7f Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Mon, 14 Jul 2008 17:35:21 +0200 Subject: [PATCH] Libucw: basecode utility and its tests. (Several Michal's commits flattened to one.) --- lib/Makefile | 7 ++- lib/asort-test.c | 2 +- lib/base224.c | 43 ----------------- lib/basecode.c | 121 +++++++++++++++++++++++++++++++++++++++++++++++ lib/basecode.t | 16 +++++++ 5 files changed, 143 insertions(+), 46 deletions(-) create mode 100644 lib/basecode.c create mode 100644 lib/basecode.t diff --git a/lib/Makefile b/lib/Makefile index b6b333db..e1cc9ec0 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -7,6 +7,8 @@ ifdef CONFIG_UCW_DBTOOL PROGS+=$(o)/lib/db-tool endif +PROGS+=$(o)/lib/basecode + LIBUCW_MODS= \ threads \ alloc alloc_str realloc bigalloc mempool mempool-str mempool-fmt eltpool \ @@ -93,12 +95,12 @@ $(o)/lib/binheap-test: $(o)/lib/binheap-test.o $(LIBUCW) $(o)/lib/lizard-test: $(o)/lib/lizard-test.o $(LIBUCW) $(o)/lib/kmp-test: $(o)/lib/kmp-test.o $(LIBUCW) $(LIBCHARSET) $(o)/lib/ipaccess-test: $(o)/lib/ipaccess-test.o $(LIBUCW) -$(o)/lib/trie-test: $(o)/lib/trie-test.o $(LIBUCW) +$(o)/lib/basecode: $(o)/lib/basecode.o $(LIBUCW) TESTS+=$(addprefix $(o)/lib/,regex.test unicode.test hash-test.test mempool.test stkstring.test \ slists.test kmp-test.test bbuf.test getopt.test fastbuf.test ff-unicode.test eltpool.test \ fb-socket.test trie-test.test string.test sha1.test asort-test.test binheap-test.test \ - redblack-test.test) + redblack-test.test basecode.test) $(o)/lib/regex.test: $(o)/lib/regex-t $(o)/lib/unicode.test: $(o)/lib/unicode-t @@ -120,6 +122,7 @@ $(o)/lib/trie-test.test: $(o)/lib/trie-test $(o)/lib/asort-test.test: $(o)/lib/asort-test $(o)/lib/binheap-test.test: $(o)/lib/binheap-test $(o)/lib/redblack-test.test: $(o)/lib/redblack-test +$(o)/lib/basecode.test: $(o)/lib/basecode ifdef CONFIG_UCW_THREADS TESTS+=$(addprefix $(o)/lib/,asio.test) diff --git a/lib/asort-test.c b/lib/asort-test.c index 8662e7a3..5eaf9e14 100644 --- a/lib/asort-test.c +++ b/lib/asort-test.c @@ -38,7 +38,7 @@ static void generate(void) #endif } -int errors = 0; +static int errors = 0; static void check(void) { diff --git a/lib/base224.c b/lib/base224.c index 3d624717..15a40243 100644 --- a/lib/base224.c +++ b/lib/base224.c @@ -168,46 +168,3 @@ base224_decode(byte *dest, const byte *src, uns len) while (len0 == 5); return dest-start; } - -#ifdef TEST - -#include - -int main(int argc, char **argv) -{ -#if 0 - byte i[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39 }; - byte o[256], w[256]; - uns l; - l = base224_encode(o, i, sizeof(i)); - fwrite(o, 1, l, stdout); - fputc(0xaa, stdout); - l = base224_decode(w, o, l); - fwrite(w, 1, l, stdout); -#else - if (argc > 1) - { - byte i[BASE224_OUT_CHUNK*17], o[BASE224_IN_CHUNK*17]; - uns l; - while (l = fread(i, 1, sizeof(i), stdin)) - { - l = base224_decode(o, i, l); - fwrite(o, 1, l, stdout); - } - } - else - { - byte i[BASE224_IN_CHUNK*23], o[BASE224_OUT_CHUNK*23]; - uns l; - while (l = fread(i, 1, sizeof(i), stdin)) - { - l = base224_encode(o, i, l); - fwrite(o, 1, l, stdout); - } - } -#endif - - return 0; -} - -#endif diff --git a/lib/basecode.c b/lib/basecode.c new file mode 100644 index 00000000..3c7f0c48 --- /dev/null +++ b/lib/basecode.c @@ -0,0 +1,121 @@ +/* + * UCW Library -- Line utility for encoding and decoding base64 & base224 + * + * (c) 2008, Michal Vaner + * + * This software may be freely distributed and used according to the terms + * of the GNU Lesser General Public License. + */ + +#include +#include +#include + +#include "lib/lib.h" +#include "lib/base64.h" +#include "lib/base224.h" +#include "lib/fastbuf.h" +#include "lib/getopt.h" + +static struct option opts[] = { + { "encode64", 0, 0, 'e' }, + { "decode64", 0, 0, 'd' }, + { "encode224", 0, 0, 'E' }, + { "decode224", 0, 0, 'D' }, + { "prefix", 1, 0, 'p' }, + { "blocks", 1, 0, 'b' }, + { 0, 0, 0, 0 } +}; + +static const struct { + uns (*function)(byte *, const byte *, uns); + uns in_block, out_block, num_blocks; + uns add_prefix; +} actions[] = { + { + base64_encode, + BASE64_IN_CHUNK, BASE64_OUT_CHUNK, 20, + 1 + }, + { + base64_decode, + BASE64_OUT_CHUNK, BASE64_IN_CHUNK, 20, + 0 + }, + { + base224_encode, + BASE224_IN_CHUNK, BASE64_OUT_CHUNK, 6, + 1 + }, + { + base224_decode, + BASE224_OUT_CHUNK, BASE224_IN_CHUNK, 6, + 0 + } +}; + +int main(int argc, char **argv) +{ + // Choose mode + int mode = -1; + char *prefix = NULL; + uns blocks = 0; + int opt; + while ((opt = getopt_long(argc, argv, "edEDp:b:", opts, NULL)) >= 0) + switch (opt) + { + case 'e': mode = 0; break; + case 'd': mode = 1; break; + case 'E': mode = 2; break; + case 'D': mode = 3; break; + case 'p': prefix = optarg; break; + case 'b': + { + char *end; + blocks = strtol(optarg, &end, 0); + if ((blocks > 0) && !*end) + break; + } + default: goto usage; + } + + if (mode == -1) + { + usage: + fprintf(stderr, "basecode mode [--prefix=prefix] [--blocks=number_of_blocks]\nMode is one of:\n\t--encode64 (-e)\n\t--decode64 (-d)\n\t--encode224 (-E)\n\t--decode224 (-D)\n"); + return 1; + } + if (!blocks) + blocks = actions[mode].num_blocks; + + // Prepare buffers + struct fastbuf *in = bfdopen_shared(0, 4096); + struct fastbuf *out = bfdopen_shared(1, 4096); + uns offset = (!actions[mode].add_prefix && prefix) ? strlen(prefix) : 0; + uns read_size = actions[mode].in_block * blocks + offset + !!offset; + uns write_size = actions[mode].out_block * blocks; + byte in_buff[read_size], out_buff[write_size]; + uns isize; + + // Recode it + while (isize = bread(in, in_buff, read_size)) + { + if (prefix) + { + if (actions[mode].add_prefix) + bputs(out, prefix); + else + if ((isize < offset) || (in_buff[isize-1] != '\n') + || (strncmp(prefix, in_buff, offset))) + die("Invalid line syntax"); + } + uns osize = actions[mode].function(out_buff, in_buff + offset, isize - offset - !!offset); + bwrite(out, out_buff, osize); + if (actions[mode].add_prefix && prefix) + bputc(out, '\n'); + } + + bclose(in); + bclose(out); + return 0; +} diff --git a/lib/basecode.t b/lib/basecode.t new file mode 100644 index 00000000..d0cb249a --- /dev/null +++ b/lib/basecode.t @@ -0,0 +1,16 @@ +# Tests for base64 and base224 modules + +Name: Base64 encode +Run: ../obj/lib/basecode -e +Input: Here are some test data +Output: SGVyZSBhcmUgc29tZSB0ZXN0IGRhdGEK + +Name: Base64 decode +Run: ../obj/lib/basecode -d +Input: SGVyZSBhcmUgc29tZSB0ZXN0IGRhdGEK +Output: Here are some test data + +Name: Base224 encode & decode +Run: ../obj/lib/basecode -E | ../obj/lib/basecode -D +Input: Some more test data for 224 encoding +Output: Some more test data for 224 encoding -- 2.39.2