-
Notifications
You must be signed in to change notification settings - Fork 2
/
reader.go
185 lines (168 loc) · 3.41 KB
/
reader.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
// Copyright 2015 The astrogo Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package asdf
import (
"bufio"
"bytes"
"encoding/binary"
"fmt"
"io"
"strings"
"github.com/astrogo/asdf/schemas/stsci.edu/asdf/core"
yaml "gopkg.in/yaml.v2"
)
// Open open an ASDF file for reading.
func Open(r io.Reader) (File, error) {
var f File
var err error
rbuf := bufio.NewReader(r)
bline, err := rbuf.ReadBytes('\n')
if err != nil {
return f, err
}
line := string(bytes.TrimSpace(bline))
if !strings.HasPrefix(line, "#ASDF") {
return f, fmt.Errorf("asdf: missing version header (got=%q)", line)
}
f.Version = line
comment_loop:
for {
c, err := rbuf.Peek(1)
if err != nil {
return f, err
}
switch c[0] {
case '#':
bline, err = rbuf.ReadBytes('\n')
if err != nil {
return f, err
}
f.Comments = append(
f.Comments,
string(bytes.TrimSpace(bline)),
)
default:
break comment_loop
}
}
c, err := rbuf.Peek(1)
if err != nil {
if err == io.EOF {
// asdf file with only a header (and comments)
return f, nil
}
return f, err
}
if c[0] == '%' {
ybuf := []byte{}
for {
bline, err = rbuf.ReadBytes('\n')
if err != nil {
return f, err
}
// FIXME(sbinet)
// see: https://github.com/go-yaml/yaml/issues/74
if bytes.HasPrefix(bline, []byte("%TAG ")) {
bline = append(
[]byte("%TAG !asdf"),
bline[len([]byte("%TAG ")):]...,
)
}
ybuf = append(ybuf, bline...)
if bytes.HasPrefix(bline, []byte("...")) {
break
}
}
fmt.Printf("=== yaml ===\n%s\n===\n", string(ybuf))
var ymap core.ASDF
err = yaml.Unmarshal(ybuf, &ymap)
if err != nil {
return f, err
}
f.Tree = &Tree{data: ymap}
}
err = decodeBlocks(rbuf, &f)
if err != nil {
return f, err
}
if len(f.Blocks) > 0 {
// only asdf files with at least a block may have an index
err = decodeIndex(rbuf, &f)
if err != nil {
return f, err
}
}
return f, err
}
func decodeBlocks(rbuf *bufio.Reader, f *File) error {
for {
c, err := rbuf.Peek(len(blockMagicToken))
if err != nil {
fmt.Printf("peek failed: %v\n", err)
if err == io.EOF {
return nil
}
return err
}
if !bytes.Equal(c, blockMagicToken) {
return nil
}
var block Block
err = decodeBlock(rbuf, &block)
if err != nil {
return err
}
f.Blocks = append(f.Blocks, block)
}
return nil
}
func decodeBlock(r io.Reader, blk *Block) error {
var err error
read := func(v interface{}) {
if err != nil {
return
}
err = binary.Read(r, binary.BigEndian, v)
}
read(blk.Header.Magic[:])
read(&blk.Header.Size)
read(&blk.Header.Flags)
read(blk.Header.Compression[:])
read(&blk.Header.AllocSize)
read(&blk.Header.UsedSize)
read(&blk.Header.DataSize)
read(blk.Header.Checksum[:])
if err != nil {
return err
}
blk.Data = make([]byte, int(blk.Header.AllocSize))
read(blk.Data)
return err
}
func decodeIndex(rbuf *bufio.Reader, f *File) error {
c, err := rbuf.Peek(len(blockIndex))
if err != nil {
if err == io.EOF {
return nil
}
fmt.Printf("index-peek failed: %v\n", err)
return err
}
if !bytes.Equal(c, blockIndex) {
return nil
}
var ybuf []byte
for {
bline, err := rbuf.ReadBytes('\n')
if err != nil {
return err
}
ybuf = append(ybuf, bline...)
if bytes.HasPrefix(bline, []byte("...")) {
break
}
}
err = yaml.Unmarshal(ybuf, &f.Index)
return err
}