summaryrefslogtreecommitdiffhomepage
path: root/pkg/p9/buffer.go
blob: 249536d8a4ca3fc563a6ee50cded764c5539cc29 (plain)
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
// Copyright 2018 The gVisor 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.

package p9

import (
	"encoding/binary"
)

// encoder is used for messages and 9P primitives.
type encoder interface {
	// Decode decodes from the given buffer. Decode may be called more than once
	// to reuse the instance. It must clear any previous state.
	//
	// This may not fail, exhaustion will be recorded in the buffer.
	Decode(b *buffer)

	// Encode encodes to the given buffer.
	//
	// This may not fail.
	Encode(b *buffer)
}

// order is the byte order used for encoding.
var order = binary.LittleEndian

// buffer is a slice that is consumed.
//
// This is passed to the encoder methods.
type buffer struct {
	// data is the underlying data. This may grow during Encode.
	data []byte

	// overflow indicates whether an overflow has occurred.
	overflow bool
}

// append appends n bytes to the buffer and returns a slice pointing to the
// newly appended bytes.
func (b *buffer) append(n int) []byte {
	b.data = append(b.data, make([]byte, n)...)
	return b.data[len(b.data)-n:]
}

// consume consumes n bytes from the buffer.
func (b *buffer) consume(n int) ([]byte, bool) {
	if !b.has(n) {
		b.markOverrun()
		return nil, false
	}
	rval := b.data[:n]
	b.data = b.data[n:]
	return rval, true
}

// has returns true if n bytes are available.
func (b *buffer) has(n int) bool {
	return len(b.data) >= n
}

// markOverrun immediately marks this buffer as overrun.
//
// This is used by ReadString, since some invalid data implies the rest of the
// buffer is no longer valid either.
func (b *buffer) markOverrun() {
	b.overflow = true
}

// isOverrun returns true if this buffer has run past the end.
func (b *buffer) isOverrun() bool {
	return b.overflow
}

// Read8 reads a byte from the buffer.
func (b *buffer) Read8() uint8 {
	v, ok := b.consume(1)
	if !ok {
		return 0
	}
	return uint8(v[0])
}

// Read16 reads a 16-bit value from the buffer.
func (b *buffer) Read16() uint16 {
	v, ok := b.consume(2)
	if !ok {
		return 0
	}
	return order.Uint16(v)
}

// Read32 reads a 32-bit value from the buffer.
func (b *buffer) Read32() uint32 {
	v, ok := b.consume(4)
	if !ok {
		return 0
	}
	return order.Uint32(v)
}

// Read64 reads a 64-bit value from the buffer.
func (b *buffer) Read64() uint64 {
	v, ok := b.consume(8)
	if !ok {
		return 0
	}
	return order.Uint64(v)
}

// ReadQIDType reads a QIDType value.
func (b *buffer) ReadQIDType() QIDType {
	return QIDType(b.Read8())
}

// ReadTag reads a Tag value.
func (b *buffer) ReadTag() Tag {
	return Tag(b.Read16())
}

// ReadFID reads a FID value.
func (b *buffer) ReadFID() FID {
	return FID(b.Read32())
}

// ReadUID reads a UID value.
func (b *buffer) ReadUID() UID {
	return UID(b.Read32())
}

// ReadGID reads a GID value.
func (b *buffer) ReadGID() GID {
	return GID(b.Read32())
}

// ReadPermissions reads a file mode value and applies the mask for permissions.
func (b *buffer) ReadPermissions() FileMode {
	return b.ReadFileMode() & permissionsMask
}

// ReadFileMode reads a file mode value.
func (b *buffer) ReadFileMode() FileMode {
	return FileMode(b.Read32())
}

// ReadOpenFlags reads an OpenFlags.
func (b *buffer) ReadOpenFlags() OpenFlags {
	return OpenFlags(b.Read32())
}

// ReadConnectFlags reads a ConnectFlags.
func (b *buffer) ReadConnectFlags() ConnectFlags {
	return ConnectFlags(b.Read32())
}

// ReadMsgType writes a MsgType.
func (b *buffer) ReadMsgType() MsgType {
	return MsgType(b.Read8())
}

// ReadString deserializes a string.
func (b *buffer) ReadString() string {
	l := b.Read16()
	if !b.has(int(l)) {
		// Mark the buffer as corrupted.
		b.markOverrun()
		return ""
	}

	bs := make([]byte, l)
	for i := 0; i < int(l); i++ {
		bs[i] = byte(b.Read8())
	}
	return string(bs)
}

// Write8 writes a byte to the buffer.
func (b *buffer) Write8(v uint8) {
	b.append(1)[0] = byte(v)
}

// Write16 writes a 16-bit value to the buffer.
func (b *buffer) Write16(v uint16) {
	order.PutUint16(b.append(2), v)
}

// Write32 writes a 32-bit value to the buffer.
func (b *buffer) Write32(v uint32) {
	order.PutUint32(b.append(4), v)
}

// Write64 writes a 64-bit value to the buffer.
func (b *buffer) Write64(v uint64) {
	order.PutUint64(b.append(8), v)
}

// WriteQIDType writes a QIDType value.
func (b *buffer) WriteQIDType(qidType QIDType) {
	b.Write8(uint8(qidType))
}

// WriteTag writes a Tag value.
func (b *buffer) WriteTag(tag Tag) {
	b.Write16(uint16(tag))
}

// WriteFID writes a FID value.
func (b *buffer) WriteFID(fid FID) {
	b.Write32(uint32(fid))
}

// WriteUID writes a UID value.
func (b *buffer) WriteUID(uid UID) {
	b.Write32(uint32(uid))
}

// WriteGID writes a GID value.
func (b *buffer) WriteGID(gid GID) {
	b.Write32(uint32(gid))
}

// WritePermissions applies a permissions mask and writes the FileMode.
func (b *buffer) WritePermissions(perm FileMode) {
	b.WriteFileMode(perm & permissionsMask)
}

// WriteFileMode writes a FileMode.
func (b *buffer) WriteFileMode(mode FileMode) {
	b.Write32(uint32(mode))
}

// WriteOpenFlags writes an OpenFlags.
func (b *buffer) WriteOpenFlags(flags OpenFlags) {
	b.Write32(uint32(flags))
}

// WriteConnectFlags writes a ConnectFlags.
func (b *buffer) WriteConnectFlags(flags ConnectFlags) {
	b.Write32(uint32(flags))
}

// WriteMsgType writes a MsgType.
func (b *buffer) WriteMsgType(t MsgType) {
	b.Write8(uint8(t))
}

// WriteString serializes the given string.
func (b *buffer) WriteString(s string) {
	b.Write16(uint16(len(s)))
	for i := 0; i < len(s); i++ {
		b.Write8(byte(s[i]))
	}
}