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

📄 icstack.c

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

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

int icStackCreate (
   icStack **stack,
   VoltLibCtx *libCtx
   )
{
  int status;
  icStack *theStack = (icStack *)0;
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  *stack = (icStack *)0;

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

    theStack->capacity = 8;
    theStack->length = 0;
    VOLT_SET_FNCT_LINE (fnctLine)
    theStack->items = (Pointer *)Z3Malloc (theStack->capacity * sizeof (Pointer));
    if (theStack->items == (Pointer *)0)
      break;
    Z2Memset (theStack->items, 0, theStack->capacity * sizeof (Pointer));

    *stack = theStack;

    status = 0;

  } while (0);

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

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

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

  return (status);
}

void icStackFree (
   icStack **stack,
   VoltLibCtx *libCtx
   )
{
  /* Anything to free?
   */
  if (stack == (icStack **)0)
    return;
  if (*stack == (icStack *)0)
    return;

  Z2Free ((*stack)->items);
  Z2Free (*stack);

  *stack = (icStack *)0;
}

int icStackPush (
   icStack *stack,
   Pointer item,
   VoltLibCtx *libCtx)
{
  int status;
  unsigned int index, newCapacity;
  Pointer *newArray = (Pointer *)0;
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  do
  {
    /* If capacity == number of elements already in stack, increase
     * amount of elements in the Stack
     */
    if (stack->capacity == stack->length)
    {
      /* We double the capacity every time the limit is reached
       */
      VOLT_SET_FNCT_LINE (fnctLine)
      status = VT_ERROR_MEMORY;
      newCapacity = stack->capacity * 2 * sizeof (int *);
      newArray = (Pointer *)Z3Malloc (newCapacity);
      if (newArray == (Pointer *)0)
        break;
      Z2Memset (newArray, 0, newCapacity);

      /* Copy the old into the new.
       */
      for (index = 0; index < stack->length; ++index)
        newArray[index] = stack->items[index];

      /* Free the old and set the object with the new.
       */
      Z2Free (stack->items);
      stack->items = newArray;
      stack->capacity *= 2;
    }

    /* Add the element.
     */
    stack->items[stack->length] = item;
    stack->length++;
    status = 0;

  } while (0);

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

  return (status);
}

int icStackPop (
   icStack *stack,
   Pointer *item,
   VoltLibCtx *libCtx
   )
{
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  VOLT_SET_FNCT_LINE (fnctLine)
  if (stack->length != 0)
  {
    stack->length--;
    if (item != (Pointer *)0)
      *item = stack->items[stack->length];

    stack->items[stack->length] = (Pointer)0;

    return (0);
  }

  if (item != (Pointer *)0)
    *item = (Pointer)0;

  VOLT_LOG_ERROR (
    (VtLibCtx)libCtx, VT_ERROR_INVALID_INPUT, VT_ERROR_TYPE_PRIMARY, fnctLine,
    "icStackPop", (char *)0)

  return (VT_ERROR_INVALID_INPUT);
}

int icStackTop (
   icStack *stack,
   Pointer *item,
   VoltLibCtx *libCtx
   )
{
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  VOLT_SET_FNCT_LINE (fnctLine)
  if (stack->length != 0)
  {
    *item = stack->items[stack->length - 1];
    return (0);
  }

  *item = (Pointer)0;

  VOLT_LOG_ERROR (
    (VtLibCtx)libCtx, VT_ERROR_INVALID_INPUT, VT_ERROR_TYPE_PRIMARY, fnctLine,
    "icStackTop", (char *)0)

  return (VT_ERROR_INVALID_INPUT);
}

⌨️ 快捷键说明

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