github.com/linuxboot/fiano@v1.2.0/pkg/intel/metadata/bg/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 -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 // 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 return totalN, nil 69 } 70 71 // RehashRecursive calls Rehash (see below) recursively. 72 func (s *StructInfo) RehashRecursive() { 73 s.Rehash() 74 } 75 76 // Rehash sets values which are calculated automatically depending on the rest 77 // data. It is usually about the total size field of an element. 78 func (s *StructInfo) Rehash() { 79 } 80 81 // WriteTo writes the StructInfo into 'w' in format defined in 82 // the document #575623. 83 func (s *StructInfo) WriteTo(w io.Writer) (int64, error) { 84 totalN := int64(0) 85 s.Rehash() 86 87 // ID (ManifestFieldType: arrayStatic) 88 { 89 n, err := 8, binary.Write(w, binary.LittleEndian, s.ID[:]) 90 if err != nil { 91 return totalN, fmt.Errorf("unable to write field 'ID': %w", err) 92 } 93 totalN += int64(n) 94 } 95 96 // Version (ManifestFieldType: endValue) 97 { 98 n, err := 1, binary.Write(w, binary.LittleEndian, &s.Version) 99 if err != nil { 100 return totalN, fmt.Errorf("unable to write field 'Version': %w", err) 101 } 102 totalN += int64(n) 103 } 104 105 return totalN, nil 106 } 107 108 // IDSize returns the size in bytes of the value of field ID 109 func (s *StructInfo) IDTotalSize() uint64 { 110 return 8 111 } 112 113 // VersionSize returns the size in bytes of the value of field Version 114 func (s *StructInfo) VersionTotalSize() uint64 { 115 return 1 116 } 117 118 // IDOffset returns the offset in bytes of field ID 119 func (s *StructInfo) IDOffset() uint64 { 120 return 0 121 } 122 123 // VersionOffset returns the offset in bytes of field Version 124 func (s *StructInfo) VersionOffset() uint64 { 125 return s.IDOffset() + s.IDTotalSize() 126 } 127 128 // Size returns the total size of the StructInfo. 129 func (s *StructInfo) TotalSize() uint64 { 130 if s == nil { 131 return 0 132 } 133 134 var size uint64 135 size += s.IDTotalSize() 136 size += s.VersionTotalSize() 137 return size 138 } 139 140 // PrettyString returns the content of the structure in an easy-to-read format. 141 func (s *StructInfo) PrettyString(depth uint, withHeader bool, opts ...pretty.Option) string { 142 var lines []string 143 if withHeader { 144 lines = append(lines, pretty.Header(depth, "Struct Info", s)) 145 } 146 if s == nil { 147 return strings.Join(lines, "\n") 148 } 149 // ManifestFieldType is arrayStatic 150 lines = append(lines, pretty.SubValue(depth+1, "ID", "", &s.ID, opts...)...) 151 // ManifestFieldType is endValue 152 lines = append(lines, pretty.SubValue(depth+1, "Version", "", &s.Version, opts...)...) 153 if depth < 2 { 154 lines = append(lines, "") 155 } 156 return strings.Join(lines, "\n") 157 }