Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 8 Mar 2013 23:57:59 GMT
From:      Brooks Davis <brooks@FreeBSD.org>
To:        Perforce Change Reviews <perforce@FreeBSD.org>
Subject:   PERFORCE change 222733 for review
Message-ID:  <201303082357.r28Nvx5R057835@skunkworks.freebsd.org>

next in thread | raw e-mail | index | archive | help
http://p4web.freebsd.org/@@222733?ac=10

Change 222733 by brooks@brooks_zenith on 2013/03/08 23:57:40

	Split the extract and verify code out into a seperate file.

Affected files ...

.. //depot/projects/ctsrd/beribsd/src/ctsrd/writefile/Makefile#2 edit
.. //depot/projects/ctsrd/beribsd/src/ctsrd/writefile/eav.c#1 add
.. //depot/projects/ctsrd/beribsd/src/ctsrd/writefile/eav.h#1 add
.. //depot/projects/ctsrd/beribsd/src/ctsrd/writefile/writefile.c#3 edit

Differences ...

==== //depot/projects/ctsrd/beribsd/src/ctsrd/writefile/Makefile#2 (text+ko) ====

@@ -3,6 +3,8 @@
 
 PROG=	writefile
 
+SRCS=	writefile.c eav.c
+
 MAN=
 
 BINDIR=	/usr/sbin

==== //depot/projects/ctsrd/beribsd/src/ctsrd/writefile/writefile.c#3 (text+ko) ====

@@ -29,45 +29,26 @@
  */
 
 #include <sys/param.h>
-#include <sys/limits.h>
 #include <sys/mman.h>
 #include <sys/stat.h>
 
 #include <ctype.h>
-#include <bzlib.h>
 #include <err.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <libutil.h>
-#include <md5.h>
 #include <stdlib.h>
+#include <stdio.h>
 #include <string.h>
 #include <unistd.h>
 
-enum eav_error {
-	EAV_SUCCESS = 0,
-	EAV_ERR_MEM,
-	EAV_ERR_DIGEST,
-	EAV_ERR_DIGEST_UNKNOWN,
-	EAV_ERR_COMP,
-	EAV_ERR_COMP_UNKNOWN,
-	EAV_ERR_COMP_UNSUPPORTED
-};
+#if 0
+#include <sys/limits.h>
 
-enum eav_digest {
-	EAV_DIGEST_NONE = 0,
-	EAV_DIGEST_MD5
-};
+#endif
 
-enum eav_compression {
-	EAV_COMP_NONE = 0,
-	EAV_COMP_BZIP2,
-	EAV_COMP_GZIP,
-	EAV_COMP_XZ,
+#include "eav.h"
 
-	EAV_COMP_UNKNOWN
-};
-
 static void usage(int) __dead2;
 
 static void
@@ -99,194 +80,6 @@
 	return(len);
 }
 
