File: reader.go

package info (click to toggle)
relic 7.6.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,108 kB
  • sloc: sh: 230; makefile: 10
file content (126 lines) | stat: -rw-r--r-- 3,437 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
//
// Copyright (c) SAS Institute Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

// Microsoft Compound Document File
// Reference: https://www.openoffice.org/sc/compdocfileformat.pdf
// ERRATA: The above document says the 0th sector is always 512 bytes into the
// file. This is not correct. If SectorSize > 512 bytes then the 0th sector is
// SectorSize bytes into the file.
package comdoc

import (
	"bytes"
	"encoding/binary"
	"errors"
	"io"
	"os"
)

// CDF file open for reading or writing
type ComDoc struct {
	File            io.ReaderAt
	Header          *Header
	SectorSize      int
	ShortSectorSize int
	FirstSector     int64
	// MSAT is a list of sector IDs holding a SAT
	MSAT []SecID
	// 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
	SAT   []SecID
	SSAT  []SecID
	Files []DirEnt

	sectorBuf   []byte
	changed     bool
	rootStorage int     // index into files
	rootFiles   []int   // index into Files
	msatList    []SecID // list of sector IDs holding a MSAT
	writer      *os.File
	closer      io.Closer
}

// Open a CDF file for reading
func ReadPath(path string) (*ComDoc, error) {
	f, err := os.Open(path)
	if err != nil {
		return nil, err
	}
	return openFile(f, nil, f)
}

// Open a CDF file for reading and writing
func WritePath(path string) (*ComDoc, error) {
	f, err := os.OpenFile(path, os.O_RDWR, 0)
	if err != nil {
		return nil, err
	}
	return openFile(f, f, f)
}

// Parse an already-open CDF file for reading
func ReadFile(reader io.ReaderAt) (*ComDoc, error) {
	return openFile(reader, nil, nil)
}

// Parse an already-open CDF file for reading and writing
func WriteFile(f *os.File) (*ComDoc, error) {
	return openFile(f, f, nil)
}

func openFile(reader io.ReaderAt, writer *os.File, closer io.Closer) (*ComDoc, error) {
	header := new(Header)
	r := &ComDoc{
		File:   reader,
		Header: header,
		writer: writer,
		closer: closer,
	}
	sr := io.NewSectionReader(reader, 0, 512)
	if err := binary.Read(sr, binary.LittleEndian, header); err != nil {
		return nil, err
	}
	if !bytes.Equal(header.Magic[:], fileMagic) {
		return nil, errors.New("not a compound document file")
	}
	if header.ByteOrder != byteOrderMarker {
		return nil, errors.New("incorrect byte order marker")
	}
	if header.SectorSize < 5 || header.SectorSize > 28 || header.ShortSectorSize >= header.SectorSize {
		return nil, errors.New("unreasonable header values")
	}
	r.SectorSize = 1 << header.SectorSize
	r.ShortSectorSize = 1 << header.ShortSectorSize
	if r.SectorSize < 512 {
		r.FirstSector = 512
	} else {
		r.FirstSector = int64(r.SectorSize)
	}
	r.sectorBuf = make([]byte, r.SectorSize)

	if err := r.readMSAT(); err != nil {
		return nil, err
	}
	if err := r.readSAT(); err != nil {
		return nil, err
	}
	if err := r.readShortSAT(); err != nil {
		return nil, err
	}
	if err := r.readDir(); err != nil {
		return nil, err
	}
	return r, nil
}