shithub: tarsum

Download patch

ref: 059f9a658d20e467bba3f42e574ed381ef0c99d7
author: B. Atticus Grobe <grobe0ba@gmail.com>
date: Fri Dec 17 21:47:24 EST 2021

initial commit

--- /dev/null
+++ b/tarsum
@@ -1,0 +1,59 @@
+#!/usr/bin/env luajit
+
+-- vim: filetype=lua :
+
+local function usage()
+    io.stderr:write("Usage: tarsum <filename>\n")
+    os.exit(1)
+end
+
+if #arg < 1 then
+    usage()
+end
+
+local tarball = io.open(arg[1], "r+b")
+if tarball == nil then
+    usage()
+    error("Unable to open file!")
+end
+
+local header = tarball:read(0x132)
+if header == fail then
+    error("Unable to read file.")
+elseif #header ~= 0x132 then
+    error("Invalid header! (Is this really a tarball?)")
+end
+
+local headertbl = {}
+for i = 1, #header, 1 do
+    table.insert(headertbl, string.sub(header, i, i))
+end
+
+for i = 0x94, 0x9b, 1 do
+    headertbl[i] = " "
+end
+
+header = ""
+for k, v in ipairs(headertbl) do
+    header = header .. v
+end
+
+local sum = 0
+for i = 1, #header, 1 do
+    sum = sum + string.byte(header,i)
+end
+
+print(string.format("Checksum: %.7o\n", sum))
+
+if tarball:seek("set", 0x94) ~= 0x94 then
+    error("Could not seek to 0x94")
+end
+
+tarball:write(string.format("%.7o", sum))
+if tarball:seek("set", 0x9b) ~= 0x9b then
+    error("Could not seek to 0x9b")
+end
+
+tarball:write(string.format("%c", 0))
+tarball:flush()
+tarball:close()
--- /dev/null
+++ b/tarsum.fnl
@@ -1,0 +1,29 @@
+#!/usr/bin/env fennel
+;; vim: set filetype=scheme :
+
+(fn usage []
+    (io.stderr:write "Usage: tarsum <filename>\n")
+    (os.exit 1))
+
+(if (< (# arg) 1) (usage))
+
+;; (local tarball (io.open (. arg 1) "r+b"))
+
+(with-open (tarball (io.open (. arg 1) "r+b"))
+(let [header (tarball:read 0x132) headertbl {}]
+  (if (= header fail) (error "Unable to read file."))
+  (if (~= (# header) 0x132) (error "Invalid header! (Is this really a tarball?)"))
+  (for [i 1 (# header) 1] (table.insert headertbl (string.sub header i i)))
+  (for [i 0x94 0x9b 1] (tset headertbl i " "))
+  (var header "")
+  (each [k v (ipairs headertbl)] (set header (.. header v)))
+  (var sum 0)
+  (for [i 1 (# header) 1] (set sum (+ sum (string.byte header i))))
+  (print (string.format "Checksum: %.7o\n" sum))
+  (if (~= (tarball:seek "set" 0x94) 0x94) (error "Could not seek to 0x94"))
+  (tarball:write (string.format "%.7o" sum))
+  (if (~= (tarball:seek "set" 0x9b) 0x9b) (error "Could not seek to 0x9b"))
+  (tarball:write (string.format "%c" 0))
+  (tarball:flush)
+  0
+  ))