13 #include <sys/socket.h>
14 #include <netinet/in.h>
16 #include <arpa/inet.h>
22 #endif /* HAVE_CONFIG_H */
25 ** Arrange to use either varargs or stdargs
28 #define MAXSHORTSTR 203 /* max short string length */
29 #define QUAD_T unsigned long long
35 # define VA_LOCAL_DECL va_list ap;
36 # define VA_START(f) va_start(ap, f)
37 # define VA_END va_end(ap)
43 # define VA_LOCAL_DECL va_list ap;
44 # define VA_START(f) va_start(ap)
45 # define VA_END va_end(ap)
50 #ifndef INCL_LIBXODE_H
51 #define INCL_LIBXODE_H
59 extern int ap_snprintf(char *, size_t, const char *, ...);
60 #define snprintf ap_snprintf
63 #ifndef HAVE_VSNPRINTF
64 extern int ap_vsnprintf(char *, size_t, const char *, va_list ap
);
65 #define vsnprintf ap_vsnprintf
68 #define ZONE zonestr(__FILE__,__LINE__)
69 char *zonestr(char *file
, int line
);
71 /* --------------------------------------------------------- */
73 /* Pool-based memory management routines */
75 /* --------------------------------------------------------- */
79 flip these, this should be a prime number for top # of pools debugging
80 #define POOL_DEBUG 40009
83 /* pheap - singular allocation of memory */
90 /* pool_cleaner - callback type which is associated
91 with a pool entry; invoked when the pool entry is
93 typedef void (*pool_cleaner
)(void *arg
);
95 /* pfree - a linked list node which stores an
96 allocation chunk, plus a callback */
105 /* pool - base node for a pool. Maintains a linked list
106 of pool entries (pfree) */
107 typedef struct pool_struct
110 struct pfree
*cleanup
;
113 char name
[8], zone
[32];
116 #define pool_new() _pool_new(ZONE)
117 #define pool_heap(i) _pool_new_heap(i,ZONE)
120 #define pool_heap(i) _pool_new_heap(i,NULL)
121 #define pool_new() _pool_new(NULL)
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 */
139 /* --------------------------------------------------------- */
141 /* Socket helper stuff */
143 /* --------------------------------------------------------- */
144 #ifndef MAXHOSTNAMELEN
145 #define MAXHOSTNAMELEN 64
148 #define NETSOCKET_SERVER 0
149 #define NETSOCKET_CLIENT 1
150 #define NETSOCKET_UDP 2
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
);
161 /* --------------------------------------------------------- */
163 /* String management routines */
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 */
177 /* --------------------------------------------------------- */
179 /* SHA calculations */
181 /* --------------------------------------------------------- */
182 #if (SIZEOF_INT == 4)
183 typedef unsigned int uint32
;
184 #elif (SIZEOF_SHORT == 4)
185 typedef unsigned short uint32
;
187 typedef unsigned int uint32
;
188 #endif /* HAVEUINT32 */
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 */
195 int strprintsha(char *dest
, int *hashval
);
198 /* --------------------------------------------------------- */
200 /* Hashtable functions */
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
);
207 typedef void *HASHTABLE
;
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
);
218 /* --------------------------------------------------------- */
220 /* XML escaping utils */
222 /* --------------------------------------------------------- */
223 char *strescape(pool p
, char *buf
); /* Escape <>&'" chars */
224 char *strunescape(pool p
, char *buf
);
227 /* --------------------------------------------------------- */
229 /* String pools (spool) functions */
231 /* --------------------------------------------------------- */
235 struct spool_node
*next
;
238 typedef struct spool_struct
242 struct spool_node
*last
;
243 struct spool_node
*first
;
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! */
253 /* --------------------------------------------------------- */
255 /* xmlnodes - Document Object Model */
257 /* --------------------------------------------------------- */
259 #define NTYPE_ATTRIB 1
260 #define NTYPE_CDATA 2
263 #define NTYPE_UNDEF -1
265 /* --------------------------------------------------------------------------
266 Node structure. Do not use directly! Always use accessor macros
268 -------------------------------------------------------------------------- */
269 typedef struct xmlnode_t
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
;
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
);
299 /* Node Memory Pool */
300 pool
xmlnode_pool(xmlnode node
);
301 xmlnode
_xmlnode_new(pool p
, const char *name
, unsigned int type
);
304 void xmlnode_hide(xmlnode child
);
305 void xmlnode_hide_attrib(xmlnode parent
, const char *name
);
307 /* Node deletion routine, also frees the node pool! */
308 void xmlnode_free(xmlnode node
);
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
);
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
);
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
);
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
);
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
);
337 int xmlnode_has_children(xmlnode node
);
338 int xmlnode_has_attribs(xmlnode node
);
340 /* Node-to-string translation */
341 char* xmlnode2str(xmlnode node
);
343 /* Node-to-terminated-string translation
344 -- useful for interfacing w/ scripting langs */
345 char* xmlnode2tstr(xmlnode node
);
347 int xmlnode_cmp(xmlnode a
, xmlnode b
); /* compares a and b for equality */
349 int xmlnode2file(char *file
, xmlnode node
); /* writes node to file */
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
);
356 /***********************
358 ***********************/
360 #define XSTREAM_MAXNODE 1000000
361 #define XSTREAM_MAXDEPTH 100
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 */
368 typedef void (*xstream_onNode
)(int type
, xmlnode x
, void *arg
); /* xstream event handler */
370 typedef struct xstream_struct
381 } *xstream
, _xstream
;
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 */
386 /* convience functions */
387 xmlnode
xstream_header(char *nspace
, char *to
, char *from
);
388 char *xstream_header_char(xmlnode x
);
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/
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.
402 * The Original Code is SHA 180-1 Header File
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
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
430 uint32_t sizeHi
,sizeLo
;
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]);
446 #endif /* INCL_LIBXODE_H */