-static enum eav_compression
-eav_taste(const unsigned char *buf, off_t len)
-{
-
-	/*
-	 * BZIP header from wikipedia:
-	 * .magic:16	= 'BZ' signature/magic number
-	 * .version:8	= 'h' for Bzip2
-	 *                 ('H'uffman coding),
-	 *		   '0' for Bzip1 (deprecated)
-	 * .hundred_k_blocksize:8 = '1'..'9'
-	 *		   block-size 100 kB-900 kB
-	 * .compressed_magic:48 =
-	 *		   0x314159265359 (BCD (pi))
-	 */
-	if( len > 10 && buf[0] == 'B' && buf[1] == 'Z' &&
-	    buf[4] == 0x31 && buf[5] == 0x41 && buf[6] == 0x59 &&
-	    buf[7] == 0x26 && buf[8] == 0x53 && buf[9] == 0x59) {
-		if (buf[2] == 'h')
-			return (EAV_COMP_BZIP2);
-		else
-			/* Could be bzip 1, but that is unsupported */
-			return (EAV_COMP_UNKNOWN);
-	} else if (len > 2 && buf[0] == 0x1f && buf[1] == 0x8b) {
-		/* gzip per RFC1952 */
-		return (EAV_COMP_GZIP);
-	} else if (len > 6 && buf[0] == 0xfd && buf[1] == '7' &&
-	    buf[2] == 'z' && buf[3] == 'X' &&
-	    buf[4] == 'Z' && buf[5] == 0x00) {
-		/* XZ per Wikipedia */
-		return (EAV_COMP_XZ);
-	} else
-		return (EAV_COMP_UNKNOWN);
-}
-
-static const char *
-eav_strerror(enum eav_error error)
-{
-
-	switch (error) {
-	case EAV_SUCCESS:
-		return "Success";
-	case EAV_ERR_MEM:
-		return "malloc error";
-	case EAV_ERR_DIGEST:
-		return "checksum mismatch";
-	case EAV_ERR_DIGEST_UNKNOWN:
-		return "unknown digest";
-	case EAV_ERR_COMP:
-		return "decompression error";
-	case EAV_ERR_COMP_UNKNOWN:
-		return "Unknown compression type";
-	case EAV_ERR_COMP_UNSUPPORTED:
-		return "Unsupported compression type";
-	default:
-		return "Unknown error";
-	}
-}
-
-static enum eav_error
-extract_and_verify(unsigned char *ibuf, size_t ilen,
-    unsigned char **obufp, size_t *olenp, size_t blocksize,
-    enum eav_compression ctype,
-    enum eav_digest dtype, const unsigned char *digest)
-{
-	int ret;
-	char *obuf = NULL;
-	size_t olen = 0, prev_total_in, total_in, total_out;
-	bz_stream bzs;
-	MD5_CTX md5ctx;
-	char i_md5sum[33];
-
-	switch (ctype) {
-	case EAV_COMP_NONE:
-	case EAV_COMP_BZIP2:
-		break;
-	case EAV_COMP_GZIP:
-	case EAV_COMP_XZ:
-		return (EAV_ERR_COMP_UNSUPPORTED);
-	default:
-		return (EAV_ERR_COMP_UNKNOWN);
-	}
-
-	switch (dtype) {
-	case EAV_DIGEST_NONE:
-	case EAV_DIGEST_MD5:
-		break;
-	default:
-		return (EAV_ERR_DIGEST_UNKNOWN);
-	}
-
-	if (dtype || ctype) {
-		if (dtype == EAV_DIGEST_MD5)
-			MD5Init(&md5ctx);
-
-		if (ctype) {
-			/* XXX: assume bzip2 for now */
-			olen = 1024 * 1024;
-			if ((obuf = malloc(olen)) == NULL)
-				return (EAV_ERR_MEM);
-
-			total_in = 0;
-			prev_total_in = 0;
-
-			bzs.bzalloc = NULL;
-			bzs.bzfree = NULL;
-			bzs.opaque = NULL;
-			bzs.next_in = ibuf;
-			bzs.avail_in = MIN(ilen, 1024 * 1024);
-			bzs.next_out = obuf;
-			bzs.avail_out = olen;
-			if (BZ2_bzDecompressInit(&bzs, 0, 0) != BZ_OK)
-				return (EAV_ERR_COMP);
-
-			while ((ret = BZ2_bzDecompress(&bzs)) !=
-			    BZ_STREAM_END) {
-				if (ret != BZ_OK) {
-					free(obuf);
-					BZ2_bzDecompressEnd(&bzs);
-					return (EAV_ERR_COMP);
-				}
-
-				total_in = ((size_t)bzs.total_in_hi32 << 32) +
-				    bzs.total_in_lo32;
-				total_out = ((size_t)bzs.total_out_hi32 << 32) +
-				    bzs.total_out_lo32;
-
-				if (dtype == EAV_DIGEST_MD5)
-					MD5Update(&md5ctx, ibuf + prev_total_in,
-					    total_in - prev_total_in);
-				prev_total_in = total_in;
-
-				if (bzs.avail_in == 0)
-					bzs.avail_in =
-					    MIN(ilen - total_in, 1024 * 1024);
-
-				if (bzs.avail_out == 0) {
-					olen *= 2;
-					if ((obuf = reallocf(obuf, olen))
-					    == NULL) {
-						BZ2_bzDecompressEnd(&bzs);
-						return (EAV_ERR_COMP);
-					}
-					bzs.next_out = obuf + total_out;
-					bzs.avail_out = olen - total_out;
-				}
-			}
-			BZ2_bzDecompressEnd(&bzs);
-			total_in = ((size_t)bzs.total_in_hi32 << 32) +
-			    bzs.total_in_lo32;
-			total_out = ((size_t)bzs.total_out_hi32 << 32) +
-			    bzs.total_out_lo32;
-
-			/* Push the last read block in the MD5 machine */
-			if (dtype == EAV_DIGEST_MD5)
-				MD5Update(&md5ctx, ibuf + prev_total_in,
-				    total_in - prev_total_in);
-
-			/* Round up to blocksize and zero pad */
-			olen = roundup2(total_out, blocksize);
-			if (olen != total_out)
-				memset(obuf + total_out, '\0',
-				    olen - total_out);
-			/* XXX: realloc to shorten allocation? */
-		} else if (dtype) {
-			if (dtype == EAV_DIGEST_MD5)
-				MD5Update(&md5ctx, ibuf, ilen);
-		}
-
-		if (dtype) {
-			if (dtype == EAV_DIGEST_MD5) {
-				MD5End(&md5ctx, i_md5sum);
-				if (strcmp(digest, i_md5sum) != 0)
-					return (EAV_ERR_DIGEST);
-			}
-		}
-	}
-
-	if (ctype == EAV_COMP_NONE) {
-		*obufp = ibuf;
-		*olenp = ilen;
-	} else {
-		*obufp = obuf;
-		*olenp = olen;
-	}
-	return (EAV_SUCCESS);
-}
-
 static off_t
 parse_offset(const char *offstr)
 {



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201303082357.r28Nvx5R057835>