From b0c16462c4bab5fb8a98cc872d40cb2875413e35 Mon Sep 17 00:00:00 2001 From: sneak Date: Tue, 6 Dec 2022 18:29:19 +0400 Subject: [PATCH] latest. linted and building, not working yet --- mfer/deserialize.go | 12 ++++++++++++ mfer/serialize.go | 20 ++++++++++++++++---- 2 files changed, 28 insertions(+), 4 deletions(-) create mode 100644 mfer/deserialize.go diff --git a/mfer/deserialize.go b/mfer/deserialize.go new file mode 100644 index 0000000..ecafc7e --- /dev/null +++ b/mfer/deserialize.go @@ -0,0 +1,12 @@ +package mfer + +import "google.golang.org/protobuf/proto" + +func NewFromProto(input []byte) (*manifest, error) { + m := New() + err := proto.Unmarshal(input, m.pbOuter) + if err != nil { + return nil, err + } + return m, nil +} diff --git a/mfer/serialize.go b/mfer/serialize.go index ba6dbe9..a3964c5 100644 --- a/mfer/serialize.go +++ b/mfer/serialize.go @@ -2,6 +2,7 @@ package mfer import ( "bytes" + "compress/gzip" "crypto/sha256" "errors" "time" @@ -37,7 +38,7 @@ func (m *manifest) generate() error { return e } } - dat, err := proto.Marshal(m.pbOuter) + dat, err := proto.MarshalOptions{Deterministic: true}.Marshal(m.pbOuter) if err != nil { return err } @@ -54,7 +55,7 @@ func (m *manifest) generateOuter() error { if m.pbInner == nil { return errors.New("internal error") } - innerData, err := proto.Marshal(m.pbInner) + innerData, err := proto.MarshalOptions{Deterministic: true}.Marshal(m.pbInner) if err != nil { return err } @@ -62,9 +63,20 @@ func (m *manifest) generateOuter() error { h := sha256.New() h.Write(innerData) + idc := new(bytes.Buffer) + gzw, err := gzip.NewWriterLevel(idc, gzip.BestCompression) + if err != nil { + return err + } + _, err = gzw.Write(innerData) + if err != nil { + return err + } + + gzw.Close() + o := &MFFileOuter{ - // FIXME add more - InnerMessage: innerData, + InnerMessage: idc.Bytes(), Size: int64(len(innerData)), Sha256: h.Sum(nil), Version: MFFileOuter_VERSION_ONE,