2 * Dolda Connect - Modular multiuser Direct Connect-style client
3 * Copyright (C) 2004 Fredrik Tolf <fredrik@dolda2000.com>
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.
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.
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
40 /* "Safe" functions */
42 #define LOGOOM(size) flog(LOG_CRIT, "%s (%s:%i): out of memory (alloc %zi)", __FUNCTION__, __FILE__, __LINE__, (size))
43 #define smalloc(size) ({void *__result__; ((__result__ = malloc(size)) == NULL)?({LOGOOM((ssize_t)(size)); abort(); (void *)0;}):__result__;})
44 #define srealloc(ptr, size) ({void *__result__; ((__result__ = realloc((ptr), (size))) == NULL)?({LOGOOM((ssize_t)(size)); abort(); (void *)0;}):__result__;})
45 #define swcsdup(wcs) ({wchar_t *__eval__; __eval__ = (wcs); (wchar_t *)wcscpy(smalloc(sizeof(wchar_t) * (wcslen(__eval__) + 1)), __eval__);})
46 #define sstrdup(str) ((char *)strcpy(smalloc(strlen(str) + 1), (str)))
49 #define smalloc(size) ({void *__result__; ((__result__ = malloc(size)) == NULL)?({exit(-1); (void *)0;}):__result__;})
50 #define srealloc(ptr, size) ({void *__result__; ((__result__ = realloc((ptr), (size))) == NULL)?({exit(-1); (void *)0;}):__result__;})
51 #define swcsdup(wcs) ((wchar_t *)wcscpy(smalloc(sizeof(wchar_t) * (wcslen(wcs) + 1)), (wcs)))
52 #define sstrdup(str) ((char *)strcpy(smalloc(strlen(str) + 1), (str)))
55 #define CBCHAIN(name, args...) \
56 struct cbchain_ ## name { \
57 struct cbchain_ ## name *next, *prev; \
58 int (*func)(args, void *data); \
59 void (*destroy)(void *data); \
63 #define GCBCHAIN(name, args...) \
64 struct cbchain_ ## name * name = NULL
66 #define EGCBCHAIN(name, args...) \
67 extern struct cbchain_ ## name { \
68 struct cbchain_ ## name *next, *prev; \
69 int (*func)(args, void *data); \
73 char *vsprintf2(char *format, va_list al);
74 char *sprintf2(char *format, ...)
76 __attribute__ ((format (printf, 1, 2)))
79 wchar_t *vswprintf2(wchar_t *format, va_list al);
80 wchar_t *swprintf2(wchar_t *format, ...);
81 int havecharset(char *charset);
82 wchar_t *icmbstowcs(char *mbs, char *charset);
83 wchar_t *icsmbstowcs(char *mbs, char *charset, wchar_t *def);
84 char *icwcstombs(wchar_t *wcs, char *charset);
85 char *icswcstombs(wchar_t *wcs, char *charset, char *def);
86 wchar_t *wcstolower(wchar_t *wcs);
87 wchar_t ucptowc(int ucp);
88 void _sizebuf(void **buf, size_t *bufsize, size_t reqsize, size_t elsize, int algo);
90 int wcsexists(wchar_t *h, wchar_t *n);
91 #ifndef HAVE_WCSCASECMP
92 int wcscasecmp(const wchar_t *s1, const wchar_t *s2);
94 char *hexencode(char *data, size_t datalen);
95 char *hexdecode(char *data, size_t *len);
96 char *base64encode(char *data, size_t datalen);
97 char *base64decode(char *data, size_t *datalen);
98 char *base32encode(char *data, size_t datalen);
99 char *base32decode(char *data, size_t *datalen);
100 void _freeparr(void **arr);
101 int _parrlen(void **arr);
102 char *findfile(char *name, char *homedir, int filldef);
103 struct strpair *newstrpair(char *key, char *val, struct strpair **list);
104 void freestrpair(struct strpair *pair, struct strpair **list);
105 char *spfind(struct strpair *list, char *key);
106 struct wcspair *newwcspair(wchar_t *key, wchar_t *val, struct wcspair **list);
107 void freewcspair(struct wcspair *pair, struct wcspair **list);
108 wchar_t *wpfind(struct wcspair *list, wchar_t *key);
110 #define sizebuf(b, bs, rs, es, a) _sizebuf((void **)(void *)(b), (bs), (rs), (es), (a))
111 #define sizebuf2(b, rs, a) _sizebuf((void **)(void *)(&(b)), &(b ## size), (rs), sizeof(*(b)), (a))
112 #define addtobuf(b, c) \
114 _sizebuf((void **)(void *)(&(b)), &(b ## size), (b ## data) + 1, sizeof(*(b)), 1); \
115 (b)[(b ## data)++] = (c); \
117 #define bufcat(d, s, n) \
119 size_t __bufcat_size__; \
120 __bufcat_size__ = (n); \
121 _sizebuf((void **)(void *)(&(d)), &(d ## size), (d ## data) + __bufcat_size__, sizeof(*(d)), 1); \
122 memcpy((d) + (d ## data), (s), sizeof(*(d)) * __bufcat_size__); \
123 (d ## data) += __bufcat_size__; \
125 #define bprintf(b, fmt...) \
127 char *__bprintf_dest__; \
128 __bprintf_dest__ = sprintf2(fmt); \
129 bufcat(b, __bprintf_dest__, strlen(__bprintf_dest__)); \
130 free(__bprintf_dest__); \
133 #define freeparr(parr) _freeparr((void **)(parr))
134 #define parrlen(parr) _parrlen((void **)(parr))
136 #define CBREG(obj, name, funca, destroya, dataa) \
138 struct cbchain_ ## name *__new_cb__; \
139 __new_cb__ = smalloc(sizeof(*__new_cb__)); \
140 __new_cb__->func = funca; \
141 __new_cb__->destroy = destroya; \
142 __new_cb__->data = dataa; \
143 __new_cb__->prev = NULL; \
144 __new_cb__->next = (obj)->name; \
145 if((obj)->name != NULL) { \
146 (obj)->name->prev = __new_cb__; \
148 (obj)->name = __new_cb__; \
151 #define CBUNREG(obj, name, dataa) \
153 struct cbchain_ ## name *__cur__; \
154 for(__cur__ = (obj)->name; __cur__ != NULL; __cur__ = __cur__->next) { \
155 if(__cur__->data == (dataa)) { \
156 if(__cur__->destroy != NULL) \
157 __cur__->destroy(__cur__->data); \
158 if(__cur__->prev != NULL) \
159 __cur__->prev->next = __cur__->next; \
160 if(__cur__->next != NULL) \
161 __cur__->next->prev = __cur__->prev; \
162 if(__cur__ == (obj)->name) \
163 (obj)->name = __cur__->next; \
170 #define GCBREG(name, funca, dataa) \
172 struct cbchain_ ## name *__new_cb__; \
173 __new_cb__ = smalloc(sizeof(*__new_cb__)); \
174 __new_cb__->func = funca; \
175 __new_cb__->data = dataa; \
176 __new_cb__->prev = NULL; \
177 __new_cb__->next = name; \
179 name->prev = __new_cb__; \
184 #define CBCHAININIT(obj, name) (obj)->name = NULL
186 #define CBCHAINFREE(obj, name) \
188 struct cbchain_ ## name *__cur__; \
189 while((__cur__ = (obj)->name) != NULL) { \
190 (obj)->name = __cur__->next; \
191 if(__cur__->destroy != NULL) \
192 __cur__->destroy(__cur__->data); \
197 #define CBCHAINDOCB(obj, name, args...) \
199 struct cbchain_ ## name *__cur__, *__next__; \
200 for(__cur__ = (obj)->name; __cur__ != NULL; __cur__ = __next__) { \
201 __next__ = __cur__->next; \
202 if(__cur__->func(args, __cur__->data)) { \
203 if(__cur__->next != NULL) \
204 __cur__->next->prev = __cur__->prev; \
205 if(__cur__->prev != NULL) \
206 __cur__->prev->next = __cur__->next; \
207 if(__cur__ == (obj)->name) \
208 (obj)->name = __cur__->next; \
214 #define GCBCHAINDOCB(name, args...) \
216 struct cbchain_ ## name *__cur__; \
219 for(__cur__ = name; __cur__ != NULL; __cur__ = __cur__->next) { \
220 if(__cur__->func(args, __cur__->data)) { \