// File: /workspace/github_c_makefile_star2_O1/inflex[P]ripMIME/ripOLE/ole.c
// Function: OLE_decode_file
int OLE_decode_file( struct OLE_object *ole, char *fname, char *decode_path )
{
	unsigned char *current_property, *property_limit;
	int result = 0;
	int i;

	// Reject any bad paramters.
	if (ole == NULL) return OLEER_DECODE_NULL_OBJECT;
	if (fname == NULL) return OLEER_DECODE_NULL_FILENAME;
	if (decode_path == NULL) return OLEER_DECODE_NULL_PATH;

	// We need to gain access to the OLE2 data file, without
	//		this pretty much everything is pointless.
	DOLE LOGGER_log("%s:%d:OLE_decode_file:DEBUG: opening %s", FL, fname );
	result = OLE_open_file( ole, fname );
	if (result != 0) return result;

	// Try create the output directory which we're using
	//		to write the decoded files out to.
	DOLE LOGGER_log("%s:%d:OLE_decode_file:DEBUG: opening output directory %s", FL, decode_path);
	result = OLE_open_directory( ole, decode_path );
	if (result != 0) return result;

	// In order to successfully decode an OLE2 stream, we have to read
	//		and understand the first 512 bytes of the file, this is the 
	//		OLE2 header. 
	DOLE LOGGER_log("%s:%d:OLE_decode_file:DEBUG: Getting main header", FL);
	result = OLE_get_header( ole );
	if (result != 0) return result;

	DOLE LOGGER_log("%s:%d:OLE_decode_file:DEBUG: Converting main header", FL);
	result = OLE_convert_header( ole );
	if (result != 0) return result;

	result = OLE_header_sanity_check( ole );
	if (result > 0) return OLEER_INSANE_OLE_FILE;

	DOLE OLE_print_header( ole );

	DOLE LOGGER_log("%s:%d:OLE_decode_file:DEBUG: Loading FAT", FL);
	result = OLE_load_FAT( ole );
	if (result != 0) return result;

	DOLE LOGGER_log("%s:%d:OLE_decode_file:DEBUG: Loading miniFAT chain", FL);
	ole->miniFAT = OLE_load_chain( ole, ole->header.mini_fat_start );
	if (ole->miniFAT == NULL) return OLEER_MINIFAT_READ_FAIL;

	DOLE LOGGER_log("%s:%d:OLE_decode_file:DEBUG: Loading Directory stream chain", FL);
	ole->properties = OLE_load_chain( ole, ole->header.directory_stream_start_sector );
	if (ole->properties == NULL) return OLEER_PROPERTIES_READ_FAIL;

	i=0;
	current_property = ole->properties;
	property_limit = current_property +ole->last_chain_size ;
	// while(1)
	while (current_property < property_limit)
	{
		struct OLE_directory_entry a_dir_object, *adir;
		int property_value=0;

		adir = &a_dir_object;

		OLE_dir_init(adir);

		property_value = get_uint8((char *)current_property);
		if (property_value < 1) break;

		DOLE LOGGER_log("%s:%d:OLE_decode_file:DEBUG:--------- DIRECTORY INDEX: %d",FL,i);

		OLE_convert_directory( ole, current_property, adir );

		DOLE {
			LOGGER_log("%s:%d:OLE_decode_file:DEBUG: Printing directory details...",FL);
			OLE_print_directory( ole, adir);
			LOGGER_log("%s:%d:OLE_decode_file:DEBUG: End of directory details",FL);
		}

		if (adir->element_colour > 1) break;

		if ((adir->element_type == STGTY_INVALID)||(adir->element_type > STGTY_ROOT))
		{
			DOLE LOGGER_log("%s:%d:OLE_decode_file:DEBUG: breaking out due to element type %d",FL, adir->element_type);
			break;

		} else if (adir->element_type == STGTY_ROOT){
			/** ROOT DIRECTORY ENTRY **/
			/** ROOT DIRECTORY ENTRY **/
			/** ROOT DIRECTORY ENTRY **/
			DOLE LOGGER_log("%s:%d:OLE_decode_file:DEBUG: Loading ministream/SmallBlockArray",FL);
			ole->ministream = OLE_load_chain( ole, adir->start_sector );
			if (ole->ministream == NULL) return OLEER_MINISTREAM_READ_FAIL;
			DOLE LOGGER_log("%s:%d:OLE_decode_file:DEBUG: ministream done",FL);







		} else if (adir->element_type == STGTY_STORAGE) {
			/** STORAGE ELEMENT **/
			/** STORAGE ELEMENT **/
			/** STORAGE ELEMENT **/
			DOLE LOGGER_log("%s:%d:OLE_decode_file:DEBUG: Item is directory, start child is at index %d\n",FL,i);
			ole->ministream = OLE_load_chain( ole, adir->start_sector );
			if (ole->ministream == NULL) return OLEER_MINISTREAM_READ_FAIL;
			DOLE LOGGER_log("%s:%d:OLE_decode_file:DEBUG: DIRECTORY ministream done",FL);




		} else if (adir->element_type == STGTY_STREAM) {
			/** STREAM ELEMENT **/
			/** STREAM ELEMENT **/
			/** STREAM ELEMENT **/
			OLE_decode_stream( ole, adir, decode_path );



		} else {
			/** If the element isn't of the above types then it's possibly 
			 ** an empty element or just one used for the MSAT/SAT
			 ** either way we just step over it and carry on **/
			DOLE LOGGER_log("%s:%d:OLE_decode_file:DEBUG: Element type %d does not need to be handled",FL,adir->element_type);
		}

		// Jump to the next property record, which
		//		is always 128 bytes ahead.
		current_property += 128;
		i++;

	} // While there are still more directory entries to read in.

	DOLE LOGGER_log("%s:%d:OLE_decode_file:DEBUG: Finished",FL);

	/*
	//if (ole->f) fclose(ole->f);
	fclose(ole->f);
	if (ole->FAT) free(ole->FAT);
	if (ole->miniFAT) free(ole->miniFAT);
	if (ole->ministream) free(ole->ministream);
	if (ole->properties) free(ole->properties);
	 */

	return OLE_OK;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
testq %rdi, %rdi
je 0x1048a
movq %rsi, %r15
testq %rsi, %rsi
je 0x10491
movq %rdx, %r14
testq %rdx, %rdx
je 0x10498
movq %rdi, %rbx
cmpl $0x0, 0x444(%rdi)
je 0x10416
leaq 0x9cec(%rip), %rdi      # 0x1a0ec
leaq 0x8a42(%rip), %rsi      # 0x18e49
movl $0x6ec, %edx            # imm = 0x6EC
movq %r15, %rcx
xorl %eax, %eax
callq 0xc960
movq %rbx, %rdi
movq %r15, %rsi
callq 0xfd86
testl %eax, %eax
jne 0x10486
cmpl $0x0, 0x444(%rbx)
je 0x1044b
leaq 0x9cdf(%rip), %rdi      # 0x1a114
leaq 0x8a0d(%rip), %rsi      # 0x18e49
movl $0x6f2, %edx            # imm = 0x6F2
movq %r14, %rcx
xorl %eax, %eax
callq 0xc960
movq %r14, %rsi
callq 0xfe9c
testl %eax, %eax
jne 0x10486
cmpl $0x0, 0x444(%rbx)
je 0x1047a
leaq 0x9ce6(%rip), %rdi      # 0x1a14d
leaq 0x89db(%rip), %rsi      # 0x18e49
movl $0x6f9, %edx            # imm = 0x6F9
xorl %eax, %eax
callq 0xc960
movq %rbx, %rdi
callq 0xee2f
testl %eax, %eax
je 0x104b1
movl %eax, %ebp
jmp 0x1049d
movl $0xa, %ebp
jmp 0x1049d
movl $0xb, %ebp
jmp 0x1049d
movl $0xc, %ebp
movl %ebp, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x0, 0x444(%rbx)
je 0x104d4
leaq 0x9cbd(%rip), %rdi      # 0x1a17e
leaq 0x8981(%rip), %rsi      # 0x18e49
movl $0x6fd, %edx            # imm = 0x6FD
xorl %eax, %eax
callq 0xc960
movq %rbx, %rdi
callq 0xee65
movq %rbx, %rdi
callq 0xefa8
movl $0x67, %ebp
testl %eax, %eax
jg 0x1049d
cmpl $0x0, 0x444(%rbx)
je 0x104fe
movq %rbx, %rdi
callq 0xefe6
cmpl $0x0, 0x444(%rbx)
je 0x10521
leaq 0x9ca4(%rip), %rdi      # 0x1a1b2
leaq 0x8934(%rip), %rsi      # 0x18e49
movl $0x706, %edx            # imm = 0x706
xorl %eax, %eax
callq 0xc960
movq %rbx, %rdi
callq 0xf251
testl %eax, %eax
jne 0x10486
cmpl $0x0, 0x444(%rbx)
je 0x10554
leaq 0x9c9a(%rip), %rdi      # 0x1a1db
leaq 0x8901(%rip), %rsi      # 0x18e49
movl $0x70a, %edx            # imm = 0x70A
xorl %eax, %eax
callq 0xc960
movl 0x280(%rbx), %esi
movq %rbx, %rdi
callq 0xfbb6
movq %rax, 0x38(%rbx)
testq %rax, %rax
je 0x10830
cmpl $0x0, 0x444(%rbx)
je 0x10592
leaq 0x9c8f(%rip), %rdi      # 0x1a20e
leaq 0x88c3(%rip), %rsi      # 0x18e49
movl $0x70e, %edx            # imm = 0x70E
xorl %eax, %eax
callq 0xc960
movl 0x278(%rbx), %esi
movq %rbx, %rdi
callq 0xfbb6
movq %rax, 0x250(%rbx)
testq %rax, %rax
je 0x1086a
movq %rax, %r15
movq 0x18(%rbx), %rax
addq %r15, %rax
movq %rax, 0x8(%rsp)
xorl %ebp, %ebp
movl $0x0, 0x4(%rsp)
cmpq 0x8(%rsp), %r15
jae 0x10840
movl %ebp, 0x74(%rsp)
movb $0x0, 0x78(%rsp)
movq %rbp, 0x88(%rsp)
movl %ebp, 0x90(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movaps %xmm0, 0x20(%rsp)
movaps %xmm0, 0x30(%rsp)
movaps %xmm0, 0x40(%rsp)
movq %rbp, 0x4e(%rsp)
leaq 0x58(%rsp), %rax
movq %rbp, 0x5(%rax)
movq %rbp, (%rax)
movq %r15, %rdi
callq 0x110ec
movl $0x3, %r13d
testl %eax, %eax
jle 0x1081d
cmpl $0x0, 0x444(%rbx)
je 0x10651
leaq 0x9c10(%rip), %rdi      # 0x1a24a
leaq 0x8808(%rip), %rsi      # 0x18e49
movl $0x722, %edx            # imm = 0x722
movl 0x4(%rsp), %ecx
xorl %eax, %eax
callq 0xc960
movq %rbx, %rdi
movq %r15, %rsi
leaq 0x10(%rsp), %r12
movq %r12, %rdx
callq 0xf0d5
cmpl $0x0, 0x444(%rbx)
je 0x106aa
leaq 0x9c10(%rip), %rdi      # 0x1a284
leaq 0x87ce(%rip), %rbp      # 0x18e49
movq %rbp, %rsi
movl $0x727, %edx            # imm = 0x727
xorl %eax, %eax
callq 0xc960
movq %r12, %rsi
callq 0xf1e5
leaq 0x9c26(%rip), %rdi      # 0x1a2bf
movq %rbp, %rsi
xorl %ebp, %ebp
movl $0x729, %edx            # imm = 0x729
xorl %eax, %eax
callq 0xc960
cmpb $0x1, 0x55(%rsp)
jg 0x1081d
movsbl 0x54(%rsp), %ecx
cmpl $0x6, %ecx
setl %al
testl %ecx, %ecx
setne %dl
testb %al, %dl
jne 0x106f5
cmpl $0x0, 0x444(%rbx)
je 0x1081d
leaq 0x9c18(%rip), %rdi      # 0x1a2f5
leaq 0x8765(%rip), %rsi      # 0x18e49
movl $0x730, %edx            # imm = 0x730
xorl %eax, %eax
callq 0xc960
jmp 0x1081d
cmpb $0x1, %cl
je 0x10788
movzbl %cl, %eax
cmpl $0x2, %eax
leaq 0x10(%rsp), %rsi
je 0x10778
cmpl $0x5, %eax
jne 0x107ef
cmpl $0x0, 0x444(%rbx)
je 0x10737
leaq 0x9c12(%rip), %rdi      # 0x1a336
leaq 0x871e(%rip), %rsi      # 0x18e49
movl $0x737, %edx            # imm = 0x737
xorl %eax, %eax
callq 0xc960
movl 0x88(%rsp), %esi
movq %rbx, %rdi
callq 0xfbb6
movq %rax, 0x248(%rbx)
testq %rax, %rax
je 0x10828
cmpl $0x0, 0x444(%rbx)
je 0x10812
leaq 0x9c0c(%rip), %rdi      # 0x1a376
leaq 0x86d8(%rip), %rsi      # 0x18e49
movl $0x73a, %edx            # imm = 0x73A
jmp 0x107e6
movq %rbx, %rdi
movq %r14, %rdx
callq 0x10089
jmp 0x10812
cmpl $0x0, 0x444(%rbx)
je 0x107af
leaq 0x9c0b(%rip), %rdi      # 0x1a3a3
leaq 0x86aa(%rip), %rsi      # 0x18e49
movl $0x746, %edx            # imm = 0x746
movl 0x4(%rsp), %ecx
xorl %eax, %eax
callq 0xc960
movl 0x88(%rsp), %esi
movq %rbx, %rdi
callq 0xfbb6
movq %rax, 0x248(%rbx)
testq %rax, %rax
je 0x10828
cmpl $0x0, 0x444(%rbx)
je 0x10812
leaq 0x9c15(%rip), %rdi      # 0x1a3ef
leaq 0x8668(%rip), %rsi      # 0x18e49
movl $0x749, %edx            # imm = 0x749
xorl %eax, %eax
callq 0xc960
jmp 0x10812
cmpl $0x0, 0x444(%rbx)
je 0x10812
leaq 0x9c27(%rip), %rdi      # 0x1a426
leaq 0x8643(%rip), %rsi      # 0x18e49
movl $0x75a, %edx            # imm = 0x75A
xorl %eax, %eax
callq 0xc960
subq $-0x80, %r15
incl 0x4(%rsp)
xorl %r13d, %r13d
testl %r13d, %r13d
je 0x105c9
jmp 0x1083a
movl $0x1, %r13d
jmp 0x1081d
movl $0x1e, %ebp
jmp 0x1049d
cmpl $0x3, %r13d
jne 0x10874
cmpl $0x0, 0x444(%rbx)
je 0x1087e
leaq 0x9c1f(%rip), %rdi      # 0x1a46f
leaq 0x85f2(%rip), %rsi      # 0x18e49
xorl %ebp, %ebp
movl $0x764, %edx            # imm = 0x764
xorl %eax, %eax
callq 0xc960
jmp 0x1049d
movl $0x1f, %ebp
jmp 0x1049d
movl $0x20, %ebp
jmp 0x1049d
xorl %ebp, %ebp
jmp 0x1049d
nopl (%rax)