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