// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/gzip.c
// Function: do_gzip_read_member
static int do_gzip_read_member(deark *c, lctx *d, i64 pos1, i64 *member_size)
{
	u8 b0, b1;
	i64 pos;
	i64 n;
	i64 foundpos;
	i64 string_len;
	i64 cmpr_data_len;
	i64 mod_time_unix;
	u32 crc_calculated;
	de_ucstring *member_name = NULL;
	int saved_indent_level;
	int ret;
	struct member_data *md = NULL;
	int retval = 0;

	md = de_malloc(c, sizeof(struct member_data));

	de_dbg_indent_save(c, &saved_indent_level);

	de_dbg(c, "gzip member at %d", (int)pos1);
	de_dbg_indent(c, 1);
	pos = pos1;

	b0 = de_getbyte(pos+0);
	b1 = de_getbyte(pos+1);
	if(b0!=0x1f || b1!=0x8b) {
		de_err(c, "Invalid gzip signature at %d. This is not a valid gzip file.",
			(int)pos1);
		goto done;
	}

	md->cmpr_code = de_getbyte(pos+2);
	if(md->cmpr_code!=0x08) {
		de_err(c, "Unsupported compression type (%d)", (int)md->cmpr_code);
		goto done;
	}

	md->flags = de_getbyte(pos+3);
	de_dbg(c, "flags: 0x%02x", (unsigned int)md->flags);
	pos += 4;

	mod_time_unix = de_getu32le(pos);
	de_unix_time_to_timestamp(mod_time_unix, &md->mod_time_ts, 0x1);
	if(md->mod_time_ts.is_valid) {
		char timestamp_buf[64];
		de_timestamp_to_string(&md->mod_time_ts, timestamp_buf, sizeof(timestamp_buf), 0);
		de_dbg(c, "mod time: %" I64_FMT " (%s)", mod_time_unix, timestamp_buf);
	}
	pos += 4;

	b0 = de_getbyte(pos++);
	de_dbg(c, "extra flags: 0x%02x", (unsigned int)b0);

	b0 = de_getbyte(pos++);
	de_dbg(c, "OS or filesystem: %d (%s)", (int)b0, get_os_name(b0));

	if(md->flags & GZIPFLAG_FEXTRA) {
		n = de_getu16le(pos); // XLEN
		// TODO: It might be interesting to dissect these extra fields, but it's
		// hard to find even a single file that uses them.
		de_dbg(c, "[extra fields at %d, dpos=%d, dlen=%d]",
			(int)pos, (int)(pos+2), (int)n);
		pos += 2;
		pos += n;
	}

	if(md->flags & GZIPFLAG_FNAME) {
		ret =  dbuf_search_byte(c->infile, 0x00, pos, c->infile->len - pos,
			&foundpos);
		if(!ret) {
			de_err(c, "Invalid NAME field");
			goto done;
		}

		string_len = foundpos - pos;

		member_name = ucstring_create(c);
#define DE_GZIP_MAX_FNLEN 300
		dbuf_read_to_ucstring_n(c->infile, pos, string_len, DE_GZIP_MAX_FNLEN,
			member_name, 0, DE_ENCODING_LATIN1);
		de_dbg(c, "file name at %d, len=%d: \"%s\"", (int)pos, (int)string_len,
			ucstring_getpsz_d(member_name));
		pos = foundpos + 1;
	}

	if(md->flags & GZIPFLAG_FCOMMENT) {
		ret =  dbuf_search_byte(c->infile, 0x00, pos, c->infile->len - pos,
			&foundpos);
		if(!ret) {
			de_err(c, "Invalid COMMENT field");
			goto done;
		}
		pos = foundpos + 1;
	}

	if(md->flags & GZIPFLAG_FHCRC) {
		md->crc16_reported = (u32)de_getu16le(pos);
		de_dbg(c, "crc16 (reported): 0x%04x", (unsigned int)md->crc16_reported);
		pos += 2;
	}

	de_dbg(c, "compressed blocks at %d", (int)pos);

	if(!d->output_file) {
		// Although any member can have a name and mod time, this metadata
		// is ignored for members after the first one.
		de_finfo *fi = NULL;

		fi = de_finfo_create(c);

		if(member_name && c->filenames_from_file) {
			de_finfo_set_name_from_ucstring(c, fi, member_name, 0);
			fi->original_filename_flag = 1;
		}

		if(md->mod_time_ts.is_valid) {
			fi->timestamp[DE_TIMESTAMPIDX_MODIFY] = md->mod_time_ts;
		}

		d->output_file = dbuf_create_output_file(c, member_name?NULL:"bin", fi, 0);
		dbuf_enable_wbuffer(d->output_file);

		de_finfo_destroy(c, fi);
	}

	dbuf_set_writelistener(d->output_file, de_writelistener_for_crc, (void*)d->crco);
	de_crcobj_reset(d->crco);

	ret = fmtutil_decompress_deflate(c->infile, pos, c->infile->len - pos, d->output_file,
		0, &cmpr_data_len, 0);
	dbuf_flush(d->output_file);

	crc_calculated = de_crcobj_getval(d->crco);
	dbuf_set_writelistener(d->output_file, NULL, NULL);

	if(!ret) goto done;
	pos += cmpr_data_len;

	de_dbg(c, "crc32 (calculated): 0x%08x", (unsigned int)crc_calculated);

	md->crc32_reported = (u32)de_getu32le(pos);
	de_dbg(c, "crc32 (reported)  : 0x%08x", (unsigned int)md->crc32_reported);
	pos += 4;

	if(crc_calculated != md->crc32_reported) {
		de_warn(c, "CRC check failed: Expected 0x%08x, got 0x%08x",
			(unsigned int)md->crc32_reported, (unsigned int)crc_calculated);
	}

	md->isize = de_getu32le(pos);
	de_dbg(c, "uncompressed size (mod 2^32): %u", (unsigned int)md->isize);
	pos += 4;

	retval = 1;

done:
	if(retval)
		*member_size = pos - pos1;
	else
		*member_size = 0;
	ucstring_destroy(member_name);
	de_free(c, md);
	de_dbg_indent_restore(c, saved_indent_level);
	return retval;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xf0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq $0x0, -0x68(%rbp)
movq $0x0, -0x78(%rbp)
movl $0x0, -0x7c(%rbp)
movq -0x8(%rbp), %rdi
movl $0x28, %esi
callq 0x15b780
movq %rax, -0x78(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x6c(%rbp), %rsi
callq 0x15b580
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movl %eax, %edx
leaq 0x134d99(%rip), %rsi    # 0x1ac1cb
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x18(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x30(%rbp), %rsi
addq $0x0, %rsi
callq 0x165060
movb %al, -0x21(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x30(%rbp), %rsi
addq $0x1, %rsi
callq 0x165060
movb %al, -0x22(%rbp)
movzbl -0x21(%rbp), %eax
cmpl $0x1f, %eax
jne 0x77493
movzbl -0x22(%rbp), %eax
cmpl $0x8b, %eax
je 0x774b0
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movl %eax, %edx
leaq 0x134d39(%rip), %rsi    # 0x1ac1dd
movb $0x0, %al
callq 0x15bfb0
jmp 0x77aaa
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x30(%rbp), %rsi
addq $0x2, %rsi
callq 0x165060
movb %al, %cl
movq -0x78(%rbp), %rax
movb %cl, 0x1(%rax)
movq -0x78(%rbp), %rax
movzbl 0x1(%rax), %eax
cmpl $0x8, %eax
je 0x774fa
movq -0x8(%rbp), %rdi
movq -0x78(%rbp), %rax
movzbl 0x1(%rax), %edx
leaq 0x134d2c(%rip), %rsi    # 0x1ac21a
movb $0x0, %al
callq 0x15bfb0
jmp 0x77aaa
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x30(%rbp), %rsi
addq $0x3, %rsi
callq 0x165060
movb %al, %cl
movq -0x78(%rbp), %rax
movb %cl, (%rax)
movq -0x8(%rbp), %rdi
movq -0x78(%rbp), %rax
movzbl (%rax), %edx
leaq 0x1445f0(%rip), %rsi    # 0x1bbb19
movb $0x0, %al
callq 0x15b0d0
movq -0x30(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x30(%rbp), %rsi
callq 0x165540
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rdi
movq -0x78(%rbp), %rsi
addq $0x18, %rsi
movl $0x1, %edx
callq 0x15d530
movq -0x78(%rbp), %rax
cmpb $0x0, 0x18(%rax)
je 0x775a9
movq -0x78(%rbp), %rdi
addq $0x18, %rdi
leaq -0xc0(%rbp), %rsi
movl $0x40, %edx
xorl %ecx, %ecx
callq 0x15e130
movq -0x8(%rbp), %rdi
movq -0x58(%rbp), %rdx
leaq -0xc0(%rbp), %rcx
leaq 0x126b2a(%rip), %rsi    # 0x19e0cc
movb $0x0, %al
callq 0x15b0d0
movq -0x30(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x30(%rbp), %rsi
movq %rsi, %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
callq 0x165060
movb %al, -0x21(%rbp)
movq -0x8(%rbp), %rdi
movzbl -0x21(%rbp), %edx
leaq 0x134c59(%rip), %rsi    # 0x1ac23c
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x30(%rbp), %rsi
movq %rsi, %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
callq 0x165060
movb %al, -0x21(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0xd8(%rbp)
movzbl -0x21(%rbp), %eax
movl %eax, -0xcc(%rbp)
movzbl -0x21(%rbp), %edi
callq 0x77b00
movq -0xd8(%rbp), %rdi
movl -0xcc(%rbp), %edx
movq %rax, %rcx
leaq 0x134c12(%rip), %rsi    # 0x1ac250
movb $0x0, %al
callq 0x15b0d0
movq -0x78(%rbp), %rax
movzbl (%rax), %eax
andl $0x4, %eax
cmpl $0x0, %eax
je 0x776aa
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x30(%rbp), %rsi
callq 0x165280
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rax
movl %eax, %edx
movq -0x30(%rbp), %rax
addq $0x2, %rax
movl %eax, %ecx
movq -0x38(%rbp), %rax
movl %eax, %r8d
leaq 0x134bdf(%rip), %rsi    # 0x1ac26a
movb $0x0, %al
callq 0x15b0d0
movq -0x30(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x30(%rbp)
movq -0x38(%rbp), %rax
addq -0x30(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x78(%rbp), %rax
movzbl (%rax), %eax
andl $0x8, %eax
cmpl $0x0, %eax
je 0x7779d
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x30(%rbp), %rdx
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %rcx
subq -0x30(%rbp), %rcx
xorl %esi, %esi
leaq -0x40(%rbp), %r8
callq 0x1667a0
movl %eax, -0x70(%rbp)
cmpl $0x0, -0x70(%rbp)
jne 0x77704
movq -0x8(%rbp), %rdi
leaq 0x134b99(%rip), %rsi    # 0x1ac291
movb $0x0, %al
callq 0x15bfb0
jmp 0x77aaa
movq -0x40(%rbp), %rax
subq -0x30(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x8(%rbp), %rdi
callq 0x1711d0
movq %rax, -0x68(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x30(%rbp), %rsi
movq -0x48(%rbp), %rdx
movq -0x68(%rbp), %r8
movl $0x12c, %ecx            # imm = 0x12C
xorl %r9d, %r9d
movl $0x5, (%rsp)
callq 0x166a40
movq -0x8(%rbp), %rax
movq %rax, -0xe8(%rbp)
movq -0x30(%rbp), %rax
movl %eax, -0xe0(%rbp)
movq -0x48(%rbp), %rax
movl %eax, -0xdc(%rbp)
movq -0x68(%rbp), %rdi
callq 0x172650
movq -0xe8(%rbp), %rdi
movl -0xe0(%rbp), %edx
movl -0xdc(%rbp), %ecx
movq %rax, %r8
leaq 0x134b1a(%rip), %rsi    # 0x1ac2a4
movb $0x0, %al
callq 0x15b0d0
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
movq -0x78(%rbp), %rax
movzbl (%rax), %eax
andl $0x10, %eax
cmpl $0x0, %eax
je 0x777ff
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x30(%rbp), %rdx
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %rcx
subq -0x30(%rbp), %rcx
xorl %esi, %esi
leaq -0x40(%rbp), %r8
callq 0x1667a0
movl %eax, -0x70(%rbp)
cmpl $0x0, -0x70(%rbp)
jne 0x777f3
movq -0x8(%rbp), %rdi
leaq 0x134adb(%rip), %rsi    # 0x1ac2c2
movb $0x0, %al
callq 0x15bfb0
jmp 0x77aaa
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
movq -0x78(%rbp), %rax
movzbl (%rax), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0x7784d
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x30(%rbp), %rsi
callq 0x165280
movl %eax, %ecx
movq -0x78(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x8(%rbp), %rdi
movq -0x78(%rbp), %rax
movl 0x4(%rax), %edx
leaq 0x134a9e(%rip), %rsi    # 0x1ac2d8
movb $0x0, %al
callq 0x15b0d0
movq -0x30(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rax
movl %eax, %edx
leaq 0x134a93(%rip), %rsi    # 0x1ac2f1
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
cmpq $0x0, (%rax)
jne 0x77938
movq $0x0, -0xc8(%rbp)
movq -0x8(%rbp), %rdi
callq 0x15d230
movq %rax, -0xc8(%rbp)
cmpq $0x0, -0x68(%rbp)
je 0x778c6
movq -0x8(%rbp), %rax
movzbl 0xd0(%rax), %eax
cmpl $0x0, %eax
je 0x778c6
movq -0x8(%rbp), %rdi
movq -0xc8(%rbp), %rsi
movq -0x68(%rbp), %rdx
xorl %ecx, %ecx
callq 0x15d2c0
movq -0xc8(%rbp), %rax
movb $0x1, 0x8(%rax)
movq -0x78(%rbp), %rax
cmpb $0x0, 0x18(%rax)
je 0x778eb
movq -0xc8(%rbp), %rax
movq -0x78(%rbp), %rcx
movq 0x18(%rcx), %rdx
movq %rdx, 0x18(%rax)
movq 0x20(%rcx), %rcx
movq %rcx, 0x20(%rax)
movq -0x8(%rbp), %rdi
movq -0x68(%rbp), %rcx
leaq 0x14990d(%rip), %rsi    # 0x1c1207
xorl %eax, %eax
cmpq $0x0, %rcx
cmovneq %rax, %rsi
movq -0xc8(%rbp), %rdx
xorl %ecx, %ecx
callq 0x166c60
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x167b90
movq -0x8(%rbp), %rdi
movq -0xc8(%rbp), %rsi
callq 0x15d260
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdx
leaq 0xf1982(%rip), %rsi     # 0x1692d0
callq 0x169280
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x15f250
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x30(%rbp), %rsi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %rdx
subq -0x30(%rbp), %rdx
movq -0x10(%rbp), %rax
movq (%rax), %rcx
xorl %eax, %eax
movl %eax, %r8d
leaq -0x50(%rbp), %r9
xorl %eax, %eax
movl $0x0, (%rsp)
callq 0x17f4f0
movl %eax, -0x70(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x167c30
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x15f340
movl %eax, -0x5c(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
xorl %eax, %eax
movl %eax, %edx
movq %rdx, %rsi
callq 0x169280
cmpl $0x0, -0x70(%rbp)
jne 0x779d7
jmp 0x77aaa
movq -0x50(%rbp), %rax
addq -0x30(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rdi
movl -0x5c(%rbp), %edx
leaq 0x134918(%rip), %rsi    # 0x1ac309
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x30(%rbp), %rsi
callq 0x165540
movl %eax, %ecx
movq -0x78(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x8(%rbp), %rdi
movq -0x78(%rbp), %rax
movl 0x8(%rax), %edx
leaq 0x134900(%rip), %rsi    # 0x1ac324
movb $0x0, %al
callq 0x15b0d0
movq -0x30(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x30(%rbp)
movl -0x5c(%rbp), %eax
movq -0x78(%rbp), %rcx
cmpl 0x8(%rcx), %eax
je 0x77a5f
movq -0x8(%rbp), %rdi
movq -0x78(%rbp), %rax
movl 0x8(%rax), %edx
movl -0x5c(%rbp), %ecx
leaq 0x15853f(%rip), %rsi    # 0x1cff97
movb $0x0, %al
callq 0x15bc10
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x30(%rbp), %rsi
callq 0x165540
movq %rax, %rcx
movq -0x78(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x8(%rbp), %rdi
movq -0x78(%rbp), %rax
movq 0x10(%rax), %rax
movl %eax, %edx
leaq 0x1348af(%rip), %rsi    # 0x1ac33f
movb $0x0, %al
callq 0x15b0d0
movq -0x30(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x30(%rbp)
movl $0x1, -0x7c(%rbp)
cmpl $0x0, -0x7c(%rbp)
je 0x77ac1
movq -0x30(%rbp), %rcx
subq -0x18(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, (%rax)
jmp 0x77acc
movq -0x20(%rbp), %rax
movq $0x0, (%rax)
movq -0x68(%rbp), %rdi
callq 0x171750
movq -0x8(%rbp), %rdi
movq -0x78(%rbp), %rsi
callq 0x15b820
movq -0x8(%rbp), %rdi
movl -0x6c(%rbp), %esi
callq 0x15b5a0
movl -0x7c(%rbp), %eax
addq $0xf0, %rsp
popq %rbp
retq
nopw (%rax,%rax)