-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathproquint_main.c
127 lines (108 loc) · 3.31 KB
/
proquint_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
/* This file is part of proquint: http://github.com/dsw/proquint .
See License.txt for copyright and terms of use. */
/*
Main driver for library proquint.c.
Please see the article on proquints: http://arXiv.org/html/0901.4016
Daniel S. Wilkerson
*/
#include "proquint.h"
#include <stdio.h> /* printf, fprintf */
#include <stdlib.h> /* exit */
#include <ctype.h> /* isdigit */
/* My atoi; it's easier to write it than to figure out how to get the
libraries to parse a string into an unsigned int.
*/
static uint32_t my_atoi(int base, char const *s) {
char const *const s_orig = s; /* for error messages */
uint32_t ret = 0;
int c;
for(; (c=*s); ++s) {
int value = -1;
switch(c) {
/* decimal digits */
case '0': value = 0; break;
case '1': value = 1; break;
case '2': value = 2; break;
case '3': value = 3; break;
case '4': value = 4; break;
case '5': value = 5; break;
case '6': value = 6; break;
case '7': value = 7; break;
case '8': value = 8; break;
case '9': value = 9; break;
case 'a': case 'A': value = 10; break;
case 'b': case 'B': value = 11; break;
case 'c': case 'C': value = 12; break;
case 'd': case 'D': value = 13; break;
case 'e': case 'E': value = 14; break;
case 'f': case 'F': value = 15; break;
/* illegal characters */
default:
fprintf(stderr, "Illegal character in uint-word '%s': '%c'.\n",
s_orig, c);
exit(1);
break;
}
if (value < 0) {
fprintf(stderr, "This cannot happen.");
exit(1);
}
if (value >= base) {
fprintf(stderr,
"Numbers of base %d may not contain the digit '%c': '%s'.\n",
base, c, s_orig);
exit(1);
}
ret *= base;
ret += value;
}
return ret;
}
static void main_convertNumber(int base, char const *s) {
/* Length of a 32-bit quint word, without trailing NUL:
two quints plus a separator. */
int const QUINT_LEN = 5*2 + 1;
/* Double length plus another separator in case we switch to
64-bits. */
int const DOUBLE_QUINT_LEN = 2*QUINT_LEN + 1;
int const n = my_atoi(base, s);
char quint[DOUBLE_QUINT_LEN+1];
int i;
for(i=0; i<DOUBLE_QUINT_LEN+1; ++i) quint[i] = '\0';
uint2quint(quint, n, '-');
/* fprintf(stderr, "uint %s -> quint %s\n", s, quint); */
printf("%s ", quint);
}
static void main_convertQuint(char const *s) {
uint32_t uint0 = quint2uint(s);
/* fprintf(stderr, "quint %s -> uint %u = x%x\n", s, uint0, uint0); */
printf("x%x ", uint0);
}
/* Convert command-line strings.
*/
int main(int argc, char const **argv) {
--argc; ++argv;
for(; *argv; --argc, ++argv) {
char const *s = *argv;
if (!s[0]) continue; /* this would be very strange */
if (s[0] == '0') {
fprintf(stderr,
"Do not lead input strings with a zero.\n"
"For decimal, trim leading zeros.\n"
"For hex, lead with an 'x'.\n"
"We do not process octal.\n");
exit(1);
} else if (s[0] == 'x' || s[0] == 'X') {
/* a hexadecimal number */
main_convertNumber(16, s+1);
} else if (isdigit(s[0])) {
/* a decimal number */
main_convertNumber(10, s);
} else {
/* a quint word */
main_convertQuint(s);
}
}
printf("\n");
return 0;
}