// File: /workspace/github_c_makefile_star2_O1/inflex[P]ripMIME/ripOLE/ole.c
// Function: OLE_follow_chain
int OLE_follow_chain( struct OLE_object *ole, int FAT_sector_start )
{
	int current_sector = FAT_sector_start;
	int chain_length=0;
	int last_sector_of_file = ole->last_sector;
	int break_out = 0;
	struct bti_node *n;

	BTI_init(&n);

	if (FAT_sector_start < 0) return 0;

	DOLE LOGGER_log("%s:%d:OLE_follow_chain:DEBUG: Starting chain follow at sector %d",FL, FAT_sector_start );

	do {
		unsigned int next_sector;
		unsigned char *next_sector_location;

		next_sector_location = ole->FAT +(LEN_ULONG *current_sector);
		if (next_sector_location > (ole->FAT_limit -4)) {
			DOLE LOGGER_log("%s:%d:OLE_follow_chain:DEBUG: ERROR: Next sector was outside of the limits of this file (%ld > %ld)",FL, next_sector_location, ole->FAT_limit);
			break;
		}

		//next_sector = get_4byte_value( ole->FAT +(LEN_ULONG *current_sector));
		next_sector = get_uint32((char*) next_sector_location );

		if (BTI_add(&n, next_sector) != 0)
		{
			DOLE LOGGER_log("%s:%d:OLE_follow_chain:DEBUG: Sector collision, terminating chain traversal",FL);
			chain_length=-1;

			break;
		}

		DOLE LOGGER_log("%s:%d:OLE_follow_chain:DEBUG: 0x%0X:%d)->(0x%0X:%d)\n",FL, current_sector, current_sector, next_sector, next_sector);

		// 20040729-10H37 Added this to prevent endless loop which sometimes occurs at sector 0

		if (next_sector == current_sector) break;

		//		fflush(stdout);
		current_sector = next_sector;
		chain_length++;

		/** Test to see if we should terminate this chain traversal **/
		switch (current_sector) {
			case OLE_SECTORID_MSAT:
			case OLE_SECTORID_SAT:
			case OLE_SECTORID_ENDOFCHAIN:
			case OLE_SECTORID_FREE:
				break_out=1;
				break;
			default:
				break_out=0;
		};
		 
		if (current_sector < 0) break_out = 1;

	} while ((break_out==0)&&(current_sector < last_sector_of_file));

	BTI_done(&n);

	return chain_length;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %esi, %r14d
movq %rdi, %rbx
movl 0x10(%rdi), %r13d
leaq 0x10(%rsp), %rdi
callq 0x11100
testl %r14d, %r14d
js 0xf898
cmpl $0x0, 0x444(%rbx)
je 0xf7a8
leaq 0x9f79(%rip), %rdi      # 0x1970b
leaq 0x96b0(%rip), %rsi      # 0x18e49
movl $0x458, %edx            # imm = 0x458
movl %r14d, %ecx
xorl %eax, %eax
callq 0xc960
xorl %ebp, %ebp
leal (,%r14,4), %eax
movslq %eax, %rcx
addq 0x28(%rbx), %rcx
movq 0x30(%rbx), %r8
leaq -0x4(%r8), %rax
cmpq %rax, %rcx
jbe 0xf7f2
cmpl $0x0, 0x444(%rbx)
je 0xf86b
xorl %r12d, %r12d
leaq 0x9f6f(%rip), %rdi      # 0x1974c
leaq 0x9665(%rip), %rsi      # 0x18e49
movl $0x460, %edx            # imm = 0x460
xorl %eax, %eax
callq 0xc960
jmp 0xf86e
movq %rcx, %rdi
callq 0x110fb
movl %eax, %r15d
leaq 0x10(%rsp), %rdi
movl %eax, %esi
callq 0x1110a
movl 0x444(%rbx), %ecx
testl %eax, %eax
je 0xf83b
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testl %ecx, %ecx
je 0xf86b
xorl %r12d, %r12d
leaq 0x9f8a(%rip), %rdi      # 0x197b0
leaq 0x961c(%rip), %rsi      # 0x18e49
movl $0x469, %edx            # imm = 0x469
xorl %eax, %eax
callq 0xc960
jmp 0xf86e
testl %ecx, %ecx
je 0xf866
movl %r15d, (%rsp)
leaq 0x9fb2(%rip), %rdi      # 0x197fc
leaq 0x95f8(%rip), %rsi      # 0x18e49
movl $0x46f, %edx            # imm = 0x46F
movl %r14d, %ecx
movl %r14d, %r8d
movl %r15d, %r9d
xorl %eax, %eax
callq 0xc960
cmpl %r14d, %r15d
jne 0xf87e
xorl %r12d, %r12d
testb %r12b, %r12b
je 0xf88c
cmpl %r13d, %r14d
jl 0xf7aa
jmp 0xf88c
incl %ebp
testl %r15d, %r15d
setns %r12b
movl %r15d, %r14d
jmp 0xf86e
leaq 0x10(%rsp), %rdi
callq 0x11240
jmp 0xf89a
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq