~rbn/neinp

ref: 062a107b140085acd6c48867e4328fb3f3466a87 neinp/basic/basic.go -rw-r--r-- 4.1 KiB
062a107bRuben Schuller use custom go.rbn.im import path 1 year, 2 months ago
                                                                                
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
157
158
159
160
//Package basic handles en-/decoding of basic types to 9p wire format.
package basic // import "go.rbn.im/neinp/basic"

import (
	"io"
)

//Uint8Decode reads a single byte.
//
//Returns the decoded byte, the count of read bytes and a possible error.
func Uint8Decode(r io.Reader) (uint8, int64, error) {
	buf := make([]byte, 1)
	n, err := r.Read(buf)
	if n < 1 || err != nil {
		return 0, int64(n), err
	}
	return uint8(buf[0]), int64(n), nil
}

//Uint8Encode writes a single byte.
//
//Returns the count of written bytes and a possible error.
func Uint8Encode(w io.Writer, x uint8) (int64, error) {
	buf := []byte{x}
	n, err := w.Write(buf)
	if n < 1 || err != nil {
		return int64(n), err
	}
	return int64(n), nil
}

//Uint16Decode reads a 16 bit value from an io.Reader.
//
//Returns the decoded uint16 value, the count of read bytes and a possible error.
func Uint16Decode(r io.Reader) (uint16, int64, error) {
	buf := make([]byte, 2)
	n, err := r.Read(buf)
	if n < 2 || err != nil {
		return 0, int64(n), err
	}
	x := uint16(buf[0]) | uint16(buf[1])<<8
	return x, int64(n), nil
}

//Uint16Encode writes a uint16 value.
//
//Returns the count of written bytes and a possible error.
func Uint16Encode(w io.Writer, x uint16) (int64, error) {
	buf := []byte{uint8(x), uint8(x >> 8)}
	n, err := w.Write(buf)
	if n < 2 || err != nil {
		return int64(n), err
	}
	return int64(n), nil
}

//Uint32Decode reads a 32 bit value from an io.Reader.
//
//Returns the decoded uint32 value, the count of read bytes and a possible error.
func Uint32Decode(r io.Reader) (uint32, int64, error) {
	buf := make([]byte, 4)
	n, err := r.Read(buf)
	if n < 4 || err != nil {
		return 0, int64(n), err
	}
	x := uint32(buf[0]) | uint32(buf[1])<<8 | uint32(buf[2])<<16 | uint32(buf[3])<<24
	return x, int64(n), nil
}

//Uint32Encode writes a uint32 value.
//
//Returns the count of written bytes and a possible error.
func Uint32Encode(w io.Writer, x uint32) (int64, error) {
	buf := []byte{uint8(x), uint8(x >> 8), uint8(x >> 16), uint8(x >> 24)}
	n, err := w.Write(buf)
	if n < 4 || err != nil {
		return int64(n), err
	}
	return int64(n), nil
}

//Uint64Decode reads a 64 bit value from an io.Reader.
//
//Returns the decoded uint64 value, the count of read bytes and a possible error.
func Uint64Decode(r io.Reader) (uint64, int64, error) {
	buf := make([]byte, 8)
	n, err := r.Read(buf)
	if n < 8 || err != nil {
		return 0, int64(n), err
	}
	x := uint64(buf[0]) | uint64(buf[1])<<8 | uint64(buf[2])<<16 | uint64(buf[3])<<24 | uint64(buf[4])<<32 | uint64(buf[5])<<40 | uint64(buf[6])<<48 | uint64(buf[7])<<56
	return x, int64(n), nil
}

//Uint64Encode writes a uint64 value.
//
//Returns the count of written bytes and a possible error.
func Uint64Encode(w io.Writer, x uint64) (int64, error) {
	buf := []byte{uint8(x), uint8(x >> 8), uint8(x >> 16), uint8(x >> 24), uint8(x >> 32), uint8(x >> 40), uint8(x >> 48), uint8(x >> 56)}
	n, err := w.Write(buf)
	if n < 8 || err != nil {
		return int64(n), err
	}
	return int64(n), nil
}

//BytesDecode reads a variable number of bytes into a byte slice.
//
//Returns a byte slice, the count of read bytes and a possible error.
func BytesDecode(r io.Reader) ([]byte, int64, error) {
	size, n1, err := Uint16Decode(r)
	if err != nil {
		return []byte{}, n1, err
	}

	buf := make([]byte, size)
	n2, err := r.Read(buf)
	if err != nil {
		return []byte{}, n1 + int64(n2), err
	}

	return buf, n1 + int64(n2), nil
}

//BytesEncode writes a byte slice.
//
//Returns the count of written bytes and a possible error.
func BytesEncode(w io.Writer, b []byte) (int64, error) {
	size := uint16(len(b))
	n1, err := Uint16Encode(w, size)
	if err != nil {
		return n1, err
	}

	n2, err := w.Write(b)
	if err != nil {
		return n1 + int64(n2), err
	}

	return n1 + int64(n2), nil
}

//StringDecode reads a string.
//
//Returns a string, the count of read bytes and a possible error.
func StringDecode(r io.Reader) (string, int64, error) {
	b, n1, err := BytesDecode(r)
	if err != nil {
		return "", n1, err
	}

	return string(b), n1, nil
}

//StringEncode writes a string.
//
//Returns the count of written bytes and a possible error.
func StringEncode(w io.Writer, s string) (int64, error) {
	return BytesEncode(w, []byte(s))
}