From 6e80b3fb8d5bfd948a46a9bf48730e460471af56 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Sun, 23 Oct 2016 21:05:07 +0200 Subject: Cleanup --- file.go | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) (limited to 'file.go') diff --git a/file.go b/file.go index 68737ad..e8218d8 100644 --- a/file.go +++ b/file.go @@ -25,31 +25,42 @@ const ( sigFrom = "signature from %s" ) +func checkComment(comment string) error { + // compatibility with original version + if len(comment) > 1024 { + return errors.New("comment line to long") + } + if !strings.HasPrefix(comment, commentHdr) { + return errors.New("expected untrusted comment") + } + return nil +} + func Parse(r io.Reader) (File, error) { buf := bufio.NewReader(r) + comment, err := buf.ReadString('\n') if err != nil { return File{}, err } - if !strings.HasPrefix(comment, commentHdr) { - return File{}, errors.New("expected untrusted header") - } - // Backward compatibility with original signify - if len(comment) > 1024 { - return File{}, errors.New("comment line too long") + if err := checkComment(comment); err != nil { + return File{}, err } comment = comment[len(commentHdr):] + b64, err := buf.ReadBytes('\n') if err != nil { return File{}, err } + body, err := ioutil.ReadAll(buf) if err != nil { return File{}, err } + return File{ - Comment: strings.TrimRight(comment, "\r\n"), - B64: bytes.TrimRight(b64, "\r\n"), + Comment: strings.TrimSpace(comment), + B64: bytes.TrimSpace(b64), Body: body, }, nil } -- cgit v1.2.3