blob: ae1be21818aca2892463f0b32f4dffe1a1e35367 [file] [log] [blame]
/* gpg-error.h - Public interface to libgpg-error.
Copyright (C) 2003, 2004, 2010 g10 Code GmbH
This file is part of libgpg-error.
libgpg-error is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public License
as published by the Free Software Foundation; either version 2.1 of
the License, or (at your option) any later version.
libgpg-error is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#ifndef GPG_ERROR_H
#define GPG_ERROR_H 1
#include <stddef.h>
#ifdef __GNUC__
#define GPG_ERR_INLINE __inline__
#elif __STDC_VERSION__ >= 199901L
#define GPG_ERR_INLINE inline
#else
#ifndef GPG_ERR_INLINE
#define GPG_ERR_INLINE
#endif
#endif
#ifdef __cplusplus
extern "C" {
#if 0 /* just to make Emacs auto-indent happy */
}
#endif
#endif /* __cplusplus */
/* The GnuPG project consists of many components. Error codes are
exchanged between all components. The common error codes and their
user-presentable descriptions are kept into a shared library to
allow adding new error codes and components without recompiling any
of the other components. The interface will not change in a
backward incompatible way.
An error code together with an error source build up an error
value. As the error value is been passed from one component to
another, it preserver the information about the source and nature
of the error.
A component of the GnuPG project can define the following macros to
tune the behaviour of the library:
GPG_ERR_SOURCE_DEFAULT: Define to an error source of type
gpg_err_source_t to make that source the default for gpg_error().
Otherwise GPG_ERR_SOURCE_UNKNOWN is used as default.
GPG_ERR_ENABLE_GETTEXT_MACROS: Define to provide macros to map the
internal gettext API to standard names. This has only an effect on
Windows platforms. */
/* The error source type gpg_err_source_t.
Where as the Poo out of a welle small
Taketh his firste springing and his sours.
--Chaucer. */
/* Only use free slots, never change or reorder the existing
entries. */
typedef enum
{
@include err-sources.h.in
/* This is one more than the largest allowed entry. */
GPG_ERR_SOURCE_DIM = 128
} gpg_err_source_t;
/* The error code type gpg_err_code_t. */
/* Only use free slots, never change or reorder the existing
entries. */
typedef enum
{
@include err-codes.h.in
/* The following error codes are used to map system errors. */
#define GPG_ERR_SYSTEM_ERROR (1 << 15)
@include errnos.in
/* This is one more than the largest allowed entry. */
GPG_ERR_CODE_DIM = 65536
} gpg_err_code_t;
/* The error value type gpg_error_t. */
/* We would really like to use bit-fields in a struct, but using
structs as return values can cause binary compatibility issues, in
particular if you want to do it effeciently (also see
-freg-struct-return option to GCC). */
typedef unsigned int gpg_error_t;
/* We use the lowest 16 bits of gpg_error_t for error codes. The 16th
bit indicates system errors. */
#define GPG_ERR_CODE_MASK (GPG_ERR_CODE_DIM - 1)
/* Bits 17 to 24 are reserved. */
/* We use the upper 7 bits of gpg_error_t for error sources. */
#define GPG_ERR_SOURCE_MASK (GPG_ERR_SOURCE_DIM - 1)
#define GPG_ERR_SOURCE_SHIFT 24
/* The highest bit is reserved. It shouldn't be used to prevent
potential negative numbers when transmitting error values as
text. */
/* GCC feature test. */
#undef _GPG_ERR_HAVE_CONSTRUCTOR
#if __GNUC__
#define _GPG_ERR_GCC_VERSION (__GNUC__ * 10000 \
+ __GNUC_MINOR__ * 100 \
+ __GNUC_PATCHLEVEL__)
#if _GPG_ERR_GCC_VERSION > 30100
#define _GPG_ERR_CONSTRUCTOR __attribute__ ((__constructor__))
#define _GPG_ERR_HAVE_CONSTRUCTOR
#endif
#endif
#ifndef _GPG_ERR_CONSTRUCTOR
#define _GPG_ERR_CONSTRUCTOR
#endif
/* Initialization function. */
/* Initialize the library. This function should be run early. */
gpg_error_t gpg_err_init (void) _GPG_ERR_CONSTRUCTOR;
/* If this is defined, the library is already initialized by the
constructor and does not need to be initialized explicitely. */
#undef GPG_ERR_INITIALIZED
#ifdef _GPG_ERR_HAVE_CONSTRUCTOR
#define GPG_ERR_INITIALIZED 1
#endif
/* See the source on how to use the deinit function; it is usually not
required. */
void gpg_err_deinit (int mode);
/* Constructor and accessor functions. */
/* Construct an error value from an error code and source. Within a
subsystem, use gpg_error. */
static GPG_ERR_INLINE gpg_error_t
gpg_err_make (gpg_err_source_t source, gpg_err_code_t code)
{
return code == GPG_ERR_NO_ERROR ? GPG_ERR_NO_ERROR
: (((source & GPG_ERR_SOURCE_MASK) << GPG_ERR_SOURCE_SHIFT)
| (code & GPG_ERR_CODE_MASK));
}
/* The user should define GPG_ERR_SOURCE_DEFAULT before including this
file to specify a default source for gpg_error. */
#ifndef GPG_ERR_SOURCE_DEFAULT
#define GPG_ERR_SOURCE_DEFAULT GPG_ERR_SOURCE_UNKNOWN
#endif
static GPG_ERR_INLINE gpg_error_t
gpg_error (gpg_err_code_t code)
{
return gpg_err_make (GPG_ERR_SOURCE_DEFAULT, code);
}
/* Retrieve the error code from an error value. */
static GPG_ERR_INLINE gpg_err_code_t
gpg_err_code (gpg_error_t err)
{
return (gpg_err_code_t) (err & GPG_ERR_CODE_MASK);
}
/* Retrieve the error source from an error value. */
static GPG_ERR_INLINE gpg_err_source_t
gpg_err_source (gpg_error_t err)
{
return (gpg_err_source_t) ((err >> GPG_ERR_SOURCE_SHIFT)
& GPG_ERR_SOURCE_MASK);
}
/* String functions. */
/* Return a pointer to a string containing a description of the error
code in the error value ERR. This function is not thread-safe. */
const char *gpg_strerror (gpg_error_t err);
/* Return the error string for ERR in the user-supplied buffer BUF of
size BUFLEN. This function is, in contrast to gpg_strerror,
thread-safe if a thread-safe strerror_r() function is provided by
the system. If the function succeeds, 0 is returned and BUF
contains the string describing the error. If the buffer was not
large enough, ERANGE is returned and BUF contains as much of the
beginning of the error string as fits into the buffer. */
int gpg_strerror_r (gpg_error_t err, char *buf, size_t buflen);
/* Return a pointer to a string containing a description of the error
source in the error value ERR. */
const char *gpg_strsource (gpg_error_t err);
/* Mapping of system errors (errno). */
/* Retrieve the error code for the system error ERR. This returns
GPG_ERR_UNKNOWN_ERRNO if the system error is not mapped (report
this). */
gpg_err_code_t gpg_err_code_from_errno (int err);
/* Retrieve the system error for the error code CODE. This returns 0
if CODE is not a system error code. */
int gpg_err_code_to_errno (gpg_err_code_t code);
/* Retrieve the error code directly from the ERRNO variable. This
returns GPG_ERR_UNKNOWN_ERRNO if the system error is not mapped
(report this) and GPG_ERR_MISSING_ERRNO if ERRNO has the value 0. */
gpg_err_code_t gpg_err_code_from_syserror (void);
/* Set the ERRNO variable. This function is the preferred way to set
ERRNO due to peculiarities on WindowsCE. */
void gpg_err_set_errno (int err);
@include extra-h.in
/* Self-documenting convenience functions. */
static GPG_ERR_INLINE gpg_error_t
gpg_err_make_from_errno (gpg_err_source_t source, int err)
{
return gpg_err_make (source, gpg_err_code_from_errno (err));
}
static GPG_ERR_INLINE gpg_error_t
gpg_error_from_errno (int err)
{
return gpg_error (gpg_err_code_from_errno (err));
}
static GPG_ERR_INLINE gpg_error_t
gpg_error_from_syserror (void)
{
return gpg_error (gpg_err_code_from_syserror ());
}
#ifdef __cplusplus
}
#endif
#endif /* GPG_ERROR_H */