// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/cab.c
// Function: do_CFHEADER
static int do_CFHEADER(deark *c, lctx *d)
{
	int retval = 0;
	i64 pos = 0;
	de_ucstring *flags_str = NULL;
	struct de_stringreaderdata *CabinetPrev = NULL;
	struct de_stringreaderdata *DiskPrev = NULL;
	struct de_stringreaderdata *CabinetNext = NULL;
	struct de_stringreaderdata *DiskNext = NULL;
	int saved_indent_level;

	de_dbg_indent_save(c, &saved_indent_level);
	de_dbg(c, "CFHEADER at %d", (int)pos);
	de_dbg_indent(c, 1);
	pos += 8; // signature, reserved1
	d->cbCabinet = de_getu32le_p(&pos);
	de_dbg(c, "cbCabinet: %"I64_FMT, d->cbCabinet);
	pos += 4; // reserved2
	d->coffFiles = de_getu32le_p(&pos);
	de_dbg(c, "coffFiles: %"I64_FMT, d->coffFiles);
	pos += 4; // reserved3
	d->versionMinor = de_getbyte_p(&pos);
	d->versionMajor = de_getbyte_p(&pos);
	de_dbg(c, "file format version: %u.%u", (unsigned int)d->versionMajor,
		(unsigned int)d->versionMinor);

	d->cFolders = de_getu16le_p(&pos);
	de_dbg(c, "cFolders: %d", (int)d->cFolders);

	d->cFiles = de_getu16le_p(&pos);
	de_dbg(c, "cFiles: %d", (int)d->cFiles);

	d->header_flags = (unsigned int)de_getu16le_p(&pos);
	flags_str = ucstring_create(c);
	// The specification has a diagram showing that PREV_CABINET is 0x2,
	// NEXT_CABINET is 0x04, etc. But the text below it says that PREV_CABINET
	// is 0x1, NEXT_CABINET is 0x02, etc. I'm sure it's the text that's correct.
	if(d->header_flags&0x0001) ucstring_append_flags_item(flags_str, "PREV_CABINET");
	if(d->header_flags&0x0002) ucstring_append_flags_item(flags_str, "NEXT_CABINET");
	if(d->header_flags&0x0004) ucstring_append_flags_item(flags_str, "RESERVE_PRESENT");
	de_dbg(c, "flags: 0x%04x (%s)", d->header_flags, ucstring_getpsz(flags_str));

	pos += 2; // setID (arbitrary ID for a collection of linked cab files)
	pos += 2; // iCabinet (sequence number in a mult-cab file)

	if(d->header_flags&0x0004) { // RESERVE_PRESENT
		d->cbCFHeader = de_getu16le_p(&pos);
		de_dbg(c, "cbCFHeader: %d", (int)d->cbCFHeader);
		d->cbCFFolder = (i64)de_getbyte_p(&pos);
		de_dbg(c, "cbCFFolder: %d", (int)d->cbCFFolder);
		d->cbCFData = (i64)de_getbyte_p(&pos);
		de_dbg(c, "cbCFData: %d", (int)d->cbCFData);

		if(d->cbCFHeader!=0) {
			de_dbg(c, "[%d bytes of abReserve data at %d]", (int)d->cbCFHeader,
				(int)pos);
			de_dbg_indent(c, 1);
			de_dbg_hexdump(c, c->infile, pos, d->cbCFHeader, 256, NULL, 0x1);
			de_dbg_indent(c, -1);
			pos += d->cbCFHeader;
		}
	}

	if(d->header_flags&0x0001) { // PREV_CABINET
		CabinetPrev = dbuf_read_string(c->infile, pos, 256, 256,
			DE_CONVFLAG_STOP_AT_NUL, DE_ENCODING_ASCII);
		de_dbg(c, "szCabinetPrev: \"%s\"", ucstring_getpsz(CabinetPrev->str));
		if(!CabinetPrev->found_nul) goto done;
		pos += CabinetPrev->bytes_consumed;

		DiskPrev = dbuf_read_string(c->infile, pos, 256, 256,
			DE_CONVFLAG_STOP_AT_NUL, DE_ENCODING_ASCII);
		de_dbg(c, "szDiskPrev: \"%s\"", ucstring_getpsz(DiskPrev->str));
		if(!DiskPrev->found_nul) goto done;
		pos += DiskPrev->bytes_consumed;
	}

	if(d->header_flags&0x0002) { // NEXT_CABINET
		CabinetNext = dbuf_read_string(c->infile, pos, 256, 256,
			DE_CONVFLAG_STOP_AT_NUL, DE_ENCODING_ASCII);
		de_dbg(c, "szCabinetNext: \"%s\"", ucstring_getpsz(CabinetNext->str));
		if(!CabinetNext->found_nul) goto done;
		pos += CabinetNext->bytes_consumed;

		DiskNext = dbuf_read_string(c->infile, pos, 256, 256,
			DE_CONVFLAG_STOP_AT_NUL, DE_ENCODING_ASCII);
		de_dbg(c, "szDiskNext: \"%s\"", ucstring_getpsz(DiskNext->str));
		if(!DiskNext->found_nul) goto done;
		pos += DiskNext->bytes_consumed;
	}

	// TODO: Additional fields may be here

	de_dbg_indent(c, -1);

	if(d->versionMajor!=1 || d->versionMinor!=3) {
		de_err(c, "Unsupported CAB format version: %u.%u",
			(unsigned int)d->versionMajor, (unsigned int)d->versionMinor);
		goto done;
	}

	d->CFHEADER_len = pos;
	retval = 1;
done:
	de_destroy_stringreaderdata(c, CabinetPrev);
	de_destroy_stringreaderdata(c, DiskPrev);
	de_destroy_stringreaderdata(c, CabinetNext);
	de_destroy_stringreaderdata(c, DiskNext);
	ucstring_destroy(flags_str);
	de_dbg_indent_restore(c, saved_indent_level);
	return retval;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x0, -0x14(%rbp)
movq $0x0, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
movq $0x0, -0x48(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x4c(%rbp), %rsi
callq 0x15b580
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rax
movl %eax, %edx
leaq 0x155e68(%rip), %rsi    # 0x1a4e60
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x20(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x165580
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdx
leaq 0x155e27(%rip), %rsi    # 0x1a4e6f
movb $0x0, %al
callq 0x15b0d0
movq -0x20(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x165580
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdx
leaq 0x155df4(%rip), %rsi    # 0x1a4e7e
movb $0x0, %al
callq 0x15b0d0
movq -0x20(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x165170
movb %al, %cl
movq -0x10(%rbp), %rax
movb %cl, (%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x165170
movb %al, %cl
movq -0x10(%rbp), %rax
movb %cl, 0x1(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movzbl 0x1(%rax), %edx
movq -0x10(%rbp), %rax
movzbl (%rax), %ecx
leaq 0x155da3(%rip), %rsi    # 0x1a4e8d
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x1652c0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movl %eax, %edx
leaq 0x155d86(%rip), %rsi    # 0x1a4ea8
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x1652c0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movl %eax, %edx
leaq 0x155d5b(%rip), %rsi    # 0x1a4eb5
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x1652c0
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x8(%rbp), %rdi
callq 0x1711d0
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movl 0x4(%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x4f1a7
movq -0x28(%rbp), %rdi
leaq 0x155d1e(%rip), %rsi    # 0x1a4ec0
callq 0x172680
movq -0x10(%rbp), %rax
movl 0x4(%rax), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0x4f1c6
movq -0x28(%rbp), %rdi
leaq 0x155d0c(%rip), %rsi    # 0x1a4ecd
callq 0x172680
movq -0x10(%rbp), %rax
movl 0x4(%rax), %eax
andl $0x4, %eax
cmpl $0x0, %eax
je 0x4f1e5
movq -0x28(%rbp), %rdi
leaq 0x155cfa(%rip), %rsi    # 0x1a4eda
callq 0x172680
movq -0x8(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x10(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x50(%rbp)
movq -0x28(%rbp), %rdi
callq 0x172500
movq -0x58(%rbp), %rdi
movl -0x50(%rbp), %edx
movq %rax, %rcx
leaq 0x181c5d(%rip), %rsi    # 0x1d0e6e
movb $0x0, %al
callq 0x15b0d0
movq -0x20(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rax
movl 0x4(%rax), %eax
andl $0x4, %eax
cmpl $0x0, %eax
je 0x4f379
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x1652c0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movl %eax, %edx
leaq 0x155c76(%rip), %rsi    # 0x1a4eea
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x165170
movzbl %al, %eax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movq %rcx, 0x30(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
movl %eax, %edx
leaq 0x155c4b(%rip), %rsi    # 0x1a4ef9
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x165170
movzbl %al, %eax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rax
movl %eax, %edx
leaq 0x155c20(%rip), %rsi    # 0x1a4f08
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
cmpq $0x0, 0x28(%rax)
je 0x4f377
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movl %eax, %edx
movq -0x20(%rbp), %rax
movl %eax, %ecx
leaq 0x155c00(%rip), %rsi    # 0x1a4f15
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x20(%rbp), %rdx
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rcx
movl $0x100, %r8d            # imm = 0x100
xorl %eax, %eax
movl %eax, %r9d
movl $0x1, (%rsp)
callq 0x15b840
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
addq -0x20(%rbp), %rax
movq %rax, -0x20(%rbp)
jmp 0x4f379
movq -0x10(%rbp), %rax
movl 0x4(%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x4f468
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x20(%rbp), %rsi
movl $0x100, %ecx            # imm = 0x100
movl $0x1, %r9d
movq %rcx, %rdx
movl %r9d, %r8d
callq 0x1664c0
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x172500
movq -0x60(%rbp), %rdi
movq %rax, %rdx
leaq 0x155b63(%rip), %rsi    # 0x1a4f38
movb $0x0, %al
callq 0x15b0d0
movq -0x30(%rbp), %rax
cmpl $0x0, 0x34(%rax)
jne 0x4f3eb
jmp 0x4f5b1
movq -0x30(%rbp), %rax
movq (%rax), %rax
addq -0x20(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x20(%rbp), %rsi
movl $0x100, %ecx            # imm = 0x100
movl $0x1, %r9d
movq %rcx, %rdx
movl %r9d, %r8d
callq 0x1664c0
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x38(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x172500
movq -0x68(%rbp), %rdi
movq %rax, %rdx
leaq 0x155b09(%rip), %rsi    # 0x1a4f4c
movb $0x0, %al
callq 0x15b0d0
movq -0x38(%rbp), %rax
cmpl $0x0, 0x34(%rax)
jne 0x4f459
jmp 0x4f5b1
movq -0x38(%rbp), %rax
movq (%rax), %rax
addq -0x20(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rax
movl 0x4(%rax), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0x4f554
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x20(%rbp), %rsi
movl $0x100, %ecx            # imm = 0x100
movl $0x1, %r9d
movq %rcx, %rdx
movl %r9d, %r8d
callq 0x1664c0
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x40(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x172500
movq -0x70(%rbp), %rdi
movq %rax, %rdx
leaq 0x155a99(%rip), %rsi    # 0x1a4f5d
movb $0x0, %al
callq 0x15b0d0
movq -0x40(%rbp), %rax
cmpl $0x0, 0x34(%rax)
jne 0x4f4da
jmp 0x4f5b1
movq -0x40(%rbp), %rax
movq (%rax), %rax
addq -0x20(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x20(%rbp), %rsi
movl $0x100, %ecx            # imm = 0x100
movl $0x1, %r9d
movq %rcx, %rdx
movl %r9d, %r8d
callq 0x1664c0
movq %rax, -0x48(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x48(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x172500
movq -0x78(%rbp), %rdi
movq %rax, %rdx
leaq 0x155a3f(%rip), %rsi    # 0x1a4f71
movb $0x0, %al
callq 0x15b0d0
movq -0x48(%rbp), %rax
cmpl $0x0, 0x34(%rax)
jne 0x4f545
jmp 0x4f5b1
movq -0x48(%rbp), %rax
movq (%rax), %rax
addq -0x20(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movq -0x10(%rbp), %rax
movzbl 0x1(%rax), %eax
cmpl $0x1, %eax
jne 0x4f57b
movq -0x10(%rbp), %rax
movzbl (%rax), %eax
cmpl $0x3, %eax
je 0x4f59e
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movzbl 0x1(%rax), %edx
movq -0x10(%rbp), %rax
movzbl (%rax), %ecx
leaq 0x1559ed(%rip), %rsi    # 0x1a4f82
movb $0x0, %al
callq 0x15bfb0
jmp 0x4f5b1
movq -0x20(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x40(%rax)
movl $0x1, -0x14(%rbp)
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x166830
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x166830
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x166830
movq -0x8(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x166830
movq -0x28(%rbp), %rdi
callq 0x171750
movq -0x8(%rbp), %rdi
movl -0x4c(%rbp), %esi
callq 0x15b5a0
movl -0x14(%rbp), %eax
addq $0x80, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)