Debian Codegraph @

internal/cdf/cdf.go

raw symbols SCIP
1 // Package cdf implements parsing of CDF (OLE2) files. It is greatly inspired
2 // by src/readcdf.c from libmagic. One difference is this implementation is
3 // permissive of truncated inputs. See readLimit in mimetype.go for the
4 // reason why truncated inputs need to be handled.
5 // http://sc.openoffice.org/compdocfileformat.pdf
6 package cdf
7
8 import (
9 "bytes"
10 "encoding/binary"
11
12 "github.com/gabriel-vasile/mimetype/internal/scan"
13 )
14
15 type CDFType int8
16
17 const (
18 CDFTypeGeneric CDFType = iota
19 CDFTypeInstaller
20 CDFTypeDoc
21 CDFTypePpt
22 CDFTypeXls
23 CDFTypeMsg
24 )
25
26 // Detect parses raw as a CDF (OLE2) compound file and returns the document type
27 // it contains. It returns CDFTypeGeneric for input that is not a CDF file or
28 // whose type cannot be narrowed down.
29 func Detect(raw []byte) CDFType {
30 if len(raw) < 512 {
31 return CDFTypeGeneric
32 }
33 var c cdf
34 if !parse(raw, &c) {
35 return CDFTypeGeneric
36 }
37 return c.detect()
38 }
39
40 // cdf holds everything we need from a CDF file to do detection.
41 type cdf struct {
42 data []byte
43 secSize int
44 shortSecSize int
45 minStdStream uint32
46 satSecs int32s // list of SAT sector ids; usually a sub-slice of raw input
47 satEntries int // number of valid SAT entries reachable through satSecs
48 firstSSAT int32
49 dirRaw []byte // directory stream bytes (entries are decoded on demand)
50 sst []byte // short-stream pool (root storage's stream)
51 sstBuilt bool // whether sst was already loaded (it is loaded lazily)
52 rootStreamFirst int32 // first sector of the root storage short-stream pool
53 rootStreamSize uint32 // size of the root storage short-stream pool
54 rootStorageUUID []byte
55 }
56
57 // parse reads the entire on-disk structure required for type detection. It
58 // returns true on success and false if the header does not look like a CDF file.
59 // Truncated or partially malformed bodies are tolerated: sector reads degrade
60 // to whatever could be collected so detection can still succeed from partial data.
61 func parse(raw []byte, c *cdf) bool {
62 if len(raw) < 512 || binary.LittleEndian.Uint64(raw) != cdfMagic {
63 return false
64 }
65 secP2 := binary.LittleEndian.Uint16(raw[30:32])
66 shortP2 := binary.LittleEndian.Uint16(raw[32:34])
67 if secP2 > 20 || shortP2 > 20 {
68 return false
69 }
70 c.data = raw
71 c.secSize = 1 << secP2
72 c.shortSecSize = 1 << shortP2
73 c.minStdStream = binary.LittleEndian.Uint32(raw[56:60])
74 if c.secSize < dirEntrySize {
75 return false
76 }
77 firstDirSec := readSecID(raw[48:52])
78 c.firstSSAT = readSecID(raw[60:64])
79 firstMSAT := readSecID(raw[68:72])
80 nMSAT := binary.LittleEndian.Uint32(raw[72:76])
81 masterSAT := int32s{b: raw[76 : 76+4*masterSATSize]}
82
83 c.buildSAT(masterSAT, firstMSAT, nMSAT)
84 c.dirRaw = c.readLong(firstDirSec, 0)
85
86 c.rootStreamFirst = -1
87 var d dirEntry
88 for i, n := 0, c.dirLen(); i < n; i++ {
89 c.dirAt(i, &d)
90 if d.typ != dirTypeRootStorage || d.streamFirst < 0 {
91 continue
92 }
93 c.rootStorageUUID = d.storageUUID[:]
94 // Record where the short-stream pool lives; it is loaded lazily by
95 // shortStream the first time a short stream is actually read.
96 c.rootStreamFirst = d.streamFirst
97 c.rootStreamSize = d.size
98 break
99 }
100 return true
101 }
102
103 func (c *cdf) detect() CDFType {
104 for _, name := range []string{"\x05SummaryInformation", "\x05DocumentSummaryInformation"} {
105 if t, ok := c.detectFromSummary(name); ok {
106 return t
107 }
108 }
109 var d dirEntry
110 for i, n := 0, c.dirLen(); i < n; i++ {
111 c.dirAt(i, &d)
112 if t, ok := lookupSection(d.nameBytes(), d.typ); ok {
113 return t
114 }
115 }
116 return CDFTypeGeneric
117 }
118
119 // detectFromSummary inspects a (Doc)SummaryInformation stream and tries to
120 // derive a CDFType from the root-storage CLSID, the property NameOfApplication,
121 // and finally the names of sibling user streams.
122 func (c *cdf) detectFromSummary(streamName string) (CDFType, bool) {
123 if c.rootStorageUUID != nil && bytes.Equal(c.rootStorageUUID, msiCLSID) {
124 return CDFTypeInstaller, true
125 }
126 raw, ok := c.userStream(streamName)
127 if !ok {
128 return CDFTypeGeneric, false
129 }
130 if app := summaryAppName(raw); len(app) > 0 {
131 if t, ok := lookupSubstring(app, app2type); ok {
132 return t, true
133 }
134 }
135 for i, n := 0, c.dirLen(); i < n; i++ {
136 var d dirEntry
137 c.dirAt(i, &d)
138 if d.nameLen == 0 {
139 continue
140 }
141 if t, ok := lookupSubstring(d.nameBytes(), name2type); ok {
142 return t, true
143 }
144 }
145 return CDFTypeGeneric, true
146 }
147
148 const (
149 cdfMagic uint64 = 0xE11AB1A1E011CFD0
150
151 dirTypeUserStorage = 1
152 dirTypeUserStream = 2
153 dirTypeRootStorage = 5
154
155 dirEntrySize = 128
156 masterSATSize = 109 // first 109 SAT secids live in the file header
157 )
158
159 // dirEntry is a single CDF directory record. The UTF-16LE name is pre-decoded
160 // into an inline ASCII buffer at parse time, avoiding a per-entry heap
161 // allocation while keeping comparisons trivial. CDF names are at most 32
162 // UTF-16 code units, so 32 bytes always suffice.
163 type dirEntry struct {
164 name [32]byte
165 nameLen uint8
166 typ uint8
167 streamFirst int32
168 size uint32
169 storageUUID [16]byte
170 }
171
172 // nameBytes returns the decoded ASCII name without copying.
173 func (d *dirEntry) nameBytes() []byte { return d.name[:d.nameLen] }
174
175 func (c *cdf) ssatAt(i int32) int32 {
176 for sid := c.firstSSAT; sid >= 0; {
177 if int(sid) >= c.satLen() {
178 break // SAT is truncated; stop collecting
179 }
180 buf, ok := c.sector(sid)
181 if !ok {
182 break
183 }
184 lbuf := int32(len(buf) / 4) //nolint:gosec // anything divided by 4 fits int32
185 if i < lbuf {
186 return int32(binary.LittleEndian.Uint32(buf[4*i:])) //nolint:gosec // intentional two's-complement reinterpretation of a sector id
187 }
188 i -= lbuf
189 sid = c.satAt(sid)
190 }
191 return -1
192 }
193
194 // shortStream returns the root storage short-stream pool, loading it on first
195 // use. Detection often finishes (e.g. via the root CLSID or a long-stream
196 // summary) without ever reading a short stream, so building this eagerly would
197 // be wasted work.
198 func (c *cdf) shortStream() []byte {
199 if !c.sstBuilt {
200 c.sstBuilt = true
201 if c.rootStreamFirst >= 0 {
202 c.sst = c.readLong(c.rootStreamFirst, c.rootStreamSize)
203 }
204 }
205 return c.sst
206 }
207
208 // int32s works like a slice of LE int32 and is backed by a slice of bytes.
209 // int32s could very well be type int32s []byte, but that would mean
210 // len function can be called on it. We don't want that, we always want to use
211 // the len method.
212 type int32s struct {
213 b []byte
214 }
215
216 func (b int32s) at(i int) int32 {
217 //nolint:gosec // intentional two's-complement reinterpretation of a sector id
218 return int32(binary.LittleEndian.Uint32(b.b[4*i:]))
219 }
220 func (b int32s) len() int {
221 return len(b.b) / 4
222 }
223
224 // readSecID reinterprets four little-endian bytes as a signed sector id.
225 // Every 32-bit pattern is a valid id (values >= 0 are sector numbers,
226 // negatives are CDF sentinels such as -2 end-of-chain), so the conversion is
227 // an intentional two's-complement reinterpretation rather than an overflow.
228 func readSecID(b []byte) int32 {
229 return int32(binary.LittleEndian.Uint32(b)) //nolint:gosec // intentional two's-complement reinterpretation
230 }
231
232 // satLen is the number of sector ids reachable through the SAT.
233 func (c *cdf) satLen() int { return c.satEntries }
234
235 // satAt returns the i-th sector id from the SAT. Callers must ensure
236 // i < satLen(). The SAT is not materialized; the entry is fetched directly
237 // from the input by translating i into (SAT sector index, entry offset).
238 func (c *cdf) satAt(i int32) int32 {
239 perSec := c.secSize / 4
240 secIdx := int(i) / perSec
241 entryIdx := int(i) % perSec
242 secID := c.satSecs.at(secIdx)
243 off := c.secSize*(1+int(secID)) + 4*entryIdx
244 return readSecID(c.data[off:])
245 }
246
247 // sector returns the bytes of long sector secid. If the file is truncated
248 // inside the requested sector the result is the available bytes (no padding).
249 // If the sector starts past EOF or secid is negative, then ok is false.
250 func (c *cdf) sector(secid int32) (_ []byte, ok bool) {
251 if secid < 0 {
252 return nil, false
253 }
254 off := int64(c.secSize) * (1 + int64(secid))
255 if off >= int64(len(c.data)) {
256 return nil, false
257 }
258 // The returned sector might be truncated,
259 // but we still return it as best effort.
260 end := min(off+int64(c.secSize), int64(len(c.data)))
261 // If not even one int32 fits, then fail.
262 if end-off < 4 {
263 return nil, false
264 }
265 return c.data[off:end], true
266 }
267
268 func (c *cdf) sectorIDs(secid int32) (int32s, bool) {
269 buf, ok := c.sector(secid)
270 if !ok {
271 return int32s{}, ok
272 }
273 return int32s{b: buf}, true
274 }
275
276 // buildSAT records the list of SAT sector ids from the master-SAT (header)
277 // plus any extension blocks chained via firstMSAT. The SAT itself is not
278 // materialized: satAt computes the requested entry directly from c.data via
279 // satSecs. In the common case (no extension chain) satSecs is a zero-copy
280 // sub-slice of the input header.
281 func (c *cdf) buildSAT(masterSAT int32s, firstMSAT int32, nMSAT uint32) {
282 // Fast path: no extension chain. masterSAT is already a sub-slice of raw
283 // input; reuse it directly.
284 if firstMSAT < 0 || nMSAT == 0 {
285 c.satSecs = masterSAT
286 c.satEntries = c.computeSATLen()
287 return
288 }
289
290 // Slow path: gather sector ids from the header plus the extension chain
291 // into a fresh buffer. Even here we only allocate space for ids (4 bytes
292 // each), not the full SAT contents.
293 maxIDs := len(c.data)/c.secSize + 1
294 buf := make([]byte, 0, 4*masterSATSize)
295 for i := 0; i < masterSAT.len(); i++ {
296 if masterSAT.at(i) < 0 {
297 break
298 }
299 buf = append(buf, masterSAT.b[4*i:4*i+4]...)
300 }
301 perSec := c.secSize/4 - 1
302 mid := firstMSAT
303 chain:
304 for j := uint32(0); j < nMSAT && mid >= 0; j++ {
305 msa, ok := c.sectorIDs(mid)
306 if !ok {
307 break
308 }
309 for k := 0; k < perSec; k++ {
310 if k >= msa.len() || msa.at(k) < 0 {
311 break chain
312 }
313 buf = append(buf, msa.b[4*k:4*k+4]...)
314 if len(buf)/4 > maxIDs {
315 break chain // cyclic MSAT chain; stop allocating
316 }
317 }
318 if perSec >= msa.len() {
319 break // no next-MSAT pointer available
320 }
321 mid = msa.at(perSec)
322 }
323 c.satSecs = int32s{b: buf}
324 c.satEntries = c.computeSATLen()
325 }
326
327 // computeSATLen walks satSecs and counts how many SAT entries are actually
328 // reachable in c.data, stopping at the first sentinel id or sector that is not
329 // fully present in the file.
330 func (c *cdf) computeSATLen() int {
331 perSec := c.secSize / 4
332 total := 0
333 for i := 0; i < c.satSecs.len(); i++ {
334 sec := c.satSecs.at(i)
335 if sec < 0 {
336 break
337 }
338 off := int64(c.secSize) * (1 + int64(sec))
339 if off >= int64(len(c.data)) {
340 break
341 }
342 avail := int64(len(c.data)) - off
343 if avail >= int64(c.secSize) {
344 total += perSec
345 continue
346 }
347 total += int(avail / 4)
348 break
349 }
350 return total
351 }
352
353 // readLong reads a long-sector chain starting at sid. If length > 0 the
354 // result is truncated to that many bytes. On truncation or any other failure
355 // it returns whatever sectors were readable.
356 func (c *cdf) readLong(sid int32, length uint32) []byte {
357 // Fast path: when the chain is a single physically contiguous run of
358 // sectors (the common case for the directory and summary streams) the data
359 // is already laid out sequentially in the input, so return a sub-slice of
360 // it instead of allocating a buffer and copying every sector.
361 if sid >= 0 {
362 maxSec := len(c.data)/c.secSize + 1
363 n, s := 0, sid
364 contiguous := true
365 for s >= 0 {
366 if int(s) >= c.satLen() {
367 break // SAT truncated; what remains is still contiguous
368 }
369 n++
370 if n > maxSec {
371 contiguous = false // cyclic chain; let the slow path guard it
372 break
373 }
374 next := c.satAt(s)
375 if next >= 0 && int64(next) != int64(s)+1 {
376 contiguous = false
377 break
378 }
379 s = next
380 }
381 if contiguous {
382 off64 := int64(c.secSize) * (1 + int64(sid))
383 if off64 >= int64(len(c.data)) {
384 return nil
385 }
386 end64 := min(off64+int64(n)*int64(c.secSize), int64(len(c.data)))
387 out := c.data[off64:end64]
388 if length > 0 && int64(length) < int64(len(out)) {
389 out = out[:length]
390 }
391 return out
392 }
393 }
394
395 // Slow path: gather a fragmented chain into a fresh buffer. Real-world
396 // writers (MSI builders, edited Office documents) routinely produce
397 // non-contiguous directory and stream chains, so this fallback is required
398 // for correct detection on those files.
399 maxBytes := len(c.data)
400 out := make([]byte, 0, c.secSize)
401 for sid >= 0 {
402 if int(sid) >= c.satLen() {
403 break // SAT truncated; return what we have
404 }
405 buf, ok := c.sector(sid)
406 if !ok {
407 break
408 }
409 out = append(out, buf...)
410 if len(out) >= maxBytes {
411 break // chain longer than the file: cyclic SAT, stop
412 }
413 sid = c.satAt(sid)
414 }
415 if length > 0 && int64(length) < int64(len(out)) {
416 out = out[:length]
417 }
418 return out
419 }
420
421 // readShort reads a short-sector chain at sid by indexing into the short-stream
422 // pool. On truncation or if the pool is unavailable it returns whatever was
423 // readable (possibly nil).
424 func (c *cdf) readShort(sid int32, length uint32) []byte {
425 sst := c.shortStream()
426 if sst == nil {
427 return nil
428 }
429 // TODO: anyway to avoid allocating and copying the bytes?
430 out := make([]byte, 0, c.shortSecSize)
431 for sid >= 0 {
432 off64 := int64(sid) * int64(c.shortSecSize)
433 if off64+int64(c.shortSecSize) > int64(len(sst)) {
434 break // short-stream pool truncated or sid out of range
435 }
436 off := int(off64)
437 out = append(out, sst[off:off+c.shortSecSize]...)
438 if len(out) >= len(sst) {
439 break // chain longer than the pool: cyclic SSAT, stop
440 }
441 sid = c.ssatAt(sid)
442 }
443 if length > 0 && int64(length) < int64(len(out)) {
444 out = out[:length]
445 }
446 return out
447 }
448
449 // readChain dispatches to the long or short reader depending on stream size.
450 func (c *cdf) readChain(sid int32, length uint32) []byte {
451 if length < c.minStdStream && c.rootStreamFirst >= 0 {
452 return c.readShort(sid, length)
453 }
454 return c.readLong(sid, length)
455 }
456
457 // dirLen returns the number of directory entries in dirRaw.
458 func (c *cdf) dirLen() int { return len(c.dirRaw) / dirEntrySize }
459
460 // dirAt decodes the i-th directory entry into *out. Callers must ensure
461 // i < dirLen(). The UTF-16LE name is decoded into out.name, ASCII-style,
462 // stopping at the first NUL.
463 func (c *cdf) dirAt(i int, out *dirEntry) {
464 raw := c.dirRaw[i*dirEntrySize:]
465 nameLen := min(int(binary.LittleEndian.Uint16(raw[64:])), 64)
466 k := uint8(0)
467 for j := 0; j < nameLen/2; j++ {
468 // Names are ASCII; keep the low byte of each little-endian UTF-16
469 // code unit and stop at the first NUL.
470 lo, hi := raw[2*j], raw[2*j+1]
471 if lo == 0 && hi == 0 {
472 break
473 }
474 out.name[k] = lo
475 k++
476 }
477 out.nameLen = k
478 out.typ = raw[66]
479 out.streamFirst = readSecID(raw[116:120])
480 out.size = binary.LittleEndian.Uint32(raw[120:])
481 copy(out.storageUUID[:], raw[80:96])
482 }
483
484 // userStream finds a user stream by name and returns its bytes.
485 func (c *cdf) userStream(name string) ([]byte, bool) {
486 var d dirEntry
487 for i, n := 0, c.dirLen(); i < n; i++ {
488 c.dirAt(i, &d)
489 if d.typ == dirTypeUserStream && string(d.nameBytes()) == name {
490 buf := c.readChain(d.streamFirst, d.size)
491 if buf == nil {
492 return nil, false
493 }
494 return buf, true
495 }
496 }
497 return nil, false
498 }
499
500 const (
501 propIDNameOfApplication = 0x12
502
503 typeMask = 0x0fff
504 typeVector = 0x1000
505 typeStringASCII = 0x1e
506 typeStringWide = 0x1f
507
508 sectionDeclOffset = 0x1c // section declaration in property-set header
509 )
510
511 // summaryAppName parses a (Doc)SummaryInformation stream and returns the
512 // value of property NameOfApplication (0x12) as printable ASCII, or nil if
513 // not present or the stream is malformed. This is the only summary property
514 // the detection logic ever consults.
515 func summaryAppName(stream []byte) []byte {
516 if len(stream) < sectionDeclOffset+20 {
517 return nil
518 }
519 sdOff := binary.LittleEndian.Uint32(stream[sectionDeclOffset+16:])
520 if uint64(sdOff)+8 > uint64(len(stream)) {
521 return nil
522 }
523 section := stream[sdOff:]
524 shLen := binary.LittleEndian.Uint32(section[0:])
525 nProps := binary.LittleEndian.Uint32(section[4:])
526 if uint64(shLen) > uint64(len(section)) || nProps > 1<<16 || 8+8*nProps > shLen {
527 return nil
528 }
529 for i := uint32(0); i < nProps; i++ {
530 base := 8 + 8*i
531 id := binary.LittleEndian.Uint32(section[base:])
532 if id != propIDNameOfApplication {
533 continue
534 }
535 off := binary.LittleEndian.Uint32(section[base+4:])
536 if uint64(off)+8 > uint64(shLen) {
537 return nil
538 }
539 typ := binary.LittleEndian.Uint32(section[off:])
540 if typ&typeVector != 0 {
541 return nil
542 }
543 step := uint32(0)
544 switch typ & typeMask {
545 case typeStringASCII:
546 step = 1
547 case typeStringWide:
548 step = 2
549 default:
550 return nil
551 }
552 slen := binary.LittleEndian.Uint32(section[off+4:])
553 start := uint64(off) + 8
554 end := start + uint64(slen)*uint64(step)
555 if end > uint64(shLen) {
556 return nil
557 }
558 return printableLowBytes(section[start:end], int(step))
559 }
560 return nil
561 }
562
563 // printableLowBytes copies the printable low byte of each step-byte unit
564 // in b, stopping at the first NUL.
565 func printableLowBytes(b []byte, step int) []byte {
566 out := make([]byte, 0, len(b)/step)
567 for i := 0; i+step <= len(b); i += step {
568 c := b[i]
569 if c == 0 {
570 break
571 }
572 if c >= 0x20 && c < 0x7f {
573 out = append(out, c)
574 }
575 }
576 return out
577 }
578
579 // pattern is a case-insensitive substring → CDFType mapping. Entries are
580 // tested in order; first match wins. needle is stored upper-cased so it can be
581 // matched case-insensitively by scan.Bytes.Search with scan.IgnoreCase.
582 type pattern struct {
583 needle []byte
584 typ CDFType
585 }
586
587 // app2type maps NameOfApplication values to CDFTypes.
588 // Mirrors app2mime[] in libmagic. Needles are upper-cased for case-insensitive
589 // matching via scan.IgnoreCase.
590 var app2type = []pattern{
591 {[]byte("WORD"), CDFTypeDoc},
592 {[]byte("EXCEL"), CDFTypeXls},
593 {[]byte("POWERPOINT"), CDFTypePpt},
594 {[]byte("ADVANCED INSTALLER"), CDFTypeInstaller},
595 {[]byte("INSTALLSHIELD"), CDFTypeInstaller},
596 {[]byte("MICROSOFT PATCH COMPILER"), CDFTypeInstaller},
597 {[]byte("NANT"), CDFTypeInstaller},
598 {[]byte("WINDOWS INSTALLER"), CDFTypeInstaller},
599 }
600
601 // name2type maps directory entry names to CDFTypes.
602 // Mirrors name2mime[] in libmagic. Needles are upper-cased for case-insensitive
603 // matching via scan.IgnoreCase.
604 var name2type = []pattern{
605 {[]byte("BOOK"), CDFTypeXls},
606 {[]byte("WORKBOOK"), CDFTypeXls},
607 {[]byte("WORDDOCUMENT"), CDFTypeDoc},
608 {[]byte("POWERPOINT"), CDFTypePpt},
609 {[]byte("DIGITALSIGNATURE"), CDFTypeInstaller},
610 }
611
612 // lookupSubstring returns the CDFType for the first entry in t whose needle
613 // is a case-insensitive substring of v. Mirrors C's strcasestr semantics
614 // under the C locale. It allocates nothing: scan.IgnoreCase matches the
615 // upper-cased needle against input of either case.
616 func lookupSubstring(v []byte, t []pattern) (CDFType, bool) {
617 s := scan.Bytes(v)
618 for _, p := range t {
619 if i, _ := s.Search(p.needle, scan.IgnoreCase); i != -1 {
620 return p.typ, true
621 }
622 }
623 return CDFTypeGeneric, false
624 }
625
626 // msiCLSID is the Microsoft Installer root-storage CLSID, in on-disk byte
627 // order (cdf_directory_t.d_storage_uuid stores two little-endian uint64s).
628 var msiCLSID = []byte{
629 0x84, 0x10, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00,
630 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46,
631 }
632
633 // section is a (directory entry name, type) → CDFType mapping.
634 type section struct {
635 name string
636 typ uint8
637 cdf CDFType
638 }
639
640 // sectionTypes maps distinctive directory entries to CDFTypes — a flattened
641 // equivalent of sectioninfo[] in libmagic. Used as a fallback when no
642 // SummaryInformation stream is present. A slice (rather than a map) lets
643 // lookupSection compare entry names without allocating a string key.
644 var sectionTypes = []section{
645 // libmagic uses application/encrypted, but that is not a registered media type.
646 // For now, we skip identifying that and fall-back on CDFTypeGeneric
647 // {"EncryptedPackage", dirTypeUserStream, CDFTypeEncrypted},
648 // {"EncryptedSummary", dirTypeUserStream, CDFTypeEncrypted},
649 {"Book", dirTypeUserStream, CDFTypeXls},
650 {"Workbook", dirTypeUserStream, CDFTypeXls},
651 {"WordDocument", dirTypeUserStream, CDFTypeDoc},
652 {"PowerPoint Document", dirTypeUserStream, CDFTypePpt},
653 {"__properties_version1.0", dirTypeUserStream, CDFTypeMsg},
654 {"__recip_version1.0_#00000000", dirTypeUserStorage, CDFTypeMsg},
655 }
656
657 // lookupSection returns the CDFType for a directory entry whose name and type
658 // match a sectionTypes entry exactly. The string(name) == comparison is
659 // optimized by the compiler to avoid allocating.
660 func lookupSection(name []byte, typ uint8) (CDFType, bool) {
661 for _, s := range sectionTypes {
662 if s.typ == typ && string(name) == s.name {
663 return s.cdf, true
664 }
665 }
666 return CDFTypeGeneric, false
667 }