github.com/linuxboot/fiano@v1.2.0/pkg/intel/metadata/cbnt/structure_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 github.com/linuxboot/fiano/pkg/intel/metadata/cbnt
    10  
    11  package cbnt
    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  // NewStructInfo returns a new instance of StructInfo with
    32  // all default values set.
    33  func NewStructInfo() *StructInfo {
    34  	s := &StructInfo{}
    35  	s.Rehash()
    36  	return s
    37  }
    38  
    39  // Validate (recursively) checks the structure if there are any unexpected
    40  // values. It returns an error if so.
    41  func (s *StructInfo) Validate() error {
    42  
    43  	return nil
    44  }
    45  
    46  // ReadFrom reads the StructInfo from 'r' in format defined in the document #575623.
    47  func (s *StructInfo) ReadFrom(r io.Reader) (int64, error) {
    48  	totalN := int64(0)
    49  
    50  	// ID (ManifestFieldType: arrayStatic)
    51  	{
    52  		n, err := 8, binary.Read(r, binary.LittleEndian, s.ID[:])
    53  		if err != nil {
    54  			return totalN, fmt.Errorf("unable to read field 'ID': %w", err)
    55  		}
    56  		totalN += int64(n)
    57  	}
    58  
    59  	// Version (ManifestFieldType: endValue)
    60  	{
    61  		n, err := 1, binary.Read(r, binary.LittleEndian, &s.Version)
    62  		if err != nil {
    63  			return totalN, fmt.Errorf("unable to read field 'Version': %w", err)
    64  		}
    65  		totalN += int64(n)
    66  	}
    67  
    68  	// Variable0 (ManifestFieldType: endValue)
    69  	{
    70  		n, err := 1, binary.Read(r, binary.LittleEndian, &s.Variable0)
    71  		if err != nil {
    72  			return totalN, fmt.Errorf("unable to read field 'Variable0': %w", err)
    73  		}
    74  		totalN += int64(n)
    75  	}
    76  
    77  	// ElementSize (ManifestFieldType: endValue)
    78  	{
    79  		n, err := 2, binary.Read(r, binary.LittleEndian, &s.ElementSize)
    80  		if err != nil {
    81  			return totalN, fmt.Errorf("unable to read field 'ElementSize': %w", err)
    82  		}
    83  		totalN += int64(n)
    84  	}
    85  
    86  	return totalN, nil
    87  }
    88  
    89  // RehashRecursive calls Rehash (see below) recursively.
    90  func (s *StructInfo) RehashRecursive() {
    91  	s.Rehash()
    92  }
    93  
    94  // Rehash sets values which are calculated automatically depending on the rest
    95  // data. It is usually about the total size field of an element.
    96  func (s *StructInfo) Rehash() {
    97  }
    98  
    99  // WriteTo writes the StructInfo into 'w' in format defined in
   100  // the document #575623.
   101  func (s *StructInfo) WriteTo(w io.Writer) (int64, error) {
   102  	totalN := int64(0)
   103  	s.Rehash()
   104  
   105  	// ID (ManifestFieldType: arrayStatic)
   106  	{
   107  		n, err := 8, binary.Write(w, binary.LittleEndian, s.ID[:])
   108  		if err != nil {
   109  			return totalN, fmt.Errorf("unable to write field 'ID': %w", err)
   110  		}
   111  		totalN += int64(n)
   112  	}
   113  
   114  	// Version (ManifestFieldType: endValue)
   115  	{
   116  		n, err := 1, binary.Write(w, binary.LittleEndian, &s.Version)
   117  		if err != nil {
   118  			return totalN, fmt.Errorf("unable to write field 'Version': %w", err)
   119  		}
   120  		totalN += int64(n)
   121  	}
   122  
   123  	// Variable0 (ManifestFieldType: endValue)
   124  	{
   125  		n, err := 1, binary.Write(w, binary.LittleEndian, &s.Variable0)
   126  		if err != nil {
   127  			return totalN, fmt.Errorf("unable to write field 'Variable0': %w", err)
   128  		}
   129  		totalN += int64(n)
   130  	}
   131  
   132  	// ElementSize (ManifestFieldType: endValue)
   133  	{
   134  		n, err := 2, binary.Write(w, binary.LittleEndian, &s.ElementSize)
   135  		if err != nil {
   136  			return totalN, fmt.Errorf("unable to write field 'ElementSize': %w", err)
   137  		}
   138  		totalN += int64(n)
   139  	}
   140  
   141  	return totalN, nil
   142  }
   143  
   144  // IDSize returns the size in bytes of the value of field ID
   145  func (s *StructInfo) IDTotalSize() uint64 {
   146  	return 8
   147  }
   148  
   149  // VersionSize returns the size in bytes of the value of field Version
   150  func (s *StructInfo) VersionTotalSize() uint64 {
   151  	return 1
   152  }
   153  
   154  // Variable0Size returns the size in bytes of the value of field Variable0
   155  func (s *StructInfo) Variable0TotalSize() uint64 {
   156  	return 1
   157  }
   158  
   159  // ElementSizeSize returns the size in bytes of the value of field ElementSize
   160  func (s *StructInfo) ElementSizeTotalSize() uint64 {
   161  	return 2
   162  }
   163  
   164  // IDOffset returns the offset in bytes of field ID
   165  func (s *StructInfo) IDOffset() uint64 {
   166  	return 0
   167  }
   168  
   169  // VersionOffset returns the offset in bytes of field Version
   170  func (s *StructInfo) VersionOffset() uint64 {
   171  	return s.IDOffset() + s.IDTotalSize()
   172  }
   173  
   174  // Variable0Offset returns the offset in bytes of field Variable0
   175  func (s *StructInfo) Variable0Offset() uint64 {
   176  	return s.VersionOffset() + s.VersionTotalSize()
   177  }
   178  
   179  // ElementSizeOffset returns the offset in bytes of field ElementSize
   180  func (s *StructInfo) ElementSizeOffset() uint64 {
   181  	return s.Variable0Offset() + s.Variable0TotalSize()
   182  }
   183  
   184  // Size returns the total size of the StructInfo.
   185  func (s *StructInfo) TotalSize() uint64 {
   186  	if s == nil {
   187  		return 0
   188  	}
   189  
   190  	var size uint64
   191  	size += s.IDTotalSize()
   192  	size += s.VersionTotalSize()
   193  	size += s.Variable0TotalSize()
   194  	size += s.ElementSizeTotalSize()
   195  	return size
   196  }
   197  
   198  // PrettyString returns the content of the structure in an easy-to-read format.
   199  func (s *StructInfo) PrettyString(depth uint, withHeader bool, opts ...pretty.Option) string {
   200  	var lines []string
   201  	if withHeader {
   202  		lines = append(lines, pretty.Header(depth, "Struct Info", s))
   203  	}
   204  	if s == nil {
   205  		return strings.Join(lines, "\n")
   206  	}
   207  	// ManifestFieldType is arrayStatic
   208  	lines = append(lines, pretty.SubValue(depth+1, "ID", "", &s.ID, opts...)...)
   209  	// ManifestFieldType is endValue
   210  	lines = append(lines, pretty.SubValue(depth+1, "Version", "", &s.Version, opts...)...)
   211  	// ManifestFieldType is endValue
   212  	lines = append(lines, pretty.SubValue(depth+1, "Variable 0", "", &s.Variable0, opts...)...)
   213  	// ManifestFieldType is endValue
   214  	lines = append(lines, pretty.SubValue(depth+1, "Element Size", "", &s.ElementSize, opts...)...)
   215  	if depth < 2 {
   216  		lines = append(lines, "")
   217  	}
   218  	return strings.Join(lines, "\n")
   219  }