1 /* $NetBSD: display.c,v 1.23 2016/03/04 02:46:19 dholland 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.23 2016/03/04 02:46:19 dholland 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 #include <util.h> 57 58 #include "hexdump.h" 59 60 enum _vflag vflag = FIRST; 61 62 static off_t address; /* address/offset in stream */ 63 static off_t eaddress; /* end address */ 64 65 static u_char *get(void); 66 static inline void print(PR *, u_char *); 67 68 void 69 display(void) 70 { 71 FS *fs; 72 FU *fu; 73 PR *pr; 74 int cnt; 75 u_char *bp; 76 off_t saveaddress; 77 u_char savech, *savebp; 78 79 savech = 0; 80 while ((bp = get()) != NULL) 81 for (fs = fshead, savebp = bp, saveaddress = address; fs; 82 fs = fs->nextfs, bp = savebp, address = saveaddress) 83 for (fu = fs->nextfu; fu; fu = fu->nextfu) { 84 if (fu->flags&F_IGNORE) 85 break; 86 for (cnt = fu->reps; cnt; --cnt) 87 for (pr = fu->nextpr; pr; address += pr->bcnt, 88 bp += pr->bcnt, pr = pr->nextpr) { 89 if (eaddress && address >= eaddress && 90 !(pr->flags & (F_TEXT|F_BPAD))) 91 bpad(pr); 92 if (cnt == 1 && pr->nospace) { 93 savech = *pr->nospace; 94 *pr->nospace = '\0'; 95 } 96 print(pr, bp); 97 if (cnt == 1 && pr->nospace) 98 *pr->nospace = savech; 99 } 100 } 101 if (endfu) { 102 /* 103 * If eaddress not set, error or file size was multiple of 104 * blocksize, and no partial block ever found. 105 */ 106 if (!eaddress) { 107 if (!address) 108 return; 109 eaddress = address; 110 } 111 for (pr = endfu->nextpr; pr; pr = pr->nextpr) 112 switch(pr->flags) { 113 case F_ADDRESS: 114 (void)printf(pr->fmt, (int64_t)eaddress); 115 break; 116 case F_TEXT: 117 (void)printf("%s", pr->fmt); 118 break; 119 } 120 } 121 } 122 123 static inline void 124 print(PR *pr, u_char *bp) 125 { 126 double f8; 127 float f4; 128 int16_t s2; 129 int32_t s4; 130 int64_t s8; 131 uint16_t u2; 132 uint32_t u4; 133 uint64_t u8; 134 135 switch(pr->flags) { 136 case F_ADDRESS: 137 (void)printf(pr->fmt, (int64_t)address); 138 break; 139 case F_BPAD: 140 (void)printf(pr->fmt, ""); 141 break; 142 case F_C: 143 conv_c(pr, bp); 144 break; 145 case F_CHAR: 146 (void)printf(pr->fmt, *bp); 147 break; 148 case F_DBL: 149 switch(pr->bcnt) { 150 case 4: 151 memmove(&f4, bp, sizeof(f4)); 152 (void)printf(pr->fmt, f4); 153 break; 154 case 8: 155 memmove(&f8, bp, sizeof(f8)); 156 (void)printf(pr->fmt, f8); 157 break; 158 } 159 break; 160 case F_INT: 161 switch(pr->bcnt) { 162 case 1: 163 (void)printf(pr->fmt, (int64_t)*bp); 164 break; 165 case 2: 166 memmove(&s2, bp, sizeof(s2)); 167 (void)printf(pr->fmt, (int64_t)s2); 168 break; 169 case 4: 170 memmove(&s4, bp, sizeof(s4)); 171 (void)printf(pr->fmt, (int64_t)s4); 172 break; 173 case 8: 174 memmove(&s8, bp, sizeof(s8)); 175 (void)printf(pr->fmt, (int64_t)s8); 176 break; 177 } 178 break; 179 case F_P: 180 (void)printf(pr->fmt, isprint(*bp) ? *bp : '.'); 181 break; 182 case F_STR: 183 (void)printf(pr->fmt, (char *)bp); 184 break; 185 case F_TEXT: 186 (void)printf("%s", pr->fmt); 187 break; 188 case F_U: 189 conv_u(pr, bp); 190 break; 191 case F_UINT: 192 switch(pr->bcnt) { 193 case 1: 194 (void)printf(pr->fmt, (uint64_t)*bp); 195 break; 196 case 2: 197 memmove(&u2, bp, sizeof(u2)); 198 (void)printf(pr->fmt, (uint64_t)u2); 199 break; 200 case 4: 201 memmove(&u4, bp, sizeof(u4)); 202 (void)printf(pr->fmt, (uint64_t)u4); 203 break; 204 case 8: 205 memmove(&u8, bp, sizeof(u8)); 206 (void)printf(pr->fmt, (uint64_t)u8); 207 break; 208 } 209 break; 210 } 211 } 212 213 void 214 bpad(PR *pr) 215 { 216 static const char *spec = " -0+#"; 217 char *p1, *p2; 218 219 /* 220 * Remove all conversion flags; '-' is the only one valid 221 * with %s, and it's not useful here. 222 */ 223 pr->flags = F_BPAD; 224 pr->cchar[0] = 's'; 225 pr->cchar[1] = '\0'; 226 for (p1 = pr->fmt; *p1 != '%'; ++p1); 227 for (p2 = ++p1; *p1 && strchr(spec, *p1); ++p1); 228 while ((*p2++ = *p1++) != '\0'); 229 } 230 231 static char **_argv; 232 233 static u_char * 234 get(void) 235 { 236 static int ateof = 1; 237 static u_char *curp, *savp; 238 int n; 239 int need, nread; 240 u_char *tmpp; 241 242 if (!curp) { 243 curp = ecalloc(blocksize, 1); 244 savp = ecalloc(blocksize, 1); 245 } else { 246 tmpp = curp; 247 curp = savp; 248 savp = tmpp; 249 address += blocksize; 250 } 251 for (need = blocksize, nread = 0;;) { 252 /* 253 * if read the right number of bytes, or at EOF for one file, 254 * and no other files are available, zero-pad the rest of the 255 * block and set the end flag. 256 */ 257 if (!length || (ateof && !next(NULL))) { 258 if (need == blocksize) 259 return(NULL); 260 if (!need && vflag != ALL && 261 !memcmp(curp, savp, nread)) { 262 if (vflag != DUP) 263 (void)printf("*\n"); 264 return(NULL); 265 } 266 memset((char *)curp + nread, 0, need); 267 eaddress = address + nread; 268 return(curp); 269 } 270 n = fread((char *)curp + nread, sizeof(u_char), 271 length == -1 ? need : MIN(length, need), stdin); 272 if (!n) { 273 if (ferror(stdin)) 274 warn("%s", _argv[-1]); 275 ateof = 1; 276 continue; 277 } 278 ateof = 0; 279 if (length != -1) 280 length -= n; 281 if (!(need -= n)) { 282 if (vflag == ALL || vflag == FIRST || 283 memcmp(curp, savp, blocksize)) { 284 if (vflag == DUP || vflag == FIRST) 285 vflag = WAIT; 286 return(curp); 287 } 288 if (vflag == WAIT) 289 (void)printf("*\n"); 290 vflag = DUP; 291 address += blocksize; 292 need = blocksize; 293 nread = 0; 294 } 295 else 296 nread += n; 297 } 298 } 299 300 int 301 next(char **argv) 302 { 303 static int done; 304 int statok; 305 306 if (argv) { 307 _argv = argv; 308 return(1); 309 } 310 for (;;) { 311 if (*_argv) { 312 if (!(freopen(*_argv, "r", stdin))) { 313 warn("%s", *_argv); 314 exitval = 1; 315 ++_argv; 316 continue; 317 } 318 statok = done = 1; 319 } else { 320 if (done++) 321 return(0); 322 statok = 0; 323 } 324 if (skip) 325 doskip(statok ? *_argv : "stdin", statok); 326 if (*_argv) 327 ++_argv; 328 if (!skip) 329 return(1); 330 } 331 /* NOTREACHED */ 332 } 333 334 void 335 doskip(const char *fname, int statok) 336 { 337 int cnt; 338 struct stat sb; 339 340 if (statok) { 341 if (fstat(fileno(stdin), &sb)) 342 err(1, "fstat %s", fname); 343 if (S_ISREG(sb.st_mode) && skip >= sb.st_size) { 344 address += sb.st_size; 345 skip -= sb.st_size; 346 return; 347 } 348 } else 349 sb.st_mode = S_IFIFO; 350 351 if (S_ISREG(sb.st_mode)) { 352 if (fseek(stdin, skip, SEEK_SET)) 353 err(1, "fseek %s", fname); 354 address += skip; 355 skip = 0; 356 } else { 357 for (cnt = 0; cnt < skip; ++cnt) 358 if (getchar() == EOF) 359 break; 360 address += cnt; 361 skip -= cnt; 362 } 363 } 364