github.com/linuxboot/fiano@v1.2.0/pkg/intel/metadata/bg/hash_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  // NewHashStructure returns a new instance of HashStructure with
    32  // all default values set.
    33  func NewHashStructure() *HashStructure {
    34  	s := &HashStructure{}
    35  	// Set through tag "default":
    36  	s.HashAlg = 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 *HashStructure) Validate() error {
    44  
    45  	return nil
    46  }
    47  
    48  // ReadFrom reads the HashStructure from 'r' in format defined in the document #575623.
    49  func (s *HashStructure) ReadFrom(r io.Reader) (int64, error) {
    50  	totalN := int64(0)
    51  
    52  	// HashAlg (ManifestFieldType: endValue)
    53  	{
    54  		n, err := 2, binary.Read(r, binary.LittleEndian, &s.HashAlg)
    55  		if err != nil {
    56  			return totalN, fmt.Errorf("unable to read field 'HashAlg': %w", err)
    57  		}
    58  		totalN += int64(n)
    59  	}
    60  
    61  	// HashBuffer (ManifestFieldType: arrayDynamic)
    62  	{
    63  		var size uint16
    64  		err := binary.Read(r, binary.LittleEndian, &size)
    65  		if err != nil {
    66  			return totalN, fmt.Errorf("unable to the read size of field 'HashBuffer': %w", err)
    67  		}
    68  		totalN += int64(binary.Size(size))
    69  		s.HashBuffer = make([]byte, size)
    70  		n, err := len(s.HashBuffer), binary.Read(r, binary.LittleEndian, s.HashBuffer)
    71  		if err != nil {
    72  			return totalN, fmt.Errorf("unable to read field 'HashBuffer': %w", err)
    73  		}
    74  		totalN += int64(n)
    75  	}
    76  
    77  	return totalN, nil
    78  }
    79  
    80  // RehashRecursive calls Rehash (see below) recursively.
    81  func (s *HashStructure) RehashRecursive() {
    82  	s.Rehash()
    83  }
    84  
    85  // Rehash sets values which are calculated automatically depending on the rest
    86  // data. It is usually about the total size field of an element.
    87  func (s *HashStructure) Rehash() {
    88  }
    89  
    90  // WriteTo writes the HashStructure into 'w' in format defined in
    91  // the document #575623.
    92  func (s *HashStructure) WriteTo(w io.Writer) (int64, error) {
    93  	totalN := int64(0)
    94  	s.Rehash()
    95  
    96  	// HashAlg (ManifestFieldType: endValue)
    97  	{
    98  		n, err := 2, binary.Write(w, binary.LittleEndian, &s.HashAlg)
    99  		if err != nil {
   100  			return totalN, fmt.Errorf("unable to write field 'HashAlg': %w", err)
   101  		}
   102  		totalN += int64(n)
   103  	}
   104  
   105  	// HashBuffer (ManifestFieldType: arrayDynamic)
   106  	{
   107  		size := uint16(len(s.HashBuffer))
   108  		err := binary.Write(w, binary.LittleEndian, size)
   109  		if err != nil {
   110  			return totalN, fmt.Errorf("unable to write the size of field 'HashBuffer': %w", err)
   111  		}
   112  		totalN += int64(binary.Size(size))
   113  		n, err := len(s.HashBuffer), binary.Write(w, binary.LittleEndian, s.HashBuffer)
   114  		if err != nil {
   115  			return totalN, fmt.Errorf("unable to write field 'HashBuffer': %w", err)
   116  		}
   117  		totalN += int64(n)
   118  	}
   119  
   120  	return totalN, nil
   121  }
   122  
   123  // HashAlgSize returns the size in bytes of the value of field HashAlg
   124  func (s *HashStructure) HashAlgTotalSize() uint64 {
   125  	return 2
   126  }
   127  
   128  // HashBufferSize returns the size in bytes of the value of field HashBuffer
   129  func (s *HashStructure) HashBufferTotalSize() uint64 {
   130  	size := uint64(binary.Size(uint16(0)))
   131  	size += uint64(len(s.HashBuffer))
   132  	return size
   133  }
   134  
   135  // HashAlgOffset returns the offset in bytes of field HashAlg
   136  func (s *HashStructure) HashAlgOffset() uint64 {
   137  	return 0
   138  }
   139  
   140  // HashBufferOffset returns the offset in bytes of field HashBuffer
   141  func (s *HashStructure) HashBufferOffset() uint64 {
   142  	return s.HashAlgOffset() + s.HashAlgTotalSize()
   143  }
   144  
   145  // Size returns the total size of the HashStructure.
   146  func (s *HashStructure) TotalSize() uint64 {
   147  	if s == nil {
   148  		return 0
   149  	}
   150  
   151  	var size uint64
   152  	size += s.HashAlgTotalSize()
   153  	size += s.HashBufferTotalSize()
   154  	return size
   155  }
   156  
   157  // PrettyString returns the content of the structure in an easy-to-read format.
   158  func (s *HashStructure) PrettyString(depth uint, withHeader bool, opts ...pretty.Option) string {
   159  	var lines []string
   160  	if withHeader {
   161  		lines = append(lines, pretty.Header(depth, "Hash Structure", s))
   162  	}
   163  	if s == nil {
   164  		return strings.Join(lines, "\n")
   165  	}
   166  	// ManifestFieldType is endValue
   167  	lines = append(lines, pretty.SubValue(depth+1, "Hash Alg", "", &s.HashAlg, opts...)...)
   168  	// ManifestFieldType is arrayDynamic
   169  	lines = append(lines, pretty.SubValue(depth+1, "Hash Buffer", "", &s.HashBuffer, opts...)...)
   170  	if depth < 2 {
   171  		lines = append(lines, "")
   172  	}
   173  	return strings.Join(lines, "\n")
   174  }
   175  
   176  // NewHashStructureFill returns a new instance of HashStructureFill with
   177  // all default values set.
   178  func NewHashStructureFill() *HashStructureFill {
   179  	s := &HashStructureFill{}
   180  	// Set through tag "default":
   181  	s.HashAlg = 0x0b
   182  	s.Rehash()
   183  	return s
   184  }
   185  
   186  // Validate (recursively) checks the structure if there are any unexpected
   187  // values. It returns an error if so.
   188  func (s *HashStructureFill) Validate() error {
   189  
   190  	return nil
   191  }
   192  
   193  // ReadFrom reads the HashStructureFill from 'r' in format defined in the document #575623.
   194  func (s *HashStructureFill) ReadFrom(r io.Reader) (int64, error) {
   195  	totalN := int64(0)
   196  
   197  	// HashAlg (ManifestFieldType: endValue)
   198  	{
   199  		n, err := 2, binary.Read(r, binary.LittleEndian, &s.HashAlg)
   200  		if err != nil {
   201  			return totalN, fmt.Errorf("unable to read field 'HashAlg': %w", err)
   202  		}
   203  		totalN += int64(n)
   204  	}
   205  
   206  	// HashBuffer (ManifestFieldType: arrayDynamic)
   207  	{
   208  		size := uint16(s.hashSize())
   209  		s.HashBuffer = make([]byte, size)
   210  		n, err := len(s.HashBuffer), binary.Read(r, binary.LittleEndian, s.HashBuffer)
   211  		if err != nil {
   212  			return totalN, fmt.Errorf("unable to read field 'HashBuffer': %w", err)
   213  		}
   214  		totalN += int64(n)
   215  	}
   216  
   217  	return totalN, nil
   218  }
   219  
   220  // RehashRecursive calls Rehash (see below) recursively.
   221  func (s *HashStructureFill) RehashRecursive() {
   222  	s.Rehash()
   223  }
   224  
   225  // Rehash sets values which are calculated automatically depending on the rest
   226  // data. It is usually about the total size field of an element.
   227  func (s *HashStructureFill) Rehash() {
   228  }
   229  
   230  // WriteTo writes the HashStructureFill into 'w' in format defined in
   231  // the document #575623.
   232  func (s *HashStructureFill) WriteTo(w io.Writer) (int64, error) {
   233  	totalN := int64(0)
   234  	s.Rehash()
   235  
   236  	// HashAlg (ManifestFieldType: endValue)
   237  	{
   238  		n, err := 2, binary.Write(w, binary.LittleEndian, &s.HashAlg)
   239  		if err != nil {
   240  			return totalN, fmt.Errorf("unable to write field 'HashAlg': %w", err)
   241  		}
   242  		totalN += int64(n)
   243  	}
   244  
   245  	// HashBuffer (ManifestFieldType: arrayDynamic)
   246  	{
   247  		n, err := len(s.HashBuffer), binary.Write(w, binary.LittleEndian, s.HashBuffer)
   248  		if err != nil {
   249  			return totalN, fmt.Errorf("unable to write field 'HashBuffer': %w", err)
   250  		}
   251  		totalN += int64(n)
   252  	}
   253  
   254  	return totalN, nil
   255  }
   256  
   257  // HashAlgSize returns the size in bytes of the value of field HashAlg
   258  func (s *HashStructureFill) HashAlgTotalSize() uint64 {
   259  	return 2
   260  }
   261  
   262  // HashBufferSize returns the size in bytes of the value of field HashBuffer
   263  func (s *HashStructureFill) HashBufferTotalSize() uint64 {
   264  	return uint64(len(s.HashBuffer))
   265  }
   266  
   267  // HashAlgOffset returns the offset in bytes of field HashAlg
   268  func (s *HashStructureFill) HashAlgOffset() uint64 {
   269  	return 0
   270  }
   271  
   272  // HashBufferOffset returns the offset in bytes of field HashBuffer
   273  func (s *HashStructureFill) HashBufferOffset() uint64 {
   274  	return s.HashAlgOffset() + s.HashAlgTotalSize()
   275  }
   276  
   277  // Size returns the total size of the HashStructureFill.
   278  func (s *HashStructureFill) TotalSize() uint64 {
   279  	if s == nil {
   280  		return 0
   281  	}
   282  
   283  	var size uint64
   284  	size += s.HashAlgTotalSize()
   285  	size += s.HashBufferTotalSize()
   286  	return size
   287  }
   288  
   289  // PrettyString returns the content of the structure in an easy-to-read format.
   290  func (s *HashStructureFill) PrettyString(depth uint, withHeader bool, opts ...pretty.Option) string {
   291  	var lines []string
   292  	if withHeader {
   293  		lines = append(lines, pretty.Header(depth, "Hash Structure Fill", s))
   294  	}
   295  	if s == nil {
   296  		return strings.Join(lines, "\n")
   297  	}
   298  	// ManifestFieldType is endValue
   299  	lines = append(lines, pretty.SubValue(depth+1, "Hash Alg", "", &s.HashAlg, opts...)...)
   300  	// ManifestFieldType is arrayDynamic
   301  	lines = append(lines, pretty.SubValue(depth+1, "Hash Buffer", "", s.hashSizePrint(), opts...)...)
   302  	if depth < 2 {
   303  		lines = append(lines, "")
   304  	}
   305  	return strings.Join(lines, "\n")
   306  }