deal with files < 64k in oshash
This commit is contained in:
parent
c3c9c49788
commit
a2bf2a77d1
1 changed files with 32 additions and 31 deletions
|
@ -20,6 +20,7 @@ def sha1sum(filename):
|
||||||
|
|
||||||
'''
|
'''
|
||||||
os hash - http://trac.opensubtitles.org/projects/opensubtitles/wiki/HashSourceCodes
|
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):
|
def oshash(filename):
|
||||||
try:
|
try:
|
||||||
|
@ -30,24 +31,24 @@ def oshash(filename):
|
||||||
|
|
||||||
filesize = os.path.getsize(filename)
|
filesize = os.path.getsize(filename)
|
||||||
hash = filesize
|
hash = filesize
|
||||||
|
|
||||||
if filesize < 65536:
|
if filesize < 65536:
|
||||||
return "SizeError"
|
for x in range(int(filesize/bytesize)):
|
||||||
|
buffer = f.read(bytesize)
|
||||||
for x in range(int(65536/bytesize)):
|
(l_value,)= struct.unpack(longlongformat, buffer)
|
||||||
buffer = f.read(bytesize)
|
hash += l_value
|
||||||
(l_value,)= struct.unpack(longlongformat, buffer)
|
hash = hash & 0xFFFFFFFFFFFFFFFF #to remain as 64bit number
|
||||||
hash += l_value
|
else:
|
||||||
hash = hash & 0xFFFFFFFFFFFFFFFF #to remain as 64bit number
|
for x in range(int(65536/bytesize)):
|
||||||
|
buffer = f.read(bytesize)
|
||||||
|
(l_value,)= struct.unpack(longlongformat, buffer)
|
||||||
f.seek(max(0,filesize-65536),0)
|
hash += l_value
|
||||||
for x in range(int(65536/bytesize)):
|
hash = hash & 0xFFFFFFFFFFFFFFFF #to remain as 64bit number
|
||||||
buffer = f.read(bytesize)
|
f.seek(max(0,filesize-65536),0)
|
||||||
(l_value,)= struct.unpack(longlongformat, buffer)
|
for x in range(int(65536/bytesize)):
|
||||||
hash += l_value
|
buffer = f.read(bytesize)
|
||||||
hash = hash & 0xFFFFFFFFFFFFFFFF
|
(l_value,)= struct.unpack(longlongformat, buffer)
|
||||||
|
hash += l_value
|
||||||
|
hash = hash & 0xFFFFFFFFFFFFFFFF
|
||||||
f.close()
|
f.close()
|
||||||
returnedhash = "%016x" % hash
|
returnedhash = "%016x" % hash
|
||||||
return returnedhash
|
return returnedhash
|
||||||
|
|
Loading…
Reference in a new issue