aboutsummaryrefslogtreecommitdiff
path: root/lib/lib.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/lib.c')
-rw-r--r--lib/lib.c23
1 files changed, 23 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;
+}