]>
git.gir.st - base65536.git/blob - main.c
1 /* base65536 en- and decoder with usage similar to GNU's base64.
2 (C) 2016 Tobias Girstmair, http://isticktoit.net/
3 Released under the GNU GPL v3. See LICENSE for details. */
10 #include "base65536.h"
14 #define DEFAULT_WRAP 72
16 #define COPYRIGHT "(C) 2016 Tobias Girstmair, GPLv3"
18 "Usage: %s [OPTION]... [FILE]\n"\
19 "Options: -d (decode), -i (ignore-garbage), -w COLS (wrap, default %d) "\
20 "-v (version) -h (help)\n"
25 int i
; /*ignore-garbage*/
29 void do_encode (FILE* in
, FILE* out
, int wrap_column
);
30 void do_decode (FILE* in
, FILE* out
, int ignore_garbage
);
32 int main (int argc
, char** argv
) {
33 struct ops op
= {stdin
, 0, 0, DEFAULT_WRAP
};
36 opterr
=0; /* suppress default error messages */
37 while ( (opt
= getopt(argc
, argv
, "diw:vh")) != -1) {
39 case 'd': op
.d
= 1; break;
40 case 'i': op
.i
= 1; break;
41 case 'w': op
.w
= atoi (optarg
); break;
43 fprintf (stderr
, "base65536 %s\n%s\n", VERSION
, COPYRIGHT
);
46 fprintf (stderr
, HELP_TEXT
, argv
[0], DEFAULT_WRAP
);
49 fprintf (stderr
, "unknown option '-%c'.\n", optopt
);
53 if (argc
-optind
> 1) {
54 fprintf (stderr
, "%s: extra operand '%s'. \n", argv
[0], argv
[argc
-1]);
56 } else if (optind
< argc
) {
57 if (strcmp (argv
[optind
], "-") != 0)
58 op
.f
= fopen (argv
[optind
], "rb");
62 do_decode (op
.f
, stdout
, op
.i
);
64 do_encode (op
.f
, stdout
, op
.w
);
67 if (op
.f
!= stdin
) fclose (op
.f
);
73 void do_encode (FILE* in
, FILE* out
, int wrap_column
) {
80 if ((in_char
[0] = getc(in
)) == EOF
) break;
81 in_char
[1] = getc(in
);
83 unicode_cp
= base65536_encode_char (in_char
);
84 codepoint_to_utf8 (unicode_cp
, out_utf8
);
85 fprintf (out
, "%s%s", wrap_column
&&!(cnt
%wrap_column
)&&cnt
?"\n":"", out_utf8
);
87 if (in_char
[1] == EOF
) break;
92 void do_decode (FILE* in
, FILE* out
, int ignore_garbage
) {
99 for (int i
= 0; i
< 5; i
++) {
103 } else if (i
!= 0 && c
< 0x80) { /* start of new asciichar */
107 } else if (i
!= 0 && c
>= 0xc0) { /* start of new utf8char */
114 unicode_cp
= utf8_to_codepoint (in_utf8
);
115 /* ignore ascii-chars, because base65k won't map to those (but
116 whitespace may be added by the encoder / medium) */
117 if (unicode_cp
< 0x80) continue;
119 chars_written
= base65536_decode_char (unicode_cp
, out_char
);
120 if (chars_written
> 0) {
121 fputc (out_char
[0], out
);
122 if (chars_written
==2) fputc (out_char
[1], out
);
124 if (!ignore_garbage
) {
125 fprintf (stderr
, "Unrecognized glyph %s\n", in_utf8
);