-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoding.go
156 lines (145 loc) · 3.68 KB
/
encoding.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
// Copyright © 2017-2018 The IPFN Developers. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cells
import (
"encoding/hex"
"encoding/json"
"fmt"
"math"
"github.com/gogo/protobuf/proto"
)
// ToHex - Encodes binary cell to hex.
func ToHex(cell Cell) (body []byte, err error) {
src, err := Marshal(cell)
if err != nil {
return
}
body = make([]byte, hex.EncodedLen(len(src)))
hex.Encode(body, src)
return
}
// FromHex - Decodes cell from hex encoded binary cell.
func FromHex(src []byte) (_ Cell, err error) {
n, err := hex.Decode(src, src)
if err != nil {
return
}
cell := new(BinaryCell)
err = Unmarshal(cell, src[:n])
if err != nil {
return
}
return cell, nil
}
// FromHexString - Decodes cell from hex encoded binary cell.
func FromHexString(src string) (_ Cell, err error) {
return FromHex([]byte(src))
}
// MarshalJSON - Marshals cell as JSON.
func MarshalJSON(cell Cell) (_ []byte, err error) {
type jsonCell struct {
OpCode ID `json:"op,omitempty"`
Memory []byte `json:"value,omitempty"`
Children []*CellPrinter `json:"ops,omitempty"`
}
childSize := cell.ChildrenSize()
children := make([]*CellPrinter, childSize)
for index := 0; index < childSize; index++ {
children[index] = NewPrinter(cell.Child(index))
}
return json.Marshal(jsonCell{
OpCode: cell.OpCode(),
Memory: cell.Memory(),
Children: children,
})
}
// Marshal - Marshals cell as byte array.
func Marshal(cell Cell) (body []byte, err error) {
switch v := cell.(type) {
case *BinaryCell:
if v.body != nil {
return v.body, nil
}
}
buff := proto.NewBuffer(nil)
err = marshal(cell, buff)
if err != nil {
return
}
body = buff.Bytes()
switch v := cell.(type) {
case *BinaryCell:
v.body = body
}
return
}
// Unmarshal - Unmarshals cell from byte array.
func Unmarshal(cell MutableCell, body []byte) error {
switch v := cell.(type) {
case *BinaryCell:
v.body = body
}
return unmarshal(cell, proto.NewBuffer(body))
}
func unmarshal(cell MutableCell, buff *proto.Buffer) (err error) {
opCode, err := buff.DecodeVarint()
if err != nil {
return err
}
cell.SetOpCode(ID(opCode))
memory, err := buff.DecodeRawBytes(false)
if err != nil {
return err
}
cell.SetMemory(memory)
childSize, err := buff.DecodeVarint()
if err != nil {
return err
}
if childSize >= math.MaxInt32 {
return fmt.Errorf("children length too big %d", childSize)
}
children := make([]Cell, childSize)
for index := 0; index < int(childSize); index++ {
child := new(BinaryCell)
if err := unmarshal(child, buff); err != nil {
return err
}
children[index] = child
}
cell.SetChildren(children)
return
}
func marshal(cell Cell, buff *proto.Buffer) (err error) {
// if cell.body != nil {
// buff.SetBuf(append(buff.Bytes(), cell.body...))
// return
// }
if err := buff.EncodeVarint(uint64(cell.OpCode())); err != nil {
return err
}
if err := buff.EncodeRawBytes(cell.Memory()); err != nil {
return err
}
children := cell.ChildrenSize()
if err := buff.EncodeVarint(uint64(children)); err != nil {
return err
}
for index := 0; index < children; index++ {
if err := marshal(cell.Child(index), buff); err != nil {
return err
}
}
return
}