// File: /workspace/github_c_makefile_star2_O1/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 0xe969b
movq %rax, %r14
leaq 0x76137(%rip), %rsi     # 0x13a788
movq %rbx, %rdi
xorl %edx, %edx
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
movq 0x18(%rbx), %rdi
movl $0x4, %esi
callq 0xef0d5
movb %al, (%r14)
movq 0x18(%rbx), %rdi
movl $0x5, %esi
callq 0xef0d5
movb %al, 0x1(%r14)
movzbl (%r14), %edx
movzbl %al, %ecx
leaq 0x76100(%rip), %rsi     # 0x13a79b
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movzbl (%r14), %edx
cmpl $0x2, %edx
ja 0xc46d9
movzbl 0x1(%r14), %ecx
leaq 0x760fb(%rip), %rsi     # 0x13a7b5
xorl %r12d, %r12d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9df7
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
jmp 0xc48bc
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
leaq 0x8(%rsp), %r8
movl $0x60, %ecx
movq %rbx, %rdi
movq %r14, %rsi
movl $0x1, %edx
callq 0xc492e
testl %eax, %eax
je 0xc479f
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 0xc492e
testl %eax, %eax
je 0xc48bc
addq 0x8(%rsp), %r15
leaq 0x4f381(%rip), %rsi     # 0x113acb
xorl %r12d, %r12d
movq %rbx, %rdi
movl %r15d, %edx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rdi
cmpq 0x18(%rdi), %r15
jg 0xc48bc
leaq 0x18(%rsp), %r12
movl $0x8, %ecx
movq %r12, %rsi
movq %r15, %rdx
callq 0xeeee8
movb (%r12), %al
movb 0x1(%r12), %cl
movl %eax, %edx
xorb $0x1f, %dl
movl %ecx, %esi
xorb $-0x75, %sil
orb %dl, %sil
jne 0xc47a7
leaq 0x75f8b(%rip), %rcx     # 0x13a728
jmp 0xc4813
xorl %r12d, %r12d
jmp 0xc48bc
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 0xc47cd
leaq 0x75f65(%rip), %rcx     # 0x13a730
jmp 0xc4813
movl %eax, %esi
xorb $-0x3, %sil
xorb $0x37, %cl
orb %sil, %cl
xorb $0x7a, %dl
orb %cl, %dl
jne 0xc47e9
leaq 0x75f52(%rip), %rcx     # 0x13a739
jmp 0xc4813
leaq 0x75f51(%rip), %rcx     # 0x13a741
cmpb $0x5d, %al
je 0xc4813
cmpl $0x3, 0x4(%r14)
je 0xc4813
leaq 0x75f49(%rip), %rsi     # 0x13a74b
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9b27
leaq 0x75f63(%rip), %rcx     # 0x13a776
cmpq $0x0, 0x8(%r14)
je 0xc48a0
cmpb $0x0, 0xd0(%rbx)
je 0xc48a0
movq 0x10(%r14), %rax
leaq 0x813aa(%rip), %rbp     # 0x145bdc
movq %rbp, %r13
testq %rax, %rax
je 0xc483e
movq 0x8(%rax), %r13
movq %rcx, 0x10(%rsp)
movq 0x18(%r14), %rax
testq %rax, %rax
je 0xc4850
movq 0x8(%rax), %rbp
movq %rbx, %rdi
callq 0xea9a9
movq %rax, %r12
movq 0x8(%r14), %rax
movq 0x8(%rax), %rcx
leaq 0x75f15(%rip), %rdx     # 0x13a77f
leaq 0x20(%rsp), %rdi
movl $0x80, %esi
movq %r13, %r8
movq %rbp, %r9
xorl %eax, %eax
callq 0xe8d67
movq %rbx, %rdi
movq %r12, %rsi
leaq 0x20(%rsp), %rdx
xorl %ecx, %ecx
movl $0x1, %r8d
callq 0xeac7c
movq 0x10(%rsp), %rcx
jmp 0xc48a3
xorl %r12d, %r12d
movq 0x18(%rbx), %rdi
movq 0x18(%rdi), %rdx
subq %r15, %rdx
movq %r15, %rsi
movq %r12, %r8
xorl %r9d, %r9d
callq 0xf0321
movq %rbx, %rdi
movq %r12, %rsi
callq 0xea9b3
movq 0x8(%r14), %rsi
movq %rbx, %rdi
callq 0xf00aa
movq 0x18(%r14), %rsi
movq %rbx, %rdi
callq 0xf00aa
movq 0x10(%r14), %rsi
movq %rbx, %rdi
callq 0xf00aa
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe9752
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq