github.com/sagernet/gvisor@v0.0.0-20240428053021-e691de28565f/pkg/p9/buffer.go (about) 1 // Copyright 2018 The gVisor Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package p9 16 17 import ( 18 "encoding/binary" 19 ) 20 21 // encoder is used for messages and 9P primitives. 22 type encoder interface { 23 // decode decodes from the given buffer. decode may be called more than once 24 // to reuse the instance. It must clear any previous state. 25 // 26 // This may not fail, exhaustion will be recorded in the buffer. 27 decode(b *buffer) 28 29 // encode encodes to the given buffer. 30 // 31 // This may not fail. 32 encode(b *buffer) 33 } 34 35 // order is the byte order used for encoding. 36 var order = binary.LittleEndian 37 38 // buffer is a slice that is consumed. 39 // 40 // This is passed to the encoder methods. 41 type buffer struct { 42 // data is the underlying data. This may grow during encode. 43 data []byte 44 45 // overflow indicates whether an overflow has occurred. 46 overflow bool 47 } 48 49 // append appends n bytes to the buffer and returns a slice pointing to the 50 // newly appended bytes. 51 func (b *buffer) append(n int) []byte { 52 b.data = append(b.data, make([]byte, n)...) 53 return b.data[len(b.data)-n:] 54 } 55 56 // consume consumes n bytes from the buffer. 57 func (b *buffer) consume(n int) ([]byte, bool) { 58 if !b.has(n) { 59 b.markOverrun() 60 return nil, false 61 } 62 rval := b.data[:n] 63 b.data = b.data[n:] 64 return rval, true 65 } 66 67 // has returns true if n bytes are available. 68 func (b *buffer) has(n int) bool { 69 return len(b.data) >= n 70 } 71 72 // markOverrun immediately marks this buffer as overrun. 73 // 74 // This is used by ReadString, since some invalid data implies the rest of the 75 // buffer is no longer valid either. 76 func (b *buffer) markOverrun() { 77 b.overflow = true 78 } 79 80 // isOverrun returns true if this buffer has run past the end. 81 func (b *buffer) isOverrun() bool { 82 return b.overflow 83 } 84 85 // Read8 reads a byte from the buffer. 86 func (b *buffer) Read8() uint8 { 87 v, ok := b.consume(1) 88 if !ok { 89 return 0 90 } 91 return uint8(v[0]) 92 } 93 94 // Read16 reads a 16-bit value from the buffer. 95 func (b *buffer) Read16() uint16 { 96 v, ok := b.consume(2) 97 if !ok { 98 return 0 99 } 100 return order.Uint16(v) 101 } 102 103 // Read32 reads a 32-bit value from the buffer. 104 func (b *buffer) Read32() uint32 { 105 v, ok := b.consume(4) 106 if !ok { 107 return 0 108 } 109 return order.Uint32(v) 110 } 111 112 // Read64 reads a 64-bit value from the buffer. 113 func (b *buffer) Read64() uint64 { 114 v, ok := b.consume(8) 115 if !ok { 116 return 0 117 } 118 return order.Uint64(v) 119 } 120 121 // ReadQIDType reads a QIDType value. 122 func (b *buffer) ReadQIDType() QIDType { 123 return QIDType(b.Read8()) 124 } 125 126 // ReadTag reads a Tag value. 127 func (b *buffer) ReadTag() Tag { 128 return Tag(b.Read16()) 129 } 130 131 // ReadFID reads a FID value. 132 func (b *buffer) ReadFID() FID { 133 return FID(b.Read32()) 134 } 135 136 // ReadUID reads a UID value. 137 func (b *buffer) ReadUID() UID { 138 return UID(b.Read32()) 139 } 140 141 // ReadGID reads a GID value. 142 func (b *buffer) ReadGID() GID { 143 return GID(b.Read32()) 144 } 145 146 // ReadPermissions reads a file mode value and applies the mask for permissions. 147 func (b *buffer) ReadPermissions() FileMode { 148 return b.ReadFileMode() & permissionsMask 149 } 150 151 // ReadFileMode reads a file mode value. 152 func (b *buffer) ReadFileMode() FileMode { 153 return FileMode(b.Read32()) 154 } 155 156 // ReadOpenFlags reads an OpenFlags. 157 func (b *buffer) ReadOpenFlags() OpenFlags { 158 return OpenFlags(b.Read32()) 159 } 160 161 // ReadSocketType reads a SocketType. 162 func (b *buffer) ReadSocketType() SocketType { 163 return SocketType(b.Read32()) 164 } 165 166 // ReadMsgType writes a MsgType. 167 func (b *buffer) ReadMsgType() MsgType { 168 return MsgType(b.Read8()) 169 } 170 171 // ReadString deserializes a string. 172 func (b *buffer) ReadString() string { 173 l := b.Read16() 174 if !b.has(int(l)) { 175 // Mark the buffer as corrupted. 176 b.markOverrun() 177 return "" 178 } 179 180 bs := make([]byte, l) 181 for i := 0; i < int(l); i++ { 182 bs[i] = byte(b.Read8()) 183 } 184 return string(bs) 185 } 186 187 // Write8 writes a byte to the buffer. 188 func (b *buffer) Write8(v uint8) { 189 b.append(1)[0] = byte(v) 190 } 191 192 // Write16 writes a 16-bit value to the buffer. 193 func (b *buffer) Write16(v uint16) { 194 order.PutUint16(b.append(2), v) 195 } 196 197 // Write32 writes a 32-bit value to the buffer. 198 func (b *buffer) Write32(v uint32) { 199 order.PutUint32(b.append(4), v) 200 } 201 202 // Write64 writes a 64-bit value to the buffer. 203 func (b *buffer) Write64(v uint64) { 204 order.PutUint64(b.append(8), v) 205 } 206 207 // WriteQIDType writes a QIDType value. 208 func (b *buffer) WriteQIDType(qidType QIDType) { 209 b.Write8(uint8(qidType)) 210 } 211 212 // WriteTag writes a Tag value. 213 func (b *buffer) WriteTag(tag Tag) { 214 b.Write16(uint16(tag)) 215 } 216 217 // WriteFID writes a FID value. 218 func (b *buffer) WriteFID(fid FID) { 219 b.Write32(uint32(fid)) 220 } 221 222 // WriteUID writes a UID value. 223 func (b *buffer) WriteUID(uid UID) { 224 b.Write32(uint32(uid)) 225 } 226 227 // WriteGID writes a GID value. 228 func (b *buffer) WriteGID(gid GID) { 229 b.Write32(uint32(gid)) 230 } 231 232 // WritePermissions applies a permissions mask and writes the FileMode. 233 func (b *buffer) WritePermissions(perm FileMode) { 234 b.WriteFileMode(perm & permissionsMask) 235 } 236 237 // WriteFileMode writes a FileMode. 238 func (b *buffer) WriteFileMode(mode FileMode) { 239 b.Write32(uint32(mode)) 240 } 241 242 // WriteOpenFlags writes an OpenFlags. 243 func (b *buffer) WriteOpenFlags(flags OpenFlags) { 244 b.Write32(uint32(flags)) 245 } 246 247 // WriteSocketType writes a SocketType. 248 func (b *buffer) WriteSocketType(flags SocketType) { 249 b.Write32(uint32(flags)) 250 } 251 252 // WriteMsgType writes a MsgType. 253 func (b *buffer) WriteMsgType(t MsgType) { 254 b.Write8(uint8(t)) 255 } 256 257 // WriteString serializes the given string. 258 func (b *buffer) WriteString(s string) { 259 b.Write16(uint16(len(s))) 260 for i := 0; i < len(s); i++ { 261 b.Write8(byte(s[i])) 262 } 263 }