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