// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl -0x14(%rbp), %eax
movl %eax, -0x18(%rbp)
movl $0x0, -0x1c(%rbp)
movq -0x10(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, -0x20(%rbp)
movl $0x0, -0x24(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x18240
cmpl $0x0, -0x14(%rbp)
jge 0x15e48
movl $0x0, -0x4(%rbp)
jmp 0x15fd6
movq -0x10(%rbp), %rax
cmpl $0x0, 0x444(%rax)
je 0x15e72
movl -0x14(%rbp), %ecx
leaq 0xa6ae(%rip), %rdi      # 0x2050d
leaq 0x9de5(%rip), %rsi      # 0x1fc4b
movl $0x458, %edx            # imm = 0x458
movb $0x0, %al
callq 0x117c0
jmp 0x15e74
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movl -0x18(%rbp), %ecx
shll $0x2, %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movq -0x10(%rbp), %rcx
movq 0x30(%rcx), %rcx
addq $-0x4, %rcx
cmpq %rcx, %rax
jbe 0x15ed9
movq -0x10(%rbp), %rax
cmpl $0x0, 0x444(%rax)
je 0x15ed4
movq -0x40(%rbp), %rcx
movq -0x10(%rbp), %rax
movq 0x30(%rax), %r8
leaq 0xa68d(%rip), %rdi      # 0x2054e
leaq 0x9d83(%rip), %rsi      # 0x1fc4b
movl $0x460, %edx            # imm = 0x460
movb $0x0, %al
callq 0x117c0
jmp 0x15fc7
movq -0x40(%rbp), %rdi
callq 0x181f0
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %esi
leaq -0x30(%rbp), %rdi
callq 0x18260
cmpl $0x0, %eax
je 0x15f29
movq -0x10(%rbp), %rax
cmpl $0x0, 0x444(%rax)
je 0x15f1d
leaq 0xa6a8(%rip), %rdi      # 0x205b2
leaq 0x9d3a(%rip), %rsi      # 0x1fc4b
movl $0x469, %edx            # imm = 0x469
movb $0x0, %al
callq 0x117c0
movl $0xffffffff, -0x1c(%rbp) # imm = 0xFFFFFFFF
jmp 0x15fc7
movq -0x10(%rbp), %rax
cmpl $0x0, 0x444(%rax)
je 0x15f61
movl -0x18(%rbp), %ecx
movl -0x18(%rbp), %r8d
movl -0x34(%rbp), %r9d
movl -0x34(%rbp), %eax
leaq 0xa6b3(%rip), %rdi      # 0x205fe
leaq 0x9cf9(%rip), %rsi      # 0x1fc4b
movl $0x46f, %edx            # imm = 0x46F
movl %eax, (%rsp)
movb $0x0, %al
callq 0x117c0
movl -0x34(%rbp), %eax
cmpl -0x18(%rbp), %eax
jne 0x15f6b
jmp 0x15fc7
movl -0x34(%rbp), %eax
movl %eax, -0x18(%rbp)
movl -0x1c(%rbp), %eax
incl %eax
movl %eax, -0x1c(%rbp)
movl -0x18(%rbp), %eax
addl $0x4, %eax
subl $0x3, %eax
ja 0x15f8f
jmp 0x15f86
movl $0x1, -0x24(%rbp)
jmp 0x15f96
movl $0x0, -0x24(%rbp)
cmpl $0x0, -0x18(%rbp)
jge 0x15fa3
movl $0x1, -0x24(%rbp)
jmp 0x15fa5
xorl %eax, %eax
cmpl $0x0, -0x24(%rbp)
movb %al, -0x41(%rbp)
jne 0x15fbc
movl -0x18(%rbp), %eax
cmpl -0x20(%rbp), %eax
setl %al
movb %al, -0x41(%rbp)
movb -0x41(%rbp), %al
testb $0x1, %al
jne 0x15e74
leaq -0x30(%rbp), %rdi
callq 0x18420
movl -0x1c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nop