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