-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecoder.go
241 lines (216 loc) · 5.37 KB
/
decoder.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// Copyright 2018 Sean.ZH
package libcode
import (
"errors"
"log"
"math"
"unicode/utf8"
dio "github.com/dilfish/tools/io"
)
// ErrBadCoreValueStr indicate an invalid string which could
// not resolved as list of code
var ErrBadCoreValueStr = errors.New("bad core value string")
// ErrTooManyEncoder indicate that you registerd more than 12 encoder
var ErrTooManyEncoder = errors.New("too many encoder")
// ErrReReg indicate that you register a registerd encoder
var ErrReReg = errors.New("re register")
// 12 words, default, unicode, common_han using 1 for each
// TotalCv is 12 because Mr Xi only uses 12 words as core value
const TotalCv = 12
// ChnPrefix used as chinese word
const ChnPrefix = 11
// UniPrefix used as unicode point
const UniPrefix = 10
// DefPrefix used as default encoder
const DefPrefix = 9
// BadPrefix used as others
const BadPrefix = -1
const modNum = int32(9)
// BadRune is defines in utf8 lib
const BadRune = utf8.RuneError
// BadCode is defined negative value
const BadCode = int32(-1)
func deBaseFunc(list []int32) rune {
if list == nil || len(list) == 0 {
return BadRune
}
v := int64(0)
for _, l := range list {
v = v*int64(modNum) + int64(l)
if v > math.MaxInt32 {
log.Println("not valid rune:", v)
return BadRune
}
}
return rune(v)
}
func (lc *LibCode) decodeWord(list []int32) rune {
if len(list) == 0 {
return 0
}
t := list[0]
code := deBaseFunc(list[1:])
if code == BadRune {
log.Println("not valid rune:", list[1:])
return BadRune
}
switch t {
case ChnPrefix:
return lc.ch.DecodeCommonHan(code)
case UniPrefix:
return DecodeUnicode(code)
case DefPrefix:
return DecodeDefault(code)
}
log.Println("not a valid prefix:", t)
return BadRune
}
func (lc *LibCode) decodeIndice(indice []int32) (string, error) {
var list []int32
orig := ""
for _, index := range indice {
if index != ChnPrefix && index != DefPrefix && index != UniPrefix {
list = append(list, index)
continue
}
o := lc.decodeWord(list)
if o == BadRune {
log.Println("bad word:", list)
return "", ErrBadCoreValueStr
}
if o != 0 {
orig = orig + string(o)
}
list = make([]int32, 0)
list = append(list, index)
}
o := lc.decodeWord(list)
if o == BadRune {
log.Println("bad word:", list)
return "", ErrBadCoreValueStr
}
if o != 0 {
orig = orig + string(o)
}
return orig, nil
}
func (lc *LibCode) unMapCoreValue(cv string) ([]int32, error) {
var cvs []string
var list []int32
for len(cv) > 0 {
r, size := utf8.DecodeLastRuneInString(cv)
cv = cv[:len(cv)-size]
cvs = append([]string{string(r)}, cvs...)
}
if len(cvs)%2 != 0 {
log.Println("core value error:", cvs)
return nil, ErrBadCoreValueStr
}
for i := 0; i < len(cvs); i = i + 2 {
cvWord := cvs[i] + cvs[i+1]
idx, ok := lc.coreValueMap[cvWord]
if ok == false {
log.Println("bad core value string:", cvWord)
return nil, ErrBadCoreValueStr
}
list = append(list, idx)
}
return list, nil
}
// Decoder read a list of encoded string
// and send them to right decoder
func (lc *LibCode) Decoder(cv string) (string, error) {
indice, err := lc.unMapCoreValue(cv)
if err != nil {
log.Println("decode error:", err)
return "", err
}
orig, err := lc.decodeIndice(indice)
if err != nil {
log.Println("decode index error:", indice)
return "", err
}
return orig, nil
}
func (lc *LibCode) readCoreValue(line string) error {
if utf8.RuneCountInString(line) != 2 {
log.Println("core value has to be 2 char:", line)
return ErrBadCoreValueStr
}
idx := (lc.idx + lc.coreValueOffset) % 12
lc.coreValueMap[line] = idx
lc.revCoreValueMap[idx] = line
lc.idx++
return nil
}
func baseFunc(index rune) []int32 {
var off []int32
for index > modNum {
num := index % modNum
index = index / modNum
off = append([]int32{num}, off...)
}
num := index % modNum
off = append([]int32{num}, off...)
return off
}
func (lc *LibCode) getCode(r rune) (int32, int32) {
code := lc.ch.EncodeCommonHan(r)
if code != BadCode {
log.Println("encode error:", r)
return code, ChnPrefix
}
code = EncodeUnicode(r)
if code != BadCode {
log.Println("encode unicode error:", r)
return code, UniPrefix
}
return EncodeDefault(r), DefPrefix
}
func (lc *LibCode) getList(r rune) []int32 {
code, prefix := lc.getCode(r)
list := baseFunc(code)
list = append([]int32{prefix}, list...)
return list
}
// Encoder transform original message to core value message
func (lc *LibCode) Encoder(orig string) string {
cv := ""
for _, o := range orig {
code := lc.getList(rune(o))
for _, c := range code {
cv = cv + lc.revCoreValueMap[c]
}
}
return cv
}
// LibCode defines a list of encoder decoder pair
type LibCode struct {
coreValueMap map[string]int32
coreValueOffset int32
revCoreValueMap map[int32]string
idx int32
ch *CommonHanEncoder
}
// NewLibCode get an object
func NewLibCode(cv, ch string, offset int32) (*LibCode, error) {
lc := new(LibCode)
lc.coreValueMap = make(map[string]int32)
lc.revCoreValueMap = make(map[int32]string)
lc.coreValueOffset = offset
err := dio.ReadLine(cv, lc.readCoreValue)
if err != nil {
log.Println("read core value error:", err)
return nil, err
}
if len(lc.coreValueMap) != TotalCv {
log.Println("core value count is not right:", len(lc.coreValueMap))
return nil, ErrBadCoreValueStr
}
lc.ch, err = NewCommonHan(ch)
if err != nil {
log.Println("common han error:", err)
return nil, err
}
return lc, nil
}