1 /*
2  *     *********************************************************************
3  *     * Copyright (C) 1988, 1990 Stanford University.                     *
4  *     * Permission to use, copy, modify, and distribute this              *
5  *     * software and its documentation for any purpose and without        *
6  *     * fee is hereby granted, provided that the above copyright          *
7  *     * notice appear in all copies.  Stanford University                 *
8  *     * makes no representations about the suitability of this            *
9  *     * software for any purpose.  It is provided "as is" without         *
10  *     * express or implied warranty.  Export of this software outside     *
11  *     * of the United States of America may require an export license.    *
12  *     *********************************************************************
13  */
14 
15 /*
16  * Macros to read/write machine-independent binary files.
17  */
18 
19 
20 /*
21  * Extract NBYTES from byte stream BUFF and place result in NUM.
22  * Restriction is 1 <= NBYTES <= 4.  (Force Unrolling of loops).
23  */
24 #define UnpackBytes( BUFF, NUM, NBYTES )			\
25   {								\
26     register unsigned char  *B_S = (unsigned char *) (BUFF);	\
27     register unsigned long  RES = 0;				\
28 								\
29     if( NBYTES == 1 )						\
30 	RES = *B_S;						\
31     else if( NBYTES == 2 )					\
32       {								\
33 	RES = B_S[0];						\
34 	RES += ( ((unsigned int) B_S[1]) << 8 ) & 0x0ff00;	\
35       }								\
36     else if( NBYTES == 3 )					\
37       {								\
38 	RES = B_S[0];						\
39 	RES += ( ((unsigned int) B_S[1]) << 8 ) & 0x0ff00;	\
40 	RES += ( ((unsigned int) B_S[2]) << 16 ) & 0x0ff0000;	\
41       }								\
42     else if( NBYTES == 4 )					\
43       {								\
44 	RES = B_S[0];						\
45 	RES += ( ((unsigned int) B_S[1]) << 8 ) & 0x0ff00;	\
46 	RES += ( ((unsigned int) B_S[2]) << 16 ) & 0x0ff0000;	\
47 	RES += ( ((unsigned int) B_S[3]) << 24 ) & 0xff000000;	\
48       }								\
49     NUM = RES;							\
50   }								\
51 
52 
53 /*
54  * Pack NBYTES from NUM into byte stream pointed to by BUFF.
55  * Restriction is 1 <= NBYTES <= 4.  (Force Unrolling of loops).
56  */
57 #define	PackBytes( BUFF, NUM, NBYTES )				\
58   {								\
59     register unsigned long  B_T = (NUM);			\
60     register unsigned char  *B_S = (unsigned char *) (BUFF);	\
61 								\
62     if( NBYTES > 3 )						\
63       {								\
64 	*B_S++ = B_T & 0x0ff;					\
65 	B_T >>= 8;						\
66       }								\
67     if( NBYTES > 2 )						\
68       {								\
69 	*B_S++ = B_T & 0x0ff;					\
70 	B_T >>= 8;						\
71       }								\
72     if( NBYTES > 1 )						\
73       {								\
74 	*B_S++ = B_T & 0x0ff;					\
75 	B_T >>= 8;						\
76       }								\
77     *B_S = B_T & 0x0ff;						\
78   }								\
79 
80