// File: /workspace/github_c_makefile_star2_O1/inflex[P]ripMIME/ripOLE/ole.c
// Function: OLE_load_chain
unsigned char *OLE_load_chain( struct OLE_object *ole, int FAT_sector_start )
{

	int chain_length = 0;
	int current_sector = FAT_sector_start;
	unsigned char *buffer = NULL;
	unsigned char *bp = NULL;


	ole->last_chain_size = 0;

	if (FAT_sector_start < 0) return NULL;

	DOLE LOGGER_log("%s:%d:OLE_load_chain:DEBUG: Loading chain, starting at sector %d",FL,FAT_sector_start);

	chain_length = OLE_follow_chain( ole, FAT_sector_start );
	DOLE LOGGER_log("%s:%d:OLE_load_chain:DEBUG: %d sectors need to be loaded",FL,chain_length);

	if (chain_length > 0)
	{
		size_t offset;

		offset = ole->last_chain_size = chain_length << ole->header.sector_shift;
		bp = buffer = malloc( offset *sizeof(unsigned char));
		if (buffer == NULL)
		{
			LOGGER_log("%s:%d:OLE_load_chain:ERROR: Cannot allocate %d bytes for OLE chain",FL,offset);
			return NULL;
		}

		if (buffer != NULL)
		{
			int tick = 0;
			unsigned char *bp_limit;
			bp_limit = bp +offset;

			do {
				int next_sector;

				DOLE LOGGER_log("%s:%d:OLE_load_chain:DEBUG: Loading sector[%d] %d",FL, tick, current_sector );

				ole->error = OLE_get_block( ole, current_sector, bp );
				if (ole->error != OLE_OK)
				{
					//FREE5					if (bp != NULL) free(bp);
					return NULL;
				}

				bp += ole->header.sector_size;
				if (bp > bp_limit) {  
					if (buffer != NULL) { free(buffer); bp = buffer = NULL; }
					VOLE LOGGER_log("%s:%d:OLE_load_chain:ERROR: Load-chain went over memory boundary",FL); 
					return NULL;
				};

				next_sector = get_uint32((char *)( ole->FAT +(LEN_ULONG *current_sector)));
				current_sector = next_sector;
				tick++;
			} while ((current_sector >= 0)&&(current_sector <= ole->last_sector));
		}
	}
	DOLE LOGGER_log("%s:%d:OLE_load_chain:DEBUG: Done loading chain",FL);

	return buffer;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq $0x0, 0x18(%rdi)
testl %esi, %esi
js 0xfd72
movl %esi, %r14d
movq %rdi, %rbx
cmpl $0x0, 0x444(%rdi)
je 0xfbfd
leaq 0x9f01(%rip), %rdi      # 0x19ae8
leaq 0x925b(%rip), %rsi      # 0x18e49
movl $0x52f, %edx            # imm = 0x52F
movl %r14d, %ecx
xorl %eax, %eax
callq 0xc960
movq %rbx, %rdi
movl %r14d, %esi
callq 0xf757
movl %eax, %ebp
cmpl $0x0, 0x444(%rbx)
je 0xfc2f
leaq 0x9f0f(%rip), %rdi      # 0x19b29
leaq 0x9228(%rip), %rsi      # 0x18e49
movl $0x532, %edx            # imm = 0x532
movl %ebp, %ecx
xorl %eax, %eax
callq 0xc960
testl %ebp, %ebp
jle 0xfd24
movb 0x264(%rbx), %cl
shll %cl, %ebp
movslq %ebp, %r12
movq %r12, 0x18(%rbx)
movq %r12, %rdi
callq 0x22b0
testq %rax, %rax
je 0xfd28
movq %rax, %rbp
addq %rax, %r12
movq %rax, %r15
xorl %r13d, %r13d
cmpl $0x0, 0x444(%rbx)
je 0xfc8c
leaq 0x9f32(%rip), %rdi      # 0x19ba5
leaq 0x91cf(%rip), %rsi      # 0x18e49
movl $0x549, %edx            # imm = 0x549
movl %r13d, %ecx
movl %r14d, %r8d
xorl %eax, %eax
callq 0xc960
movq %rbx, %rdi
movl %r14d, %esi
movq %r15, %rdx
callq 0xe9bd
movl %eax, (%rbx)
testl %eax, %eax
je 0xfcbe
xorl %eax, %eax
testb %al, %al
je 0xfd49
testl %r14d, %r14d
js 0xfd49
cmpl 0x10(%rbx), %r14d
jle 0xfc63
jmp 0xfd49
movl 0x268(%rbx), %eax
addq %rax, %r15
cmpq %r12, %r15
jbe 0xfd03
testq %rbp, %rbp
je 0xfcdc
movq %rbp, %rdi
callq 0x2030
xorl %r15d, %r15d
cmpl $0x0, 0x448(%rbx)
je 0xfd1d
xorl %ebp, %ebp
leaq 0x9ee9(%rip), %rdi      # 0x19bd7
leaq 0x9154(%rip), %rsi      # 0x18e49
movl $0x555, %edx            # imm = 0x555
xorl %eax, %eax
callq 0xc960
jmp 0xfca0
shll $0x2, %r14d
addq 0x28(%rbx), %r14
movq %r14, %rdi
callq 0x110fb
movl %eax, %r14d
incl %r13d
movb $0x1, %al
jmp 0xfca2
xorl %ebp, %ebp
jmp 0xfca0
xorl %ebp, %ebp
jmp 0xfd4d
leaq 0x9e33(%rip), %rdi      # 0x19b62
leaq 0x9113(%rip), %rsi      # 0x18e49
xorl %ebp, %ebp
movl $0x53c, %edx            # imm = 0x53C
movq %r12, %rcx
xorl %eax, %eax
callq 0xc960
xorl %eax, %eax
testb %al, %al
je 0xfd72
cmpl $0x0, 0x444(%rbx)
je 0xfd74
leaq 0x9ebb(%rip), %rdi      # 0x19c18
leaq 0x90e5(%rip), %rsi      # 0x18e49
movl $0x55f, %edx            # imm = 0x55F
xorl %eax, %eax
callq 0xc960
jmp 0xfd74
xorl %ebp, %ebp
movq %rbp, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq