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 extern int vswprintf (wchar_t *__restrict __s, size_t __n,
74 __const wchar_t *__restrict __format,
75 __gnuc_va_list __arg);
76 extern int swprintf (wchar_t *__restrict __s, size_t __n,
77 __const wchar_t *__restrict __format, ...);
79 char *vsprintf2(char *format, va_list al);
80 char *sprintf2(char *format, ...)
82 __attribute__ ((format (printf, 1, 2)))
85 wchar_t *vswprintf2(wchar_t *format, va_list al);
86 wchar_t *swprintf2(wchar_t *format, ...);
87 int havecharset(char *charset);
88 wchar_t *icmbstowcs(char *mbs, char *charset);
89 wchar_t *icsmbstowcs(char *mbs, char *charset, wchar_t *def);
90 char *icwcstombs(wchar_t *wcs, char *charset);
91 char *icswcstombs(wchar_t *wcs, char *charset, char *def);
92 wchar_t *wcstolower(wchar_t *wcs);
93 wchar_t ucptowc(int ucp);
94 void _sizebuf(void **buf, size_t *bufsize, size_t reqsize, size_t elsize, int algo);
96 int wcsexists(wchar_t *h, wchar_t *n);
97 #ifndef HAVE_WCSCASECMP
98 int wcscasecmp(const wchar_t *s1, const wchar_t *s2);
100 char *hexencode(char *data, size_t datalen);
101 char *hexdecode(char *data, size_t *len);
102 char *base64encode(char *data, size_t datalen);
103 char *base64decode(char *data, size_t *datalen);
104 char *base32encode(char *data, size_t datalen);
105 char *base32decode(char *data, size_t *datalen);
106 void _freeparr(void **arr);
107 int _parrlen(void **arr);
108 char *findfile(char *name, char *homedir, int filldef);
109 struct strpair *newstrpair(char *key, char *val, struct strpair **list);
110 void freestrpair(struct strpair *pair, struct strpair **list);
111 char *spfind(struct strpair *list, char *key);
112 struct wcspair *newwcspair(wchar_t *key, wchar_t *val, struct wcspair **list);
113 void freewcspair(struct wcspair *pair, struct wcspair **list);
114 wchar_t *wpfind(struct wcspair *list, wchar_t *key);
116 #define sizebuf(b, bs, rs, es, a) _sizebuf((void **)(b), (bs), (rs), (es), (a))
117 #define sizebuf2(b, rs, a) _sizebuf((void **)(&(b)), &(b ## size), (rs), sizeof(*(b)), (a))
118 #define addtobuf(b, c) \
120 _sizebuf((void **)(&(b)), &(b ## size), (b ## data) + 1, sizeof(*(b)), 1); \
121 (b)[(b ## data)++] = (c); \
123 #define bufcat(d, s, n) \
125 size_t __bufcat_size__; \
126 __bufcat_size__ = (n); \
127 _sizebuf((void **)(&(d)), &(d ## size), (d ## data) + __bufcat_size__, sizeof(*(d)), 1); \
128 memcpy((d) + (d ## data), (s), sizeof(*(d)) * __bufcat_size__); \
129 (d ## data) += __bufcat_size__; \
131 #define bprintf(b, fmt...) \
133 char *__bprintf_dest__; \
134 __bprintf_dest__ = sprintf2(fmt); \
135 bufcat(b, __bprintf_dest__, strlen(__bprintf_dest__)); \
136 free(__bprintf_dest__); \
139 #define freeparr(parr) _freeparr((void **)(parr))
140 #define parrlen(parr) _parrlen((void **)(parr))
142 #define CBREG(obj, name, funca, destroya, dataa) \
144 struct cbchain_ ## name *__new_cb__; \
145 __new_cb__ = smalloc(sizeof(*__new_cb__)); \
146 __new_cb__->func = funca; \
147 __new_cb__->destroy = destroya; \
148 __new_cb__->data = dataa; \
149 __new_cb__->prev = NULL; \
150 __new_cb__->next = (obj)->name; \
151 if((obj)->name != NULL) { \
152 (obj)->name->prev = __new_cb__; \
154 (obj)->name = __new_cb__; \
157 #define CBUNREG(obj, name, dataa) \
159 struct cbchain_ ## name *__cur__; \
160 for(__cur__ = (obj)->name; __cur__ != NULL; __cur__ = __cur__->next) { \
161 if(__cur__->data == (dataa)) { \
162 if(__cur__->destroy != NULL) \
163 __cur__->destroy(__cur__->data); \
164 if(__cur__->prev != NULL) \
165 __cur__->prev->next = __cur__->next; \
166 if(__cur__->next != NULL) \
167 __cur__->next->prev = __cur__->prev; \
168 if(__cur__ == (obj)->name) \
169 (obj)->name = __cur__->next; \
176 #define GCBREG(name, funca, dataa) \
178 struct cbchain_ ## name *__new_cb__; \
179 __new_cb__ = smalloc(sizeof(*__new_cb__)); \
180 __new_cb__->func = funca; \
181 __new_cb__->data = dataa; \
182 __new_cb__->prev = NULL; \
183 __new_cb__->next = name; \
185 name->prev = __new_cb__; \
190 #define CBCHAININIT(obj, name) (obj)->name = NULL
192 #define CBCHAINFREE(obj, name) \
194 struct cbchain_ ## name *__cur__; \
195 while((__cur__ = (obj)->name) != NULL) { \
196 (obj)->name = __cur__->next; \
197 if(__cur__->destroy != NULL) \
198 __cur__->destroy(__cur__->data); \
203 #define CBCHAINDOCB(obj, name, args...) \
205 struct cbchain_ ## name *__cur__, *__next__; \
206 for(__cur__ = (obj)->name; __cur__ != NULL; __cur__ = __next__) { \
207 __next__ = __cur__->next; \
208 if(__cur__->func(args, __cur__->data)) { \
209 if(__cur__->next != NULL) \
210 __cur__->next->prev = __cur__->prev; \
211 if(__cur__->prev != NULL) \
212 __cur__->prev->next = __cur__->next; \
213 if(__cur__ == (obj)->name) \
214 (obj)->name = __cur__->next; \
220 #define GCBCHAINDOCB(name, args...) \
222 struct cbchain_ ## name *__cur__; \
225 for(__cur__ = name; __cur__ != NULL; __cur__ = __cur__->next) { \
226 if(__cur__->func(args, __cur__->data)) { \