1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package cryptobyte
19
20
21
22 type String []byte
23
24
25
26 func (s *String) read(n int) []byte {
27 if len(*s) < n || n < 0 {
28 return nil
29 }
30 v := (*s)[:n]
31 *s = (*s)[n:]
32 return v
33 }
34
35
36 func (s *String) Skip(n int) bool {
37 return s.read(n) != nil
38 }
39
40
41
42 func (s *String) ReadUint8(out *uint8) bool {
43 v := s.read(1)
44 if v == nil {
45 return false
46 }
47 *out = uint8(v[0])
48 return true
49 }
50
51
52
53 func (s *String) ReadUint16(out *uint16) bool {
54 v := s.read(2)
55 if v == nil {
56 return false
57 }
58 *out = uint16(v[0])<<8 | uint16(v[1])
59 return true
60 }
61
62
63
64 func (s *String) ReadUint24(out *uint32) bool {
65 v := s.read(3)
66 if v == nil {
67 return false
68 }
69 *out = uint32(v[0])<<16 | uint32(v[1])<<8 | uint32(v[2])
70 return true
71 }
72
73
74
75 func (s *String) ReadUint32(out *uint32) bool {
76 v := s.read(4)
77 if v == nil {
78 return false
79 }
80 *out = uint32(v[0])<<24 | uint32(v[1])<<16 | uint32(v[2])<<8 | uint32(v[3])
81 return true
82 }
83
84
85
86 func (s *String) ReadUint48(out *uint64) bool {
87 v := s.read(6)
88 if v == nil {
89 return false
90 }
91 *out = uint64(v[0])<<40 | uint64(v[1])<<32 | uint64(v[2])<<24 | uint64(v[3])<<16 | uint64(v[4])<<8 | uint64(v[5])
92 return true
93 }
94
95
96
97 func (s *String) ReadUint64(out *uint64) bool {
98 v := s.read(8)
99 if v == nil {
100 return false
101 }
102 *out = uint64(v[0])<<56 | uint64(v[1])<<48 | uint64(v[2])<<40 | uint64(v[3])<<32 | uint64(v[4])<<24 | uint64(v[5])<<16 | uint64(v[6])<<8 | uint64(v[7])
103 return true
104 }
105
106 func (s *String) readUnsigned(out *uint32, length int) bool {
107 v := s.read(length)
108 if v == nil {
109 return false
110 }
111 var result uint32
112 for i := 0; i < length; i++ {
113 result <<= 8
114 result |= uint32(v[i])
115 }
116 *out = result
117 return true
118 }
119
120 func (s *String) readLengthPrefixed(lenLen int, outChild *String) bool {
121 lenBytes := s.read(lenLen)
122 if lenBytes == nil {
123 return false
124 }
125 var length uint32
126 for _, b := range lenBytes {
127 length = length << 8
128 length = length | uint32(b)
129 }
130 v := s.read(int(length))
131 if v == nil {
132 return false
133 }
134 *outChild = v
135 return true
136 }
137
138
139
140 func (s *String) ReadUint8LengthPrefixed(out *String) bool {
141 return s.readLengthPrefixed(1, out)
142 }
143
144
145
146
147 func (s *String) ReadUint16LengthPrefixed(out *String) bool {
148 return s.readLengthPrefixed(2, out)
149 }
150
151
152
153
154 func (s *String) ReadUint24LengthPrefixed(out *String) bool {
155 return s.readLengthPrefixed(3, out)
156 }
157
158
159
160 func (s *String) ReadBytes(out *[]byte, n int) bool {
161 v := s.read(n)
162 if v == nil {
163 return false
164 }
165 *out = v
166 return true
167 }
168
169
170
171 func (s *String) CopyBytes(out []byte) bool {
172 n := len(out)
173 v := s.read(n)
174 if v == nil {
175 return false
176 }
177 return copy(out, v) == n
178 }
179
180
181 func (s String) Empty() bool {
182 return len(s) == 0
183 }
184
View as plain text