Merge code from upstream libtar + bug fixes

All updates and fixes applied from upstream libtar as of
March 1, 2016.

Debug flag is disabled, however non-debug output now
provides 1 line of useful output per object extracted.

I've also merged some fixes from CyanogenMod's
fork of libtar:

From: Tom Marshall <tdm@cyngn.com>
Date: Thu, 11 Feb 2016 16:24:40 -0800
Subject: libtar: Cleanup, secure, and extend numeric fields
Commit: e18b457ea1cbf6be1adc3b75450ed1c737cd82ea

From: Tom Marshall <tdm@cyngn.com>
Date: Thu, 11 Feb 2016 12:49:30 -0800
Subject: libtar: Make file sizes 64-bit clean
Commit: e628c2025549a24018bc568351465130a05daafb

From: Tom Marshall <tdm@cyngn.com>
Date: Thu, 17 Apr 2014 09:39:25 -0700
Subject: libtar: Add methods for in-memory files
Commit: 8ec5627a8ff0a91724c6d5b344f0e887da922527

From: Tom Marshall <tdm@cyngn.com>
Date: Wed, 2 Jul 2014 09:34:40 -0700
Subject: libtar: Fix hardlink extract
Commit: 166d83a51e0c51abcea37694dbd7df92d03c1f56

From: philz-cwm6 <phytowardt@gmail.com>
Date: Sat, 26 Apr 2014 01:11:35 +0200
Subject: libtar: Various bug fixes and enhancements
Commit: a271d763e94235ccee9ecaabdb52bf4b9b2f8c06
(Some of this was not merged in, as better solutions were
available from upstream libtar)

From: Tom Marshall <tdm@cyngn.com>
Date: Wed, 9 Apr 2014 09:35:54 -0700
Subject: libtar: Add const qualifiers to reduce compile warnings
Commit: 0600afa19fe827d06d3fcf24a7aabd52dbf487b4

Change-Id: I6d008cb6fdf950f835bbed63aeb8727cc5c86083
diff --git a/libtar/encode.c b/libtar/encode.c
index 662eff5..c937152 100644
--- a/libtar/encode.c
+++ b/libtar/encode.c
@@ -68,10 +68,11 @@
 
 /* encode file path */
 void
