Module Name:    othersrc
Committed By:   agc
Date:           Wed Aug  3 05:52:19 UTC 2011

Modified Files:
        othersrc/external/bsd/circa/dist: circa.c circa.h libcirca.3 main.c

Log Message:
The function to circa encode/decode a file is useful in itself. Move it
from the driver program to the library itself, and rename it circa_file()


To generate a diff of this commit:
cvs rdiff -u -r1.3 -r1.4 othersrc/external/bsd/circa/dist/circa.c
cvs rdiff -u -r1.2 -r1.3 othersrc/external/bsd/circa/dist/circa.h
cvs rdiff -u -r1.4 -r1.5 othersrc/external/bsd/circa/dist/libcirca.3 \
    othersrc/external/bsd/circa/dist/main.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: othersrc/external/bsd/circa/dist/circa.c
diff -u othersrc/external/bsd/circa/dist/circa.c:1.3 othersrc/external/bsd/circa/dist/circa.c:1.4
--- othersrc/external/bsd/circa/dist/circa.c:1.3	Thu May 12 05:42:18 2011
+++ othersrc/external/bsd/circa/dist/circa.c	Wed Aug  3 05:52:19 2011
@@ -290,3 +290,86 @@
 	}
 	return 1;
 }
+
+/* process a file - encoding or decoding */
+int
+circa_file(circa_t *circa, size_t sectorsize, int encoding, char *in, char *fout)
+{
+	uint8_t	*buf;
+	uint8_t	*out;
+	ssize_t	 outcc;
+	ssize_t	 cc;
+	size_t	 readsize;
+	FILE	*fpout;
+	FILE	*fpin;
+	int	 ok;
+
+	if (in == NULL) {
+		fpin = stdin;
+	} else if ((fpin = fopen(in, "r")) == NULL) {
+		(void) fprintf(stderr, "can't read '%s'\n", in);
+		return 0;
+	}
+	if (fout == NULL) {
+		fpout = stdout;
+	} else if ((fpout = fopen(fout, "w")) == NULL) {
+		(void) fprintf(stderr, "can't write '%s'\n", fout);
+		if (fpin != stdin) {
+			(void) fclose(fpin);
+		}
+		return 0;
+	}
+	if (encoding) {
+		/* copy the encoding paramters to output */
+		if (!circa_init(circa, sectorsize)) {
+			(void) fprintf(stderr, "bad sectorsize %zu\n", sectorsize);
+			return 0;
+		}
+		readsize = circa->datasize;
+		if ((buf = calloc(1, sectorsize)) == NULL || (out = calloc(1, sectorsize)) == NULL) {
+			(void) fprintf(stderr, "can't allocate sectorsize buffers\n");
+			return 0;
+		}
+		outcc = circa_put_header(circa, out, sectorsize);
+		if (write(fileno(fpout), out, (size_t)outcc) != outcc) {
+			(void) fprintf(stderr, "short write\n");
+			return 0;
+		}
+	} else {
+		/* read the encoding paramters from input */
+		if (read(fileno(fpin), &circa->header, sizeof(circa->header)) != sizeof(circa->header)) {
+			(void) fprintf(stderr, "short read\n");
+			return 0;
+		}
+		if (circa_get_header(circa, &circa->header, sizeof(circa->header)) != sizeof(circa->header) ||
+		    !circa_init(circa, readsize = sectorsize = circa->header.sectorsize)) {
+			(void) fprintf(stderr, "bad sectorsize %zu\n", sectorsize);
+			return 0;
+		}
+		if ((buf = calloc(1, sectorsize)) == NULL ||
+		    (out = calloc(1, sectorsize)) == NULL) {
+			(void) fprintf(stderr, "can't allocate sectorsize buffers\n");
+			return 0;
+		}
+	}
+	for (ok = 1 ; ok && (cc = read(fileno(fpin), buf, readsize)) > 0 ; ) {
+		if (encoding) {
+			outcc = circa_encode(circa, buf, (size_t)cc, out, sectorsize);
+		} else {
+			outcc = circa_decode(circa, buf, (size_t)cc, out, sectorsize);
+		}
+		if (write(fileno(fpout), out, outcc) != outcc) {
+			(void) fprintf(stderr, "short write (%zd)\n", outcc);
+			ok = 0;
+		}
+	}
+	if (fpin != stdin) {
+		fclose(fpin);
+	}
+	if (fpout != stdout) {
+		fclose(fpout);
+	}
+	free(out);
+	free(buf);
+	return ok;
+}

