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