// File: /workspace/github_c_makefile_star2_O2/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
andq $0x0, 0x18(%rdi)
testl %esi, %esi
js 0xf525
movl %esi, %r15d
movq %rdi, %rbx
cmpl $0x0, 0x444(%rdi)
je 0xf3df
leaq 0x970f(%rip), %rdi      # 0x18ad8
leaq 0x8a69(%rip), %rsi      # 0x17e39
movl $0x52f, %edx            # imm = 0x52F
movl %r15d, %ecx
xorl %eax, %eax
callq 0xc2a1
movq %rbx, %rdi
movl %r15d, %esi
callq 0xef7d
movl %eax, %ebp
cmpl $0x0, 0x444(%rbx)
je 0xf411
leaq 0x971d(%rip), %rdi      # 0x18b19
leaq 0x8a36(%rip), %rsi      # 0x17e39
movl $0x532, %edx            # imm = 0x532
movl %ebp, %ecx
xorl %eax, %eax
callq 0xc2a1
testl %ebp, %ebp
jle 0xf4b2
movb 0x264(%rbx), %cl
shll %cl, %ebp
movslq %ebp, %r12
movq %r12, 0x18(%rbx)
movq %r12, %rdi
callq 0x22a0
testq %rax, %rax
je 0xf4da
movq %rax, %r13
addq %rax, %r12
movq %rax, %r14
xorl %ebp, %ebp
cmpl $0x0, 0x444(%rbx)
je 0xf46c
leaq 0x9741(%rip), %rdi      # 0x18b95
leaq 0x89de(%rip), %rsi      # 0x17e39
movl $0x549, %edx            # imm = 0x549
movl %ebp, %ecx
movl %r15d, %r8d
xorl %eax, %eax
callq 0xc2a1
movq %rbx, %rdi
movl %r15d, %esi
movq %r14, %rdx
callq 0xe24a
movl %eax, (%rbx)
testl %eax, %eax
jne 0xf525
movl 0x268(%rbx), %eax
addq %rax, %r14
cmpq %r12, %r14
ja 0xf4fc
shll $0x2, %r15d
addq 0x28(%rbx), %r15
movq %r15, %rdi
callq 0x1082f
testl %eax, %eax
js 0xf4b5
movl %eax, %r15d
incl %ebp
cmpl 0x10(%rbx), %eax
jle 0xf444
jmp 0xf4b5
xorl %r13d, %r13d
cmpl $0x0, 0x444(%rbx)
je 0xf528
leaq 0x9743(%rip), %rdi      # 0x18c08
leaq 0x896d(%rip), %rsi      # 0x17e39
movl $0x55f, %edx            # imm = 0x55F
xorl %eax, %eax
callq 0xc2a1
jmp 0xf528
leaq 0x9671(%rip), %rdi      # 0x18b52
leaq 0x8951(%rip), %rsi      # 0x17e39
xorl %r13d, %r13d
movl $0x53c, %edx            # imm = 0x53C
movq %r12, %rcx
xorl %eax, %eax
callq 0xc2a1
jmp 0xf528
movq %r13, %rdi
callq 0x2030
cmpl $0x0, 0x448(%rbx)
je 0xf525
leaq 0x96b3(%rip), %rdi      # 0x18bc7
leaq 0x891e(%rip), %rsi      # 0x17e39
xorl %r13d, %r13d
movl $0x555, %edx            # imm = 0x555
jmp 0xf4d1
xorl %r13d, %r13d
movq %r13, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq