blob: 702485b85a4501f2c2004271259ac5be40f756bf [file] [log] [blame]
whrb973f2b2000-05-05 19:34:50 +00001/*
2 * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
vapier45a8ba02009-07-20 10:59:32 +00003 *
whrb973f2b2000-05-05 19:34:50 +00004 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
vapier45a8ba02009-07-20 10:59:32 +00007 *
whrb973f2b2000-05-05 19:34:50 +00008 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
vapier45a8ba02009-07-20 10:59:32 +000011 *
whrb973f2b2000-05-05 19:34:50 +000012 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
vapier45a8ba02009-07-20 10:59:32 +000018 *
whrb973f2b2000-05-05 19:34:50 +000019 * You should have received a copy of the GNU General Public License along
Wanlong Gaofed96412012-10-24 10:10:29 +080020 * with this program; if not, write the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
vapier45a8ba02009-07-20 10:59:32 +000022 *
whrb973f2b2000-05-05 19:34:50 +000023 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
vapier45a8ba02009-07-20 10:59:32 +000025 *
26 * http://www.sgi.com
27 *
28 * For further information regarding this notice, see:
29 *
whrb973f2b2000-05-05 19:34:50 +000030 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
31 */
32#include <stdio.h>
33#include <sys/param.h>
Wanlong Gao354ebb42012-12-07 10:10:04 +080034#include <string.h> /* memset */
35#include <stdlib.h> /* rand */
alaffincc2e5552000-07-27 17:13:18 +000036#include "databin.h"
whrb973f2b2000-05-05 19:34:50 +000037
38#if UNIT_TEST
Garrett Cooperd3bbf202014-07-02 20:20:40 -070039#include <stdlib.h>
whrb973f2b2000-05-05 19:34:50 +000040#endif
41
42static char Errmsg[80];
43
Wanlong Gao365a3c12012-12-25 15:25:35 +080044void databingen(int mode, char *buffer, int bsize, int offset)
whrb973f2b2000-05-05 19:34:50 +000045{
Wanlong Gao354ebb42012-12-07 10:10:04 +080046 int ind;
whrb973f2b2000-05-05 19:34:50 +000047
Wanlong Gao354ebb42012-12-07 10:10:04 +080048 switch (mode) {
49 default:
50 case 'a': /* alternating bit pattern */
51 memset(buffer, 0x55, bsize);
52 break;
whrb973f2b2000-05-05 19:34:50 +000053
Wanlong Gao354ebb42012-12-07 10:10:04 +080054 case 'c': /* checkerboard pattern */
55 memset(buffer, 0xf0, bsize);
56 break;
whrb973f2b2000-05-05 19:34:50 +000057
Wanlong Gao354ebb42012-12-07 10:10:04 +080058 case 'C': /* */
Wanlong Gao365a3c12012-12-25 15:25:35 +080059 for (ind = 0; ind < bsize; ind++)
Wanlong Gao354ebb42012-12-07 10:10:04 +080060 buffer[ind] = ((offset + ind) % 8 & 0177);
Wanlong Gao365a3c12012-12-25 15:25:35 +080061
whrb973f2b2000-05-05 19:34:50 +000062 break;
63
64 case 'o':
Wanlong Gao354ebb42012-12-07 10:10:04 +080065 memset(buffer, 0xff, bsize);
whrb973f2b2000-05-05 19:34:50 +000066 break;
67
68 case 'z':
Wanlong Gao354ebb42012-12-07 10:10:04 +080069 memset(buffer, 0x0, bsize);
whrb973f2b2000-05-05 19:34:50 +000070 break;
71
Wanlong Gao354ebb42012-12-07 10:10:04 +080072 case 'r': /* random */
Wanlong Gao365a3c12012-12-25 15:25:35 +080073 for (ind = 0; ind < bsize; ind++)
Wanlong Gao354ebb42012-12-07 10:10:04 +080074 buffer[ind] = (rand() & 0177) | 0100;
Wanlong Gao354ebb42012-12-07 10:10:04 +080075 }
whrb973f2b2000-05-05 19:34:50 +000076}
77
Wanlong Gao365a3c12012-12-25 15:25:35 +080078/*
whrb973f2b2000-05-05 19:34:50 +000079 * return values:
80 * >= 0 : error at byte offset into the file, offset+buffer[0-(bsize-1)]
81 * < 0 : no error
Wanlong Gao365a3c12012-12-25 15:25:35 +080082 */
83int databinchk(int mode, char *buffer, int bsize, int offset, char **errmsg)
whrb973f2b2000-05-05 19:34:50 +000084{
Wanlong Gao354ebb42012-12-07 10:10:04 +080085 int cnt;
86 unsigned char *chr;
87 long expbits;
88 long actbits;
whrb973f2b2000-05-05 19:34:50 +000089
Wanlong Gao354ebb42012-12-07 10:10:04 +080090 chr = (unsigned char *)buffer;
whrb973f2b2000-05-05 19:34:50 +000091
Wanlong Gao365a3c12012-12-25 15:25:35 +080092 if (errmsg != NULL)
Wanlong Gao354ebb42012-12-07 10:10:04 +080093 *errmsg = Errmsg;
vapier45a8ba02009-07-20 10:59:32 +000094
Wanlong Gao354ebb42012-12-07 10:10:04 +080095 switch (mode) {
96 default:
97 case 'a': /* alternating bit pattern */
98 expbits = 0x55;
99 break;
whrb973f2b2000-05-05 19:34:50 +0000100
Wanlong Gao354ebb42012-12-07 10:10:04 +0800101 case 'c': /* checkerboard pattern */
102 expbits = 0xf0;
103 break;
whrb973f2b2000-05-05 19:34:50 +0000104
Wanlong Gao354ebb42012-12-07 10:10:04 +0800105 case 'C': /* counting pattern */
106 for (cnt = 0; cnt < bsize; cnt++) {
107 expbits = ((offset + cnt) % 8 & 0177);
whrb973f2b2000-05-05 19:34:50 +0000108
Wanlong Gao354ebb42012-12-07 10:10:04 +0800109 if (buffer[cnt] != expbits) {
110 sprintf(Errmsg,
111 "data mismatch at offset %d, exp:%#lo, act:%#o",
112 offset + cnt, expbits, buffer[cnt]);
113 return offset + cnt;
114 }
whrb973f2b2000-05-05 19:34:50 +0000115 }
116 sprintf(Errmsg, "all %d bytes match desired pattern", bsize);
117 return -1;
118
119 case 'o':
Wanlong Gao354ebb42012-12-07 10:10:04 +0800120 expbits = 0xff;
whrb973f2b2000-05-05 19:34:50 +0000121 break;
122
123 case 'z':
Wanlong Gao354ebb42012-12-07 10:10:04 +0800124 expbits = 0;
whrb973f2b2000-05-05 19:34:50 +0000125 break;
126
Wanlong Gao354ebb42012-12-07 10:10:04 +0800127 case 'r':
whrb973f2b2000-05-05 19:34:50 +0000128 return -1; /* no check can be done for random */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800129 }
whrb973f2b2000-05-05 19:34:50 +0000130
Wanlong Gao354ebb42012-12-07 10:10:04 +0800131 for (cnt = 0; cnt < bsize; chr++, cnt++) {
132 actbits = (long)*chr;
whrb973f2b2000-05-05 19:34:50 +0000133
Wanlong Gao354ebb42012-12-07 10:10:04 +0800134 if (actbits != expbits) {
135 sprintf(Errmsg,
136 "data mismatch at offset %d, exp:%#lo, act:%#lo",
137 offset + cnt, expbits, actbits);
138 return offset + cnt;
139 }
whrb973f2b2000-05-05 19:34:50 +0000140 }
141
142 sprintf(Errmsg, "all %d bytes match desired pattern", bsize);
Wanlong Gao365a3c12012-12-25 15:25:35 +0800143 return -1;
whrb973f2b2000-05-05 19:34:50 +0000144}
145
146#if UNIT_TEST
147
Wanlong Gao365a3c12012-12-25 15:25:35 +0800148int main(int ac, char **ag)
whrb973f2b2000-05-05 19:34:50 +0000149{
Wanlong Gao354ebb42012-12-07 10:10:04 +0800150 int size = 1023;
151 int offset;
152 int number;
153 unsigned char *buffer;
154 int ret;
155 char *errmsg;
whrb973f2b2000-05-05 19:34:50 +0000156
Wanlong Gao365a3c12012-12-25 15:25:35 +0800157 buffer = malloc(size);
158 if (buffer == NULL) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800159 perror("malloc");
160 exit(2);
161 }
whrb973f2b2000-05-05 19:34:50 +0000162
Wanlong Gao354ebb42012-12-07 10:10:04 +0800163 printf("***** for a ****************************\n");
164 databingen('a', buffer, size, 0);
165 printf("databingen('a', buffer, %d, 0)\n", size);
whrb973f2b2000-05-05 19:34:50 +0000166
Wanlong Gao354ebb42012-12-07 10:10:04 +0800167 ret = databinchk('a', buffer, size, 0, &errmsg);
168 printf("databinchk('a', buffer, %d, 0, &errmsg) returned %d: %s\n",
169 size, ret, errmsg);
170 if (ret == -1)
171 printf("\tPASS return value of -1 as expected\n");
172 else
173 printf("\tFAIL return value %d, expected -1\n", ret);
whrb973f2b2000-05-05 19:34:50 +0000174
Wanlong Gao354ebb42012-12-07 10:10:04 +0800175 offset = 232400;
176 ret = databinchk('a', &buffer[1], size - 1, offset, &errmsg);
177 printf("databinchk('a', &buffer[1], %d, %d, &errmsg) returned %d: %s\n",
178 size, offset, ret, errmsg);
179 if (ret == -1)
180 printf("\tPASS return value of -1 as expected\n");
181 else
182 printf("\tFAIL return value %d, expected -1\n", ret);
whrb973f2b2000-05-05 19:34:50 +0000183
Wanlong Gao354ebb42012-12-07 10:10:04 +0800184 buffer[15] = 0x0;
185 printf("changing char 15 (offset (%d+15) = %d) to 0x0\n", offset,
186 offset + 15);
187 number = offset + 15;
whrb973f2b2000-05-05 19:34:50 +0000188
Wanlong Gao354ebb42012-12-07 10:10:04 +0800189 ret = databinchk('a', &buffer[1], size - 1, offset + 1, &errmsg);
190 printf("databinchk('a', &buffer[1], %d, %d, &errmsg) returned %d: %s\n",
191 size - 1, offset + 1, ret, errmsg);
192 if (ret == number)
193 printf("\tPASS return value of %d as expected\n", number);
194 else
195 printf("\tFAIL return value %d, expected %d\n", ret, number);
whrb973f2b2000-05-05 19:34:50 +0000196
Wanlong Gao354ebb42012-12-07 10:10:04 +0800197 printf("***** for c ****************************\n");
198 databingen('c', buffer, size, 0);
199 printf("databingen('c', buffer, %d, 0)\n", size);
whrb973f2b2000-05-05 19:34:50 +0000200
Wanlong Gao354ebb42012-12-07 10:10:04 +0800201 ret = databinchk('c', buffer, size, 0, &errmsg);
202 printf("databinchk('c', buffer, %d, 0, &errmsg) returned %d: %s\n",
203 size, ret, errmsg);
204 if (ret == -1)
205 printf("\tPASS return value of -1 as expected\n");
206 else
207 printf("\tFAIL return value %d, expected -1\n", ret);
whrb973f2b2000-05-05 19:34:50 +0000208
Wanlong Gao354ebb42012-12-07 10:10:04 +0800209 offset = 232400;
210 ret = databinchk('c', &buffer[1], size - 1, offset, &errmsg);
211 printf("databinchk('c', &buffer[1], %d, %d, &errmsg) returned %d: %s\n",
212 size, offset, ret, errmsg);
213 if (ret == -1)
214 printf("\tPASS return value of -1 as expected\n");
215 else
216 printf("\tFAIL return value %d, expected -1\n", ret);
whrb973f2b2000-05-05 19:34:50 +0000217
Wanlong Gao354ebb42012-12-07 10:10:04 +0800218 buffer[15] = 0x0;
219 printf("changing char 15 (offset (%d+15) = %d) to 0x0\n", offset,
220 offset + 15);
221 number = offset + 15;
whrb973f2b2000-05-05 19:34:50 +0000222
Wanlong Gao354ebb42012-12-07 10:10:04 +0800223 ret = databinchk('c', &buffer[1], size - 1, offset + 1, &errmsg);
224 printf("databinchk('c', &buffer[1], %d, %d, &errmsg) returned %d: %s\n",
225 size - 1, offset + 1, ret, errmsg);
226 if (ret == number)
227 printf("\tPASS return value of %d as expected\n", number);
228 else
229 printf("\tFAIL return value %d, expected %d\n", ret, number);
whrb973f2b2000-05-05 19:34:50 +0000230
Wanlong Gao354ebb42012-12-07 10:10:04 +0800231 printf("***** for C ****************************\n");
whrb973f2b2000-05-05 19:34:50 +0000232
Wanlong Gao354ebb42012-12-07 10:10:04 +0800233 databingen('C', buffer, size, 0);
234 printf("databingen('C', buffer, %d, 0)\n", size);
whrb973f2b2000-05-05 19:34:50 +0000235
Wanlong Gao354ebb42012-12-07 10:10:04 +0800236 ret = databinchk('C', buffer, size, 0, &errmsg);
237 printf("databinchk('C', buffer, %d, 0, &errmsg) returned %d: %s\n",
238 size, ret, errmsg);
239 if (ret == -1)
240 printf("\tPASS return value of -1 as expected\n");
241 else
242 printf("\tFAIL return value %d, expected -1\n", ret);
whrb973f2b2000-05-05 19:34:50 +0000243
Wanlong Gao354ebb42012-12-07 10:10:04 +0800244 offset = 18;
245 ret = databinchk('C', &buffer[18], size - 18, 18, &errmsg);
246 printf
247 ("databinchk('C', &buffer[18], %d, 18, &errmsg) returned %d: %s\n",
248 size - 18, ret, errmsg);
249 if (ret == -1)
250 printf("\tPASS return value of -1 as expected\n");
251 else
252 printf("\tFAIL return value %d, expected -1\n", ret);
whrb973f2b2000-05-05 19:34:50 +0000253
Wanlong Gao354ebb42012-12-07 10:10:04 +0800254 buffer[20] = 0x0;
255 buffer[21] = 0x0;
256 printf("changing char 20 and 21 to 0x0 (offset %d and %d)\n", 20, 21);
whrb973f2b2000-05-05 19:34:50 +0000257
Wanlong Gao354ebb42012-12-07 10:10:04 +0800258 ret = databinchk('C', &buffer[18], size - 18, 18, &errmsg);
Wanlong Gao365a3c12012-12-25 15:25:35 +0800259 printf("databinchk('C', &buffer[18], %d, 18, &errmsg) returned %d: %s\n",
260 size - 18, ret, errmsg);
whrb973f2b2000-05-05 19:34:50 +0000261
Wanlong Gao354ebb42012-12-07 10:10:04 +0800262 if (ret == 20 || ret == 21)
263 printf("\tPASS return value of %d or %d as expected\n", 20, 21);
264 else
265 printf("\tFAIL return value %d, expected %d or %d\n", ret,
266 20, 21);
whrb973f2b2000-05-05 19:34:50 +0000267
Wanlong Gao354ebb42012-12-07 10:10:04 +0800268 exit(0);
whrb973f2b2000-05-05 19:34:50 +0000269
270}
271
272#endif