7 #define isbase64(c) ( ((c) >= 'A' && (c) <= 'Z') || \
8 ((c) >= 'a' && (c) <= 'z') || \
9 ((c) >= '0' && (c) <= '9') || \
10 (c) == '+' || (c) == '/' || (c) == '=' \
13 int base64_decode_atom(char *atom
, unsigned char *out
) {
19 for (i
= 0; i
< 4; i
++) {
21 if (c
>= 'A' && c
<= 'Z')
23 else if (c
>= 'a' && c
<= 'z')
25 else if (c
>= '0' && c
<= '9')
34 return 0; /* invalid atom */
38 if (vals
[0] == -1 || vals
[1] == -1)
40 if (vals
[2] == -1 && vals
[3] != -1)
45 else if (vals
[2] != -1)
50 word
= ((vals
[0] << 18) |
52 ((vals
[2] & 0x3F) << 6) |
54 out
[0] = (word
>> 16) & 0xFF;
56 out
[1] = (word
>> 8) & 0xFF;
62 void base64_encode_atom(unsigned char *data
, int n
, char *out
) {
63 static const char base64_chars
[] =
64 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
73 out
[0] = base64_chars
[(word
>> 18) & 0x3F];
74 out
[1] = base64_chars
[(word
>> 12) & 0x3F];
76 out
[2] = base64_chars
[(word
>> 6) & 0x3F];
80 out
[3] = base64_chars
[word
& 0x3F];
85 const char usagemsg
[] =
86 "usage: base64 [-d] [filename] decode from a file or from stdin\n"
87 " or: base64 -e [-cNNN] [filename] encode from a file or from stdin\n"
88 "where: -d decode mode (default)\n"
90 " -cNNN set number of chars per line for encoded output\n"
91 " also: base64 --version report version number\n"
92 " base64 --help display this help text\n"
93 " base64 --licence display the (MIT) licence text\n"
97 fputs(usagemsg
, stdout
);
100 const char licencemsg
[] =
101 "base64 is copyright 2001,2004 Simon Tatham.\n"
103 "Permission is hereby granted, free of charge, to any person\n"
104 "obtaining a copy of this software and associated documentation files\n"
105 "(the \"Software\"), to deal in the Software without restriction,\n"
106 "including without limitation the rights to use, copy, modify, merge,\n"
107 "publish, distribute, sublicense, and/or sell copies of the Software,\n"
108 "and to permit persons to whom the Software is furnished to do so,\n"
109 "subject to the following conditions:\n"
111 "The above copyright notice and this permission notice shall be\n"
112 "included in all copies or substantial portions of the Software.\n"
114 "THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND,\n"
115 "EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\n"
116 "MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\n"
117 "NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS\n"
118 "BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN\n"
119 "ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\n"
120 "CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n"
125 fputs(licencemsg
, stdout
);
129 #define SVN_REV "$Revision$"
130 char rev
[sizeof(SVN_REV
)];
133 strcpy(rev
, SVN_REV
);
135 for (p
= rev
; *p
&& *p
!= ':'; p
++);
138 while (*p
&& isspace(*p
)) p
++;
139 for (q
= p
; *q
&& *q
!= '$'; q
++);
141 printf("base64 revision %s\n", p
);
143 printf("base64: unknown version\n");
147 int main(int ac
, char **av
) {
164 if (!strcmp(p
, "version")) {
167 } else if (!strcmp(p
, "help")) {
170 } else if (!strcmp(p
, "licence") ||
171 !strcmp(p
, "license")) {
175 fprintf(stderr
, "base64: unknown long option '--%s'\n",
198 * Options requiring values.
206 fprintf(stderr
, "base64: option '-%c' expects"
207 " an argument\n", c
);
212 cpl
= strtol(v
, &eptr
, 10);
214 fprintf(stderr
, "base64: option -c expects"
215 " a numeric argument\n");
219 fprintf(stderr
, "base64: chars per line should be"
220 " divisible by 4\n");
233 fprintf(stderr
, "base64: expected only one filename\n");
240 fp
= fopen(fname
, encoding ?
"rb" : "r");
242 fprintf(stderr
, "base64: unable to open '%s': %s\n", fname
,
257 if (cpl
&& column
>= cpl
) {
261 n
= fread(in
, 1, 3, fp
);
263 base64_encode_atom(in
, n
, out
);
264 fwrite(out
, 1, 4, stdout
);
271 unsigned char out
[3];
276 for (i
= 0; i
< 4; i
++) {
279 } while (c
!= EOF
&& !isbase64(c
));
288 fprintf(stderr
, "base64: warning: number of base64"
289 " characters was not a multiple of 4\n");
290 while (i
< 4) in
[i
++] = '=';
292 n
= base64_decode_atom(in
, out
);
293 fwrite(out
, 1, n
, stdout
);