blob: 9f34a6156326e9ed6392de64f4de32170ece5db9 [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
45
468 bits to a bytes == character
vapier45a8ba02009-07-20 10:59:32 +000047 NBPW 8
whrb973f2b2000-05-05 19:34:50 +000048************/
49
50#include <stdio.h>
51#include <sys/param.h>
52#ifdef UNIT_TEST
53#include <unistd.h>
54#include <stdlib.h>
55#endif
56
57static char Errmsg[80];
58
59#define LOWER16BITS(X) (X & 0177777)
60#define LOWER32BITS(X) (X & 0xffffffff)
61
62/***
63#define HIGHBITS(WRD, bits) ( (-1 << (64-bits)) & WRD)
64#define LOWBITS(WRD, bits) ( (-1 >> (64-bits)) & WRD)
65****/
66
67#define NBPBYTE 8 /* number bits per byte */
68
69#ifndef DEBUG
70#define DEBUG 0
71#endif
72
73/***********************************************************************
74 *
vapier45a8ba02009-07-20 10:59:32 +000075 *
whrb973f2b2000-05-05 19:34:50 +000076 * 1 2 3 4 5 6 7 8 9 10 11 12 13 14 14 15 bytes
77 * 1234567890123456789012345678901234567890123456789012345678901234 bits
78 * ________________________________________________________________ 1 word
79 * < pid >< offset in file of this word >< pid >
vapier45a8ba02009-07-20 10:59:32 +000080 *
whrb973f2b2000-05-05 19:34:50 +000081 * the words are put together where offset zero is the start.
82 * thus, offset 16 is the start of the second full word
83 * Thus, offset 8 is in middle of word 1
84 ***********************************************************************/
85int
86datapidgen(pid, buffer, bsize, offset)
87int pid;
88char *buffer;
89int bsize;
90int offset;
91{
92#if CRAY
vapier45a8ba02009-07-20 10:59:32 +000093
whrb973f2b2000-05-05 19:34:50 +000094 int cnt;
95 int tmp;
96 char *chr;
97 long *wptr;
98 long word;
99 int woff; /* file offset for the word */
100 int boff; /* buffer offset or index */
101 int num_full_words;
102
103 num_full_words = bsize/NBPW;
104 boff = 0;
105
Garrett Cooper903910d2010-11-23 09:27:44 -0800106 if (cnt=(offset % NBPW)) { /* partial word */
whrb973f2b2000-05-05 19:34:50 +0000107
108 woff = offset - cnt;
109#if DEBUG
110printf("partial at beginning, cnt = %d, woff = %d\n", cnt, woff);
111#endif
112
113 word = ((LOWER16BITS(pid) << 48) | (LOWER32BITS(woff) << 16) | LOWER16BITS(pid));
114
115 chr = (char *)&word;
116
117 for (tmp=0; tmp<cnt; tmp++) { /* skip unused bytes */
118 chr++;
119 }
120
121 for (; boff<(NBPW-cnt) && boff<bsize; boff++, chr++) {
122 buffer[boff] = *chr;
123 }
124 }
125
126 /*
vapier45a8ba02009-07-20 10:59:32 +0000127 * full words
whrb973f2b2000-05-05 19:34:50 +0000128 */
129
130 num_full_words = (bsize-boff)/NBPW;
vapier45a8ba02009-07-20 10:59:32 +0000131
whrb973f2b2000-05-05 19:34:50 +0000132 woff = offset+boff;
vapier45a8ba02009-07-20 10:59:32 +0000133
Garrett Cooper903910d2010-11-23 09:27:44 -0800134 for (cnt=0; cnt<num_full_words; woff += NBPW, cnt++) {
whrb973f2b2000-05-05 19:34:50 +0000135
136 word = ((LOWER16BITS(pid) << 48) | (LOWER32BITS(woff) << 16) | LOWER16BITS(pid));
137
138 chr = (char *)&word;
Garrett Cooper903910d2010-11-23 09:27:44 -0800139 for (tmp=0; tmp<NBPW; tmp++, chr++) {
whrb973f2b2000-05-05 19:34:50 +0000140 buffer[boff++] = *chr;
141 }
142/****** Only if wptr is a word ellined
143 wptr = (long *)&buffer[boff];
144 *wptr = word;
145 boff += NBPW;
146*****/
147
148 }
149
150 /*
151 * partial word at end of buffer
152 */
153
Garrett Cooper903910d2010-11-23 09:27:44 -0800154 if (cnt=((bsize-boff) % NBPW)) {
whrb973f2b2000-05-05 19:34:50 +0000155#if DEBUG
156printf("partial at end\n");
157#endif
158 word = ((LOWER16BITS(pid) << 48) | (LOWER32BITS(woff) << 16) | LOWER16BITS(pid));
159
160 chr = (char *)&word;
161
162 for (tmp=0; tmp<cnt && boff<bsize; tmp++, chr++) {
163 buffer[boff++] = *chr;
164 }
165 }
166
167 return bsize;
168
169#else
170 return -1; /* not support on non-64 bits word machines */
171
172#endif
173
vapier45a8ba02009-07-20 10:59:32 +0000174}
whrb973f2b2000-05-05 19:34:50 +0000175
176/***********************************************************************
177 *
178 *
179 ***********************************************************************/
180int
181datapidchk(pid, buffer, bsize, offset, errmsg)
182int pid;
183char *buffer;
184int bsize;
185int offset;
186char **errmsg;
187{
188#if CRAY
vapier45a8ba02009-07-20 10:59:32 +0000189
whrb973f2b2000-05-05 19:34:50 +0000190 int cnt;
191 int tmp;
192 char *chr;
193 long *wptr;
194 long word;
195 int woff; /* file offset for the word */
196 int boff; /* buffer offset or index */
197 int num_full_words;
198
199
Garrett Cooper903910d2010-11-23 09:27:44 -0800200 if (errmsg != NULL) {
whrb973f2b2000-05-05 19:34:50 +0000201 *errmsg = Errmsg;
202 }
203
204
205 num_full_words = bsize/NBPW;
206 boff = 0;
207
Garrett Cooper903910d2010-11-23 09:27:44 -0800208 if (cnt=(offset % NBPW)) { /* partial word */
whrb973f2b2000-05-05 19:34:50 +0000209 woff = offset - cnt;
210 word = ((LOWER16BITS(pid) << 48) | (LOWER32BITS(woff) << 16) | LOWER16BITS(pid));
211
212 chr = (char *)&word;
213
214 for (tmp=0; tmp<cnt; tmp++) { /* skip unused bytes */
215 chr++;
216 }
217
218 for (; boff<(NBPW-cnt) && boff<bsize; boff++, chr++) {
219 if (buffer[boff] != *chr) {
220 sprintf(Errmsg, "Data mismatch at offset %d, exp:%#o, act:%#o",
221 offset+boff, *chr, buffer[boff]);
222 return offset+boff;
223 }
224 }
225 }
226
227 /*
vapier45a8ba02009-07-20 10:59:32 +0000228 * full words
whrb973f2b2000-05-05 19:34:50 +0000229 */
230
231 num_full_words = (bsize-boff)/NBPW;
vapier45a8ba02009-07-20 10:59:32 +0000232
whrb973f2b2000-05-05 19:34:50 +0000233 woff = offset+boff;
vapier45a8ba02009-07-20 10:59:32 +0000234
Garrett Cooper903910d2010-11-23 09:27:44 -0800235 for (cnt=0; cnt<num_full_words; woff += NBPW, cnt++) {
whrb973f2b2000-05-05 19:34:50 +0000236 word = ((LOWER16BITS(pid) << 48) | (LOWER32BITS(woff) << 16) | LOWER16BITS(pid));
237
238 chr = (char *)&word;
Garrett Cooper903910d2010-11-23 09:27:44 -0800239 for (tmp=0; tmp<NBPW; tmp++, boff++, chr++) {
240 if (buffer[boff] != *chr) {
whrb973f2b2000-05-05 19:34:50 +0000241 sprintf(Errmsg, "Data mismatch at offset %d, exp:%#o, act:%#o",
242 woff, *chr, buffer[boff]);
243 return woff;
244 }
245 }
246
247/****** only if a word elined
248 wptr = (long *)&buffer[boff];
Garrett Cooper903910d2010-11-23 09:27:44 -0800249 if (*wptr != word) {
whrb973f2b2000-05-05 19:34:50 +0000250 sprintf(Errmsg, "Data mismatch at offset %d, exp:%#o, act:%#o",
251 woff, word, *wptr);
252 return woff;
253 }
254 boff += NBPW;
255******/
256 }
257
258 /*
259 * partial word at end of buffer
260 */
261
Garrett Cooper903910d2010-11-23 09:27:44 -0800262 if (cnt=((bsize-boff) % NBPW)) {
whrb973f2b2000-05-05 19:34:50 +0000263#if DEBUG
264printf("partial at end\n");
265#endif
266 word = ((LOWER16BITS(pid) << 48) | (LOWER32BITS(woff) << 16) | LOWER16BITS(pid));
267
268 chr = (char *)&word;
269
270
271 for (tmp=0; tmp<cnt && boff<bsize; boff++, tmp++, chr++) {
Garrett Cooper903910d2010-11-23 09:27:44 -0800272 if (buffer[boff] != *chr) {
whrb973f2b2000-05-05 19:34:50 +0000273 sprintf(Errmsg, "Data mismatch at offset %d, exp:%#o, act:%#o",
274 offset+boff, *chr, buffer[boff]);
275 return offset+boff;
276 }
277 }
278 }
279
280 sprintf(Errmsg, "all %d bytes match desired pattern", bsize);
281 return -1; /* buffer is ok */
282
283#else
vapier45a8ba02009-07-20 10:59:32 +0000284
Garrett Cooper903910d2010-11-23 09:27:44 -0800285 if (errmsg != NULL) {
whrb973f2b2000-05-05 19:34:50 +0000286 *errmsg = Errmsg;
287 }
288 sprintf(Errmsg, "Not supported on this OS.");
289 return 0;
290
291#endif
292
293
294} /* end of datapidchk */
295
296#if UNIT_TEST
297
298/***********************************************************************
299 * main for doing unit testing
300 ***********************************************************************/
301int
302main(ac, ag)
303int ac;
304char **ag;
305{
306
307int size=1234;
308char *buffer;
309int ret;
310char *errmsg;
311
Garrett Cooper903910d2010-11-23 09:27:44 -0800312 if ((buffer=(char *)malloc(size)) == NULL) {
whrb973f2b2000-05-05 19:34:50 +0000313 perror("malloc");
314 exit(2);
315 }
316
317
318 datapidgen(-1, buffer, size, 3);
319
320/***
321fwrite(buffer, size, 1, stdout);
322fwrite("\n", 1, 1, stdout);
323****/
324
325 printf("datapidgen(-1, buffer, size, 3)\n");
326
327 ret=datapidchk(-1, buffer, size, 3, &errmsg);
328 printf("datapidchk(-1, buffer, %d, 3, &errmsg) returned %d %s\n",
329 size, ret, errmsg);
330 ret=datapidchk(-1, &buffer[1], size-1, 4, &errmsg);
331 printf("datapidchk(-1, &buffer[1], %d, 4, &errmsg) returned %d %s\n",
332 size-1, ret, errmsg);
333
334 buffer[25]= 0x0;
335 buffer[26]= 0x0;
336 buffer[27]= 0x0;
337 buffer[28]= 0x0;
338 printf("changing char 25-28\n");
339
340 ret=datapidchk(-1, &buffer[1], size-1, 4, &errmsg);
341 printf("datapidchk(-1, &buffer[1], %d, 4, &errmsg) returned %d %s\n",
342 size-1, ret, errmsg);
343
344printf("------------------------------------------\n");
345
346 datapidgen(getpid(), buffer, size, 5);
347
348/*******
349fwrite(buffer, size, 1, stdout);
350fwrite("\n", 1, 1, stdout);
351******/
352
353 printf("\ndatapidgen(getpid(), buffer, size, 5)\n");
354
355 ret=datapidchk(getpid(), buffer, size, 5, &errmsg);
356 printf("datapidchk(getpid(), buffer, %d, 5, &errmsg) returned %d %s\n",
357 size, ret, errmsg);
358
359 ret=datapidchk(getpid(), &buffer[1], size-1, 6, &errmsg);
360 printf("datapidchk(getpid(), &buffer[1], %d, 6, &errmsg) returned %d %s\n",
361 size-1, ret, errmsg);
362
363 buffer[25]= 0x0;
364 printf("changing char 25\n");
365
366 ret=datapidchk(getpid(), &buffer[1], size-1, 6, &errmsg);
367 printf("datapidchk(getpid(), &buffer[1], %d, 6, &errmsg) returned %d %s\n",
368 size-1, ret, errmsg);
369
370 exit(0);
371}
372
373#endif