Module Name:    othersrc
Committed By:   agc
Date:           Thu Aug 22 01:08:10 UTC 2013

Modified Files:
        othersrc/external/bsd/multigest/dist: multigest.c multigest.h

Log Message:
First pass at isolating userland-only code.

Also, only expose a regular expression if we're not in kernel code


To generate a diff of this commit:
cvs rdiff -u -r1.5 -r1.6 othersrc/external/bsd/multigest/dist/multigest.c \
    othersrc/external/bsd/multigest/dist/multigest.h

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/multigest/dist/multigest.c
diff -u othersrc/external/bsd/multigest/dist/multigest.c:1.5 othersrc/external/bsd/multigest/dist/multigest.c:1.6
--- othersrc/external/bsd/multigest/dist/multigest.c:1.5	Sun Aug 18 06:03:14 2013
+++ othersrc/external/bsd/multigest/dist/multigest.c	Thu Aug 22 01:08:10 2013
@@ -26,6 +26,10 @@
 #include <sys/stat.h>
 #include <sys/mman.h>
 
+#ifdef _KERNEL
+#include <inttypes.h>
+#include <string.h>
+#else
 #include <ctype.h>
 #include <inttypes.h>
 #include <regex.h>
@@ -33,6 +37,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#endif
 
 /* digests */
 #include "md5.h"
@@ -376,19 +381,36 @@ findalg(const char *algname)
 	return NULL;
 }
 
