github.com/anchore/syft@v1.4.2-0.20240516191711-1bec1fc5d397/syft/format/internal/cyclonedxutil/decoder.go (about) 1 package cyclonedxutil 2 3 import ( 4 "fmt" 5 "io" 6 7 "github.com/CycloneDX/cyclonedx-go" 8 9 "github.com/anchore/syft/syft/format/internal/stream" 10 ) 11 12 type Decoder struct { 13 format cyclonedx.BOMFileFormat 14 } 15 16 func NewDecoder(format cyclonedx.BOMFileFormat) Decoder { 17 return Decoder{ 18 format: format, 19 } 20 } 21 22 func (d Decoder) Decode(r io.Reader) (*cyclonedx.BOM, error) { 23 reader, err := stream.SeekableReader(r) 24 if err != nil { 25 return nil, err 26 } 27 28 doc := &cyclonedx.BOM{ 29 Components: &[]cyclonedx.Component{}, 30 } 31 if _, err := reader.Seek(0, io.SeekStart); err != nil { 32 return nil, fmt.Errorf("unable to seek to start of CycloneDX SBOM: %w", err) 33 } 34 35 err = cyclonedx.NewBOMDecoder(reader, d.format).Decode(doc) 36 if err != nil { 37 return nil, err 38 } 39 40 return doc, nil 41 }