-
Notifications
You must be signed in to change notification settings - Fork 22
/
iso9660.go
215 lines (174 loc) · 6.06 KB
/
iso9660.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
package vm
import (
"bufio"
"bytes"
"encoding/binary"
"fmt"
"io"
"strings"
"time"
. "github.com/johto/iso9660wrap"
)
// More or less vendored from https://github.com/johto/iso9660wrap/blob/master/iso9660wrap.go
type ISO9660 struct {
FileName string
Contents []byte
}
const (
volumeDescriptorSetMagic = "\x43\x44\x30\x30\x31\x01"
primaryVolumeSectorNum uint32 = 16
numVolumeSectors uint32 = 2 // primary + terminator
littleEndianPathTableSectorNum uint32 = primaryVolumeSectorNum + numVolumeSectors
bigEndianPathTableSectorNum uint32 = littleEndianPathTableSectorNum + 1
numPathTableSectors = 2 // no secondaries
rootDirectorySectorNum uint32 = primaryVolumeSectorNum + numVolumeSectors + numPathTableSectors
)
func (i ISO9660) inputLen() uint32 {
return uint32(len(i.Contents))
}
func (i ISO9660) Bytes() ([]byte, error) {
i.FileName = strings.ToUpper(i.FileName)
if !i.fileNameSatisfiesISOConstraints(i.FileName) {
return nil, fmt.Errorf("File name '%s' violates ISO9660 constraints", i.FileName)
}
buf := bytes.NewBuffer([]byte{})
bufw := bufio.NewWriter(buf)
w := NewISO9660Writer(bufw)
err := i.writePrimaryVolumeDescriptor(w)
if err != nil {
return nil, err
}
err = i.writeVolumeDescriptorSetTerminator(w)
if err != nil {
return nil, err
}
i.writePathTable(w, binary.LittleEndian)
i.writePathTable(w, binary.BigEndian)
err = i.writeData(w)
if err != nil {
return nil, err
}
w.Finish()
bufw.Flush()
reservedBytes := make([]byte, int64(16*SectorSize))
return append(reservedBytes, buf.Bytes()...), nil
}
func (i ISO9660) writePrimaryVolumeDescriptor(w *ISO9660Writer) error {
if len(i.FileName) > 32 {
i.FileName = i.FileName[:32]
}
now := time.Now()
sw := w.NextSector()
if w.CurrentSector() != primaryVolumeSectorNum {
return fmt.Errorf("internal error: unexpected primary volume sector %d", w.CurrentSector())
}
sw.WriteByte('\x01')
sw.WriteString(volumeDescriptorSetMagic)
sw.WriteByte('\x00')
sw.WritePaddedString("", 32)
sw.WritePaddedString(i.FileName, 32)
sw.WriteZeros(8)
sw.WriteBothEndianDWord(i.numTotalSectors())
sw.WriteZeros(32)
sw.WriteBothEndianWord(1) // volume set size
sw.WriteBothEndianWord(1) // volume sequence number
sw.WriteBothEndianWord(uint16(SectorSize))
sw.WriteBothEndianDWord(SectorSize) // path table length
sw.WriteLittleEndianDWord(littleEndianPathTableSectorNum)
sw.WriteLittleEndianDWord(0) // no secondary path tables
sw.WriteBigEndianDWord(bigEndianPathTableSectorNum)
sw.WriteBigEndianDWord(0) // no secondary path tables
WriteDirectoryRecord(sw, "\x00", rootDirectorySectorNum) // root directory
sw.WritePaddedString("", 128) // volume set identifier
sw.WritePaddedString("", 128) // publisher identifier
sw.WritePaddedString("", 128) // data preparer identifier
sw.WritePaddedString("", 128) // application identifier
sw.WritePaddedString("", 37) // copyright file identifier
sw.WritePaddedString("", 37) // abstract file identifier
sw.WritePaddedString("", 37) // bibliographical file identifier
sw.WriteDateTime(now) // volume creation
sw.WriteDateTime(now) // most recent modification
sw.WriteUnspecifiedDateTime() // expires
sw.WriteUnspecifiedDateTime() // is effective (?)
sw.WriteByte('\x01') // version
sw.WriteByte('\x00') // reserved
sw.PadWithZeros() // 512 (reserved for app) + 653 (zeros)
return nil
}
func (i ISO9660) writeVolumeDescriptorSetTerminator(w *ISO9660Writer) error {
sw := w.NextSector()
if w.CurrentSector() != primaryVolumeSectorNum+1 {
return fmt.Errorf("internal error: unexpected volume descriptor set terminator sector %d", w.CurrentSector())
}
sw.WriteByte('\xFF')
sw.WriteString(volumeDescriptorSetMagic)
sw.PadWithZeros()
return nil
}
func (i ISO9660) writePathTable(w *ISO9660Writer, bo binary.ByteOrder) {
sw := w.NextSector()
sw.WriteByte(1) // name length
sw.WriteByte(0) // number of sectors in extended attribute record
sw.WriteDWord(bo, rootDirectorySectorNum)
sw.WriteWord(bo, 1) // parent directory recno (root directory)
sw.WriteByte(0) // identifier (root directory)
sw.WriteByte(1) // padding
sw.PadWithZeros()
}
func (i ISO9660) writeData(w *ISO9660Writer) error {
sw := w.NextSector()
if w.CurrentSector() != rootDirectorySectorNum {
return fmt.Errorf("internal error: unexpected root directory sector %d", w.CurrentSector())
}
WriteDirectoryRecord(sw, "\x00", w.CurrentSector())
WriteDirectoryRecord(sw, "\x01", rootDirectorySectorNum)
WriteFileRecordHeader(sw, i.FileName, w.CurrentSector()+1, i.inputLen())
inputBuf := bytes.NewBuffer(i.Contents)
// Now stream the data. Note that the first buffer is never of SectorSize,
// since we've already filled a part of the sector.
b := make([]byte, SectorSize)
total := uint32(0)
for {
l, err := inputBuf.Read(b)
if err != nil && err != io.EOF {
return fmt.Errorf("could not read from input file: %s", err)
}
if l > 0 {
sw = w.NextSector()
sw.Write(b[:l])
total += uint32(l)
}
if err == io.EOF {
break
}
}
if total != i.inputLen() {
return fmt.Errorf("input file size changed (expected to read %d, read %d)", i.inputLen(), total)
} else if w.CurrentSector() != i.numTotalSectors()-1 {
return fmt.Errorf("internal error: unexpected last sector number (expected %d, actual %d)", i.numTotalSectors()-1, w.CurrentSector())
}
return nil
}
func (i ISO9660) numTotalSectors() uint32 {
var numDataSectors uint32
numDataSectors = (i.inputLen() + (SectorSize - 1)) / SectorSize
return 1 + rootDirectorySectorNum + numDataSectors
}
func (ISO9660) fileNameSatisfiesISOConstraints(filename string) bool {
invalidCharacter := func(r rune) bool {
// According to ISO9660, only capital letters, digits, and underscores
// are permitted. Some sources say a dot is allowed as well. I'm too
// lazy to figure it out right now.
if r >= 'A' && r <= 'Z' {
return false
} else if r >= '0' && r <= '9' {
return false
} else if r == '_' {
return false
} else if r == '.' {
return false
}
return true
}
return strings.IndexFunc(filename, invalidCharacter) == -1
}