#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <unistd.h>
#include "g10lib.h"
#include "mpi.h"
#include "context.h"
#define CTX_MAGIC "cTx"
#define CTX_MAGIC_LEN 3
struct gcry_context
{
char magic[CTX_MAGIC_LEN];
char type;
void (*deinit)(void*);
PROPERLY_ALIGNED_TYPE u;
};
gcry_ctx_t
_gcry_ctx_alloc (int type, size_t length, void (*deinit)(void*))
{
gcry_ctx_t ctx;
switch (type)
{
case CONTEXT_TYPE_EC:
break;
default:
log_bug ("bad context type %d given to _gcry_ctx_alloc\n", type);
break;
}
if (length < sizeof (PROPERLY_ALIGNED_TYPE))
length = sizeof (PROPERLY_ALIGNED_TYPE);
ctx = xtrycalloc (1, sizeof *ctx - sizeof (PROPERLY_ALIGNED_TYPE) + length);
if (!ctx)
return NULL;
memcpy (ctx->magic, CTX_MAGIC, CTX_MAGIC_LEN);
ctx->type = type;
ctx->deinit = deinit;
return ctx;
}
void *
_gcry_ctx_get_pointer (gcry_ctx_t ctx, int type)
{
if (!ctx || memcmp (ctx->magic, CTX_MAGIC, CTX_MAGIC_LEN))
log_fatal ("bad pointer %p passed to _gcry_ctx_get_pointer\n", ctx);
if (ctx->type != type)
log_fatal ("wrong context type %d request for context %p of type %d\n",
type, ctx, ctx->type);
return &ctx->u;
}
void *
_gcry_ctx_find_pointer (gcry_ctx_t ctx, int type)
{
if (!ctx)
return NULL;
if (memcmp (ctx->magic, CTX_MAGIC, CTX_MAGIC_LEN))
log_fatal ("bad pointer %p passed to _gcry_ctx_get_pointer\n", ctx);
if (ctx->type != type)
return NULL;
return &ctx->u;
}
void
_gcry_ctx_release (gcry_ctx_t ctx)
{
if (!ctx)
return;
if (memcmp (ctx->magic, CTX_MAGIC, CTX_MAGIC_LEN))
log_fatal ("bad pointer %p passed to gcry_ctx_relase\n", ctx);
switch (ctx->type)
{
case CONTEXT_TYPE_EC:
break;
default:
log_fatal ("bad context type %d detected in gcry_ctx_relase\n",
ctx->type);
break;
}
if (ctx->deinit)
ctx->deinit (&ctx->u);
xfree (ctx);
}