// File: /workspace/github_c_makefile_star2_O1/jsummers[P]deark/modules/rpm.c
// Function: do_header_structure
static int do_header_structure(deark *c, lctx *d, int is_sig, i64 pos1,
	i64 *section_size)
{
	i64 pos;
	i64 indexcount;
	i64 storesize;
	u8 buf[4];
	u8 header_ver;
	i64 i;
	i64 tag_id, tag_type, tag_offset, tag_count;
	i64 data_store_pos;
	const char *hdrname;
	int retval = 0;

	hdrname = is_sig?"sig":"hdr";
	pos = pos1;
	de_dbg(c, "%s section at %d", hdrname, (int)pos1);
	de_dbg_indent(c, 1);

	de_read(buf, pos, 4);
	if(buf[0]!=0x8e || buf[1]!=0xad || buf[2]!=0xe8) {
		de_err(c, "Bad header signature at %d", (int)pos);
		goto done;
	}
	header_ver = buf[3];
	if(header_ver != 1) {
		de_err(c, "Unsupported header version");
		goto done;
	}
	pos += 8;

	indexcount = de_getu32be(pos);
	storesize = de_getu32be(pos+4);
	de_dbg(c, "%s: pos=%d indexcount=%d storesize=%d", hdrname,
		(int)pos, (int)indexcount, (int)storesize);
	pos += 8;

	if(indexcount>1000) goto done;

	data_store_pos = pos + 16*indexcount;

	de_dbg(c, "%s: tag table at %d", hdrname, (int)pos);
	de_dbg_indent(c, 1);

	for(i=0; i<indexcount; i++) {
		tag_id = de_getu32be(pos);
		tag_type = de_getu32be(pos+4);
		tag_offset = de_getu32be(pos+8);
		tag_count = de_getu32be(pos+12);

		de_dbg2(c, "tag #%d type=%d offset=%d count=%d", (int)tag_id,
			(int)tag_type, (int)tag_offset, (int)tag_count);


		if(is_sig==0 && tag_id==DE_RPMTAG_PAYLOADCOMPRESSOR && tag_type==DE_RPM_STRING_TYPE) {
			read_compression_type(c, d, data_store_pos+tag_offset);
		}
		else if(is_sig==0 && tag_id==DE_RPMTAG_NAME && tag_type==DE_RPM_STRING_TYPE) {
			if(!d->name_srd) {
				d->name_srd = dbuf_read_string(c->infile, data_store_pos+tag_offset,
					DE_DBG_MAX_STRLEN, DE_DBG_MAX_STRLEN,
					DE_CONVFLAG_STOP_AT_NUL, DE_ENCODING_ASCII);
				de_dbg(c, "name: \"%s\"", ucstring_getpsz(d->name_srd->str));
			}
		}
		else if(is_sig==0 && tag_id==DE_RPMTAG_VERSION && tag_type==DE_RPM_STRING_TYPE) {
			if(!d->version_srd) {
				d->version_srd = dbuf_read_string(c->infile, data_store_pos+tag_offset,
					DE_DBG_MAX_STRLEN, DE_DBG_MAX_STRLEN,
					DE_CONVFLAG_STOP_AT_NUL, DE_ENCODING_ASCII);
				de_dbg(c, "version: \"%s\"", ucstring_getpsz(d->version_srd->str));
			}
		}
		else if(is_sig==0 && tag_id==DE_RPMTAG_RELEASE && tag_type==DE_RPM_STRING_TYPE) {
			if(!d->release_srd) {
				d->release_srd = dbuf_read_string(c->infile, data_store_pos+tag_offset,
					DE_DBG_MAX_STRLEN, DE_DBG_MAX_STRLEN,
					DE_CONVFLAG_STOP_AT_NUL, DE_ENCODING_ASCII);
				de_dbg(c, "release: \"%s\"", ucstring_getpsz(d->release_srd->str));
			}
		}

		pos += 16;
	}

	de_dbg_indent(c, -1);

	pos = data_store_pos;
	de_dbg(c, "%s: data store at %d", hdrname, (int)pos);
	pos += storesize;

	*section_size = pos - pos1;
	retval = 1;
done:
	de_dbg_indent(c, -1);
	return retval;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %r8, %r12
movq %rcx, %r15
movq %rsi, (%rsp)
movq %rdi, %rbp
movl %edx, 0x8(%rsp)
testl %edx, %edx
leaq 0x6bc1c(%rip), %rax     # 0x130572
leaq 0x75e78(%rip), %r13     # 0x13a7d5
cmoveq %rax, %r13
leaq 0x75e71(%rip), %rsi     # 0x13a7d9
movq %r13, %rdx
xorl %eax, %eax
callq 0xe9226
movq %rbp, %rdi
movl $0x1, %esi
callq 0xe9585
movq 0x18(%rbp), %rdi
leaq 0xc(%rsp), %r14
movl $0x4, %ecx
movq %r14, %rsi
movq %r15, %rdx
callq 0xeeee8
cmpb $-0x72, (%r14)
jne 0xc4d07
cmpb $-0x53, 0xd(%rsp)
jne 0xc4d07
cmpb $-0x18, 0xe(%rsp)
jne 0xc4d07
cmpb $0x1, 0xf(%rsp)
jne 0xc4d1f
leaq 0x8(%r15), %r14
movq 0x18(%rbp), %rdi
movq %r14, %rsi
callq 0xef32a
movq %rax, %rbx
movq 0x18(%rbp), %rdi
leaq 0xc(%r15), %rsi
callq 0xef32a
leaq 0x75e36(%rip), %rsi     # 0x13a820
movq %rbp, %rdi
movq %r13, %rdx
movl %r14d, %ecx
movq %rbx, %r14
xorl %ebx, %ebx
movl %r14d, %r8d
movq %rax, 0x30(%rsp)
movl %eax, %r9d
xorl %eax, %eax
callq 0xe9226
cmpq $0x3e8, %r14            # imm = 0x3E8
jg 0xc4d32
movq %r12, 0x20(%rsp)
movq %r13, %rdx
leaq 0x10(%r15), %r13
movq %r14, %rax
shlq $0x4, %rax
movq %r15, 0x28(%rsp)
addq %r15, %rax
addq $0x10, %rax
movq %rax, 0x10(%rsp)
leaq 0x75e04(%rip), %rsi     # 0x13a846
movq %rbp, %rdi
movq %rdx, 0x18(%rsp)
movl %r13d, %ecx
xorl %eax, %eax
callq 0xe9226
movq %rbp, %rdi
movl $0x1, %esi
callq 0xe9585
testq %r14, %r14
movl 0x8(%rsp), %r15d
jle 0xc4cc1
movq %r14, 0x38(%rsp)
testl %r15d, %r15d
sete %r15b
movq 0x18(%rbp), %rdi
movq %r13, %rsi
callq 0xef32a
movq %rax, %r12
movq 0x18(%rbp), %rdi
leaq 0x4(%r13), %rsi
callq 0xef32a
movq %rax, %rbx
movq 0x18(%rbp), %rdi
leaq 0x8(%r13), %rsi
callq 0xef32a
movq %rax, %r14
movq 0x18(%rbp), %rdi
addq $0xc, %r13
movq %r13, %rsi
callq 0xef32a
movq %rbp, %rdi
leaq 0x75d96(%rip), %rsi     # 0x13a85a
movl %r12d, %edx
movl %ebx, %ecx
movl %r14d, %r8d
movl %eax, %r9d
xorl %eax, %eax
callq 0xe93cf
cmpq $0x465, %r12            # imm = 0x465
sete %al
andb %r15b, %al
cmpq $0x6, %rbx
sete %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0xc4b49
addq 0x10(%rsp), %r14
movq %rbp, %rdi
leaq 0x75da0(%rip), %rsi     # 0x13a8a0
movl %r14d, %edx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbp), %rdi
movl $0x10, %ecx
leaq 0x40(%rsp), %rsi
movq %r14, %rdx
callq 0xeeee8
movl 0x40(%rsp), %eax
movl $0x616d7a6c, %ecx       # imm = 0x616D7A6C
xorl %ecx, %eax
movzbl 0x44(%rsp), %ecx
orl %eax, %ecx
jne 0xc4b3f
movq (%rsp), %rax
movl $0x3, 0x4(%rax)
movl 0x8(%rsp), %r15d
jmp 0xc4caf
cmpq $0x6, %rbx
sete %al
movl 0x8(%rsp), %esi
testl %esi, %esi
sete %cl
cmpq $0x3e8, %r12            # imm = 0x3E8
sete %dl
andb %cl, %dl
andb %al, %dl
cmpb $0x1, %dl
movl %esi, %r15d
jne 0xc4bc5
movq (%rsp), %rax
cmpq $0x0, 0x8(%rax)
jne 0xc4caf
movq 0x18(%rbp), %rdi
addq 0x10(%rsp), %r14
movl $0x1f4, %edx            # imm = 0x1F4
movl $0x1f4, %ecx            # imm = 0x1F4
movq %r14, %rsi
movl $0x1, %r8d
movl $0x1, %r9d
callq 0xefe67
movq (%rsp), %rcx
movq %rax, 0x8(%rcx)
movq 0x18(%rax), %rdi
callq 0xf74bd
movq %rbp, %rdi
leaq 0x5d9b3(%rip), %rsi     # 0x122573
jmp 0xc4ca5
cmpq $0x6, %rbx
sete %al
testl %r15d, %r15d
sete %cl
cmpq $0x3e9, %r12            # imm = 0x3E9
sete %dl
andb %cl, %dl
andb %al, %dl
cmpb $0x1, %dl
jne 0xc4c38
movq (%rsp), %rax
cmpq $0x0, 0x10(%rax)
jne 0xc4caf
movq 0x18(%rbp), %rdi
addq 0x10(%rsp), %r14
movl $0x1f4, %edx            # imm = 0x1F4
movl $0x1f4, %ecx            # imm = 0x1F4
movq %r14, %rsi
movl $0x1, %r8d
movl $0x1, %r9d
callq 0xefe67
movq (%rsp), %rcx
movq %rax, 0x10(%rcx)
movq 0x18(%rax), %rdi
callq 0xf74bd
movq %rbp, %rdi
leaq 0x6c4b4(%rip), %rsi     # 0x1310ea
jmp 0xc4ca5
cmpq $0x6, %rbx
sete %al
testl %r15d, %r15d
sete %cl
cmpq $0x3ea, %r12            # imm = 0x3EA
sete %dl
andb %cl, %dl
andb %al, %dl
cmpb $0x1, %dl
jne 0xc4caf
movq (%rsp), %rax
cmpq $0x0, 0x18(%rax)
jne 0xc4caf
movq 0x18(%rbp), %rdi
addq 0x10(%rsp), %r14
movl $0x1f4, %edx            # imm = 0x1F4
movl $0x1f4, %ecx            # imm = 0x1F4
movq %r14, %rsi
movl $0x1, %r8d
movl $0x1, %r9d
callq 0xefe67
movq (%rsp), %rcx
movq %rax, 0x18(%rcx)
movq 0x18(%rax), %rdi
callq 0xf74bd
movq %rbp, %rdi
leaq 0x75bd8(%rip), %rsi     # 0x13a87d
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9226
addq $0x4, %r13
movq 0x38(%rsp), %r14
decq %r14
jne 0xc4a6f
movq %rbp, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
leaq 0x75bb6(%rip), %rsi     # 0x13a88b
movq %rbp, %rdi
movq 0x18(%rsp), %rdx
movq 0x10(%rsp), %rbx
movl %ebx, %ecx
xorl %eax, %eax
callq 0xe9226
movq 0x30(%rsp), %rcx
subq 0x28(%rsp), %rcx
addq %rbx, %rcx
movq 0x20(%rsp), %rax
movq %rcx, (%rax)
movl $0x1, %ebx
jmp 0xc4d32
leaq 0x75adc(%rip), %rsi     # 0x13a7ea
movq %rbp, %rdi
movl %r15d, %edx
xorl %eax, %eax
callq 0xe9df7
xorl %ebx, %ebx
jmp 0xc4d32
leaq 0x75adf(%rip), %rsi     # 0x13a805
xorl %ebx, %ebx
movq %rbp, %rdi
xorl %eax, %eax
callq 0xe9df7
movq %rbp, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
movl %ebx, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq