// File: /workspace/github_c_makefile_star2_O2/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 0xfc0b
movq %rsi, %r15
testq %rsi, %rsi
je 0xfc0f
movq %rdx, %r14
testq %rdx, %rdx
je 0xfc13
movq %rdi, %rbx
cmpl $0x0, 0x444(%rdi)
je 0xfb97
leaq 0x955b(%rip), %rdi      # 0x190dc
leaq 0x82b1(%rip), %rsi      # 0x17e39
movl $0x6ec, %edx            # imm = 0x6EC
movq %r15, %rcx
xorl %eax, %eax
callq 0xc2a1
movq %rbx, %rdi
movq %r15, %rsi
callq 0xf53a
testl %eax, %eax
jne 0xfc07
cmpl $0x0, 0x444(%rbx)
je 0xfbcc
leaq 0x954e(%rip), %rdi      # 0x19104
leaq 0x827c(%rip), %rsi      # 0x17e39
movl $0x6f2, %edx            # imm = 0x6F2
movq %r14, %rcx
xorl %eax, %eax
callq 0xc2a1
movq %r14, %rsi
callq 0xf644
testl %eax, %eax
jne 0xfc07
cmpl $0x0, 0x444(%rbx)
je 0xfbfb
leaq 0x9555(%rip), %rdi      # 0x1913d
leaq 0x824a(%rip), %rsi      # 0x17e39
movl $0x6f9, %edx            # imm = 0x6F9
xorl %eax, %eax
callq 0xc2a1
movq %rbx, %rdi
callq 0xe6d2
testl %eax, %eax
je 0xfc2a
movl %eax, %ebp
jmp 0xfc16
pushq $0xa
jmp 0xfc15
pushq $0xb
jmp 0xfc15
pushq $0xc
popq %rbp
movl %ebp, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x0, 0x444(%rbx)
je 0xfc4d
leaq 0x9534(%rip), %rdi      # 0x1916e
leaq 0x81f8(%rip), %rsi      # 0x17e39
movl $0x6fd, %edx            # imm = 0x6FD
xorl %eax, %eax
callq 0xc2a1
movq %rbx, %rdi
callq 0xe704
movq %rbx, %rdi
callq 0xe83b
testl %eax, %eax
jle 0xfc65
pushq $0x67
jmp 0xfc15
cmpl $0x0, 0x444(%rbx)
je 0xfc99
movq %rbx, %rdi
callq 0xe879
cmpl $0x0, 0x444(%rbx)
je 0xfc99
leaq 0x951c(%rip), %rdi      # 0x191a2
leaq 0x81ac(%rip), %rsi      # 0x17e39
movl $0x706, %edx            # imm = 0x706
xorl %eax, %eax
callq 0xc2a1
movq %rbx, %rdi
callq 0xeadb
testl %eax, %eax
jne 0xfc07
cmpl $0x0, 0x444(%rbx)
je 0xfccc
leaq 0x9512(%rip), %rdi      # 0x191cb
leaq 0x8179(%rip), %rsi      # 0x17e39
movl $0x70a, %edx            # imm = 0x70A
xorl %eax, %eax
callq 0xc2a1
movl 0x280(%rbx), %esi
movq %rbx, %rdi
callq 0xf39b
movq %rax, 0x38(%rbx)
testq %rax, %rax
je 0xff6b
cmpl $0x0, 0x444(%rbx)
je 0xfd0a
leaq 0x9507(%rip), %rdi      # 0x191fe
leaq 0x813b(%rip), %rsi      # 0x17e39
movl $0x70e, %edx            # imm = 0x70E
xorl %eax, %eax
callq 0xc2a1
movl 0x278(%rbx), %esi
movq %rbx, %rdi
callq 0xf39b
movq %rax, 0x250(%rbx)
testq %rax, %rax
je 0xff72
movq %rax, %r12
movq 0x18(%rbx), %rax
addq %r12, %rax
movq %rax, 0x8(%rsp)
xorl %r13d, %r13d
cmpq 0x8(%rsp), %r12
jae 0xff9c
andl $0x0, 0x74(%rsp)
movb $0x0, 0x78(%rsp)
andq $0x0, 0x88(%rsp)
andl $0x0, 0x90(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movaps %xmm0, 0x20(%rsp)
movaps %xmm0, 0x40(%rsp)
andq $0x0, 0x4e(%rsp)
movaps %xmm0, 0x30(%rsp)
leaq 0x58(%rsp), %rax
andq $0x0, 0x5(%rax)
andq $0x0, (%rax)
movq %r12, %rdi
callq 0x10820
testl %eax, %eax
jle 0xff9c
cmpl $0x0, 0x444(%rbx)
je 0xfdc1
leaq 0x948f(%rip), %rdi      # 0x1923a
leaq 0x8087(%rip), %rsi      # 0x17e39
movl $0x722, %edx            # imm = 0x722
movl %r13d, %ecx
xorl %eax, %eax
callq 0xc2a1
movq %rbx, %rdi
movq %r12, %rsi
leaq 0x10(%rsp), %rbp
movq %rbp, %rdx
callq 0xe961
cmpl $0x0, 0x444(%rbx)
je 0xfe1e
leaq 0x9490(%rip), %rdi      # 0x19274
movl %r13d, %r15d
leaq 0x804b(%rip), %r13      # 0x17e39
movq %r13, %rsi
movl $0x727, %edx            # imm = 0x727
xorl %eax, %eax
callq 0xc2a1
movq %rbp, %rsi
callq 0xea71
leaq 0x94a3(%rip), %rdi      # 0x192af
movq %r13, %rsi
movl %r15d, %r13d
movl $0x729, %edx            # imm = 0x729
xorl %eax, %eax
callq 0xc2a1
cmpb $0x1, 0x55(%rsp)
jg 0xff9c
movsbl 0x54(%rsp), %ecx
testl %ecx, %ecx
je 0xff79
cmpb $0x6, %cl
jge 0xff79
cmpb $0x1, %cl
je 0xfed2
movzbl %cl, %eax
cmpl $0x2, %eax
je 0xfebd
cmpl $0x5, %eax
jne 0xff3c
cmpl $0x0, 0x444(%rbx)
je 0xfe7c
leaq 0x94bd(%rip), %rdi      # 0x19326
leaq 0x7fc9(%rip), %rsi      # 0x17e39
movl $0x737, %edx            # imm = 0x737
xorl %eax, %eax
callq 0xc2a1
movl 0x88(%rsp), %esi
movq %rbx, %rdi
callq 0xf39b
movq %rax, 0x248(%rbx)
testq %rax, %rax
je 0xffcd
cmpl $0x0, 0x444(%rbx)
je 0xff5f
leaq 0x94b7(%rip), %rdi      # 0x19366
leaq 0x7f83(%rip), %rsi      # 0x17e39
movl $0x73a, %edx            # imm = 0x73A
jmp 0xff33
movq %rbx, %rdi
leaq 0x10(%rsp), %rsi
movq %r14, %rdx
callq 0xf810
jmp 0xff5f
cmpl $0x0, 0x444(%rbx)
je 0xfef8
leaq 0x94b1(%rip), %rdi      # 0x19393
leaq 0x7f50(%rip), %rsi      # 0x17e39
movl $0x746, %edx            # imm = 0x746
movl %r13d, %ecx
xorl %eax, %eax
callq 0xc2a1
movl 0x88(%rsp), %esi
movq %rbx, %rdi
callq 0xf39b
movq %rax, 0x248(%rbx)
testq %rax, %rax
je 0xffcd
cmpl $0x0, 0x444(%rbx)
je 0xff5f
leaq 0x94b8(%rip), %rdi      # 0x193df
leaq 0x7f0b(%rip), %rsi      # 0x17e39
movl $0x749, %edx            # imm = 0x749
xorl %eax, %eax
callq 0xc2a1
jmp 0xff5f
cmpl $0x0, 0x444(%rbx)
je 0xff5f
leaq 0x94ca(%rip), %rdi      # 0x19416
leaq 0x7ee6(%rip), %rsi      # 0x17e39
movl $0x75a, %edx            # imm = 0x75A
xorl %eax, %eax
callq 0xc2a1
subq $-0x80, %r12
incl %r13d
jmp 0xfd3a
pushq $0x1e
jmp 0xfc15
pushq $0x1f
jmp 0xfc15
cmpl $0x0, 0x444(%rbx)
je 0xff9c
leaq 0x935c(%rip), %rdi      # 0x192e5
leaq 0x7ea9(%rip), %rsi      # 0x17e39
movl $0x730, %edx            # imm = 0x730
xorl %eax, %eax
callq 0xc2a1
cmpl $0x0, 0x444(%rbx)
je 0xffc6
leaq 0x94b3(%rip), %rdi      # 0x1945f
leaq 0x7e86(%rip), %rsi      # 0x17e39
xorl %ebp, %ebp
movl $0x764, %edx            # imm = 0x764
xorl %eax, %eax
callq 0xc2a1
jmp 0xfc16
xorl %ebp, %ebp
jmp 0xfc16
pushq $0x20
jmp 0xfc15