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