| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
package id3 |
| 16 |
|
| 17 |
import ( |
| 18 |
"encoding/binary" |
| 19 |
"errors" |
| 20 |
"fmt" |
| 21 |
"strconv" |
| 22 |
"strings" |
| 23 |
"unicode/utf16" |
| 24 |
) |
| 25 |
|
| 26 |
type ErrFormat struct { |
| 27 |
Format string |
| 28 |
Err error |
| 29 |
} |
| 30 |
|
| 31 |
func (e ErrFormat) Error() string { |
| 32 |
return fmt.Sprintf("gotaglib: error parsing format %q: %v", e.Format, e.Err) |
| 33 |
} |
| 34 |
|
| 35 |
func parseBase128Int(bytes []byte) uint64 { |
| 36 |
var result uint64 |
| 37 |
for _, b := range bytes { |
| 38 |
result = result << 7 |
| 39 |
result |= uint64(b) |
| 40 |
} |
| 41 |
return result |
| 42 |
} |
| 43 |
|
| 44 |
func parseLeadingInt(s string) (int, error) { |
| 45 |
var intEnd int |
| 46 |
for intEnd < len(s) && '0' <= s[intEnd] && s[intEnd] <= '9' { |
| 47 |
intEnd++ |
| 48 |
} |
| 49 |
return strconv.Atoi(s[0:intEnd]) |
| 50 |
} |
| 51 |
|
| 52 |
func getTextIdentificationFrame(content []byte) ([]string, error) { |
| 53 |
normalized, err := parseText(content) |
| 54 |
if err != nil { |
| 55 |
return nil, err |
| 56 |
} |
| 57 |
return strings.Split(normalized, string([]byte{0})), nil |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
func parseText(strBytes []byte) (string, error) { |
| 68 |
if len(strBytes) == 0 { |
| 69 |
return "", errors.New("empty id3 frame") |
| 70 |
} |
| 71 |
if len(strBytes) < 2 { |
| 72 |
|
| 73 |
return "", nil |
| 74 |
} |
| 75 |
encoding, strBytes := strBytes[0], strBytes[1:] |
| 76 |
|
| 77 |
switch encoding { |
| 78 |
case 0: |
| 79 |
return parseIso8859(strBytes), nil |
| 80 |
|
| 81 |
case 1: |
| 82 |
return parseUtf16WithBOM(strBytes) |
| 83 |
|
| 84 |
case 2: |
| 85 |
return parseUtf16(strBytes, binary.BigEndian) |
| 86 |
|
| 87 |
case 3: |
| 88 |
return parseUtf8(strBytes) |
| 89 |
|
| 90 |
default: |
| 91 |
return "", id3v24Err("invalid encoding byte %x", encoding) |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
func parseIso8859(strBytes []byte) string { |
| 96 |
runes := make([]rune, len(strBytes)) |
| 97 |
for i, b := range strBytes { |
| 98 |
runes[i] = rune(b) |
| 99 |
} |
| 100 |
return string(runes) |
| 101 |
} |
| 102 |
|
| 103 |
func parseUtf16WithBOM(strBytes []byte) (string, error) { |
| 104 |
if strBytes[0] == 0xFE && strBytes[1] == 0xFF { |
| 105 |
return parseUtf16(strBytes[2:], binary.BigEndian) |
| 106 |
} |
| 107 |
if strBytes[0] == 0xFF && strBytes[1] == 0xFE { |
| 108 |
return parseUtf16(strBytes[2:], binary.LittleEndian) |
| 109 |
} |
| 110 |
return "", id3v24Err("invalid byte order marker %x %x", strBytes[0], strBytes[1]) |
| 111 |
} |
| 112 |
|
| 113 |
func parseUtf16(strBytes []byte, bo binary.ByteOrder) (string, error) { |
| 114 |
shorts := make([]uint16, 0, len(strBytes)/2) |
| 115 |
for i := 0; i < len(strBytes); i += 2 { |
| 116 |
short := bo.Uint16(strBytes[i : i+2]) |
| 117 |
shorts = append(shorts, short) |
| 118 |
} |
| 119 |
|
| 120 |
return string(utf16.Decode(shorts)), nil |
| 121 |
} |
| 122 |
|
| 123 |
func parseUtf8(strBytes []byte) (string, error) { |
| 124 |
return string(strBytes), nil |
| 125 |
} |