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