#include <string.h>
#include <stdio.h>

/* convert CR, LF sequences to CR */
void stringop_crlfToCr(unsigned char **inputString, int *inputLength, unsigned char *outputString, int *outputLength, int *notDone)
{
  int inPtr=0, outPtr=0;

  while ((inPtr<*inputLength) && (outPtr<*outputLength))
  {
    if ((*inputString)[inPtr]=='\r')
    {
      if (outPtr+1==*outputLength)
      {
        /* if this is the last character in the output buffer,
         * don't handle it - leave it so we don't split sequences over buffers */
        break;
      }
      if ((*inputString)[inPtr+1]=='\n')
      {
        /* CR,LF */
        outputString[outPtr] = '\r';
        outPtr++;
        inPtr+=2; /* skip over the LF as well */
      }
      else
      {
        /* CR, something else */
        outputString[outPtr] = '\r';
        outPtr++;
        inPtr++;/* don't skip next */
      }
    }
    else
    {
      outputString[outPtr] = (*inputString)[inPtr];
      outPtr++;
      inPtr++;
    }
  }

  /* update values so we can just recall this function to handle the rest */
  if (inPtr<*inputLength)
    *notDone = *inputLength - inPtr;
  else
    *notDone = 0;
  *inputString += inPtr;
  *inputLength -= inPtr;
  *outputLength = outPtr;

  return;
}
