// File: /workspace/github_c_makefile_star2_O2/jsummers[P]deark/src/deark-dbuf.c
// Function: dbuf_read_string
struct de_stringreaderdata *dbuf_read_string(dbuf *f, i64 pos,
	i64 max_bytes_to_scan,
	i64 max_bytes_to_keep,
	unsigned int flags, de_ext_encoding ee)
{
	deark *c = f->c;
	struct de_stringreaderdata *srd;
	i64 foundpos = 0;
	int ret;
	i64 bytes_avail_to_read;
	i64 bytes_to_malloc;
	i64 x_strlen = 0;

	srd = de_malloc(c, sizeof(struct de_stringreaderdata));
	srd->str = ucstring_create(c);
	if(max_bytes_to_scan<0) max_bytes_to_scan = 0;
	if(max_bytes_to_keep<0) max_bytes_to_keep = 0;

	bytes_avail_to_read = max_bytes_to_scan;
	if(bytes_avail_to_read > f->len-pos) {
		bytes_avail_to_read = f->len-pos;
	}
	if(bytes_avail_to_read<0) bytes_avail_to_read = 0;

	srd->bytes_consumed = bytes_avail_to_read; // default

	// From here on, we can safely bail out ("goto done"). The
	// de_stringreaderdata struct is sufficiently valid.

	if(!(flags&DE_CONVFLAG_STOP_AT_NUL) &&
		(max_bytes_to_scan != max_bytes_to_keep))
	{
		// To reduce possible confusion, we require that
		// max_bytes_to_scan==max_bytes_to_keep in this case.
		srd->sz = de_malloc(c, max_bytes_to_keep+1);
		goto done;
	}

	if(flags&DE_CONVFLAG_STOP_AT_NUL) {
		ret = dbuf_search_byte(f, 0x00, pos, bytes_avail_to_read, &foundpos);
		if(ret) {
			srd->found_nul = 1;
		}
		else {
			// No NUL byte found. Could be an error in some formats, but in
			// others NUL is used as separator or as padding, not a terminator.
			foundpos = pos+bytes_avail_to_read;
		}

		x_strlen = foundpos-pos;
		srd->bytes_consumed = x_strlen+1;
	}
	else {
		x_strlen = max_bytes_to_keep;
		srd->bytes_consumed = x_strlen;
	}

	bytes_to_malloc = x_strlen+1;
	if(bytes_to_malloc>(max_bytes_to_keep+1)) {
		bytes_to_malloc = max_bytes_to_keep+1;
		srd->was_truncated = 1;
	}

	srd->sz = de_malloc(c, bytes_to_malloc);
	dbuf_read(f, (u8*)srd->sz, pos, bytes_to_malloc-1); // The last byte remains NUL

	ucstring_append_bytes(srd->str, (const u8*)srd->sz, bytes_to_malloc-1, 0, ee);

	if(flags&DE_CONVFLAG_WANT_UTF8) {
		srd->sz_utf8_strlen = (size_t)ucstring_count_utf8_bytes(srd->str);
		srd->sz_utf8 = de_malloc(c, (i64)srd->sz_utf8_strlen + 1);
		ucstring_to_sz(srd->str, srd->sz_utf8, srd->sz_utf8_strlen + 1, 0, DE_ENCODING_UTF8);
	}

done:
	if(!srd->sz) {
		// Always return a valid sz, even on failure.
		srd->sz = de_malloc(c, 1);
	}
	if((flags&DE_CONVFLAG_WANT_UTF8) && !srd->sz_utf8) {
		// Always return a valid sz_utf8 if it was requested, even on failure.
		srd->sz_utf8 = de_malloc(c, 1);
		srd->sz_utf8_strlen = 0;
	}
	srd->sz_strlen = (size_t)x_strlen;
	return srd;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %r9d, (%rsp)
movl %r8d, 0x4(%rsp)
movq %rcx, %r13
movq %rdx, %r14
movq %rsi, %rbp
movq %rdi, %r15
movq 0x8(%rdi), %r12
andq $0x0, 0x10(%rsp)
pushq $0x38
popq %rsi
movq %r12, %rdi
callq 0xdecaf
movq %rax, %rbx
movq %r12, %rdi
callq 0xeac74
movq %rax, 0x18(%rbx)
xorl %eax, %eax
testq %r14, %r14
cmovleq %rax, %r14
testq %r13, %r13
cmovleq %rax, %r13
movq %r15, 0x8(%rsp)
movq 0x18(%r15), %r15
movq %rbp, %rcx
subq %rbp, %r15
movl 0x4(%rsp), %ebp
cmpq %r15, %r14
cmovlq %r14, %r15
testq %r15, %r15
cmovleq %rax, %r15
movq %r15, (%rbx)
cmpq %r13, %r14
sete %al
orb %bpl, %al
testb $0x1, %al
je 0xe4a19
movq %r12, %r14
testb $0x1, %bpl
je 0xe4a36
leaq 0x10(%rsp), %r8
movq 0x8(%rsp), %rdi
xorl %esi, %esi
movq %rcx, %r12
movq %rcx, %rdx
movq %r15, %rcx
callq 0xe4b21
testl %eax, %eax
je 0xe4a41
movl $0x1, 0x34(%rbx)
movq 0x10(%rsp), %r15
jmp 0xe4a44
incq %r13
movq %r12, %rdi
movq %r13, %rsi
callq 0xdecaf
movq %rax, 0x8(%rbx)
xorl %r15d, %r15d
movq %r12, %r14
jmp 0xe4ad4
movq %r13, (%rbx)
movq %r13, %r15
movq %rcx, %r12
jmp 0xe4a5f
addq %r12, %r15
subq %r12, %r15
leaq 0x1(%r15), %rax
movq %rax, (%rbx)
cmpq %r13, %r15
jle 0xe4a5c
movl $0x1, 0x30(%rbx)
jmp 0xe4a5f
movq %r15, %r13
leaq 0x1(%r13), %rsi
movq %r14, %rdi
callq 0xdecaf
movq %rax, 0x8(%rbx)
movq 0x8(%rsp), %rdi
movq %rax, %rsi
movq %r12, %rdx
movq %r13, %rcx
callq 0xe3cb7
movq 0x8(%rbx), %rsi
movq 0x18(%rbx), %rdi
movq %r13, %rdx
xorl %ecx, %ecx
movl (%rsp), %r8d
callq 0xeb4a9
testb $0x10, %bpl
je 0xe4ad4
movq 0x18(%rbx), %rdi
callq 0xeb519
movq %rax, 0x28(%rbx)
leaq 0x1(%rax), %rsi
movq %r14, %rdi
callq 0xdecaf
movq %rax, 0x20(%rbx)
movq 0x18(%rbx), %rdi
movq 0x28(%rbx), %rdx
incq %rdx
pushq $0x2
popq %r8
movq %rax, %rsi
xorl %ecx, %ecx
callq 0xeb5de
cmpq $0x0, 0x8(%rbx)
jne 0xe4aea
pushq $0x1
popq %rsi
movq %r14, %rdi
callq 0xdecaf
movq %rax, 0x8(%rbx)
testb $0x10, %bpl
je 0xe4b0b
cmpq $0x0, 0x20(%rbx)
jne 0xe4b0b
pushq $0x1
popq %rsi
movq %r14, %rdi
callq 0xdecaf
movq %rax, 0x20(%rbx)
andq $0x0, 0x28(%rbx)
movq %r15, 0x10(%rbx)
movq %rbx, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq