// File: /workspace/github_c_makefile_star2_O3/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 $0x28, %rsp
movl %r9d, 0xc(%rsp)
movl %r8d, 0x8(%rsp)
movq %rcx, %r13
movq %rdx, %r15
movq %rsi, %rbp
movq %rdi, %r12
movq 0x8(%rdi), %r14
movl $0x38, %esi
movq %r14, %rdi
callq 0xe8b2b
movq %rax, %rbx
movq %r14, 0x18(%rsp)
movq %r14, %rdi
callq 0xf6410
movq %rax, 0x18(%rbx)
xorl %r14d, %r14d
testq %r15, %r15
cmovleq %r14, %r15
testq %r13, %r13
cmovleq %r14, %r13
movq %r12, 0x20(%rsp)
movq 0x18(%r12), %r12
movq %rbp, 0x10(%rsp)
subq %rbp, %r12
cmpq %r12, %r15
cmovlq %r15, %r12
testq %r12, %r12
cmovgq %r12, %r14
movq %r14, (%rbx)
cmpq %r13, %r15
sete %al
movl 0x8(%rsp), %ecx
movl %ecx, %ebp
orb %cl, %al
testb $0x1, %al
je 0xef941
testb $0x1, %bpl
je 0xef960
testq %r12, %r12
jle 0xef934
movq 0x10(%rsp), %r15
movq 0x20(%rsp), %rdi
movq %r15, %rsi
callq 0xeead3
testb %al, %al
je 0xef976
incq %r15
decq %r12
jne 0xef91b
movq 0x10(%rsp), %rax
addq %rax, %r14
movq %r14, %r15
jmp 0xef982
incq %r13
movq 0x18(%rsp), %r14
movq %r14, %rdi
movq %r13, %rsi
callq 0xe8b2b
movq %rax, 0x8(%rbx)
xorl %r15d, %r15d
jmp 0xefa23
movq %r13, (%rbx)
movq %r13, %r15
movq 0x18(%rsp), %r14
movl 0x8(%rsp), %ebp
movl 0xc(%rsp), %r12d
jmp 0xef9ab
movl $0x1, 0x34(%rbx)
movq 0x10(%rsp), %rax
subq %rax, %r15
leaq 0x1(%r15), %rax
movq %rax, (%rbx)
cmpq %r13, %r15
movq 0x18(%rsp), %r14
movl 0xc(%rsp), %r12d
jle 0xef9a4
movl $0x1, 0x30(%rbx)
jmp 0xef9a7
movq %r15, %r13
movl 0x8(%rsp), %ebp
leaq 0x1(%r13), %rsi
movq %r14, %rdi
callq 0xe8b2b
movq %rax, 0x8(%rbx)
movq 0x20(%rsp), %rdi
movq %rax, %rsi
movq 0x10(%rsp), %rdx
movq %r13, %rcx
callq 0xee8d4
movq 0x8(%rbx), %rsi
movq 0x18(%rbx), %rdi
movq %r13, %rdx
xorl %ecx, %ecx
movl %r12d, %r8d
callq 0xf6cdd
testb $0x10, %bpl
je 0xefa23
movq 0x18(%rbx), %rdi
callq 0xf6d4d
movq %rax, 0x28(%rbx)
leaq 0x1(%rax), %rsi
movq %r14, %rdi
callq 0xe8b2b
movq %rax, 0x20(%rbx)
movq 0x18(%rbx), %rdi
movq 0x28(%rbx), %rdx
incq %rdx
movq %rax, %rsi
xorl %ecx, %ecx
movl $0x2, %r8d
callq 0xf6e1a
cmpq $0x0, 0x8(%rbx)
jne 0xefa3b
movl $0x1, %esi
movq %r14, %rdi
callq 0xe8b2b
movq %rax, 0x8(%rbx)
testb $0x10, %bpl
je 0xefa61
cmpq $0x0, 0x20(%rbx)
jne 0xefa61
movl $0x1, %esi
movq %r14, %rdi
callq 0xe8b2b
movq %rax, 0x20(%rbx)
movq $0x0, 0x28(%rbx)
movq %r15, 0x10(%rbx)
movq %rbx, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq