gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/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 "gvisor.dev/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) []byte {
    35  	dst = t.X.MarshalBytes(dst)
    36  	for _, x := range t.Y {
    37  		dst = x.MarshalBytes(dst)
    38  	}
    39  	return dst
    40  }
    41  
    42  // UnmarshalBytes implements marshal.Marshallable.UnmarshalBytes.
    43  func (t *Type12Dynamic) UnmarshalBytes(src []byte) []byte {
    44  	src = t.X.UnmarshalBytes(src)
    45  	if t.Y != nil {
    46  		t.Y = t.Y[:0]
    47  	}
    48  	for len(src) > 0 {
    49  		var x primitive.Int64
    50  		src = x.UnmarshalBytes(src)
    51  		t.Y = append(t.Y, x)
    52  	}
    53  	return src
    54  }
    55  
    56  // Type13Dynamic is a dynamically sized struct which depends on the
    57  // autogenerator to generate some Marshallable methods for it.
    58  //
    59  // It represents a string in memory which is preceded by a uint32 indicating
    60  // the string size.
    61  //
    62  // +marshal dynamic
    63  type Type13Dynamic string
    64  
    65  // SizeBytes implements marshal.Marshallable.SizeBytes.
    66  func (t *Type13Dynamic) SizeBytes() int {
    67  	return (*primitive.Uint32)(nil).SizeBytes() + len(*t)
    68  }
    69  
    70  // MarshalBytes implements marshal.Marshallable.MarshalBytes.
    71  func (t *Type13Dynamic) MarshalBytes(dst []byte) []byte {
    72  	strLen := primitive.Uint32(len(*t))
    73  	dst = strLen.MarshalBytes(dst)
    74  	return dst[copy(dst[:strLen], *t):]
    75  }
    76  
    77  // UnmarshalBytes implements marshal.Marshallable.UnmarshalBytes.
    78  func (t *Type13Dynamic) UnmarshalBytes(src []byte) []byte {
    79  	var strLen primitive.Uint32
    80  	src = strLen.UnmarshalBytes(src)
    81  	*t = Type13Dynamic(src[:strLen])
    82  	return src[strLen:]
    83  }