aboutsummaryrefslogtreecommitdiff
path: root/analyze_gz.c
diff options
context:
space:
mode:
Diffstat (limited to 'analyze_gz.c')
-rw-r--r--analyze_gz.c88
1 files changed, 88 insertions, 0 deletions
diff --git a/analyze_gz.c b/analyze_gz.c
new file mode 100644
index 0000000..1ea0d78
--- /dev/null
+++ b/analyze_gz.c
@@ -0,0 +1,88 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+
+#include "hexdump_pretty.h"
+
+#include "gzip.h"
+
+int main(int argc, char *argv[]) {
+ if (argc == 1) {
+ fprintf(stderr, "Usage: %s <filename.gz>\n", argv[0]);
+ return 1;
+ }
+
+ FILE *f = fopen(argv[1], "rb");
+ size_t len;
+
+ struct member header;
+ uint8_t *extra_fields = NULL;
+ uint16_t crc16 = 0;
+ char *filename = NULL;
+ char *file_comment = NULL;
+
+ len = fread(&header, sizeof(header), 1, f);
+
+ if (len != 1) {
+ fprintf(stderr, "Unexpected end of header\n");
+ return 1;
+ }
+
+ write_chunk_s((uint8_t[2]) {header.id1, header.id2}, 2);
+ write_chunk_s(&header.cm, 1);
+ write_chunk_s(&header.flg, 1);
+ {
+ size_t s = sizeof(header.mtime);
+ uint8_t *buf = malloc(s);
+ memcpy(buf, &header.mtime, s);
+ write_chunk(buf, s);
+ }
+ write_chunk_s(&header.xfl, 1);
+ write_chunk_s(&header.os, 1);
+
+ if (header.flg & FEXTRA) {
+ uint16_t xlen;
+ len = fread(&xlen, sizeof(xlen), 1, f);
+ {
+ uint8_t *buf = malloc(len);
+ memcpy(buf, &xlen, len);
+ write_chunk(buf, len);
+ }
+ extra_fields = malloc(xlen);
+ len = fread(extra_fields, 1, xlen, f);
+ write_chunk(extra_fields, xlen);
+ }
+
+ if (header.flg & FNAME) {
+ size_t n;
+ len = getdelim(&filename, &n, '\0', f);
+ write_chunk((uint8_t*) filename, n);
+ }
+
+ if (header.flg & FCOMMENT) {
+ size_t n;
+ len = getdelim(&file_comment, &n, '\0', f);
+ write_chunk((uint8_t*) file_comment, n);
+ }
+
+ if (header.flg & FHCRC) {
+ len = fread(&crc16, sizeof(crc16), 1, f);
+ {
+ uint8_t *buf = malloc(len);
+ memcpy(buf, &crc16, len);
+ write_chunk(buf, len);
+ }
+ }
+
+// cleanup:
+ if (extra_fields) free(extra_fields);
+ if (filename) free(filename);
+ if (file_comment) free(file_comment);
+ flush_line();
+ cleanup_line();
+
+ printf("\n");
+
+
+}