-
Notifications
You must be signed in to change notification settings - Fork 52
/
encoding.go
183 lines (148 loc) · 4.57 KB
/
encoding.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
package id3v2
import (
"bytes"
"io/ioutil"
xencoding "golang.org/x/text/encoding"
"golang.org/x/text/encoding/charmap"
"golang.org/x/text/encoding/unicode"
)
// Encoding is a struct for encodings.
type Encoding struct {
Name string
Key byte
TerminationBytes []byte
}
func (e Encoding) Equals(other Encoding) bool {
return e.Key == other.Key
}
func (e Encoding) String() string {
return e.Name
}
// xencodingWrapper is a struct that stores decoder and encoder for
// appropriate x/text/encoding. It's used to reduce allocations
// through creating decoder and encoder only one time and storing it.
type xencodingWrapper struct {
decoder *xencoding.Decoder
encoder *xencoding.Encoder
}
func newXEncodingWrapper(e xencoding.Encoding) xencodingWrapper {
return xencodingWrapper{
decoder: e.NewDecoder(),
encoder: e.NewEncoder(),
}
}
func (e *xencodingWrapper) Decoder() *xencoding.Decoder {
return e.decoder
}
func (e *xencodingWrapper) Encoder() *xencoding.Encoder {
return e.encoder
}
// Available encodings.
var (
// EncodingISO is ISO-8859-1 encoding.
EncodingISO = Encoding{
Name: "ISO-8859-1",
Key: 0,
TerminationBytes: []byte{0},
}
// EncodingUTF16 is UTF-16 encoded Unicode with BOM.
EncodingUTF16 = Encoding{
Name: "UTF-16 encoded Unicode with BOM",
Key: 1,
TerminationBytes: []byte{0, 0},
}
// EncodingUTF16BE is UTF-16BE encoded Unicode without BOM.
EncodingUTF16BE = Encoding{
Name: "UTF-16BE encoded Unicode without BOM",
Key: 2,
TerminationBytes: []byte{0, 0},
}
// EncodingUTF8 is UTF-8 encoded Unicode.
EncodingUTF8 = Encoding{
Name: "UTF-8 encoded Unicode",
Key: 3,
TerminationBytes: []byte{0},
}
encodings = []Encoding{EncodingISO, EncodingUTF16, EncodingUTF16BE, EncodingUTF8}
xencodingISO = newXEncodingWrapper(charmap.ISO8859_1)
xencodingUTF16BEBOM = newXEncodingWrapper(unicode.UTF16(unicode.BigEndian, unicode.ExpectBOM))
xencodingUTF16LEBOM = newXEncodingWrapper(unicode.UTF16(unicode.LittleEndian, unicode.ExpectBOM))
xencodingUTF16BE = newXEncodingWrapper(unicode.UTF16(unicode.BigEndian, unicode.IgnoreBOM))
xencodingUTF8 = newXEncodingWrapper(unicode.UTF8)
)
// bom is used in UTF-16 encoded Unicode with BOM.
// See https://en.wikipedia.org/wiki/Byte_order_mark.
var bom = []byte{0xFF, 0xFE}
// getEncoding returns Encoding in accordance with ID3v2 key.
func getEncoding(key byte) Encoding {
if key > 3 {
return EncodingUTF8
}
return encodings[key]
}
// encodedSize counts length of UTF-8 src if it's encoded to enc.
func encodedSize(src string, enc Encoding) int {
if enc.Equals(EncodingUTF8) {
return len(src)
}
bw := getBufWriter(ioutil.Discard)
defer putBufWriter(bw)
encodeWriteText(bw, src, enc)
return bw.Written()
}
// decodeText decodes src from "from" encoding to UTF-8.
func decodeText(src []byte, from Encoding) string {
src = bytes.TrimSuffix(src, from.TerminationBytes) // See https://github.com/bogem/id3v2/issues/41
if from.Equals(EncodingUTF8) {
return string(src)
}
// If src is just BOM, then it's an empty string.
if from.Equals(EncodingUTF16) && bytes.Equal(src, bom) {
return ""
}
fromXEncoding := resolveXEncoding(src, from)
result, err := fromXEncoding.Decoder().Bytes(src)
if err != nil {
return string(src)
}
// HACK: Delete REPLACEMENT CHARACTER (�) if encoding went wrong.
// See https://apps.timwhitlock.info/unicode/inspect?s=%EF%BF%BD.
// See https://en.wikipedia.org/wiki/Byte_order_mark#UTF-8.
if from.Equals(EncodingUTF16) {
// bytes.Replace(s, old, new, -1) is the same as bytes.ReplaceAll(s, old, new),
// but bytes.ReplaceAll is only added in Go 1.12.
result = bytes.Replace(result, []byte{0xEF, 0xBF, 0xBD}, []byte{}, -1)
}
return string(result)
}
// encodeWriteText encodes src from UTF-8 to "to" encoding and writes to bw.
func encodeWriteText(bw *bufWriter, src string, to Encoding) error {
if to.Equals(EncodingUTF8) {
bw.WriteString(src)
return nil
}
toXEncoding := resolveXEncoding(nil, to)
encoded, err := toXEncoding.Encoder().String(src)
if err != nil {
return err
}
bw.WriteString(encoded)
if to.Equals(EncodingUTF16) && !bytes.HasSuffix([]byte(encoded), []byte{0}) {
bw.WriteByte(0)
}
return nil
}
func resolveXEncoding(src []byte, encoding Encoding) xencodingWrapper {
switch encoding.Key {
case 0:
return xencodingISO
case 1:
if len(src) > 2 && bytes.Equal(src[:2], bom) {
return xencodingUTF16LEBOM
}
return xencodingUTF16BEBOM
case 2:
return xencodingUTF16BE
}
return xencodingUTF8
}