Add trivial HM decoder
[doldaconnect.git] / daemon / 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>
24#include <malloc.h>
25#include "log.h"
26
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)))
32
33#define CBCHAIN(name, args...) \
34struct cbchain_ ## name { \
35 struct cbchain_ ## name *next, *prev; \
36 int (*func)(args, void *data); \
37 void (*destroy)(void *data); \
38 void *data; \
39} * name
40
41#define GCBCHAIN(name, args...) \
42struct cbchain_ ## name * name = NULL
43
44#define EGCBCHAIN(name, args...) \
45extern struct cbchain_ ## name { \
46 struct cbchain_ ## name *next, *prev; \
47 int (*func)(args, void *data); \
48 void *data; \
49} * name
50
51extern int vswprintf (wchar_t *__restrict __s, size_t __n,
52 __const wchar_t *__restrict __format,
53 __gnuc_va_list __arg);
54extern int swprintf (wchar_t *__restrict __s, size_t __n,
55 __const wchar_t *__restrict __format, ...);
56
57char *vsprintf2(char *format, va_list al);
58char *sprintf2(char *format, ...)
59#if defined(__GNUC__) && 0
60 __attribute__ ((format (printf, 1, 2)))
61#endif
62
63;
64wchar_t *vswprintf2(wchar_t *format, va_list al);
65wchar_t *swprintf2(wchar_t *format, ...);
66wchar_t *icmbstowcs(char *mbs, char *charset);
67wchar_t *icsmbstowcs(char *mbs, char *charset, wchar_t *def);
68char *icwcstombs(wchar_t *wcs, char *charset);
69char *icswcstombs(wchar_t *wcs, char *charset, char *def);
70wchar_t *wcstolower(wchar_t *wcs);
71wchar_t ucptowc(int ucp);
72void _sizebuf(void **buf, size_t *bufsize, size_t reqsize, size_t elsize, int algo);
73double ntime(void);
74int wcsexists(wchar_t *h, wchar_t *n);
75#ifndef HAVE_WCSCASECMP
76int wcscasecmp(const wchar_t *s1, const wchar_t *s2);
77#endif
78char *hexencode(char *data, size_t datalen);
79char *hexdecode(char *data, size_t *len);
80char *base64encode(char *data, size_t datalen);
81char *base64decode(char *data, size_t *datalen);
82char *base32encode(char *data, size_t datalen);
83char *base32decode(char *data, size_t *datalen);
84void _freeparr(void **arr);
85int _parrlen(void **arr);
86char *findfile(char *gname, char *uname, char *homedir);
87
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) \
91do { \
92 _sizebuf((void **)(&(b)), &(b ## size), (b ## data) + 1, sizeof(*(b)), 1); \
93 (b)[(b ## data)++] = (c); \
94} while(0)
95#define bufcat(d, s, n) \
96do { \
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__; \
102} while (0)
103
104#define freeparr(parr) _freeparr((void **)(parr))
105#define parrlen(parr) _parrlen((void **)(parr))
106
107#define CBREG(obj, name, funca, destroya, dataa) \
108do { \
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__; \
117} while(0)
118
119#define CBUNREG(obj, name, dataa) \
120do { \
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; \
132 free(__cur__); \
133 break; \
134 } \
135 } \
136} while(0)
137
138#define GCBREG(name, funca, dataa) \
139do { \
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; \
146 name = __new_cb__; \
147} while(0)
148
149#define CBCHAININIT(obj, name) (obj)->name = NULL
150
151#define CBCHAINFREE(obj, name) \
152do { \
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); \
158 free(__cur__); \
159 } \
160} while(0)
161
162#define CBCHAINDOCB(obj, name, args...) \
163do { \
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; \
174 free(__cur__); \
175 } \
176 } \
177} while(0)
178
179#define GCBCHAINDOCB(name, args...) \
180({ \
181 struct cbchain_ ## name *__cur__; \
182 int __ret__; \
183 __ret__ = 0; \
184 for(__cur__ = name; __cur__ != NULL; __cur__ = __cur__->next) { \
185 if(__cur__->func(args, __cur__->data)) { \
186 __ret__ = 1; \
187 break; \
188 } \
189 } \
190 __ret__; \
191})
192
193#endif