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