The "enc" application provides a "-bufsize number" option to control the
I/O buffer size, but this does not work correctly for use in pipelines
since stdin and stdout are buffered by default on most OSes. Also, the
code imposes a minimum of 80 byte block sizes for all I/O, this minimum
should be used only when base64 encoding is active.

For example, openssl calls fread() to request 80 bytes, but this results
in a "read" syscall requesting 4096 bytes to fill the stdin buffer. (This
is on a Linux system, but other OSes generally behave in a similar way.)

The following patch uses the ANSI C setvbuf(3) function to turn off stdio
buffering on stdin and stdout as necessary, and allows use of small block
sizes when using binary I/O.  This makes it possible to feed small chunks
of data to "openssl enc" in a pipeline and immediately read back the
corresponding chunks of encrypted output.

-Klaus

diff --minimal -r -ur openssl_0.9.8a.orig.tar.gz.content.14777/apps/enc.c 
openssl-0.9.8a/apps/enc.c
--- openssl_0.9.8a.orig.tar.gz.content.14777/apps/enc.c 2005-04-30 
10:17:05.000000000 -0500
+++ openssl-0.9.8a/apps/enc.c   2006-05-22 11:25:23.000000000 -0500
@@ -340,7 +340,7 @@
                        }
 
                /* It must be large enough for a base64 encoded line */
-               if (n < 80) n=80;
+               if (base64 && n < 80) n=80;
 
                bsize=(int)n;
                if (verbose) BIO_printf(bio_err,"bufsize=%d\n",bsize);
@@ -370,7 +370,10 @@
                }
 
        if (inf == NULL)
+               {
+               setvbuf(stdin, (char *)NULL, _IONBF, 0);
                BIO_set_fp(in,stdin,BIO_NOCLOSE);
+               }
        else
                {
                if (BIO_read_filename(in,inf) <= 0)
@@ -421,6 +424,7 @@
        if (outf == NULL)
                {
                BIO_set_fp(out,stdout,BIO_NOCLOSE);
+               setvbuf(stdout, (char *)NULL, _IONBF, 0);
 #ifdef OPENSSL_SYS_VMS
                {
                BIO *tmpbio = BIO_new(BIO_f_linebuffer());
______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
Development Mailing List                       [email protected]
Automated List Manager                           [EMAIL PROTECTED]

Reply via email to