Send and receive Unix creds.
[doldaconnect.git] / include / utils.h
CommitLineData
d3372da9 1/*
2 * Dolda Connect - Modular multiuser Direct Connect-style client
3 * Copyright (C) 2004 Fredrik Tolf (fredrik@dolda2000.com)
4 *
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.
9 *
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.
14 *
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
18*/
19#ifndef _UTILS_H
20#define _UTILS_H
21
22#include <stdarg.h>
23#include <stdlib.h>
8b17e919 24#ifdef DAEMON
d3372da9 25#include "log.h"
8b17e919 26#endif
d3372da9 27
9ec790e8 28struct wcspair {
29 struct wcspair *next;
30 wchar_t *key;
31 wchar_t *val;
32};
33
8b17e919 34
d3372da9 35/* "Safe" functions */
8b17e919 36#ifdef DAEMON
37#define LOGOOM(size) flog(LOG_CRIT, "%s (%s:%i): out of memory (alloc %i)", __FUNCTION__, __FILE__, __LINE__, (size))
d3372da9 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)))
8b17e919 42#else
43#define LOGOOM(size)
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)))
48#endif
d3372da9 49
50#define CBCHAIN(name, args...) \
51struct cbchain_ ## name { \
52 struct cbchain_ ## name *next, *prev; \
53 int (*func)(args, void *data); \
54 void (*destroy)(void *data); \
55 void *data; \
56} * name
57
58#define GCBCHAIN(name, args...) \
59struct cbchain_ ## name * name = NULL
60
61#define EGCBCHAIN(name, args...) \
62extern struct cbchain_ ## name { \
63 struct cbchain_ ## name *next, *prev; \
64 int (*func)(args, void *data); \
65 void *data; \
66} * name
67
68extern int vswprintf (wchar_t *__restrict __s, size_t __n,
69 __const wchar_t *__restrict __format,
70 __gnuc_va_list __arg);
71extern int swprintf (wchar_t *__restrict __s, size_t __n,
72 __const wchar_t *__restrict __format, ...);
73
74char *vsprintf2(char *format, va_list al);
75char *sprintf2(char *format, ...)
76#if defined(__GNUC__) && 0
77 __attribute__ ((format (printf, 1, 2)))
78#endif
d3372da9 79;
80wchar_t *vswprintf2(wchar_t *format, va_list al);
81wchar_t *swprintf2(wchar_t *format, ...);
6a3ef546 82int havecharset(char *charset);
d3372da9 83wchar_t *icmbstowcs(char *mbs, char *charset);
84wchar_t *icsmbstowcs(char *mbs, char *charset, wchar_t *def);
85char *icwcstombs(wchar_t *wcs, char *charset);
86char *icswcstombs(wchar_t *wcs, char *charset, char *def);
87wchar_t *wcstolower(wchar_t *wcs);
88wchar_t ucptowc(int ucp);
89void _sizebuf(void **buf, size_t *bufsize, size_t reqsize, size_t elsize, int algo);
90double ntime(void);
91int wcsexists(wchar_t *h, wchar_t *n);
92#ifndef HAVE_WCSCASECMP
93int wcscasecmp(const wchar_t *s1, const wchar_t *s2);
94#endif
95char *hexencode(char *data, size_t datalen);
96char *hexdecode(char *data, size_t *len);
97char *base64encode(char *data, size_t datalen);
98char *base64decode(char *data, size_t *datalen);
99char *base32encode(char *data, size_t datalen);
100char *base32decode(char *data, size_t *datalen);
101void _freeparr(void **arr);
102int _parrlen(void **arr);
7a83517c 103char *findfile(char *name, char *homedir, int filldef);
9ec790e8 104struct wcspair *newwcspair(wchar_t *key, wchar_t *val, struct wcspair **list);
105void freewcspair(struct wcspair *pair, struct wcspair **list);
14a9e5fd 106wchar_t *wpfind(struct wcspair *list, wchar_t *key);
d3372da9 107
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) \
111do { \
112 _sizebuf((void **)(&(b)), &(b ## size), (b ## data) + 1, sizeof(*(b)), 1); \
113 (b)[(b ## data)++] = (c); \
114} while(0)
115#define bufcat(d, s, n) \
116do { \
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__; \
122} while (0)
123
124#define freeparr(parr) _freeparr((void **)(parr))
125#define parrlen(parr) _parrlen((void **)(parr))
126
127#define CBREG(obj, name, funca, destroya, dataa) \
128do { \
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; \
5c851d43 136 if((obj)->name != NULL) { \
137 (obj)->name->prev = __new_cb__; \
138 } \
d3372da9 139 (obj)->name = __new_cb__; \
140} while(0)
141
cab0b442 142#define CBUNREG(obj, name, dataa) \
143do { \
d3372da9 144 struct cbchain_ ## name *__cur__; \
145 for(__cur__ = (obj)->name; __cur__ != NULL; __cur__ = __cur__->next) { \
cab0b442 146 if(__cur__->data == (dataa)) { \
d3372da9 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; \
155 free(__cur__); \
156 break; \
157 } \
158 } \
cab0b442 159} while(0)
d3372da9 160
161#define GCBREG(name, funca, dataa) \
162do { \
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; \
5c851d43 169 if(name != NULL) { \
170 name->prev = __new_cb__; \
171 } \
d3372da9 172 name = __new_cb__; \
173} while(0)
174
175#define CBCHAININIT(obj, name) (obj)->name = NULL
176
177#define CBCHAINFREE(obj, name) \
178do { \
179 struct cbchain_ ## name *__cur__; \
180 while((__cur__ = (obj)->name) != NULL) { \
181 (obj)->name = __cur__->next; \
cab0b442 182 if(__cur__->destroy != NULL) \
183 __cur__->destroy(__cur__->data); \
184 free(__cur__); \
d3372da9 185 } \
186} while(0)
187
188#define CBCHAINDOCB(obj, name, args...) \
189do { \
190 struct cbchain_ ## name *__cur__, *__next__; \
191 for(__cur__ = (obj)->name; __cur__ != NULL; __cur__ = __next__) { \
192 __next__ = __cur__->next; \
cab0b442 193 if(__cur__->func(args, __cur__->data)) { \
d3372da9 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; \
200 free(__cur__); \
201 } \
202 } \
203} while(0)
204
205#define GCBCHAINDOCB(name, args...) \
206({ \
207 struct cbchain_ ## name *__cur__; \
208 int __ret__; \
209 __ret__ = 0; \
210 for(__cur__ = name; __cur__ != NULL; __cur__ = __cur__->next) { \
211 if(__cur__->func(args, __cur__->data)) { \
212 __ret__ = 1; \
213 break; \
214 } \
215 } \
216 __ret__; \
217})
218
219#endif