-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathbytes.go
109 lines (83 loc) · 3.07 KB
/
bytes.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
/*
Copyright 2016 The Smudge Authors.
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.
*/
// This file contains a variety of methods to encode/decode various primitive
// data types into a byte slice using a fixed length encoding scheme. We
// chose fixed lengths (as opposed to the variable length encoding provided by
// the encoding/binary package) because it makes decoding easier. It's also
// (very slightly) more efficient.
//
// These functions are used mostly by the components defined in message.go.
package smudge
func decodeByte(bytes []byte, startIndex int) (byte, int) {
return bytes[startIndex], startIndex + 1
}
func decodeUint8(bytes []byte, startIndex int) (uint8, int) {
n, i := decodeByte(bytes, startIndex)
return byte(n), i
}
func decodeUint16(bytes []byte, startIndex int) (uint16, int) {
var number uint16
number = uint16(bytes[startIndex+1])<<8 |
uint16(bytes[startIndex+0])
return number, startIndex + 2
}
func decodeUint32(bytes []byte, startIndex int) (uint32, int) {
var number uint32
number = uint32(bytes[startIndex+3])<<24 |
uint32(bytes[startIndex+2])<<16 |
uint32(bytes[startIndex+1])<<8 |
uint32(bytes[startIndex+0])
return number, startIndex + 4
}
func decodeUint64(bytes []byte, startIndex int) (uint64, int) {
var number uint64
number = uint64(bytes[startIndex+7])<<56 |
uint64(bytes[startIndex+6])<<48 |
uint64(bytes[startIndex+5])<<40 |
uint64(bytes[startIndex+4])<<32 |
uint64(bytes[startIndex+3])<<24 |
uint64(bytes[startIndex+2])<<16 |
uint64(bytes[startIndex+1])<<8 |
uint64(bytes[startIndex+0])
return number, startIndex + 8
}
func encodeByte(number byte, bytes []byte, startIndex int) int {
bytes[startIndex+0] = number
return 1
}
func encodeUint8(number uint8, bytes []byte, startIndex int) int {
return encodeByte(byte(number), bytes, startIndex)
}
func encodeUint16(number uint16, bytes []byte, startIndex int) int {
bytes[startIndex+0] = byte(number)
bytes[startIndex+1] = byte(number >> 8)
return 2
}
func encodeUint32(number uint32, bytes []byte, startIndex int) int {
bytes[startIndex+0] = byte(number)
bytes[startIndex+1] = byte(number >> 8)
bytes[startIndex+2] = byte(number >> 16)
bytes[startIndex+3] = byte(number >> 24)
return 4
}
func encodeUint64(number uint64, bytes []byte, startIndex int) int {
bytes[startIndex+0] = byte(number)
bytes[startIndex+1] = byte(number >> 8)
bytes[startIndex+2] = byte(number >> 16)
bytes[startIndex+3] = byte(number >> 24)
bytes[startIndex+4] = byte(number >> 32)
bytes[startIndex+5] = byte(number >> 40)
bytes[startIndex+6] = byte(number >> 48)
bytes[startIndex+7] = byte(number >> 56)
return 8
}