test: Fix test dependancy
[govpp.git] / codec / codec.go
1 //  Copyright (c) 2020 Cisco and/or its affiliates.
2 //
3 //  Licensed under the Apache License, Version 2.0 (the "License");
4 //  you may not use this file except in compliance with the License.
5 //  You may obtain a copy of the License at:
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 //  Unless required by applicable law or agreed to in writing, software
10 //  distributed under the License is distributed on an "AS IS" BASIS,
11 //  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 //  See the License for the specific language governing permissions and
13 //  limitations under the License.
14
15 package codec
16
17 import (
18         "bytes"
19         "encoding/binary"
20         "math"
21 )
22
23 // Buffer provides buffer for encoding and decoding data on wire.
24 type Buffer struct {
25         buf []byte
26         pos int
27 }
28
29 // NewBuffer creates new buffer using b as data.
30 func NewBuffer(b []byte) *Buffer {
31         return &Buffer{
32                 buf: b,
33         }
34 }
35
36 // Bytes returns buffer data up to current position.
37 func (b *Buffer) Bytes() []byte {
38         return b.buf[:b.pos]
39 }
40
41 func (b *Buffer) EncodeBytes(v []byte, length int) {
42         if length == 0 {
43                 length = len(v)
44         }
45         copy(b.buf[b.pos:b.pos+length], v)
46         b.pos += length
47 }
48
49 func (b *Buffer) DecodeBytes(length int) []byte {
50         v := b.buf[b.pos : b.pos+length]
51         b.pos += length
52         return v
53 }
54
55 func (b *Buffer) EncodeBool(v bool) {
56         if v {
57                 b.buf[b.pos] = 1
58         } else {
59                 b.buf[b.pos] = 0
60         }
61         b.pos += 1
62 }
63
64 func (b *Buffer) DecodeBool() bool {
65         v := b.buf[b.pos] != 0
66         b.pos += 1
67         return v
68 }
69
70 func (b *Buffer) EncodeUint8(v uint8) {
71         b.buf[b.pos] = byte(v)
72         b.pos += 1
73 }
74
75 func (b *Buffer) DecodeUint8() uint8 {
76         v := uint8(b.buf[b.pos])
77         b.pos += 1
78         return v
79 }
80
81 func (b *Buffer) EncodeUint16(v uint16) {
82         binary.BigEndian.PutUint16(b.buf[b.pos:b.pos+2], v)
83         b.pos += 2
84 }
85
86 func (b *Buffer) DecodeUint16() uint16 {
87         v := binary.BigEndian.Uint16(b.buf[b.pos : b.pos+2])
88         b.pos += 2
89         return v
90 }
91
92 func (b *Buffer) EncodeUint32(v uint32) {
93         binary.BigEndian.PutUint32(b.buf[b.pos:b.pos+4], v)
94         b.pos += 4
95 }
96
97 func (b *Buffer) DecodeUint32() uint32 {
98         v := binary.BigEndian.Uint32(b.buf[b.pos : b.pos+4])
99         b.pos += 4
100         return v
101 }
102
103 func (b *Buffer) EncodeUint64(v uint64) {
104         binary.BigEndian.PutUint64(b.buf[b.pos:b.pos+8], v)
105         b.pos += 8
106 }
107
108 func (b *Buffer) DecodeUint64() uint64 {
109         v := binary.BigEndian.Uint64(b.buf[b.pos : b.pos+8])
110         b.pos += 8
111         return v
112 }
113
114 func (b *Buffer) EncodeInt8(v int8) {
115         b.buf[b.pos] = byte(v)
116         b.pos += 1
117 }
118
119 func (b *Buffer) DecodeInt8() int8 {
120         v := int8(b.buf[b.pos])
121         b.pos += 1
122         return v
123 }
124
125 func (b *Buffer) EncodeInt16(v int16) {
126         binary.BigEndian.PutUint16(b.buf[b.pos:b.pos+2], uint16(v))
127         b.pos += 2
128 }
129
130 func (b *Buffer) DecodeInt16() int16 {
131         v := int16(binary.BigEndian.Uint16(b.buf[b.pos : b.pos+2]))
132         b.pos += 2
133         return v
134 }
135
136 func (b *Buffer) EncodeInt32(v int32) {
137         binary.BigEndian.PutUint32(b.buf[b.pos:b.pos+4], uint32(v))
138         b.pos += 4
139 }
140
141 func (b *Buffer) DecodeInt32() int32 {
142         v := int32(binary.BigEndian.Uint32(b.buf[b.pos : b.pos+4]))
143         b.pos += 4
144         return v
145 }
146
147 func (b *Buffer) EncodeInt64(v int64) {
148         binary.BigEndian.PutUint64(b.buf[b.pos:b.pos+8], uint64(v))
149         b.pos += 8
150 }
151
152 func (b *Buffer) DecodeInt64() int64 {
153         v := int64(binary.BigEndian.Uint64(b.buf[b.pos : b.pos+8]))
154         b.pos += 8
155         return v
156 }
157
158 func (b *Buffer) EncodeFloat64(v float64) {
159         binary.LittleEndian.PutUint64(b.buf[b.pos:b.pos+8], math.Float64bits(v))
160         b.pos += 8
161 }
162
163 func (b *Buffer) DecodeFloat64() float64 {
164         v := math.Float64frombits(binary.LittleEndian.Uint64(b.buf[b.pos : b.pos+8]))
165         b.pos += 8
166         return v
167 }
168
169 func (b *Buffer) EncodeString(v string, length int) {
170         if length == 0 {
171                 b.EncodeUint32(uint32(len(v)))
172                 length = len(v)
173         }
174         copy(b.buf[b.pos:b.pos+length], v)
175         b.pos += length
176 }
177
178 func (b *Buffer) DecodeString(length int) string {
179         var v []byte
180         if length == 0 {
181                 length = int(b.DecodeUint32())
182                 v = b.buf[b.pos : b.pos+length]
183         } else {
184                 v = b.buf[b.pos : b.pos+length]
185                 if nul := bytes.Index(v, []byte{0x00}); nul >= 0 {
186                         v = v[:nul]
187                 }
188         }
189         b.pos += length
190         return string(v)
191 }