Mercurial > pidgin
annotate plugins/jabber/libxode.h @ 1646:f1a019d30e01
[gaim-migrate @ 1656]
fpmuller said this fixed solaris 2.6 builds
committer: Tailor Script <tailor@pidgin.im>
author | Eric Warmenhoven <eric@warmenhoven.org> |
---|---|
date | Mon, 26 Mar 2001 08:43:35 +0000 |
parents | afa63ac2fd84 |
children |
rev | line source |
---|---|
1347 | 1 #include <string.h> |
2 #include <stdlib.h> | |
3 #include <sys/types.h> | |
4 #include <stdio.h> | |
5 #include <setjmp.h> | |
6 #include <sys/stat.h> | |
7 #include <fcntl.h> | |
8 #include <errno.h> | |
9 #include <signal.h> | |
10 #include <syslog.h> | |
11 #include <strings.h> | |
12 #include <unistd.h> | |
13 #include <sys/socket.h> | |
14 #include <netinet/in.h> | |
15 #include <netdb.h> | |
16 #include <arpa/inet.h> | |
1646
f1a019d30e01
[gaim-migrate @ 1656]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1347
diff
changeset
|
17 #include <arpa/nameser.h> |
1347 | 18 #include <resolv.h> |
19 #include <sys/time.h> | |
20 | |
21 #include "xmlparse.h" | |
22 #ifdef HAVE_CONFIG_H | |
23 #include <config.h> | |
24 #endif /* HAVE_CONFIG_H */ | |
25 | |
26 /* | |
27 ** Arrange to use either varargs or stdargs | |
28 */ | |
29 | |
30 #define MAXSHORTSTR 203 /* max short string length */ | |
31 #define QUAD_T unsigned long long | |
32 | |
33 #ifdef __STDC__ | |
34 | |
35 #include <stdarg.h> | |
36 | |
37 # define VA_LOCAL_DECL va_list ap; | |
38 # define VA_START(f) va_start(ap, f) | |
39 # define VA_END va_end(ap) | |
40 | |
41 #else /* __STDC__ */ | |
42 | |
43 # include <varargs.h> | |
44 | |
45 # define VA_LOCAL_DECL va_list ap; | |
46 # define VA_START(f) va_start(ap) | |
47 # define VA_END va_end(ap) | |
48 | |
49 #endif /* __STDC__ */ | |
50 | |
51 | |
52 #ifndef INCL_LIBXODE_H | |
53 #define INCL_LIBXODE_H | |
54 | |
55 #ifdef __cplusplus | |
56 extern "C" { | |
57 #endif | |
58 | |
59 | |
60 #ifndef HAVE_SNPRINTF | |
61 extern int ap_snprintf(char *, size_t, const char *, ...); | |
62 #define snprintf ap_snprintf | |
63 #endif | |
64 | |
65 #ifndef HAVE_VSNPRINTF | |
66 extern int ap_vsnprintf(char *, size_t, const char *, va_list ap); | |
67 #define vsnprintf ap_vsnprintf | |
68 #endif | |
69 | |
70 #define ZONE zonestr(__FILE__,__LINE__) | |
71 char *zonestr(char *file, int line); | |
72 | |
73 /* --------------------------------------------------------- */ | |
74 /* */ | |
75 /* Pool-based memory management routines */ | |
76 /* */ | |
77 /* --------------------------------------------------------- */ | |
78 | |
79 #undef POOL_DEBUG | |
80 /* | |
81 flip these, this should be a prime number for top # of pools debugging | |
82 #define POOL_DEBUG 40009 | |
83 */ | |
84 | |
85 /* pheap - singular allocation of memory */ | |
86 struct pheap | |
87 { | |
88 void *block; | |
89 int size, used; | |
90 }; | |
91 | |
92 /* pool_cleaner - callback type which is associated | |
93 with a pool entry; invoked when the pool entry is | |
94 free'd */ | |
95 typedef void (*pool_cleaner)(void *arg); | |
96 | |
97 /* pfree - a linked list node which stores an | |
98 allocation chunk, plus a callback */ | |
99 struct pfree | |
100 { | |
101 pool_cleaner f; | |
102 void *arg; | |
103 struct pheap *heap; | |
104 struct pfree *next; | |
105 }; | |
106 | |
107 /* pool - base node for a pool. Maintains a linked list | |
108 of pool entries (pfree) */ | |
109 typedef struct pool_struct | |
110 { | |
111 int size; | |
112 struct pfree *cleanup; | |
113 struct pheap *heap; | |
114 #ifdef POOL_DEBUG | |
115 char name[8], zone[32]; | |
116 int lsize; | |
117 } _pool, *pool; | |
118 #define pool_new() _pool_new(ZONE) | |
119 #define pool_heap(i) _pool_new_heap(i,ZONE) | |
120 #else | |
121 } _pool, *pool; | |
122 #define pool_heap(i) _pool_new_heap(i,NULL) | |
123 #define pool_new() _pool_new(NULL) | |
124 #endif | |
125 | |
126 pool _pool_new(char *zone); /* new pool :) */ | |
127 pool _pool_new_heap(int size, char *zone); /* creates a new memory pool with an initial heap size */ | |
128 void *pmalloc(pool p, int size); /* wrapper around malloc, takes from the pool, cleaned up automatically */ | |
129 void *pmalloc_x(pool p, int size, char c); /* Wrapper around pmalloc which prefils buffer with c */ | |
130 void *pmalloco(pool p, int size); /* YAPW for zeroing the block */ | |
131 char *pstrdup(pool p, const char *src); /* wrapper around strdup, gains mem from pool */ | |
132 void pool_stat(int full); /* print to stderr the changed pools and reset */ | |
133 char *pstrdupx(pool p, const char *src); /* temp stub */ | |
134 void pool_cleanup(pool p, pool_cleaner f, void *arg); /* calls f(arg) before the pool is freed during cleanup */ | |
135 void pool_free(pool p); /* calls the cleanup functions, frees all the data on the pool, and deletes the pool itself */ | |
136 int pool_size(pool p); /* returns total bytes allocated in this pool */ | |
137 | |
138 | |
139 | |
140 | |
141 /* --------------------------------------------------------- */ | |
142 /* */ | |
143 /* Socket helper stuff */ | |
144 /* */ | |
145 /* --------------------------------------------------------- */ | |
146 #ifndef MAXHOSTNAMELEN | |
147 #define MAXHOSTNAMELEN 64 | |
148 #endif | |
149 | |
150 #define NETSOCKET_SERVER 0 | |
151 #define NETSOCKET_CLIENT 1 | |
152 #define NETSOCKET_UDP 2 | |
153 | |
154 #ifndef WIN32 | |
155 int make_netsocket(u_short port, char *host, int type); | |
156 struct in_addr *make_addr(char *host); | |
157 int set_fd_close_on_exec(int fd, int flag); | |
158 #endif | |
159 | |
160 | |
161 /* --------------------------------------------------------- */ | |
162 /* */ | |
163 /* String management routines */ | |
164 /* */ | |
165 /* --------------------------------------------------------- */ | |
166 char *j_strdup(const char *str); /* provides NULL safe strdup wrapper */ | |
167 char *j_strcat(char *dest, char *txt); /* strcpy() clone */ | |
168 int j_strcmp(const char *a, const char *b); /* provides NULL safe strcmp wrapper */ | |
169 int j_strcasecmp(const char *a, const char *b); /* provides NULL safe strcasecmp wrapper */ | |
170 int j_strncmp(const char *a, const char *b, int i); /* provides NULL safe strncmp wrapper */ | |
171 int j_strncasecmp(const char *a, const char *b, int i); /* provides NULL safe strncasecmp wrapper */ | |
172 int j_strlen(const char *a); /* provides NULL safe strlen wrapper */ | |
173 int j_atoi(const char *a, int def); /* checks for NULL and uses default instead, convienence */ | |
174 void str_b64decode(char *str); /* what it says */ | |
175 | |
176 | |
177 /* --------------------------------------------------------- */ | |
178 /* */ | |
179 /* SHA calculations */ | |
180 /* */ | |
181 /* --------------------------------------------------------- */ | |
182 #if (SIZEOF_INT == 4) | |
183 typedef unsigned int uint32; | |
184 #elif (SIZEOF_SHORT == 4) | |
185 typedef unsigned short uint32; | |
186 #else | |
187 typedef unsigned int uint32; | |
188 #endif /* HAVEUINT32 */ | |
189 | |
190 int sha_hash(int *data, int *hash); | |
191 int sha_init(int *hash); | |
192 char *shahash(char *str); /* NOT THREAD SAFE */ | |
193 void shahash_r(const char* str, char hashbuf[40]); /* USE ME */ | |
194 | |
195 int strprintsha(char *dest, int *hashval); | |
196 | |
197 | |
198 /* --------------------------------------------------------- */ | |
199 /* */ | |
200 /* Hashtable functions */ | |
201 /* */ | |
202 /* --------------------------------------------------------- */ | |
203 typedef int (*KEYHASHFUNC)(const void *key); | |
204 typedef int (*KEYCOMPAREFUNC)(const void *key1, const void *key2); | |
205 typedef int (*TABLEWALKFUNC)(void *user_data, const void *key, void *data); | |
206 | |
207 typedef void *HASHTABLE; | |
208 | |
209 HASHTABLE ghash_create(int buckets, KEYHASHFUNC hash, KEYCOMPAREFUNC cmp); | |
210 void ghash_destroy(HASHTABLE tbl); | |
211 void *ghash_get(HASHTABLE tbl, const void *key); | |
212 int ghash_put(HASHTABLE tbl, const void *key, void *value); | |
213 int ghash_remove(HASHTABLE tbl, const void *key); | |
214 int ghash_walk(HASHTABLE tbl, TABLEWALKFUNC func, void *user_data); | |
215 int str_hash_code(const char *s); | |
216 | |
217 | |
218 /* --------------------------------------------------------- */ | |
219 /* */ | |
220 /* XML escaping utils */ | |
221 /* */ | |
222 /* --------------------------------------------------------- */ | |
223 char *strescape(pool p, char *buf); /* Escape <>&'" chars */ | |
224 char *strunescape(pool p, char *buf); | |
225 | |
226 | |
227 /* --------------------------------------------------------- */ | |
228 /* */ | |
229 /* String pools (spool) functions */ | |
230 /* */ | |
231 /* --------------------------------------------------------- */ | |
232 struct spool_node | |
233 { | |
234 char *c; | |
235 struct spool_node *next; | |
236 }; | |
237 | |
238 typedef struct spool_struct | |
239 { | |
240 pool p; | |
241 int len; | |
242 struct spool_node *last; | |
243 struct spool_node *first; | |
244 } *spool; | |
245 | |
246 spool spool_new(pool p); /* create a string pool */ | |
247 void spooler(spool s, ...); /* append all the char * args to the pool, terminate args with s again */ | |
248 char *spool_print(spool s); /* return a big string */ | |
249 void spool_add(spool s, char *str); /* add a single char to the pool */ | |
250 char *spools(pool p, ...); /* wrap all the spooler stuff in one function, the happy fun ball! */ | |
251 | |
252 | |
253 /* --------------------------------------------------------- */ | |
254 /* */ | |
255 /* xmlnodes - Document Object Model */ | |
256 /* */ | |
257 /* --------------------------------------------------------- */ | |
258 #define NTYPE_TAG 0 | |
259 #define NTYPE_ATTRIB 1 | |
260 #define NTYPE_CDATA 2 | |
261 | |
262 #define NTYPE_LAST 2 | |
263 #define NTYPE_UNDEF -1 | |
264 | |
265 /* -------------------------------------------------------------------------- | |
266 Node structure. Do not use directly! Always use accessor macros | |
267 and methods! | |
268 -------------------------------------------------------------------------- */ | |
269 typedef struct xmlnode_t | |
270 { | |
271 char* name; | |
272 unsigned short type; | |
273 char* data; | |
274 int data_sz; | |
275 int complete; | |
276 pool p; | |
277 struct xmlnode_t* parent; | |
278 struct xmlnode_t* firstchild; | |
279 struct xmlnode_t* lastchild; | |
280 struct xmlnode_t* prev; | |
281 struct xmlnode_t* next; | |
282 struct xmlnode_t* firstattrib; | |
283 struct xmlnode_t* lastattrib; | |
284 } _xmlnode, *xmlnode; | |
285 | |
286 /* Node creation routines */ | |
287 xmlnode xmlnode_wrap(xmlnode x,const char* wrapper); | |
288 xmlnode xmlnode_new_tag(const char* name); | |
289 xmlnode xmlnode_new_tag_pool(pool p, const char* name); | |
290 xmlnode xmlnode_insert_tag(xmlnode parent, const char* name); | |
291 xmlnode xmlnode_insert_cdata(xmlnode parent, const char* CDATA, unsigned int size); | |
292 xmlnode xmlnode_insert_tag_node(xmlnode parent, xmlnode node); | |
293 void xmlnode_insert_node(xmlnode parent, xmlnode node); | |
294 xmlnode xmlnode_str(char *str, int len); | |
295 xmlnode xmlnode_file(char *file); | |
296 xmlnode xmlnode_dup(xmlnode x); /* duplicate x */ | |
297 xmlnode xmlnode_dup_pool(pool p, xmlnode x); | |
298 | |
299 /* Node Memory Pool */ | |
300 pool xmlnode_pool(xmlnode node); | |
301 xmlnode _xmlnode_new(pool p, const char *name, unsigned int type); | |
302 | |
303 /* Node editing */ | |
304 void xmlnode_hide(xmlnode child); | |
305 void xmlnode_hide_attrib(xmlnode parent, const char *name); | |
306 | |
307 /* Node deletion routine, also frees the node pool! */ | |
308 void xmlnode_free(xmlnode node); | |
309 | |
310 /* Locates a child tag by name and returns it */ | |
311 xmlnode xmlnode_get_tag(xmlnode parent, const char* name); | |
312 char* xmlnode_get_tag_data(xmlnode parent, const char* name); | |
313 | |
314 /* Attribute accessors */ | |
315 void xmlnode_put_attrib(xmlnode owner, const char* name, const char* value); | |
316 char* xmlnode_get_attrib(xmlnode owner, const char* name); | |
317 void xmlnode_put_expat_attribs(xmlnode owner, const char** atts); | |
318 | |
319 /* Bastard am I, but these are fun for internal use ;-) */ | |
320 void xmlnode_put_vattrib(xmlnode owner, const char* name, void *value); | |
321 void* xmlnode_get_vattrib(xmlnode owner, const char* name); | |
322 | |
323 /* Node traversal routines */ | |
324 xmlnode xmlnode_get_firstattrib(xmlnode parent); | |
325 xmlnode xmlnode_get_firstchild(xmlnode parent); | |
326 xmlnode xmlnode_get_lastchild(xmlnode parent); | |
327 xmlnode xmlnode_get_nextsibling(xmlnode sibling); | |
328 xmlnode xmlnode_get_prevsibling(xmlnode sibling); | |
329 xmlnode xmlnode_get_parent(xmlnode node); | |
330 | |
331 /* Node information routines */ | |
332 char* xmlnode_get_name(xmlnode node); | |
333 char* xmlnode_get_data(xmlnode node); | |
334 int xmlnode_get_datasz(xmlnode node); | |
335 int xmlnode_get_type(xmlnode node); | |
336 | |
337 int xmlnode_has_children(xmlnode node); | |
338 int xmlnode_has_attribs(xmlnode node); | |
339 | |
340 /* Node-to-string translation */ | |
341 char* xmlnode2str(xmlnode node); | |
342 | |
343 /* Node-to-terminated-string translation | |
344 -- useful for interfacing w/ scripting langs */ | |
345 char* xmlnode2tstr(xmlnode node); | |
346 | |
347 int xmlnode_cmp(xmlnode a, xmlnode b); /* compares a and b for equality */ | |
348 | |
349 int xmlnode2file(char *file, xmlnode node); /* writes node to file */ | |
350 | |
351 /* Expat callbacks */ | |
352 void expat_startElement(void* userdata, const char* name, const char** atts); | |
353 void expat_endElement(void* userdata, const char* name); | |
354 void expat_charData(void* userdata, const char* s, int len); | |
355 | |
356 /*********************** | |
357 * XSTREAM Section | |
358 ***********************/ | |
359 | |
360 #define XSTREAM_MAXNODE 1000000 | |
361 #define XSTREAM_MAXDEPTH 100 | |
362 | |
363 #define XSTREAM_ROOT 0 /* root element */ | |
364 #define XSTREAM_NODE 1 /* normal node */ | |
365 #define XSTREAM_CLOSE 2 /* closed </stream:stream> */ | |
366 #define XSTREAM_ERR 4 /* parser error */ | |
367 | |
368 typedef void (*xstream_onNode)(int type, xmlnode x, void *arg); /* xstream event handler */ | |
369 | |
370 typedef struct xstream_struct | |
371 { | |
372 XML_Parser parser; | |
373 xmlnode node; | |
374 char *cdata; | |
375 int cdata_len; | |
376 pool p; | |
377 xstream_onNode f; | |
378 void *arg; | |
379 int status; | |
380 int depth; | |
381 } *xstream, _xstream; | |
382 | |
383 xstream xstream_new(pool p, xstream_onNode f, void *arg); /* create a new xstream */ | |
384 int xstream_eat(xstream xs, char *buff, int len); /* parse new data for this xstream, returns last XSTREAM_* status */ | |
385 | |
386 /* convience functions */ | |
387 xmlnode xstream_header(char *namespace, char *to, char *from); | |
388 char *xstream_header_char(xmlnode x); | |
389 | |
390 /* SHA.H */ | |
391 /* | |
392 * The contents of this file are subject to the Mozilla Public | |
393 * License Version 1.1 (the "License"); you may not use this file | |
394 * except in compliance with the License. You may obtain a copy of | |
395 * the License at http://www.mozilla.org/MPL/ | |
396 * | |
397 * Software distributed under the License is distributed on an "AS | |
398 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or | |
399 * implied. See the License for the specific language governing | |
400 * rights and limitations under the License. | |
401 * | |
402 * The Original Code is SHA 180-1 Header File | |
403 * | |
404 * The Initial Developer of the Original Code is Paul Kocher of | |
405 * Cryptography Research. Portions created by Paul Kocher are | |
406 * Copyright (C) 1995-9 by Cryptography Research, Inc. All | |
407 * Rights Reserved. | |
408 * | |
409 * Contributor(s): | |
410 * | |
411 * Paul Kocher | |
412 * | |
413 * Alternatively, the contents of this file may be used under the | |
414 * terms of the GNU General Public License Version 2 or later (the | |
415 * "GPL"), in which case the provisions of the GPL are applicable | |
416 * instead of those above. If you wish to allow use of your | |
417 * version of this file only under the terms of the GPL and not to | |
418 * allow others to use your version of this file under the MPL, | |
419 * indicate your decision by deleting the provisions above and | |
420 * replace them with the notice and other provisions required by | |
421 * the GPL. If you do not delete the provisions above, a recipient | |
422 * may use your version of this file under either the MPL or the | |
423 * GPL. | |
424 */ | |
425 | |
426 typedef struct { | |
427 unsigned long H[5]; | |
428 unsigned long W[80]; | |
429 int lenW; | |
430 unsigned long sizeHi,sizeLo; | |
431 } SHA_CTX; | |
432 | |
433 | |
434 void shaInit(SHA_CTX *ctx); | |
435 void shaUpdate(SHA_CTX *ctx, unsigned char *dataIn, int len); | |
436 void shaFinal(SHA_CTX *ctx, unsigned char hashout[20]); | |
437 void shaBlock(unsigned char *dataIn, int len, unsigned char hashout[20]); | |
438 | |
439 | |
440 /* END SHA.H */ | |
441 | |
442 #ifdef __cplusplus | |
443 } | |
444 #endif | |
445 | |
446 #endif /* INCL_LIBXODE_H */ |