This repository has been archived by the owner on Jun 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement gzip stream calculation
- Loading branch information
Showing
4 changed files
with
274 additions
and
36 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
// THIS FILE IS AN EXACT DUPLICATE OF STUFF IN ALPINE-GO! | ||
// Unfortunately, all of that is package-private rather than public, | ||
// so we are duplicating it here. As soon as we can upstream this entire impl, | ||
// this duplicate file goes away! | ||
|
||
//nolint:all | ||
package apk | ||
|
||
import ( | ||
"archive/tar" | ||
"context" | ||
"crypto/sha1" | ||
"encoding/hex" | ||
"fmt" | ||
"hash" | ||
"io" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/klauspost/compress/gzip" | ||
"gitlab.alpinelinux.org/alpine/go/repository" | ||
|
||
"go.opentelemetry.io/otel" | ||
) | ||
|
||
type APKResolved struct { | ||
Package *repository.RepositoryPackage | ||
|
||
SignatureSize int | ||
SignatureHash []byte | ||
|
||
ControlSize int | ||
ControlHash []byte | ||
|
||
DataSize int | ||
DataHash []byte | ||
} | ||
|
||
type countingWriter struct { | ||
bytesWritten int | ||
} | ||
|
||
func (r *countingWriter) Write(p []byte) (n int, err error) { | ||
r.bytesWritten += len(p) | ||
return n, nil | ||
} | ||
|
||
func ResolveApk(ctx context.Context, source io.Reader) (*APKResolved, error) { | ||
ctx, span := otel.Tracer("go-apk").Start(ctx, "ResolveApk") | ||
defer span.End() | ||
|
||
gzipStreamSizes := make([]int, 3) | ||
hashes := make([][]byte, 3) | ||
maxStreams := 2 | ||
streamId := 0 | ||
controlIdx := 0 | ||
signed := false | ||
|
||
var gzi *gzip.Reader | ||
cr := &countingWriter{} | ||
tr := io.TeeReader(source, cr) | ||
norar := newExpandApkReader(tr) | ||
|
||
for { | ||
var h hash.Hash = sha1.New() //nolint:gosec | ||
|
||
hr := io.TeeReader(norar, h) | ||
|
||
var err error | ||
|
||
if gzi == nil { | ||
gzi, err = gzip.NewReader(hr) | ||
} else { | ||
err = gzi.Reset(hr) | ||
} | ||
|
||
if err == io.EOF { | ||
break | ||
} else if err != nil { | ||
return nil, fmt.Errorf("creating gzip reader: %w", err) | ||
} | ||
gzi.Multistream(false) | ||
|
||
if streamId == 0 { | ||
tr := tar.NewReader(gzi) | ||
hdr, err := tr.Next() | ||
if err != nil { | ||
return nil, fmt.Errorf("ResolveApk error 1: %v", err) | ||
} | ||
if strings.HasPrefix(hdr.Name, ".SIGN.") { | ||
maxStreams = 3 | ||
controlIdx = 1 | ||
signed = true | ||
} | ||
} else if controlIdx == streamId { | ||
mapping, err := controlValue(gzi, "datahash", "size") | ||
if err != nil { | ||
return nil, fmt.Errorf("reading datahash and size from control: %w", err) | ||
} | ||
|
||
if sizes, ok := mapping["size"]; !ok { | ||
return nil, fmt.Errorf("reading size from control: %w", err) | ||
} else if len(sizes) != 1 { | ||
return nil, fmt.Errorf("saw %d size values", len(sizes)) | ||
} else if size, err := strconv.Atoi(sizes[0]); err != nil { | ||
return nil, fmt.Errorf("parsing size from control: %w", err) | ||
} else { | ||
gzipStreamSizes[maxStreams-1] = size | ||
} | ||
|
||
if datahashes, ok := mapping["datahash"]; !ok { | ||
return nil, fmt.Errorf("reading datahash from control: %w", err) | ||
} else if len(datahashes) != 1 { | ||
return nil, fmt.Errorf("saw %d datahash values", len(datahashes)) | ||
} else if hash, err := hex.DecodeString(datahashes[0]); err != nil { | ||
return nil, fmt.Errorf("reading datahash from control: %w", err) | ||
} else { | ||
hashes[maxStreams-1] = hash | ||
} | ||
} | ||
|
||
if streamId <= controlIdx { | ||
if _, err := io.Copy(io.Discard, gzi); err != nil { | ||
return nil, fmt.Errorf("ResolveApk error 2: %v", err) | ||
} | ||
|
||
hashes[streamId] = h.Sum(nil) | ||
gzipStreamSizes[streamId] = cr.bytesWritten | ||
} else { | ||
gzi.Close() | ||
break | ||
} | ||
|
||
streamId++ | ||
} | ||
|
||
resolved := &APKResolved{ | ||
SignatureSize: 0, | ||
ControlSize: gzipStreamSizes[controlIdx], | ||
ControlHash: hashes[controlIdx], | ||
DataSize: gzipStreamSizes[controlIdx+1], | ||
DataHash: hashes[controlIdx+1], | ||
} | ||
|
||
if signed { | ||
resolved.SignatureSize = gzipStreamSizes[0] | ||
resolved.SignatureHash = hashes[0] | ||
} | ||
|
||
return resolved, nil | ||
} |
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