Blame src/trust-item.c

Packit Service 672cf4
/* trust-item.c - Trust item objects.
Packit Service 6c01f9
   Copyright (C) 2000 Werner Koch (dd9jn)
Packit Service 6c01f9
   Copyright (C) 2001, 2002, 2003, 2004 g10 Code GmbH
Packit Service 6c01f9
Packit Service 6c01f9
   This file is part of GPGME.
Packit Service 6c01f9
Packit Service 6c01f9
   GPGME is free software; you can redistribute it and/or modify it
Packit Service 6c01f9
   under the terms of the GNU Lesser General Public License as
Packit Service 6c01f9
   published by the Free Software Foundation; either version 2.1 of
Packit Service 6c01f9
   the License, or (at your option) any later version.
Packit Service 6c01f9
Packit Service 6c01f9
   GPGME is distributed in the hope that it will be useful, but
Packit Service 6c01f9
   WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 6c01f9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 6c01f9
   Lesser General Public License for more details.
Packit Service 6c01f9
Packit Service 6c01f9
   You should have received a copy of the GNU Lesser General Public
Packit Service 6c01f9
   License along with this program; if not, write to the Free Software
Packit Service 6c01f9
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
Packit Service 6c01f9
   02111-1307, USA.  */
Packit Service 672cf4
Packit Service 672cf4
#if HAVE_CONFIG_H
Packit Service 672cf4
#include <config.h>
Packit Service 672cf4
#endif
Packit Service 672cf4
#include <stdlib.h>
Packit Service 672cf4
#include <string.h>
Packit Service 672cf4
#include <assert.h>
Packit Service 672cf4
#include <errno.h>
Packit Service 672cf4
Packit Service 672cf4
#include "util.h"
Packit Service 672cf4
#include "ops.h"
Packit Service 672cf4
#include "sema.h"
Packit Service 672cf4
#include "debug.h"
Packit Service 672cf4
Packit Service 672cf4
Packit Service 672cf4
/* Protects all reference counters in trust items.  All other accesses
Packit Service 672cf4
   to a trust item are either read only or happen before the trust
Packit Service 672cf4
   item is available to the user.  */
Packit Service 672cf4
DEFINE_STATIC_LOCK (trust_item_ref_lock);
Packit Service 672cf4
Packit Service 672cf4

