-
Notifications
You must be signed in to change notification settings - Fork 20
/
mnencode.c
90 lines (78 loc) · 1.67 KB
/
mnencode.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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <ctype.h>
#include "mnemonic.h"
int counthex(const mn_byte *s, size_t len) {
int total = 0;
size_t i;
for(i = 0; i < len; i++) {
if(isxdigit(s[i])) total++;
}
return total;
}
int countLeadingCrapAndZeros(const mn_byte *in, size_t inlen) {
size_t i;
for(i = 0; i < inlen; i++) {
if(in[i] != '0' && isxdigit(in[i])) break; /* Find first non-zero hex digit */
}
return i;
}
int hex2bytes(int inlen, const mn_byte *in, mn_byte *out) {
char t[3];
int i, j = 0, k = 0;
i = countLeadingCrapAndZeros(in, inlen);
inlen = inlen - i;
in = in + i;
if(counthex(in, inlen) % 2 != 0) {
t[j++] = '0';
}
for(i = 0; i < inlen; i++) {
if(isxdigit(in[i])) {
t[j++] = in[i];
if(j == 2) {
t[2] = '\0';
errno = 0;
out[k++] = strtol(t, NULL, 16);
if(errno != 0) {
return -1;
}
j = 0;
}
}
}
return k;
}
int main(int argc, char **argv) {
mn_byte *buf;
mn_byte cbuf[0x10000];
mn_byte xbuf[0x8000];
char outbuf[0x90000];
int buflen;
int n;
int hex_encoded_input = 0;
if(argc > 1 && strcmp(argv[1],"-x") == 0) {
hex_encoded_input = 1;
}
fprintf (stderr, "%s\n", mn_wordlist_version);
buflen = fread(cbuf, 1, sizeof cbuf, stdin);
if(hex_encoded_input) {
buflen = hex2bytes(buflen, cbuf, xbuf);
if(buflen < 0) {
perror("hex2bytes");
return EXIT_FAILURE;
}
buf = xbuf;
} else {
buf = cbuf;
}
n = mn_encode(buf, buflen, outbuf, sizeof outbuf, "x x x. x x x. x x x\n");
if(n != 0) {
fprintf(stderr, "mn_encode error %d\n", n);
return EXIT_FAILURE;
}
fwrite(outbuf, 1, strlen (outbuf), stdout);
putchar('\n');
return EXIT_SUCCESS;
}