[vlc-devel] [PATCH 3/3] VLSub: Use native opensubtitle hash function

Hugo Beauzée-Luyssen hugo at beauzee.fr
Mon Apr 20 18:59:57 CEST 2015


---
 share/lua/extensions/VLSub.lua | 105 +++--------------------------------------
 1 file changed, 7 insertions(+), 98 deletions(-)

diff --git a/share/lua/extensions/VLSub.lua b/share/lua/extensions/VLSub.lua
index 33bdfea..ecc252e 100644
--- a/share/lua/extensions/VLSub.lua
+++ b/share/lua/extensions/VLSub.lua
@@ -1468,113 +1468,22 @@ openSub = {
       progressBarContent(0))
     
     local item = openSub.getInputItem()
-    
     if not item then
       setError(lang["mess_no_input"])
       return false
     end
-    
-    openSub.getFileInfo()
-      
-    if not openSub.file.path then
-      setError(lang["mess_not_found"])
-      return false
-    end
-    
-    local data_start = ""
-    local data_end = ""
-    local size
-    local chunk_size = 65536
-        
-    -- Get data for hash calculation
-    if openSub.file.is_archive then
-      vlc.msg.dbg("[VLSub] Read hash data from stream")
-    
-      local file = vlc.stream(openSub.file.uri)
-      local dataTmp1 = ""
-      local dataTmp2 = ""
-      size = chunk_size
-      
-      data_start = file:read(chunk_size)
-      
-      while data_end do
-        size = size + string.len(data_end)
-        dataTmp1 = dataTmp2
-        dataTmp2 = data_end
-        data_end = file:read(chunk_size)
-        collectgarbage()
-      end
-      data_end = string.sub((dataTmp1..dataTmp2), -chunk_size)
-    elseif not file_exist(openSub.file.path) 
-    and openSub.file.stat then
-      vlc.msg.dbg("[VLSub] Read hash data from stream")
-      
-      local file = vlc.stream(openSub.file.uri)
-      
-      if not file then
-        vlc.msg.dbg("[VLSub] No stream")
-        return false
-      end
-      
-      size = openSub.file.stat.size
-      local decal = size%chunk_size
-      
-      data_start = file:read(chunk_size)
-      
-      -- "Seek" to the end 
-      file:read(decal)
-      
-      for i = 1, math.floor(((size-decal)/chunk_size))-2 do
-        file:read(chunk_size)
-      end
-      
-      data_end = file:read(chunk_size)
-        
-      file = nil
-    else
-      vlc.msg.dbg("[VLSub] Read hash data from file")
-      local file = io.open( openSub.file.path, "rb")
-      if not file then
-        vlc.msg.dbg("[VLSub] No stream")
+
+    local hash, size = vlc.hash.open_subtitles( item:uri() )
+    if hash == nil then
+        setError("Failed to compute hash")
         return false
-      end
-      
-      data_start = file:read(chunk_size)
-      size = file:seek("end", -chunk_size) + chunk_size
-      data_end = file:read(chunk_size)
-      file = nil
     end
-    
-  -- Hash calculation
-    local lo = size
-    local hi = 0
-    local o,a,b,c,d,e,f,g,h
-    local hash_data = data_start..data_end
-    local max_size = 4294967296
-    local overflow
-    
-    for i = 1,  #hash_data, 8 do
-      a,b,c,d,e,f,g,h = hash_data:byte(i,i+7)
-      lo = lo + a + b*256 + c*65536 + d*16777216
-      hi = hi + e + f*256 + g*65536 + h*16777216
-      
-      if lo > max_size then
-        overflow = math.floor(lo/max_size)
-        lo = lo-(overflow*max_size)
-        hi = hi+overflow
-      end
-      
-      if hi > max_size then
-        overflow = math.floor(hi/max_size)
-        hi = hi-(overflow*max_size)
-      end
-    end
-    
+
+    openSub.file.hash = hash
     openSub.file.bytesize = size
-    openSub.file.hash = string.format("%08x%08x", hi,lo)
+
     vlc.msg.dbg("[VLSub] Video hash: "..openSub.file.hash)
     vlc.msg.dbg("[VLSub] Video bytesize: "..size)
-    collectgarbage()
     return true
   end,
   checkSession = function()
-- 
2.1.0




More information about the vlc-devel mailing list