forked from yvasiyarov/php_session_decoder
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdecoder.go
67 lines (58 loc) · 1.21 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
package php_session_decoder
import (
"bytes"
"io"
"strings"
"github.com/yvasiyarov/php_session_decoder/php_serialize"
)
type PhpDecoder struct {
source *strings.Reader
decoder *php_serialize.UnSerializer
}
func NewPhpDecoder(phpSession string) *PhpDecoder {
decoder := &PhpDecoder{
source: strings.NewReader(phpSession),
decoder: php_serialize.NewUnSerializer(""),
}
decoder.decoder.SetReader(decoder.source)
return decoder
}
func (self *PhpDecoder) SetSerializedDecodeFunc(f php_serialize.SerializedDecodeFunc) {
self.decoder.SetSerializedDecodeFunc(f)
}
func (self *PhpDecoder) Decode() (PhpSession, error) {
var (
name string
err error
value php_serialize.PhpValue
)
res := make(PhpSession)
for {
if name, err = self.readName(); err != nil {
break
}
if value, err = self.decoder.Decode(); err != nil {
break
}
res[name] = value
}
if err == io.EOF {
err = nil
}
return res, err
}
func (self *PhpDecoder) readName() (string, error) {
var (
token rune
err error
)
buf := bytes.NewBuffer([]byte{})
for {
if token, _, err = self.source.ReadRune(); err != nil || token == SEPARATOR_VALUE_NAME {
break
} else {
buf.WriteRune(token)
}
}
return buf.String(), err
}