1
2
3
4
5 package tlog
6
7 import (
8 "bytes"
9 "encoding/base64"
10 "errors"
11 "fmt"
12 "strconv"
13 "strings"
14 "unicode/utf8"
15 )
16
17
18 type Tree struct {
19 N int64
20 Hash Hash
21 }
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37 func FormatTree(tree Tree) []byte {
38 return []byte(fmt.Sprintf("go.sum database tree\n%d\n%s\n", tree.N, tree.Hash))
39 }
40
41 var errMalformedTree = errors.New("malformed tree note")
42 var treePrefix = []byte("go.sum database tree\n")
43
44
45 func ParseTree(text []byte) (tree Tree, err error) {
46
47
48
49
50
51
52
53 if !bytes.HasPrefix(text, treePrefix) || bytes.Count(text, []byte("\n")) < 3 || len(text) > 1e6 {
54 return Tree{}, errMalformedTree
55 }
56
57 lines := strings.SplitN(string(text), "\n", 4)
58 n, err := strconv.ParseInt(lines[1], 10, 64)
59 if err != nil || n < 0 || lines[1] != strconv.FormatInt(n, 10) {
60 return Tree{}, errMalformedTree
61 }
62
63 h, err := base64.StdEncoding.DecodeString(lines[2])
64 if err != nil || len(h) != HashSize {
65 return Tree{}, errMalformedTree
66 }
67
68 var hash Hash
69 copy(hash[:], h)
70 return Tree{n, hash}, nil
71 }
72
73 var errMalformedRecord = errors.New("malformed record data")
74
75
76
77
78
79
80
81
82
83 func FormatRecord(id int64, text []byte) (msg []byte, err error) {
84 if !isValidRecordText(text) {
85 return nil, errMalformedRecord
86 }
87 msg = []byte(fmt.Sprintf("%d\n", id))
88 msg = append(msg, text...)
89 msg = append(msg, '\n')
90 return msg, nil
91 }
92
93
94 func isValidRecordText(text []byte) bool {
95 var last rune
96 for i := 0; i < len(text); {
97 r, size := utf8.DecodeRune(text[i:])
98 if r < 0x20 && r != '\n' || r == utf8.RuneError && size == 1 || last == '\n' && r == '\n' {
99 return false
100 }
101 i += size
102 last = r
103 }
104 if last != '\n' {
105 return false
106 }
107 return true
108 }
109
110
111
112
113 func ParseRecord(msg []byte) (id int64, text, rest []byte, err error) {
114
115 i := bytes.IndexByte(msg, '\n')
116 if i < 0 {
117 return 0, nil, nil, errMalformedRecord
118 }
119 id, err = strconv.ParseInt(string(msg[:i]), 10, 64)
120 if err != nil {
121 return 0, nil, nil, errMalformedRecord
122 }
123 msg = msg[i+1:]
124
125
126 i = bytes.Index(msg, []byte("\n\n"))
127 if i < 0 {
128 return 0, nil, nil, errMalformedRecord
129 }
130 text, rest = msg[:i+1], msg[i+2:]
131 if !isValidRecordText(text) {
132 return 0, nil, nil, errMalformedRecord
133 }
134 return id, text, rest, nil
135 }
136
View as plain text