-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathqcfm_parse.py
More file actions
executable file
·83 lines (72 loc) · 2.13 KB
/
qcfm_parse.py
File metadata and controls
executable file
·83 lines (72 loc) · 2.13 KB
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
#!/usr/bin/python
from struct import unpack
from sys import exit
from sys import argv
import os
def parseHeader(handle,offset,seekRel=1):
handle.seek(offset,seekRel)
magic = handle.read(4)
handle.seek(8, 1)
records = unpack('<L', handle.read(4))[0]
block = unpack('<L', handle.read(4))[0]
return magic,records,block
def parseEachQcfp(handle,offset=0):
handle.seek(offset,1)
qcfpHeader = parseHeader(handle,offset)
if qcfpHeader[0] != "qcfp":
exit("Something went wrong... encountered a non-qcfp record: " + qcfpHeader[0])
elif qcfpHeader[2] != 65536:
exit("Something went wrong... encountered an unexpected block size: " + qcfpHeader[2])
else:
handle.seek(20,1)
qcfpRecs = []
for record in range(qcfpHeader[1]):
blockPos = unpack('<L', handle.read(4))[0]
blockCount = unpack('<L', handle.read(4))[0]
if blockCount:
qcfpRecs.append((blockPos,blockCount))
return qcfpRecs
def parseQcfps(handle,qcfpCount,seekPos=32,seekRel=0):
handle.seek(seekPos,seekRel)
qcfpRecs = []
for qcfp in range(qcfpCount):
qcfpRecs.append((parseEachQcfp(handle)))
return qcfpRecs
def writeDummy(file, size):
if file.write("\x00"*size):
return True
else:
return False
def writeNewRecord(qcfpTuple,origHan,newHan,seekPos=0,seekRel=1,bs=65536):
origHan.seek(seekPos,seekRel)
blockOffset = qcfpTuple[0][0]
for q in qcfpTuple:
newHan.seek((q[0]-blockOffset)*bs)
newHan.write(origHan.read(q[1]*bs))
def decompress(handle,qcfpList,bs=65536):
handle.seek(bs)
i=0
for q in qcfpList:
newFileName = handle.name + "_qcfp_" + str(i) + ".bin"
bc = 0
for r in q:
bc += r[1]
newFileSize = bc * bs
if os.path.isfile(newFileName):
exit(newFileName + " exists! Quitting...")
outputFile = open(newFileName, 'w')
writeDummy(outputFile,newFileSize)
writeNewRecord(q,handle,outputFile)
outputFile.close()
print "wrote out: " + newFileName
i+=1
file = argv[1]
if file is None:
exit("No file to parse...")
f = open(file, 'rb')
qcfmHeader = parseHeader(f,0)
if qcfmHeader[0] != "mfcq":
exit("Something went wrong... incorrect qcfm magic #: " + qcfmHeader[0])
qcfpList = parseQcfps(f,qcfmHeader[1])
decompress(f,qcfpList)
f.close()