From 39d333ae8300eb3bc19384626d1b3f08b3eb7333 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Sun, 23 Jul 2017 01:55:55 +0200 Subject: ... --- file/file.go | 42 +++++++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 15 deletions(-) (limited to 'file/file.go') diff --git a/file/file.go b/file/file.go index fa77cd0..d3dc3e7 100644 --- a/file/file.go +++ b/file/file.go @@ -72,21 +72,35 @@ func DecodeFile(fname string, u encoding.BinaryUnmarshaler) (string, []byte, err } func Decode(r io.Reader, u encoding.BinaryUnmarshaler) (string, []byte, error) { - block, err := decodeBlock(r) + buf := bufio.NewReader(r) + + comment, err := buf.ReadString('\n') if err != nil { return "", nil, err } - if err := u.UnmarshalBinary(block.Bytes); err != nil { + if !strings.HasPrefix(comment, untrusted) { + return "", nil, ErrComment + } + comment = strings.TrimSpace(comment[len(untrusted):]) + + raw, err := buf.ReadString('\n') + if err != nil { + return "", nil, err + } + b, err := base64.StdEncoding.DecodeString(raw) + if err != nil { + return "", nil, err + } + if err := u.UnmarshalBinary(b); err != nil { return "", nil, err } - return block.Comment, block.Message, nil -} -func encodeBlock(w io.Writer, b *block) error { - fmt.Fprintln(w, untrusted, b.Comment) - fmt.Fprintln(w, base64.StdEncoding.EncodeToString(b.Bytes)) - w.Write(b.Message) - return nil + message, err := ioutil.ReadAll(buf) + if err != nil { + return "", nil, err + } + + return comment, message, nil } func EncodeFile(fname string, perm os.FileMode, u encoding.BinaryMarshaler, comment string, msg []byte) error { @@ -103,10 +117,8 @@ func Encode(w io.Writer, u encoding.BinaryMarshaler, comment string, msg []byte) if err != nil { return err } - b := &block{ - Comment: comment, - Bytes: raw, - Message: msg, - } - return encodeBlock(w, b) + fmt.Fprintln(w, untrusted, comment) + fmt.Fprintln(w, base64.StdEncoding.EncodeToString(raw)) + w.Write(msg) + return nil } -- cgit v1.2.3