| 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 | "bytes" |
| 21 | "encoding/binary" |
| 22 | "errors" |
| 23 | "io" |
| 24 | ) |
| 25 | |
| 26 | // Convert a sector ID to an absolute file position |
| 27 | func (r *ComDoc) sectorToOffset(sector SecID) int64 { |
| 28 | if sector < 0 { |
| 29 | return -1 |
| 30 | } |
| 31 | return r.FirstSector + int64(sector)*int64(r.SectorSize) |
| 32 | } |
| 33 | |
| 34 | // Read the specified sector |
| 35 | func (r *ComDoc) readSector(sector SecID, buf []byte) (int, error) { |
| 36 | return r.File.ReadAt(buf, r.sectorToOffset(sector)) |
| 37 | } |
| 38 | |
| 39 | // Read the specified sector into a binary structure. It must be exactly 1 |
| 40 | // sector in size already. |
| 41 | func (r *ComDoc) readSectorStruct(sector SecID, v interface{}) error { |
| 42 | n, err := r.readSector(sector, r.sectorBuf) |
| 43 | if err != nil && err != io.EOF { |
| 44 | return err |
| 45 | } else if n < r.SectorSize { |
| 46 | return io.ErrUnexpectedEOF |
| 47 | } |
| 48 | return binary.Read(bytes.NewReader(r.sectorBuf), binary.LittleEndian, v) |
| 49 | } |
| 50 | |
| 51 | // Write to a specified sector. Content will be padded with zeroes if it is |
| 52 | // less than a full sector in length. |
| 53 | func (r *ComDoc) writeSector(sector SecID, content []byte) error { |
| 54 | if len(content) > r.SectorSize { |
| 55 | panic("excessive write") |
| 56 | } else if len(content) < r.SectorSize { |
| 57 | buf := r.sectorBuf |
| 58 | copy(buf, content) |
| 59 | for i := len(content); i < r.SectorSize; i++ { |
| 60 | buf[i] = 0 |
| 61 | } |
| 62 | content = buf[:r.SectorSize] |
| 63 | } |
| 64 | _, err := r.writer.WriteAt(content, r.sectorToOffset(sector)) |
| 65 | return err |
| 66 | } |
| 67 | |
| 68 | // Mark a chain of sectors as free |
| 69 | func freeSectors(sat []SecID, sector SecID) { |
| 70 | for { |
| 71 | nextSector := sat[sector] |
| 72 | sat[sector] = SecIDFree |
| 73 | if nextSector < 0 { |
| 74 | break |
| 75 | } |
| 76 | sector = nextSector |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | // Return a list of "count" free sectors or short sectors, extending the table if needed |
| 81 | func (r *ComDoc) makeFreeSectors(count int, short bool) []SecID { |
| 82 | if count <= 0 { |
| 83 | return nil |
| 84 | } |
| 85 | freeList := make([]SecID, 0, count) |
| 86 | var sat []SecID |
| 87 | if short { |
| 88 | sat = r.SSAT |
| 89 | } else { |
| 90 | sat = r.SAT |
| 91 | } |
| 92 | // scan for existing free sectors |
| 93 | for i, j := range sat { |
| 94 | if j != SecIDFree { |
| 95 | continue |
| 96 | } |
| 97 | freeList = append(freeList, SecID(i)) |
| 98 | count-- |
| 99 | if count == 0 { |
| 100 | return freeList |
| 101 | } |
| 102 | } |
| 103 | // extend the sector table |
| 104 | sectorsPerBlock := r.SectorSize / 4 |
| 105 | needBlocks := (count + sectorsPerBlock - 1) / sectorsPerBlock |
| 106 | oldCount := len(sat) |
| 107 | newSAT := append(sat, make([]SecID, needBlocks*sectorsPerBlock)...) |
| 108 | for i := oldCount; i < len(newSAT); i++ { |
| 109 | newSAT[i] = SecIDFree |
| 110 | if count > 0 { |
| 111 | freeList = append(freeList, SecID(i)) |
| 112 | count-- |
| 113 | } |
| 114 | } |
| 115 | if short { |
| 116 | r.SSAT = newSAT |
| 117 | } else { |
| 118 | r.SAT = newSAT |
| 119 | } |
| 120 | return freeList |
| 121 | } |
| 122 | |
| 123 | // Read the sector allocation table. |
| 124 | // |
| 125 | // Each index within the table corresponds to a sector within the file itself. |
| 126 | // The value held at that index in the SAT either points to the ID of the |
| 127 | // sector holding the next chunk of data for that stream, or -2 to indicate |
| 128 | // there are no more sectors. |
| 129 | func (r *ComDoc) readSAT() error { |
| 130 | count := r.SectorSize / 4 |
| 131 | sat := make([]SecID, count*int(r.Header.SATSectors)) |
| 132 | position := 0 |
| 133 | for _, sector := range r.MSAT { |
| 134 | if sector < 0 { |
| 135 | continue |
| 136 | } |
| 137 | if position >= len(sat) { |
| 138 | return errors.New("msat has more sectors than indicated") |
| 139 | } |
| 140 | if err := r.readSectorStruct(sector, sat[position:position+count]); err != nil { |
| 141 | return err |
| 142 | } |
| 143 | position += count |
| 144 | } |
| 145 | r.SAT = sat |
| 146 | return nil |
| 147 | } |
| 148 | |
| 149 | // Write the new sector allocation table to the listed sector IDs |
| 150 | func (r *ComDoc) writeSAT() error { |
| 151 | satPerSector := r.SectorSize / 4 |
| 152 | buf := bytes.NewBuffer(r.sectorBuf) |
| 153 | for i, sector := range r.MSAT { |
| 154 | j := i * satPerSector |
| 155 | buf.Reset() |
| 156 | _ = binary.Write(buf, binary.LittleEndian, r.SAT[j:j+satPerSector]) |
| 157 | if err := r.writeSector(sector, buf.Bytes()); err != nil { |
| 158 | return err |
| 159 | } |
| 160 | } |
| 161 | return nil |
| 162 | } |