diff options
author | Marc Balmer <mbalmer@cvs.openbsd.org> | 2008-05-13 17:44:47 +0000 |
---|---|---|
committer | Marc Balmer <mbalmer@cvs.openbsd.org> | 2008-05-13 17:44:47 +0000 |
commit | 058c9ffd814b7610bedd175a46763ef16917794e (patch) | |
tree | 05aeb3f09c33792913c880bb2449ac8abd79ef7b | |
parent | d9e4f2c585c031eacb24851dcddb6eb55746047a (diff) |
A first chunk of readability/knf changes. Since there is nothing more
to merge from upstream, we can safely sanitize the code and hopefully
the build system.
Discussed with and feedback from sthen, todd, dlg and henning.
no binary changes.
-rw-r--r-- | usr.sbin/httpd/src/main/alloc.c | 3228 | ||||
-rw-r--r-- | usr.sbin/httpd/src/main/buff.c | 1559 | ||||
-rw-r--r-- | usr.sbin/httpd/src/main/gen_test_char.c | 104 | ||||
-rw-r--r-- | usr.sbin/httpd/src/main/gen_uri_delims.c | 39 | ||||
-rw-r--r-- | usr.sbin/httpd/src/main/rfc1413.c | 272 | ||||
-rw-r--r-- | usr.sbin/httpd/src/main/util.c | 485 | ||||
-rw-r--r-- | usr.sbin/httpd/src/main/util_date.c | 326 | ||||
-rw-r--r-- | usr.sbin/httpd/src/main/util_md5.c | 119 |
8 files changed, 3130 insertions, 3002 deletions
diff --git a/usr.sbin/httpd/src/main/alloc.c b/usr.sbin/httpd/src/main/alloc.c index e4fb1da7755..d917db267bf 100644 --- a/usr.sbin/httpd/src/main/alloc.c +++ b/usr.sbin/httpd/src/main/alloc.c @@ -137,36 +137,36 @@ static AP_MM *mm = NULL; */ union align { - /* Types which are likely to have the longest RELEVANT alignment - * restrictions... - */ - - char *cp; - void (*f) (void); - long l; - FILE *fp; - double d; + /* + * Types which are likely to have the longest RELEVANT alignment + * restrictions... + */ + char *cp; + void (*f)(void); + long l; + FILE *fp; + double d; }; #define CLICK_SZ (sizeof(union align)) union block_hdr { - union align a; + union align a; - /* Actual header... */ + /* Actual header... */ - struct { - char *endp; - union block_hdr *next; - char *first_avail; + struct { + char *endp; + union block_hdr *next; + char *first_avail; #if defined(EAPI_MM) - int is_shm; + int is_shm; #endif #ifdef POOL_DEBUG - union block_hdr *global_next; - struct pool *owning_pool; + union block_hdr *global_next; + struct pool *owning_pool; #endif - } h; + } h; }; static union block_hdr *block_freelist = NULL; @@ -184,16 +184,16 @@ static union block_hdr *global_block_list; #define debug_fill(ptr,size) ((void)memset((ptr), FILL_BYTE, (size))) -static ap_inline void debug_verify_filled(const char *ptr, - const char *endp, const char *error_msg) +static ap_inline void +debug_verify_filled(const char *ptr, const char *endp, const char *error_msg) { - for (; ptr < endp; ++ptr) { - if (*ptr != FILL_BYTE) { - fputs(error_msg, stderr); - abort(); - exit(1); + for (; ptr < endp; ++ptr) { + if (*ptr != FILL_BYTE) { + fputs(error_msg, stderr); + abort(); + exit(1); + } } - } } #else @@ -206,197 +206,205 @@ static ap_inline void debug_verify_filled(const char *ptr, malloc() to provide aligned memory. */ #if defined(EAPI_MM) -static union block_hdr *malloc_block(int size, int is_shm) +static union block_hdr +*malloc_block(int size, int is_shm) #else -static union block_hdr *malloc_block(int size) +static union block_hdr +*malloc_block(int size) #endif { - union block_hdr *blok; - int request_size; + union block_hdr *blok; + int request_size; #ifdef ALLOC_DEBUG - /* make some room at the end which we'll fill and expect to be - * always filled - */ - size += CLICK_SZ; + /* + * make some room at the end which we'll fill and expect to be + * always filled + */ + size += CLICK_SZ; #endif - request_size = size + sizeof(union block_hdr); + request_size = size + sizeof(union block_hdr); #if defined(EAPI_MM) - if (is_shm) - blok = (union block_hdr *)ap_mm_malloc(mm, request_size); - else -#endif - blok = (union block_hdr *) malloc(request_size); - if (blok == NULL) { - fprintf(stderr, "Ouch! malloc(%d) failed in malloc_block()\n", - request_size); - exit(1); - } - debug_fill(blok, size + sizeof(union block_hdr)); + if (is_shm) + blok = (union block_hdr *)ap_mm_malloc(mm, request_size); + else +#endif + blok = (union block_hdr *) malloc(request_size); + if (blok == NULL) { + fprintf(stderr, "Ouch! malloc(%d) failed in malloc_block()\n", + request_size); + exit(1); + } + debug_fill(blok, size + sizeof(union block_hdr)); #if defined(EAPI_MM) - blok->h.is_shm = is_shm; + blok->h.is_shm = is_shm; #endif - blok->h.next = NULL; - blok->h.first_avail = (char *) (blok + 1); - blok->h.endp = size + blok->h.first_avail; + blok->h.next = NULL; + blok->h.first_avail = (char *)(blok + 1); + blok->h.endp = size + blok->h.first_avail; #ifdef ALLOC_DEBUG - blok->h.endp -= CLICK_SZ; + blok->h.endp -= CLICK_SZ; #endif #ifdef POOL_DEBUG - blok->h.global_next = global_block_list; - global_block_list = blok; - blok->h.owning_pool = NULL; + blok->h.global_next = global_block_list; + global_block_list = blok; + blok->h.owning_pool = NULL; #endif - return blok; + return blok; } - - #if defined(ALLOC_DEBUG) && !defined(ALLOC_USE_MALLOC) -static void chk_on_blk_list(union block_hdr *blok, union block_hdr *free_blk) -{ - debug_verify_filled(blok->h.endp, blok->h.endp + CLICK_SZ, - "Ouch! Someone trounced the padding at the end of a block!\n"); - while (free_blk) { - if (free_blk == blok) { - fprintf(stderr, "Ouch! Freeing free block\n"); - abort(); - exit(1); +static void +chk_on_blk_list(union block_hdr *blok, union block_hdr *free_blk) +{ + debug_verify_filled(blok->h.endp, blok->h.endp + CLICK_SZ, + "Ouch! Someone trounced the padding at the end of a block!\n"); + while (free_blk) { + if (free_blk == blok) { + fprintf(stderr, "Ouch! Freeing free block\n"); + abort(); + exit(1); + } + free_blk = free_blk->h.next; } - free_blk = free_blk->h.next; - } } #else #define chk_on_blk_list(_x, _y) #endif /* Free a chain of blocks --- must be called with alarms blocked. */ - -static void free_blocks(union block_hdr *blok) +static void +free_blocks(union block_hdr *blok) { #ifdef ALLOC_USE_MALLOC - union block_hdr *next; + union block_hdr *next; - for (; blok; blok = next) { - next = blok->h.next; - free(blok); - } + for (; blok; blok = next) { + next = blok->h.next; + free(blok); + } #else - /* First, put new blocks at the head of the free list --- - * we'll eventually bash the 'next' pointer of the last block - * in the chain to point to the free blocks we already had. - */ - - union block_hdr *old_free_list; + /* + * First, put new blocks at the head of the free list --- + * we'll eventually bash the 'next' pointer of the last block + * in the chain to point to the free blocks we already had. + */ + union block_hdr *old_free_list; - if (blok == NULL) - return; /* Sanity check --- freeing empty pool? */ + /* Sanity check --- freeing empty pool? */ + if (blok == NULL) + return; #if defined(EAPI_MM) - if (blok->h.is_shm) - (void)ap_mm_lock(mm, AP_MM_LOCK_RW); + if (blok->h.is_shm) + (void)ap_mm_lock(mm, AP_MM_LOCK_RW); #endif - (void) ap_acquire_mutex(alloc_mutex); - old_free_list = block_freelist; - block_freelist = blok; + (void) ap_acquire_mutex(alloc_mutex); + old_free_list = block_freelist; + block_freelist = blok; - /* - * Next, adjust first_avail pointers of each block --- have to do it - * sooner or later, and it simplifies the search in new_block to do it - * now. - */ + /* + * Next, adjust first_avail pointers of each block --- have to do it + * sooner or later, and it simplifies the search in new_block to do it + * now. + */ + while (blok->h.next != NULL) { + chk_on_blk_list(blok, old_free_list); + blok->h.first_avail = (char *)(blok + 1); + debug_fill(blok->h.first_avail, + blok->h.endp - blok->h.first_avail); +#ifdef POOL_DEBUG + blok->h.owning_pool = FREE_POOL; +#endif + blok = blok->h.next; + } - while (blok->h.next != NULL) { chk_on_blk_list(blok, old_free_list); - blok->h.first_avail = (char *) (blok + 1); + blok->h.first_avail = (char *)(blok + 1); debug_fill(blok->h.first_avail, blok->h.endp - blok->h.first_avail); #ifdef POOL_DEBUG blok->h.owning_pool = FREE_POOL; #endif - blok = blok->h.next; - } - - chk_on_blk_list(blok, old_free_list); - blok->h.first_avail = (char *) (blok + 1); - debug_fill(blok->h.first_avail, blok->h.endp - blok->h.first_avail); -#ifdef POOL_DEBUG - blok->h.owning_pool = FREE_POOL; -#endif - - /* Finally, reset next pointer to get the old free blocks back */ - blok->h.next = old_free_list; + /* Finally, reset next pointer to get the old free blocks back */ + blok->h.next = old_free_list; - (void) ap_release_mutex(alloc_mutex); + (void) ap_release_mutex(alloc_mutex); #if defined(EAPI_MM) - if (blok->h.is_shm) - (void)ap_mm_unlock(mm); + if (blok->h.is_shm) + (void)ap_mm_unlock(mm); #endif #endif } -/* Get a new block, from our own free list if possible, from the system +/* + * Get a new block, from our own free list if possible, from the system * if necessary. Must be called with alarms blocked. */ - #if defined(EAPI_MM) -static union block_hdr *new_block(int min_size, int is_shm) +static union block_hdr +*new_block(int min_size, int is_shm) #else -static union block_hdr *new_block(int min_size) +static union block_hdr +*new_block(int min_size) #endif { - union block_hdr **lastptr = &block_freelist; - union block_hdr *blok = block_freelist; + union block_hdr **lastptr = &block_freelist; + union block_hdr *blok = block_freelist; - /* First, see if we have anything of the required size - * on the free list... - */ - - while (blok != NULL) { + /* + * First, see if we have anything of the required size + * on the free list... + */ + while (blok != NULL) { #if defined(EAPI_MM) - if (blok->h.is_shm == is_shm && - min_size + BLOCK_MINFREE <= blok->h.endp - blok->h.first_avail) { + if (blok->h.is_shm == is_shm && + min_size + BLOCK_MINFREE <= blok->h.endp - + blok->h.first_avail) { #else - if (min_size + BLOCK_MINFREE <= blok->h.endp - blok->h.first_avail) { -#endif - *lastptr = blok->h.next; - blok->h.next = NULL; - debug_verify_filled(blok->h.first_avail, blok->h.endp, - "Ouch! Someone trounced a block on the free list!\n"); - return blok; - } - else { - lastptr = &blok->h.next; - blok = blok->h.next; + if (min_size + BLOCK_MINFREE <= blok->h.endp - + blok->h.first_avail) { +#endif + *lastptr = blok->h.next; + blok->h.next = NULL; + debug_verify_filled(blok->h.first_avail, blok->h.endp, + "Ouch! Someone trounced a block on the free " + "list!\n"); + return blok; + } + else { + lastptr = &blok->h.next; + blok = blok->h.next; + } } - } - - /* Nope. */ - min_size += BLOCK_MINFREE; + /* Nope. */ + min_size += BLOCK_MINFREE; #if defined(EAPI_MM) - blok = malloc_block((min_size > BLOCK_MINALLOC) ? min_size : BLOCK_MINALLOC, is_shm); + blok = malloc_block((min_size > BLOCK_MINALLOC) ? + min_size : BLOCK_MINALLOC, is_shm); #else - blok = malloc_block((min_size > BLOCK_MINALLOC) ? min_size : BLOCK_MINALLOC); + blok = malloc_block((min_size > BLOCK_MINALLOC) ? + min_size : BLOCK_MINALLOC); #endif - return blok; + return blok; } /* Accounting */ - -static long bytes_in_block_list(union block_hdr *blok) +static long +bytes_in_block_list(union block_hdr *blok) { - long size = 0; + long size = 0; - while (blok) { - size += blok->h.endp - (char *) (blok + 1); - blok = blok->h.next; - } + while (blok) { + size += blok->h.endp - (char *)(blok + 1); + blok = blok->h.next; + } - return size; + return size; } @@ -415,23 +423,23 @@ static void run_cleanups(struct cleanup *); static void free_proc_chain(struct process_chain *); struct pool { - union block_hdr *first; - union block_hdr *last; - struct cleanup *cleanups; - struct process_chain *subprocesses; - struct pool *sub_pools; - struct pool *sub_next; - struct pool *sub_prev; - struct pool *parent; - char *free_first_avail; + union block_hdr *first; + union block_hdr *last; + struct cleanup *cleanups; + struct process_chain *subprocesses; + struct pool *sub_pools; + struct pool *sub_next; + struct pool *sub_prev; + struct pool *parent; + char *free_first_avail; #ifdef ALLOC_USE_MALLOC - void *allocation_list; + void *allocation_list; #endif #ifdef POOL_DEBUG - struct pool *joined; + struct pool *joined; #endif #if defined(EAPI_MM) - int is_shm; + int is_shm; #endif }; @@ -448,258 +456,274 @@ static pool *permanent_pool; #define POOL_HDR_BYTES (POOL_HDR_CLICKS * CLICK_SZ) #if defined(EAPI_MM) -static struct pool *make_sub_pool_internal(struct pool *p, int is_shm) +static struct pool +*make_sub_pool_internal(struct pool *p, int is_shm) #else -API_EXPORT(struct pool *) ap_make_sub_pool(struct pool *p) +API_EXPORT(struct pool *) +ap_make_sub_pool(struct pool *p) #endif { - union block_hdr *blok; - pool *new_pool; + union block_hdr *blok; + pool *new_pool; - ap_block_alarms(); + ap_block_alarms(); #if defined(EAPI_MM) - if (is_shm) - (void)ap_mm_lock(mm, AP_MM_LOCK_RW); + if (is_shm) + (void)ap_mm_lock(mm, AP_MM_LOCK_RW); #endif - (void) ap_acquire_mutex(alloc_mutex); + (void) ap_acquire_mutex(alloc_mutex); #if defined(EAPI_MM) - blok = new_block(POOL_HDR_BYTES, is_shm); + blok = new_block(POOL_HDR_BYTES, is_shm); #else - blok = new_block(POOL_HDR_BYTES); + blok = new_block(POOL_HDR_BYTES); #endif - new_pool = (pool *) blok->h.first_avail; - blok->h.first_avail += POOL_HDR_BYTES; + new_pool = (pool *)blok->h.first_avail; + blok->h.first_avail += POOL_HDR_BYTES; #ifdef POOL_DEBUG - blok->h.owning_pool = new_pool; + blok->h.owning_pool = new_pool; #endif - memset((char *) new_pool, '\0', sizeof(struct pool)); - new_pool->free_first_avail = blok->h.first_avail; - new_pool->first = new_pool->last = blok; + memset((char *)new_pool, '\0', sizeof(struct pool)); + new_pool->free_first_avail = blok->h.first_avail; + new_pool->first = new_pool->last = blok; - if (p) { - new_pool->parent = p; - new_pool->sub_next = p->sub_pools; - if (new_pool->sub_next) - new_pool->sub_next->sub_prev = new_pool; - p->sub_pools = new_pool; - } + if (p) { + new_pool->parent = p; + new_pool->sub_next = p->sub_pools; + if (new_pool->sub_next) + new_pool->sub_next->sub_prev = new_pool; + p->sub_pools = new_pool; + } #if defined(EAPI_MM) - new_pool->is_shm = is_shm; + new_pool->is_shm = is_shm; #endif - (void) ap_release_mutex(alloc_mutex); + (void)ap_release_mutex(alloc_mutex); #if defined(EAPI_MM) - if (is_shm) - (void)ap_mm_unlock(mm); + if (is_shm) + (void)ap_mm_unlock(mm); #endif - ap_unblock_alarms(); + ap_unblock_alarms(); - return new_pool; + return new_pool; } #if defined(EAPI_MM) -API_EXPORT(struct pool *) ap_make_sub_pool(struct pool *p) +API_EXPORT(struct pool *) +ap_make_sub_pool(struct pool *p) { - return make_sub_pool_internal(p, 0); + return make_sub_pool_internal(p, 0); } -API_EXPORT(struct pool *) ap_make_shared_sub_pool(struct pool *p) +API_EXPORT(struct pool *) +ap_make_shared_sub_pool(struct pool *p) { - return make_sub_pool_internal(p, 1); + return make_sub_pool_internal(p, 1); } #else -API_EXPORT(struct pool *) ap_make_shared_sub_pool(struct pool *p) +API_EXPORT(struct pool *) +ap_make_shared_sub_pool(struct pool *p) { - return NULL; + return NULL; } #endif #ifdef POOL_DEBUG -static void stack_var_init(char *s) +static void +stack_var_init(char *s) { - char t; + char t; - if (s < &t) { - stack_direction = 1; /* stack grows up */ - } - else { - stack_direction = -1; /* stack grows down */ - } + if (s < &t) + stack_direction = 1; /* stack grows up */ + else + stack_direction = -1; /* stack grows down */ } #endif -int ap_shared_pool_possible(void) +int +ap_shared_pool_possible(void) { - return ap_mm_useable(); + return ap_mm_useable(); } -API_EXPORT(pool *) ap_init_alloc(void) +API_EXPORT(pool *) +ap_init_alloc(void) { #ifdef POOL_DEBUG - char s; + char s; - known_stack_point = &s; - stack_var_init(&s); + known_stack_point = &s; + stack_var_init(&s); #endif - alloc_mutex = ap_create_mutex(NULL); - spawn_mutex = ap_create_mutex(NULL); - permanent_pool = ap_make_sub_pool(NULL); - return permanent_pool; + alloc_mutex = ap_create_mutex(NULL); + spawn_mutex = ap_create_mutex(NULL); + permanent_pool = ap_make_sub_pool(NULL); + return permanent_pool; } -void ap_init_alloc_shared(int early) +void +ap_init_alloc_shared(int early) { #if defined(EAPI_MM) - int mm_size; - char *mm_path; - char *err1, *err2; - - if (early) { - /* process very early on startup */ - mm_size = ap_mm_maxsize(); - if (mm_size > EAPI_MM_CORE_MAXSIZE) - mm_size = EAPI_MM_CORE_MAXSIZE; - mm_path = ap_server_root_relative(permanent_pool, - ap_psprintf(permanent_pool, "%s.%ld", - EAPI_MM_CORE_PATH, (long)getpid())); - if ((mm = ap_mm_create(mm_size, mm_path)) == NULL) { - fprintf(stderr, "Ouch! ap_mm_create(%d, \"%s\") failed\n", mm_size, mm_path); - err1 = ap_mm_error(); - if (err1 == NULL) - err1 = "-unknown-"; - err2 = strerror(errno); - if (err2 == NULL) - err2 = "-unknown-"; - fprintf(stderr, "Error: MM: %s: OS: %s\n", err1, err2); - exit(1); - } - } - else { - /* process a lot later on startup */ - ap_mm_permission(mm, (S_IRUSR|S_IWUSR), ap_user_id, -1); - } + int mm_size; + char *mm_path; + char *err1, *err2; + + if (early) { + /* process very early on startup */ + mm_size = ap_mm_maxsize(); + if (mm_size > EAPI_MM_CORE_MAXSIZE) + mm_size = EAPI_MM_CORE_MAXSIZE; + mm_path = ap_server_root_relative(permanent_pool, + ap_psprintf(permanent_pool, "%s.%ld", + EAPI_MM_CORE_PATH, (long)getpid())); + if ((mm = ap_mm_create(mm_size, mm_path)) == NULL) { + fprintf(stderr, "Ouch! ap_mm_create(%d, \"%s\") " + "failed\n", mm_size, mm_path); + err1 = ap_mm_error(); + if (err1 == NULL) + err1 = "-unknown-"; + err2 = strerror(errno); + if (err2 == NULL) + err2 = "-unknown-"; + fprintf(stderr, "Error: MM: %s: OS: %s\n", err1, err2); + exit(1); + } + } else { + /* process a lot later on startup */ + ap_mm_permission(mm, (S_IRUSR|S_IWUSR), ap_user_id, -1); + } #endif /* EAPI_MM */ - return; + return; } -void ap_kill_alloc_shared(void) +void +ap_kill_alloc_shared(void) { #if defined(EAPI_MM) - if (mm != NULL) { - ap_mm_destroy(mm); - mm = NULL; - } + if (mm != NULL) { + ap_mm_destroy(mm); + mm = NULL; + } #endif /* EAPI_MM */ - return; + return; } -void ap_cleanup_alloc(void) +void +ap_cleanup_alloc(void) { - ap_destroy_mutex(alloc_mutex); - ap_destroy_mutex(spawn_mutex); + ap_destroy_mutex(alloc_mutex); + ap_destroy_mutex(spawn_mutex); } -API_EXPORT(void) ap_clear_pool(struct pool *a) +API_EXPORT(void) +ap_clear_pool(struct pool *a) { - ap_block_alarms(); + ap_block_alarms(); #if defined(EAPI_MM) - if (a->is_shm) - (void)ap_mm_lock(mm, AP_MM_LOCK_RW); + if (a->is_shm) + (void)ap_mm_lock(mm, AP_MM_LOCK_RW); #endif - (void) ap_acquire_mutex(alloc_mutex); - while (a->sub_pools) - ap_destroy_pool(a->sub_pools); - (void) ap_release_mutex(alloc_mutex); + (void) ap_acquire_mutex(alloc_mutex); + while (a->sub_pools) + ap_destroy_pool(a->sub_pools); + (void) ap_release_mutex(alloc_mutex); #if defined(EAPI_MM) - if (a->is_shm) - (void)ap_mm_unlock(mm); -#endif - /* Don't hold the mutex during cleanups. */ - run_cleanups(a->cleanups); - a->cleanups = NULL; - free_proc_chain(a->subprocesses); - a->subprocesses = NULL; - free_blocks(a->first->h.next); - a->first->h.next = NULL; - - a->last = a->first; - a->first->h.first_avail = a->free_first_avail; - debug_fill(a->first->h.first_avail, + if (a->is_shm) + ( void)ap_mm_unlock(mm); +#endif + /* Don't hold the mutex during cleanups. */ + run_cleanups(a->cleanups); + a->cleanups = NULL; + free_proc_chain(a->subprocesses); + a->subprocesses = NULL; + free_blocks(a->first->h.next); + a->first->h.next = NULL; + + a->last = a->first; + a->first->h.first_avail = a->free_first_avail; + debug_fill(a->first->h.first_avail, a->first->h.endp - a->first->h.first_avail); #ifdef ALLOC_USE_MALLOC - { - void *c, *n; + { + void *c, *n; - for (c = a->allocation_list; c; c = n) { - n = *(void **)c; - free(c); + for (c = a->allocation_list; c; c = n) { + n = *(void **)c; + free(c); + } + a->allocation_list = NULL; } - a->allocation_list = NULL; - } #endif - ap_unblock_alarms(); + ap_unblock_alarms(); } -API_EXPORT(void) ap_destroy_pool(pool *a) +API_EXPORT(void) +ap_destroy_pool(pool *a) { - ap_block_alarms(); - ap_clear_pool(a); + ap_block_alarms(); + ap_clear_pool(a); #if defined(EAPI_MM) - if (a->is_shm) - (void)ap_mm_lock(mm, AP_MM_LOCK_RW); -#endif - (void) ap_acquire_mutex(alloc_mutex); - if (a->parent) { - if (a->parent->sub_pools == a) - a->parent->sub_pools = a->sub_next; - if (a->sub_prev) - a->sub_prev->sub_next = a->sub_next; - if (a->sub_next) - a->sub_next->sub_prev = a->sub_prev; - } - (void) ap_release_mutex(alloc_mutex); + if (a->is_shm) + (void)ap_mm_lock(mm, AP_MM_LOCK_RW); +#endif + (void)ap_acquire_mutex(alloc_mutex); + if (a->parent) { + if (a->parent->sub_pools == a) + a->parent->sub_pools = a->sub_next; + if (a->sub_prev) + a->sub_prev->sub_next = a->sub_next; + if (a->sub_next) + a->sub_next->sub_prev = a->sub_prev; + } + (void)ap_release_mutex(alloc_mutex); #if defined(EAPI_MM) - if (a->is_shm) - (void)ap_mm_unlock(mm); + if (a->is_shm) + (void)ap_mm_unlock(mm); #endif - free_blocks(a->first); - ap_unblock_alarms(); + free_blocks(a->first); + ap_unblock_alarms(); } -API_EXPORT(long) ap_bytes_in_pool(pool *p) +API_EXPORT(long) +ap_bytes_in_pool(pool *p) { - return bytes_in_block_list(p->first); + return bytes_in_block_list(p->first); } -API_EXPORT(long) ap_bytes_in_free_blocks(void) +API_EXPORT(long) +ap_bytes_in_free_blocks(void) { - return bytes_in_block_list(block_freelist); + return bytes_in_block_list(block_freelist); } -API_EXPORT(int) ap_acquire_pool(pool *p, ap_pool_lock_mode mode) +API_EXPORT(int) +ap_acquire_pool(pool *p, ap_pool_lock_mode mode) { #if defined(EAPI_MM) - if (!p->is_shm) - return 1; - return ap_mm_lock(mm, mode == AP_POOL_RD ? AP_MM_LOCK_RD : AP_MM_LOCK_RW); + if (!p->is_shm) + return 1; + return ap_mm_lock(mm, mode == AP_POOL_RD ? + AP_MM_LOCK_RD : AP_MM_LOCK_RW); #else return 1; #endif } -API_EXPORT(int) ap_release_pool(pool *p) +API_EXPORT(int) +ap_release_pool(pool *p) { #if defined(EAPI_MM) - if (!p->is_shm) - return 1; - return ap_mm_unlock(mm); + if (!p->is_shm) + return 1; + return ap_mm_unlock(mm); #else return 1; #endif @@ -724,94 +748,100 @@ extern char _end; /* Find the pool that ts belongs to, return NULL if it doesn't * belong to any pool. */ -API_EXPORT(pool *) ap_find_pool(const void *ts) -{ - const char *s = ts; - union block_hdr **pb; - union block_hdr *b; - - /* short-circuit stuff which is in TEXT, BSS, or DATA */ - if (is_ptr_in_range(s, 0, &_end)) { - return NULL; - } - /* consider stuff on the stack to also be in the NULL pool... - * XXX: there's cases where we don't want to assume this - */ - if ((stack_direction == -1 && is_ptr_in_range(s, &ts, known_stack_point)) - || (stack_direction == 1 && is_ptr_in_range(s, known_stack_point, &ts))) { - abort(); - return NULL; - } - ap_block_alarms(); - /* search the global_block_list */ - for (pb = &global_block_list; *pb; pb = &b->h.global_next) { - b = *pb; - if (is_ptr_in_range(s, b, b->h.endp)) { - if (b->h.owning_pool == FREE_POOL) { - fprintf(stderr, - "Ouch! find_pool() called on pointer in a free block\n"); +API_EXPORT(pool *) +ap_find_pool(const void *ts) +{ + const char *s = ts; + union block_hdr **pb; + union block_hdr *b; + + /* short-circuit stuff which is in TEXT, BSS, or DATA */ + if (is_ptr_in_range(s, 0, &_end)) + return NULL; + + /* consider stuff on the stack to also be in the NULL pool... + * XXX: there's cases where we don't want to assume this + */ + if ((stack_direction == -1 && + is_ptr_in_range(s, &ts, known_stack_point)) + || (stack_direction == 1 && + is_ptr_in_range(s, known_stack_point, &ts))) { abort(); - exit(1); - } - if (b != global_block_list) { - /* promote b to front of list, this is a hack to speed - * up the lookup */ - *pb = b->h.global_next; - b->h.global_next = global_block_list; - global_block_list = b; - } - ap_unblock_alarms(); - return b->h.owning_pool; - } - } - ap_unblock_alarms(); - return NULL; + return NULL; + } + ap_block_alarms(); + /* search the global_block_list */ + for (pb = &global_block_list; *pb; pb = &b->h.global_next) { + b = *pb; + if (is_ptr_in_range(s, b, b->h.endp)) { + if (b->h.owning_pool == FREE_POOL) { + fprintf(stderr, + "Ouch! find_pool() called on pointer in " + "a free block\n"); + abort(); + exit(1); + } + if (b != global_block_list) { + /* + * promote b to front of list, this is a + * hack to speed up the lookup + */ + *pb = b->h.global_next; + b->h.global_next = global_block_list; + global_block_list = b; + } + ap_unblock_alarms(); + return b->h.owning_pool; + } + } + ap_unblock_alarms(); + return NULL; } /* return TRUE iff a is an ancestor of b * NULL is considered an ancestor of all pools */ -API_EXPORT(int) ap_pool_is_ancestor(pool *a, pool *b) +API_EXPORT(int) +ap_pool_is_ancestor(pool *a, pool *b) { - if (a == NULL) { - return 1; - } - while (a->joined) { - a = a->joined; - } - while (b) { - if (a == b) { - return 1; + if (a == NULL) + return 1; + + while (a->joined) + a = a->joined; + + while (b) { + if (a == b) + return 1; + b = b->parent; } - b = b->parent; - } - return 0; + return 0; } /* All blocks belonging to sub will be changed to point to p * instead. This is a guarantee by the caller that sub will not * be destroyed before p is. */ -API_EXPORT(void) ap_pool_join(pool *p, pool *sub) +API_EXPORT(void) +ap_pool_join(pool *p, pool *sub) { - union block_hdr *b; + union block_hdr *b; - /* We could handle more general cases... but this is it for now. */ - if (sub->parent != p) { - fprintf(stderr, "pool_join: p is not parent of sub\n"); - abort(); - } - ap_block_alarms(); - while (p->joined) { - p = p->joined; - } - sub->joined = p; - for (b = global_block_list; b; b = b->h.global_next) { - if (b->h.owning_pool == sub) { - b->h.owning_pool = p; + /* We could handle more general cases... but this is it for now. */ + if (sub->parent != p) { + fprintf(stderr, "pool_join: p is not parent of sub\n"); + abort(); + } + ap_block_alarms(); + while (p->joined) + p = p->joined; + + sub->joined = p; + for (b = global_block_list; b; b = b->h.global_next) { + if (b->h.owning_pool == sub) + b->h.owning_pool = p; } - } - ap_unblock_alarms(); + ap_unblock_alarms(); } #endif @@ -821,158 +851,159 @@ API_EXPORT(void) ap_pool_join(pool *p, pool *sub) */ -API_EXPORT(void *) ap_palloc(struct pool *a, int reqsize) +API_EXPORT(void *) +ap_palloc(struct pool *a, int reqsize) { #ifdef ALLOC_USE_MALLOC - int size = reqsize + CLICK_SZ; - void *ptr; - - ap_block_alarms(); - ptr = malloc(size); - if (ptr == NULL) { - fputs("Ouch! Out of memory!\n", stderr); - exit(1); - } - debug_fill(ptr, size); /* might as well get uninitialized protection */ - *(void **)ptr = a->allocation_list; - a->allocation_list = ptr; - ap_unblock_alarms(); - return (char *)ptr + CLICK_SZ; -#else - - /* Round up requested size to an even number of alignment units (core clicks) - */ + int size = reqsize + CLICK_SZ; + void *ptr; - int nclicks = 1 + ((reqsize - 1) / CLICK_SZ); - int size = nclicks * CLICK_SZ; - - /* First, see if we have space in the block most recently - * allocated to this pool - */ + ap_block_alarms(); + ptr = malloc(size); + if (ptr == NULL) { + fputs("Ouch! Out of memory!\n", stderr); + exit(1); + } + debug_fill(ptr, size); /* might as well get uninitialized protection */ + *(void **)ptr = a->allocation_list; + a->allocation_list = ptr; + ap_unblock_alarms(); + return (char *)ptr + CLICK_SZ; +#else - union block_hdr *blok = a->last; - char *first_avail = blok->h.first_avail; - char *new_first_avail; + /* + * Round up requested size to an even number of alignment units + * (core clicks) + */ + int nclicks = 1 + ((reqsize - 1) / CLICK_SZ); + int size = nclicks * CLICK_SZ; - if (reqsize <= 0) - return NULL; + /* + * First, see if we have space in the block most recently + * allocated to this pool + */ + union block_hdr *blok = a->last; + char *first_avail = blok->h.first_avail; + char *new_first_avail; - new_first_avail = first_avail + size; + if (reqsize <= 0) + return NULL; - if (new_first_avail <= blok->h.endp) { - debug_verify_filled(first_avail, blok->h.endp, - "Ouch! Someone trounced past the end of their allocation!\n"); - blok->h.first_avail = new_first_avail; - return (void *) first_avail; - } + new_first_avail = first_avail + size; - /* Nope --- get a new one that's guaranteed to be big enough */ + if (new_first_avail <= blok->h.endp) { + debug_verify_filled(first_avail, blok->h.endp, + "Ouch! Someone trounced past the end of their " + "allocation!\n"); + blok->h.first_avail = new_first_avail; + return (void *)first_avail; + } - ap_block_alarms(); + /* Nope --- get a new one that's guaranteed to be big enough */ + ap_block_alarms(); #if defined(EAPI_MM) - if (a->is_shm) - (void)ap_mm_lock(mm, AP_MM_LOCK_RW); + if (a->is_shm) + (void)ap_mm_lock(mm, AP_MM_LOCK_RW); #endif - (void) ap_acquire_mutex(alloc_mutex); + (void) ap_acquire_mutex(alloc_mutex); #if defined(EAPI_MM) - blok = new_block(size, a->is_shm); + blok = new_block(size, a->is_shm); #else - blok = new_block(size); + blok = new_block(size); #endif - a->last->h.next = blok; - a->last = blok; + a->last->h.next = blok; + a->last = blok; #ifdef POOL_DEBUG - blok->h.owning_pool = a; + blok->h.owning_pool = a; #endif #if defined(EAPI_MM) - blok->h.is_shm = a->is_shm; + blok->h.is_shm = a->is_shm; #endif - (void) ap_release_mutex(alloc_mutex); + (void)ap_release_mutex(alloc_mutex); #if defined(EAPI_MM) - if (a->is_shm) - (void)ap_mm_unlock(mm); + if (a->is_shm) + (void)ap_mm_unlock(mm); #endif - ap_unblock_alarms(); + ap_unblock_alarms(); - first_avail = blok->h.first_avail; - blok->h.first_avail += size; + first_avail = blok->h.first_avail; + blok->h.first_avail += size; - return (void *) first_avail; + return (void *)first_avail; #endif } -API_EXPORT(void *) ap_pcalloc(struct pool *a, int size) +API_EXPORT(void *) +ap_pcalloc(struct pool *a, int size) { - void *res = ap_palloc(a, size); - memset(res, '\0', size); - return res; + void *res = ap_palloc(a, size); + memset(res, '\0', size); + return res; } -API_EXPORT(char *) ap_pstrdup(struct pool *a, const char *s) +API_EXPORT(char *) +ap_pstrdup(struct pool *a, const char *s) { - char *res; - size_t len; + char *res; + size_t len; - if (s == NULL) - return NULL; - len = strlen(s) + 1; - res = ap_palloc(a, len); - memcpy(res, s, len); - return res; + if (s == NULL) + return NULL; + len = strlen(s) + 1; + res = ap_palloc(a, len); + memcpy(res, s, len); + return res; } -API_EXPORT(char *) ap_pstrndup(struct pool *a, const char *s, int n) +API_EXPORT(char *) +ap_pstrndup(struct pool *a, const char *s, int n) { - char *res; + char *res; - if (s == NULL) - return NULL; - res = ap_palloc(a, n + 1); - memcpy(res, s, n); - res[n] = '\0'; - return res; + if (s == NULL) + return NULL; + res = ap_palloc(a, n + 1); + memcpy(res, s, n); + res[n] = '\0'; + return res; } API_EXPORT_NONSTD(char *) ap_pstrcat(pool *a,...) { - char *cp, *argp, *res; - - /* Pass one --- find length of required string */ + char *cp, *argp, *res; - int len = 0; - va_list adummy; + /* Pass one --- find length of required string */ + int len = 0; + va_list adummy; - va_start(adummy, a); + va_start(adummy, a); - while ((cp = va_arg(adummy, char *)) != NULL) - len += strlen(cp); + while ((cp = va_arg(adummy, char *)) != NULL) + len += strlen(cp); - va_end(adummy); + va_end(adummy); - /* Allocate the required string */ + /* Allocate the required string */ + res = (char *) ap_palloc(a, len + 1); + cp = res; + *cp = '\0'; - res = (char *) ap_palloc(a, len + 1); - cp = res; - *cp = '\0'; + /* Pass two --- copy the argument strings into the result space */ + va_start(adummy, a); - /* Pass two --- copy the argument strings into the result space */ - - va_start(adummy, a); - - while ((argp = va_arg(adummy, char *)) != NULL) { - strlcpy(cp, argp, len + 1); - cp += strlen(argp); - } - - va_end(adummy); + while ((argp = va_arg(adummy, char *)) != NULL) { + strlcpy(cp, argp, len + 1); + cp += strlen(argp); + } - /* Return the result string */ + va_end(adummy); - return res; + /* Return the result string */ + return res; } /* ap_psprintf is implemented by writing directly into the current @@ -989,186 +1020,191 @@ API_EXPORT_NONSTD(char *) ap_pstrcat(pool *a,...) */ struct psprintf_data { - ap_vformatter_buff vbuff; + ap_vformatter_buff vbuff; #ifdef ALLOC_USE_MALLOC - char *base; + char *base; #else - union block_hdr *blok; - int got_a_new_block; + union block_hdr *blok; + int got_a_new_block; #endif }; #define AP_PSPRINTF_MIN_SIZE 32 /* Minimum size of allowable avail block */ -static int psprintf_flush(ap_vformatter_buff *vbuff) +static int +psprintf_flush(ap_vformatter_buff *vbuff) { - struct psprintf_data *ps = (struct psprintf_data *)vbuff; + struct psprintf_data *ps = (struct psprintf_data *)vbuff; #ifdef ALLOC_USE_MALLOC - int cur_len, size; - char *ptr; + int cur_len, size; + char *ptr; - cur_len = (char *)ps->vbuff.curpos - ps->base; - size = cur_len << 1; - if (size < AP_PSPRINTF_MIN_SIZE) - size = AP_PSPRINTF_MIN_SIZE; + cur_len = (char *)ps->vbuff.curpos - ps->base; + size = cur_len << 1; + if (size < AP_PSPRINTF_MIN_SIZE) + size = AP_PSPRINTF_MIN_SIZE; #if defined(EAPI_MM) - if (ps->block->h.is_shm) - ptr = ap_mm_realloc(ps->base, size); - else -#endif - ptr = realloc(ps->base, size); - if (ptr == NULL) { - fputs("Ouch! Out of memory!\n", stderr); - exit(1); - } - ps->base = ptr; - ps->vbuff.curpos = ptr + cur_len; - ps->vbuff.endpos = ptr + size - 1; - return 0; + if (ps->block->h.is_shm) + ptr = ap_mm_realloc(ps->base, size); + else +#endif + ptr = realloc(ps->base, size); + if (ptr == NULL) { + fputs("Ouch! Out of memory!\n", stderr); + exit(1); + } + ps->base = ptr; + ps->vbuff.curpos = ptr + cur_len; + ps->vbuff.endpos = ptr + size - 1; + return 0; #else - union block_hdr *blok; - union block_hdr *nblok; - size_t cur_len, size; - char *strp; - - blok = ps->blok; - strp = ps->vbuff.curpos; - cur_len = strp - blok->h.first_avail; - size = cur_len << 1; - if (size < AP_PSPRINTF_MIN_SIZE) - size = AP_PSPRINTF_MIN_SIZE; - - /* must try another blok */ + union block_hdr *blok; + union block_hdr *nblok; + size_t cur_len, size; + char *strp; + + blok = ps->blok; + strp = ps->vbuff.curpos; + cur_len = strp - blok->h.first_avail; + size = cur_len << 1; + if (size < AP_PSPRINTF_MIN_SIZE) + size = AP_PSPRINTF_MIN_SIZE; + + /* must try another blok */ #if defined(EAPI_MM) - if (blok->h.is_shm) - (void)ap_mm_lock(mm, AP_MM_LOCK_RW); + if (blok->h.is_shm) + (void)ap_mm_lock(mm, AP_MM_LOCK_RW); #endif - (void) ap_acquire_mutex(alloc_mutex); + (void)ap_acquire_mutex(alloc_mutex); #if defined(EAPI_MM) - nblok = new_block(size, blok->h.is_shm); + nblok = new_block(size, blok->h.is_shm); #else - nblok = new_block(size); + nblok = new_block(size); #endif - (void) ap_release_mutex(alloc_mutex); + (void)ap_release_mutex(alloc_mutex); #if defined(EAPI_MM) - if (blok->h.is_shm) - (void)ap_mm_unlock(mm); -#endif - memcpy(nblok->h.first_avail, blok->h.first_avail, cur_len); - ps->vbuff.curpos = nblok->h.first_avail + cur_len; - /* save a byte for the NUL terminator */ - ps->vbuff.endpos = nblok->h.endp - 1; - - /* did we allocate the current blok? if so free it up */ - if (ps->got_a_new_block) { - debug_fill(blok->h.first_avail, blok->h.endp - blok->h.first_avail); + if (blok->h.is_shm) + (void)ap_mm_unlock(mm); +#endif + memcpy(nblok->h.first_avail, blok->h.first_avail, cur_len); + ps->vbuff.curpos = nblok->h.first_avail + cur_len; + /* save a byte for the NUL terminator */ + ps->vbuff.endpos = nblok->h.endp - 1; + + /* did we allocate the current blok? if so free it up */ + if (ps->got_a_new_block) { + debug_fill(blok->h.first_avail, + blok->h.endp - blok->h.first_avail); #if defined(EAPI_MM) - if (blok->h.is_shm) - (void)ap_mm_lock(mm, AP_MM_LOCK_RW); + if (blok->h.is_shm) + (void)ap_mm_lock(mm, AP_MM_LOCK_RW); #endif - (void) ap_acquire_mutex(alloc_mutex); - blok->h.next = block_freelist; - block_freelist = blok; - (void) ap_release_mutex(alloc_mutex); + (void)ap_acquire_mutex(alloc_mutex); + blok->h.next = block_freelist; + block_freelist = blok; + (void)ap_release_mutex(alloc_mutex); #if defined(EAPI_MM) - if (blok->h.is_shm) - (void)ap_mm_unlock(mm); + if (blok->h.is_shm) + (void)ap_mm_unlock(mm); #endif - } - ps->blok = nblok; - ps->got_a_new_block = 1; - /* note that we've deliberately not linked the new block onto - * the pool yet... because we may need to flush again later, and - * we'd have to spend more effort trying to unlink the block. - */ - return 0; + } + ps->blok = nblok; + ps->got_a_new_block = 1; + /* + * note that we've deliberately not linked the new block onto + * the pool yet... because we may need to flush again later, and + * we'd have to spend more effort trying to unlink the block. + */ + return 0; #endif } -API_EXPORT(char *) ap_pvsprintf(pool *p, const char *fmt, va_list ap) +API_EXPORT(char *) +ap_pvsprintf(pool *p, const char *fmt, va_list ap) { #ifdef ALLOC_USE_MALLOC - struct psprintf_data ps; - void *ptr; + struct psprintf_data ps; + void *ptr; - ap_block_alarms(); + ap_block_alarms(); #if defined(EAPI_MM) - if (p->is_shm) - ps.base = ap_mm_malloc(mm, 512); - else -#endif - ps.base = malloc(512); - if (ps.base == NULL) { - fputs("Ouch! Out of memory!\n", stderr); - exit(1); - } - /* need room at beginning for allocation_list */ - ps.vbuff.curpos = ps.base + CLICK_SZ; - ps.vbuff.endpos = ps.base + 511; - ap_vformatter(psprintf_flush, &ps.vbuff, fmt, ap); - *ps.vbuff.curpos++ = '\0'; - ptr = ps.base; - /* shrink */ + if (p->is_shm) + ps.base = ap_mm_malloc(mm, 512); + else +#endif + ps.base = malloc(512); + if (ps.base == NULL) { + fputs("Ouch! Out of memory!\n", stderr); + exit(1); + } + /* need room at beginning for allocation_list */ + ps.vbuff.curpos = ps.base + CLICK_SZ; + ps.vbuff.endpos = ps.base + 511; + ap_vformatter(psprintf_flush, &ps.vbuff, fmt, ap); + *ps.vbuff.curpos++ = '\0'; + ptr = ps.base; + /* shrink */ #if defined(EAPI_MM) - if (p->is_shm) - ptr = ap_mm_realloc(ptr, (char *)ps.vbuff.curpos - (char *)ptr); - else -#endif - ptr = realloc(ptr, (char *)ps.vbuff.curpos - (char *)ptr); - if (ptr == NULL) { - fputs("Ouch! Out of memory!\n", stderr); - exit(1); - } - *(void **)ptr = p->allocation_list; - p->allocation_list = ptr; - ap_unblock_alarms(); - return (char *)ptr + CLICK_SZ; + if (p->is_shm) + ptr = ap_mm_realloc(ptr, (char *)ps.vbuff.curpos - (char *)ptr); + else +#endif + ptr = realloc(ptr, (char *)ps.vbuff.curpos - (char *)ptr); + if (ptr == NULL) { + fputs("Ouch! Out of memory!\n", stderr); + exit(1); + } + *(void **)ptr = p->allocation_list; + p->allocation_list = ptr; + ap_unblock_alarms(); + return (char *)ptr + CLICK_SZ; #else - struct psprintf_data ps; - char *strp; - int size; - - ap_block_alarms(); - ps.blok = p->last; - ps.vbuff.curpos = ps.blok->h.first_avail; - ps.vbuff.endpos = ps.blok->h.endp - 1; /* save one for NUL */ - ps.got_a_new_block = 0; - - if (ps.blok->h.first_avail == ps.blok->h.endp) - psprintf_flush(&ps.vbuff); /* ensure room for NUL */ - ap_vformatter(psprintf_flush, &ps.vbuff, fmt, ap); - - strp = ps.vbuff.curpos; - *strp++ = '\0'; - - size = strp - ps.blok->h.first_avail; - size = (1 + ((size - 1) / CLICK_SZ)) * CLICK_SZ; - strp = ps.blok->h.first_avail; /* save away result pointer */ - ps.blok->h.first_avail += size; - - /* have to link the block in if it's a new one */ - if (ps.got_a_new_block) { - p->last->h.next = ps.blok; - p->last = ps.blok; + struct psprintf_data ps; + char *strp; + int size; + + ap_block_alarms(); + ps.blok = p->last; + ps.vbuff.curpos = ps.blok->h.first_avail; + ps.vbuff.endpos = ps.blok->h.endp - 1; /* save one for NUL */ + ps.got_a_new_block = 0; + + if (ps.blok->h.first_avail == ps.blok->h.endp) + psprintf_flush(&ps.vbuff); /* ensure room for NUL */ + ap_vformatter(psprintf_flush, &ps.vbuff, fmt, ap); + + strp = ps.vbuff.curpos; + *strp++ = '\0'; + + size = strp - ps.blok->h.first_avail; + size = (1 + ((size - 1) / CLICK_SZ)) * CLICK_SZ; + strp = ps.blok->h.first_avail; /* save away result pointer */ + ps.blok->h.first_avail += size; + + /* have to link the block in if it's a new one */ + if (ps.got_a_new_block) { + p->last->h.next = ps.blok; + p->last = ps.blok; #ifdef POOL_DEBUG - ps.blok->h.owning_pool = p; + ps.blok->h.owning_pool = p; #endif - } - ap_unblock_alarms(); + } + ap_unblock_alarms(); - return strp; + return strp; #endif } -API_EXPORT_NONSTD(char *) ap_psprintf(pool *p, const char *fmt, ...) +API_EXPORT_NONSTD(char *) +ap_psprintf(pool *p, const char *fmt, ...) { - va_list ap; - char *res; + va_list ap; + char *res; - va_start(ap, fmt); - res = ap_pvsprintf(p, fmt, ap); - va_end(ap); - return res; + va_start(ap, fmt); + res = ap_pvsprintf(p, fmt, ap); + va_end(ap); + return res; } /***************************************************************** @@ -1176,75 +1212,81 @@ API_EXPORT_NONSTD(char *) ap_psprintf(pool *p, const char *fmt, ...) * The 'array' functions... */ -static void make_array_core(array_header *res, pool *p, int nelts, int elt_size) +static void +make_array_core(array_header *res, pool *p, int nelts, int elt_size) { - if (nelts < 1) - nelts = 1; /* Assure sanity if someone asks for + if (nelts < 1) + nelts = 1; /* Assure sanity if someone asks for * array of zero elts. */ - res->elts = ap_pcalloc(p, nelts * elt_size); + res->elts = ap_pcalloc(p, nelts * elt_size); - res->pool = p; - res->elt_size = elt_size; - res->nelts = 0; /* No active elements yet... */ - res->nalloc = nelts; /* ...but this many allocated */ + res->pool = p; + res->elt_size = elt_size; + res->nelts = 0; /* No active elements yet... */ + res->nalloc = nelts; /* ...but this many allocated */ } -API_EXPORT(array_header *) ap_make_array(pool *p, int nelts, int elt_size) +API_EXPORT(array_header *) +ap_make_array(pool *p, int nelts, int elt_size) { - array_header *res = (array_header *) ap_palloc(p, sizeof(array_header)); + array_header *res = (array_header *)ap_palloc(p, sizeof(array_header)); - make_array_core(res, p, nelts, elt_size); - return res; + make_array_core(res, p, nelts, elt_size); + return res; } -API_EXPORT(void *) ap_push_array(array_header *arr) +API_EXPORT(void *) +ap_push_array(array_header *arr) { - if (arr->nelts == arr->nalloc) { - int new_size = (arr->nalloc <= 0) ? 1 : arr->nalloc * 2; - char *new_data; + if (arr->nelts == arr->nalloc) { + int new_size = (arr->nalloc <= 0) ? 1 : arr->nalloc * 2; + char *new_data; - new_data = ap_pcalloc(arr->pool, arr->elt_size * new_size); + new_data = ap_pcalloc(arr->pool, arr->elt_size * new_size); - memcpy(new_data, arr->elts, arr->nalloc * arr->elt_size); - arr->elts = new_data; - arr->nalloc = new_size; - } + memcpy(new_data, arr->elts, arr->nalloc * arr->elt_size); + arr->elts = new_data; + arr->nalloc = new_size; + } - ++arr->nelts; - return arr->elts + (arr->elt_size * (arr->nelts - 1)); + ++arr->nelts; + return arr->elts + (arr->elt_size * (arr->nelts - 1)); } -API_EXPORT(void) ap_array_cat(array_header *dst, const array_header *src) +API_EXPORT(void) +ap_array_cat(array_header *dst, const array_header *src) { - int elt_size = dst->elt_size; + int elt_size = dst->elt_size; - if (dst->nelts + src->nelts > dst->nalloc) { - int new_size = (dst->nalloc <= 0) ? 1 : dst->nalloc * 2; - char *new_data; + if (dst->nelts + src->nelts > dst->nalloc) { + int new_size = (dst->nalloc <= 0) ? 1 : dst->nalloc * 2; + char *new_data; - while (dst->nelts + src->nelts > new_size) - new_size *= 2; + while (dst->nelts + src->nelts > new_size) + new_size *= 2; - new_data = ap_pcalloc(dst->pool, elt_size * new_size); - memcpy(new_data, dst->elts, dst->nalloc * elt_size); + new_data = ap_pcalloc(dst->pool, elt_size * new_size); + memcpy(new_data, dst->elts, dst->nalloc * elt_size); - dst->elts = new_data; - dst->nalloc = new_size; - } + dst->elts = new_data; + dst->nalloc = new_size; + } - memcpy(dst->elts + dst->nelts * elt_size, src->elts, elt_size * src->nelts); - dst->nelts += src->nelts; + memcpy(dst->elts + dst->nelts * elt_size, src->elts, + elt_size * src->nelts); + dst->nelts += src->nelts; } -API_EXPORT(array_header *) ap_copy_array(pool *p, const array_header *arr) +API_EXPORT(array_header *) +ap_copy_array(pool *p, const array_header *arr) { - array_header *res = ap_make_array(p, arr->nalloc, arr->elt_size); + array_header *res = ap_make_array(p, arr->nalloc, arr->elt_size); - memcpy(res->elts, arr->elts, arr->elt_size * arr->nelts); - res->nelts = arr->nelts; - return res; + memcpy(res->elts, arr->elts, arr->elt_size * arr->nelts); + res->nelts = arr->nelts; + return res; } /* This cute function copies the array header *only*, but arranges @@ -1254,34 +1296,34 @@ API_EXPORT(array_header *) ap_copy_array(pool *p, const array_header *arr) * overhead of the full copy only where it is really needed. */ -static ap_inline void copy_array_hdr_core(array_header *res, - const array_header *arr) +static ap_inline void +copy_array_hdr_core(array_header *res, const array_header *arr) { - res->elts = arr->elts; - res->elt_size = arr->elt_size; - res->nelts = arr->nelts; - res->nalloc = arr->nelts; /* Force overflow on push */ + res->elts = arr->elts; + res->elt_size = arr->elt_size; + res->nelts = arr->nelts; + res->nalloc = arr->nelts; /* Force overflow on push */ } -API_EXPORT(array_header *) ap_copy_array_hdr(pool *p, const array_header *arr) +API_EXPORT(array_header *) +ap_copy_array_hdr(pool *p, const array_header *arr) { - array_header *res = (array_header *) ap_palloc(p, sizeof(array_header)); + array_header *res = (array_header *) ap_palloc(p, sizeof(array_header)); - res->pool = p; - copy_array_hdr_core(res, arr); - return res; + res->pool = p; + copy_array_hdr_core(res, arr); + return res; } /* The above is used here to avoid consing multiple new array bodies... */ -API_EXPORT(array_header *) ap_append_arrays(pool *p, - const array_header *first, - const array_header *second) +API_EXPORT(array_header *) +ap_append_arrays(pool *p, const array_header *first, const array_header *second) { - array_header *res = ap_copy_array_hdr(p, first); + array_header *res = ap_copy_array_hdr(p, first); - ap_array_cat(res, second); - return res; + ap_array_cat(res, second); + return res; } /* ap_array_pstrcat generates a new string from the pool containing @@ -1290,52 +1332,48 @@ API_EXPORT(array_header *) ap_append_arrays(pool *p, * or if there are no elements in the array. * If sep is non-NUL, it will be inserted between elements as a separator. */ -API_EXPORT(char *) ap_array_pstrcat(pool *p, const array_header *arr, - const char sep) +API_EXPORT(char *) +ap_array_pstrcat(pool *p, const array_header *arr, const char sep) { - char *cp, *res, **strpp; - int i, len; - - if (arr->nelts <= 0 || arr->elts == NULL) /* Empty table? */ - return (char *) ap_pcalloc(p, 1); - - /* Pass one --- find length of required string */ - - len = 0; - for (i = 0, strpp = (char **) arr->elts; ; ++strpp) { - if (strpp && *strpp != NULL) { - len += strlen(*strpp); - } - if (++i >= arr->nelts) - break; - if (sep) - ++len; - } + char *cp, *res, **strpp; + int i, len; - /* Allocate the required string */ + if (arr->nelts <= 0 || arr->elts == NULL) /* Empty table? */ + return (char *)ap_pcalloc(p, 1); - res = (char *) ap_palloc(p, len + 1); - cp = res; + /* Pass one --- find length of required string */ + len = 0; + for (i = 0, strpp = (char **)arr->elts; ; ++strpp) { + if (strpp && *strpp != NULL) + len += strlen(*strpp); - /* Pass two --- copy the argument strings into the result space */ + if (++i >= arr->nelts) + break; + if (sep) + ++len; + } - for (i = 0, strpp = (char **) arr->elts; ; ++strpp) { - if (strpp && *strpp != NULL) { - len = strlen(*strpp); - memcpy(cp, *strpp, len); - cp += len; - } - if (++i >= arr->nelts) - break; - if (sep) - *cp++ = sep; - } + /* Allocate the required string */ + res = (char *)ap_palloc(p, len + 1); + cp = res; - *cp = '\0'; + /* Pass two --- copy the argument strings into the result space */ + for (i = 0, strpp = (char **)arr->elts; ; ++strpp) { + if (strpp && *strpp != NULL) { + len = strlen(*strpp); + memcpy(cp, *strpp, len); + cp += len; + } + if (++i >= arr->nelts) + break; + if (sep) + *cp++ = sep; + } - /* Return the result string */ + *cp = '\0'; - return res; + /* Return the result string */ + return res; } @@ -1347,289 +1385,295 @@ API_EXPORT(char *) ap_array_pstrcat(pool *p, const array_header *arr, /* XXX: if you tweak this you should look at is_empty_table() and table_elts() * in ap_alloc.h */ struct table { - /* This has to be first to promote backwards compatibility with - * older modules which cast a table * to an array_header *... - * they should use the table_elts() function for most of the - * cases they do this for. - */ - array_header a; + /* This has to be first to promote backwards compatibility with + * older modules which cast a table * to an array_header *... + * they should use the table_elts() function for most of the + * cases they do this for. + */ + array_header a; #ifdef MAKE_TABLE_PROFILE - void *creator; + void *creator; #endif }; #ifdef MAKE_TABLE_PROFILE -static table_entry *table_push(table *t) +static table_entry +*table_push(table *t) { - if (t->a.nelts == t->a.nalloc) { - fprintf(stderr, - "table_push: table created by %p hit limit of %u\n", - t->creator, t->a.nalloc); - } - return (table_entry *) ap_push_array(&t->a); + if (t->a.nelts == t->a.nalloc) { + fprintf(stderr, + "table_push: table created by %p hit limit of %u\n", + t->creator, t->a.nalloc); + } + return (table_entry *)ap_push_array(&t->a); } #else -#define table_push(t) ((table_entry *) ap_push_array(&(t)->a)) +#define table_push(t) ((table_entry *)ap_push_array(&(t)->a)) #endif - -API_EXPORT(table *) ap_make_table(pool *p, int nelts) +API_EXPORT(table *) +ap_make_table(pool *p, int nelts) { - table *t = ap_palloc(p, sizeof(table)); + table *t = ap_palloc(p, sizeof(table)); - make_array_core(&t->a, p, nelts, sizeof(table_entry)); + make_array_core(&t->a, p, nelts, sizeof(table_entry)); #ifdef MAKE_TABLE_PROFILE - t->creator = __builtin_return_address(0); + t->creator = __builtin_return_address(0); #endif - return t; + return t; } -API_EXPORT(table *) ap_copy_table(pool *p, const table *t) +API_EXPORT(table *) +ap_copy_table(pool *p, const table *t) { - table *new = ap_palloc(p, sizeof(table)); + table *new = ap_palloc(p, sizeof(table)); #ifdef POOL_DEBUG - /* we don't copy keys and values, so it's necessary that t->a.pool - * have a life span at least as long as p - */ - if (!ap_pool_is_ancestor(t->a.pool, p)) { - fprintf(stderr, "copy_table: t's pool is not an ancestor of p\n"); - abort(); - } + /* we don't copy keys and values, so it's necessary that t->a.pool + * have a life span at least as long as p + */ + if (!ap_pool_is_ancestor(t->a.pool, p)) { + fprintf(stderr, "copy_table: t's pool is not an " + "ancestor of p\n"); + abort(); + } #endif - make_array_core(&new->a, p, t->a.nalloc, sizeof(table_entry)); - memcpy(new->a.elts, t->a.elts, t->a.nelts * sizeof(table_entry)); - new->a.nelts = t->a.nelts; - return new; + make_array_core(&new->a, p, t->a.nalloc, sizeof(table_entry)); + memcpy(new->a.elts, t->a.elts, t->a.nelts * sizeof(table_entry)); + new->a.nelts = t->a.nelts; + return new; } -API_EXPORT(void) ap_clear_table(table *t) +API_EXPORT(void) +ap_clear_table(table *t) { - t->a.nelts = 0; + t->a.nelts = 0; } -API_EXPORT(const char *) ap_table_get(const table *t, const char *key) +API_EXPORT(const char *) +ap_table_get(const table *t, const char *key) { - table_entry *elts = (table_entry *) t->a.elts; - int i; + table_entry *elts = (table_entry *) t->a.elts; + int i; - if (key == NULL) - return NULL; + if (key == NULL) + return NULL; - for (i = 0; i < t->a.nelts; ++i) - if (!strcasecmp(elts[i].key, key)) - return elts[i].val; + for (i = 0; i < t->a.nelts; ++i) + if (!strcasecmp(elts[i].key, key)) + return elts[i].val; - return NULL; + return NULL; } -API_EXPORT(void) ap_table_set(table *t, const char *key, const char *val) -{ - register int i, j, k; - table_entry *elts = (table_entry *) t->a.elts; - int done = 0; - - for (i = 0; i < t->a.nelts; ) { - if (!strcasecmp(elts[i].key, key)) { - if (!done) { - elts[i].val = ap_pstrdup(t->a.pool, val); - done = 1; - ++i; - } - else { /* delete an extraneous element */ - for (j = i, k = i + 1; k < t->a.nelts; ++j, ++k) { - elts[j].key = elts[k].key; - elts[j].val = elts[k].val; - } - --t->a.nelts; - } +API_EXPORT(void) +ap_table_set(table *t, const char *key, const char *val) +{ + register int i, j, k; + table_entry *elts = (table_entry *) t->a.elts; + int done = 0; + + for (i = 0; i < t->a.nelts; ) { + if (!strcasecmp(elts[i].key, key)) { + if (!done) { + elts[i].val = ap_pstrdup(t->a.pool, val); + done = 1; + ++i; + } else { /* delete an extraneous element */ + for (j = i, k = i + 1; k < t->a.nelts; + ++j, ++k) { + elts[j].key = elts[k].key; + elts[j].val = elts[k].val; + } + --t->a.nelts; + } + } else + ++i; } - else { - ++i; - } - } - if (!done) { - elts = (table_entry *) table_push(t); - elts->key = ap_pstrdup(t->a.pool, key); - elts->val = ap_pstrdup(t->a.pool, val); - } + if (!done) { + elts = (table_entry *)table_push(t); + elts->key = ap_pstrdup(t->a.pool, key); + elts->val = ap_pstrdup(t->a.pool, val); + } } -API_EXPORT(void) ap_table_setn(table *t, const char *key, const char *val) +API_EXPORT(void) +ap_table_setn(table *t, const char *key, const char *val) { - register int i, j, k; - table_entry *elts = (table_entry *) t->a.elts; - int done = 0; + register int i, j, k; + table_entry *elts = (table_entry *) t->a.elts; + int done = 0; #ifdef POOL_DEBUG - { if (!ap_pool_is_ancestor(ap_find_pool(key), t->a.pool)) { - fprintf(stderr, "table_set: key not in ancestor pool of t\n"); - abort(); + fprintf(stderr, "table_set: key not in ancestor pool of t\n"); + abort(); } if (!ap_pool_is_ancestor(ap_find_pool(val), t->a.pool)) { - fprintf(stderr, "table_set: val not in ancestor pool of t\n"); - abort(); - } - } -#endif - - for (i = 0; i < t->a.nelts; ) { - if (!strcasecmp(elts[i].key, key)) { - if (!done) { - elts[i].val = (char *)val; - done = 1; - ++i; - } - else { /* delete an extraneous element */ - for (j = i, k = i + 1; k < t->a.nelts; ++j, ++k) { - elts[j].key = elts[k].key; - elts[j].val = elts[k].val; - } - --t->a.nelts; - } + fprintf(stderr, "table_set: val not in ancestor pool of t\n"); + abort(); } - else { - ++i; +#endif + + for (i = 0; i < t->a.nelts; ) { + if (!strcasecmp(elts[i].key, key)) { + if (!done) { + elts[i].val = (char *)val; + done = 1; + ++i; + } else { /* delete an extraneous element */ + for (j = i, k = i + 1; k < t->a.nelts; + ++j, ++k) { + elts[j].key = elts[k].key; + elts[j].val = elts[k].val; + } + --t->a.nelts; + } + } else + ++i; } - } - if (!done) { - elts = (table_entry *) table_push(t); - elts->key = (char *)key; - elts->val = (char *)val; - } + if (!done) { + elts = (table_entry *)table_push(t); + elts->key = (char *)key; + elts->val = (char *)val; + } } -API_EXPORT(void) ap_table_unset(table *t, const char *key) -{ - register int i, j, k; - table_entry *elts = (table_entry *) t->a.elts; - - for (i = 0; i < t->a.nelts;) { - if (!strcasecmp(elts[i].key, key)) { - - /* found an element to skip over - * there are any number of ways to remove an element from - * a contiguous block of memory. I've chosen one that - * doesn't do a memcpy/bcopy/array_delete, *shrug*... - */ - for (j = i, k = i + 1; k < t->a.nelts; ++j, ++k) { - elts[j].key = elts[k].key; - elts[j].val = elts[k].val; - } - --t->a.nelts; +API_EXPORT(void) +ap_table_unset(table *t, const char *key) +{ + register int i, j, k; + table_entry *elts = (table_entry *) t->a.elts; + + for (i = 0; i < t->a.nelts;) { + if (!strcasecmp(elts[i].key, key)) { + + /* found an element to skip over there are any + * number of ways to remove an element from a + * contiguous block of memory. I've chosen one + * that doesn't do a memcpy/bcopy/array_delete, + * *shrug*... + */ + for (j = i, k = i + 1; k < t->a.nelts; ++j, ++k) { + elts[j].key = elts[k].key; + elts[j].val = elts[k].val; + } + --t->a.nelts; + } else + ++i; } - else { - ++i; - } - } } -API_EXPORT(void) ap_table_merge(table *t, const char *key, const char *val) +API_EXPORT(void) +ap_table_merge(table *t, const char *key, const char *val) { - table_entry *elts = (table_entry *) t->a.elts; - int i; + table_entry *elts = (table_entry *) t->a.elts; + int i; - for (i = 0; i < t->a.nelts; ++i) - if (!strcasecmp(elts[i].key, key)) { - elts[i].val = ap_pstrcat(t->a.pool, elts[i].val, ", ", val, NULL); - return; - } + for (i = 0; i < t->a.nelts; ++i) + if (!strcasecmp(elts[i].key, key)) { + elts[i].val = ap_pstrcat(t->a.pool, elts[i].val, + ", ", val, NULL); + return; + } - elts = (table_entry *) table_push(t); - elts->key = ap_pstrdup(t->a.pool, key); - elts->val = ap_pstrdup(t->a.pool, val); + elts = (table_entry *)table_push(t); + elts->key = ap_pstrdup(t->a.pool, key); + elts->val = ap_pstrdup(t->a.pool, val); } -API_EXPORT(void) ap_table_mergen(table *t, const char *key, const char *val) +API_EXPORT(void) +ap_table_mergen(table *t, const char *key, const char *val) { - table_entry *elts = (table_entry *) t->a.elts; - int i; + table_entry *elts = (table_entry *)t->a.elts; + int i; #ifdef POOL_DEBUG - { if (!ap_pool_is_ancestor(ap_find_pool(key), t->a.pool)) { - fprintf(stderr, "table_set: key not in ancestor pool of t\n"); - abort(); + fprintf(stderr, "table_set: key not in ancestor pool of t\n"); + abort(); } if (!ap_pool_is_ancestor(ap_find_pool(val), t->a.pool)) { - fprintf(stderr, "table_set: key not in ancestor pool of t\n"); - abort(); + fprintf(stderr, "table_set: key not in ancestor pool of t\n"); + abort(); } - } #endif - for (i = 0; i < t->a.nelts; ++i) { - if (!strcasecmp(elts[i].key, key)) { - elts[i].val = ap_pstrcat(t->a.pool, elts[i].val, ", ", val, NULL); - return; + for (i = 0; i < t->a.nelts; ++i) { + if (!strcasecmp(elts[i].key, key)) { + elts[i].val = ap_pstrcat(t->a.pool, elts[i].val, + ", ", val, NULL); + return; + } } - } - elts = (table_entry *) table_push(t); - elts->key = (char *)key; - elts->val = (char *)val; + elts = (table_entry *)table_push(t); + elts->key = (char *)key; + elts->val = (char *)val; } -API_EXPORT(void) ap_table_add(table *t, const char *key, const char *val) +API_EXPORT(void) +ap_table_add(table *t, const char *key, const char *val) { - table_entry *elts = (table_entry *) t->a.elts; + table_entry *elts = (table_entry *)t->a.elts; - elts = (table_entry *) table_push(t); - elts->key = ap_pstrdup(t->a.pool, key); - elts->val = ap_pstrdup(t->a.pool, val); + elts = (table_entry *)table_push(t); + elts->key = ap_pstrdup(t->a.pool, key); + elts->val = ap_pstrdup(t->a.pool, val); } -API_EXPORT(void) ap_table_addn(table *t, const char *key, const char *val) +API_EXPORT(void) +ap_table_addn(table *t, const char *key, const char *val) { - table_entry *elts = (table_entry *) t->a.elts; + table_entry *elts = (table_entry *) t->a.elts; #ifdef POOL_DEBUG - { if (!ap_pool_is_ancestor(ap_find_pool(key), t->a.pool)) { - fprintf(stderr, "table_set: key not in ancestor pool of t\n"); - abort(); + fprintf(stderr, "table_set: key not in ancestor pool of t\n"); + abort(); } if (!ap_pool_is_ancestor(ap_find_pool(val), t->a.pool)) { - fprintf(stderr, "table_set: key not in ancestor pool of t\n"); - abort(); + fprintf(stderr, "table_set: key not in ancestor pool of t\n"); + abort(); } - } #endif - elts = (table_entry *) table_push(t); - elts->key = (char *)key; - elts->val = (char *)val; + elts = (table_entry *)table_push(t); + elts->key = (char *)key; + elts->val = (char *)val; } -API_EXPORT(table *) ap_overlay_tables(pool *p, const table *overlay, const table *base) +API_EXPORT(table *) +ap_overlay_tables(pool *p, const table *overlay, const table *base) { - table *res; + table *res; #ifdef POOL_DEBUG - /* we don't copy keys and values, so it's necessary that - * overlay->a.pool and base->a.pool have a life span at least - * as long as p - */ - if (!ap_pool_is_ancestor(overlay->a.pool, p)) { - fprintf(stderr, "overlay_tables: overlay's pool is not an ancestor of p\n"); - abort(); - } - if (!ap_pool_is_ancestor(base->a.pool, p)) { - fprintf(stderr, "overlay_tables: base's pool is not an ancestor of p\n"); - abort(); - } + /* we don't copy keys and values, so it's necessary that + * overlay->a.pool and base->a.pool have a life span at least + * as long as p + */ + if (!ap_pool_is_ancestor(overlay->a.pool, p)) { + fprintf(stderr, "overlay_tables: overlay's pool is not an " + "ancestor of p\n"); + abort(); + } + if (!ap_pool_is_ancestor(base->a.pool, p)) { + fprintf(stderr, "overlay_tables: base's pool is not an " + "ancestor of p\n"); + abort(); + } #endif - res = ap_palloc(p, sizeof(table)); - /* behave like append_arrays */ - res->a.pool = p; - copy_array_hdr_core(&res->a, &overlay->a); - ap_array_cat(&res->a, &base->a); + res = ap_palloc(p, sizeof(table)); + /* behave like append_arrays */ + res->a.pool = p; + copy_array_hdr_core(&res->a, &overlay->a); + ap_array_cat(&res->a, &base->a); - return res; + return res; } /* And now for something completely abstract ... @@ -1654,27 +1698,28 @@ API_EXPORT(table *) ap_overlay_tables(pool *p, const table *overlay, const table * Note that rec is simply passed-on to the comp function, so that the * caller can pass additional info for the task. */ -API_EXPORT_NONSTD(void) ap_table_do(int (*comp) (void *, const char *, const char *), - void *rec, const table *t,...) +API_EXPORT_NONSTD(void) +ap_table_do(int (*comp)(void *, const char *, const char *), void *rec, + const table *t,...) { - va_list vp; - char *argp; - table_entry *elts = (table_entry *) t->a.elts; - int rv, i; + va_list vp; + char *argp; + table_entry *elts = (table_entry *)t->a.elts; + int rv, i; - va_start(vp, t); + va_start(vp, t); - argp = va_arg(vp, char *); + argp = va_arg(vp, char *); - do { - for (rv = 1, i = 0; rv && (i < t->a.nelts); ++i) { - if (elts[i].key && (!argp || !strcasecmp(elts[i].key, argp))) { - rv = (*comp) (rec, elts[i].key, elts[i].val); - } - } - } while (argp && ((argp = va_arg(vp, char *)) != NULL)); + do { + for (rv = 1, i = 0; rv && (i < t->a.nelts); ++i) { + if (elts[i].key && (!argp || + !strcasecmp(elts[i].key, argp))) + rv = (*comp) (rec, elts[i].key, elts[i].val); + } + } while (argp && ((argp = va_arg(vp, char *)) != NULL)); - va_end(vp); + va_end(vp); } /* Curse libc and the fact that it doesn't guarantee a stable sort. We @@ -1686,22 +1731,22 @@ API_EXPORT_NONSTD(void) ap_table_do(int (*comp) (void *, const char *, const cha * ordering in the output.) */ typedef struct { - char *key; - char *val; - int order; + char *key; + char *val; + int order; } overlap_key; -static int sort_overlap(const void *va, const void *vb) +static int +sort_overlap(const void *va, const void *vb) { - const overlap_key *a = va; - const overlap_key *b = vb; - int r; + const overlap_key *a = va; + const overlap_key *b = vb; + int r; - r = strcasecmp(a->key, b->key); - if (r) { - return r; - } - return a->order - b->order; + r = strcasecmp(a->key, b->key); + if (r) + return r; + return a->order - b->order; } /* prefer to use the stack for temp storage for overlaps smaller than this */ @@ -1709,129 +1754,131 @@ static int sort_overlap(const void *va, const void *vb) #define AP_OVERLAP_TABLES_ON_STACK (512) #endif -API_EXPORT(void) ap_overlap_tables(table *a, const table *b, unsigned flags) -{ - overlap_key cat_keys_buf[AP_OVERLAP_TABLES_ON_STACK]; - overlap_key *cat_keys; - int nkeys; - table_entry *e; - table_entry *last_e; - overlap_key *left; - overlap_key *right; - overlap_key *last; - - nkeys = a->a.nelts + b->a.nelts; - if (nkeys < AP_OVERLAP_TABLES_ON_STACK) { - cat_keys = cat_keys_buf; - } - else { - /* XXX: could use scratch free space in a or b's pool instead... - * which could save an allocation in b's pool. +API_EXPORT(void) +ap_overlap_tables(table *a, const table *b, unsigned flags) +{ + overlap_key cat_keys_buf[AP_OVERLAP_TABLES_ON_STACK]; + overlap_key *cat_keys; + int nkeys; + table_entry *e; + table_entry *last_e; + overlap_key *left; + overlap_key *right; + overlap_key *last; + + nkeys = a->a.nelts + b->a.nelts; + if (nkeys < AP_OVERLAP_TABLES_ON_STACK) { + cat_keys = cat_keys_buf; + } else { + /* XXX: could use scratch free space in a or b's pool instead... + * which could save an allocation in b's pool. + */ + cat_keys = ap_palloc(b->a.pool, sizeof(overlap_key) * nkeys); + } + + nkeys = 0; + + /* Create a list of the entries from a concatenated with the entries + * from b. + */ + e = (table_entry *)a->a.elts; + last_e = e + a->a.nelts; + while (e < last_e) { + cat_keys[nkeys].key = e->key; + cat_keys[nkeys].val = e->val; + cat_keys[nkeys].order = nkeys; + ++nkeys; + ++e; + } + + e = (table_entry *)b->a.elts; + last_e = e + b->a.nelts; + while (e < last_e) { + cat_keys[nkeys].key = e->key; + cat_keys[nkeys].val = e->val; + cat_keys[nkeys].order = nkeys; + ++nkeys; + ++e; + } + + qsort(cat_keys, nkeys, sizeof(overlap_key), sort_overlap); + + /* Now iterate over the sorted list and rebuild a. + * Start by making sure it has enough space. */ - cat_keys = ap_palloc(b->a.pool, sizeof(overlap_key) * nkeys); - } - - nkeys = 0; - - /* Create a list of the entries from a concatenated with the entries - * from b. - */ - e = (table_entry *)a->a.elts; - last_e = e + a->a.nelts; - while (e < last_e) { - cat_keys[nkeys].key = e->key; - cat_keys[nkeys].val = e->val; - cat_keys[nkeys].order = nkeys; - ++nkeys; - ++e; - } - - e = (table_entry *)b->a.elts; - last_e = e + b->a.nelts; - while (e < last_e) { - cat_keys[nkeys].key = e->key; - cat_keys[nkeys].val = e->val; - cat_keys[nkeys].order = nkeys; - ++nkeys; - ++e; - } - - qsort(cat_keys, nkeys, sizeof(overlap_key), sort_overlap); - - /* Now iterate over the sorted list and rebuild a. - * Start by making sure it has enough space. - */ - a->a.nelts = 0; - if (a->a.nalloc < nkeys) { - a->a.elts = ap_palloc(a->a.pool, a->a.elt_size * nkeys * 2); - a->a.nalloc = nkeys * 2; - } - - /* - * In both the merge and set cases we retain the invariant: - * - * left->key, (left+1)->key, (left+2)->key, ..., (right-1)->key - * are all equal keys. (i.e. strcasecmp returns 0) - * - * We essentially need to find the maximal - * right for each key, then we can do a quick merge or set as - * appropriate. - */ - - if (flags & AP_OVERLAP_TABLES_MERGE) { - left = cat_keys; - last = left + nkeys; - while (left < last) { - right = left + 1; - if (right == last - || strcasecmp(left->key, right->key)) { - ap_table_addn(a, left->key, left->val); - left = right; - } - else { - char *strp; - char *value; - size_t len; - - /* Have to merge some headers. Let's re-use the order field, - * since it's handy... we'll store the length of val there. - */ - left->order = strlen(left->val); - len = left->order; - do { - right->order = strlen(right->val); - len += 2 + right->order; - ++right; - } while (right < last - && !strcasecmp(left->key, right->key)); - /* right points one past the last header to merge */ - value = ap_palloc(a->a.pool, len + 1); - strp = value; - for (;;) { - memcpy(strp, left->val, left->order); - strp += left->order; - ++left; - if (left == right) break; - *strp++ = ','; - *strp++ = ' '; - } - *strp = 0; - ap_table_addn(a, (left-1)->key, value); - } + a->a.nelts = 0; + if (a->a.nalloc < nkeys) { + a->a.elts = ap_palloc(a->a.pool, a->a.elt_size * nkeys * 2); + a->a.nalloc = nkeys * 2; } - } - else { - left = cat_keys; - last = left + nkeys; - while (left < last) { - right = left + 1; - while (right < last && !strcasecmp(left->key, right->key)) { - ++right; - } - ap_table_addn(a, (right-1)->key, (right-1)->val); - left = right; + + /* + * In both the merge and set cases we retain the invariant: + * + * left->key, (left+1)->key, (left+2)->key, ..., (right-1)->key + * are all equal keys. (i.e. strcasecmp returns 0) + * + * We essentially need to find the maximal + * right for each key, then we can do a quick merge or set as + * appropriate. + */ + + if (flags & AP_OVERLAP_TABLES_MERGE) { + left = cat_keys; + last = left + nkeys; + while (left < last) { + right = left + 1; + if (right == last + || strcasecmp(left->key, right->key)) { + ap_table_addn(a, left->key, left->val); + left = right; + } else { + char *strp; + char *value; + size_t len; + + /* Have to merge some headers. Let's re-use + * the order field, since it's handy... we'll + * store the length of val there. + */ + left->order = strlen(left->val); + len = left->order; + do { + right->order = strlen(right->val); + len += 2 + right->order; + ++right; + } while (right < last + && !strcasecmp(left->key, right->key)); + /* right points one past the last header to + * merge + */ + value = ap_palloc(a->a.pool, len + 1); + strp = value; + for (;;) { + memcpy(strp, left->val, left->order); + strp += left->order; + ++left; + if (left == right) break; + *strp++ = ','; + *strp++ = ' '; + } + *strp = 0; + ap_table_addn(a, (left-1)->key, value); + } + } + } else { + left = cat_keys; + last = left + nkeys; + while (left < last) { + right = left + 1; + while (right < last + && !strcasecmp(left->key, right->key)) { + ++right; + } + ap_table_addn(a, (right-1)->key, (right-1)->val); + left = right; + } } - } } /***************************************************************** @@ -1840,110 +1887,116 @@ API_EXPORT(void) ap_overlap_tables(table *a, const table *b, unsigned flags) */ struct cleanup { - void *data; - void (*plain_cleanup) (void *); - void (*child_cleanup) (void *); - struct cleanup *next; + void *data; + void (*plain_cleanup)(void *); + void (*child_cleanup)(void *); + struct cleanup *next; }; -API_EXPORT(void) ap_register_cleanup_ex(pool *p, void *data, - void (*plain_cleanup) (void *), - void (*child_cleanup) (void *), - int (*magic_cleanup) (void *)) -{ - struct cleanup *c; - if (p) { - c = (struct cleanup *) ap_palloc(p, sizeof(struct cleanup)); - c->data = data; - c->plain_cleanup = plain_cleanup; - c->child_cleanup = child_cleanup; - c->next = p->cleanups; - p->cleanups = c; - } - /* attempt to do magic even if not passed a pool. Allows us - * to perform the magic, therefore, "whenever" we want/need */ - if (magic_cleanup) { - if (!magic_cleanup(data)) - ap_log_error(APLOG_MARK, APLOG_WARNING, NULL, - "exec() may not be safe"); - } +API_EXPORT(void) +ap_register_cleanup_ex(pool *p, void *data, void (*plain_cleanup)(void *), + void (*child_cleanup)(void *), int (*magic_cleanup)(void *)) +{ + struct cleanup *c; + if (p) { + c = (struct cleanup *)ap_palloc(p, sizeof(struct cleanup)); + c->data = data; + c->plain_cleanup = plain_cleanup; + c->child_cleanup = child_cleanup; + c->next = p->cleanups; + p->cleanups = c; + } + /* attempt to do magic even if not passed a pool. Allows us + * to perform the magic, therefore, "whenever" we want/need */ + if (magic_cleanup) { + if (!magic_cleanup(data)) + ap_log_error(APLOG_MARK, APLOG_WARNING, NULL, + "exec() may not be safe"); + } } -API_EXPORT(void) ap_register_cleanup(pool *p, void *data, - void (*plain_cleanup) (void *), - void (*child_cleanup) (void *)) +API_EXPORT(void) +ap_register_cleanup(pool *p, void *data, void (*plain_cleanup)(void *), + void (*child_cleanup)(void *)) { - ap_register_cleanup_ex(p, data, plain_cleanup, child_cleanup, NULL); + ap_register_cleanup_ex(p, data, plain_cleanup, child_cleanup, NULL); } -API_EXPORT(void) ap_kill_cleanup(pool *p, void *data, void (*cleanup) (void *)) +API_EXPORT(void) +ap_kill_cleanup(pool *p, void *data, void (*cleanup)(void *)) { - struct cleanup *c = p->cleanups; - struct cleanup **lastp = &p->cleanups; + struct cleanup *c = p->cleanups; + struct cleanup **lastp = &p->cleanups; - while (c) { - if (c->data == data && c->plain_cleanup == cleanup) { - *lastp = c->next; - break; - } + while (c) { + if (c->data == data && c->plain_cleanup == cleanup) { + *lastp = c->next; + break; + } - lastp = &c->next; - c = c->next; - } + lastp = &c->next; + c = c->next; + } } -API_EXPORT(void) ap_run_cleanup(pool *p, void *data, void (*cleanup) (void *)) +API_EXPORT(void) +ap_run_cleanup(pool *p, void *data, void (*cleanup)(void *)) { - ap_block_alarms(); /* Run cleanup only once! */ - (*cleanup) (data); - ap_kill_cleanup(p, data, cleanup); - ap_unblock_alarms(); + ap_block_alarms(); /* Run cleanup only once! */ + (*cleanup)(data); + ap_kill_cleanup(p, data, cleanup); + ap_unblock_alarms(); } -static void run_cleanups(struct cleanup *c) +static void +run_cleanups(struct cleanup *c) { - while (c) { - (*c->plain_cleanup) (c->data); - c = c->next; - } + while (c) { + (*c->plain_cleanup)(c->data); + c = c->next; + } } -static void run_child_cleanups(struct cleanup *c) +static void +run_child_cleanups(struct cleanup *c) { - while (c) { - (*c->child_cleanup) (c->data); - c = c->next; - } + while (c) { + (*c->child_cleanup)(c->data); + c = c->next; + } } -static void cleanup_pool_for_exec(pool *p) +static void +cleanup_pool_for_exec(pool *p) { - run_child_cleanups(p->cleanups); - p->cleanups = NULL; + run_child_cleanups(p->cleanups); + p->cleanups = NULL; - for (p = p->sub_pools; p; p = p->sub_next) - cleanup_pool_for_exec(p); + for (p = p->sub_pools; p; p = p->sub_next) + cleanup_pool_for_exec(p); } -API_EXPORT(void) ap_cleanup_for_exec(void) +API_EXPORT(void) +ap_cleanup_for_exec(void) { - /* - * Don't need to do anything on NT, NETWARE or OS/2, because I - * am actually going to spawn the new process - not - * exec it. All handles that are not inheritable, will - * be automajically closed. The only problem is with - * file handles that are open, but there isn't much - * I can do about that (except if the child decides - * to go out and close them - */ - ap_block_alarms(); - cleanup_pool_for_exec(permanent_pool); - ap_unblock_alarms(); + /* + * Don't need to do anything on NT, NETWARE or OS/2, because I + * am actually going to spawn the new process - not + * exec it. All handles that are not inheritable, will + * be automajically closed. The only problem is with + * file handles that are open, but there isn't much + * I can do about that (except if the child decides + * to go out and close them + */ + ap_block_alarms(); + cleanup_pool_for_exec(permanent_pool); + ap_unblock_alarms(); } -API_EXPORT_NONSTD(void) ap_null_cleanup(void *data) +API_EXPORT_NONSTD(void) +ap_null_cleanup(void *data) { - /* do nothing cleanup routine */ + /* do nothing cleanup routine */ } /***************************************************************** @@ -1952,79 +2005,87 @@ API_EXPORT_NONSTD(void) ap_null_cleanup(void *data) * generic cleanup interface. */ -int ap_close_fd_on_exec(int fd) +int +ap_close_fd_on_exec(int fd) { - /* Protect the fd so that it will not be inherited by child processes */ - if(fcntl(fd, F_SETFD, FD_CLOEXEC) < 0) { - ap_log_error(APLOG_MARK, APLOG_ERR, NULL, - "fcntl(%d, F_SETFD, FD_CLOEXEC) failed", fd); - return 0; - } + /* Protect the fd so that it will not be inherited by child processes */ + if(fcntl(fd, F_SETFD, FD_CLOEXEC) < 0) { + ap_log_error(APLOG_MARK, APLOG_ERR, NULL, + "fcntl(%d, F_SETFD, FD_CLOEXEC) failed", fd); + return 0; + } - return 1; + return 1; } -static void fd_cleanup(void *fdv) +static void +fd_cleanup(void *fdv) { - close((int) (long) fdv); + close((int)(long)fdv); } -static int fd_magic_cleanup(void *fdv) +static int +fd_magic_cleanup(void *fdv) { - return ap_close_fd_on_exec((int) (long) fdv); + return ap_close_fd_on_exec((int)(long)fdv); } -API_EXPORT(void) ap_note_cleanups_for_fd_ex(pool *p, int fd, int domagic) +API_EXPORT(void) +ap_note_cleanups_for_fd_ex(pool *p, int fd, int domagic) { - ap_register_cleanup_ex(p, (void *) (long) fd, fd_cleanup, fd_cleanup, - domagic ? fd_magic_cleanup : NULL); + ap_register_cleanup_ex(p, (void *)(long)fd, fd_cleanup, fd_cleanup, + domagic ? fd_magic_cleanup : NULL); } -API_EXPORT(void) ap_note_cleanups_for_fd(pool *p, int fd) +API_EXPORT(void) +ap_note_cleanups_for_fd(pool *p, int fd) { - ap_note_cleanups_for_fd_ex(p, fd, 0); + ap_note_cleanups_for_fd_ex(p, fd, 0); } -API_EXPORT(void) ap_kill_cleanups_for_fd(pool *p, int fd) +API_EXPORT(void) +ap_kill_cleanups_for_fd(pool *p, int fd) { - ap_kill_cleanup(p, (void *) (long) fd, fd_cleanup); + ap_kill_cleanup(p, (void *)(long)fd, fd_cleanup); } -API_EXPORT(int) ap_popenf_ex(pool *a, const char *name, int flg, int mode, - int domagic) +API_EXPORT(int) +ap_popenf_ex(pool *a, const char *name, int flg, int mode, int domagic) { - int fd; - int save_errno; + int fd; + int save_errno; - ap_block_alarms(); - fd = open(name, flg, mode); - save_errno = errno; - if (fd >= 0) { - fd = ap_slack(fd, AP_SLACK_HIGH); - ap_note_cleanups_for_fd_ex(a, fd, domagic); - } - ap_unblock_alarms(); - errno = save_errno; - return fd; + ap_block_alarms(); + fd = open(name, flg, mode); + save_errno = errno; + if (fd >= 0) { + fd = ap_slack(fd, AP_SLACK_HIGH); + ap_note_cleanups_for_fd_ex(a, fd, domagic); + } + ap_unblock_alarms(); + errno = save_errno; + return fd; } -API_EXPORT(int) ap_popenf(pool *a, const char *name, int flg, int mode) +API_EXPORT(int) +ap_popenf(pool *a, const char *name, int flg, int mode) { - return ap_popenf_ex(a, name, flg, mode, 0); + return ap_popenf_ex(a, name, flg, mode, 0); } -API_EXPORT(int) ap_pclosef(pool *a, int fd) +API_EXPORT(int) +ap_pclosef(pool *a, int fd) { - int res; - int save_errno; + int res; + int save_errno; - ap_block_alarms(); - res = close(fd); - save_errno = errno; - ap_kill_cleanup(a, (void *) (long) fd, fd_cleanup); - ap_unblock_alarms(); - errno = save_errno; - return res; + ap_block_alarms(); + res = close(fd); + save_errno = errno; + ap_kill_cleanup(a, (void *)(long)fd, fd_cleanup); + ap_unblock_alarms(); + errno = save_errno; + return res; } @@ -2033,124 +2094,134 @@ API_EXPORT(int) ap_pclosef(pool *a, int fd) * we just close the descriptor. */ -static void file_cleanup(void *fpv) +static void +file_cleanup(void *fpv) { - fclose((FILE *) fpv); + fclose((FILE *)fpv); } -static void file_child_cleanup(void *fpv) +static void +file_child_cleanup(void *fpv) { - close(fileno((FILE *) fpv)); + close(fileno((FILE *)fpv)); } -static int file_magic_cleanup(void *fpv) +static int +file_magic_cleanup(void *fpv) { - return ap_close_fd_on_exec(fileno((FILE *) fpv)); + return ap_close_fd_on_exec(fileno((FILE *)fpv)); } -API_EXPORT(void) ap_note_cleanups_for_file_ex(pool *p, FILE *fp, int domagic) +API_EXPORT(void) +ap_note_cleanups_for_file_ex(pool *p, FILE *fp, int domagic) { - ap_register_cleanup_ex(p, (void *) fp, file_cleanup, file_child_cleanup, - domagic ? file_magic_cleanup : NULL); + ap_register_cleanup_ex(p, (void *)fp, file_cleanup, file_child_cleanup, + domagic ? file_magic_cleanup : NULL); } -API_EXPORT(void) ap_note_cleanups_for_file(pool *p, FILE *fp) +API_EXPORT(void) +ap_note_cleanups_for_file(pool *p, FILE *fp) { - ap_note_cleanups_for_file_ex(p, fp, 0); + ap_note_cleanups_for_file_ex(p, fp, 0); } -API_EXPORT(FILE *) ap_pfopen(pool *a, const char *name, const char *mode) +API_EXPORT(FILE *) +ap_pfopen(pool *a, const char *name, const char *mode) { - FILE *fd = NULL; - int baseFlag, desc; - int modeFlags = 0; - int saved_errno; + FILE *fd = NULL; + int baseFlag, desc; + int modeFlags = 0; + int saved_errno; - modeFlags = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH; + modeFlags = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH; - ap_block_alarms(); + ap_block_alarms(); - if (*mode == 'a') { - /* Work around faulty implementations of fopen */ - baseFlag = (*(mode + 1) == '+') ? O_RDWR : O_WRONLY; - desc = open(name, baseFlag | O_APPEND | O_CREAT, - modeFlags); - if (desc >= 0) { - desc = ap_slack(desc, AP_SLACK_LOW); - fd = ap_fdopen(desc, mode); + if (*mode == 'a') { + /* Work around faulty implementations of fopen */ + baseFlag = (*(mode + 1) == '+') ? O_RDWR : O_WRONLY; + desc = open(name, baseFlag | O_APPEND | O_CREAT, + modeFlags); + if (desc >= 0) { + desc = ap_slack(desc, AP_SLACK_LOW); + fd = ap_fdopen(desc, mode); + } + } else { + fd = fopen(name, mode); } - } - else { - fd = fopen(name, mode); - } - saved_errno = errno; - if (fd != NULL) - ap_note_cleanups_for_file(a, fd); - ap_unblock_alarms(); - errno = saved_errno; - return fd; + saved_errno = errno; + if (fd != NULL) + ap_note_cleanups_for_file(a, fd); + ap_unblock_alarms(); + errno = saved_errno; + return fd; } -API_EXPORT(FILE *) ap_pfdopen(pool *a, int fd, const char *mode) +API_EXPORT(FILE *) +ap_pfdopen(pool *a, int fd, const char *mode) { - FILE *f; - int saved_errno; + FILE *f; + int saved_errno; - ap_block_alarms(); - f = ap_fdopen(fd, mode); - saved_errno = errno; - if (f != NULL) - ap_note_cleanups_for_file(a, f); - ap_unblock_alarms(); - errno = saved_errno; - return f; + ap_block_alarms(); + f = ap_fdopen(fd, mode); + saved_errno = errno; + if (f != NULL) + ap_note_cleanups_for_file(a, f); + ap_unblock_alarms(); + errno = saved_errno; + return f; } -API_EXPORT(int) ap_pfclose(pool *a, FILE *fd) +API_EXPORT(int) +ap_pfclose(pool *a, FILE *fd) { - int res; + int res; - ap_block_alarms(); - res = fclose(fd); - ap_kill_cleanup(a, (void *) fd, file_cleanup); - ap_unblock_alarms(); - return res; + ap_block_alarms(); + res = fclose(fd); + ap_kill_cleanup(a, (void *)fd, file_cleanup); + ap_unblock_alarms(); + return res; } /* * DIR * with cleanup */ -static void dir_cleanup(void *dv) +static void +dir_cleanup(void *dv) { - closedir((DIR *) dv); + closedir((DIR *) dv); } -API_EXPORT(DIR *) ap_popendir(pool *p, const char *name) +API_EXPORT(DIR *) +ap_popendir(pool *p, const char *name) { - DIR *d; - int save_errno; + DIR *d; + int save_errno; - ap_block_alarms(); - d = opendir(name); - if (d == NULL) { - save_errno = errno; + ap_block_alarms(); + d = opendir(name); + if (d == NULL) { + save_errno = errno; + ap_unblock_alarms(); + errno = save_errno; + return NULL; + } + ap_register_cleanup(p, (void *)d, dir_cleanup, dir_cleanup); ap_unblock_alarms(); - errno = save_errno; - return NULL; - } - ap_register_cleanup(p, (void *) d, dir_cleanup, dir_cleanup); - ap_unblock_alarms(); - return d; + return d; } -API_EXPORT(void) ap_pclosedir(pool *p, DIR * d) +API_EXPORT(void) +ap_pclosedir(pool *p, DIR * d) { - ap_block_alarms(); - ap_kill_cleanup(p, (void *) d, dir_cleanup); - closedir(d); - ap_unblock_alarms(); + ap_block_alarms(); + ap_kill_cleanup(p, (void *)d, dir_cleanup); + closedir(d); + ap_unblock_alarms(); } /***************************************************************** @@ -2159,68 +2230,74 @@ API_EXPORT(void) ap_pclosedir(pool *p, DIR * d) * generic cleanup interface. */ -static void socket_cleanup(void *fdv) +static void +socket_cleanup(void *fdv) { - closesocket((int) (long) fdv); + closesocket((int)(long)fdv); } -static int socket_magic_cleanup(void *fpv) +static int +socket_magic_cleanup(void *fpv) { - return ap_close_fd_on_exec((int) (long) fpv); + return ap_close_fd_on_exec((int)(long)fpv); } -API_EXPORT(void) ap_note_cleanups_for_socket_ex(pool *p, int fd, int domagic) +API_EXPORT(void) +ap_note_cleanups_for_socket_ex(pool *p, int fd, int domagic) { - ap_register_cleanup_ex(p, (void *) (long) fd, socket_cleanup, - socket_cleanup, - domagic ? socket_magic_cleanup : NULL); + ap_register_cleanup_ex(p, (void *)(long) fd, socket_cleanup, + socket_cleanup, domagic ? socket_magic_cleanup : NULL); } -API_EXPORT(void) ap_note_cleanups_for_socket(pool *p, int fd) +API_EXPORT(void) +ap_note_cleanups_for_socket(pool *p, int fd) { - ap_note_cleanups_for_socket_ex(p, fd, 0); + ap_note_cleanups_for_socket_ex(p, fd, 0); } -API_EXPORT(void) ap_kill_cleanups_for_socket(pool *p, int sock) +API_EXPORT(void) +ap_kill_cleanups_for_socket(pool *p, int sock) { - ap_kill_cleanup(p, (void *) (long) sock, socket_cleanup); + ap_kill_cleanup(p, (void *)(long)sock, socket_cleanup); } -API_EXPORT(int) ap_psocket_ex(pool *p, int domain, int type, int protocol, - int domagic) +API_EXPORT(int) +ap_psocket_ex(pool *p, int domain, int type, int protocol, int domagic) { - int fd; + int fd; - ap_block_alarms(); - fd = socket(domain, type, protocol); - if (fd == -1) { - int save_errno = errno; + ap_block_alarms(); + fd = socket(domain, type, protocol); + if (fd == -1) { + int save_errno = errno; + ap_unblock_alarms(); + errno = save_errno; + return -1; + } + ap_note_cleanups_for_socket_ex(p, fd, domagic); ap_unblock_alarms(); - errno = save_errno; - return -1; - } - ap_note_cleanups_for_socket_ex(p, fd, domagic); - ap_unblock_alarms(); - return fd; + return fd; } -API_EXPORT(int) ap_psocket(pool *p, int domain, int type, int protocol) +API_EXPORT(int) +ap_psocket(pool *p, int domain, int type, int protocol) { - return ap_psocket_ex(p, domain, type, protocol, 0); + return ap_psocket_ex(p, domain, type, protocol, 0); } -API_EXPORT(int) ap_pclosesocket(pool *a, int sock) +API_EXPORT(int) +ap_pclosesocket(pool *a, int sock) { - int res; - int save_errno; + int res; + int save_errno; - ap_block_alarms(); - res = closesocket(sock); - save_errno = errno; - ap_kill_cleanup(a, (void *) (long) sock, socket_cleanup); - ap_unblock_alarms(); - errno = save_errno; - return res; + ap_block_alarms(); + res = closesocket(sock); + save_errno = errno; + ap_kill_cleanup(a, (void *)(long)sock, socket_cleanup); + ap_unblock_alarms(); + errno = save_errno; + return res; } @@ -2232,30 +2309,33 @@ API_EXPORT(int) ap_pclosesocket(pool *a, int sock) * regfree() doesn't clear it. So we don't allow it. */ -static void regex_cleanup(void *preg) +static void +regex_cleanup(void *preg) { - regfree((regex_t *) preg); + regfree((regex_t *)preg); } -API_EXPORT(regex_t *) ap_pregcomp(pool *p, const char *pattern, int cflags) +API_EXPORT(regex_t *) +ap_pregcomp(pool *p, const char *pattern, int cflags) { - regex_t *preg = ap_palloc(p, sizeof(regex_t)); + regex_t *preg = ap_palloc(p, sizeof(regex_t)); - if (regcomp(preg, pattern, cflags)) - return NULL; + if (regcomp(preg, pattern, cflags)) + return NULL; - ap_register_cleanup(p, (void *) preg, regex_cleanup, regex_cleanup); + ap_register_cleanup(p, (void *)preg, regex_cleanup, regex_cleanup); - return preg; + return preg; } -API_EXPORT(void) ap_pregfree(pool *p, regex_t * reg) +API_EXPORT(void) +ap_pregfree(pool *p, regex_t *reg) { - ap_block_alarms(); - regfree(reg); - ap_kill_cleanup(p, (void *) reg, regex_cleanup); - ap_unblock_alarms(); + ap_block_alarms(); + regfree(reg); + ap_kill_cleanup(p, (void *)reg, regex_cleanup); + ap_unblock_alarms(); } /***************************************************************** @@ -2270,20 +2350,21 @@ API_EXPORT(void) ap_pregfree(pool *p, regex_t * reg) */ struct process_chain { - pid_t pid; - enum kill_conditions kill_how; - struct process_chain *next; + pid_t pid; + enum kill_conditions kill_how; + struct process_chain *next; }; -API_EXPORT(void) ap_note_subprocess(pool *a, pid_t pid, enum kill_conditions -how) { - struct process_chain *new = - (struct process_chain *) ap_palloc(a, sizeof(struct process_chain)); +API_EXPORT(void) +ap_note_subprocess(pool *a, pid_t pid, enum kill_conditions how) +{ + struct process_chain *new = + (struct process_chain *)ap_palloc(a, sizeof(struct process_chain)); - new->pid = pid; - new->kill_how = how; - new->next = a->subprocesses; - a->subprocesses = new; + new->pid = pid; + new->kill_how = how; + new->next = a->subprocesses; + a->subprocesses = new; } #define os_pipe(fds) pipe(fds) @@ -2291,207 +2372,209 @@ how) { /* for ap_fdopen, to get binary mode */ #define BINMODE -static pid_t spawn_child_core(pool *p, int (*func) (void *, child_info *), - void *data,enum kill_conditions kill_how, - int *pipe_in, int *pipe_out, int *pipe_err) +static pid_t +spawn_child_core(pool *p, int (*func)(void *, child_info *), void *data, +enum kill_conditions kill_how, int *pipe_in, int *pipe_out, int *pipe_err) { - pid_t pid; - int in_fds[2]; - int out_fds[2]; - int err_fds[2]; - int save_errno; + pid_t pid; + int in_fds[2]; + int out_fds[2]; + int err_fds[2]; + int save_errno; - if (pipe_in && os_pipe(in_fds) < 0) { - return 0; - } + if (pipe_in && os_pipe(in_fds) < 0) + return 0; - if (pipe_out && os_pipe(out_fds) < 0) { - save_errno = errno; - if (pipe_in) { - close(in_fds[0]); - close(in_fds[1]); + if (pipe_out && os_pipe(out_fds) < 0) { + save_errno = errno; + if (pipe_in) { + close(in_fds[0]); + close(in_fds[1]); + } + errno = save_errno; + return 0; } - errno = save_errno; - return 0; - } - if (pipe_err && os_pipe(err_fds) < 0) { - save_errno = errno; - if (pipe_in) { - close(in_fds[0]); - close(in_fds[1]); - } - if (pipe_out) { - close(out_fds[0]); - close(out_fds[1]); + if (pipe_err && os_pipe(err_fds) < 0) { + save_errno = errno; + if (pipe_in) { + close(in_fds[0]); + close(in_fds[1]); + } + if (pipe_out) { + close(out_fds[0]); + close(out_fds[1]); + } + errno = save_errno; + return 0; } - errno = save_errno; - return 0; - } - if ((pid = fork()) < 0) { - save_errno = errno; - if (pipe_in) { - close(in_fds[0]); - close(in_fds[1]); - } - if (pipe_out) { - close(out_fds[0]); - close(out_fds[1]); + if ((pid = fork()) < 0) { + save_errno = errno; + if (pipe_in) { + close(in_fds[0]); + close(in_fds[1]); + } + if (pipe_out) { + close(out_fds[0]); + close(out_fds[1]); + } + if (pipe_err) { + close(err_fds[0]); + close(err_fds[1]); + } + errno = save_errno; + return 0; } - if (pipe_err) { - close(err_fds[0]); - close(err_fds[1]); + + if (!pid) { + /* Child process */ + RAISE_SIGSTOP(SPAWN_CHILD); + + if (pipe_out) { + close(out_fds[0]); + dup2(out_fds[1], STDOUT_FILENO); + close(out_fds[1]); + } + + if (pipe_in) { + close(in_fds[1]); + dup2(in_fds[0], STDIN_FILENO); + close(in_fds[0]); + } + + if (pipe_err) { + close(err_fds[0]); + dup2(err_fds[1], STDERR_FILENO); + close(err_fds[1]); + } + + /* HP-UX SIGCHLD fix goes here, if someone will remind me + * what it is... */ + signal(SIGCHLD, SIG_DFL); /* Was that it? */ + + func(data, NULL); + exit(1); /* Should only get here if + * the exec in func() failed + */ } - errno = save_errno; - return 0; - } - if (!pid) { - /* Child process */ - RAISE_SIGSTOP(SPAWN_CHILD); + /* Parent process */ + ap_note_subprocess(p, pid, kill_how); if (pipe_out) { - close(out_fds[0]); - dup2(out_fds[1], STDOUT_FILENO); - close(out_fds[1]); + close(out_fds[1]); + *pipe_out = out_fds[0]; } if (pipe_in) { - close(in_fds[1]); - dup2(in_fds[0], STDIN_FILENO); - close(in_fds[0]); + close(in_fds[0]); + *pipe_in = in_fds[1]; } if (pipe_err) { - close(err_fds[0]); - dup2(err_fds[1], STDERR_FILENO); - close(err_fds[1]); + close(err_fds[1]); + *pipe_err = err_fds[0]; } - /* HP-UX SIGCHLD fix goes here, if someone will remind me what it is... */ - signal(SIGCHLD, SIG_DFL); /* Was that it? */ - - func(data, NULL); - exit(1); /* Should only get here if the exec in func() failed */ - } - - /* Parent process */ - - ap_note_subprocess(p, pid, kill_how); - - if (pipe_out) { - close(out_fds[1]); - *pipe_out = out_fds[0]; - } - - if (pipe_in) { - close(in_fds[0]); - *pipe_in = in_fds[1]; - } - - if (pipe_err) { - close(err_fds[1]); - *pipe_err = err_fds[0]; - } - - return pid; + return pid; } -API_EXPORT(int) ap_spawn_child(pool *p, int (*func) (void *, child_info *), - void *data, enum kill_conditions kill_how, - FILE **pipe_in, FILE **pipe_out, - FILE **pipe_err) +API_EXPORT(int) +ap_spawn_child(pool *p, int (*func)(void *, child_info *), void *data, + enum kill_conditions kill_how, FILE **pipe_in, FILE **pipe_out, + FILE **pipe_err) { - int fd_in, fd_out, fd_err; - pid_t pid; - int save_errno; + int fd_in, fd_out, fd_err; + pid_t pid; + int save_errno; - ap_block_alarms(); + ap_block_alarms(); - pid = spawn_child_core(p, func, data, kill_how, - pipe_in ? &fd_in : NULL, - pipe_out ? &fd_out : NULL, - pipe_err ? &fd_err : NULL); + pid = spawn_child_core(p, func, data, kill_how, + pipe_in ? &fd_in : NULL, + pipe_out ? &fd_out : NULL, + pipe_err ? &fd_err : NULL); - if (pid == 0) { - save_errno = errno; - ap_unblock_alarms(); - errno = save_errno; - return 0; - } + if (pid == 0) { + save_errno = errno; + ap_unblock_alarms(); + errno = save_errno; + return 0; + } - if (pipe_out) { - *pipe_out = ap_fdopen(fd_out, "r" BINMODE); - if (*pipe_out) - ap_note_cleanups_for_file(p, *pipe_out); - else - close(fd_out); - } + if (pipe_out) { + *pipe_out = ap_fdopen(fd_out, "r" BINMODE); + if (*pipe_out) + ap_note_cleanups_for_file(p, *pipe_out); + else + close(fd_out); + } - if (pipe_in) { - *pipe_in = ap_fdopen(fd_in, "w" BINMODE); - if (*pipe_in) - ap_note_cleanups_for_file(p, *pipe_in); - else - close(fd_in); - } + if (pipe_in) { + *pipe_in = ap_fdopen(fd_in, "w" BINMODE); + if (*pipe_in) + ap_note_cleanups_for_file(p, *pipe_in); + else + close(fd_in); + } - if (pipe_err) { - *pipe_err = ap_fdopen(fd_err, "r" BINMODE); - if (*pipe_err) - ap_note_cleanups_for_file(p, *pipe_err); - else - close(fd_err); - } + if (pipe_err) { + *pipe_err = ap_fdopen(fd_err, "r" BINMODE); + if (*pipe_err) + ap_note_cleanups_for_file(p, *pipe_err); + else + close(fd_err); + } - ap_unblock_alarms(); - return pid; + ap_unblock_alarms(); + return pid; } -API_EXPORT(int) ap_bspawn_child(pool *p, int (*func) (void *, child_info *), void *data, - enum kill_conditions kill_how, - BUFF **pipe_in, BUFF **pipe_out, BUFF **pipe_err) +API_EXPORT(int) +ap_bspawn_child(pool *p, int (*func)(void *, child_info *), void *data, + enum kill_conditions kill_how, BUFF **pipe_in, BUFF **pipe_out, + BUFF **pipe_err) { - int fd_in, fd_out, fd_err; - pid_t pid; - int save_errno; + int fd_in, fd_out, fd_err; + pid_t pid; + int save_errno; - ap_block_alarms(); + ap_block_alarms(); - pid = spawn_child_core(p, func, data, kill_how, - pipe_in ? &fd_in : NULL, - pipe_out ? &fd_out : NULL, - pipe_err ? &fd_err : NULL); + pid = spawn_child_core(p, func, data, kill_how, + pipe_in ? &fd_in : NULL, + pipe_out ? &fd_out : NULL, + pipe_err ? &fd_err : NULL); - if (pid == 0) { - save_errno = errno; - ap_unblock_alarms(); - errno = save_errno; - return 0; - } + if (pid == 0) { + save_errno = errno; + ap_unblock_alarms(); + errno = save_errno; + return 0; + } - if (pipe_out) { - *pipe_out = ap_bcreate(p, B_RD); - ap_note_cleanups_for_fd_ex(p, fd_out, 0); - ap_bpushfd(*pipe_out, fd_out, fd_out); - } + if (pipe_out) { + *pipe_out = ap_bcreate(p, B_RD); + ap_note_cleanups_for_fd_ex(p, fd_out, 0); + ap_bpushfd(*pipe_out, fd_out, fd_out); + } - if (pipe_in) { - *pipe_in = ap_bcreate(p, B_WR); - ap_note_cleanups_for_fd_ex(p, fd_in, 0); - ap_bpushfd(*pipe_in, fd_in, fd_in); - } + if (pipe_in) { + *pipe_in = ap_bcreate(p, B_WR); + ap_note_cleanups_for_fd_ex(p, fd_in, 0); + ap_bpushfd(*pipe_in, fd_in, fd_in); + } - if (pipe_err) { - *pipe_err = ap_bcreate(p, B_RD); - ap_note_cleanups_for_fd_ex(p, fd_err, 0); - ap_bpushfd(*pipe_err, fd_err, fd_err); - } + if (pipe_err) { + *pipe_err = ap_bcreate(p, B_RD); + ap_note_cleanups_for_fd_ex(p, fd_err, 0); + ap_bpushfd(*pipe_err, fd_err, fd_err); + } - ap_unblock_alarms(); - return pid; + ap_unblock_alarms(); + return pid; } @@ -2505,93 +2588,92 @@ API_EXPORT(int) ap_bspawn_child(pool *p, int (*func) (void *, child_info *), voi #define TIMEOUT_USECS 3000000 #define TIMEOUT_INTERVAL 46875 -static void free_proc_chain(struct process_chain *procs) -{ - /* Dispose of the subprocesses we've spawned off in the course of - * whatever it was we're cleaning up now. This may involve killing - * some of them off... - */ - struct process_chain *p; - int need_timeout = 0; - int status; - int timeout_interval; - struct timeval tv; - - if (procs == NULL) - return; /* No work. Whew! */ - - /* First, check to see if we need to do the SIGTERM, sleep, SIGKILL - * dance with any of the processes we're cleaning up. If we've got - * any kill-on-sight subprocesses, ditch them now as well, so they - * don't waste any more cycles doing whatever it is that they shouldn't - * be doing anymore. - */ - /* Pick up all defunct processes */ - for (p = procs; p; p = p->next) { - if (waitpid(p->pid, (int *) 0, WNOHANG) > 0) { - p->kill_how = kill_never; - } - } - - for (p = procs; p; p = p->next) { - if ((p->kill_how == kill_after_timeout) - || (p->kill_how == kill_only_once)) { - /* - * This is totally bogus, but seems to be the - * only portable (as in reliable) way to accomplish - * this. Note that this implies an unavoidable - * delay. - */ - ap_os_kill(p->pid, SIGTERM); - need_timeout = 1; - } - else if (p->kill_how == kill_always) { - kill(p->pid, SIGKILL); - } - } - - /* Sleep only if we have to. The sleep algorithm grows - * by a factor of two on each iteration. TIMEOUT_INTERVAL - * is equal to TIMEOUT_USECS / 64. - */ - if (need_timeout) { - timeout_interval = TIMEOUT_INTERVAL; - tv.tv_sec = 0; - tv.tv_usec = timeout_interval; - ap_select(0, NULL, NULL, NULL, &tv); - - do { - need_timeout = 0; - for (p = procs; p; p = p->next) { - if (p->kill_how == kill_after_timeout) { - if (waitpid(p->pid, (int *) 0, WNOHANG | WUNTRACED) > 0) - p->kill_how = kill_never; - else - need_timeout = 1; - } - } - if (need_timeout) { - if (timeout_interval >= TIMEOUT_USECS) { - break; - } - tv.tv_sec = timeout_interval / 1000000; - tv.tv_usec = timeout_interval % 1000000; - ap_select(0, NULL, NULL, NULL, &tv); - timeout_interval *= 2; - } - } while (need_timeout); - } - - /* OK, the scripts we just timed out for have had a chance to clean up - * --- now, just get rid of them, and also clean up the system accounting - * goop... - */ - - for (p = procs; p; p = p->next) { - if (p->kill_how == kill_after_timeout) - kill(p->pid, SIGKILL); - - if (p->kill_how != kill_never) - waitpid(p->pid, &status, 0); - } +static void +free_proc_chain(struct process_chain *procs) +{ + /* Dispose of the subprocesses we've spawned off in the course of + * whatever it was we're cleaning up now. This may involve killing + * some of them off... + */ + struct process_chain *p; + int need_timeout = 0; + int status; + int timeout_interval; + struct timeval tv; + + if (procs == NULL) + return; /* No work. Whew! */ + + /* First, check to see if we need to do the SIGTERM, sleep, SIGKILL + * dance with any of the processes we're cleaning up. If we've got + * any kill-on-sight subprocesses, ditch them now as well, so they + * don't waste any more cycles doing whatever it is that they shouldn't + * be doing anymore. + */ + /* Pick up all defunct processes */ + for (p = procs; p; p = p->next) { + if (waitpid(p->pid, (int *) 0, WNOHANG) > 0) { + p->kill_how = kill_never; + } + } + + for (p = procs; p; p = p->next) { + if ((p->kill_how == kill_after_timeout) + || (p->kill_how == kill_only_once)) { + /* + * This is totally bogus, but seems to be the + * only portable (as in reliable) way to accomplish + * this. Note that this implies an unavoidable + * delay. + */ + ap_os_kill(p->pid, SIGTERM); + need_timeout = 1; + } else if (p->kill_how == kill_always) { + kill(p->pid, SIGKILL); + } + } + + /* Sleep only if we have to. The sleep algorithm grows + * by a factor of two on each iteration. TIMEOUT_INTERVAL + * is equal to TIMEOUT_USECS / 64. + */ + if (need_timeout) { + timeout_interval = TIMEOUT_INTERVAL; + tv.tv_sec = 0; + tv.tv_usec = timeout_interval; + ap_select(0, NULL, NULL, NULL, &tv); + + do { + need_timeout = 0; + for (p = procs; p; p = p->next) { + if (p->kill_how == kill_after_timeout) { + if (waitpid(p->pid, (int *)0, + WNOHANG | WUNTRACED) > 0) + p->kill_how = kill_never; + else + need_timeout = 1; + } + } + if (need_timeout) { + if (timeout_interval >= TIMEOUT_USECS) + break; + tv.tv_sec = timeout_interval / 1000000; + tv.tv_usec = timeout_interval % 1000000; + ap_select(0, NULL, NULL, NULL, &tv); + timeout_interval *= 2; + } + } while (need_timeout); + } + + /* OK, the scripts we just timed out for have had a chance to clean up + * --- now, just get rid of them, and also clean up the system + * accounting goop... + */ + for (p = procs; p; p = p->next) { + if (p->kill_how == kill_after_timeout) + kill(p->pid, SIGKILL); + + if (p->kill_how != kill_never) + waitpid(p->pid, &status, 0); + } } diff --git a/usr.sbin/httpd/src/main/buff.c b/usr.sbin/httpd/src/main/buff.c index bcd40b59e31..ecb7ac0bcf0 100644 --- a/usr.sbin/httpd/src/main/buff.c +++ b/usr.sbin/httpd/src/main/buff.c @@ -78,7 +78,8 @@ #define CHUNK_HEADER_SIZE (5) #endif -#define ascii_CRLF "\015\012" /* A CRLF which won't pass the conversion machinery */ +#define ascii_CRLF "\015\012" /* A CRLF which won't pass the conversion + * machinery */ /* bwrite()s of greater than this size can result in a large_write() call, * which can result in a writev(). It's a little more work to set up the @@ -118,135 +119,145 @@ /* the lowest level reading primitive */ -static int ap_read(BUFF *fb, void *buf, int nbyte) +static int +ap_read(BUFF *fb, void *buf, int nbyte) { - int rv; - + int rv; + if (!ap_hook_call("ap::buff::read", &rv, fb, buf, nbyte)) - rv = read(fb->fd_in, buf, nbyte); - - return rv; + rv = read(fb->fd_in, buf, nbyte); + + return rv; } -static ap_inline int buff_read(BUFF *fb, void *buf, int nbyte) +static ap_inline int +buff_read(BUFF *fb, void *buf, int nbyte) { - int rv; + int rv; - rv = ap_read(fb, buf, nbyte); - return rv; + rv = ap_read(fb, buf, nbyte); + return rv; } /* the lowest level writing primitive */ -static int ap_write(BUFF *fb, const void *buf, int nbyte) +static int +ap_write(BUFF *fb, const void *buf, int nbyte) { - int rv; - + int rv; + if (!ap_hook_call("ap::buff::write", &rv, fb, buf, nbyte)) - rv = write(fb->fd, buf, nbyte); - return rv; + rv = write(fb->fd, buf, nbyte); + return rv; } -static ap_inline int buff_write(BUFF *fb, const void *buf, int nbyte) +static ap_inline int +buff_write(BUFF *fb, const void *buf, int nbyte) { - int rv; - - if (fb->filter_callback != NULL) { - fb->filter_callback(fb, buf, nbyte); - } - - rv = ap_write(fb, buf, nbyte); - return rv; + int rv; + + if (fb->filter_callback != NULL) + fb->filter_callback(fb, buf, nbyte); + + rv = ap_write(fb, buf, nbyte); + return rv; } -static void doerror(BUFF *fb, int direction) +static void +doerror(BUFF *fb, int direction) { - int errsave = errno; /* Save errno to prevent overwriting it below */ + int errsave = errno; /* Save errno to prevent overwriting it below */ - fb->flags |= (direction == B_RD ? B_RDERR : B_WRERR); - if (fb->error != NULL) - (*fb->error) (fb, direction, fb->error_data); + fb->flags |= (direction == B_RD ? B_RDERR : B_WRERR); + if (fb->error != NULL) + (*fb->error)(fb, direction, fb->error_data); - errno = errsave; + errno = errsave; } /* Buffering routines */ /* * Create a new buffered stream */ -API_EXPORT(BUFF *) ap_bcreate(pool *p, int flags) +API_EXPORT(BUFF *) +ap_bcreate(pool *p, int flags) { - BUFF *fb; + BUFF *fb; - fb = ap_palloc(p, sizeof(BUFF)); - fb->pool = p; - fb->bufsiz = DEFAULT_BUFSIZE; - fb->flags = flags & (B_RDWR | B_SOCKET); + fb = ap_palloc(p, sizeof(BUFF)); + fb->pool = p; + fb->bufsiz = DEFAULT_BUFSIZE; + fb->flags = flags & (B_RDWR | B_SOCKET); - if (flags & B_RD) - fb->inbase = ap_palloc(p, fb->bufsiz); - else - fb->inbase = NULL; + if (flags & B_RD) + fb->inbase = ap_palloc(p, fb->bufsiz); + else + fb->inbase = NULL; - /* overallocate so that we can put a chunk trailer of CRLF into this - * buffer */ - if (flags & B_WR) - fb->outbase = ap_palloc(p, fb->bufsiz + 2); - else - fb->outbase = NULL; + /* overallocate so that we can put a chunk trailer of CRLF into this + * buffer + */ + if (flags & B_WR) + fb->outbase = ap_palloc(p, fb->bufsiz + 2); + else + fb->outbase = NULL; - fb->inptr = fb->inbase; + fb->inptr = fb->inbase; - fb->incnt = 0; - fb->outcnt = 0; - fb->outchunk = -1; - fb->error = NULL; - fb->bytes_sent = 0L; + fb->incnt = 0; + fb->outcnt = 0; + fb->outchunk = -1; + fb->error = NULL; + fb->bytes_sent = 0L; - fb->fd = -1; - fb->fd_in = -1; + fb->fd = -1; + fb->fd_in = -1; - fb->callback_data = NULL; - fb->filter_callback = NULL; + fb->callback_data = NULL; + fb->filter_callback = NULL; - fb->ctx = ap_ctx_new(p); + fb->ctx = ap_ctx_new(p); - return fb; + return fb; } /* * Push some I/O file descriptors onto the stream */ -API_EXPORT(void) ap_bpushfd(BUFF *fb, int fd_in, int fd_out) +API_EXPORT(void) +ap_bpushfd(BUFF *fb, int fd_in, int fd_out) { - fb->fd = fd_out; - fb->fd_in = fd_in; + fb->fd = fd_out; + fb->fd_in = fd_in; } -API_EXPORT(int) ap_bsetopt(BUFF *fb, int optname, const void *optval) +API_EXPORT(int) +ap_bsetopt(BUFF *fb, int optname, const void *optval) { - if (optname == BO_BYTECT) { - fb->bytes_sent = *(const long int *) optval - (long int) fb->outcnt; - return 0; - } - else { - errno = EINVAL; - return -1; - } + if (optname == BO_BYTECT) { + fb->bytes_sent = *(const long int *)optval - + (long int)fb->outcnt; + return 0; + } + else { + errno = EINVAL; + return -1; + } } -API_EXPORT(int) ap_bgetopt(BUFF *fb, int optname, void *optval) +API_EXPORT(int) +ap_bgetopt(BUFF *fb, int optname, void *optval) { - if (optname == BO_BYTECT) { - long int bs = fb->bytes_sent + fb->outcnt; - if (bs < 0L) - bs = 0L; - *(long int *) optval = bs; - return 0; - } - else { - errno = EINVAL; - return -1; - } + if (optname == BO_BYTECT) { + long int bs = fb->bytes_sent + fb->outcnt; + if (bs < 0L) + bs = 0L; + *(long int *)optval = bs; + return 0; + } + else { + errno = EINVAL; + return -1; + } } static int bflush_core(BUFF *fb); @@ -260,108 +271,110 @@ static int bflush_core(BUFF *fb); * and writes something on the wire, then it has to call start_chunk() or set * an error condition before returning. */ -static void start_chunk(BUFF *fb) +static void +start_chunk(BUFF *fb) { - if (fb->outchunk != -1) { - /* already chunking */ - return; - } - if ((fb->flags & (B_WRERR | B_EOUT | B_WR)) != B_WR) { - /* unbuffered writes */ - return; - } - - /* we need at least the header_len + at least 1 data byte - * remember that we've overallocated fb->outbase so that we can always - * fit the two byte CRLF trailer - */ - if (fb->bufsiz - fb->outcnt < CHUNK_HEADER_SIZE + 1) { - bflush_core(fb); - } - fb->outchunk = fb->outcnt; - fb->outcnt += CHUNK_HEADER_SIZE; + if (fb->outchunk != -1) { + /* already chunking */ + return; + } + if ((fb->flags & (B_WRERR | B_EOUT | B_WR)) != B_WR) { + /* unbuffered writes */ + return; + } + + /* we need at least the header_len + at least 1 data byte + * remember that we've overallocated fb->outbase so that we can always + * fit the two byte CRLF trailer + */ + if (fb->bufsiz - fb->outcnt < CHUNK_HEADER_SIZE + 1) + bflush_core(fb); + + fb->outchunk = fb->outcnt; + fb->outcnt += CHUNK_HEADER_SIZE; } /* * end a chunk -- tweak the chunk_header from start_chunk, and add a trailer */ -static void end_chunk(BUFF *fb) +static void +end_chunk(BUFF *fb) { - int i; - unsigned char *strp; + int i; + unsigned char *strp; - if (fb->outchunk == -1) { - /* not chunking */ - return; - } + if (fb->outchunk == -1) { + /* not chunking */ + return; + } + + if (fb->outchunk + CHUNK_HEADER_SIZE == fb->outcnt) { + /* nothing was written into this chunk, and we can't write a + * 0 size chunk because that signifies EOF, so just erase it + */ + fb->outcnt = fb->outchunk; + fb->outchunk = -1; + return; + } - if (fb->outchunk + CHUNK_HEADER_SIZE == fb->outcnt) { - /* nothing was written into this chunk, and we can't write a 0 size - * chunk because that signifies EOF, so just erase it + /* we know this will fit because of how we wrote it in start_chunk() */ + i = ap_snprintf((char *)&fb->outbase[fb->outchunk], CHUNK_HEADER_SIZE, + "%x", fb->outcnt - fb->outchunk - CHUNK_HEADER_SIZE); + + /* we may have to tack some trailing spaces onto the number we just + * wrote in case it was smaller than our estimated size. We've also + * written a \0 into the buffer with ap_snprintf so we might have to + * put a \r back in. */ - fb->outcnt = fb->outchunk; + strp = &fb->outbase[fb->outchunk + i]; + while (i < CHUNK_HEADER_SIZE - 2) { + *strp++ = ' '; + ++i; + } + *strp++ = CR; + *strp = LF; + + /* tack on the trailing CRLF, we've reserved room for this */ + fb->outbase[fb->outcnt++] = CR; + fb->outbase[fb->outcnt++] = LF; + fb->outchunk = -1; - return; - } - - /* we know this will fit because of how we wrote it in start_chunk() */ - i = ap_snprintf((char *) &fb->outbase[fb->outchunk], CHUNK_HEADER_SIZE, - "%x", fb->outcnt - fb->outchunk - CHUNK_HEADER_SIZE); - - /* we may have to tack some trailing spaces onto the number we just wrote - * in case it was smaller than our estimated size. We've also written - * a \0 into the buffer with ap_snprintf so we might have to put a - * \r back in. - */ - strp = &fb->outbase[fb->outchunk + i]; - while (i < CHUNK_HEADER_SIZE - 2) { - *strp++ = ' '; - ++i; - } - *strp++ = CR; - *strp = LF; - - /* tack on the trailing CRLF, we've reserved room for this */ - fb->outbase[fb->outcnt++] = CR; - fb->outbase[fb->outcnt++] = LF; - - fb->outchunk = -1; } /* * Set a flag on (1) or off (0). */ -API_EXPORT(int) ap_bsetflag(BUFF *fb, int flag, int value) +API_EXPORT(int) +ap_bsetflag(BUFF *fb, int flag, int value) { - if (value) { - fb->flags |= flag; - if (flag & B_CHUNK) { - start_chunk(fb); + if (value) { + fb->flags |= flag; + if (flag & B_CHUNK) + start_chunk(fb); + } else { + fb->flags &= ~flag; + if (flag & B_CHUNK) + end_chunk(fb); } - } - else { - fb->flags &= ~flag; - if (flag & B_CHUNK) { - end_chunk(fb); - } - } - return value; + return value; } -API_EXPORT(int) ap_bnonblock(BUFF *fb, int direction) +API_EXPORT(int) +ap_bnonblock(BUFF *fb, int direction) { - int fd; + int fd; - fd = (direction == B_RD) ? fb->fd_in : fb->fd; - return fcntl(fd, F_SETFL, O_NONBLOCK); + fd = (direction == B_RD) ? fb->fd_in : fb->fd; + return fcntl(fd, F_SETFL, O_NONBLOCK); } -API_EXPORT(int) ap_bfileno(BUFF *fb, int direction) +API_EXPORT(int) +ap_bfileno(BUFF *fb, int direction) { - return (direction == B_RD) ? fb->fd_in : fb->fd; + return (direction == B_RD) ? fb->fd_in : fb->fd; } /* @@ -378,45 +391,46 @@ API_EXPORT(int) ap_bfileno(BUFF *fb, int direction) * * Note we assume the caller has ensured that fb->fd_in <= FD_SETSIZE */ -API_EXPORT(void) ap_bhalfduplex(BUFF *fb) +API_EXPORT(void) +ap_bhalfduplex(BUFF *fb) { - int rv; - fd_set fds; - struct timeval tv; - - /* We don't need to do anything if the connection has been closed - * or there is something readable in the incoming buffer - * or there is nothing flushable in the output buffer. - */ - if (fb == NULL || fb->fd_in < 0 || fb->incnt > 0 || fb->outcnt == 0) { - return; - } - /* test for a block */ - do { - FD_ZERO(&fds); - FD_SET(fb->fd_in, &fds); - tv.tv_sec = 0; - tv.tv_usec = 0; - rv = ap_select(fb->fd_in + 1, &fds, NULL, NULL, &tv); - } while (rv < 0 && errno == EINTR && !(fb->flags & B_EOUT)); - - /* treat any error as if it would block as well */ - if (rv != 1) { - ap_bflush(fb); - } + int rv; + fd_set fds; + struct timeval tv; + + /* We don't need to do anything if the connection has been closed + * or there is something readable in the incoming buffer + * or there is nothing flushable in the output buffer. + */ + if (fb == NULL || fb->fd_in < 0 || fb->incnt > 0 || fb->outcnt == 0) + return; + + /* test for a block */ + do { + FD_ZERO(&fds); + FD_SET(fb->fd_in, &fds); + tv.tv_sec = 0; + tv.tv_usec = 0; + rv = ap_select(fb->fd_in + 1, &fds, NULL, NULL, &tv); + } while (rv < 0 && errno == EINTR && !(fb->flags & B_EOUT)); + + /* treat any error as if it would block as well */ + if (rv != 1) + ap_bflush(fb); } -static ap_inline int saferead_guts(BUFF *fb, void *buf, int nbyte) +static ap_inline int +saferead_guts(BUFF *fb, void *buf, int nbyte) { - int rv; - - if (fb->flags & B_SAFEREAD) { - ap_bhalfduplex(fb); - } - do { - rv = buff_read(fb, buf, nbyte); - } while (rv == -1 && errno == EINTR && !(fb->flags & B_EOUT)); - return (rv); + int rv; + + if (fb->flags & B_SAFEREAD) + ap_bhalfduplex(fb); + + do { + rv = buff_read(fb, buf, nbyte); + } while (rv == -1 && errno == EINTR && !(fb->flags & B_EOUT)); + return (rv); } @@ -425,18 +439,17 @@ static ap_inline int saferead_guts(BUFF *fb, void *buf, int nbyte) * and then there's the SFIO case. Note that saferead takes care * of EINTR. */ -static int read_with_errors(BUFF *fb, void *buf, int nbyte) +static int +read_with_errors(BUFF *fb, void *buf, int nbyte) { - int rv; - - rv = saferead(fb, buf, nbyte); - if (rv == 0) { - fb->flags |= B_EOF; - } - else if (rv == -1 && errno != EAGAIN) { - doerror(fb, B_RD); - } - return rv; + int rv; + + rv = saferead(fb, buf, nbyte); + if (rv == 0) + fb->flags |= B_EOF; + else if (rv == -1 && errno != EAGAIN) + doerror(fb, B_RD); + return rv; } @@ -448,70 +461,69 @@ static int read_with_errors(BUFF *fb, void *buf, int nbyte) * Only when the caller retrieves data from the buffer (calls bread) * is a conversion done, if the conversion flag is set at that time. */ -API_EXPORT(int) ap_bread(BUFF *fb, void *buf, int nbyte) +API_EXPORT(int) +ap_bread(BUFF *fb, void *buf, int nbyte) { - int i, nrd; + int i, nrd; - if (fb->flags & B_RDERR) - return -1; - if (nbyte == 0) - return 0; + if (fb->flags & B_RDERR) + return -1; + if (nbyte == 0) + return 0; + + if (!(fb->flags & B_RD)) { + /* Unbuffered reading. First check if there was something in + * the buffer from before we went unbuffered. */ + if (fb->incnt) { + i = (fb->incnt > nbyte) ? nbyte : fb->incnt; + memcpy(buf, fb->inptr, i); + fb->incnt -= i; + fb->inptr += i; + return i; + } + i = read_with_errors(fb, buf, nbyte); + return i; + } - if (!(fb->flags & B_RD)) { - /* Unbuffered reading. First check if there was something in the - * buffer from before we went unbuffered. */ - if (fb->incnt) { - i = (fb->incnt > nbyte) ? nbyte : fb->incnt; - memcpy(buf, fb->inptr, i); - fb->incnt -= i; - fb->inptr += i; - return i; + nrd = fb->incnt; + /* can we fill the buffer */ + if (nrd >= nbyte) { + memcpy(buf, fb->inptr, nbyte); + fb->incnt = nrd - nbyte; + fb->inptr += nbyte; + return nbyte; } - i = read_with_errors(fb, buf, nbyte); - return i; - } - - nrd = fb->incnt; -/* can we fill the buffer */ - if (nrd >= nbyte) { - memcpy(buf, fb->inptr, nbyte); - fb->incnt = nrd - nbyte; - fb->inptr += nbyte; - return nbyte; - } - if (nrd > 0) { - memcpy(buf, fb->inptr, nrd); - nbyte -= nrd; - buf = nrd + (char *) buf; - fb->incnt = 0; - } - if (fb->flags & B_EOF) - return nrd; - -/* do a single read */ - if (nbyte >= fb->bufsiz) { -/* read directly into caller's buffer */ - i = read_with_errors(fb, buf, nbyte); - if (i == -1) { - return nrd ? nrd : -1; + if (nrd > 0) { + memcpy(buf, fb->inptr, nrd); + nbyte -= nrd; + buf = nrd + (char *)buf; + fb->incnt = 0; } - } - else { -/* read into hold buffer, then memcpy */ - fb->inptr = fb->inbase; - i = read_with_errors(fb, fb->inptr, fb->bufsiz); - if (i == -1) { - return nrd ? nrd : -1; + if (fb->flags & B_EOF) + return nrd; + + /* do a single read */ + if (nbyte >= fb->bufsiz) { + /* read directly into caller's buffer */ + i = read_with_errors(fb, buf, nbyte); + if (i == -1) + return nrd ? nrd : -1; } - fb->incnt = i; - if (i > nbyte) - i = nbyte; - memcpy(buf, fb->inptr, i); - fb->incnt -= i; - fb->inptr += i; - } - return nrd + i; + else { + /* read into hold buffer, then memcpy */ + fb->inptr = fb->inbase; + i = read_with_errors(fb, fb->inptr, fb->bufsiz); + if (i == -1) + return nrd ? nrd : -1; + fb->incnt = i; + if (i > nbyte) + i = nbyte; + memcpy(buf, fb->inptr, i); + fb->incnt -= i; + fb->inptr += i; + } + return nrd + i; } @@ -533,64 +545,65 @@ API_EXPORT(int) ap_bread(BUFF *fb, void *buf, int nbyte) * CR characters in the byte stream not immediately followed by a LF * will be preserved. */ -API_EXPORT(int) ap_bgets(char *buff, int n, BUFF *fb) +API_EXPORT(int) +ap_bgets(char *buff, int n, BUFF *fb) { - int i, ch, ct; - -/* Can't do bgets on an unbuffered stream */ - if (!(fb->flags & B_RD)) { - errno = EINVAL; - return -1; - } - if (fb->flags & B_RDERR) - return -1; - - ct = 0; - i = 0; - for (;;) { - if (i == fb->incnt) { -/* no characters left */ - fb->inptr = fb->inbase; - fb->incnt = 0; - if (fb->flags & B_EOF) - break; - i = read_with_errors(fb, fb->inptr, fb->bufsiz); - if (i == -1) { - buff[ct] = '\0'; - return ct ? ct : -1; - } - fb->incnt = i; - if (i == 0) - break; /* EOF */ - i = 0; - continue; /* restart with the new data */ - } + int i, ch, ct; - ch = fb->inptr[i++]; - if (ch == LF) { /* got LF */ - if (ct == 0) - buff[ct++] = '\n'; -/* if just preceded by CR, replace CR with LF */ - else if (buff[ct - 1] == CR) - buff[ct - 1] = '\n'; - else if (ct < n - 1) - buff[ct++] = '\n'; - else - i--; /* no room for LF */ - break; - } - if (ct == n - 1) { - i--; /* push back ch */ - break; + /* Can't do bgets on an unbuffered stream */ + if (!(fb->flags & B_RD)) { + errno = EINVAL; + return -1; } + if (fb->flags & B_RDERR) + return -1; - buff[ct++] = ch; - } - fb->incnt -= i; - fb->inptr += i; + ct = 0; + i = 0; + for (;;) { + if (i == fb->incnt) { + /* no characters left */ + fb->inptr = fb->inbase; + fb->incnt = 0; + if (fb->flags & B_EOF) + break; + i = read_with_errors(fb, fb->inptr, fb->bufsiz); + if (i == -1) { + buff[ct] = '\0'; + return ct ? ct : -1; + } + fb->incnt = i; + if (i == 0) + break; /* EOF */ + i = 0; + continue; /* restart with the new data */ + } + + ch = fb->inptr[i++]; + if (ch == LF) { /* got LF */ + if (ct == 0) + buff[ct++] = '\n'; + /* if just preceded by CR, replace CR with LF */ + else if (buff[ct - 1] == CR) + buff[ct - 1] = '\n'; + else if (ct < n - 1) + buff[ct++] = '\n'; + else + i--; /* no room for LF */ + break; + } + if (ct == n - 1) { + i--; /* push back ch */ + break; + } + + buff[ct++] = ch; + } + fb->incnt -= i; + fb->inptr += i; - buff[ct] = '\0'; - return ct; + buff[ct] = '\0'; + return ct; } /* @@ -600,99 +613,103 @@ API_EXPORT(int) ap_bgets(char *buff, int n, BUFF *fb) * Returns 1 on success, zero on end of transmission, or -1 on an error. * */ -API_EXPORT(int) ap_blookc(char *buff, BUFF *fb) +API_EXPORT(int) +ap_blookc(char *buff, BUFF *fb) { - int i; + int i; - *buff = '\0'; + *buff = '\0'; - if (!(fb->flags & B_RD)) { /* Can't do blookc on an unbuffered stream */ - errno = EINVAL; - return -1; - } - if (fb->flags & B_RDERR) - return -1; + if (!(fb->flags & B_RD)) { /* Can't do blookc on an unbuffered + * stream */ + errno = EINVAL; + return -1; + } + if (fb->flags & B_RDERR) + return -1; - if (fb->incnt == 0) { /* no characters left in stream buffer */ - fb->inptr = fb->inbase; - if (fb->flags & B_EOF) - return 0; + if (fb->incnt == 0) { /* no characters left in stream buffer */ + fb->inptr = fb->inbase; + if (fb->flags & B_EOF) + return 0; - i = read_with_errors(fb, fb->inptr, fb->bufsiz); - if (i <= 0) { - return i; + i = read_with_errors(fb, fb->inptr, fb->bufsiz); + if (i <= 0) + return i; + fb->incnt = i; } - fb->incnt = i; - } - *buff = fb->inptr[0]; - return 1; + *buff = fb->inptr[0]; + return 1; } /* * Skip data until a linefeed character is read * Returns 1 on success, 0 if no LF found, or -1 on error */ -API_EXPORT(int) ap_bskiplf(BUFF *fb) +API_EXPORT(int) +ap_bskiplf(BUFF *fb) { - unsigned char *x; - int i; - -/* Can't do bskiplf on an unbuffered stream */ - if (!(fb->flags & B_RD)) { - errno = EINVAL; - return -1; - } - if (fb->flags & B_RDERR) - return -1; - - for (;;) { - x = (unsigned char *) memchr(fb->inptr, '\012', fb->incnt); - if (x != NULL) { - x++; - fb->incnt -= x - fb->inptr; - fb->inptr = x; - return 1; + unsigned char *x; + int i; + + /* Can't do bskiplf on an unbuffered stream */ + if (!(fb->flags & B_RD)) { + errno = EINVAL; + return -1; } + if (fb->flags & B_RDERR) + return -1; - fb->inptr = fb->inbase; - fb->incnt = 0; - if (fb->flags & B_EOF) - return 0; - i = read_with_errors(fb, fb->inptr, fb->bufsiz); - if (i <= 0) - return i; - fb->incnt = i; - } + for (;;) { + x = (unsigned char *)memchr(fb->inptr, '\012', fb->incnt); + if (x != NULL) { + x++; + fb->incnt -= x - fb->inptr; + fb->inptr = x; + return 1; + } + + fb->inptr = fb->inbase; + fb->incnt = 0; + if (fb->flags & B_EOF) + return 0; + i = read_with_errors(fb, fb->inptr, fb->bufsiz); + if (i <= 0) + return i; + fb->incnt = i; + } } /* * output a single character. Used by ap_bputs when the buffer * is full... and so it'll cause the buffer to be flushed first. */ -API_EXPORT(int) ap_bflsbuf(int c, BUFF *fb) +API_EXPORT(int) +ap_bflsbuf(int c, BUFF *fb) { - char ss[1]; + char ss[1]; - ss[0] = c; - return ap_bwrite(fb, ss, 1); + ss[0] = c; + return ap_bwrite(fb, ss, 1); } /* * Fill the buffer and read a character from it */ -API_EXPORT(int) ap_bfilbuf(BUFF *fb) +API_EXPORT(int) +ap_bfilbuf(BUFF *fb) { - int i; - char buf[1]; - - i = ap_bread(fb, buf, 1); - if (i == 0) - errno = 0; /* no error; EOF */ - if (i != 1) - return EOF; - else - return buf[0]; + int i; + char buf[1]; + + i = ap_bread(fb, buf, 1); + if (i == 0) + errno = 0; /* no error; EOF */ + if (i != 1) + return EOF; + else + return buf[0]; } @@ -705,30 +722,31 @@ API_EXPORT(int) ap_bfilbuf(BUFF *fb) * * Deals with calling doerror and setting bytes_sent. */ -static int write_it_all(BUFF *fb, const void *buf, int nbyte) +static int +write_it_all(BUFF *fb, const void *buf, int nbyte) { - int i; - - if (fb->flags & (B_WRERR | B_EOUT)) - return -1; + int i; - while (nbyte > 0) { - i = buff_write(fb, buf, nbyte); - if (i < 0) { - if (errno != EAGAIN && errno != EINTR) { - doerror(fb, B_WR); + if (fb->flags & (B_WRERR | B_EOUT)) return -1; - } - } - else { - nbyte -= i; - buf = i + (const char *) buf; - fb->bytes_sent += i; + + while (nbyte > 0) { + i = buff_write(fb, buf, nbyte); + if (i < 0) { + if (errno != EAGAIN && errno != EINTR) { + doerror(fb, B_WR); + return -1; + } + } + else { + nbyte -= i; + buf = i + (const char *) buf; + fb->bytes_sent += i; + } + if (fb->flags & B_EOUT) + return -1; } - if (fb->flags & B_EOUT) - return -1; - } - return 0; + return 0; } @@ -737,74 +755,74 @@ static int write_it_all(BUFF *fb, const void *buf, int nbyte) * * Deals with doerror() and bytes_sent. */ -static int writev_it_all(BUFF *fb, struct iovec *vec, int nvec) +static int +writev_it_all(BUFF *fb, struct iovec *vec, int nvec) { - int i, rv; - - if (fb->filter_callback != NULL) { - for (i = 0; i < nvec; i++) { - fb->filter_callback(fb, vec[i].iov_base, vec[i].iov_len); - } - } - - /* while it's nice an easy to build the vector and crud, it's painful - * to deal with a partial writev() - */ - i = 0; - while (i < nvec) { - do - if (!ap_hook_call("ap::buff::writev", &rv, fb, &vec[i], nvec -i)) - rv = writev(fb->fd, &vec[i], nvec - i); - while (rv == -1 && (errno == EINTR || errno == EAGAIN) - && !(fb->flags & B_EOUT)); - if (rv == -1) { - if (errno != EINTR && errno != EAGAIN) { - doerror(fb, B_WR); - } - return -1; + int i, rv; + + if (fb->filter_callback != NULL) { + for (i = 0; i < nvec; i++) + fb->filter_callback(fb, vec[i].iov_base, + vec[i].iov_len); } - fb->bytes_sent += rv; - /* recalculate vec to deal with partial writes */ - while (rv > 0) { - if (rv < vec[i].iov_len) { - vec[i].iov_base = (char *) vec[i].iov_base + rv; - vec[i].iov_len -= rv; - rv = 0; - } - else { - rv -= vec[i].iov_len; - ++i; - } + + /* while it's nice an easy to build the vector and crud, it's painful + * to deal with a partial writev() + */ + i = 0; + while (i < nvec) { + do + if (!ap_hook_call("ap::buff::writev", &rv, fb, &vec[i], + nvec -i)) + rv = writev(fb->fd, &vec[i], nvec - i); + while (rv == -1 && (errno == EINTR || errno == EAGAIN) + && !(fb->flags & B_EOUT)); + if (rv == -1) { + if (errno != EINTR && errno != EAGAIN) + doerror(fb, B_WR); + + return -1; + } + fb->bytes_sent += rv; + /* recalculate vec to deal with partial writes */ + while (rv > 0) { + if (rv < vec[i].iov_len) { + vec[i].iov_base = (char *)vec[i].iov_base + rv; + vec[i].iov_len -= rv; + rv = 0; + } else { + rv -= vec[i].iov_len; + ++i; + } + } + if (fb->flags & B_EOUT) + return -1; } - if (fb->flags & B_EOUT) - return -1; - } - /* if we got here, we wrote it all */ - return 0; + /* if we got here, we wrote it all */ + return 0; } /* A wrapper for buff_write which deals with error conditions and * bytes_sent. Also handles non-blocking writes. */ -static int write_with_errors(BUFF *fb, const void *buf, int nbyte) +static int +write_with_errors(BUFF *fb, const void *buf, int nbyte) { - int rv; - - do - rv = buff_write(fb, buf, nbyte); - while (rv == -1 && errno == EINTR && !(fb->flags & B_EOUT)); - if (rv == -1) { - if (errno != EAGAIN) { - doerror(fb, B_WR); + int rv; + + do + rv = buff_write(fb, buf, nbyte); + while (rv == -1 && errno == EINTR && !(fb->flags & B_EOUT)); + if (rv == -1) { + if (errno != EAGAIN) + doerror(fb, B_WR); + return -1; + } else if (rv == 0) { + errno = EAGAIN; + return -1; } - return -1; - } - else if (rv == 0) { - errno = EAGAIN; - return -1; - } - fb->bytes_sent += rv; - return rv; + fb->bytes_sent += rv; + return rv; } @@ -817,27 +835,28 @@ static int write_with_errors(BUFF *fb, const void *buf, int nbyte) * Can be used on non-blocking descriptors, but only if they're not chunked. * Deals with doerror() and bytes_sent. */ -static int bcwrite(BUFF *fb, const void *buf, int nbyte) +static int +bcwrite(BUFF *fb, const void *buf, int nbyte) { - char chunksize[16]; /* Big enough for practically anything */ - struct iovec vec[3]; + char chunksize[16]; /* Big enough for practically anything */ + struct iovec vec[3]; - if (fb->flags & (B_WRERR | B_EOUT)) - return -1; + if (fb->flags & (B_WRERR | B_EOUT)) + return -1; - if (!(fb->flags & B_CHUNK)) { - return write_with_errors(fb, buf, nbyte); - } + if (!(fb->flags & B_CHUNK)) + return write_with_errors(fb, buf, nbyte); - vec[0].iov_base = chunksize; - vec[0].iov_len = ap_snprintf(chunksize, sizeof(chunksize), "%x" CRLF, - nbyte); - vec[1].iov_base = (void *) buf; /* cast is to avoid const warning */ - vec[1].iov_len = nbyte; - vec[2].iov_base = ascii_CRLF; - vec[2].iov_len = 2; + vec[0].iov_base = chunksize; + vec[0].iov_len = ap_snprintf(chunksize, sizeof(chunksize), "%x" CRLF, + nbyte); + vec[1].iov_base = (void *)buf; /* cast is to avoid const warning */ + vec[1].iov_len = nbyte; + vec[2].iov_base = ascii_CRLF; + vec[2].iov_len = 2; - return writev_it_all(fb, vec, (sizeof(vec) / sizeof(vec[0]))) ? -1 : nbyte; + return writev_it_all(fb, vec, + (sizeof(vec) / sizeof(vec[0]))) ? -1 : nbyte; } @@ -845,48 +864,47 @@ static int bcwrite(BUFF *fb, const void *buf, int nbyte) * Used to combine the contents of the fb buffer, and a large buffer * passed in. */ -static int large_write(BUFF *fb, const void *buf, int nbyte) +static int +large_write(BUFF *fb, const void *buf, int nbyte) { - struct iovec vec[4]; - int nvec; - char chunksize[16]; - - /* it's easiest to end the current chunk */ - if (fb->flags & B_CHUNK) { - end_chunk(fb); - } - nvec = 0; - if (fb->outcnt > 0) { - vec[nvec].iov_base = (void *) fb->outbase; - vec[nvec].iov_len = fb->outcnt; - ++nvec; - } - if (fb->flags & B_CHUNK) { - vec[nvec].iov_base = chunksize; - vec[nvec].iov_len = ap_snprintf(chunksize, sizeof(chunksize), - "%x" CRLF, nbyte); - ++nvec; - vec[nvec].iov_base = (void *) buf; - vec[nvec].iov_len = nbyte; - ++nvec; - vec[nvec].iov_base = ascii_CRLF; - vec[nvec].iov_len = 2; - ++nvec; - } - else { - vec[nvec].iov_base = (void *) buf; - vec[nvec].iov_len = nbyte; - ++nvec; - } - - fb->outcnt = 0; - if (writev_it_all(fb, vec, nvec)) { - return -1; - } - else if (fb->flags & B_CHUNK) { - start_chunk(fb); - } - return nbyte; + struct iovec vec[4]; + int nvec; + char chunksize[16]; + + /* it's easiest to end the current chunk */ + if (fb->flags & B_CHUNK) + end_chunk(fb); + + nvec = 0; + if (fb->outcnt > 0) { + vec[nvec].iov_base = (void *)fb->outbase; + vec[nvec].iov_len = fb->outcnt; + ++nvec; + } + if (fb->flags & B_CHUNK) { + vec[nvec].iov_base = chunksize; + vec[nvec].iov_len = ap_snprintf(chunksize, sizeof(chunksize), + "%x" CRLF, nbyte); + ++nvec; + vec[nvec].iov_base = (void *)buf; + vec[nvec].iov_len = nbyte; + ++nvec; + vec[nvec].iov_base = ascii_CRLF; + vec[nvec].iov_len = 2; + ++nvec; + } else { + vec[nvec].iov_base = (void *)buf; + vec[nvec].iov_len = nbyte; + ++nvec; + } + + fb->outcnt = 0; + if (writev_it_all(fb, vec, nvec)) + return -1; + else if (fb->flags & B_CHUNK) + start_chunk(fb); + + return nbyte; } @@ -897,176 +915,176 @@ static int large_write(BUFF *fb, const void *buf, int nbyte) * It is worth noting that if an error occurs, the buffer is in an unknown * state. */ -API_EXPORT(int) ap_bwrite(BUFF *fb, const void *buf, int nbyte) +API_EXPORT(int) +ap_bwrite(BUFF *fb, const void *buf, int nbyte) { - int i, nwr, useable_bufsiz; + int i, nwr, useable_bufsiz; - if (fb->flags & (B_WRERR | B_EOUT)) - return -1; - if (nbyte == 0) - return 0; + if (fb->flags & (B_WRERR | B_EOUT)) + return -1; + if (nbyte == 0) + return 0; + + if (!(fb->flags & B_WR)) { + /* unbuffered write -- have to use bcwrite since we aren't + * taking care of chunking any other way + */ + return bcwrite(fb, buf, nbyte); + } - if (!(fb->flags & B_WR)) { -/* unbuffered write -- have to use bcwrite since we aren't taking care - * of chunking any other way */ - return bcwrite(fb, buf, nbyte); - } + /* + * Detect case where we're asked to write a large buffer, and combine our + * current buffer with it in a single writev(). Note we don't consider + * the case nbyte == 1 because modules which use rputc() loops will cause + * us to use writev() too frequently. In those cases we really should just + * start a new buffer. + */ + if (fb->outcnt > 0 && nbyte > LARGE_WRITE_THRESHOLD + && nbyte + fb->outcnt >= fb->bufsiz) + return large_write(fb, buf, nbyte); -/* - * Detect case where we're asked to write a large buffer, and combine our - * current buffer with it in a single writev(). Note we don't consider - * the case nbyte == 1 because modules which use rputc() loops will cause - * us to use writev() too frequently. In those cases we really should just - * start a new buffer. - */ - if (fb->outcnt > 0 && nbyte > LARGE_WRITE_THRESHOLD - && nbyte + fb->outcnt >= fb->bufsiz) { - return large_write(fb, buf, nbyte); - } -/* - * Whilst there is data in the buffer, keep on adding to it and writing it - * out - */ - nwr = 0; - while (fb->outcnt > 0) { -/* can we accept some data? */ - i = fb->bufsiz - fb->outcnt; - if (i > 0) { - if (i > nbyte) - i = nbyte; - memcpy(fb->outbase + fb->outcnt, buf, i); - fb->outcnt += i; - nbyte -= i; - buf = i + (const char *) buf; - nwr += i; - if (nbyte == 0) - return nwr; /* return if none left */ - } - -/* the buffer must be full */ - if (fb->flags & B_CHUNK) { - end_chunk(fb); - /* it is just too painful to try to re-cram the buffer while - * chunking - */ - if (write_it_all(fb, fb->outbase, fb->outcnt) == -1) { - /* we cannot continue after a chunked error */ + /* + * Whilst there is data in the buffer, keep on adding to it and + * writing it out + */ + nwr = 0; + while (fb->outcnt > 0) { + /* can we accept some data? */ + i = fb->bufsiz - fb->outcnt; + if (i > 0) { + if (i > nbyte) + i = nbyte; + memcpy(fb->outbase + fb->outcnt, buf, i); + fb->outcnt += i; + nbyte -= i; + buf = i + (const char *)buf; + nwr += i; + if (nbyte == 0) + return nwr; /* return if none left */ + } + + /* the buffer must be full */ + if (fb->flags & B_CHUNK) { + end_chunk(fb); + /* it is just too painful to try to re-cram the buffer while + * chunking + */ + if (write_it_all(fb, fb->outbase, fb->outcnt) == -1) { + /* we cannot continue after a chunked error */ + return -1; + } + fb->outcnt = 0; + break; + } + i = write_with_errors(fb, fb->outbase, fb->outcnt); + if (i <= 0) + return nwr ? nwr : -1; + + /* deal with a partial write */ + if (i < fb->outcnt) { + int j, n = fb->outcnt; + unsigned char *x = fb->outbase; + for (j = i; j < n; j++) + x[j - i] = x[j]; + fb->outcnt -= i; + } else + fb->outcnt = 0; + + if (fb->flags & B_EOUT) return -1; - } - fb->outcnt = 0; - break; - } - i = write_with_errors(fb, fb->outbase, fb->outcnt); - if (i <= 0) { - return nwr ? nwr : -1; } - - /* deal with a partial write */ - if (i < fb->outcnt) { - int j, n = fb->outcnt; - unsigned char *x = fb->outbase; - for (j = i; j < n; j++) - x[j - i] = x[j]; - fb->outcnt -= i; - } - else - fb->outcnt = 0; - - if (fb->flags & B_EOUT) - return -1; - } -/* we have emptied the file buffer. Now try to write the data from the - * original buffer until there is less than bufsiz left. Note that we - * use bcwrite() to do this for us, it will do the chunking so that - * we don't have to dink around building a chunk in our own buffer. - * - * Note also that bcwrite never does a partial write if we're chunking, - * so we're guaranteed to either end in an error state, or make it - * out of this loop and call start_chunk() below. - * - * Remember we may not be able to use the entire buffer if we're - * chunking. - */ - useable_bufsiz = fb->bufsiz; - if (fb->flags & B_CHUNK) useable_bufsiz -= CHUNK_HEADER_SIZE; - while (nbyte >= useable_bufsiz) { - i = bcwrite(fb, buf, nbyte); - if (i <= 0) { - return nwr ? nwr : -1; + /* we have emptied the file buffer. Now try to write the data from the + * original buffer until there is less than bufsiz left. Note that we + * use bcwrite() to do this for us, it will do the chunking so that + * we don't have to dink around building a chunk in our own buffer. + * + * Note also that bcwrite never does a partial write if we're chunking, + * so we're guaranteed to either end in an error state, or make it + * out of this loop and call start_chunk() below. + * + * Remember we may not be able to use the entire buffer if we're + * chunking. + */ + useable_bufsiz = fb->bufsiz; + if (fb->flags & B_CHUNK) + useable_bufsiz -= CHUNK_HEADER_SIZE; + while (nbyte >= useable_bufsiz) { + i = bcwrite(fb, buf, nbyte); + if (i <= 0) + return nwr ? nwr : -1; + + buf = i + (const char *)buf; + nwr += i; + nbyte -= i; + + if (fb->flags & B_EOUT) + return -1; } - - buf = i + (const char *) buf; - nwr += i; - nbyte -= i; - - if (fb->flags & B_EOUT) - return -1; - } -/* copy what's left to the file buffer */ - fb->outcnt = 0; - if (fb->flags & B_CHUNK) - start_chunk(fb); - if (nbyte > 0) - memcpy(fb->outbase + fb->outcnt, buf, nbyte); - fb->outcnt += nbyte; - nwr += nbyte; - return nwr; + /* copy what's left to the file buffer */ + fb->outcnt = 0; + if (fb->flags & B_CHUNK) + start_chunk(fb); + if (nbyte > 0) + memcpy(fb->outbase + fb->outcnt, buf, nbyte); + fb->outcnt += nbyte; + nwr += nbyte; + return nwr; } -static int bflush_core(BUFF *fb) +static int +bflush_core(BUFF *fb) { - int i; - - while (fb->outcnt > 0) { - i = write_with_errors(fb, fb->outbase, fb->outcnt); - if (i <= 0) - return -1; - - /* - * We should have written all the data, but if the fd was in a - * strange (non-blocking) mode, then we might not have done so. - */ - if (i < fb->outcnt) { - int j, n = fb->outcnt; - unsigned char *x = fb->outbase; - for (j = i; j < n; j++) - x[j - i] = x[j]; + int i; + + while (fb->outcnt > 0) { + i = write_with_errors(fb, fb->outbase, fb->outcnt); + if (i <= 0) + return -1; + + /* + * We should have written all the data, but if the fd was in a + * strange (non-blocking) mode, then we might not have done so. + */ + if (i < fb->outcnt) { + int j, n = fb->outcnt; + unsigned char *x = fb->outbase; + for (j = i; j < n; j++) + x[j - i] = x[j]; + } + fb->outcnt -= i; + + /* If a soft timeout occurs while flushing, the handler should + * have set the buffer flag B_EOUT. + */ + if (fb->flags & B_EOUT) + return -1; } - fb->outcnt -= i; - - /* If a soft timeout occurs while flushing, the handler should - * have set the buffer flag B_EOUT. - */ - if (fb->flags & B_EOUT) - return -1; - } - - return 0; + return 0; } /* * Flushes the buffered stream. * Returns 0 on success or -1 on error */ -API_EXPORT(int) ap_bflush(BUFF *fb) +API_EXPORT(int) +ap_bflush(BUFF *fb) { - int ret; + int ret; - if ((fb->flags & (B_WRERR | B_EOUT | B_WR)) != B_WR) - return -1; + if ((fb->flags & (B_WRERR | B_EOUT | B_WR)) != B_WR) + return -1; - if (fb->flags & B_CHUNK) - end_chunk(fb); + if (fb->flags & B_CHUNK) + end_chunk(fb); - ret = bflush_core(fb); + ret = bflush_core(fb); - if (ret == 0 && (fb->flags & B_CHUNK)) { - start_chunk(fb); - } + if (ret == 0 && (fb->flags & B_CHUNK)) + start_chunk(fb); - return ret; + return ret; } /* @@ -1075,153 +1093,152 @@ API_EXPORT(int) ap_bflush(BUFF *fb) * Sets the EOF flag to indicate no further data can be read, * and the EOUT flag to indicate no further data can be written. */ -API_EXPORT(int) ap_bclose(BUFF *fb) +API_EXPORT(int) +ap_bclose(BUFF *fb) { - int rc1, rc2, rc3; - - if (fb->flags & B_WR) - rc1 = ap_bflush(fb); - else - rc1 = 0; - if (fb->flags & B_SOCKET) { - rc2 = ap_pclosesocket(fb->pool, fb->fd); - if (fb->fd_in != fb->fd) { - rc3 = ap_pclosesocket(fb->pool, fb->fd_in); - } - else { - rc3 = 0; - } - } else { - rc2 = ap_pclosef(fb->pool, fb->fd); - if (fb->fd_in != fb->fd) { - rc3 = ap_pclosef(fb->pool, fb->fd_in); - } - else { - rc3 = 0; + int rc1, rc2, rc3; + + if (fb->flags & B_WR) + rc1 = ap_bflush(fb); + else + rc1 = 0; + if (fb->flags & B_SOCKET) { + rc2 = ap_pclosesocket(fb->pool, fb->fd); + if (fb->fd_in != fb->fd) + rc3 = ap_pclosesocket(fb->pool, fb->fd_in); + else + rc3 = 0; + } else { + rc2 = ap_pclosef(fb->pool, fb->fd); + if (fb->fd_in != fb->fd) + rc3 = ap_pclosef(fb->pool, fb->fd_in); + else + rc3 = 0; } - } - - fb->inptr = fb->inbase; - fb->incnt = 0; - fb->outcnt = 0; - - fb->flags |= B_EOF | B_EOUT; - fb->fd = -1; - fb->fd_in = -1; - - if (rc1 != 0) - return rc1; - else if (rc2 != 0) - return rc2; - else - return rc3; + + fb->inptr = fb->inbase; + fb->incnt = 0; + fb->outcnt = 0; + + fb->flags |= B_EOF | B_EOUT; + fb->fd = -1; + fb->fd_in = -1; + + if (rc1 != 0) + return rc1; + else if (rc2 != 0) + return rc2; + else + return rc3; } /* * returns the number of bytes written or -1 on error */ -API_EXPORT(int) ap_bputs(const char *x, BUFF *fb) +API_EXPORT(int) +ap_bputs(const char *x, BUFF *fb) { - int i, j = strlen(x); - i = ap_bwrite(fb, x, j); - if (i != j) - return -1; - else - return j; + int i, j = strlen(x); + i = ap_bwrite(fb, x, j); + if (i != j) + return -1; + else + return j; } /* * returns the number of bytes written or -1 on error */ -API_EXPORT_NONSTD(int) ap_bvputs(BUFF *fb,...) +API_EXPORT_NONSTD(int) +ap_bvputs(BUFF *fb,...) { - int i, j, k; - va_list v; - const char *x; - - va_start(v, fb); - for (k = 0;;) { - x = va_arg(v, const char *); - if (x == NULL) - break; - j = strlen(x); - i = ap_bwrite(fb, x, j); - if (i != j) { - va_end(v); - return -1; + int i, j, k; + va_list v; + const char *x; + + va_start(v, fb); + for (k = 0;;) { + x = va_arg(v, const char *); + if (x == NULL) + break; + j = strlen(x); + i = ap_bwrite(fb, x, j); + if (i != j) { + va_end(v); + return -1; + } + k += i; } - k += i; - } - va_end(v); + va_end(v); - return k; + return k; } -API_EXPORT(void) ap_bonerror(BUFF *fb, void (*error) (BUFF *, int, void *), - void *data) +API_EXPORT(void) +ap_bonerror(BUFF *fb, void (*error) (BUFF *, int, void *), void *data) { - fb->error = error; - fb->error_data = data; + fb->error = error; + fb->error_data = data; } struct bprintf_data { - ap_vformatter_buff vbuff; - BUFF *fb; + ap_vformatter_buff vbuff; + BUFF *fb; }; -static int bprintf_flush(ap_vformatter_buff *vbuff) +static int +bprintf_flush(ap_vformatter_buff *vbuff) { - struct bprintf_data *b = (struct bprintf_data *)vbuff; - BUFF *fb = b->fb; + struct bprintf_data *b = (struct bprintf_data *)vbuff; + BUFF *fb = b->fb; - fb->outcnt += b->vbuff.curpos - (char *)&fb->outbase[fb->outcnt]; - if (fb->outcnt == fb->bufsiz) { - if (ap_bflush(fb)) { - return -1; - } - } - vbuff->curpos = (char *)&fb->outbase[fb->outcnt]; - vbuff->endpos = (char *)&fb->outbase[fb->bufsiz]; - return 0; + fb->outcnt += b->vbuff.curpos - (char *)&fb->outbase[fb->outcnt]; + if (fb->outcnt == fb->bufsiz) + if (ap_bflush(fb)) + return -1; + + vbuff->curpos = (char *)&fb->outbase[fb->outcnt]; + vbuff->endpos = (char *)&fb->outbase[fb->bufsiz]; + return 0; } -API_EXPORT_NONSTD(int) ap_bprintf(BUFF *fb, const char *fmt, ...) +API_EXPORT_NONSTD(int) +ap_bprintf(BUFF *fb, const char *fmt, ...) { - va_list ap; - int res; - struct bprintf_data b; - - /* XXX: only works with buffered writes */ - if ((fb->flags & (B_WRERR | B_EOUT | B_WR)) != B_WR) - return -1; - b.vbuff.curpos = (char *)&fb->outbase[fb->outcnt]; - b.vbuff.endpos = (char *)&fb->outbase[fb->bufsiz]; - b.fb = fb; - va_start(ap, fmt); - res = ap_vformatter(bprintf_flush, &b.vbuff, fmt, ap); - va_end(ap); - if (res != -1) { - fb->outcnt += b.vbuff.curpos - (char *)&fb->outbase[fb->outcnt]; - } - return res; + va_list ap; + int res; + struct bprintf_data b; + + /* XXX: only works with buffered writes */ + if ((fb->flags & (B_WRERR | B_EOUT | B_WR)) != B_WR) + return -1; + b.vbuff.curpos = (char *)&fb->outbase[fb->outcnt]; + b.vbuff.endpos = (char *)&fb->outbase[fb->bufsiz]; + b.fb = fb; + va_start(ap, fmt); + res = ap_vformatter(bprintf_flush, &b.vbuff, fmt, ap); + va_end(ap); + if (res != -1) + fb->outcnt += b.vbuff.curpos - (char *)&fb->outbase[fb->outcnt]; + return res; } -API_EXPORT(int) ap_vbprintf(BUFF *fb, const char *fmt, va_list ap) +API_EXPORT(int) +ap_vbprintf(BUFF *fb, const char *fmt, va_list ap) { - struct bprintf_data b; - int res; - - /* XXX: only works with buffered writes */ - if ((fb->flags & (B_WRERR | B_EOUT | B_WR)) != B_WR) - return -1; - b.vbuff.curpos = (char *)&fb->outbase[fb->outcnt]; - b.vbuff.endpos = (char *)&fb->outbase[fb->bufsiz]; - b.fb = fb; - res = ap_vformatter(bprintf_flush, &b.vbuff, fmt, ap); - if (res != -1) { - fb->outcnt += b.vbuff.curpos - (char *)&fb->outbase[fb->outcnt]; - } - return res; + struct bprintf_data b; + int res; + + /* XXX: only works with buffered writes */ + if ((fb->flags & (B_WRERR | B_EOUT | B_WR)) != B_WR) + return -1; + b.vbuff.curpos = (char *)&fb->outbase[fb->outcnt]; + b.vbuff.endpos = (char *)&fb->outbase[fb->bufsiz]; + b.fb = fb; + res = ap_vformatter(bprintf_flush, &b.vbuff, fmt, ap); + if (res != -1) + fb->outcnt += b.vbuff.curpos - (char *)&fb->outbase[fb->outcnt]; + return res; } diff --git a/usr.sbin/httpd/src/main/gen_test_char.c b/usr.sbin/httpd/src/main/gen_test_char.c index 10d5360249c..51d2c4f3a56 100644 --- a/usr.sbin/httpd/src/main/gen_test_char.c +++ b/usr.sbin/httpd/src/main/gen_test_char.c @@ -10,67 +10,69 @@ #define T_HTTP_TOKEN_STOP (0x08) #define T_ESCAPE_LOGITEM (0x10) -int main(int argc, char *argv[]) +int +main(int argc, char *argv[]) { - unsigned c; - unsigned char flags; + unsigned c; + unsigned char flags; - printf( -"/* this file is automatically generated by gen_test_char, do not edit */\n" -"#define T_ESCAPE_SHELL_CMD 0x%02x /* chars with special meaning in the shell */\n" -"#define T_ESCAPE_PATH_SEGMENT 0x%02x /* find path segment, as defined in RFC1808 */\n" -"#define T_OS_ESCAPE_PATH 0x%02x /* escape characters in a path or uri */\n" -"#define T_HTTP_TOKEN_STOP 0x%02x /* find http tokens, as defined in RFC2616 */\n" -"#define T_ESCAPE_LOGITEM 0x%02x /* filter what should go in the log file */\n" -"\n", - T_ESCAPE_SHELL_CMD, - T_ESCAPE_PATH_SEGMENT, - T_OS_ESCAPE_PATH, - T_HTTP_TOKEN_STOP, - T_ESCAPE_LOGITEM - ); + printf( + "/* this file is automatically generated by gen_test_char, " + "do not edit */\n" + "#define T_ESCAPE_SHELL_CMD 0x%02x " + "/* chars with special meaning in the shell */\n" + "#define T_ESCAPE_PATH_SEGMENT 0x%02x " + "/* find path segment, as defined in RFC1808 */\n" + "#define T_OS_ESCAPE_PATH 0x%02x " + "/* escape characters in a path or uri */\n" + "#define T_HTTP_TOKEN_STOP 0x%02x " + "/* find http tokens, as defined in RFC2616 */\n" + "#define T_ESCAPE_LOGITEM 0x%02x " + "/* filter what should go in the log file */\n" + "\n", + T_ESCAPE_SHELL_CMD, + T_ESCAPE_PATH_SEGMENT, + T_OS_ESCAPE_PATH, + T_HTTP_TOKEN_STOP, + T_ESCAPE_LOGITEM); - /* we explicitly dealt with NUL above - * in case some strchr() do bogosity with it */ + /* we explicitly dealt with NUL above + * in case some strchr() do bogosity with it + */ - printf("static const unsigned char test_char_table[256] = {\n" - " 0x00, "); /* print initial item */ + printf("static const unsigned char test_char_table[256] = {\n" + " 0x00, "); /* print initial item */ - for (c = 1; c < 256; ++c) { - flags = 0; + for (c = 1; c < 256; ++c) { + flags = 0; - /* escape_shell_cmd */ - if (strchr("&;`'\"|*?~<>^()[]{}$\\\n", c)) { - flags |= T_ESCAPE_SHELL_CMD; - } + /* escape_shell_cmd */ + if (strchr("&;`'\"|*?~<>^()[]{}$\\\n", c)) + flags |= T_ESCAPE_SHELL_CMD; - if (!ap_isalnum(c) && !strchr("$-_.+!*'(),:@&=~", c)) { - flags |= T_ESCAPE_PATH_SEGMENT; - } + if (!ap_isalnum(c) && !strchr("$-_.+!*'(),:@&=~", c)) + flags |= T_ESCAPE_PATH_SEGMENT; - if (!ap_isalnum(c) && !strchr("$-_.+!*'(),:@&=/~", c)) { - flags |= T_OS_ESCAPE_PATH; - } + if (!ap_isalnum(c) && !strchr("$-_.+!*'(),:@&=/~", c)) + flags |= T_OS_ESCAPE_PATH; - /* these are the "tspecials" from RFC2068 */ - if (ap_iscntrl(c) || strchr(" \t()<>@,;:\\/[]?={}", c)) { - flags |= T_HTTP_TOKEN_STOP; - } + /* these are the "tspecials" from RFC2068 */ + if (ap_iscntrl(c) || strchr(" \t()<>@,;:\\/[]?={}", c)) + flags |= T_HTTP_TOKEN_STOP; - /* For logging, escape all control characters, - * double quotes (because they delimit the request in the log file) - * backslashes (because we use backslash for escaping) - * and 8-bit chars with the high bit set - */ - if (!ap_isprint(c) || c == '"' || c == '\\' || ap_iscntrl(c)) { - flags |= T_ESCAPE_LOGITEM; - } - printf("0x%02x%s", flags, (c < 255) ? ", " : " "); + /* For logging, escape all control characters, double quotes + * (because they delimit the request in the log file) + * backslashes (because we use backslash for escaping) + * and 8-bit chars with the high bit set + */ + if (!ap_isprint(c) || c == '"' || c == '\\' || ap_iscntrl(c)) + flags |= T_ESCAPE_LOGITEM; + printf("0x%02x%s", flags, (c < 255) ? ", " : " "); - if ((c % 8) == 7) - printf(" /*0x%02x...0x%02x*/\n ", c-7, c); - } - printf("\n};\n"); + if ((c % 8) == 7) + printf(" /*0x%02x...0x%02x*/\n ", c-7, c); + } + printf("\n};\n"); - return 0; + return 0; } diff --git a/usr.sbin/httpd/src/main/gen_uri_delims.c b/usr.sbin/httpd/src/main/gen_uri_delims.c index 15a73fbc7a4..c0a69d09d9a 100644 --- a/usr.sbin/httpd/src/main/gen_uri_delims.c +++ b/usr.sbin/httpd/src/main/gen_uri_delims.c @@ -4,28 +4,29 @@ * marked "interesting"... for the uri parsing process. */ -int main(int argc, char *argv[]) +int +main(int argc, char *argv[]) { - int i; - char *value; + int i; + char *value; - printf("/* this file is automatically generated by " + printf("/* this file is automatically generated by " "gen_uri_delims, do not edit */\n"); - printf("static const unsigned char uri_delims[256] = {"); - for (i = 0; i < 256; ++i) { - if (i % 20 == 0) - printf("\n "); - switch (i) { - case ':': value = "T_COLON"; break; - case '/': value = "T_SLASH"; break; - case '?': value = "T_QUESTION"; break; - case '#': value = "T_HASH"; break; - case '\0': value = "T_NUL"; break; - default: value = "0"; break; + printf("static const unsigned char uri_delims[256] = {"); + for (i = 0; i < 256; ++i) { + if (i % 20 == 0) + printf("\n "); + switch (i) { + case ':': value = "T_COLON"; break; + case '/': value = "T_SLASH"; break; + case '?': value = "T_QUESTION"; break; + case '#': value = "T_HASH"; break; + case '\0': value = "T_NUL"; break; + default: value = "0"; break; + } + printf("%s%c", value, (i < 255) ? ',' : ' '); } - printf("%s%c", value, (i < 255) ? ',' : ' '); - } - printf("\n};\n"); + printf("\n};\n"); - return 0; + return 0; } diff --git a/usr.sbin/httpd/src/main/rfc1413.c b/usr.sbin/httpd/src/main/rfc1413.c index b543e298d58..af647420eb9 100644 --- a/usr.sbin/httpd/src/main/rfc1413.c +++ b/usr.sbin/httpd/src/main/rfc1413.c @@ -103,178 +103,182 @@ int ap_rfc1413_timeout = RFC1413_TIMEOUT; /* Global so it can be changed */ static JMP_BUF timebuf; /* ident_timeout - handle timeouts */ -static void ident_timeout(int sig) +static void +ident_timeout(int sig) { - ap_longjmp(timebuf, sig); + ap_longjmp(timebuf, sig); } /* bind_connect - bind both ends of a socket */ /* Ambarish fix this. Very broken */ -static int get_rfc1413(int sock, const struct sockaddr *our_sin, - const struct sockaddr *rmt_sin, - char user[RFC1413_USERLEN+1], server_rec *srv) +static int +get_rfc1413(int sock, const struct sockaddr *our_sin, + const struct sockaddr *rmt_sin, char user[RFC1413_USERLEN+1], + server_rec *srv) { - struct sockaddr_storage rmt_query_sin, our_query_sin; - unsigned int o_rmt_port, o_our_port; /* original port pair */ - unsigned int rmt_port, our_port; /* replied port pair */ - int i; - char *cp; - char buffer[RFC1413_MAXDATA + 1]; - int buflen; + struct sockaddr_storage rmt_query_sin, our_query_sin; + unsigned int o_rmt_port, o_our_port; /* original port pair */ + unsigned int rmt_port, our_port; /* replied port pair */ + int i; + char *cp; + char buffer[RFC1413_MAXDATA + 1]; + int buflen; - /* - * Bind the local and remote ends of the query socket to the same - * IP addresses as the connection under investigation. We go - * through all this trouble because the local or remote system - * might have more than one network address. The RFC1413 etc. - * client sends only port numbers; the server takes the IP - * addresses from the query socket. - */ + /* + * Bind the local and remote ends of the query socket to the same + * IP addresses as the connection under investigation. We go + * through all this trouble because the local or remote system + * might have more than one network address. The RFC1413 etc. + * client sends only port numbers; the server takes the IP + * addresses from the query socket. + */ #ifndef SIN6_LEN - memcpy(&our_query_sin, our_sin, SA_LEN(our_sin)); - memcpy(&rmt_query_sin, rmt_sin, SA_LEN(rmt_sin)); + memcpy(&our_query_sin, our_sin, SA_LEN(our_sin)); + memcpy(&rmt_query_sin, rmt_sin, SA_LEN(rmt_sin)); #else - memcpy(&our_query_sin, our_sin, our_sin->sa_len); - memcpy(&rmt_query_sin, rmt_sin, rmt_sin->sa_len); + memcpy(&our_query_sin, our_sin, our_sin->sa_len); + memcpy(&rmt_query_sin, rmt_sin, rmt_sin->sa_len); #endif - switch (our_sin->sa_family) { - case AF_INET: - ((struct sockaddr_in *)&our_query_sin)->sin_port = htons(ANY_PORT); - o_our_port = ntohs(((struct sockaddr_in *)our_sin)->sin_port); - ((struct sockaddr_in *)&rmt_query_sin)->sin_port = htons(RFC1413_PORT); - o_rmt_port = ntohs(((struct sockaddr_in *)rmt_sin)->sin_port); - break; - case AF_INET6: - ((struct sockaddr_in6 *)&our_query_sin)->sin6_port = htons(ANY_PORT); - o_our_port = ntohs(((struct sockaddr_in6 *)our_sin)->sin6_port); - ((struct sockaddr_in6 *)&rmt_query_sin)->sin6_port = htons(RFC1413_PORT); - o_rmt_port = ntohs(((struct sockaddr_in6 *)rmt_sin)->sin6_port); - break; - default: - /* unsupported AF */ - return -1; - } + switch (our_sin->sa_family) { + case AF_INET: + ((struct sockaddr_in *)&our_query_sin)->sin_port = + htons(ANY_PORT); + o_our_port = ntohs(((struct sockaddr_in *)our_sin)->sin_port); + ((struct sockaddr_in *)&rmt_query_sin)->sin_port = + htons(RFC1413_PORT); + o_rmt_port = ntohs(((struct sockaddr_in *)rmt_sin)->sin_port); + break; + case AF_INET6: + ((struct sockaddr_in6 *)&our_query_sin)->sin6_port = + htons(ANY_PORT); + o_our_port = + ntohs(((struct sockaddr_in6 *)our_sin)->sin6_port); + ((struct sockaddr_in6 *)&rmt_query_sin)->sin6_port = + htons(RFC1413_PORT); + o_rmt_port = + ntohs(((struct sockaddr_in6 *)rmt_sin)->sin6_port); + break; + default: + /* unsupported AF */ + return -1; + } - if (bind(sock, (struct sockaddr *) &our_query_sin, + if (bind(sock, (struct sockaddr *) &our_query_sin, #ifndef SIN6_LEN - SA_LEN((struct sockaddr *) &our_query_sin) + SA_LEN((struct sockaddr *) &our_query_sin) #else - our_query_sin.ss_len + our_query_sin.ss_len #endif - ) < 0) { - ap_log_error(APLOG_MARK, APLOG_CRIT, srv, + ) < 0) { + ap_log_error(APLOG_MARK, APLOG_CRIT, srv, "bind: rfc1413: Error binding to local port"); - return -1; - } + return -1; + } -/* - * errors from connect usually imply the remote machine doesn't support - * the service - */ - if (connect(sock, (struct sockaddr *) &rmt_query_sin, + /* + * errors from connect usually imply the remote machine doesn't + * support the service + */ + if (connect(sock, (struct sockaddr *) &rmt_query_sin, #ifndef SIN6_LEN - SA_LEN((struct sockaddr *) &rmt_query_sin) + SA_LEN((struct sockaddr *) &rmt_query_sin) #else - rmt_query_sin.ss_len + rmt_query_sin.ss_len #endif - ) < 0) { - return -1; - } + ) < 0) + return -1; -/* send the data */ - buflen = ap_snprintf(buffer, sizeof(buffer), "%u,%u\r\n", o_rmt_port, - o_our_port); + /* send the data */ + buflen = ap_snprintf(buffer, sizeof(buffer), "%u,%u\r\n", o_rmt_port, + o_our_port); - /* send query to server. Handle short write. */ - i = 0; - while(i < (int)strlen(buffer)) { - int j; - j = write(sock, buffer+i, (strlen(buffer+i))); - if (j < 0 && errno != EINTR) { - ap_log_error(APLOG_MARK, APLOG_CRIT, srv, - "write: rfc1413: error sending request"); - return -1; + /* send query to server. Handle short write. */ + i = 0; + while(i < (int)strlen(buffer)) { + int j; + j = write(sock, buffer+i, (strlen(buffer+i))); + if (j < 0 && errno != EINTR) { + ap_log_error(APLOG_MARK, APLOG_CRIT, srv, + "write: rfc1413: error sending request"); + return -1; + } else if (j > 0) + i+=j; } - else if (j > 0) { - i+=j; - } - } - /* - * Read response from server. - the response should be newline - * terminated according to rfc - make sure it doesn't stomp it's - * way out of the buffer. - */ + /* + * Read response from server. - the response should be newline + * terminated according to rfc - make sure it doesn't stomp it's + * way out of the buffer. + */ + i = 0; + memset(buffer, '\0', sizeof(buffer)); + /* + * Note that the strchr function below checks for \012 instead of '\n' + * this allows it to work on both ASCII and EBCDIC machines. + */ + while((cp = strchr(buffer, '\012')) == NULL && + i < sizeof(buffer) - 1) { + int j; - i = 0; - memset(buffer, '\0', sizeof(buffer)); - /* - * Note that the strchr function below checks for \012 instead of '\n' - * this allows it to work on both ASCII and EBCDIC machines. - */ - while((cp = strchr(buffer, '\012')) == NULL && i < sizeof(buffer) - 1) { - int j; - - j = read(sock, buffer+i, (sizeof(buffer) - 1) - i); - if (j < 0 && errno != EINTR) { - ap_log_error(APLOG_MARK, APLOG_CRIT, srv, - "read: rfc1413: error reading response"); - return -1; - } - else if (j > 0) { - i+=j; + j = read(sock, buffer+i, (sizeof(buffer) - 1) - i); + if (j < 0 && errno != EINTR) { + ap_log_error(APLOG_MARK, APLOG_CRIT, srv, + "read: rfc1413: error reading response"); + return -1; + } + else if (j > 0) + i+=j; } - } -/* RFC1413_USERLEN = 512 */ - if (sscanf(buffer, "%u , %u : USERID :%*[^:]:%512s", &rmt_port, &our_port, - user) != 3 || o_rmt_port != rmt_port || o_our_port != our_port) { - return -1; - } + /* RFC1413_USERLEN = 512 */ + if (sscanf(buffer, "%u , %u : USERID :%*[^:]:%512s", &rmt_port, + &our_port, user) != 3 || o_rmt_port != rmt_port || + o_our_port != our_port) + return -1; - /* - * Strip trailing carriage return. It is part of the - * protocol, not part of the data. - */ + /* + * Strip trailing carriage return. It is part of the + * protocol, not part of the data. + */ + if ((cp = strchr(user, '\r'))) + *cp = '\0'; - if ((cp = strchr(user, '\r'))) - *cp = '\0'; - - return 0; + return 0; } /* rfc1413 - return remote user name, given socket structures */ -API_EXPORT(char *) ap_rfc1413(conn_rec *conn, server_rec *srv) +API_EXPORT(char *) +ap_rfc1413(conn_rec *conn, server_rec *srv) { - RFC_USER_STATIC char user[RFC1413_USERLEN + 1]; /* XXX */ - RFC_USER_STATIC char *result; - RFC_USER_STATIC int sock; + RFC_USER_STATIC char user[RFC1413_USERLEN + 1]; /* XXX */ + RFC_USER_STATIC char *result; + RFC_USER_STATIC int sock; - result = FROM_UNKNOWN; + result = FROM_UNKNOWN; - sock = ap_psocket_ex(conn->pool, conn->remote_addr.ss_family, SOCK_STREAM, IPPROTO_TCP, 1); - if (sock < 0) { - ap_log_error(APLOG_MARK, APLOG_CRIT, srv, - "socket: rfc1413: error creating socket"); - conn->remote_logname = result; - } + sock = ap_psocket_ex(conn->pool, conn->remote_addr.ss_family, + SOCK_STREAM, IPPROTO_TCP, 1); + if (sock < 0) { + ap_log_error(APLOG_MARK, APLOG_CRIT, srv, + "socket: rfc1413: error creating socket"); + conn->remote_logname = result; + } - /* - * Set up a timer so we won't get stuck while waiting for the server. - */ - if (ap_setjmp(timebuf) == 0) { - ap_set_callback_and_alarm(ident_timeout, ap_rfc1413_timeout); + /* + * Set up a timer so we won't get stuck while waiting for the server. + */ + if (ap_setjmp(timebuf) == 0) { + ap_set_callback_and_alarm(ident_timeout, ap_rfc1413_timeout); - if (get_rfc1413(sock, (struct sockaddr *)&conn->local_addr, - (struct sockaddr *)&conn->remote_addr, user, srv) >= 0) { - result = user; + if (get_rfc1413(sock, (struct sockaddr *)&conn->local_addr, + (struct sockaddr *)&conn->remote_addr, user, srv) >= 0) + result = user; } - } - ap_set_callback_and_alarm(NULL, 0); - ap_pclosesocket(conn->pool, sock); - conn->remote_logname = result; + ap_set_callback_and_alarm(NULL, 0); + ap_pclosesocket(conn->pool, sock); + conn->remote_logname = result; - return conn->remote_logname; + return conn->remote_logname; } diff --git a/usr.sbin/httpd/src/main/util.c b/usr.sbin/httpd/src/main/util.c index a473eee9f74..e07a393f8c7 100644 --- a/usr.sbin/httpd/src/main/util.c +++ b/usr.sbin/httpd/src/main/util.c @@ -86,125 +86,132 @@ */ #define TEST_CHAR(c, f) (test_char_table[(unsigned)(c)] & (f)) -void ap_util_init(void) +void +ap_util_init(void) { - /* nothing to do... previously there was run-time initialization of - * test_char_table here - */ + /* nothing to do... previously there was run-time initialization of + * test_char_table here + */ } API_VAR_EXPORT const char ap_month_snames[12][4] = { - "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" + "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", + "Aug", "Sep", "Oct", "Nov", "Dec" }; API_VAR_EXPORT const char ap_day_snames[7][4] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; -API_EXPORT(char *) ap_get_time(void) +API_EXPORT(char *) +ap_get_time(void) { - time_t t; - char *time_string; + time_t t; + char *time_string; - t = time(NULL); - time_string = ctime(&t); - time_string[strlen(time_string) - 1] = '\0'; - return (time_string); + t = time(NULL); + time_string = ctime(&t); + time_string[strlen(time_string) - 1] = '\0'; + return (time_string); } /* * Examine a field value (such as a media-/content-type) string and return * it sans any parameters; e.g., strip off any ';charset=foo' and the like. */ -API_EXPORT(char *) ap_field_noparam(pool *p, const char *intype) +API_EXPORT(char *) +ap_field_noparam(pool *p, const char *intype) { - const char *semi; + const char *semi; - if (intype == NULL) return NULL; + if (intype == NULL) return NULL; - semi = strchr(intype, ';'); - if (semi == NULL) { - return ap_pstrdup(p, intype); - } - else { - while ((semi > intype) && ap_isspace(semi[-1])) { - semi--; + semi = strchr(intype, ';'); + if (semi == NULL) + return ap_pstrdup(p, intype); + else { + while ((semi > intype) && ap_isspace(semi[-1])) + semi--; + + return ap_pstrndup(p, intype, semi - intype); } - return ap_pstrndup(p, intype, semi - intype); - } } -API_EXPORT(char *) ap_ht_time(pool *p, time_t t, const char *fmt, int gmt) +API_EXPORT(char *) +ap_ht_time(pool *p, time_t t, const char *fmt, int gmt) { - char ts[MAX_STRING_LEN]; - char tf[MAX_STRING_LEN]; - struct tm *tms; + char ts[MAX_STRING_LEN]; + char tf[MAX_STRING_LEN]; + struct tm *tms; - tms = (gmt ? gmtime(&t) : localtime(&t)); - if(gmt) { - /* Convert %Z to "GMT" and %z to "+0000"; - * on hosts that do not have a time zone string in struct tm, - * strftime must assume its argument is local time. - */ - const char *f; - char *strp; - for(strp = tf, f = fmt; strp < tf + sizeof(tf) - 6 && (*strp = *f) - ; f++, strp++) { - if (*f != '%') continue; - switch (f[1]) { - case '%': - *++strp = *++f; - break; - case 'Z': - *strp++ = 'G'; - *strp++ = 'M'; - *strp = 'T'; - f++; - break; - case 'z': /* common extension */ - *strp++ = '+'; - *strp++ = '0'; - *strp++ = '0'; - *strp++ = '0'; - *strp = '0'; - f++; - break; - } + tms = (gmt ? gmtime(&t) : localtime(&t)); + if(gmt) { + /* Convert %Z to "GMT" and %z to "+0000"; + * on hosts that do not have a time zone string in struct tm, + * strftime must assume its argument is local time. + */ + const char *f; + char *strp; + for(strp = tf, f = fmt; strp < tf + sizeof(tf) - 6 + && (*strp = *f); f++, strp++) { + if (*f != '%') + continue; + switch (f[1]) { + case '%': + *++strp = *++f; + break; + case 'Z': + *strp++ = 'G'; + *strp++ = 'M'; + *strp = 'T'; + f++; + break; + case 'z': /* common extension */ + *strp++ = '+'; + *strp++ = '0'; + *strp++ = '0'; + *strp++ = '0'; + *strp = '0'; + f++; + break; + } + } + *strp = '\0'; + fmt = tf; } - *strp = '\0'; - fmt = tf; - } - /* check return code? */ - strftime(ts, MAX_STRING_LEN, fmt, tms); - ts[MAX_STRING_LEN - 1] = '\0'; - return ap_pstrdup(p, ts); + /* check return code? */ + strftime(ts, MAX_STRING_LEN, fmt, tms); + ts[MAX_STRING_LEN - 1] = '\0'; + return ap_pstrdup(p, ts); } -API_EXPORT(char *) ap_gm_timestr_822(pool *p, time_t sec) +API_EXPORT(char *) +ap_gm_timestr_822(pool *p, time_t sec) { - struct tm *tms; + struct tm *tms; - tms = gmtime(&sec); + tms = gmtime(&sec); - /* RFC date format; as strftime '%a, %d %b %Y %T GMT' */ - return ap_psprintf(p, - "%s, %.2d %s %d %.2d:%.2d:%.2d GMT", ap_day_snames[tms->tm_wday], - tms->tm_mday, ap_month_snames[tms->tm_mon], tms->tm_year + 1900, - tms->tm_hour, tms->tm_min, tms->tm_sec); + /* RFC date format; as strftime '%a, %d %b %Y %T GMT' */ + return ap_psprintf(p, "%s, %.2d %s %d %.2d:%.2d:%.2d GMT", + ap_day_snames[tms->tm_wday], tms->tm_mday, + ap_month_snames[tms->tm_mon], tms->tm_year + 1900, + tms->tm_hour, tms->tm_min, tms->tm_sec); } /* What a pain in the ass. */ -API_EXPORT(struct tm *) ap_get_gmtoff(int *tz) +API_EXPORT(struct tm *) +ap_get_gmtoff(int *tz) { - time_t tt = time(NULL); - struct tm *t; + time_t tt = time(NULL); + struct tm *t; - t = localtime(&tt); - *tz = (int) (t->tm_gmtoff / 60); - return t; + t = localtime(&tt); + *tz = (int)(t->tm_gmtoff / 60); + return t; } /* Roy owes Rob beer. */ @@ -220,95 +227,103 @@ API_EXPORT(struct tm *) ap_get_gmtoff(int *tz) * Based loosely on sections of wildmat.c by Rich Salz * Hmmm... shouldn't this really go component by component? */ -API_EXPORT(int) ap_strcmp_match(const char *str, const char *exp) -{ - int x, y; - - for (x = 0, y = 0; exp[y]; ++y, ++x) { - if ((!str[x]) && (exp[y] != '*')) - return -1; - if (exp[y] == '*') { - while (exp[++y] == '*'); - if (!exp[y]) - return 0; - while (str[x]) { - int ret; - if ((ret = ap_strcmp_match(&str[x++], &exp[y])) != 1) - return ret; - } - return -1; +API_EXPORT(int) +ap_strcmp_match(const char *str, const char *exp) +{ + int x, y; + + for (x = 0, y = 0; exp[y]; ++y, ++x) { + if ((!str[x]) && (exp[y] != '*')) + return -1; + if (exp[y] == '*') { + while (exp[++y] == '*'); + if (!exp[y]) + return 0; + while (str[x]) { + int ret; + if ((ret = ap_strcmp_match(&str[x++], + &exp[y])) != 1) + return ret; + } + return -1; + } else if ((exp[y] != '?') && (str[x] != exp[y])) + return 1; } - else if ((exp[y] != '?') && (str[x] != exp[y])) - return 1; - } - return (str[x] != '\0'); -} - -API_EXPORT(int) ap_strcasecmp_match(const char *str, const char *exp) -{ - int x, y; - - for (x = 0, y = 0; exp[y]; ++y, ++x) { - if ((!str[x]) && (exp[y] != '*')) - return -1; - if (exp[y] == '*') { - while (exp[++y] == '*'); - if (!exp[y]) - return 0; - while (str[x]) { - int ret; - if ((ret = ap_strcasecmp_match(&str[x++], &exp[y])) != 1) - return ret; - } - return -1; + return (str[x] != '\0'); +} + +API_EXPORT(int) +ap_strcasecmp_match(const char *str, const char *exp) +{ + int x, y; + + for (x = 0, y = 0; exp[y]; ++y, ++x) { + if ((!str[x]) && (exp[y] != '*')) + return -1; + if (exp[y] == '*') { + while (exp[++y] == '*'); + if (!exp[y]) + return 0; + while (str[x]) { + int ret; + if ((ret = ap_strcasecmp_match(&str[x++], + &exp[y])) != 1) + return ret; + } + return -1; + } else if ((exp[y] != '?') && + (ap_tolower(str[x]) != ap_tolower(exp[y]))) + return 1; } - else if ((exp[y] != '?') && (ap_tolower(str[x]) != ap_tolower(exp[y]))) - return 1; - } - return (str[x] != '\0'); + return (str[x] != '\0'); } -API_EXPORT(int) ap_is_matchexp(const char *str) +API_EXPORT(int) +ap_is_matchexp(const char *str) { - register int x; + register int x; - for (x = 0; str[x]; x++) - if ((str[x] == '*') || (str[x] == '?')) - return 1; - return 0; + for (x = 0; str[x]; x++) + if ((str[x] == '*') || (str[x] == '?')) + return 1; + return 0; } /* * Similar to standard strstr() but we ignore case in this version. * Based on the strstr() implementation further below. */ -API_EXPORT(char *) ap_strcasestr(const char *s1, const char *s2) +API_EXPORT(char *) +ap_strcasestr(const char *s1, const char *s2) { - char *p1, *p2; - if (*s2 == '\0') { - /* an empty s2 */ - return((char *)s1); - } - while(1) { - for ( ; (*s1 != '\0') && (ap_tolower(*s1) != ap_tolower(*s2)); s1++); - if (*s1 == '\0') return(NULL); - /* found first character of s2, see if the rest matches */ - p1 = (char *)s1; - p2 = (char *)s2; - while (ap_tolower(*++p1) == ap_tolower(*++p2)) { - if (*p1 == '\0') { - /* both strings ended together */ - return((char *)s1); - } - } - if (*p2 == '\0') { - /* second string ended, a match */ - break; - } - /* didn't find a match here, try starting at next character in s1 */ - s1++; - } - return((char *)s1); + char *p1, *p2; + if (*s2 == '\0') { + /* an empty s2 */ + return((char *)s1); + } + while(1) { + for ( ; (*s1 != '\0') && (ap_tolower(*s1) != ap_tolower(*s2)); + s1++); + if (*s1 == '\0') + return(NULL); + /* found first character of s2, see if the rest matches */ + p1 = (char *)s1; + p2 = (char *)s2; + while (ap_tolower(*++p1) == ap_tolower(*++p2)) { + if (*p1 == '\0') + /* both strings ended together */ + return((char *)s1); + } + if (*p2 == '\0') + /* second string ended, a match */ + break; + + /* didn't find a match here, try starting at next character + * in s1 + */ + s1++; + } + return((char *)s1); } /* @@ -319,21 +334,22 @@ API_EXPORT(char *) ap_strcasestr(const char *s1, const char *s2) * can use standard pointer comparisons in the calling function * (eg: test if ap_stripprefix(a,b) == a) */ -API_EXPORT(char *) ap_stripprefix(const char *bigstring, const char *prefix) +API_EXPORT(char *) +ap_stripprefix(const char *bigstring, const char *prefix) { - char *p1; - if (*prefix == '\0') { - return( (char *)bigstring); - } - p1 = (char *)bigstring; - while(*p1 && *prefix) { - if (*p1++ != *prefix++) - return( (char *)bigstring); - } - if (*prefix == '\0') - return(p1); - else /* hit the end of bigstring! */ - return( (char *)bigstring); + char *p1; + if (*prefix == '\0') + return( (char *)bigstring); + + p1 = (char *)bigstring; + while(*p1 && *prefix) + if (*p1++ != *prefix++) + return( (char *)bigstring); + + if (*prefix == '\0') + return(p1); + else /* hit the end of bigstring! */ + return((char *)bigstring); } /* @@ -342,15 +358,17 @@ API_EXPORT(char *) ap_stripprefix(const char *bigstring, const char *prefix) * This is especially important for the DSO situations of modules. * DO NOT MAKE A MACRO OUT OF THIS FUNCTION! */ -API_EXPORT(int) ap_regexec(const regex_t *preg, const char *string, - size_t nmatch, regmatch_t pmatch[], int eflags) +API_EXPORT(int) +ap_regexec(const regex_t *preg, const char *string, size_t nmatch, + regmatch_t pmatch[], int eflags) { - return regexec(preg, string, nmatch, pmatch, eflags); + return regexec(preg, string, nmatch, pmatch, eflags); } -API_EXPORT(size_t) ap_regerror(int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size) +API_EXPORT(size_t) +ap_regerror(int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size) { - return regerror(errcode, preg, errbuf, errbuf_size); + return regerror(errcode, preg, errbuf, errbuf_size); } @@ -368,72 +386,68 @@ API_EXPORT(size_t) ap_regerror(int errcode, const regex_t *preg, char *errbuf, s * AT&T V8 regexp package. */ -API_EXPORT(char *) ap_pregsub(pool *p, const char *input, const char *source, - size_t nmatch, regmatch_t pmatch[]) +API_EXPORT(char *) +ap_pregsub(pool *p, const char *input, const char *source, size_t nmatch, +regmatch_t pmatch[]) { - const char *src = input; - char *dest, *dst; - char c; - size_t no; - int len; + const char *src = input; + char *dest, *dst; + char c; + size_t no; + int len; - if (!source) + if (!source) return NULL; - if (!nmatch) + if (!nmatch) return ap_pstrdup(p, src); - /* First pass, find the size */ + /* First pass, find the size */ - len = 0; + len = 0; - while ((c = *src++) != '\0') { - if (c == '&') - no = 0; - else if (c == '$' && ap_isdigit(*src)) - no = *src++ - '0'; - else - no = 10; + while ((c = *src++) != '\0') { + if (c == '&') + no = 0; + else if (c == '$' && ap_isdigit(*src)) + no = *src++ - '0'; + else + no = 10; - if (no > 9) { /* Ordinary character. */ - if (c == '\\' && (*src == '$' || *src == '&')) - c = *src++; - len++; + if (no > 9) { /* Ordinary character. */ + if (c == '\\' && (*src == '$' || *src == '&')) + c = *src++; + len++; + } else if (no < nmatch && pmatch[no].rm_so < pmatch[no].rm_eo) + len += pmatch[no].rm_eo - pmatch[no].rm_so; } - else if (no < nmatch && pmatch[no].rm_so < pmatch[no].rm_eo) { - len += pmatch[no].rm_eo - pmatch[no].rm_so; - } - - } - - dest = dst = ap_pcalloc(p, len + 1); - - /* Now actually fill in the string */ - - src = input; - - while ((c = *src++) != '\0') { - if (c == '&') - no = 0; - else if (c == '$' && ap_isdigit(*src)) - no = *src++ - '0'; - else - no = 10; - if (no > 9) { /* Ordinary character. */ - if (c == '\\' && (*src == '$' || *src == '&')) - c = *src++; - *dst++ = c; - } - else if (no < nmatch && pmatch[no].rm_so < pmatch[no].rm_eo) { - len = pmatch[no].rm_eo - pmatch[no].rm_so; - memcpy(dst, source + pmatch[no].rm_so, len); - dst += len; + dest = dst = ap_pcalloc(p, len + 1); + + /* Now actually fill in the string */ + src = input; + + while ((c = *src++) != '\0') { + if (c == '&') + no = 0; + else if (c == '$' && ap_isdigit(*src)) + no = *src++ - '0'; + else + no = 10; + + if (no > 9) { /* Ordinary character. */ + if (c == '\\' && (*src == '$' || *src == '&')) + c = *src++; + *dst++ = c; + } else if (no < nmatch && pmatch[no].rm_so < + pmatch[no].rm_eo) { + len = pmatch[no].rm_eo - pmatch[no].rm_so; + memcpy(dst, source + pmatch[no].rm_so, len); + dst += len; + } } + *dst = '\0'; - } - *dst = '\0'; - - return dest; + return dest; } /* @@ -2124,12 +2138,13 @@ API_EXPORT(char *) ap_escape_quotes (pool *p, const char *instring) /* dest = src with whitespace removed * length of dest assumed >= length of src */ -API_EXPORT(void) ap_remove_spaces(char *dest, char *src) +API_EXPORT(void) +ap_remove_spaces(char *dest, char *src) { - while (*src) { - if (!ap_isspace(*src)) - *dest++ = *src; - src++; - } - *dest = 0; + while (*src) { + if (!ap_isspace(*src)) + *dest++ = *src; + src++; + } + *dest = 0; } diff --git a/usr.sbin/httpd/src/main/util_date.c b/usr.sbin/httpd/src/main/util_date.c index f237279864e..4b08c47b07d 100644 --- a/usr.sbin/httpd/src/main/util_date.c +++ b/usr.sbin/httpd/src/main/util_date.c @@ -83,47 +83,46 @@ * * - swallow remaining characters * <x> - exact match for any other character */ -API_EXPORT(int) ap_checkmask(const char *data, const char *mask) +API_EXPORT(int) +ap_checkmask(const char *data, const char *mask) { - int i; - char d; - - for (i = 0; i < 256; i++) { - d = data[i]; - switch (mask[i]) { - case '\0': - return (d == '\0'); - - case '*': - return 1; - - case '@': - if (!ap_isupper(d)) - return 0; - break; - case '$': - if (!ap_islower(d)) - return 0; - break; - case '#': - if (!ap_isdigit(d)) - return 0; - break; - case '&': - if (!ap_isxdigit(d)) - return 0; - break; - case '~': - if ((d != ' ') && !ap_isdigit(d)) - return 0; - break; - default: - if (mask[i] != d) - return 0; - break; + int i; + char d; + + for (i = 0; i < 256; i++) { + d = data[i]; + switch (mask[i]) { + case '\0': + return (d == '\0'); + case '*': + return 1; + case '@': + if (!ap_isupper(d)) + return 0; + break; + case '$': + if (!ap_islower(d)) + return 0; + break; + case '#': + if (!ap_isdigit(d)) + return 0; + break; + case '&': + if (!ap_isxdigit(d)) + return 0; + break; + case '~': + if ((d != ' ') && !ap_isdigit(d)) + return 0; + break; + default: + if (mask[i] != d) + return 0; + break; + } } - } - return 0; /* We only get here if mask is corrupted (exceeds 256) */ + return 0; /* We only get here if mask is corrupted (exceeds 256) */ } /* @@ -137,35 +136,36 @@ API_EXPORT(int) ap_checkmask(const char *data, const char *mask) * * This routine is intended to be very fast, much faster than mktime(). */ -API_EXPORT(time_t) ap_tm2sec(const struct tm * t) +API_EXPORT(time_t) +ap_tm2sec(const struct tm * t) { - int year; - time_t days; - static const int dayoffset[12] = - {306, 337, 0, 31, 61, 92, 122, 153, 184, 214, 245, 275}; + int year; + time_t days; + static const int dayoffset[12] = + { 306, 337, 0, 31, 61, 92, 122, 153, 184, 214, 245, 275 }; - year = t->tm_year; + year = t->tm_year; - if (year < 70 || ((sizeof(time_t) <= 4) && (year >= 138))) - return BAD_DATE; + if (year < 70 || ((sizeof(time_t) <= 4) && (year >= 138))) + return BAD_DATE; - /* shift new year to 1st March in order to make leap year calc easy */ + /* shift new year to 1st March in order to make leap year calc easy */ - if (t->tm_mon < 2) - year--; + if (t->tm_mon < 2) + year--; - /* Find number of days since 1st March 1900 (in the Gregorian calendar). */ + /* Find number of days since 1st March 1900 (in the Gregorian calendar). */ - days = year * 365 + year / 4 - year / 100 + (year / 100 + 3) / 4; - days += dayoffset[t->tm_mon] + t->tm_mday - 1; - days -= 25508; /* 1 jan 1970 is 25508 days since 1 mar 1900 */ + days = year * 365 + year / 4 - year / 100 + (year / 100 + 3) / 4; + days += dayoffset[t->tm_mon] + t->tm_mday - 1; + days -= 25508; /* 1 jan 1970 is 25508 days since 1 mar 1900 */ - days = ((days * 24 + t->tm_hour) * 60 + t->tm_min) * 60 + t->tm_sec; + days = ((days * 24 + t->tm_hour) * 60 + t->tm_min) * 60 + t->tm_sec; - if (days < 0) - return BAD_DATE; /* must have overflowed */ - else - return days; /* must be a valid time */ + if (days < 0) + return BAD_DATE; /* must have overflowed */ + else + return days; /* must be a valid time */ } /* @@ -215,108 +215,110 @@ API_EXPORT(time_t) ap_tm2sec(const struct tm * t) * but many changes since then. * */ -API_EXPORT(time_t) ap_parseHTTPdate(const char *date) +API_EXPORT(time_t) +ap_parseHTTPdate(const char *date) { - struct tm ds; - int mint, mon; - const char *monstr, *timstr; - static const int months[12] = - { - ('J' << 16) | ('a' << 8) | 'n', ('F' << 16) | ('e' << 8) | 'b', - ('M' << 16) | ('a' << 8) | 'r', ('A' << 16) | ('p' << 8) | 'r', - ('M' << 16) | ('a' << 8) | 'y', ('J' << 16) | ('u' << 8) | 'n', - ('J' << 16) | ('u' << 8) | 'l', ('A' << 16) | ('u' << 8) | 'g', - ('S' << 16) | ('e' << 8) | 'p', ('O' << 16) | ('c' << 8) | 't', - ('N' << 16) | ('o' << 8) | 'v', ('D' << 16) | ('e' << 8) | 'c'}; - - if (!date) - return BAD_DATE; - - while (ap_isspace(*date)) /* Find first non-whitespace char */ - ++date; - - if (*date == '\0') - return BAD_DATE; - - if ((date = strchr(date, ' ')) == NULL) /* Find space after weekday */ - return BAD_DATE; - - ++date; /* Now pointing to first char after space, which should be */ - /* start of the actual date information for all 3 formats. */ - - if (ap_checkmask(date, "## @$$ #### ##:##:## *")) { /* RFC 1123 format */ - ds.tm_year = ((date[7] - '0') * 10 + (date[8] - '0') - 19) * 100; - if (ds.tm_year < 0) - return BAD_DATE; - - ds.tm_year += ((date[9] - '0') * 10) + (date[10] - '0'); - - ds.tm_mday = ((date[0] - '0') * 10) + (date[1] - '0'); - - monstr = date + 3; - timstr = date + 12; - } - else if (ap_checkmask(date, "##-@$$-## ##:##:## *")) { /* RFC 850 format */ - ds.tm_year = ((date[7] - '0') * 10) + (date[8] - '0'); - if (ds.tm_year < 70) - ds.tm_year += 100; - - ds.tm_mday = ((date[0] - '0') * 10) + (date[1] - '0'); - - monstr = date + 3; - timstr = date + 10; - } - else if (ap_checkmask(date, "@$$ ~# ##:##:## ####*")) { /* asctime format */ - ds.tm_year = ((date[16] - '0') * 10 + (date[17] - '0') - 19) * 100; - if (ds.tm_year < 0) - return BAD_DATE; - - ds.tm_year += ((date[18] - '0') * 10) + (date[19] - '0'); - - if (date[4] == ' ') - ds.tm_mday = 0; - else - ds.tm_mday = (date[4] - '0') * 10; - - ds.tm_mday += (date[5] - '0'); - - monstr = date; - timstr = date + 7; - } - else - return BAD_DATE; - - if (ds.tm_mday <= 0 || ds.tm_mday > 31) - return BAD_DATE; - - ds.tm_hour = ((timstr[0] - '0') * 10) + (timstr[1] - '0'); - ds.tm_min = ((timstr[3] - '0') * 10) + (timstr[4] - '0'); - ds.tm_sec = ((timstr[6] - '0') * 10) + (timstr[7] - '0'); - - if ((ds.tm_hour > 23) || (ds.tm_min > 59) || (ds.tm_sec > 61)) - return BAD_DATE; - - mint = (monstr[0] << 16) | (monstr[1] << 8) | monstr[2]; - for (mon = 0; mon < 12; mon++) - if (mint == months[mon]) - break; - if (mon == 12) - return BAD_DATE; - - if ((ds.tm_mday == 31) && (mon == 3 || mon == 5 || mon == 8 || mon == 10)) - return BAD_DATE; - - /* February gets special check for leapyear */ - - if ((mon == 1) && - ((ds.tm_mday > 29) - || ((ds.tm_mday == 29) - && ((ds.tm_year & 3) - || (((ds.tm_year % 100) == 0) - && (((ds.tm_year % 400) != 100))))))) - return BAD_DATE; - - ds.tm_mon = mon; - - return ap_tm2sec(&ds); + struct tm ds; + int mint, mon; + const char *monstr, *timstr; + static const int months[12] = { + ('J' << 16) | ('a' << 8) | 'n', ('F' << 16) | ('e' << 8) | 'b', + ('M' << 16) | ('a' << 8) | 'r', ('A' << 16) | ('p' << 8) | 'r', + ('M' << 16) | ('a' << 8) | 'y', ('J' << 16) | ('u' << 8) | 'n', + ('J' << 16) | ('u' << 8) | 'l', ('A' << 16) | ('u' << 8) | 'g', + ('S' << 16) | ('e' << 8) | 'p', ('O' << 16) | ('c' << 8) | 't', + ('N' << 16) | ('o' << 8) | 'v', ('D' << 16) | ('e' << 8) | 'c'}; + + if (!date) + return BAD_DATE; + + while (ap_isspace(*date)) /* Find first non-whitespace char */ + ++date; + + if (*date == '\0') + return BAD_DATE; + + if ((date = strchr(date, ' ')) == NULL) /* Find space after weekday */ + return BAD_DATE; + + ++date; /* Now pointing to first char after space, which should be + * start of the actual date information for all 3 formats. + */ + + if (ap_checkmask(date, "## @$$ #### ##:##:## *")) { + /* RFC 1123 format */ + ds.tm_year = ((date[7] - '0') * 10 + (date[8] - '0') - 19) * + 100; + if (ds.tm_year < 0) + return BAD_DATE; + + ds.tm_year += ((date[9] - '0') * 10) + (date[10] - '0'); + ds.tm_mday = ((date[0] - '0') * 10) + (date[1] - '0'); + + monstr = date + 3; + timstr = date + 12; + } else if (ap_checkmask(date, "##-@$$-## ##:##:## *")) { + /* RFC 850 format */ + ds.tm_year = ((date[7] - '0') * 10) + (date[8] - '0'); + if (ds.tm_year < 70) + ds.tm_year += 100; + + ds.tm_mday = ((date[0] - '0') * 10) + (date[1] - '0'); + + monstr = date + 3; + timstr = date + 10; + } else if (ap_checkmask(date, "@$$ ~# ##:##:## ####*")) { + /* asctime format */ + ds.tm_year = ((date[16] - '0') * 10 + (date[17] - '0') - 19) * + 100; + if (ds.tm_year < 0) + return BAD_DATE; + + ds.tm_year += ((date[18] - '0') * 10) + (date[19] - '0'); + + if (date[4] == ' ') + ds.tm_mday = 0; + else + ds.tm_mday = (date[4] - '0') * 10; + + ds.tm_mday += (date[5] - '0'); + + monstr = date; + timstr = date + 7; + } else + return BAD_DATE; + + if (ds.tm_mday <= 0 || ds.tm_mday > 31) + return BAD_DATE; + + ds.tm_hour = ((timstr[0] - '0') * 10) + (timstr[1] - '0'); + ds.tm_min = ((timstr[3] - '0') * 10) + (timstr[4] - '0'); + ds.tm_sec = ((timstr[6] - '0') * 10) + (timstr[7] - '0'); + + if ((ds.tm_hour > 23) || (ds.tm_min > 59) || (ds.tm_sec > 61)) + return BAD_DATE; + + mint = (monstr[0] << 16) | (monstr[1] << 8) | monstr[2]; + for (mon = 0; mon < 12; mon++) + if (mint == months[mon]) + break; + if (mon == 12) + return BAD_DATE; + + if ((ds.tm_mday == 31) && (mon == 3 || mon == 5 || mon == 8 || + mon == 10)) + return BAD_DATE; + + /* February gets special check for leapyear */ + if ((mon == 1) && + ((ds.tm_mday > 29) + || ((ds.tm_mday == 29) + && ((ds.tm_year & 3) + || (((ds.tm_year % 100) == 0) + && (((ds.tm_year % 400) != 100))))))) + return BAD_DATE; + + ds.tm_mon = mon; + + return ap_tm2sec(&ds); } diff --git a/usr.sbin/httpd/src/main/util_md5.c b/usr.sbin/httpd/src/main/util_md5.c index 738231c8af8..6d3dcaa0fad 100644 --- a/usr.sbin/httpd/src/main/util_md5.c +++ b/usr.sbin/httpd/src/main/util_md5.c @@ -88,34 +88,35 @@ #include "httpd.h" #include "util_md5.h" -API_EXPORT(char *) ap_md5_binary(pool *p, const unsigned char *buf, int length) +API_EXPORT(char *) +ap_md5_binary(pool *p, const unsigned char *buf, int length) { - const char *hex = "0123456789abcdef"; - AP_MD5_CTX my_md5; - unsigned char hash[16]; - char *r, result[33]; - int i; - - /* - * Take the MD5 hash of the string argument. - */ - - ap_MD5Init(&my_md5); - ap_MD5Update(&my_md5, buf, (unsigned int)length); - ap_MD5Final(hash, &my_md5); - - for (i = 0, r = result; i < 16; i++) { - *r++ = hex[hash[i] >> 4]; - *r++ = hex[hash[i] & 0xF]; - } - *r = '\0'; - - return ap_pstrdup(p, result); + const char *hex = "0123456789abcdef"; + AP_MD5_CTX my_md5; + unsigned char hash[16]; + char *r, result[33]; + int i; + + /* + * Take the MD5 hash of the string argument. + */ + ap_MD5Init(&my_md5); + ap_MD5Update(&my_md5, buf, (unsigned int)length); + ap_MD5Final(hash, &my_md5); + + for (i = 0, r = result; i < 16; i++) { + *r++ = hex[hash[i] >> 4]; + *r++ = hex[hash[i] & 0xF]; + } + *r = '\0'; + + return ap_pstrdup(p, result); } -API_EXPORT(char *) ap_md5(pool *p, const unsigned char *string) +API_EXPORT(char *) +ap_md5(pool *p, const unsigned char *string) { - return ap_md5_binary(p, string, (int) strlen((char *)string)); + return ap_md5_binary(p, string, (int) strlen((char *)string)); } /* these portions extracted from mpack, John G. Myers - jgm+@cmu.edu */ @@ -163,41 +164,45 @@ API_EXPORT(char *) ap_md5(pool *p, const unsigned char *string) static char basis_64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; -API_EXPORT(char *) ap_md5contextTo64(pool *a, AP_MD5_CTX * context) +API_EXPORT(char *) +ap_md5contextTo64(pool *a, AP_MD5_CTX * context) { - unsigned char digest[18]; - char *encodedDigest; - int i; - char *p; - - encodedDigest = (char *) ap_pcalloc(a, 25 * sizeof(char)); - - ap_MD5Final(digest, context); - digest[sizeof(digest) - 1] = digest[sizeof(digest) - 2] = 0; - - p = encodedDigest; - for (i = 0; i < sizeof(digest); i += 3) { - *p++ = basis_64[digest[i] >> 2]; - *p++ = basis_64[((digest[i] & 0x3) << 4) | ((int) (digest[i + 1] & 0xF0) >> 4)]; - *p++ = basis_64[((digest[i + 1] & 0xF) << 2) | ((int) (digest[i + 2] & 0xC0) >> 6)]; - *p++ = basis_64[digest[i + 2] & 0x3F]; - } - *p-- = '\0'; - *p-- = '='; - *p-- = '='; - return encodedDigest; + unsigned char digest[18]; + char *encodedDigest; + int i; + char *p; + + encodedDigest = (char *)ap_pcalloc(a, 25 * sizeof(char)); + + ap_MD5Final(digest, context); + digest[sizeof(digest) - 1] = digest[sizeof(digest) - 2] = 0; + + p = encodedDigest; + for (i = 0; i < sizeof(digest); i += 3) { + *p++ = basis_64[digest[i] >> 2]; + *p++ = basis_64[((digest[i] & 0x3) << 4) | + ((int)(digest[i + 1] & 0xF0) >> 4)]; + *p++ = basis_64[((digest[i + 1] & 0xF) << 2) | + ((int)(digest[i + 2] & 0xC0) >> 6)]; + *p++ = basis_64[digest[i + 2] & 0x3F]; + } + *p-- = '\0'; + *p-- = '='; + *p-- = '='; + return encodedDigest; } -API_EXPORT(char *) ap_md5digest(pool *p, FILE *infile) +API_EXPORT(char *) +ap_md5digest(pool *p, FILE *infile) { - AP_MD5_CTX context; - unsigned char buf[1000]; - unsigned int nbytes; - - ap_MD5Init(&context); - while ((nbytes = fread(buf, 1, sizeof(buf), infile))) { - ap_MD5Update(&context, buf, nbytes); - } - rewind(infile); - return ap_md5contextTo64(p, &context); + AP_MD5_CTX context; + unsigned char buf[1000]; + unsigned int nbytes; + + ap_MD5Init(&context); + while ((nbytes = fread(buf, 1, sizeof(buf), infile))) + ap_MD5Update(&context, buf, nbytes); + + rewind(infile); + return ap_md5contextTo64(p, &context); } |