forked from ascherkus/go-id3
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutil.go
159 lines (142 loc) · 3.55 KB
/
util.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
// Copyright 2011 Andrew Scherkus
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package id3
import (
"bufio"
"fmt"
"strings"
"unicode/utf16"
)
var skipBuffer []byte = make([]byte, 1024*4)
func ISO8859_1ToUTF8(data []byte) string {
p := make([]rune, len(data))
for i, b := range data {
p[i] = rune(b)
}
return string(p)
}
func toUTF16(data []byte) []uint16 {
if len(data) < 2 {
panic("Sequence is too short too contain a UTF-16 BOM")
}
if len(data)%2 > 0 {
// TODO: if this is UTF-16 BE then this is likely encoded wrong
data = append(data, 0)
}
var shift0, shift1 uint
if data[0] == 0xFF && data[1] == 0xFE {
// UTF-16 LE
shift0 = 0
shift1 = 8
} else if data[0] == 0xFE && data[1] == 0xFF {
// UTF-16 BE
shift0 = 8
shift1 = 0
panic("UTF-16 BE found!")
} else {
panic(fmt.Sprintf("Unrecognized UTF-16 BOM: 0x%02X%02X", data[0], data[1]))
}
s := make([]uint16, 0, len(data)/2)
for i := 2; i < len(data); i += 2 {
s = append(s, uint16(data[i])<<shift0|uint16(data[i+1])<<shift1)
}
return s
}
// Peeks at the buffer to see if there is a valid frame.
func hasFrame(reader *bufio.Reader, frameSize int) bool {
data, err := reader.Peek(frameSize)
if err != nil {
return false
}
for _, c := range data {
if (c < 'A' || c > 'Z') && (c < '0' || c > '9') {
return false
}
}
return true
}
// Sizes are stored big endian but with the first bit set to 0 and always ignored.
//
// Refer to section 3.1 of http://id3.org/id3v2.4.0-structure
func parseSize(data []byte) int32 {
size := int32(0)
for i, b := range data {
if b&0x80 > 0 {
fmt.Println("Size byte had non-zero first bit")
}
shift := uint32(len(data)-i-1) * 7
size |= int32(b&0x7f) << shift
}
return size
}
// Parses a string from frame data. The first byte represents the encoding:
// 0x01 ISO-8859-1
// 0x02 UTF-16 w/ BOM
// 0x03 UTF-16BE w/o BOM
// 0x04 UTF-8
//
// Refer to section 4 of http://id3.org/id3v2.4.0-structure
func parseString(data []byte) string {
var s string
switch data[0] {
case 0: // ISO-8859-1 text.
s = ISO8859_1ToUTF8(data[1:])
break
case 1: // UTF-16 with BOM.
s = string(utf16.Decode(toUTF16(data[1:])))
break
case 2: // UTF-16BE without BOM.
panic("Unsupported text encoding UTF-16BE.")
case 3: // UTF-8 text.
s = string(data[1:])
break
default:
// No encoding, assume ISO-8859-1 text.
s = ISO8859_1ToUTF8(data)
}
return strings.TrimRight(s, "\u0000")
}
func readBytes(reader *bufio.Reader, c int) []byte {
b := make([]byte, c)
pos := 0
for pos < c {
i, err := reader.Read(b[pos:])
pos += i
if err != nil {
panic(err)
}
}
return b
}
func readString(reader *bufio.Reader, c int) string {
return parseString(readBytes(reader, c))
}
func readGenre(reader *bufio.Reader, c int) string {
genre := parseString(readBytes(reader, c))
return convertID3v1Genre(genre)
}
func skipBytes(reader *bufio.Reader, c int) {
pos := 0
for pos < c {
end := c - pos
if end > len(skipBuffer) {
end = len(skipBuffer)
}
i, err := reader.Read(skipBuffer[0:end])
pos += i
if err != nil {
panic(err)
}
}
}