-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencoder.go
More file actions
262 lines (203 loc) · 6.07 KB
/
encoder.go
File metadata and controls
262 lines (203 loc) · 6.07 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
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
257
258
259
260
261
262
package puz
import (
"bytes"
"encoding/binary"
"fmt"
)
// EncodePuz encodes the data in puzzle to bytes that can be saved as a .puz file
func EncodePuz(puzzle *Puzzle) ([]byte, error) {
writer := newPuzzleWriter()
writer.writeBytes(puzzle.UnusedData.Preamble)
err := encodeHeader(puzzle, writer)
if err != nil {
return nil, fmt.Errorf("Failed to encode header: %w", err)
}
encodeSolutionAndState(puzzle, writer)
err = encodeStringsSection(puzzle, writer)
if err != nil {
return nil, fmt.Errorf("Failed to encode strings section: %w", err)
}
err = encodeExtraSections(puzzle, writer)
if err != nil {
return nil, fmt.Errorf("Failed to encode extra sections: %w", err)
}
writer.writeBytes(puzzle.UnusedData.Postscript)
bodyBytes := writer.bytes()[len(puzzle.UnusedData.Preamble) : len(writer.bytes())-len(puzzle.UnusedData.Postscript)]
computedChecksums := computeChecksums(bodyBytes, puzzle.Board.Width()*puzzle.Board.Height(), puzzle.Title, puzzle.Author, puzzle.Copyright, puzzle.clues, puzzle.Notes, puzzle.version)
preambleOffset := len(puzzle.UnusedData.Preamble)
err = writer.overwriteShort(preambleOffset+0, computedChecksums.checksum)
if err != nil {
return nil, err
}
err = writer.overwriteShort(preambleOffset+14, computedChecksums.cibChecksum)
if err != nil {
return nil, err
}
err = writer.overwrite(preambleOffset+16, computedChecksums.maskedLowChecksum[:])
if err != nil {
return nil, err
}
err = writer.overwrite(preambleOffset+20, computedChecksums.maskedHighChecksum[:])
if err != nil {
return nil, err
}
return writer.bytes(), nil
}
type puzzleWriter struct {
buffer bytes.Buffer
}
func newPuzzleWriter() *puzzleWriter {
return &puzzleWriter{
bytes.Buffer{},
}
}
func (w *puzzleWriter) writeString(str string) {
w.buffer.WriteString(str)
w.buffer.WriteByte(0x00)
}
func (w *puzzleWriter) writePlaceholder(amount int) {
b := make([]byte, amount)
for i := range b {
b[i] = 0x00
}
w.buffer.Write(b)
}
func (w *puzzleWriter) writeBytes(bytes []byte) {
w.buffer.Write(bytes)
}
func (w *puzzleWriter) writeShort(short uint16) {
b := make([]byte, 2)
binary.LittleEndian.PutUint16(b, short)
w.buffer.Write(b)
}
func (w *puzzleWriter) writeByte(b byte) error {
return w.buffer.WriteByte(b)
}
func (w *puzzleWriter) bytes() []byte {
return w.buffer.Bytes()
}
func (w *puzzleWriter) overwrite(offset int, newBytes []byte) error {
data := w.buffer.Bytes()
if offset < 0 || offset > len(data) || offset+len(newBytes) > len(data) {
return OutOfBoundsWriteError
}
copy(data[offset:], newBytes)
return nil
}
func (w *puzzleWriter) overwriteShort(offset int, short uint16) error {
b := make([]byte, 2)
binary.LittleEndian.PutUint16(b, short)
err := w.overwrite(offset, b)
if err != nil {
return err
}
return nil
}
func encodeHeader(puzzle *Puzzle, writer *puzzleWriter) error {
// placeholder for file checksum, computed and inserted later
writer.writePlaceholder(2)
writer.writeString(fileMagic)
// placeholder for cib, maskedLow, and maskedHigh checksums, computed and inserted later
writer.writePlaceholder(10)
writer.writeBytes([]byte(puzzle.version)) // not using write str because it already has the null terminator
writer.writeBytes(puzzle.UnusedData.reserved1)
writer.writeShort(puzzle.scramble.scrambledChecksum)
writer.writeBytes(puzzle.UnusedData.reserved2)
writer.writeByte(byte(puzzle.Board.Width()))
writer.writeByte(byte(puzzle.Board.Height()))
writer.writeShort(uint16(len(puzzle.clues)))
writer.writeShort(uint16(puzzle.PuzzleType))
writer.writeShort(puzzle.scramble.scrambledTag)
return nil
}
func encodeSolutionAndState(puzzle *Puzzle, writer *puzzleWriter) {
height := puzzle.Board.Height()
width := puzzle.Board.Width()
size := height * width
solution := make([]byte, size)
state := make([]byte, size)
for y := range height {
for x := range width {
solution[(y*width)+x] = puzzle.Board[y][x].Answer
state[(y*width)+x] = puzzle.Board[y][x].Guess
}
}
writer.writeBytes(solution)
writer.writeBytes(state)
}
func encodeStringsSection(puzzle *Puzzle, writer *puzzleWriter) error {
if len(puzzle.clues) != int(puzzle.expectedClues) {
return &ClueCountMismatchError{
int(puzzle.expectedClues),
len(puzzle.clues),
}
}
writer.writeString(puzzle.Title)
writer.writeString(puzzle.Author)
writer.writeString(puzzle.Copyright)
for _, clue := range puzzle.clues {
writer.writeString(clue.Clue)
}
writer.writeString(puzzle.Notes)
return nil
}
func encodeExtraSections(puzzle *Puzzle, writer *puzzleWriter) error {
for _, section := range puzzle.Extras.extraSectionOrder {
var data []byte
switch section {
case RebusSection, MarkupBoardSection:
height := puzzle.Board.Height()
width := puzzle.Board.Width()
size := height * width
board := make([]byte, size)
for y := range height {
for x := range width {
var val byte
if section == RebusSection {
val = puzzle.Board[y][x].RebusKey
} else {
val = puzzle.Board[y][x].Markup
}
board[(y*width)+x] = val
}
}
data = board
case RebusTableSection:
if puzzle.Extras.RebusTable == nil {
return MissingExtraSectionError
}
for _, entry := range puzzle.Extras.RebusTable {
padding := ""
if entry.Key-1 < 10 {
padding = " "
}
data = fmt.Appendf(data, "%s%d:%s;", padding, entry.Key-1, entry.Value)
}
case TimerSection:
runningRep := 0
if !puzzle.Extras.Timer.Running {
runningRep = 1
}
data = fmt.Appendf(data, "%d,%d", puzzle.Extras.Timer.SecondsPassed, runningRep)
case UserRebusTableSection:
if puzzle.Extras.UserRebusTable == nil {
return MissingExtraSectionError
}
for _, entry := range puzzle.Extras.UserRebusTable {
padding := ""
if entry.Key-1 < 10 {
padding = " "
}
data = fmt.Appendf(data, "%s%d:%s;", padding, entry.Key-1, entry.Value)
}
}
sectionLength := uint16(len(data))
checksum := checksumRegion(data, 0x00)
writer.writeBytes([]byte(section.String()))
writer.writeShort(sectionLength)
writer.writeShort(checksum)
writer.writeBytes(data)
writer.writeByte(0x00)
}
return nil
}