⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 icinputstream.c

📁 IBE是一种非对称密码技术
💻 C
字号:
/* Copyright 2003-2006, Voltage Security, all rights reserved.
*/

#include "vibe.h"
#include "environment.h"
#include "base.h"
#include "libctx.h"
#include "icInputStream.h"
#include "errorctx.h"

static int icStringInputStreamPeek (
   icInputStreamPtr in,
   unsigned char *buf,
   int count,
   int *bytesRead,
   VoltLibCtx *libCtx
   );

static int icStringInputStreamSkip (
   icInputStreamPtr in,
   int count,
   int *bytesRead,
   VoltLibCtx *libCtx
   );

int icInputStreamCreate (
   ICInputStreamImpl StreamImpl,
   unsigned char *str,
   int len,
   int copyFlag,
   icInputStream **in,
   VoltLibCtx *libCtx
   )
{
  int status;
  icInputStream *newStream = (icInputStream *)0;
  VOLT_DECLARE_ERROR_TYPE (errorType)
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  *in = (icInputStream *)0;

  do
  {
    /* Build the general InputStream shell.
     */
    VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_MEMORY;
    newStream = (icInputStream *)Z3Malloc (sizeof (icInputStream));
    if (newStream == (icInputStream *)0)
      break;
    Z2Memset (newStream, 0, sizeof (icInputStream));

    /* Call the Impl.
     */
    VOLT_SET_ERROR_TYPE (errorType, 0)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = StreamImpl (str, len, copyFlag, newStream, libCtx);
    if (status != 0)
      break;

    /* If everything worked, return the created stream.
     */
    *in = newStream;

  } while (0);

  if (status == 0)
    return (0);

  /* If there was an error, free what we allocated.
   */
  if (newStream != (icInputStream *)0)
    Z2Free (newStream);

  VOLT_LOG_ERROR (
    (VtLibCtx)libCtx, status, errorType, fnctLine,
    "icInputStreamCreate", (char *)0)

  return (status);
}

int icInputStreamRead (
   icInputStream *in,
   unsigned char *buf,
   int count,
   int *bytesRead,
   VoltLibCtx *libCtx
   )
{
  int status;
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  VOLT_SET_FNCT_LINE (fnctLine)
  status = in->read (in, buf, count, bytesRead, libCtx);

  VOLT_LOG_ERROR_COMPARE (
    status, (VtLibCtx)libCtx, status, 0, fnctLine,
    "icInputStreamRead", (char *)0)

  return (status);
}

int icInputStreamPeek (
   icInputStream *in,
   unsigned char *buf,
   int count,
   int *bytesRead,
   VoltLibCtx *libCtx
   )
{
  int status;
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  VOLT_SET_FNCT_LINE (fnctLine)
  status = in->peek (in, buf, count, bytesRead, libCtx);

  VOLT_LOG_ERROR_COMPARE (
    status, (VtLibCtx)libCtx, status, 0, fnctLine,
    "icInputStreamPeek", (char *)0)

  return (status);
}

