// File: /workspace/github_c_makefile_star2_O2/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, %r15
movq %rcx, %rbx
movq %rsi, (%rsp)
movq %rdi, %r12
movl %edx, 0x8(%rsp)
testl %edx, %edx
leaq 0x67477(%rip), %rax     # 0x1235ca
leaq 0x71723(%rip), %r13     # 0x12d87d
cmoveq %rax, %r13
leaq 0x7171c(%rip), %rsi     # 0x12d881
xorl %ebp, %ebp
movq %r13, %rdx
xorl %eax, %eax
callq 0xde896
pushq $0x1
popq %rsi
movq %r12, %rdi
callq 0xdeba7
movq 0x18(%r12), %rdi
leaq 0xc(%rsp), %r14
pushq $0x4
popq %rcx
movq %r14, %rsi
movq %rbx, %rdx
callq 0xe3cb7
cmpb $-0x72, (%r14)
jne 0xbc4bd
cmpb $-0x53, 0xd(%rsp)
jne 0xbc4bd
cmpb $-0x18, 0xe(%rsp)
jne 0xbc4bd
cmpb $0x1, 0xf(%rsp)
jne 0xbc4d2
movq %r15, 0x30(%rsp)
leaq 0x8(%rbx), %r14
movq 0x18(%r12), %rdi
movq %r14, %rsi
callq 0xe404f
movq %rax, %r15
movq 0x18(%r12), %rdi
leaq 0xc(%rbx), %rsi
callq 0xe404f
leaq 0x716db(%rip), %rsi     # 0x12d8c8
xorl %ebp, %ebp
movq %r12, %rdi
movq %r13, %rdx
movl %r14d, %ecx
movl %r15d, %r8d
movq %rax, 0x28(%rsp)
movl %eax, %r9d
xorl %eax, %eax
callq 0xde896
cmpq $0x3e8, %r15            # imm = 0x3E8
jg 0xbc524
movq %r13, %rdx
leaq 0x10(%rbx), %r13
movq %r15, %rax
shlq $0x4, %rax
movq %rbx, 0x20(%rsp)
addq %rbx, %rax
addq $0x10, %rax
movq %rax, 0x10(%rsp)
leaq 0x716b1(%rip), %rsi     # 0x12d8ee
xorl %ebx, %ebx
movq %r12, %rdi
movq %rdx, 0x18(%rsp)
movl %r13d, %ecx
xorl %eax, %eax
callq 0xde896
pushq $0x1
popq %rbp
movq %r12, %rdi
movl %ebp, %esi
callq 0xdeba7
testq %r15, %r15
cmovleq %rbx, %r15
movl 0x8(%rsp), %ebx
subq $0x1, %r15
jb 0xbc4e7
movq %r15, 0x38(%rsp)
testl %ebx, %ebx
sete %bl
movq 0x18(%r12), %rdi
movq %r13, %rsi
callq 0xe404f
movq %rax, %r15
movq 0x18(%r12), %rdi
leaq 0x4(%r13), %rsi
callq 0xe404f
movq %rax, %rbp
movq 0x18(%r12), %rdi
leaq 0x8(%r13), %rsi
callq 0xe404f
movq %rax, %r14
movq 0x18(%r12), %rdi
addq $0xc, %r13
movq %r13, %rsi
callq 0xe404f
movq %r12, %rdi
leaq 0x71638(%rip), %rsi     # 0x12d902
movl %r15d, %edx
movl %ebp, %ecx
movl %r14d, %r8d
movl %eax, %r9d
xorl %eax, %eax
callq 0xde9f1
cmpq $0x465, %r15            # imm = 0x465
sete %al
andb %bl, %al
cmpq $0x6, %rbp
sete %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0xbc354
addq 0x10(%rsp), %r14
movq %r12, %rdi
leaq 0x71643(%rip), %rsi     # 0x12d948
movl %r14d, %edx
xorl %eax, %eax
callq 0xde896
movq 0x18(%r12), %rdi
leaq 0x40(%rsp), %rbx
movq %rbx, %rsi
movq %r14, %rdx
pushq $0x10
popq %rcx
callq 0xe3cb7
movq %rbx, %rdi
leaq 0x71633(%rip), %rsi     # 0x12d964
pushq $0x5
popq %rdx
callq 0x18230
testl %eax, %eax
pushq $0x1
popq %rbp
jne 0xbc34b
movq (%rsp), %rax
movl $0x3, 0x4(%rax)
movl 0x8(%rsp), %ebx
jmp 0xbc4af
cmpq $0x6, %rbp
sete %al
movl 0x8(%rsp), %ebx
testl %ebx, %ebx
sete %cl
cmpq $0x3e8, %r15            # imm = 0x3E8
sete %dl
andb %cl, %dl
andb %al, %dl
cmpb $0x1, %dl
jne 0xbc3cb
movq (%rsp), %rax
cmpq $0x0, 0x8(%rax)
pushq $0x1
popq %rbp
jne 0xbc4af
movq 0x18(%r12), %rdi
addq 0x10(%rsp), %r14
movl $0x1f4, %edx            # imm = 0x1F4
movl $0x1f4, %ecx            # imm = 0x1F4
movq %r14, %rsi
movl %ebp, %r8d
movl %ebp, %r9d
callq 0xe495c
movq (%rsp), %rcx
movq %rax, 0x8(%rcx)
movq 0x18(%rax), %rdi
callq 0xeb7e4
movq %r12, %rdi
leaq 0x591f5(%rip), %rsi     # 0x1155bb
jmp 0xbc4a5
cmpq $0x6, %rbp
sete %al
testl %ebx, %ebx
sete %cl
cmpq $0x3e9, %r15            # imm = 0x3E9
sete %dl
andb %cl, %dl
andb %al, %dl
cmpb $0x1, %dl
jne 0xbc43b
movq (%rsp), %rax
cmpq $0x0, 0x10(%rax)
pushq $0x1
popq %rbp
jne 0xbc4af
movq 0x18(%r12), %rdi
addq 0x10(%rsp), %r14
movl $0x1f4, %edx            # imm = 0x1F4
movl $0x1f4, %ecx            # imm = 0x1F4
movq %r14, %rsi
movl %ebp, %r8d
movl %ebp, %r9d
callq 0xe495c
movq (%rsp), %rcx
movq %rax, 0x10(%rcx)
movq 0x18(%rax), %rdi
callq 0xeb7e4
movq %r12, %rdi
leaq 0x67d01(%rip), %rsi     # 0x12413a
jmp 0xbc4a5
cmpq $0x6, %rbp
sete %al
testl %ebx, %ebx
sete %cl
cmpq $0x3ea, %r15            # imm = 0x3EA
sete %dl
andb %cl, %dl
andb %al, %dl
cmpb $0x1, %dl
pushq $0x1
popq %rbp
jne 0xbc4af
movq (%rsp), %rax
cmpq $0x0, 0x18(%rax)
jne 0xbc4af
movq 0x18(%r12), %rdi
addq 0x10(%rsp), %r14
movl $0x1f4, %edx            # imm = 0x1F4
movl $0x1f4, %ecx            # imm = 0x1F4
movq %r14, %rsi
movl %ebp, %r8d
movl %ebp, %r9d
callq 0xe495c
movq (%rsp), %rcx
movq %rax, 0x18(%rcx)
movq 0x18(%rax), %rdi
callq 0xeb7e4
movq %r12, %rdi
leaq 0x71480(%rip), %rsi     # 0x12d925
movq %rax, %rdx
xorl %eax, %eax
callq 0xde896
addq $0x4, %r13
movq 0x38(%rsp), %r15
jmp 0xbc269
leaq 0x713ce(%rip), %rsi     # 0x12d892
movq %r12, %rdi
movl %ebx, %edx
xorl %eax, %eax
callq 0xdf2a5
jmp 0xbc524
leaq 0x713d4(%rip), %rsi     # 0x12d8ad
xorl %ebp, %ebp
movq %r12, %rdi
xorl %eax, %eax
callq 0xdf2a5
jmp 0xbc524
pushq $-0x1
popq %rsi
movq %r12, %rdi
callq 0xdeba7
leaq 0x7143a(%rip), %rsi     # 0x12d933
movq %r12, %rdi
movq 0x18(%rsp), %rdx
movq 0x10(%rsp), %rbx
movl %ebx, %ecx
xorl %eax, %eax
callq 0xde896
movq 0x28(%rsp), %rcx
subq 0x20(%rsp), %rcx
addq %rbx, %rcx
movq 0x30(%rsp), %rax
movq %rcx, (%rax)
pushq $-0x1
popq %rsi
movq %r12, %rdi
callq 0xdeba7
movl %ebp, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq