github.com/nicocha30/gvisor-ligolo@v0.0.0-20230726075806-989fa2c0a413/pkg/bpf/input_bytes.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 bpf
    16  
    17  import (
    18  	"encoding/binary"
    19  )
    20  
    21  // InputBytes implements the Input interface by providing access to a byte
    22  // slice. Unaligned loads are supported.
    23  type InputBytes struct {
    24  	// Data is the data accessed through the Input interface.
    25  	Data []byte
    26  
    27  	// Order is the byte order the data is accessed with.
    28  	Order binary.ByteOrder
    29  }
    30  
    31  // Load32 implements Input.Load32.
    32  func (i InputBytes) Load32(off uint32) (uint32, bool) {
    33  	if uint64(off)+4 > uint64(len(i.Data)) {
    34  		return 0, false
    35  	}
    36  	return i.Order.Uint32(i.Data[int(off):]), true
    37  }
    38  
    39  // Load16 implements Input.Load16.
    40  func (i InputBytes) Load16(off uint32) (uint16, bool) {
    41  	if uint64(off)+2 > uint64(len(i.Data)) {
    42  		return 0, false
    43  	}
    44  	return i.Order.Uint16(i.Data[int(off):]), true
    45  }
    46  
    47  // Load8 implements Input.Load8.
    48  func (i InputBytes) Load8(off uint32) (uint8, bool) {
    49  	if uint64(off)+1 > uint64(len(i.Data)) {
    50  		return 0, false
    51  	}
    52  	return i.Data[int(off)], true
    53  }
    54  
    55  // Length implements Input.Length.
    56  func (i InputBytes) Length() uint32 {
    57  	return uint32(len(i.Data))
    58  }