int icInputStreamSkipWhitespace (
   icInputStream *in,
   int *bytesRead,
   VoltLibCtx *libCtx
   )
{
  int status, peekRead;
  char theChar;
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  *bytesRead = 0;

  do
  {
    VOLT_SET_FNCT_LINE (fnctLine)
    status = in->peek (in, &theChar, 1, &peekRead, libCtx);
    if (status != 0)
      break;

    /* If we reach the end of stream, just return 0.
     */
    if (peekRead == 0)
      break;

    /* If the current char is not a whitespace, everything is ready to
     * return.
     */
    if ( (theChar != ' ' ) && (theChar != '\f') && (theChar != '\n') &&
         (theChar != '\r') && (theChar != '\t') && (theChar != '\v') )
      break;

    /* If we reach this point in the code, the current char is
     * whitespace, so skip it.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = in->skip (in, 1, &peekRead, libCtx);
    if (status != 0)
      break;

    (*bytesRead)++;

  } while (1);

  VOLT_LOG_ERROR_COMPARE (
    status, (VtLibCtx)libCtx, status, 0, fnctLine,
    "icInputStreamSkipWhitespace", (char *)0)

  return (status);
}

void icInputStreamFree (
   icInputStream **in,
   VoltLibCtx *libCtx
   )
{
  /* Anything to free?
   */
  if (in == (icInputStream **)0)
    return;
  if (*in == (icInputStream *)0)
    return;

  if ((*in)->free != (ICFree)0)
    (*in)->free (*in, libCtx);

  Z2Free (*in);
  *in = (icInputStream *)0;
}

int ICInputStreamImplString (
   unsigned char *str,
   int len,
   int copyFlag,
   icInputStream *in,
   VoltLibCtx *libCtx
   )
{
  int status;
  icStringInputStreamData *localStream = (icStringInputStreamData *)0;
  unsigned char *buffer = (unsigned char *)0;
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  do
  {
    /* Allocate the buffer for the localStream.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
      status = VT_ERROR_MEMORY;
    localStream = (icStringInputStreamData *)Z3Malloc (
      sizeof (icStringInputStreamData));
    if (localStream == (icStringInputStreamData *)0)
      break;
    Z2Memset (localStream, 0, sizeof (icStringInputStreamData));

    /* Initialize the fields of localStream.
     */
    localStream->str = str;
    localStream->length = len;
    localStream->copyFlag = VOLT_IC_STREAM_COPY_REFERENCE;

    if (copyFlag == VOLT_IC_STREAM_COPY_DATA)
    {
      VOLT_SET_FNCT_LINE (fnctLine)
        buffer = (unsigned char *)Z3Malloc (len + 1);
      if (buffer == (unsigned char *)0)
        break;

      Z2Memcpy (buffer, str, len);
      buffer[len] = 0;
      localStream->str = buffer;
      localStream->copyFlag = VOLT_IC_STREAM_COPY_DATA;
    }

    localStream->position = 0;
    in->localStream = (Pointer)localStream;

    /* Set the function pointers.
     */
    in->read = icStringInputStreamRead;
    in->peek = icStringInputStreamPeek;
    in->skip = icStringInputStreamSkip;
    in->free = icStringInputStreamFree;

    status = 0;

  } while (0);

  if (status == 0)
    return (0);

  /* If there's an error, destroy what we created.
   */
  if (localStream != (icStringInputStreamData *)0)
    Z2Free (localStream);
  if (buffer != (unsigned char *)0)
    Z2Free (buffer);

  VOLT_LOG_ERROR (
    (VtLibCtx)libCtx, status, VT_ERROR_TYPE_PRIMARY, fnctLine,
    "ICInputStreamImplString", (char *)0)

    return (status);
}

void icStringInputStreamFree (
   icInputStream *in,
   VoltLibCtx *libCtx
   )
{
  icStringInputStreamData *localStream;

  if (in != (icInputStream *)0)
  {
    if (in->localStream != (Pointer)0)
    {
      localStream = (icStringInputStreamData *)(in->localStream);
      if (localStream->copyFlag == VOLT_IC_STREAM_COPY_DATA)
      {
        if (localStream->str != (unsigned char *)0)
          Z2Free (localStream->str);
      }
      Z2Free (in->localStream);
    }
  }
}

int icStringInputStreamRead (
   icInputStream *in,
   unsigned char *buf,
   int count,
   int *bytesRead,
   VoltLibCtx *libCtx
   )
{
  int readCount;
  icStringInputStreamData *localStream =
    (icStringInputStreamData *)(in->localStream);

  *bytesRead = 0;

  /* If pointer to the start offset in the stream is greater than or
   * equal to the size of the stream, we're at the end. Return 0 with
   * bytesRead set at 0.
   * If there are bytes to read, read them.
   */
  if (localStream->position < localStream->length)
  {
    /* We have bytes to read, but if there are not count bytes left in
     * the stream, read to the end.
     */
    readCount = localStream->length - localStream->position;
    if (count < readCount)
      readCount = count;

    /* Read the information and adjust the pointer accordingly
     */
    Z2Memcpy (buf, localStream->str + localStream->position, readCount);
    localStream->position += readCount;

    *bytesRead = readCount;
  }

  return (0);
}

static int icStringInputStreamPeek (
   icInputStream *in,
   unsigned char *buf,
   int count,
   int *bytesRead,
   VoltLibCtx *libCtx
   )
{
  int readCount;
  icStringInputStreamData *localStream =
    (icStringInputStreamData *)(in->localStream);

  *bytesRead = 0;

  /* If pointer to the start offset in the stream is greater than or
   * equal to the size of the stream, we're at the end. Return 0 with
   * bytesRead set at 0.
   * If there are bytes to read, read them.
   */
  if (localStream->position < localStream->length)
  {
    /* We have bytes to read, but if there are not count bytes left in
     * the stream, read to the end.
     */
    readCount = localStream->length - localStream->position;
    if (count < readCount)
      readCount = count;

    /* Read the information and adjust the pointer accordingly
     */
    Z2Memcpy (buf, localStream->str + localStream->position, readCount);

    *bytesRead = readCount;
  }

  return (0);
}

static int icStringInputStreamSkip (
   icInputStream *in,
   int count,
   int *bytesRead,
   VoltLibCtx *libCtx
   )
{
  int readCount;
  icStringInputStreamData *localStream =
    (icStringInputStreamData *)(in->localStream);

  /* If we're at the end of the stream, or if the skip would put us
   * beyond the end of the stream, skip to the end.
   */
  readCount = localStream->length - localStream->position;
  if (count < readCount)
    readCount = count;

  localStream->position += readCount;

  *bytesRead = readCount;

  return (0);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -