// File: /workspace/github_c_makefile_star2_O1/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, 0x1c(%rsp)
movl %r8d, 0xc(%rsp)
movq %rcx, %r13
movq %rdx, %r15
movq %rsi, %rbp
movq %rdi, %r14
movq 0x8(%rdi), %r12
movl $0x38, %esi
movq %r12, %rdi
callq 0xe969b
movq %rax, %rbx
movq %r12, %rdi
callq 0xf6870
movq %r13, %rsi
movq %rax, 0x18(%rbx)
xorl %r13d, %r13d
testq %r15, %r15
cmovleq %r13, %r15
testq %rsi, %rsi
cmovleq %r13, %rsi
movq %r14, %rcx
movq 0x18(%r14), %r14
movq %rbp, 0x10(%rsp)
subq %rbp, %r14
movl 0xc(%rsp), %ebp
cmpq %r14, %r15
cmovlq %r15, %r14
testq %r14, %r14
cmovgq %r14, %r13
movq %r13, (%rbx)
cmpq %rsi, %r15
sete %al
orb %bpl, %al
testb $0x1, %al
je 0xeff32
movq %rsi, %rax
movq %rsi, %r15
testb $0x1, %bpl
movq %rcx, %rbp
je 0xeff72
testq %r14, %r14
jle 0xeff49
movq %rsi, 0x20(%rsp)
movq 0x10(%rsp), %r15
movq %rbp, %rdi
movq %r15, %rsi
callq 0xef0d5
testb %al, %al
setne %al
je 0xeff2b
incq %r15
decq %r14
jne 0xeff0e
xorl %r15d, %r15d
movq 0x20(%rsp), %rsi
jmp 0xeff4e
incq %rsi
movq %r12, %rdi
callq 0xe969b
movq %rax, 0x8(%rbx)
xorl %r15d, %r15d
jmp 0xf0003
movb $0x1, %al
xorl %r15d, %r15d
testb %al, %al
je 0xeff5f
movq 0x10(%rsp), %rax
addq %rax, %r13
movq %r13, %r15
jmp 0xeff6b
movl $0x1, 0x34(%rbx)
movq 0x10(%rsp), %rax
subq %rax, %r15
leaq 0x1(%r15), %rax
movq %rax, (%rbx)
movq %r15, %r13
cmpq %rsi, %r15
jle 0xeff87
movl $0x1, 0x30(%rbx)
movq %rsi, %r13
leaq 0x1(%r13), %rsi
movq %r12, %rdi
callq 0xe969b
movq %rax, 0x8(%rbx)
movq %rbp, %rdi
movq %rax, %rsi
movq 0x10(%rsp), %rdx
movq %r13, %rcx
callq 0xeeee8
movq 0x8(%rbx), %rsi
movq 0x18(%rbx), %rdi
movq %r13, %rdx
xorl %ecx, %ecx
movl 0x1c(%rsp), %r8d
callq 0xf7131
movl 0xc(%rsp), %ebp
testb $0x10, %bpl
je 0xf0003
movq 0x18(%rbx), %rdi
callq 0xf71a1
movq %rax, 0x28(%rbx)
leaq 0x1(%rax), %rsi
movq %r12, %rdi
callq 0xe969b
movq %rax, 0x20(%rbx)
movq 0x18(%rbx), %rdi
movq 0x28(%rbx), %rdx
incq %rdx
movq %rax, %rsi
xorl %ecx, %ecx
movl $0x2, %r8d
callq 0xf727b
cmpq $0x0, 0x8(%rbx)
jne 0xf001b
movl $0x1, %esi
movq %r12, %rdi
callq 0xe969b
movq %rax, 0x8(%rbx)
testb $0x10, %bpl
je 0xf0041
cmpq $0x0, 0x20(%rbx)
jne 0xf0041
movl $0x1, %esi
movq %r12, %rdi
callq 0xe969b
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