]> mj.ucw.cz Git - libucw.git/blob - ucw/b224.c
Moved some utils from utils/ to ucw/
[libucw.git] / ucw / b224.c
1 /*
2  *      A Program For Manipulation With Base224 Encoded Files
3  *
4  *      (c) 2002 Martin Mares <mj@ucw.cz>
5  */
6
7 #include "ucw/lib.h"
8 #include "ucw/fastbuf.h"
9 #include "ucw/base224.h"
10
11 #include <stdio.h>
12
13 int main(int argc, char **argv)
14 {
15   struct fastbuf *in = bfdopen_shared(0, 4096);
16   struct fastbuf *out = bfdopen_shared(1, 4096);
17   byte ib[BASE224_IN_CHUNK*10], ob[BASE224_OUT_CHUNK*10], *b;
18   uns il, ol;
19
20   if (argc != 2 || argv[1][0] != '-')
21     goto usage;
22
23   switch (argv[1][1])
24     {
25     case 'e':                           /* Plain encoding */
26       while (il = bread(in, ib, sizeof(ib)))
27         {
28           ol = base224_encode(ob, ib, il);
29           bwrite(out, ob, ol);
30         }
31       break;
32     case 'E':                           /* Line block encoding */
33       while (il = bread(in, ib, BASE224_IN_CHUNK*6))
34         {
35           ol = base224_encode(ob, ib, il);
36           bputc(out, 'N');
37           bwrite(out, ob, ol);
38           bputc(out, '\n');
39         }
40       break;
41     case 'd':                           /* Plain decoding */
42       while (ol = bread(in, ob, sizeof(ob)))
43         {
44           il = base224_decode(ib, ob, ol);
45           bwrite(out, ib, il);
46         }
47       break;
48     case 'D':                           /* Line block decoding */
49       while (b = bgets(in, ob, sizeof(ob)))
50         {
51           if (!ob[0])
52             die("Invalid line syntax");
53           il = base224_decode(ib, ob+1, b-ob-1);
54           bwrite(out, ib, il);
55         }
56       break;
57     default:
58     usage:
59       fputs("Usage: b224 (-e|-E|-d|-D)\n", stderr);
60       return 1;
61     }
62
63   bclose(in);
64   bclose(out);
65   return 0;
66 }