aboutsummaryrefslogtreecommitdiff
path: root/file/file.go
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2017-07-23 01:55:55 +0200
committerDimitri Sokolyuk <demon@dim13.org>2017-07-23 01:55:55 +0200
commit39d333ae8300eb3bc19384626d1b3f08b3eb7333 (patch)
tree809a094f2a6892f03ebfa15af380344ceab04cb4 /file/file.go
parent088135569acd6617c929b64e644e377e316f7df1 (diff)
...
Diffstat (limited to 'file/file.go')
-rw-r--r--file/file.go42
1 files changed, 27 insertions, 15 deletions
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
}