/
multilocalisedunicode.go
146 lines (121 loc) · 3.25 KB
/
multilocalisedunicode.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
package icc
import (
"bytes"
"fmt"
"github.com/mandykoh/prism/meta/binary"
"unicode/utf16"
)
type MultiLocalisedUnicode struct {
entriesByLanguageCountry map[[2]byte]map[[2]byte]string
}
func (mluc *MultiLocalisedUnicode) getAnyString() string {
for _, country := range mluc.entriesByLanguageCountry {
for _, s := range country {
return s
}
}
return ""
}
func (mluc *MultiLocalisedUnicode) getString(language [2]byte, country [2]byte) string {
countries, ok := mluc.entriesByLanguageCountry[language]
if !ok {
return ""
}
return countries[country]
}
func (mluc *MultiLocalisedUnicode) getStringForLanguage(language [2]byte) string {
for _, s := range mluc.entriesByLanguageCountry[language] {
return s
}
return ""
}
func (mluc *MultiLocalisedUnicode) setString(language [2]byte, country [2]byte, text string) {
countries, ok := mluc.entriesByLanguageCountry[language]
if !ok {
countries = map[[2]byte]string{
country: text,
}
mluc.entriesByLanguageCountry[language] = countries
} else {
countries[country] = text
}
}
func parseMultiLocalisedUnicode(data []byte) (MultiLocalisedUnicode, error) {
result := MultiLocalisedUnicode{
entriesByLanguageCountry: make(map[[2]byte]map[[2]byte]string),
}
reader := bytes.NewReader(data)
sig, err := binary.ReadU32Big(reader)
if err != nil {
return result, err
}
if s := Signature(sig); s != MultiLocalisedUnicodeSignature {
return result, fmt.Errorf("expected %v but got %v", MultiLocalisedUnicodeSignature, s)
}
// Reserved field
_, err = binary.ReadU32Big(reader)
if err != nil {
return result, err
}
recordCount, err := binary.ReadU32Big(reader)
if err != nil {
return result, err
}
recordSize, err := binary.ReadU32Big(reader)
if err != nil {
return result, err
}
for i := uint32(0); i < recordCount; i++ {
language := [2]byte{}
n, err := reader.Read(language[:])
if err != nil {
return result, err
}
if n < len(language) {
return result, fmt.Errorf("unexpected eof when reading language code")
}
country := [2]byte{}
n, err = reader.Read(country[:])
if err != nil {
return result, err
}
if n < len(country) {
return result, fmt.Errorf("unexpected eof when reading country code")
}
stringLength, err := binary.ReadU32Big(reader)
if err != nil {
return result, err
}
stringOffset, err := binary.ReadU32Big(reader)
if err != nil {
return result, err
}
if uint64(stringOffset+stringLength) > uint64(len(data)) {
return result, fmt.Errorf("record exceeds tag data length")
}
recordStringBytes := data[stringOffset : stringOffset+stringLength]
recordStringUTF16 := make([]uint16, len(recordStringBytes)/2)
for j := 0; j < len(recordStringUTF16); j++ {
recordStringUTF16[j], err = binary.ReadU16Big(reader)
if err != nil {
return result, err
}
}
result.setString(language, country, string(utf16.Decode(recordStringUTF16)))
// Skip to next record
for j := uint32(12); j < recordSize; j++ {
_, err := reader.ReadByte()
if err != nil {
return result, err
}
}
}
return result, nil
}
type languageCountry struct {
language [2]byte
country [2]byte
}
func (lc languageCountry) String() string {
return fmt.Sprintf("%c%c_%c%c", lc.language[0], lc.language[1], lc.country[0], lc.country[1])
}