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