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