| 1 | // |
| 2 | // Copyright (c) SAS Institute Inc. |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
| 16 | |
| 17 | package comdoc |
| 18 | |
| 19 | import ( |
| 20 | "unicode/utf16" |
| 21 | ) |
| 22 | |
| 23 | type SecID int32 |
| 24 | type DirType uint8 |
| 25 | type Color uint8 |
| 26 | |
| 27 | const ( |
| 28 | SecIDFree SecID = -1 |
| 29 | SecIDEndOfChain SecID = -2 |
| 30 | SecIDSAT SecID = -3 |
| 31 | SecIDMSAT SecID = -4 |
| 32 | ) |
| 33 | |
| 34 | const ( |
| 35 | DirEmpty DirType = 0 |
| 36 | DirStorage DirType = 1 |
| 37 | DirStream DirType = 2 |
| 38 | DirRoot DirType = 5 |
| 39 | ) |
| 40 | |
| 41 | const ( |
| 42 | Red Color = 0 |
| 43 | Black Color = 1 |
| 44 | ) |
| 45 | |
| 46 | const byteOrderMarker uint16 = 0xfffe |
| 47 | const msatInHeader = 109 |
| 48 | |
| 49 | var fileMagic = []byte{0xd0, 0xcf, 0x11, 0xe0, 0xa1, 0xb1, 0x1a, 0xe1} |
| 50 | |
| 51 | // Raw CDF file header |
| 52 | type Header struct { |
| 53 | Magic [8]byte |
| 54 | UID [16]byte |
| 55 | Revision uint16 |
| 56 | Version uint16 |
| 57 | ByteOrder uint16 |
| 58 | SectorSize uint16 // power of 2 |
| 59 | ShortSectorSize uint16 // power of 2 |
| 60 | Reserved1 [6]byte |
| 61 | DirSectorCount uint32 // undocumented? |
| 62 | SATSectors uint32 |
| 63 | DirNextSector SecID |
| 64 | Reserved2 uint32 |
| 65 | MinStdStreamSize uint32 |
| 66 | SSATNextSector SecID |
| 67 | SSATSectorCount uint32 |
| 68 | MSATNextSector SecID |
| 69 | MSATSectorCount uint32 |
| 70 | MSAT [msatInHeader]SecID |
| 71 | } |
| 72 | |
| 73 | // Raw CDF directory entry |
| 74 | type RawDirEnt struct { |
| 75 | NameRunes [32]uint16 |
| 76 | NameLength uint16 |
| 77 | Type DirType |
| 78 | Color Color |
| 79 | LeftChild int32 |
| 80 | RightChild int32 |
| 81 | StorageRoot int32 |
| 82 | UID [16]byte |
| 83 | UserFlags uint32 |
| 84 | CreateTime uint64 |
| 85 | ModifyTime uint64 |
| 86 | NextSector SecID |
| 87 | StreamSize uint32 |
| 88 | _ uint32 |
| 89 | } |
| 90 | |
| 91 | // Return the UTF8 name of this entry |
| 92 | func (e RawDirEnt) Name() string { |
| 93 | used := e.NameLength/2 - 1 |
| 94 | if e.Type == DirEmpty || used > 32 { |
| 95 | return "" |
| 96 | } |
| 97 | return string(utf16.Decode(e.NameRunes[:used])) |
| 98 | } |
| 99 | |
| 100 | // Parsed CDF directory entry |
| 101 | type DirEnt struct { |
| 102 | RawDirEnt |
| 103 | // Index into the directory stream holding this entry |
| 104 | Index int |
| 105 | name string |
| 106 | } |
| 107 | |
| 108 | // Return the UTF8 name of this entry |
| 109 | func (e DirEnt) Name() string { |
| 110 | return e.name |
| 111 | } |