// File: /workspace/github_c_makefile_star2_O2/jsummers[P]deark/src/deark-dbuf.c
// Function: dbuf_read
void dbuf_read(dbuf *f, u8 *buf, i64 pos, i64 len)
{
	i64 bytes_read = 0;
	i64 bytes_to_read;
	deark *c;

	c = f->c;

	if(len <= 0) goto done_read;
	if(len > DE_MAX_MALLOC) {
		de_fatalerror(c);
		return;
	}

	if(pos < 0) {
		if(pos <= (-len)) {
			// All requested bytes are before the beginning of the file
			de_zeromem(buf, (size_t)len);
			return;
		}
		// Some requested bytes are before the beginning of the file.
		// Zero out the ones that are:
		de_zeromem(buf, (size_t)(-pos));
		// And adjust the parameters:
		buf += (-pos);
		len -= (-pos);
		pos = 0;
	}

	bytes_to_read = len;
	if(pos >= f->len) {
		bytes_to_read = 0;
	}
	else if(pos + bytes_to_read > f->len) {
		bytes_to_read = f->len - pos;
	}

	if(bytes_to_read<1) {
		goto done_read;
	}

	// If the data we need is all cached, get it from cache.
	if(f->rcache &&
		pos >= 0 &&
		pos + bytes_to_read <= f->rcache_bytes_used)
	{
		de_memcpy(buf, &f->rcache[pos], (size_t)bytes_to_read);
		bytes_read = bytes_to_read;
		goto done_read;
	}

	switch(f->btype) {
	case DBUF_TYPE_IFILE:
		if(!f->fp) {
			de_internal_err_fatal(c, "File not open");
			goto done_read;
		}

		// For performance reasons, don't call fseek if we're already at the
		// right position.
		if(!f->file_pos_known || f->file_pos!=pos) {
			de_fseek(f->fp, pos, SEEK_SET);
		}

		bytes_read = fread(buf, 1, (size_t)bytes_to_read, f->fp);

		f->file_pos = pos + bytes_read;
		f->file_pos_known = 1;
		break;

	case DBUF_TYPE_IDBUF:
		// Recursive call to the parent dbuf.
		dbuf_read(f->parent_dbuf, buf, f->offset_into_parent_dbuf+pos, bytes_to_read);

		// The parent dbuf always writes 'bytes_to_read' bytes.
		bytes_read = bytes_to_read;
		break;

	case DBUF_TYPE_MEMBUF:
		de_memcpy(buf, &f->membuf_buf[pos], (size_t)bytes_to_read);
		bytes_read = bytes_to_read;
		break;

	default:
		de_internal_err_fatal(c, "getbytes from this I/O type not implemented");
		goto done_read;
	}

done_read:
	// Zero out any requested bytes that were not read.
	if(bytes_read < len) {
		de_zeromem(buf+bytes_read, (size_t)(len - bytes_read));
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rcx, %rbx
movq %rsi, %r14
testq %rcx, %rcx
jle 0xe3de6
movq %rdi, %r12
movq 0x8(%rdi), %rdi
cmpq $0x1dcd6501, %rbx       # imm = 0x1DCD6501
jb 0xe3cf4
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xdf4d0
movq %rdx, %r15
testq %rdx, %rdx
jns 0xe3d2a
movq %rbx, %rax
negq %rax
cmpq %r15, %rax
jae 0xe3df1
movq %r15, %rdx
negq %rdx
movq %rdi, %r13
movq %r14, %rdi
xorl %esi, %esi
callq 0x18140
movq %r13, %rdi
subq %r15, %r14
addq %r15, %rbx
xorl %r15d, %r15d
movq 0x18(%r12), %rax
movq %rax, %r13
subq %r15, %r13
leaq (%rbx,%r15), %rcx
cmpq %rax, %rcx
cmovleq %rbx, %r13
cmpq %r15, %rax
movl $0x0, %ebp
cmovgq %r13, %rbp
testq %rbp, %rbp
jle 0xe3de6
movq 0xc8(%r12), %rsi
testq %rsi, %rsi
je 0xe3d74
leaq (%r15,%rbp), %rax
cmpq 0xc0(%r12), %rax
jle 0xe3e1b
movl (%r12), %eax
cmpl $0x4, %eax
je 0xe3e38
cmpl $0x3, %eax
je 0xe3e23
cmpl $0x1, %eax
jne 0xe3e52
movq 0x10(%r12), %rcx
testq %rcx, %rcx
je 0xe3e5b
cmpl $0x0, 0x34(%r12)
je 0xe3db0
cmpq %r15, 0x38(%r12)
je 0xe3dc2
movq %rcx, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0xfcb29
movq 0x10(%r12), %rcx
pushq $0x1
popq %rsi
movq %r14, %rdi
movq %rbp, %rdx
callq 0x18080
movq %rax, %r13
addq %rax, %r15
movq %r15, 0x38(%r12)
movl $0x1, 0x34(%r12)
jmp 0xe3de9
xorl %r13d, %r13d
subq %r13, %rbx
jle 0xe3e0c
addq %r13, %r14
movq %r14, %rdi
xorl %esi, %esi
movq %rbx, %rdx
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x18140
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
addq %r15, %rsi
movq %r14, %rdi
jmp 0xe3e2e
addq 0x78(%r12), %r15
movq %r14, %rdi
movq %r15, %rsi
movq %rbp, %rdx
callq 0x181e0
jmp 0xe3de9
movq 0x50(%r12), %rdi
addq 0x58(%r12), %r15
movq %r14, %rsi
movq %r15, %rdx
movq %rbp, %rcx
callq 0xe3cb7
jmp 0xe3de9
leaq 0x57ec5(%rip), %rsi     # 0x13bd1e
jmp 0xe3e62
leaq 0x57eae(%rip), %rsi     # 0x13bd10
xorl %r13d, %r13d
xorl %eax, %eax
callq 0xdf4f0
jmp 0xe3de9