-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspec.go
566 lines (506 loc) · 14.8 KB
/
spec.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
package bgcodego
import (
"bufio"
"bytes"
"compress/zlib"
"encoding/base64"
"encoding/binary"
"errors"
"fmt"
"hash/crc32"
"io"
"slices"
"strings"
heatshrink "github.com/currantlabs/goheatshrink"
)
// FileHeaderVersion for FileHeader
// Refer to https://github.com/prusa3d/libbgcode/blob/main/doc/specifications.md#file-header
type FileHeaderVersion uint32
func (fhv FileHeaderVersion) IsValid() bool {
return fhv == Version1
}
const (
Version1 FileHeaderVersion = 1
)
// ChecksumType for FileHeader
// Refer to https://github.com/prusa3d/libbgcode/blob/main/doc/specifications.md#file-header
type ChecksumType uint16
func (ct ChecksumType) IsValid() bool {
return ct == ChecksumTypeNone || ct == ChecksumTypeCRC32
}
const (
ChecksumTypeNone ChecksumType = 0
ChecksumTypeCRC32 ChecksumType = 1
)
// FileHeader implements https://github.com/prusa3d/libbgcode/blob/main/doc/specifications.md#file-header
type FileHeader struct {
MagicNumber uint32
Version FileHeaderVersion // Version of the G-code binarization
ChecksumType ChecksumType // Algorithm used for checksum
}
func (fh *FileHeader) Parse(r io.Reader) error {
if err := binary.Read(r, binary.LittleEndian, fh); err != nil {
return err
}
if fh.MagicNumber != 1162101575 {
return errors.New("invalid BGCode file")
}
if !fh.Version.IsValid() {
return fmt.Errorf("non-supported bgcode version: %v", fh.Version)
}
if !fh.ChecksumType.IsValid() {
return fmt.Errorf("non-supported checksum type: %v", fh.ChecksumType)
}
return nil
}
// BlockHeaderCompression according to https://github.com/prusa3d/libbgcode/blob/main/doc/specifications.md#block-header
type BlockHeaderType uint16
func (bht BlockHeaderType) IsValid() bool {
return bht == BlockHeaderTypeFileMetadata ||
bht == BlockHeaderTypeGCode ||
bht == BlockHeaderTypeSlicerMetadata ||
bht == BlockHeaderTypePrinterMetadata ||
bht == BlockHeaderTypePrintMetadata ||
bht == BlockHeaderTypeThumbnail
}
const (
BlockHeaderTypeFileMetadata BlockHeaderType = 0
BlockHeaderTypeGCode BlockHeaderType = 1
BlockHeaderTypeSlicerMetadata BlockHeaderType = 2
BlockHeaderTypePrinterMetadata BlockHeaderType = 3
BlockHeaderTypePrintMetadata BlockHeaderType = 4
BlockHeaderTypeThumbnail BlockHeaderType = 5
)
// BlockHeaderCompression according to https://github.com/prusa3d/libbgcode/blob/main/doc/specifications.md#block-header
type BlockHeaderCompression uint16
func (bhc BlockHeaderCompression) String() string {
switch bhc {
case BlockHeaderCompressionNone:
return "None"
case BlockHeaderCompressionDeflate:
return "Deflate"
case BlockHeaderCompressionHeatshrink114:
return "Heatshrink114"
case BlockHeaderCompressionHeatshrink124:
return "Heatshrink124"
default:
return "Unknown"
}
}
func (bhc BlockHeaderCompression) IsValid() bool {
return bhc == BlockHeaderCompressionNone ||
bhc == BlockHeaderCompressionDeflate ||
bhc == BlockHeaderCompressionHeatshrink114 ||
bhc == BlockHeaderCompressionHeatshrink124
}
const (
BlockHeaderCompressionNone BlockHeaderCompression = 0
BlockHeaderCompressionDeflate BlockHeaderCompression = 1
BlockHeaderCompressionHeatshrink114 BlockHeaderCompression = 2
BlockHeaderCompressionHeatshrink124 BlockHeaderCompression = 3
)
// BlockHeader according to https://github.com/prusa3d/libbgcode/blob/main/doc/specifications.md#block-header
type BlockHeader struct {
basic struct {
Type BlockHeaderType
Compression BlockHeaderCompression
UncompressedSize uint32
}
extended struct {
CompressedSize uint32
}
}
func (bh *BlockHeader) Type() BlockHeaderType {
return bh.basic.Type
}
func (bh *BlockHeader) Parse(r io.Reader) error {
if err := binary.Read(r, binary.LittleEndian, &bh.basic); err != nil {
return err
}
if !bh.basic.Type.IsValid() {
return fmt.Errorf("non-supported header type: %v", bh.basic.Type)
}
if !bh.basic.Compression.IsValid() {
return fmt.Errorf("non-supported compression algorithm: %v", bh.basic.Compression)
}
if bh.basic.Compression == BlockHeaderCompressionNone {
return nil
}
if err := binary.Read(r, binary.LittleEndian, &bh.extended); err != nil {
return err
}
return nil
}
func (bh *BlockHeader) Length() uint32 {
if bh.basic.Compression == BlockHeaderCompressionNone {
return bh.basic.UncompressedSize
}
return bh.extended.CompressedSize
}
func (bh *BlockHeader) Compression() BlockHeaderCompression {
return bh.basic.Compression
}
func (bh *BlockHeader) Inflate(body []byte) ([]byte, error) {
switch bh.Compression() {
case BlockHeaderCompressionDeflate:
r, err := zlib.NewReader(bytes.NewReader(body))
if err != nil {
return nil, fmt.Errorf("cannot create zlib inflator: %w", err)
}
return io.ReadAll(r)
case BlockHeaderCompressionHeatshrink114:
r := heatshrink.NewReader(bytes.NewReader(body), heatshrink.Window(11), heatshrink.Lookahead(4))
return io.ReadAll(r)
case BlockHeaderCompressionHeatshrink124:
r := heatshrink.NewReader(bytes.NewReader(body), heatshrink.Window(12), heatshrink.Lookahead(4))
return io.ReadAll(r)
default:
return body, nil
}
}
type BlockEncoding uint16
const (
BlockEncodingINI BlockEncoding = 0
)
// BlockFileMetadata according to https://github.com/prusa3d/libbgcode/blob/main/doc/specifications.md#file-metadata
type BlockFileMetadata struct {
header struct {
Encoding BlockEncoding
}
Values KeyValues
}
func (bfm *BlockFileMetadata) Render() string {
return "; generated by " + bfm.Values.First("Producer") + "\n\n"
}
func (bfm *BlockFileMetadata) Parse(r io.Reader, hdr *BlockHeader) error {
if err := binary.Read(r, binary.LittleEndian, &bfm.header); err != nil {
return err
}
switch bfm.header.Encoding {
case BlockEncodingINI:
body := make([]byte, hdr.Length())
if _, err := io.ReadFull(r, body); err != nil {
return fmt.Errorf("cannot read block encoding: %w", err)
}
body, err := hdr.Inflate(body)
if err != nil {
return fmt.Errorf("cannot create body inflator: %w", err)
}
v, err := iniDecode(body)
if err != nil {
return fmt.Errorf("cannot decode INI key-table: %w", err)
}
bfm.Values = v
return nil
default:
return errors.New("bad encoding")
}
}
// BlockPrinterMetadata according to https://github.com/prusa3d/libbgcode/blob/main/doc/specifications.md#printer-metadata
type BlockPrinterMetadata struct {
header struct {
Encoding BlockEncoding
}
Values KeyValues
}
func (bprm *BlockPrinterMetadata) Render() string {
return bprm.Values.Render()
}
func (bprm *BlockPrinterMetadata) Parse(r io.Reader, hdr *BlockHeader) error {
if err := binary.Read(r, binary.LittleEndian, &bprm.header); err != nil {
return err
}
switch bprm.header.Encoding {
case BlockEncodingINI:
body := make([]byte, hdr.Length())
if _, err := io.ReadFull(r, body); err != nil {
return fmt.Errorf("cannot read block encoding: %w", err)
}
body, err := hdr.Inflate(body)
if err != nil {
return fmt.Errorf("cannot create body inflator: %w", err)
}
v, err := iniDecode(body)
if err != nil {
return fmt.Errorf("cannot decode INI key-table: %w", err)
}
bprm.Values = v
return nil
default:
return errors.New("bad encoding")
}
}
// BlockThumbnailFormat according to https://github.com/prusa3d/libbgcode/blob/main/doc/specifications.md#thumbnail
type BlockThumbnailFormat uint16
func (btf BlockThumbnailFormat) String() string {
switch btf {
case BlockThumbnailFormatPNG:
return "PNG"
case BlockThumbnailFormatJPG:
return "JPG"
case BlockThumbnailFormatQOI:
return "QOI"
default:
return "Unknown"
}
}
const (
BlockThumbnailFormatPNG BlockThumbnailFormat = 0
BlockThumbnailFormatJPG BlockThumbnailFormat = 1
BlockThumbnailFormatQOI BlockThumbnailFormat = 2
)
// BlockThumbnail according to https://github.com/prusa3d/libbgcode/blob/main/doc/specifications.md#thumbnail
type BlockThumbnail struct {
header struct {
Format BlockThumbnailFormat
Width uint16
Height uint16
}
Body []byte
}
func (bt *BlockThumbnail) Render() string {
out := &strings.Builder{}
fmt.Fprintln(out, ";")
encoded := base64.StdEncoding.EncodeToString(bt.Body)
fmt.Fprintf(out, "; thumbnail begin %vx%v %v\n", bt.header.Width, bt.header.Height, len(encoded))
for len(encoded) > 78 {
chunk, rest := encoded[:78], encoded[78:]
fmt.Fprintln(out, ";", chunk)
encoded = rest
}
fmt.Fprintln(out, ";", encoded)
fmt.Fprintf(out, "; thumbnail end\n")
fmt.Fprintln(out, ";")
return out.String()
}
func (bt *BlockThumbnail) Parse(r io.Reader, hdr *BlockHeader) error {
if err := binary.Read(r, binary.LittleEndian, &bt.header); err != nil {
return err
}
bt.Body = make([]byte, hdr.Length())
_, err := io.ReadFull(r, bt.Body)
return err
}
// BlockPrintMetadata according to https://github.com/prusa3d/libbgcode/blob/main/doc/specifications.md#print-metadata
type BlockPrintMetadata struct {
header struct {
Encoding BlockEncoding
}
Values KeyValues
}
func (bprm *BlockPrintMetadata) Render() string {
return bprm.Values.Render()
}
func (bprm *BlockPrintMetadata) Parse(r io.Reader, hdr *BlockHeader) error {
if err := binary.Read(r, binary.LittleEndian, &bprm.header); err != nil {
return err
}
switch bprm.header.Encoding {
case BlockEncodingINI:
body := make([]byte, hdr.Length())
if _, err := io.ReadFull(r, body); err != nil {
return fmt.Errorf("cannot read block encoding: %w", err)
}
body, err := hdr.Inflate(body)
if err != nil {
return fmt.Errorf("cannot create body inflator: %w", err)
}
v, err := iniDecode(body)
if err != nil {
return fmt.Errorf("cannot decode INI key-table: %w", err)
}
bprm.Values = v
return nil
default:
return errors.New("bad encoding")
}
}
// BlockSlicerMetadata according to https://github.com/prusa3d/libbgcode/blob/main/doc/specifications.md#slicer-metadata
type BlockSlicerMetadata struct {
header struct {
Encoding BlockEncoding
}
Values KeyValues
}
func (bsm *BlockSlicerMetadata) Render() string {
out := &strings.Builder{}
fmt.Fprintln(out, "; prusaslicer_config = begin")
fmt.Fprint(out, bsm.Values.Render())
fmt.Fprintln(out, "; prusaslicer_config = end")
fmt.Fprintln(out, "")
return out.String()
}
func (bsm *BlockSlicerMetadata) Parse(r io.Reader, hdr *BlockHeader) error {
if err := binary.Read(r, binary.LittleEndian, &bsm.header); err != nil {
return err
}
switch bsm.header.Encoding {
case BlockEncodingINI:
body := make([]byte, hdr.Length())
if _, err := io.ReadFull(r, body); err != nil {
return fmt.Errorf("cannot read block encoding: %w", err)
}
body, err := hdr.Inflate(body)
if err != nil {
return fmt.Errorf("cannot create body inflator: %w", err)
}
v, err := iniDecode(body)
if err != nil {
return fmt.Errorf("cannot decode INI key-table: %w", err)
}
bsm.Values = v
return nil
default:
return errors.New("bad encoding")
}
}
// GCodeEncoding according to https://github.com/prusa3d/libbgcode/blob/main/doc/specifications.md#gcode
type GCodeEncoding uint16
const (
GCodeEncodingNone GCodeEncoding = 0
GCodeEncodingMeatpack GCodeEncoding = 1
GCodeEncodingMeatpackWithComments GCodeEncoding = 2
)
// BlockGCode according to https://github.com/prusa3d/libbgcode/blob/main/doc/specifications.md#gcode
type BlockGCode struct {
header struct {
Encoding GCodeEncoding
}
Body string
}
func (bg *BlockGCode) Render() string {
return bg.Body
}
func (bg *BlockGCode) Parse(r io.Reader, hdr *BlockHeader) error {
if err := binary.Read(r, binary.LittleEndian, &bg.header); err != nil {
return err
}
body := make([]byte, hdr.Length())
if _, err := io.ReadFull(r, body); err != nil {
return err
}
body, err := hdr.Inflate(body)
if err != nil {
return err
}
bg.Body = unbinarize(body)
return nil
}
type KeyValues []KeyValue
func (kv KeyValues) First(key string) string {
idx := slices.IndexFunc(kv, func(kv KeyValue) bool {
return kv.Key == key
})
if idx == -1 {
return ""
}
return kv[idx].Value
}
func (kvs KeyValues) Render() string {
out := &strings.Builder{}
for _, kv := range kvs {
line := fmt.Sprint("; ", kv.Key, " = ", kv.Value)
fmt.Fprintln(out, strings.TrimSpace(line))
}
return out.String()
}
// KeyValue is the tuple used by tables inside of the blocks.
type KeyValue struct {
Key string
Value string
}
func iniDecode(body []byte) (KeyValues, error) {
var res KeyValues
scanner := bufio.NewScanner(bytes.NewReader(body))
for scanner.Scan() {
key, value, ok := strings.Cut(scanner.Text(), "=")
if !ok {
return nil, errors.New("malformed key-value pair")
}
res = append(res, KeyValue{
Key: strings.TrimSpace(key),
Value: strings.TrimSpace(value),
})
}
return res, nil
}
type BlockRenderer interface{ Render() string }
// Parse converts a BGCode input into regular GCode output
func Parse(fd io.Reader) (string, error) {
out := &strings.Builder{}
fh := &FileHeader{}
if err := fh.Parse(fd); err != nil {
return "", fmt.Errorf("cannot parse file header: %w", err)
}
blocks := make(map[BlockHeaderType][]BlockRenderer)
for {
buf := &bytes.Buffer{}
r := io.TeeReader(fd, buf)
hdr := &BlockHeader{}
err := hdr.Parse(r)
if errors.Is(err, io.EOF) {
break
} else if err != nil {
return "", fmt.Errorf("cannot parse block header: %w", err)
}
var block interface {
Parse(r io.Reader, hdr *BlockHeader) error
}
switch hdr.Type() {
case BlockHeaderTypeFileMetadata:
block = &BlockFileMetadata{}
case BlockHeaderTypeGCode:
block = &BlockGCode{}
case BlockHeaderTypeSlicerMetadata:
block = &BlockSlicerMetadata{}
case BlockHeaderTypePrinterMetadata:
block = &BlockPrinterMetadata{}
case BlockHeaderTypePrintMetadata:
block = &BlockPrintMetadata{}
case BlockHeaderTypeThumbnail:
block = &BlockThumbnail{}
}
if err := block.Parse(r, hdr); err != nil {
return "", fmt.Errorf("cannot parse %q block: %w", hdr.Type(), err)
}
if fh.ChecksumType == ChecksumTypeCRC32 {
var crc32footer uint32
err := binary.Read(fd, binary.LittleEndian, &crc32footer)
if err != nil {
return "", fmt.Errorf("cannot read CRC32 footer: %w", err)
}
if crc32footer != crc32.ChecksumIEEE(buf.Bytes()) {
return "", errors.New("bad checksum")
}
}
blocks[hdr.Type()] = append(blocks[hdr.Type()], block.(BlockRenderer))
}
if b, ok := blocks[BlockHeaderTypeFileMetadata]; ok {
fmt.Fprint(out, b[0].Render())
}
if b, ok := blocks[BlockHeaderTypePrinterMetadata]; ok {
fmt.Fprintln(out)
fmt.Fprint(out, b[0].Render())
}
if thumbnails, ok := blocks[BlockHeaderTypeThumbnail]; ok {
for _, thumbnail := range thumbnails {
fmt.Fprintln(out)
fmt.Fprint(out, thumbnail.Render())
}
}
if gcodes, ok := blocks[BlockHeaderTypeGCode]; ok {
fmt.Fprintln(out)
for _, gcode := range gcodes {
fmt.Fprint(out, gcode.Render())
}
}
if b, ok := blocks[BlockHeaderTypePrintMetadata]; ok {
fmt.Fprintln(out)
fmt.Fprint(out, b[0].Render())
}
if b, ok := blocks[BlockHeaderTypeSlicerMetadata]; ok {
fmt.Fprintln(out)
fmt.Fprint(out, b[0].Render())
}
return out.String(), nil
}