25
|
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 <sys/time.h> |
|
18 |
|
19 #include "xmlparse.h" |
|
20 #ifdef HAVE_CONFIG_H |
|
21 #include <config.h> |
|
22 #endif /* HAVE_CONFIG_H */ |
|
23 |
|
24 /* |
|
25 ** Arrange to use either varargs or stdargs |
|
26 */ |
|
27 |
|
28 #define MAXSHORTSTR 203 /* max short string length */ |
|
29 #define QUAD_T unsigned long long |
|
30 |
|
31 #ifdef __STDC__ |
|
32 |
|
33 #include <stdarg.h> |
|
34 |
|
35 # define VA_LOCAL_DECL va_list ap; |
|
36 # define VA_START(f) va_start(ap, f) |
|
37 # define VA_END va_end(ap) |
|
38 |
|
39 #else /* __STDC__ */ |
|
40 |
|
41 # include <varargs.h> |
|
42 |
|
43 # define VA_LOCAL_DECL va_list ap; |
|
44 # define VA_START(f) va_start(ap) |
|
45 # define VA_END va_end(ap) |
|
46 |
|
47 #endif /* __STDC__ */ |
|
48 |
|
49 |
|
50 #ifndef INCL_LIBXODE_H |
|
51 #define INCL_LIBXODE_H |
|
52 |
|
53 #ifdef __cplusplus |
|
54 extern "C" { |
|
55 #endif |
|
56 |
|
57 |
|
58 #ifndef HAVE_SNPRINTF |
|
59 extern int ap_snprintf(char *, size_t, const char *, ...); |
|
60 #define snprintf ap_snprintf |
|
61 #endif |
|
62 |
|
63 #ifndef HAVE_VSNPRINTF |
|
64 extern int ap_vsnprintf(char *, size_t, const char *, va_list ap); |
|
65 #define vsnprintf ap_vsnprintf |
|
66 #endif |
|
67 |
|
68 #define ZONE zonestr(__FILE__,__LINE__) |
|
69 char *zonestr(char *file, int line); |
|
70 |
|
71 /* --------------------------------------------------------- */ |
|
72 /* */ |
|
73 /* Pool-based memory management routines */ |
|
74 /* */ |
|
75 /* --------------------------------------------------------- */ |
|
76 |
|
77 #undef POOL_DEBUG |
|
78 /* |
|
79 flip these, this should be a prime number for top # of pools debugging |
|
80 #define POOL_DEBUG 40009 |
|
81 */ |
|
82 |
|
83 /* pheap - singular allocation of memory */ |
|
84 struct pheap |
|
85 { |
|
86 void *block; |
|
87 int size, used; |
|
88 }; |
|
89 |
|
90 /* pool_cleaner - callback type which is associated |
|
91 with a pool entry; invoked when the pool entry is |
|
92 free'd */ |
|
93 typedef void (*pool_cleaner)(void *arg); |
|
94 |
|
95 /* pfree - a linked list node which stores an |
|
96 allocation chunk, plus a callback */ |
|
97 struct pfree |
|
98 { |
|
99 pool_cleaner f; |
|
100 void *arg; |
|
101 struct pheap *heap; |
|
102 struct pfree *next; |
|
103 }; |
|
104 |
|
105 /* pool - base node for a pool. Maintains a linked list |
|
106 of pool entries (pfree) */ |
|
107 typedef struct pool_struct |
|
108 { |
|
109 int size; |
|
110 struct pfree *cleanup; |
|
111 struct pheap *heap; |
|
112 #ifdef POOL_DEBUG |
|
113 char name[8], zone[32]; |
|
114 int lsize; |
|
115 } _pool, *pool; |
|
116 #define pool_new() _pool_new(ZONE) |
|
117 #define pool_heap(i) _pool_new_heap(i,ZONE) |
|
118 #else |
|
119 } _pool, *pool; |
|
120 #define pool_heap(i) _pool_new_heap(i,NULL) |
|
121 #define pool_new() _pool_new(NULL) |
|
122 #endif |
|
123 |
|
124 pool _pool_new(char *zone); /* new pool :) */ |
|
125 pool _pool_new_heap(int size, char *zone); /* creates a new memory pool with an initial heap size */ |
|
126 void *pmalloc(pool p, int size); /* wrapper around malloc, takes from the pool, cleaned up automatically */ |
|
127 void *pmalloc_x(pool p, int size, char c); /* Wrapper around pmalloc which prefils buffer with c */ |
|
128 void *pmalloco(pool p, int size); /* YAPW for zeroing the block */ |
|
129 char *pstrdup(pool p, const char *src); /* wrapper around strdup, gains mem from pool */ |
|
130 void pool_stat(int full); /* print to stderr the changed pools and reset */ |
|
131 char *pstrdupx(pool p, const char *src); /* temp stub */ |
|
132 void pool_cleanup(pool p, pool_cleaner f, void *arg); /* calls f(arg) before the pool is freed during cleanup */ |
|
133 void pool_free(pool p); /* calls the cleanup functions, frees all the data on the pool, and deletes the pool itself */ |
|
134 int pool_size(pool p); /* returns total bytes allocated in this pool */ |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 /* --------------------------------------------------------- */ |
|
140 /* */ |
|
141 /* Socket helper stuff */ |
|
142 /* */ |
|
143 /* --------------------------------------------------------- */ |
|
144 #ifndef MAXHOSTNAMELEN |
|
145 #define MAXHOSTNAMELEN 64 |
|
146 #endif |
|
147 |
|
148 #define NETSOCKET_SERVER 0 |
|
149 #define NETSOCKET_CLIENT 1 |
|
150 #define NETSOCKET_UDP 2 |
|
151 |
|
152 #ifndef WIN32 |
|
153 int make_netsocket(u_short port, char *host, int type, int ssl); |
|
154 int make_nb_netsocket(u_short port, char *host, int type, int ssl, int * state); |
|
155 void change_socket_to_blocking(int socket); |
|
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 *nspace, 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 */ |