From c8198418eecf50ed2deec4fbac55eaaf6cdd8a6a Mon Sep 17 00:00:00 2001
From: sinanmohd <sinan@sinanmohd.com>
Date: Sun, 1 Oct 2023 12:00:19 +0530
Subject: lib/util: init opensubtitle_hash

---
 lib/util.lua | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++
 tests.lua    | 11 +++--------
 2 files changed, 53 insertions(+), 8 deletions(-)

diff --git a/lib/util.lua b/lib/util.lua
index a1de817..2ea4708 100644
--- a/lib/util.lua
+++ b/lib/util.lua
@@ -72,11 +72,61 @@ local error = function (str)
 	end
 end
 
+local opensubtitles_hash = function (fileName)
+        local fil = io.open(fileName, "rb")
+	if not fil then
+		return nil
+	end
+
+        local lo,hi = 0,0
+        for i = 1,8192 do
+                local a,b,c,d = fil:read(4):byte(1,4)
+                lo = lo + a + b*256 + c*65536 + d*16777216
+                a,b,c,d = fil:read(4):byte(1,4)
+                hi = hi + a + b*256 + c*65536 + d*16777216
+                while lo >= 4294967296 do
+                        lo = lo - 4294967296
+                        hi = hi + 1
+                end
+                while hi >= 4294967296 do
+                        hi = hi - 4294967296
+                end
+        end
+
+        local size = fil:seek("end", -65536) + 65536
+        for i = 1,8192 do
+                local a,b,c,d = fil:read(4):byte(1,4)
+                lo = lo + a + b*256 + c*65536 + d*16777216
+                a,b,c,d = fil:read(4):byte(1,4)
+                hi = hi + a + b*256 + c*65536 + d*16777216
+                while lo >= 4294967296 do
+                        lo = lo - 4294967296
+                        hi = hi + 1
+                end
+                while hi >= 4294967296 do
+                        hi = hi - 4294967296
+                end
+        end
+
+        lo = lo + size
+	while lo >= 4294967296 do
+		lo = lo - 4294967296
+		hi = hi + 1
+	end
+	while hi >= 4294967296 do
+		hi = hi - 4294967296
+	end
+
+        fil:close()
+        return string.format("%08x%08x", hi,lo), size
+end
+
 return {
 	table_merge = table_merge,
 	table_print = table_print,
 	table_match_or_any = table_match_or_any,
 	zip_ext_first = zip_ext_first,
 	string_rm_vid_ext = string_rm_vid_ext,
+	opensubtitles_hash = opensubtitles_hash,
 	error = error,
 }
diff --git a/tests.lua b/tests.lua
index 8033acb..bc3ddf1 100755
--- a/tests.lua
+++ b/tests.lua
@@ -5,24 +5,19 @@ local util = require 'lib/util'
 
 local errs = 0
 
-local file_hash_verify = function (path, sha256)
-	local newhash = io.popen('sha256sum ' .. path):read(64)
-	return (newhash == sha256)
-end
-
 local test_subscene = function ()
-    local out, sha256, name
+    local out, ohash, name
 
     out = os.tmpname()
     name = 'Fight Club (1999) (1080p BluRay x265 10bit Tigole) [QxR].mp4'
-    sha256 = 'fe88e2c1345a7daed82cb570952c13fae6a6867449f7dd5e719ea0a0c1e2e242'
+    ohash = 'ffec132e13e08f4c'
 
     if not subscene.search(name, out) then
         util.error('subscene: fetch failed')
         errs = errs + 1
     end
 
-    if not file_hash_verify(out, sha256) then
+    if ohash ~= util.opensubtitles_hash(out) then
         util.error('subscene: hash mismatch')
         errs = errs + 1
     end
-- 
cgit v1.2.3