Move changes from exp2 to main
This is largely a move to simplify a lot of the readers Also further breaks out functions.
This commit is contained in:
@@ -1,76 +0,0 @@
|
||||
package compression
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"io"
|
||||
|
||||
"github.com/klauspost/compress/zlib"
|
||||
)
|
||||
|
||||
type gzipInit struct {
|
||||
CompressionLevel int32
|
||||
WindowSize int16
|
||||
Strategies int16
|
||||
}
|
||||
|
||||
//Gzip is a decompressor for gzip type compression. Uses zlib for compression and decompression
|
||||
type Gzip struct {
|
||||
wrt *zlib.Writer
|
||||
gzipInit
|
||||
HasCustomWindow bool
|
||||
HasStrategies bool
|
||||
}
|
||||
|
||||
//NewGzipCompressorWithOptions creates a new gzip compressor/decompressor with options read from the given reader.
|
||||
func NewGzipCompressorWithOptions(r io.Reader) (*Gzip, error) {
|
||||
var gzip Gzip
|
||||
err := binary.Read(r, binary.LittleEndian, &gzip.gzipInit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
//TODO: proper support for window size and strategies
|
||||
gzip.HasCustomWindow = gzip.WindowSize != 15
|
||||
gzip.HasStrategies = gzip.Strategies != 0 && gzip.Strategies != 1
|
||||
return &gzip, nil
|
||||
}
|
||||
|
||||
//Decompress reads the entirety of the given reader and returns it uncompressed as a byte slice.
|
||||
func (g *Gzip) Decompress(r io.Reader) ([]byte, error) {
|
||||
rdr, err := zlib.NewReader(r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var data bytes.Buffer
|
||||
_, err = io.Copy(&data, rdr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return data.Bytes(), nil
|
||||
}
|
||||
|
||||
//Compress compresses the given data (as a byte array) and returns the compressed data.
|
||||
func (g *Gzip) Compress(data []byte) ([]byte, error) {
|
||||
var buf bytes.Buffer
|
||||
var err error
|
||||
if g.wrt == nil {
|
||||
if g.CompressionLevel == 0 {
|
||||
g.wrt = zlib.NewWriter(&buf)
|
||||
} else {
|
||||
g.wrt, err = zlib.NewWriterLevel(&buf, int(g.CompressionLevel))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
wrt, err := zlib.NewWriterLevel(&buf, int(g.CompressionLevel))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_, err = wrt.Write(data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
wrt.Close()
|
||||
return buf.Bytes(), nil
|
||||
}
|
||||
@@ -1,55 +0,0 @@
|
||||
package compression
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"io"
|
||||
|
||||
"github.com/pierrec/lz4/v4"
|
||||
)
|
||||
|
||||
//Lz4 is a Lz4 Compressor/Decompressor
|
||||
type Lz4 struct {
|
||||
HC bool
|
||||
}
|
||||
|
||||
//NewLz4CompressorWithOptions creates a new lz4 compressor/decompressor with options read from the given reader.
|
||||
func NewLz4CompressorWithOptions(r io.Reader) (*Lz4, error) {
|
||||
var lz4 Lz4
|
||||
var init struct {
|
||||
Version int32
|
||||
Flags int32
|
||||
}
|
||||
err := binary.Read(r, binary.LittleEndian, &init)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
lz4.HC = init.Flags == 1
|
||||
return &lz4, nil
|
||||
}
|
||||
|
||||
//Decompress decompresses all data from r and returns the uncompressed bytes
|
||||
func (l *Lz4) Decompress(r io.Reader) ([]byte, error) {
|
||||
rdr := lz4.NewReader(r)
|
||||
var buf bytes.Buffer
|
||||
_, err := io.Copy(&buf, rdr)
|
||||
return buf.Bytes(), err
|
||||
}
|
||||
|
||||
//Compress implements compression.Compress
|
||||
func (l *Lz4) Compress(data []byte) ([]byte, error) {
|
||||
var buf bytes.Buffer
|
||||
w := lz4.NewWriter(&buf)
|
||||
if l.HC {
|
||||
err := w.Apply(lz4.CompressionLevelOption(lz4.Level9))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
_, err := w.Write(data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
w.Close()
|
||||
return buf.Bytes(), nil
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
package compression
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
|
||||
"github.com/ulikunitz/xz/lzma"
|
||||
)
|
||||
|
||||
//Lzma is a lzma decompressor
|
||||
type Lzma struct{}
|
||||
|
||||
//Decompress decompresses all the data in the given reader and returns the uncompressed bytes.
|
||||
func (l *Lzma) Decompress(rdr io.Reader) ([]byte, error) {
|
||||
r, err := lzma.NewReader(rdr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var buf bytes.Buffer
|
||||
_, err = io.Copy(&buf, r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return buf.Bytes(), nil
|
||||
}
|
||||
|
||||
//Compress implements compression.Compress
|
||||
func (l *Lzma) Compress(data []byte) ([]byte, error) {
|
||||
var buf bytes.Buffer
|
||||
w, err := lzma.NewWriter(&buf)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_, err = w.Write(data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
w.Close()
|
||||
return buf.Bytes(), nil
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
package compression
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"io"
|
||||
|
||||
lzo "github.com/rasky/go-lzo"
|
||||
)
|
||||
|
||||
type Lzo struct {
|
||||
Algorithm int32
|
||||
Level int32
|
||||
}
|
||||
|
||||
func NewLzoCompressorWithOptions(rdr io.Reader) (*Lzo, error) {
|
||||
var lz Lzo
|
||||
err := binary.Read(rdr, binary.LittleEndian, &lz)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &lz, nil
|
||||
}
|
||||
|
||||
func (l Lzo) Decompress(rdr io.Reader) ([]byte, error) {
|
||||
byt, err := lzo.Decompress1X(rdr, 0, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return byt, nil
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
package compression
|
||||
|
||||
import "io"
|
||||
|
||||
//Compressor is a squashfs decompressor interface. Allows for easy compression.
|
||||
type Compressor interface {
|
||||
Compress([]byte) ([]byte, error)
|
||||
}
|
||||
|
||||
//Decompressor is a squashfs decompressor interface. Allows for easy decompression no matter the type of compression.
|
||||
type Decompressor interface {
|
||||
Decompress(io.Reader) ([]byte, error)
|
||||
}
|
||||
@@ -1,60 +0,0 @@
|
||||
package compression
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"io"
|
||||
|
||||
"github.com/therootcompany/xz"
|
||||
|
||||
wrtXz "github.com/ulikunitz/xz"
|
||||
)
|
||||
|
||||
type Xz struct {
|
||||
DictionarySize int32
|
||||
Filters int32
|
||||
}
|
||||
|
||||
//NewXzCompressorWithOptions creates a new Xz compressor/decompressor that reads the compressor options from the given reader.
|
||||
func NewXzCompressorWithOptions(rdr io.Reader) (*Xz, error) {
|
||||
var x Xz
|
||||
err := binary.Read(rdr, binary.LittleEndian, &x)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &x, nil
|
||||
}
|
||||
|
||||
//Decompress decompresses all the data from the rdr and returns the uncompressed bytes.
|
||||
func (x *Xz) Decompress(rdr io.Reader) ([]byte, error) {
|
||||
r, err := xz.NewReader(rdr, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var buf bytes.Buffer
|
||||
_, err = io.Copy(&buf, r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return buf.Bytes(), nil
|
||||
}
|
||||
|
||||
//Compress implements compression.Compress
|
||||
func (x *Xz) Compress(data []byte) ([]byte, error) {
|
||||
var buf bytes.Buffer
|
||||
w, err := wrtXz.NewWriter(&buf)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
w.DictCap = int(x.DictionarySize)
|
||||
err = w.Verify()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_, err = w.Write(data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
w.Close()
|
||||
return buf.Bytes(), nil
|
||||
}
|
||||
@@ -1,51 +0,0 @@
|
||||
package compression
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"io"
|
||||
|
||||
"github.com/klauspost/compress/zstd"
|
||||
)
|
||||
|
||||
//Zstd is a zstd compressor/decompressor
|
||||
type Zstd struct {
|
||||
CompressionLevel int32
|
||||
}
|
||||
|
||||
//NewZstdCompressorWithOptions creates a new Zstd with options read from the given reader
|
||||
func NewZstdCompressorWithOptions(r io.Reader) (*Zstd, error) {
|
||||
var zstd Zstd
|
||||
err := binary.Read(r, binary.LittleEndian, &zstd)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &zstd, nil
|
||||
}
|
||||
|
||||
//Decompress decompresses all data from the reader and returns the uncompressed data
|
||||
func (z *Zstd) Decompress(r io.Reader) ([]byte, error) {
|
||||
rdr, err := zstd.NewReader(r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rdr.Close()
|
||||
var buf bytes.Buffer
|
||||
_, err = io.Copy(&buf, rdr)
|
||||
return buf.Bytes(), err
|
||||
}
|
||||
|
||||
//Compress impelements compression.Compress
|
||||
func (z *Zstd) Compress(data []byte) ([]byte, error) {
|
||||
var buf bytes.Buffer
|
||||
w, err := zstd.NewWriter(&buf, zstd.WithEncoderLevel(zstd.EncoderLevel(z.CompressionLevel)))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_, err = w.Write(data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
w.Close()
|
||||
return buf.Bytes(), nil
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
package data
|
||||
|
||||
import (
|
||||
"io"
|
||||
|
||||
"github.com/CalebQ42/squashfs/internal/decompress"
|
||||
"github.com/CalebQ42/squashfs/internal/toreader"
|
||||
)
|
||||
|
||||
type FullReader struct {
|
||||
r io.ReaderAt
|
||||
d decompress.Decompressor
|
||||
fragRdr io.Reader
|
||||
sizes []uint32
|
||||
blockSize uint32
|
||||
start uint64
|
||||
}
|
||||
|
||||
func NewFullReader(r io.ReaderAt, start uint64, d decompress.Decompressor, blockSizes []uint32, blockSize uint32) *FullReader {
|
||||
return &FullReader{
|
||||
r: r,
|
||||
start: start,
|
||||
blockSize: blockSize,
|
||||
sizes: blockSizes,
|
||||
d: d,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *FullReader) AddFragment(rdr io.Reader) {
|
||||
r.fragRdr = rdr
|
||||
}
|
||||
|
||||
type outDat struct {
|
||||
err error
|
||||
data []byte
|
||||
i int
|
||||
}
|
||||
|
||||
func (r FullReader) process(index int, offset int64, out chan outDat) {
|
||||
var err error
|
||||
var dat []byte
|
||||
size := realSize(r.sizes[index])
|
||||
rdr := io.LimitReader(toreader.NewReader(r.r, offset), int64(size))
|
||||
if size == r.sizes[index] {
|
||||
rdr, err = r.d.Reader(rdr)
|
||||
}
|
||||
if err == nil {
|
||||
dat, err = io.ReadAll(rdr)
|
||||
}
|
||||
out <- outDat{
|
||||
i: index,
|
||||
err: err,
|
||||
data: dat,
|
||||
}
|
||||
if clr, ok := rdr.(io.Closer); ok {
|
||||
clr.Close()
|
||||
}
|
||||
}
|
||||
|
||||
func (r FullReader) WriteTo(w io.Writer) (n int64, err error) {
|
||||
out := make(chan outDat, len(r.sizes))
|
||||
offset := r.start
|
||||
num := len(r.sizes)
|
||||
for i := 0; i < num; i++ {
|
||||
if i == num-1 && r.fragRdr != nil {
|
||||
go func() {
|
||||
dat, e := io.ReadAll(r.fragRdr)
|
||||
out <- outDat{
|
||||
i: num,
|
||||
err: e,
|
||||
data: dat,
|
||||
}
|
||||
if clr, ok := r.fragRdr.(io.Closer); ok {
|
||||
clr.Close()
|
||||
}
|
||||
}()
|
||||
continue
|
||||
}
|
||||
go r.process(i, int64(offset), out)
|
||||
offset += uint64(realSize(r.sizes[i]))
|
||||
}
|
||||
cache := make(map[int]outDat)
|
||||
var tmpN int
|
||||
for cur := 0; cur < num; {
|
||||
dat := <-out
|
||||
if dat.err != nil {
|
||||
err = dat.err
|
||||
return
|
||||
}
|
||||
if dat.i != cur {
|
||||
cache[dat.i] = dat
|
||||
continue
|
||||
}
|
||||
tmpN, err = w.Write(dat.data)
|
||||
n += int64(tmpN)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
cur++
|
||||
var ok bool
|
||||
for {
|
||||
dat, ok = cache[cur]
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
tmpN, err = w.Write(dat.data)
|
||||
n += int64(tmpN)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
cur++
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
package data
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
|
||||
"github.com/CalebQ42/squashfs/internal/decompress"
|
||||
)
|
||||
|
||||
type Reader struct {
|
||||
master io.Reader
|
||||
cur io.Reader
|
||||
fragRdr io.Reader
|
||||
d decompress.Decompressor
|
||||
blockSizes []uint32
|
||||
blockSize uint32
|
||||
}
|
||||
|
||||
func NewReader(r io.Reader, d decompress.Decompressor, blockSizes []uint32, blockSize uint32) (*Reader, error) {
|
||||
var out Reader
|
||||
out.d = d
|
||||
out.master = r
|
||||
out.blockSizes = blockSizes
|
||||
out.blockSize = blockSize
|
||||
err := out.advance()
|
||||
return &out, err
|
||||
}
|
||||
|
||||
func (r *Reader) AddFragment(rdr io.Reader) {
|
||||
r.fragRdr = rdr
|
||||
}
|
||||
|
||||
func realSize(siz uint32) uint32 {
|
||||
return siz &^ (1 << 24)
|
||||
}
|
||||
|
||||
func (r *Reader) advance() (err error) {
|
||||
if clr, ok := r.cur.(io.Closer); ok {
|
||||
clr.Close()
|
||||
}
|
||||
if len(r.blockSizes) == 0 {
|
||||
return io.EOF
|
||||
}
|
||||
if len(r.blockSizes) == 1 && r.fragRdr != nil {
|
||||
r.cur = r.fragRdr
|
||||
} else {
|
||||
size := realSize(r.blockSizes[0])
|
||||
if size == 0 {
|
||||
r.cur = bytes.NewReader(make([]byte, r.blockSize))
|
||||
} else {
|
||||
r.cur = io.LimitReader(r.master, int64(size))
|
||||
if size == r.blockSizes[0] {
|
||||
r.cur, err = r.d.Reader(r.cur)
|
||||
}
|
||||
}
|
||||
}
|
||||
r.blockSizes = r.blockSizes[1:]
|
||||
return
|
||||
}
|
||||
|
||||
func (r *Reader) Read(p []byte) (n int, err error) {
|
||||
n, err = r.cur.Read(p)
|
||||
if err == io.EOF {
|
||||
err = r.advance()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
var tmpN int
|
||||
tmp := make([]byte, len(p)-n)
|
||||
tmpN, err = r.Read(tmp)
|
||||
for i := range tmp {
|
||||
p[n+i] = tmp[i]
|
||||
}
|
||||
n += tmpN
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package decompress
|
||||
|
||||
import (
|
||||
"io"
|
||||
|
||||
"github.com/klauspost/compress/zlib"
|
||||
)
|
||||
|
||||
type GZip struct{}
|
||||
|
||||
func (g GZip) Reader(src io.Reader) (io.ReadCloser, error) {
|
||||
return zlib.NewReader(src)
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package decompress
|
||||
|
||||
import "io"
|
||||
|
||||
type Decompressor interface {
|
||||
Reader(src io.Reader) (io.ReadCloser, error)
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package decompress
|
||||
|
||||
import (
|
||||
"io"
|
||||
|
||||
"github.com/pierrec/lz4/v4"
|
||||
)
|
||||
|
||||
type Lz4 struct{}
|
||||
|
||||
func (l Lz4) Reader(r io.Reader) (io.ReadCloser, error) {
|
||||
return io.NopCloser(lz4.NewReader(r)), nil
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package decompress
|
||||
|
||||
import (
|
||||
"io"
|
||||
|
||||
"github.com/ulikunitz/xz/lzma"
|
||||
)
|
||||
|
||||
type Lzma struct{}
|
||||
|
||||
func (l Lzma) Reader(r io.Reader) (io.ReadCloser, error) {
|
||||
rdr, err := lzma.NewReader(r)
|
||||
return io.NopCloser(rdr), err
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package decompress
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
|
||||
"github.com/rasky/go-lzo"
|
||||
)
|
||||
|
||||
type Lzo struct{}
|
||||
|
||||
func (l Lzo) Reader(r io.Reader) (io.ReadCloser, error) {
|
||||
cache, err := lzo.Decompress1X(r, 0, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return io.NopCloser(bytes.NewReader(cache)), nil
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package decompress
|
||||
|
||||
import (
|
||||
"io"
|
||||
|
||||
"github.com/therootcompany/xz"
|
||||
)
|
||||
|
||||
type Xz struct{}
|
||||
|
||||
func (x Xz) Reader(r io.Reader) (io.ReadCloser, error) {
|
||||
rdr, err := xz.NewReader(r, 0)
|
||||
return io.NopCloser(rdr), err
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package decompress
|
||||
|
||||
import (
|
||||
"io"
|
||||
|
||||
"github.com/klauspost/compress/zstd"
|
||||
)
|
||||
|
||||
type Zstd struct{}
|
||||
|
||||
func (z Zstd) Reader(src io.Reader) (io.ReadCloser, error) {
|
||||
r, err := zstd.NewReader(src)
|
||||
return r.IOReadCloser(), err
|
||||
}
|
||||
@@ -6,82 +6,73 @@ import (
|
||||
"io"
|
||||
)
|
||||
|
||||
//Header is the header for a directory in the directory table
|
||||
type Header struct {
|
||||
Count uint32
|
||||
InodeOffset uint32
|
||||
InodeNumber uint32
|
||||
type header struct {
|
||||
Entries uint32
|
||||
InodeStart uint32
|
||||
Num uint32
|
||||
}
|
||||
|
||||
//EntryRaw is the values that can be easily decoded
|
||||
type EntryRaw struct {
|
||||
Offset uint16
|
||||
InodeOffset int16
|
||||
Type uint16
|
||||
NameSize uint16
|
||||
type entryInit struct {
|
||||
Offset uint16
|
||||
NumOffset int16
|
||||
Type uint16
|
||||
NameSize uint16
|
||||
}
|
||||
|
||||
type entry struct {
|
||||
entryInit
|
||||
Name []byte
|
||||
}
|
||||
|
||||
//Entry is an entry in a directory.
|
||||
type Entry struct {
|
||||
Name string
|
||||
InodeOffset uint32
|
||||
InodeBlockOffset uint16
|
||||
Type uint16
|
||||
Name string
|
||||
BlockStart uint32
|
||||
Type uint16
|
||||
Offset uint16
|
||||
}
|
||||
|
||||
//NewEntry creates a new directory entry
|
||||
func NewEntry(rdr io.Reader) (*Entry, error) {
|
||||
var raw EntryRaw
|
||||
err := binary.Read(rdr, binary.LittleEndian, &raw)
|
||||
func readEntry(r io.Reader) (e entry, err error) {
|
||||
err = binary.Read(r, binary.LittleEndian, &e.entryInit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
tmp := make([]byte, raw.NameSize+1)
|
||||
err = binary.Read(rdr, binary.LittleEndian, &tmp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Entry{
|
||||
InodeBlockOffset: raw.Offset,
|
||||
Type: raw.Type,
|
||||
Name: string(tmp),
|
||||
}, nil
|
||||
}
|
||||
|
||||
//NewDirectory reads the directory from rdr
|
||||
func NewDirectory(base io.Reader, size uint32) (entries []*Entry, err error) {
|
||||
tmp := make([]byte, size)
|
||||
base.Read(tmp)
|
||||
rdr := bytes.NewBuffer(tmp)
|
||||
for {
|
||||
var hdr Header
|
||||
err = binary.Read(rdr, binary.LittleEndian, &hdr)
|
||||
if err == io.ErrUnexpectedEOF {
|
||||
err = nil
|
||||
break
|
||||
} else if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
hdr.Count++
|
||||
headers := hdr.Count / 256
|
||||
if hdr.Count%256 > 0 {
|
||||
headers++
|
||||
}
|
||||
for i := uint32(0); i < hdr.Count; i++ {
|
||||
if i != 0 && i%256 == 0 {
|
||||
err = binary.Read(rdr, binary.LittleEndian, &hdr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
var ent *Entry
|
||||
ent, err = NewEntry(rdr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ent.InodeOffset = hdr.InodeOffset
|
||||
entries = append(entries, ent)
|
||||
}
|
||||
return
|
||||
}
|
||||
e.Name = make([]byte, e.NameSize+1)
|
||||
err = binary.Read(r, binary.LittleEndian, &e.Name)
|
||||
return
|
||||
}
|
||||
|
||||
func ReadEntries(rdr io.Reader, size uint32) (e []Entry, err error) {
|
||||
dat := make([]byte, size)
|
||||
rdr.Read(dat)
|
||||
r := bytes.NewReader(dat)
|
||||
var h header
|
||||
var en entry
|
||||
for {
|
||||
err = binary.Read(r, binary.LittleEndian, &h)
|
||||
if err == io.ErrUnexpectedEOF {
|
||||
err = nil
|
||||
return
|
||||
} else if err != nil {
|
||||
return
|
||||
}
|
||||
h.Entries++
|
||||
for i := 0; i < int(h.Entries); i++ {
|
||||
if i != 0 && i%256 == 0 {
|
||||
err = binary.Read(r, binary.LittleEndian, &h)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
en, err = readEntry(r)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
e = append(e, Entry{
|
||||
Name: string(en.Name),
|
||||
BlockStart: h.InodeStart,
|
||||
Type: en.Type,
|
||||
Offset: en.Offset,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
package inode
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"io"
|
||||
)
|
||||
|
||||
type Directory struct {
|
||||
BlockStart uint32
|
||||
LinkCount uint32
|
||||
Size uint16
|
||||
Offset uint16
|
||||
ParentNum uint32
|
||||
}
|
||||
|
||||
type eDirectoryInit struct {
|
||||
LinkCount uint32
|
||||
Size uint32
|
||||
BlockStart uint32
|
||||
ParentNum uint32
|
||||
IndCount uint16
|
||||
Offset uint16
|
||||
XattrInd uint32
|
||||
}
|
||||
|
||||
type EDirectory struct {
|
||||
eDirectoryInit
|
||||
Indexes []DirectoryIndex
|
||||
}
|
||||
|
||||
type directoryIndexInit struct {
|
||||
Ind uint32
|
||||
Start uint32
|
||||
NameSize uint32
|
||||
}
|
||||
|
||||
type DirectoryIndex struct {
|
||||
directoryIndexInit
|
||||
Name []byte
|
||||
}
|
||||
|
||||
func ReadDir(r io.Reader) (d Directory, err error) {
|
||||
err = binary.Read(r, binary.LittleEndian, &d)
|
||||
return
|
||||
}
|
||||
|
||||
func ReadEDir(r io.Reader) (d EDirectory, err error) {
|
||||
err = binary.Read(r, binary.LittleEndian, &d.eDirectoryInit)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
d.Indexes = make([]DirectoryIndex, d.IndCount)
|
||||
for i := range d.Indexes {
|
||||
err = binary.Read(r, binary.LittleEndian, &d.Indexes[i].directoryIndexInit)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
d.Indexes[i].Name = make([]byte, d.Indexes[i].NameSize+1)
|
||||
err = binary.Read(r, binary.LittleEndian, &d.Indexes[i].Name)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package inode
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"io"
|
||||
"math"
|
||||
)
|
||||
|
||||
type fileInit struct {
|
||||
BlockStart uint32
|
||||
FragInd uint32
|
||||
Offset uint32
|
||||
Size uint32
|
||||
}
|
||||
|
||||
type File struct {
|
||||
fileInit
|
||||
BlockSizes []uint32
|
||||
}
|
||||
|
||||
type eFileInit struct {
|
||||
BlockStart uint32
|
||||
Size uint64
|
||||
Sparse uint64
|
||||
LinkCount uint32
|
||||
FragInd uint32
|
||||
Offset uint32
|
||||
XattrInd uint32
|
||||
}
|
||||
|
||||
type EFile struct {
|
||||
eFileInit
|
||||
BlockSizes []uint32
|
||||
}
|
||||
|
||||
func ReadFile(r io.Reader, blockSize uint32) (f File, err error) {
|
||||
err = binary.Read(r, binary.LittleEndian, &f.fileInit)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
f.BlockSizes = make([]uint32, int(math.Ceil(float64(f.Size)/float64(blockSize))))
|
||||
err = binary.Read(r, binary.LittleEndian, &f.BlockSizes)
|
||||
return
|
||||
}
|
||||
|
||||
func ReadEFile(r io.Reader, blockSize uint32) (f EFile, err error) {
|
||||
err = binary.Read(r, binary.LittleEndian, &f.eFileInit)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
f.BlockSizes = make([]uint32, int(math.Ceil(float64(f.Size)/float64(blockSize))))
|
||||
err = binary.Read(r, binary.LittleEndian, &f.BlockSizes)
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package inode
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"io"
|
||||
)
|
||||
|
||||
const (
|
||||
Dir = uint16(iota + 1)
|
||||
Fil
|
||||
Sym
|
||||
Block
|
||||
Char
|
||||
Fifo
|
||||
Sock
|
||||
EDir
|
||||
EFil
|
||||
ESym
|
||||
EBlock
|
||||
EChar
|
||||
EFifo
|
||||
ESock
|
||||
)
|
||||
|
||||
type Header struct {
|
||||
Type uint16
|
||||
Perm uint16
|
||||
UidInd uint16
|
||||
GidInd uint16
|
||||
ModTime uint32
|
||||
Num uint32
|
||||
}
|
||||
|
||||
type Inode struct {
|
||||
Header
|
||||
Data any
|
||||
}
|
||||
|
||||
func Read(r io.Reader, blockSize uint32) (i Inode, err error) {
|
||||
err = binary.Read(r, binary.LittleEndian, &i.Header)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
switch i.Type {
|
||||
case Dir:
|
||||
i.Data, err = ReadDir(r)
|
||||
case Fil:
|
||||
i.Data, err = ReadFile(r, blockSize)
|
||||
case Sym:
|
||||
i.Data, err = ReadSym(r)
|
||||
case Block:
|
||||
fallthrough
|
||||
case Char:
|
||||
i.Data, err = ReadDevice(r)
|
||||
case Fifo:
|
||||
fallthrough
|
||||
case Sock:
|
||||
i.Data, err = ReadIPC(r)
|
||||
case EDir:
|
||||
i.Data, err = ReadEDir(r)
|
||||
case EFil:
|
||||
i.Data, err = ReadEFile(r, blockSize)
|
||||
case ESym:
|
||||
i.Data, err = ReadESym(r)
|
||||
case EBlock:
|
||||
fallthrough
|
||||
case EChar:
|
||||
i.Data, err = ReadEDevice(r)
|
||||
case EFifo:
|
||||
fallthrough
|
||||
case ESock:
|
||||
i.Data, err = ReadEIPC(r)
|
||||
default:
|
||||
return i, errors.New("invalid inode type")
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -1,257 +0,0 @@
|
||||
package inode
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"io"
|
||||
)
|
||||
|
||||
//The different types of inodes as defined by inodetype
|
||||
const (
|
||||
DirType = iota + 1
|
||||
FileType
|
||||
SymType
|
||||
BlockDevType
|
||||
CharDevType
|
||||
FifoType
|
||||
SocketType
|
||||
ExtDirType
|
||||
ExtFileType
|
||||
ExtSymType
|
||||
ExtBlockDeviceType
|
||||
ExtCharDeviceType
|
||||
ExtFifoType
|
||||
ExtSocketType
|
||||
)
|
||||
|
||||
//Header is the common header for all inodes
|
||||
type Header struct {
|
||||
Type uint16
|
||||
Permissions uint16
|
||||
UID uint16
|
||||
GID uint16
|
||||
ModifiedTime uint32
|
||||
Number uint32
|
||||
}
|
||||
|
||||
//Dir is self explainatory
|
||||
type Dir struct {
|
||||
DirectoryIndex uint32
|
||||
HardLinks uint32
|
||||
DirectorySize uint16
|
||||
DirectoryOffset uint16
|
||||
ParentInodeNumber uint32
|
||||
}
|
||||
|
||||
//ExtDirInit is the information that can be directoy decoded
|
||||
type ExtDirInit struct {
|
||||
HardLinks uint32
|
||||
DirectorySize uint32
|
||||
DirectoryIndex uint32
|
||||
ParentInodeNumber uint32
|
||||
IndexCount uint16 //one less then directory indexes following structure
|
||||
DirectoryOffset uint16
|
||||
XattrIndex uint32
|
||||
}
|
||||
|
||||
//ExtDir is a directory with extra info
|
||||
type ExtDir struct {
|
||||
Indexes []DirIndex
|
||||
ExtDirInit
|
||||
}
|
||||
|
||||
//NewExtendedDirectory creates a new ExtendedDirectory
|
||||
func NewExtendedDirectory(rdr io.Reader) (ExtDir, error) {
|
||||
var inode ExtDir
|
||||
err := binary.Read(rdr, binary.LittleEndian, &inode.ExtDirInit)
|
||||
if err != nil {
|
||||
return inode, err
|
||||
}
|
||||
for i := uint16(0); i < inode.IndexCount; i++ {
|
||||
var tmp DirIndex
|
||||
tmp, err = NewDirectoryIndex(rdr)
|
||||
if err != nil {
|
||||
return inode, err
|
||||
}
|
||||
inode.Indexes = append(inode.Indexes, tmp)
|
||||
}
|
||||
return inode, err
|
||||
}
|
||||
|
||||
//DirIndexInit holds the values that can be easily decoded
|
||||
type DirIndexInit struct {
|
||||
Offset uint32
|
||||
DirTableOffset uint32
|
||||
NameSize uint32
|
||||
}
|
||||
|
||||
//DirIndex is a quick lookup provided by an ExtendedDirectory
|
||||
type DirIndex struct {
|
||||
Name string
|
||||
DirIndexInit
|
||||
}
|
||||
|
||||
//NewDirectoryIndex return a new DirectoryIndex
|
||||
func NewDirectoryIndex(rdr io.Reader) (DirIndex, error) {
|
||||
var index DirIndex
|
||||
err := binary.Read(rdr, binary.LittleEndian, &index.DirIndexInit)
|
||||
if err != nil {
|
||||
return index, err
|
||||
}
|
||||
tmp := make([]byte, index.NameSize+1, index.NameSize+1)
|
||||
err = binary.Read(rdr, binary.LittleEndian, &tmp)
|
||||
if err != nil {
|
||||
return index, err
|
||||
}
|
||||
index.Name = string(tmp)
|
||||
return index, nil
|
||||
}
|
||||
|
||||
//FileInit is the information that can be directly decoded
|
||||
type FileInit struct {
|
||||
BlockStart uint32
|
||||
FragmentIndex uint32
|
||||
FragmentOffset uint32
|
||||
Size uint32
|
||||
}
|
||||
|
||||
//File is self explainatory
|
||||
type File struct {
|
||||
BlockSizes []uint32
|
||||
Fragmented bool
|
||||
FileInit
|
||||
}
|
||||
|
||||
//NewFile creates a new File
|
||||
func NewFile(rdr io.Reader, blockSize uint32) (File, error) {
|
||||
var inode File
|
||||
err := binary.Read(rdr, binary.LittleEndian, &inode.FileInit)
|
||||
if err != nil {
|
||||
return inode, err
|
||||
}
|
||||
inode.Fragmented = inode.FragmentIndex != 0xFFFFFFFF
|
||||
blocks := inode.Size / blockSize
|
||||
if inode.Size%blockSize > 0 {
|
||||
blocks++
|
||||
}
|
||||
inode.BlockSizes = make([]uint32, blocks, blocks)
|
||||
err = binary.Read(rdr, binary.LittleEndian, &inode.BlockSizes)
|
||||
return inode, err
|
||||
}
|
||||
|
||||
//ExtFileInit is the information that can be directly decoded
|
||||
type ExtFileInit struct {
|
||||
BlockStart uint64
|
||||
Size uint64
|
||||
Sparse uint64
|
||||
HardLinks uint32
|
||||
FragmentIndex uint32
|
||||
FragmentOffset uint32
|
||||
XattrIndex uint32
|
||||
}
|
||||
|
||||
//ExtFile is a file with more information
|
||||
type ExtFile struct {
|
||||
BlockSizes []uint32
|
||||
Fragmented bool
|
||||
ExtFileInit
|
||||
}
|
||||
|
||||
//NewExtendedFile creates a new ExtendedFile
|
||||
func NewExtendedFile(rdr io.Reader, blockSize uint32) (ExtFile, error) {
|
||||
var inode ExtFile
|
||||
err := binary.Read(rdr, binary.LittleEndian, &inode.ExtFileInit)
|
||||
if err != nil {
|
||||
return inode, err
|
||||
}
|
||||
inode.Fragmented = inode.FragmentIndex != 0xFFFFFFFF
|
||||
blocks := inode.Size / uint64(blockSize)
|
||||
if inode.Size%uint64(blockSize) > 0 {
|
||||
blocks++
|
||||
}
|
||||
inode.BlockSizes = make([]uint32, blocks, blocks)
|
||||
err = binary.Read(rdr, binary.LittleEndian, &inode.BlockSizes)
|
||||
return inode, err
|
||||
}
|
||||
|
||||
//SymInit is all the values that can be directly decoded
|
||||
type SymInit struct {
|
||||
HardLinks uint32
|
||||
TargetPathSize uint32
|
||||
}
|
||||
|
||||
//Sym is a symlink
|
||||
type Sym struct {
|
||||
Path string
|
||||
targetPath []byte //len is TargetPathSize
|
||||
SymInit
|
||||
}
|
||||
|
||||
//NewSymlink creates a new Symlink
|
||||
func NewSymlink(rdr io.Reader) (Sym, error) {
|
||||
var inode Sym
|
||||
err := binary.Read(rdr, binary.LittleEndian, &inode.SymInit)
|
||||
if err != nil {
|
||||
return inode, err
|
||||
}
|
||||
inode.targetPath = make([]byte, inode.TargetPathSize, inode.TargetPathSize)
|
||||
err = binary.Read(rdr, binary.LittleEndian, &inode.targetPath)
|
||||
if err != nil {
|
||||
return inode, err
|
||||
}
|
||||
inode.Path = string(inode.targetPath)
|
||||
return inode, err
|
||||
}
|
||||
|
||||
//ExtSymInit is all the values that can be directly decoded
|
||||
type ExtSymInit struct {
|
||||
HardLinks uint32
|
||||
TargetPathSize uint32
|
||||
}
|
||||
|
||||
//ExtSym is a symlink with extra information
|
||||
type ExtSym struct {
|
||||
Path string
|
||||
targetPath []uint8
|
||||
ExtSymInit
|
||||
XattrIndex uint32
|
||||
}
|
||||
|
||||
//NewExtendedSymlink creates a new ExtendedSymlink
|
||||
func NewExtendedSymlink(rdr io.Reader) (ExtSym, error) {
|
||||
var inode ExtSym
|
||||
err := binary.Read(rdr, binary.LittleEndian, &inode.ExtSymInit)
|
||||
if err != nil {
|
||||
return inode, err
|
||||
}
|
||||
inode.targetPath = make([]uint8, inode.TargetPathSize, inode.TargetPathSize)
|
||||
err = binary.Read(rdr, binary.LittleEndian, &inode.targetPath)
|
||||
if err != nil {
|
||||
return inode, err
|
||||
}
|
||||
inode.Path = string(inode.targetPath)
|
||||
err = binary.Read(rdr, binary.LittleEndian, &inode.XattrIndex)
|
||||
return inode, err
|
||||
}
|
||||
|
||||
//Device is a device
|
||||
type Device struct {
|
||||
HardLinks uint32
|
||||
Device uint32
|
||||
}
|
||||
|
||||
//ExtDevice is a device with more info
|
||||
type ExtDevice struct {
|
||||
Device
|
||||
XattrIndex uint32
|
||||
}
|
||||
|
||||
//IPC is a Fifo or Socket device
|
||||
type IPC struct {
|
||||
HardLink uint32
|
||||
}
|
||||
|
||||
//ExtIPC is a IPC device with extra info
|
||||
type ExtIPC struct {
|
||||
IPC
|
||||
XattrIndex uint32
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package inode
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"io"
|
||||
)
|
||||
|
||||
type Device struct {
|
||||
LinkCount uint32
|
||||
Dev uint32
|
||||
}
|
||||
|
||||
type EDevice struct {
|
||||
Device
|
||||
XattrInd uint32
|
||||
}
|
||||
|
||||
func ReadDevice(r io.Reader) (d Device, err error) {
|
||||
err = binary.Read(r, binary.LittleEndian, &d)
|
||||
return
|
||||
}
|
||||
|
||||
func ReadEDevice(r io.Reader) (d EDevice, err error) {
|
||||
err = binary.Read(r, binary.LittleEndian, &d)
|
||||
return
|
||||
}
|
||||
|
||||
type IPC struct {
|
||||
LinkCount uint32
|
||||
}
|
||||
|
||||
type EIPC struct {
|
||||
IPC
|
||||
XattrInd uint32
|
||||
}
|
||||
|
||||
func ReadIPC(r io.Reader) (i IPC, err error) {
|
||||
err = binary.Read(r, binary.LittleEndian, &i)
|
||||
return
|
||||
}
|
||||
|
||||
func ReadEIPC(r io.Reader) (i EIPC, err error) {
|
||||
err = binary.Read(r, binary.LittleEndian, &i)
|
||||
return
|
||||
}
|
||||
@@ -1,128 +0,0 @@
|
||||
package inode
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"io"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
//Inode holds an inode. Header is the header that's common for all inodes.
|
||||
//
|
||||
//Info holds the actual Inode. Due to each inode type being a different type, it's store as an interface{}
|
||||
type Inode struct {
|
||||
Header
|
||||
Info interface{} //Info is the parsed specific data. It's type is defined by Type.
|
||||
}
|
||||
|
||||
//ProcessInode tries to read an inode from the BlockReader
|
||||
func ProcessInode(br io.Reader, blockSize uint32) (*Inode, error) {
|
||||
var in Inode
|
||||
err := binary.Read(br, binary.LittleEndian, &in.Header)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
switch in.Type {
|
||||
case DirType:
|
||||
var inode Dir
|
||||
err = binary.Read(br, binary.LittleEndian, &inode)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
in.Info = inode
|
||||
case FileType:
|
||||
var inode File
|
||||
inode, err = NewFile(br, blockSize)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
in.Info = inode
|
||||
case SymType:
|
||||
var inode Sym
|
||||
inode, err = NewSymlink(br)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
in.Info = inode
|
||||
case BlockDevType:
|
||||
var inode Device
|
||||
err = binary.Read(br, binary.LittleEndian, &inode)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
in.Info = inode
|
||||
case CharDevType:
|
||||
var inode Device
|
||||
err = binary.Read(br, binary.LittleEndian, &inode)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
in.Info = inode
|
||||
case FifoType:
|
||||
var inode IPC
|
||||
err = binary.Read(br, binary.LittleEndian, &inode)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
in.Info = inode
|
||||
case SocketType:
|
||||
var inode IPC
|
||||
err = binary.Read(br, binary.LittleEndian, &inode)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
in.Info = inode
|
||||
case ExtDirType:
|
||||
var inode ExtDir
|
||||
inode, err = NewExtendedDirectory(br)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
in.Info = inode
|
||||
case ExtFileType:
|
||||
var inode ExtFile
|
||||
inode, err = NewExtendedFile(br, blockSize)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
in.Info = inode
|
||||
case ExtSymType:
|
||||
var inode ExtSym
|
||||
inode, err = NewExtendedSymlink(br)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
in.Info = inode
|
||||
case ExtBlockDeviceType:
|
||||
var inode ExtDevice
|
||||
err = binary.Read(br, binary.LittleEndian, &inode)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
in.Info = inode
|
||||
case ExtCharDeviceType:
|
||||
var inode ExtDevice
|
||||
err = binary.Read(br, binary.LittleEndian, &inode)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
in.Info = inode
|
||||
case ExtFifoType:
|
||||
var inode ExtIPC
|
||||
err = binary.Read(br, binary.LittleEndian, &inode)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
in.Info = inode
|
||||
case ExtSocketType:
|
||||
var inode ExtIPC
|
||||
err = binary.Read(br, binary.LittleEndian, &inode)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
in.Info = inode
|
||||
default:
|
||||
return nil, errors.New("Unsupported inode type: " + strconv.Itoa(int(in.Type)))
|
||||
}
|
||||
return &in, nil
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package inode
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"io"
|
||||
)
|
||||
|
||||
type symlinkInit struct {
|
||||
LinkCount uint32
|
||||
TargetSize uint32
|
||||
}
|
||||
|
||||
type Symlink struct {
|
||||
symlinkInit
|
||||
Target []byte
|
||||
}
|
||||
|
||||
type ESymlink struct {
|
||||
symlinkInit
|
||||
Target []byte
|
||||
XattrInd uint32
|
||||
}
|
||||
|
||||
func ReadSym(r io.Reader) (s Symlink, err error) {
|
||||
err = binary.Read(r, binary.LittleEndian, &s.symlinkInit)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
s.Target = make([]byte, s.TargetSize)
|
||||
err = binary.Read(r, binary.LittleEndian, &s.Target)
|
||||
return
|
||||
}
|
||||
|
||||
func ReadESym(r io.Reader) (s ESymlink, err error) {
|
||||
err = binary.Read(r, binary.LittleEndian, &s.symlinkInit)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
s.Target = make([]byte, s.TargetSize)
|
||||
err = binary.Read(r, binary.LittleEndian, &s.Target)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = binary.Read(r, binary.LittleEndian, &s.XattrInd)
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package metadata
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"io"
|
||||
|
||||
"github.com/CalebQ42/squashfs/internal/decompress"
|
||||
)
|
||||
|
||||
type Reader struct {
|
||||
master io.Reader
|
||||
cur io.Reader
|
||||
d decompress.Decompressor
|
||||
}
|
||||
|
||||
func NewReader(r io.Reader, d decompress.Decompressor) (*Reader, error) {
|
||||
var out Reader
|
||||
out.d = d
|
||||
out.master = r
|
||||
return &out, out.Advance()
|
||||
}
|
||||
|
||||
func (r *Reader) Advance() error {
|
||||
if clr, ok := r.cur.(io.Closer); ok {
|
||||
clr.Close()
|
||||
}
|
||||
var size uint16
|
||||
err := binary.Read(r.master, binary.LittleEndian, &size)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
comp := size&0x8000 != 0x8000
|
||||
size &^= 0x8000
|
||||
r.cur = io.LimitReader(r.master, int64(size))
|
||||
if comp {
|
||||
r.cur, err = r.d.Reader(r.cur)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r Reader) Read(p []byte) (n int, err error) {
|
||||
n, err = r.cur.Read(p)
|
||||
if err == io.EOF {
|
||||
err = r.Advance()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
var tmpN int
|
||||
tmp := make([]byte, len(p)-n)
|
||||
tmpN, err = r.Read(tmp)
|
||||
for i := range tmp {
|
||||
p[n+i] = tmp[i]
|
||||
}
|
||||
n += tmpN
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -1,135 +0,0 @@
|
||||
package rawreader
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"io"
|
||||
)
|
||||
|
||||
func ConvertReader(r io.Reader) (RawReader, error) {
|
||||
if rr, ok := r.(RawReader); ok {
|
||||
return rr, nil
|
||||
}
|
||||
if rs, is := r.(io.ReadSeeker); is {
|
||||
return &fromReadSeeker{
|
||||
ReadSeeker: rs,
|
||||
}, nil
|
||||
}
|
||||
buf := new(bytes.Buffer)
|
||||
_, err := io.Copy(buf, r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &fromReader{
|
||||
data: buf.Bytes(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func ConvertReaderAt(r io.ReaderAt) RawReader {
|
||||
if rr, ok := r.(RawReader); ok {
|
||||
return rr
|
||||
}
|
||||
return &fromReaderAt{
|
||||
ReaderAt: r,
|
||||
}
|
||||
}
|
||||
|
||||
//TODO: Add way to discard data from fromReader
|
||||
//RawReader implements the needed interfaces for reading a squashfs archive.
|
||||
type RawReader interface {
|
||||
io.ReadSeeker
|
||||
io.ReaderAt
|
||||
}
|
||||
|
||||
type fromReader struct {
|
||||
data []byte
|
||||
off int
|
||||
}
|
||||
|
||||
func (r *fromReader) ReadAt(p []byte, off int64) (n int, err error) {
|
||||
n = len(p)
|
||||
if int(off)+len(p) > len(r.data) {
|
||||
n = len(r.data) - int(off)
|
||||
err = io.EOF
|
||||
}
|
||||
if n < 0 {
|
||||
n = 0
|
||||
}
|
||||
for i := 0; i < n; i++ {
|
||||
p[i] = r.data[int(off)+i]
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (r *fromReader) Seek(off int64, whence int) (n int64, err error) {
|
||||
switch whence {
|
||||
case io.SeekEnd:
|
||||
r.off = len(r.data) - int(off)
|
||||
if r.off < 0 {
|
||||
r.off = 0
|
||||
err = io.EOF
|
||||
}
|
||||
case io.SeekCurrent:
|
||||
r.off += int(off)
|
||||
case io.SeekStart:
|
||||
r.off = int(off)
|
||||
}
|
||||
if r.off > len(r.data) {
|
||||
r.off = len(r.data)
|
||||
return int64(r.off), io.EOF
|
||||
}
|
||||
return int64(r.off), err
|
||||
}
|
||||
|
||||
func (r *fromReader) Read(p []byte) (n int, err error) {
|
||||
n = len(p)
|
||||
if r.off+len(p) > len(r.data) {
|
||||
n = len(r.data) - r.off
|
||||
err = io.EOF
|
||||
}
|
||||
if n < 0 {
|
||||
n = 0
|
||||
}
|
||||
for i := 0; i < n; i++ {
|
||||
p[i] = r.data[r.off+i]
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
type fromReadSeeker struct {
|
||||
io.ReadSeeker
|
||||
}
|
||||
|
||||
func (r *fromReadSeeker) ReadAt(p []byte, off int64) (n int, err error) {
|
||||
tmp, _ := r.Seek(0, io.SeekCurrent)
|
||||
defer r.Seek(tmp, io.SeekStart)
|
||||
_, err = r.Seek(off, io.SeekStart)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return r.Read(p)
|
||||
}
|
||||
|
||||
type fromReaderAt struct {
|
||||
io.ReaderAt
|
||||
|
||||
off int
|
||||
}
|
||||
|
||||
func (r *fromReaderAt) Read(p []byte) (n int, err error) {
|
||||
n, err = r.ReadAt(p, int64(r.off))
|
||||
r.off += n
|
||||
return
|
||||
}
|
||||
|
||||
func (r *fromReaderAt) Seek(off int64, whence int) (n int64, err error) {
|
||||
switch whence {
|
||||
case io.SeekEnd:
|
||||
return 0, errors.New("cannot SeekEnd RawReader")
|
||||
case io.SeekCurrent:
|
||||
r.off += int(off)
|
||||
case io.SeekStart:
|
||||
r.off = int(off)
|
||||
}
|
||||
return int64(r.off), nil
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package toreader
|
||||
|
||||
import "io"
|
||||
|
||||
type Reader struct {
|
||||
r io.ReaderAt
|
||||
off int64
|
||||
}
|
||||
|
||||
func NewReader(r io.ReaderAt, start int64) *Reader {
|
||||
return &Reader{
|
||||
r: r,
|
||||
off: start,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Reader) Read(p []byte) (n int, err error) {
|
||||
n, err = r.r.ReadAt(p, r.off)
|
||||
r.off += int64(n)
|
||||
return
|
||||
}
|
||||
|
||||
func (r Reader) Offset() int64 {
|
||||
return r.off
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package toreader
|
||||
|
||||
import "io"
|
||||
|
||||
type ReaderAt struct {
|
||||
d []byte
|
||||
}
|
||||
|
||||
func NewReaderAt(r io.Reader) (ra ReaderAt, err error) {
|
||||
ra.d, err = io.ReadAll(r)
|
||||
return
|
||||
}
|
||||
|
||||
func (r ReaderAt) ReadAt(p []byte, off int64) (n int, err error) {
|
||||
if int(off) >= len(r.d) {
|
||||
return 0, io.EOF
|
||||
}
|
||||
n = copy(p, r.d[off:])
|
||||
if n != len(p) {
|
||||
err = io.EOF
|
||||
}
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user