Restarted some stuff so I can do it better.
Made a reader that can reade across data blocks if necessary Still can't get things to read right
This commit is contained in:
+28
-80
@@ -4,7 +4,7 @@ import (
|
||||
"compress/gzip"
|
||||
"io"
|
||||
|
||||
"gopkg.in/src-d/go-git.v4/utils/ioutil"
|
||||
"github.com/CalebQ42/GoSquashfs/bytereadwrite"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -18,19 +18,18 @@ const (
|
||||
|
||||
//TODO: implement for each type of Options
|
||||
type CompressionOptions interface {
|
||||
Decompress(*io.SectionReader, int) ([]byte, error)
|
||||
Decompress(io.Reader) ([]byte, error)
|
||||
DecompressCopy(*io.Reader, *io.Writer) (int, error)
|
||||
Compress(*io.SectionReader, int) ([]byte, error)
|
||||
Compress(*io.Reader) ([]byte, error)
|
||||
CompressCopy(*io.Reader, *io.Writer) (int, error)
|
||||
Reader(io.Reader) (*io.ReadCloser, error)
|
||||
}
|
||||
|
||||
//TODO: Allow creation of options for compression.
|
||||
|
||||
type gzipOptionsRaw struct {
|
||||
compressionLevel int32
|
||||
windowSize int16
|
||||
strategies int16
|
||||
CompressionLevel int32
|
||||
WindowSize int16
|
||||
Strategies int16
|
||||
}
|
||||
|
||||
//GzipOptions is the options used for gzip compression. Backed by the raw format, with strategies parsed.
|
||||
@@ -44,51 +43,6 @@ type GzipOptions struct {
|
||||
FixedStretegy bool
|
||||
}
|
||||
|
||||
type byteWriterReader struct {
|
||||
byts []byte
|
||||
offset int
|
||||
}
|
||||
|
||||
func newByteReadWrite(length int) *byteWriterReader {
|
||||
return &byteWriterReader{
|
||||
byts: make([]byte, length),
|
||||
offset: 0,
|
||||
}
|
||||
}
|
||||
|
||||
func newByteReadWriteFromBytes(byts []byte) *byteWriterReader {
|
||||
return &byteWriterReader{
|
||||
byts: byts,
|
||||
offset: 0,
|
||||
}
|
||||
}
|
||||
|
||||
func (bwr *byteWriterReader) getBytes() []byte {
|
||||
return bwr.byts
|
||||
}
|
||||
|
||||
//Read reads the bytes.
|
||||
func (bwr *byteWriterReader) Read(byt []byte) (int, error) {
|
||||
if len(bwr.byts) < bwr.offset+len(byt) {
|
||||
bytesWritten := len(bwr.byts) - bwr.offset
|
||||
for i := 0; i < bytesWritten; i++ {
|
||||
byt[i] = bwr.byts[i+bwr.offset]
|
||||
}
|
||||
return bytesWritten, io.EOF
|
||||
}
|
||||
for i := 0; i < len(byt); i++ {
|
||||
byt[i] = bwr.byts[bwr.offset+i]
|
||||
}
|
||||
bwr.offset += len(byt)
|
||||
return len(byt), nil
|
||||
}
|
||||
|
||||
//Write writes to the bytes. WILL expand to accept the incoming bytes.
|
||||
func (bwr *byteWriterReader) Write(byts []byte) (int, error) {
|
||||
bwr.byts = append(bwr.byts, byts...)
|
||||
return len(byts), nil
|
||||
}
|
||||
|
||||
func NewGzipOptions(raw gzipOptionsRaw) *GzipOptions {
|
||||
//TODO: parse strategies
|
||||
return &GzipOptions{
|
||||
@@ -96,18 +50,18 @@ func NewGzipOptions(raw gzipOptionsRaw) *GzipOptions {
|
||||
}
|
||||
}
|
||||
|
||||
func (gzipOp *GzipOptions) Decompress(rdr *io.SectionReader, blockSize int) ([]byte, error) {
|
||||
func (gzipOp *GzipOptions) Decompress(rdr io.Reader) ([]byte, error) {
|
||||
gzipRdr, err := gzip.NewReader(rdr)
|
||||
defer gzipRdr.Close()
|
||||
// defer gzipRdr.Close()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
bytrw := newByteReadWrite(0)
|
||||
bytrw := bytereadwrite.NewByteReaderWriter()
|
||||
_, err = io.Copy(bytrw, gzipRdr)
|
||||
if err != nil {
|
||||
return bytrw.byts, err
|
||||
return nil, err
|
||||
}
|
||||
return bytrw.byts, nil
|
||||
return bytrw.GetBytes(), nil
|
||||
}
|
||||
|
||||
func (gzipOp *GzipOptions) DecompressCopy(rdr *io.Reader, wrt *io.Writer) (int, error) {
|
||||
@@ -120,15 +74,15 @@ func (gzipOp *GzipOptions) DecompressCopy(rdr *io.Reader, wrt *io.Writer) (int,
|
||||
return int(n), err
|
||||
}
|
||||
|
||||
func (gzipOp *GzipOptions) Compress(rdr *io.SectionReader, blockSize int) ([]byte, error) {
|
||||
bytWrt := newByteReadWrite(0)
|
||||
func (gzipOp *GzipOptions) Compress(rdr *io.Reader) ([]byte, error) {
|
||||
bytWrt := bytereadwrite.NewByteReaderWriter()
|
||||
gzipWrt := gzip.NewWriter(bytWrt) //TODO: allow setting level
|
||||
defer gzipWrt.Close()
|
||||
_, err := io.Copy(gzipWrt, rdr)
|
||||
_, err := io.Copy(gzipWrt, *rdr)
|
||||
if err != nil {
|
||||
return bytWrt.byts, err
|
||||
return bytWrt.GetBytes(), err
|
||||
}
|
||||
return bytWrt.byts, nil
|
||||
return bytWrt.GetBytes(), nil
|
||||
}
|
||||
|
||||
func (gzipOp *GzipOptions) CompressCopy(rdr *io.Reader, wrt *io.Writer) (int, error) {
|
||||
@@ -138,15 +92,9 @@ func (gzipOp *GzipOptions) CompressCopy(rdr *io.Reader, wrt *io.Writer) (int, er
|
||||
return int(n), err
|
||||
}
|
||||
|
||||
func (gzipOp *GzipOptions) Reader(rdr io.Reader) (*io.ReadCloser, error) {
|
||||
read, err := gzip.NewReader(rdr)
|
||||
redClo := ioutil.NewReadCloser(read, read)
|
||||
return &redClo, err
|
||||
}
|
||||
|
||||
type xzOptionsRaw struct {
|
||||
dictionarySize int32
|
||||
executableFilters int32
|
||||
DictionarySize int32
|
||||
ExecutableFilters int32
|
||||
}
|
||||
|
||||
type XzOptions struct {
|
||||
@@ -163,18 +111,18 @@ type XzOptions struct {
|
||||
func NewXzOption(raw xzOptionsRaw) XzOptions {
|
||||
return XzOptions{
|
||||
raw: &raw,
|
||||
Execx86: raw.executableFilters&0x1 == 0x1,
|
||||
ExecPower: raw.executableFilters&0x2 == 0x2,
|
||||
Execa64: raw.executableFilters&0x4 == 0x4,
|
||||
ExecArm: raw.executableFilters&0x8 == 0x8,
|
||||
ExecArmThumb: raw.executableFilters&0x10 == 0x10,
|
||||
ExecSparc: raw.executableFilters&0x20 == 0x20,
|
||||
Execx86: raw.ExecutableFilters&0x1 == 0x1,
|
||||
ExecPower: raw.ExecutableFilters&0x2 == 0x2,
|
||||
Execa64: raw.ExecutableFilters&0x4 == 0x4,
|
||||
ExecArm: raw.ExecutableFilters&0x8 == 0x8,
|
||||
ExecArmThumb: raw.ExecutableFilters&0x10 == 0x10,
|
||||
ExecSparc: raw.ExecutableFilters&0x20 == 0x20,
|
||||
}
|
||||
}
|
||||
|
||||
type lz4OptionsRaw struct {
|
||||
version int32
|
||||
flags int32
|
||||
Version int32
|
||||
Flags int32
|
||||
}
|
||||
|
||||
//ZstdOptions is the options set for zstdOptions
|
||||
@@ -183,6 +131,6 @@ type ZstdOptions struct {
|
||||
}
|
||||
|
||||
type lzoOptionsRaw struct {
|
||||
algorithm int32
|
||||
compressionLevel int32
|
||||
Algorithm int32
|
||||
CompressionLevel int32
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user