1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 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 #ifndef lint 33 #if 0 34 static char sccsid[] = "@(#)display.c 8.1 (Berkeley) 6/6/93"; 35 #endif 36 #endif /* not lint */ 37 #include <sys/cdefs.h> 38 __FBSDID("$FreeBSD$"); 39 40 #include <sys/param.h> 41 #include <sys/capsicum.h> 42 #include <sys/stat.h> 43 44 #include <capsicum_helpers.h> 45 #include <ctype.h> 46 #include <err.h> 47 #include <errno.h> 48 #include <stdio.h> 49 #include <stdlib.h> 50 #include <string.h> 51 #include <unistd.h> 52 #include "hexdump.h" 53 54 enum _vflag vflag = FIRST; 55 56 static off_t address; /* address/offset in stream */ 57 static off_t eaddress; /* end address */ 58 59 static void print(PR *, u_char *); 60 61 void 62 display(void) 63 { 64 FS *fs; 65 FU *fu; 66 PR *pr; 67 int cnt; 68 u_char *bp; 69 off_t saveaddress; 70 u_char savech, *savebp; 71 72 savech = 0; 73 while ((bp = get())) 74 for (fs = fshead, savebp = bp, saveaddress = address; fs; 75 fs = fs->nextfs, bp = savebp, address = saveaddress) 76 for (fu = fs->nextfu; fu; fu = fu->nextfu) { 77 if (fu->flags&F_IGNORE) 78 break; 79 for (cnt = fu->reps; cnt; --cnt) 80 for (pr = fu->nextpr; pr; address += pr->bcnt, 81 bp += pr->bcnt, pr = pr->nextpr) { 82 if (eaddress && address >= eaddress && 83 !(pr->flags & (F_TEXT|F_BPAD))) 84 bpad(pr); 85 if (cnt == 1 && pr->nospace) { 86 savech = *pr->nospace; 87 *pr->nospace = '\0'; 88 } 89 print(pr, bp); 90 if (cnt == 1 && pr->nospace) 91 *pr->nospace = savech; 92 } 93 } 94 if (endfu) { 95 /* 96 * If eaddress not set, error or file size was multiple of 97 * blocksize, and no partial block ever found. 98 */ 99 if (!eaddress) { 100 if (!address) 101 return; 102 eaddress = address; 103 } 104 for (pr = endfu->nextpr; pr; pr = pr->nextpr) 105 switch(pr->flags) { 106 case F_ADDRESS: 107 (void)printf(pr->fmt, (quad_t)eaddress); 108 break; 109 case F_TEXT: 110 (void)printf("%s", pr->fmt); 111 break; 112 } 113 } 114 } 115 116 static void 117 print(PR *pr, u_char *bp) 118 { 119 long double ldbl; 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, eaddress ? eaddress - address : 138 blocksize - address % blocksize); 139 break; 140 case F_CHAR: 141 (void)printf(pr->fmt, *bp); 142 break; 143 case F_DBL: 144 switch(pr->bcnt) { 145 case 4: 146 bcopy(bp, &f4, sizeof(f4)); 147 (void)printf(pr->fmt, f4); 148 break; 149 case 8: 150 bcopy(bp, &f8, sizeof(f8)); 151 (void)printf(pr->fmt, f8); 152 break; 153 default: 154 if (pr->bcnt == sizeof(long double)) { 155 bcopy(bp, &ldbl, sizeof(ldbl)); 156 (void)printf(pr->fmt, ldbl); 157 } 158 break; 159 } 160 break; 161 case F_INT: 162 switch(pr->bcnt) { 163 case 1: 164 (void)printf(pr->fmt, (quad_t)(signed char)*bp); 165 break; 166 case 2: 167 bcopy(bp, &s2, sizeof(s2)); 168 (void)printf(pr->fmt, (quad_t)s2); 169 break; 170 case 4: 171 bcopy(bp, &s4, sizeof(s4)); 172 (void)printf(pr->fmt, (quad_t)s4); 173 break; 174 case 8: 175 bcopy(bp, &s8, sizeof(s8)); 176 (void)printf(pr->fmt, s8); 177 break; 178 } 179 break; 180 case F_P: 181 (void)printf(pr->fmt, isprint(*bp) ? *bp : '.'); 182 break; 183 case F_STR: 184 (void)printf(pr->fmt, (char *)bp); 185 break; 186 case F_TEXT: 187 (void)printf("%s", pr->fmt); 188 break; 189 case F_U: 190 conv_u(pr, bp); 191 break; 192 case F_UINT: 193 switch(pr->bcnt) { 194 case 1: 195 (void)printf(pr->fmt, (u_quad_t)*bp); 196 break; 197 case 2: 198 bcopy(bp, &u2, sizeof(u2)); 199 (void)printf(pr->fmt, (u_quad_t)u2); 200 break; 201 case 4: 202 bcopy(bp, &u4, sizeof(u4)); 203 (void)printf(pr->fmt, (u_quad_t)u4); 204 break; 205 case 8: 206 bcopy(bp, &u8, sizeof(u8)); 207 (void)printf(pr->fmt, u8); 208 break; 209 } 210 break; 211 } 212 } 213 214 void 215 bpad(PR *pr) 216 { 217 static char const *spec = " -0+#"; 218 char *p1, *p2; 219 220 /* 221 * Remove all conversion flags; '-' is the only one valid 222 * with %s, and it's not useful here. 223 */ 224 pr->flags = F_BPAD; 225 pr->cchar[0] = 's'; 226 pr->cchar[1] = '\0'; 227 for (p1 = pr->fmt; *p1 != '%'; ++p1); 228 for (p2 = ++p1; *p1 && strchr(spec, *p1); ++p1); 229 while ((*p2++ = *p1++)); 230 } 231 232 static char **_argv; 233 234 u_char * 235 get(void) 236 { 237 static int ateof = 1; 238 static u_char *curp, *savp; 239 int n; 240 int need, nread; 241 int valid_save = 0; 242 u_char *tmpp; 243 244 if (!curp) { 245 if ((curp = calloc(1, blocksize)) == NULL) 246 err(1, NULL); 247 if ((savp = calloc(1, blocksize)) == NULL) 248 err(1, NULL); 249 } else { 250 tmpp = curp; 251 curp = savp; 252 savp = tmpp; 253 address += blocksize; 254 valid_save = 1; 255 } 256 for (need = blocksize, nread = 0;;) { 257 /* 258 * if read the right number of bytes, or at EOF for one file, 259 * and no other files are available, zero-pad the rest of the 260 * block and set the end flag. 261 */ 262 if (!length || (ateof && !next((char **)NULL))) { 263 if (odmode && address < skip) 264 errx(1, "cannot skip past end of input"); 265 if (need == blocksize) 266 return((u_char *)NULL); 267 /* 268 * XXX bcmp() is not quite right in the presence 269 * of multibyte characters. 270 */ 271 if (vflag != ALL && 272 valid_save && 273 bcmp(curp, savp, nread) == 0) { 274 if (vflag != DUP) 275 (void)printf("*\n"); 276 return((u_char *)NULL); 277 } 278 bzero((char *)curp + nread, need); 279 eaddress = address + nread; 280 return(curp); 281 } 282 n = fread((char *)curp + nread, sizeof(u_char), 283 length == -1 ? need : MIN(length, need), stdin); 284 if (!n) { 285 if (ferror(stdin)) 286 warn("%s", _argv[-1]); 287 ateof = 1; 288 continue; 289 } 290 ateof = 0; 291 if (length != -1) 292 length -= n; 293 if (!(need -= n)) { 294 /* 295 * XXX bcmp() is not quite right in the presence 296 * of multibyte characters. 297 */ 298 if (vflag == ALL || vflag == FIRST || 299 valid_save == 0 || 300 bcmp(curp, savp, blocksize) != 0) { 301 if (vflag == DUP || vflag == FIRST) 302 vflag = WAIT; 303 return(curp); 304 } 305 if (vflag == WAIT) 306 (void)printf("*\n"); 307 vflag = DUP; 308 address += blocksize; 309 need = blocksize; 310 nread = 0; 311 } 312 else 313 nread += n; 314 } 315 } 316 317 size_t 318 peek(u_char *buf, size_t nbytes) 319 { 320 size_t n, nread; 321 int c; 322 323 if (length != -1 && nbytes > (unsigned int)length) 324 nbytes = length; 325 nread = 0; 326 while (nread < nbytes && (c = getchar()) != EOF) { 327 *buf++ = c; 328 nread++; 329 } 330 n = nread; 331 while (n-- > 0) { 332 c = *--buf; 333 ungetc(c, stdin); 334 } 335 return (nread); 336 } 337 338 int 339 next(char **argv) 340 { 341 static int done; 342 int statok; 343 344 if (argv) { 345 _argv = argv; 346 return(1); 347 } 348 for (;;) { 349 if (*_argv) { 350 done = 1; 351 if (!(freopen(*_argv, "r", stdin))) { 352 warn("%s", *_argv); 353 exitval = 1; 354 ++_argv; 355 continue; 356 } 357 statok = 1; 358 } else { 359 if (done++) 360 return(0); 361 statok = 0; 362 } 363 364 if (caph_limit_stream(fileno(stdin), CAPH_READ) < 0) 365 err(1, "unable to restrict %s", 366 statok ? *_argv : "stdin"); 367 368 /* 369 * We've opened our last input file; enter capsicum sandbox. 370 */ 371 if (statok == 0 || *(_argv + 1) == NULL) { 372 if (cap_enter() < 0 && errno != ENOSYS) 373 err(1, "unable to enter capability mode"); 374 } 375 376 if (skip) 377 doskip(statok ? *_argv : "stdin", statok); 378 if (*_argv) 379 ++_argv; 380 if (!skip) 381 return(1); 382 } 383 /* NOTREACHED */ 384 } 385 386 void 387 doskip(const char *fname, int statok) 388 { 389 int cnt; 390 struct stat sb; 391 392 if (statok) { 393 if (fstat(fileno(stdin), &sb)) 394 err(1, "%s", fname); 395 if (S_ISREG(sb.st_mode) && skip > sb.st_size) { 396 address += sb.st_size; 397 skip -= sb.st_size; 398 return; 399 } 400 } 401 if (statok && S_ISREG(sb.st_mode)) { 402 if (fseeko(stdin, skip, SEEK_SET)) 403 err(1, "%s", fname); 404 address += skip; 405 skip = 0; 406 } else { 407 for (cnt = 0; cnt < skip; ++cnt) 408 if (getchar() == EOF) 409 break; 410 address += cnt; 411 skip -= cnt; 412 } 413 } 414