Skip to content

Commit

Permalink
pkg/boot: Use a library for xz files
Browse files Browse the repository at this point in the history
Fixes u-root#2750

At the moment, we shell out for decompressing xz files. This change
will change that to using a library instead.

Signed-off-by: Navid Paya <navidpaya@google.com>
  • Loading branch information
navidpaya committed Jun 24, 2024
1 parent cea6f52 commit 3065e58
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
5 changes: 2 additions & 3 deletions pkg/boot/bzimage/bzimage.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ var (
// GZIP
{[]byte{0x1F, 0x8B}, gunzip},
// XZ
// It would be nice to use a Go package instead of shelling out to 'unxz'.
// https://github.com/ulikunitz/xz fails to decompress the payloads and returns an error: "unsupported filter count"
{[]byte{0xFD, 0x37, 0x7A, 0x58, 0x5A, 0x00}, stripSize(execer("unxz"))},
{[]byte{0xFD, 0x37, 0x7A, 0x58, 0x5A, 0x00}, stripSize(unxz)},
// LZMA
{[]byte{0x5D, 0x00, 0x00}, stripSize(unlzma)},
// LZO
Expand All @@ -83,6 +81,7 @@ var (
func findDecompressor(b []byte) (decompressor, error) {
for _, m := range magics {
if bytes.Index(b, m.signature) == 0 {
fmt.Println("found decompressor for signature: ", m.signature)
return m.decompressor, nil
}
}
Expand Down
15 changes: 15 additions & 0 deletions pkg/boot/bzimage/bzimage_decompress.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"io"
"os/exec"

"github.com/therootcompany/xz/v/v1/xz"
"github.com/klauspost/compress/zstd"
"github.com/pierrec/lz4/v4"
"github.com/ulikunitz/xz/lzma"
Expand Down Expand Up @@ -127,3 +128,17 @@ func unzstd(w io.Writer, r io.Reader) error {
}
return nil
}

// unxz reads compressed bytes from the io.Reader and writes the uncompressed bytes to the
// writer. unxz satisfies the decompressor interface.
func unxz(w io.Writer, r io.Reader) error {
unxzReader, err := xz.NewReader(r, 0)
if err != nil {
return fmt.Errorf("error creating unxz reader: %v", err)
}

if _, err := io.Copy(w, unxzReader); err != nil {
return fmt.Errorf("failed writing decompressed bytes to writer: %v", err)
}
return nil
}

0 comments on commit 3065e58

Please sign in to comment.