-
-
Notifications
You must be signed in to change notification settings - Fork 182
/
plistcodec.go
executable file
·59 lines (52 loc) · 1.61 KB
/
plistcodec.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
package ios
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"io"
"reflect"
log "github.com/sirupsen/logrus"
)
//PlistCodec is a codec for PLIST based services with [4 byte big endian length][plist-payload] based messages
type PlistCodec struct {
}
//NewPlistCodec create a codec for PLIST based services with [4 byte big endian length][plist-payload] based messages
func NewPlistCodec() PlistCodec {
return PlistCodec{}
}
//Encode encodes a LockDown Struct to a byte[] with the lockdown plist format.
//It returns a byte array that contains a 4 byte length unsigned big endian integer
//followed by the plist as a string
func (plistCodec PlistCodec) Encode(message interface{}) ([]byte, error) {
stringContent := ToPlist(message)
log.Tracef("Lockdown send %v", reflect.TypeOf(message))
buf := new(bytes.Buffer)
length := len(stringContent)
messageLength := uint32(length)
err := binary.Write(buf, binary.BigEndian, messageLength)
if err != nil {
return nil, err
}
buf.Write([]byte(stringContent))
return buf.Bytes(), nil
}
//Decode reads a Lockdown Message from the provided reader and
//sends it to the ResponseChannel
func (plistCodec PlistCodec) Decode(r io.Reader) ([]byte, error) {
if r == nil {
return nil, errors.New("Reader was nil")
}
buf := make([]byte, 4)
err := binary.Read(r, binary.BigEndian, buf)
if err != nil {
return nil, err
}
length := binary.BigEndian.Uint32(buf)
payloadBytes := make([]byte, length)
n, err := io.ReadFull(r, payloadBytes)
if err != nil {
return nil, fmt.Errorf("lockdown Payload had incorrect size: %d original error: %s", n, err)
}
return payloadBytes, nil
}