// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/sauce.c
// Function: do_read_SAUCE
static int do_read_SAUCE(deark *c, dbuf *f, struct de_SAUCE_info *si)
{
	unsigned int t;
	u8 tmpbuf[40];
	i64 tmpbuf_len;
	i64 pos;
	const char *name;
	de_ucstring *tflags_descr = NULL;
	int retval = 0;
	struct sauce_private_ctx *d = NULL;

	pos = f->len - 128;
	if(dbuf_memcmp(f, pos+0, "SAUCE00", 7)) {
		goto done;
	}

	si->is_valid = 1;

	d = de_malloc(c, sizeof(struct sauce_private_ctx));
	d->encoding_for_strings = DE_ENCODING_CP437;
	d->encoding_for_comments = DE_EXTENC_MAKE(d->encoding_for_strings, DE_ENCSUBTYPE_HYBRID);
	d->combine_comments = de_get_ext_option_bool(c, "sauce:combinecomments", 0);

	// Title
	dbuf_read(f, tmpbuf, pos+7, 35);
	tmpbuf_len = sauce_get_string_length(tmpbuf, 35, 0);
	if(tmpbuf_len>0) {
		si->title = ucstring_create(c);
		sauce_bytes_to_ucstring(c, tmpbuf, tmpbuf_len, si->title, d->encoding_for_strings);
		de_dbg(c, "title: \"%s\"", ucstring_getpsz_d(si->title));
	}

	// Artist / Creator
	dbuf_read(f, tmpbuf, pos+42, 20);
	tmpbuf_len = sauce_get_string_length(tmpbuf, 20, 0);
	if(tmpbuf_len>0) {
		si->artist = ucstring_create(c);
		sauce_bytes_to_ucstring(c, tmpbuf, tmpbuf_len, si->artist, d->encoding_for_strings);
		de_dbg(c, "artist: \"%s\"", ucstring_getpsz_d(si->artist));
	}

	// Organization
	dbuf_read(f, tmpbuf, pos+62, 20);
	tmpbuf_len = sauce_get_string_length(tmpbuf, 20, 0);
	if(tmpbuf_len>0) {
		si->organization = ucstring_create(c);
		sauce_bytes_to_ucstring(c, tmpbuf, tmpbuf_len, si->organization, d->encoding_for_strings);
		de_dbg(c, "organization: \"%s\"", ucstring_getpsz_d(si->organization));
	}

	// Creation date
	dbuf_read(f, tmpbuf, pos+82, 8);
	if(sauce_is_valid_date_string(tmpbuf, 8)) {
		do_SAUCE_creation_date(c, si, tmpbuf, 8);
	}

	si->original_file_size = dbuf_getu32le(f, pos+90);
	de_dbg(c, "original file size: %d", (int)si->original_file_size);

	si->data_type = dbuf_getbyte(f, pos+94);
	name = get_sauce_datatype_name(si->data_type);
	de_dbg(c, "data type: %d (%s)", (int)si->data_type, name);

	si->file_type = dbuf_getbyte(f, pos+95);
	t = 256*(unsigned int)si->data_type + si->file_type;
	name = get_sauce_filetype_name(si->data_type, t);
	de_dbg(c, "file type: %d (%s)", (int)si->file_type, name);

	si->tinfo1 = (u16)dbuf_getu16le(f, pos+96);
	si->tinfo2 = (u16)dbuf_getu16le(f, pos+98);
	si->tinfo3 = (u16)dbuf_getu16le(f, pos+100);
	si->tinfo4 = (u16)dbuf_getu16le(f, pos+102);
	de_dbg(c, "TInfo1: %u", (unsigned int)si->tinfo1);
	de_dbg(c, "TInfo2: %u", (unsigned int)si->tinfo2);
	de_dbg(c, "TInfo3: %u", (unsigned int)si->tinfo3);
	de_dbg(c, "TInfo4: %u", (unsigned int)si->tinfo4);

	if(t==0x0100 || t==0x0101 || t==0x0102 || t==0x0104 || t==0x0105 || t==0x0108 || t==0x0600) {
		si->width_in_chars = (i64)si->tinfo1;
		de_dbg(c, "width in chars: %d", (int)si->width_in_chars);
	}
	if(t==0x0100 || t==0x0101 || t==0x0104 || t==0x0105 || t==0x0108 || t==0x0600) {
		si->number_of_lines = (i64)si->tinfo2;
		de_dbg(c, "number of lines: %d", (int)si->number_of_lines);
	}

	d->num_comments = (i64)dbuf_getbyte(f, pos+104);
	de_dbg(c, "num comments: %d", (int)d->num_comments);
	if(d->num_comments>0) {
		sauce_read_comments(c, d, f, si);
	}

	si->tflags = dbuf_getbyte(f, pos+105);
	if(si->tflags!=0) {
		tflags_descr = ucstring_create(c);
		if(t==0x0100 || t==0x0101 || t==0x0102 || si->data_type==5) {
			// ANSiFlags
			if(si->tflags&0x01) {
				ucstring_append_flags_item(tflags_descr, "non-blink mode");
			}
			if((si->tflags & 0x06)>>1 == 1) {
				ucstring_append_flags_item(tflags_descr, "8-pixel font");
			}
			else if((si->tflags & 0x06)>>1 == 2) {
				ucstring_append_flags_item(tflags_descr, "9-pixel font");
			}
			if((si->tflags & 0x18)>>3 == 1) {
				ucstring_append_flags_item(tflags_descr, "non-square pixels");
			}
			else if((si->tflags & 0x18)>>3 == 2) {
				ucstring_append_flags_item(tflags_descr, "square pixels");
			}

		}
		de_dbg(c, "tflags: 0x%02x (%s)", (unsigned int)si->tflags,
			ucstring_getpsz(tflags_descr));
	}

	if(si->original_file_size==0 || si->original_file_size>f->len-128) {
		// If this field seems bad, try to correct it.
		si->original_file_size = f->len-128-(5+d->num_comments*64);
	}

	retval = 1;
done:
	ucstring_destroy(tflags_descr);
	de_free(c, d);
	return retval;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq $0x0, -0x70(%rbp)
movl $0x0, -0x74(%rbp)
movq $0x0, -0x80(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
subq $0x80, %rax
movq %rax, -0x60(%rbp)
movq -0x10(%rbp), %rdi
movq -0x60(%rbp), %rsi
addq $0x0, %rsi
leaq 0x8a3b2(%rip), %rdx     # 0x1ce8f5
movl $0x7, %ecx
callq 0x166ab0
cmpl $0x0, %eax
je 0x144557
jmp 0x144c5d
movq -0x18(%rbp), %rax
movl $0x1, (%rax)
movq -0x8(%rbp), %rdi
movl $0x18, %esi
callq 0x15b780
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rax
movl $0xd, 0x10(%rax)
movq -0x80(%rbp), %rax
movl 0x10(%rax), %ecx
andl $0xff, %ecx
orl $0x300, %ecx            # imm = 0x300
movq -0x80(%rbp), %rax
movl %ecx, 0x14(%rax)
movq -0x8(%rbp), %rdi
leaq 0x8a35a(%rip), %rsi     # 0x1ce8fd
xorl %edx, %edx
callq 0x15cbc0
movl %eax, %ecx
movq -0x80(%rbp), %rax
movl %ecx, (%rax)
movq -0x10(%rbp), %rdi
leaq -0x50(%rbp), %rsi
movq -0x60(%rbp), %rdx
addq $0x7, %rdx
movl $0x23, %ecx
callq 0x164d10
leaq -0x50(%rbp), %rdi
movl $0x23, %esi
xorl %edx, %edx
callq 0x144c80
movq %rax, -0x58(%rbp)
cmpq $0x0, -0x58(%rbp)
jle 0x14464c
movq -0x8(%rbp), %rdi
callq 0x1711d0
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x8(%rbp), %rdi
leaq -0x50(%rbp), %rsi
movq -0x58(%rbp), %rdx
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x80(%rbp), %rax
movl 0x10(%rax), %r8d
callq 0x144d00
movq -0x8(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x172650
movq -0x88(%rbp), %rdi
movq %rax, %rdx
leaq 0x6f2dc(%rip), %rsi     # 0x1b3921
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rdi
leaq -0x50(%rbp), %rsi
movq -0x60(%rbp), %rdx
addq $0x2a, %rdx
movl $0x14, %ecx
callq 0x164d10
leaq -0x50(%rbp), %rdi
movl $0x14, %esi
xorl %edx, %edx
callq 0x144c80
movq %rax, -0x58(%rbp)
cmpq $0x0, -0x58(%rbp)
jle 0x1446e6
movq -0x8(%rbp), %rdi
callq 0x1711d0
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x8(%rbp), %rdi
leaq -0x50(%rbp), %rsi
movq -0x58(%rbp), %rdx
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rcx
movq -0x80(%rbp), %rax
movl 0x10(%rax), %r8d
callq 0x144d00
movq -0x8(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x172650
movq -0x90(%rbp), %rdi
movq %rax, %rdx
leaq 0x6f24e(%rip), %rsi     # 0x1b392d
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rdi
leaq -0x50(%rbp), %rsi
movq -0x60(%rbp), %rdx
addq $0x3e, %rdx
movl $0x14, %ecx
callq 0x164d10
leaq -0x50(%rbp), %rdi
movl $0x14, %esi
xorl %edx, %edx
callq 0x144c80
movq %rax, -0x58(%rbp)
cmpq $0x0, -0x58(%rbp)
jle 0x144780
movq -0x8(%rbp), %rdi
callq 0x1711d0
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x8(%rbp), %rdi
leaq -0x50(%rbp), %rsi
movq -0x58(%rbp), %rdx
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rcx
movq -0x80(%rbp), %rax
movl 0x10(%rax), %r8d
callq 0x144d00
movq -0x8(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x172650
movq -0x98(%rbp), %rdi
movq %rax, %rdx
leaq 0x8a19a(%rip), %rsi     # 0x1ce913
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rdi
leaq -0x50(%rbp), %rsi
movq -0x60(%rbp), %rdx
addq $0x52, %rdx
movl $0x8, %ecx
callq 0x164d10
leaq -0x50(%rbp), %rdi
movl $0x8, %esi
callq 0x144d40
cmpl $0x0, %eax
je 0x1447c3
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
leaq -0x50(%rbp), %rdx
movl $0x8, %ecx
callq 0x144dd0
movq -0x10(%rbp), %rdi
movq -0x60(%rbp), %rsi
addq $0x5a, %rsi
callq 0x165540
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x30(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rax
movl %eax, %edx
leaq 0x8a132(%rip), %rsi     # 0x1ce926
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rdi
movq -0x60(%rbp), %rsi
addq $0x5e, %rsi
callq 0x165060
movb %al, %cl
movq -0x18(%rbp), %rax
movb %cl, 0x38(%rax)
movq -0x18(%rbp), %rax
movzbl 0x38(%rax), %edi
callq 0x144ee0
movq %rax, -0x68(%rbp)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movzbl 0x38(%rax), %edx
movq -0x68(%rbp), %rcx
leaq 0x8a100(%rip), %rsi     # 0x1ce93d
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rdi
movq -0x60(%rbp), %rsi
addq $0x5f, %rsi
callq 0x165060
movb %al, %cl
movq -0x18(%rbp), %rax
movb %cl, 0x39(%rax)
movq -0x18(%rbp), %rax
movzbl 0x38(%rax), %eax
shll $0x8, %eax
movq -0x18(%rbp), %rcx
movzbl 0x39(%rcx), %ecx
addl %ecx, %eax
movl %eax, -0x1c(%rbp)
movq -0x18(%rbp), %rax
movb 0x38(%rax), %al
movl -0x1c(%rbp), %esi
movzbl %al, %edi
callq 0x144fa0
movq %rax, -0x68(%rbp)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movzbl 0x39(%rax), %edx
movq -0x68(%rbp), %rcx
leaq 0x8a0ad(%rip), %rsi     # 0x1ce950
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rdi
movq -0x60(%rbp), %rsi
addq $0x60, %rsi
callq 0x165280
movw %ax, %cx
movq -0x18(%rbp), %rax
movw %cx, 0x58(%rax)
movq -0x10(%rbp), %rdi
movq -0x60(%rbp), %rsi
addq $0x62, %rsi
callq 0x165280
movw %ax, %cx
movq -0x18(%rbp), %rax
movw %cx, 0x5a(%rax)
movq -0x10(%rbp), %rdi
movq -0x60(%rbp), %rsi
addq $0x64, %rsi
callq 0x165280
movw %ax, %cx
movq -0x18(%rbp), %rax
movw %cx, 0x5c(%rax)
movq -0x10(%rbp), %rdi
movq -0x60(%rbp), %rsi
addq $0x66, %rsi
callq 0x165280
movw %ax, %cx
movq -0x18(%rbp), %rax
movw %cx, 0x5e(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movzwl 0x58(%rax), %edx
leaq 0x8a036(%rip), %rsi     # 0x1ce963
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movzwl 0x5a(%rax), %edx
leaq 0x8a027(%rip), %rsi     # 0x1ce96e
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movzwl 0x5c(%rax), %edx
leaq 0x8a018(%rip), %rsi     # 0x1ce979
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movzwl 0x5e(%rax), %edx
leaq 0x8a009(%rip), %rsi     # 0x1ce984
movb $0x0, %al
callq 0x15b0d0
cmpl $0x100, -0x1c(%rbp)     # imm = 0x100
je 0x1449c1
cmpl $0x101, -0x1c(%rbp)     # imm = 0x101
je 0x1449c1
cmpl $0x102, -0x1c(%rbp)     # imm = 0x102
je 0x1449c1
cmpl $0x104, -0x1c(%rbp)     # imm = 0x104
je 0x1449c1
cmpl $0x105, -0x1c(%rbp)     # imm = 0x105
je 0x1449c1
cmpl $0x108, -0x1c(%rbp)     # imm = 0x108
je 0x1449c1
cmpl $0x600, -0x1c(%rbp)     # imm = 0x600
jne 0x1449ef
movq -0x18(%rbp), %rax
movzwl 0x58(%rax), %eax
movl %eax, %ecx
movq -0x18(%rbp), %rax
movq %rcx, 0x40(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x40(%rax), %rax
movl %eax, %edx
leaq 0x89fa7(%rip), %rsi     # 0x1ce98f
movb $0x0, %al
callq 0x15b0d0
cmpl $0x100, -0x1c(%rbp)     # imm = 0x100
je 0x144a25
cmpl $0x101, -0x1c(%rbp)     # imm = 0x101
je 0x144a25
cmpl $0x104, -0x1c(%rbp)     # imm = 0x104
je 0x144a25
cmpl $0x105, -0x1c(%rbp)     # imm = 0x105
je 0x144a25
cmpl $0x108, -0x1c(%rbp)     # imm = 0x108
je 0x144a25
cmpl $0x600, -0x1c(%rbp)     # imm = 0x600
jne 0x144a53
movq -0x18(%rbp), %rax
movzwl 0x5a(%rax), %eax
movl %eax, %ecx
movq -0x18(%rbp), %rax
movq %rcx, 0x48(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rax
movl %eax, %edx
leaq 0x89f56(%rip), %rsi     # 0x1ce9a2
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rdi
movq -0x60(%rbp), %rsi
addq $0x68, %rsi
callq 0x165060
movzbl %al, %eax
movl %eax, %ecx
movq -0x80(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x8(%rbp), %rdi
movq -0x80(%rbp), %rax
movq 0x8(%rax), %rax
movl %eax, %edx
leaq 0x6f56d(%rip), %rsi     # 0x1b3ff3
movb $0x0, %al
callq 0x15b0d0
movq -0x80(%rbp), %rax
cmpq $0x0, 0x8(%rax)
jle 0x144aad
movq -0x8(%rbp), %rdi
movq -0x80(%rbp), %rsi
movq -0x10(%rbp), %rdx
movq -0x18(%rbp), %rcx
callq 0x145190
movq -0x10(%rbp), %rdi
movq -0x60(%rbp), %rsi
addq $0x69, %rsi
callq 0x165060
movb %al, %cl
movq -0x18(%rbp), %rax
movb %cl, 0x3a(%rax)
movq -0x18(%rbp), %rax
movzbl 0x3a(%rax), %eax
cmpl $0x0, %eax
je 0x144c05
movq -0x8(%rbp), %rdi
callq 0x1711d0
movq %rax, -0x70(%rbp)
cmpl $0x100, -0x1c(%rbp)     # imm = 0x100
je 0x144b11
cmpl $0x101, -0x1c(%rbp)     # imm = 0x101
je 0x144b11
cmpl $0x102, -0x1c(%rbp)     # imm = 0x102
je 0x144b11
movq -0x18(%rbp), %rax
movzbl 0x38(%rax), %eax
cmpl $0x5, %eax
jne 0x144bc5
movq -0x18(%rbp), %rax
movzbl 0x3a(%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x144b31
movq -0x70(%rbp), %rdi
leaq 0x89e8a(%rip), %rsi     # 0x1ce9b6
callq 0x172680
movq -0x18(%rbp), %rax
movzbl 0x3a(%rax), %eax
andl $0x6, %eax
sarl %eax
cmpl $0x1, %eax
jne 0x144b55
movq -0x70(%rbp), %rdi
leaq 0x89e77(%rip), %rsi     # 0x1ce9c5
callq 0x172680
jmp 0x144b79
movq -0x18(%rbp), %rax
movzbl 0x3a(%rax), %eax
andl $0x6, %eax
sarl %eax
cmpl $0x2, %eax
jne 0x144b77
movq -0x70(%rbp), %rdi
leaq 0x89e60(%rip), %rsi     # 0x1ce9d2
callq 0x172680
jmp 0x144b79
movq -0x18(%rbp), %rax
movzbl 0x3a(%rax), %eax
andl $0x18, %eax
sarl $0x3, %eax
cmpl $0x1, %eax
jne 0x144b9e
movq -0x70(%rbp), %rdi
leaq 0x89e48(%rip), %rsi     # 0x1ce9df
callq 0x172680
jmp 0x144bc3
movq -0x18(%rbp), %rax
movzbl 0x3a(%rax), %eax
andl $0x18, %eax
sarl $0x3, %eax
cmpl $0x2, %eax
jne 0x144bc1
movq -0x70(%rbp), %rdi
leaq 0x89e27(%rip), %rsi     # 0x1ce9e3
callq 0x172680
jmp 0x144bc3
jmp 0x144bc5
movq -0x8(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0x18(%rbp), %rax
movzbl 0x3a(%rax), %eax
movl %eax, -0x9c(%rbp)
movq -0x70(%rbp), %rdi
callq 0x172500
movq -0xa8(%rbp), %rdi
movl -0x9c(%rbp), %edx
movq %rax, %rcx
leaq 0x89df3(%rip), %rsi     # 0x1ce9f1
movb $0x0, %al
callq 0x15b0d0
movq -0x18(%rbp), %rax
cmpq $0x0, 0x30(%rax)
je 0x144c2c
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x18(%rcx), %rcx
subq $0x80, %rcx
cmpq %rcx, %rax
jle 0x144c56
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rcx
subq $0x80, %rcx
movq -0x80(%rbp), %rax
movq 0x8(%rax), %rax
shlq $0x6, %rax
addq $0x5, %rax
subq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x30(%rax)
movl $0x1, -0x74(%rbp)
movq -0x70(%rbp), %rdi
callq 0x171750
movq -0x8(%rbp), %rdi
movq -0x80(%rbp), %rsi
callq 0x15b820
movl -0x74(%rbp), %eax
addq $0xb0, %rsp
popq %rbp
retq
nop