// File: /workspace/github_c_makefile_star2_O1/inflex[P]ripMIME/ripOLE/pldstr.c
// Function: PLD_strreplace_general
char *PLD_strreplace_general( struct PLD_strreplace *replace_details )
{
	char *new_buffer=NULL;
	char *source_end;
	char *segment_start, *segment_end, *segment_p;
	char *new_p;
	char *preexist_location=NULL;
	char *postexist_location=NULL;
	int replace_count=0;
	int size_required;
	int size_difference;
	int source_length;
	int searchfor_length;
	int replacewith_length;
	int segment_ok;

	if (replace_details->source == NULL) return NULL;

	source_length = strlen( replace_details->source );
	source_end = replace_details->source +source_length;
	searchfor_length = strlen(replace_details->searchfor);
	replacewith_length = strlen(replace_details->replacewith);
	size_difference = replacewith_length -searchfor_length;
	size_required = source_length;
	replace_count = replace_details->replacenumber;

	if ((replace_details->preexist != NULL)&&(strlen(replace_details->preexist) < 1)) replace_details->preexist = NULL;
	if ((replace_details->postexist != NULL)&&(strlen(replace_details->postexist) < 1)) replace_details->postexist = NULL;

	// If we have a 'pre-exist' request, then we need to check this out first
	//		because if the pre-exist string cannot be found, then there's very
	//		little point us continuing on in our search ( because without the
	//		preexist string existing, we are thus not qualified to replace anything )
	if (replace_details->preexist != NULL)
	{
		preexist_location = PLD_strstr(replace_details->source, replace_details->preexist, replace_details->insensitive);
		if (preexist_location == NULL)
		{
			return replace_details->source;
		}
	} 

	// Determine if initial POSTexist tests will pass, if we don't pick up
	//		anything here, then there's no point in continuing either
	if (replace_details->postexist != NULL)
	{
		char *p = replace_details->source;
		postexist_location = NULL;
		do {
			p = PLD_strstr(p, replace_details->postexist, replace_details->insensitive);
			if (p != NULL)
			{
				postexist_location = p;
				p = p +strlen(replace_details->postexist);
			} 
		} while (p != NULL);

		if (postexist_location == NULL)
		{
			return replace_details->source;
		}
	} 


	// Step 1 - determine the MAXIMUM number of times we might have to replace this string ( or the limit
	//		set by replacenumber
	//
	//	Note - we only need this number if the string we're going to be inserting into the 
	//	source is larger than the one we're replacing - this is so that we can ensure that
	//	we have sufficient memory available in the buffer.
	if (size_difference > 0)
	{
		if (replace_count == 0)
		{
			char *p, *q;

			p = replace_details->source;
			q = PLD_strstr(p, replace_details->searchfor, replace_details->insensitive);
			while (q != NULL)
			{
				replace_count++;
				//size_required += size_difference;
				p = q +searchfor_length;
				q = PLD_strstr(p, replace_details->searchfor, replace_details->insensitive);
			}

		}
		size_required = source_length +(size_difference *replace_count) +1;
	} else size_required = source_length +1;

	
	// Allocate the memory required to hold the new string [at least], check to see that
	//		all went well, if not, then return an error
	new_buffer = malloc( sizeof(char) *size_required);
	if (new_buffer == NULL)
	{
		LOGGER_log("%s:%d:PLD_strreplace:ERROR: Cannot allocate %d bytes of memory to perform replacement operation", FL, size_required);
		return replace_details->source;
	} 

	// Our segment must always start at the beginning of the source, 
	//		on the other hand, the segment_end can be anything from the
	//		next byte to NULL ( which is specially treated to mean to 
	//		the end of the source )
	segment_start = replace_details->source;


	// Locate the first segment 
	segment_ok = 0;
	segment_end = PLD_strstr(replace_details->source, replace_details->searchfor, replace_details->insensitive);

	// Determine if the first segment is valid in the presence of the 
	//	pre-exist and post-exist requirements
	while ((segment_end != NULL)&&(segment_ok == 0)\
			&&((replace_details->preexist != NULL)||(replace_details->postexist != NULL)))
	{
		int pre_ok = 0;
		int post_ok = 0;

		// The PREexist test assumes a couple of factors - please ensure these are
		//		relevant if you change any code prior to this point.
		//	
		//	1. preexist_location has already been computed and is not NULL
		//
		//	2. By relative position, the first preexist_location will be a valid location
		//			on which to validate for ALL replacements beyond that point, thus, we
		//			never actually have to recompute preexist_location again.
		//
		// 3. Conversely, the last computed postexist_location is valid for all 
		//			matches before it
		//
		if (preexist_location == NULL) pre_ok = 1;
		else if (preexist_location < segment_end){ pre_ok = 1;}

		if (postexist_location == NULL) post_ok = 1;
		else if (postexist_location > segment_end){ post_ok = 1;}

		if ((pre_ok == 0)||(post_ok == 0)) { segment_end = PLD_strstr(segment_end +searchfor_length, replace_details->searchfor, replace_details->insensitive); }
		else segment_ok = 1;
	} 

	segment_p = segment_start;
	new_p = new_buffer;
	while (segment_start != NULL)
	{	
		int replacewith_count;
		char *replacewith_p;

		if (segment_end == NULL) segment_end = source_end;

		replace_count--;

		// Perform the segment copy
		segment_p = segment_start;
		while ((segment_p < segment_end)&&(size_required > 0))
		{
			*new_p = *segment_p;
			new_p++;
			segment_p++;
			size_required--;
		}

		// Perform the string replacement
		if (segment_end < source_end)
		{
			replacewith_count = replacewith_length;
			replacewith_p = replace_details->replacewith;
			while ((replacewith_count--)&&(size_required > 0))
			{
				*new_p = *replacewith_p;
				new_p++;
				replacewith_p++;
				size_required--;
			}
		}

		if (size_required < 1 )
		{
			LOGGER_log("%s:%d:PLD_strreplace_general: Allocated memory ran out while replacing '%s' with '%s'",FL, replace_details->searchfor, replace_details->replacewith);
			*new_p='\0';
			break;
		}

		// Find the next segment
		segment_start = segment_end +searchfor_length;

		// If we've reached the end of the number of replacements we're supposed
		//		to do, then we prepare the termination of the while loop by setting
		//		our segment end to the end of the source.
		//
		// NOTE: Remember that the replace_count is pre-decremented at the start
		//		of the while loop, so, if the caller requested '0' replacements
		//		this will now be -1, thus, it won't get terminated from this == 0
		//		match.  Just thought you'd like to be reminded of that incase you
		//		were wondering "Huh? this would terminate an unlimited replacement"
		if (replace_count == 0)
		{
			segment_end = NULL;
		} else {
			// If our new segment to copy starts after the
			//		end of the source, then we actually have 
			//		nothing else to copy, thus, we prepare the
			//		segment_start varible to cause the while loop 
			//		to terminate.
			//
			// Otherwise, we try and locate the next segment
			//		ending point, and set the starting point to
			//		be on the 'other side' of the 'searchfor' string
			//		which we found in the last search.
			//
			if (segment_start > source_end) 
			{
				segment_start = NULL;
			} else {

				// Try find the next segment
				segment_ok = 0;
				segment_end = PLD_strstr(segment_end +searchfor_length, replace_details->searchfor, replace_details->insensitive);

				// If we have a pre/post-exist requirement, then enter into this
				//		series of tests.  NOTE - at least one of the pre or post tests
				//		must fire to give an meaningful result - else we'll end up with 
				//		a loop which simply goes to the end of the searchspace buffer
				while ((segment_end != NULL)&&(segment_ok == 0)\
						&&((replace_details->preexist != NULL)||(replace_details->postexist != NULL)))
				{
					int pre_ok = 0;
					int post_ok = 0;

					// The PREexist test assumes a couple of factors - please ensure these are
					//		relevant if you change any code prior to this point.
					//	
					//	1. preexist_location has already been computed and is not NULL
					//
					//	2. By relative position, the first preexist_location will be a valid location
					//			on which to validate for ALL replacements beyond that point, thus, we
					//			never actually have to recompute preexist_location again.
					//
					// 3. Conversely, the last computed postexist_location is valid for all 
					//			matches before it
					//
					if (preexist_location == NULL) pre_ok = 1;
					else if (preexist_location < segment_end){ pre_ok = 1;}

					if (postexist_location == NULL) post_ok = 1;
					else if (postexist_location > segment_end){ post_ok = 1;}

					if ((pre_ok == 0)||(post_ok == 0)) { segment_end = PLD_strstr(segment_end +searchfor_length, replace_details->searchfor, replace_details->insensitive); }
					else segment_ok = 1;
				} 

			} // If-else segment_start > source_end

		}

	}

	*new_p = '\0';

//	if (replace_details->source != NULL) free (replace_details->source);
//	replace_details->source = new_buffer;
	return new_buffer;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq (%rdi), %r12
testq %r12, %r12
je 0x42ca
movq %rdi, %rbx
movq %r12, %rdi
callq 0x10b0
movq %rax, 0x8(%rsp)
movq 0x8(%rbx), %rdi
callq 0x10b0
movq %rax, %r15
movq 0x10(%rbx), %rdi
callq 0x10b0
movq %rax, 0x30(%rsp)
movq 0x18(%rbx), %rax
testq %rax, %rax
je 0x428e
cmpb $0x0, (%rax)
jne 0x428e
movq $0x0, 0x18(%rbx)
movq 0x20(%rbx), %rax
testq %rax, %rax
je 0x42a4
cmpb $0x0, (%rax)
jne 0x42a4
movq $0x0, 0x20(%rbx)
movl 0x28(%rbx), %ebp
movq 0x18(%rbx), %rsi
testq %rsi, %rsi
je 0x42d1
movl 0x2c(%rbx), %edx
movq %r12, %rdi
callq 0x3eec
movq %rax, 0x38(%rsp)
testq %rax, %rax
jne 0x42da
jmp 0x4466
xorl %eax, %eax
jmp 0x4631
movq $0x0, 0x38(%rsp)
cmpq $0x0, 0x20(%rbx)
je 0x4328
movq (%rbx), %rdi
xorl %r14d, %r14d
movq 0x20(%rbx), %rsi
movl 0x2c(%rbx), %edx
callq 0x3eec
testq %rax, %rax
je 0x430f
movq %rax, %r13
movq 0x20(%rbx), %rdi
callq 0x10b0
movq %rax, %rdi
addq %r13, %rdi
movq %r13, %r14
jmp 0x4311
xorl %edi, %edi
testq %rdi, %rdi
jne 0x42e7
testq %r14, %r14
jne 0x431e
movq (%rbx), %rax
testq %r14, %r14
jne 0x432b
jmp 0x4631
xorl %r14d, %r14d
movq 0x30(%rsp), %r13
subl %r15d, %r13d
testl %r13d, %r13d
jle 0x4375
movq %r14, 0x18(%rsp)
testl %ebp, %ebp
jne 0x437e
movq (%rbx), %rdi
movq 0x8(%rbx), %rsi
movl 0x2c(%rbx), %edx
callq 0x3eec
testq %rax, %rax
je 0x437c
movslq %r15d, %r14
xorl %ebp, %ebp
addq %r14, %rax
incl %ebp
movq 0x8(%rbx), %rsi
movl 0x2c(%rbx), %edx
movq %rax, %rdi
callq 0x3eec
testq %rax, %rax
jne 0x435a
jmp 0x437e
movq 0x8(%rsp), %r13
jmp 0x438c
xorl %ebp, %ebp
imull %ebp, %r13d
addl 0x8(%rsp), %r13d
movq 0x18(%rsp), %r14
incl %r13d
movslq %r13d, %rdi
callq 0x1110
testq %rax, %rax
je 0x4449
movq %r15, 0x10(%rsp)
movq %rax, 0x40(%rsp)
movl %ebp, 0x2c(%rsp)
movq %r12, 0x18(%rsp)
movslq 0x8(%rsp), %rax
movq %rax, 0x20(%rsp)
movq (%rbx), %rdi
movq 0x8(%rbx), %rsi
movl 0x2c(%rbx), %edx
movq %rdi, %rbp
callq 0x3eec
movq %rax, %r15
testq %rax, %rax
je 0x446e
movslq 0x10(%rsp), %rax
movq %rax, 0x8(%rsp)
movq %rbp, %rdi
cmpq $0x0, 0x18(%rbx)
jne 0x43f6
cmpq $0x0, 0x20(%rbx)
je 0x4471
testq %r14, %r14
setne %al
movq 0x38(%rsp), %rdx
testq %rdx, %rdx
setne %cl
cmpq %r15, %rdx
setae %dl
andb %cl, %dl
cmpq %r15, %r14
setbe %r12b
andb %al, %r12b
orb %dl, %r12b
cmpb $0x1, %r12b
jne 0x443c
addq 0x8(%rsp), %r15
movq 0x8(%rbx), %rsi
movl 0x2c(%rbx), %edx
movq %r15, %rdi
callq 0x3eec
movq %rbp, %rdi
movq %rax, %r15
testq %r15, %r15
setne %al
testb %r12b, %al
jne 0x43e8
jmp 0x4471
leaq 0x2a95(%rip), %rdi      # 0x6ee5
leaq 0x2aee(%rip), %rsi      # 0x6f45
movl $0x1f2, %edx            # imm = 0x1F2
movl %r13d, %ecx
xorl %eax, %eax
callq 0x3d40
movq (%rbx), %rax
jmp 0x4631
movq %rbp, %rdi
movq 0x20(%rsp), %rax
addq %rax, 0x18(%rsp)
movslq 0x10(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x30(%rsp), %rax
leal -0x1(%rax), %r8d
movq 0x40(%rsp), %r12
movq %r8, 0x10(%rsp)
testq %rdi, %rdi
je 0x4627
testq %r15, %r15
cmoveq 0x18(%rsp), %r15
cmpq %r15, %rdi
jae 0x44d9
testl %r13d, %r13d
jle 0x44d9
leaq 0x1(%rdi), %rcx
movb -0x1(%rcx), %al
movb %al, (%r12)
incq %r12
leal -0x1(%r13), %eax
cmpq %r15, %rcx
jae 0x44dc
incq %rcx
cmpl $0x1, %r13d
movl %eax, %r13d
jg 0x44b8
jmp 0x44dc
movl %r13d, %eax
cmpq 0x18(%rsp), %r15
jae 0x4517
cmpl $0x0, 0x30(%rsp)
je 0x4517
testl %eax, %eax
jle 0x4517
movq 0x10(%rbx), %rdx
xorl %ecx, %ecx
movb (%rdx,%rcx), %sil
movb %sil, (%r12,%rcx)
leal -0x1(%rax), %r13d
movl %ecx, %esi
incq %rcx
cmpl %esi, %r8d
je 0x4512
cmpl $0x1, %eax
movl %r13d, %eax
jg 0x44f4
addq %rcx, %r12
jmp 0x451a
movl %eax, %r13d
movl 0x2c(%rsp), %eax
decl %eax
testl %r13d, %r13d
movl %eax, 0x2c(%rsp)
jle 0x4544
movq 0x8(%rsp), %rcx
leaq (%r15,%rcx), %rdi
testl %eax, %eax
je 0x457b
cmpq 0x18(%rsp), %rdi
jbe 0x4583
xorl %edi, %edi
jmp 0x461e
movq 0x8(%rbx), %rcx
movq 0x10(%rbx), %r8
movq %rdi, %rbp
leaq 0x29f8(%rip), %rdi      # 0x6f4e
leaq 0x29e8(%rip), %rsi      # 0x6f45
movl $0x244, %edx            # imm = 0x244
xorl %eax, %eax
callq 0x3d40
movq 0x10(%rsp), %r8
movq %rbp, %rdi
movb $0x0, (%r12)
jmp 0x461e
xorl %r15d, %r15d
jmp 0x461e
movq 0x8(%rbx), %rsi
movl 0x2c(%rbx), %edx
movq %rdi, 0x20(%rsp)
callq 0x3eec
movq %rax, %r15
testq %rax, %rax
je 0x4614
movq 0x20(%rsp), %rdi
movq 0x10(%rsp), %r8
cmpq $0x0, 0x18(%rbx)
jne 0x45b4
cmpq $0x0, 0x20(%rbx)
je 0x461e
testq %r14, %r14
setne %al
movq 0x38(%rsp), %rdx
testq %rdx, %rdx
setne %cl
cmpq %r15, %rdx
setae %dl
andb %cl, %dl
movq %r14, %rbp
cmpq %r15, %r14
setbe %r14b
andb %al, %r14b
orb %dl, %r14b
cmpb $0x1, %r14b
jne 0x4604
addq 0x8(%rsp), %r15
movq 0x8(%rbx), %rsi
movl 0x2c(%rbx), %edx
movq %r15, %rdi
callq 0x3eec
movq 0x10(%rsp), %r8
movq 0x20(%rsp), %rdi
movq %rax, %r15
testq %r15, %r15
setne %al
testb %r14b, %al
movq %rbp, %r14
jne 0x45a6
jmp 0x461e
movq 0x20(%rsp), %rdi
movq 0x10(%rsp), %r8
testl %r13d, %r13d
jg 0x4498
movb $0x0, (%r12)
movq 0x40(%rsp), %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq