📄 util.h
字号:
/* * jabberd - Jabber Open Source Server * Copyright (c) 2002 Jeremie Miller, Thomas Muldowney, * Ryan Eatmon, Robert Norris * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307USA */#ifdef HAVE_CONFIG_H# include <config.h>#endif#include "ac-stdint.h"#include <stdio.h>#include <stdlib.h>#include <stdarg.h>#include <time.h>#include <errno.h>#include <assert.h>#ifdef HAVE_SYS_TYPES_H# include <sys/types.h>#endif#ifdef HAVE_NETINET_IN_H# include <netinet/in.h>#endif#if defined(HAVE_SYS_TIME_H)# include <sys/time.h>#elif defined(HAVE_SYS_TIMEB_H)# include <sys/timeb.h>#endif#ifdef HAVE_SYSLOG_H# include <syslog.h>#endif#ifdef HAVE_UNISTD_H# include <unistd.h>#endif#include <ctype.h>#ifdef HAVE_SYS_SOCKET_H# include <sys/socket.h>#endif#ifdef HAVE_NETINET_IN_H# include <netinet/in.h>#endif#ifdef HAVE_ARPA_INET_H# include <arpa/inet.h>#endif#include "subst/subst.h"#include "util/util_compat.h"#ifndef INCL_UTIL_H#define INCL_UTIL_H#ifdef __cplusplusextern "C" {#endif/* expat is available */#define HAVE_EXPAT 1/* crypto hashing utils */#include "sha1.h"#include "md5.h"/* --------------------------------------------------------- *//* *//* Pool-based memory management routines *//* *//* --------------------------------------------------------- */#ifdef POOL_DEBUG/* prime number for top # of pools debugging */#define POOL_NUM 40009#endif/** pheap - singular allocation of memory */struct pheap{ void *block; int size, used;};/** pool_cleaner - callback type which is associated with a pool entry; invoked when the pool entry is free'd */typedef void (*pool_cleaner)(void *arg);/** pfree - a linked list node which stores an allocation chunk, plus a callback */struct pfree{ pool_cleaner f; void *arg; struct pheap *heap; struct pfree *next;};/** pool - base node for a pool. Maintains a linked list of pool entries (pfree) */typedef struct pool_struct{ int size; struct pfree *cleanup; struct pfree *cleanup_tail; struct pheap *heap;#ifdef POOL_DEBUG char name[8], zone[32]; int lsize;} _pool, *pool;#define pool_new() _pool_new(ZONE) #define pool_heap(i) _pool_new_heap(i,ZONE) #else} _pool, *pool;#define pool_heap(i) _pool_new_heap(i,NULL,0) #define pool_new() _pool_new(NULL,0)#endifpool _pool_new(char *zone, int line); /* new pool :) */pool _pool_new_heap(int size, char *zone, int line); /* creates a new memory pool with an initial heap size */void *pmalloc(pool p, int size); /* wrapper around malloc, takes from the pool, cleaned up automatically */void *pmalloc_x(pool p, int size, char c); /* Wrapper around pmalloc which prefils buffer with c */void *pmalloco(pool p, int size); /* YAPW for zeroing the block */char *pstrdup(pool p, const char *src); /* wrapper around strdup, gains mem from pool */void pool_stat(int full); /* print to stderr the changed pools and reset */char *pstrdupx(pool p, const char *src, int len); /* use given len */void pool_cleanup(pool p, pool_cleaner f, void *arg); /* calls f(arg) before the pool is freed during cleanup */void pool_free(pool p); /* calls the cleanup functions, frees all the data on the pool, and deletes the pool itself */int pool_size(pool p); /* returns total bytes allocated in this pool *//* --------------------------------------------------------- *//* *//* String management routines *//* *//** --------------------------------------------------------- */char *j_strdup(const char *str); /* provides NULL safe strdup wrapper */char *j_strcat(char *dest, char *txt); /* strcpy() clone */int j_strcmp(const char *a, const char *b); /* provides NULL safe strcmp wrapper */int j_strcasecmp(const char *a, const char *b); /* provides NULL safe strcasecmp wrapper */int j_strncmp(const char *a, const char *b, int i); /* provides NULL safe strncmp wrapper */int j_strncasecmp(const char *a, const char *b, int i); /* provides NULL safe strncasecmp wrapper */int j_strlen(const char *a); /* provides NULL safe strlen wrapper */int j_atoi(const char *a, int def); /* checks for NULL and uses default instead, convienence */char *j_attr(const char** atts, char *attr); /* decode attr's (from expat) */char *j_strnchr(const char *s, int c, int n); /* like strchr, but only searches n chars *//** old convenience function, now in str.c */void shahash_r(const char* str, char hashbuf[41]);/* --------------------------------------------------------- *//* *//* Hashtable functions *//* *//* --------------------------------------------------------- */typedef struct xhn_struct{ struct xhn_struct *next; const char *key; void *val;} *xhn, _xhn;typedef struct xht_struct{ pool p; int prime; int dirty; int count; struct xhn_struct *zen; int iter_bucket; xhn iter_node;} *xht, _xht;xht xhash_new(int prime);void xhash_put(xht h, const char *key, void *val);void xhash_putx(xht h, const char *key, int len, void *val);void *xhash_get(xht h, const char *key);void *xhash_getx(xht h, const char *key, int len);void xhash_zap(xht h, const char *key);void xhash_zapx(xht h, const char *key, int len);void xhash_free(xht h);typedef void (*xhash_walker)(xht h, const char *key, void *val, void *arg);void xhash_walk(xht h, xhash_walker w, void *arg);int xhash_dirty(xht h);int xhash_count(xht h);pool xhash_pool(xht h);/* iteration functions */int xhash_iter_first(xht h);int xhash_iter_next(xht h);void xhash_iter_zap(xht h);void xhash_iter_get(xht h, const char **key, void **val);/* --------------------------------------------------------- *//* *//* XML escaping utils *//* *//* --------------------------------------------------------- */char *strescape(pool p, char *buf, int len); /* Escape <>&'" chars */char *strunescape(pool p, char *buf);/* --------------------------------------------------------- *//* *//* String pools (spool) functions *//* *//* --------------------------------------------------------- */struct spool_node{ char *c; struct spool_node *next;};typedef struct spool_struct{ pool p; int len; struct spool_node *last; struct spool_node *first;} *spool;spool spool_new(pool p); /* create a string pool */void spooler(spool s, ...); /* append all the char * args to the pool, terminate args with s again */char *spool_print(spool s); /* return a big string */void spool_add(spool s, char *str); /* add a single string to the pool */void spool_escape(spool s, char *raw, int len); /* add and xml escape a single string to the pool */char *spools(pool p, ...); /* wrap all the spooler stuff in one function, the happy fun ball! *//* known namespace uri */#define uri_STREAMS "http://etherx.jabber.org/streams"#define uri_CLIENT "jabber:client"#define uri_SERVER "jabber:server"#define uri_DIALBACK "jabber:server:dialback"#define uri_TLS "urn:ietf:params:xml:ns:xmpp-tls"#define uri_SASL "urn:ietf:params:xml:ns:xmpp-sasl"#define uri_BIND "urn:ietf:params:xml:ns:xmpp-bind"#define uri_XSESSION "urn:ietf:params:xml:ns:xmpp-session"#define uri_STREAM_ERR "urn:ietf:params:xml:ns:xmpp-streams"#define uri_STANZA_ERR "urn:ietf:params:xml:ns:xmpp-stanzas"#define uri_COMPONENT "http://jabberd.jabberstudio.org/ns/component/1.0"#define uri_SESSION "http://jabberd.jabberstudio.org/ns/session/1.0"#define uri_RESOLVER "http://jabberd.jabberstudio.org/ns/resolver/1.0"#define uri_XDATA "jabber:x:data"#define uri_XML "http://www.w3.org/XML/1998/namespace"/* * JID manipulation. Validity is checked via stringprep, using the "nodeprep", * "nameprep" and "resourceprep" profiles (see xmpp-core section 3). * * The provided functions are mainly for convenience. The application should * fill out node, domain and resource directly. When they modify these, they * should either call jid_expand(), or set the dirty flag. *//** preparation cache, for speed */typedef struct prep_cache_st { xht node; xht domain; xht resource;} *prep_cache_t;prep_cache_t prep_cache_new(void);void prep_cache_free(prep_cache_t pc);char *prep_cache_node_get(prep_cache_t pc, char *from);void prep_cache_node_set(prep_cache_t pc, char *from, char *to);char *prep_cache_domain_get(prep_cache_t pc, char *from);void prep_cache_domain_set(prep_cache_t pc, char *from, char *to);char *prep_cache_resource_get(prep_cache_t pc, char *from);void prep_cache_resource_set(prep_cache_t pc, char *from, char *to);/** these sizings come from xmpp-core */typedef struct jid_st { /* cache for prep, if any */ prep_cache_t pc; /* basic components of the jid */ unsigned char node[1024]; unsigned char domain[1024]; unsigned char resource[1024]; /* the "user" part of the jid (sans resource) */ unsigned char *_user; /* node(1023) + '@'(1) + domain(1023) + '\0' */ /* the complete jid */ unsigned char *_full; /* node(1023) + '@'(1) + domain(1023) + '/'(1) + resource(1023) + '\0'(1) == 770 */ /* application should set to 1 if user/full need regenerating */ int dirty; /* for lists of jids */ struct jid_st *next;} *jid_t;/** make a new jid, and call jid_reset() to populate it */jid_t jid_new(prep_cache_t pc, const unsigned char *id, int len);/** clear and populate the jid with the given id. if id == NULL, just clears the jid to 0 */jid_t jid_reset(jid_t jid, const unsigned char *id, int len);/** free the jid */void jid_free(jid_t jid);/** do string preparation on a jid */int jid_prep(jid_t jid);/** expands user and full if the dirty flag is set */void jid_expand(jid_t jid);/** return the user or full jid. these call jid_expand to make sure the user and * full jid are up to date */const unsigned char *jid_user(jid_t jid);const unsigned char *jid_full(jid_t jid);/** compare two user or full jids. these call jid_expand, then strcmp. returns * 0 if they're the same, < 0 if a < b, > 0 if a > b */int jid_compare_user(jid_t a, jid_t b);int jid_compare_full(jid_t a, jid_t b);/** duplicate a jid */jid_t jid_dup(jid_t jid);/** list helpers *//** see if a jid is present in a list */int jid_search(jid_t list, jid_t jid);/** remove a jid from a list, and return the new list */jid_t jid_zap(jid_t list, jid_t jid);/** insert of a copy of jid into list, avoiding dups */jid_t jid_append(jid_t list, jid_t jid);/* logging */typedef enum { log_STDOUT, log_SYSLOG, log_FILE} log_type_t;typedef struct log_st{ log_type_t type; FILE *file;} *log_t;typedef struct log_facility_st
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -