-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathiso9660_writer.go
149 lines (124 loc) · 3.12 KB
/
iso9660_writer.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
package main
import (
"encoding/binary"
"io"
"math"
"strings"
"time"
)
const SectorSize uint32 = 2048
type SectorWriter struct {
w io.Writer
p uint32
}
func (w *SectorWriter) Write(p []byte) uint32 {
if len(p) >= math.MaxUint32 {
Panicf("attempted write of length %d is out of sector bounds", len(p))
}
l := uint32(len(p))
if l > w.RemainingSpace() {
Panicf("attempted write of length %d at offset %d is out of sector bounds", w.p, len(p))
}
w.p += l
_, err := w.w.Write(p)
if err != nil {
panic(err)
}
return l
}
func (w *SectorWriter) WriteUnspecifiedDateTime() uint32 {
b := make([]byte, 17)
for i := 0; i < 16; i++ {
b[i] = '0'
}
b[16] = 0
return w.Write(b)
}
func (w *SectorWriter) WriteDateTime(t time.Time) uint32 {
f := t.UTC().Format("20060102150405")
f += "00" // 1/100
f += "\x00" // UTC offset
if len(f) != 17 {
Panicf("date and time field %q is of unexpected length %d", f, len(f))
}
return w.WriteString(f)
}
func (w *SectorWriter) WriteString(str string) uint32 {
return w.Write([]byte(str))
}
func (w *SectorWriter) WritePaddedString(str string, length uint32) uint32 {
l := w.WriteString(str)
if l > 32 {
Panicf("padded string %q exceeds length %d", str, length)
} else if l < 32 {
w.WriteString(strings.Repeat(" ", int(32 - l)))
}
return 32
}
func (w *SectorWriter) WriteByte(b byte) uint32 {
return w.Write([]byte{b})
}
func (w *SectorWriter) WriteWord(bo binary.ByteOrder, word uint16) uint32 {
b := make([]byte, 2)
bo.PutUint16(b, word)
return w.Write(b)
}
func (w *SectorWriter) WriteBothEndianWord(word uint16) uint32 {
w.WriteWord(binary.LittleEndian, word)
w.WriteWord(binary.BigEndian, word)
return 4
}
func (w *SectorWriter) WriteDWord(bo binary.ByteOrder, dword uint32) uint32 {
b := make([]byte, 4)
bo.PutUint32(b, dword)
return w.Write(b)
}
func (w *SectorWriter) WriteLittleEndianDWord(dword uint32) uint32 {
return w.WriteDWord(binary.LittleEndian, dword)
}
func (w *SectorWriter) WriteBigEndianDWord(dword uint32) uint32 {
return w.WriteDWord(binary.BigEndian, dword)
}
func (w *SectorWriter) WriteBothEndianDWord(dword uint32) uint32 {
w.WriteLittleEndianDWord(dword)
w.WriteBigEndianDWord(dword)
return 8
}
func (w *SectorWriter) WriteZeros(c int) uint32 {
return w.Write(make([]byte, c))
}
func (w *SectorWriter) PadWithZeros() uint32 {
return w.Write(make([]byte, w.RemainingSpace()))
}
func (w *SectorWriter) RemainingSpace() uint32 {
return SectorSize - w.p
}
func (w *SectorWriter) Reset() {
w.p = 0
}
type ISO9660Writer struct {
sw *SectorWriter
sectorNum uint32
}
func (w *ISO9660Writer) CurrentSector() uint32 {
return uint32(w.sectorNum)
}
func (w *ISO9660Writer) NextSector() *SectorWriter {
if w.sw.RemainingSpace() == SectorSize {
Panicf("internal error: tried to leave sector %d empty", w.sectorNum)
}
w.sw.PadWithZeros()
w.sw.Reset()
w.sectorNum++
return w.sw
}
func (w *ISO9660Writer) Finish() {
if w.sw.RemainingSpace() != SectorSize {
w.sw.PadWithZeros()
}
w.sw = nil
}
func NewISO9660Writer(w io.Writer) *ISO9660Writer {
// start at the end of the last reserved sector
return &ISO9660Writer{&SectorWriter{w, SectorSize}, 16 - 1}
}