aboutsummaryrefslogtreecommitdiff
path: root/b64file/file.go
blob: 5cefea7f2f48da8601a9ecd1cefac2b9206e0022 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Package b64file implements signify file format
package b64file

import (
	"bufio"
	"encoding"
	"encoding/base64"
	"errors"
	"fmt"
	"io"
	"io/ioutil"
	"strings"
)

const untrusted = "untrusted comment: "

// Original Error: "invalid comment in %s; must start with 'untrusted comment: '"
var (
	ErrUntrusted     = errors.New("comment must start with 'untrusted comment: '")
	ErrMissingHeader = errors.New("missing required header fields")
)

type Header struct {
	Comment string
	Bytes   []byte
}

type Reader struct {
	Header
	r io.Reader
}

type Writer struct {
	Header
	w           io.Writer
	wroteHeader bool
}

func NewReader(r io.Reader) (*Reader, error) {
	buf := bufio.NewReader(r)

	comment, err := buf.ReadString('\n')
	if err != nil {
		return nil, err
	}
	if !strings.HasPrefix(comment, untrusted) {
		return nil, ErrUntrusted
	}
	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
	}

	return &Reader{
		Header: Header{
			Comment: comment,
			Bytes:   b,
		},
		r: buf,
	}, nil
}

func (r *Reader) Read(p []byte) (n int, err error) {
	return r.r.Read(p)
}

func Decode(r io.Reader, u encoding.BinaryUnmarshaler) (string, []byte, error) {
	buf := bufio.NewReader(r)

	comment, err := buf.ReadString('\n')
	if err != nil {
		return "", nil, err
	}
	if !strings.HasPrefix(comment, untrusted) {
		return "", nil, ErrUntrusted
	}
	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
	}

	message, err := ioutil.ReadAll(buf)
	if err != nil {
		return "", nil, err
	}

	return comment, message, nil
}

func NewWriter(w io.Writer) (*Writer, error) {
	return &Writer{w: w}, nil
}

func (w *Writer) Write(p []byte) (n int, err error) {
	if !w.wroteHeader {
		if w.Comment == "" || w.Bytes == nil {
			return 0, ErrMissingHeader
		}
		fmt.Fprintf(w.w, "%s%s\n", untrusted, w.Comment)
		fmt.Fprintf(w.w, "%s\n", base64.StdEncoding.EncodeToString(w.Bytes))
		w.wroteHeader = true
	}
	return w.w.Write(p)
}

func Encode(w io.Writer, u encoding.BinaryMarshaler, comment string, msg []byte) error {
	raw, err := u.MarshalBinary()
	if err != nil {
		return err
	}
	fmt.Fprintf(w, "%s%s\n", untrusted, comment)
	fmt.Fprintf(w, "%s\n", base64.StdEncoding.EncodeToString(raw))
	w.Write(msg)
	return nil
}