| 1 | /* |
| 2 | * Dolda Connect - Modular multiuser Direct Connect-style client |
| 3 | * Copyright (C) 2004 Fredrik Tolf (fredrik@dolda2000.com) |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | */ |
| 19 | #ifndef _UTILS_H |
| 20 | #define _UTILS_H |
| 21 | |
| 22 | #include <stdarg.h> |
| 23 | #include <stdlib.h> |
| 24 | #ifdef DAEMON |
| 25 | #include "log.h" |
| 26 | #endif |
| 27 | |
| 28 | struct wcspair { |
| 29 | struct wcspair *next; |
| 30 | wchar_t *key; |
| 31 | wchar_t *val; |
| 32 | }; |
| 33 | |
| 34 | |
| 35 | /* "Safe" functions */ |
| 36 | #ifdef DAEMON |
| 37 | #define LOGOOM(size) flog(LOG_CRIT, "%s (%s:%i): out of memory (alloc %i)", __FUNCTION__, __FILE__, __LINE__, (size)) |
| 38 | #define smalloc(size) ({void *__result__; ((__result__ = malloc(size)) == NULL)?({LOGOOM(size); abort(); (void *)0;}):__result__;}) |
| 39 | #define srealloc(ptr, size) ({void *__result__; ((__result__ = realloc((ptr), (size))) == NULL)?({LOGOOM(size); abort(); (void *)0;}):__result__;}) |
| 40 | #define swcsdup(wcs) ((wchar_t *)wcscpy(smalloc(sizeof(wchar_t) * (wcslen(wcs) + 1)), (wcs))) |
| 41 | #define sstrdup(str) ((char *)strcpy(smalloc(strlen(str) + 1), (str))) |
| 42 | #else |
| 43 | #define LOGOOM(size) |
| 44 | #define smalloc(size) ({void *__result__; ((__result__ = malloc(size)) == NULL)?({exit(-1); (void *)0;}):__result__;}) |
| 45 | #define srealloc(ptr, size) ({void *__result__; ((__result__ = realloc((ptr), (size))) == NULL)?({exit(-1); (void *)0;}):__result__;}) |
| 46 | #define swcsdup(wcs) ((wchar_t *)wcscpy(smalloc(sizeof(wchar_t) * (wcslen(wcs) + 1)), (wcs))) |
| 47 | #define sstrdup(str) ((char *)strcpy(smalloc(strlen(str) + 1), (str))) |
| 48 | #endif |
| 49 | |
| 50 | #define CBCHAIN(name, args...) \ |
| 51 | struct cbchain_ ## name { \ |
| 52 | struct cbchain_ ## name *next, *prev; \ |
| 53 | int (*func)(args, void *data); \ |
| 54 | void (*destroy)(void *data); \ |
| 55 | void *data; \ |
| 56 | } * name |
| 57 | |
| 58 | #define GCBCHAIN(name, args...) \ |
| 59 | struct cbchain_ ## name * name = NULL |
| 60 | |
| 61 | #define EGCBCHAIN(name, args...) \ |
| 62 | extern struct cbchain_ ## name { \ |
| 63 | struct cbchain_ ## name *next, *prev; \ |
| 64 | int (*func)(args, void *data); \ |
| 65 | void *data; \ |
| 66 | } * name |
| 67 | |
| 68 | extern int vswprintf (wchar_t *__restrict __s, size_t __n, |
| 69 | __const wchar_t *__restrict __format, |
| 70 | __gnuc_va_list __arg); |
| 71 | extern int swprintf (wchar_t *__restrict __s, size_t __n, |
| 72 | __const wchar_t *__restrict __format, ...); |
| 73 | |
| 74 | char *vsprintf2(char *format, va_list al); |
| 75 | char *sprintf2(char *format, ...) |
| 76 | #if defined(__GNUC__) && 0 |
| 77 | __attribute__ ((format (printf, 1, 2))) |
| 78 | #endif |
| 79 | ; |
| 80 | wchar_t *vswprintf2(wchar_t *format, va_list al); |
| 81 | wchar_t *swprintf2(wchar_t *format, ...); |
| 82 | int havecharset(char *charset); |
| 83 | wchar_t *icmbstowcs(char *mbs, char *charset); |
| 84 | wchar_t *icsmbstowcs(char *mbs, char *charset, wchar_t *def); |
| 85 | char *icwcstombs(wchar_t *wcs, char *charset); |
| 86 | char *icswcstombs(wchar_t *wcs, char *charset, char *def); |
| 87 | wchar_t *wcstolower(wchar_t *wcs); |
| 88 | wchar_t ucptowc(int ucp); |
| 89 | void _sizebuf(void **buf, size_t *bufsize, size_t reqsize, size_t elsize, int algo); |
| 90 | double ntime(void); |
| 91 | int wcsexists(wchar_t *h, wchar_t *n); |
| 92 | #ifndef HAVE_WCSCASECMP |
| 93 | int wcscasecmp(const wchar_t *s1, const wchar_t *s2); |
| 94 | #endif |
| 95 | char *hexencode(char *data, size_t datalen); |
| 96 | char *hexdecode(char *data, size_t *len); |
| 97 | char *base64encode(char *data, size_t datalen); |
| 98 | char *base64decode(char *data, size_t *datalen); |
| 99 | char *base32encode(char *data, size_t datalen); |
| 100 | char *base32decode(char *data, size_t *datalen); |
| 101 | void _freeparr(void **arr); |
| 102 | int _parrlen(void **arr); |
| 103 | char *findfile(char *name, char *homedir, int filldef); |
| 104 | struct wcspair *newwcspair(wchar_t *key, wchar_t *val, struct wcspair **list); |
| 105 | void freewcspair(struct wcspair *pair, struct wcspair **list); |
| 106 | wchar_t *wpfind(struct wcspair *list, wchar_t *key); |
| 107 | |
| 108 | #define sizebuf(b, bs, rs, es, a) _sizebuf((void **)(b), (bs), (rs), (es), (a)) |
| 109 | #define sizebuf2(b, rs, a) _sizebuf((void **)(&(b)), &(b ## size), (rs), sizeof(*(b)), (a)) |
| 110 | #define addtobuf(b, c) \ |
| 111 | do { \ |
| 112 | _sizebuf((void **)(&(b)), &(b ## size), (b ## data) + 1, sizeof(*(b)), 1); \ |
| 113 | (b)[(b ## data)++] = (c); \ |
| 114 | } while(0) |
| 115 | #define bufcat(d, s, n) \ |
| 116 | do { \ |
| 117 | size_t __bufcat_size__; \ |
| 118 | __bufcat_size__ = (n); \ |
| 119 | _sizebuf((void **)(&(d)), &(d ## size), (d ## data) + __bufcat_size__, sizeof(*(d)), 1); \ |
| 120 | memcpy((d) + (d ## data), (s), sizeof(*(d)) * __bufcat_size__); \ |
| 121 | (d ## data) += __bufcat_size__; \ |
| 122 | } while (0) |
| 123 | |
| 124 | #define freeparr(parr) _freeparr((void **)(parr)) |
| 125 | #define parrlen(parr) _parrlen((void **)(parr)) |
| 126 | |
| 127 | #define CBREG(obj, name, funca, destroya, dataa) \ |
| 128 | do { \ |
| 129 | struct cbchain_ ## name *__new_cb__; \ |
| 130 | __new_cb__ = smalloc(sizeof(*__new_cb__)); \ |
| 131 | __new_cb__->func = funca; \ |
| 132 | __new_cb__->destroy = destroya; \ |
| 133 | __new_cb__->data = dataa; \ |
| 134 | __new_cb__->prev = NULL; \ |
| 135 | __new_cb__->next = (obj)->name; \ |
| 136 | if((obj)->name != NULL) { \ |
| 137 | (obj)->name->prev = __new_cb__; \ |
| 138 | } \ |
| 139 | (obj)->name = __new_cb__; \ |
| 140 | } while(0) |
| 141 | |
| 142 | #define CBUNREG(obj, name, dataa) \ |
| 143 | do { \ |
| 144 | struct cbchain_ ## name *__cur__; \ |
| 145 | for(__cur__ = (obj)->name; __cur__ != NULL; __cur__ = __cur__->next) { \ |
| 146 | if(__cur__->data == (dataa)) { \ |
| 147 | if(__cur__->destroy != NULL) \ |
| 148 | __cur__->destroy(__cur__->data); \ |
| 149 | if(__cur__->prev != NULL) \ |
| 150 | __cur__->prev->next = __cur__->next; \ |
| 151 | if(__cur__->next != NULL) \ |
| 152 | __cur__->next->prev = __cur__->prev; \ |
| 153 | if(__cur__ == (obj)->name) \ |
| 154 | (obj)->name = __cur__->next; \ |
| 155 | free(__cur__); \ |
| 156 | break; \ |
| 157 | } \ |
| 158 | } \ |
| 159 | } while(0) |
| 160 | |
| 161 | #define GCBREG(name, funca, dataa) \ |
| 162 | do { \ |
| 163 | struct cbchain_ ## name *__new_cb__; \ |
| 164 | __new_cb__ = smalloc(sizeof(*__new_cb__)); \ |
| 165 | __new_cb__->func = funca; \ |
| 166 | __new_cb__->data = dataa; \ |
| 167 | __new_cb__->prev = NULL; \ |
| 168 | __new_cb__->next = name; \ |
| 169 | if(name != NULL) { \ |
| 170 | name->prev = __new_cb__; \ |
| 171 | } \ |
| 172 | name = __new_cb__; \ |
| 173 | } while(0) |
| 174 | |
| 175 | #define CBCHAININIT(obj, name) (obj)->name = NULL |
| 176 | |
| 177 | #define CBCHAINFREE(obj, name) \ |
| 178 | do { \ |
| 179 | struct cbchain_ ## name *__cur__; \ |
| 180 | while((__cur__ = (obj)->name) != NULL) { \ |
| 181 | (obj)->name = __cur__->next; \ |
| 182 | if(__cur__->destroy != NULL) \ |
| 183 | __cur__->destroy(__cur__->data); \ |
| 184 | free(__cur__); \ |
| 185 | } \ |
| 186 | } while(0) |
| 187 | |
| 188 | #define CBCHAINDOCB(obj, name, args...) \ |
| 189 | do { \ |
| 190 | struct cbchain_ ## name *__cur__, *__next__; \ |
| 191 | for(__cur__ = (obj)->name; __cur__ != NULL; __cur__ = __next__) { \ |
| 192 | __next__ = __cur__->next; \ |
| 193 | if(__cur__->func(args, __cur__->data)) { \ |
| 194 | if(__cur__->next != NULL) \ |
| 195 | __cur__->next->prev = __cur__->prev; \ |
| 196 | if(__cur__->prev != NULL) \ |
| 197 | __cur__->prev->next = __cur__->next; \ |
| 198 | if(__cur__ == (obj)->name) \ |
| 199 | (obj)->name = __cur__->next; \ |
| 200 | free(__cur__); \ |
| 201 | } \ |
| 202 | } \ |
| 203 | } while(0) |
| 204 | |
| 205 | #define GCBCHAINDOCB(name, args...) \ |
| 206 | ({ \ |
| 207 | struct cbchain_ ## name *__cur__; \ |
| 208 | int __ret__; \ |
| 209 | __ret__ = 0; \ |
| 210 | for(__cur__ = name; __cur__ != NULL; __cur__ = __cur__->next) { \ |
| 211 | if(__cur__->func(args, __cur__->data)) { \ |
| 212 | __ret__ = 1; \ |
| 213 | break; \ |
| 214 | } \ |
| 215 | } \ |
| 216 | __ret__; \ |
| 217 | }) |
| 218 | |
| 219 | #endif |