From a2bf2a77d14234748da050e16dc4b0cfd57a95e5 Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Sun, 14 Jun 2009 21:22:47 +0200 Subject: [PATCH] deal with files < 64k in oshash --- oxlib/file.py | 63 ++++++++++++++++++++++++++------------------------- 1 file changed, 32 insertions(+), 31 deletions(-) diff --git a/oxlib/file.py b/oxlib/file.py index 0396959..38edfef 100644 --- a/oxlib/file.py +++ b/oxlib/file.py @@ -5,7 +5,7 @@ from __future__ import division import os import hashlib import sys -import struct +import struct def sha1sum(filename): @@ -20,37 +20,38 @@ def sha1sum(filename): ''' os hash - http://trac.opensubtitles.org/projects/opensubtitles/wiki/HashSourceCodes + plus modification for files < 64k, buffer is filled with file data and padded with 0 ''' -def oshash(filename): - try: - longlongformat = 'q' # long long - bytesize = struct.calcsize(longlongformat) - - f = open(filename, "rb") - - filesize = os.path.getsize(filename) - hash = filesize - - if filesize < 65536: - return "SizeError" - - for x in range(int(65536/bytesize)): - buffer = f.read(bytesize) - (l_value,)= struct.unpack(longlongformat, buffer) - hash += l_value - hash = hash & 0xFFFFFFFFFFFFFFFF #to remain as 64bit number - +def oshash(filename): + try: + longlongformat = 'q' # long long + bytesize = struct.calcsize(longlongformat) - f.seek(max(0,filesize-65536),0) - for x in range(int(65536/bytesize)): - buffer = f.read(bytesize) - (l_value,)= struct.unpack(longlongformat, buffer) - hash += l_value - hash = hash & 0xFFFFFFFFFFFFFFFF - - f.close() - returnedhash = "%016x" % hash - return returnedhash - except(IOError): + f = open(filename, "rb") + + filesize = os.path.getsize(filename) + hash = filesize + if filesize < 65536: + for x in range(int(filesize/bytesize)): + buffer = f.read(bytesize) + (l_value,)= struct.unpack(longlongformat, buffer) + hash += l_value + hash = hash & 0xFFFFFFFFFFFFFFFF #to remain as 64bit number + else: + for x in range(int(65536/bytesize)): + buffer = f.read(bytesize) + (l_value,)= struct.unpack(longlongformat, buffer) + hash += l_value + hash = hash & 0xFFFFFFFFFFFFFFFF #to remain as 64bit number + f.seek(max(0,filesize-65536),0) + for x in range(int(65536/bytesize)): + buffer = f.read(bytesize) + (l_value,)= struct.unpack(longlongformat, buffer) + hash += l_value + hash = hash & 0xFFFFFFFFFFFFFFFF + f.close() + returnedhash = "%016x" % hash + return returnedhash + except(IOError): return "IOError"