-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgophpserialize.go
201 lines (170 loc) · 3.22 KB
/
gophpserialize.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
package gophpserialize
import (
"encoding/json"
"strconv"
"strings"
)
type Serializer struct {
raw []byte
pos int
}
func (s *Serializer) SetRaw(msg []byte) {
s.raw = msg
}
func (s *Serializer) read() map[string]interface{} {
r := s.readValue().(map[string]interface{})
return r
}
func (s *Serializer) readType() byte {
result := s.raw[s.pos]
s.move()
return result
}
func (s *Serializer) readBool() bool {
result := s.raw[s.pos]
s.move()
if result == '0' {
return false
}
return true
}
func (s *Serializer) readInt() int {
start := s.pos
end := start + 1
c := s.raw[end]
for c != ':' && c != ';' {
end = end + 1
c = s.raw[end]
}
s.pos = end
i, _ := strconv.ParseInt(string(s.raw[start:end]), 10, 32)
return int(i)
}
func (s *Serializer) readFloat() float64 {
start := s.pos
end := start + 1
c := s.raw[end]
for c != ':' && c != ';' {
end = end + 1
c = s.raw[end]
}
s.pos = end
d, _ := strconv.ParseFloat(string(s.raw[start:end]), 64)
return d
}
func (s *Serializer) readString(size int) string {
s.move()
result := string(s.raw[s.pos : s.pos+size])
s.pos += size + 1
return strings.Replace(result, "\000", "", -1)
}
func (s *Serializer) readValue() interface{} {
objType := s.readType()
if objType == 'N' {
s.move()
return nil
}
if objType == 'i' {
s.move()
val := s.readInt()
s.move()
return val
}
if objType == 'd' {
s.move()
val := s.readFloat()
s.move()
return val
}
if objType == 'b' {
s.move()
val := s.readBool()
s.move()
return val
}
if objType == 's' {
s.move()
size := s.readInt()
s.move()
val := s.readString(size)
s.move()
return val
}
if objType == 'a' {
s.move()
size := s.readInt()
s.move()
// array open {
s.move()
r := make(map[string]interface{})
l := make([]interface{}, 0)
//hack to handle array that has both string/int as key
//convert int key to string key
hasStringKey := false
notAList := false
for i := 0; i < size; i++ {
key := s.readValue()
val := s.readValue()
switch v2 := key.(type) {
case string:
hasStringKey = true
r[v2] = val
case int:
if hasStringKey || v2 != i || notAList {
r[strconv.Itoa(v2)] = val
if v2 != i {
notAList = true
}
} else {
l = append(l, val)
}
}
}
if len(r) > 0 && len(l) > 0 {
for i, val := range l {
r[strconv.Itoa(i)] = val
}
}
// array close }
s.move()
if len(r) == 0 {
return l
}
return r
}
if objType == 'O' {
s.move()
size := s.readInt()
s.move()
s.readString(size)
s.move()
objectSize := s.readInt()
s.move()
// object content open
s.move()
r := make(map[string]interface{})
//hack to handle array that has both string/int as key
for i := 0; i < objectSize; i++ {
propertyName := s.readValue().(string)
val := s.readValue()
r[propertyName] = val
}
// object content close }
s.move()
return r
}
panic("Unknown objType: " + string(objType) + "\n" + string(s.raw))
}
func (s *Serializer) move() {
s.pos += 1
}
func Unmarshal(data []byte) interface{} {
s := new(Serializer)
s.SetRaw(data)
return s.readValue()
}
func PhpToJson(phpData []byte) (jsonData []byte, err error) {
r := Unmarshal(phpData)
jsonData, err = json.Marshal(r)
return
}