// File: /workspace/github_c_makefile_star2_O0/inflex[P]ripMIME/ffget.c
// Function: FFGET_fgets
char *FFGET_fgets( char *linein, int maxsize, FFGET_FILE *f )
{
	char *line = linein;
	char *crlfpos = NULL;
	int charstoCRLF = 0;
	int chardiff = 0;
	int result = 0;
	int max_size = maxsize;
	int endpoint_tainted = 0;
	int extra_char_kept=0;
	int c, nextchar;

	f->trueblank = 0;
	f->linebreak = FFGET_LINEBREAK_NONE;
	f->lastbreak[0] = '\0';

	if (f->FFEOF != 0)
	{
		return NULL;
	}

	if ((FFGET_SDL_WATCH > 0)||(FFGET_SDL_MODE != 0))
	{
		DELIMITERS = SDL_MODE_DELIMITS;
	}
	else DELIMITERS = NORM_MODE_DELIMITS;


	//	fprintf(stderr,"FFGET_called, SDLMODE = %d, Offset = %d, maxsize = %d, DATA left = %d, first char is '%02X'\n", FFGET_SDL_MODE, (f->startpoint -f->buffer), max_size, (f->endpoint -f->startpoint)+1, (int)(*f->startpoint));

	max_size = maxsize = maxsize -2;

	//	memset(line, 0, max_size+1);

	// If we dont have enough data in the buffer to fill up the fgets request
	// we'll have to do a two step fill

	//fprintf(stderr,"DATA Reminianing : %d\n", (int)(f->endpoint -f->startpoint)+1);

	if ((f->startpoint > f->endpoint))
	{
		result = FFGET_getnewblock(f);
		if (result == 0)
		{
			*linein = '\0';
			return NULL;
		}
	}



	// This loop does not go around too many times, once, maybe twice max.

	while ((max_size > 0)&&(f->FFEOF == 0))
	{

		crlfpos = strpbrk( f->startpoint, DELIMITERS);
		if (crlfpos)
		{
				extra_char_kept = 0;
				endpoint_tainted = 0;
				nextchar = -1;
			// if our next char is a CR or LF, then pick it up and
			// return it with the line.  NOTE - this is to deal with
			// CRLF pairings which are common on DOS files.  In fact,
			// this is a case of where UNIX is actually -wrong-.  It
			// should have also used CRLF pairing to mark line ends, but
			// someone obviously (and understandably, to save space)
			// thought they'd leave make LF imply a CR as well.
			//   Well done... another bugger up in life.


			// The logic of this nested IF statement is as follows...
			//	If we do have another char available...
			//		and if the pairing is not \n\n (which should be treated as two lines
			//			and if the next char is a \n or a \r,
			//				THEN we should increment the end of line pointer so that we
			//				include the additional \n or \r into the line we're going to
			//				return

			// If we are NOT in the Single-delimeter mode (SDL_MODE), and the next
			// char is available, then commence the delimeter testing routines

			if ((0==f->FILEEND)&&(0==f->FFEOF)&&( ((crlfpos +1) >  f->endpoint)))
			{

				// We have an EOL character, get 1 more from the stream to test the next character


				nextchar = c = fgetc(f->f);
				if (c==EOF)
				{
					//					fprintf(stderr,"EOF hit due to fgetc()\n");
					f->FILEEND = 1;
				}
				else
				{
					if (c == '\0') c = ' ';

					// Check for character value vadality

					if ((c > 0) && (c <= 255))
					{
						if (FFGET_DNORMAL) LOGGER_log("%s:%d:FFGET_fgets:DEBUG: Tainting endpoint +1 (%p -> %p,  hard buffer end = %p, file read bytes = %ld)", FL, f->endpoint, f->endpoint+1, f->buffer_end, f->bytes);
						f->endpoint++;
						*(f->endpoint) = c;
						*(f->endpoint+1) = '\0';
						endpoint_tainted = 1;
					}

				}
			} // If (crlfpos +1) is /not/ within our buffer bounds


			// If the next char from our CRLF pickup is within the bounds of
			// our endpoint, then proceed to test the CRLF combo

			if ( ((crlfpos +1) <= f->endpoint))
			{

				//				fprintf(stderr,"Found '%02X' [next is '%02X']\n",*crlfpos, *(crlfpos+1));

				if ( *crlfpos == '\n' )
				{
					f->linebreak = FFGET_LINEBREAK_LF;
					snprintf(f->lastbreak,sizeof(f->lastbreak),"\n");

					if ( *(crlfpos +1) == '\r' )
					{
						f->linebreak |= FFGET_LINEBREAK_CR;
						snprintf(f->lastbreak,sizeof(f->lastbreak),"\n\r");
						crlfpos++;
						extra_char_kept = 1;
					}
				}


				// If our combo starts with a \r, then test it to see
				// if we have another \r after it, in which case, we
				// turn on SINGLE_DELIMETER_MODE.

				if ( (*crlfpos == '\r') )
				{
					f->linebreak = FFGET_LINEBREAK_CR;
					snprintf(f->lastbreak,sizeof(f->lastbreak),"\r");

					if ( *(crlfpos +1) == '\r' )
					{
						// A \r\r sequence has just been detected, set our doubleCR flag
						// 	so that MIME_headers can read it and react accordingly.
						// Look out for single \r's from here on, as they are now seen as
						// 	EOL markers in Outlook.

						f->linebreak = FFGET_LINEBREAK_CR;
						FFGET_doubleCR=1;
						FFGET_SDL_MODE=1;
						crlfpos++;
						extra_char_kept = 1;

					} else if ( *(crlfpos +1) == '\n' ) {
						// If we see a \n after our \r, then treat this as a single
						// 	line delimeter if we are NOT in Single Delimeter mode
						//

						snprintf(f->lastbreak,sizeof(f->lastbreak),"\r\n");
						f->linebreak |= FFGET_LINEBREAK_LF;
						if (!FFGET_SDL_MODE) {
						  	crlfpos++;
							extra_char_kept = 1;
					  	}// 20040208-1706:PLD
						//crlfpos++;// 20040208-1706:PLD	 // 20040306-0003:PLD - this line causes a CRCR test to fail; mailpack.virus.badtrans
					} else {
						// If we saw a \r, but then there was no other EOL type char (\r or \n)
						//	then switch to SDL mode (Single delimeter).

						FFGET_SDL_MODE=1;

					}

				} // If combo starts with a \r

			} // If crlfpos +1  is within the bounds of our buffer.

			// Determine how many characters/bytes there are from the startpoint,
			// to the CRLF position.


			charstoCRLF	= crlfpos -f->startpoint;

			// If the number of chars is -less- than that of the maximum line read
			// size which our calling program has specified, then we set the max_size
			// to be the number of chars.

			//DEBUG			fprintf(stderr, "MAX_size = %d, charstoCRLF = %d\n", max_size, charstoCRLF);

			if ((charstoCRLF >= 0)&&(charstoCRLF < max_size)) max_size = charstoCRLF;

			if ((extra_char_kept == 0) && (nextchar != -1)) ungetc(nextchar,f->f);

		} // If CRLF pos found.


		//		else crlfpos = (f->endpoint +1);




		// If the buffer amount remaining in our FFGET buffer is greater than
		// the maximum size available in our line buffer, then we
		//  only copy the max_size amount across

		if (( f->endpoint -f->startpoint) >= max_size)
		{
			if (max_size < 0) LOGGER_log("%s:%d:FFGET_fgets:ERROR: Max size < 0\n", FL);
			memcpy(line, f->startpoint, max_size +1);//+1
			f->startpoint += (max_size +1); //+1
			*(line +max_size +1) = '\0'; //+1
			max_size = 0;

		} else {

			// else, if the amount of data available is /LESS/ than what we can
			// accept in the line buffer then copy what remains out to the line
			// buffer and then tack on the results of a new read.

			chardiff = f->endpoint -f->startpoint;

			//			fprintf(stderr,"CHARDiff = %d, FFEOF = %d, FILEEND = %d\n",chardiff, f->FFEOF, f->FILEEND);

			if (chardiff >= 0)
			{
				memcpy(line, f->startpoint, chardiff +1);
				*(line +chardiff +1) = '\0';  // 12-11-2002: Added this line to terminate the input buffer incase it wasn't already flushed with \0's
				line += (chardiff +1);
				max_size -= (chardiff +1);
				f->startpoint = f->endpoint +1;
				if (max_size < 0) max_size = 0;
			}

			FFGET_getnewblock(f);
			endpoint_tainted=0;

		} // If there wasn't enough data to satisfy ends.

		if (endpoint_tainted) {
			FFGET_getnewblock(f);
			endpoint_tainted = 0;
		}

	} // While we've got space to fill, and we've got data to read

	line = linein;

	f->trueblank = 0;

	if ((f->lastchar == '\n')||(f->lastchar == '\r'))
	{
		if ((line[0] == '\n')||(line[0] == '\r'))
		{
			f->trueblank = 1;
		}
	}

	f->lastchar = line[strlen(line) -1];

	f->linecount++;

	//	LOGGER_log("%s:%d:LINE='%s'",FL,linein);

	return linein;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
movl $0x0, -0x34(%rbp)
movl $0x0, -0x38(%rbp)
movl $0x0, -0x3c(%rbp)
movl -0x14(%rbp), %eax
movl %eax, -0x40(%rbp)
movl $0x0, -0x44(%rbp)
movl $0x0, -0x48(%rbp)
movq -0x20(%rbp), %rax
movl $0x0, 0x2054(%rax)
movq -0x20(%rbp), %rax
movl $0x0, 0x205c(%rax)
movq -0x20(%rbp), %rax
movb $0x0, 0x2060(%rax)
movq -0x20(%rbp), %rax
cmpl $0x0, 0x2034(%rax)
je 0x9b3d
movq $0x0, -0x8(%rbp)
jmp 0xa116
cmpl $0x0, 0x21420(%rip)     # 0x2af64
jg 0x9b4f
cmpl $0x0, 0x21413(%rip)     # 0x2af60
je 0x9b5f
leaq 0x1d43a(%rip), %rax     # 0x26f90
movq %rax, 0x1d43b(%rip)     # 0x26f98
jmp 0x9b6d
leaq 0x1d42d(%rip), %rax     # 0x26f93
movq %rax, 0x1d42b(%rip)     # 0x26f98
movl -0x14(%rbp), %eax
subl $0x2, %eax
movl %eax, -0x14(%rbp)
movl %eax, -0x40(%rbp)
movq -0x20(%rbp), %rax
movq 0x2010(%rax), %rax
movq -0x20(%rbp), %rcx
cmpq 0x2018(%rcx), %rax
jbe 0x9bb9
movq -0x20(%rbp), %rdi
callq 0x94f0
movl %eax, -0x3c(%rbp)
cmpl $0x0, -0x3c(%rbp)
jne 0x9bb7
movq -0x10(%rbp), %rax
movb $0x0, (%rax)
movq $0x0, -0x8(%rbp)
jmp 0xa116
jmp 0x9bb9
jmp 0x9bbb
xorl %eax, %eax
cmpl $0x0, -0x40(%rbp)
movb %al, -0x51(%rbp)
jle 0x9bd7
movq -0x20(%rbp), %rax
cmpl $0x0, 0x2034(%rax)
sete %al
movb %al, -0x51(%rbp)
movb -0x51(%rbp), %al
testb $0x1, %al
jne 0x9be3
jmp 0xa071
movq -0x20(%rbp), %rax
movq 0x2010(%rax), %rdi
movq 0x1d3a3(%rip), %rsi     # 0x26f98
callq 0x22e0
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
je 0x9f19
movl $0x0, -0x48(%rbp)
movl $0x0, -0x44(%rbp)
movl $0xffffffff, -0x50(%rbp) # imm = 0xFFFFFFFF
movq -0x20(%rbp), %rcx
xorl %eax, %eax
cmpl 0x2030(%rcx), %eax
jne 0x9d43
movq -0x20(%rbp), %rcx
xorl %eax, %eax
cmpl 0x2034(%rcx), %eax
jne 0x9d43
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq -0x20(%rbp), %rcx
cmpq 0x2018(%rcx), %rax
jbe 0x9d43
movq -0x20(%rbp), %rax
movq (%rax), %rdi
callq 0x2160
movl %eax, -0x4c(%rbp)
movl %eax, -0x50(%rbp)
cmpl $-0x1, -0x4c(%rbp)
jne 0x9c86
movq -0x20(%rbp), %rax
movl $0x1, 0x2030(%rax)
jmp 0x9d41
cmpl $0x0, -0x4c(%rbp)
jne 0x9c93
movl $0x20, -0x4c(%rbp)
cmpl $0x0, -0x4c(%rbp)
jle 0x9d3f
cmpl $0xff, -0x4c(%rbp)
jg 0x9d3f
cmpl $0x1, 0x212bb(%rip)     # 0x2af6c
jl 0x9d01
movq -0x20(%rbp), %rax
movq 0x2018(%rax), %rcx
movq -0x20(%rbp), %rax
movq 0x2018(%rax), %r8
addq $0x1, %r8
movq -0x20(%rbp), %rax
movq 0x2020(%rax), %r9
movq -0x20(%rbp), %rax
movq 0x2040(%rax), %rax
leaq 0x124e3(%rip), %rdi     # 0x1c1cd
leaq 0x1244d(%rip), %rsi     # 0x1c13e
movl $0x203, %edx            # imm = 0x203
movq %rax, (%rsp)
movb $0x0, %al
callq 0x117c0
movq -0x20(%rbp), %rax
movq 0x2018(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x2018(%rax)
movl -0x4c(%rbp), %eax
movb %al, %cl
movq -0x20(%rbp), %rax
movq 0x2018(%rax), %rax
movb %cl, (%rax)
movq -0x20(%rbp), %rax
movq 0x2018(%rax), %rax
movb $0x0, 0x1(%rax)
movl $0x1, -0x44(%rbp)
jmp 0x9d41
jmp 0x9d43
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq -0x20(%rbp), %rcx
cmpq 0x2018(%rcx), %rax
ja 0x9ed3
movq -0x30(%rbp), %rax
movsbl (%rax), %eax
cmpl $0xa, %eax
jne 0x9de7
movq -0x20(%rbp), %rax
movl $0x1, 0x205c(%rax)
movq -0x20(%rbp), %rdi
addq $0x2060, %rdi           # imm = 0x2060
movl $0xa, %esi
leaq 0xfc79(%rip), %rdx      # 0x19a06
movb $0x0, %al
callq 0x2130
movq -0x30(%rbp), %rax
movsbl 0x1(%rax), %eax
cmpl $0xd, %eax
jne 0x9de5
movq -0x20(%rbp), %rax
movl 0x205c(%rax), %ecx
orl $0x2, %ecx
movl %ecx, 0x205c(%rax)
movq -0x20(%rbp), %rdi
addq $0x2060, %rdi           # imm = 0x2060
movl $0xa, %esi
leaq 0x14efc(%rip), %rdx     # 0x1ecc7
movb $0x0, %al
callq 0x2130
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
movl $0x1, -0x48(%rbp)
jmp 0x9de7
movq -0x30(%rbp), %rax
movsbl (%rax), %eax
cmpl $0xd, %eax
jne 0x9ed1
movq -0x20(%rbp), %rax
movl $0x2, 0x205c(%rax)
movq -0x20(%rbp), %rdi
addq $0x2060, %rdi           # imm = 0x2060
movl $0xa, %esi
leaq 0x14eac(%rip), %rdx     # 0x1ecc8
movb $0x0, %al
callq 0x2130
movq -0x30(%rbp), %rax
movsbl 0x1(%rax), %eax
cmpl $0xd, %eax
jne 0x9e67
movq -0x20(%rbp), %rax
movl $0x2, 0x205c(%rax)
movl $0x1, 0x21114(%rip)     # 0x2af5c
movl $0x1, 0x2110e(%rip)     # 0x2af60
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
movl $0x1, -0x48(%rbp)
jmp 0x9ecf
movq -0x30(%rbp), %rax
movsbl 0x1(%rax), %eax
cmpl $0xa, %eax
jne 0x9ec3
movq -0x20(%rbp), %rdi
addq $0x2060, %rdi           # imm = 0x2060
movl $0xa, %esi
leaq 0x123a9(%rip), %rdx     # 0x1c234
movb $0x0, %al
callq 0x2130
movq -0x20(%rbp), %rax
movl 0x205c(%rax), %ecx
orl $0x1, %ecx
movl %ecx, 0x205c(%rax)
cmpl $0x0, 0x210b4(%rip)     # 0x2af60
jne 0x9ec1
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
movl $0x1, -0x48(%rbp)
jmp 0x9ecd
movl $0x1, 0x21093(%rip)     # 0x2af60
jmp 0x9ecf
jmp 0x9ed1
jmp 0x9ed3
movq -0x30(%rbp), %rax
movq -0x20(%rbp), %rcx
movq 0x2010(%rcx), %rcx
subq %rcx, %rax
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x34(%rbp)
jl 0x9efc
movl -0x34(%rbp), %eax
cmpl -0x40(%rbp), %eax
jge 0x9efc
movl -0x34(%rbp), %eax
movl %eax, -0x40(%rbp)
cmpl $0x0, -0x48(%rbp)
jne 0x9f17
cmpl $-0x1, -0x50(%rbp)
je 0x9f17
movl -0x50(%rbp), %edi
movq -0x20(%rbp), %rax
movq (%rax), %rsi
callq 0x22c0
jmp 0x9f19
movq -0x20(%rbp), %rax
movq 0x2018(%rax), %rax
movq -0x20(%rbp), %rcx
movq 0x2010(%rcx), %rcx
subq %rcx, %rax
movslq -0x40(%rbp), %rcx
cmpq %rcx, %rax
jl 0x9faf
cmpl $0x0, -0x40(%rbp)
jge 0x9f5b
leaq 0x122ef(%rip), %rdi     # 0x1c237
leaq 0x121ef(%rip), %rsi     # 0x1c13e
movl $0x271, %edx            # imm = 0x271
movb $0x0, %al
callq 0x117c0
movq -0x28(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x2010(%rax), %rsi
movl -0x40(%rbp), %eax
addl $0x1, %eax
movslq %eax, %rdx
callq 0x2250
movl -0x40(%rbp), %edx
addl $0x1, %edx
movq -0x20(%rbp), %rax
movq 0x2010(%rax), %rcx
movslq %edx, %rdx
addq %rdx, %rcx
movq %rcx, 0x2010(%rax)
movq -0x28(%rbp), %rax
movslq -0x40(%rbp), %rcx
movb $0x0, 0x1(%rax,%rcx)
movl $0x0, -0x40(%rbp)
jmp 0xa056
movq -0x20(%rbp), %rax
movq 0x2018(%rax), %rax
movq -0x20(%rbp), %rcx
movq 0x2010(%rcx), %rcx
subq %rcx, %rax
movl %eax, -0x38(%rbp)
cmpl $0x0, -0x38(%rbp)
jl 0xa046
movq -0x28(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x2010(%rax), %rsi
movl -0x38(%rbp), %eax
addl $0x1, %eax
movslq %eax, %rdx
callq 0x2250
movq -0x28(%rbp), %rax
movslq -0x38(%rbp), %rcx
movb $0x0, 0x1(%rax,%rcx)
movl -0x38(%rbp), %ecx
addl $0x1, %ecx
movq -0x28(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x28(%rbp)
movl -0x38(%rbp), %ecx
addl $0x1, %ecx
movl -0x40(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x40(%rbp)
movq -0x20(%rbp), %rax
movq 0x2018(%rax), %rcx
addq $0x1, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x2010(%rax)
cmpl $0x0, -0x40(%rbp)
jge 0xa044
movl $0x0, -0x40(%rbp)
jmp 0xa046
movq -0x20(%rbp), %rdi
callq 0x94f0
movl $0x0, -0x44(%rbp)
cmpl $0x0, -0x44(%rbp)
je 0xa06c
movq -0x20(%rbp), %rdi
callq 0x94f0
movl $0x0, -0x44(%rbp)
jmp 0x9bbb
movq -0x10(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x20(%rbp), %rax
movl $0x0, 0x2054(%rax)
movq -0x20(%rbp), %rax
movsbl 0x2058(%rax), %eax
cmpl $0xa, %eax
je 0xa0a7
movq -0x20(%rbp), %rax
movsbl 0x2058(%rax), %eax
cmpl $0xd, %eax
jne 0xa0cf
movq -0x28(%rbp), %rax
movsbl (%rax), %eax
cmpl $0xa, %eax
je 0xa0bf
movq -0x28(%rbp), %rax
movsbl (%rax), %eax
cmpl $0xd, %eax
jne 0xa0cd
movq -0x20(%rbp), %rax
movl $0x1, 0x2054(%rax)
jmp 0xa0cf
movq -0x28(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x28(%rbp), %rdi
callq 0x2100
movq %rax, %rcx
movq -0x60(%rbp), %rax
subq $0x1, %rcx
movb (%rax,%rcx), %cl
movq -0x20(%rbp), %rax
movb %cl, 0x2058(%rax)
movq -0x20(%rbp), %rax
movq 0x2048(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x2048(%rax)
movq -0x10(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x70, %rsp
popq %rbp
retq