Debian Codegraph @

smbios/decoder.go

raw symbols SCIP
1 // Copyright 2017-2018 DigitalOcean.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package smbios
16
17 import (
18 "bufio"
19 "bytes"
20 "encoding/binary"
21 "io"
22 )
23
24 const (
25 // headerLen is the length of the Header structure.
26 headerLen = 4
27
28 // typeEndOfTable indicates the end of a stream of Structures.
29 typeEndOfTable = 127
30 )
31
32 var (
33 // Byte slices used to help parsing string-sets.
34 null = []byte{0x00}
35 endStringSet = []byte{0x00, 0x00}
36 )
37
38 // A Decoder decodes Structures from a stream.
39 type Decoder struct {
40 br *bufio.Reader
41 b []byte
42 }
43
44 // Stream locates and opens a stream of SMBIOS data and the SMBIOS entry
45 // point from an operating system-specific location. The stream must be
46 // closed after decoding to free its resources.
47 //
48 // If no suitable location is found, an error is returned.
49 func Stream() (io.ReadCloser, EntryPoint, error) {
50 rc, ep, err := stream()
51 if err != nil {
52 return nil, nil, err
53 }
54
55 // The io.ReadCloser from stream could be any one of a number of types
56 // depending on the source of the SMBIOS stream information.
57 //
58 // To prevent the caller from potentially tampering with something dangerous
59 // like mmap'd memory by using a type assertion, we make the io.ReadCloser
60 // into an opaque and unexported type to prevent type assertion.
61 return &opaqueReadCloser{rc: rc}, ep, nil
62 }
63
64 // NewDecoder creates a Decoder which decodes Structures from the input stream.
65 func NewDecoder(r io.Reader) *Decoder {
66 return &Decoder{
67 br: bufio.NewReader(r),
68 b: make([]byte, 1024),
69 }
70 }
71
72 // Decode decodes Structures from the Decoder's stream until an End-of-table
73 // structure is found.
74 func (d *Decoder) Decode() ([]*Structure, error) {
75 var ss []*Structure
76
77 for {
78 s, err := d.next()
79 if err != nil {
80 return nil, err
81 }
82
83 // End-of-table structure indicates end of stream.
84 ss = append(ss, s)
85 if s.Header.Type == typeEndOfTable {
86 break
87 }
88 }
89
90 return ss, nil
91 }
92
93 // next decodes the next Structure from the stream.
94 func (d *Decoder) next() (*Structure, error) {
95 h, err := d.parseHeader()
96 if err != nil {
97 return nil, err
98 }
99
100 // Length of formatted section is length specified by header, minus
101 // the length of the header itself.
102 l := int(h.Length) - headerLen
103 fb, err := d.parseFormatted(l)
104 if err != nil {
105 return nil, err
106 }
107
108 ss, err := d.parseStrings()
109 if err != nil {
110 return nil, err
111 }
112
113 return &Structure{
114 Header: *h,
115 Formatted: fb,
116 Strings: ss,
117 }, nil
118 }
119
120 // parseHeader parses a Structure's Header from the stream.
121 func (d *Decoder) parseHeader() (*Header, error) {
122 if _, err := io.ReadFull(d.br, d.b[:headerLen]); err != nil {
123 return nil, err
124 }
125
126 return &Header{
127 Type: d.b[0],
128 Length: d.b[1],
129 Handle: binary.LittleEndian.Uint16(d.b[2:4]),
130 }, nil
131 }
132
133 // parseFormatted parses a Structure's formatted data from the stream.
134 func (d *Decoder) parseFormatted(l int) ([]byte, error) {
135 // Guard against malformed input length.
136 if l < 0 {
137 return nil, io.ErrUnexpectedEOF
138 }
139 if l == 0 {
140 // No formatted data.
141 return nil, nil
142 }
143
144 if _, err := io.ReadFull(d.br, d.b[:l]); err != nil {
145 return nil, err
146 }
147
148 // Make a copy to free up the internal buffer.
149 fb := make([]byte, len(d.b[:l]))
150 copy(fb, d.b[:l])
151
152 return fb, nil
153 }
154
155 // parseStrings parses a Structure's strings from the stream, if they
156 // are present.
157 func (d *Decoder) parseStrings() ([]string, error) {
158 term, err := d.br.Peek(2)
159 if err != nil {
160 return nil, err
161 }
162
163 // If no string-set present, discard delimeter and end parsing.
164 if bytes.Equal(term, endStringSet) {
165 if _, err := d.br.Discard(2); err != nil {
166 return nil, err
167 }
168
169 return nil, nil
170 }
171
172 var ss []string
173 for {
174 s, more, err := d.parseString()
175 if err != nil {
176 return nil, err
177 }
178
179 // When final string is received, end parse loop.
180 ss = append(ss, s)
181 if !more {
182 break
183 }
184 }
185
186 return ss, nil
187 }
188
189 // parseString parses a single string from the stream, and returns if
190 // any more strings are present.
191 func (d *Decoder) parseString() (str string, more bool, err error) {
192 // We initially read bytes because it's more efficient to manipulate bytes
193 // and allocate a string once we're all done.
194 //
195 // Strings are null-terminated.
196 raw, err := d.br.ReadBytes(0x00)
197 if err != nil {
198 return "", false, err
199 }
200
201 b := bytes.TrimRight(raw, "\x00")
202
203 peek, err := d.br.Peek(1)
204 if err != nil {
205 return "", false, err
206 }
207
208 if !bytes.Equal(peek, null) {
209 // Next byte isn't null; more strings to come.
210 return string(b), true, nil
211 }
212
213 // If two null bytes appear in a row, end of string-set.
214 // Discard the null and indicate no more strings.
215 if _, err := d.br.Discard(1); err != nil {
216 return "", false, err
217 }
218
219 return string(b), false, nil
220 }
221
222 var _ io.ReadCloser = &opaqueReadCloser{}
223
224 // An opaqueReadCloser masks the type of the underlying io.ReadCloser to
225 // prevent type assertions.
226 type opaqueReadCloser struct {
227 rc io.ReadCloser
228 }
229
230 func (rc *opaqueReadCloser) Read(b []byte) (int, error) { return rc.rc.Read(b) }
231 func (rc *opaqueReadCloser) Close() error { return rc.rc.Close() }