// File: /workspace/github_c_makefile_star2_O2/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, %r15d
movq %rdi, %r14
movl 0x10(%rdi), %ebp
leaq 0x10(%rsp), %rdi
callq 0x10834
testl %r15d, %r15d
js 0xf052
cmpl $0x0, 0x444(%r14)
je 0xefce
leaq 0x9743(%rip), %rdi      # 0x186fb
leaq 0x8e7a(%rip), %rsi      # 0x17e39
movl $0x458, %edx            # imm = 0x458
movl %r15d, %ecx
xorl %eax, %eax
callq 0xc2a1
leaq 0x10(%rsp), %r12
xorl %ebx, %ebx
leal (,%r15,4), %eax
movslq %eax, %rcx
addq 0x28(%r14), %rcx
movq 0x30(%r14), %r8
leaq -0x4(%r8), %rax
cmpq %rax, %rcx
ja 0xf056
movq %rcx, %rdi
callq 0x1082f
movl %eax, %r13d
movq %r12, %rdi
movl %eax, %esi
callq 0x1083b
movl 0x444(%r14), %ecx
testl %eax, %eax
jne 0xf07c
testl %ecx, %ecx
je 0xf03c
movl %r13d, (%rsp)
leaq 0x97cc(%rip), %rdi      # 0x187ec
leaq 0x8e12(%rip), %rsi      # 0x17e39
movl $0x46f, %edx            # imm = 0x46F
movl %r15d, %ecx
movl %r15d, %r8d
movl %r13d, %r9d
xorl %eax, %eax
callq 0xc2a1
cmpl %r15d, %r13d
je 0xf09d
incl %ebx
testl %r13d, %r13d
js 0xf09d
movl %r13d, %r15d
cmpl %ebp, %r13d
jl 0xefd5
jmp 0xf09d
xorl %ebx, %ebx
jmp 0xf0a7
cmpl $0x0, 0x444(%r14)
je 0xf09d
leaq 0x96d5(%rip), %rdi      # 0x1873c
leaq 0x8dcb(%rip), %rsi      # 0x17e39
movl $0x460, %edx            # imm = 0x460
xorl %eax, %eax
callq 0xc2a1
jmp 0xf09d
pushq $-0x1
popq %rbx
testl %ecx, %ecx
je 0xf09d
leaq 0x9716(%rip), %rdi      # 0x187a0
leaq 0x8da8(%rip), %rsi      # 0x17e39
movl $0x469, %edx            # imm = 0x469
xorl %eax, %eax
callq 0xc2a1
leaq 0x10(%rsp), %rdi
callq 0x1093d
movl %ebx, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq