Skip to content

Commit

Permalink
migrate to arc
Browse files Browse the repository at this point in the history
  • Loading branch information
jm33-m0 committed Nov 23, 2024
1 parent 789a310 commit 08331e4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 37 deletions.
29 changes: 25 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,9 +1,30 @@
module github.com/jm33-m0/go-lpe

go 1.16
go 1.23.3

require (
github.com/creack/pty v1.1.17
github.com/dsnet/compress v0.0.2-0.20210315054119-f66993602bf5 // indirect
github.com/mholt/archiver/v4 v4.0.0-alpha.8
github.com/creack/pty v1.1.24
github.com/jm33-m0/arc v0.0.0-20241123070138-1a10ff9f0fe4
)

require (
github.com/STARRY-S/zip v0.2.1 // indirect
github.com/andybalholm/brotli v1.1.1 // indirect
github.com/bodgit/plumbing v1.3.0 // indirect
github.com/bodgit/sevenzip v1.6.0 // indirect
github.com/bodgit/windows v1.0.1 // indirect
github.com/dsnet/compress v0.0.2-0.20230904184137-39efe44ab707 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
github.com/klauspost/compress v1.17.11 // indirect
github.com/klauspost/pgzip v1.2.6 // indirect
github.com/mholt/archives v0.0.0-20241123043835-f28756b8d6c7 // indirect
github.com/nwaples/rardecode/v2 v2.0.0-beta.4.0.20241112120701-034e449c6e78 // indirect
github.com/pierrec/lz4/v4 v4.1.21 // indirect
github.com/sorairolake/lzip-go v0.3.5 // indirect
github.com/therootcompany/xz v1.0.1 // indirect
github.com/ulikunitz/xz v0.5.12 // indirect
go4.org v0.0.0-20230225012048-214862532bf5 // indirect
golang.org/x/text v0.20.0 // indirect
)
37 changes: 4 additions & 33 deletions util.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package golpe

import (
"bytes"
"encoding/base64"
"fmt"
"log"

"github.com/mholt/archiver/v4"
"github.com/jm33-m0/arc"
)

// Base64Encode encodes a byte slice to a base64 URL-encoded string
Expand All @@ -26,24 +25,11 @@ func Base64Decode(text string) []byte {

// Bin2String compresses a binary file and encodes it with base64
func Bin2String(data []byte) (res string) {
var compressedBuf bytes.Buffer
// Wrap the underlying writer with BZ2 compressor
compressor, err := archiver.Bz2{}.OpenWriter(&compressedBuf)
compressedBin, err := arc.Compress(data, arc.CompressionMap["bz2"])
if err != nil {
log.Printf("Bin2String: %v", err)
return
}
defer compressor.Close()

// Compress the data
_, err = compressor.Write(data)
if err != nil {
log.Printf("Bin2String: Write to compressor failed: %v", err)
return
}

// Get the compressed binary data
compressedBin := compressedBuf.Bytes()

// Encode the compressed data to base64
res = Base64Encode(compressedBin)
Expand All @@ -55,28 +41,13 @@ func Bin2String(data []byte) (res string) {
}

// ExtractFileFromString base64 decodes and decompresses using BZ2
func ExtractFileFromString(data string) (out []byte, err error) {
func ExtractFileFromString(data string) ([]byte, error) {
// Decode base64
decoded := Base64Decode(data)
if len(decoded) == 0 {
return nil, fmt.Errorf("ExtractFileFromString: Failed to decode")
}

var decompressBuf bytes.Buffer
// Wrap the underlying reader with BZ2 decompressor
decompressor, err := archiver.Bz2{}.OpenReader(bytes.NewReader(decoded))
if err != nil {
return nil, fmt.Errorf("ExtractFileFromString: %v", err)
}
defer decompressor.Close()

// Decompress the data
_, err = decompressBuf.ReadFrom(decompressor)
if err != nil {
return nil, fmt.Errorf("ExtractFileFromString: Read from decompressor failed: %v", err)
}

// Get the decompressed data
out = decompressBuf.Bytes()
return
return arc.Decompress(decoded, arc.CompressionMap["bz2"])
}

0 comments on commit 08331e4

Please sign in to comment.