// File: /workspace/github_c_makefile_star2_O3/jsummers[P]deark/modules/aldus.c
// Function: aldus_main
static void aldus_main(deark *c, de_module_params *mparams, UI fmtcode)
{
	lctx *d = NULL;
	i64 pos;
	i64 n;
	u8 frag_flag = 0;

	d = de_malloc(c, sizeof(lctx));
	d->fmtcode = fmtcode;
	d->rfork.is_rsrc_fork = 1;
	d->dfork.forkname = "data";
	d->rfork.forkname = "resource";

	d->advf = de_advfile_create(c);
	d->advf->userdata = (void*)d;
	d->advf->enable_wbuffer = 1;

	// For lack of a better idea, this is how we distinguish Mac and PC formats.
	if(d->fmtcode==1 && de_getbyte(60)!=0 && de_getbyte(64)!=0) {
		d->has_4cc_codes = 1;
	}

	d->input_encoding = de_get_input_encoding(c, NULL,
		(d->has_4cc_codes?DE_ENCODING_MACROMAN:DE_ENCODING_WINDOWS1252));

	d->fork1pos = de_getu16be(16);
	de_dbg(c, "first fork pos: %"I64_FMT, d->fork1pos);
	if(d->fork1pos<100) {
		d->need_errmsg = 1;
		goto done;
	}

	d->fname = dbuf_read_string(c->infile, 18, 32, 32, DE_CONVFLAG_STOP_AT_NUL,
		d->input_encoding);
	de_dbg(c, "filename: \"%s\"", ucstring_getpsz_d(d->fname->str));

	pos = 50;
	d->total_orig_len = de_getu32be_p(&pos);
	de_dbg(c, "orig len (sum of all forks): %"I64_FMT, d->total_orig_len);

	read_timestamp(c, d); // offset 54 or 68

	if(d->has_4cc_codes) {
		read_typecreator_codes(c, d, 60);
	}

	n = de_getu32be(78);
	if(n != d->fork1pos) {
		// The 2-byte field at offset 16, and the 4(?)-byte field at offset 78,
		// are always the same. Either 100 or 200.
		// So I don't know which is the one I want.
		d->need_errmsg = 1;
		goto done;
	}

	if(d->fmtcode==1) {
		d->no_dfork_flag = de_getbyte(82);
	}

	if(d->fmtcode==2) {
		frag_flag = de_getbyte(98);
	}
	if(frag_flag) {
		de_err(c, "Unsupported type of file (fragmented?)");
		goto done;
	}

	if(d->no_dfork_flag) {
		d->rfork.pos = d->fork1pos;
		aldus_read_fork(c, d, &d->rfork);
		if(d->errflag) goto done;
	}
	else {
		// We're assuming that if both forks exist, the data fork is always
		// first.
		d->dfork.pos = d->fork1pos;
		aldus_read_fork(c, d, &d->dfork);
		if(d->errflag) goto done;

		if(d->found_rsrc_fork) {
			aldus_read_fork(c, d, &d->rfork);
			if(d->errflag) goto done;
		}
	}

	ucstring_append_ucstring(d->advf->filename, d->fname->str);
	d->advf->original_filename_flag = 1;

	de_advfile_set_orig_filename(d->advf, d->fname->sz,
		d->fname->sz_strlen);

	if(!d->dfork.fork_exists && !d->rfork.fork_exists) goto done;

	if(d->dfork.fork_exists) {
		d->advf->mainfork.fork_exists = 1;
		d->advf->mainfork.fork_len = d->dfork.orig_len;
	}
	if(d->rfork.fork_exists) {
		d->advf->rsrcfork.fork_exists = 1;
		d->advf->rsrcfork.fork_len = d->rfork.orig_len;
	}

	d->advf->writefork_cbfn = my_advfile_cbfn;

	de_advfile_run(d->advf);

done:
	if(d) {
		if(d->need_errmsg) {
			de_err(c, "Bad or unsupported file");
		}
		de_advfile_destroy(d->advf);
		destroy_fork_contents(c, &d->dfork);
		destroy_fork_contents(c, &d->rfork);
		de_destroy_stringreaderdata(c, d->fname);
		de_free(c, d);
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x50, %rsp
movl %esi, %ebp
movq %rdi, %rbx
movl $0x110, %esi            # imm = 0x110
callq 0xe8b2b
movq %rax, %r14
movl %ebp, (%rax)
movb $0x1, %bpl
movb %bpl, 0x91(%rax)
leaq 0x1005a5(%rip), %rax    # 0x13a325
movq %rax, 0xe8(%r14)
leaq 0xf6bc2(%rip), %rax     # 0x130950
movq %rax, 0xa8(%r14)
movq %rbx, %rdi
callq 0xfc498
movq %rax, 0x20(%r14)
movq %r14, 0x8(%rax)
movb %bpl, 0x78(%rax)
cmpl $0x1, (%r14)
jne 0x39dd8
movq 0x18(%rbx), %rdi
movl $0x3c, %esi
callq 0xeead3
testb %al, %al
je 0x39dd8
movq 0x18(%rbx), %rdi
movl $0x40, %esi
callq 0xeead3
testb %al, %al
je 0x39dd8
movb $0x1, 0xa(%r14)
xorl %eax, %eax
cmpb $0x0, 0xa(%r14)
setne %al
leal 0x9(,%rax,8), %edx
movq %rbx, %rdi
xorl %esi, %esi
callq 0xeb220
movl %eax, 0x4(%r14)
movq 0x18(%rbx), %rdi
movl $0x10, %esi
callq 0xeeb67
movq %rax, 0x10(%r14)
leaq 0xdf123(%rip), %rsi     # 0x118f33
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
cmpq $0x63, 0x10(%r14)
jle 0x3a048
movq 0x18(%rbx), %rdi
movl 0x4(%r14), %r9d
movl $0x12, %esi
movl $0x20, %edx
movl $0x20, %ecx
movl $0x1, %r8d
callq 0xef87c
movq %rax, 0x28(%r14)
movq 0x18(%rax), %rdi
callq 0xf70f6
leaq 0x101ef0(%rip), %rsi    # 0x13bd4e
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
leaq 0x8(%rsp), %rsi
movq $0x32, (%rsi)
movq 0x18(%rbx), %rdi
callq 0xeed31
movq %rax, 0x18(%r14)
leaq 0xdf0bc(%rip), %rsi     # 0x118f47
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
movq 0x20(%r14), %rax
movq 0x20(%rax), %r15
addq $0x18, %r15
movl (%r14), %eax
cmpl $0x1, %eax
je 0x39ee0
cmpl $0x2, %eax
jne 0x39f51
movq 0x18(%rbx), %rdi
movl $0x36, %esi
callq 0xeed61
movl $0x83ad2181, %ecx       # imm = 0x83AD2181
cmpq %rcx, %rax
jl 0x39f51
movabsq $-0x83ad2180, %rcx      # imm = 0xFFFFFFFF7C52DE80
addq %rcx, %rax
jmp 0x39f19
cmpb $0x0, 0xa(%r14)
movq 0x18(%rbx), %rdi
je 0x39f0a
movl $0x44, %esi
callq 0xeed0d
cmpq $0x1000001, %rax        # imm = 0x1000001
jl 0x39f51
movq %rax, %rdi
movq %r15, %rsi
callq 0xea8ff
jmp 0x39f26
movl $0x36, %esi
callq 0xeedfa
testq %rax, %rax
jle 0x39f51
movq %rax, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0xea7f5
leaq 0x10(%rsp), %r12
movl $0x40, %edx
movq %r15, %rdi
movq %r12, %rsi
xorl %ecx, %ecx
callq 0xeaf46
leaq 0xdf063(%rip), %rsi     # 0x118fa7
movq %rbx, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0xe8617
cmpb $0x0, 0xa(%r14)
je 0x39fe9
movq 0x18(%rbx), %rdi
leaq 0x30(%r14), %rdx
movl $0x3c, %esi
movl $0x4, %ecx
xorl %r8d, %r8d
callq 0xf1762
leaq 0x40(%r14), %rdx
leaq 0xda9ba(%rip), %rsi     # 0x11493b
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq 0x20(%r14), %rax
movl 0x30(%r14), %ecx
movl %ecx, 0x90(%rax)
movq 0x20(%r14), %rax
movb $0x1, %bpl
movb %bpl, 0x7b(%rax)
movq 0x18(%rbx), %rdi
leaq 0x60(%r14), %rdx
movl $0x40, %esi
movl $0x4, %ecx
xorl %r8d, %r8d
callq 0xf1762
leaq 0x70(%r14), %rdx
leaq 0xfa322(%rip), %rsi     # 0x1342eb
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq 0x20(%r14), %rax
movl 0x60(%r14), %ecx
movl %ecx, 0x94(%rax)
movq 0x20(%r14), %rax
movb %bpl, 0x7c(%rax)
movq 0x18(%rbx), %rdi
movl $0x4e, %esi
callq 0xeed0d
cmpq 0x10(%r14), %rax
jne 0x3a048
movl (%r14), %eax
cmpl $0x1, %eax
jne 0x3a01a
movq 0x18(%rbx), %rdi
movl $0x52, %esi
callq 0xeead3
movb %al, 0xb(%r14)
movl (%r14), %eax
cmpl $0x2, %eax
jne 0x3a0b0
movq 0x18(%rbx), %rdi
movl $0x62, %esi
callq 0xeead3
testb %al, %al
je 0x3a0b0
leaq 0xdef2c(%rip), %rsi     # 0x118f68
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9295
jmp 0x3a04d
movb $0x1, 0x9(%r14)
cmpb $0x0, 0x9(%r14)
je 0x3a065
leaq 0xdef34(%rip), %rsi     # 0x118f8f
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9295
movq 0x20(%r14), %rdi
callq 0xfc4de
movq 0x108(%r14), %rsi
movq %rbx, %rdi
callq 0xe8be2
movq 0xc8(%r14), %rsi
movq %rbx, %rdi
callq 0xe8be2
movq 0x28(%r14), %rsi
movq %rbx, %rdi
callq 0xefaca
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe8be2
addq $0x50, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
leaq 0x90(%r14), %r15
cmpb $0x0, 0xb(%r14)
movq 0x10(%r14), %rax
je 0x3a139
movq %rax, 0x98(%r14)
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x3a1a2
cmpb $0x0, 0x8(%r14)
jne 0x3a04d
movq 0x20(%r14), %rax
movq 0x28(%r14), %rcx
movq 0x68(%rax), %rdi
movq 0x18(%rcx), %rsi
callq 0xf6509
movq 0x20(%r14), %rdi
movb $0x1, 0x79(%rdi)
movq 0x28(%r14), %rax
movq 0x8(%rax), %rsi
movq 0x10(%rax), %rdx
callq 0xfc530
cmpb $0x0, 0xd0(%r14)
je 0x3a170
movq 0x20(%r14), %rdi
movb $0x1, 0x10(%rdi)
movq 0xe0(%r14), %rax
movq %rax, 0x18(%rdi)
cmpb $0x0, 0x90(%r14)
jne 0x3a17e
jmp 0x3a18d
movq %r14, %rdx
addq $0xd0, %rdx
movq %rax, 0xd8(%r14)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3a1a2
cmpb $0x0, 0x8(%r14)
jne 0x3a04d
cmpb $0x0, 0xc(%r14)
jne 0x3a0c9
jmp 0x3a0e2
cmpb $0x0, (%r15)
je 0x3a04d
movq 0x20(%r14), %rdi
movb $0x1, 0x38(%rdi)
movq 0xa0(%r14), %rax
movq %rax, 0x40(%rdi)
leaq 0x29a(%rip), %rax       # 0x3a42e
movq %rax, 0x60(%rdi)
callq 0xfc5b7
jmp 0x3a04d