diff --git a/common/iobuf.c b/common/iobuf.c index 284408b1f..5e6624f20 100644 --- a/common/iobuf.c +++ b/common/iobuf.c @@ -138,13 +138,6 @@ typedef struct #define OP_MIN_PARTIAL_CHUNK 512 #define OP_MIN_PARTIAL_CHUNK_2POW 9 -enum - { - BLOCK_FILTER_INPUT=1, - BLOCK_FILTER_OUTPUT=2, - BLOCK_FILTER_TEMP=3 - }; - /* The context we use for the block filter (used to handle OpenPGP length information header). */ typedef struct @@ -1001,7 +994,7 @@ block_filter (void *opaque, int control, iobuf_t chain, byte * buffer, log_debug ("init block_filter %p\n", a); if (a->partial) a->count = 0; - else if (a->use == BLOCK_FILTER_INPUT) + else if (a->use == IOBUF_INPUT) a->count = a->size = 0; else a->count = a->size; /* force first length bytes */ @@ -1015,7 +1008,7 @@ block_filter (void *opaque, int control, iobuf_t chain, byte * buffer, } else if (control == IOBUFCTRL_FREE) { - if (a->use == BLOCK_FILTER_OUTPUT) + if (a->use == IOBUF_OUTPUT) { /* write the end markers */ if (a->partial) { @@ -1105,9 +1098,8 @@ iobuf_print_chain (iobuf_t a) /* Allocate a new io buffer, with no function assigned. - USE is the desired usage: BLOCK_FILTER_INPUT for input, - BLOCK_FILTER_OUTPUT for output, or BLOCK_FILTER_TEMP for a temp - buffer. + USE is the desired usage: IOBUF_INPUT for input, IOBUF_OUTPUT for + output, or IOBUF_TEMP for a temp buffer. BUFSIZE is a suggested buffer size. */ @@ -1117,9 +1109,7 @@ iobuf_alloc (int use, size_t bufsize) iobuf_t a; static int number = 0; - assert (use == BLOCK_FILTER_INPUT - || use == BLOCK_FILTER_OUTPUT - || use == BLOCK_FILTER_TEMP); + assert (use == IOBUF_INPUT || use == IOBUF_OUTPUT || use == IOBUF_TEMP); a = xcalloc (1, sizeof *a); a->use = use; @@ -1151,7 +1141,7 @@ iobuf_close (iobuf_t a) for (; a && !rc; a = a2) { a2 = a->chain; - if (a->use == BLOCK_FILTER_OUTPUT && (rc = iobuf_flush (a))) + if (a->use == IOBUF_OUTPUT && (rc = iobuf_flush (a))) log_error ("iobuf_flush failed on close: %s\n", gpg_strerror (rc)); if (DBG_IOBUF) @@ -1181,7 +1171,7 @@ iobuf_cancel (iobuf_t a) char *remove_name = NULL; #endif - if (a && a->use == BLOCK_FILTER_OUTPUT) + if (a && a->use == IOBUF_OUTPUT) { s = iobuf_get_real_fname (a); if (s && *s) @@ -1233,7 +1223,7 @@ iobuf_temp () { iobuf_t a; - a = iobuf_alloc (3, IOBUF_BUFFER_SIZE); + a = iobuf_alloc (IOBUF_TEMP, IOBUF_BUFFER_SIZE); return a; } @@ -1244,7 +1234,7 @@ iobuf_temp_with_content (const char *buffer, size_t length) iobuf_t a; int i; - a = iobuf_alloc (3, length); + a = iobuf_alloc (IOBUF_TEMP, length); /* memcpy (a->d.buf, buffer, length); */ for (i=0; i < length; i++) a->d.buf[i] = buffer[i]; @@ -1332,7 +1322,7 @@ iobuf_open (const char *fname) return iobuf_fdopen (translate_file_handle (fd, 0), "rb"); else if ((fp = fd_cache_open (fname, "rb")) == GNUPG_INVALID_FD) return NULL; - a = iobuf_alloc (1, IOBUF_BUFFER_SIZE); + a = iobuf_alloc (IOBUF_INPUT, IOBUF_BUFFER_SIZE); fcx = xmalloc (sizeof *fcx + strlen (fname)); fcx->fp = fp; fcx->print_only_name = print_only; @@ -1361,7 +1351,8 @@ do_iobuf_fdopen (int fd, const char *mode, int keep_open) fp = INT2FD (fd); - a = iobuf_alloc (strchr (mode, 'w') ? 2 : 1, IOBUF_BUFFER_SIZE); + a = iobuf_alloc (strchr (mode, 'w') ? IOBUF_OUTPUT : IOBUF_INPUT, + IOBUF_BUFFER_SIZE); fcx = xmalloc (sizeof *fcx + 20); fcx->fp = fp; fcx->print_only_name = 1; @@ -1401,7 +1392,8 @@ iobuf_esopen (estream_t estream, const char *mode, int keep_open) file_es_filter_ctx_t *fcx; size_t len; - a = iobuf_alloc (strchr (mode, 'w') ? 2 : 1, IOBUF_BUFFER_SIZE); + a = iobuf_alloc (strchr (mode, 'w') ? IOBUF_OUTPUT : IOBUF_INPUT, + IOBUF_BUFFER_SIZE); fcx = xtrymalloc (sizeof *fcx + 30); fcx->fp = estream; fcx->print_only_name = 1; @@ -1426,7 +1418,8 @@ iobuf_sockopen (int fd, const char *mode) sock_filter_ctx_t *scx; size_t len; - a = iobuf_alloc (strchr (mode, 'w') ? 2 : 1, IOBUF_BUFFER_SIZE); + a = iobuf_alloc (strchr (mode, 'w') ? IOBUF_OUTPUT : IOBUF_INPUT, + IOBUF_BUFFER_SIZE); scx = xmalloc (sizeof *scx + 25); scx->sock = fd; scx->print_only_name = 1; @@ -1468,7 +1461,7 @@ iobuf_create (const char *fname, int mode700) return iobuf_fdopen (translate_file_handle (fd, 1), "wb"); else if ((fp = direct_open (fname, "wb", mode700)) == GNUPG_INVALID_FD) return NULL; - a = iobuf_alloc (2, IOBUF_BUFFER_SIZE); + a = iobuf_alloc (IOBUF_OUTPUT, IOBUF_BUFFER_SIZE); fcx = xmalloc (sizeof *fcx + strlen (fname)); fcx->fp = fp; fcx->print_only_name = print_only; @@ -1499,7 +1492,7 @@ iobuf_openrw (const char *fname) return NULL; else if ((fp = direct_open (fname, "r+b", 0)) == GNUPG_INVALID_FD) return NULL; - a = iobuf_alloc (2, IOBUF_BUFFER_SIZE); + a = iobuf_alloc (IOBUF_OUTPUT, IOBUF_BUFFER_SIZE); fcx = xmalloc (sizeof *fcx + strlen (fname)); fcx->fp = fp; strcpy (fcx->fname, fname); @@ -1624,7 +1617,7 @@ iobuf_push_filter2 (iobuf_t a, if (a->directfp) BUG (); - if (a->use == BLOCK_FILTER_OUTPUT && (rc = iobuf_flush (a))) + if (a->use == IOBUF_OUTPUT && (rc = iobuf_flush (a))) return rc; if (a->subno >= MAX_NESTING_FILTER) @@ -1649,11 +1642,11 @@ iobuf_push_filter2 (iobuf_t a, a->filter_ov = NULL; a->filter_ov_owner = 0; a->filter_eof = 0; - if (a->use == BLOCK_FILTER_TEMP) + if (a->use == IOBUF_TEMP) /* make a write stream from a temp stream */ - a->use = BLOCK_FILTER_OUTPUT; + a->use = IOBUF_OUTPUT; - if (a->use == BLOCK_FILTER_OUTPUT) + if (a->use == IOBUF_OUTPUT) { /* allocate a fresh buffer for the original stream */ b->d.buf = xmalloc (a->d.size); @@ -1732,7 +1725,7 @@ pop_filter (iobuf_t a, int (*f) (void *opaque, int control, log_bug ("pop_filter(): filter function not found\n"); /* flush this stream if it is an output stream */ - if (a->use == BLOCK_FILTER_OUTPUT && (rc = iobuf_flush (b))) + if (a->use == IOBUF_OUTPUT && (rc = iobuf_flush (b))) { log_error ("iobuf_flush failed in pop_filter: %s\n", gpg_strerror (rc)); return rc; @@ -1791,7 +1784,7 @@ underflow (iobuf_t a) int rc; assert (a->d.start == a->d.len); - if (a->use == BLOCK_FILTER_TEMP) + if (a->use == IOBUF_TEMP) return -1; /* EOF because a temp buffer can't do an underflow */ if (a->filter_eof) @@ -1853,7 +1846,7 @@ underflow (iobuf_t a) /* if( a->no == 1 ) */ /* log_hexdump (" data:", a->d.buf, len); */ } - if (a->use == BLOCK_FILTER_INPUT && rc == -1) + if (a->use == IOBUF_INPUT && rc == -1) { /* EOF: we can remove the filter */ size_t dummy_len = 0; @@ -1915,7 +1908,7 @@ iobuf_flush (iobuf_t a) if (a->directfp) return 0; - if (a->use == BLOCK_FILTER_TEMP) + if (a->use == IOBUF_TEMP) { /* increase the temp buffer */ unsigned char *newbuf; size_t newsize = a->d.size + IOBUF_BUFFER_SIZE; @@ -1930,7 +1923,7 @@ iobuf_flush (iobuf_t a) a->d.size = newsize; return 0; } - else if (a->use != BLOCK_FILTER_OUTPUT) + else if (a->use != IOBUF_OUTPUT) log_bug ("flush on non-output iobuf\n"); else if (!a->filter) log_bug ("iobuf_flush: no filter\n"); @@ -2346,7 +2339,7 @@ iobuf_seek (iobuf_t a, off_t newpos) } clearerr (fp); } - else if (a->use != BLOCK_FILTER_TEMP) + else if (a->use != IOBUF_TEMP) { for (; a; a = a->chain) { @@ -2374,7 +2367,7 @@ iobuf_seek (iobuf_t a, off_t newpos) #endif } /* Discard the buffer unless it is a temp stream. */ - if (a->use != BLOCK_FILTER_TEMP) + if (a->use != IOBUF_TEMP) a->d.len = 0; a->d.start = 0; a->nbytes = 0; @@ -2454,11 +2447,11 @@ iobuf_set_partial_block_mode (iobuf_t a, size_t len) { block_filter_ctx_t *ctx = xcalloc (1, sizeof *ctx); - assert (a->use == BLOCK_FILTER_INPUT || a->use == BLOCK_FILTER_OUTPUT); + assert (a->use == IOBUF_INPUT || a->use == IOBUF_OUTPUT); ctx->use = a->use; if (!len) { - if (a->use == BLOCK_FILTER_INPUT) + if (a->use == IOBUF_INPUT) log_debug ("pop_filter called in set_partial_block_mode" " - please report\n"); pop_filter (a, block_filter, NULL); diff --git a/common/iobuf.h b/common/iobuf.h index 3889459e3..23b1d091e 100644 --- a/common/iobuf.h +++ b/common/iobuf.h @@ -55,6 +55,13 @@ typedef enum IOBUF_IOCTL_FSYNC = 4 /* Uses ptrval. */ } iobuf_ioctl_t; +enum + { + IOBUF_INPUT=1, + IOBUF_OUTPUT=2, + IOBUF_TEMP=3 + }; + typedef struct iobuf_struct *iobuf_t; typedef struct iobuf_struct *IOBUF; /* Compatibility with gpg 1.4. */ @@ -62,7 +69,9 @@ typedef struct iobuf_struct *IOBUF; /* Compatibility with gpg 1.4. */ /* fixme: we should hide most of this stuff */ struct iobuf_struct { - int use; /* 1 input , 2 output, 3 temp */ + /* The type of filter. Either IOBUF_INPUT, IOBUF_OUTPUT or + IOBUF_TEMP. */ + int use; off_t nlimit; off_t nbytes; /* Used together with nlimit. */ off_t ntotal; /* Total bytes read (position of stream). */ @@ -184,6 +193,8 @@ void iobuf_skip_rest (iobuf_t a, unsigned long n, int partial); #define iobuf_get_temp_buffer(a) ( (a)->d.buf ) #define iobuf_get_temp_length(a) ( (a)->d.len ) -#define iobuf_is_temp(a) ( (a)->use == 3 ) + +/* Whether the filter uses an in-memory buffer. */ +#define iobuf_is_temp(a) ( (a)->use == IOBUF_TEMP ) #endif /*GNUPG_COMMON_IOBUF_H*/