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
33 /* "Safe" functions */
34 #define smalloc(size) ({void *__result__; ((__result__ = malloc(size)) == NULL)?({LOGOOM(size); abort(); (void *)0;}):__result__;})
35 #define srealloc(ptr, size) ({void *__result__; ((__result__ = realloc((ptr), (size))) == NULL)?({LOGOOM(size); abort(); (void *)0;}):__result__;})
36 #define swcsdup(wcs) ((wchar_t *)wcscpy(smalloc(sizeof(wchar_t) * (wcslen(wcs) + 1)), (wcs)))
37 #define sstrdup(str) ((char *)strcpy(smalloc(strlen(str) + 1), (str)))
39 #define CBCHAIN(name, args...) \
40 struct cbchain_ ## name { \
41 struct cbchain_ ## name *next, *prev; \
42 int (*func)(args, void *data); \
43 void (*destroy)(void *data); \
47 #define GCBCHAIN(name, args...) \
48 struct cbchain_ ## name * name = NULL
50 #define EGCBCHAIN(name, args...) \
51 extern struct cbchain_ ## name { \
52 struct cbchain_ ## name *next, *prev; \
53 int (*func)(args, void *data); \
57 extern int vswprintf (wchar_t *__restrict __s, size_t __n,
58 __const wchar_t *__restrict __format,
59 __gnuc_va_list __arg);
60 extern int swprintf (wchar_t *__restrict __s, size_t __n,
61 __const wchar_t *__restrict __format, ...);
63 char *vsprintf2(char *format, va_list al);
64 char *sprintf2(char *format, ...)
65 #if defined(__GNUC__) && 0
66 __attribute__ ((format (printf, 1, 2)))
69 wchar_t *vswprintf2(wchar_t *format, va_list al);
70 wchar_t *swprintf2(wchar_t *format, ...);
71 wchar_t *icmbstowcs(char *mbs, char *charset);
72 wchar_t *icsmbstowcs(char *mbs, char *charset, wchar_t *def);
73 char *icwcstombs(wchar_t *wcs, char *charset);
74 char *icswcstombs(wchar_t *wcs, char *charset, char *def);
75 wchar_t *wcstolower(wchar_t *wcs);
76 wchar_t ucptowc(int ucp);
77 void _sizebuf(void **buf, size_t *bufsize, size_t reqsize, size_t elsize, int algo);
79 int wcsexists(wchar_t *h, wchar_t *n);
80 #ifndef HAVE_WCSCASECMP
81 int wcscasecmp(const wchar_t *s1, const wchar_t *s2);
83 char *hexencode(char *data, size_t datalen);
84 char *hexdecode(char *data, size_t *len);
85 char *base64encode(char *data, size_t datalen);
86 char *base64decode(char *data, size_t *datalen);
87 char *base32encode(char *data, size_t datalen);
88 char *base32decode(char *data, size_t *datalen);
89 void _freeparr(void **arr);
90 int _parrlen(void **arr);
91 char *findfile(char *gname, char *uname, char *homedir);
92 struct wcspair *newwcspair(wchar_t *key, wchar_t *val, struct wcspair **list);
93 void freewcspair(struct wcspair *pair, struct wcspair **list);
94 wchar_t *wpfind(struct wcspair *list, wchar_t *key);
96 #define sizebuf(b, bs, rs, es, a) _sizebuf((void **)(b), (bs), (rs), (es), (a))
97 #define sizebuf2(b, rs, a) _sizebuf((void **)(&(b)), &(b ## size), (rs), sizeof(*(b)), (a))
98 #define addtobuf(b, c) \
100 _sizebuf((void **)(&(b)), &(b ## size), (b ## data) + 1, sizeof(*(b)), 1); \
101 (b)[(b ## data)++] = (c); \
103 #define bufcat(d, s, n) \
105 size_t __bufcat_size__; \
106 __bufcat_size__ = (n); \
107 _sizebuf((void **)(&(d)), &(d ## size), (d ## data) + __bufcat_size__, sizeof(*(d)), 1); \
108 memcpy((d) + (d ## data), (s), sizeof(*(d)) * __bufcat_size__); \
109 (d ## data) += __bufcat_size__; \
112 #define freeparr(parr) _freeparr((void **)(parr))
113 #define parrlen(parr) _parrlen((void **)(parr))
115 #define CBREG(obj, name, funca, destroya, dataa) \
117 struct cbchain_ ## name *__new_cb__; \
118 __new_cb__ = smalloc(sizeof(*__new_cb__)); \
119 __new_cb__->func = funca; \
120 __new_cb__->destroy = destroya; \
121 __new_cb__->data = dataa; \
122 __new_cb__->prev = NULL; \
123 __new_cb__->next = (obj)->name; \
124 (obj)->name = __new_cb__; \
127 #define CBUNREG(obj, name, dataa) \
129 struct cbchain_ ## name *__cur__; \
130 for(__cur__ = (obj)->name; __cur__ != NULL; __cur__ = __cur__->next) { \
131 if(__cur__->data == (dataa)) { \
132 if(__cur__->destroy != NULL) \
133 __cur__->destroy(__cur__->data); \
134 if(__cur__->prev != NULL) \
135 __cur__->prev->next = __cur__->next; \
136 if(__cur__->next != NULL) \
137 __cur__->next->prev = __cur__->prev; \
138 if(__cur__ == (obj)->name) \
139 (obj)->name = __cur__->next; \
146 #define GCBREG(name, funca, dataa) \
148 struct cbchain_ ## name *__new_cb__; \
149 __new_cb__ = smalloc(sizeof(*__new_cb__)); \
150 __new_cb__->func = funca; \
151 __new_cb__->data = dataa; \
152 __new_cb__->prev = NULL; \
153 __new_cb__->next = name; \
157 #define CBCHAININIT(obj, name) (obj)->name = NULL
159 #define CBCHAINFREE(obj, name) \
161 struct cbchain_ ## name *__cur__; \
162 while((__cur__ = (obj)->name) != NULL) { \
163 (obj)->name = __cur__->next; \
164 if(__cur__->destroy != NULL) \
165 __cur__->destroy(__cur__->data); \
170 #define CBCHAINDOCB(obj, name, args...) \
172 struct cbchain_ ## name *__cur__, *__next__; \
173 for(__cur__ = (obj)->name; __cur__ != NULL; __cur__ = __next__) { \
174 __next__ = __cur__->next; \
175 if(__cur__->func(args, __cur__->data)) { \
176 if(__cur__->next != NULL) \
177 __cur__->next->prev = __cur__->prev; \
178 if(__cur__->prev != NULL) \
179 __cur__->prev->next = __cur__->next; \
180 if(__cur__ == (obj)->name) \
181 (obj)->name = __cur__->next; \
187 #define GCBCHAINDOCB(name, args...) \
189 struct cbchain_ ## name *__cur__; \
192 for(__cur__ = name; __cur__ != NULL; __cur__ = __cur__->next) { \
193 if(__cur__->func(args, __cur__->data)) { \