Index: othersrc/external/bsd/circa/dist/circa.h
diff -u othersrc/external/bsd/circa/dist/circa.h:1.2 othersrc/external/bsd/circa/dist/circa.h:1.3
--- othersrc/external/bsd/circa/dist/circa.h:1.2	Thu May 12 04:11:15 2011
+++ othersrc/external/bsd/circa/dist/circa.h	Wed Aug  3 05:52:19 2011
@@ -75,6 +75,8 @@
 ssize_t circa_encode(circa_t */*circa*/, const void */*in*/, size_t /*insize*/, uint8_t */*out*/, size_t /*outsize*/);
 ssize_t circa_decode(circa_t */*circa*/, const void */*in*/, size_t /*insize*/, uint8_t */*out*/, size_t /*outsize*/);
 
+int circa_file(circa_t */*circa*/, size_t /*sectorsize*/, int /*encoding*/, char */*in*/, char */*fout*/);
+
 /* resilient header routines */
 ssize_t circa_get_header(circa_t */*circa*/, const void */*in*/, size_t /*size*/);
 ssize_t circa_put_header(circa_t */*circa*/, void */*out*/, size_t /*size*/);

Index: othersrc/external/bsd/circa/dist/libcirca.3
diff -u othersrc/external/bsd/circa/dist/libcirca.3:1.4 othersrc/external/bsd/circa/dist/libcirca.3:1.5
--- othersrc/external/bsd/circa/dist/libcirca.3:1.4	Thu May 12 08:40:02 2011
+++ othersrc/external/bsd/circa/dist/libcirca.3	Wed Aug  3 05:52:19 2011
@@ -1,4 +1,4 @@
-.\" $NetBSD: libcirca.3,v 1.4 2011/05/12 08:40:02 wiz Exp $
+.\" $NetBSD: libcirca.3,v 1.5 2011/08/03 05:52:19 agc Exp $
 .\"
 .\" Copyright (c) 2011 Alistair Crooks <[email protected]>
 .\" All rights reserved.
@@ -23,7 +23,7 @@
 .\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 .\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 .\"/
-.Dd May 11, 2011
+.Dd August 2, 2011
 .Dt LIBCIRCA 3
 .Os
 .Sh NAME
@@ -46,6 +46,10 @@
 .Fo circa_decode
 .Fa "circa_t *circa" "const void *input" "size_t insize" "uint8_t *output" "size_t outsize"
 .Fc
+.Ft int
+.Fo circa_file
+.Fa "circa_t *circa" "size_t sectorsize" "int encoding" "char *in" "char *fout"
+.Fc
 .Ft ssize_t
 .Fo circa_get_header
 .Fa "circa_t *circa" "const void *input" "size_t size"
@@ -103,6 +107,8 @@
 To decode the original data,
 .Fn circa_decode
 is called.
+.Fn circa_file
+is used to encode and decode files.
 .Pp
 When creating output files, the two functions
 .Fn circa_get_header
Index: othersrc/external/bsd/circa/dist/main.c
diff -u othersrc/external/bsd/circa/dist/main.c:1.4 othersrc/external/bsd/circa/dist/main.c:1.5
--- othersrc/external/bsd/circa/dist/main.c:1.4	Thu May 12 05:42:18 2011
+++ othersrc/external/bsd/circa/dist/main.c	Wed Aug  3 05:52:19 2011
@@ -37,89 +37,6 @@
 #define PKG_VERSION	"20110511"
 #define PKG_AUTHOR	"Alistair Crooks ([email protected])"
 
