include-afsconfig-before-param-h-20010712
[openafs.git] / src / util / test / b32.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #include <afsconfig.h>
11 #include <afs/param.h>
12
13 RCSID("$Header$");
14
15 #include <stdio.h>
16 #if !defined(AFS_NT40_ENV)
17 main() { printf("b32 not required for this operating system.\n"); exit(1); }
18 #else
19
20 #include "../afsutil.h"
21
22 char *prog = "b32";
23
24 void Usage(void) {
25     printf("Usage: %s -s n [n ...] (converts int to base 32 string)\n", prog);
26     printf("Usage: %s -i s [s ...] (converts base 32 string to int)\n", prog);
27     printf("Usage: %s -c n [n ...] (converts to base 32 and back)\n", prog);
28     printf("Usage: %s -r low high inc (verify converter using range and inc)\n",
29     prog);
30     exit(1);
31 }
32
33 void btoi(int ac, char **av);
34 void itob(int ac, char **av);
35 void check(int ac, char **av);
36 void verifyRange(int ac, char **av);
37
38
39 main(int ac, char **av)
40 {
41     if (ac < 3)
42         Usage();
43
44     if (!strcmp(av[1], "-s"))
45         itob(ac, av);
46     else if (!strcmp(av[1], "-i"))
47         btoi(ac, av);
48     else if (!strcmp(av[1], "-c"))
49         check(ac, av);
50     else if (!strcmp(av[1], "-r"))
51         verifyRange(ac, av);
52     else
53         Usage();
54
55     exit(0);
56 }
57
58 void btoi(int ac, char **av)
59 {
60     int i;
61
62     if (ac == 3)
63         printf("%d\n", base32_to_int(av[2]));
64     else {
65         for (i=2; i<ac; i++)
66             printf("%s: %d\n", av[i], base32_to_int(av[i]));
67     }
68 }
69
70 void itob(int ac, char **av)
71 {
72     int i;
73     b32_string_t str;
74
75     if (ac == 3)
76         printf("%s\n", int_to_base32(str, atoi(av[2])));
77     else {
78         for (i=2; i<ac; i++)
79             printf("%d: %s\n", atoi(av[i]), int_to_base32(str, atoi(av[i])));
80     }
81 }
82
83 void check(int ac, char **av)
84 {
85     int i;
86     int in, out;
87     b32_string_t str;
88
89     printf("%10s %10s %10s\n", "input", "base32", "output");
90     for (i=2; i<ac; i++) {
91         in = atoi(av[i]);
92         (void) int_to_base32(str, in);
93         out = base32_to_int(str);
94         printf("%10d %10s %10d\n", in, str, out);
95     }
96 }
97
98 #define PROGRESS 1000000
99 void verifyRange(int ac, char **av)
100 {
101     unsigned int inc, low, high;
102     int n;
103     unsigned int in, out;
104     b32_string_t str;
105
106     if (ac != 5) Usage();
107
108     low = (unsigned int)atoi(av[2]);
109     high = (unsigned int)atoi(av[3]);
110     inc = (unsigned int)atoi(av[4]);
111
112     n = 0;
113     for (in=low; in <= high; in += inc) {
114         n ++;
115         if (n % PROGRESS == 0) 
116             printf(" %d", n);
117         (void) int_to_base32(str, in);
118         out = base32_to_int(str);
119         if (in != out) {
120             printf("\n\nERROR: in=%u, str='%s', out=%u\n", in, str, out);
121             exit(1);
122         }
123     }
124     printf("\nCOMPLETE - no errors found in range %u,%u,%u\n",
125            low, high, inc);
126 }
127
128
129 #endif /* !NT */