1 /* $NetBSD: display.c,v 1.19 2006/01/04 01:30:21 perry Exp $ */ 2 3 /* 4 * Copyright (c) 1989, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #if HAVE_NBTOOL_CONFIG_H 33 #include "nbtool_config.h" 34 #endif 35 36 #include <sys/cdefs.h> 37 #if !defined(lint) 38 #if 0 39 static char sccsid[] = "@(#)display.c 8.1 (Berkeley) 6/6/93"; 40 #else 41 __RCSID("$NetBSD: display.c,v 1.19 2006/01/04 01:30:21 perry Exp $"); 42 #endif 43 #endif /* not lint */ 44 45 #include <sys/param.h> 46 #include <sys/stat.h> 47 48 #include <ctype.h> 49 #include <err.h> 50 #include <errno.h> 51 #include <inttypes.h> 52 #include <stdio.h> 53 #include <stdlib.h> 54 #include <string.h> 55 #include <unistd.h> 56 57 #include "hexdump.h" 58 59 enum _vflag vflag = FIRST; 60 61 static off_t address; /* address/offset in stream */ 62 static off_t eaddress; /* end address */ 63 64 static inline void print(PR *, u_char *); 65 66 void 67 display(void) 68 { 69 FS *fs; 70 FU *fu; 71 PR *pr; 72 int cnt; 73 u_char *bp; 74 off_t saveaddress; 75 u_char savech, *savebp; 76 77 savech = 0; 78 while ((bp = get()) != NULL) 79 for (fs = fshead, savebp = bp, saveaddress = address; fs; 80 fs = fs->nextfs, bp = savebp, address = saveaddress) 81 for (fu = fs->nextfu; fu; fu = fu->nextfu) { 82 if (fu->flags&F_IGNORE) 83 break; 84 for (cnt = fu->reps; cnt; --cnt) 85 for (pr = fu->nextpr; pr; address += pr->bcnt, 86 bp += pr->bcnt, pr = pr->nextpr) { 87 if (eaddress && address >= eaddress && 88 !(pr->flags & (F_TEXT|F_BPAD))) 89 bpad(pr); 90 if (cnt == 1 && pr->nospace) { 91 savech = *pr->nospace; 92 *pr->nospace = '\0'; 93 } 94 print(pr, bp); 95 if (cnt == 1 && pr->nospace) 96 *pr->nospace = savech; 97 } 98 } 99 if (endfu) { 100 /* 101 * If eaddress not set, error or file size was multiple of 102 * blocksize, and no partial block ever found. 103 */ 104 if (!eaddress) { 105 if (!address) 106 return; 107 eaddress = address; 108 } 109 for (pr = endfu->nextpr; pr; pr = pr->nextpr) 110 switch(pr->flags) { 111 case F_ADDRESS: 112 (void)printf(pr->fmt, (int64_t)eaddress); 113 break; 114 case F_TEXT: 115 (void)printf("%s", pr->fmt); 116 break; 117 } 118 } 119 } 120 121 static inline void 122 print(PR *pr, u_char *bp) 123 { 124 double f8; 125 float f4; 126 int16_t s2; 127 int32_t s4; 128 int64_t s8; 129 u_int16_t u2; 130 u_int32_t u4; 131 u_int64_t u8; 132 133 switch(pr->flags) { 134 case F_ADDRESS: 135 (void)printf(pr->fmt, (int64_t)address); 136 break; 137 case F_BPAD: 138 (void)printf(pr->fmt, ""); 139 break; 140 case F_C: 141 conv_c(pr, bp); 142 break; 143 case F_CHAR: 144 (void)printf(pr->fmt, *bp); 145 break; 146 case F_DBL: 147 switch(pr->bcnt) { 148 case 4: 149 memmove(&f4, bp, sizeof(f4)); 150 (void)printf(pr->fmt, f4); 151 break; 152 case 8: 153 memmove(&f8, bp, sizeof(f8)); 154 (void)printf(pr->fmt, f8); 155 break; 156 } 157 break; 158 case F_INT: 159 switch(pr->bcnt) { 160 case 1: 161 (void)printf(pr->fmt, (int64_t)*bp); 162 break; 163 case 2: 164 memmove(&s2, bp, sizeof(s2)); 165 (void)printf(pr->fmt, (int64_t)s2); 166 break; 167 case 4: 168 memmove(&s4, bp, sizeof(s4)); 169 (void)printf(pr->fmt, (int64_t)s4); 170 break; 171 case 8: 172 memmove(&s8, bp, sizeof(s8)); 173 (void)printf(pr->fmt, s8); 174 break; 175 } 176 break; 177 case F_P: 178 (void)printf(pr->fmt, isprint(*bp) ? *bp : '.'); 179 break; 180 case F_STR: 181 (void)printf(pr->fmt, (char *)bp); 182 break; 183 case F_TEXT: 184 (void)printf("%s", pr->fmt); 185 break; 186 case F_U: 187 conv_u(pr, bp); 188 break; 189 case F_UINT: 190 switch(pr->bcnt) { 191 case 1: 192 (void)printf(pr->fmt, (uint64_t)*bp); 193 break; 194 case 2: 195 memmove(&u2, bp, sizeof(u2)); 196 (void)printf(pr->fmt, (uint64_t)u2); 197 break; 198 case 4: 199 memmove(&u4, bp, sizeof(u4)); 200 (void)printf(pr->fmt, (uint64_t)u4); 201 break; 202 case 8: 203 memmove(&u8, bp, sizeof(u8)); 204 (void)printf(pr->fmt, u8); 205 break; 206 } 207 break; 208 } 209 } 210 211 void 212 bpad(PR *pr) 213 { 214 static const char *spec = " -0+#"; 215 char *p1, *p2; 216 217 /* 218 * Remove all conversion flags; '-' is the only one valid 219 * with %s, and it's not useful here. 220 */ 221 pr->flags = F_BPAD; 222 pr->cchar[0] = 's'; 223 pr->cchar[1] = '\0'; 224 for (p1 = pr->fmt; *p1 != '%'; ++p1); 225 for (p2 = ++p1; *p1 && strchr(spec, *p1); ++p1); 226 while ((*p2++ = *p1++) != '\0'); 227 } 228 229 static char **_argv; 230 231 u_char * 232 get(void) 233 { 234 static int ateof = 1; 235 static u_char *curp, *savp; 236 int n; 237 int need, nread; 238 u_char *tmpp; 239 240 if (!curp) { 241 curp = emalloc(blocksize); 242 savp = emalloc(blocksize); 243 } else { 244 tmpp = curp; 245 curp = savp; 246 savp = tmpp; 247 address += blocksize; 248 } 249 for (need = blocksize, nread = 0;;) { 250 /* 251 * if read the right number of bytes, or at EOF for one file, 252 * and no other files are available, zero-pad the rest of the 253 * block and set the end flag. 254 */ 255 if (!length || (ateof && !next(NULL))) { 256 if (need == blocksize) 257 return(NULL); 258 if (!need && vflag != ALL && 259 !memcmp(curp, savp, nread)) { 260 if (vflag != DUP) 261 (void)printf("*\n"); 262 return(NULL); 263 } 264 memset((char *)curp + nread, 0, need); 265 eaddress = address + nread; 266 return(curp); 267 } 268 n = fread((char *)curp + nread, sizeof(u_char), 269 length == -1 ? need : MIN(length, need), stdin); 270 if (!n) { 271 if (ferror(stdin)) 272 warn("%s", _argv[-1]); 273 ateof = 1; 274 continue; 275 } 276 ateof = 0; 277 if (length != -1) 278 length -= n; 279 if (!(need -= n)) { 280 if (vflag == ALL || vflag == FIRST || 281 memcmp(curp, savp, blocksize)) { 282 if (vflag == DUP || vflag == FIRST) 283 vflag = WAIT; 284 return(curp); 285 } 286 if (vflag == WAIT) 287 (void)printf("*\n"); 288 vflag = DUP; 289 address += blocksize; 290 need = blocksize; 291 nread = 0; 292 } 293 else 294 nread += n; 295 } 296 } 297 298 int 299 next(char **argv) 300 { 301 static int done; 302 int statok; 303 304 if (argv) { 305 _argv = argv; 306 return(1); 307 } 308 for (;;) { 309 if (*_argv) { 310 if (!(freopen(*_argv, "r", stdin))) { 311 warn("%s", *_argv); 312 exitval = 1; 313 ++_argv; 314 continue; 315 } 316 statok = done = 1; 317 } else { 318 if (done++) 319 return(0); 320 statok = 0; 321 } 322 if (skip) 323 doskip(statok ? *_argv : "stdin", statok); 324 if (*_argv) 325 ++_argv; 326 if (!skip) 327 return(1); 328 } 329 /* NOTREACHED */ 330 } 331 332 void 333 doskip(const char *fname, int statok) 334 { 335 int cnt; 336 struct stat sb; 337 338 if (statok) { 339 if (fstat(fileno(stdin), &sb)) 340 err(1, "fstat %s", fname); 341 if (S_ISREG(sb.st_mode) && skip >= sb.st_size) { 342 address += sb.st_size; 343 skip -= sb.st_size; 344 return; 345 } 346 } 347 if (S_ISREG(sb.st_mode)) { 348 if (fseek(stdin, skip, SEEK_SET)) 349 err(1, "fseek %s", fname); 350 address += skip; 351 skip = 0; 352 } else { 353 for (cnt = 0; cnt < skip; ++cnt) 354 if (getchar() == EOF) 355 break; 356 address += cnt; 357 skip -= cnt; 358 } 359 } 360 361 void * 362 emalloc(int allocsize) 363 { 364 void *p; 365 366 if ((p = malloc((u_int)allocsize)) == NULL) 367 nomem(); 368 memset(p, 0, allocsize); 369 return(p); 370 } 371 372 void 373 nomem(void) 374 { 375 err(1, NULL); 376 } 377