// File: /workspace/github_c_makefile_star2_O1/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 0xe969b
movq %rax, %r14
movl %ebp, (%rax)
movb $0x1, %bpl
movb %bpl, 0x91(%rax)
leaq 0x100125(%rip), %rax    # 0x139fa1
movq %rax, 0xe8(%r14)
leaq 0xf678b(%rip), %rax     # 0x130615
movq %rax, 0xa8(%r14)
movq %rbx, %rdi
callq 0xfca54
movq %rax, 0x20(%r14)
movq %r14, 0x8(%rax)
movb %bpl, 0x78(%rax)
cmpl $0x1, (%r14)
jne 0x39ed4
movq 0x18(%rbx), %rdi
movl $0x3c, %esi
callq 0xef0d5
testb %al, %al
je 0x39ed4
movq 0x18(%rbx), %rdi
movl $0x40, %esi
callq 0xef0d5
testb %al, %al
je 0x39ed4
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 0xeb7d1
movl %eax, 0x4(%r14)
movq 0x18(%rbx), %rdi
movl $0x10, %esi
callq 0xef169
movq %rax, 0x10(%r14)
leaq 0xdef07(%rip), %rsi     # 0x118e13
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9226
cmpq $0x63, 0x10(%r14)
jle 0x3a149
movq 0x18(%rbx), %rdi
movl 0x4(%r14), %r9d
movl $0x12, %esi
movl $0x20, %edx
movl $0x20, %ecx
movl $0x1, %r8d
callq 0xefe67
movq %rax, 0x28(%r14)
movq 0x18(%rax), %rdi
callq 0xf7557
leaq 0x101a7a(%rip), %rsi    # 0x13b9d4
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9226
leaq 0x8(%rsp), %rsi
movq $0x32, (%rsi)
movq 0x18(%rbx), %rdi
callq 0xef34e
movq %rax, 0x18(%r14)
leaq 0xdeea0(%rip), %rsi     # 0x118e27
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9226
movq 0x20(%r14), %rax
movq 0x20(%rax), %r15
addq $0x18, %r15
movl (%r14), %eax
cmpl $0x1, %eax
je 0x39fdc
cmpl $0x2, %eax
jne 0x3a056
movq 0x18(%rbx), %rdi
movl $0x36, %esi
callq 0xef37e
movl $0x83ad2181, %ecx       # imm = 0x83AD2181
cmpq %rcx, %rax
jl 0x3a056
movabsq $-0x83ad2180, %rcx      # imm = 0xFFFFFFFF7C52DE80
addq %rcx, %rax
jmp 0x3a01e
cmpb $0x0, 0xa(%r14)
je 0x3a00b
cmpl $0x1, %eax
jne 0x3a056
movq 0x18(%rbx), %rdi
movl $0x44, %esi
callq 0xef32a
cmpq $0x1000001, %rax        # imm = 0x1000001
jl 0x3a056
movq %rax, %rdi
movq %r15, %rsi
callq 0xeae04
jmp 0x3a02b
movq 0x18(%rbx), %rdi
movl $0x36, %esi
callq 0xef417
testq %rax, %rax
jle 0x3a056
movq %rax, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0xeace2
leaq 0x10(%rsp), %r12
movl $0x40, %edx
movq %r15, %rdi
movq %r12, %rsi
xorl %ecx, %ecx
callq 0xeb4f8
leaq 0xdee3e(%rip), %rsi     # 0x118e87
movq %rbx, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0xe9226
cmpb $0x0, 0xa(%r14)
je 0x3a0ee
movq 0x18(%rbx), %rdi
leaq 0x30(%r14), %rdx
movl $0x3c, %esi
movl $0x4, %ecx
xorl %r8d, %r8d
callq 0xf1d74
leaq 0x40(%r14), %rdx
leaq 0xda7da(%rip), %rsi     # 0x114860
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
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 0xf1d74
leaq 0x70(%r14), %rdx
leaq 0xf9e25(%rip), %rsi     # 0x133ef3
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
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 0xef32a
cmpq 0x10(%r14), %rax
jne 0x3a149
cmpl $0x1, (%r14)
jne 0x3a11a
movq 0x18(%rbx), %rdi
movl $0x52, %esi
callq 0xef0d5
movb %al, 0xb(%r14)
cmpl $0x2, (%r14)
jne 0x3a1b1
movq 0x18(%rbx), %rdi
movl $0x62, %esi
callq 0xef0d5
testb %al, %al
je 0x3a1b1
leaq 0xded0b(%rip), %rsi     # 0x118e48
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9df7
jmp 0x3a14e
movb $0x1, 0x9(%r14)
cmpb $0x0, 0x9(%r14)
je 0x3a166
leaq 0xded13(%rip), %rsi     # 0x118e6f
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9df7
movq 0x20(%r14), %rdi
callq 0xfca9a
movq 0x108(%r14), %rsi
movq %rbx, %rdi
callq 0xe9752
movq 0xc8(%r14), %rsi
movq %rbx, %rdi
callq 0xe9752
movq 0x28(%r14), %rsi
movq %rbx, %rdi
callq 0xf00aa
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe9752
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 0x3a265
movq %rax, 0x98(%r14)
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x3a2b0
cmpb $0x0, 0x8(%r14)
jne 0x3a14e
movq 0x20(%r14), %rax
movq 0x28(%r14), %rcx
movq 0x68(%rax), %rdi
movq 0x18(%rcx), %rsi
callq 0xf6969
movq 0x20(%r14), %rdi
movb $0x1, 0x79(%rdi)
movq 0x28(%r14), %rax
movq 0x8(%rax), %rsi
movq 0x10(%rax), %rdx
callq 0xfcaec
movb 0xd0(%r14), %al
testb %al, %al
je 0x3a29c
movq 0x20(%r14), %rax
movb $0x1, 0x10(%rax)
movq 0xe0(%r14), %rcx
movq %rcx, 0x18(%rax)
cmpb $0x0, (%r15)
je 0x3a24c
movq 0x20(%r14), %rax
movb $0x1, 0x38(%rax)
movq 0xa0(%r14), %rcx
movq %rcx, 0x40(%rax)
movq 0x20(%r14), %rdi
leaq 0x2e7(%rip), %rax       # 0x3a53e
movq %rax, 0x60(%rdi)
callq 0xfcb73
jmp 0x3a14e
movq %r14, %rdx
addq $0xd0, %rdx
movq %rax, 0xd8(%r14)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3a2b0
cmpb $0x0, 0x8(%r14)
jne 0x3a14e
cmpb $0x0, 0xc(%r14)
jne 0x3a1ce
jmp 0x3a1e7
cmpb $0x0, (%r15)
je 0x3a14e
testb %al, %al
jne 0x3a220
jmp 0x3a233