-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extensions break on path change (Issue portapps/phyrox-portable#2)
- Loading branch information
Showing
4 changed files
with
150 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"encoding/binary" | ||
"io/ioutil" | ||
"os" | ||
|
||
"github.com/pierrec/lz4" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
const ( | ||
mozLz4Header = "mozLz40\x00" | ||
mozLz4DecompSize = 4 | ||
) | ||
|
||
func mozLz4Decompress(filename string) ([]byte, error) { | ||
file, err := os.Open(filename) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
header := make([]byte, len(mozLz4Header)) | ||
_, err = file.Read(header) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "couldn't read header") | ||
} | ||
if string(header) != mozLz4Header { | ||
return nil, errors.New("no mozLz4 header") | ||
} | ||
|
||
var size uint32 | ||
err = binary.Read(file, binary.LittleEndian, &size) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "couldn't read size") | ||
} | ||
|
||
src, err := ioutil.ReadAll(file) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "couldn't read compressed data") | ||
} | ||
|
||
out := make([]byte, size) | ||
_, err = lz4.UncompressBlock(src, out) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "couldn't decompress data") | ||
} | ||
|
||
return out, nil | ||
} | ||
|
||
func mozLz4Compress(src []byte) ([]byte, error) { | ||
out := new(bytes.Buffer) | ||
|
||
_, err := out.Write([]byte(mozLz4Header)) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "couldn't write header") | ||
} | ||
|
||
err = binary.Write(out, binary.LittleEndian, uint32(len(src))) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "couldn't encode length") | ||
} | ||
|
||
buf := make([]byte, 10*len(src)) | ||
sz, err := lz4.CompressBlockHC(src, buf, -1) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "couldn't compress data") | ||
} | ||
if sz == 0 { | ||
return nil, errors.New("data incompressible") | ||
} | ||
|
||
_, err = out.Write(buf[:sz]) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "couldn't write compressed data") | ||
} | ||
|
||
return out.Bytes(), nil | ||
} |