github.com/nicocha30/gvisor-ligolo@v0.0.0-20230726075806-989fa2c0a413/pkg/lisafs/sample_message.go (about)

     1  // Copyright 2021 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 lisafs
    16  
    17  import (
    18  	"fmt"
    19  	"math/rand"
    20  
    21  	"github.com/nicocha30/gvisor-ligolo/pkg/marshal/primitive"
    22  )
    23  
    24  // MsgSimple is a sample packed struct which can be used to test message passing.
    25  //
    26  // +marshal slice:Msg1Slice
    27  type MsgSimple struct {
    28  	A uint16
    29  	B uint16
    30  	C uint32
    31  	D uint64
    32  }
    33  
    34  // Randomize randomizes the contents of m.
    35  func (m *MsgSimple) Randomize() {
    36  	m.A = uint16(rand.Uint32())
    37  	m.B = uint16(rand.Uint32())
    38  	m.C = rand.Uint32()
    39  	m.D = rand.Uint64()
    40  }
    41  
    42  // MsgDynamic is a sample dynamic struct which can be used to test message passing.
    43  //
    44  // +marshal dynamic
    45  type MsgDynamic struct {
    46  	N   primitive.Uint32
    47  	Arr []MsgSimple
    48  }
    49  
    50  // String implements fmt.Stringer.String.
    51  func (m *MsgDynamic) String() string {
    52  	return fmt.Sprintf("MsgDynamic{N: %d, Arr: %v}", m.N, m.Arr)
    53  }
    54  
    55  // SizeBytes implements marshal.Marshallable.SizeBytes.
    56  func (m *MsgDynamic) SizeBytes() int {
    57  	return m.N.SizeBytes() +
    58  		(int(m.N) * (*MsgSimple)(nil).SizeBytes())
    59  }
    60  
    61  // MarshalBytes implements marshal.Marshallable.MarshalBytes.
    62  func (m *MsgDynamic) MarshalBytes(dst []byte) []byte {
    63  	dst = m.N.MarshalUnsafe(dst)
    64  	return MarshalUnsafeMsg1Slice(m.Arr, dst)
    65  }
    66  
    67  // UnmarshalBytes implements marshal.Marshallable.UnmarshalBytes.
    68  func (m *MsgDynamic) UnmarshalBytes(src []byte) []byte {
    69  	src = m.N.UnmarshalUnsafe(src)
    70  	m.Arr = make([]MsgSimple, m.N)
    71  	return UnmarshalUnsafeMsg1Slice(m.Arr, src)
    72  }
    73  
    74  // CheckedUnmarshal implements marshal.CheckedMarshallable.CheckedUnmarshal.
    75  func (m *MsgDynamic) CheckedUnmarshal(src []byte) ([]byte, bool) {
    76  	m.Arr = m.Arr[:0]
    77  	if m.SizeBytes() > len(src) {
    78  		return nil, false
    79  	}
    80  	src = m.N.UnmarshalUnsafe(src)
    81  	if int(m.N) > cap(m.Arr) {
    82  		m.Arr = make([]MsgSimple, m.N)
    83  	} else {
    84  		m.Arr = m.Arr[:m.N]
    85  	}
    86  	if int(m.N)*(*MsgSimple)(nil).SizeBytes() > len(src) {
    87  		return nil, false
    88  	}
    89  	return UnmarshalUnsafeMsg1Slice(m.Arr, src), true
    90  }
    91  
    92  // Randomize randomizes the contents of m.
    93  func (m *MsgDynamic) Randomize(arrLen int) {
    94  	m.N = primitive.Uint32(arrLen)
    95  	m.Arr = make([]MsgSimple, arrLen)
    96  	for i := 0; i < arrLen; i++ {
    97  		m.Arr[i].Randomize()
    98  	}
    99  }
   100  
   101  // P9Version mimics p9.TVersion and p9.Rversion.
   102  type P9Version struct {
   103  	MSize   primitive.Uint32
   104  	Version string
   105  }
   106  
   107  // String implements fmt.Stringer.String.
   108  func (v *P9Version) String() string {
   109  	return fmt.Sprintf("P9Version{MSize: %d, Version: %s}", v.MSize, v.Version)
   110  }
   111  
   112  // SizeBytes implements marshal.Marshallable.SizeBytes.
   113  func (v *P9Version) SizeBytes() int {
   114  	return (*primitive.Uint32)(nil).SizeBytes() + (*primitive.Uint16)(nil).SizeBytes() + len(v.Version)
   115  }
   116  
   117  // MarshalBytes implements marshal.Marshallable.MarshalBytes.
   118  func (v *P9Version) MarshalBytes(dst []byte) []byte {
   119  	dst = v.MSize.MarshalUnsafe(dst)
   120  	versionLen := primitive.Uint16(len(v.Version))
   121  	dst = versionLen.MarshalUnsafe(dst)
   122  	return dst[copy(dst, v.Version):]
   123  }
   124  
   125  // CheckedUnmarshal implements marshal.CheckedMarshallable.CheckedUnmarshal.
   126  func (v *P9Version) CheckedUnmarshal(src []byte) ([]byte, bool) {
   127  	v.Version = ""
   128  	if v.SizeBytes() > len(src) {
   129  		return nil, false
   130  	}
   131  	src = v.MSize.UnmarshalUnsafe(src)
   132  	var versionLen primitive.Uint16
   133  	src = versionLen.UnmarshalUnsafe(src)
   134  	if int(versionLen) > len(src) {
   135  		return nil, false
   136  	}
   137  	v.Version = string(src[:versionLen])
   138  	return src[versionLen:], true
   139  }