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
27 /* "Safe" functions */
28 #define smalloc(size) ({void *__result__; ((__result__ = malloc(size)) == NULL)?({LOGOOM(size); abort(); (void *)0;}):__result__;})
29 #define srealloc(ptr, size) ({void *__result__; ((__result__ = realloc((ptr), (size))) == NULL)?({LOGOOM(size); abort(); (void *)0;}):__result__;})
30 #define swcsdup(wcs) ((wchar_t *)wcscpy(smalloc(sizeof(wchar_t) * (wcslen(wcs) + 1)), (wcs)))
31 #define sstrdup(str) ((char *)strcpy(smalloc(strlen(str) + 1), (str)))
33 #define CBCHAIN(name, args...) \
34 struct cbchain_ ## name { \
35 struct cbchain_ ## name *next, *prev; \
36 int (*func)(args, void *data); \
37 void (*destroy)(void *data); \
41 #define GCBCHAIN(name, args...) \
42 struct cbchain_ ## name * name = NULL
44 #define EGCBCHAIN(name, args...) \
45 extern struct cbchain_ ## name { \
46 struct cbchain_ ## name *next, *prev; \
47 int (*func)(args, void *data); \
51 extern int vswprintf (wchar_t *__restrict __s, size_t __n,
52 __const wchar_t *__restrict __format,
53 __gnuc_va_list __arg);
54 extern int swprintf (wchar_t *__restrict __s, size_t __n,
55 __const wchar_t *__restrict __format, ...);
57 char *vsprintf2(char *format, va_list al);
58 char *sprintf2(char *format, ...)
59 #if defined(__GNUC__) && 0
60 __attribute__ ((format (printf, 1, 2)))
64 wchar_t *vswprintf2(wchar_t *format, va_list al);
65 wchar_t *swprintf2(wchar_t *format, ...);
66 wchar_t *icmbstowcs(char *mbs, char *charset);
67 wchar_t *icsmbstowcs(char *mbs, char *charset, wchar_t *def);
68 char *icwcstombs(wchar_t *wcs, char *charset);
69 char *icswcstombs(wchar_t *wcs, char *charset, char *def);
70 wchar_t *wcstolower(wchar_t *wcs);
71 wchar_t ucptowc(int ucp);
72 void _sizebuf(void **buf, size_t *bufsize, size_t reqsize, size_t elsize, int algo);
74 int wcsexists(wchar_t *h, wchar_t *n);
75 #ifndef HAVE_WCSCASECMP
76 int wcscasecmp(const wchar_t *s1, const wchar_t *s2);
78 char *hexencode(char *data, size_t datalen);
79 char *hexdecode(char *data, size_t *len);
80 char *base64encode(char *data, size_t datalen);
81 char *base64decode(char *data, size_t *datalen);
82 char *base32encode(char *data, size_t datalen);
83 char *base32decode(char *data, size_t *datalen);
84 void _freeparr(void **arr);
85 int _parrlen(void **arr);
86 char *findfile(char *gname, char *uname, char *homedir);
88 #define sizebuf(b, bs, rs, es, a) _sizebuf((void **)(b), (bs), (rs), (es), (a))
89 #define sizebuf2(b, rs, a) _sizebuf((void **)(&(b)), &(b ## size), (rs), sizeof(*(b)), (a))
90 #define addtobuf(b, c) \
92 _sizebuf((void **)(&(b)), &(b ## size), (b ## data) + 1, sizeof(*(b)), 1); \
93 (b)[(b ## data)++] = (c); \
95 #define bufcat(d, s, n) \
97 size_t __bufcat_size__; \
98 __bufcat_size__ = (n); \
99 _sizebuf((void **)(&(d)), &(d ## size), (d ## data) + __bufcat_size__, sizeof(*(d)), 1); \
100 memcpy((d) + (d ## data), (s), sizeof(*(d)) * __bufcat_size__); \
101 (d ## data) += __bufcat_size__; \
104 #define freeparr(parr) _freeparr((void **)(parr))
105 #define parrlen(parr) _parrlen((void **)(parr))
107 #define CBREG(obj, name, funca, destroya, dataa) \
109 struct cbchain_ ## name *__new_cb__; \
110 __new_cb__ = smalloc(sizeof(*__new_cb__)); \
111 __new_cb__->func = funca; \
112 __new_cb__->destroy = destroya; \
113 __new_cb__->data = dataa; \
114 __new_cb__->prev = NULL; \
115 __new_cb__->next = (obj)->name; \
116 (obj)->name = __new_cb__; \
119 #define CBUNREG(obj, name, dataa) \
121 struct cbchain_ ## name *__cur__; \
122 for(__cur__ = (obj)->name; __cur__ != NULL; __cur__ = __cur__->next) { \
123 if(__cur__->data == (dataa)) { \
124 if(__cur__->destroy != NULL) \
125 __cur__->destroy(__cur__->data); \
126 if(__cur__->prev != NULL) \
127 __cur__->prev->next = __cur__->next; \
128 if(__cur__->next != NULL) \
129 __cur__->next->prev = __cur__->prev; \
130 if(__cur__ == (obj)->name) \
131 (obj)->name = __cur__->next; \
138 #define GCBREG(name, funca, dataa) \
140 struct cbchain_ ## name *__new_cb__; \
141 __new_cb__ = smalloc(sizeof(*__new_cb__)); \
142 __new_cb__->func = funca; \
143 __new_cb__->data = dataa; \
144 __new_cb__->prev = NULL; \
145 __new_cb__->next = name; \
149 #define CBCHAININIT(obj, name) (obj)->name = NULL
151 #define CBCHAINFREE(obj, name) \
153 struct cbchain_ ## name *__cur__; \
154 while((__cur__ = (obj)->name) != NULL) { \
155 (obj)->name = __cur__->next; \
156 if(__cur__->destroy != NULL) \
157 __cur__->destroy(__cur__->data); \
162 #define CBCHAINDOCB(obj, name, args...) \
164 struct cbchain_ ## name *__cur__, *__next__; \
165 for(__cur__ = (obj)->name; __cur__ != NULL; __cur__ = __next__) { \
166 __next__ = __cur__->next; \
167 if(__cur__->func(args, __cur__->data)) { \
168 if(__cur__->next != NULL) \
169 __cur__->next->prev = __cur__->prev; \
170 if(__cur__->prev != NULL) \
171 __cur__->prev->next = __cur__->next; \
172 if(__cur__ == (obj)->name) \
173 (obj)->name = __cur__->next; \
179 #define GCBCHAINDOCB(name, args...) \
181 struct cbchain_ ## name *__cur__; \
184 for(__cur__ = name; __cur__ != NULL; __cur__ = __cur__->next) { \
185 if(__cur__->func(args, __cur__->data)) { \