blob: 23164d3e33b28a7e1bfcec114c0bbd967b610067 [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/************
33
3464 bits in a Cray word
35
36 12345678901234567890123456789012
371234567890123456789012345678901234567890123456789012345678901234
38________________________________________________________________
39< pid >< word-offset in file (same #) >< pid >
40
411234567890123456789012345678901234567890123456789012345678901234
42________________________________________________________________
43< pid >< offset in file of this word >< pid >
44
whrb973f2b2000-05-05 19:34:50 +0000458 bits to a bytes == character
vapier45a8ba02009-07-20 10:59:32 +000046 NBPW 8
whrb973f2b2000-05-05 19:34:50 +000047************/
48
49#include <stdio.h>
50#include <sys/param.h>
51#ifdef UNIT_TEST
52#include <unistd.h>
53#include <stdlib.h>
54#endif
55
56static char Errmsg[80];
57
58#define LOWER16BITS(X) (X & 0177777)
59#define LOWER32BITS(X) (X & 0xffffffff)
60
61/***
62#define HIGHBITS(WRD, bits) ( (-1 << (64-bits)) & WRD)
63#define LOWBITS(WRD, bits) ( (-1 >> (64-bits)) & WRD)
64****/
65
Wanlong Gao354ebb42012-12-07 10:10:04 +080066#define NBPBYTE 8 /* number bits per byte */
whrb973f2b2000-05-05 19:34:50 +000067
68#ifndef DEBUG
69#define DEBUG 0
70#endif
71
72/***********************************************************************
73 *
vapier45a8ba02009-07-20 10:59:32 +000074 *
whrb973f2b2000-05-05 19:34:50 +000075 * 1 2 3 4 5 6 7 8 9 10 11 12 13 14 14 15 bytes
76 * 1234567890123456789012345678901234567890123456789012345678901234 bits
77 * ________________________________________________________________ 1 word
78 * < pid >< offset in file of this word >< pid >
vapier45a8ba02009-07-20 10:59:32 +000079 *
whrb973f2b2000-05-05 19:34:50 +000080 * the words are put together where offset zero is the start.
81 * thus, offset 16 is the start of the second full word
82 * Thus, offset 8 is in middle of word 1
83 ***********************************************************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +080084int datapidgen(pid, buffer, bsize, offset)
whrb973f2b2000-05-05 19:34:50 +000085int pid;
86char *buffer;
87int bsize;
88int offset;
89{
90#if CRAY
vapier45a8ba02009-07-20 10:59:32 +000091
Wanlong Gao354ebb42012-12-07 10:10:04 +080092 int cnt;
93 int tmp;
94 char *chr;
95 long *wptr;
96 long word;
97 int woff; /* file offset for the word */
98 int boff; /* buffer offset or index */
99 int num_full_words;
whrb973f2b2000-05-05 19:34:50 +0000100
Wanlong Gao354ebb42012-12-07 10:10:04 +0800101 num_full_words = bsize / NBPW;
102 boff = 0;
whrb973f2b2000-05-05 19:34:50 +0000103
Wanlong Gao354ebb42012-12-07 10:10:04 +0800104 if (cnt = (offset % NBPW)) { /* partial word */
whrb973f2b2000-05-05 19:34:50 +0000105
Wanlong Gao354ebb42012-12-07 10:10:04 +0800106 woff = offset - cnt;
whrb973f2b2000-05-05 19:34:50 +0000107#if DEBUG
Wanlong Gao354ebb42012-12-07 10:10:04 +0800108 printf("partial at beginning, cnt = %d, woff = %d\n", cnt,
109 woff);
whrb973f2b2000-05-05 19:34:50 +0000110#endif
111
Wanlong Gao354ebb42012-12-07 10:10:04 +0800112 word =
113 ((LOWER16BITS(pid) << 48) | (LOWER32BITS(woff) << 16) |
114 LOWER16BITS(pid));
whrb973f2b2000-05-05 19:34:50 +0000115
Wanlong Gao354ebb42012-12-07 10:10:04 +0800116 chr = (char *)&word;
whrb973f2b2000-05-05 19:34:50 +0000117
Wanlong Gao354ebb42012-12-07 10:10:04 +0800118 for (tmp = 0; tmp < cnt; tmp++) { /* skip unused bytes */
119 chr++;
120 }
whrb973f2b2000-05-05 19:34:50 +0000121
Wanlong Gao354ebb42012-12-07 10:10:04 +0800122 for (; boff < (NBPW - cnt) && boff < bsize; boff++, chr++) {
123 buffer[boff] = *chr;
124 }
whrb973f2b2000-05-05 19:34:50 +0000125 }
whrb973f2b2000-05-05 19:34:50 +0000126
Wanlong Gao354ebb42012-12-07 10:10:04 +0800127 /*
128 * full words
129 */
whrb973f2b2000-05-05 19:34:50 +0000130
Wanlong Gao354ebb42012-12-07 10:10:04 +0800131 num_full_words = (bsize - boff) / NBPW;
vapier45a8ba02009-07-20 10:59:32 +0000132
Wanlong Gao354ebb42012-12-07 10:10:04 +0800133 woff = offset + boff;
vapier45a8ba02009-07-20 10:59:32 +0000134
Wanlong Gao354ebb42012-12-07 10:10:04 +0800135 for (cnt = 0; cnt < num_full_words; woff += NBPW, cnt++) {
whrb973f2b2000-05-05 19:34:50 +0000136
Wanlong Gao354ebb42012-12-07 10:10:04 +0800137 word =
138 ((LOWER16BITS(pid) << 48) | (LOWER32BITS(woff) << 16) |
139 LOWER16BITS(pid));
whrb973f2b2000-05-05 19:34:50 +0000140
Wanlong Gao354ebb42012-12-07 10:10:04 +0800141 chr = (char *)&word;
142 for (tmp = 0; tmp < NBPW; tmp++, chr++) {
143 buffer[boff++] = *chr;
144 }
whrb973f2b2000-05-05 19:34:50 +0000145/****** Only if wptr is a word ellined
146 wptr = (long *)&buffer[boff];
147 *wptr = word;
148 boff += NBPW;
149*****/
150
whrb973f2b2000-05-05 19:34:50 +0000151 }
whrb973f2b2000-05-05 19:34:50 +0000152
Wanlong Gao354ebb42012-12-07 10:10:04 +0800153 /*
154 * partial word at end of buffer
155 */
156
157 if (cnt = ((bsize - boff) % NBPW)) {
158#if DEBUG
159 printf("partial at end\n");
160#endif
161 word =
162 ((LOWER16BITS(pid) << 48) | (LOWER32BITS(woff) << 16) |
163 LOWER16BITS(pid));
164
165 chr = (char *)&word;
166
167 for (tmp = 0; tmp < cnt && boff < bsize; tmp++, chr++) {
168 buffer[boff++] = *chr;
169 }
170 }
171
172 return bsize;
whrb973f2b2000-05-05 19:34:50 +0000173
174#else
Wanlong Gao354ebb42012-12-07 10:10:04 +0800175 return -1; /* not support on non-64 bits word machines */
whrb973f2b2000-05-05 19:34:50 +0000176
177#endif
178
vapier45a8ba02009-07-20 10:59:32 +0000179}
whrb973f2b2000-05-05 19:34:50 +0000180
181/***********************************************************************
182 *
183 *
184 ***********************************************************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800185int datapidchk(pid, buffer, bsize, offset, errmsg)
whrb973f2b2000-05-05 19:34:50 +0000186int pid;
187char *buffer;
188int bsize;
189int offset;
190char **errmsg;
191{
192#if CRAY
vapier45a8ba02009-07-20 10:59:32 +0000193
Wanlong Gao354ebb42012-12-07 10:10:04 +0800194 int cnt;
195 int tmp;
196 char *chr;
197 long *wptr;
198 long word;
199 int woff; /* file offset for the word */
200 int boff; /* buffer offset or index */
201 int num_full_words;
whrb973f2b2000-05-05 19:34:50 +0000202
Wanlong Gao354ebb42012-12-07 10:10:04 +0800203 if (errmsg != NULL) {
204 *errmsg = Errmsg;
whrb973f2b2000-05-05 19:34:50 +0000205 }
whrb973f2b2000-05-05 19:34:50 +0000206
Wanlong Gao354ebb42012-12-07 10:10:04 +0800207 num_full_words = bsize / NBPW;
208 boff = 0;
whrb973f2b2000-05-05 19:34:50 +0000209
Wanlong Gao354ebb42012-12-07 10:10:04 +0800210 if (cnt = (offset % NBPW)) { /* partial word */
211 woff = offset - cnt;
212 word =
213 ((LOWER16BITS(pid) << 48) | (LOWER32BITS(woff) << 16) |
214 LOWER16BITS(pid));
vapier45a8ba02009-07-20 10:59:32 +0000215
Wanlong Gao354ebb42012-12-07 10:10:04 +0800216 chr = (char *)&word;
vapier45a8ba02009-07-20 10:59:32 +0000217
Wanlong Gao354ebb42012-12-07 10:10:04 +0800218 for (tmp = 0; tmp < cnt; tmp++) { /* skip unused bytes */
219 chr++;
220 }
whrb973f2b2000-05-05 19:34:50 +0000221
Wanlong Gao354ebb42012-12-07 10:10:04 +0800222 for (; boff < (NBPW - cnt) && boff < bsize; boff++, chr++) {
223 if (buffer[boff] != *chr) {
224 sprintf(Errmsg,
225 "Data mismatch at offset %d, exp:%#o, act:%#o",
226 offset + boff, *chr, buffer[boff]);
227 return offset + boff;
228 }
229 }
whrb973f2b2000-05-05 19:34:50 +0000230 }
231
Wanlong Gao354ebb42012-12-07 10:10:04 +0800232 /*
233 * full words
234 */
235
236 num_full_words = (bsize - boff) / NBPW;
237
238 woff = offset + boff;
239
240 for (cnt = 0; cnt < num_full_words; woff += NBPW, cnt++) {
241 word =
242 ((LOWER16BITS(pid) << 48) | (LOWER32BITS(woff) << 16) |
243 LOWER16BITS(pid));
244
245 chr = (char *)&word;
246 for (tmp = 0; tmp < NBPW; tmp++, boff++, chr++) {
247 if (buffer[boff] != *chr) {
248 sprintf(Errmsg,
249 "Data mismatch at offset %d, exp:%#o, act:%#o",
250 woff, *chr, buffer[boff]);
251 return woff;
252 }
253 }
254
whrb973f2b2000-05-05 19:34:50 +0000255/****** only if a word elined
256 wptr = (long *)&buffer[boff];
Garrett Cooper903910d2010-11-23 09:27:44 -0800257 if (*wptr != word) {
whrb973f2b2000-05-05 19:34:50 +0000258 sprintf(Errmsg, "Data mismatch at offset %d, exp:%#o, act:%#o",
259 woff, word, *wptr);
260 return woff;
261 }
262 boff += NBPW;
263******/
whrb973f2b2000-05-05 19:34:50 +0000264 }
whrb973f2b2000-05-05 19:34:50 +0000265
Wanlong Gao354ebb42012-12-07 10:10:04 +0800266 /*
267 * partial word at end of buffer
268 */
269
270 if (cnt = ((bsize - boff) % NBPW)) {
271#if DEBUG
272 printf("partial at end\n");
273#endif
274 word =
275 ((LOWER16BITS(pid) << 48) | (LOWER32BITS(woff) << 16) |
276 LOWER16BITS(pid));
277
278 chr = (char *)&word;
279
280 for (tmp = 0; tmp < cnt && boff < bsize; boff++, tmp++, chr++) {
281 if (buffer[boff] != *chr) {
282 sprintf(Errmsg,
283 "Data mismatch at offset %d, exp:%#o, act:%#o",
284 offset + boff, *chr, buffer[boff]);
285 return offset + boff;
286 }
287 }
288 }
289
290 sprintf(Errmsg, "all %d bytes match desired pattern", bsize);
291 return -1; /* buffer is ok */
whrb973f2b2000-05-05 19:34:50 +0000292
293#else
vapier45a8ba02009-07-20 10:59:32 +0000294
Wanlong Gao354ebb42012-12-07 10:10:04 +0800295 if (errmsg != NULL) {
296 *errmsg = Errmsg;
297 }
298 sprintf(Errmsg, "Not supported on this OS.");
299 return 0;
whrb973f2b2000-05-05 19:34:50 +0000300
301#endif
302
Wanlong Gao354ebb42012-12-07 10:10:04 +0800303} /* end of datapidchk */
whrb973f2b2000-05-05 19:34:50 +0000304
305#if UNIT_TEST
306
307/***********************************************************************
308 * main for doing unit testing
309 ***********************************************************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800310int main(ac, ag)
whrb973f2b2000-05-05 19:34:50 +0000311int ac;
312char **ag;
313{
314
Wanlong Gao354ebb42012-12-07 10:10:04 +0800315 int size = 1234;
316 char *buffer;
317 int ret;
318 char *errmsg;
whrb973f2b2000-05-05 19:34:50 +0000319
Wanlong Gao354ebb42012-12-07 10:10:04 +0800320 if ((buffer = (char *)malloc(size)) == NULL) {
321 perror("malloc");
322 exit(2);
323 }
whrb973f2b2000-05-05 19:34:50 +0000324
Wanlong Gao354ebb42012-12-07 10:10:04 +0800325 datapidgen(-1, buffer, size, 3);
whrb973f2b2000-05-05 19:34:50 +0000326
327/***
328fwrite(buffer, size, 1, stdout);
329fwrite("\n", 1, 1, stdout);
330****/
331
Wanlong Gao354ebb42012-12-07 10:10:04 +0800332 printf("datapidgen(-1, buffer, size, 3)\n");
whrb973f2b2000-05-05 19:34:50 +0000333
Wanlong Gao354ebb42012-12-07 10:10:04 +0800334 ret = datapidchk(-1, buffer, size, 3, &errmsg);
335 printf("datapidchk(-1, buffer, %d, 3, &errmsg) returned %d %s\n",
336 size, ret, errmsg);
337 ret = datapidchk(-1, &buffer[1], size - 1, 4, &errmsg);
338 printf("datapidchk(-1, &buffer[1], %d, 4, &errmsg) returned %d %s\n",
339 size - 1, ret, errmsg);
whrb973f2b2000-05-05 19:34:50 +0000340
Wanlong Gao354ebb42012-12-07 10:10:04 +0800341 buffer[25] = 0x0;
342 buffer[26] = 0x0;
343 buffer[27] = 0x0;
344 buffer[28] = 0x0;
345 printf("changing char 25-28\n");
whrb973f2b2000-05-05 19:34:50 +0000346
Wanlong Gao354ebb42012-12-07 10:10:04 +0800347 ret = datapidchk(-1, &buffer[1], size - 1, 4, &errmsg);
348 printf("datapidchk(-1, &buffer[1], %d, 4, &errmsg) returned %d %s\n",
349 size - 1, ret, errmsg);
whrb973f2b2000-05-05 19:34:50 +0000350
Wanlong Gao354ebb42012-12-07 10:10:04 +0800351 printf("------------------------------------------\n");
whrb973f2b2000-05-05 19:34:50 +0000352
Wanlong Gao354ebb42012-12-07 10:10:04 +0800353 datapidgen(getpid(), buffer, size, 5);
whrb973f2b2000-05-05 19:34:50 +0000354
355/*******
356fwrite(buffer, size, 1, stdout);
357fwrite("\n", 1, 1, stdout);
358******/
359
Wanlong Gao354ebb42012-12-07 10:10:04 +0800360 printf("\ndatapidgen(getpid(), buffer, size, 5)\n");
whrb973f2b2000-05-05 19:34:50 +0000361
Wanlong Gao354ebb42012-12-07 10:10:04 +0800362 ret = datapidchk(getpid(), buffer, size, 5, &errmsg);
363 printf("datapidchk(getpid(), buffer, %d, 5, &errmsg) returned %d %s\n",
364 size, ret, errmsg);
whrb973f2b2000-05-05 19:34:50 +0000365
Wanlong Gao354ebb42012-12-07 10:10:04 +0800366 ret = datapidchk(getpid(), &buffer[1], size - 1, 6, &errmsg);
367 printf
368 ("datapidchk(getpid(), &buffer[1], %d, 6, &errmsg) returned %d %s\n",
369 size - 1, ret, errmsg);
whrb973f2b2000-05-05 19:34:50 +0000370
Wanlong Gao354ebb42012-12-07 10:10:04 +0800371 buffer[25] = 0x0;
372 printf("changing char 25\n");
whrb973f2b2000-05-05 19:34:50 +0000373
Wanlong Gao354ebb42012-12-07 10:10:04 +0800374 ret = datapidchk(getpid(), &buffer[1], size - 1, 6, &errmsg);
375 printf
376 ("datapidchk(getpid(), &buffer[1], %d, 6, &errmsg) returned %d %s\n",
377 size - 1, ret, errmsg);
whrb973f2b2000-05-05 19:34:50 +0000378
Wanlong Gao354ebb42012-12-07 10:10:04 +0800379 exit(0);
whrb973f2b2000-05-05 19:34:50 +0000380}
381
382#endif