// File: /workspace/github_c_makefile_star2_O3/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 0x6cad7(%rip), %rax     # 0x1308ad
leaq 0x76d90(%rip), %r13     # 0x13ab6d
cmoveq %rax, %r13
leaq 0x76d89(%rip), %rsi     # 0x13ab71
movq %r13, %rdx
xorl %eax, %eax
callq 0xe8617
movq %rbp, %rdi
movl $0x1, %esi
callq 0xe8a15
movq 0x18(%rbp), %rdi
leaq 0xc(%rsp), %r14
movl $0x4, %ecx
movq %r14, %rsi
movq %r15, %rdx
callq 0xee8d4
cmpb $-0x72, (%r14)
jne 0xc4187
cmpb $-0x53, 0xd(%rsp)
jne 0xc4187
cmpb $-0x18, 0xe(%rsp)
jne 0xc4187
cmpb $0x1, 0xf(%rsp)
jne 0xc419f
leaq 0x8(%r15), %r14
movq 0x18(%rbp), %rdi
movq %r14, %rsi
callq 0xeed0d
movq %rax, %rbx
movq 0x18(%rbp), %rdi
leaq 0xc(%r15), %rsi
callq 0xeed0d
leaq 0x76d4e(%rip), %rsi     # 0x13abb8
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 0xe8617
cmpq $0x3e8, %r14            # imm = 0x3E8
jg 0xc41b2
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 0x76d1c(%rip), %rsi     # 0x13abde
movq %rbp, %rdi
movq %rdx, 0x18(%rsp)
movl %r13d, %ecx
xorl %eax, %eax
callq 0xe8617
movq %rbp, %rdi
movl $0x1, %esi
callq 0xe8a15
testq %r14, %r14
movl 0x8(%rsp), %r15d
jle 0xc4141
movq %r14, 0x38(%rsp)
testl %r15d, %r15d
sete %r15b
movq 0x18(%rbp), %rdi
movq %r13, %rsi
callq 0xeed0d
movq %rax, %r12
movq 0x18(%rbp), %rdi
leaq 0x4(%r13), %rsi
callq 0xeed0d
movq %rax, %rbx
movq 0x18(%rbp), %rdi
leaq 0x8(%r13), %rsi
callq 0xeed0d
movq %rax, %r14
movq 0x18(%rbp), %rdi
addq $0xc, %r13
movq %r13, %rsi
callq 0xeed0d
movq %rbp, %rdi
leaq 0x76cae(%rip), %rsi     # 0x13abf2
movl %r12d, %edx
movl %ebx, %ecx
movl %r14d, %r8d
movl %eax, %r9d
xorl %eax, %eax
callq 0xe87e4
cmpq $0x465, %r12            # imm = 0x465
sete %al
andb %r15b, %al
cmpq $0x6, %rbx
sete %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0xc3fc9
addq 0x10(%rsp), %r14
movq %rbp, %rdi
leaq 0x76cb8(%rip), %rsi     # 0x13ac38
movl %r14d, %edx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbp), %rdi
movl $0x10, %ecx
leaq 0x40(%rsp), %rsi
movq %r14, %rdx
callq 0xee8d4
movl 0x40(%rsp), %eax
movl $0x616d7a6c, %ecx       # imm = 0x616D7A6C
xorl %ecx, %eax
movzbl 0x44(%rsp), %ecx
orl %eax, %ecx
jne 0xc3fbf
movq (%rsp), %rax
movl $0x3, 0x4(%rax)
movl 0x8(%rsp), %r15d
jmp 0xc412f
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 0xc4045
movq (%rsp), %rax
cmpq $0x0, 0x8(%rax)
jne 0xc412f
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 0xef87c
movq (%rsp), %rcx
movq %rax, 0x8(%rcx)
movq 0x18(%rax), %rdi
callq 0xf705c
movq %rbp, %rdi
leaq 0x5e73b(%rip), %rsi     # 0x12277b
jmp 0xc4125
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 0xc40b8
movq (%rsp), %rax
cmpq $0x0, 0x10(%rax)
jne 0xc412f
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 0xef87c
movq (%rsp), %rcx
movq %rax, 0x10(%rcx)
movq 0x18(%rax), %rdi
callq 0xf705c
movq %rbp, %rdi
leaq 0x6d374(%rip), %rsi     # 0x13142a
jmp 0xc4125
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 0xc412f
movq (%rsp), %rax
cmpq $0x0, 0x18(%rax)
jne 0xc412f
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 0xef87c
movq (%rsp), %rcx
movq %rax, 0x18(%rcx)
movq 0x18(%rax), %rdi
callq 0xf705c
movq %rbp, %rdi
leaq 0x76af0(%rip), %rsi     # 0x13ac15
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
addq $0x4, %r13
movq 0x38(%rsp), %r14
decq %r14
jne 0xc3eef
movq %rbp, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
leaq 0x76ace(%rip), %rsi     # 0x13ac23
movq %rbp, %rdi
movq 0x18(%rsp), %rdx
movq 0x10(%rsp), %rbx
movl %ebx, %ecx
xorl %eax, %eax
callq 0xe8617
movq 0x30(%rsp), %rcx
subq 0x28(%rsp), %rcx
addq %rbx, %rcx
movq 0x20(%rsp), %rax
movq %rcx, (%rax)
movl $0x1, %ebx
jmp 0xc41b2
leaq 0x769f4(%rip), %rsi     # 0x13ab82
movq %rbp, %rdi
movl %r15d, %edx
xorl %eax, %eax
callq 0xe9295
xorl %ebx, %ebx
jmp 0xc41b2
leaq 0x769f7(%rip), %rsi     # 0x13ab9d
xorl %ebx, %ebx
movq %rbp, %rdi
xorl %eax, %eax
callq 0xe9295
movq %rbp, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
movl %ebx, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq