// File: /workspace/github_c_makefile_star2_O3/inflex[P]ripMIME/ripOLE/olestream-unwrap.c
// Function: OLEUNWRAP_decode_attachment
int OLEUNWRAP_decode_attachment( struct OLEUNWRAP_object *oleuw, char *stream, size_t stream_size, char *decode_path )
{
	struct OLE10_header oh;
	char *sp = stream;
	char *data_start_point = stream;
	int result = OLEUW_OK;

	// Get the data size
	oh.attach_size_1 = (size_t)get_int32( sp );
	sp += 4;

	DUW LOGGER_log("%s:%d:OLEUNWRAP_decode_attachment:DEBUG: attachsize = %d [ 0x%x ], stream length = %d [ 0x%x] \n", FL, oh.attach_size_1, oh.attach_size_1, stream_size, stream_size );

	oh.attach_start_offset = (stream_size -oh.attach_size_1);
	data_start_point = stream +oh.attach_start_offset;

	//if (oh.attach_start_offset == 4)
	if (oh.attach_start_offset < 4)
	{
		int cbheader; // number of bytes in PIC
		int mfpmm;
		int mfpxext;
		int mfpyext;
		int mfphmf;
			
		// check next 4 bytes.

		cbheader = get_uint16( sp );
		DUW LOGGER_log("%s:%d:OLEUNWRAP_decode_attachment:DEBUG: cbHeader = %d [ 0x%x ]", FL, cbheader, cbheader);
		mfpmm = get_uint16( sp +2 );
		DUW LOGGER_log("%s:%d:OLEUNWRAP_decode_attachment:DEBUG: mfp.mm = %d [ 0x%x ]", FL, mfpmm, mfpmm);
		mfpxext = get_uint16( sp +4 );
		DUW LOGGER_log("%s:%d:OLEUNWRAP_decode_attachment:DEBUG: mfp.xext = %d [ 0x%x ]", FL, mfpxext, mfpxext);
		mfpyext = get_uint16( sp +8 );
		DUW LOGGER_log("%s:%d:OLEUNWRAP_decode_attachment:DEBUG: mfp.yext = %d [ 0x%x ]", FL, mfpyext, mfpyext);
		mfphmf = get_uint16( sp +10 );
		DUW LOGGER_log("%s:%d:OLEUNWRAP_decode_attachment:DEBUG: mfp.hmf = %d [ 0x%x ]", FL, mfphmf, mfphmf);
		// If we only had the stream byte-lenght in our header
		//		then we know we don't have a complex header.
		 
		DUW {
		switch (mfpmm) {
			case 100:
				LOGGER_log("%s:%d:OLEUNWRAP_decode_attachment:DEBUG: Image is Escher format",FL);
				break;
			case 99:
				LOGGER_log("%s:%d:OLEUNWRAP_decode_attachment:DEBUG: Image is Bitmapped",FL);
				break;
			case 98:
				LOGGER_log("%s:%d:OLEUNWRAP_decode_attachment:DEBUG: Image is TIFF",FL);
				break;
			default:
				LOGGER_log("%s:%d:OLEUNWRAP_decode_attachment:DEBUG: Unknown image type for code '%d'",FL, mfpmm);
		}
		}

		data_start_point = sp +cbheader -4;

		
		if (mfpmm == 100) {
			int imageoffset = 0;
			int search_size = 500;

			DUW LOGGER_log("%s:%d:OLEUNWRAP_decode_attachment:DEBUG: searcing for image signatures",FL);
			if (stream_size < (search_size +68)) search_size = (stream_size -69); /** just make sure we don't over-search the stream **/

			imageoffset = OLEUNWRAP_seach_for_file_sig(oleuw, data_start_point, search_size);
			if (imageoffset >= 0) {
				DUW LOGGER_log("%s:%d:OLEUNWRAP_decode_attachment:DEBUG: Image data found at offset %d",FL,imageoffset);
				data_start_point += imageoffset;
			} else {
				DUW LOGGER_log("%s:%d:OLEUNWRAP_decode_attachment:DEBUG: Could not detect image signature, dumping whole stream",FL);
			}
		}
			
		oh.attach_name = PLD_dprintf("image-%ld",oh.attach_size_1);
		oh.attach_size = oh.attach_size_1;
		oh.fname_1 = oh.fname_2 = NULL;
		DUW LOGGER_log("%s:%d:OLEUNWRAP_decode_attachment:DEBUG: Setting attachment name to '%s', size = %d",FL,oh.attach_name, oh.attach_size);
	} else {

		DUW LOGGER_log("%s:%d:OLEUNWRAP_decode_attachment:DEBUG: Decoding file information header",FL);
		// Unknown memory segment
		memcpy( oh.data, sp, 2 );
		sp += 2;
		
		// Full attachment string
		oh.attach_name = strdup( sp );
		sp = sp + strlen(oh.attach_name) +1;

		// Attachment full path
		oh.fname_1 = strdup( sp );
		sp += strlen(oh.fname_1) +1;

		// Unknown memory segment
		memcpy( oh.data2, sp, 8 );
		sp = sp +8;

		// Attachment full path
		oh.fname_2 = strdup( sp );
		sp += strlen(oh.fname_2) +1;

		oh.attach_size = (size_t)get_uint32( sp );
		sp += 4;

		if (oh.attach_size > stream_size) oh.attach_size = stream_size;
		
		data_start_point = sp;
	} 

	DUW LOGGER_log("%s:%d:OLEUNWRAP_decode_attachment:DEBUG: Attachment %s:%s:%s size = %d\n",FL, oh.attach_name, oh.fname_1, oh.fname_2, oh.attach_size );


	/** 20050119:2053:PLD - Added to sanitize 8-bit filenames **/
	/** Sanitize the output filename **/
	OLEUNWRAP_sanitize_filename(oh.attach_name);
	OLEUNWRAP_sanitize_filename(oh.fname_1);
	OLEUNWRAP_sanitize_filename(oh.fname_2);

	DUW LOGGER_log("%s:%d:OLEUNWRAP_decode_attachment:DEBUG: Sanitized attachment filenames",FL);

	result = OLEUNWRAP_save_stream( oleuw, oh.attach_name, decode_path, data_start_point, oh.attach_size );
	if (result == OLEUW_OK)
	{
		DUW LOGGER_log("%s:%d:OLEUNWRAP_decode_attachment:DEBUG: Calling reporter for the filename",FL);
		if ((oleuw->verbose > 0)&&(oleuw->filename_report_fn != NULL))
		{
			oleuw->filename_report_fn(oh.attach_name);
		}
		// Do call back to reporting function
	}

	DUW LOGGER_log("%s:%d:OLEUNWRAP_decode_attachment:DEBUG: Cleaning up",FL);
	// Clean up our previously allocated data
	if (oh.fname_1 != NULL) free(oh.fname_1);
	if (oh.attach_name != NULL) free(oh.attach_name);
	if (oh.fname_2 != NULL) free(oh.fname_2);
	
	DUW LOGGER_log("%s:%d:OLEUNWRAP_decode_attachment:DEBUG: done.",FL);
	return OLEUW_OK;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rcx, %r12
movq %rdx, %r13
movq %rsi, %r14
movq %rdi, %rbx
movq %rsi, %rdi
callq 0x10ee8
movslq %eax, %r15
cmpl $0x0, 0x8(%rbx)
je 0x10968
movq %r13, (%rsp)
leaq 0x9d03(%rip), %rdi      # 0x1a64f
leaq 0x9b77(%rip), %rsi      # 0x1a4ca
movl $0x142, %edx            # imm = 0x142
movq %r15, %rcx
movq %r15, %r8
movq %r13, %r9
xorl %eax, %eax
callq 0xc84f
movq %r13, %rax
subq %r15, %rax
cmpq $0x3, %rax
movq %r12, 0x20(%rsp)
movq %r13, 0x18(%rsp)
ja 0x10aba
leaq 0x4(%r14), %rdi
movq %rdi, 0x10(%rsp)
callq 0x10ee4
movl %eax, %ebp
cmpl $0x0, 0x8(%rbx)
je 0x109b7
leaq 0x9d10(%rip), %rdi      # 0x1a6af
leaq 0x9b24(%rip), %rsi      # 0x1a4ca
movl $0x153, %edx            # imm = 0x153
movl %ebp, %ecx
movl %ebp, %r8d
xorl %eax, %eax
callq 0xc84f
leaq 0x6(%r14), %rdi
callq 0x10ee4
movl %eax, %r13d
cmpl $0x0, 0x8(%rbx)
je 0x109e9
leaq 0x9d1f(%rip), %rdi      # 0x1a6ef
leaq 0x9af3(%rip), %rsi      # 0x1a4ca
movl $0x155, %edx            # imm = 0x155
movl %r13d, %ecx
movl %r13d, %r8d
xorl %eax, %eax
callq 0xc84f
leaq 0x8(%r14), %rdi
callq 0x10ee4
cmpl $0x0, 0x8(%rbx)
je 0x10a17
leaq 0x9d2e(%rip), %rdi      # 0x1a72d
leaq 0x9ac4(%rip), %rsi      # 0x1a4ca
movl $0x157, %edx            # imm = 0x157
movl %eax, %ecx
movl %eax, %r8d
xorl %eax, %eax
callq 0xc84f
leaq 0xc(%r14), %rdi
callq 0x10ee4
cmpl $0x0, 0x8(%rbx)
je 0x10a45
leaq 0x9d40(%rip), %rdi      # 0x1a76d
leaq 0x9a96(%rip), %rsi      # 0x1a4ca
movl $0x159, %edx            # imm = 0x159
movl %eax, %ecx
movl %eax, %r8d
xorl %eax, %eax
callq 0xc84f
addq $0xe, %r14
movq %r14, %rdi
callq 0x10ee4
cmpl $0x0, 0x8(%rbx)
je 0x10b57
leaq 0x9d4b(%rip), %rdi      # 0x1a7ad
leaq 0x9a61(%rip), %rsi      # 0x1a4ca
movl $0x15b, %edx            # imm = 0x15B
movl %eax, %ecx
movl %eax, %r8d
xorl %eax, %eax
callq 0xc84f
cmpl $0x0, 0x8(%rbx)
je 0x10b57
cmpl $0x62, %r13d
je 0x10c43
cmpl $0x64, %r13d
je 0x10bf0
cmpl $0x63, %r13d
jne 0x10c5f
leaq 0x9d83(%rip), %rdi      # 0x1a82c
leaq 0x9a1a(%rip), %rsi      # 0x1a4ca
movl $0x165, %edx            # imm = 0x165
jmp 0x10c56
cmpl $0x0, 0x8(%rbx)
je 0x10ada
leaq 0x9f6e(%rip), %rdi      # 0x1aa35
leaq 0x99fc(%rip), %rsi      # 0x1a4ca
movl $0x188, %edx            # imm = 0x188
xorl %eax, %eax
callq 0xc84f
leaq 0x6(%r14), %rdi
callq 0x23a0
movq %rax, %r13
movq %rax, %rdi
callq 0x2110
leaq (%r14,%rax), %r15
addq $0x6, %r15
leaq (%r14,%rax), %rdi
addq $0x7, %rdi
callq 0x23a0
movq %rax, %rbp
movq %rax, %rdi
callq 0x2110
leaq (%rax,%r15), %r12
incq %r12
leaq (%rax,%r15), %rdi
addq $0xa, %rdi
callq 0x23a0
movq %rax, %r14
movq %rax, %rdi
callq 0x2110
movq %rax, %r15
leaq (%rax,%r12), %rdi
addq $0xa, %rdi
callq 0x10eeb
movl %eax, %eax
addq %r15, %r12
addq $0xe, %r12
movq 0x18(%rsp), %r15
cmpq %r15, %rax
cmovbq %rax, %r15
jmp 0x10cce
movslq %ebp, %rax
movq 0x10(%rsp), %rcx
leaq (%rcx,%rax), %r12
addq $-0x4, %r12
cmpl $0x64, %r13d
jne 0x10c8c
movq 0x18(%rsp), %rcx
leal -0x45(%rcx), %eax
cmpq $0x238, %rcx            # imm = 0x238
cltq
movl $0x1f4, %edx            # imm = 0x1F4
cmovbq %rax, %rdx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x10861
movl %eax, %ebp
movl 0x8(%rbx), %eax
testl %ebp, %ebp
js 0x10bc9
testl %eax, %eax
je 0x10bbf
leaq 0x9d86(%rip), %rdi      # 0x1a930
leaq 0x9919(%rip), %rsi      # 0x1a4ca
movl $0x17b, %edx            # imm = 0x17B
movl %ebp, %ecx
xorl %eax, %eax
callq 0xc84f
movl %ebp, %eax
addq %rax, %r12
jmp 0x10c8c
testl %eax, %eax
je 0x10c8c
leaq 0x9d9f(%rip), %rdi      # 0x1a977
leaq 0x98eb(%rip), %rsi      # 0x1a4ca
movl $0x17e, %edx            # imm = 0x17E
xorl %eax, %eax
callq 0xc84f
jmp 0x10c8c
leaq 0x9bf5(%rip), %rdi      # 0x1a7ec
leaq 0x98cc(%rip), %rsi      # 0x1a4ca
movl $0x162, %edx            # imm = 0x162
xorl %eax, %eax
callq 0xc84f
movslq %ebp, %rax
movq 0x10(%rsp), %rcx
leaq (%rcx,%rax), %r12
addq $-0x4, %r12
cmpl $0x0, 0x8(%rbx)
je 0x10b71
leaq 0x9cbe(%rip), %rdi      # 0x1a8e9
leaq 0x9898(%rip), %rsi      # 0x1a4ca
movl $0x176, %edx            # imm = 0x176
xorl %eax, %eax
callq 0xc84f
jmp 0x10b71
leaq 0x9c1e(%rip), %rdi      # 0x1a868
leaq 0x9879(%rip), %rsi      # 0x1a4ca
movl $0x168, %edx            # imm = 0x168
xorl %eax, %eax
callq 0xc84f
jmp 0x10c7c
leaq 0x9c39(%rip), %rdi      # 0x1a89f
leaq 0x985d(%rip), %rsi      # 0x1a4ca
movl $0x16b, %edx            # imm = 0x16B
movl %r13d, %ecx
xorl %eax, %eax
callq 0xc84f
movslq %ebp, %rax
movq 0x10(%rsp), %rcx
leaq (%rcx,%rax), %r12
addq $-0x4, %r12
leaq 0x9d44(%rip), %rdi      # 0x1a9d7
xorl %r14d, %r14d
movq %r15, %rsi
xorl %eax, %eax
callq 0xc566
movq %rax, %r13
cmpl $0x0, 0x8(%rbx)
je 0x10cfd
leaq 0x9d31(%rip), %rdi      # 0x1a9e1
leaq 0x9813(%rip), %rsi      # 0x1a4ca
xorl %ebp, %ebp
movl $0x185, %edx            # imm = 0x185
movq %r13, %rcx
movq %r15, %r8
xorl %eax, %eax
callq 0xc84f
xorl %r14d, %r14d
cmpl $0x0, 0x8(%rbx)
je 0x10cff
movq %r15, (%rsp)
leaq 0x9da0(%rip), %rdi      # 0x1aa7f
leaq 0x97e4(%rip), %rsi      # 0x1a4ca
movl $0x1a5, %edx            # imm = 0x1A5
movq %r13, %rcx
movq %rbp, %r8
movq %r14, %r9
xorl %eax, %eax
callq 0xc84f
jmp 0x10cff
xorl %ebp, %ebp
movq %r13, %rdi
callq 0x10809
movq %rbp, %rdi
callq 0x10809
movq %r14, %rdi
callq 0x10809
cmpl $0x0, 0x8(%rbx)
je 0x10d37
leaq 0x9da3(%rip), %rdi      # 0x1aac7
leaq 0x979f(%rip), %rsi      # 0x1a4ca
movl $0x1ae, %edx            # imm = 0x1AE
xorl %eax, %eax
callq 0xc84f
movq %rbx, %rdi
movq %r13, %rsi
movq 0x20(%rsp), %rdx
movq %r12, %rcx
movq %r15, %r8
callq 0x106b7
testl %eax, %eax
je 0x10dc9
cmpl $0x0, 0x8(%rbx)
je 0x10d71
leaq 0x9dfc(%rip), %rdi      # 0x1ab5a
leaq 0x9765(%rip), %rsi      # 0x1a4ca
movl $0x1bb, %edx            # imm = 0x1BB
xorl %eax, %eax
callq 0xc84f
testq %rbp, %rbp
je 0x10d7e
movq %rbp, %rdi
callq 0x2030
testq %r13, %r13
je 0x10d8b
movq %r13, %rdi
callq 0x2030
testq %r14, %r14
je 0x10d98
movq %r14, %rdi
callq 0x2030
cmpl $0x0, 0x8(%rbx)
je 0x10db8
leaq 0x9dea(%rip), %rdi      # 0x1ab8f
leaq 0x971e(%rip), %rsi      # 0x1a4ca
movl $0x1c1, %edx            # imm = 0x1C1
xorl %eax, %eax
callq 0xc84f
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x0, 0x8(%rbx)
je 0x10de9
leaq 0x9d39(%rip), %rdi      # 0x1ab0f
leaq 0x96ed(%rip), %rsi      # 0x1a4ca
movl $0x1b3, %edx            # imm = 0x1B3
xorl %eax, %eax
callq 0xc84f
cmpl $0x0, 0xc(%rbx)
jle 0x10d51
movq (%rbx), %rax
testq %rax, %rax
je 0x10d51
movq %r13, %rdi
callq *%rax
jmp 0x10d51