| 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 | "errors" |
| 21 | "fmt" |
| 22 | "io" |
| 23 | ) |
| 24 | |
| 25 | type streamReader struct { |
| 26 | remaining uint32 |
| 27 | nextSector SecID |
| 28 | sat []SecID |
| 29 | sectorSize int |
| 30 | readSector func(SecID, []byte) (int, error) |
| 31 | buf, saved []byte |
| 32 | } |
| 33 | |
| 34 | // Open a stream for reading |
| 35 | func (r *ComDoc) ReadStream(e *DirEnt) (io.Reader, error) { |
| 36 | if e.Type != DirStream { |
| 37 | return nil, errors.New("not a stream") |
| 38 | } |
| 39 | sr := &streamReader{ |
| 40 | remaining: e.StreamSize, |
| 41 | nextSector: e.NextSector, |
| 42 | } |
| 43 | if e.StreamSize < r.Header.MinStdStreamSize { |
| 44 | sr.sectorSize = r.ShortSectorSize |
| 45 | sr.sat = r.SSAT |
| 46 | sr.readSector = r.readShortSector |
| 47 | } else { |
| 48 | sr.sectorSize = r.SectorSize |
| 49 | sr.sat = r.SAT |
| 50 | sr.readSector = r.readSector |
| 51 | } |
| 52 | sr.buf = make([]byte, sr.sectorSize) |
| 53 | return sr, nil |
| 54 | } |
| 55 | |
| 56 | func (sr *streamReader) Read(d []byte) (copied int, err error) { |
| 57 | if sr.remaining == 0 { |
| 58 | return 0, io.EOF |
| 59 | } else if len(d) == 0 { |
| 60 | return 0, nil |
| 61 | } |
| 62 | if int64(len(d)) > int64(sr.remaining) { |
| 63 | d = d[:int(sr.remaining)] |
| 64 | } |
| 65 | // read from previously buffered sector |
| 66 | if len(sr.saved) > 0 { |
| 67 | n := copy(d, sr.saved) |
| 68 | d = d[n:] |
| 69 | sr.saved = sr.saved[n:] |
| 70 | copied += n |
| 71 | sr.remaining -= uint32(n) |
| 72 | } |
| 73 | // read whole sectors |
| 74 | for len(d) >= sr.sectorSize { |
| 75 | if sr.nextSector < 0 { |
| 76 | return copied, errors.New("unexpected end to stream") |
| 77 | } |
| 78 | n, err := sr.readSector(sr.nextSector, d[:sr.sectorSize]) |
| 79 | if n > 0 { |
| 80 | d = d[n:] |
| 81 | copied += n |
| 82 | sr.remaining -= uint32(n) |
| 83 | } |
| 84 | if err != io.EOF && err != nil { |
| 85 | return copied, err |
| 86 | } else if n < sr.sectorSize && sr.remaining > 0 { |
| 87 | return copied, fmt.Errorf("short read of sector %d: expected %d bytes but got %d", sr.nextSector, sr.sectorSize, n) |
| 88 | } |
| 89 | sr.nextSector = sr.sat[sr.nextSector] |
| 90 | } |
| 91 | // read partial sector and buffer the rest |
| 92 | if len(d) > 0 { |
| 93 | if sr.nextSector < 0 { |
| 94 | return copied, errors.New("unexpected end to stream") |
| 95 | } |
| 96 | // read the full sector |
| 97 | sectorN, err := sr.readSector(sr.nextSector, sr.buf) |
| 98 | if sectorN > 0 { |
| 99 | // fill the rest of the result |
| 100 | copyN := copy(d, sr.buf) |
| 101 | copied += copyN |
| 102 | sr.remaining -= uint32(copyN) |
| 103 | } |
| 104 | if err != io.EOF && err != nil { |
| 105 | return copied, err |
| 106 | } else if sectorN < sr.sectorSize && sr.remaining > 0 { |
| 107 | // it's ok if the final sector is truncated if there are no more bytes in the stream |
| 108 | return copied, fmt.Errorf("short read of sector %d: expected %d bytes but got %d", sr.nextSector, sr.remaining, sectorN) |
| 109 | } |
| 110 | // save the remainder, if anything |
| 111 | sr.saved = sr.buf[len(d):] |
| 112 | sr.nextSector = sr.sat[sr.nextSector] |
| 113 | } |
| 114 | return copied, nil |
| 115 | } |
| 116 | |
| 117 | // Store a blob as a chain of sectors, updating the sector table (or |
| 118 | // short-sector table if "short" is set) and return the first sector ID |
| 119 | func (r *ComDoc) addStream(contents []byte, short bool) (SecID, error) { |
| 120 | var sectorSize int |
| 121 | var sat, freeList []SecID |
| 122 | if short { |
| 123 | sectorSize = int(r.ShortSectorSize) |
| 124 | needSectors := (len(contents) + sectorSize - 1) / sectorSize |
| 125 | freeList = r.makeFreeSectors(needSectors, true) |
| 126 | sat = r.SSAT |
| 127 | } else { |
| 128 | sectorSize = int(r.SectorSize) |
| 129 | needSectors := (len(contents) + sectorSize - 1) / sectorSize |
| 130 | freeList = r.makeFreeSectors(needSectors, false) |
| 131 | sat = r.SAT |
| 132 | } |
| 133 | first := SecIDEndOfChain |
| 134 | previous := first |
| 135 | for _, i := range freeList { |
| 136 | if previous == SecIDEndOfChain { |
| 137 | first = i |
| 138 | } else { |
| 139 | sat[previous] = i |
| 140 | } |
| 141 | previous = i |
| 142 | // write to file |
| 143 | n := sectorSize |
| 144 | if n > len(contents) { |
| 145 | n = len(contents) |
| 146 | } |
| 147 | var err error |
| 148 | if short { |
| 149 | err = r.writeShortSector(i, contents[:n]) |
| 150 | } else { |
| 151 | err = r.writeSector(i, contents[:n]) |
| 152 | } |
| 153 | if err != nil { |
| 154 | return 0, err |
| 155 | } |
| 156 | contents = contents[n:] |
| 157 | } |
| 158 | sat[previous] = SecIDEndOfChain |
| 159 | if len(contents) > 0 { |
| 160 | panic("didn't allocate enough sectors") |
| 161 | } |
| 162 | return first, nil |
| 163 | } |