-
Notifications
You must be signed in to change notification settings - Fork 39
/
serialize.go
315 lines (260 loc) Β· 8.57 KB
/
serialize.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package phpserialize
import (
"bytes"
"fmt"
"reflect"
"sort"
"strconv"
"strings"
)
// MarshalOptions must be provided when invoking Marshal(). Use
// DefaultMarshalOptions() for sensible defaults.
type MarshalOptions struct {
// If this is true, then all struct names will be stripped from objects
// and "stdClass" will be used instead. The default value is false.
OnlyStdClass bool
}
// DefaultMarshalOptions will create a new instance of MarshalOptions with
// sensible defaults. See MarshalOptions for a full description of options.
func DefaultMarshalOptions() *MarshalOptions {
options := new(MarshalOptions)
options.OnlyStdClass = false
return options
}
// MarshalBool returns the bytes to represent a PHP serialized bool value. This
// would be the equivalent to running:
//
// echo serialize(false);
// // b:0;
//
// The same result would be returned by marshalling a boolean value:
//
// Marshal(true)
func MarshalBool(value bool) []byte {
if value {
return []byte("b:1;")
}
return []byte("b:0;")
}
// MarshalInt returns the bytes to represent a PHP serialized integer value.
// This would be the equivalent to running:
//
// echo serialize(123);
// // i:123;
//
// The same result would be returned by marshalling an integer value:
//
// Marshal(123)
func MarshalInt(value int64) []byte {
return []byte("i:" + strconv.FormatInt(value, 10) + ";")
}
// MarshalUint is provided for compatibility with unsigned types in Go. It works
// the same way as MarshalInt.
func MarshalUint(value uint64) []byte {
return []byte("i:" + strconv.FormatUint(value, 10) + ";")
}
// MarshalFloat returns the bytes to represent a PHP serialized floating-point
// value. This would be the equivalent to running:
//
// echo serialize(1.23);
// // d:1.23;
//
// The bitSize should represent the size of the float. This makes conversion to
// a string value more accurate, for example:
//
// // float64 is implicit for literals
// MarshalFloat(1.23, 64)
//
// // If the original value was cast from a float32
// f := float32(1.23)
// MarshalFloat(float64(f), 32)
//
// The same result would be returned by marshalling a floating-point value:
//
// Marshal(1.23)
func MarshalFloat(value float64, bitSize int) []byte {
return []byte("d:" + strconv.FormatFloat(value, 'f', -1, bitSize) + ";")
}
// MarshalString returns the bytes to represent a PHP serialized string value.
// This would be the equivalent to running:
//
// echo serialize('Hello world');
// // s:11:"Hello world";
//
// The same result would be returned by marshalling a string value:
//
// Marshal('Hello world')
//
// One important distinction is that PHP stores binary data in strings. See
// MarshalBytes for more information.
func MarshalString(value string) []byte {
// As far as I can tell only the single-quote is escaped. Not even the
// backslash itself is escaped. Weird. See escapeTests for more information.
value = strings.Replace(value, "'", "\\'", -1)
return []byte(fmt.Sprintf("s:%d:\"%s\";", len(value), value))
}
// MarshalBytes returns the bytes to represent a PHP serialized string value
// that contains binary data. This is because PHP does not have a distinct type
// for binary data.
//
// This can cause some confusion when decoding the value as it will want to
// unmarshal as a string type. The Unmarshal() function will be sensitive to
// this condition and allow either a string or []byte when unserializing a PHP
// string.
func MarshalBytes(value []byte) []byte {
var buffer bytes.Buffer
for _, c := range value {
buffer.WriteString(fmt.Sprintf("\\x%02x", c))
}
return []byte(fmt.Sprintf("s:%d:\"%s\";", len(value), buffer.String()))
}
// MarshalNil returns the bytes to represent a PHP serialized null value.
// This would be the equivalent to running:
//
// echo serialize(null);
// // N;
//
// Unlike the other specific Marshal functions it does not take an argument
// because the output is a constant value.
func MarshalNil() []byte {
return []byte("N;")
}
// MarshalStruct returns the bytes that represent a PHP encoded class from a
// struct or pointer to a struct.
//
// Fields that are not exported (starting with a lowercase letter) will not be
// present in the output. All fields that appear in the output will have their
// first letter converted to lowercase. Any other uppercase letters in the field
// name are maintained. At the moment there is no way to change this behaviour,
// unlike other marshallers that use a tag on the field.
func MarshalStruct(input interface{}, options *MarshalOptions) ([]byte, error) {
value := reflect.ValueOf(input)
typeOfValue := value.Type()
// Some of the fields in the struct may not be visible (unexported). We
// need to make sure we count all the visible ones for the final result.
visibleFieldCount := 0
var buffer bytes.Buffer
for i := 0; i < value.NumField(); i++ {
f := value.Field(i)
if !f.CanInterface() {
// This is an unexported field, we cannot read it.
continue
}
visibleFieldCount++
fieldName, fieldOptions := parseTag(typeOfValue.Field(i).Tag.Get("php"))
if fieldOptions.Contains("omitnilptr") {
if f.Kind() == reflect.Ptr && f.IsNil() {
visibleFieldCount--
continue
}
}
if fieldName == "-" {
visibleFieldCount--
continue
} else if fieldName == "" {
fieldName = lowerCaseFirstLetter(typeOfValue.Field(i).Name)
}
buffer.Write(MarshalString(fieldName))
m, err := Marshal(f.Interface(), options)
if err != nil {
return nil, err
}
buffer.Write(m)
}
className := reflect.ValueOf(input).Type().Name()
if options.OnlyStdClass {
className = "stdClass"
}
return []byte(fmt.Sprintf("O:%d:\"%s\":%d:{%s}", len(className),
className, visibleFieldCount, buffer.String())), nil
}
// Marshal is the canonical way to perform the equivalent of serialize() in PHP.
// It can handle encoding scalar types, slices and maps.
func Marshal(input interface{}, options *MarshalOptions) ([]byte, error) {
if options == nil {
options = DefaultMarshalOptions()
}
// []byte is a special case because all strings (binary and otherwise)
// are handled as strings in PHP.
if bytesToEncode, ok := input.([]byte); ok {
return MarshalBytes(bytesToEncode), nil
}
// Nil is another special case because it is typeless and must be
// handled before trying to determine the type.
if input == nil {
return MarshalNil(), nil
}
// Otherwise we need to decide if it is a scalar value, map or slice.
value := reflect.ValueOf(input)
switch value.Kind() {
case reflect.Bool:
return MarshalBool(value.Bool()), nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32,
reflect.Int64:
return MarshalInt(value.Int()), nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return MarshalUint(value.Uint()), nil
case reflect.Float32:
return MarshalFloat(value.Float(), 32), nil
case reflect.Float64:
return MarshalFloat(value.Float(), 64), nil
case reflect.String:
return MarshalString(value.String()), nil
case reflect.Slice:
return marshalSlice(value.Interface(), options)
case reflect.Map:
return marshalMap(value.Interface(), options)
case reflect.Struct:
return MarshalStruct(input, options)
case reflect.Ptr:
if value.IsNil() {
return MarshalNil(), nil
}
return Marshal(value.Elem().Interface(), options)
default:
return nil, fmt.Errorf("can not encode: %T", input)
}
}
func marshalSlice(input interface{}, options *MarshalOptions) ([]byte, error) {
s := reflect.ValueOf(input)
var buffer bytes.Buffer
for i := 0; i < s.Len(); i++ {
m, err := Marshal(i, options)
if err != nil {
return nil, err
}
buffer.Write(m)
m, err = Marshal(s.Index(i).Interface(), options)
if err != nil {
return nil, err
}
buffer.Write(m)
}
return []byte(fmt.Sprintf("a:%d:{%s}", s.Len(), buffer.String())), nil
}
func marshalMap(input interface{}, options *MarshalOptions) ([]byte, error) {
s := reflect.ValueOf(input)
// Go randomises maps. To be able to test this we need to make sure the
// map keys always come out in the same order. So we sort them first.
mapKeys := s.MapKeys()
sort.Slice(mapKeys, func(i, j int) bool {
return lessValue(mapKeys[i], mapKeys[j])
})
var buffer bytes.Buffer
for _, mapKey := range mapKeys {
m, err := Marshal(mapKey.Interface(), options)
if err != nil {
return nil, err
}
buffer.Write(m)
m, err = Marshal(s.MapIndex(mapKey).Interface(), options)
if err != nil {
return nil, err
}
buffer.Write(m)
}
return []byte(fmt.Sprintf("a:%d:{%s}", s.Len(), buffer.String())), nil
}
func lowerCaseFirstLetter(s string) string {
return strings.ToLower(s[0:1]) + s[1:]
}