// File: /workspace/github_c_makefile_star2_O3/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 0x10ef0
testl %r15d, %r15d
js 0xf6db
cmpl $0x0, 0x444(%r14)
je 0xf657
leaq 0xa0ba(%rip), %rdi      # 0x196fb
leaq 0x97f1(%rip), %rsi      # 0x18e39
movl $0x458, %edx            # imm = 0x458
movl %r15d, %ecx
xorl %eax, %eax
callq 0xc84f
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 0xf6df
movq %rcx, %rdi
callq 0x10eeb
movl %eax, %r13d
movq %r12, %rdi
movl %eax, %esi
callq 0x10efa
movl 0x444(%r14), %ecx
testl %eax, %eax
jne 0xf705
testl %ecx, %ecx
je 0xf6c5
movl %r13d, (%rsp)
leaq 0xa143(%rip), %rdi      # 0x197ec
leaq 0x9789(%rip), %rsi      # 0x18e39
movl $0x46f, %edx            # imm = 0x46F
movl %r15d, %ecx
movl %r15d, %r8d
movl %r13d, %r9d
xorl %eax, %eax
callq 0xc84f
cmpl %r15d, %r13d
je 0xf728
incl %ebx
testl %r13d, %r13d
js 0xf728
movl %r13d, %r15d
cmpl %ebp, %r13d
jl 0xf65e
jmp 0xf728
xorl %ebx, %ebx
jmp 0xf732
cmpl $0x0, 0x444(%r14)
je 0xf728
leaq 0xa04c(%rip), %rdi      # 0x1973c
leaq 0x9742(%rip), %rsi      # 0x18e39
movl $0x460, %edx            # imm = 0x460
xorl %eax, %eax
callq 0xc84f
jmp 0xf728
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
testl %ecx, %ecx
je 0xf728
leaq 0xa08b(%rip), %rdi      # 0x197a0
leaq 0x971d(%rip), %rsi      # 0x18e39
movl $0x469, %edx            # imm = 0x469
xorl %eax, %eax
callq 0xc84f
leaq 0x10(%rsp), %rdi
callq 0x1100b
movl %ebx, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq