github.com/dylandreimerink/gobpfld@v0.6.1-0.20220205171531-e79c330ad608/emulator/inst_end.go (about)

     1  package emulator
     2  
     3  import (
     4  	"encoding/binary"
     5  	"fmt"
     6  
     7  	"github.com/dylandreimerink/gobpfld/ebpf"
     8  )
     9  
    10  var _ Instruction = (*End16ToLE)(nil)
    11  
    12  type End16ToLE struct {
    13  	ebpf.End16ToLE
    14  }
    15  
    16  func (i *End16ToLE) Clone() Instruction {
    17  	c := *i
    18  	return &c
    19  }
    20  
    21  func (i *End16ToLE) Execute(vm *VM) error {
    22  	rv, r, err := readReg(vm, i.Dest)
    23  	if err != nil {
    24  		return err
    25  	}
    26  
    27  	v := binary.LittleEndian.Uint16([]byte{
    28  		byte(rv >> 8),
    29  		byte(rv >> 0),
    30  	})
    31  
    32  	err = r.Assign(int64(v))
    33  	if err != nil {
    34  		return fmt.Errorf("assign value  %s: %w", i.Dest.String(), err)
    35  	}
    36  
    37  	return nil
    38  }
    39  
    40  var _ Instruction = (*End32ToLE)(nil)
    41  
    42  type End32ToLE struct {
    43  	ebpf.End32ToLE
    44  }
    45  
    46  func (i *End32ToLE) Clone() Instruction {
    47  	c := *i
    48  	return &c
    49  }
    50  
    51  func (i *End32ToLE) Execute(vm *VM) error {
    52  	rv, r, err := readReg(vm, i.Dest)
    53  	if err != nil {
    54  		return err
    55  	}
    56  
    57  	v := binary.LittleEndian.Uint32([]byte{
    58  		byte(rv >> 24),
    59  		byte(rv >> 16),
    60  		byte(rv >> 8),
    61  		byte(rv >> 0),
    62  	})
    63  
    64  	err = r.Assign(int64(v))
    65  	if err != nil {
    66  		return fmt.Errorf("assign value  %s: %w", i.Dest.String(), err)
    67  	}
    68  
    69  	return nil
    70  }
    71  
    72  var _ Instruction = (*End64ToLE)(nil)
    73  
    74  type End64ToLE struct {
    75  	ebpf.End64ToLE
    76  }
    77  
    78  func (i *End64ToLE) Clone() Instruction {
    79  	c := *i
    80  	return &c
    81  }
    82  
    83  func (i *End64ToLE) Execute(vm *VM) error {
    84  	rv, r, err := readReg(vm, i.Dest)
    85  	if err != nil {
    86  		return err
    87  	}
    88  
    89  	v := binary.LittleEndian.Uint64([]byte{
    90  		byte(rv >> 56),
    91  		byte(rv >> 48),
    92  		byte(rv >> 40),
    93  		byte(rv >> 32),
    94  		byte(rv >> 24),
    95  		byte(rv >> 16),
    96  		byte(rv >> 8),
    97  		byte(rv >> 0),
    98  	})
    99  
   100  	err = r.Assign(int64(v))
   101  	if err != nil {
   102  		return fmt.Errorf("assign value  %s: %w", i.Dest.String(), err)
   103  	}
   104  
   105  	return nil
   106  }
   107  
   108  var _ Instruction = (*End16ToBE)(nil)
   109  
   110  type End16ToBE struct {
   111  	ebpf.End16ToBE
   112  }
   113  
   114  func (i *End16ToBE) Clone() Instruction {
   115  	c := *i
   116  	return &c
   117  }
   118  
   119  func (i *End16ToBE) Execute(vm *VM) error {
   120  	rv, r, err := readReg(vm, i.Dest)
   121  	if err != nil {
   122  		return err
   123  	}
   124  
   125  	v := binary.BigEndian.Uint16([]byte{
   126  		byte(rv >> 8),
   127  		byte(rv >> 0),
   128  	})
   129  
   130  	err = r.Assign(int64(v))
   131  	if err != nil {
   132  		return fmt.Errorf("assign value  %s: %w", i.Dest.String(), err)
   133  	}
   134  
   135  	return nil
   136  }
   137  
   138  var _ Instruction = (*End32ToBE)(nil)
   139  
   140  type End32ToBE struct {
   141  	ebpf.End32ToBE
   142  }
   143  
   144  func (i *End32ToBE) Clone() Instruction {
   145  	c := *i
   146  	return &c
   147  }
   148  
   149  func (i *End32ToBE) Execute(vm *VM) error {
   150  	rv, r, err := readReg(vm, i.Dest)
   151  	if err != nil {
   152  		return err
   153  	}
   154  
   155  	v := binary.BigEndian.Uint32([]byte{
   156  		byte(rv >> 24),
   157  		byte(rv >> 16),
   158  		byte(rv >> 8),
   159  		byte(rv >> 0),
   160  	})
   161  
   162  	err = r.Assign(int64(v))
   163  	if err != nil {
   164  		return fmt.Errorf("assign value  %s: %w", i.Dest.String(), err)
   165  	}
   166  
   167  	return nil
   168  }
   169  
   170  var _ Instruction = (*End64ToBE)(nil)
   171  
   172  type End64ToBE struct {
   173  	ebpf.End64ToBE
   174  }
   175  
   176  func (i *End64ToBE) Clone() Instruction {
   177  	c := *i
   178  	return &c
   179  }
   180  
   181  func (i *End64ToBE) Execute(vm *VM) error {
   182  	rv, r, err := readReg(vm, i.Dest)
   183  	if err != nil {
   184  		return err
   185  	}
   186  
   187  	v := binary.BigEndian.Uint64([]byte{
   188  		byte(rv >> 56),
   189  		byte(rv >> 48),
   190  		byte(rv >> 40),
   191  		byte(rv >> 32),
   192  		byte(rv >> 24),
   193  		byte(rv >> 16),
   194  		byte(rv >> 8),
   195  		byte(rv >> 0),
   196  	})
   197  
   198  	err = r.Assign(int64(v))
   199  	if err != nil {
   200  		return fmt.Errorf("assign value  %s: %w", i.Dest.String(), err)
   201  	}
   202  
   203  	return nil
   204  }