no-copy-libafs-builds-20021015
[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 RCSID("$Header$");
14
15 #include <stdio.h>
16 #if !defined(AFS_NAMEI_ENV)
17 main() { printf("fb64 not required for this operating system.\n"); exit(1); }
18 #else
19
20 #include "afsutil.h"
21
22 char *prog = "fb64";
23
24 void Usage(void) {
25     printf("Usage: %s -s n [n ...] (converts int to base 64 string)\n", prog);
26     printf("Usage: %s -i s [s ...] (converts base 64 string to int)\n", prog);
27     printf("Usage: %s -c n [n ...] (converts to base 64 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     int64_t o;
62
63     if (ac == 3) {
64         printf("%Lu\n", flipbase64_to_int64(av[2]));
65     }
66     else {
67         for (i=2; i<ac; i++)
68             printf("%s: %Lu\n", av[i], flipbase64_to_int64(av[i]));
69     }
70 }
71
72 void itob(int ac, char **av)
73 {
74     int i;
75     lb64_string_t str;
76     int64_t in;
77
78     if (ac == 3) {
79         sscanf(av[2], "%Ld", &in);
80         printf("%s\n", int64_to_flipbase64(str, in));
81     }
82     else {
83         for (i=2; i<ac; i++) {
84             sscanf(av[i], "%Ld", &in);
85             printf("%Ld: %s\n", in, int64_to_flipbase64(str, in));
86         }
87     }
88 }
89
90 void check(int ac, char **av)
91 {
92     int i;
93     int64_t in, out;
94     lb64_string_t str;
95
96     printf("%10s %10s %10s\n", "input", "base64", "output");
97     for (i=2; i<ac; i++) {
98         sscanf(av[i], "%Ld", &in);
99         (void) int64_to_flipbase64(str, in);
100         out = flipbase64_to_int64(str);
101         printf("%10Ld %10s %10Ld\n", in, str, out);
102     }
103 }
104
105 #define PROGRESS 1000000
106 void verifyRange(int ac, char **av)
107 {
108     u_int64_t inc, low, high;
109     u_int64_t  n;
110     int64_t in, out;
111     lb64_string_t str;
112
113     if (ac != 5) Usage();
114
115     sscanf(av[2], "%lu", &low);
116     sscanf(av[3], "%lu", &high);
117     sscanf(av[4], "%lu", &inc);
118
119     n = 0;
120     for (in=low; in <= high; in += inc) {
121         n ++;
122         if (n % PROGRESS == 0) 
123             printf(" L%d", n);
124         (void) int64_to_flipbase64(str, in);
125         out = flipbase64_to_int64(str);
126         if (in != out) {
127             printf("\n\nERROR: in=%Lu, str='%s', out=%Lu\n", in, str, out);
128             exit(1);
129         }
130     }
131     printf("\nCOMPLETE - no errors found in range %Lu,%Lu,%Lu\n",
132            low, high, inc);
133 }
134
135
136 #endif /* AFS_NAMEI_ENV */