github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/tools/go_marshal/test/dynamic.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 test
    16  
    17  import "github.com/SagerNet/gvisor/pkg/marshal/primitive"
    18  
    19  // Type12Dynamic is a dynamically sized struct which depends on the
    20  // autogenerator to generate some Marshallable methods for it.
    21  //
    22  // +marshal dynamic
    23  type Type12Dynamic struct {
    24  	X primitive.Int64
    25  	Y []primitive.Int64
    26  }
    27  
    28  // SizeBytes implements marshal.Marshallable.SizeBytes.
    29  func (t *Type12Dynamic) SizeBytes() int {
    30  	return (len(t.Y) * 8) + t.X.SizeBytes()
    31  }
    32  
    33  // MarshalBytes implements marshal.Marshallable.MarshalBytes.
    34  func (t *Type12Dynamic) MarshalBytes(dst []byte) {
    35  	t.X.MarshalBytes(dst)
    36  	dst = dst[t.X.SizeBytes():]
    37  	for i, x := range t.Y {
    38  		x.MarshalBytes(dst[i*8 : (i+1)*8])
    39  	}
    40  }
    41  
    42  // UnmarshalBytes implements marshal.Marshallable.UnmarshalBytes.
    43  func (t *Type12Dynamic) UnmarshalBytes(src []byte) {
    44  	t.X.UnmarshalBytes(src)
    45  	if t.Y != nil {
    46  		t.Y = t.Y[:0]
    47  	}
    48  	for i := t.X.SizeBytes(); i < len(src); i += 8 {
    49  		var x primitive.Int64
    50  		x.UnmarshalBytes(src[i:])
    51  		t.Y = append(t.Y, x)
    52  	}
    53  }
    54  
    55  // Type13Dynamic is a dynamically sized struct which depends on the
    56  // autogenerator to generate some Marshallable methods for it.
    57  //
    58  // It represents a string in memory which is preceded by a uint32 indicating
    59  // the string size.
    60  //
    61  // +marshal dynamic
    62  type Type13Dynamic string
    63  
    64  // SizeBytes implements marshal.Marshallable.SizeBytes.
    65  func (t *Type13Dynamic) SizeBytes() int {
    66  	return (*primitive.Uint32)(nil).SizeBytes() + len(*t)
    67  }
    68  
    69  // MarshalBytes implements marshal.Marshallable.MarshalBytes.
    70  func (t *Type13Dynamic) MarshalBytes(dst []byte) {
    71  	strLen := primitive.Uint32(len(*t))
    72  	strLen.MarshalBytes(dst)
    73  	dst = dst[strLen.SizeBytes():]
    74  	copy(dst[:strLen], *t)
    75  }
    76  
    77  // UnmarshalBytes implements marshal.Marshallable.UnmarshalBytes.
    78  func (t *Type13Dynamic) UnmarshalBytes(src []byte) {
    79  	var strLen primitive.Uint32
    80  	strLen.UnmarshalBytes(src)
    81  	src = src[strLen.SizeBytes():]
    82  	*t = Type13Dynamic(src[:strLen])
    83  }