1f08c3bdfSopenharmony_ci/*
2f08c3bdfSopenharmony_ci * Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
3f08c3bdfSopenharmony_ci *
4f08c3bdfSopenharmony_ci * This program is free software; you can redistribute it and/or modify it
5f08c3bdfSopenharmony_ci * under the terms of version 2 of the GNU General Public License as
6f08c3bdfSopenharmony_ci * published by the Free Software Foundation.
7f08c3bdfSopenharmony_ci *
8f08c3bdfSopenharmony_ci * This program is distributed in the hope that it would be useful, but
9f08c3bdfSopenharmony_ci * WITHOUT ANY WARRANTY; without even the implied warranty of
10f08c3bdfSopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11f08c3bdfSopenharmony_ci *
12f08c3bdfSopenharmony_ci * Further, this software is distributed without any warranty that it is
13f08c3bdfSopenharmony_ci * free of the rightful claim of any third person regarding infringement
14f08c3bdfSopenharmony_ci * or the like.  Any license provided herein, whether implied or
15f08c3bdfSopenharmony_ci * otherwise, applies only to this software file.  Patent licenses, if
16f08c3bdfSopenharmony_ci * any, provided herein do not apply to combinations of this program with
17f08c3bdfSopenharmony_ci * other software, or any other product whatsoever.
18f08c3bdfSopenharmony_ci *
19f08c3bdfSopenharmony_ci * You should have received a copy of the GNU General Public License along
20f08c3bdfSopenharmony_ci * with this program; if not, write the Free Software Foundation, Inc.,
21f08c3bdfSopenharmony_ci * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22f08c3bdfSopenharmony_ci *
23f08c3bdfSopenharmony_ci * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24f08c3bdfSopenharmony_ci * Mountain View, CA  94043, or:
25f08c3bdfSopenharmony_ci *
26f08c3bdfSopenharmony_ci * http://www.sgi.com
27f08c3bdfSopenharmony_ci *
28f08c3bdfSopenharmony_ci * For further information regarding this notice, see:
29f08c3bdfSopenharmony_ci *
30f08c3bdfSopenharmony_ci * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
31f08c3bdfSopenharmony_ci */
32f08c3bdfSopenharmony_ci#include <stdio.h>
33f08c3bdfSopenharmony_ci#include <sys/param.h>
34f08c3bdfSopenharmony_ci#include <string.h>		/* memset */
35f08c3bdfSopenharmony_ci#include <stdlib.h>		/* rand */
36f08c3bdfSopenharmony_ci#include "databin.h"
37f08c3bdfSopenharmony_ci
38f08c3bdfSopenharmony_ci#if UNIT_TEST
39f08c3bdfSopenharmony_ci#include <stdlib.h>
40f08c3bdfSopenharmony_ci#endif
41f08c3bdfSopenharmony_ci
42f08c3bdfSopenharmony_cistatic char Errmsg[80];
43f08c3bdfSopenharmony_ci
44f08c3bdfSopenharmony_civoid databingen(int mode, char *buffer, int bsize, int offset)
45f08c3bdfSopenharmony_ci{
46f08c3bdfSopenharmony_ci	int ind;
47f08c3bdfSopenharmony_ci
48f08c3bdfSopenharmony_ci	switch (mode) {
49f08c3bdfSopenharmony_ci	default:
50f08c3bdfSopenharmony_ci	case 'a':		/* alternating bit pattern */
51f08c3bdfSopenharmony_ci		memset(buffer, 0x55, bsize);
52f08c3bdfSopenharmony_ci		break;
53f08c3bdfSopenharmony_ci
54f08c3bdfSopenharmony_ci	case 'c':		/* checkerboard pattern */
55f08c3bdfSopenharmony_ci		memset(buffer, 0xf0, bsize);
56f08c3bdfSopenharmony_ci		break;
57f08c3bdfSopenharmony_ci
58f08c3bdfSopenharmony_ci	case 'C':		/* */
59f08c3bdfSopenharmony_ci		for (ind = 0; ind < bsize; ind++)
60f08c3bdfSopenharmony_ci			buffer[ind] = ((offset + ind) % 8 & 0177);
61f08c3bdfSopenharmony_ci
62f08c3bdfSopenharmony_ci		break;
63f08c3bdfSopenharmony_ci
64f08c3bdfSopenharmony_ci	case 'o':
65f08c3bdfSopenharmony_ci		memset(buffer, 0xff, bsize);
66f08c3bdfSopenharmony_ci		break;
67f08c3bdfSopenharmony_ci
68f08c3bdfSopenharmony_ci	case 'z':
69f08c3bdfSopenharmony_ci		memset(buffer, 0x0, bsize);
70f08c3bdfSopenharmony_ci		break;
71f08c3bdfSopenharmony_ci
72f08c3bdfSopenharmony_ci	case 'r':		/* random */
73f08c3bdfSopenharmony_ci		for (ind = 0; ind < bsize; ind++)
74f08c3bdfSopenharmony_ci			buffer[ind] = (rand() & 0177) | 0100;
75f08c3bdfSopenharmony_ci	}
76f08c3bdfSopenharmony_ci}
77f08c3bdfSopenharmony_ci
78f08c3bdfSopenharmony_ci/*
79f08c3bdfSopenharmony_ci * return values:
80f08c3bdfSopenharmony_ci *      >= 0 : error at byte offset into the file, offset+buffer[0-(bsize-1)]
81f08c3bdfSopenharmony_ci *      < 0  : no error
82f08c3bdfSopenharmony_ci */
83f08c3bdfSopenharmony_ciint databinchk(int mode, char *buffer, int bsize, int offset, char **errmsg)
84f08c3bdfSopenharmony_ci{
85f08c3bdfSopenharmony_ci	int cnt;
86f08c3bdfSopenharmony_ci	unsigned char *chr;
87f08c3bdfSopenharmony_ci	long expbits;
88f08c3bdfSopenharmony_ci	long actbits;
89f08c3bdfSopenharmony_ci
90f08c3bdfSopenharmony_ci	chr = (unsigned char *)buffer;
91f08c3bdfSopenharmony_ci
92f08c3bdfSopenharmony_ci	if (errmsg != NULL)
93f08c3bdfSopenharmony_ci		*errmsg = Errmsg;
94f08c3bdfSopenharmony_ci
95f08c3bdfSopenharmony_ci	switch (mode) {
96f08c3bdfSopenharmony_ci	default:
97f08c3bdfSopenharmony_ci	case 'a':		/* alternating bit pattern */
98f08c3bdfSopenharmony_ci		expbits = 0x55;
99f08c3bdfSopenharmony_ci		break;
100f08c3bdfSopenharmony_ci
101f08c3bdfSopenharmony_ci	case 'c':		/* checkerboard pattern */
102f08c3bdfSopenharmony_ci		expbits = 0xf0;
103f08c3bdfSopenharmony_ci		break;
104f08c3bdfSopenharmony_ci
105f08c3bdfSopenharmony_ci	case 'C':		/* counting pattern */
106f08c3bdfSopenharmony_ci		for (cnt = 0; cnt < bsize; cnt++) {
107f08c3bdfSopenharmony_ci			expbits = ((offset + cnt) % 8 & 0177);
108f08c3bdfSopenharmony_ci
109f08c3bdfSopenharmony_ci			if (buffer[cnt] != expbits) {
110f08c3bdfSopenharmony_ci				sprintf(Errmsg,
111f08c3bdfSopenharmony_ci					"data mismatch at offset %d, exp:%#lo, act:%#o",
112f08c3bdfSopenharmony_ci					offset + cnt, expbits, buffer[cnt]);
113f08c3bdfSopenharmony_ci				return offset + cnt;
114f08c3bdfSopenharmony_ci			}
115f08c3bdfSopenharmony_ci		}
116f08c3bdfSopenharmony_ci		sprintf(Errmsg, "all %d bytes match desired pattern", bsize);
117f08c3bdfSopenharmony_ci		return -1;
118f08c3bdfSopenharmony_ci
119f08c3bdfSopenharmony_ci	case 'o':
120f08c3bdfSopenharmony_ci		expbits = 0xff;
121f08c3bdfSopenharmony_ci		break;
122f08c3bdfSopenharmony_ci
123f08c3bdfSopenharmony_ci	case 'z':
124f08c3bdfSopenharmony_ci		expbits = 0;
125f08c3bdfSopenharmony_ci		break;
126f08c3bdfSopenharmony_ci
127f08c3bdfSopenharmony_ci	case 'r':
128f08c3bdfSopenharmony_ci		return -1;	/* no check can be done for random */
129f08c3bdfSopenharmony_ci	}
130f08c3bdfSopenharmony_ci
131f08c3bdfSopenharmony_ci	for (cnt = 0; cnt < bsize; chr++, cnt++) {
132f08c3bdfSopenharmony_ci		actbits = (long)*chr;
133f08c3bdfSopenharmony_ci
134f08c3bdfSopenharmony_ci		if (actbits != expbits) {
135f08c3bdfSopenharmony_ci			sprintf(Errmsg,
136f08c3bdfSopenharmony_ci				"data mismatch at offset %d, exp:%#lo, act:%#lo",
137f08c3bdfSopenharmony_ci				offset + cnt, expbits, actbits);
138f08c3bdfSopenharmony_ci			return offset + cnt;
139f08c3bdfSopenharmony_ci		}
140f08c3bdfSopenharmony_ci	}
141f08c3bdfSopenharmony_ci
142f08c3bdfSopenharmony_ci	sprintf(Errmsg, "all %d bytes match desired pattern", bsize);
143f08c3bdfSopenharmony_ci	return -1;
144f08c3bdfSopenharmony_ci}
145f08c3bdfSopenharmony_ci
146f08c3bdfSopenharmony_ci#if UNIT_TEST
147f08c3bdfSopenharmony_ci
148f08c3bdfSopenharmony_ciint main(int ac, char **ag)
149f08c3bdfSopenharmony_ci{
150f08c3bdfSopenharmony_ci	int size = 1023;
151f08c3bdfSopenharmony_ci	int offset;
152f08c3bdfSopenharmony_ci	int number;
153f08c3bdfSopenharmony_ci	unsigned char *buffer;
154f08c3bdfSopenharmony_ci	int ret;
155f08c3bdfSopenharmony_ci	char *errmsg;
156f08c3bdfSopenharmony_ci
157f08c3bdfSopenharmony_ci	buffer = malloc(size);
158f08c3bdfSopenharmony_ci	if (buffer == NULL) {
159f08c3bdfSopenharmony_ci		perror("malloc");
160f08c3bdfSopenharmony_ci		exit(2);
161f08c3bdfSopenharmony_ci	}
162f08c3bdfSopenharmony_ci
163f08c3bdfSopenharmony_ci	printf("***** for a ****************************\n");
164f08c3bdfSopenharmony_ci	databingen('a', buffer, size, 0);
165f08c3bdfSopenharmony_ci	printf("databingen('a', buffer, %d, 0)\n", size);
166f08c3bdfSopenharmony_ci
167f08c3bdfSopenharmony_ci	ret = databinchk('a', buffer, size, 0, &errmsg);
168f08c3bdfSopenharmony_ci	printf("databinchk('a', buffer, %d, 0, &errmsg) returned %d: %s\n",
169f08c3bdfSopenharmony_ci	       size, ret, errmsg);
170f08c3bdfSopenharmony_ci	if (ret == -1)
171f08c3bdfSopenharmony_ci		printf("\tPASS return value of -1 as expected\n");
172f08c3bdfSopenharmony_ci	else
173f08c3bdfSopenharmony_ci		printf("\tFAIL return value %d, expected -1\n", ret);
174f08c3bdfSopenharmony_ci
175f08c3bdfSopenharmony_ci	offset = 232400;
176f08c3bdfSopenharmony_ci	ret = databinchk('a', &buffer[1], size - 1, offset, &errmsg);
177f08c3bdfSopenharmony_ci	printf("databinchk('a', &buffer[1], %d, %d, &errmsg) returned %d: %s\n",
178f08c3bdfSopenharmony_ci	       size, offset, ret, errmsg);
179f08c3bdfSopenharmony_ci	if (ret == -1)
180f08c3bdfSopenharmony_ci		printf("\tPASS return value of -1 as expected\n");
181f08c3bdfSopenharmony_ci	else
182f08c3bdfSopenharmony_ci		printf("\tFAIL return value %d, expected -1\n", ret);
183f08c3bdfSopenharmony_ci
184f08c3bdfSopenharmony_ci	buffer[15] = 0x0;
185f08c3bdfSopenharmony_ci	printf("changing char 15 (offset (%d+15) = %d) to 0x0\n", offset,
186f08c3bdfSopenharmony_ci	       offset + 15);
187f08c3bdfSopenharmony_ci	number = offset + 15;
188f08c3bdfSopenharmony_ci
189f08c3bdfSopenharmony_ci	ret = databinchk('a', &buffer[1], size - 1, offset + 1, &errmsg);
190f08c3bdfSopenharmony_ci	printf("databinchk('a', &buffer[1], %d, %d, &errmsg) returned %d: %s\n",
191f08c3bdfSopenharmony_ci	       size - 1, offset + 1, ret, errmsg);
192f08c3bdfSopenharmony_ci	if (ret == number)
193f08c3bdfSopenharmony_ci		printf("\tPASS return value of %d as expected\n", number);
194f08c3bdfSopenharmony_ci	else
195f08c3bdfSopenharmony_ci		printf("\tFAIL return value %d, expected %d\n", ret, number);
196f08c3bdfSopenharmony_ci
197f08c3bdfSopenharmony_ci	printf("***** for c ****************************\n");
198f08c3bdfSopenharmony_ci	databingen('c', buffer, size, 0);
199f08c3bdfSopenharmony_ci	printf("databingen('c', buffer, %d, 0)\n", size);
200f08c3bdfSopenharmony_ci
201f08c3bdfSopenharmony_ci	ret = databinchk('c', buffer, size, 0, &errmsg);
202f08c3bdfSopenharmony_ci	printf("databinchk('c', buffer, %d, 0, &errmsg) returned %d: %s\n",
203f08c3bdfSopenharmony_ci	       size, ret, errmsg);
204f08c3bdfSopenharmony_ci	if (ret == -1)
205f08c3bdfSopenharmony_ci		printf("\tPASS return value of -1 as expected\n");
206f08c3bdfSopenharmony_ci	else
207f08c3bdfSopenharmony_ci		printf("\tFAIL return value %d, expected -1\n", ret);
208f08c3bdfSopenharmony_ci
209f08c3bdfSopenharmony_ci	offset = 232400;
210f08c3bdfSopenharmony_ci	ret = databinchk('c', &buffer[1], size - 1, offset, &errmsg);
211f08c3bdfSopenharmony_ci	printf("databinchk('c', &buffer[1], %d, %d, &errmsg) returned %d: %s\n",
212f08c3bdfSopenharmony_ci	       size, offset, ret, errmsg);
213f08c3bdfSopenharmony_ci	if (ret == -1)
214f08c3bdfSopenharmony_ci		printf("\tPASS return value of -1 as expected\n");
215f08c3bdfSopenharmony_ci	else
216f08c3bdfSopenharmony_ci		printf("\tFAIL return value %d, expected -1\n", ret);
217f08c3bdfSopenharmony_ci
218f08c3bdfSopenharmony_ci	buffer[15] = 0x0;
219f08c3bdfSopenharmony_ci	printf("changing char 15 (offset (%d+15) = %d) to 0x0\n", offset,
220f08c3bdfSopenharmony_ci	       offset + 15);
221f08c3bdfSopenharmony_ci	number = offset + 15;
222f08c3bdfSopenharmony_ci
223f08c3bdfSopenharmony_ci	ret = databinchk('c', &buffer[1], size - 1, offset + 1, &errmsg);
224f08c3bdfSopenharmony_ci	printf("databinchk('c', &buffer[1], %d, %d, &errmsg) returned %d: %s\n",
225f08c3bdfSopenharmony_ci	       size - 1, offset + 1, ret, errmsg);
226f08c3bdfSopenharmony_ci	if (ret == number)
227f08c3bdfSopenharmony_ci		printf("\tPASS return value of %d as expected\n", number);
228f08c3bdfSopenharmony_ci	else
229f08c3bdfSopenharmony_ci		printf("\tFAIL return value %d, expected %d\n", ret, number);
230f08c3bdfSopenharmony_ci
231f08c3bdfSopenharmony_ci	printf("***** for C ****************************\n");
232f08c3bdfSopenharmony_ci
233f08c3bdfSopenharmony_ci	databingen('C', buffer, size, 0);
234f08c3bdfSopenharmony_ci	printf("databingen('C', buffer, %d, 0)\n", size);
235f08c3bdfSopenharmony_ci
236f08c3bdfSopenharmony_ci	ret = databinchk('C', buffer, size, 0, &errmsg);
237f08c3bdfSopenharmony_ci	printf("databinchk('C', buffer, %d, 0, &errmsg) returned %d: %s\n",
238f08c3bdfSopenharmony_ci	       size, ret, errmsg);
239f08c3bdfSopenharmony_ci	if (ret == -1)
240f08c3bdfSopenharmony_ci		printf("\tPASS return value of -1 as expected\n");
241f08c3bdfSopenharmony_ci	else
242f08c3bdfSopenharmony_ci		printf("\tFAIL return value %d, expected -1\n", ret);
243f08c3bdfSopenharmony_ci
244f08c3bdfSopenharmony_ci	offset = 18;
245f08c3bdfSopenharmony_ci	ret = databinchk('C', &buffer[18], size - 18, 18, &errmsg);
246f08c3bdfSopenharmony_ci	printf
247f08c3bdfSopenharmony_ci	    ("databinchk('C', &buffer[18], %d, 18, &errmsg) returned %d: %s\n",
248f08c3bdfSopenharmony_ci	     size - 18, ret, errmsg);
249f08c3bdfSopenharmony_ci	if (ret == -1)
250f08c3bdfSopenharmony_ci		printf("\tPASS return value of -1 as expected\n");
251f08c3bdfSopenharmony_ci	else
252f08c3bdfSopenharmony_ci		printf("\tFAIL return value %d, expected -1\n", ret);
253f08c3bdfSopenharmony_ci
254f08c3bdfSopenharmony_ci	buffer[20] = 0x0;
255f08c3bdfSopenharmony_ci	buffer[21] = 0x0;
256f08c3bdfSopenharmony_ci	printf("changing char 20 and 21 to 0x0 (offset %d and %d)\n", 20, 21);
257f08c3bdfSopenharmony_ci
258f08c3bdfSopenharmony_ci	ret = databinchk('C', &buffer[18], size - 18, 18, &errmsg);
259f08c3bdfSopenharmony_ci	printf("databinchk('C', &buffer[18], %d, 18, &errmsg) returned %d: %s\n",
260f08c3bdfSopenharmony_ci		size - 18, ret, errmsg);
261f08c3bdfSopenharmony_ci
262f08c3bdfSopenharmony_ci	if (ret == 20 || ret == 21)
263f08c3bdfSopenharmony_ci		printf("\tPASS return value of %d or %d as expected\n", 20, 21);
264f08c3bdfSopenharmony_ci	else
265f08c3bdfSopenharmony_ci		printf("\tFAIL return value %d, expected %d or %d\n", ret,
266f08c3bdfSopenharmony_ci		       20, 21);
267f08c3bdfSopenharmony_ci
268f08c3bdfSopenharmony_ci	exit(0);
269f08c3bdfSopenharmony_ci
270f08c3bdfSopenharmony_ci}
271f08c3bdfSopenharmony_ci
272f08c3bdfSopenharmony_ci#endif
273