-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
176 lines (144 loc) · 4.15 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/* base1 en- and decoder with usage similar to GNU's base64.
(C) 2016 Tobias Girstmair, http://isticktoit.net/
Released under the GNU GPL v3. See LICENSE for details. */
#define _XOPEN_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <limits.h>
#include <stdint.h>
#define VERSION "0.1"
#define DEFAULT_WRAP 72
#define COPYRIGHT "(C) 2016 Tobias Girstmair, GPLv3"
#define HELP_TEXT \
"Usage: %s [OPTION]... [FILE]\n"\
"Options: -d (decode), -i (ignore-garbage), -w COLS (wrap, default %d)"\
" -t (test) -v (version) -h (help)\n"
#define BASE ((1<<CHAR_BIT)) //2 ^ (bits of a byte)
struct ops {
FILE* f;
int d; /*decode*/
int i; /*ignore-garbage*/
int w; /*wrap*/
int t; /*test*/
};
size_t highestOneBitPosition(unsigned long long a);
int multiplication_is_safe(unsigned long long a, unsigned long long b);
int addition_is_safe(unsigned long long a, unsigned long long b);
int do_encode (FILE* in, FILE* out, int wrap_column, int test_only);
int do_decode (FILE* in, FILE* out, int ignore_garbage);
int main (int argc, char** argv) {
struct ops op = {stdin, 0, 0, DEFAULT_WRAP, 0};
int opt;
opterr=0; /* suppress default error messages */
while ( (opt = getopt(argc, argv, "diw:tvh")) != -1) {
switch (opt) {
case 'd': op.d = 1; break;
case 'i': op.i = 1; break;
case 'w': op.w = atoi (optarg); break;
case 't': op.t = 1; break;
case 'v':
fprintf (stderr, "base1 %s\n%s\n", VERSION, COPYRIGHT);
return 0;
case 'h':
fprintf (stderr, HELP_TEXT, argv[0], DEFAULT_WRAP);
return 0;
default:
fprintf (stderr, "unknown option '-%c'.\n", optopt);
return 1;
}
}
if (argc-optind > 1) {
fprintf (stderr, "%s: extra operand '%s'. \n", argv[0], argv[argc-1]);
return 1;
} else if (optind < argc) {
if (strcmp (argv [optind], "-") != 0)
op.f = fopen (argv[optind], "rb");
}
int retval;
if (op.d) {
retval = do_decode (op.f, stdout, op.i);
} else {
retval = do_encode (op.f, stdout, op.w, op.t);
}
if (op.f != stdin) retval = !(fclose (op.f) == 0);
return retval;
}
int do_encode (FILE* in, FILE* out, int wrap_column, int test_only) {
int in_len = 0;
unsigned long long out_len = 0;
for (int in_char; (in_char = getc(in)) != EOF; in_len++) {
if (!multiplication_is_safe (out_len, BASE)){
fputs ("overflowed.", stderr);
return 1;
}
out_len *= BASE;
if (!addition_is_safe (out_len, in_char)) {
fputs ("overflowed.", stderr);
return 1;
}
out_len += in_char;
}
if (test_only) {
printf ("Length of output:\t%llu\nMaximum value of ULL:\t%llu\n", out_len, ULLONG_MAX);
return 0;
}
unsigned long long block_size = 1;
for (int i = 0; i < in_len; i++) {
out_len += block_size;
block_size *= BASE;
}
for (unsigned long long i = 0; i < out_len; i++)
fprintf (out, "%s%c", wrap_column&&!(i%wrap_column)&&i?"\n":"", 'A');
//TODO: use faster function
return 0;
}
int do_decode (FILE* in, FILE* out, int ignore_garbage) {
unsigned long long in_len = 0;
for (int in_char; (in_char = getc (in)) != EOF;) {
if (in_char == ' ' || in_char == '\n' || in_char == '\t') {
continue;
} else if (in_char == 'A') {
in_len++;
} else if (!ignore_garbage) {
fprintf (stderr, "Unrecognized glyph %c\n", in_char);
return 1;
}
// TODO: check for overflow!
}
int bin_len = 0;
unsigned long long block_size = 1;
while (in_len >= block_size) {
in_len -= block_size;
bin_len++;
block_size *= BASE;
}
char b;
char out_buf[bin_len];
for (int byte_num = bin_len-1; byte_num >= 0; byte_num--) {
b = in_len & (BASE - 1);
out_buf[byte_num] = b;
in_len -= b;
in_len /= BASE;
}
for (int i = 0; i < bin_len; i++) putc (out_buf[i], out);
return 0;
}
/* http://stackoverflow.com/a/199455 */
int addition_is_safe(unsigned long long a, unsigned long long b) {
size_t a_bits=highestOneBitPosition(a), b_bits=highestOneBitPosition(b);
return (a_bits<64 && b_bits<64);
}
int multiplication_is_safe(unsigned long long a, unsigned long long b) {
size_t a_bits=highestOneBitPosition(a), b_bits=highestOneBitPosition(b);
return (a_bits+b_bits<=64);
}
size_t highestOneBitPosition(unsigned long long a) {
size_t bits=0;
while (a!=0) {
++bits;
a>>=1;
};
return bits;
}