// File: /workspace/github_c_makefile_star2_O2/jsummers[P]deark/modules/rpm.c
// Function: de_run_rpm
static void de_run_rpm(deark *c, de_module_params *mparams)
{
	lctx *d = NULL;
	i64 pos;
	u8 buf[8];
	const char *ext;
	i64 section_size = 0;
	de_finfo *fi = NULL;
	char filename[128];

	d = de_malloc(c, sizeof(lctx));

	if(!do_lead_section(c, d)) {
		goto done;
	}

	pos = 96;

	if(!do_header_structure(c, d, 1, pos, &section_size)) {
		goto done;
	}
	pos += section_size;

	// Header structures are 8-byte aligned. The first one always starts at
	// offset 96, so we don't have to worry about it. But we need to make
	// sure the second one is aligned.
	pos = ((pos + 7)/8)*8;

	if(!do_header_structure(c, d, 0, pos, &section_size)) {
		goto done;
	}
	pos += section_size;

	de_dbg(c, "data pos: %d", (int)pos);
	if(pos > c->infile->len) goto done;

	// There is usually a tag that indicates the compression format, but we
	// primarily figure out the format by sniffing its magic number, on the
	// theory that that's more reliable.

	// TODO: I think it's also theoretically possible that it could use an archive
	// format other than cpio.

	de_read(buf, pos, 8);

	if(buf[0]==0x1f && buf[1]==0x8b) {
		ext = "cpio.gz";
	}
	else if(buf[0]==0x42 && buf[1]==0x5a && buf[2]==0x68) {
		ext = "cpio.bz2";
	}
	else if(buf[0]==0xfd && buf[1]==0x37 && buf[2]==0x7a) {
		ext = "cpio.xz";
	}
	else if(d->cmpr_type==DE_RPM_CMPR_LZMA || buf[0]==0x5d) {
		ext = "cpio.lzma";
	}
	else {
		de_warn(c, "Unidentified compression or archive format");
		ext = "cpio.bin";
	}

	if(d->name_srd && c->filenames_from_file) {
		const char *version2 = "x";
		const char *release2 = "x";

		if(d->version_srd) version2 = d->version_srd->sz;
		if(d->release_srd) release2 = d->release_srd->sz;

		fi = de_finfo_create(c);
		de_snprintf(filename, sizeof(filename), "%s-%s.%s",
			d->name_srd->sz, version2, release2);
		de_finfo_set_name_from_sz(c, fi, filename, 0, DE_ENCODING_ASCII);
	}

	dbuf_create_file_from_slice(c->infile, pos, c->infile->len - pos, ext, fi, 0);

done:
	de_finfo_destroy(c, fi);
	if(d) {
		de_destroy_stringreaderdata(c, d->name_srd);
		de_destroy_stringreaderdata(c, d->release_srd);
		de_destroy_stringreaderdata(c, d->version_srd);
		de_free(c, d);
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %rdi, %rbx
andq $0x0, 0x8(%rsp)
pushq $0x20
popq %rsi
callq 0xdecaf
movq %rax, %r14
leaq 0x719cc(%rip), %rsi     # 0x12d830
movq %rbx, %rdi
xorl %edx, %edx
xorl %eax, %eax
callq 0xde896
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
movq 0x18(%rbx), %rdi
pushq $0x4
popq %rsi
callq 0xe3eb4
movb %al, (%r14)
movq 0x18(%rbx), %rdi
pushq $0x5
popq %rsi
callq 0xe3eb4
movb %al, 0x1(%r14)
movzbl (%r14), %edx
movzbl %al, %ecx
leaq 0x7199b(%rip), %rsi     # 0x12d843
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
movzbl (%r14), %edx
cmpl $0x2, %edx
ja 0xbbee4
movzbl 0x1(%r14), %ecx
leaq 0x71996(%rip), %rsi     # 0x12d85d
xorl %r12d, %r12d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf2a5
pushq $-0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
jmp 0xbc0bd
pushq $-0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
pushq $0x1
popq %rdx
pushq $0x60
popq %rcx
leaq 0x8(%rsp), %r8
movq %rbx, %rdi
movq %r14, %rsi
callq 0xbc12b
testl %eax, %eax
je 0xbbfa2
leaq 0x8(%rsp), %r8
movq (%r8), %rax
addq $0x67, %rax
pushq $0x8
popq %rcx
cqto
idivq %rcx
movq %rax, %r15
shlq $0x3, %r15
xorl %r12d, %r12d
movq %rbx, %rdi
movq %r14, %rsi
xorl %edx, %edx
movq %r15, %rcx
callq 0xbc12b
testl %eax, %eax
je 0xbc0bd
addq 0x8(%rsp), %r15
leaq 0x4abcc(%rip), %rsi     # 0x106b1b
xorl %r12d, %r12d
movq %rbx, %rdi
movl %r15d, %edx
xorl %eax, %eax
callq 0xde896
movq 0x18(%rbx), %rdi
cmpq 0x18(%rdi), %r15
jg 0xbc0bd
leaq 0x18(%rsp), %r12
pushq $0x8
popq %rcx
movq %r12, %rsi
movq %r15, %rdx
callq 0xe3cb7
movb (%r12), %al
movb 0x1(%r12), %cl
movl %eax, %edx
xorb $0x1f, %dl
movl %ecx, %esi
xorb $-0x75, %sil
orb %dl, %sil
jne 0xbbfaa
leaq 0x71830(%rip), %rcx     # 0x12d7d0
jmp 0xbc016
xorl %r12d, %r12d
jmp 0xbc0bd
movl %eax, %edx
xorb $0x42, %dl
movl %ecx, %esi
xorb $0x5a, %sil
orb %dl, %sil
movb 0x1a(%rsp), %dl
movl %edx, %edi
xorb $0x68, %dil
orb %sil, %dil
jne 0xbbfd0
leaq 0x7180a(%rip), %rcx     # 0x12d7d8
jmp 0xbc016
movl %eax, %esi
xorb $-0x3, %sil
xorb $0x37, %cl
orb %sil, %cl
xorb $0x7a, %dl
orb %cl, %dl
jne 0xbbfec
leaq 0x717f7(%rip), %rcx     # 0x12d7e1
jmp 0xbc016
leaq 0x717f6(%rip), %rcx     # 0x12d7e9
cmpb $0x5d, %al
je 0xbc016
cmpl $0x3, 0x4(%r14)
je 0xbc016
leaq 0x717ee(%rip), %rsi     # 0x12d7f3
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf05a
leaq 0x71808(%rip), %rcx     # 0x12d81e
cmpq $0x0, 0x8(%r14)
je 0xbc0a1
cmpb $0x0, 0xd0(%rbx)
je 0xbc0a1
movq 0x10(%r14), %rax
leaq 0x7cc41(%rip), %rbp     # 0x138c76
movq %rbp, %r13
testq %rax, %rax
je 0xbc041
movq 0x8(%rax), %r13
movq %rcx, 0x10(%rsp)
movq 0x18(%r14), %rax
testq %rax, %rax
je 0xbc053
movq 0x8(%rax), %rbp
movq %rbx, %rdi
callq 0xdfd1a
movq %rax, %r12
movq 0x8(%r14), %rax
movq 0x8(%rax), %rcx
leaq 0x717ba(%rip), %rdx     # 0x12d827
leaq 0x20(%rsp), %rdi
movl $0x80, %esi
movq %r13, %r8
movq %rbp, %r9
xorl %eax, %eax
callq 0xde40c
pushq $0x1
popq %r8
movq %rbx, %rdi
movq %r12, %rsi
leaq 0x20(%rsp), %rdx
xorl %ecx, %ecx
callq 0xdffbd
movq 0x10(%rsp), %rcx
jmp 0xbc0a4
xorl %r12d, %r12d
movq 0x18(%rbx), %rdi
movq 0x18(%rdi), %rdx
subq %r15, %rdx
movq %r15, %rsi
movq %r12, %r8
xorl %r9d, %r9d
callq 0xe4e0b
movq %rbx, %rdi
movq %r12, %rsi
callq 0xdfd24
movq 0x8(%r14), %rsi
movq %rbx, %rdi
callq 0xe4b94
movq 0x18(%r14), %rsi
movq %rbx, %rdi
callq 0xe4b94
movq 0x10(%r14), %rsi
movq %rbx, %rdi
callq 0xe4b94
movq %rbx, %rdi
movq %r14, %rsi
callq 0xded32
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq