aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2019-06-01 17:16:08 -0500
committerRob Landley <rob@landley.net>2019-06-01 17:16:08 -0500
commitae3e3ae4a00b3ed4d763aea498e7e5e0eb137f1e (patch)
treeb9bd1ee63e529b759739309b9f7513777e3b37bc /lib
parent8994b82280f0b562ed70d9ae38f1a5103b2791f2 (diff)
downloadtoybox-ae3e3ae4a00b3ed4d763aea498e7e5e0eb137f1e.tar.gz
Teach file to recognize xz archives and old style tarballs.
Diffstat (limited to 'lib')
-rw-r--r--lib/lib.c23
-rw-r--r--lib/lib.h2
2 files changed, 25 insertions, 0 deletions
diff --git a/lib/lib.c b/lib/lib.c
index b5825fd2..ffb24f65 100644
--- a/lib/lib.c
+++ b/lib/lib.c
@@ -1416,3 +1416,26 @@ void loggit(int priority, char *format, ...)
va_end(va);
closelog();
}
+
+// Calculate tar packet checksum, with cksum field treated as 8 spaces
+unsigned tar_cksum(void *data)
+{
+ unsigned i, cksum = 8*' ';
+
+ for (i = 0; i<500; i += (i==147) ? 9 : 1) cksum += ((char *)data)[i];
+
+ return cksum;
+}
+
+// is this a valid tar header?
+int is_tar_header(void *pkt)
+{
+ char *p = pkt;
+ int i = 0;
+
+ if (p[257] && memcmp("ustar", p+257, 5)) return 0;
+ if (p[148] != '0') return 0;
+ sscanf(p+148, "%8o", &i);
+
+ return i && tar_cksum(pkt) == i;
+}
diff --git a/lib/lib.h b/lib/lib.h
index 5ce29380..080c533f 100644
--- a/lib/lib.h
+++ b/lib/lib.h
@@ -265,6 +265,8 @@ long long millitime(void);
char *format_iso_time(char *buf, size_t len, struct timespec *ts);
void reset_env(struct passwd *p, int clear);
void loggit(int priority, char *format, ...);
+unsigned tar_cksum(void *data);
+int is_tar_header(void *pkt);
#define HR_SPACE 1 // Space between number and units
#define HR_B 2 // Use "B" for single byte units