github.com/linuxboot/fiano@v1.2.0/pkg/intel/metadata/bg/key_manifestcodegen.go (about)

     1  // Copyright 2017-2021 the LinuxBoot Authors. All rights reserved
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build !manifestcodegen
     6  // +build !manifestcodegen
     7  
     8  // Code generated by "menifestcodegen". DO NOT EDIT.
     9  // To reproduce: go run github.com/linuxboot/fiano/pkg/intel/metadata/common/manifestcodegen/cmd/manifestcodegen -package bg github.com/linuxboot/fiano/pkg/intel/metadata/bg
    10  
    11  package bg
    12  
    13  import (
    14  	"encoding/binary"
    15  	"fmt"
    16  	"io"
    17  	"strings"
    18  
    19  	"github.com/linuxboot/fiano/pkg/intel/metadata/common/pretty"
    20  )
    21  
    22  var (
    23  	// Just to avoid errors in "import" above in case if it wasn't used below
    24  	_ = binary.LittleEndian
    25  	_ = (fmt.Stringer)(nil)
    26  	_ = (io.Reader)(nil)
    27  	_ = pretty.Header
    28  	_ = strings.Join
    29  )
    30  
    31  // NewKey returns a new instance of Key with
    32  // all default values set.
    33  func NewKey() *Key {
    34  	s := &Key{}
    35  	// Set through tag "required":
    36  	s.Version = 0x10
    37  	s.Rehash()
    38  	return s
    39  }
    40  
    41  // Validate (recursively) checks the structure if there are any unexpected
    42  // values. It returns an error if so.
    43  func (s *Key) Validate() error {
    44  	// See tag "require"
    45  	if s.Version != 0x10 {
    46  		return fmt.Errorf("field 'Version' expects value '0x10', but has %v", s.Version)
    47  	}
    48  
    49  	return nil
    50  }
    51  
    52  // ReadFrom reads the Key from 'r' in format defined in the document #575623.
    53  func (s *Key) ReadFrom(r io.Reader) (int64, error) {
    54  	totalN := int64(0)
    55  
    56  	// KeyAlg (ManifestFieldType: endValue)
    57  	{
    58  		n, err := 2, binary.Read(r, binary.LittleEndian, &s.KeyAlg)
    59  		if err != nil {
    60  			return totalN, fmt.Errorf("unable to read field 'KeyAlg': %w", err)
    61  		}
    62  		totalN += int64(n)
    63  	}
    64  
    65  	// Version (ManifestFieldType: endValue)
    66  	{
    67  		n, err := 1, binary.Read(r, binary.LittleEndian, &s.Version)
    68  		if err != nil {
    69  			return totalN, fmt.Errorf("unable to read field 'Version': %w", err)
    70  		}
    71  		totalN += int64(n)
    72  	}
    73  
    74  	// KeySize (ManifestFieldType: endValue)
    75  	{
    76  		n, err := 2, binary.Read(r, binary.LittleEndian, &s.KeySize)
    77  		if err != nil {
    78  			return totalN, fmt.Errorf("unable to read field 'KeySize': %w", err)
    79  		}
    80  		totalN += int64(n)
    81  	}
    82  
    83  	// Data (ManifestFieldType: arrayDynamic)
    84  	{
    85  		size := uint16(s.keyDataSize())
    86  		s.Data = make([]byte, size)
    87  		n, err := len(s.Data), binary.Read(r, binary.LittleEndian, s.Data)
    88  		if err != nil {
    89  			return totalN, fmt.Errorf("unable to read field 'Data': %w", err)
    90  		}
    91  		totalN += int64(n)
    92  	}
    93  
    94  	return totalN, nil
    95  }
    96  
    97  // RehashRecursive calls Rehash (see below) recursively.
    98  func (s *Key) RehashRecursive() {
    99  	s.Rehash()
   100  }
   101  
   102  // Rehash sets values which are calculated automatically depending on the rest
   103  // data. It is usually about the total size field of an element.
   104  func (s *Key) Rehash() {
   105  }
   106  
   107  // WriteTo writes the Key into 'w' in format defined in
   108  // the document #575623.
   109  func (s *Key) WriteTo(w io.Writer) (int64, error) {
   110  	totalN := int64(0)
   111  	s.Rehash()
   112  
   113  	// KeyAlg (ManifestFieldType: endValue)
   114  	{
   115  		n, err := 2, binary.Write(w, binary.LittleEndian, &s.KeyAlg)
   116  		if err != nil {
   117  			return totalN, fmt.Errorf("unable to write field 'KeyAlg': %w", err)
   118  		}
   119  		totalN += int64(n)
   120  	}
   121  
   122  	// Version (ManifestFieldType: endValue)
   123  	{
   124  		n, err := 1, binary.Write(w, binary.LittleEndian, &s.Version)
   125  		if err != nil {
   126  			return totalN, fmt.Errorf("unable to write field 'Version': %w", err)
   127  		}
   128  		totalN += int64(n)
   129  	}
   130  
   131  	// KeySize (ManifestFieldType: endValue)
   132  	{
   133  		n, err := 2, binary.Write(w, binary.LittleEndian, &s.KeySize)
   134  		if err != nil {
   135  			return totalN, fmt.Errorf("unable to write field 'KeySize': %w", err)
   136  		}
   137  		totalN += int64(n)
   138  	}
   139  
   140  	// Data (ManifestFieldType: arrayDynamic)
   141  	{
   142  		n, err := len(s.Data), binary.Write(w, binary.LittleEndian, s.Data)
   143  		if err != nil {
   144  			return totalN, fmt.Errorf("unable to write field 'Data': %w", err)
   145  		}
   146  		totalN += int64(n)
   147  	}
   148  
   149  	return totalN, nil
   150  }
   151  
   152  // KeyAlgSize returns the size in bytes of the value of field KeyAlg
   153  func (s *Key) KeyAlgTotalSize() uint64 {
   154  	return 2
   155  }
   156  
   157  // VersionSize returns the size in bytes of the value of field Version
   158  func (s *Key) VersionTotalSize() uint64 {
   159  	return 1
   160  }
   161  
   162  // KeySizeSize returns the size in bytes of the value of field KeySize
   163  func (s *Key) KeySizeTotalSize() uint64 {
   164  	return 2
   165  }
   166  
   167  // DataSize returns the size in bytes of the value of field Data
   168  func (s *Key) DataTotalSize() uint64 {
   169  	return uint64(len(s.Data))
   170  }
   171  
   172  // KeyAlgOffset returns the offset in bytes of field KeyAlg
   173  func (s *Key) KeyAlgOffset() uint64 {
   174  	return 0
   175  }
   176  
   177  // VersionOffset returns the offset in bytes of field Version
   178  func (s *Key) VersionOffset() uint64 {
   179  	return s.KeyAlgOffset() + s.KeyAlgTotalSize()
   180  }
   181  
   182  // KeySizeOffset returns the offset in bytes of field KeySize
   183  func (s *Key) KeySizeOffset() uint64 {
   184  	return s.VersionOffset() + s.VersionTotalSize()
   185  }
   186  
   187  // DataOffset returns the offset in bytes of field Data
   188  func (s *Key) DataOffset() uint64 {
   189  	return s.KeySizeOffset() + s.KeySizeTotalSize()
   190  }
   191  
   192  // Size returns the total size of the Key.
   193  func (s *Key) TotalSize() uint64 {
   194  	if s == nil {
   195  		return 0
   196  	}
   197  
   198  	var size uint64
   199  	size += s.KeyAlgTotalSize()
   200  	size += s.VersionTotalSize()
   201  	size += s.KeySizeTotalSize()
   202  	size += s.DataTotalSize()
   203  	return size
   204  }
   205  
   206  // PrettyString returns the content of the structure in an easy-to-read format.
   207  func (s *Key) PrettyString(depth uint, withHeader bool, opts ...pretty.Option) string {
   208  	var lines []string
   209  	if withHeader {
   210  		lines = append(lines, pretty.Header(depth, "Key", s))
   211  	}
   212  	if s == nil {
   213  		return strings.Join(lines, "\n")
   214  	}
   215  	// ManifestFieldType is endValue
   216  	lines = append(lines, pretty.SubValue(depth+1, "Key Alg", "", &s.KeyAlg, opts...)...)
   217  	// ManifestFieldType is endValue
   218  	lines = append(lines, pretty.SubValue(depth+1, "Version", "", &s.Version, opts...)...)
   219  	// ManifestFieldType is endValue
   220  	lines = append(lines, pretty.SubValue(depth+1, "Key Size", "", &s.KeySize, opts...)...)
   221  	// ManifestFieldType is arrayDynamic
   222  	lines = append(lines, pretty.SubValue(depth+1, "Data", "", &s.Data, opts...)...)
   223  	if depth < 2 {
   224  		lines = append(lines, "")
   225  	}
   226  	return strings.Join(lines, "\n")
   227  }
   228  
   229  // PrettyString returns the bits of the flags in an easy-to-read format.
   230  func (v BitSize) PrettyString(depth uint, withHeader bool, opts ...pretty.Option) string {
   231  	var lines []string
   232  	if withHeader {
   233  		lines = append(lines, pretty.Header(depth, "Bit Size", v))
   234  	}
   235  	lines = append(lines, pretty.SubValue(depth+1, "In Bits", "", v.InBits(), opts...)...)
   236  	lines = append(lines, pretty.SubValue(depth+1, "In Bytes", "", v.InBytes(), opts...)...)
   237  	return strings.Join(lines, "\n")
   238  }
   239  
   240  // TotalSize returns the total size measured through binary.Size.
   241  func (v BitSize) TotalSize() uint64 {
   242  	return uint64(binary.Size(v))
   243  }
   244  
   245  // WriteTo writes the BitSize into 'w' in binary format.
   246  func (v BitSize) WriteTo(w io.Writer) (int64, error) {
   247  	return int64(v.TotalSize()), binary.Write(w, binary.LittleEndian, v)
   248  }
   249  
   250  // ReadFrom reads the BitSize from 'r' in binary format.
   251  func (v BitSize) ReadFrom(r io.Reader) (int64, error) {
   252  	return int64(v.TotalSize()), binary.Read(r, binary.LittleEndian, v)
   253  }