aboutsummaryrefslogtreecommitdiff
path: root/toys/other/base64.c
blob: 22652fd055a6cbf055ea1dad249b8f1bfbc94c59 (plain)
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/* base64.c - Encode and decode base64
 *
 * Copyright 2014 Rob Landley <rob@landley.net>
 *
 * See https://tools.ietf.org/html/rfc4648

// These optflags have to match. Todo: cleanup and collapse together?
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"
  default y
  help
    usage: base64 [-di] [-w COLUMNS] [FILE...]

    Encode or decode in 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
#define FORCE_FLAGS
#include "toys.h"

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)
{
  putchar(c);
  TT.total++;
  if (TT.w && ++*x == TT.w) {
    *x = 0;
    xputc('\n');
  };
}

static void do_base(int fd, char *name)
{
  int out = 0, bits = 0, x = 0, i, len;
  char *buf = toybuf+128;

  TT.total = 0;

  for (;;) {
    // If no more data, flush buffer
    if (!(len = xread(fd, buf, sizeof(toybuf)-128))) {
      if (!FLAG(d)) {
        if (bits) wraputchar(toybuf[out<<(TT.n-bits)], &x);
        while (TT.total&TT.align) wraputchar('=', &x);
        if (x) xputc('\n');
      }

      return;
    }

    for (i=0; i<len; i++) {
      if (FLAG(d)) {
        if (buf[i] == '=') return;

        if ((x = stridx(toybuf, buf[i])) != -1) {
          out = (out<<TT.n) + x;
          bits += TT.n;
          if (bits >= 8) {
            putchar(out >> (bits -= 8));
            out &= (1<<bits)-1;
            if (ferror(stdout)) perror_exit(0);
          }

          continue;
        }
        if (buf[i] == '\n' || FLAG(i)) continue;

        break;
      } else {
        out = (out<<8) + buf[i];
        bits += 8;
        while (bits >= TT.n) {
          wraputchar(toybuf[out >> (bits -= TT.n)], &x);
          out &= (1<<bits)-1;
        }
      }
    }
  }
}

void base64_main(void)
{
  TT.n = 6;
  TT.align = 3;
  base64_init(toybuf);
  loopfiles(toys.optargs, do_base);
}

void base32_main(void)
{
  int i;

  TT.n = 5;
  TT.align = 7;
  for (i = 0; i<32; i++) toybuf[i] = i+(i<26 ? 'A' : 24);
  loopfiles(toys.optargs, do_base);
}