Import Upstream version 1.8.5
[hcoop/debian/openafs.git] / src / util / test / fb64.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
14 #include <stdio.h>
15 #if !defined(AFS_NAMEI_ENV)
16 main()
17 {
18 printf("fb64 not required for this operating system.\n");
19 exit(1);
20 }
21 #else
22
23 #include "afsutil.h"
24
25 char *prog = "fb64";
26
27 void
28 Usage(void)
29 {
30 printf("Usage: %s -s n [n ...] (converts int to base 64 string)\n", prog);
31 printf("Usage: %s -i s [s ...] (converts base 64 string to int)\n", prog);
32 printf("Usage: %s -c n [n ...] (converts to base 64 and back)\n", prog);
33 printf
34 ("Usage: %s -r low high inc (verify converter using range and inc)\n",
35 prog);
36 exit(1);
37 }
38
39 void btoi(int ac, char **av);
40 void itob(int ac, char **av);
41 void check(int ac, char **av);
42 void verifyRange(int ac, char **av);
43
44
45 main(int ac, char **av)
46 {
47 if (ac < 3)
48 Usage();
49
50 if (!strcmp(av[1], "-s"))
51 itob(ac, av);
52 else if (!strcmp(av[1], "-i"))
53 btoi(ac, av);
54 else if (!strcmp(av[1], "-c"))
55 check(ac, av);
56 else if (!strcmp(av[1], "-r"))
57 verifyRange(ac, av);
58 else
59 Usage();
60
61 exit(0);
62 }
63
64 void
65 btoi(int ac, char **av)
66 {
67 int i;
68 int64_t o;
69
70 if (ac == 3) {
71 printf("%Lu\n", flipbase64_to_int64(av[2]));
72 } else {
73 for (i = 2; i < ac; i++)
74 printf("%s: %Lu\n", av[i], flipbase64_to_int64(av[i]));
75 }
76 }
77
78 void
79 itob(int ac, char **av)
80 {
81 int i;
82 lb64_string_t str;
83 int64_t in;
84
85 if (ac == 3) {
86 sscanf(av[2], "%Ld", &in);
87 printf("%s\n", int64_to_flipbase64(str, in));
88 } else {
89 for (i = 2; i < ac; i++) {
90 sscanf(av[i], "%Ld", &in);
91 printf("%Ld: %s\n", in, int64_to_flipbase64(str, in));
92 }
93 }
94 }
95
96 void
97 check(int ac, char **av)
98 {
99 int i;
100 int64_t in, out;
101 lb64_string_t str;
102
103 printf("%10s %10s %10s\n", "input", "base64", "output");
104 for (i = 2; i < ac; i++) {
105 sscanf(av[i], "%Ld", &in);
106 (void)int64_to_flipbase64(str, in);
107 out = flipbase64_to_int64(str);
108 printf("%10Ld %10s %10Ld\n", in, str, out);
109 }
110 }
111
112 #define PROGRESS 1000000
113 void
114 verifyRange(int ac, char **av)
115 {
116 u_int64_t inc, low, high;
117 u_int64_t n;
118 int64_t in, out;
119 lb64_string_t str;
120
121 if (ac != 5)
122 Usage();
123
124 sscanf(av[2], "%lu", &low);
125 sscanf(av[3], "%lu", &high);
126 sscanf(av[4], "%lu", &inc);
127
128 n = 0;
129 for (in = low; in <= high; in += inc) {
130 n++;
131 if (n % PROGRESS == 0)
132 printf(" L%d", n);
133 (void)int64_to_flipbase64(str, in);
134 out = flipbase64_to_int64(str);
135 if (in != out) {
136 printf("\n\nERROR: in=%Lu, str='%s', out=%Lu\n", in, str, out);
137 exit(1);
138 }
139 }
140 printf("\nCOMPLETE - no errors found in range %Lu,%Lu,%Lu\n", low, high,
141 inc);
142 }
143
144
145 #endif /* AFS_NAMEI_ENV */