| RSA BSAFE Cert-C |
Certificate Components for C |
| Crypto-C 6.2.1 Developer's Guide | ||
| Search |
/* $Id: memio.c,v 1.4 2005/02/25 06:09:30 alockwoo Exp $ */ /* memio.c ** Copyright (c) 2001-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. ** ** This file contains a sample implementation of a stream service provider ** which simply reads data from a given memory buffer. */ #include "certc.h" #include "memio.h" #include "demoutil.h" static void Finalize (CERTC_CTX ctx, POINTER handle) { MEM_IO_PVT *memInfo = (MEM_IO_PVT *)handle; UNUSED_ARG (ctx); RSA_PrintMessage ("***Calling Memory IO Finalize\n"); if (memInfo) T_free (memInfo->buffer.data); T_free (handle); return; } /* end Finalize */ static int Open (CERTC_CTX ctx, POINTER handle, POINTER name, unsigned int flags, POINTER *streamID) { MEM_IO_PVT *memInfo = (MEM_IO_PVT *)handle; UNUSED_ARG (ctx); UNUSED_ARG (name); UNUSED_ARG (flags); /* If we wanted to be fancy, we could do something with the streamID, possibly support multiple memory buffers instead of just one ITEM or something like that, but we leave that as an exercise for the user if that's desired. */ *streamID = NULL_PTR; RSA_PrintMessage ("***Calling Memory IO Open\n"); /* Set the stream to the beginning of the memory buffer. This is necessary because we could open the same buffer more than once. */ memInfo->bytesRead = 0; return 0; } /* end Open */ static int Close (CERTC_CTX ctx, POINTER handle, POINTER streamID) { UNUSED_ARG (ctx); UNUSED_ARG (handle); UNUSED_ARG (streamID); RSA_PrintMessage ("***Calling Memory IO Close\n"); return 0; } /* end Close */ static int Read (CERTC_CTX ctx, POINTER handle, POINTER streamID, unsigned char *buf, unsigned int len, unsigned int *actualLen) { int status = 0; MEM_IO_PVT *memInfo = (MEM_IO_PVT *)handle; unsigned char *memBuf = NULL; unsigned int bytesLeft = 0; UNUSED_ARG (ctx); UNUSED_ARG (streamID); *actualLen = 0; RSA_PrintMessage ("***Calling Memory IO Read\n"); memBuf = memInfo->buffer.data + memInfo->bytesRead; bytesLeft = memInfo->buffer.len - memInfo->bytesRead; if (bytesLeft != 0) { /* if we cannot output all of the data, copy as much as we can, then return 0 to indicate the end of stream has not been reached */ if (bytesLeft > len) { T_memcpy (buf, memBuf, len); memInfo->bytesRead += len; *actualLen = len; } else { T_memcpy (buf, memBuf, bytesLeft); memInfo->bytesRead += bytesLeft; *actualLen = bytesLeft; } status = 0; } else { status = E_EOS; } return status; } /* end Read */ static int Rewind (CERTC_CTX ctx, POINTER handle, POINTER streamID) { int status = 0; MEM_IO_PVT *memInfo = (MEM_IO_PVT *)handle; UNUSED_ARG (ctx); UNUSED_ARG (streamID); memInfo->bytesRead = 0; return status; } /* end Rewind */ static int Write (CERTC_CTX ctx, POINTER handle, POINTER streamID, unsigned char *buf, unsigned int len) { int status = 0; MEM_IO_PVT *memInfo = (MEM_IO_PVT *)handle; POINTER ptr; UNUSED_ARG (ctx); UNUSED_ARG (streamID); ptr = T_realloc (memInfo->buffer.data, memInfo->buffer.len + len); if (ptr == NULL_PTR) { status = RSA_DEMO_E_ALLOC; goto CLEANUP; } memInfo->buffer.data = ptr; T_memcpy (memInfo->buffer.data + memInfo->buffer.len, buf, len); memInfo->buffer.len += len; CLEANUP: return status; } /* end Write */ int S_InitializeMemoryIO (CERTC_CTX ctx, POINTER params, SERVICE_FUNCS *funcs, POINTER *handle) { IO_FUNCS *ioFuncs = (IO_FUNCS *)funcs; MEM_IO_PVT *memInfo = NULL; ITEM *inputBuf = (ITEM *)params; UNUSED_ARG (ctx); UNUSED_ARG (params); T_memset ((POINTER)ioFuncs, 0, sizeof (*ioFuncs)); /* handle is used to keep track of bytes read */ *handle = T_malloc (sizeof (MEM_IO_PVT)); if (*handle == NULL) return E_ALLOC; T_memset ((POINTER)*handle, 0, sizeof (MEM_IO_PVT)); memInfo = (MEM_IO_PVT *)*handle; memInfo->bytesRead = 0; /* copy the input buffer because we have no guarantees about how long the caller will keep it around intact */ memInfo->buffer.data = T_malloc (inputBuf->len); if (memInfo->buffer.data == NULL_PTR) return E_ALLOC; memInfo->buffer.len = inputBuf->len; T_memcpy (memInfo->buffer.data, inputBuf->data, memInfo->buffer.len); RSA_PrintMessage ("***Calling S_InitializeMemoryIO\n"); ioFuncs->Finalize = Finalize; ioFuncs->Open = Open; ioFuncs->Close = Close; ioFuncs->Read = Read; ioFuncs->Rewind = Rewind; ioFuncs->Write = Write; return 0; } /* end S_InitializeMemoryIO */