dumb seeking (embedded) GZ data - randomcrap - random crap programs of varying quality
 (HTM) git clone git://git.codemadness.org/randomcrap
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit 08a7c0a44ec4915bb924f312b8cc126bc9b9ade2
 (DIR) parent d9ab73fea1755e1f53589be43f7c59e8a2820d5e
 (HTM) Author: Hiltjo Posthuma <hiltjo@codemadness.org>
       Date:   Fri,  5 Dec 2025 14:08:48 +0100
       
       dumb seeking (embedded) GZ data
       
       The output is dumb but shows some indication of interesting data.
       A brain is still required.
       
       Example use-case:
       
       - extract binary blobs from a self-extracting shellscript (makeself)
         (smartphone firmware blobs).
       - seek data in firmware dumps.
       
       Diffstat:
         A seekgz.c                            |      57 +++++++++++++++++++++++++++++++
       
       1 file changed, 57 insertions(+), 0 deletions(-)
       ---
 (DIR) diff --git a/seekgz.c b/seekgz.c
       @@ -0,0 +1,57 @@
       +/* seek a file for (embedded) GZ data, list them one by line:
       +   offset<TAB>length
       +
       +   For each line one can write the data segments:
       +
       +   dd skip=offsetB count=lengthB if=input of=output */
       +
       +#include <stdio.h>
       +
       +#define GETCHAR getchar_unlocked
       +
       +void
       +output(size_t o1, size_t o2)
       +{
       +        printf("%zu\t%zu", o1, o2 - o1);
       +        /* dd command for copy-pasta (I'm lazy) */
       +        if (o1)
       +                printf("\tdd skip=%zuB count=%zuB", o1, o2 - o1);
       +        else
       +                printf("\tdd count=%zuB", o2 - o1);
       +        printf("\n");
       +}
       +
       +int
       +main(void)
       +{
       +        size_t offset = 0, prevoffset = 0, len = 0;
       +        unsigned char tab[] = { 0x1f, 0x8b, 0x08 }; /* GZ magic (0x1f, 0x8b), 0x08 is deflate */
       +        int c, b = 0, tablen = 3, f = 0;
       +
       +        while ((c = GETCHAR()) != EOF) {
       +                len++;
       +                if (c == tab[b]) {
       +                        b++;
       +                        if (b == tablen) {
       +                                f = 1;
       +                                b = 0;
       +                                offset = len - tablen;
       +                                if (prevoffset)
       +                                        output(prevoffset, offset);
       +                                prevoffset = offset;
       +                        }
       +                } else {
       +                        b = 0;
       +                }
       +        }
       +
       +        if (ferror(stdin)) {
       +                perror(NULL);
       +                return 1;
       +        }
       +
       +        if (len > offset && f)
       +                output(offset, len);
       +
       +        return 0;
       +}