1
0
mirror of git://git.gnupg.org/gnupg.git synced 2025-05-24 16:43:28 +02:00

common: Allow a readlimit for iobuf_esopen.

* common/iobuf.c (file_es_filter_ctx_t): Add fields use_readlimit and
readlimit.
(file_es_filter): Implement them.
(iobuf_esopen): Add new arg readlimit.
* g10/decrypt-data.c (decrypt_data): Adjust for change.
* g10/import.c (import_keys_es_stream): Ditto.
--

This comes handy for (length,datablob) style streams.

Signed-off-by: Werner Koch <wk@gnupg.org>
This commit is contained in:
Werner Koch 2019-09-10 15:45:58 +02:00
parent aba82684fe
commit 2f0fdab8aa
No known key found for this signature in database
GPG Key ID: E3FDFF218E45B72B
4 changed files with 41 additions and 6 deletions

View File

@ -106,6 +106,8 @@ typedef struct
int keep_open; int keep_open;
int no_cache; int no_cache;
int eof_seen; int eof_seen;
int use_readlimit; /* Take care of the readlimit. */
size_t readlimit; /* Number of bytes left to read. */
int print_only_name; /* Flags indicating that fname is not a real file. */ int print_only_name; /* Flags indicating that fname is not a real file. */
char fname[1]; /* Name of the file. */ char fname[1]; /* Name of the file. */
} file_es_filter_ctx_t; } file_es_filter_ctx_t;
@ -635,6 +637,34 @@ file_es_filter (void *opaque, int control, iobuf_t chain, byte * buf,
rc = -1; rc = -1;
*ret_len = 0; *ret_len = 0;
} }
else if (a->use_readlimit)
{
nbytes = 0;
if (!a->readlimit)
{ /* eof */
a->eof_seen = 1;
rc = -1;
}
else
{
if (size > a->readlimit)
size = a->readlimit;
rc = es_read (f, buf, size, &nbytes);
if (rc == -1)
{ /* error */
rc = gpg_error_from_syserror ();
log_error ("%s: read error: %s\n", a->fname,strerror (errno));
}
else if (!nbytes)
{ /* eof */
a->eof_seen = 1;
rc = -1;
}
else
a->readlimit -= nbytes;
}
*ret_len = nbytes;
}
else else
{ {
nbytes = 0; nbytes = 0;
@ -1412,7 +1442,8 @@ iobuf_fdopen_nc (int fd, const char *mode)
iobuf_t iobuf_t
iobuf_esopen (estream_t estream, const char *mode, int keep_open) iobuf_esopen (estream_t estream, const char *mode, int keep_open,
size_t readlimit)
{ {
iobuf_t a; iobuf_t a;
file_es_filter_ctx_t *fcx; file_es_filter_ctx_t *fcx;
@ -1424,7 +1455,9 @@ iobuf_esopen (estream_t estream, const char *mode, int keep_open)
fcx->fp = estream; fcx->fp = estream;
fcx->print_only_name = 1; fcx->print_only_name = 1;
fcx->keep_open = keep_open; fcx->keep_open = keep_open;
sprintf (fcx->fname, "[fd %p]", estream); fcx->readlimit = readlimit;
fcx->use_readlimit = !!readlimit;
snprintf (fcx->fname, 30, "[fd %p]", estream);
a->filter = file_es_filter; a->filter = file_es_filter;
a->filter_ov = fcx; a->filter_ov = fcx;
file_es_filter (fcx, IOBUFCTRL_INIT, NULL, NULL, &len); file_es_filter (fcx, IOBUFCTRL_INIT, NULL, NULL, &len);

View File

@ -328,8 +328,10 @@ iobuf_t iobuf_fdopen_nc (int fd, const char *mode);
letter 'w', creates an output filter. Otherwise, creates an input letter 'w', creates an output filter. Otherwise, creates an input
filter. If KEEP_OPEN is TRUE, then the stream is not closed when filter. If KEEP_OPEN is TRUE, then the stream is not closed when
the filter is destroyed. Otherwise, the stream is closed when the the filter is destroyed. Otherwise, the stream is closed when the
filter is destroyed. */ filter is destroyed. If READLIMIT is not 0 this gives a limit on
iobuf_t iobuf_esopen (estream_t estream, const char *mode, int keep_open); the number of bytes to read from estream. */
iobuf_t iobuf_esopen (estream_t estream, const char *mode, int keep_open,
size_t readlimit);
/* Create a filter using an existing socket. On Windows creates a /* Create a filter using an existing socket. On Windows creates a
special socket filter. On non-Windows systems simply, this simply special socket filter. On non-Windows systems simply, this simply

View File

@ -475,7 +475,7 @@ decrypt_data (ctrl_t ctrl, void *procctx, PKT_encrypted *ed, DEK *dek)
rc = get_output_file ("", 0, ed->buf, &filename, &fp); rc = get_output_file ("", 0, ed->buf, &filename, &fp);
if (! rc) if (! rc)
{ {
iobuf_t output = iobuf_esopen (fp, "w", 0); iobuf_t output = iobuf_esopen (fp, "w", 0, 0);
armor_filter_context_t *afx = NULL; armor_filter_context_t *afx = NULL;
if (opt.armor) if (opt.armor)

View File

@ -550,7 +550,7 @@ import_keys_es_stream (ctrl_t ctrl, estream_t fp,
gpg_error_t err; gpg_error_t err;
iobuf_t inp; iobuf_t inp;
inp = iobuf_esopen (fp, "rb", 1); inp = iobuf_esopen (fp, "rb", 1, 0);
if (!inp) if (!inp)
{ {
err = gpg_error_from_syserror (); err = gpg_error_from_syserror ();