-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwriter.go
256 lines (204 loc) · 5.59 KB
/
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
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
252
253
254
255
256
package gc
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"io"
"sync"
"time"
)
var (
errDuplicateName = errors.New("duplicate name")
errInvalidLength = errors.New("invalid length")
errNoFreeSpace = errors.New("no free space")
)
type fileWriter struct {
buf *bytes.Buffer
w *Writer
}
func (w *fileWriter) maxSize() int {
// Maximum file size is the size of the card plus the size of the
// directory entry, (i.e. .gci header), minus the reserved block size
return w.w.mc.size() + binary.Size(entry{}) - reservedBlocks*blockSize
}
func (w *fileWriter) Write(p []byte) (int, error) {
if len(p)+w.buf.Len() > w.maxSize() {
// Would exceed the maximum size
return 0, errInvalidLength
}
return w.buf.Write(p) //nolint:wrapcheck
}
//nolint:gochecknoglobals
var gamePatches = map[string]func(io.Reader, *memoryCard) (io.Reader, error){
"f_zero.dat": patchFZero,
"PSO_SYSTEM": patchPSO12,
"PSO3_SYSTEM": patchPSO3,
}
//nolint:cyclop,funlen
func (w *fileWriter) Close() error {
w.w.mu.Lock()
defer w.w.mu.Unlock()
delete(w.w.fw, w)
mc := w.w.mc
e := new(entry)
if err := binary.Read(w.buf, binary.BigEndian, e); err != nil {
return fmt.Errorf("unable to read header: %w", err)
}
for _, x := range mc.directory[mc.activeDirectory()].Entries {
if x.isEmpty() {
continue
}
if x.filename() == e.filename() {
return errDuplicateName
}
}
if w.buf.Len() != int(e.FileLength)*blockSize {
return errInvalidLength
}
if mc.count() == maxEntries || e.FileLength > mc.blockMap[mc.activeBlockMap()].FreeBlocks {
return errNoFreeSpace
}
var (
r io.Reader = w.buf
err error
)
patchFunc, ok := gamePatches[e.filename()]
if ok {
if r, err = patchFunc(w.buf, mc); err != nil {
return err
}
}
// Set e.FirstBlock to the correct location
e.FirstBlock = mc.blockMap[mc.activeBlockMap()].LastAllocatedBlock + 1
// Write out the blocks
lastBlock := e.FirstBlock + e.FileLength - reservedBlocks
for i := e.FirstBlock - reservedBlocks; i < lastBlock; i++ {
_, _ = r.Read(mc.blocks[i][:])
if i+1 < lastBlock {
mc.blockMap[mc.activeBlockMap()].Blocks[i] = i + reservedBlocks + 1
} else {
mc.blockMap[mc.activeBlockMap()].Blocks[i] = 0xffff
}
}
mc.directory[mc.activeDirectory()].Entries[mc.count()] = *e
mc.blockMap[mc.activeBlockMap()].LastAllocatedBlock += e.FileLength
mc.blockMap[mc.activeBlockMap()].FreeBlocks -= e.FileLength
return mc.checksum()
}
// A Writer is used for creating a new memory card image with files written to
// it.
type Writer struct {
mu sync.Mutex
w io.Writer
mc *memoryCard
fw map[*fileWriter]struct{}
formatTime uint64
flashID [12]byte
cardSize uint16
encoding uint16
}
// Create returns an io.WriteCloser for writing a new file on the memory card.
// The file should consist of a 64 byte header followed by one or more 8 KiB
// blocks as indicated in the header.
func (w *Writer) Create() (io.WriteCloser, error) {
w.mu.Lock()
defer w.mu.Unlock()
if w.mc.count() == maxEntries || w.mc.blockMap[w.mc.activeBlockMap()].FreeBlocks == 0 {
return nil, errNoFreeSpace
}
fw := &fileWriter{new(bytes.Buffer), w}
w.fw[fw] = struct{}{}
return fw, nil
}
// Close writes out the memory card to the underlying io.Writer. Any in-flight
// open memory card files are closed first.
func (w *Writer) Close() error {
w.mu.Lock()
defer w.mu.Unlock()
// Close any open files
for fw := range w.fw {
if err := fw.Close(); err != nil {
return err
}
delete(w.fw, fw)
}
b, err := w.mc.MarshalBinary()
if err != nil {
return err
}
if n, err := w.w.Write(b); err != nil || n != w.mc.size() {
if err != nil {
return err //nolint:wrapcheck
}
return errInvalidLength
}
return nil
}
// Credit to libogc/gc/ogc/lwp_watchdog.h.
const (
busClock uint64 = 162000000
timerClock = busClock / 4000
)
func now() uint64 {
// Number of seconds since 00:00:00, 1st January 2000, expressed as ticks
return timerClock * 1000 * uint64(time.Now().UTC().Sub(epoch).Seconds())
}
// NewWriter returns a Writer targeting a new blank memory card which defaults
// to 59 block capacity, ANSI encoding and an all-zeroes Flash ID.
func NewWriter(w io.Writer, options ...func(*Writer) error) (*Writer, error) {
nw := &Writer{
w: w,
fw: make(map[*fileWriter]struct{}),
formatTime: now(),
cardSize: MemoryCard59,
}
if err := nw.setOption(options...); err != nil {
return nil, err
}
mc, err := newMemoryCard(nw.flashID, nw.formatTime, nw.cardSize, nw.encoding)
if err != nil {
return nil, err
}
nw.mc = mc
return nw, nil
}
func (w *Writer) setOption(options ...func(*Writer) error) error {
for _, option := range options {
if err := option(w); err != nil {
return err
}
}
return nil
}
// FlashID sets the 12 byte Flash ID. If the target is an official memory card
// then this must be set correctly.
func FlashID(flashID [12]byte) func(*Writer) error {
return func(w *Writer) error {
w.flashID = flashID
return nil
}
}
// FormatTime overrides the formatting time, which is expressed in seconds
// since 00:00:00, 1st January 2000. Setting it to 0 stops the serial number
// from being computed.
func FormatTime(formatTime uint64) func(*Writer) error {
return func(w *Writer) error {
w.formatTime = formatTime
return nil
}
}
// CardSize sets the memory card capacity.
func CardSize(cardSize uint16) func(*Writer) error {
return func(w *Writer) error {
w.cardSize = cardSize
return nil
}
}
// Encoding sets the memory card encoding.
func Encoding(encoding uint16) func(*Writer) error {
return func(w *Writer) error {
w.encoding = encoding
return nil
}
}