Module Name: src Committed By: agc Date: Sat May 2 04:19:44 UTC 2009
Modified Files: src/crypto/external/bsd/netpgp/dist/src/lib: netpgp.c Log Message: Reorder the args to a static function to mirror some other function calls. Attempt to use mmap(2) to read a file, and fall back to multiple read(2) calls if that fails. To generate a diff of this commit: cvs rdiff -u -r1.5 -r1.6 src/crypto/external/bsd/netpgp/dist/src/lib/netpgp.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/crypto/external/bsd/netpgp/dist/src/lib/netpgp.c diff -u src/crypto/external/bsd/netpgp/dist/src/lib/netpgp.c:1.5 src/crypto/external/bsd/netpgp/dist/src/lib/netpgp.c:1.6 --- src/crypto/external/bsd/netpgp/dist/src/lib/netpgp.c:1.5 Sat May 2 02:38:55 2009 +++ src/crypto/external/bsd/netpgp/dist/src/lib/netpgp.c Sat May 2 04:19:43 2009 @@ -29,7 +29,9 @@ #include "config.h" #include <sys/types.h> +#include <sys/stat.h> #include <sys/param.h> +#include <sys/mman.h> #ifdef HAVE_OPENSSL_CAST_H #include <openssl/cast.h> @@ -158,13 +160,15 @@ /* sign a file, and put the signature in a separate file */ static int -sign_detached(__ops_secret_key_t *seckey, const char *hashstr, char *f, - char *sigfile) +sign_detached(char *f, char *sigfile, __ops_secret_key_t *seckey, + const char *hashstr) { __ops_create_signature_t *sig; __ops_hash_algorithm_t alg; __ops_create_info_t *info; unsigned char keyid[OPS_KEY_ID_SIZE]; + unsigned char *mmapped; + struct stat st; time_t t; char fname[MAXPATHLEN]; int fd; @@ -188,19 +192,32 @@ f); return 0; } - for (;;) { - unsigned char buf[8192]; - int n; + /* attempt to mmap(2) the file - if that fails, fall back to + * standard read(2) */ + mmapped = MAP_FAILED; + if (fstat(fd, &st) == 0) { + mmapped = mmap(NULL, (size_t)st.st_size, PROT_READ, + MAP_FILE | MAP_PRIVATE, fd, 0); + } + if (mmapped == MAP_FAILED) { + for (;;) { + unsigned char buf[8192]; + int n; - if ((n = read(fd, buf, sizeof(buf))) == 0) { - break; - } - if (n < 0) { - (void) fprintf(stderr, "short read \"%s\"\n", f); - (void) close(fd); - return 0; + if ((n = read(fd, buf, sizeof(buf))) == 0) { + break; + } + if (n < 0) { + (void) fprintf(stderr, "short read \"%s\"\n", + f); + (void) close(fd); + return 0; + } + __ops_signature_add_data(sig, buf, (unsigned)n); } - __ops_signature_add_data(sig, buf, (unsigned)n); + } else { + __ops_signature_add_data(sig, mmapped, (unsigned)st.st_size); + (void) munmap(mmapped, (unsigned)st.st_size); } (void) close(fd); @@ -463,7 +480,7 @@ if (cleartext) { __ops_sign_file_as_cleartext(f, out, seckey, true); } else if (detached) { - sign_detached(seckey, "SHA1", f, out); + sign_detached(f, out, seckey, "SHA1"); } else { __ops_sign_file(f, out, seckey, armored, true); }