// File: /workspace/github_c_makefile_star2_O2/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 0x1082c
movslq %eax, %r15
cmpl $0x0, 0x8(%rbx)
je 0x102ba
movq %r13, (%rsp)
leaq 0x93b1(%rip), %rdi      # 0x1964f
leaq 0x9225(%rip), %rsi      # 0x194ca
movl $0x142, %edx            # imm = 0x142
movq %r15, %rcx
movq %r15, %r8
movq %r13, %r9
xorl %eax, %eax
callq 0xc2a1
movq %r13, %rax
subq %r15, %rax
cmpq $0x3, %rax
movq %r12, 0x20(%rsp)
movq %r13, 0x18(%rsp)
ja 0x1040c
leaq 0x4(%r14), %rdi
movq %rdi, 0x10(%rsp)
callq 0x10828
movl %eax, %ebp
cmpl $0x0, 0x8(%rbx)
je 0x10309
leaq 0x93be(%rip), %rdi      # 0x196af
leaq 0x91d2(%rip), %rsi      # 0x194ca
movl $0x153, %edx            # imm = 0x153
movl %ebp, %ecx
movl %ebp, %r8d
xorl %eax, %eax
callq 0xc2a1
leaq 0x6(%r14), %rdi
callq 0x10828
movl %eax, %r13d
cmpl $0x0, 0x8(%rbx)
je 0x1033b
leaq 0x93cd(%rip), %rdi      # 0x196ef
leaq 0x91a1(%rip), %rsi      # 0x194ca
movl $0x155, %edx            # imm = 0x155
movl %r13d, %ecx
movl %r13d, %r8d
xorl %eax, %eax
callq 0xc2a1
leaq 0x8(%r14), %rdi
callq 0x10828
cmpl $0x0, 0x8(%rbx)
je 0x10369
leaq 0x93dc(%rip), %rdi      # 0x1972d
leaq 0x9172(%rip), %rsi      # 0x194ca
movl $0x157, %edx            # imm = 0x157
movl %eax, %ecx
movl %eax, %r8d
xorl %eax, %eax
callq 0xc2a1
leaq 0xc(%r14), %rdi
callq 0x10828
cmpl $0x0, 0x8(%rbx)
je 0x10397
leaq 0x93ee(%rip), %rdi      # 0x1976d
leaq 0x9144(%rip), %rsi      # 0x194ca
movl $0x159, %edx            # imm = 0x159
movl %eax, %ecx
movl %eax, %r8d
xorl %eax, %eax
callq 0xc2a1
addq $0xe, %r14
movq %r14, %rdi
callq 0x10828
cmpl $0x0, 0x8(%rbx)
je 0x104a9
leaq 0x93f9(%rip), %rdi      # 0x197ad
leaq 0x910f(%rip), %rsi      # 0x194ca
movl $0x15b, %edx            # imm = 0x15B
movl %eax, %ecx
movl %eax, %r8d
xorl %eax, %eax
callq 0xc2a1
cmpl $0x0, 0x8(%rbx)
je 0x104a9
cmpl $0x62, %r13d
je 0x10595
cmpl $0x64, %r13d
je 0x10542
cmpl $0x63, %r13d
jne 0x105b1
leaq 0x9431(%rip), %rdi      # 0x1982c
leaq 0x90c8(%rip), %rsi      # 0x194ca
movl $0x165, %edx            # imm = 0x165
jmp 0x105a8
cmpl $0x0, 0x8(%rbx)
je 0x1042c
leaq 0x961c(%rip), %rdi      # 0x19a35
leaq 0x90aa(%rip), %rsi      # 0x194ca
movl $0x188, %edx            # imm = 0x188
xorl %eax, %eax
callq 0xc2a1
leaq 0x6(%r14), %rdi
callq 0x23b0
movq %rax, %rbp
movq %rax, %rdi
callq 0x2110
leaq (%r14,%rax), %r15
addq $0x6, %r15
leaq (%r14,%rax), %rdi
addq $0x7, %rdi
callq 0x23b0
movq %rax, %r14
movq %rax, %rdi
callq 0x2110
leaq (%rax,%r15), %r12
incq %r12
leaq (%rax,%r15), %rdi
addq $0xa, %rdi
callq 0x23b0
movq %rax, %r13
movq %rax, %rdi
callq 0x2110
movq %rax, %r15
leaq (%rax,%r12), %rdi
addq $0xa, %rdi
callq 0x1082f
movl %eax, %eax
addq %r15, %r12
addq $0xe, %r12
movq 0x18(%rsp), %r15
cmpq %r15, %rax
cmovbq %rax, %r15
jmp 0x10621
movslq %ebp, %rax
movq 0x10(%rsp), %rcx
leaq (%rcx,%rax), %r12
addq $-0x4, %r12
cmpl $0x64, %r13d
jne 0x105de
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 0x10187
movl %eax, %ebp
movl 0x8(%rbx), %eax
testl %ebp, %ebp
js 0x1051b
testl %eax, %eax
je 0x10511
leaq 0x9434(%rip), %rdi      # 0x19930
leaq 0x8fc7(%rip), %rsi      # 0x194ca
movl $0x17b, %edx            # imm = 0x17B
movl %ebp, %ecx
xorl %eax, %eax
callq 0xc2a1
movl %ebp, %eax
addq %rax, %r12
jmp 0x105de
testl %eax, %eax
je 0x105de
leaq 0x944d(%rip), %rdi      # 0x19977
leaq 0x8f99(%rip), %rsi      # 0x194ca
movl $0x17e, %edx            # imm = 0x17E
xorl %eax, %eax
callq 0xc2a1
jmp 0x105de
leaq 0x92a3(%rip), %rdi      # 0x197ec
leaq 0x8f7a(%rip), %rsi      # 0x194ca
movl $0x162, %edx            # imm = 0x162
xorl %eax, %eax
callq 0xc2a1
movslq %ebp, %rax
movq 0x10(%rsp), %rcx
leaq (%rcx,%rax), %r12
addq $-0x4, %r12
cmpl $0x0, 0x8(%rbx)
je 0x104c3
leaq 0x936c(%rip), %rdi      # 0x198e9
leaq 0x8f46(%rip), %rsi      # 0x194ca
movl $0x176, %edx            # imm = 0x176
xorl %eax, %eax
callq 0xc2a1
jmp 0x104c3
leaq 0x92cc(%rip), %rdi      # 0x19868
leaq 0x8f27(%rip), %rsi      # 0x194ca
movl $0x168, %edx            # imm = 0x168
xorl %eax, %eax
callq 0xc2a1
jmp 0x105ce
leaq 0x92e7(%rip), %rdi      # 0x1989f
leaq 0x8f0b(%rip), %rsi      # 0x194ca
movl $0x16b, %edx            # imm = 0x16B
movl %r13d, %ecx
xorl %eax, %eax
callq 0xc2a1
movslq %ebp, %rax
movq 0x10(%rsp), %rcx
leaq (%rcx,%rax), %r12
addq $-0x4, %r12
leaq 0x93f2(%rip), %rdi      # 0x199d7
xorl %r13d, %r13d
movq %r15, %rsi
xorl %eax, %eax
callq 0xbfbe
movq %rax, %rbp
cmpl $0x0, 0x8(%rbx)
je 0x10650
leaq 0x93df(%rip), %rdi      # 0x199e1
leaq 0x8ec1(%rip), %rsi      # 0x194ca
xorl %r14d, %r14d
movl $0x185, %edx            # imm = 0x185
movq %rbp, %rcx
movq %r15, %r8
xorl %eax, %eax
callq 0xc2a1
xorl %r13d, %r13d
cmpl $0x0, 0x8(%rbx)
je 0x10653
movq %r15, (%rsp)
leaq 0x944d(%rip), %rdi      # 0x19a7f
leaq 0x8e91(%rip), %rsi      # 0x194ca
movl $0x1a5, %edx            # imm = 0x1A5
movq %rbp, %rcx
movq %r14, %r8
movq %r13, %r9
xorl %eax, %eax
callq 0xc2a1
jmp 0x10653
xorl %r14d, %r14d
movq %rbp, %rdi
callq 0x1013b
movq %r14, %rdi
callq 0x1013b
movq %r13, %rdi
callq 0x1013b
cmpl $0x0, 0x8(%rbx)
je 0x1068b
leaq 0x944f(%rip), %rdi      # 0x19ac7
leaq 0x8e4b(%rip), %rsi      # 0x194ca
movl $0x1ae, %edx            # imm = 0x1AE
xorl %eax, %eax
callq 0xc2a1
movq %rbx, %rdi
movq %rbp, %rsi
movq 0x20(%rsp), %rdx
movq %r12, %rcx
movq %r15, %r8
callq 0xffef
testl %eax, %eax
je 0x1070e
cmpl $0x0, 0x8(%rbx)
je 0x106c5
leaq 0x94a8(%rip), %rdi      # 0x19b5a
leaq 0x8e11(%rip), %rsi      # 0x194ca
movl $0x1bb, %edx            # imm = 0x1BB
xorl %eax, %eax
callq 0xc2a1
movq %r14, %rdi
callq 0x2030
movq %rbp, %rdi
callq 0x2030
movq %r13, %rdi
callq 0x2030
cmpl $0x0, 0x8(%rbx)
je 0x106fd
leaq 0x94a5(%rip), %rdi      # 0x19b8f
leaq 0x8dd9(%rip), %rsi      # 0x194ca
movl $0x1c1, %edx            # imm = 0x1C1
xorl %eax, %eax
callq 0xc2a1
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x0, 0x8(%rbx)
je 0x1072e
leaq 0x93f4(%rip), %rdi      # 0x19b0f
leaq 0x8da8(%rip), %rsi      # 0x194ca
movl $0x1b3, %edx            # imm = 0x1B3
xorl %eax, %eax
callq 0xc2a1
cmpl $0x0, 0xc(%rbx)
jle 0x106a5
movq (%rbx), %rax
testq %rax, %rax
je 0x106a5
movq %rbp, %rdi
callq *%rax
jmp 0x106a5