darwin-x86-and-leopard-20060309
[openafs.git] / src / des / misc.c
1 /*
2  * Copyright 1988 by the Massachusetts Institute of Technology.
3  *
4  * For copying and distribution information,
5  * please seethe file <mit-cpyright.h>.
6  *
7  * This file contains most of the routines needed by the various
8  * make_foo programs, to account for bit- and byte-ordering on
9  * different machine types.  It also contains other routines useful in
10  * generating the intermediate source files.
11  */
12
13 #include <afsconfig.h>
14 #include <afs/param.h>
15
16 RCSID
17     ("$Header$");
18
19 #include <mit-cpyright.h>
20 #ifndef KERNEL
21 #include <stdio.h>
22 #endif
23 #include <des.h>
24 #include "des_internal.h"
25 #include "des_prototypes.h"
26
27 int des_debug;
28
29 /*
30  * The DES algorithm is defined in terms of MSBFIRST, so sometimes,
31  * e.g.  VAXes, we need to fix it up.  ANSI order means the DES
32  * MSBFIRST order.
33  */
34
35 #if 0                           /* These don't seem to get used anywhere.... */
36 void
37 swap_bits(char *array)
38 {
39 #ifdef MSBFIRST
40     /* just return */
41     return;
42 #else /* LSBFIRST */
43     register int old, new, i, j;
44
45     /* for an eight byte block-- */
46     /* flips the bit order within each byte from 0 lsb to 0 msb */
47     for (i = 0; i <= 7; i++) {
48         old = *array;
49         new = 0;
50         for (j = 0; j <= 7; j++) {
51             new |= old & 01;    /* copy a bit */
52             if (j < 7) {
53                 /* rotate in opposite directions */
54                 old = old >> 1;
55                 new = new << 1;
56             }
57         }
58         *array++ = new;
59     }
60 #endif /* MSBFIRST */
61 }
62
63 afs_uint32
64 long_swap_bits(afs_uint32 x)
65 {
66 #ifdef MSBFIRST
67     return x;
68 #else
69     char *array = (char *)&x;
70     register int old, new, i, j;
71
72     /* flips the bit order within each byte from 0 lsb to 0 msb */
73     for (i = 0; i <= (sizeof(afs_int32) - 1); i++) {
74         old = *array;
75         new = 0;
76         for (j = 0; j <= 7; j++) {
77             if (old & 01)
78                 new = new | 01;
79             if (j < 7) {
80                 old = old >> 1;
81                 new = new << 1;
82             }
83         }
84         *array++ = new;
85     }
86     return x;
87 #endif /* LSBFIRST */
88 }
89 #endif /* 0 */
90
91 afs_uint32
92 swap_six_bits_to_ansi(afs_uint32 old)
93 {
94     register afs_uint32 new, j;
95
96     /* flips the bit order within each byte from 0 lsb to 0 msb */
97     new = 0;
98     for (j = 0; j <= 5; j++) {
99         new |= old & 01;        /* copy a bit */
100         if (j < 5) {
101             /* rotate in opposite directions */
102             old = old >> 1;
103             new = new << 1;
104         }
105     }
106     return new;
107 }
108
109 afs_uint32
110 swap_four_bits_to_ansi(afs_uint32 old)
111 {
112     register afs_uint32 new, j;
113
114     /* flips the bit order within each byte from 0 lsb to 0 msb */
115     new = 0;
116     for (j = 0; j <= 3; j++) {
117         new |= (old & 01);      /* copy a bit */
118         if (j < 3) {
119             old = old >> 1;
120             new = new << 1;
121         }
122     }
123     return new;
124 }
125
126 afs_uint32
127 swap_bit_pos_1(afs_uint32 x)
128 {
129     /*
130      * This corrects for the bit ordering of the algorithm, e.g.
131      * bit 0 ==> msb, bit 7 lsb.
132      *
133      * given the number of a bit position, >=1, flips the bit order
134      * each byte. e.g. bit 3 --> bit 6, bit 13 --> bit 12
135      */
136     register int y, z;
137
138     /* always do it, only used by des_make_key_perm.c so far */
139     y = (x - 1) / 8;
140     z = (x - 1) % 8;
141
142     x = (8 - z) + (y * 8);
143
144     return x;
145 }
146
147 afs_uint32
148 swap_bit_pos_0(afs_uint32 x)
149 {
150     /*  zero based version */
151
152     /*
153      * This corrects for the bit ordering of the algorithm, e.g.
154      * bit 0 ==> msb, bit 7 lsb.
155      */
156
157 #ifdef MSBFIRST
158     return x;
159 #else /* LSBFIRST */
160     register int y, z;
161
162     /*
163      * given the number of a bit position, >=0, flips the bit order
164      * each byte. e.g. bit 3 --> bit 6, bit 13 --> bit 12
165      */
166     y = x / 8;
167     z = x % 8;
168
169     x = (7 - z) + (y * 8);
170
171     return x;
172 #endif /* LSBFIRST */
173 }
174
175 afs_uint32
176 swap_bit_pos_0_to_ansi(afs_uint32 x)
177 {
178     /* zero based version */
179
180     /*
181      * This corrects for the bit ordering of the algorithm, e.g.
182      * bit 0 ==> msb, bit 7 lsb.
183      */
184
185     register int y, z;
186     /*
187      * given the number of a bit position, >=0, flips the bit order each
188      * byte. e.g. bit 3 --> bit 6, bit 13 --> bit 12
189      */
190     y = x / 8;
191     z = x % 8;
192
193     x = (7 - z) + (y * 8);
194
195     return x;
196 }
197
198 afs_uint32
199 rev_swap_bit_pos_0(afs_uint32 x)
200 {
201     /* zero based version */
202
203     /*
204      * This corrects for the bit ordering of the algorithm, e.g.
205      *  bit 0 ==> msb, bit 7 lsb.
206      *
207      * Role of LSB and MSB flipped from the swap_bit_pos_0()
208      */
209
210 #ifdef LSBFIRST
211     return x;
212 #else /* MSBFIRST */
213
214     register int y, z;
215
216     /*
217      * given the number of a bit position, >=0, flips the bit order each
218      * byte. e.g. bit 3 --> bit 6, bit 13 --> bit 12
219      */
220     y = x / 8;
221     z = x % 8;
222
223     x = (7 - z) + (y * 8);
224
225     return x;
226 #endif /* MSBFIRST */
227 }
228
229 afs_uint32
230 swap_byte_bits(afs_uint32 x)
231 {
232 #ifdef MSBFIRST
233     return x;
234 #else /* LSBFIRST */
235
236     char *array = (char *)&x;
237     register afs_uint32 old, new, j;
238
239     /* flips the bit order within each byte from 0 lsb to 0 msb */
240     old = *array;
241     new = 0;
242     for (j = 0; j <= 7; j++) {
243         new |= (old & 01);      /* copy a bit */
244         if (j < 7) {
245             old = old >> 1;
246             new = new << 1;
247         }
248     }
249     return new;
250 #endif /* LSBFIRST */
251 }
252
253 int
254 swap_long_bytes_bit_number(afs_uint32 x)
255 {
256     /*
257      * given a bit number (0-31) from a vax, swap the byte part of the
258      * bit number to change the byte ordering to mSBFIRST type
259      */
260 #ifdef LSBFIRST
261     return x;
262 #else /* MSBFIRST */
263     afs_uint32 y, z;
264
265     y = x / 8;                  /* initial byte component */
266     z = x % 8;                  /* bit within byte */
267
268     x = (3 - y) * 8 + z;
269     return x;
270 #endif /* MSBFIRST */
271 }
272
273 #if !defined(KERNEL) && defined(AFS_DARWIN80_ENV)
274 char *_darwin_whichstr[] = {
275     "#if defined(__ppc__)\n",
276     "#elif defined(__i386__)\n",
277     "#else\n#error architecture unsupported\n#endif\n"
278 };
279 int _darwin_which = 1;
280
281 int
282 _darwin_swap_long_bytes_bit_number(afs_uint32 x)
283 {
284     /*
285      * given a bit number (0-31) from a vax, swap the byte part of the
286      * bit number to change the byte ordering to mSBFIRST type
287      */
288
289     afs_uint32 y, z;
290
291     if (!_darwin_which)
292         return x;
293
294     y = x / 8;                  /* initial byte component */
295     z = x % 8;                  /* bit within byte */
296
297     x = (3 - y) * 8 + z;
298     return x;
299 }
300 #endif /* !KERNEL && AFS_DARWIN80_ENV */
301
302 void
303 test_set(FILE * stream, const char *src, int testbit, const char *dest,
304          int setbit)
305 {
306 #ifdef DES_SHIFT_SHIFT
307     if (testbit == setbit)
308         fprintf(stream, "    %s |=  %s & (1<<%2d);\n", dest, src, testbit);
309     else
310         fprintf(stream, "    %s |= (%s & (1<<%2d)) %s %2d;\n", dest, src,
311                 testbit, (testbit < setbit) ? "<<" : ">>",
312                 abs(testbit - setbit));
313 #else
314     fprintf(stream, "    if (%s & (1<<%2d))  %s |= 1<<%2d;\n", src, testbit,
315             dest, setbit);
316 #endif
317 }