| 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 | // Microsoft Compound Document File |
| 18 | // Reference: https://www.openoffice.org/sc/compdocfileformat.pdf |
| 19 | // ERRATA: The above document says the 0th sector is always 512 bytes into the |
| 20 | // file. This is not correct. If SectorSize > 512 bytes then the 0th sector is |
| 21 | // SectorSize bytes into the file. |
| 22 | package comdoc |
| 23 | |
| 24 | import ( |
| 25 | "bytes" |
| 26 | "encoding/binary" |
| 27 | "errors" |
| 28 | "io" |
| 29 | "os" |
| 30 | ) |
| 31 | |
| 32 | // CDF file open for reading or writing |
| 33 | type ComDoc struct { |
| 34 | File io.ReaderAt |
| 35 | Header *Header |
| 36 | SectorSize int |
| 37 | ShortSectorSize int |
| 38 | FirstSector int64 |
| 39 | // MSAT is a list of sector IDs holding a SAT |
| 40 | MSAT []SecID |
| 41 | // SAT is a table where the index is the sector ID and the value is a pointer to the next sector ID in the same stream |
| 42 | SAT []SecID |
| 43 | SSAT []SecID |
| 44 | Files []DirEnt |
| 45 | |
| 46 | sectorBuf []byte |
| 47 | changed bool |
| 48 | rootStorage int // index into files |
| 49 | rootFiles []int // index into Files |
| 50 | msatList []SecID // list of sector IDs holding a MSAT |
| 51 | writer *os.File |
| 52 | closer io.Closer |
| 53 | } |
| 54 | |
| 55 | // Open a CDF file for reading |
| 56 | func ReadPath(path string) (*ComDoc, error) { |
| 57 | f, err := os.Open(path) |
| 58 | if err != nil { |
| 59 | return nil, err |
| 60 | } |
| 61 | return openFile(f, nil, f) |
| 62 | } |
| 63 | |
| 64 | // Open a CDF file for reading and writing |
| 65 | func WritePath(path string) (*ComDoc, error) { |
| 66 | f, err := os.OpenFile(path, os.O_RDWR, 0) |
| 67 | if err != nil { |
| 68 | return nil, err |
| 69 | } |
| 70 | return openFile(f, f, f) |
| 71 | } |
| 72 | |
| 73 | // Parse an already-open CDF file for reading |
| 74 | func ReadFile(reader io.ReaderAt) (*ComDoc, error) { |
| 75 | return openFile(reader, nil, nil) |
| 76 | } |
| 77 | |
| 78 | // Parse an already-open CDF file for reading and writing |
| 79 | func WriteFile(f *os.File) (*ComDoc, error) { |
| 80 | return openFile(f, f, nil) |
| 81 | } |
| 82 | |
| 83 | func openFile(reader io.ReaderAt, writer *os.File, closer io.Closer) (*ComDoc, error) { |
| 84 | header := new(Header) |
| 85 | r := &ComDoc{ |
| 86 | File: reader, |
| 87 | Header: header, |
| 88 | writer: writer, |
| 89 | closer: closer, |
| 90 | } |
| 91 | sr := io.NewSectionReader(reader, 0, 512) |
| 92 | if err := binary.Read(sr, binary.LittleEndian, header); err != nil { |
| 93 | return nil, err |
| 94 | } |
| 95 | if !bytes.Equal(header.Magic[:], fileMagic) { |
| 96 | return nil, errors.New("not a compound document file") |
| 97 | } |
| 98 | if header.ByteOrder != byteOrderMarker { |
| 99 | return nil, errors.New("incorrect byte order marker") |
| 100 | } |
| 101 | if header.SectorSize < 5 || header.SectorSize > 28 || header.ShortSectorSize >= header.SectorSize { |
| 102 | return nil, errors.New("unreasonable header values") |
| 103 | } |
| 104 | r.SectorSize = 1 << header.SectorSize |
| 105 | r.ShortSectorSize = 1 << header.ShortSectorSize |
| 106 | if r.SectorSize < 512 { |
| 107 | r.FirstSector = 512 |
| 108 | } else { |
| 109 | r.FirstSector = int64(r.SectorSize) |
| 110 | } |
| 111 | r.sectorBuf = make([]byte, r.SectorSize) |
| 112 | |
| 113 | if err := r.readMSAT(); err != nil { |
| 114 | return nil, err |
| 115 | } |
| 116 | if err := r.readSAT(); err != nil { |
| 117 | return nil, err |
| 118 | } |
| 119 | if err := r.readShortSAT(); err != nil { |
| 120 | return nil, err |
| 121 | } |
| 122 | if err := r.readDir(); err != nil { |
| 123 | return nil, err |
| 124 | } |
| 125 | return r, nil |
| 126 | } |