-/* process a file - encoding or decoding */
-static int
-do_circa(circa_t *circa, size_t sectorsize, int encoding, char *in, char *fout)
-{
-	uint8_t	*buf;
-	uint8_t	*out;
-	size_t	 readsize;
-	FILE	*fpout;
-	FILE	*fpin;
-	int	 outcc;
-	int	 cc;
-	int	 ok;
-
-	if (in == NULL) {
-		fpin = stdin;
-	} else if ((fpin = fopen(in, "r")) == NULL) {
-		(void) fprintf(stderr, "can't read '%s'\n", in);
-		return 0;
-	}
-	if (fout == NULL) {
-		fpout = stdout;
-	} else if ((fpout = fopen(fout, "w")) == NULL) {
-		(void) fprintf(stderr, "can't write '%s'\n", fout);
-		if (fpin != stdin) {
-			(void) fclose(fpin);
-		}
-		return 0;
-	}
-	if (encoding) {
-		/* copy the encoding paramters to output */
-		if (!circa_init(circa, sectorsize)) {
-			(void) fprintf(stderr, "bad sectorsize %zu\n", sectorsize);
-			return 0;
-		}
-		readsize = circa->datasize;
-		if ((buf = calloc(1, sectorsize)) == NULL || (out = calloc(1, sectorsize)) == NULL) {
-			(void) fprintf(stderr, "can't allocate sectorsize buffers\n");
-			return 0;
-		}
-		outcc = circa_put_header(circa, out, sectorsize);
-		if (write(fileno(fpout), out, outcc) != outcc) {
-			(void) fprintf(stderr, "short write\n");
-			return 0;
-		}
-	} else {
-		/* read the encoding paramters from input */
-		if (read(fileno(fpin), &circa->header, sizeof(circa->header)) != sizeof(circa->header)) {
-			(void) fprintf(stderr, "short read\n");
-			return 0;
-		}
-		if (circa_get_header(circa, &circa->header, sizeof(circa->header)) != sizeof(circa->header) ||
-		    !circa_init(circa, readsize = sectorsize = circa->header.sectorsize)) {
-			(void) fprintf(stderr, "bad sectorsize %zu\n", sectorsize);
-			return 0;
-		}
-		if ((buf = calloc(1, sectorsize)) == NULL ||
-		    (out = calloc(1, sectorsize)) == NULL) {
-			(void) fprintf(stderr, "can't allocate sectorsize buffers\n");
-			return 0;
-		}
-	}
-	for (ok = 1 ; ok && (cc = read(fileno(fpin), buf, readsize)) > 0 ; ) {
-		if (encoding) {
-			outcc = circa_encode(circa, buf, cc, out, sectorsize);
-		} else {
-			outcc = circa_decode(circa, buf, cc, out, sectorsize);
-		}
-		if (write(fileno(fpout), out, outcc) != outcc) {
-			(void) fprintf(stderr, "short write (%d)\n", outcc);
-			ok = 0;
-		}
-	}
-	if (fpin != stdin) {
-		fclose(fpin);
-	}
-	if (fpout != stdout) {
-		fclose(fpout);
-	}
-	free(out);
-	free(buf);
-	return ok;
-}
-
 int
 main(int argc, char **argv)
 {
@@ -154,10 +71,10 @@
 	}
 	/* Initialization of the ECC library */
 	if (optind == argc) {
-		do_circa(&circa, sectorsize, encoding, NULL, out);
+		circa_file(&circa, sectorsize, encoding, NULL, out);
 	} else {
 		for (i = optind ; i < argc ; i++) {
-			do_circa(&circa, sectorsize, encoding, argv[i], out);
+			circa_file(&circa, sectorsize, encoding, argv[i], out);
 		}
 	}
 	circa_end(&circa);

Reply via email to