-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlznt1.py
executable file
·129 lines (115 loc) · 3.62 KB
/
lznt1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#! /usr/bin/python3
import struct
import sys
import copy
def _decompress_chunk(chunk):
out = bytes()
while chunk:
flags = ord(chunk[0:1])
chunk = chunk[1:]
for i in range(8):
if not (flags >> i & 1):
out += chunk[0:1]
chunk = chunk[1:]
else:
flag = struct.unpack('<H', chunk[:2])[0]
pos = len(out) - 1
l_mask = 0xFFF
o_shift = 12
while pos >= 0x10:
l_mask >>= 1
o_shift -= 1
pos >>= 1
length = (flag & l_mask) + 3
offset = (flag >> o_shift) + 1
if length >= offset:
tmp = out[-offset:] * int(0xFFF / len(out[-offset:]) + 1)
out += tmp[:length]
else:
out += out[-offset:-offset+length]
chunk = chunk[2:]
if len(chunk) == 0:
break
return out
def decompress(buf, length_check=True):
out = bytes()
while len(buf)>=2:
header = struct.unpack('<H', buf[:2])[0]
if header==0: break # FIXME?
length = (header & 0xFFF) + 1
# print("LZNT1: hdr=%d len=%d"%(header>>12,length))
if length_check and length > len(buf[2:]):
raise ValueError('invalid chunk length')
else:
chunk = buf[2:2+length]
if header & 0x8000:
out += _decompress_chunk(chunk)
else:
out += chunk
buf = buf[2+length:]
return out
def decomp2(buf,compr=0):
try:
inlen=0
out = bytes()
while len(buf)>=2:
header = struct.unpack('<H', buf[:2])[0]
# if header==0: break # FIXME?
# 5 hdr=0 hibasak!
# 1602524 hdr=11
# 314996 hdr=3
length = (header & 0xFFF) + 1
if (header&0x7000)!=0x3000: # a felso 4 bit 3 vagy 11 szokott lenni...
if header!=0: print("LZNT1: hdr=%d len=%d"%(header>>12,length))
break
if length > len(buf[2:]):
raise ValueError('invalid chunk length')
else:
chunk = buf[2:2+length]
if header & 0x8000:
out += _decompress_chunk(chunk)
else:
out += chunk
if compr and len(out)>compr: ValueError('invalid output length')
# if len(out)==compr: break # megvagyunk!
buf = buf[2+length:]
inlen+=2+length
return out,inlen
except Exception as e:
print(repr(e));
return buf,-1
if __name__ == "__main__":
# data=open("know.pdf.nt1","rb").read()
# outf=open("know.pdf","wb")
# size=204246
data=open("docfix.doc.NT","rb").read()
outf=open("docfix.doc","wb")
size=4159488
compr=65536
blksize=4096
total=0
pos=0
while total<size:
# detect if compressed!
out,inlen=decomp2(data[pos:pos+compr],compr)
print("pos=0x%X inlen=%d size=%d"%(pos,inlen,len(out)))
if inlen==0 and len(out)==0:
print("0x%08X: zero padding block found!"%(pos))
pos+=blksize
continue
if inlen<=0 or len(out)!=compr:
# probably uncompressed!
out=data[pos:pos+compr]
inlen=compr
print("0x%08X: uncompressed block found! size=%d"%(pos,compr))
else:
# tomoritett block
pad=inlen&(blksize-1)
if pad: pad=blksize-pad
print("0x%08X: compressed block found! size=%d+%d (%d blocks)"%(pos,inlen,pad,(inlen+pad)//blksize))
inlen+=pad
# inlen=65536 # ha nem sparse
pos+=inlen
outf.write(out)
total+=len(out)
outf.truncate(size)