...

Source file src/github.com/sassoftware/relic/lib/comdoc/msat.go

Documentation: github.com/sassoftware/relic/lib/comdoc

     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  )
    23  
    24  // Read the master/meta sector allocation table. It is an array of all the
    25  // sectors holding pieces of the sector allocation table (SAT). The main file
    26  // header holds 109 MSAT entries and MSATNextSector may point to a sector
    27  // holding yet more. The last entry in each additional sector points to another
    28  // sector holding more MSAT entries, or -2 if none.
    29  func (r *ComDoc) readMSAT() error {
    30  	msat := r.Header.MSAT[:]
    31  	nextSector := r.Header.MSATNextSector
    32  	count := r.SectorSize / 4
    33  	values := make([]SecID, count)
    34  	for nextSector >= 0 {
    35  		if err := r.readSectorStruct(nextSector, values); err != nil {
    36  			return err
    37  		}
    38  		msat = append(msat, values[:count-1]...)
    39  		nextSector = values[count-1]
    40  	}
    41  	r.MSAT = msat
    42  	return nil
    43  }
    44  
    45  // Allocate sectors for the SAT and MSAT
    46  func (r *ComDoc) allocSectorTables() (satList, msatList []SecID) {
    47  	// free existing sectors
    48  	for i, j := range r.SAT {
    49  		if j == SecIDSAT || j == SecIDMSAT {
    50  			r.SAT[i] = SecIDFree
    51  		}
    52  	}
    53  	// work out how many sectors are needed for both
    54  	satPerSector := r.SectorSize / 4
    55  	msatPerSector := satPerSector - 1
    56  	for {
    57  		if len(r.SAT)%satPerSector != 0 {
    58  			panic("irregularly sized sector table")
    59  		}
    60  		satSectors := len(r.SAT) / satPerSector
    61  		// 109 MSAT entries fit into the file header, the rest need more sectors
    62  		msatSectors := (satSectors - msatInHeader + msatPerSector - 1) / msatPerSector
    63  		// Check if there's room
    64  		oldSize := len(r.SAT)
    65  		freeList := r.makeFreeSectors(satSectors+msatSectors, false)
    66  		if oldSize == len(r.SAT) {
    67  			msatList = freeList[:msatSectors]
    68  			satList = freeList[msatSectors:]
    69  			break
    70  		}
    71  		// The SAT was extended so go around again to make sure that didn't
    72  		// increase the requirements further
    73  	}
    74  	// Mark used sectors
    75  	for _, i := range msatList {
    76  		r.SAT[i] = SecIDMSAT
    77  	}
    78  	for _, i := range satList {
    79  		r.SAT[i] = SecIDSAT
    80  	}
    81  	return satList, msatList
    82  }
    83  
    84  // Write an updated MSAT out to the header and msatSectors, with satSectors
    85  // being the contents of the MSAT
    86  func (r *ComDoc) writeMSAT(satSectors, msatSectors []SecID) error {
    87  	satPerSector := r.SectorSize / 4
    88  	msatPerSector := satPerSector - 1
    89  	msatCount := msatInHeader + len(msatSectors)*msatPerSector
    90  	msat := make([]SecID, msatCount)
    91  	copy(msat, satSectors)
    92  	for i := len(satSectors); i < msatCount; i++ {
    93  		msat[i] = SecIDFree
    94  	}
    95  	copy(r.Header.MSAT[:], msat)
    96  	msat = msat[msatInHeader:] // done with the first 109
    97  	buf := bytes.NewBuffer(r.sectorBuf)
    98  	nextSector := SecIDEndOfChain
    99  	chunk := make([]SecID, r.SectorSize/4)
   100  	for i := len(msatSectors) - 1; i >= 0; i-- { // each needs to link to the next, so walk backwards
   101  		sector := msatSectors[i]
   102  		j := i * msatPerSector
   103  		copy(chunk, msat[j:])
   104  		msat = msat[:j]
   105  		chunk[msatPerSector] = nextSector
   106  		nextSector = sector
   107  
   108  		buf.Reset()
   109  		binary.Write(buf, binary.LittleEndian, chunk)
   110  		if err := r.writeSector(sector, buf.Bytes()); err != nil {
   111  			return err
   112  		}
   113  	}
   114  	r.Header.MSATNextSector = nextSector
   115  	r.MSAT = msat
   116  	return nil
   117  }
   118  

View as plain text