-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdc2iso.py
More file actions
executable file
·225 lines (174 loc) · 5.76 KB
/
dc2iso.py
File metadata and controls
executable file
·225 lines (174 loc) · 5.76 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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/usr/bin/python3.4
import sys
import os
import shutil
from enum import Enum, unique
def outFileName(infilename, ext="iso"):
try:
dotindex = infilename.rindex(".")
if dotindex == 0:
outfilename = infilename + "." + ext
else:
outfilename = infilename[:dotindex] + "." + ext
except ValueError:
outfilename = infilename + "." + ext
return outfilename
@unique
class GDITrackType(Enum):
audio = 0
data = 4
def readGDI(gdifile):
tracks = list()
tracksfound = list()
thistrack = 1
line = gdifile.readline()
if len(line) == 0:
return False
ntracks = int(line[:-1])
if ntracks < 1 or ntracks > 99:
print("WARNING: GDI file claims track number is less than 1 or greater than 99!")
while True:
line = gdifile.readline()
if len(line) == 0:
break
track = line[:-1].split()
tracknum = int(track[0])
if tracknum < 1 or tracknum > 99:
print("WARNING: Track claims to be less than 1 or greater than 99!")
if tracknum > ntracks:
print("WARNING: Track claims to be a higher track number than specified by GDI file.")
if tracknum < thistrack:
print("WARNING: Track numbering isn't ascending!")
if tracknum > thistrack:
print("WARNING: Track numbering skips a track!")
if tracknum in tracksfound:
print("WARNING: Repeat track number!")
tracks.append({'num': tracknum,
'blkpos': int(track[1]),
'type': GDITrackType(int(track[2])),
'blksize': int(track[3]),
'file': track[4],
'unknown': int(track[5])})
thistrack = tracknum + 1
if thistrack == 1: #we didn't find any tracks
return False
return tracks
def sortTracks(tracks):
return sorted(tracks, key=lambda x: x['num'])
def monotonicTracks(tracks):
curtrack = 1
for track in tracks:
if track['num'] != curtrack:
return False
curtrack += 1
return True
def ascendingTrackBlocks(tracks):
for track in range(0, len(tracks) -1):
if tracks[track+1]['blkpos'] <= tracks[track]['blkpos']:
return False
return True
def trimTracks(tracks, count, fixlba=True):
if count > 0:
newtracks = tracks[count:]
startlba = tracks[count]['blkpos']
for track in newtracks:
track['num'] -= count
if fixlba:
track['blkpos'] -= startlba
return newtracks
if count < 0:
return tracks[:-count]
return tracks #if 0 do nothing
def getTrackSizes(tracks):
tracksizes = list()
ntracks = len(tracks)
#we can't determine the size of this track based on the track table so
#exclude it
for track in enumerate(tracks[:-1]):
tracksizes.append(0)
tracksizes[track[0]]= (tracks[track[0]+1]['blkpos'] - track[1]['blkpos'])\
* 2048
continue #try to treat all tracks equally
if track[1]['type'] == GDITrackType.audio:
tracksizes[track[0]]= (tracks[track[0]+1]['blkpos'] - track[1]['blkpos'])\
* 2352
#pretty sure audio tracks have no subchannel data
if track[1]['type'] == GDITrackType.data:
tracksizes[track[0]]= (tracks[track[0]+1]['blkpos'] - track[1]['blkpos'])\
* 2048
#also reasonably sure data tracks are almost always 2048 block sectors, at
#least in this case
tracksizes.append(-1)
return tracksizes
def getTrackPositions(tracksize):
trackpos = list()
curtrackpos = 0
for size in tracksize:
trackpos.append(curtrackpos)
curtrackpos += size
return trackpos
def dcbin2iso(inf, outf):
while True:
seeked = inf.seek(16, os.SEEK_CUR)
if seeked < 16:
if seeked > 0:
print("WARNING: Incomplete seek: Seeked " + seeked + " bytes.")
break
sector = inf.read(2048)
if len(sector) < 2048:
if len(sector) > 0:
print("WARNING: Incomplete read: Read " + len(sector) + " bytes.")
break
seeked = inf.seek(288, os.SEEK_CUR)
if seeked < 288:
if seeked > 0:
print("WARNING: Incomplete seek: Seeked " + seeked + " bytes.")
break
outf.write(sector)
if len(sys.argv) < 2:
print("No file name given.")
exit()
outname = outFileName(sys.argv[1])
inf = open(sys.argv[1], "r")
gditbl = readGDI(inf)
inf.close()
gditbl = sortTracks(gditbl)
if not monotonicTracks(gditbl):
print("ERROR: Tracks must be ascending with none skipped.")
exit()
if not ascendingTrackBlocks(gditbl):
print("ERROR: Track offsets must be ascending.")
exit()
startlba = gditbl[2]['blkpos']
gditbl = trimTracks(gditbl, 2)
tracksize = getTrackSizes(gditbl)
trackpos = getTrackPositions(tracksize)
outf = open(outname, "wb")
for track in gditbl:
print(str(track['num']) + ": " + track['file'] + " " + str(track['blksize']) + " " + \
str(track['blkpos']) + " " + track['type'].name + " " + \
str(trackpos[track['num']-1]) + " " + str(tracksize[track['num']-1]) + \
"... ")
outf.seek(trackpos[track['num']-1])
if track['type'] == GDITrackType.audio: # just copy the data straight over
if track['num'] == 1: #first (third) track must be data
print("ERROR: Initial data track not found!")
break
continue #don't bother writing audio data, we don't need it
inf = open(track['file'], "rb")
shutil.copyfileobj(inf, outf, length=tracksize[track['num']-1])
inf.close()
if track['type'] == GDITrackType.data:
if track['blksize'] == 2352: # need to strip subchannel
inf = open(track['file'], "rb")
dcbin2iso(inf, outf)
inf.close()
elif track['blksize'] == 2048: # just copy it
inf = open(track['file'], "rb")
shutil.copyfileobj(inf, outf, length=tracksize[track['num']-1])
inf.close()
else:
print("ERROR: Unsupported block size.")
exit()
print("Start LBA is: " + str(-startlba))
inf.close()