52d8a1292d38bbb6747eaa5821434cd878cde2b3
6 #define isbase64(c) ( ((c) >= 'A' && (c) <= 'Z') || \
7 ((c) >= 'a' && (c) <= 'z') || \
8 ((c) >= '0' && (c) <= '9') || \
9 (c) == '+' || (c) == '/' || (c) == '=' \
12 int base64_decode_atom(char *atom
, unsigned char *out
) {
18 for (i
= 0; i
< 4; i
++) {
20 if (c
>= 'A' && c
<= 'Z')
22 else if (c
>= 'a' && c
<= 'z')
24 else if (c
>= '0' && c
<= '9')
33 return 0; /* invalid atom */
37 if (vals
[0] == -1 || vals
[1] == -1)
39 if (vals
[2] == -1 && vals
[3] != -1)
44 else if (vals
[2] != -1)
49 word
= ((vals
[0] << 18) |
51 ((vals
[2] & 0x3F) << 6) |
53 out
[0] = (word
>> 16) & 0xFF;
55 out
[1] = (word
>> 8) & 0xFF;
61 void base64_encode_atom(unsigned char *data
, int n
, char *out
) {
62 static const char base64_chars
[] =
63 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
72 out
[0] = base64_chars
[(word
>> 18) & 0x3F];
73 out
[1] = base64_chars
[(word
>> 12) & 0x3F];
75 out
[2] = base64_chars
[(word
>> 6) & 0x3F];
79 out
[3] = base64_chars
[word
& 0x3F];
84 const char usagemsg
[] =
85 "usage: base64 [-d] [filename] decode from a file or from stdin\n"
86 " or: base64 -e [-cNNN] [filename] encode from a file or from stdin\n"
87 "where: -d decode mode (default)\n"
89 " -cNNN set number of chars per line for encoded output\n"
90 " also: base64 --version report version number\n"
91 " base64 --help display this help text\n"
92 " base64 --licence display the (MIT) licence text\n"
96 fputs(usagemsg
, stdout
);
99 const char licencemsg
[] =
100 "base64 is copyright 2001,2004 Simon Tatham.\n"
102 "Permission is hereby granted, free of charge, to any person\n"
103 "obtaining a copy of this software and associated documentation files\n"
104 "(the \"Software\"), to deal in the Software without restriction,\n"
105 "including without limitation the rights to use, copy, modify, merge,\n"
106 "publish, distribute, sublicense, and/or sell copies of the Software,\n"
107 "and to permit persons to whom the Software is furnished to do so,\n"
108 "subject to the following conditions:\n"
110 "The above copyright notice and this permission notice shall be\n"
111 "included in all copies or substantial portions of the Software.\n"
113 "THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND,\n"
114 "EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\n"
115 "MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\n"
116 "NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS\n"
117 "BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN\n"
118 "ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\n"
119 "CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n"
124 fputs(licencemsg
, stdout
);
128 #define SVN_REV "$Revision$"
129 char rev
[sizeof(SVN_REV
)];
132 strcpy(rev
, SVN_REV
);
134 for (p
= rev
; *p
&& *p
!= ':'; p
++);
137 while (*p
&& isspace(*p
)) p
++;
138 for (q
= p
; *q
&& *q
!= '$'; q
++);
140 printf("base64 revision %s\n", p
);
142 printf("base64: unknown version\n");
146 int main(int ac
, char **av
) {
163 if (!strcmp(p
, "version")) {
166 } else if (!strcmp(p
, "help")) {
169 } else if (!strcmp(p
, "licence") ||
170 !strcmp(p
, "license")) {
174 fprintf(stderr
, "base64: unknown long option '--%s'\n",
197 * Options requiring values.
205 fprintf(stderr
, "base64: option '-%c' expects"
206 " an argument\n", c
);
211 cpl
= strtol(v
, &eptr
, 10);
213 fprintf(stderr
, "base64: option -c expects"
214 " a numeric argument\n");
218 fprintf(stderr
, "base64: chars per line should be"
219 " divisible by 4\n");
232 fprintf(stderr
, "base64: expected only one filename\n");
239 fp
= fopen(fname
, encoding ?
"rb" : "r");
241 fprintf(stderr
, "base64: unable to open '%s': %s\n", fname
,
256 if (cpl
&& column
>= cpl
) {
260 n
= fread(in
, 1, 3, fp
);
262 base64_encode_atom(in
, n
, out
);
263 fwrite(out
, 1, 4, stdout
);
270 unsigned char out
[3];
275 for (i
= 0; i
< 4; i
++) {
278 } while (c
!= EOF
&& !isbase64(c
));
287 fprintf(stderr
, "base64: warning: number of base64"
288 " characters was not a multiple of 4\n");
289 while (i
< 4) in
[i
++] = '=';
291 n
= base64_decode_atom(in
, out
);
292 fwrite(out
, 1, n
, stdout
);