| 1 |
/* util.h |
| 2 |
* Copyright (C) 2001, 2009, 2012 g10 Code GmbH |
| 3 |
* |
| 4 |
* This file is part of KSBA. |
| 5 |
* |
| 6 |
* KSBA is free software; you can redistribute it and/or modify |
| 7 |
* it under the terms of either |
| 8 |
* |
| 9 |
* - the GNU Lesser General Public License as published by the Free |
| 10 |
* Software Foundation; either version 3 of the License, or (at |
| 11 |
* your option) any later version. |
| 12 |
* |
| 13 |
* or |
| 14 |
* |
| 15 |
* - the GNU General Public License as published by the Free |
| 16 |
* Software Foundation; either version 2 of the License, or (at |
| 17 |
* your option) any later version. |
| 18 |
* |
| 19 |
* or both in parallel, as here. |
| 20 |
* |
| 21 |
* KSBA is distributed in the hope that it will be useful, but WITHOUT |
| 22 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 23 |
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public |
| 24 |
* License for more details. |
| 25 |
* |
| 26 |
* You should have received a copies of the GNU General Public License |
| 27 |
* and the GNU Lesser General Public License along with this program; |
| 28 |
* if not, see <http://www.gnu.org/licenses/>. |
| 29 |
*/ |
| 30 |
|
| 31 |
#ifndef UTIL_H |
| 32 |
#define UTIL_H |
| 33 |
|
| 34 |
#ifdef BUILD_GENTOOLS |
| 35 |
#error file may not be be used for build time tools |
| 36 |
#endif |
| 37 |
|
| 38 |
|
| 39 |
#include "visibility.h" |
| 40 |
|
| 41 |
|
| 42 |
gpg_error_t _ksba_hash_buffer (const char *oid, |
| 43 |
const void *buffer, size_t length, |
| 44 |
size_t resultsize, |
| 45 |
unsigned char *result, size_t *resultlen); |
| 46 |
|
| 47 |
void *_ksba_reallocarray (void *a, size_t oldnmemb, size_t nmemb, size_t size); |
| 48 |
|
| 49 |
void *_ksba_xmalloc (size_t n ); |
| 50 |
void *_ksba_xcalloc (size_t n, size_t m ); |
| 51 |
void *_ksba_xrealloc (void *p, size_t n); |
| 52 |
char *_ksba_xstrdup (const char *p); |
| 53 |
|
| 54 |
#define xtrymalloc(a) ksba_malloc((a)) |
| 55 |
#define xtrycalloc(a,b) ksba_calloc((a),(b)) |
| 56 |
#define xtryrealloc(a,b) ksba_realloc((a),(b)) |
| 57 |
#define xtrystrdup(a) ksba_strdup((a)) |
| 58 |
#define xfree(a) ksba_free((a)) |
| 59 |
|
| 60 |
#define xmalloc(a) _ksba_xmalloc((a)) |
| 61 |
#define xcalloc(a,b) _ksba_xcalloc((a),(b)) |
| 62 |
#define xrealloc(a,b) _ksba_xrealloc((a),(b)) |
| 63 |
#define xstrdup(a) _ksba_xstrdup((a)) |
| 64 |
|
| 65 |
|
| 66 |
#define DIM(v) (sizeof(v)/sizeof((v)[0])) |
| 67 |
#define DIMof(type,member) DIM(((type *)0)->member) |
| 68 |
#ifndef STR |
| 69 |
# define STR(v) #v |
| 70 |
#endif |
| 71 |
#ifndef STR2 |
| 72 |
# define STR2(v) STR(v) |
| 73 |
#endif |
| 74 |
|
| 75 |
#define return_if_fail(expr) do { \ |
| 76 |
if (!(expr)) { \ |
| 77 |
fprintf (stderr, "%s:%d: assertion `%s' failed\n", \ |
| 78 |
__FILE__, __LINE__, #expr ); \ |
| 79 |
return; \ |
| 80 |
} } while (0) |
| 81 |
#define return_null_if_fail(expr) do { \ |
| 82 |
if (!(expr)) { \ |
| 83 |
fprintf (stderr, "%s:%d: assertion `%s' failed\n", \ |
| 84 |
__FILE__, __LINE__, #expr ); \ |
| 85 |
return NULL; \ |
| 86 |
} } while (0) |
| 87 |
#define return_val_if_fail(expr,val) do { \ |
| 88 |
if (!(expr)) { \ |
| 89 |
fprintf (stderr, "%s:%d: assertion `%s' failed\n", \ |
| 90 |
__FILE__, __LINE__, #expr ); \ |
| 91 |
return (val); \ |
| 92 |
} } while (0) |
| 93 |
#define never_reached() do { \ |
| 94 |
fprintf (stderr, "%s:%d: oops; should never get here\n", \ |
| 95 |
__FILE__, __LINE__ ); \ |
| 96 |
} while (0) |
| 97 |
|
| 98 |
|
| 99 |
#ifndef HAVE_STPCPY |
| 100 |
char *_ksba_stpcpy (char *a, const char *b); |
| 101 |
#define stpcpy(a,b) _ksba_stpcpy ((a), (b)) |
| 102 |
#endif |
| 103 |
|
| 104 |
int _ksba_ascii_memcasecmp (const void *a_arg, const void *b_arg, size_t n); |
| 105 |
#define ascii_memcasecmp(a,b,n) _ksba_ascii_memcasecmp ((a),(b),(n)) |
| 106 |
|
| 107 |
/* some macros to replace ctype ones and avoid locale problems */ |
| 108 |
#define spacep(p) (*(p) == ' ' || *(p) == '\t') |
| 109 |
#define digitp(p) (*(p) >= '0' && *(p) <= '9') |
| 110 |
#define hexdigitp(a) (digitp (a) \ |
| 111 |
|| (*(a) >= 'A' && *(a) <= 'F') \ |
| 112 |
|| (*(a) >= 'a' && *(a) <= 'f')) |
| 113 |
/* the atoi macros assume that the buffer has only valid digits */ |
| 114 |
#define atoi_1(p) (*(p) - '0' ) |
| 115 |
#define atoi_2(p) ((atoi_1(p) * 10) + atoi_1((p)+1)) |
| 116 |
#define atoi_4(p) ((atoi_2(p) * 100) + atoi_2((p)+2)) |
| 117 |
#define xtoi_1(p) (*(p) <= '9'? (*(p)- '0'): \ |
| 118 |
*(p) <= 'F'? (*(p)-'A'+10):(*(p)-'a'+10)) |
| 119 |
#define xtoi_2(p) ((xtoi_1(p) * 16) + xtoi_1((p)+1)) |
| 120 |
|
| 121 |
#endif /* UTIL_H */ |