Debian Codegraph @

taglib/id3/util.go

raw symbols SCIP
1 // Copyright 2013 Google Inc. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
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 // Parses a string from frame data. The first byte represents the encoding:
61 // 0x01 ISO-8859-1
62 // 0x02 UTF-16 w/ BOM
63 // 0x03 UTF-16BE w/o BOM
64 // 0x04 UTF-8
65 //
66 // Refer to section 4 of http://id3.org/id3v2.4.0-structure
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 // Not an error according to the spec (because at least 1 byte big)
73 return "", nil
74 }
75 encoding, strBytes := strBytes[0], strBytes[1:]
76
77 switch encoding {
78 case 0: // ISO-8859-1 text.
79 return parseIso8859(strBytes), nil
80
81 case 1: // UTF-16 with BOM.
82 return parseUtf16WithBOM(strBytes)
83
84 case 2: // UTF-16BE without BOM.
85 return parseUtf16(strBytes, binary.BigEndian)
86
87 case 3: // UTF-8 text.
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 }