github.com/shakinm/xlsReader@v0.9.12/cfb/sector.go (about)

     1  package cfb
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/binary"
     6  )
     7  
     8  // FREESECT - Specifies an unallocated sector in the FAT, Mini FAT, or DIFAT
     9  var FREESECT = []byte{0xFF, 0xFF, 0xFF, 0xFF}
    10  
    11  // ENDOFCHAIN -  End of a linked chain of sectors.
    12  var ENDOFCHAIN = []byte{0xFE, 0xFF, 0xFF, 0xFF}
    13  
    14  // FATSECT - Specifies a FAT sector in the FAT.
    15  var FATSECT = []byte{0xFD, 0xFF, 0xFF, 0xFF}
    16  
    17  // DIFSECT - Specifies a DIFAT sector in the FAT.
    18  var DIFSECT = []byte{0xFC, 0xFF, 0xFF, 0xFF}
    19  
    20  // Sector struct
    21  type Sector struct {
    22  	SectorSize uint32
    23  	Data       []byte
    24  }
    25  
    26  func (s *Sector) getSector() *Sector {
    27  	return s
    28  }
    29  
    30  func (s *Sector) findBlock(block []byte) bool {
    31  
    32  	var section = make([]byte, 0)
    33  	for _, value := range s.Data {
    34  		section = append(section, value)
    35  		if len(section) == 4 {
    36  			if bytes.Compare(section, block) == 0 {
    37  				return true
    38  			}
    39  			section = make([]byte, 0)
    40  
    41  		}
    42  	}
    43  	return false
    44  }
    45  
    46  func (s *Sector) getFATSectorLocations() []byte {
    47  	return s.Data[0 : s.SectorSize-4]
    48  }
    49  
    50  func (s *Sector) getMiniFatFATSectorLocations() []byte {
    51  	return s.Data[0 : s.SectorSize]
    52  }
    53  
    54  func (s *Sector) getNextDIFATSectorLocation() []byte {
    55  	return s.Data[s.SectorSize-4:]
    56  }
    57  
    58  // NewSector - Create new Sector struct fot FAT
    59  func NewSector(header *Header) Sector {
    60  	return Sector{
    61  		SectorSize: header.sectorSize(),
    62  		Data:       make([]byte, header.sectorSize()),
    63  	}
    64  
    65  }
    66  
    67  // NewMiniFatSector - Create new Sector struct for MiniFat
    68  func NewMiniFatSector(header *Header) Sector {
    69  	return Sector{
    70  		SectorSize: 64,
    71  		Data:       make([]byte, 64),
    72  	}
    73  }
    74  
    75  
    76  func (s *Sector) values(length int) (res []uint32 ) {
    77  
    78  	  res = make([]uint32, length)
    79  
    80  	buf := bytes.NewBuffer(s.Data)
    81  
    82  	 _ = binary.Read(buf, binary.LittleEndian, res) // nolint: gosec
    83  
    84  	return res
    85  }