| 1 | /* ==================================================================== |
| 2 | * Copyright (c) 1996,1997 The Apache Group. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in |
| 13 | * the documentation and/or other materials provided with the |
| 14 | * distribution. |
| 15 | * |
| 16 | * 3. All advertising materials mentioning features or use of this |
| 17 | * software must display the following acknowledgment: |
| 18 | * "This product includes software developed by the Apache Group |
| 19 | * for use in the Apache HTTP server project (http://www.apache.org/)." |
| 20 | * |
| 21 | * 4. The names "Apache Server" and "Apache Group" must not be used to |
| 22 | * endorse or promote products derived from this software without |
| 23 | * prior written permission. |
| 24 | * |
| 25 | * 5. Redistributions of any form whatsoever must retain the following |
| 26 | * acknowledgment: |
| 27 | * "This product includes software developed by the Apache Group |
| 28 | * for use in the Apache HTTP server project (http://www.apache.org/)." |
| 29 | * |
| 30 | * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY |
| 31 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 32 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 33 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE GROUP OR |
| 34 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 35 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 36 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 37 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 38 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 39 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 40 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 41 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
| 42 | * ==================================================================== |
| 43 | * |
| 44 | * This software consists of voluntary contributions made by many |
| 45 | * individuals on behalf of the Apache Group and was originally based |
| 46 | * on public domain software written at the National Center for |
| 47 | * Supercomputing Applications, University of Illinois, Urbana-Champaign. |
| 48 | * For more information on the Apache Group and the Apache HTTP server |
| 49 | * project, please see <http://www.apache.org/>. |
| 50 | * |
| 51 | */ |
| 52 | |
| 53 | #include <stdarg.h> |
| 54 | |
| 55 | /* Reading is buffered */ |
| 56 | #define B_RD (1) |
| 57 | /* Writing is buffered */ |
| 58 | #define B_WR (2) |
| 59 | #define B_RDWR (3) |
| 60 | /* At end of file, or closed stream; no further input allowed */ |
| 61 | #define B_EOF (4) |
| 62 | /* No further output possible */ |
| 63 | #define B_EOUT (8) |
| 64 | /* A read error has occurred */ |
| 65 | #define B_RDERR (16) |
| 66 | /* A write error has occurred */ |
| 67 | #define B_WRERR (32) |
| 68 | #ifdef B_ERROR /* in SVR4: sometimes defined in /usr/include/sys/buf.h */ |
| 69 | #undef B_ERROR /* avoid "warning: `B_ERROR' redefined" */ |
| 70 | #endif |
| 71 | #define B_ERROR (48) |
| 72 | /* Use chunked writing */ |
| 73 | #define B_CHUNK (64) |
| 74 | /* bflush() if a read would block */ |
| 75 | #define B_SAFEREAD (128) |
| 76 | |
| 77 | typedef struct buff_struct BUFF; |
| 78 | |
| 79 | struct buff_struct { |
| 80 | int flags; /* flags */ |
| 81 | unsigned char *inptr; /* pointer to next location to read */ |
| 82 | int incnt; /* number of bytes left to read from input buffer; |
| 83 | * always 0 if had a read error */ |
| 84 | int outchunk; /* location of chunk header when chunking */ |
| 85 | int outcnt; /* number of byte put in output buffer */ |
| 86 | unsigned char *inbase; |
| 87 | unsigned char *outbase; |
| 88 | int bufsiz; |
| 89 | void (*error) (BUFF * fb, int op, void *data); |
| 90 | void *error_data; |
| 91 | long int bytes_sent; /* number of bytes actually written */ |
| 92 | |
| 93 | pool *pool; |
| 94 | |
| 95 | /* could also put pointers to the basic I/O routines here */ |
| 96 | int fd; /* the file descriptor */ |
| 97 | int fd_in; /* input file descriptor, if different */ |
| 98 | }; |
| 99 | |
| 100 | /* Options to bset/getopt */ |
| 101 | #define BO_BYTECT (1) |
| 102 | |
| 103 | /* Stream creation and modification */ |
| 104 | extern BUFF *bcreate(pool * p, int flags); |
| 105 | extern void bpushfd(BUFF * fb, int fd_in, int fd_out); |
| 106 | extern int bsetopt(BUFF * fb, int optname, const void *optval); |
| 107 | extern int bgetopt(BUFF * fb, int optname, void *optval); |
| 108 | extern int bsetflag(BUFF * fb, int flag, int value); |
| 109 | extern int bclose(BUFF * fb); |
| 110 | |
| 111 | #define bgetflag(fb, flag) ((fb)->flags & (flag)) |
| 112 | |
| 113 | /* Error handling */ |
| 114 | extern void bonerror(BUFF * fb, void (*error) (BUFF *, int, void *), |
| 115 | void *data); |
| 116 | |
| 117 | /* I/O */ |
| 118 | extern int bread(BUFF * fb, void *buf, int nbyte); |
| 119 | extern int bgets(char *s, int n, BUFF * fb); |
| 120 | extern int blookc(char *buff, BUFF * fb); |
| 121 | extern int bskiplf(BUFF * fb); |
| 122 | extern int bwrite(BUFF * fb, const void *buf, int nbyte); |
| 123 | extern int bflush(BUFF * fb); |
| 124 | extern int bputs(const char *x, BUFF * fb); |
| 125 | extern int bvputs(BUFF * fb, ...); |
| 126 | extern int bprintf(BUFF * fb, const char *fmt, ...); |
| 127 | extern int vbprintf(BUFF * fb, const char *fmt, va_list vlist); |
| 128 | |
| 129 | /* Internal routines */ |
| 130 | extern int bflsbuf(int c, BUFF * fb); |
| 131 | extern int bfilbuf(BUFF * fb); |
| 132 | |
| 133 | #define bgetc(fb) ( ((fb)->incnt == 0) ? bfilbuf(fb) : \ |
| 134 | ((fb)->incnt--, *((fb)->inptr++)) ) |
| 135 | |
| 136 | #define bputc(c, fb) ((((fb)->flags & (B_EOUT|B_WRERR|B_WR)) != B_WR || \ |
| 137 | (fb)->outcnt == (fb)->bufsiz) ? bflsbuf(c, (fb)) : \ |
| 138 | ((fb)->outbase[(fb)->outcnt++] = (c), 0)) |