File: sex.c

package info (click to toggle)
imgvtopgm 2.0-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 876 kB
  • ctags: 270
  • sloc: ansic: 1,670; sh: 449; makefile: 109
file content (235 lines) | stat: -rw-r--r-- 4,112 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
 * sex.c
 *	Byte mangling functions for ipdb.c.
 *
 * Copyright (C) 1997 Eric A. Howe
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *   Authors:  Eric A. Howe (mu@trends.net)
 */
#include <mine/slop.h>
MU_ID("$Mu: imgvtopgm/sex.c 1.10 1998/12/16 18:25:11 $")

#include <mine/sex.h>

#if !defined(MU_BIGENDIAN) && !defined(MU_LITTLEENDIAN) && !defined(MU_UNKNOWNENDIAN)
#	define	MU_UNKNOWNENDIAN
#endif

/**
 ** Big endian.
 **/
#if defined(MU_BIGENDIAN) || defined(MU_UNKNOWNENDIAN)
	static u1 *
	flip2_big(u2 x, u1 *b)
	{
		b[0] = ((u1 *)&x)[0];
		b[1] = ((u1 *)&x)[1];
		return b;
	}

	static u2
	unflip2_big(u1 *b)
	{
		u2	x;

		((u1 *)&x)[0] = b[0];
		((u1 *)&x)[1] = b[1];
		return x;
	}

	static u1 *
	flip4_big(u4 x, u1 *b)
	{
		b[0] = ((u1 *)&x)[0];
		b[1] = ((u1 *)&x)[1];
		b[2] = ((u1 *)&x)[2];
		b[3] = ((u1 *)&x)[3];
		return b;
	}

	static u4
	unflip4_big(u1 *b)
	{
		u4	x;

		((u1 *)&x)[0] = b[0];
		((u1 *)&x)[1] = b[1];
		((u1 *)&x)[2] = b[2];
		((u1 *)&x)[3] = b[3];
		return x;
	}
#endif

/**
 ** Little endian.
 **/
#if defined(MU_LITTLEENDIAN) || defined(MU_UNKNOWNENDIAN)
	static u1 *
	flip2_little(u2 x, u1 *b)
	{
		b[0] = ((u1 *)&x)[1];
		b[1] = ((u1 *)&x)[0];
		return b;
	}

	static u2
	unflip2_little(u1 *b)
	{
		u2	x;

		((u1 *)&x)[0] = b[1];
		((u1 *)&x)[1] = b[0];
		return x;
	}

	static u1 *
	flip4_little(u4 x, u1 *b)
	{
		b[0] = ((u1 *)&x)[3];
		b[1] = ((u1 *)&x)[2];
		b[2] = ((u1 *)&x)[1];
		b[3] = ((u1 *)&x)[0];
		return b;
	}

	static u4
	unflip4_little(u1 *b)
	{
		u4	x;

		((u1 *)&x)[0] = b[3];
		((u1 *)&x)[1] = b[2];
		((u1 *)&x)[2] = b[1];
		((u1 *)&x)[3] = b[0];
		return x;
	}
#endif

#if defined(MU_UNKNOWNENDIAN)
	static u1 *flip2_unknown(u2, u1 *);
	static u1 *flip4_unknown(u4, u1 *);
	static u2  unflip2_unknown(u1 *);
	static u4  unflip4_unknown(u1 *);

	static u1 *(*flip2)(u2, u1 *) = flip2_unknown;
	static u1 *(*flip4)(u4, u1 *) = flip4_unknown;
	static u2  (*unflip2)(u1 *)   = unflip2_unknown;
	static u4  (*unflip4)(u1 *)   = unflip4_unknown;

	static void
	fix(void)
	{
		union {
			u1 c[4];
			u4 i;
		} u;
		u.i = 0x01020304;
		if(u.c[0] == 4
		&& u.c[1] == 3
		&& u.c[2] == 2
		&& u.c[3] == 1) {
			flip2   = flip2_little;
			unflip2 = unflip2_little;
			flip4   = flip4_little;
			unflip4 = unflip4_little;
		}
		else {
			flip2   = flip2_big;
			unflip2 = unflip2_big;
			flip4   = flip4_big;
			unflip4 = unflip4_big;
		}
	}

	static u1 *
	flip2_unknown(u2 x, u1 *b)
	{
		fix();
		return flip2(x, b);
	}

	static u2
	unflip2_unknown(u1 *b)
	{
		fix();
		return unflip2(b);
	}

	static u1 *
	flip4_unknown(u4 x, u1 *b)
	{
		fix();
		return flip4(x, b);
	}

	static u4
	unflip4_unknown(u1 *b)
	{
		fix();
		return unflip4(b);
	}
#endif

#if defined(MU_BIGENDIAN)
#	define	flip2	flip2_big
#	define	flip4	flip4_big
#	define	unflip2	unflip2_big
#	define	unflip4	unflip4_big
#elif defined(MU_LITTLEENDIAN)
#	define	flip2	flip2_little
#	define	flip4	flip4_little
#	define	unflip2	unflip2_little
#	define	unflip4	unflip4_little
#endif

size_t
fread_u2(u2 *x, FILE *fp)
{
	u1	b[2];
	size_t	len;

	if((len = fread(b, 1, 2, fp)) != 2)
		return len;
	*x = unflip2(b);
	return 2;
}

size_t
fread_u4(u4 *x, FILE *fp)
{
	u1	b[4];
	size_t	len;

	if((len = fread(b, 1, 4, fp)) != 4)
		return len;
	*x = unflip4(b);
	return 4;
}

size_t
fwrite_u2(u2 x, FILE *fp)
{
	u1	b[2];
	return fwrite(flip2(x, b), 1, 2, fp);
}

size_t
fwrite_u4(u4 x, FILE *fp)
{
	u1	b[4];
	return fwrite(flip4(x, b), 1, 4, fp);
}