示例#1
0
static void
put_membuf (assuan_context_t ctx,
	    struct membuf *mb, const void *buf, size_t len)
{
  if (mb->out_of_core || mb->too_large)
    return;

  if (mb->maxlen && mb->len + len > mb->maxlen)
    {
      mb->too_large = 1;
      return;
    }

  if (mb->len + len >= mb->size)
    {
      char *p;
      
      mb->size += len + 1024;
      /* we need to allocate one byte more for get_membuf */
      p = _assuan_realloc (ctx, mb->buf, mb->size + 1);
      if (!p)
        {
          mb->out_of_core = 1;
          return;
        }
      mb->buf = p;
    }
  memcpy (mb->buf + mb->len, buf, len);
  mb->len += len;
}
示例#2
0
/**
 * assuan_register_command:
 * @ctx: the server context
 * @cmd_name: A string with the command name
 * @handler: The handler function to be called or NULL to use a default
 *           handler.
 * HELPSTRING
 *
 * Register a handler to be used for a given command.  Note that
 * several default handlers are already regsitered with a new context.
 * This function however allows to override them.
 *
 * Return value: 0 on success or an error code
 **/
gpg_error_t
assuan_register_command (assuan_context_t ctx, const char *cmd_name,
                         assuan_handler_t handler, const char *help_string)
{
  int i, cmd_index = -1;
  const char *s;

  if (cmd_name && !*cmd_name)
    cmd_name = NULL;

  if (!cmd_name)
    return _assuan_error (ctx, GPG_ERR_ASS_INV_VALUE);

  if (!handler)
    { /* find a default handler. */
      for (i=0; (s=std_cmd_table[i].name) && strcmp (cmd_name, s); i++)
        ;
      if (!s)
        { /* Try again but case insensitive. */
          for (i=0; (s=std_cmd_table[i].name)
                    && my_strcasecmp (cmd_name, s); i++)
            ;
        }
      if (s)
        handler = std_cmd_table[i].handler;
      if (!handler)
        handler = dummy_handler; /* Last resort is the dummy handler. */
    }

  if (!ctx->cmdtbl)
    {
      ctx->cmdtbl_size = 50;
      ctx->cmdtbl = _assuan_calloc (ctx, ctx->cmdtbl_size, sizeof *ctx->cmdtbl);
      if (!ctx->cmdtbl)
	return _assuan_error (ctx, gpg_err_code_from_syserror ());
      ctx->cmdtbl_used = 0;
    }
  else if (ctx->cmdtbl_used >= ctx->cmdtbl_size)
    {
      struct cmdtbl_s *x;

      x = _assuan_realloc (ctx, ctx->cmdtbl, (ctx->cmdtbl_size+10) * sizeof *x);
      if (!x)
	return _assuan_error (ctx, gpg_err_code_from_syserror ());
      ctx->cmdtbl = x;
      ctx->cmdtbl_size += 50;
    }

  for (i=0; i<ctx->cmdtbl_used; i++)
    {
      if (!my_strcasecmp (cmd_name, ctx->cmdtbl[i].name))
        {
	  cmd_index = i;
	  break;
	}
    }

  if (cmd_index == -1)
    cmd_index = ctx->cmdtbl_used++;

  ctx->cmdtbl[cmd_index].name = cmd_name;
  ctx->cmdtbl[cmd_index].handler = handler;
  ctx->cmdtbl[cmd_index].helpstr = help_string;
  return 0;
}