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
35 /* "Safe" functions */
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)))
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)))
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); \
58 #define GCBCHAIN(name, args...) \
59 struct cbchain_ ## name * name = NULL
61 #define EGCBCHAIN(name, args...) \
62 extern struct cbchain_ ## name { \
63 struct cbchain_ ## name *next, *prev; \
64 int (*func)(args, void *data); \
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, ...);
74 char *vsprintf2(char *format, va_list al);
75 char *sprintf2(char *format, ...)
76 #if defined(__GNUC__) && 0
77 __attribute__ ((format (printf, 1, 2)))
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);
91 int wcsexists(wchar_t *h, wchar_t *n);
92 #ifndef HAVE_WCSCASECMP
93 int wcscasecmp(const wchar_t *s1, const wchar_t *s2);
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);
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) \
112 _sizebuf((void **)(&(b)), &(b ## size), (b ## data) + 1, sizeof(*(b)), 1); \
113 (b)[(b ## data)++] = (c); \
115 #define bufcat(d, s, n) \
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__; \
124 #define freeparr(parr) _freeparr((void **)(parr))
125 #define parrlen(parr) _parrlen((void **)(parr))
127 #define CBREG(obj, name, funca, destroya, dataa) \
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__; \
139 (obj)->name = __new_cb__; \
142 #define CBUNREG(obj, name, dataa) \
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; \
161 #define GCBREG(name, funca, dataa) \
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; \
170 name->prev = __new_cb__; \
175 #define CBCHAININIT(obj, name) (obj)->name = NULL
177 #define CBCHAINFREE(obj, name) \
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); \
188 #define CBCHAINDOCB(obj, name, args...) \
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; \
205 #define GCBCHAINDOCB(name, args...) \
207 struct cbchain_ ## name *__cur__; \
210 for(__cur__ = name; __cur__ != NULL; __cur__ = __cur__->next) { \
211 if(__cur__->func(args, __cur__->data)) { \