-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.go
49 lines (40 loc) · 1.22 KB
/
utils.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
// Package qpack provides fast data (de)serializing.
// Maintainer: Jeroen van der Heijden / Transceptor Technology
package qpack
import (
"encoding/binary"
"math"
)
func int16FromBytes(b []byte) int16 {
return int16(b[0]) + int16(b[1])<<8
}
func int16toBytes(i int16) []byte {
return []byte{uint8(i & 0xff), uint8(i >> 8)}
}
func int32FromBytes(b []byte) int32 {
return int32(b[0]) + int32(b[1])<<8 + int32(b[2])<<16 + int32(b[3])<<24
}
func int32toBytes(i int32) []byte {
return []byte{
uint8(i & 0xff), uint8(i >> 8), uint8(i >> 16), uint8(i >> 24)}
}
func int64FromBytes(b []byte) int64 {
return int64(b[0]) + int64(b[1])<<8 + int64(b[2])<<16 + int64(b[3])<<24 +
int64(b[4])<<32 + int64(b[5])<<40 + int64(b[6])<<48 + int64(b[7])<<56
}
func int64toBytes(i int64) []byte {
return []byte{
uint8(i & 0xff), uint8(i >> 8), uint8(i >> 16), uint8(i >> 24),
uint8(i >> 32), uint8(i >> 40), uint8(i >> 48), uint8(i >> 56)}
}
func float64FromBytes(bytes []byte) float64 {
bits := binary.LittleEndian.Uint64(bytes)
float := math.Float64frombits(bits)
return float
}
func float64toBytes(float float64) []byte {
bits := math.Float64bits(float)
bytes := make([]byte, 8)
binary.LittleEndian.PutUint64(bytes, bits)
return bytes
}