RSA BSAFE Cert-C

Certificate Components for C

Crypto-C 6.2.1 Developer's Guide
Search

simpleio.c

Contains I/O interface routines such as the IO_CTX Read() callback. The routine SimpleWriteText() uses the constant SKIP_CR or SKIP_LF. SimpleWriteText() is used to handle output for "text" files properly, by converting CR/LF end-of-line delimiters to the local format. For example, on UNIX, define SKIP_CR=1 in order to remove carriage return characters, leaving only line feed as the delimiter. The default is not to skip CR and LF, leaving them both in as delimiters. If any of these values need to be changed from the default, a compiler flag can set them. For example:
-DSKIP_CR=1

/* $Id: simpleio.c,v 1.3 2004/03/02 05:18:34 gsingh Exp $ */
/* simpleio.c
** Copyright (c) 1992-2002, RSA Security Inc.
**
** This file is used to demonstrate how to interface to an RSA Security
** licensed development product.  You have a royalty-free right to use,
** modify, reproduce and distribute this demonstration file (including
** any modified version), provided that you agree that RSA Security has
** no warranty, implied or otherwise, or liability for this demonstration
** file or any modified version.
*/

#include <stdio.h>
#include <string.h>
#include "aglobal.h"
#include "bcert.h"
#include "demo.h"
#include "simpleio.h"
#include "stdlibrf.h"

/* SKIP_CR and SKIP_LF are used by SimpleWriteText to convert a block
     with CR/LF line delimiters to the loca format.  For example, on
     UNIX which uses only LF line delimiters, SKIP_CR should be defined
     as 1 and SKIP_LF should be defined as 0.
   The following defines SKIP_CR and SKIP_LF as 0 if they have not already
     been defined as 1 with C compiler flags.  This preserves all CR/LF
     line delimiters as a default (which is correct for MS-DOS).
 */
#ifndef SKIP_CR
#define SKIP_CR 0
#endif
#ifndef SKIP_LF
#define SKIP_LF 0
#endif


static int SimpleFileGetLine PROTO_LIST ((char *, unsigned int, FILE *));
static int SimpleFileWriteText PROTO_LIST
  ((unsigned char *, unsigned int, FILE *));


     
/* Initialize all fields of a IO_CTX structure and set the handle to NULL_PTR.
   If useBinary is 0, set up a context for processing Internet
     standard text files, if non-zero then set up for binary files.
 */
void InitSimpleIOContext (ioContext, useBinary)
IO_CTX *ioContext;
int useBinary;
{
  ioContext->GetLine = SimpleGetLine;
  ioContext->PutLine = SimplePutLine;
  if (useBinary) {
    ioContext->Read = SimpleReadBinary;
    ioContext->Write = SimpleWriteBinary;
  }
  else {
    ioContext->Read = SimpleReadText;
    ioContext->Write = SimpleWriteText;
  }
  ioContext->Rewind = SimpleRewind;
  ioContext->handle = NULL_PTR;
  ioContext->reserved = NULL_PTR;
}

/* The file type of thisStream is expected to be a binary stream
     (no line delimiter translation).
 */
int SimpleGetLine (lineBuffer, maxLineSize, thisStream)
char *lineBuffer;
unsigned int maxLineSize;
POINTER thisStream;
{
  return (SimpleFileGetLine (lineBuffer, maxLineSize, (FILE *)thisStream));
}

/* The file type of thisStream is expected to be a binary stream
     (no line delimiter translation).
 */
int SimplePutLine (lineBuffer, thisStream)
char *lineBuffer;
POINTER thisStream;
{
  if (*lineBuffer != '\0') {
    if (fputs (lineBuffer, (FILE *)thisStream) < 0)
      return (D_IO);
  }
  
  /* SimpleWriteText will correctly translate the end of line characters. */
  return (SimpleFileWriteText
          ((unsigned char *)"\015\012", 2, (FILE *)thisStream));
}

/* The file type of thisStream is expected to be a binary stream
     (no line delimiter translation).
 */
int SimpleReadBinary (inBuffer, inLen, maxLen, thisStream)
unsigned char *inBuffer;
unsigned int *inLen;
unsigned int maxLen;
POINTER thisStream;
{
  /* fread () returns the number of items read in.  If the number read in
       is less than maxLen, then check for end of file.
   */
  if ((*inLen = fread (inBuffer, 1, maxLen, (FILE *)thisStream)) == 0) {
    if (feof ((FILE *)thisStream))
      return (D_EOS);
    else
      return (D_IO);
  }
  else if (*inLen < maxLen && !feof ((FILE *)thisStream))
    return (D_IO);
  
  return (0);
}

/* Because SimpleFileGetLine will correctly read the last line even if it is
     not explicitly terminated with end of line characters, this 
     implementation of Read guarantees that the end of line characters
     are put on the end of the last line.
   The file type of thisStream is expected to be a binary stream
     (no line delimiter translation).
 */
int SimpleReadText (inBuffer, inLen, maxLen, thisStream)
unsigned char *inBuffer;
unsigned int *inLen;
unsigned int maxLen;
POINTER thisStream;
{
  int status;

  /* Set inLen to 0 in case SimpleGetLine returns D_EOS */
  *inLen = 0;
  
  /* SimpleGetLine strips the end of line */
  if ((status = SimpleFileGetLine
       ((char *)inBuffer, maxLen-1, (FILE *)thisStream)) != 0)
    return (status);
  
  *inLen = T_strlen ((char *)inBuffer);
  /* add <CR><LF> */
  inBuffer [(*inLen) ++] = 13;
  inBuffer [(*inLen) ++] = 10;

  return (0);
}

int SimpleRewind (thisStream)
POINTER thisStream;
{
  rewind ((FILE *)thisStream);
  return (0);
}

/* The file type of thisStream is expected to be a binary stream
     (no line delimiter translation).
 */
int SimpleWriteBinary (outBuffer, outLen, thisStream)
unsigned char *outBuffer;
unsigned int outLen;
POINTER thisStream;
{
  if (fwrite (outBuffer, 1, outLen, (FILE *)thisStream) < outLen)
    return (D_IO);

  return (0);
}

/* The file type of thisStream is expected to be a binary stream
     (no line delimiter translation).
   This uses SKIP_CR and SKIP_LF to omit a carriage return or line feed
     when converting to local format.
   This assumes that both SKIP_CR and SKIP_LF are not defined as 1.
 */
int SimpleWriteText (outBuffer, outLen, thisStream)
unsigned char *outBuffer;
unsigned int outLen;
<