whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved. |
| 3 | * |
| 4 | * 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. |
| 7 | * |
| 8 | * 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. |
| 11 | * |
| 12 | * 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. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License along |
| 20 | * with this program; if not, write the Free Software Foundation, Inc., 59 |
| 21 | * Temple Place - Suite 330, Boston MA 02111-1307, USA. |
| 22 | * |
| 23 | * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy, |
| 24 | * Mountain View, CA 94043, or: |
| 25 | * |
| 26 | * http://www.sgi.com |
| 27 | * |
| 28 | * For further information regarding this notice, see: |
| 29 | * |
| 30 | * http://oss.sgi.com/projects/GenInfo/NoticeExplan/ |
| 31 | */ |
| 32 | /************ |
| 33 | |
| 34 | 64 bits in a Cray word |
| 35 | |
| 36 | 12345678901234567890123456789012 |
| 37 | 1234567890123456789012345678901234567890123456789012345678901234 |
| 38 | ________________________________________________________________ |
| 39 | < pid >< word-offset in file (same #) >< pid > |
| 40 | |
| 41 | 1234567890123456789012345678901234567890123456789012345678901234 |
| 42 | ________________________________________________________________ |
| 43 | < pid >< offset in file of this word >< pid > |
| 44 | |
| 45 | |
| 46 | 8 bits to a bytes == character |
| 47 | NBPW 8 |
| 48 | ************/ |
| 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 | |
| 57 | static 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 | * |
| 75 | * |
| 76 | * 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 > |
| 80 | * |
| 81 | * 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 | ***********************************************************************/ |
| 85 | int |
| 86 | datapidgen(pid, buffer, bsize, offset) |
| 87 | int pid; |
| 88 | char *buffer; |
| 89 | int bsize; |
| 90 | int offset; |
| 91 | { |
| 92 | #if CRAY |
| 93 | |
| 94 | 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 | |
| 106 | if ( cnt=(offset % NBPW) ) { /* partial word */ |
| 107 | |
| 108 | woff = offset - cnt; |
| 109 | #if DEBUG |
| 110 | printf("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 | /* |
| 127 | * full words |
| 128 | */ |
| 129 | |
| 130 | num_full_words = (bsize-boff)/NBPW; |
| 131 | |
| 132 | woff = offset+boff; |
| 133 | |
| 134 | for (cnt=0; cnt<num_full_words; woff += NBPW, cnt++ ) { |
| 135 | |
| 136 | word = ((LOWER16BITS(pid) << 48) | (LOWER32BITS(woff) << 16) | LOWER16BITS(pid)); |
| 137 | |
| 138 | chr = (char *)&word; |
| 139 | for(tmp=0; tmp<NBPW; tmp++, chr++) { |
| 140 | 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 | |
| 154 | if ( cnt=((bsize-boff) % NBPW) ) { |
| 155 | #if DEBUG |
| 156 | printf("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 | |
| 174 | } |
| 175 | |
| 176 | /*********************************************************************** |
| 177 | * |
| 178 | * |
| 179 | ***********************************************************************/ |
| 180 | int |
| 181 | datapidchk(pid, buffer, bsize, offset, errmsg) |
| 182 | int pid; |
| 183 | char *buffer; |
| 184 | int bsize; |
| 185 | int offset; |
| 186 | char **errmsg; |
| 187 | { |
| 188 | #if CRAY |
| 189 | |
| 190 | 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 | |
| 200 | if ( errmsg != NULL ) { |
| 201 | *errmsg = Errmsg; |
| 202 | } |
| 203 | |
| 204 | |
| 205 | num_full_words = bsize/NBPW; |
| 206 | boff = 0; |
| 207 | |
| 208 | if ( cnt=(offset % NBPW) ) { /* partial word */ |
| 209 | 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 | /* |
| 228 | * full words |
| 229 | */ |
| 230 | |
| 231 | num_full_words = (bsize-boff)/NBPW; |
| 232 | |
| 233 | woff = offset+boff; |
| 234 | |
| 235 | for (cnt=0; cnt<num_full_words; woff += NBPW, cnt++ ) { |
| 236 | word = ((LOWER16BITS(pid) << 48) | (LOWER32BITS(woff) << 16) | LOWER16BITS(pid)); |
| 237 | |
| 238 | chr = (char *)&word; |
| 239 | for(tmp=0; tmp<NBPW; tmp++, boff++, chr++) { |
| 240 | if ( buffer[boff] != *chr ) { |
| 241 | 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]; |
| 249 | if ( *wptr != word ) { |
| 250 | 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 | |
| 262 | if ( cnt=((bsize-boff) % NBPW) ) { |
| 263 | #if DEBUG |
| 264 | printf("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++) { |
| 272 | if ( buffer[boff] != *chr ) { |
| 273 | 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 |
| 284 | |
| 285 | if ( errmsg != NULL ) { |
| 286 | *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 | ***********************************************************************/ |
| 301 | int |
| 302 | main(ac, ag) |
| 303 | int ac; |
| 304 | char **ag; |
| 305 | { |
| 306 | |
| 307 | int size=1234; |
| 308 | char *buffer; |
| 309 | int ret; |
| 310 | char *errmsg; |
| 311 | |
| 312 | if ((buffer=(char *)malloc(size)) == NULL ) { |
| 313 | perror("malloc"); |
| 314 | exit(2); |
| 315 | } |
| 316 | |
| 317 | |
| 318 | datapidgen(-1, buffer, size, 3); |
| 319 | |
| 320 | /*** |
| 321 | fwrite(buffer, size, 1, stdout); |
| 322 | fwrite("\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 | |
| 344 | printf("------------------------------------------\n"); |
| 345 | |
| 346 | datapidgen(getpid(), buffer, size, 5); |
| 347 | |
| 348 | /******* |
| 349 | fwrite(buffer, size, 1, stdout); |
| 350 | fwrite("\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 |
| 374 | |