github.com/linuxboot/fiano@v1.2.0/pkg/intel/metadata/cbnt/cbntkey/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 github.com/linuxboot/fiano/pkg/intel/metadata/cbnt/cbntkey
    10  
    11  package cbntkey
    12  
    13  import (
    14  	"encoding/binary"
    15  	"fmt"
    16  	"io"
    17  	"strings"
    18  
    19  	"github.com/linuxboot/fiano/pkg/intel/metadata/cbnt"
    20  	"github.com/linuxboot/fiano/pkg/intel/metadata/common/pretty"
    21  )
    22  
    23  var (
    24  	// Just to avoid errors in "import" above in case if it wasn't used below
    25  	_ = binary.LittleEndian
    26  	_ = (fmt.Stringer)(nil)
    27  	_ = (io.Reader)(nil)
    28  	_ = pretty.Header
    29  	_ = strings.Join
    30  	_ = cbnt.StructInfo{}
    31  )
    32  
    33  // NewHash returns a new instance of Hash with
    34  // all default values set.
    35  func NewHash() *Hash {
    36  	s := &Hash{}
    37  	// Recursively initializing a child structure:
    38  	s.Digest = *cbnt.NewHashStructure()
    39  	s.Rehash()
    40  	return s
    41  }
    42  
    43  // Validate (recursively) checks the structure if there are any unexpected
    44  // values. It returns an error if so.
    45  func (s *Hash) Validate() error {
    46  	// Recursively validating a child structure:
    47  	if err := s.Digest.Validate(); err != nil {
    48  		return fmt.Errorf("error on field 'Digest': %w", err)
    49  	}
    50  
    51  	return nil
    52  }
    53  
    54  // ReadFrom reads the Hash from 'r' in format defined in the document #575623.
    55  func (s *Hash) ReadFrom(r io.Reader) (int64, error) {
    56  	totalN := int64(0)
    57  
    58  	// Usage (ManifestFieldType: endValue)
    59  	{
    60  		n, err := 8, binary.Read(r, binary.LittleEndian, &s.Usage)
    61  		if err != nil {
    62  			return totalN, fmt.Errorf("unable to read field 'Usage': %w", err)
    63  		}
    64  		totalN += int64(n)
    65  	}
    66  
    67  	// Digest (ManifestFieldType: subStruct)
    68  	{
    69  		n, err := s.Digest.ReadFrom(r)
    70  		if err != nil {
    71  			return totalN, fmt.Errorf("unable to read field 'Digest': %w", err)
    72  		}
    73  		totalN += int64(n)
    74  	}
    75  
    76  	return totalN, nil
    77  }
    78  
    79  // RehashRecursive calls Rehash (see below) recursively.
    80  func (s *Hash) RehashRecursive() {
    81  	s.Digest.Rehash()
    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 *Hash) Rehash() {
    88  }
    89  
    90  // WriteTo writes the Hash into 'w' in format defined in
    91  // the document #575623.
    92  func (s *Hash) WriteTo(w io.Writer) (int64, error) {
    93  	totalN := int64(0)
    94  	s.Rehash()
    95  
    96  	// Usage (ManifestFieldType: endValue)
    97  	{
    98  		n, err := 8, binary.Write(w, binary.LittleEndian, &s.Usage)
    99  		if err != nil {
   100  			return totalN, fmt.Errorf("unable to write field 'Usage': %w", err)
   101  		}
   102  		totalN += int64(n)
   103  	}
   104  
   105  	// Digest (ManifestFieldType: subStruct)
   106  	{
   107  		n, err := s.Digest.WriteTo(w)
   108  		if err != nil {
   109  			return totalN, fmt.Errorf("unable to write field 'Digest': %w", err)
   110  		}
   111  		totalN += int64(n)
   112  	}
   113  
   114  	return totalN, nil
   115  }
   116  
   117  // UsageSize returns the size in bytes of the value of field Usage
   118  func (s *Hash) UsageTotalSize() uint64 {
   119  	return 8
   120  }
   121  
   122  // DigestSize returns the size in bytes of the value of field Digest
   123  func (s *Hash) DigestTotalSize() uint64 {
   124  	return s.Digest.TotalSize()
   125  }
   126  
   127  // UsageOffset returns the offset in bytes of field Usage
   128  func (s *Hash) UsageOffset() uint64 {
   129  	return 0
   130  }
   131  
   132  // DigestOffset returns the offset in bytes of field Digest
   133  func (s *Hash) DigestOffset() uint64 {
   134  	return s.UsageOffset() + s.UsageTotalSize()
   135  }
   136  
   137  // Size returns the total size of the Hash.
   138  func (s *Hash) TotalSize() uint64 {
   139  	if s == nil {
   140  		return 0
   141  	}
   142  
   143  	var size uint64
   144  	size += s.UsageTotalSize()
   145  	size += s.DigestTotalSize()
   146  	return size
   147  }
   148  
   149  // PrettyString returns the content of the structure in an easy-to-read format.
   150  func (s *Hash) PrettyString(depth uint, withHeader bool, opts ...pretty.Option) string {
   151  	var lines []string
   152  	if withHeader {
   153  		lines = append(lines, pretty.Header(depth, "Hash", s))
   154  	}
   155  	if s == nil {
   156  		return strings.Join(lines, "\n")
   157  	}
   158  	// ManifestFieldType is endValue
   159  	lines = append(lines, pretty.SubValue(depth+1, "Usage", "", &s.Usage, opts...)...)
   160  	// ManifestFieldType is subStruct
   161  	lines = append(lines, pretty.SubValue(depth+1, "Digest", "", &s.Digest, opts...)...)
   162  	if depth < 2 {
   163  		lines = append(lines, "")
   164  	}
   165  	return strings.Join(lines, "\n")
   166  }
   167  
   168  // PrettyString returns the bits of the flags in an easy-to-read format.
   169  func (v Usage) PrettyString(depth uint, withHeader bool, opts ...pretty.Option) string {
   170  	return v.String()
   171  }
   172  
   173  // TotalSize returns the total size measured through binary.Size.
   174  func (v Usage) TotalSize() uint64 {
   175  	return uint64(binary.Size(v))
   176  }
   177  
   178  // WriteTo writes the Usage into 'w' in binary format.
   179  func (v Usage) WriteTo(w io.Writer) (int64, error) {
   180  	return int64(v.TotalSize()), binary.Write(w, binary.LittleEndian, v)
   181  }
   182  
   183  // ReadFrom reads the Usage from 'r' in binary format.
   184  func (v Usage) ReadFrom(r io.Reader) (int64, error) {
   185  	return int64(v.TotalSize()), binary.Read(r, binary.LittleEndian, v)
   186  }