-
Notifications
You must be signed in to change notification settings - Fork 52
/
parse.go
187 lines (148 loc) · 3.98 KB
/
parse.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
// Copyright 2016 Albert Nigmatzianov. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package id3v2
import (
"errors"
"fmt"
"io"
)
const frameHeaderSize = 10
var ErrUnsupportedVersion = errors.New("unsupported version of ID3 tag")
var errBlankFrame = errors.New("id or size of frame are blank")
// ErrBodyOverflow is returned when a frame has greater size than the remaining tag size
var ErrBodyOverflow = errors.New("frame went over tag area")
type frameHeader struct {
ID string
BodySize int64
}
// parse finds ID3v2 tag in rd and parses it to tag considering opts.
// If rd is smaller than expected, it returns ErrSmallHeaderSize.
func (tag *Tag) parse(rd io.Reader, opts Options) error {
if rd == nil {
return errors.New("rd is nil")
}
header, err := parseHeader(rd)
if err == errNoTag || err == io.EOF {
tag.init(rd, 0, 4)
return nil
}
if err != nil {
return fmt.Errorf("error by parsing tag header: %v", err)
}
if header.Version < 3 {
return ErrUnsupportedVersion
}
tag.init(rd, tagHeaderSize+header.FramesSize, header.Version)
if !opts.Parse {
return nil
}
return tag.parseFrames(opts)
}
func (tag *Tag) init(rd io.Reader, originalSize int64, version byte) {
tag.DeleteAllFrames()
tag.reader = rd
tag.originalSize = originalSize
tag.version = version
tag.setDefaultEncodingBasedOnVersion(version)
}
func (tag *Tag) parseFrames(opts Options) error {
framesSize := tag.originalSize - tagHeaderSize
parseableIDs := tag.makeIDsFromDescriptions(opts.ParseFrames)
isParseFramesProvided := len(opts.ParseFrames) > 0
synchSafe := tag.Version() == 4
br := getBufReader(nil)
defer putBufReader(br)
buf := getByteSlice(32 * 1024)
defer putByteSlice(buf)
for framesSize > 0 {
header, err := parseFrameHeader(buf, tag.reader, synchSafe)
if err == io.EOF || err == errBlankFrame || err == ErrInvalidSizeFormat {
break
}
if err != nil {
return err
}
id, bodySize := header.ID, header.BodySize
framesSize -= frameHeaderSize + bodySize
if framesSize < 0 {
return ErrBodyOverflow
}
bodyRd := getLimitedReader(tag.reader, bodySize)
defer putLimitedReader(bodyRd)
if isParseFramesProvided && !parseableIDs[id] {
if err := skipReaderBuf(bodyRd, buf); err != nil {
return err
}
continue
}
br.Reset(bodyRd)
frame, err := parseFrameBody(id, br)
if err != nil && err != io.EOF {
return err
}
tag.AddFrame(id, frame)
if isParseFramesProvided && !mustFrameBeInSequence(id) {
delete(parseableIDs, id)
// If it was last ID in parseIDs, we don't need to parse
// other frames, so end the parsing.
if len(parseableIDs) == 0 {
break
}
}
if err == io.EOF {
break
}
}
return nil
}
func (tag *Tag) makeIDsFromDescriptions(parseFrames []string) map[string]bool {
ids := make(map[string]bool, len(parseFrames))
for _, description := range parseFrames {
ids[tag.CommonID(description)] = true
}
return ids
}
func parseFrameHeader(buf []byte, rd io.Reader, synchSafe bool) (frameHeader, error) {
var header frameHeader
if len(buf) < frameHeaderSize {
return header, errors.New("parseFrameHeader: buf is smaller than frame header size")
}
fhBuf := buf[:frameHeaderSize]
if _, err := rd.Read(fhBuf); err != nil {
return header, err
}
id := string(fhBuf[:4])
bodySize, err := parseSize(fhBuf[4:8], synchSafe)
if err != nil {
return header, err
}
if id == "" || bodySize == 0 {
return header, errBlankFrame
}
header.ID = id
header.BodySize = bodySize
return header, nil
}
// skipReaderBuf just reads rd until io.EOF.
func skipReaderBuf(rd io.Reader, buf []byte) error {
for {
_, err := rd.Read(buf)
if err == io.EOF {
break
}
if err != nil {
return err
}
}
return nil
}
func parseFrameBody(id string, br *bufReader) (Framer, error) {
if id[0] == 'T' && id != "TXXX" {
return parseTextFrame(br)
}
if parseFunc, exists := parsers[id]; exists {
return parseFunc(br)
}
return parseUnknownFrame(br)
}