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

📄 icstringbuffer.c

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

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

int icStringBufferCreate (
   icStringBuffer **buffer,
   VoltLibCtx *libCtx
   )
{
  int status;
  icStringBuffer *newBuf = (icStringBuffer *)0;
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  *buffer = (icStringBuffer *)0;

  do
  {
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_MEMORY;
    newBuf = (icStringBuffer *)Z3Malloc (sizeof (icStringBuffer));
    if (newBuf == (icStringBuffer *)0)
      break;
    Z2Memset (newBuf, 0, sizeof (icStringBuffer));

    /* Create an initial string of 8 bytes.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    newBuf->str = (char *)Z3Malloc (8);
    if (newBuf->str == (char *)0)
      break;
    Z2Memset (newBuf->str, 0, 8);

    newBuf->capacity = 8;

    *buffer = newBuf;

    status = 0;

  } while (0);

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

  /* If there was an error, free any memory we allocated.
   */
  if (newBuf != (icStringBuffer *)0)
    Z2Free (newBuf);

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

  return (status);
}

void icStringBufferFree (
   icStringBuffer **buffer,
   VoltLibCtx *libCtx
   )
{
  /* Anything to free?
   */
  if (buffer == (icStringBuffer **)0)
    return;
  if (*buffer == (icStringBuffer *)0)
    return;

  Z2Free ((*buffer)->str);
  Z2Free (*buffer);

  *buffer = (icStringBuffer *)0;
}

int icStringBufferAppend (
   icStringBuffer *buffer,
   int theChar,
   VoltLibCtx *libCtx
   )
{
  int status;
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  do
  {
    /* If buffer + 1 >= max capacity we need to increase the size.
     * We'll double it to be convenient.
     */
    if (buffer->length + 1 >= buffer->capacity)
    {
      VOLT_SET_FNCT_LINE (fnctLine)
      status = VT_ERROR_MEMORY;
      buffer->str = (char *)Z2Realloc (buffer->str, buffer->capacity * 2);
      if (buffer->str == (char *)0)
        break;
      buffer->capacity *= 2;
      Z2Memset (
        buffer->str + buffer->length, 0, buffer->capacity - buffer->length);
    }

    buffer->str[buffer->length] = theChar;
    buffer->length++;

    status = 0;

  } while (0);

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

  return (status);
}

⌨️ 快捷键说明

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