-
Notifications
You must be signed in to change notification settings - Fork 0
/
XenoHandler.py
154 lines (119 loc) · 3.71 KB
/
XenoHandler.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# XenoHandler.py
#
# Copyright (c) 2017 Andrea Uccheddu (.ghost)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
# (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do
# so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
# IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE
#
#
import struct
import io
class XenoHandler:
'''
Constructor
'''
def __init__(self, tFile):
self.rawSectS = 2352
self.isoSectS = 2048
self.isoHedrS = 24
self.crcS = 280
self.realTocI = 24
self.mFile = tFile
self.mFile.seek(0, 2)
self.mSize = self.mFile.tell()
self.mFile.seek(0)
self.mFileIndex = self.getHiddenToc()
'''
Raw Data Access
'''
def getRawSec(self, tSecN):
self.pFile.seek(tSecN*gRawSectS)
tRet = self.pFile.read(gRawSectS)
self.pFile.seek(0)
return tRet
def getIsoSec(self, tSecN):
self.mFile.seek(tSecN*self.rawSectS + self.isoHedrS)
tRet = self.mFile.read(self.isoSectS)
self.mFile.seek(0)
return tRet
'''
Init Helper Functions
'''
def getHiddenToc(self):
eot = bytearray([ 0xFF, 0xFF, 0xFF, 0, 0, 0, 0, 0])
i = self.realTocI
toc = dict()
data = bytearray()
run = True
buf = bytearray()
folderCount = 0
while(run):
data = self.getIsoSec(i)
j = 0
nrun = True
while(nrun):
if j > len(data):
nrun = False
break
#check for leftover bytes before end of sector
if j >= len(data)-7:
buf = bytearray()
for k in range(j,len(data)):
buf.append(data[k])
nrun = False
break
#check from leftover bytes from previous sector
if len(buf) > 0:
buf.extend(bytearray(data))
data=buf
buf = bytearray()
#default case, read the 7 byte entry
byteSeq = bytearray([ data[j],data[j+1],data[j+2], 0,data[j+3],data[j+4],data[j+5],data[j+6]])
#break loop on find end of table
if byteSeq == eot:
run = False
break;
entry = dict()
offset, size = struct.unpack("<Ii",byteSeq)
entry["offset"] = offset
entry["size"] = size
if size < 0:
entry["label"] = "FOLDER"
entry["folderNum"] = folderCount
folderCount += 1
else:
entry["label"] = "FILE"
entry["fileNum"] = len(toc)
toc[entry["fileNum"]] = entry
j +=7
i+=1
return toc
'''
File Utilities
'''
def getFile(self, fileNum):
startSector = self.mFileIndex[fileNum]["offset"]
endSector = startSector + int( (self.mFileIndex[fileNum]["size"] -1) / 2048 + 1)
data = bytearray()
for i in range(startSector, endSector):
data.extend(self.getIsoSec(i))
data = data[:self.mFileIndex[fileNum]["size"]]
data = io.BytesIO(data)
data.seek(0)
return data
def isFolder(self, fileNum):
return self.mFileIndex[fileNum]["label"] == "FOLDER"
def isFile(self, fileNum):
return self.mFileIndex[fileNum]["label"] == "FILE"