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