]>
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. */
12 #define DEFAULT_WRAP 72
18 void do_encode (FILE* in
, FILE* out
, int wrap_column
);
19 void do_decode (FILE* in
, FILE* out
, int ignore_garbage
);
21 int main (int argc
, char** argv
) {
22 int operation
= ENCODE
;
23 int wrap
= DEFAULT_WRAP
;
25 FILE* file_to_op
= stdin
;
27 for (int i
= 1; i
< argc
; i
++) {
28 //TODO: not efficient and ugly
29 if (strcmp (argv
[i
], "-d") == 0 || strcmp (argv
[i
], "--decode") == 0) {
31 } else if (strcmp (argv
[i
], "-i") == 0 || strcmp (argv
[i
], "--ignore-garbage") == 0) {
33 } else if (strcmp (argv
[i
], "-w") == 0) {
35 fputs ("option required for argument -w", stderr
);
38 for (int j
= 0; argv
[i
+1][j
] != '\0'; j
++)
39 if (argv
[i
+1][j
] < '0' || argv
[i
+1][j
] > '9') {
40 fputs ("option required for argument -w", stderr
);
44 wrap
= atoi (argv
[++i
]);
46 } else if (strcmp (argv
[i
], "--help") == 0) {
47 fprintf (stderr
, "help. ");
49 } else if (strcmp (argv
[i
], "--version") == 0) {
50 fprintf (stderr
, "base65536 version %s", VERSION
);
53 if (argv
[i
][0] == '-') {
54 fprintf (stderr
, "%s is not a valid argument.\n", argv
[i
]);
56 } else if (file_to_op
== stdin
) {
57 file_to_op
= fopen (argv
[i
], "rb");
59 fputs ("cannot give more than one file.\n", stderr
);
65 if (operation
== DECODE
) {
66 do_decode (file_to_op
, stdout
, garb_ignore
);
68 do_encode (file_to_op
, stdout
, wrap
);
71 if (file_to_op
!= stdin
) fclose (file_to_op
);
77 void do_encode (FILE* in
, FILE* out
, int wrap_column
) {
84 if ((in_char
[0] = getc(in
)) == EOF
) break;
85 in_char
[1] = getc(in
);
87 unicode_cp
= base65536_encode_char (in_char
);
88 codepoint_to_utf8 (unicode_cp
, out_utf8
);
89 fprintf (out
, "%s%s", wrap_column
&&!(cnt
%wrap_column
)&&cnt
?"\n":"", out_utf8
);
91 if (in_char
[1] == EOF
) break;
96 void do_decode (FILE* in
, FILE* out
, int ignore_garbage
) {
102 for (int i
= 0; i
< 5; i
++) {
106 } else if (i
!= 0 && c
< 0x80) { //start of new asciichar
110 } else if (i
!= 0 && c
>= 0xc0) { //start of new utf8char
117 unicode_cp
= utf8_to_codepoint (in_utf8
);
118 //ignore ascii-chars, because base65k won't map to those (but
119 //whitespace may be added by the encoder / medium)
120 if (unicode_cp
< 0x80) continue;
122 if (base65536_decode_char (unicode_cp
, out_char
) > 0) {
123 fputc (out_char
[0], out
);
124 fputc (out_char
[1], out
);
126 if (!ignore_garbage
) {
127 fprintf (stderr
, "Unrecognized glyph %s\n", in_utf8
);