PROGS+=$(o)/lib/db-tool
endif
+PROGS+=$(o)/lib/basecode
+
LIBUCW_MODS= \
threads \
alloc alloc_str realloc bigalloc mempool mempool-str mempool-fmt eltpool \
$(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
$(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)
#endif
}
-int errors = 0;
+static int errors = 0;
static void check(void)
{
while (len0 == 5);
return dest-start;
}
-
-#ifdef TEST
-
-#include <stdio.h>
-
-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
--- /dev/null
+/*
+ * UCW Library -- Line utility for encoding and decoding base64 & base224
+ *
+ * (c) 2008, Michal Vaner <vorner@ucw.cz>
+ *
+ * This software may be freely distributed and used according to the terms
+ * of the GNU Lesser General Public License.
+ */
+
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#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;
+}
--- /dev/null
+# 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