// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/cfb.c
// Function: read_thumbsdb_catalog
static int read_thumbsdb_catalog(deark *c, lctx *d, struct dir_entry_info *dei)
{
	i64 item_len;
	i64 n;
	i64 i;
	i64 pos;
	int retval = 0;
	dbuf *catf = NULL;

	if(d->thumbsdb_catalog) return 0; // Already read a catalog

	de_dbg(c, "reading thumbsdb catalog");
	de_dbg_indent(c, 1);

	catf = dbuf_create_membuf(c, dei->stream_size, 0);
	copy_any_stream_to_dbuf(c, d, dei, 0, dei->stream_size, catf);

	item_len = dbuf_getu16le(catf, 0);
	de_dbg(c, "header size: %d", (int)item_len); // (?)
	if(item_len!=16) goto done;

	n = dbuf_getu16le(catf, 2);
	de_dbg(c, "catalog version: %d", (int)n); // (?)
	if(n!=5 && n!=6 && n!=7) {
		de_warn(c, "Unsupported Catalog version: %d", (int)n);
		goto done;
	}

	d->thumbsdb_catalog_num_entries = dbuf_getu16le(catf, 4); // This might really be a 4 byte int.
	de_dbg(c, "num entries: %d", (int)d->thumbsdb_catalog_num_entries);
	if(d->thumbsdb_catalog_num_entries>2048)
		d->thumbsdb_catalog_num_entries = 2048;

	d->thumbsdb_catalog = de_mallocarray(c, d->thumbsdb_catalog_num_entries,
		sizeof(struct thumbsdb_catalog_entry));

	pos = item_len;

	for(i=0; i<d->thumbsdb_catalog_num_entries; i++) {
		i64 name_len;

		if(pos >= catf->len) goto done;
		item_len = dbuf_getu32le(catf, pos);
		de_dbg(c, "catalog entry #%d, len=%d", (int)i, (int)item_len);
		if(item_len<20) goto done;
		if(pos+item_len > catf->len) goto done;

		de_dbg_indent(c, 1);

		d->thumbsdb_catalog[i].id = (u32)dbuf_getu32le(catf, pos+4);
		de_dbg(c, "id: %u", (unsigned int)d->thumbsdb_catalog[i].id);

		read_and_cvt_timestamp(c, catf, pos+8, &d->thumbsdb_catalog[i].mod_time);
		dbg_timestamp(c, &d->thumbsdb_catalog[i].mod_time, "timestamp");

		name_len = de_min_int(item_len-20, 65536);
		d->thumbsdb_catalog[i].fname_srd = dbuf_read_string(catf, pos+16, name_len, name_len,
			DE_CONVFLAG_WANT_UTF8, DE_ENCODING_UTF16LE);
		de_dbg(c, "name: \"%s\"", ucstring_getpsz(d->thumbsdb_catalog[i].fname_srd->str));

		de_dbg_indent(c, -1);

		pos += item_len;
	}

	retval = 1;
done:
	de_dbg_indent(c, -1);
	dbuf_close(catf);
	if(!retval) {
		d->thumbsdb_catalog_num_entries = 0; // Make sure we don't use a bad catalog.
	}
	return retval;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movl $0x0, -0x44(%rbp)
movq $0x0, -0x50(%rbp)
movq -0x18(%rbp), %rax
cmpq $0x0, 0xb0(%rax)
je 0x539bd
movl $0x0, -0x4(%rbp)
jmp 0x53d6a
movq -0x10(%rbp), %rdi
leaq 0x152106(%rip), %rsi    # 0x1a5ace
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rsi
xorl %edx, %edx
callq 0x168020
movq %rax, -0x50(%rbp)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
movq -0x20(%rbp), %rax
movq 0x10(%rax), %r8
movq -0x50(%rbp), %r9
xorl %eax, %eax
movl %eax, %ecx
callq 0x543d0
movq -0x50(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x165280
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rax
movl %eax, %edx
leaq 0x15f6d7(%rip), %rsi    # 0x1b310e
movb $0x0, %al
callq 0x15b0d0
cmpq $0x10, -0x28(%rbp)
je 0x53a4a
jmp 0x53d38
movq -0x50(%rbp), %rdi
movl $0x2, %esi
callq 0x165280
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rdi
movq -0x30(%rbp), %rax
movl %eax, %edx
leaq 0x15207a(%rip), %rsi    # 0x1a5ae7
movb $0x0, %al
callq 0x15b0d0
cmpq $0x5, -0x30(%rbp)
je 0x53aa6
cmpq $0x6, -0x30(%rbp)
je 0x53aa6
cmpq $0x7, -0x30(%rbp)
je 0x53aa6
movq -0x10(%rbp), %rdi
movq -0x30(%rbp), %rax
movl %eax, %edx
leaq 0x152061(%rip), %rsi    # 0x1a5afb
movb $0x0, %al
callq 0x15bc10
jmp 0x53d38
movq -0x50(%rbp), %rdi
movl $0x4, %esi
callq 0x165280
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0xa8(%rax)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0xa8(%rax), %rax
movl %eax, %edx
leaq 0x152041(%rip), %rsi    # 0x1a5b1b
movb $0x0, %al
callq 0x15b0d0
movq -0x18(%rbp), %rax
cmpq $0x800, 0xa8(%rax) # imm = 0x800
jle 0x53b01
movq -0x18(%rbp), %rax
movq $0x800, 0xa8(%rax) # imm = 0x800
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0xa8(%rax), %rsi
movl $0x20, %edx
callq 0x15c450
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0xb0(%rax)
movq -0x28(%rbp), %rax
movq %rax, -0x40(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq -0x18(%rbp), %rcx
cmpq 0xa8(%rcx), %rax
jge 0x53d31
movq -0x40(%rbp), %rax
movq -0x50(%rbp), %rcx
cmpq 0x18(%rcx), %rax
jl 0x53b60
jmp 0x53d38
movq -0x50(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x165540
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rdi
movq -0x38(%rbp), %rax
movl %eax, %edx
movq -0x28(%rbp), %rax
movl %eax, %ecx
leaq 0x151fa3(%rip), %rsi    # 0x1a5b2b
movb $0x0, %al
callq 0x15b0d0
cmpq $0x14, -0x28(%rbp)
jge 0x53b9b
jmp 0x53d38
movq -0x40(%rbp), %rax
addq -0x28(%rbp), %rax
movq -0x50(%rbp), %rcx
cmpq 0x18(%rcx), %rax
jle 0x53bb2
jmp 0x53d38
movq -0x10(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x50(%rbp), %rdi
movq -0x40(%rbp), %rsi
addq $0x4, %rsi
callq 0x165540
movl %eax, %ecx
movq -0x18(%rbp), %rax
movq 0xb0(%rax), %rax
movq -0x38(%rbp), %rdx
shlq $0x5, %rdx
addq %rdx, %rax
movl %ecx, (%rax)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0xb0(%rax), %rax
movq -0x38(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl (%rax), %edx
leaq 0x166c21(%rip), %rsi    # 0x1ba82f
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rdi
movq -0x50(%rbp), %rsi
movq -0x40(%rbp), %rdx
addq $0x8, %rdx
movq -0x18(%rbp), %rax
movq 0xb0(%rax), %rcx
movq -0x38(%rbp), %rax
shlq $0x5, %rax
addq %rax, %rcx
addq $0x10, %rcx
callq 0x52fa0
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0xb0(%rax), %rsi
movq -0x38(%rbp), %rax
shlq $0x5, %rax
addq %rax, %rsi
addq $0x10, %rsi
leaq 0x17bdad(%rip), %rdx    # 0x1cfa16
callq 0x53890
movq -0x28(%rbp), %rdi
subq $0x14, %rdi
movl $0x10000, %esi          # imm = 0x10000
callq 0x15ccf0
movq %rax, -0x58(%rbp)
movq -0x50(%rbp), %rdi
movq -0x40(%rbp), %rsi
addq $0x10, %rsi
movq -0x58(%rbp), %rdx
movq -0x58(%rbp), %rcx
movl $0x10, %r8d
movl $0x3, %r9d
callq 0x1664c0
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq 0xb0(%rax), %rax
movq -0x38(%rbp), %rdx
shlq $0x5, %rdx
addq %rdx, %rax
movq %rcx, 0x8(%rax)
movq -0x10(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x18(%rbp), %rax
movq 0xb0(%rax), %rax
movq -0x38(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
movq 0x18(%rax), %rdi
callq 0x172500
movq -0x60(%rbp), %rdi
movq %rax, %rdx
leaq 0x15a4d1(%rip), %rsi    # 0x1ae1d0
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movq -0x28(%rbp), %rax
addq -0x40(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x53b38
movl $0x1, -0x44(%rbp)
movq -0x10(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movq -0x50(%rbp), %rdi
callq 0x1676a0
cmpl $0x0, -0x44(%rbp)
jne 0x53d64
movq -0x18(%rbp), %rax
movq $0x0, 0xa8(%rax)
movl -0x44(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)