forked from bigdrum/phpserialize
-
Notifications
You must be signed in to change notification settings - Fork 1
/
decoder.go
167 lines (156 loc) · 4.38 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
package phpserialize
import (
"bytes"
"errors"
"fmt"
"strconv"
"strings"
)
type PhpDecoder struct {
source *strings.Reader
}
func Decode(value string) (result interface{}, err error) {
decoder := &PhpDecoder{
source: strings.NewReader(value),
}
result, err = decoder.DecodeValue()
return
}
//all integer is int64,float number is float64
func (decoder *PhpDecoder) DecodeValue() (value interface{}, err error) {
if token, _, err := decoder.source.ReadRune(); err == nil {
if token == 'N' {
err = decoder.expect(VALUES_SEPARATOR)
return nil, err
}
decoder.expect(TYPE_VALUE_SEPARATOR)
switch token {
case 'b':
if rawValue, _, _err := decoder.source.ReadRune(); _err == nil {
value = rawValue == '1'
} else {
err = errors.New("Can not read boolean value")
}
if err != nil {
return nil, err
}
err = decoder.expect(VALUES_SEPARATOR)
case 'i':
if rawValue, _err := decoder.readUntil(VALUES_SEPARATOR); _err == nil {
if tmpv, _err := strconv.Atoi(rawValue); _err != nil {
err = fmt.Errorf("Can not convert %v to Int:%v", rawValue, _err)
} else {
value = int64(tmpv)
}
} else {
err = errors.New("Can not read int value")
}
case 'd':
if rawValue, _err := decoder.readUntil(VALUES_SEPARATOR); _err == nil {
if value, _err = strconv.ParseFloat(rawValue, 64); _err != nil {
err = fmt.Errorf("Can not convert %v to Float:%v", rawValue, _err)
}
} else {
err = errors.New("Can not read float value")
}
case 's':
value, err = decoder.decodeString()
if err != nil {
return nil, err
}
err = decoder.expect(VALUES_SEPARATOR)
case 'a':
value, err = decoder.decodeArray()
case 'O':
value, err = decoder.decodeObject()
}
}
return value, err
}
func (decoder *PhpDecoder) decodeObject() (*PhpObject, error) {
value := &PhpObject{}
var err error
if value.className, err = decoder.decodeString(); err != nil {
return nil, err
}
if err = decoder.expect(TYPE_VALUE_SEPARATOR); err != nil {
return nil, err
}
if value.members, err = decoder.decodeArray(); err != nil {
return nil, err
}
return value, err
}
func (decoder *PhpDecoder) decodeArray() (value map[interface{}]interface{}, err error) {
value = make(map[interface{}]interface{})
if rawArrlen, _err := decoder.readUntil(TYPE_VALUE_SEPARATOR); _err == nil {
if arrLen, _err := strconv.Atoi(rawArrlen); _err != nil {
err = fmt.Errorf("Can not convert array length %v to int:%v", rawArrlen, _err)
} else {
decoder.expect('{')
for i := 0; i < arrLen; i++ {
if k, _err := decoder.DecodeValue(); _err != nil {
err = fmt.Errorf("Can not read array key %v", _err)
} else if v, _err := decoder.DecodeValue(); _err != nil {
err = fmt.Errorf("Can not read array value %v", _err)
} else {
switch t := k.(type) {
default:
err = fmt.Errorf("Unexpected key type %T", t)
case string, int64, float64:
value[k] = v
}
}
}
decoder.expect('}')
}
} else {
err = errors.New("Can not read array length")
}
return value, err
}
func (decoder *PhpDecoder) decodeString() (value string, err error) {
if rawStrlen, _err := decoder.readUntil(TYPE_VALUE_SEPARATOR); _err == nil {
if strLen, _err := strconv.Atoi(rawStrlen); _err != nil {
err = errors.New(fmt.Sprintf("Can not convert string length %v to int:%v", rawStrlen, _err))
} else {
if err = decoder.expect('"'); err != nil {
return
}
tmpValue := make([]byte, strLen, strLen)
if nRead, _err := decoder.source.Read(tmpValue); _err != nil || nRead != strLen {
err = errors.New(fmt.Sprintf("Can not read string content %v. Read only: %v from %v", _err, nRead, strLen))
} else {
value = string(tmpValue)
err = decoder.expect('"')
}
}
} else {
err = errors.New("Can not read string length")
}
return value, err
}
func (decoder *PhpDecoder) readUntil(stopByte byte) (string, error) {
result := new(bytes.Buffer)
var (
token byte
err error
)
for {
if token, err = decoder.source.ReadByte(); err != nil || token == stopByte {
break
} else {
result.WriteByte(token)
}
}
return result.String(), err
}
func (decoder *PhpDecoder) expect(expectRune rune) error {
token, _, err := decoder.source.ReadRune()
if err != nil {
err = fmt.Errorf("Can not read expected: %v", expectRune)
} else if token != expectRune {
err = fmt.Errorf("Read %v, but expected: %v", token, expectRune)
}
return err
}