// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x70(%rbp)
movl $0x0, -0x74(%rbp)
movq -0x68(%rbp), %rdi
callq 0x181a0
cltq
movq %rax, -0x30(%rbp)
movq -0x68(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x68(%rbp)
movq -0x8(%rbp), %rax
cmpl $0x0, 0x8(%rax)
je 0x17a05
movq -0x30(%rbp), %rcx
movq -0x30(%rbp), %r8
movq -0x18(%rbp), %r9
movq -0x18(%rbp), %rax
leaq 0x9a73(%rip), %rdi      # 0x21461
leaq 0x98e7(%rip), %rsi      # 0x212dc
movl $0x142, %edx            # imm = 0x142
movq %rax, (%rsp)
movb $0x0, %al
callq 0x117c0
movq -0x18(%rbp), %rax
subq -0x30(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rax
addq -0x28(%rbp), %rax
movq %rax, -0x70(%rbp)
cmpq $0x4, -0x28(%rbp)
jae 0x17d6a
movq -0x68(%rbp), %rdi
callq 0x18170
movl %eax, -0x78(%rbp)
movq -0x8(%rbp), %rax
cmpl $0x0, 0x8(%rax)
je 0x17a5f
movl -0x78(%rbp), %ecx
movl -0x78(%rbp), %r8d
leaq 0x9a75(%rip), %rdi      # 0x214c1
leaq 0x9889(%rip), %rsi      # 0x212dc
movl $0x153, %edx            # imm = 0x153
movb $0x0, %al
callq 0x117c0
movq -0x68(%rbp), %rdi
addq $0x2, %rdi
callq 0x18170
movl %eax, -0x7c(%rbp)
movq -0x8(%rbp), %rax
cmpl $0x0, 0x8(%rax)
je 0x17a9a
movl -0x7c(%rbp), %ecx
movl -0x7c(%rbp), %r8d
leaq 0x9a7a(%rip), %rdi      # 0x21501
leaq 0x984e(%rip), %rsi      # 0x212dc
movl $0x155, %edx            # imm = 0x155
movb $0x0, %al
callq 0x117c0
movq -0x68(%rbp), %rdi
addq $0x4, %rdi
callq 0x18170
movl %eax, -0x80(%rbp)
movq -0x8(%rbp), %rax
cmpl $0x0, 0x8(%rax)
je 0x17ad5
movl -0x80(%rbp), %ecx
movl -0x80(%rbp), %r8d
leaq 0x9a7d(%rip), %rdi      # 0x2153f
leaq 0x9813(%rip), %rsi      # 0x212dc
movl $0x157, %edx            # imm = 0x157
movb $0x0, %al
callq 0x117c0
movq -0x68(%rbp), %rdi
addq $0x8, %rdi
callq 0x18170
movl %eax, -0x84(%rbp)
movq -0x8(%rbp), %rax
cmpl $0x0, 0x8(%rax)
je 0x17b19
movl -0x84(%rbp), %ecx
movl -0x84(%rbp), %r8d
leaq 0x9a79(%rip), %rdi      # 0x2157f
leaq 0x97cf(%rip), %rsi      # 0x212dc
movl $0x159, %edx            # imm = 0x159
movb $0x0, %al
callq 0x117c0
movq -0x68(%rbp), %rdi
addq $0xa, %rdi
callq 0x18170
movl %eax, -0x88(%rbp)
movq -0x8(%rbp), %rax
cmpl $0x0, 0x8(%rax)
je 0x17b5d
movl -0x88(%rbp), %ecx
movl -0x88(%rbp), %r8d
leaq 0x9a75(%rip), %rdi      # 0x215bf
leaq 0x978b(%rip), %rsi      # 0x212dc
movl $0x15b, %edx            # imm = 0x15B
movb $0x0, %al
callq 0x117c0
movq -0x8(%rbp), %rax
cmpl $0x0, 0x8(%rax)
je 0x17c08
movl -0x7c(%rbp), %eax
movl %eax, -0x94(%rbp)
subl $0x62, %eax
je 0x17bcd
jmp 0x17b7b
movl -0x94(%rbp), %eax
subl $0x63, %eax
je 0x17bb1
jmp 0x17b88
movl -0x94(%rbp), %eax
subl $0x64, %eax
jne 0x17be9
jmp 0x17b95
leaq 0x9a62(%rip), %rdi      # 0x215fe
leaq 0x9739(%rip), %rsi      # 0x212dc
movl $0x162, %edx            # imm = 0x162
movb $0x0, %al
callq 0x117c0
jmp 0x17c06
leaq 0x9a86(%rip), %rdi      # 0x2163e
leaq 0x971d(%rip), %rsi      # 0x212dc
movl $0x165, %edx            # imm = 0x165
movb $0x0, %al
callq 0x117c0
jmp 0x17c06
leaq 0x9aa6(%rip), %rdi      # 0x2167a
leaq 0x9701(%rip), %rsi      # 0x212dc
movl $0x168, %edx            # imm = 0x168
movb $0x0, %al
callq 0x117c0
jmp 0x17c06
movl -0x7c(%rbp), %ecx
leaq 0x9abe(%rip), %rdi      # 0x216b1
leaq 0x96e2(%rip), %rsi      # 0x212dc
movl $0x16b, %edx            # imm = 0x16B
movb $0x0, %al
callq 0x117c0
jmp 0x17c08
movq -0x68(%rbp), %rax
movslq -0x78(%rbp), %rcx
addq %rcx, %rax
addq $-0x4, %rax
movq %rax, -0x70(%rbp)
cmpl $0x64, -0x7c(%rbp)
jne 0x17d0b
movl $0x0, -0x8c(%rbp)
movl $0x1f4, -0x90(%rbp)     # imm = 0x1F4
movq -0x8(%rbp), %rax
cmpl $0x0, 0x8(%rax)
je 0x17c5d
leaq 0x9ab1(%rip), %rdi      # 0x216fb
leaq 0x968b(%rip), %rsi      # 0x212dc
movl $0x176, %edx            # imm = 0x176
movb $0x0, %al
callq 0x117c0
movq -0x18(%rbp), %rax
movl -0x90(%rbp), %ecx
addl $0x44, %ecx
movslq %ecx, %rcx
cmpq %rcx, %rax
jae 0x17c80
movq -0x18(%rbp), %rax
subq $0x45, %rax
movl %eax, -0x90(%rbp)
movq -0x8(%rbp), %rdi
movq -0x70(%rbp), %rsi
movslq -0x90(%rbp), %rdx
callq 0x17820
movl %eax, -0x8c(%rbp)
cmpl $0x0, -0x8c(%rbp)
jl 0x17ce3
movq -0x8(%rbp), %rax
cmpl $0x0, 0x8(%rax)
je 0x17ccd
movl -0x8c(%rbp), %ecx
leaq 0x9a88(%rip), %rdi      # 0x21742
leaq 0x961b(%rip), %rsi      # 0x212dc
movl $0x17b, %edx            # imm = 0x17B
movb $0x0, %al
callq 0x117c0
movl -0x8c(%rbp), %ecx
movq -0x70(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x70(%rbp)
jmp 0x17d09
movq -0x8(%rbp), %rax
cmpl $0x0, 0x8(%rax)
je 0x17d07
leaq 0x9a95(%rip), %rdi      # 0x21789
leaq 0x95e1(%rip), %rsi      # 0x212dc
movl $0x17e, %edx            # imm = 0x17E
movb $0x0, %al
callq 0x117c0
jmp 0x17d09
jmp 0x17d0b
movq -0x30(%rbp), %rsi
leaq 0x9ad3(%rip), %rdi      # 0x217e9
movb $0x0, %al
callq 0x11330
movq %rax, -0x58(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
movq $0x0, -0x48(%rbp)
movq -0x8(%rbp), %rax
cmpl $0x0, 0x8(%rax)
je 0x17d65
movq -0x58(%rbp), %rcx
movq -0x38(%rbp), %r8
leaq 0x9aa1(%rip), %rdi      # 0x217f3
leaq 0x9583(%rip), %rsi      # 0x212dc
movl $0x185, %edx            # imm = 0x185
movb $0x0, %al
callq 0x117c0
jmp 0x17e6b
movq -0x8(%rbp), %rax
cmpl $0x0, 0x8(%rax)
je 0x17d8e
leaq 0x9acc(%rip), %rdi      # 0x21847
leaq 0x955a(%rip), %rsi      # 0x212dc
movl $0x188, %edx            # imm = 0x188
movb $0x0, %al
callq 0x117c0
movq -0x68(%rbp), %rax
movw (%rax), %ax
movw %ax, -0x60(%rbp)
movq -0x68(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rdi
callq 0x2390
movq %rax, -0x58(%rbp)
movq -0x68(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0x58(%rbp), %rdi
callq 0x2100
movq %rax, %rcx
movq -0xa0(%rbp), %rax
addq %rcx, %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rdi
callq 0x2390
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rdi
callq 0x2100
addq $0x1, %rax
addq -0x68(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x50(%rbp)
movq -0x68(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rdi
callq 0x2390
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rdi
callq 0x2100
addq $0x1, %rax
addq -0x68(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rdi
callq 0x181f0
movl %eax, %eax
movq %rax, -0x38(%rbp)
movq -0x68(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x68(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x18(%rbp), %rax
jbe 0x17e63
movq -0x18(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x68(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x8(%rbp), %rax
cmpl $0x0, 0x8(%rax)
je 0x17ea3
movq -0x58(%rbp), %rcx
movq -0x48(%rbp), %r8
movq -0x40(%rbp), %r9
movq -0x38(%rbp), %rax
leaq 0x9a05(%rip), %rdi      # 0x21891
leaq 0x9449(%rip), %rsi      # 0x212dc
movl $0x1a5, %edx            # imm = 0x1A5
movq %rax, (%rsp)
movb $0x0, %al
callq 0x117c0
movq -0x58(%rbp), %rdi
callq 0x17780
movq -0x48(%rbp), %rdi
callq 0x17780
movq -0x40(%rbp), %rdi
callq 0x17780
movq -0x8(%rbp), %rax
cmpl $0x0, 0x8(%rax)
je 0x17ee2
leaq 0x9a0a(%rip), %rdi      # 0x218d9
leaq 0x9406(%rip), %rsi      # 0x212dc
movl $0x1ae, %edx            # imm = 0x1AE
movb $0x0, %al
callq 0x117c0
movq -0x8(%rbp), %rdi
movq -0x58(%rbp), %rsi
movq -0x20(%rbp), %rdx
movq -0x70(%rbp), %rcx
movq -0x38(%rbp), %r8
callq 0x175e0
movl %eax, -0x74(%rbp)
cmpl $0x0, -0x74(%rbp)
jne 0x17f4b
movq -0x8(%rbp), %rax
cmpl $0x0, 0x8(%rax)
je 0x17f28
leaq 0x9a0c(%rip), %rdi      # 0x21921
leaq 0x93c0(%rip), %rsi      # 0x212dc
movl $0x1b3, %edx            # imm = 0x1B3
movb $0x0, %al
callq 0x117c0
movq -0x8(%rbp), %rax
cmpl $0x0, 0xc(%rax)
jle 0x17f49
movq -0x8(%rbp), %rax
cmpq $0x0, (%rax)
je 0x17f49
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq -0x58(%rbp), %rdi
callq *%rax
jmp 0x17f4b
movq -0x8(%rbp), %rax
cmpl $0x0, 0x8(%rax)
je 0x17f6f
leaq 0x9a10(%rip), %rdi      # 0x2196c
leaq 0x9379(%rip), %rsi      # 0x212dc
movl $0x1bb, %edx            # imm = 0x1BB
movb $0x0, %al
callq 0x117c0
cmpq $0x0, -0x48(%rbp)
je 0x17f7f
movq -0x48(%rbp), %rdi
callq 0x2030
cmpq $0x0, -0x58(%rbp)
je 0x17f8f
movq -0x58(%rbp), %rdi
callq 0x2030
cmpq $0x0, -0x40(%rbp)
je 0x17f9f
movq -0x40(%rbp), %rdi
callq 0x2030
movq -0x8(%rbp), %rax
cmpl $0x0, 0x8(%rax)
je 0x17fc3
leaq 0x99f1(%rip), %rdi      # 0x219a1
leaq 0x9325(%rip), %rsi      # 0x212dc
movl $0x1c1, %edx            # imm = 0x1C1
movb $0x0, %al
callq 0x117c0
xorl %eax, %eax
addq $0xb0, %rsp
popq %rbp
retq
nop