// File: /workspace/github_c_makefile_star2_O1/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 0x110f8
movslq %eax, %r15
cmpl $0x0, 0x8(%rbx)
je 0x10bb8
movq %r13, (%rsp)
leaq 0x9ac3(%rip), %rdi      # 0x1a65f
leaq 0x9937(%rip), %rsi      # 0x1a4da
movl $0x142, %edx            # imm = 0x142
movq %r15, %rcx
movq %r15, %r8
movq %r13, %r9
xorl %eax, %eax
callq 0xc960
movq %r13, %rax
subq %r15, %rax
cmpq $0x3, %rax
movq %r12, 0x20(%rsp)
movq %r13, 0x10(%rsp)
ja 0x10d06
leaq 0x4(%r14), %rdi
movq %rdi, 0x18(%rsp)
callq 0x110f4
movl %eax, %ebp
cmpl $0x0, 0x8(%rbx)
je 0x10c07
leaq 0x9ad0(%rip), %rdi      # 0x1a6bf
leaq 0x98e4(%rip), %rsi      # 0x1a4da
movl $0x153, %edx            # imm = 0x153
movl %ebp, %ecx
movl %ebp, %r8d
xorl %eax, %eax
callq 0xc960
leaq 0x6(%r14), %rdi
callq 0x110f4
movl %eax, %r13d
cmpl $0x0, 0x8(%rbx)
je 0x10c39
leaq 0x9adf(%rip), %rdi      # 0x1a6ff
leaq 0x98b3(%rip), %rsi      # 0x1a4da
movl $0x155, %edx            # imm = 0x155
movl %r13d, %ecx
movl %r13d, %r8d
xorl %eax, %eax
callq 0xc960
leaq 0x8(%r14), %rdi
callq 0x110f4
cmpl $0x0, 0x8(%rbx)
je 0x10c67
leaq 0x9aee(%rip), %rdi      # 0x1a73d
leaq 0x9884(%rip), %rsi      # 0x1a4da
movl $0x157, %edx            # imm = 0x157
movl %eax, %ecx
movl %eax, %r8d
xorl %eax, %eax
callq 0xc960
leaq 0xc(%r14), %rdi
callq 0x110f4
cmpl $0x0, 0x8(%rbx)
je 0x10c95
leaq 0x9b00(%rip), %rdi      # 0x1a77d
leaq 0x9856(%rip), %rsi      # 0x1a4da
movl $0x159, %edx            # imm = 0x159
movl %eax, %ecx
movl %eax, %r8d
xorl %eax, %eax
callq 0xc960
addq $0xe, %r14
movq %r14, %rdi
callq 0x110f4
cmpl $0x0, 0x8(%rbx)
je 0x10cc6
leaq 0x9b0f(%rip), %rdi      # 0x1a7bd
leaq 0x9825(%rip), %rsi      # 0x1a4da
movl $0x15b, %edx            # imm = 0x15B
movl %eax, %ecx
movl %eax, %r8d
xorl %eax, %eax
callq 0xc960
cmpl $0x0, 0x8(%rbx)
je 0x10df1
cmpl $0x62, %r13d
je 0x10db8
cmpl $0x63, %r13d
je 0x10da3
cmpl $0x64, %r13d
jne 0x10dd4
leaq 0x9b07(%rip), %rdi      # 0x1a7fc
leaq 0x97de(%rip), %rsi      # 0x1a4da
movl $0x162, %edx            # imm = 0x162
jmp 0x10dcb
cmpl $0x0, 0x8(%rbx)
je 0x10d26
leaq 0x9d32(%rip), %rdi      # 0x1aa45
leaq 0x97c0(%rip), %rsi      # 0x1a4da
movl $0x188, %edx            # imm = 0x188
xorl %eax, %eax
callq 0xc960
leaq 0x6(%r14), %rdi
callq 0x23b0
movq %rax, %r13
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, %rbp
movq %rax, %rdi
callq 0x2110
movq %rax, %r15
leaq (%rax,%r12), %rdi
addq $0xa, %rdi
callq 0x110fb
movl %eax, %eax
addq %r15, %r12
addq $0xe, %r12
movq 0x10(%rsp), %r15
cmpq %r15, %rax
cmovbq %rax, %r15
jmp 0x10ee0
leaq 0x9a92(%rip), %rdi      # 0x1a83c
leaq 0x9729(%rip), %rsi      # 0x1a4da
movl $0x165, %edx            # imm = 0x165
jmp 0x10dcb
leaq 0x9ab9(%rip), %rdi      # 0x1a878
leaq 0x9714(%rip), %rsi      # 0x1a4da
movl $0x168, %edx            # imm = 0x168
xorl %eax, %eax
callq 0xc960
jmp 0x10df1
leaq 0x9ad4(%rip), %rdi      # 0x1a8af
leaq 0x96f8(%rip), %rsi      # 0x1a4da
movl $0x16b, %edx            # imm = 0x16B
movl %r13d, %ecx
xorl %eax, %eax
callq 0xc960
movslq %ebp, %rax
movq 0x18(%rsp), %rcx
leaq (%rcx,%rax), %r12
addq $-0x4, %r12
cmpl $0x64, %r13d
jne 0x10e9e
cmpl $0x0, 0x8(%rbx)
je 0x10e2b
leaq 0x9ae1(%rip), %rdi      # 0x1a8f9
leaq 0x96bb(%rip), %rsi      # 0x1a4da
movl $0x176, %edx            # imm = 0x176
xorl %eax, %eax
callq 0xc960
movq 0x10(%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 0x10a48
movl %eax, %ebp
movl 0x8(%rbx), %eax
testl %ebp, %ebp
js 0x10e80
testl %eax, %eax
je 0x10e79
leaq 0x9adc(%rip), %rdi      # 0x1a940
leaq 0x966f(%rip), %rsi      # 0x1a4da
movl $0x17b, %edx            # imm = 0x17B
movl %ebp, %ecx
xorl %eax, %eax
callq 0xc960
movl %ebp, %eax
addq %rax, %r12
jmp 0x10e9e
testl %eax, %eax
je 0x10e9e
leaq 0x9afc(%rip), %rdi      # 0x1a987
leaq 0x9648(%rip), %rsi      # 0x1a4da
movl $0x17e, %edx            # imm = 0x17E
xorl %eax, %eax
callq 0xc960
leaq 0x9b42(%rip), %rdi      # 0x1a9e7
xorl %r14d, %r14d
movq %r15, %rsi
xorl %eax, %eax
callq 0xc66c
movq %rax, %r13
cmpl $0x0, 0x8(%rbx)
je 0x10ede
leaq 0x9b2f(%rip), %rdi      # 0x1a9f1
leaq 0x9611(%rip), %rsi      # 0x1a4da
xorl %r14d, %r14d
movl $0x185, %edx            # imm = 0x185
movq %r13, %rcx
movq %r15, %r8
xorl %eax, %eax
callq 0xc960
xorl %ebp, %ebp
cmpl $0x0, 0x8(%rbx)
je 0x10f0d
movq %r15, (%rsp)
leaq 0x9b9e(%rip), %rdi      # 0x1aa8f
leaq 0x95e2(%rip), %rsi      # 0x1a4da
movl $0x1a5, %edx            # imm = 0x1A5
movq %r13, %rcx
movq %r14, %r8
movq %rbp, %r9
xorl %eax, %eax
callq 0xc960
movq %r13, %rdi
callq 0x109f5
movq %r14, %rdi
callq 0x109f5
movq %rbp, %rdi
callq 0x109f5
cmpl $0x0, 0x8(%rbx)
je 0x10f45
leaq 0x9ba5(%rip), %rdi      # 0x1aad7
leaq 0x95a1(%rip), %rsi      # 0x1a4da
movl $0x1ae, %edx            # imm = 0x1AE
xorl %eax, %eax
callq 0xc960
movq %rbx, %rdi
movq %r13, %rsi
movq 0x20(%rsp), %rdx
movq %r12, %rcx
movq %r15, %r8
callq 0x108a3
testl %eax, %eax
je 0x10fd7
cmpl $0x0, 0x8(%rbx)
je 0x10f7f
leaq 0x9bfe(%rip), %rdi      # 0x1ab6a
leaq 0x9567(%rip), %rsi      # 0x1a4da
movl $0x1bb, %edx            # imm = 0x1BB
xorl %eax, %eax
callq 0xc960
testq %r14, %r14
je 0x10f8c
movq %r14, %rdi
callq 0x2030
testq %r13, %r13
je 0x10f99
movq %r13, %rdi
callq 0x2030
testq %rbp, %rbp
je 0x10fa6
movq %rbp, %rdi
callq 0x2030
cmpl $0x0, 0x8(%rbx)
je 0x10fc6
leaq 0x9bec(%rip), %rdi      # 0x1ab9f
leaq 0x9520(%rip), %rsi      # 0x1a4da
movl $0x1c1, %edx            # imm = 0x1C1
xorl %eax, %eax
callq 0xc960
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x0, 0x8(%rbx)
je 0x10ff7
leaq 0x9b3b(%rip), %rdi      # 0x1ab1f
leaq 0x94ef(%rip), %rsi      # 0x1a4da
movl $0x1b3, %edx            # imm = 0x1B3
xorl %eax, %eax
callq 0xc960
cmpl $0x0, 0xc(%rbx)
jle 0x10f5f
movq (%rbx), %rax
testq %rax, %rax
je 0x10f5f
movq %r13, %rdi
callq *%rax
jmp 0x10f5f