From af36f9b9aaf4116d5db4ac6482078fe7b3b130c2 Mon Sep 17 00:00:00 2001 From: Caleb Gardner Date: Wed, 25 Nov 2020 01:15:54 -0600 Subject: [PATCH] Documentation! Though may functions/structs will be made private, I still want things documented. --- compression.go | 3 +++ datareader.go | 4 ++++ metadatareader.go | 6 +++++- reader.go | 1 + superblock.go | 3 +++ 5 files changed, 16 insertions(+), 1 deletion(-) diff --git a/compression.go b/compression.go index 74ab900..a97e05e 100644 --- a/compression.go +++ b/compression.go @@ -6,12 +6,15 @@ import ( "io" ) +//Decompressor is a squashfs decompressor interface. Allows for easy decompression no matter the type of compression. type Decompressor interface { Decompress(io.Reader) ([]byte, error) } +//ZlibDecompressor is a decompressor for gzip type compression type ZlibDecompressor struct{} +//Decompress reads the entirety of the given reader and returns it uncompressed as a byte slice. func (z *ZlibDecompressor) Decompress(r io.Reader) ([]byte, error) { rdr, err := zlib.NewReader(r) if err != nil { diff --git a/datareader.go b/datareader.go index 2ad09c8..842795f 100644 --- a/datareader.go +++ b/datareader.go @@ -106,6 +106,8 @@ func (d *DataReader) readCurBlock() error { d.curData = make([]byte, d.r.super.BlockSize) d.blocks[d.curBlock].uncompressedSize = d.r.super.BlockSize d.blocks[d.curBlock].begOffset = d.offset + fmt.Println("dat red") + fmt.Println(len(d.curData)) return nil } sec := io.NewSectionReader(d.r.r, d.offset, int64(d.blocks[d.curBlock].size)) @@ -118,6 +120,8 @@ func (d *DataReader) readCurBlock() error { d.curData = btys d.blocks[d.curBlock].begOffset = d.offset d.offset += int64(d.blocks[d.curBlock].size) + fmt.Println("dat red") + fmt.Println(len(d.curData)) return nil } var buf bytes.Buffer diff --git a/metadatareader.go b/metadatareader.go index cc33ed2..061b5b9 100644 --- a/metadatareader.go +++ b/metadatareader.go @@ -13,6 +13,7 @@ type metadata struct { compressed bool } +//MetadataReader is a block reader for metadata. It will automatically read the next block, when it reaches the end of a block. type MetadataReader struct { s *Reader offset int64 @@ -21,6 +22,7 @@ type MetadataReader struct { readOffset int } +//NewMetadataReader creates a new MetadataReader, beginning to read at the given offset. It will automatically cache the first block at the location. func (s *Reader) NewMetadataReader(offset int64) (*MetadataReader, error) { var br MetadataReader br.s = s @@ -36,6 +38,7 @@ func (s *Reader) NewMetadataReader(offset int64) (*MetadataReader, error) { return &br, nil } +//NewMetadataReaderFromInodeRef creates a new MetadataReader with the offsets set by the given inode reference. func (s *Reader) NewMetadataReaderFromInodeRef(ref uint64) (*MetadataReader, error) { offset, metaOffset := processInodeRef(ref) br, err := s.NewMetadataReader(int64(s.super.InodeTableStart + offset)) @@ -88,6 +91,7 @@ func (br *MetadataReader) readNextDataBlock() error { return nil } +//Read reads bytes into the given byte slice. Returns the amount of data read. func (br *MetadataReader) Read(p []byte) (int, error) { if br.readOffset+len(p) < len(br.data) { for i := 0; i < len(p); i++ { @@ -123,7 +127,7 @@ func (br *MetadataReader) Read(p []byte) (int, error) { return read, nil } -//Seek will seek to the specified location (if possible). +//Seek will seek to the specified location (if possible). Seeking is relative to the uncompressed data. //When io.SeekCurrent or io.SeekStart is set, if seeking would put the offset beyond the current cached data, it will try to read the next data blocks to accomodate. On a failure it will seek to the end of the data. //When io.SeekEnd is set, it wil seek reletive to the currently cached data. func (br *MetadataReader) Seek(offset int64, whence int) (int64, error) { diff --git a/reader.go b/reader.go index 606b63d..8687ff6 100644 --- a/reader.go +++ b/reader.go @@ -129,6 +129,7 @@ func (r *Reader) readDir(i *inode.Inode) (paths []string, err error) { return } +//GetFileStructure returns ALL folders and files contained in the squashfs. Folders end with a "/". func (r *Reader) GetFileStructure() ([]string, error) { in, err := r.GetInodeFromPath("") if err != nil { diff --git a/superblock.go b/superblock.go index ec9ce9a..32623ba 100644 --- a/superblock.go +++ b/superblock.go @@ -9,6 +9,7 @@ const ( zstdCompression ) +//Superblock contains important information about a squashfs file. Located at the very front of the archive. type Superblock struct { Magic uint32 InodeCount uint32 @@ -31,6 +32,7 @@ type Superblock struct { ExportTableStart uint64 } +//SuperblockFlags is the parsed version of Superblock.Flags type SuperblockFlags struct { UncompressedInodes bool UncompressedData bool @@ -46,6 +48,7 @@ type SuperblockFlags struct { UncompressedIDs bool } +//GetFlags returns a SuperblockFlags for a given superblock. func (s *Superblock) GetFlags() SuperblockFlags { return SuperblockFlags{ UncompressedInodes: s.Flags&0x1 == 0x1,