3 * Type conversion routines, these have been rewritten for portability.
5 * The only requirement is now that the u2_t and u4_t must be big enough.
13 void xxerr
P((char *));
14 void xxerr(x
) char * x
; { write(2, x
, strlen(x
)); }
16 static int no_swap
= 1;
18 static int long_off
[4] = {0,1,2,3};
19 static int int_off
[2] = {0,1};
21 PUBLIC bool_pt
typeconv_init(big_endian
, long_big_endian
)
23 bool_pt long_big_endian
;
26 no_swap
= (!big_endian
&& !long_big_endian
);
28 for(i
=0; i
<4; i
++) long_off
[i
] = i
;
29 for(i
=0; i
<2; i
++) int_off
[i
] = i
;
33 i
= long_off
[0]; long_off
[0] = long_off
[2]; long_off
[2] = i
;
34 i
= long_off
[1]; long_off
[1] = long_off
[3]; long_off
[3] = i
;
38 i
= long_off
[2]; long_off
[2] = long_off
[3]; long_off
[3] = i
;
39 i
= long_off
[0]; long_off
[0] = long_off
[1]; long_off
[1] = i
;
41 i
= int_off
[0]; int_off
[0] = int_off
[1]; int_off
[1] = i
;
46 PUBLIC
void u2c2(buf
, offset
)
53 *((unsigned short*)buf
) = offset
; /* UNALIGNED ACCESS! */
57 buf
[int_off
[0]] = offset
;
58 buf
[int_off
[1]] = (offset
>>8);
61 PUBLIC
void u4c4(buf
, offset
)
69 *((unsigned long*)buf
) = offset
; /* UNALIGNED ACCESS! */
75 buf
[long_off
[i
]] = offset
;
80 PUBLIC
void u4cn(buf
, offset
, count
)
88 buf
[0] = (char) offset
;
91 u2c2(buf
, (u2_pt
) offset
);
94 u4c4(buf
, (u4_t
) offset
);
97 xxerr("WARNING: typeconv.c(u4cn) illegal count\n");
102 PUBLIC
void u2cn(buf
, offset
, count
)
110 buf
[0] = (char) offset
;
113 u2c2(buf
, (u2_pt
) offset
);
116 u4c4(buf
, (u4_t
) offset
);
119 xxerr("WARNING: typeconv.c(u2cn) illegal count\n");
124 PUBLIC u2_pt
c2u2(buf
)
129 if( no_swap
) return *((u2_pt
*)buf
); /* UNALIGNED ACCESS! */
132 res
= ((unsigned char *)buf
) [int_off
[0]]
133 + ((((unsigned char *)buf
) [int_off
[1]]) << 8);
137 PUBLIC u4_t
c4u4(buf
)
143 if( no_swap
) return *((u4_t
*)buf
); /* UNALIGNED ACCESS! */
148 res
= (res
<<8) + ((unsigned char *)buf
) [long_off
[i
]];
153 PUBLIC u4_t
cnu4(buf
, count
)
162 return buf
[0] & 0xFF;
168 xxerr("WARNING: typeconv.c(cnu4) illegal count\n");
173 PUBLIC u2_pt
cnu2(buf
, count
)
182 return buf
[0] & 0xFF;
186 return (u2_pt
) c4u4(buf
);
188 xxerr("WARNING: typeconv.c(cnu2) illegal count\n");