-/* percent encode (pseudo-RFC1738) a string */
-static void
-pcstring(FILE *fp, const char *s)
+/* normalise through regexp substitution */
+static int
+normalise(multigest_t *multigest, const char *data, size_t len, int64_t *from)
 {
-	static const char	*pcencodes = "%$\r\n\t ";
+#ifndef _KERNEL
+	multigest_dig_t	*d;
+	regmatch_t	 match[2];
+	uint32_t	 i;
 
-	for ( ; *s ; s++) {
-		if (strchr(pcencodes, *s) == NULL) {
-			fprintf(fp, "%c", *s);
-		} else {
-			fprintf(fp, "%%%02hhx", *s);
+	*from = 0;
+	while (multigest->r && len > 0) {
+		match[0].rm_so = *from;
+		match[0].rm_eo = len;
+		if (regexec(multigest->r, data, 2, match, REG_STARTEND) != 0) {
+			break;
 		}
+		for (d = multigest->digs, i = 0 ; i < multigest->digc ; i++, d++) {
+			(*d->update)(&multigest->ctx[d->ctxoff], &data[*from],
+				(unsigned)(match[0].rm_so - *from));
+			if (multigest->repllen) {
+				(*d->update)(&multigest->ctx[d->ctxoff], multigest->repl,
+					multigest->repllen);
+			}
+		}
+		*from = match[0].rm_eo;
 	}
+#else
+	*from = 0;
+#endif
+	return 1;
 }
 
 /***************************************************************************/
@@ -449,7 +471,8 @@ int
 multigest_add_subst(multigest_t *multigest, const char *from, const char *to)
 {
 	if (multigest && from && from[0]) {
-		if (regcomp(&multigest->r, from, REG_EXTENDED) != 0) {
+		if ((multigest->r = calloc(1, sizeof(regex_t))) == NULL ||
+		    regcomp(multigest->r, from, REG_EXTENDED) != 0) {
 			return 0;
 		}
 		multigest->pat = strdup(from);
@@ -467,27 +490,11 @@ void
 multigest_update(multigest_t *multigest, const char *data, size_t len)
 {
 	multigest_dig_t	*d;
-	regmatch_t	 match[2];
-	regoff_t	 from;
 	uint32_t	 i;
+	int64_t	 	 from;
 
 	if (multigest && data) {
-		for (from = 0; len > 0 ; ) {
-			match[0].rm_so = from;
-			match[0].rm_eo = len;
-			if (regexec(&multigest->r, data, 2, match, REG_STARTEND) != 0) {
-				break;
-			}
-			for (d = multigest->digs, i = 0 ; i < multigest->digc ; i++, d++) {
-				(*d->update)(&multigest->ctx[d->ctxoff], &data[from],
-					(unsigned)(match[0].rm_so - from));
-				if (multigest->repllen) {
-					(*d->update)(&multigest->ctx[d->ctxoff], multigest->repl,
-						multigest->repllen);
-				}
-			}
-			from = match[0].rm_eo;
-		}
+		normalise(multigest, data, len, &from);
 		for (d = multigest->digs, i = 0 ; i < multigest->digc ; i++, d++) {
 			(*d->update)(&multigest->ctx[d->ctxoff], &data[from], (unsigned)(len - from));
 		}
@@ -529,67 +536,6 @@ multigest_data(const char *alg, const ch
 	return NULL;
 }
 
-/* run sed, then digest on a file */
-uint8_t *
-multigest_file(const char *alg, const char *f, uint8_t *raw, const char *pat, const char *repl)
-{
-	struct stat	 st;
-	ssize_t		 rc;
-	size_t		 size;
-	size_t		 cc;
-	char		*mapped;
-	FILE		*fp;
-
-	if (f && alg && raw) {
-		if ((fp = fopen(f, "r")) == NULL) {
-			fprintf(stderr, "can't open '%s'\n", f);
-			return 0;
-		}
-		fstat(fileno(fp), &st);
-		size = st.st_size;
-		mapped = mmap(NULL, size, PROT_READ, MAP_SHARED, fileno(fp), 0);
-		if (mapped == MAP_FAILED) {
-			mapped = calloc(1, MB(1));
-			for (cc = 0 ; cc < size ; cc += rc) {
-				if ((rc = read(fileno(fp), mapped, MB(1))) <= 0) {
-					break;
-				}
-				multigest_data(alg, mapped, (size_t)rc, raw, pat, repl);
-			}
-			free(mapped);
-		} else {
-			multigest_data(alg, mapped, size, raw, pat, repl);
-			munmap(mapped, size);
-		}
-		fclose(fp);
-		return raw;
-	}
-	return NULL;
-}
-
-/* free resources used in a sedded digest */
-void
-multigest_free(multigest_t *s)
-{
-	uint32_t	i;
-
-	if (s) {
-		if (s->ctx) {
-			free(s->ctx);
-		}
-		if (s->pat) {
-			free(s->pat);
-			regfree(&s->r);
-		}
-		if (s->repl) {
-			free(s->repl);
-		}
-		for (i = 0 ; i < s->digc ; i++) {
-			free(s->digs[i].alg);
-		}
-	}
-}
-
 /* percent decode (pseudo-RFC1738) a string */
 void
 multigest_unpcstring(const char *in, size_t isize, char *out, size_t osize)
@@ -605,7 +551,7 @@ multigest_unpcstring(const char *in, siz
 			    (p[1] = strchr(hexes, i[2])) == NULL) {
 				break;
 			}
-			*o = ((p[0] - hexes) * 16) + (p[1] - hexes);
+			*o = ((char)(p[0] - hexes) * 16) + (p[1] - hexes);
 			i += 3;
 		} else {
 			*o = *i++;
@@ -637,6 +583,31 @@ multigest_format_hex(uint8_t *raw, const
 	return (int)(rawsize + rawsize + 1);
 }
 
+/* return the size of output array we'll need */
+uint32_t
+multigest_get_rawsize(multigest_t *multigest)
+{
+	return (multigest) ? (uint32_t)multigest->rawsize : 0;
+}
+
+/*****************************************************************/
+
+#ifndef _KERNEL
+/* percent encode (pseudo-RFC1738) a string */
+static void
+pcstring(FILE *fp, const char *s)
+{
+	static const char	*pcencodes = "%$\r\n\t ";
+
+	for ( ; *s ; s++) {
+		if (strchr(pcencodes, *s) == NULL) {
+			fprintf(fp, "%c", *s);
+		} else {
+			fprintf(fp, "%%%02hhx", *s);
+		}
+	}
+}
+
 /* print as hex string */
 int
 multigest_print_hex(uint8_t *raw, const char *algname, const char *outname,
@@ -693,11 +664,65 @@ multigest_print_hex(uint8_t *raw, const 
 	return 1;
 }
 
-/* return the size of output array we'll need */
-uint32_t
-multigest_get_rawsize(multigest_t *multigest)
+/* run sed, then digest on a file */
+uint8_t *
+multigest_file(const char *alg, const char *f, uint8_t *raw, const char *pat, const char *repl)
 {
-	return (multigest) ? (uint32_t)multigest->rawsize : 0;
+	struct stat	 st;
+	ssize_t		 rc;
+	size_t		 size;
+	size_t		 cc;
+	char		*mapped;
+	FILE		*fp;
+
+	if (f && alg && raw) {
+		if ((fp = fopen(f, "r")) == NULL) {
+			fprintf(stderr, "can't open '%s'\n", f);
+			return 0;
+		}
+		fstat(fileno(fp), &st);
+		size = st.st_size;
+		mapped = mmap(NULL, size, PROT_READ, MAP_SHARED, fileno(fp), 0);
+		if (mapped == MAP_FAILED) {
+			mapped = calloc(1, MB(1));
+			for (cc = 0 ; cc < size ; cc += rc) {
+				if ((rc = read(fileno(fp), mapped, MB(1))) <= 0) {
+					break;
+				}
+				multigest_data(alg, mapped, (size_t)rc, raw, pat, repl);
+			}
+			free(mapped);
+		} else {
+			multigest_data(alg, mapped, size, raw, pat, repl);
+			munmap(mapped, size);
+		}
+		fclose(fp);
+		return raw;
+	}
+	return NULL;
 }
 
-/*****************************************************************/
+/* free resources used in a sedded digest */
+void
+multigest_free(multigest_t *s)
+{
+	uint32_t	i;
+
+	if (s) {
+		if (s->ctx) {
+			free(s->ctx);
+		}
+		if (s->pat) {
+			free(s->pat);
+			regfree(s->r);
+		}
+		if (s->repl) {
+			free(s->repl);
+		}
+		for (i = 0 ; i < s->digc ; i++) {
+			free(s->digs[i].alg);
+		}
+	}
+}
+#endif
+
Index: othersrc/external/bsd/multigest/dist/multigest.h
diff -u othersrc/external/bsd/multigest/dist/multigest.h:1.5 othersrc/external/bsd/multigest/dist/multigest.h:1.6
--- othersrc/external/bsd/multigest/dist/multigest.h:1.5	Sun Aug 18 06:03:14 2013
+++ othersrc/external/bsd/multigest/dist/multigest.h	Thu Aug 22 01:08:10 2013
@@ -28,7 +28,6 @@
 #include <sys/types.h>
 
 #include <inttypes.h>
-#include <regex.h>
 
 typedef void	(*mg_initfunc_t)(void *);
 typedef void	(*mg_updatefunc_t)(void *, const char *, unsigned);
@@ -47,7 +46,7 @@ typedef struct multigest_dig_t {
 
 /* a multiple digest struct */
 typedef struct multigest_t {
-	regex_t		 r;		/* regular expression for matching */
+	void		*r;		/* regular expression for matching */
 	char		*pat;		/* pattern we match */
 	char		*repl;		/* replacement text */
 	unsigned	 repllen;	/* cached length of replacement */

Reply via email to