Home | History | Annotate | Line # | Download | only in cd9660
      1 /*	$NetBSD: h_hexdump_r.c,v 1.1 2024/04/28 14:39:22 rillig Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2024 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code was contributed to The NetBSD Foundation by Roland Illig.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     20  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     21  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     28  * POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 /* Given the output from "hexdump -C", reconstruct the original file. */
     32 
     33 #include <err.h>
     34 #include <inttypes.h>
     35 #include <regex.h>
     36 #include <stdio.h>
     37 #include <stdlib.h>
     38 #include <string.h>
     39 
     40 #define	H	"[0-9a-f]"
     41 #define	HH	" (" H H ")"
     42 
     43 static off_t off, noff;
     44 static unsigned char prev_bytes[16], bytes[16], zeroes[16];
     45 
     46 int
     47 main(void)
     48 {
     49 	char line[81];
     50 	regex_t data_re, end_re;
     51 	regmatch_t m[18];
     52 
     53 	if (regcomp(&data_re, "^(" H "{8,9})"
     54 	    " " HH HH HH HH HH HH HH HH " " HH HH HH HH HH HH HH HH
     55 	    "  \\|.{16}\\|$", REG_EXTENDED) != 0)
     56 		err(1, "regcomp");
     57 	if (regcomp(&end_re, "^(" H "{8,9})$", REG_EXTENDED) != 0)
     58 		err(1, "regcomp");
     59 
     60 	while (fgets(line, sizeof(line), stdin) != NULL) {
     61 		line[strcspn(line, "\n")] = '\0';
     62 
     63 		if (strcmp(line, "*") == 0)
     64 			continue;
     65 
     66 		if (regexec(&data_re, line, 18, m, 0) == 0) {
     67 			noff = (off_t)strtoimax(line + m[1].rm_so, NULL, 16);
     68 			for (size_t i = 0; i < 16; i++)
     69 				bytes[i] = (unsigned char)strtoumax(
     70 				    line + m[2 + i].rm_so, NULL, 16);
     71 
     72 		} else if (regexec(&end_re, line, 2, m, 0) == 0) {
     73 			noff = (off_t)strtoimax(line + m[1].rm_so, NULL, 16);
     74 			if (off < noff) {
     75 				if (fseeko(stdout, noff - 16, SEEK_SET) != 0)
     76 					err(1, "fseeko");
     77 				if (fwrite(prev_bytes, 1, 16, stdout) != 16)
     78 					err(1, "fwrite");
     79 			}
     80 		} else
     81 			err(1, "invalid line '%s'", line);
     82 
     83 		if (memcmp(prev_bytes, zeroes, 16) != 0) {
     84 			while (off < noff) {
     85 				if (fwrite(prev_bytes, 1, 16, stdout) != 16)
     86 					err(1, "fwrite");
     87 				off += 16;
     88 			}
     89 			if (off != noff)
     90 				err(1, "off");
     91 		} else {
     92 			if (fseeko(stdout, noff, SEEK_SET) != 0)
     93 				err(1, "fseeko");
     94 			off = noff;
     95 		}
     96 
     97 		memcpy(prev_bytes, bytes, 16);
     98 	}
     99 	return 0;
    100 }
    101