-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
251 lines (208 loc) · 4.42 KB
/
main.go
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package main
import (
"bytes"
"encoding/binary"
"fmt"
"io"
"os"
"reflect"
"github.com/alecthomas/kong"
)
const (
NeroV1 NeroFormat = iota
NeroV2
)
var (
NeroV1FormatCode = []byte("NERO")
NeroV2FormatCode = []byte("NER5")
)
var cli struct {
SourceNRG string `arg`
TargetISO string `arg`
}
func main() {
kong.Parse(&cli,
kong.Description("This command-line tool converts NERO image files (.nrg) to ISO image format (.iso)."))
err := convert()
if err != nil {
fmt.Printf("%v\n", err)
os.Exit(1)
}
fmt.Printf("Done!\n")
}
func convert() error {
inFilePath := cli.SourceNRG
outFilePath := cli.TargetISO
inFile, err := os.Open(inFilePath)
if err != nil {
return fmt.Errorf("failed to open source file: %w", err)
}
defer inFile.Close()
format, err := getFormat(inFile)
if err != nil {
return fmt.Errorf("failed parsing source file: %w", err)
}
firstChunkOffset, err := getFirstChunkOffset(inFile, format)
if err != nil {
return fmt.Errorf("failed parsing source file: %w", err)
}
_, err = inFile.Seek(firstChunkOffset, io.SeekStart)
if err != nil {
return fmt.Errorf("failed reading source file: %w", err)
}
for {
chunk, err := readChunk(inFile)
if err != nil {
return fmt.Errorf("failed reading source file: %w", err)
}
if daox, ok := chunk.(*DAOX); ok {
outFile, err := os.Create(outFilePath)
if err != nil {
return fmt.Errorf("create target file error: %w", err)
}
defer outFile.Close()
_, err = inFile.Seek(daox.Index1, io.SeekStart)
if err != nil {
return fmt.Errorf("seek error: %w", err)
}
_, err = io.CopyN(outFile, inFile, daox.EndOfTrack-daox.Index1)
if err != nil {
return fmt.Errorf("copy error: %w", err)
}
break
}
if _, ok := chunk.(*END); ok {
return fmt.Errorf("image format not supported: %w", err)
}
}
return nil
}
func readChunk(file *os.File) (any, error) {
chunkHeader := ChunkHeader{}
err := binary.Read(file, binary.BigEndian, &chunkHeader)
if err != nil {
return nil, fmt.Errorf("read chunk header error: %w", err)
}
chunkID := string(chunkHeader.ID[:])
var chunk any
switch chunkID {
case "CUEX":
chunk = &CUEX{}
case "DAOX":
chunk = &DAOX{}
case "SINF":
chunk = &SINF{}
case "MTYP":
chunk = &MTYP{}
case "END!":
chunk = &END{}
default:
return nil, fmt.Errorf("uknown chunk ID: %s", chunkID)
}
err = binary.Read(file, binary.BigEndian, chunk)
if err != nil {
return nil, fmt.Errorf("read chunk error: %w", err)
}
chunkBodySize := reflect.Indirect(reflect.ValueOf(chunk)).Type().Size()
offset := int64(chunkHeader.Size) - int64(chunkBodySize)
if offset > 0 {
_, err = file.Seek(offset, io.SeekCurrent)
if err != nil {
return nil, fmt.Errorf("seek error: %w", err)
}
}
return chunk, nil
}
func getFirstChunkOffset(file *os.File, format NeroFormat) (int64, error) {
size := 4
if format == NeroV2 {
size = 8
}
_, err := file.Seek(-int64(size), io.SeekEnd)
if err != nil {
return 0, err
}
var offset int64
err = binary.Read(file, binary.BigEndian, &offset)
if err != nil {
return 0, err
}
return offset, nil
}
func getFormat(file *os.File) (NeroFormat, error) {
formats := []struct {
offset int64
format NeroFormat
code []byte
}{
{
offset: -8,
code: NeroV1FormatCode,
format: NeroV1,
},
{
offset: -12,
code: NeroV2FormatCode,
format: NeroV2,
},
}
for _, f := range formats {
_, err := file.Seek(f.offset, io.SeekEnd)
if err != nil {
return 0, fmt.Errorf("seek error: %w", err)
}
buffer := make([]byte, 4)
_, err = file.Read(buffer)
if err != nil {
return 0, err
}
if bytes.Equal(buffer, f.code) {
return f.format, nil
}
}
return 0, fmt.Errorf("unable to determine format")
}
type NeroFormat int
func (f NeroFormat) String() string {
switch f {
case NeroV1:
return string(NeroV1FormatCode)
case NeroV2:
return string(NeroV2FormatCode)
default:
return fmt.Sprintf("%d", int(f))
}
}
type ChunkHeader struct {
ID [4]byte
Size uint32
}
type CUEX struct {
Mode byte
TrackNumber byte
IndexNumber byte
Padding byte
LbaPosition int32
}
type DAOX struct {
Size uint32
Upc [13]byte
Padding byte
TocType uint16
FirstTrack byte
LastTrack byte
Isrc [12]byte
SectorSize uint16
Mode uint16
Uknown uint16
Index0 int64
Index1 int64
EndOfTrack int64
}
type SINF struct {
TracksInSession int32
}
type MTYP struct {
Uknown int32
}
type END struct{}