blob: dad1a12b68e564bbfd4f2c6115b057570ceb6c88 [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 ***********************************************************************/
Stanislav Kholmanskikh87aa2dc2014-04-11 11:45:19 +040084int datapidgen(int pid, char *buffer, int bsize, int offset)
whrb973f2b2000-05-05 19:34:50 +000085{
86#if CRAY
vapier45a8ba02009-07-20 10:59:32 +000087
Wanlong Gao354ebb42012-12-07 10:10:04 +080088 int cnt;
89 int tmp;
90 char *chr;
91 long *wptr;
92 long word;
93 int woff; /* file offset for the word */
94 int boff; /* buffer offset or index */
95 int num_full_words;
whrb973f2b2000-05-05 19:34:50 +000096
Wanlong Gao354ebb42012-12-07 10:10:04 +080097 num_full_words = bsize / NBPW;
98 boff = 0;
whrb973f2b2000-05-05 19:34:50 +000099
Wanlong Gao354ebb42012-12-07 10:10:04 +0800100 if (cnt = (offset % NBPW)) { /* partial word */
whrb973f2b2000-05-05 19:34:50 +0000101
Wanlong Gao354ebb42012-12-07 10:10:04 +0800102 woff = offset - cnt;
whrb973f2b2000-05-05 19:34:50 +0000103#if DEBUG
Wanlong Gao354ebb42012-12-07 10:10:04 +0800104 printf("partial at beginning, cnt = %d, woff = %d\n", cnt,
105 woff);
whrb973f2b2000-05-05 19:34:50 +0000106#endif
107
Wanlong Gao354ebb42012-12-07 10:10:04 +0800108 word =
109 ((LOWER16BITS(pid) << 48) | (LOWER32BITS(woff) << 16) |
110 LOWER16BITS(pid));
whrb973f2b2000-05-05 19:34:50 +0000111
Wanlong Gao354ebb42012-12-07 10:10:04 +0800112 chr = (char *)&word;
whrb973f2b2000-05-05 19:34:50 +0000113
Wanlong Gao354ebb42012-12-07 10:10:04 +0800114 for (tmp = 0; tmp < cnt; tmp++) { /* skip unused bytes */
115 chr++;
116 }
whrb973f2b2000-05-05 19:34:50 +0000117
Wanlong Gao354ebb42012-12-07 10:10:04 +0800118 for (; boff < (NBPW - cnt) && boff < bsize; boff++, chr++) {
119 buffer[boff] = *chr;
120 }
whrb973f2b2000-05-05 19:34:50 +0000121 }
whrb973f2b2000-05-05 19:34:50 +0000122
Wanlong Gao354ebb42012-12-07 10:10:04 +0800123 /*
124 * full words
125 */
whrb973f2b2000-05-05 19:34:50 +0000126
Wanlong Gao354ebb42012-12-07 10:10:04 +0800127 num_full_words = (bsize - boff) / NBPW;
vapier45a8ba02009-07-20 10:59:32 +0000128
Wanlong Gao354ebb42012-12-07 10:10:04 +0800129 woff = offset + boff;
vapier45a8ba02009-07-20 10:59:32 +0000130
Wanlong Gao354ebb42012-12-07 10:10:04 +0800131 for (cnt = 0; cnt < num_full_words; woff += NBPW, cnt++) {
whrb973f2b2000-05-05 19:34:50 +0000132
Wanlong Gao354ebb42012-12-07 10:10:04 +0800133 word =
134 ((LOWER16BITS(pid) << 48) | (LOWER32BITS(woff) << 16) |
135 LOWER16BITS(pid));
whrb973f2b2000-05-05 19:34:50 +0000136
Wanlong Gao354ebb42012-12-07 10:10:04 +0800137 chr = (char *)&word;
138 for (tmp = 0; tmp < NBPW; tmp++, chr++) {
139 buffer[boff++] = *chr;
140 }
whrb973f2b2000-05-05 19:34:50 +0000141/****** Only if wptr is a word ellined
142 wptr = (long *)&buffer[boff];
143 *wptr = word;
144 boff += NBPW;
145*****/
146
whrb973f2b2000-05-05 19:34:50 +0000147 }
whrb973f2b2000-05-05 19:34:50 +0000148
Wanlong Gao354ebb42012-12-07 10:10:04 +0800149 /*
150 * partial word at end of buffer
151 */
152
153 if (cnt = ((bsize - boff) % NBPW)) {
154#if DEBUG
155 printf("partial at end\n");
156#endif
157 word =
158 ((LOWER16BITS(pid) << 48) | (LOWER32BITS(woff) << 16) |
159 LOWER16BITS(pid));
160
161 chr = (char *)&word;
162
163 for (tmp = 0; tmp < cnt && boff < bsize; tmp++, chr++) {
164 buffer[boff++] = *chr;
165 }
166 }
167
168 return bsize;
whrb973f2b2000-05-05 19:34:50 +0000169
170#else
Wanlong Gao354ebb42012-12-07 10:10:04 +0800171 return -1; /* not support on non-64 bits word machines */
whrb973f2b2000-05-05 19:34:50 +0000172
173#endif
174
vapier45a8ba02009-07-20 10:59:32 +0000175}
whrb973f2b2000-05-05 19:34:50 +0000176
177/***********************************************************************
178 *
179 *
180 ***********************************************************************/
Stanislav Kholmanskikh87aa2dc2014-04-11 11:45:19 +0400181int datapidchk(int pid, char *buffer, int bsize, int offset, char **errmsg)
whrb973f2b2000-05-05 19:34:50 +0000182{
183#if CRAY
vapier45a8ba02009-07-20 10:59:32 +0000184
Wanlong Gao354ebb42012-12-07 10:10:04 +0800185 int cnt;
186 int tmp;
187 char *chr;
188 long *wptr;
189 long word;
190 int woff; /* file offset for the word */
191 int boff; /* buffer offset or index */
192 int num_full_words;
whrb973f2b2000-05-05 19:34:50 +0000193
Wanlong Gao354ebb42012-12-07 10:10:04 +0800194 if (errmsg != NULL) {
195 *errmsg = Errmsg;
whrb973f2b2000-05-05 19:34:50 +0000196 }
whrb973f2b2000-05-05 19:34:50 +0000197
Wanlong Gao354ebb42012-12-07 10:10:04 +0800198 num_full_words = bsize / NBPW;
199 boff = 0;
whrb973f2b2000-05-05 19:34:50 +0000200
Wanlong Gao354ebb42012-12-07 10:10:04 +0800201 if (cnt = (offset % NBPW)) { /* partial word */
202 woff = offset - cnt;
203 word =
204 ((LOWER16BITS(pid) << 48) | (LOWER32BITS(woff) << 16) |
205 LOWER16BITS(pid));
vapier45a8ba02009-07-20 10:59:32 +0000206
Wanlong Gao354ebb42012-12-07 10:10:04 +0800207 chr = (char *)&word;
vapier45a8ba02009-07-20 10:59:32 +0000208
Wanlong Gao354ebb42012-12-07 10:10:04 +0800209 for (tmp = 0; tmp < cnt; tmp++) { /* skip unused bytes */
210 chr++;
211 }
whrb973f2b2000-05-05 19:34:50 +0000212
Wanlong Gao354ebb42012-12-07 10:10:04 +0800213 for (; boff < (NBPW - cnt) && boff < bsize; boff++, chr++) {
214 if (buffer[boff] != *chr) {
215 sprintf(Errmsg,
216 "Data mismatch at offset %d, exp:%#o, act:%#o",
217 offset + boff, *chr, buffer[boff]);
218 return offset + boff;
219 }
220 }
whrb973f2b2000-05-05 19:34:50 +0000221 }
222
Wanlong Gao354ebb42012-12-07 10:10:04 +0800223 /*
224 * full words
225 */
226
227 num_full_words = (bsize - boff) / NBPW;
228
229 woff = offset + boff;
230
231 for (cnt = 0; cnt < num_full_words; woff += NBPW, cnt++) {
232 word =
233 ((LOWER16BITS(pid) << 48) | (LOWER32BITS(woff) << 16) |
234 LOWER16BITS(pid));
235
236 chr = (char *)&word;
237 for (tmp = 0; tmp < NBPW; tmp++, boff++, chr++) {
238 if (buffer[boff] != *chr) {
239 sprintf(Errmsg,
240 "Data mismatch at offset %d, exp:%#o, act:%#o",
241 woff, *chr, buffer[boff]);
242 return woff;
243 }
244 }
245
whrb973f2b2000-05-05 19:34:50 +0000246/****** only if a word elined
247 wptr = (long *)&buffer[boff];
Garrett Cooper903910d2010-11-23 09:27:44 -0800248 if (*wptr != word) {
whrb973f2b2000-05-05 19:34:50 +0000249 sprintf(Errmsg, "Data mismatch at offset %d, exp:%#o, act:%#o",
250 woff, word, *wptr);
251 return woff;
252 }
253 boff += NBPW;
254******/
whrb973f2b2000-05-05 19:34:50 +0000255 }
whrb973f2b2000-05-05 19:34:50 +0000256
Wanlong Gao354ebb42012-12-07 10:10:04 +0800257 /*
258 * partial word at end of buffer
259 */
260
261 if (cnt = ((bsize - boff) % NBPW)) {
262#if DEBUG
263 printf("partial at end\n");
264#endif
265 word =
266 ((LOWER16BITS(pid) << 48) | (LOWER32BITS(woff) << 16) |
267 LOWER16BITS(pid));
268
269 chr = (char *)&word;
270
271 for (tmp = 0; tmp < cnt && boff < bsize; boff++, tmp++, chr++) {
272 if (buffer[boff] != *chr) {
273 sprintf(Errmsg,
274 "Data mismatch at offset %d, exp:%#o, act:%#o",
275 offset + boff, *chr, buffer[boff]);
276 return offset + boff;
277 }
278 }
279 }
280
281 sprintf(Errmsg, "all %d bytes match desired pattern", bsize);
282 return -1; /* buffer is ok */
whrb973f2b2000-05-05 19:34:50 +0000283
284#else
vapier45a8ba02009-07-20 10:59:32 +0000285
Wanlong Gao354ebb42012-12-07 10:10:04 +0800286 if (errmsg != NULL) {
287 *errmsg = Errmsg;
288 }
289 sprintf(Errmsg, "Not supported on this OS.");
290 return 0;
whrb973f2b2000-05-05 19:34:50 +0000291
292#endif
293
Wanlong Gao354ebb42012-12-07 10:10:04 +0800294} /* end of datapidchk */
whrb973f2b2000-05-05 19:34:50 +0000295
296#if UNIT_TEST
297
298/***********************************************************************
299 * main for doing unit testing
300 ***********************************************************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800301int main(ac, ag)
whrb973f2b2000-05-05 19:34:50 +0000302int ac;
303char **ag;
304{
305
Wanlong Gao354ebb42012-12-07 10:10:04 +0800306 int size = 1234;
307 char *buffer;
308 int ret;
309 char *errmsg;
whrb973f2b2000-05-05 19:34:50 +0000310
Wanlong Gao354ebb42012-12-07 10:10:04 +0800311 if ((buffer = (char *)malloc(size)) == NULL) {
312 perror("malloc");
313 exit(2);
314 }
whrb973f2b2000-05-05 19:34:50 +0000315
Wanlong Gao354ebb42012-12-07 10:10:04 +0800316 datapidgen(-1, buffer, size, 3);
whrb973f2b2000-05-05 19:34:50 +0000317
318/***
319fwrite(buffer, size, 1, stdout);
320fwrite("\n", 1, 1, stdout);
321****/
322
Wanlong Gao354ebb42012-12-07 10:10:04 +0800323 printf("datapidgen(-1, buffer, size, 3)\n");
whrb973f2b2000-05-05 19:34:50 +0000324
Wanlong Gao354ebb42012-12-07 10:10:04 +0800325 ret = datapidchk(-1, buffer, size, 3, &errmsg);
326 printf("datapidchk(-1, buffer, %d, 3, &errmsg) returned %d %s\n",
327 size, ret, errmsg);
328 ret = datapidchk(-1, &buffer[1], size - 1, 4, &errmsg);
329 printf("datapidchk(-1, &buffer[1], %d, 4, &errmsg) returned %d %s\n",
330 size - 1, ret, errmsg);
whrb973f2b2000-05-05 19:34:50 +0000331
Wanlong Gao354ebb42012-12-07 10:10:04 +0800332 buffer[25] = 0x0;
333 buffer[26] = 0x0;
334 buffer[27] = 0x0;
335 buffer[28] = 0x0;
336 printf("changing char 25-28\n");
whrb973f2b2000-05-05 19:34:50 +0000337
Wanlong Gao354ebb42012-12-07 10:10:04 +0800338 ret = datapidchk(-1, &buffer[1], size - 1, 4, &errmsg);
339 printf("datapidchk(-1, &buffer[1], %d, 4, &errmsg) returned %d %s\n",
340 size - 1, ret, errmsg);
whrb973f2b2000-05-05 19:34:50 +0000341
Wanlong Gao354ebb42012-12-07 10:10:04 +0800342 printf("------------------------------------------\n");
whrb973f2b2000-05-05 19:34:50 +0000343
Wanlong Gao354ebb42012-12-07 10:10:04 +0800344 datapidgen(getpid(), buffer, size, 5);
whrb973f2b2000-05-05 19:34:50 +0000345
346/*******
347fwrite(buffer, size, 1, stdout);
348fwrite("\n", 1, 1, stdout);
349******/
350
Wanlong Gao354ebb42012-12-07 10:10:04 +0800351 printf("\ndatapidgen(getpid(), buffer, size, 5)\n");
whrb973f2b2000-05-05 19:34:50 +0000352
Wanlong Gao354ebb42012-12-07 10:10:04 +0800353 ret = datapidchk(getpid(), buffer, size, 5, &errmsg);
354 printf("datapidchk(getpid(), buffer, %d, 5, &errmsg) returned %d %s\n",
355 size, ret, errmsg);
whrb973f2b2000-05-05 19:34:50 +0000356
Wanlong Gao354ebb42012-12-07 10:10:04 +0800357 ret = datapidchk(getpid(), &buffer[1], size - 1, 6, &errmsg);
358 printf
359 ("datapidchk(getpid(), &buffer[1], %d, 6, &errmsg) returned %d %s\n",
360 size - 1, ret, errmsg);
whrb973f2b2000-05-05 19:34:50 +0000361
Wanlong Gao354ebb42012-12-07 10:10:04 +0800362 buffer[25] = 0x0;
363 printf("changing char 25\n");
whrb973f2b2000-05-05 19:34:50 +0000364
Wanlong Gao354ebb42012-12-07 10:10:04 +0800365 ret = datapidchk(getpid(), &buffer[1], size - 1, 6, &errmsg);
366 printf
367 ("datapidchk(getpid(), &buffer[1], %d, 6, &errmsg) returned %d %s\n",
368 size - 1, ret, errmsg);
whrb973f2b2000-05-05 19:34:50 +0000369
Wanlong Gao354ebb42012-12-07 10:10:04 +0800370 exit(0);
whrb973f2b2000-05-05 19:34:50 +0000371}
372
373#endif