// File: /workspace/github_c_makefile_star2_O3/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
movq $0x0, 0x8(%rsp)
movl $0x20, %esi
callq 0xe8b2b
movq %rax, %r14
leaq 0x7704f(%rip), %rsi     # 0x13ab20
movq %rbx, %rdi
xorl %edx, %edx
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
movq 0x18(%rbx), %rdi
movl $0x4, %esi
callq 0xeead3
movb %al, (%r14)
movq 0x18(%rbx), %rdi
movl $0x5, %esi
callq 0xeead3
movb %al, 0x1(%r14)
movzbl (%r14), %edx
movzbl %al, %ecx
leaq 0x77018(%rip), %rsi     # 0x13ab33
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movzbl (%r14), %edx
cmpl $0x2, %edx
ja 0xc3b59
movzbl 0x1(%r14), %ecx
leaq 0x77013(%rip), %rsi     # 0x13ab4d
xorl %r12d, %r12d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9295
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
jmp 0xc3d3c
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
leaq 0x8(%rsp), %r8
movl $0x60, %ecx
movq %rbx, %rdi
movq %r14, %rsi
movl $0x1, %edx
callq 0xc3dae
testl %eax, %eax
je 0xc3c1f
leaq 0x8(%rsp), %r8
movq (%r8), %r15
leaq 0x67(%r15), %rax
addq $0x6e, %r15
testq %rax, %rax
cmovnsq %rax, %r15
andq $-0x8, %r15
xorl %r12d, %r12d
movq %rbx, %rdi
movq %r14, %rsi
xorl %edx, %edx
movq %r15, %rcx
callq 0xc3dae
testl %eax, %eax
je 0xc3d3c
addq 0x8(%rsp), %r15
leaq 0x4ffb1(%rip), %rsi     # 0x113b7b
xorl %r12d, %r12d
movq %rbx, %rdi
movl %r15d, %edx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
cmpq 0x18(%rdi), %r15
jg 0xc3d3c
leaq 0x18(%rsp), %r12
movl $0x8, %ecx
movq %r12, %rsi
movq %r15, %rdx
callq 0xee8d4
movb (%r12), %al
movb 0x1(%r12), %cl
movl %eax, %edx
xorb $0x1f, %dl
movl %ecx, %esi
xorb $-0x75, %sil
orb %dl, %sil
jne 0xc3c27
leaq 0x76ea3(%rip), %rcx     # 0x13aac0
jmp 0xc3c93
xorl %r12d, %r12d
jmp 0xc3d3c
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 0xc3c4d
leaq 0x76e7d(%rip), %rcx     # 0x13aac8
jmp 0xc3c93
movl %eax, %esi
xorb $-0x3, %sil
xorb $0x37, %cl
orb %sil, %cl
xorb $0x7a, %dl
orb %cl, %dl
jne 0xc3c69
leaq 0x76e6a(%rip), %rcx     # 0x13aad1
jmp 0xc3c93
leaq 0x76e69(%rip), %rcx     # 0x13aad9
cmpb $0x5d, %al
je 0xc3c93
cmpl $0x3, 0x4(%r14)
je 0xc3c93
leaq 0x76e61(%rip), %rsi     # 0x13aae3
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8faa
leaq 0x76e7b(%rip), %rcx     # 0x13ab0e
cmpq $0x0, 0x8(%r14)
je 0xc3d20
cmpb $0x0, 0xd0(%rbx)
je 0xc3d20
movq 0x10(%r14), %rax
leaq 0x82414(%rip), %rbp     # 0x1460c6
movq %rbp, %r13
testq %rax, %rax
je 0xc3cbe
movq 0x8(%rax), %r13
movq %rcx, 0x10(%rsp)
movq 0x18(%r14), %rax
testq %rax, %rax
je 0xc3cd0
movq 0x8(%rax), %rbp
movq %rbx, %rdi
callq 0xea504
movq %rax, %r12
movq 0x8(%r14), %rax
movq 0x8(%rax), %rcx
leaq 0x76e2d(%rip), %rdx     # 0x13ab17
leaq 0x20(%rsp), %rdi
movl $0x80, %esi
movq %r13, %r8
movq %rbp, %r9
xorl %eax, %eax
callq 0xe8193
movq %rbx, %rdi
movq %r12, %rsi
leaq 0x20(%rsp), %rdx
xorl %ecx, %ecx
movl $0x1, %r8d
callq 0xea78f
movq 0x10(%rsp), %rcx
jmp 0xc3d23
xorl %r12d, %r12d
movq 0x18(%rbx), %rdi
movq 0x18(%rdi), %rdx
subq %r15, %rdx
movq %r15, %rsi
movq %r12, %r8
xorl %r9d, %r9d
callq 0xefd48
movq %rbx, %rdi
movq %r12, %rsi
callq 0xea50e
movq 0x8(%r14), %rsi
movq %rbx, %rdi
callq 0xefaca
movq 0x18(%r14), %rsi
movq %rbx, %rdi
callq 0xefaca
movq 0x10(%r14), %rsi
movq %rbx, %rdi
callq 0xefaca
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe8be2
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq