Module Name:    src
Committed By:   christos
Date:           Fri Nov 30 00:53:11 UTC 2018

Modified Files:
        src/bin/pax: gen_subs.c tar.c

Log Message:
Add base-256 decoding support (Micha Gorny)


To generate a diff of this commit:
cvs rdiff -u -r1.36 -r1.37 src/bin/pax/gen_subs.c
cvs rdiff -u -r1.73 -r1.74 src/bin/pax/tar.c

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

Modified files:

Index: src/bin/pax/gen_subs.c
diff -u src/bin/pax/gen_subs.c:1.36 src/bin/pax/gen_subs.c:1.37
--- src/bin/pax/gen_subs.c:1.36	Thu Aug  9 04:09:21 2012
+++ src/bin/pax/gen_subs.c	Thu Nov 29 19:53:11 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: gen_subs.c,v 1.36 2012/08/09 08:09:21 christos Exp $	*/
+/*	$NetBSD: gen_subs.c,v 1.37 2018/11/30 00:53:11 christos Exp $	*/
 
 /*-
  * Copyright (c) 1992 Keith Muller.
@@ -42,7 +42,7 @@
 #if 0
 static char sccsid[] = "@(#)gen_subs.c	8.1 (Berkeley) 5/31/93";
 #else
-__RCSID("$NetBSD: gen_subs.c,v 1.36 2012/08/09 08:09:21 christos Exp $");
+__RCSID("$NetBSD: gen_subs.c,v 1.37 2018/11/30 00:53:11 christos Exp $");
 #endif
 #endif /* not lint */
 
@@ -306,12 +306,10 @@ u32_asc(uintmax_t val, char *str, int le
 
 /*
  * asc_umax()
- *	convert hex/octal character string into a uintmax. We do
- *	not have to to check for overflow! (the headers in all supported
- *	formats are not large enough to create an overflow).
+ *	convert hex/octal/base-256 value into a uintmax.
  *	NOTE: strings passed to us are NOT TERMINATED.
  * Return:
- *	uintmax_t value
+ *	uintmax_t value; UINTMAX_MAX for overflow/negative
  */
 
 uintmax_t
@@ -323,6 +321,30 @@ asc_umax(char *str, int len, int base)
 	stop = str + len;
 
 	/*
+	 * if the highest bit of first byte is set, it's base-256 encoded
+	 * (base-256 is basically (n-1)-bit big endian signed
+	 */
+	if (str < stop && (*str & 0x80)) {
+		/*
+		 * uintmax_t can't be negative, so fail on negative numbers
+		 */
+		if (*str & 0x40)
+			return UINTMAX_MAX;
+
+		tval = *str++ & 0x3f;
+		while (str < stop) {
+			/*
+			 * check for overflow
+			 */
+			if (tval > (UINTMAX_MAX/256))
+				return UINTMAX_MAX;
+			tval = (tval << 8) | ((*str++) & 0xFF);
+		}
+
+		return tval;
+	}
+
+	/*
 	 * skip over leading blanks and zeros
 	 */
 	while ((str < stop) && ((*str == ' ') || (*str == '0')))

Index: src/bin/pax/tar.c
diff -u src/bin/pax/tar.c:1.73 src/bin/pax/tar.c:1.74
--- src/bin/pax/tar.c:1.73	Sat Dec 19 13:28:54 2015
+++ src/bin/pax/tar.c	Thu Nov 29 19:53:11 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: tar.c,v 1.73 2015/12/19 18:28:54 christos Exp $	*/
+/*	$NetBSD: tar.c,v 1.74 2018/11/30 00:53:11 christos Exp $	*/
 
 /*-
  * Copyright (c) 1992 Keith Muller.
@@ -42,7 +42,7 @@
 #if 0
 static char sccsid[] = "@(#)tar.c	8.2 (Berkeley) 4/18/94";
 #else
-__RCSID("$NetBSD: tar.c,v 1.73 2015/12/19 18:28:54 christos Exp $");
+__RCSID("$NetBSD: tar.c,v 1.74 2018/11/30 00:53:11 christos Exp $");
 #endif
 #endif /* not lint */
 
@@ -486,6 +486,8 @@ tar_rd(ARCHD *arcn, char *buf)
 	arcn->sb.st_uid = (uid_t)asc_u32(hd->uid, sizeof(hd->uid), OCT);
 	arcn->sb.st_gid = (gid_t)asc_u32(hd->gid, sizeof(hd->gid), OCT);
 	arcn->sb.st_size = (off_t)ASC_OFFT(hd->size, sizeof(hd->size), OCT);
+	if (arcn->sb.st_size == -1)
+		return -1;
 	arcn->sb.st_mtime = (time_t)(int32_t)asc_u32(hd->mtime, sizeof(hd->mtime), OCT);
 	arcn->sb.st_ctime = arcn->sb.st_atime = arcn->sb.st_mtime;
 
@@ -860,6 +862,8 @@ ustar_rd(ARCHD *arcn, char *buf)
 	arcn->sb.st_mode = (mode_t)(asc_u32(hd->mode, sizeof(hd->mode), OCT) &
 	    0xfff);
 	arcn->sb.st_size = (off_t)ASC_OFFT(hd->size, sizeof(hd->size), OCT);
+	if (arcn->sb.st_size == -1)
+		return -1;
 	arcn->sb.st_mtime = (time_t)(int32_t)asc_u32(hd->mtime, sizeof(hd->mtime), OCT);
 	arcn->sb.st_ctime = arcn->sb.st_atime = arcn->sb.st_mtime;
 

Reply via email to