-th_set_path(TAR *t, char *pathname)
+th_set_path(TAR *t, const char *pathname)
 {
 	char suffix[2] = "";
 	char *tmp;
+	size_t pathname_len = strlen(pathname);
 
 #ifdef DEBUG
 	printf("in th_set_path(th, pathname=\"%s\")\n", pathname);
@@ -81,32 +82,49 @@
 		free(t->th_buf.gnu_longname);
 	t->th_buf.gnu_longname = NULL;
 
-	if (pathname[strlen(pathname) - 1] != '/' && TH_ISDIR(t))
+	/* old archive compatibility (not needed for gnu): add trailing / to directories */
+	if (pathname[pathname_len - 1] != '/' && TH_ISDIR(t))
 		strcpy(suffix, "/");
 
-	if (strlen(pathname) > T_NAMELEN-1 && (t->options & TAR_GNU))
+	if (pathname_len >= T_NAMELEN && (t->options & TAR_GNU))
 	{
-		/* GNU-style long name */
+		/* GNU-style long name (no file name length limit) */
 		t->th_buf.gnu_longname = strdup(pathname);
 		strncpy(t->th_buf.name, t->th_buf.gnu_longname, T_NAMELEN);
 	}
-	else if (strlen(pathname) > T_NAMELEN)
+	else if (pathname_len >= T_NAMELEN)
 	{
-		/* POSIX-style prefix field */
-		tmp = strchr(&(pathname[strlen(pathname) - T_NAMELEN - 1]), '/');
+		/* POSIX-style prefix field:
+		 *   The maximum length of a file name is limited to 256 characters,
+		 *   provided that the file name can be split at a directory separator
+		 *   in two parts.  The first part being at most 155 bytes long and
+		 *   the second part being at most 100 bytes long.  So, in most cases
+		 *   the maximum file name length will be shorter than 256 characters.
+		 */
+		char tail_path[T_NAMELEN + 1];
+		tmp = strchr(&(pathname[pathname_len - T_NAMELEN]), '/');
 		if (tmp == NULL)
 		{
 			printf("!!! '/' not found in \"%s\"\n", pathname);
 			return;
 		}
-		snprintf(t->th_buf.name, 100, "%s%s", &(tmp[1]), suffix);
-		snprintf(t->th_buf.prefix,
-			 ((tmp - pathname + 1) <
-			  155 ? (tmp - pathname + 1) : 155), "%s", pathname);
+		snprintf(tail_path, T_NAMELEN + 1, "%s%s", &tmp[1], suffix);
+		strncpy(t->th_buf.name, tail_path, T_NAMELEN);
+
+		/*
+		 * first part, max = 155 == sizeof(t->th_buf.prefix) , include NULL if it fits
+		 * trailing '/' is added during decode: decode.c/th_get_pathname()
+		 */
+		if (tmp - pathname >= 155) {
+			strncpy(t->th_buf.prefix, pathname, 155);
+		} else {
+			snprintf(t->th_buf.prefix, (tmp - pathname + 1), "%s", pathname);
+		}
 	}
-	else
-		/* classic tar format */
-		snprintf(t->th_buf.name, 100, "%s%s", pathname, suffix);
+	else {
+		/* any short name for all formats, or classic tar format (99 chars max) */
+		snprintf(t->th_buf.name, T_NAMELEN, "%s%s", pathname, suffix);
+	}
 
 #ifdef DEBUG
 	puts("returning from th_set_path()...");
@@ -116,23 +134,28 @@
 
 /* encode link path */
 void
-th_set_link(TAR *t, char *linkname)
+th_set_link(TAR *t, const char *linkname)
 {
 #ifdef DEBUG
 	printf("==> th_set_link(th, linkname=\"%s\")\n", linkname);
 #endif
 
-	if (strlen(linkname) > T_NAMELEN-1 && (t->options & TAR_GNU))
+	if (strlen(linkname) >= T_NAMELEN && (t->options & TAR_GNU))
 	{
-		/* GNU longlink format */
+		/* --format=gnu: GNU-style long name (no file name length limit) */
 		t->th_buf.gnu_longlink = strdup(linkname);
 		strcpy(t->th_buf.linkname, "././@LongLink");
 	}
-	else
+	else if (strlen(linkname) >= T_NAMELEN)
 	{
-		/* classic tar format */
-		strlcpy(t->th_buf.linkname, linkname,
-			sizeof(t->th_buf.linkname));
+		/* --format=ustar: 100 chars max limit for symbolic links */
+		strncpy(t->th_buf.linkname, linkname, T_NAMELEN);
+		if (t->th_buf.gnu_longlink != NULL)
+			free(t->th_buf.gnu_longlink);
+		t->th_buf.gnu_longlink = NULL;
+	} else {
+		/* all short links or v7 tar format: The maximum length of a symbolic link name is limited to 99 characters */
+		snprintf(t->th_buf.linkname, T_NAMELEN, "%s", linkname);
 		if (t->th_buf.gnu_longlink != NULL)
 			free(t->th_buf.gnu_longlink);
 		t->th_buf.gnu_longlink = NULL;
@@ -159,9 +182,11 @@
 {
 	struct passwd *pw;
 
-	pw = getpwuid(uid);
-	if (pw != NULL)
-		strlcpy(t->th_buf.uname, pw->pw_name, sizeof(t->th_buf.uname));
+	if (!(t->options & TAR_USE_NUMERIC_ID)) {
+		pw = getpwuid(uid);
+		if (pw != NULL)
+			strlcpy(t->th_buf.uname, pw->pw_name, sizeof(t->th_buf.uname));
+	}
 
 	int_to_oct(uid, t->th_buf.uid, 8);
 }
@@ -173,9 +198,11 @@
 {
 	struct group *gr;
 
-	gr = getgrgid(gid);
-	if (gr != NULL)
-		strlcpy(t->th_buf.gname, gr->gr_name, sizeof(t->th_buf.gname));
+	if (!(t->options & TAR_USE_NUMERIC_ID)) {
+		gr = getgrgid(gid);
+		if (gr != NULL)
+			strlcpy(t->th_buf.gname, gr->gr_name, sizeof(t->th_buf.gname));
+	}
 
 	int_to_oct(gid, t->th_buf.gid, 8);
 }
@@ -209,5 +236,3 @@
 	else
 		th_set_size(t, 0);
 }
-
-