forked from n10v/id3v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.go
169 lines (140 loc) · 3.66 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
// 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"
"io"
"strconv"
)
const frameHeaderSize = 10
var errBlankFrame = errors.New("id or size of frame are blank")
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 errors.New("error by parsing tag header: " + err.Error())
}
if header.Version < 3 {
err = errors.New("unsupported version of ID3 tag: " + strconv.Itoa(int(header.Version)))
return err
}
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()
if version == 4 {
tag.defaultEncoding = EncodingUTF8
} else {
tag.defaultEncoding = EncodingISO
}
tag.reader = rd
tag.originalSize = originalSize
tag.version = version
}
func (tag *Tag) parseFrames(opts Options) error {
// Size of frames in tag = size of whole tag - size of tag header.
framesSize := tag.originalSize - tagHeaderSize
// Convert descriptions, specified by user in opts.ParseFrames, to IDs.
// Use map for speed.
parseIDs := make(map[string]bool, len(opts.ParseFrames))
for _, description := range opts.ParseFrames {
parseIDs[tag.CommonID(description)] = true
}
buf := getByteSlice(32 * 1024)
defer putByteSlice(buf)
for framesSize > 0 {
// Parse frame header.
header, err := parseFrameHeader(buf, tag.reader)
if err == io.EOF || err == errBlankFrame || err == ErrInvalidSizeFormat {
break
}
if err != nil {
return err
}
id := header.ID
bodySize := header.BodySize
// Substitute the size of the whole frame from framesSize.
framesSize -= frameHeaderSize + bodySize
// Limit tag.reader by header.BodySize.
bodyRd := getLimitedReader(tag.reader, bodySize)
defer putLimitedReader(bodyRd)
// If user set opts.ParseFrames, take it into consideration.
if len(parseIDs) > 0 && !parseIDs[id] {
if err := skipReaderBuf(bodyRd, buf); err != nil {
return err
}
continue
}
// Parse frame body.
frame, err := parseFrameBody(id, bodyRd)
if err != nil && err != io.EOF {
return err
}
// Add frame to tag.
tag.AddFrame(id, frame)
if err == io.EOF {
break
}
}
return nil
}
func parseFrameHeader(buf []byte, rd io.Reader) (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])
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, rd io.Reader) (Framer, error) {
if id[0] == 'T' {
return parseTextFrame(rd)
}
if parseFunc, exists := parsers[id]; exists {
return parseFunc(rd)
}
return parseUnknownFrame(rd)
}