From 947acb45106f9fc9b1dab32a1d815f5d8d084e68 Mon Sep 17 00:00:00 2001 From: Benny Prijono Date: Tue, 8 Nov 2005 09:51:21 +0000 Subject: Split PJLIB utilities into pjutil git-svn-id: http://svn.pjsip.org/repos/pjproject/main@29 74dad513-b988-da41-8d7b-12977e46ad98 --- pjlib/include/pj++/hash.hpp | 73 +++++++ pjlib/include/pj++/ioqueue.hpp | 174 +++++++++++++++ pjlib/include/pj++/list.hpp | 184 ++++++++++++++++ pjlib/include/pj++/os.hpp | 344 ++++++++++++++++++++++++++++++ pjlib/include/pj++/pool.hpp | 86 ++++++++ pjlib/include/pj++/proactor.hpp | 88 ++++++++ pjlib/include/pj++/scanner.hpp | 173 +++++++++++++++ pjlib/include/pj++/sock.hpp | 196 +++++++++++++++++ pjlib/include/pj++/string.hpp | 249 ++++++++++++++++++++++ pjlib/include/pj++/timer.hpp | 107 ++++++++++ pjlib/include/pj++/tree.hpp | 109 ++++++++++ pjlib/include/pj++/types.hpp | 61 ++++++ pjlib/include/pj/md5.h | 94 --------- pjlib/include/pj/scanner.h | 456 ---------------------------------------- pjlib/include/pj/stun.h | 123 ----------- pjlib/include/pj/xml.h | 157 -------------- 16 files changed, 1844 insertions(+), 830 deletions(-) create mode 100644 pjlib/include/pj++/hash.hpp create mode 100644 pjlib/include/pj++/ioqueue.hpp create mode 100644 pjlib/include/pj++/list.hpp create mode 100644 pjlib/include/pj++/os.hpp create mode 100644 pjlib/include/pj++/pool.hpp create mode 100644 pjlib/include/pj++/proactor.hpp create mode 100644 pjlib/include/pj++/scanner.hpp create mode 100644 pjlib/include/pj++/sock.hpp create mode 100644 pjlib/include/pj++/string.hpp create mode 100644 pjlib/include/pj++/timer.hpp create mode 100644 pjlib/include/pj++/tree.hpp create mode 100644 pjlib/include/pj++/types.hpp delete mode 100644 pjlib/include/pj/md5.h delete mode 100644 pjlib/include/pj/scanner.h delete mode 100644 pjlib/include/pj/stun.h delete mode 100644 pjlib/include/pj/xml.h (limited to 'pjlib/include') diff --git a/pjlib/include/pj++/hash.hpp b/pjlib/include/pj++/hash.hpp new file mode 100644 index 00000000..26f48010 --- /dev/null +++ b/pjlib/include/pj++/hash.hpp @@ -0,0 +1,73 @@ +/* $Id$ + * + */ +#ifndef __PJPP_HASH_H__ +#define __PJPP_HASH_H__ + +#include +#include + +class PJ_Hash_Table +{ +public: + class iterator + { + public: + iterator() {} + explicit iterator(pj_hash_table_t *h, pj_hash_iterator_t *i) : ht_(h), it_(i) {} + iterator(const iterator &rhs) : ht_(rhs.ht_), it_(rhs.it_) {} + void operator++() { it_ = pj_hash_next(ht_, it_); } + bool operator==(const iterator &rhs) { return ht_ == rhs.ht_ && it_ == rhs.it_; } + iterator & operator=(const iterator &rhs) { ht_=rhs.ht_; it_=rhs.it_; return *this; } + private: + pj_hash_table_t *ht_; + pj_hash_iterator_t it_val_; + pj_hash_iterator_t *it_; + + friend class PJ_Hash_Table; + }; + + static PJ_Hash_Table *create(PJ_Pool *pool, unsigned size) + { + return (PJ_Hash_Table*) pj_hash_create(pool->pool_(), size); + } + + static pj_uint32_t calc(pj_uint32_t initial_hval, const void *key, unsigned keylen) + { + return pj_hash_calc(initial_hval, key, keylen); + } + + pj_hash_table_t *hash_table_() + { + return (pj_hash_table_t*)this; + } + + void *get(const void *key, unsigned keylen) + { + return pj_hash_get(this->hash_table_(), key, keylen); + } + + void set(PJ_Pool *pool, const void *key, unsigned keylen, void *value) + { + pj_hash_set(pool->pool_(), this->hash_table_(), key, keylen, value); + } + + unsigned count() + { + return pj_hash_count(this->hash_table_()); + } + + iterator begin() + { + iterator it(this->hash_table_(), NULL); + it.it_ = pj_hash_first(this->hash_table_(), &it.it_val_); + return it; + } + + iterator end() + { + return iterator(this->hash_table_(), NULL); + } +}; + +#endif /* __PJPP_HASH_H__ */ diff --git a/pjlib/include/pj++/ioqueue.hpp b/pjlib/include/pj++/ioqueue.hpp new file mode 100644 index 00000000..5724ecd6 --- /dev/null +++ b/pjlib/include/pj++/ioqueue.hpp @@ -0,0 +1,174 @@ +/* $Id$ + * + */ +#ifndef __PJPP_IOQUEUE_H__ +#define __PJPP_IOQUEUE_H__ + +#include +#include +#include +#include + +class PJ_IOQueue; + +class PJ_IOQueue_Event_Handler +{ +public: + virtual ~PJ_IOQueue_Event_Handler() + { + } + + pj_ioqueue_key_t* get_key() const + { + return key_; + } + +protected: + // + // Override this to get notification from I/O Queue + // + virtual void on_read_complete(pj_ssize_t bytes_read) + { + } + + virtual void on_write_complete(pj_ssize_t bytes_sent) + { + } + + virtual void on_accept_complete(int status) + { + } + + virtual void on_connect_complete(int status) + { + } + +protected: + PJ_IOQueue_Event_Handler() + : ioqueue_(NULL), key_(NULL) + { + } + +private: + PJ_IOQueue *ioqueue_; + pj_ioqueue_key_t *key_; + + static void read_complete_cb(pj_ioqueue_key_t *key, pj_ssize_t bytes_read) + { + PJ_IOQueue_Event_Handler *handler = + (PJ_IOQueue_Event_Handler*)pj_ioqueue_get_user_data(key); + handler->on_read_complete(bytes_read); + } + + static void write_complete_cb(pj_ioqueue_key_t *key, pj_ssize_t bytes_sent); + static void accept_complete_cb(pj_ioqueue_key_t *key, int status); + static void connect_complete_cb(pj_ioqueue_key_t *key, int status); + + friend class PJ_IOQueue; +}; + + +class PJ_IOQueue +{ + typedef pj_ioqueue_t *B_; + +public: + typedef pj_ioqueue_key_t Key; + + enum Operation + { + OP_NONE = PJ_IOQUEUE_OP_NONE, + OP_READ = PJ_IOQUEUE_OP_READ, + OP_RECV_FROM = PJ_IOQUEUE_OP_RECV_FROM, + OP_WRITE = PJ_IOQUEUE_OP_WRITE, + OP_SEND_TO = PJ_IOQUEUE_OP_SEND_TO, +#if PJ_HAS_TCP + OP_ACCEPT = PJ_IOQUEUE_OP_ACCEPT, + OP_CONNECT = PJ_IOQUEUE_OP_CONNECT, +#endif + }; + + enum Status + { + IS_PENDING = PJ_IOQUEUE_PENDING + }; + + static PJ_IOQueue *create(PJ_Pool *pool, pj_size_t max_fd) + { + return (PJ_IOQueue*) pj_ioqueue_create(pool->pool_(), max_fd); + } + + operator B_() + { + return (pj_ioqueue_t*)(PJ_IOQueue*)this; + } + + pj_ioqueue_t *ioq_() + { + return (B_)this; + } + + void destroy() + { + pj_ioqueue_destroy(this->ioq_()); + } + + Key *register_handle(PJ_Pool *pool, pj_oshandle_t hnd, void *user_data) + { + return pj_ioqueue_register(pool->pool_(), this->ioq_(), hnd, user_data); + } + + Key *register_socket(PJ_Pool *pool, pj_sock_t hnd, void *user_data) + { + return pj_ioqueue_register(pool->pool_(), this->ioq_(), (pj_oshandle_t)hnd, user_data); + } + + pj_status_t unregister(Key *key) + { + return pj_ioqueue_unregister(this->ioq_(), key); + } + + void *get_user_data(Key *key) + { + return pj_ioqueue_get_user_data(key); + } + + int poll(Key **key, pj_ssize_t *bytes_status, Operation *op, const PJ_Time_Val *timeout) + { + return pj_ioqueue_poll(this->ioq_(), key, bytes_status, (pj_ioqueue_operation_e*)op, timeout); + } + +#if PJ_HAS_TCP + pj_status_t connect(Key *key, const pj_sockaddr_t *addr, int addrlen) + { + return pj_ioqueue_connect(this->ioq_(), key, addr, addrlen); + } + + pj_status_t accept(Key *key, PJ_Socket *sock, pj_sockaddr_t *local, pj_sockaddr_t *remote, int *addrlen) + { + return pj_ioqueue_accept(this->ioq_(), key, &sock->get_handle(), local, remote, addrlen); + } +#endif + + int read(Key *key, void *buf, pj_size_t len) + { + return pj_ioqueue_read(this->ioq_(), key, buf, len); + } + + int recvfrom(Key *key, void *buf, pj_size_t len, pj_sockaddr_t *addr, int *addrlen) + { + return pj_ioqueue_recvfrom(this->ioq_(), key, buf, len, addr, addrlen); + } + + int write(Key *key, const void *data, pj_size_t len) + { + return pj_ioqueue_write(this->ioq_(), key, data, len); + } + + int sendto(Key *key, const void *data, pj_size_t len, const pj_sockaddr_t *addr, int addrlen) + { + return pj_ioqueue_sendto(this->ioq_(), key, data, len, addr, addrlen); + } +}; + +#endif /* __PJPP_IOQUEUE_H__ */ diff --git a/pjlib/include/pj++/list.hpp b/pjlib/include/pj++/list.hpp new file mode 100644 index 00000000..68076785 --- /dev/null +++ b/pjlib/include/pj++/list.hpp @@ -0,0 +1,184 @@ +/* $Id$ + * + */ +#ifndef __PJPP_LIST_H__ +#define __PJPP_LIST_H__ + +#include + +template +struct PJ_List_Node +{ + PJ_DECL_LIST_MEMBER(T) +}; + + +template +class PJ_List +{ +public: + PJ_List() { pj_list_init(&root_); if (0) compiletest(); } + ~PJ_List() {} + + class const_iterator + { + public: + const_iterator() : node_(NULL) {} + const_iterator(const Node *nd) : node_((Node*)nd) {} + const Node * operator *() { return node_; } + const Node * operator -> () { return node_; } + const_iterator operator++() { return const_iterator(node_->next); } + bool operator==(const const_iterator &rhs) { return node_ == rhs.node_; } + bool operator!=(const const_iterator &rhs) { return node_ != rhs.node_; } + + protected: + Node *node_; + }; + + class iterator : public const_iterator + { + public: + iterator() {} + iterator(Node *nd) : const_iterator(nd) {} + Node * operator *() { return node_; } + Node * operator -> () { return node_; } + iterator operator++() { return iterator(node_->next); } + bool operator==(const iterator &rhs) { return node_ == rhs.node_; } + bool operator!=(const iterator &rhs) { return node_ != rhs.node_; } + }; + + bool empty() const + { + return pj_list_empty(&root_); + } + + iterator begin() + { + return iterator(root_.next); + } + + const_iterator begin() const + { + return const_iterator(root_.next); + } + + const_iterator end() const + { + return const_iterator((Node*)&root_); + } + + iterator end() + { + return iterator((Node*)&root_); + } + + void insert_before (iterator &pos, Node *node) + { + pj_list_insert_before( *pos, node ); + } + + void insert_after(iterator &pos, Node *node) + { + pj_list_insert_after(*pos, node); + } + + void merge_first(Node *list2) + { + pj_list_merge_first(&root_, list2); + } + + void merge_last(PJ_List *list) + { + pj_list_merge_last(&root_, &list->root_); + } + + void insert_nodes_before(iterator &pos, PJ_List *list2) + { + pj_list_insert_nodes_before(*pos, &list2->root_); + } + + void insert_nodes_after(iterator &pos, PJ_List *list2) + { + pj_list_insert_nodes_after(*pos, &list2->root_); + } + + void erase(iterator &it) + { + pj_list_erase(*it); + } + + Node *front() + { + return root_.next; + } + + const Node *front() const + { + return root_.next; + } + + void pop_front() + { + pj_list_erase(root_.next); + } + + Node *back() + { + return root_.prev; + } + + const Node *back() const + { + return root_.prev; + } + + void pop_back() + { + pj_list_erase(root_.prev); + } + + iterator find(Node *node) + { + Node *n = pj_list_find_node(&root_, node); + return n ? iterator(n) : end(); + } + + const_iterator find(Node *node) const + { + Node *n = pj_list_find_node(&root_, node); + return n ? const_iterator(n) : end(); + } + + void push_back(Node *node) + { + pj_list_insert_after(root_.prev, node); + } + + void push_front(Node *node) + { + pj_list_insert_before(root_.next, node); + } + + void clear() + { + root_.next = &root_; + root_.prev = &root_; + } + +private: + struct RootNode + { + PJ_DECL_LIST_MEMBER(Node) + } root_; + + void compiletest() + { + // If you see error in this line, + // it's because Node is not derived from PJ_List_Node. + Node *n = (Node*)0; + n = n->next; n = n->prev; + } +}; + + +#endif /* __PJPP_LIST_H__ */ diff --git a/pjlib/include/pj++/os.hpp b/pjlib/include/pj++/os.hpp new file mode 100644 index 00000000..af89ac87 --- /dev/null +++ b/pjlib/include/pj++/os.hpp @@ -0,0 +1,344 @@ +/* $Id$ + * + */ +#ifndef __PJPP_OS_H__ +#define __PJPP_OS_H__ + +#include +#include +#include + +class PJ_Thread +{ +public: + enum Flags + { + FLAG_SUSPENDED = PJ_THREAD_SUSPENDED + }; + + static PJ_Thread *create( PJ_Pool *pool, const char *thread_name, + pj_thread_proc *proc, void *arg, + pj_size_t stack_size, void *stack_ptr, + unsigned flags) + { + return (PJ_Thread*) pj_thread_create( pool->pool_(), thread_name, proc, arg, stack_size, stack_ptr, flags); + } + + static PJ_Thread *register_current_thread(const char *name, pj_thread_desc desc) + { + return (PJ_Thread*) pj_thread_register(name, desc); + } + + static PJ_Thread *get_current_thread() + { + return (PJ_Thread*) pj_thread_this(); + } + + static pj_status_t sleep(unsigned msec) + { + return pj_thread_sleep(msec); + } + + static pj_status_t usleep(unsigned usec) + { + return pj_thread_usleep(usec); + } + + pj_thread_t *pj_thread_t_() + { + return (pj_thread_t*)this; + } + + const char *get_name() + { + return pj_thread_get_name( this->pj_thread_t_() ); + } + + pj_status_t resume() + { + return pj_thread_resume( this->pj_thread_t_() ); + } + + pj_status_t join() + { + return pj_thread_join( this->pj_thread_t_() ); + } + + pj_status_t destroy() + { + return pj_thread_destroy( this->pj_thread_t_() ); + } +}; + + +class PJ_Thread_Local +{ +public: + static PJ_Thread_Local *alloc() + { + long index = pj_thread_local_alloc(); + return index < 0 ? NULL : (PJ_Thread_Local*)index; + } + void free() + { + pj_thread_local_free( this->tls_() ); + } + + long tls_() const + { + return (long)this; + } + + void set(void *value) + { + pj_thread_local_set( this->tls_(), value ); + } + + void *get() + { + return pj_thread_local_get( this->tls_() ); + } +}; + + +class PJ_Atomic +{ +public: + static PJ_Atomic *create(PJ_Pool *pool, long initial) + { + return (PJ_Atomic*) pj_atomic_create(pool->pool_(), initial); + } + + pj_atomic_t *pj_atomic_t_() + { + return (pj_atomic_t*)this; + } + + pj_status_t destroy() + { + return pj_atomic_destroy( this->pj_atomic_t_() ); + } + + long set(long val) + { + return pj_atomic_set( this->pj_atomic_t_(), val); + } + + long get() + { + return pj_atomic_get( this->pj_atomic_t_() ); + } + + long inc() + { + return pj_atomic_inc( this->pj_atomic_t_() ); + } + + long dec() + { + return pj_atomic_dec( this->pj_atomic_t_() ); + } +}; + + +class PJ_Mutex +{ +public: + enum Type + { + DEFAULT = PJ_MUTEX_DEFAULT, + SIMPLE = PJ_MUTEX_SIMPLE, + RECURSE = PJ_MUTEX_RECURSE, + }; + + static PJ_Mutex *create( PJ_Pool *pool, const char *name, Type type) + { + return (PJ_Mutex*) pj_mutex_create( pool->pool_(), name, type); + } + + pj_mutex_t *pj_mutex_() + { + return (pj_mutex_t*)this; + } + + pj_status_t destroy() + { + return pj_mutex_destroy( this->pj_mutex_() ); + } + + pj_status_t lock() + { + return pj_mutex_lock( this->pj_mutex_() ); + } + + pj_status_t unlock() + { + return pj_mutex_unlock( this->pj_mutex_() ); + } + + pj_status_t trylock() + { + return pj_mutex_trylock( this->pj_mutex_() ); + } + +#if PJ_DEBUG + pj_status_t is_locked() + { + return pj_mutex_is_locked( this->pj_mutex_() ); + } +#endif +}; + + +class PJ_Semaphore +{ +public: + static PJ_Semaphore *create( PJ_Pool *pool, const char *name, unsigned initial, unsigned max) + { + return (PJ_Semaphore*) pj_sem_create( pool->pool_(), name, initial, max); + } + + pj_sem_t *pj_sem_t_() + { + return (pj_sem_t*)this; + } + + pj_status_t destroy() + { + return pj_sem_destroy(this->pj_sem_t_()); + } + + pj_status_t wait() + { + return pj_sem_wait(this->pj_sem_t_()); + } + + pj_status_t lock() + { + return wait(); + } + + pj_status_t trywait() + { + return pj_sem_trywait(this->pj_sem_t_()); + } + + pj_status_t trylock() + { + return trywait(); + } + + pj_status_t post() + { + return pj_sem_post(this->pj_sem_t_()); + } + + pj_status_t unlock() + { + return post(); + } +}; + + +class PJ_Event +{ +public: + static PJ_Event *create( PJ_Pool *pool, const char *name, bool manual_reset, bool initial) + { + return (PJ_Event*) pj_event_create(pool->pool_(), name, manual_reset, initial); + } + + pj_event_t *pj_event_t_() + { + return (pj_event_t*)this; + } + + pj_status_t destroy() + { + return pj_event_destroy(this->pj_event_t_()); + } + + pj_status_t wait() + { + return pj_event_wait(this->pj_event_t_()); + } + + pj_status_t trywait() + { + return pj_event_trywait(this->pj_event_t_()); + } + + pj_status_t set() + { + return pj_event_set(this->pj_event_t_()); + } + + pj_status_t pulse() + { + return pj_event_pulse(this->pj_event_t_()); + } + + pj_status_t reset() + { + return pj_event_reset(this->pj_event_t_()); + } +}; + +class PJ_OS +{ +public: + static pj_status_t gettimeofday( PJ_Time_Val *tv ) + { + return pj_gettimeofday(tv); + } + + static pj_status_t time_decode( const PJ_Time_Val *tv, pj_parsed_time *pt ) + { + return pj_time_decode(tv, pt); + } + + static pj_status_t time_encode(const pj_parsed_time *pt, PJ_Time_Val *tv) + { + return pj_time_encode(pt, tv); + } + + static pj_status_t time_local_to_gmt( PJ_Time_Val *tv ) + { + return pj_time_local_to_gmt( tv ); + } + + static pj_status_t time_gmt_to_local( PJ_Time_Val *tv) + { + return pj_time_gmt_to_local( tv ); + } +}; + + +inline pj_status_t PJ_Time_Val::gettimeofday() +{ + return PJ_OS::gettimeofday(this); +} + +inline pj_parsed_time PJ_Time_Val::decode() +{ + pj_parsed_time pt; + PJ_OS::time_decode(this, &pt); + return pt; +} + +inline pj_status_t PJ_Time_Val::encode(const pj_parsed_time *pt) +{ + return PJ_OS::time_encode(pt, this); +} + +inline pj_status_t PJ_Time_Val::to_gmt() +{ + return PJ_OS::time_local_to_gmt(this); +} + +inline pj_status_t PJ_Time_Val::to_local() +{ + return PJ_OS::time_gmt_to_local(this); +} + +#endif /* __PJPP_OS_H__ */ diff --git a/pjlib/include/pj++/pool.hpp b/pjlib/include/pj++/pool.hpp new file mode 100644 index 00000000..d2af77bb --- /dev/null +++ b/pjlib/include/pj++/pool.hpp @@ -0,0 +1,86 @@ +/* $Id$ + * + */ +#ifndef __PJPP_POOL_H__ +#define __PJPP_POOL_H__ + +#include + +class PJ_Pool +{ +public: + const char *getobjname() const + { + return pj_pool_getobjname(this->pool_()); + } + + pj_pool_t *pool_() + { + return (pj_pool_t*)this; + } + + const pj_pool_t *pool_() const + { + return (const pj_pool_t*)this; + } + + void release() + { + pj_pool_release(this->pool_()); + } + + void reset() + { + pj_pool_reset(this->pool_()); + } + + pj_size_t get_capacity() + { + pj_pool_get_capacity(this->pool_()); + } + + pj_size_t get_used_size() + { + pj_pool_get_used_size(this->pool_()); + } + + void *alloc(pj_size_t size) + { + return pj_pool_alloc(this->pool_(), size); + } + + void *calloc(pj_size_t count, pj_size_t elem) + { + return pj_pool_calloc(this->pool_(), count, elem); + } +}; + +class PJ_Caching_Pool +{ +public: + void init(pj_size_t max_capacity, + const pj_pool_factory_policy *pol=&pj_pool_factory_default_policy) + { + pj_caching_pool_init(&cp_, pol, max_capacity); + } + + void destroy() + { + pj_caching_pool_destroy(&cp_); + } + + PJ_Pool *create_pool(const char *name, pj_size_t initial_size, pj_size_t increment_size, pj_pool_callback *callback) + { + return (PJ_Pool*) (*cp_.factory.create_pool)(&cp_.factory, name, initial_size, increment_size, callback); + } + + void release_pool( PJ_Pool *pool ) + { + pj_pool_release(pool->pool_()); + } + +private: + pj_caching_pool cp_; +}; + +#endif /* __PJPP_POOL_H__ */ diff --git a/pjlib/include/pj++/proactor.hpp b/pjlib/include/pj++/proactor.hpp new file mode 100644 index 00000000..cae9cf43 --- /dev/null +++ b/pjlib/include/pj++/proactor.hpp @@ -0,0 +1,88 @@ +/* $Id$ + * + */ +#ifndef __PJPP_EVENT_HANDLER_H__ +#define __PJPP_EVENT_HANDLER_H__ + +#include +#include +#include +#include + +class PJ_Proactor; + + +class PJ_Event_Handler +{ + friend class PJ_Proactor; +public: + PJ_Event_Handler(); + virtual ~PJ_Event_Handler(); + + virtual pj_oshandle_t get_handle() = 0; + + bool read(void *buf, pj_size_t len); + bool recvfrom(void *buf, pj_size_t len, PJ_INET_Addr *addr); + bool write(const void *data, pj_size_t len); + bool sendto(const void *data, pj_size_t len, const PJ_INET_Addr &addr); +#if PJ_HAS_TCP + bool connect(const PJ_INET_Addr &addr); + bool accept(PJ_Socket *sock, PJ_INET_Addr *local=NULL, PJ_INET_Addr *remote=NULL); +#endif + +protected: + // + // Overridables + // + virtual void on_timeout(int data) {} + virtual void on_read_complete(pj_ssize_t bytes_read) {} + virtual void on_write_complete(pj_ssize_t bytes_sent) {} +#if PJ_HAS_TCP + virtual void on_connect_complete(int status) {} + virtual void on_accept_complete(int status) {} +#endif + +private: + PJ_Proactor *proactor_; + pj_ioqueue_key_t *key_; + pj_timer_entry timer_; + int tmp_recvfrom_addr_len; + +public: + // Internal IO Queue/timer callback. + static void timer_callback( pj_timer_heap_t *timer_heap, struct pj_timer_entry *entry); + static void read_complete_cb(pj_ioqueue_key_t *key, pj_ssize_t bytes_read); + static void write_complete_cb(pj_ioqueue_key_t *key, pj_ssize_t bytes_sent); + static void accept_complete_cb(pj_ioqueue_key_t *key, int status); + static void connect_complete_cb(pj_ioqueue_key_t *key, int status); +}; + +class PJ_Proactor +{ +public: + static PJ_Proactor *create(PJ_Pool *pool, pj_size_t max_fd, + pj_size_t timer_entry_count, unsigned timer_flags=0); + + void destroy(); + + bool register_handler(PJ_Pool *pool, PJ_Event_Handler *handler); + void unregister_handler(PJ_Event_Handler *handler); + + static bool schedule_timer( pj_timer_heap_t *timer, PJ_Event_Handler *handler, + const PJ_Time_Val &delay, int id=-1); + bool schedule_timer(PJ_Event_Handler *handler, const PJ_Time_Val &delay, int id=-1); + bool cancel_timer(PJ_Event_Handler *handler); + + bool handle_events(PJ_Time_Val *timeout); + + pj_ioqueue_t *get_io_queue(); + pj_timer_heap_t *get_timer_heap(); + +private: + pj_ioqueue_t *ioq_; + pj_timer_heap_t *th_; + + PJ_Proactor() {} +}; + +#endif /* __PJPP_EVENT_HANDLER_H__ */ diff --git a/pjlib/include/pj++/scanner.hpp b/pjlib/include/pj++/scanner.hpp new file mode 100644 index 00000000..80ac0a8b --- /dev/null +++ b/pjlib/include/pj++/scanner.hpp @@ -0,0 +1,173 @@ +/* $Id$ + * + */ +#ifndef __PJPP_SCANNER_H__ +#define __PJPP_SCANNER_H__ + +#include +#include + +class PJ_CharSpec +{ +public: + PJ_CharSpec() { pj_cs_init(cs__); } + + void set(int c) { pj_cs_set(cs__, c); } + void add_range(int begin, int end) { pj_cs_add_range(cs__, begin, end); } + void add_alpha() { pj_cs_add_alpha(cs__); } + void add_num() { pj_cs_add_num(cs__); } + void add_str(const char *str) { pj_cs_add_str(cs__, str); } + void del_range(int begin, int end) { pj_cs_del_range(cs__, begin, end); } + void del_str(const char *str) { pj_cs_del_str(cs__, str); } + void invert() { pj_cs_invert(cs__); } + int match(int c) { return pj_cs_match(cs__, c); } + + pj_char_spec_element_t *cs_() + { + return cs__; + } + + const pj_char_spec_element_t *cs_() const + { + return cs__; + } + +private: + pj_char_spec cs__; +}; + +class PJ_Scanner +{ +public: + PJ_Scanner() {} + + enum + { + SYNTAX_ERROR = 101 + }; + static void syntax_error_handler_throw_pj(pj_scanner *); + + typedef pj_scan_state State; + + void init(char *buf, int len, unsigned options=PJ_SCAN_AUTOSKIP_WS, + pj_syn_err_func_ptr callback = &syntax_error_handler_throw_pj) + { + pj_scan_init(&scanner_, buf, len, options, callback); + } + + void fini() + { + pj_scan_fini(&scanner_); + } + + int eof() const + { + return pj_scan_is_eof(&scanner_); + } + + int peek_char() const + { + return *scanner_.current; + } + + int peek(const PJ_CharSpec *cs, PJ_String *out) + { + return pj_scan_peek(&scanner_, cs->cs_(), out); + } + + int peek_n(pj_size_t len, PJ_String *out) + { + return pj_scan_peek_n(&scanner_, len, out); + } + + int peek_until(const PJ_CharSpec *cs, PJ_String *out) + { + return pj_scan_peek_until(&scanner_, cs->cs_(), out); + } + + void get(const PJ_CharSpec *cs, PJ_String *out) + { + pj_scan_get(&scanner_, cs->cs_(), out); + } + + void get_n(unsigned N, PJ_String *out) + { + pj_scan_get_n(&scanner_, N, out); + } + + int get_char() + { + return pj_scan_get_char(&scanner_); + } + + void get_quote(int begin_quote, int end_quote, PJ_String *out) + { + pj_scan_get_quote(&scanner_, begin_quote, end_quote, out); + } + + void get_newline() + { + pj_scan_get_newline(&scanner_); + } + + void get_until(const PJ_CharSpec *cs, PJ_String *out) + { + pj_scan_get_until(&scanner_, cs->cs_(), out); + } + + void get_until_ch(int until_ch, PJ_String *out) + { + pj_scan_get_until_ch(&scanner_, until_ch, out); + } + + void get_until_chr(const char *spec, PJ_String *out) + { + pj_scan_get_until_chr(&scanner_, spec, out); + } + + void advance_n(unsigned N, bool skip_ws=true) + { + pj_scan_advance_n(&scanner_, N, skip_ws); + } + + int strcmp(const char *s, int len) + { + return pj_scan_strcmp(&scanner_, s, len); + } + + int stricmp(const char *s, int len) + { + return pj_scan_stricmp(&scanner_, s, len); + } + + void skip_ws() + { + pj_scan_skip_whitespace(&scanner_); + } + + void save_state(State *state) + { + pj_scan_save_state(&scanner_, state); + } + + void restore_state(State *state) + { + pj_scan_restore_state(&scanner_, state); + } + + int get_pos_line() const + { + return scanner_.line; + } + + int get_pos_col() const + { + return scanner_.col; + } + + +private: + pj_scanner scanner_; +}; + +#endif /* __PJPP_SCANNER_H__ */ diff --git a/pjlib/include/pj++/sock.hpp b/pjlib/include/pj++/sock.hpp new file mode 100644 index 00000000..a38fd22d --- /dev/null +++ b/pjlib/include/pj++/sock.hpp @@ -0,0 +1,196 @@ +/* $Id$ + * + */ +#ifndef __PJPP_SOCK_H__ +#define __PJPP_SOCK_H__ + +#include + +class PJ_Addr +{ +}; + +class PJ_INET_Addr : public pj_sockaddr_in, public PJ_Addr +{ +public: + pj_uint16_t get_port_number() const + { + return pj_sockaddr_get_port(this); + } + + void set_port_number(pj_uint16_t port) + { + sin_family = PJ_AF_INET; + pj_sockaddr_set_port(this, port); + } + + pj_uint32_t get_ip_address() const + { + return pj_sockaddr_get_addr(this); + } + + const char *get_address() const + { + return pj_sockaddr_get_str_addr(this); + } + + void set_ip_address(pj_uint32_t addr) + { + sin_family = PJ_AF_INET; + pj_sockaddr_set_addr(this, addr); + } + + pj_status_t set_address(const pj_str_t *addr) + { + return pj_sockaddr_set_str_addr(this, addr); + } + + pj_status_t set_address(const char *addr) + { + return pj_sockaddr_set_str_addr2(this, addr); + } + + int cmp(const PJ_INET_Addr &rhs) const + { + return pj_sockaddr_cmp(this, &rhs); + } + + bool operator==(const PJ_INET_Addr &rhs) const + { + return cmp(rhs) == 0; + } +}; + +class PJ_Socket +{ +public: + PJ_Socket() {} + PJ_Socket(const PJ_Socket &rhs) : sock_(rhs.sock_) {} + + void set_handle(pj_sock_t sock) + { + sock_ = sock; + } + + pj_sock_t get_handle() const + { + return sock_; + } + + pj_sock_t& get_handle() + { + return sock_; + } + + bool socket(int af, int type, int proto, pj_uint32_t flag=0) + { + sock_ = pj_sock_socket(af, type, proto, flag); + return sock_ != -1; + } + + bool bind(const PJ_INET_Addr &addr) + { + return pj_sock_bind(sock_, &addr, sizeof(PJ_INET_Addr)) == 0; + } + + bool close() + { + return pj_sock_close(sock_) == 0; + } + + bool getpeername(PJ_INET_Addr *addr) + { + int namelen; + return pj_sock_getpeername(sock_, addr, &namelen) == 0; + } + + bool getsockname(PJ_INET_Addr *addr) + { + int namelen; + return pj_sock_getsockname(sock_, addr, &namelen) == 0; + } + + bool getsockopt(int level, int optname, void *optval, int *optlen) + { + return pj_sock_getsockopt(sock_, level, optname, optval, optlen) == 0; + } + + bool setsockopt(int level, int optname, const void *optval, int optlen) + { + return pj_sock_setsockopt(sock_, level, optname, optval, optlen) == 0; + } + + bool ioctl(long cmd, pj_uint32_t *val) + { + return pj_sock_ioctl(sock_, cmd, val) == 0; + } + + int recv(void *buf, int len, int flag = 0) + { + return pj_sock_recv(sock_, buf, len, flag); + } + + int send(const void *buf, int len, int flag = 0) + { + return pj_sock_send(sock_, buf, len, flag); + } + +protected: + pj_sock_t sock_; +}; + +#if PJ_HAS_TCP +class PJ_Sock_Stream : public PJ_Socket +{ +public: + PJ_Sock_Stream() {} + PJ_Sock_Stream(const PJ_Sock_Stream &rhs) : PJ_Socket(rhs) {} + PJ_Sock_Stream &operator=(const PJ_Sock_Stream &rhs) { sock_ = rhs.sock_; return *this; } + + bool listen(int backlog = 5) + { + return pj_sock_listen(sock_, backlog) == 0; + } + + bool accept(PJ_Sock_Stream *new_sock, PJ_INET_Addr *addr, int *addrlen) + { + pj_sock_t s = pj_sock_accept(sock_, addr, addrlen); + if (s == -1) + return false; + new_sock->set_handle(s); + return true; + } + + bool connect(const PJ_INET_Addr &addr) + { + return pj_sock_connect(sock_, &addr, sizeof(PJ_INET_Addr)) == 0; + } + + bool shutdown(int how) + { + return pj_sock_shutdown(sock_, how) == 0; + } + +}; +#endif + +class PJ_Sock_Dgram : public PJ_Socket +{ +public: + PJ_Sock_Dgram() {} + PJ_Sock_Dgram(const PJ_Sock_Dgram &rhs) : PJ_Socket(rhs) {} + PJ_Sock_Dgram &operator=(const PJ_Sock_Dgram &rhs) { sock_ = rhs.sock_; return *this; } + + int recvfrom(void *buf, int len, int flag, PJ_INET_Addr *fromaddr) + { + int addrlen; + return pj_sock_recvfrom(sock_, buf, len, flag, fromaddr, &addrlen); + } + + int sendto(const void *buf, int len, int flag, const PJ_INET_Addr &addr) + { + return pj_sock_sendto(sock_, buf, len, flag, &addr, sizeof(PJ_INET_Addr)); + } +}; + +#endif /* __PJPP_SOCK_H__ */ diff --git a/pjlib/include/pj++/string.hpp b/pjlib/include/pj++/string.hpp new file mode 100644 index 00000000..d55e737b --- /dev/null +++ b/pjlib/include/pj++/string.hpp @@ -0,0 +1,249 @@ +/* $Id$ + * + */ +#ifndef __PJPP_STRING_H__ +#define __PJPP_STRING_H__ + +#include +#include + +class PJ_String : public pj_str_t +{ +public: + PJ_String() + { + pj_assert(sizeof(PJ_String) == sizeof(pj_str_t)); + ptr=NULL; slen=0; + } + + explicit PJ_String(char *str) + { + set(str); + } + + PJ_String(PJ_Pool *pool, const char *src) + { + set(pool, src); + } + + explicit PJ_String(pj_str_t *s) + { + set(s); + } + + PJ_String(PJ_Pool *pool, const pj_str_t *s) + { + set(pool, s); + } + + explicit PJ_String(PJ_String &rhs) + { + set(rhs); + } + + PJ_String(PJ_Pool *pool, const PJ_String &rhs) + { + set(pool, rhs); + } + + PJ_String(char *str, pj_size_t len) + { + set(str, len); + } + + PJ_String(char *begin, char *end) + { + pj_strset3(this, begin, end); + } + + pj_size_t length() const + { + return pj_strlen(this); + } + + pj_size_t size() const + { + return length(); + } + + const char *buf() const + { + return ptr; + } + + void set(char *str) + { + pj_strset2(this, str); + } + + void set(PJ_Pool *pool, const char *s) + { + pj_strdup2(pool->pool_(), this, s); + } + + void set(pj_str_t *s) + { + pj_strassign(this, s); + } + + void set(PJ_Pool *pool, const pj_str_t *s) + { + pj_strdup(pool->pool_(), this, s); + } + + void set(char *str, pj_size_t len) + { + pj_strset(this, str, len); + } + + void set(char *begin, char *end) + { + pj_strset3(this, begin, end); + } + + void set(PJ_String &rhs) + { + pj_strassign(this, &rhs); + } + + void set(PJ_Pool *pool, const PJ_String *s) + { + pj_strdup(pool->pool_(), this, s); + } + + void set(PJ_Pool *pool, const PJ_String &s) + { + pj_strdup(pool->pool_(), this, &s); + } + + void strcpy(const pj_str_t *s) + { + pj_strcpy(this, s); + } + + void strcpy(const PJ_String &rhs) + { + pj_strcpy(this, &rhs); + } + + void strcpy(const char *s) + { + pj_strcpy2(this, s); + } + + int strcmp(const char *s) const + { + return pj_strcmp2(this, s); + } + + int strcmp(const pj_str_t *s) const + { + return pj_strcmp(this, s); + } + + int strcmp(const PJ_String &rhs) const + { + return pj_strcmp(this, &rhs); + } + + int strncmp(const char *s, pj_size_t len) const + { + return pj_strncmp2(this, s, len); + } + + int strncmp(const pj_str_t *s, pj_size_t len) const + { + return pj_strncmp(this, s, len); + } + + int strncmp(const PJ_String &rhs, pj_size_t len) const + { + return pj_strncmp(this, &rhs, len); + } + + int stricmp(const char *s) const + { + return pj_stricmp2(this, s); + } + + int stricmp(const pj_str_t *s) const + { + return pj_stricmp(this, s); + } + + int stricmp(const PJ_String &rhs) const + { + return stricmp(&rhs); + } + + int strnicmp(const char *s, pj_size_t len) const + { + return pj_strnicmp2(this, s, len); + } + + int strnicmp(const pj_str_t *s, pj_size_t len) const + { + return pj_strnicmp(this, s, len); + } + + int strnicmp(const PJ_String &rhs, pj_size_t len) const + { + return strnicmp(&rhs, len); + } + + bool operator==(const char *s) const + { + return strcmp(s) == 0; + } + + bool operator==(const pj_str_t *s) const + { + return strcmp(s) == 0; + } + + bool operator==(const PJ_String &rhs) const + { + return pj_strcmp(this, &rhs) == 0; + } + + char *strchr(int chr) + { + return pj_strchr(this, chr); + } + + char *find(int chr) + { + return strchr(chr); + } + + void strcat(const PJ_String &rhs) + { + pj_strcat(this, &rhs); + } + + void ltrim() + { + pj_strltrim(this); + } + + void rtrim() + { + pj_strrtrim(this); + } + + void trim() + { + pj_strtrim(this); + } + + unsigned long toul() const + { + return pj_strtoul(this); + } + +private: + //PJ_String(const PJ_String &rhs) {} + void operator=(const PJ_String &rhs) { pj_assert(false); } +}; + +#endif /* __PJPP_STRING_H__ */ diff --git a/pjlib/include/pj++/timer.hpp b/pjlib/include/pj++/timer.hpp new file mode 100644 index 00000000..8357a198 --- /dev/null +++ b/pjlib/include/pj++/timer.hpp @@ -0,0 +1,107 @@ +/* $Id$ + * + */ +#ifndef __PJPP_TIMER_H__ +#define __PJPP_TIMER_H__ + +#include +#include + +class PJ_Timer_Heap; + +class PJ_Timer_Entry : private pj_timer_entry +{ + friend class PJ_Timer_Heap; + +public: + static void timer_heap_callback(pj_timer_heap_t *, pj_timer_entry *); + + PJ_Timer_Entry() { cb = &timer_heap_callback; } + PJ_Timer_Entry(int arg_id, void *arg_user_data) + { + cb = &timer_heap_callback; + init(arg_id, arg_user_data); + } + + virtual void on_timeout() = 0; + + void init(int arg_id, void *arg_user_data) + { + id = arg_id; + user_data = arg_user_data; + } + + int get_id() const + { + return id; + } + + void set_id(int arg_id) + { + id = arg_id; + } + + void set_user_data(void *arg_user_data) + { + user_data = arg_user_data; + } + + void *get_user_data() const + { + return user_data; + } + + const PJ_Time_Val &get_timeout() const + { + pj_assert(sizeof(PJ_Time_Val) == sizeof(pj_time_val)); + return (PJ_Time_Val&)_timer_value; + } +}; + +class PJ_Timer_Heap +{ +public: + PJ_Timer_Heap() {} + + bool create(PJ_Pool *pool, pj_size_t initial_count, + unsigned flag = PJ_TIMER_HEAP_SYNCHRONIZE) + { + ht_ = pj_timer_heap_create(pool->pool_(), initial_count, flag); + return ht_ != NULL; + } + + pj_timer_heap_t *get_timer_heap() + { + return ht_; + } + + bool schedule( PJ_Timer_Entry *ent, const PJ_Time_Val &delay) + { + return pj_timer_heap_schedule(ht_, ent, &delay) == 0; + } + + bool cancel(PJ_Timer_Entry *ent) + { + return pj_timer_heap_cancel(ht_, ent) == 1; + } + + pj_size_t count() + { + return pj_timer_heap_count(ht_); + } + + void earliest_time(PJ_Time_Val *t) + { + pj_timer_heap_earliest_time(ht_, t); + } + + int poll(PJ_Time_Val *next_delay = NULL) + { + return pj_timer_heap_poll(ht_, next_delay); + } + +private: + pj_timer_heap_t *ht_; +}; + +#endif /* __PJPP_TIMER_H__ */ diff --git a/pjlib/include/pj++/tree.hpp b/pjlib/include/pj++/tree.hpp new file mode 100644 index 00000000..db5e8d2a --- /dev/null +++ b/pjlib/include/pj++/tree.hpp @@ -0,0 +1,109 @@ +/* $Id$ + * + */ +#ifndef __PJPP_TREE_H__ +#define __PJPP_TREE_H__ + +#include + +class PJ_Tree +{ +public: + typedef pj_rbtree_comp Comp; + class iterator; + class reverse_iterator; + + class Node : private pj_rbtree_node + { + friend class PJ_Tree; + friend class iterator; + friend class reverse_iterator; + + public: + Node() {} + explicit Node(void *data) { user_data = data; } + void set_user_data(void *data) { user_data = data; } + void *get_user_data() const { return user_data; } + }; + + class iterator + { + public: + iterator() {} + iterator(const iterator &rhs) : tr_(rhs.tr_), nd_(rhs.nd_) {} + iterator(pj_rbtree *tr, pj_rbtree_node *nd) : tr_(tr), nd_(nd) {} + Node *operator*() { return (Node*)nd_; } + bool operator==(const iterator &rhs) const { return tr_==rhs.tr_ && nd_==rhs.nd_; } + iterator &operator=(const iterator &rhs) { tr_=rhs.tr_; nd_=rhs.nd_; return *this; } + void operator++() { nd_=pj_rbtree_next(tr_, nd_); } + void operator--() { nd_=pj_rbtree_prev(tr_, nd_); } + protected: + pj_rbtree *tr_; + pj_rbtree_node *nd_; + }; + + class reverse_iterator : public iterator + { + public: + reverse_iterator() {} + reverse_iterator(const reverse_iterator &it) : iterator(it) {} + reverse_iterator(pj_rbtree *t, pj_rbtree_node *n) : iterator(t, n) {} + reverse_iterator &operator=(const reverse_iterator &rhs) { iterator::operator=(rhs); return *this; } + Node *operator*() { return (Node*)nd_; } + bool operator==(const reverse_iterator &rhs) const { return iterator::operator==(rhs); } + void operator++() { nd_=pj_rbtree_prev(tr_, nd_); } + void operator--() { nd_=pj_rbtree_next(tr_, nd_); } + }; + + explicit PJ_Tree(Comp *comp) { pj_rbtree_init(&t_, comp); } + + iterator begin() + { + return iterator(&t_, pj_rbtree_first(&t_)); + } + + iterator end() + { + return iterator(&t_, NULL); + } + + reverse_iterator rbegin() + { + return reverse_iterator(&t_, pj_rbtree_last(&t_)); + } + + reverse_iterator rend() + { + return reverse_iterator(&t_, NULL); + } + + bool insert(Node *node) + { + return pj_rbtree_insert(&t_, node)==0 ? true : false; + } + + Node *find(const void *key) + { + return (Node*)pj_rbtree_find(&t_, key); + } + + Node *erase(Node *node) + { + return (Node*)pj_rbtree_erase(&t_, node); + } + + unsigned max_height(Node *node=NULL) + { + return pj_rbtree_max_height(&t_, node); + } + + unsigned min_height(Node *node=NULL) + { + return pj_rbtree_min_height(&t_, node); + } + +private: + pj_rbtree t_; +}; + +#endif /* __PJPP_TREE_H__ */ diff --git a/pjlib/include/pj++/types.hpp b/pjlib/include/pj++/types.hpp new file mode 100644 index 00000000..efa74399 --- /dev/null +++ b/pjlib/include/pj++/types.hpp @@ -0,0 +1,61 @@ +/* $Id$ + * + */ +#ifndef __PJPP_TYPES_H__ +#define __PJPP_TYPES_H__ + +#include + +class PJ_Pool; +class PJ_Socket; + + +class PJ_Time_Val : public pj_time_val +{ +public: + PJ_Time_Val() {} + PJ_Time_Val(const PJ_Time_Val &rhs) { sec=rhs.sec; msec=rhs.msec; } + explicit PJ_Time_Val(const pj_time_val &tv) { sec = tv.sec; msec = tv.msec; } + + long get_sec() const { return sec; } + long get_msec() const { return msec; } + void set_sec (long s) { sec = s; } + void set_msec(long ms) { msec = ms; normalize(); } + long to_msec() const { return PJ_TIME_VAL_MSEC((*this)); } + + bool operator == (const PJ_Time_Val &rhs) const { return PJ_TIME_VAL_EQ((*this), rhs); } + bool operator > (const PJ_Time_Val &rhs) const { return PJ_TIME_VAL_GT((*this), rhs); } + bool operator >= (const PJ_Time_Val &rhs) const { return PJ_TIME_VAL_GTE((*this), rhs); } + bool operator < (const PJ_Time_Val &rhs) const { return PJ_TIME_VAL_LT((*this), rhs); } + bool operator <= (const PJ_Time_Val &rhs) const { return PJ_TIME_VAL_LTE((*this), rhs); } + + PJ_Time_Val & operator = (const PJ_Time_Val &rhs) { + sec = rhs.sec; + msec = rhs.msec; + return *this; + } + + PJ_Time_Val & operator += (const PJ_Time_Val &rhs) { + PJ_TIME_VAL_ADD((*this), rhs); + return *this; + } + + PJ_Time_Val & operator -= (const PJ_Time_Val &rhs) { + PJ_TIME_VAL_SUB((*this), rhs); + return *this; + } + + /* Must include os.hpp to use these, otherwise unresolved in linking */ + pj_status_t gettimeofday(); + pj_parsed_time decode(); + pj_status_t encode(const pj_parsed_time *pt); + pj_status_t to_gmt(); + pj_status_t to_local(); + + +private: + void normalize() { pj_time_val_normalize(this); } + +}; + +#endif /* __PJPP_TYPES_H__ */ diff --git a/pjlib/include/pj/md5.h b/pjlib/include/pj/md5.h deleted file mode 100644 index 8f3f6145..00000000 --- a/pjlib/include/pj/md5.h +++ /dev/null @@ -1,94 +0,0 @@ -/* $Id$ - * - */ -/* - Copyright (C) 1999, 2002 Aladdin Enterprises. All rights reserved. - - This software is provided 'as-is', without any express or implied - warranty. In no event will the authors be held liable for any damages - arising from the use of this software. - - Permission is granted to anyone to use this software for any purpose, - including commercial applications, and to alter it and redistribute it - freely, subject to the following restrictions: - - 1. The origin of this software must not be misrepresented; you must not - claim that you wrote the original software. If you use this software - in a product, an acknowledgment in the product documentation would be - appreciated but is not required. - 2. Altered source versions must be plainly marked as such, and must not be - misrepresented as being the original software. - 3. This notice may not be removed or altered from any source distribution. - - L. Peter Deutsch - ghost@aladdin.com - - */ -/* $Id$ */ -/* - Independent implementation of MD5 (RFC 1321). - - This code implements the MD5 Algorithm defined in RFC 1321, whose - text is available at - http://www.ietf.org/rfc/rfc1321.txt - The code is derived from the text of the RFC, including the test suite - (section A.5) but excluding the rest of Appendix A. It does not include - any code or documentation that is identified in the RFC as being - copyrighted. - - The original and principal author of md5.h is L. Peter Deutsch - . Other authors are noted in the change history - that follows (in reverse chronological order): - - 2002-04-13 lpd Removed support for non-ANSI compilers; removed - references to Ghostscript; clarified derivation from RFC 1321; - now handles byte order either statically or dynamically. - 1999-11-04 lpd Edited comments slightly for automatic TOC extraction. - 1999-10-18 lpd Fixed typo in header comment (ansi2knr rather than md5); - added conditionalization for C++ compilation from Martin - Purschke . - 1999-05-03 lpd Original version. - */ - -#ifndef md5_INCLUDED -# define md5_INCLUDED - -/* - * This package supports both compile-time and run-time determination of CPU - * byte order. If ARCH_IS_BIG_ENDIAN is defined as 0, the code will be - * compiled to run only on little-endian CPUs; if ARCH_IS_BIG_ENDIAN is - * defined as non-zero, the code will be compiled to run only on big-endian - * CPUs; if ARCH_IS_BIG_ENDIAN is not defined, the code will be compiled to - * run on either big- or little-endian CPUs, but will run slightly less - * efficiently on either one than if ARCH_IS_BIG_ENDIAN is defined. - */ - -typedef unsigned char md5_byte_t; /* 8-bit byte */ -typedef unsigned long md5_word_t; /* 32-bit word */ - -/** Define the state of the MD5 Algorithm. */ -typedef struct md5_state_s { - md5_word_t count[2]; /**< message length in bits, lsw first */ - md5_word_t abcd[4]; /**< digest buffer */ - md5_byte_t buf[64]; /**< accumulate block */ -} md5_state_t; - -#ifdef __cplusplus -extern "C" -{ -#endif - -/** Initialize the algorithm. */ -void md5_init(md5_state_t *pms); - -/** Append a string to the message. */ -void md5_append(md5_state_t *pms, const md5_byte_t *data, int nbytes); - -/** Finish the message and return the digest. */ -void md5_finish(md5_state_t *pms, md5_byte_t digest[16]); - -#ifdef __cplusplus -} /* end extern "C" */ -#endif - -#endif /* md5_INCLUDED */ diff --git a/pjlib/include/pj/scanner.h b/pjlib/include/pj/scanner.h deleted file mode 100644 index f1b0b133..00000000 --- a/pjlib/include/pj/scanner.h +++ /dev/null @@ -1,456 +0,0 @@ -/* $Id$ - * - */ - -#ifndef __PJ_PARSER_H__ -#define __PJ_PARSER_H__ - -/** - * @file scanner.h - * @brief Text Scanning. - */ - -#include - -PJ_BEGIN_DECL - -/** - * @defgroup PJ_SCAN Text Scanning - * @ingroup PJ_MISC - * @brief - * Text scanning utility. - */ - -/** - * @defgroup PJ_CHARSPEC Character Filter Specification - * @ingroup PJ_SCAN - * @brief - * The type pj_char_spec is a specification of character set used in - * scanner. Application can define multiple character specs, such as to - * scan alpha numerics, numbers, tokens, etc. - * @{ - */ - -/** - * This describes the type of individual character specification in - * #pj_char_spec. - */ -typedef pj_uint8_t pj_char_spec_element_t; - -/** - * The character specification is implemented as array of boolean flags. Each - * flag indicates the membership of the character in the spec. If the flag - * at one position is non-zero, then the character at that position belongs - * to the specification, and vice versa. - */ -typedef pj_char_spec_element_t pj_char_spec[256]; -// Note: it's got to be 256 (not 128) to cater for extended character in input. - -/** - * Initialize character spec. - * @param cs the scanner character specification. - */ -PJ_DECL(void) pj_cs_init( pj_char_spec cs); - -/** - * Set the membership of the specified character to TRUE. - * @param cs the scanner character specification. - * @param c the character. - */ -PJ_DECL(void) pj_cs_set( pj_char_spec cs, int c); - -/** - * Add the characters in the specified range '[cstart, cend)' to the - * specification (the last character itself ('cend') is not added). - * @param cs the scanner character specification. - * @param cstart the first character in the range. - * @param cend the next character after the last character in the range. - */ -PJ_DECL(void) pj_cs_add_range( pj_char_spec cs, int cstart, int cend); - -/** - * Add alphabetic characters to the specification. - * @param cs the scanner character specification. - */ -PJ_DECL(void) pj_cs_add_alpha( pj_char_spec cs); - -/** - * Add numeric characters to the specification. - * @param cs the scanner character specification. - */ -PJ_DECL(void) pj_cs_add_num( pj_char_spec cs); - -/** - * Add the characters in the string to the specification. - * @param cs the scanner character specification. - * @param str the string. - */ -PJ_DECL(void) pj_cs_add_str( pj_char_spec cs, const char *str); - -/** - * Delete characters in the specified range from the specification. - * @param cs the scanner character specification. - * @param cstart the first character in the range. - * @param cend the next character after the last character in the range. - */ -PJ_DECL(void) pj_cs_del_range( pj_char_spec cs, int cstart, int cend); - -/** - * Delete characters in the specified string from the specification. - * @param cs the scanner character specification. - * @param str the string. - */ -PJ_DECL(void) pj_cs_del_str( pj_char_spec cs, const char *str); - -/** - * Invert specification. - * @param cs the scanner character specification. - */ -PJ_DECL(void) pj_cs_invert( pj_char_spec cs ); - -/** - * Check whether the specified character belongs to the specification. - * @param cs the scanner character specification. - * @param c the character to check for matching. - */ -PJ_INLINE(int) pj_cs_match( const pj_char_spec cs, int c ) -{ - return cs[c]; -} - -/** - * @} - */ - -/** - * @defgroup PJ_SCANNER Text Scanner - * @ingroup PJ_SCAN - * @{ - */ - -/** - * Flags for scanner. - */ -enum -{ - /** This flags specifies that the scanner should automatically skip - whitespaces - */ - PJ_SCAN_AUTOSKIP_WS = 1, - - /** This flags specifies that the scanner should automatically skip - SIP header continuation. This flag implies PJ_SCAN_AUTOSKIP_WS. - */ - PJ_SCAN_AUTOSKIP_WS_HEADER = 3, - - /** Auto-skip new lines. - */ - PJ_SCAN_AUTOSKIP_NEWLINE = 4, -}; - - -/* Forward decl. */ -struct pj_scanner; - - -/** - * The callback function type to be called by the scanner when it encounters - * syntax error. - * @param scanner The scanner instance that calls the callback . - */ -typedef void (*pj_syn_err_func_ptr)(struct pj_scanner *scanner); - - -/** - * The text scanner structure. - */ -typedef struct pj_scanner -{ - char *begin; /**< Start of input buffer. */ - char *end; /**< End of input buffer. */ - char *curptr; /**< Current pointer. */ - int line; /**< Current line. */ - int col; /**< Current column. */ - int skip_ws; /**< Skip whitespace flag. */ - pj_syn_err_func_ptr callback; /**< Syntax error callback. */ -} pj_scanner; - - -/** - * This structure can be used by application to store the state of the parser, - * so that the scanner state can be rollback to this state when necessary. - */ -typedef struct pj_scan_state -{ - char *curptr; /**< Current scanner's pointer. */ - int line; /**< Current line. */ - int col; /**< Current column. */ -} pj_scan_state; - - -/** - * Initialize the scanner. Note that the input string buffer must have - * length at least buflen+1 because the scanner will NULL terminate the - * string during initialization. - * - * @param scanner The scanner to be initialized. - * @param bufstart The input buffer to scan. Note that buffer[buflen] will be - * filled with NULL char until scanner is destroyed, so - * the actual buffer length must be at least buflen+1. - * @param buflen The length of the input buffer, which normally is - * strlen(bufstart). - * @param options Zero, or combination of PJ_SCAN_AUTOSKIP_WS or - * PJ_SCAN_AUTOSKIP_WS_HEADER - * @param callback Callback to be called when the scanner encounters syntax - * error condition. - */ -PJ_DECL(void) pj_scan_init( pj_scanner *scanner, char *bufstart, int buflen, - unsigned options, - pj_syn_err_func_ptr callback ); - - -/** - * Call this function when application has finished using the scanner. - * - * @param scanner The scanner. - */ -PJ_DECL(void) pj_scan_fini( pj_scanner *scanner ); - - -/** - * Determine whether the EOF condition for the scanner has been met. - * - * @param scanner The scanner. - * - * @return Non-zero if scanner is EOF. - */ -PJ_INLINE(int) pj_scan_is_eof( const pj_scanner *scanner) -{ - return scanner->curptr >= scanner->end; -} - - -/** - * Peek strings in current position according to parameter spec, and return - * the strings in parameter out. The current scanner position will not be - * moved. If the scanner is already in EOF state, syntax error callback will - * be called thrown. - * - * @param scanner The scanner. - * @param spec The spec to match input string. - * @param out String to store the result. - * - * @return the character right after the peek-ed position or zero if there's - * no more characters. - */ -PJ_DECL(int) pj_scan_peek( pj_scanner *scanner, - const pj_char_spec spec, pj_str_t *out); - - -/** - * Peek len characters in current position, and return them in out parameter. - * Note that whitespaces or newlines will be returned as it is, regardless - * of PJ_SCAN_AUTOSKIP_WS settings. If the character left is less than len, - * syntax error callback will be called. - * - * @param scanner The scanner. - * @param len Length to peek. - * @param out String to store the result. - * - * @return the character right after the peek-ed position or zero if there's - * no more characters. - */ -PJ_DECL(int) pj_scan_peek_n( pj_scanner *scanner, - pj_size_t len, pj_str_t *out); - - -/** - * Peek strings in current position until spec is matched, and return - * the strings in parameter out. The current scanner position will not be - * moved. If the scanner is already in EOF state, syntax error callback will - * be called. - * - * @param scanner The scanner. - * @param spec The peeking will stop when the input match this spec. - * @param out String to store the result. - * - * @return the character right after the peek-ed position. - */ -PJ_DECL(int) pj_scan_peek_until( pj_scanner *scanner, - const pj_char_spec spec, - pj_str_t *out); - - -/** - * Get characters from the buffer according to the spec, and return them - * in out parameter. The scanner will attempt to get as many characters as - * possible as long as the spec matches. If the first character doesn't - * match the spec, or scanner is already in EOF when this function is called, - * an exception will be thrown. - * - * @param scanner The scanner. - * @param spec The spec to match input string. - * @param out String to store the result. - */ -PJ_DECL(void) pj_scan_get( pj_scanner *scanner, - const pj_char_spec spec, pj_str_t *out); - - -/** - * Get characters between quotes. If current input doesn't match begin_quote, - * syntax error will be thrown. - * - * @param scanner The scanner. - * @param begin_quote The character to begin the quote. - * @param end_quote The character to end the quote. - * @param out String to store the result. - */ -PJ_DECL(void) pj_scan_get_quote( pj_scanner *scanner, - int begin_quote, int end_quote, - pj_str_t *out); - -/** - * Get N characters from the scanner. - * - * @param scanner The scanner. - * @param N Number of characters to get. - * @param out String to store the result. - */ -PJ_DECL(void) pj_scan_get_n( pj_scanner *scanner, - unsigned N, pj_str_t *out); - - -/** - * Get one character from the scanner. - * - * @param scanner The scanner. - * - * @return (unknown) - */ -PJ_DECL(int) pj_scan_get_char( pj_scanner *scanner ); - - -/** - * Get a newline from the scanner. A newline is defined as '\\n', or '\\r', or - * "\\r\\n". If current input is not newline, syntax error will be thrown. - * - * @param scanner The scanner. - */ -PJ_DECL(void) pj_scan_get_newline( pj_scanner *scanner ); - - -/** - * Get characters from the scanner and move the scanner position until the - * current character matches the spec. - * - * @param scanner The scanner. - * @param spec Get until the input match this spec. - * @param out String to store the result. - */ -PJ_DECL(void) pj_scan_get_until( pj_scanner *scanner, - const pj_char_spec spec, pj_str_t *out); - - -/** - * Get characters from the scanner and move the scanner position until the - * current character matches until_char. - * - * @param scanner The scanner. - * @param until_char Get until the input match this character. - * @param out String to store the result. - */ -PJ_DECL(void) pj_scan_get_until_ch( pj_scanner *scanner, - int until_char, pj_str_t *out); - - -/** - * Get characters from the scanner and move the scanner position until the - * current character matches until_char. - * - * @param scanner The scanner. - * @param until_spec Get until the input match any of these characters. - * @param out String to store the result. - */ -PJ_DECL(void) pj_scan_get_until_chr( pj_scanner *scanner, - const char *until_spec, pj_str_t *out); - -/** - * Advance the scanner N characters, and skip whitespace - * if necessary. - * - * @param scanner The scanner. - * @param N Number of characters to skip. - * @param skip Flag to specify whether whitespace should be skipped - * after skipping the characters. - */ -PJ_DECL(void) pj_scan_advance_n( pj_scanner *scanner, - unsigned N, pj_bool_t skip); - - -/** - * Compare string in current position with the specified string. - * - * @param scanner The scanner. - * @param s The string to compare with. - * @param len Length of the string to compare. - * - * @return zero, <0, or >0 (just like strcmp()). - */ -PJ_DECL(int) pj_scan_strcmp( pj_scanner *scanner, const char *s, int len); - - -/** - * Case-less string comparison of current position with the specified - * string. - * - * @param scanner The scanner. - * @param s The string to compare with. - * @param len Length of the string to compare with. - * - * @return zero, <0, or >0 (just like strcmp()). - */ -PJ_DECL(int) pj_scan_stricmp( pj_scanner *scanner, const char *s, int len); - - -/** - * Manually skip whitespaces according to flag that was specified when - * the scanner was initialized. - * - * @param scanner The scanner. - */ -PJ_DECL(void) pj_scan_skip_whitespace( pj_scanner *scanner ); - - -/** - * Save the full scanner state. - * - * @param scanner The scanner. - * @param state Variable to store scanner's state. - */ -PJ_DECL(void) pj_scan_save_state( pj_scanner *scanner, pj_scan_state *state); - - -/** - * Restore the full scanner state. - * Note that this would not restore the string if application has modified - * it. This will only restore the scanner scanning position. - * - * @param scanner The scanner. - * @param state State of the scanner. - */ -PJ_DECL(void) pj_scan_restore_state( pj_scanner *scanner, - pj_scan_state *state); - -/** - * @} - */ - -#if PJ_FUNCTIONS_ARE_INLINED -# include "scanner_i.h" -#endif - - -PJ_END_DECL - -#endif - diff --git a/pjlib/include/pj/stun.h b/pjlib/include/pj/stun.h deleted file mode 100644 index 90c20ec6..00000000 --- a/pjlib/include/pj/stun.h +++ /dev/null @@ -1,123 +0,0 @@ -#ifndef __PJ_STUN_H__ -#define __PJ_STUN_H__ - -#include -#include - -PJ_BEGIN_DECL - -#define PJ_STUN_MAX_ATTR 16 - -typedef enum pj_stun_msg_type -{ - PJ_STUN_BINDING_REQUEST = 0x0001, - PJ_STUN_BINDING_RESPONSE = 0x0101, - PJ_STUN_BINDING_ERROR_RESPONSE = 0x0111, - PJ_STUN_SHARED_SECRET_REQUEST = 0x0002, - PJ_STUN_SHARED_SECRET_RESPONSE = 0x0102, - PJ_STUN_SHARED_SECRET_ERROR_RESPONSE = 0x0112 -} pj_stun_msg_type; - -typedef enum pj_stun_attr_type -{ - PJ_STUN_ATTR_MAPPED_ADDR = 1, - PJ_STUN_ATTR_RESPONSE_ADDR, - PJ_STUN_ATTR_CHANGE_REQUEST, - PJ_STUN_ATTR_SOURCE_ADDR, - PJ_STUN_ATTR_CHANGED_ADDR, - PJ_STUN_ATTR_USERNAME, - PJ_STUN_ATTR_PASSWORD, - PJ_STUN_ATTR_MESSAGE_INTEGRITY, - PJ_STUN_ATTR_ERROR_CODE, - PJ_STUN_ATTR_UNKNOWN_ATTRIBUTES, - PJ_STUN_ATTR_REFLECTED_FORM -} pj_stun_attr_type; - -typedef struct pj_stun_msg_hdr -{ - pj_uint16_t type; - pj_uint16_t length; - pj_uint32_t tsx[4]; -} pj_stun_msg_hdr; - -typedef struct pj_stun_attr_hdr -{ - pj_uint16_t type; - pj_uint16_t length; -} pj_stun_attr_hdr; - -typedef struct pj_stun_mapped_addr_attr -{ - pj_stun_attr_hdr hdr; - pj_uint8_t ignored; - pj_uint8_t family; - pj_uint16_t port; - pj_uint32_t addr; -} pj_stun_mapped_addr_attr; - -typedef pj_stun_mapped_addr_attr pj_stun_response_addr_attr; -typedef pj_stun_mapped_addr_attr pj_stun_changed_addr_attr; -typedef pj_stun_mapped_addr_attr pj_stun_src_addr_attr; -typedef pj_stun_mapped_addr_attr pj_stun_reflected_form_attr; - -typedef struct pj_stun_change_request_attr -{ - pj_stun_attr_hdr hdr; - pj_uint32_t value; -} pj_stun_change_request_attr; - -typedef struct pj_stun_username_attr -{ - pj_stun_attr_hdr hdr; - pj_uint32_t value[1]; -} pj_stun_username_attr; - -typedef pj_stun_username_attr pj_stun_password_attr; - -typedef struct pj_stun_error_code_attr -{ - pj_stun_attr_hdr hdr; - pj_uint16_t ignored; - pj_uint8_t err_class; - pj_uint8_t number; - char reason[4]; -} pj_stun_error_code_attr; - -typedef struct pj_stun_msg -{ - pj_stun_msg_hdr *hdr; - int attr_count; - pj_stun_attr_hdr *attr[PJ_STUN_MAX_ATTR]; -} pj_stun_msg; - -/* STUN message API (stun.c). */ - -PJ_DECL(pj_status_t) pj_stun_create_bind_req( pj_pool_t *pool, - void **msg, pj_size_t *len, - pj_uint32_t id_hi, - pj_uint32_t id_lo); -PJ_DECL(pj_status_t) pj_stun_parse_msg( void *buf, pj_size_t len, - pj_stun_msg *msg); -PJ_DECL(void*) pj_stun_msg_find_attr( pj_stun_msg *msg, pj_stun_attr_type t); - -/* STUN simple client API (stun_client.c) */ -enum pj_stun_err_code { - PJ_STUN_ERR_MEMORY = (-2), - PJ_STUN_ERR_RESOLVE = (-3), - PJ_STUN_ERR_TRANSPORT = (-4), - PJ_STUN_ERR_INVALID_MSG = (-5), - PJ_STUN_ERR_NO_RESPONSE = (-6), - PJ_STUN_ERR_SYMETRIC = (-7), -}; - -PJ_DECL(pj_status_t) pj_stun_get_mapped_addr( pj_pool_factory *pf, - int sock_cnt, pj_sock_t sock[], - const pj_str_t *srv1, int port1, - const pj_str_t *srv2, int port2, - pj_sockaddr_in mapped_addr[]); -PJ_DECL(const char*) pj_stun_get_err_msg(pj_status_t status); - -PJ_END_DECL - -#endif /* __PJ_STUN_H__ */ - diff --git a/pjlib/include/pj/xml.h b/pjlib/include/pj/xml.h deleted file mode 100644 index fd7978ae..00000000 --- a/pjlib/include/pj/xml.h +++ /dev/null @@ -1,157 +0,0 @@ -/* $Id$ - * - */ - -#ifndef __PJ_XML_H__ -#define __PJ_XML_H__ - -/** - * @file xml.h - * @brief PJLIB XML Parser/Helper. - */ - -#include -#include - -PJ_BEGIN_DECL - -/** - * @defgroup PJ_XML XML Parser/Helper. - * @ingroup PJ - * @{ - */ - -/** Typedef for XML attribute. */ -typedef struct pj_xml_attr pj_xml_attr; - -/** Typedef for XML nodes. */ -typedef struct pj_xml_node pj_xml_node; - -/** This structure declares XML attribute. */ -struct pj_xml_attr -{ - PJ_DECL_LIST_MEMBER(pj_xml_attr); /**< Standard list elements. */ - pj_str_t name; /**< Attribute name. */ - pj_str_t value; /**< Attribute value. */ -}; - -/** This structure describes XML node head inside XML node structure. - */ -typedef struct pj_xml_node_head -{ - PJ_DECL_LIST_MEMBER(pj_xml_node); /**< Standard list elements. */ -} pj_xml_node_head; - -/** This structure describes XML node. */ -struct pj_xml_node -{ - PJ_DECL_LIST_MEMBER(pj_xml_node); /**< List @a prev and @a next member */ - pj_str_t name; /**< Node name. */ - pj_xml_attr attr_head; /**< Attribute list. */ - pj_xml_node_head node_head; /**< Node list. */ - pj_str_t content; /**< Node content. */ -}; - -/** - * Parse XML message into XML document with a single root node. The parser - * is capable of parsing XML processing instruction construct ("