Packit Service 672cf4
/* Create a new trust item.  */
Packit Service 672cf4
gpgme_error_t
Packit Service 672cf4
_gpgme_trust_item_new (gpgme_trust_item_t *r_item)
Packit Service 672cf4
{
Packit Service 672cf4
  gpgme_trust_item_t item;
Packit Service 672cf4
Packit Service 672cf4
  item = calloc (1, sizeof *item);
Packit Service 672cf4
  if (!item)
Packit Service 672cf4
    return gpg_error_from_syserror ();
Packit Service 672cf4
  item->_refs = 1;
Packit Service 672cf4
  item->keyid = item->_keyid;
Packit Service 672cf4
  item->_keyid[16] = '\0';
Packit Service 672cf4
  item->owner_trust = item->_owner_trust;
Packit Service 672cf4
  item->_owner_trust[1] = '\0';
Packit Service 672cf4
  item->validity = item->_validity;
Packit Service 672cf4
  item->_validity[1] = '\0';
Packit Service 672cf4
  *r_item = item;
Packit Service 672cf4
  return 0;
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4

Packit Service 672cf4
/* Acquire a reference to ITEM.  */
Packit Service 672cf4
void
Packit Service 672cf4
gpgme_trust_item_ref (gpgme_trust_item_t item)
Packit Service 672cf4
{
Packit Service 672cf4
  LOCK (trust_item_ref_lock);
Packit Service 672cf4
  item->_refs++;
Packit Service 672cf4
  UNLOCK (trust_item_ref_lock);
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
Packit Service 672cf4
/* gpgme_trust_item_unref releases the trust item object. Note that
Packit Service 672cf4
   this function may not do an actual release if there are other
Packit Service 672cf4
   shallow copies of the object.  You have to call this function for
Packit Service 672cf4
   every newly created trust item object as well as for every
Packit Service 672cf4
   gpgme_trust_item_ref() done on the trust item object.  */
Packit Service 672cf4
void
Packit Service 672cf4
gpgme_trust_item_unref (gpgme_trust_item_t item)
Packit Service 672cf4
{
Packit Service 672cf4
  LOCK (trust_item_ref_lock);
Packit Service 672cf4
  assert (item->_refs > 0);
Packit Service 672cf4
  if (--item->_refs)
Packit Service 672cf4
    {
Packit Service 672cf4
      UNLOCK (trust_item_ref_lock);
Packit Service 672cf4
      return;
Packit Service 672cf4
    }
Packit Service 672cf4
  UNLOCK (trust_item_ref_lock);
Packit Service 672cf4
Packit Service 672cf4
  if (item->name)
Packit Service 672cf4
    free (item->name);
Packit Service 672cf4
  free (item);
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4

Packit Service 672cf4
/* Compatibility interfaces.  */
Packit Service 672cf4
void
Packit Service 672cf4
gpgme_trust_item_release (gpgme_trust_item_t item)
Packit Service 672cf4
{
Packit Service 672cf4
  gpgme_trust_item_unref (item);
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
/* Return the value of the attribute WHAT of ITEM, which has to be
Packit Service 672cf4
   representable by a string.  */
Packit Service 672cf4
const char *gpgme_trust_item_get_string_attr (gpgme_trust_item_t item,
Packit Service 672cf4
					      _gpgme_attr_t what,
Packit Service 672cf4
					      const void *reserved, int idx)
Packit Service 672cf4
{
Packit Service 672cf4
  const char *val = NULL;
Packit Service 672cf4
Packit Service 672cf4
  if (!item)
Packit Service 672cf4
    return NULL;
Packit Service 672cf4
  if (reserved)
Packit Service 672cf4
    return NULL;
Packit Service 672cf4
  if (idx)
Packit Service 672cf4
    return NULL;
Packit Service 672cf4
Packit Service 672cf4
  switch (what)
Packit Service 672cf4
    {
Packit Service 672cf4
    case GPGME_ATTR_KEYID:
Packit Service 672cf4
      val = item->keyid;
Packit Service 672cf4
      break;
Packit Service 672cf4
Packit Service 672cf4
    case GPGME_ATTR_OTRUST:
Packit Service 672cf4
      val = item->owner_trust;
Packit Service 672cf4
      break;
Packit Service 672cf4
Packit Service 672cf4
    case GPGME_ATTR_VALIDITY:
Packit Service 672cf4
      val = item->validity;
Packit Service 672cf4
      break;
Packit Service 672cf4
Packit Service 672cf4
    case GPGME_ATTR_USERID:
Packit Service 672cf4
      val = item->name;
Packit Service 672cf4
      break;
Packit Service 672cf4
Packit Service 672cf4
    default:
Packit Service 672cf4
      break;
Packit Service 672cf4
    }
Packit Service 672cf4
  return val;
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
Packit Service 672cf4
/* Return the value of the attribute WHAT of KEY, which has to be
Packit Service 672cf4
   representable by an integer.  IDX specifies a running index if the
Packit Service 672cf4
   attribute appears more than once in the key.  */
Packit Service 672cf4
int gpgme_trust_item_get_int_attr (gpgme_trust_item_t item, _gpgme_attr_t what,
Packit Service 672cf4
				   const void *reserved, int idx)
Packit Service 672cf4
{
Packit Service 672cf4
  int val = 0;
Packit Service 672cf4
Packit Service 672cf4
  if (!item)
Packit Service 672cf4
    return 0;
Packit Service 672cf4
  if (reserved)
Packit Service 672cf4
    return 0;
Packit Service 672cf4
  if (idx)
Packit Service 672cf4
    return 0;
Packit Service 672cf4
Packit Service 672cf4
  switch (what)
Packit Service 672cf4
    {
Packit Service 672cf4
    case GPGME_ATTR_LEVEL:
Packit Service 672cf4
      val = item->level;
Packit Service 672cf4
      break;
Packit Service 672cf4
Packit Service 672cf4
    case GPGME_ATTR_TYPE:
Packit Service 672cf4
      val = item->type;
Packit Service 672cf4
      break;
Packit Service 672cf4
Packit Service 672cf4
    default:
Packit Service 672cf4
      break;
Packit Service 672cf4
    }
Packit Service 672cf4
  return val;
Packit Service 672cf4
}