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
36 /* "Safe" functions */
38 #define LOGOOM(size) flog(LOG_CRIT, "%s (%s:%i): out of memory (alloc %i)", __FUNCTION__, __FILE__, __LINE__, (size))
39 #define smalloc(size) ({void *__result__; ((__result__ = malloc(size)) == NULL)?({LOGOOM(size); abort(); (void *)0;}):__result__;})
40 #define srealloc(ptr, size) ({void *__result__; ((__result__ = realloc((ptr), (size))) == NULL)?({LOGOOM(size); abort(); (void *)0;}):__result__;})
41 #define swcsdup(wcs) ((wchar_t *)wcscpy(smalloc(sizeof(wchar_t) * (wcslen(wcs) + 1)), (wcs)))
42 #define sstrdup(str) ((char *)strcpy(smalloc(strlen(str) + 1), (str)))
45 #define smalloc(size) ({void *__result__; ((__result__ = malloc(size)) == NULL)?({exit(-1); (void *)0;}):__result__;})
46 #define srealloc(ptr, size) ({void *__result__; ((__result__ = realloc((ptr), (size))) == NULL)?({exit(-1); (void *)0;}):__result__;})
47 #define swcsdup(wcs) ((wchar_t *)wcscpy(smalloc(sizeof(wchar_t) * (wcslen(wcs) + 1)), (wcs)))
48 #define sstrdup(str) ((char *)strcpy(smalloc(strlen(str) + 1), (str)))
51 #define CBCHAIN(name, args...) \
52 struct cbchain_ ## name { \
53 struct cbchain_ ## name *next, *prev; \
54 int (*func)(args, void *data); \
55 void (*destroy)(void *data); \
59 #define GCBCHAIN(name, args...) \
60 struct cbchain_ ## name * name = NULL
62 #define EGCBCHAIN(name, args...) \
63 extern struct cbchain_ ## name { \
64 struct cbchain_ ## name *next, *prev; \
65 int (*func)(args, void *data); \
69 extern int vswprintf (wchar_t *__restrict __s, size_t __n,
70 __const wchar_t *__restrict __format,
71 __gnuc_va_list __arg);
72 extern int swprintf (wchar_t *__restrict __s, size_t __n,
73 __const wchar_t *__restrict __format, ...);
75 char *vsprintf2(char *format, va_list al);
76 char *sprintf2(char *format, ...)
77 #if defined(__GNUC__) && 0
78 __attribute__ ((format (printf, 1, 2)))
81 wchar_t *vswprintf2(wchar_t *format, va_list al);
82 wchar_t *swprintf2(wchar_t *format, ...);
83 int havecharset(char *charset);
84 wchar_t *icmbstowcs(char *mbs, char *charset);
85 wchar_t *icsmbstowcs(char *mbs, char *charset, wchar_t *def);
86 char *icwcstombs(wchar_t *wcs, char *charset);
87 char *icswcstombs(wchar_t *wcs, char *charset, char *def);
88 wchar_t *wcstolower(wchar_t *wcs);
89 wchar_t ucptowc(int ucp);
90 void _sizebuf(void **buf, size_t *bufsize, size_t reqsize, size_t elsize, int algo);
92 int wcsexists(wchar_t *h, wchar_t *n);
93 #ifndef HAVE_WCSCASECMP
94 int wcscasecmp(const wchar_t *s1, const wchar_t *s2);
96 char *hexencode(char *data, size_t datalen);
97 char *hexdecode(char *data, size_t *len);
98 char *base64encode(char *data, size_t datalen);
99 char *base64decode(char *data, size_t *datalen);
100 char *base32encode(char *data, size_t datalen);
101 char *base32decode(char *data, size_t *datalen);
102 void _freeparr(void **arr);
103 int _parrlen(void **arr);
104 char *findfile(char *gname, char *uname, char *homedir, int filldef);
105 struct wcspair *newwcspair(wchar_t *key, wchar_t *val, struct wcspair **list);
106 void freewcspair(struct wcspair *pair, struct wcspair **list);
107 wchar_t *wpfind(struct wcspair *list, wchar_t *key);
109 #define sizebuf(b, bs, rs, es, a) _sizebuf((void **)(b), (bs), (rs), (es), (a))
110 #define sizebuf2(b, rs, a) _sizebuf((void **)(&(b)), &(b ## size), (rs), sizeof(*(b)), (a))
111 #define addtobuf(b, c) \
113 _sizebuf((void **)(&(b)), &(b ## size), (b ## data) + 1, sizeof(*(b)), 1); \
114 (b)[(b ## data)++] = (c); \
116 #define bufcat(d, s, n) \
118 size_t __bufcat_size__; \
119 __bufcat_size__ = (n); \
120 _sizebuf((void **)(&(d)), &(d ## size), (d ## data) + __bufcat_size__, sizeof(*(d)), 1); \
121 memcpy((d) + (d ## data), (s), sizeof(*(d)) * __bufcat_size__); \
122 (d ## data) += __bufcat_size__; \
125 #define freeparr(parr) _freeparr((void **)(parr))
126 #define parrlen(parr) _parrlen((void **)(parr))
128 #define CBREG(obj, name, funca, destroya, dataa) \
130 struct cbchain_ ## name *__new_cb__; \
131 __new_cb__ = smalloc(sizeof(*__new_cb__)); \
132 __new_cb__->func = funca; \
133 __new_cb__->destroy = destroya; \
134 __new_cb__->data = dataa; \
135 __new_cb__->prev = NULL; \
136 __new_cb__->next = (obj)->name; \
137 if((obj)->name != NULL) { \
138 (obj)->name->prev = __new_cb__; \
140 (obj)->name = __new_cb__; \
143 #define CBUNREG(obj, name, dataa) \
145 struct cbchain_ ## name *__cur__; \
146 for(__cur__ = (obj)->name; __cur__ != NULL; __cur__ = __cur__->next) { \
147 if(__cur__->data == (dataa)) { \
148 if(__cur__->destroy != NULL) \
149 __cur__->destroy(__cur__->data); \
150 if(__cur__->prev != NULL) \
151 __cur__->prev->next = __cur__->next; \
152 if(__cur__->next != NULL) \
153 __cur__->next->prev = __cur__->prev; \
154 if(__cur__ == (obj)->name) \
155 (obj)->name = __cur__->next; \
162 #define GCBREG(name, funca, dataa) \
164 struct cbchain_ ## name *__new_cb__; \
165 __new_cb__ = smalloc(sizeof(*__new_cb__)); \
166 __new_cb__->func = funca; \
167 __new_cb__->data = dataa; \
168 __new_cb__->prev = NULL; \
169 __new_cb__->next = name; \
171 name->prev = __new_cb__; \
176 #define CBCHAININIT(obj, name) (obj)->name = NULL
178 #define CBCHAINFREE(obj, name) \
180 struct cbchain_ ## name *__cur__; \
181 while((__cur__ = (obj)->name) != NULL) { \
182 (obj)->name = __cur__->next; \
183 if(__cur__->destroy != NULL) \
184 __cur__->destroy(__cur__->data); \
189 #define CBCHAINDOCB(obj, name, args...) \
191 struct cbchain_ ## name *__cur__, *__next__; \
192 for(__cur__ = (obj)->name; __cur__ != NULL; __cur__ = __next__) { \
193 __next__ = __cur__->next; \
194 if(__cur__->func(args, __cur__->data)) { \
195 if(__cur__->next != NULL) \
196 __cur__->next->prev = __cur__->prev; \
197 if(__cur__->prev != NULL) \
198 __cur__->prev->next = __cur__->next; \
199 if(__cur__ == (obj)->name) \
200 (obj)->name = __cur__->next; \
206 #define GCBCHAINDOCB(name, args...) \
208 struct cbchain_ ## name *__cur__; \
211 for(__cur__ = name; __cur__ != NULL; __cur__ = __cur__->next) { \
212 if(__cur__->func(args, __cur__->data)) { \