-
Notifications
You must be signed in to change notification settings - Fork 0
/
drunken_bishop.c
411 lines (346 loc) · 8.71 KB
/
drunken_bishop.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#include <memory.h>
#include <malloc.h>
#include <stdlib.h>
#include <ctype.h>
#define max(x, y) ((x) > (y) ? (x) : (y))
#define min(x, y) ((x) < (y) ? (x) : (y))
#define limit(n, from, to) max(min(n, to - 1), from)
#define single_hex(dec) \
((dec) < 10 ? '0' + (dec) : 'a' + (dec)-10)
#define F_WIDTH 17
#define F_HEIGHT 9
#define BUFSIZE 1024 * 1024 // 1MB
#define VERSION "1.1.0"
#define STORY \
"Bishop Peter finds himself in the middle of an\n" \
"ambient atrium. There are walls on all four sides\n" \
"and apparently there is no exit. The floor is paved\n" \
"with square tiles, strictly alternating between\n" \
"black and white. His head heavily aching—probably\n" \
"from too much wine he had before—he starts\n" \
"wandering around randomly. Well, to be exact, he\n" \
"only makes diagonal steps—just like a bishop on a\n" \
"chess board. When he hits a wall, he moves to the\n" \
"side, which takes him from the black tiles to the\n" \
"white tiles (or vice versa). And after each move,\n" \
"he places a coin on the floor, to remember that he\n" \
"has been there before. After 64 steps, just when no\n" \
"coins are left, Peter suddenly wakes up. What a\n" \
"strange dream!\n"
static void to_lower(char* dest, const char* src) {
char* i = dest;
const char* c = src;
while(*c) {
if(*c >= 'A' && *c <= 'Z')
*i = *c - 'A' + 'a';
else
*i = *c;
++i;
++c;
}
}
enum InputType {
/** \brief 十六进制字符串 */
HEX,
/** \brief 字节流 */
BYTES
};
enum InputType get_input_type(const char* in) {
char buf[16] = {0};
to_lower(buf, in);
if(strcmp(buf, "hex") == 0)
return HEX;
else if(strcmp(buf, "bytes") == 0)
return BYTES;
else
return -1;
}
typedef struct {
int x, y;
} Point;
static int get_pos(const Point pnt) {
return pnt.x + pnt.y * F_WIDTH;
}
typedef struct {
size_t w, h;
int* filed;
} Filed;
static void filed_print(const Filed* filed) {
putc('+', stdout);
for(int i = 0; i < filed->w; i++)
putc('-', stdout);
fputs("+\n", stdout);
for(int y = 0; y < filed->h; y++) {
putc('|', stdout);
for(int x = 0; x < filed->w; x++) {
Point pnt = {x, y};
putc(" .o+=*BOX@%&#/^SE"[filed->filed[get_pos(
pnt)]],
stdout);
}
fputs("|\n", stdout);
}
putc('+', stdout);
for(int i = 0; i < filed->w; i++)
putc('-', stdout);
fputs("+\n", stdout);
}
static void filed_init(Filed* filed) {
filed->w = F_WIDTH;
filed->h = F_HEIGHT;
filed->filed =
(int*)calloc(filed->w * filed->h, sizeof(int));
}
static void filed_free(Filed* filed) {
free(filed->filed);
filed->filed = NULL;
}
/**
* \param dir direction
*
* 0b00 UP_LEFT \n
* 0b01 UP_RIGHT \n
* 0b10 DOWN_LEFT \n
* 0b11 DOWN_RIGHT \n
*/
static void move(Point* pnt, int dir) {
pnt->x += ((dir & 0b01) << 1) - 1;
pnt->x = limit(pnt->x, 0, F_WIDTH);
pnt->y += (dir & 0b10) - 1;
pnt->y = limit(pnt->y, 0, F_HEIGHT);
}
static void bytes_to_hex(const char* const bytes,
size_t size, char* hex) {
char* offset = hex;
for(int i = 0; i < size; i++) {
*(offset++) = single_hex(
(bytes[i] & (((1 << 4) - 1) << 4)) >> 4);
*(offset++) = single_hex(bytes[i] & ((1 << 4) - 1));
}
*offset = '\0';
}
static int any_to_dec(const char* const str, int base) {
char buffer[BUFSIZE];
int digit = 0;
to_lower(buffer, str);
for(const char* i = buffer; *i; i++)
digit = digit * base + *i
- (*i >= 'a' ? 'a' - 10 : '0');
return digit;
}
static int bishop_move(const char* const hex,
Point* const pnt,
Filed* const filed) {
Point start_pnt = *pnt;
const char* c = hex;
char block[3];
block[2] = '\0';
while(*c) {
block[0] = *c++;
if(!*c) {
fputs(
"length of given hex-string is not even.\n",
stderr);
return -1;
}
block[1] = *c++;
int bits = any_to_dec(block, 16);
for(int i = 0; i < 4; i++) {
move(pnt, bits & 0b11);
bits >>= 2;
filed->filed[get_pos(*pnt)] += 1;
}
}
Point end_pnt = *pnt;
filed->filed[get_pos(start_pnt)] = 15;
filed->filed[get_pos(end_pnt)] = 16;
return 0;
}
static int print_fingerprint(const char* const hex_str) {
int err = 0;
Filed filed;
filed_init(&filed);
Point bishop_pnt = {8, 4};
if((err = bishop_move(hex_str, &bishop_pnt, &filed)))
goto ERROR;
filed_print(&filed);
ERROR:
filed_free(&filed);
return err;
}
/**
* \brief 删除文件尾部连续的空行与空字符
*/
static void remove_end_whitespace(char* buffer) {
char* offset = buffer;
while(offset != buffer) {
if(isspace(*(--offset)))
*offset = '\0';
else
break;
}
}
static size_t read_file(const char* file_path,
char* buffer) {
FILE* file = NULL;
if(strcmp(file_path, "-") == 0)
file = stdin;
else
file = fopen(file_path, "r");
if(file != NULL) {
char* offset = buffer;
while((*offset++ = fgetc(file)) != EOF)
/* nothing */;
*(--offset) = '\0';
fclose(file);
return (size_t)(offset - buffer);
} else {
fputs(file_path, stderr);
fputs(": no such file\n", stderr);
return -1;
}
}
int main(int argc, char* argv[]) {
const char* hex_str =
"fc94b0c1e5b0987c5843997697ee9fb7";
int is_help = 0; // 打印帮助信息
int is_version = 0; // 打印版本信息
int is_quiet = 0; // quiet 模式
int is_story = 0; // 打印 Peter 的故事
char* type_str = "hex"; // 输入的类型, 以字符串表示
enum InputType type = HEX; // 输入的类型
const char* file_path = NULL;
int opt_hex = 0; // 是hash字符串
for(int i = 1; i < argc; i++) {
char* arg = argv[i];
if(*arg != '-') {
if(!opt_hex) {
hex_str = arg;
opt_hex = 1;
} else {
fputs("unknown param: `", stderr);
fputs(arg, stderr);
fputs("'\n", stderr);
return 1;
}
continue;
}
arg++;
if(*arg == '-') { // 长选项
int opt_is_value = 0;
arg++;
if(strcmp(arg, "version") == 0) {
is_version = 1;
} else if(strcmp(arg, "help") == 0) {
is_help = 1;
} else if(strcmp(arg, "story") == 0) {
is_story = 1;
} else if(strcmp(arg, "quiet") == 0) {
is_quiet = 1;
} else if(strcmp(arg, "in") == 0) {
opt_is_value = 1;
file_path = argv[++i];
} else if(strcmp(arg, "type") == 0) {
opt_is_value = 1;
type_str = argv[++i];
} else {
fputs("unrecognized flag `--", stderr);
fputs(arg, stderr);
fputs("'\n", stderr);
return 1;
}
if(opt_is_value && i == argc) {
// 需要值的选项是参数列表最后一个时
// e.g. -q --in
fputs("expected value after `", stderr);
fputs(argv[i - 1], stderr);
fputs("'\n", stderr);
return 1;
}
continue;
}
for(; *arg != '\0'; ++arg) { // 短选项
int opt_is_value = 0;
switch(*arg) {
case 'h':
is_help = 1;
break;
case 'q':
is_quiet = 1;
break;
case 'i':
opt_is_value = 1;
file_path = argv[++i];
break;
case 't':
opt_is_value = 1;
type_str = argv[++i];
break;
default:
fputs("unrecognized flag `-", stderr);
fputs(arg, stderr);
fputs("'\n", stderr);
return 1;
}
if(opt_is_value
&& (*(arg + 1) != '\0' || i == argc)) {
// 需要值的短选项不是最后一个短选项时
// e.g. -iq -
// 或需要值的短选项是参数列表最后一个时
// e.g. -q -i
fputs("expected value after `-", stderr);
fputc(*arg, stderr);
fputs("'\n", stderr);
return 1;
}
}
}
if(is_version) {
fputs("bishop v" VERSION " by EiEddie\n", stdout);
return 0;
}
if(is_help) {
fputs(
"The hash fingerprint visualization algorithm, like OpenSSH.\n"
"Usage: bishop [OPTION] [data]\n"
"\n"
" -h, --help print help info\n"
" --version print version info\n"
" -q, --quiet don't echo hex input\n"
" -i, --in <file> use file, if '-' use stdin\n"
" --story read the story of Bishop Peter\n"
" -t, --type <type> specify the input type of data or file\n"
" hex - HEX string (default)\n"
" bytes - bytes stream\n",
stdout);
return 0;
}
if(is_story) {
fputs(STORY, stdout);
return 0;
}
type = get_input_type(type_str);
if(type == -1) {
fputs("unknown input type\n", stderr);
return 1;
}
char buffer[BUFSIZE];
char bytes[BUFSIZE];
size_t size;
if(file_path != NULL) {
if((size = read_file(file_path, buffer)) != -1) {
if(type == HEX) {
remove_end_whitespace(buffer);
} else if(type == BYTES) {
memcpy(bytes, buffer, size * sizeof(char));
bytes_to_hex(bytes, size, buffer);
}
hex_str = buffer;
} else {
return 1;
}
}
if(!is_quiet)
printf("fingerprint of %s:\n", hex_str);
return print_fingerprint(hex_str);
}