Module Name:    src
Committed By:   agc
Date:           Thu Jul  1 00:42:51 UTC 2010

Modified Files:
        src/crypto/external/bsd/netpgp/dist/src/lib: netpgp.c

Log Message:
factor out the code which recognises ASCII-armour into its own function.


To generate a diff of this commit:
cvs rdiff -u -r1.60 -r1.61 \
    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.60 src/crypto/external/bsd/netpgp/dist/src/lib/netpgp.c:1.61
--- src/crypto/external/bsd/netpgp/dist/src/lib/netpgp.c:1.60	Wed Jun 30 15:17:40 2010
+++ src/crypto/external/bsd/netpgp/dist/src/lib/netpgp.c	Thu Jul  1 00:42:51 2010
@@ -34,7 +34,7 @@
 
 #if defined(__NetBSD__)
 __COPYRIGHT("@(#) Copyright (c) 2009 The NetBSD Foundation, Inc. All rights reserved.");
-__RCSID("$NetBSD: netpgp.c,v 1.60 2010/06/30 15:17:40 agc Exp $");
+__RCSID("$NetBSD: netpgp.c,v 1.61 2010/07/01 00:42:51 agc Exp $");
 #endif
 
 #include <sys/types.h>
@@ -437,6 +437,26 @@
 	return 1;
 }
 
+/* return 1 if the file contains ascii-armoured text */
+static unsigned
+isarmoured(__ops_io_t *io, const char *f, const char *text)
+{
+	unsigned	 armoured;
+	FILE		*fp;
+	char	 	 buf[BUFSIZ];
+
+	armoured = 0;
+	if ((fp = fopen(f, "r")) == NULL) {
+		(void) fprintf(io->errs, "isarmoured: can't open '%s'\n", f);
+		return 0;
+	}
+	if (fgets(buf, sizeof(buf), fp) != NULL) {
+		armoured = (strncmp(buf, text, strlen(text)) == 0);
+	}
+	(void) fclose(fp);
+	return armoured;
+}
+
 /***************************************************************************/
 /* exported functions start here */
 /***************************************************************************/
@@ -754,25 +774,13 @@
 #endif
 	__ops_io_t	*io;
 	unsigned	 realarmor;
-	FILE		*fp;
 #if 0
 	char		 ringfile[MAXPATHLEN];
 #endif
-	char		 buf[BUFSIZ];
 	int		 done;
 
 	io = netpgp->io;
-	realarmor = 0;
-	if ((fp = fopen(f, "r")) == NULL) {
-		(void) fprintf(io->errs, "netpgp_import_key: can't open '%s'\n", f);
-		return 0;
-	}
-	if (fgets(buf, sizeof(buf), fp) == NULL) {
-		realarmor = 0;
-	} else {
-		realarmor = (strncmp(buf, IMPORT_ARMOR_HEAD, strlen(IMPORT_ARMOR_HEAD)) == 0);
-	}
-	(void) fclose(fp);
+	realarmor = isarmoured(io, f, IMPORT_ARMOR_HEAD);
 	done = __ops_keyring_fileread(netpgp->pubring, realarmor, f);
 	if (!done) {
 		(void) fprintf(io->errs, "Cannot import key from file %s\n", f);
@@ -901,30 +909,18 @@
 	const unsigned	 overwrite = 1;
 	__ops_io_t	*io;
 	unsigned	 realarmor;
-	FILE		*fp;
-	char		 buf[BUFSIZ];
 
+	__OPS_USED(armored);
 	io = netpgp->io;
 	if (f == NULL) {
 		(void) fprintf(io->errs,
 			"netpgp_decrypt_file: no filename specified\n");
 		return 0;
 	}
-	realarmor = (unsigned)armored;
-	if ((fp = fopen(f, "r")) == NULL) {
-		(void) fprintf(io->errs,
-			"netpgp_decrypt_file: can't open '%s'\n", f);
-		return 0;
-	}
-	if (fgets(buf, sizeof(buf), fp) == NULL) {
-		realarmor = 0;
-	} else {
-		realarmor = (strcmp(buf, ARMOR_HEAD) == 0);
-	}
-	(void) fclose(fp);
+	realarmor = isarmoured(io, f, ARMOR_HEAD);
 	return __ops_decrypt_file(netpgp->io, f, out, netpgp->secring,
 				netpgp->pubring,
-				(unsigned)realarmor, overwrite,
+				realarmor, overwrite,
 				netpgp->passfp, get_passphrase_cb);
 }
 
@@ -1014,9 +1010,8 @@
 	__ops_validation_t	 result;
 	__ops_io_t		*io;
 	unsigned		 realarmor;
-	FILE			*fp;
-	char			 buf[BUFSIZ];
 
+	__OPS_USED(armored);
 	(void) memset(&result, 0x0, sizeof(result));
 	io = netpgp->io;
 	if (in == NULL) {
@@ -1024,18 +1019,7 @@
 			"netpgp_verify_file: no filename specified\n");
 		return 0;
 	}
-	realarmor = (unsigned)armored;
-	if ((fp = fopen(in, "r")) == NULL) {
-		(void) fprintf(io->errs,
-			"netpgp_decrypt_file: can't open '%s'\n", in);
-		return 0;
-	}
-	if (fgets(buf, sizeof(buf), fp) == NULL) {
-		realarmor = 0;
-	} else {
-		realarmor = (strcmp(buf, ARMOR_SIG_HEAD) == 0);
-	}
-	(void) fclose(fp);
+	realarmor = isarmoured(io, in, ARMOR_SIG_HEAD);
 	if (__ops_validate_file(io, &result, in, out, (const int)realarmor, netpgp->pubring)) {
 		resultp(io, in, &result, netpgp->pubring);
 		return 1;

Reply via email to