// File: /workspace/github_c_makefile_star2_O3/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 0x102c1
movq %rsi, %r15
testq %rsi, %rsi
je 0x102c8
movq %rdx, %r14
testq %rdx, %rdx
je 0x102cf
movq %rdi, %rbx
cmpl $0x0, 0x444(%rdi)
je 0x1024d
leaq 0x9ea5(%rip), %rdi      # 0x1a0dc
leaq 0x8bfb(%rip), %rsi      # 0x18e39
movl $0x6ec, %edx            # imm = 0x6EC
movq %r15, %rcx
xorl %eax, %eax
callq 0xc84f
movq %rbx, %rdi
movq %r15, %rsi
callq 0xfbc8
testl %eax, %eax
jne 0x102bd
cmpl $0x0, 0x444(%rbx)
je 0x10282
leaq 0x9e98(%rip), %rdi      # 0x1a104
leaq 0x8bc6(%rip), %rsi      # 0x18e39
movl $0x6f2, %edx            # imm = 0x6F2
movq %r14, %rcx
xorl %eax, %eax
callq 0xc84f
movq %r14, %rsi
callq 0xfcdc
testl %eax, %eax
jne 0x102bd
cmpl $0x0, 0x444(%rbx)
je 0x102b1
leaq 0x9e9f(%rip), %rdi      # 0x1a13d
leaq 0x8b94(%rip), %rsi      # 0x18e39
movl $0x6f9, %edx            # imm = 0x6F9
xorl %eax, %eax
callq 0xc84f
movq %rbx, %rdi
callq 0xed26
testl %eax, %eax
je 0x102e8
movl %eax, %ebp
jmp 0x102d4
movl $0xa, %ebp
jmp 0x102d4
movl $0xb, %ebp
jmp 0x102d4
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 0x1030b
leaq 0x9e76(%rip), %rdi      # 0x1a16e
leaq 0x8b3a(%rip), %rsi      # 0x18e39
movl $0x6fd, %edx            # imm = 0x6FD
xorl %eax, %eax
callq 0xc84f
movq %rbx, %rdi
callq 0xed5c
movq %rbx, %rdi
callq 0xee9f
movl $0x67, %ebp
testl %eax, %eax
jg 0x102d4
cmpl $0x0, 0x444(%rbx)
je 0x10358
movq %rbx, %rdi
callq 0xeedd
cmpl $0x0, 0x444(%rbx)
je 0x10358
leaq 0x9e5d(%rip), %rdi      # 0x1a1a2
leaq 0x8aed(%rip), %rsi      # 0x18e39
movl $0x706, %edx            # imm = 0x706
xorl %eax, %eax
callq 0xc84f
movq %rbx, %rdi
callq 0xf148
testl %eax, %eax
jne 0x102bd
cmpl $0x0, 0x444(%rbx)
je 0x1038b
leaq 0x9e53(%rip), %rdi      # 0x1a1cb
leaq 0x8aba(%rip), %rsi      # 0x18e39
movl $0x70a, %edx            # imm = 0x70A
xorl %eax, %eax
callq 0xc84f
movl 0x280(%rbx), %esi
movq %rbx, %rdi
callq 0xfa26
movq %rax, 0x38(%rbx)
testq %rax, %rax
je 0x10628
cmpl $0x0, 0x444(%rbx)
je 0x103c9
leaq 0x9e48(%rip), %rdi      # 0x1a1fe
leaq 0x8a7c(%rip), %rsi      # 0x18e39
movl $0x70e, %edx            # imm = 0x70E
xorl %eax, %eax
callq 0xc84f
movl 0x278(%rbx), %esi
movq %rbx, %rdi
callq 0xfa26
movq %rax, 0x250(%rbx)
testq %rax, %rax
je 0x10632
movq %rax, %r15
movq 0x18(%rbx), %rax
movq %rax, 0x8(%rsp)
testq %rax, %rax
jle 0x1065f
addq %r15, 0x8(%rsp)
xorl %r13d, %r13d
xorl %ecx, %ecx
movl %ecx, 0x74(%rsp)
movb $0x0, 0x78(%rsp)
movq %rcx, 0x88(%rsp)
movl %ecx, 0x90(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movaps %xmm0, 0x20(%rsp)
movaps %xmm0, 0x30(%rsp)
movaps %xmm0, 0x40(%rsp)
movq %rcx, 0x4e(%rsp)
leaq 0x58(%rsp), %rax
movq %rcx, 0x5(%rax)
movq %rcx, (%rax)
movq %r15, %rdi
callq 0x10edc
testl %eax, %eax
jle 0x1065f
cmpl $0x0, 0x444(%rbx)
je 0x1047c
leaq 0x9dd4(%rip), %rdi      # 0x1a23a
leaq 0x89cc(%rip), %rsi      # 0x18e39
movl $0x722, %edx            # imm = 0x722
movl %r13d, %ecx
xorl %eax, %eax
callq 0xc84f
movq %rbx, %rdi
movq %r15, %rsi
leaq 0x10(%rsp), %r12
movq %r12, %rdx
callq 0xefcc
cmpl $0x0, 0x444(%rbx)
je 0x104d3
leaq 0x9dd5(%rip), %rdi      # 0x1a274
leaq 0x8993(%rip), %rbp      # 0x18e39
movq %rbp, %rsi
movl $0x727, %edx            # imm = 0x727
xorl %eax, %eax
callq 0xc84f
movq %r12, %rsi
callq 0xf0dc
leaq 0x9deb(%rip), %rdi      # 0x1a2af
movq %rbp, %rsi
movl $0x729, %edx            # imm = 0x729
xorl %eax, %eax
callq 0xc84f
cmpb $0x1, 0x55(%rsp)
jg 0x1065f
movsbl 0x54(%rsp), %ecx
testl %ecx, %ecx
je 0x1063c
cmpb $0x6, %cl
jge 0x1063c
cmpb $0x1, %cl
leaq 0x10(%rsp), %rsi
je 0x10587
movzbl %cl, %eax
cmpl $0x2, %eax
je 0x10577
cmpl $0x5, %eax
jne 0x105f1
cmpl $0x0, 0x444(%rbx)
je 0x10536
leaq 0x9e03(%rip), %rdi      # 0x1a326
leaq 0x890f(%rip), %rsi      # 0x18e39
movl $0x737, %edx            # imm = 0x737
xorl %eax, %eax
callq 0xc84f
movl 0x88(%rsp), %esi
movq %rbx, %rdi
callq 0xfa26
movq %rax, 0x248(%rbx)
testq %rax, %rax
je 0x10690
cmpl $0x0, 0x444(%rbx)
je 0x10614
leaq 0x9dfd(%rip), %rdi      # 0x1a366
leaq 0x88c9(%rip), %rsi      # 0x18e39
movl $0x73a, %edx            # imm = 0x73A
jmp 0x105e8
movq %rbx, %rdi
movq %r14, %rdx
callq 0xfec0
jmp 0x10614
cmpl $0x0, 0x444(%rbx)
je 0x105ad
leaq 0x9dfc(%rip), %rdi      # 0x1a393
leaq 0x889b(%rip), %rsi      # 0x18e39
movl $0x746, %edx            # imm = 0x746
movl %r13d, %ecx
xorl %eax, %eax
callq 0xc84f
movl 0x88(%rsp), %esi
movq %rbx, %rdi
callq 0xfa26
movq %rax, 0x248(%rbx)
testq %rax, %rax
je 0x10690
cmpl $0x0, 0x444(%rbx)
je 0x10614
leaq 0x9e03(%rip), %rdi      # 0x1a3df
leaq 0x8856(%rip), %rsi      # 0x18e39
movl $0x749, %edx            # imm = 0x749
xorl %eax, %eax
callq 0xc84f
jmp 0x10614
cmpl $0x0, 0x444(%rbx)
je 0x10614
leaq 0x9e15(%rip), %rdi      # 0x1a416
leaq 0x8831(%rip), %rsi      # 0x18e39
movl $0x75a, %edx            # imm = 0x75A
xorl %eax, %eax
callq 0xc84f
subq $-0x80, %r15
incl %r13d
cmpq 0x8(%rsp), %r15
jb 0x10404
jmp 0x1065f
movl $0x1e, %ebp
jmp 0x102d4
movl $0x1f, %ebp
jmp 0x102d4
cmpl $0x0, 0x444(%rbx)
je 0x1065f
leaq 0x9c99(%rip), %rdi      # 0x1a2e5
leaq 0x87e6(%rip), %rsi      # 0x18e39
movl $0x730, %edx            # imm = 0x730
xorl %eax, %eax
callq 0xc84f
cmpl $0x0, 0x444(%rbx)
je 0x10689
leaq 0x9df0(%rip), %rdi      # 0x1a45f
leaq 0x87c3(%rip), %rsi      # 0x18e39
xorl %ebp, %ebp
movl $0x764, %edx            # imm = 0x764
xorl %eax, %eax
callq 0xc84f
jmp 0x102d4
xorl %ebp, %ebp
jmp 0x102d4
movl $0x20, %ebp
jmp 0x102d4
nop