forked from mikkyang/id3-go
/
util.go
152 lines (123 loc) · 3.12 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
// Copyright 2013 Michael Yang. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package encodedbytes
import (
"bytes"
"errors"
iconv "github.com/djimenez/iconv-go"
)
const (
BytesPerInt = 4
SynchByteLength = 7
NormByteLength = 8
NativeEncoding = 3
)
type Encoding struct {
Name string
NullLength int
}
var (
EncodingMap = [...]Encoding{
{Name: "ISO-8859-1", NullLength: 1},
{Name: "UTF-16", NullLength: 2},
{Name: "UTF-16BE", NullLength: 2},
{Name: "UTF-8", NullLength: 1},
}
Decoders = make([]*iconv.Converter, len(EncodingMap))
Encoders = make([]*iconv.Converter, len(EncodingMap))
)
func init() {
n := EncodingForIndex(NativeEncoding)
for i, e := range EncodingMap {
Decoders[i], _ = iconv.NewConverter(e.Name, n)
Encoders[i], _ = iconv.NewConverter(n, e.Name)
}
}
// Form an integer from concatenated bits
func ByteInt(buf []byte, base uint) (i uint32, err error) {
if len(buf) > BytesPerInt {
err = errors.New("byte integer: invalid []byte length")
return
}
for _, b := range buf {
if base < NormByteLength && b >= (1<<base) {
err = errors.New("byte integer: exceed max bit")
return
}
i = (i << base) | uint32(b)
}
return
}
func SynchInt(buf []byte) (i uint32, err error) {
i, err = ByteInt(buf, SynchByteLength)
return
}
func NormInt(buf []byte) (i uint32, err error) {
i, err = ByteInt(buf, NormByteLength)
return
}
// Form a byte slice from an integer
func IntBytes(n uint32, base uint) []byte {
mask := uint32(1<<base - 1)
bytes := make([]byte, BytesPerInt)
for i, _ := range bytes {
bytes[len(bytes)-i-1] = byte(n & mask)
n >>= base
}
return bytes
}
func SynchBytes(n uint32) []byte {
return IntBytes(n, SynchByteLength)
}
func NormBytes(n uint32) []byte {
return IntBytes(n, NormByteLength)
}
func EncodingForIndex(b byte) string {
encodingIndex := int(b)
if encodingIndex < 0 || encodingIndex > len(EncodingMap) {
encodingIndex = 0
}
return EncodingMap[encodingIndex].Name
}
func EncodingNullLengthForIndex(b byte) int {
encodingIndex := int(b)
if encodingIndex < 0 || encodingIndex > len(EncodingMap) {
encodingIndex = 0
}
return EncodingMap[encodingIndex].NullLength
}
func IndexForEncoding(e string) byte {
for i, v := range EncodingMap {
if v.Name == e {
return byte(i)
}
}
return 0
}
func nullIndex(data []byte, encoding byte) (atIndex, afterIndex int) {
byteCount := EncodingNullLengthForIndex(encoding)
limit := len(data)
null := bytes.Repeat([]byte{0x0}, byteCount)
for i, _ := range data[:limit/byteCount] {
atIndex = byteCount * i
afterIndex = atIndex + byteCount
if bytes.Equal(data[atIndex:afterIndex], null) {
return
}
}
atIndex = -1
afterIndex = -1
return
}
func EncodedDiff(newEncoding byte, newString string, oldEncoding byte, oldString string) (int, error) {
newEncodedString, err := Encoders[newEncoding].ConvertString(newString)
if err != nil {
return 0, err
}
oldEncodedString, err := Encoders[oldEncoding].ConvertString(oldString)
if err != nil {
return 0, err
}
return len(newEncodedString) - len(oldEncodedString), nil
}