5 #define isbase64(c) ( ((c) >= 'A' && (c) <= 'Z') || \
6 ((c) >= 'a' && (c) <= 'z') || \
7 ((c) >= '0' && (c) <= '9') || \
8 (c) == '+' || (c) == '/' || (c) == '=' \
11 int base64_decode_atom(char *atom
, unsigned char *out
) {
17 for (i
= 0; i
< 4; i
++) {
19 if (c
>= 'A' && c
<= 'Z')
21 else if (c
>= 'a' && c
<= 'z')
23 else if (c
>= '0' && c
<= '9')
32 return 0; /* invalid atom */
36 if (vals
[0] == -1 || vals
[1] == -1)
38 if (vals
[2] == -1 && vals
[3] != -1)
43 else if (vals
[2] != -1)
48 word
= ((vals
[0] << 18) |
50 ((vals
[2] & 0x3F) << 6) |
52 out
[0] = (word
>> 16) & 0xFF;
54 out
[1] = (word
>> 8) & 0xFF;
60 void base64_encode_atom(unsigned char *data
, int n
, char *out
) {
61 static const char base64_chars
[] =
62 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
71 out
[0] = base64_chars
[(word
>> 18) & 0x3F];
72 out
[1] = base64_chars
[(word
>> 12) & 0x3F];
74 out
[2] = base64_chars
[(word
>> 6) & 0x3F];
78 out
[3] = base64_chars
[word
& 0x3F];
83 const char usagemsg
[] =
84 "usage: base64 [-d] [filename] decode from a file or from stdin\n"
85 " or: base64 -e [-cNNN] [filename] encode from a file or from stdin\n"
86 "where: -d decode mode (default)\n"
88 " -cNNN set number of chars per line for encoded output\n"
89 " also: base64 --version report version number\n"
90 " base64 --help display this help text\n"
91 " base64 --licence display the (MIT) licence text\n"
95 fputs(usagemsg
, stdout
);
98 const char licencemsg
[] =
99 "base64 is copyright 2001,2004 Simon Tatham.\n"
101 "Permission is hereby granted, free of charge, to any person\n"
102 "obtaining a copy of this software and associated documentation files\n"
103 "(the \"Software\"), to deal in the Software without restriction,\n"
104 "including without limitation the rights to use, copy, modify, merge,\n"
105 "publish, distribute, sublicense, and/or sell copies of the Software,\n"
106 "and to permit persons to whom the Software is furnished to do so,\n"
107 "subject to the following conditions:\n"
109 "The above copyright notice and this permission notice shall be\n"
110 "included in all copies or substantial portions of the Software.\n"
112 "THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND,\n"
113 "EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\n"
114 "MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\n"
115 "NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS\n"
116 "BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN\n"
117 "ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\n"
118 "CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n"
123 fputs(licencemsg
, stdout
);
127 #define SVN_REV "$Revision$"
128 char rev
[sizeof(SVN_REV
)];
131 strcpy(rev
, SVN_REV
);
133 for (p
= rev
; *p
&& *p
!= ':'; p
++);
136 while (*p
&& isspace(*p
)) p
++;
137 for (q
= p
; *q
&& *q
!= '$'; q
++);
139 printf("base64 revision %s\n", p
);
141 printf("base64: unknown version\n");
145 int main(int ac
, char **av
) {
162 if (!strcmp(p
, "version")) {
165 } else if (!strcmp(p
, "help")) {
168 } else if (!strcmp(p
, "licence") ||
169 !strcmp(p
, "license")) {
173 fprintf(stderr
, "base64: unknown long option '--%s'\n",
196 * Options requiring values.
204 fprintf(stderr
, "base64: option '-%c' expects"
205 " an argument\n", c
);
210 cpl
= strtol(v
, &eptr
, 10);
212 fprintf(stderr
, "base64: option -c expects"
213 " a numeric argument\n");
217 fprintf(stderr
, "base64: chars per line should be"
218 " divisible by 4\n");
231 fprintf(stderr
, "base64: expected only one filename\n");
238 fp
= fopen(fname
, encoding ?
"rb" : "r");
240 fprintf(stderr
, "base64: unable to open '%s': %s\n", fname
,
255 if (cpl
&& column
>= cpl
) {
259 n
= fread(in
, 1, 3, fp
);
261 base64_encode_atom(in
, n
, out
);
262 fwrite(out
, 1, 4, stdout
);
269 unsigned char out
[3];
274 for (i
= 0; i
< 4; i
++) {
277 } while (c
!= EOF
&& !isbase64(c
));
286 fprintf(stderr
, "base64: warning: number of base64"
287 " characters was not a multiple of 4\n");
288 while (i
< 4) in
[i
++] = '=';
290 n
= base64_decode_atom(in
, out
);
291 fwrite(out
, 1, n
, stdout
);