-
Notifications
You must be signed in to change notification settings - Fork 35
/
encoding.go
61 lines (49 loc) · 1.19 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
package encodings
import (
"encoding/binary"
"errors"
"github.com/vprix/vncproxy/internal/dbuffer"
"github.com/vprix/vncproxy/rfb"
"io"
)
func ReadUint8(r io.Reader) (uint8, error) {
var myUint uint8
if err := binary.Read(r, binary.BigEndian, &myUint); err != nil {
return 0, err
}
return myUint, nil
}
func ReadUint16(r io.Reader) (uint16, error) {
var myUint uint16
if err := binary.Read(r, binary.BigEndian, &myUint); err != nil {
return 0, err
}
return myUint, nil
}
func ReadUint32(r io.Reader) (uint32, error) {
var myUint uint32
if err := binary.Read(r, binary.BigEndian, &myUint); err != nil {
return 0, err
}
return myUint, nil
}
func ReadBytes(count int, r io.Reader) ([]byte, error) {
buff := dbuffer.GetByteBuffer()
defer dbuffer.ReleaseByteBuffer(buff)
buff.ChangeLen(count)
lengthRead, err := io.ReadFull(r, buff.B)
if lengthRead != count {
return nil, errors.New("ReadBytes unable to read bytes")
}
if err != nil {
return nil, err
}
return buff.Bytes(), nil
}
func ReadPixel(c io.Reader, pf *rfb.PixelFormat) ([]byte, error) {
px := make([]byte, int(pf.BPP/8))
if err := binary.Read(c, pf.Order(), &px); err != nil {
return nil, err
}
return px, nil
}