-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphpserialize.go
More file actions
730 lines (633 loc) · 17.7 KB
/
phpserialize.go
File metadata and controls
730 lines (633 loc) · 17.7 KB
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
// Package phpserialize provides PHP-compatible serialize and un-serialize functions for Go.
// It supports all PHP data types and maintains compatibility with PHP 4, 5, 7, and 8.
package phpserialize
import (
"bytes"
"fmt"
"math"
"reflect"
"strconv"
"strings"
"unicode/utf8"
)
// PHPObject represents a PHP object in Go
type PHPObject struct {
ClassName string
Properties map[string]interface{}
}
type marshalConfig struct {
phpStrict bool
maxDepth int
}
type unmarshalConfig struct {
allowedClasses map[string]bool
allowAll bool
maxDepth int
}
// Option allows customization of serialize/un-serialize behavior
// This unified interface works for both Marshal and Unmarshal
type Option interface {
applyMarshal(*marshalConfig)
applyUnmarshal(*unmarshalConfig)
}
// maxDepthOption implements Option for depth limiting
type maxDepthOption struct {
depth int
}
func (o maxDepthOption) applyMarshal(cfg *marshalConfig) {
cfg.maxDepth = o.depth
}
func (o maxDepthOption) applyUnmarshal(cfg *unmarshalConfig) {
if o.depth == 0 {
cfg.maxDepth = 4096 // PHP default when 0 specified
} else {
cfg.maxDepth = o.depth
}
}
// strictPHPOption implements Option for PHP strict mode
type strictPHPOption struct {
strict bool
}
func (o strictPHPOption) applyMarshal(cfg *marshalConfig) {
cfg.phpStrict = o.strict
}
func (o strictPHPOption) applyUnmarshal(*unmarshalConfig) {
// No effect on unmarshal
}
// allowedClassesOption implements Option for class filtering
type allowedClassesOption struct {
classes []string
}
func (o allowedClassesOption) applyMarshal(*marshalConfig) {
// No effect on marshal
}
func (o allowedClassesOption) applyUnmarshal(cfg *unmarshalConfig) {
if o.classes == nil {
cfg.allowAll = false
cfg.allowedClasses = nil
return
}
cfg.allowAll = false
allowed := make(map[string]bool)
for _, c := range o.classes {
allowed[c] = true
}
cfg.allowedClasses = allowed
}
// WithMaxDepth limits nesting depth for both Marshal and Unmarshal
// For Marshal: 0 = unlimited (default)
// For Unmarshal: 0 will use PHP default of 4096
func WithMaxDepth(depth int) Option {
if depth < 0 {
depth = 0
}
return maxDepthOption{depth: depth}
}
// WithStrictPHP ensures output matches PHP rules (default true)
func WithStrictPHP(strict bool) Option {
return strictPHPOption{strict: strict}
}
// WithAllowedClasses restricts which PHP classes can be un-serialized
// If classes = nil, it disables object un-serialization (like PHP allowed_classes = false)
func WithAllowedClasses(classes []string) Option {
return allowedClassesOption{classes: classes}
}
// Marshal converts a Go value to PHP serialized format
func Marshal(value interface{}, options ...Option) (string, error) {
config := &marshalConfig{
phpStrict: true,
maxDepth: 0, // 0 = unlimited (PHP serialize has no max_depth)
}
for _, opt := range options {
opt.applyMarshal(config)
}
var buf bytes.Buffer
buf.Grow(256) // Pre-allocate reasonable size
err := marshalValue(&buf, value, config, 0)
if err != nil {
return "", err
}
return buf.String(), nil
}
// MarshalObject serializes a PHPObject
func MarshalObject(obj PHPObject, options ...Option) (string, error) {
config := &marshalConfig{
phpStrict: true,
maxDepth: 0,
}
for _, opt := range options {
opt.applyMarshal(config)
}
var buf bytes.Buffer
buf.Grow(256)
err := marshalObject(&buf, obj, config, 0)
if err != nil {
return "", err
}
return buf.String(), nil
}
// Unmarshal converts PHP serialized data to Go values
func Unmarshal(data string, options ...Option) (interface{}, error) {
config := &unmarshalConfig{
allowAll: true, // PHP default = all classes allowed
maxDepth: 4096, // PHP default max depth
}
for _, opt := range options {
opt.applyUnmarshal(config)
}
reader := &stringReader{data: data, pos: 0}
return unmarshalValue(reader, config, 0)
}
// stringReader helps to parse serialized data
type stringReader struct {
data string
pos int
}
func (r *stringReader) read() (byte, error) {
if r.pos >= len(r.data) {
return 0, fmt.Errorf("unexpected end of data at position %d", r.pos)
}
b := r.data[r.pos]
r.pos++
return b, nil
}
func (r *stringReader) peek() (byte, error) {
if r.pos >= len(r.data) {
return 0, fmt.Errorf("unexpected end of data at position %d", r.pos)
}
return r.data[r.pos], nil
}
func (r *stringReader) readUntil(delim byte) (string, error) {
start := r.pos
for r.pos < len(r.data) {
if r.data[r.pos] == delim {
result := r.data[start:r.pos]
r.pos++ // skip delimiter
return result, nil
}
r.pos++
}
return "", fmt.Errorf("delimiter '%c' not found after position %d", delim, start)
}
func (r *stringReader) readBytes(n int) (string, error) {
if r.pos+n > len(r.data) {
return "", fmt.Errorf("not enough data at position %d: need %d bytes, have %d", r.pos, n, len(r.data)-r.pos)
}
result := r.data[r.pos : r.pos+n]
r.pos += n
return result, nil
}
// marshalValue serializes any Go value
func marshalValue(buf *bytes.Buffer, value interface{}, cfg *marshalConfig, depth int) error {
if cfg.maxDepth > 0 && depth >= cfg.maxDepth {
return fmt.Errorf("exceeded max depth %d", cfg.maxDepth)
}
if value == nil {
buf.WriteString("N;")
return nil
}
v := reflect.ValueOf(value)
// Check for circular references in pointers
if v.Kind() == reflect.Ptr || v.Kind() == reflect.Map || v.Kind() == reflect.Slice {
if v.Kind() == reflect.Ptr {
if v.IsNil() {
buf.WriteString("N;")
return nil
}
// Dereference pointer
v = v.Elem()
}
}
switch v.Kind() {
case reflect.Bool:
if v.Bool() {
buf.WriteString("b:1;")
} else {
buf.WriteString("b:0;")
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
buf.WriteString(fmt.Sprintf("i:%d;", v.Int()))
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
u := v.Uint()
if cfg.phpStrict {
if u > math.MaxInt64 {
return fmt.Errorf("uint %d exceeds PHP int range", u)
}
buf.WriteString(fmt.Sprintf("i:%d;", int64(u)))
} else {
// Go-native: keep uint64 as-is in the serialized form.
buf.WriteString(fmt.Sprintf("u:%d;", u))
}
case reflect.Float32, reflect.Float64:
f := v.Float()
// Handle special float cases like PHP does
if math.IsNaN(f) {
buf.WriteString("d:NAN;")
} else if math.IsInf(f, 1) {
buf.WriteString("d:INF;")
} else if math.IsInf(f, -1) {
buf.WriteString("d:-INF;")
} else {
buf.WriteString("d:" + strconv.FormatFloat(f, 'f', -1, 64) + ";")
}
case reflect.String:
str := v.String()
// PHP serialization uses byte length, not character count
byteLen := len(str)
buf.WriteString(fmt.Sprintf("s:%d:\"%s\";", byteLen, str))
case reflect.Slice, reflect.Array:
length := v.Len()
buf.WriteString(fmt.Sprintf("a:%d:{", length))
for i := 0; i < length; i++ {
// Serialize index
buf.WriteString(fmt.Sprintf("i:%d;", i))
// Serialize value with incremented depth
if err := marshalValue(buf, v.Index(i).Interface(), cfg, depth+1); err != nil {
return err
}
}
buf.WriteString("}")
case reflect.Map:
length := v.Len()
buf.WriteString(fmt.Sprintf("a:%d:{", length))
keys := v.MapKeys()
for _, key := range keys {
switch key.Kind() {
case reflect.String:
keyStr := key.String()
buf.WriteString(fmt.Sprintf("s:%d:\"%s\";", len(keyStr), keyStr))
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
buf.WriteString(fmt.Sprintf("i:%d;", key.Int()))
default:
return fmt.Errorf("cannot serialize map with key type %v", key.Kind())
}
if err := marshalValue(buf, v.MapIndex(key).Interface(), cfg, depth+1); err != nil {
return err
}
}
buf.WriteString("}")
case reflect.Struct:
// Check if it's a PHPObject
if obj, ok := value.(PHPObject); ok {
return marshalObject(buf, obj, cfg, depth)
}
// For other structs, convert to map
return fmt.Errorf("cannot serialize struct type %T directly, use PHPObject or convert to map", value)
case reflect.Ptr:
if v.IsNil() {
buf.WriteString("N;")
return nil
}
return marshalValue(buf, v.Elem().Interface(), cfg, depth)
default:
return fmt.Errorf("cannot serialize type %s", v.Kind())
}
return nil
}
// marshalObject serializes a PHPObject
func marshalObject(buf *bytes.Buffer, obj PHPObject, cfg *marshalConfig, depth int) error {
if cfg.maxDepth > 0 && depth >= cfg.maxDepth {
return fmt.Errorf("exceeded max depth %d", cfg.maxDepth)
}
classNameLen := len(obj.ClassName)
propCount := len(obj.Properties)
buf.WriteString(fmt.Sprintf("O:%d:\"%s\":%d:{", classNameLen, obj.ClassName, propCount))
for key, value := range obj.Properties {
// Serialize property name
buf.WriteString(fmt.Sprintf("s:%d:\"%s\";", len(key), key))
// Serialize property value with incremented depth
if err := marshalValue(buf, value, cfg, depth+1); err != nil {
return err
}
}
buf.WriteString("}")
return nil
}
// unmarshalValue un-serializes a single value
func unmarshalValue(r *stringReader, cfg *unmarshalConfig, depth int) (interface{}, error) {
if cfg.maxDepth > 0 && depth >= cfg.maxDepth {
return nil, fmt.Errorf("exceeded max depth %d at position %d", cfg.maxDepth, r.pos)
}
typeChar, err := r.read()
if err != nil {
return nil, err
}
// Expect ':' after type (except for N)
if typeChar != 'N' {
colon, err := r.read()
if err != nil {
return nil, err
}
if colon != ':' {
return nil, fmt.Errorf("at position %d: expected ':' after type '%c', got '%c'", r.pos-1, typeChar, colon)
}
}
switch typeChar {
case 'N': // NULL
semicolon, err := r.read()
if err != nil {
return nil, err
}
if semicolon != ';' {
return nil, fmt.Errorf("at position %d: expected ';' after NULL, got '%c'", r.pos-1, semicolon)
}
return nil, nil
case 'b': // Boolean
valStr, err := r.readUntil(';')
if err != nil {
return nil, err
}
return valStr == "1", nil
case 'i': // Integer
valStr, err := r.readUntil(';')
if err != nil {
return nil, err
}
val, err := strconv.ParseInt(valStr, 10, 64)
if err != nil {
return nil, fmt.Errorf("at position %d: invalid integer: %s", r.pos, valStr)
}
return val, nil
case 'd': // Double/Float
valStr, err := r.readUntil(';')
if err != nil {
return nil, err
}
// Handle special cases
switch valStr {
case "NAN":
return math.NaN(), nil
case "INF":
return math.Inf(1), nil
case "-INF":
return math.Inf(-1), nil
}
val, err := strconv.ParseFloat(valStr, 64)
if err != nil {
return nil, fmt.Errorf("at position %d: invalid float: %s", r.pos, valStr)
}
return val, nil
case 's': // String
lenStr, err := r.readUntil(':')
if err != nil {
return nil, err
}
length, err := strconv.Atoi(lenStr)
if err != nil {
return nil, fmt.Errorf("at position %d: invalid string length: %s", r.pos, lenStr)
}
// Validate string length
if length < 0 {
return nil, fmt.Errorf("at position %d: negative string length: %d", r.pos, length)
}
// Read opening quote
quote, err := r.read()
if err != nil {
return nil, err
}
if quote != '"' {
return nil, fmt.Errorf("at position %d: expected '\"' before string, got '%c'", r.pos-1, quote)
}
// Read string bytes (not characters)
str, err := r.readBytes(length)
if err != nil {
return nil, err
}
// Read closing quote
quote, err = r.read()
if err != nil {
return nil, err
}
if quote != '"' {
return nil, fmt.Errorf("at position %d: expected '\"' after string, got '%c'", r.pos-1, quote)
}
// Read semicolon
semicolon, err := r.read()
if err != nil {
return nil, err
}
if semicolon != ';' {
return nil, fmt.Errorf("at position %d: expected ';' after string, got '%c'", r.pos-1, semicolon)
}
return str, nil
case 'a': // Array
countStr, err := r.readUntil(':')
if err != nil {
return nil, err
}
count, err := strconv.Atoi(countStr)
if err != nil {
return nil, fmt.Errorf("at position %d: invalid array count: %s", r.pos, countStr)
}
// Validate array size
if count < 0 {
return nil, fmt.Errorf("at position %d: negative array count: %d", r.pos, count)
}
// Read opening brace
brace, err := r.read()
if err != nil {
return nil, err
}
if brace != '{' {
return nil, fmt.Errorf("at position %d: expected '{' for array, got '%c'", r.pos-1, brace)
}
// Check if it's an indexed array (all keys are sequential integers starting from 0)
isIndexed := true
tempMap := make(map[string]interface{})
indices := make([]int, 0, count)
for i := 0; i < count; i++ {
// Read key with incremented depth
key, err := unmarshalValue(r, cfg, depth+1)
if err != nil {
return nil, err
}
// Read value with incremented depth
value, err := unmarshalValue(r, cfg, depth+1)
if err != nil {
return nil, err
}
// Check if key is an integer
switch k := key.(type) {
case int64:
indices = append(indices, int(k))
tempMap[strconv.Itoa(int(k))] = value
case string:
isIndexed = false
tempMap[k] = value
default:
keyStr := fmt.Sprintf("%v", k)
tempMap[keyStr] = value
isIndexed = false
}
}
// Read closing brace
brace, err = r.read()
if err != nil {
return nil, err
}
if brace != '}' {
return nil, fmt.Errorf("at position %d: expected '}' for array, got '%c'", r.pos-1, brace)
}
// If it's an indexed array with sequential keys, return a slice
if isIndexed && len(indices) > 0 {
// Check if indices are sequential starting from 0
sequential := true
for i, idx := range indices {
if idx != i {
sequential = false
break
}
}
if sequential {
result := make([]interface{}, len(indices))
for i := 0; i < len(indices); i++ {
result[i] = tempMap[strconv.Itoa(i)]
}
return result, nil
}
}
// Otherwise, return a map
if len(tempMap) == 0 {
return make(map[string]interface{}), nil
}
return tempMap, nil
case 'O': // Object
classLenStr, err := r.readUntil(':')
if err != nil {
return nil, err
}
classLen, err := strconv.Atoi(classLenStr)
if err != nil {
return nil, fmt.Errorf("at position %d: invalid class name length: %s", r.pos, classLenStr)
}
if classLen < 0 {
return nil, fmt.Errorf("at position %d: negative class name length: %d", r.pos, classLen)
}
// Read opening quote
quote, err := r.read()
if err != nil {
return nil, err
}
if quote != '"' {
return nil, fmt.Errorf("at position %d: expected '\"' before class name, got '%c'", r.pos-1, quote)
}
// Read class name
className, err := r.readBytes(classLen)
if err != nil {
return nil, err
}
if !cfg.allowAll {
if cfg.allowedClasses == nil || !cfg.allowedClasses[className] {
return nil, fmt.Errorf("at position %d: class %q not allowed", r.pos, className)
}
}
// Read closing quote
quote, err = r.read()
if err != nil {
return nil, err
}
if quote != '"' {
return nil, fmt.Errorf("at position %d: expected '\"' after class name, got '%c'", r.pos-1, quote)
}
// Read colon
colon, err := r.read()
if err != nil {
return nil, err
}
if colon != ':' {
return nil, fmt.Errorf("at position %d: expected ':' after class name, got '%c'", r.pos-1, colon)
}
// Read property count
propCountStr, err := r.readUntil(':')
if err != nil {
return nil, err
}
propCount, err := strconv.Atoi(propCountStr)
if err != nil {
return nil, fmt.Errorf("at position %d: invalid property count: %s", r.pos, propCountStr)
}
// Validate property count
if propCount < 0 {
return nil, fmt.Errorf("at position %d: negative property count: %d", r.pos, propCount)
}
// Read opening brace
brace, err := r.read()
if err != nil {
return nil, err
}
if brace != '{' {
return nil, fmt.Errorf("at position %d: expected '{' for object properties, got '%c'", r.pos-1, brace)
}
properties := make(map[string]interface{})
for i := 0; i < propCount; i++ {
// Read property name with incremented depth
propName, err := unmarshalValue(r, cfg, depth+1)
if err != nil {
return nil, err
}
// Read property value with incremented depth
propValue, err := unmarshalValue(r, cfg, depth+1)
if err != nil {
return nil, err
}
if name, ok := propName.(string); ok {
// Remove visibility prefixes if present (PHP private/protected properties)
// Private: \0ClassName\0propertyName
// Protected: \0*\0propertyName
if strings.Contains(name, "\x00") {
parts := strings.Split(name, "\x00")
if len(parts) > 0 {
name = parts[len(parts)-1]
}
}
properties[name] = propValue
} else {
properties[fmt.Sprintf("%v", propName)] = propValue
}
}
// Read closing brace
brace, err = r.read()
if err != nil {
return nil, err
}
if brace != '}' {
return nil, fmt.Errorf("at position %d: expected '}' for object, got '%c'", r.pos-1, brace)
}
return PHPObject{
ClassName: className,
Properties: properties,
}, nil
default:
return nil, fmt.Errorf("at position %d: unknown type '%c'", r.pos-1, typeChar)
}
}
// Helper functions for common use cases
// IsValidMarshaled checks if a string is valid PHP serialized data
func IsValidMarshaled(data string) bool {
_, err := Unmarshal(data)
return err == nil
}
// MustMarshal serializes a value and panics on error
func MustMarshal(value interface{}, options ...Option) string {
result, err := Marshal(value, options...)
if err != nil {
panic(err)
}
return result
}
// MustUnmarshal un-serializes data and panics on error
func MustUnmarshal(data string, options ...Option) interface{} {
result, err := Unmarshal(data, options...)
if err != nil {
panic(err)
}
return result
}
// GetStringLength returns the byte length of a string as PHP would calculate it
func GetStringLength(s string) int {
return len(s)
}
// IsUTF8 checks if a string is valid UTF-8
func IsUTF8(s string) bool {
return utf8.ValidString(s)
}