1 // Copyright 2019 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
17 // A Tree is a tree description, to be signed by a go.sum database server.
23 // FormatTree formats a tree description for inclusion in a note.
25 // The encoded form is three lines, each ending in a newline (U+000A):
27 // go.sum database tree
31 // where N is in decimal and Hash is in base64.
33 // A future backwards-compatible encoding may add additional lines,
34 // which the parser can ignore.
35 // A future backwards-incompatible encoding would use a different
36 // first line (for example, "go.sum database tree v2").
37 func FormatTree(tree Tree) []byte {
38 return []byte(fmt.Sprintf("go.sum database tree\n%d\n%s\n", tree.N, tree.Hash))
41 var errMalformedTree = errors.New("malformed tree note")
42 var treePrefix = []byte("go.sum database tree\n")
44 // ParseTree parses a formatted tree root description.
45 func ParseTree(text []byte) (tree Tree, err error) {
46 // The message looks like:
48 // go.sum database tree
50 // nND/nri/U0xuHUrYSy0HtMeal2vzD9V4k/BO79C+QeI=
52 // For forwards compatibility, extra text lines after the encoding are ignored.
53 if !bytes.HasPrefix(text, treePrefix) || bytes.Count(text, []byte("\n")) < 3 || len(text) > 1e6 {
54 return Tree{}, errMalformedTree
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
63 h, err := base64.StdEncoding.DecodeString(lines[2])
64 if err != nil || len(h) != HashSize {
65 return Tree{}, errMalformedTree
70 return Tree{n, hash}, nil
73 var errMalformedRecord = errors.New("malformed record data")
75 // FormatRecord formats a record for serving to a client
76 // in a lookup response or data tile.
78 // The encoded form is the record ID as a single number,
79 // then the text of the record, and then a terminating blank line.
80 // Record text must be valid UTF-8 and must not contain any ASCII control
81 // characters (those below U+0020) other than newline (U+000A).
82 // It must end in a terminating newline and not contain any blank lines.
83 func FormatRecord(id int64, text []byte) (msg []byte, err error) {
84 if !isValidRecordText(text) {
85 return nil, errMalformedRecord
87 msg = []byte(fmt.Sprintf("%d\n", id))
88 msg = append(msg, text...)
89 msg = append(msg, '\n')
93 // isValidRecordText reports whether text is syntactically valid record text.
94 func isValidRecordText(text []byte) bool {
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' {
110 // ParseRecord parses a record description at the start of text,
111 // stopping immediately after the terminating blank line.
112 // It returns the record id, the record text, and the remainder of text.
113 func ParseRecord(msg []byte) (id int64, text, rest []byte, err error) {
114 // Leading record id.
115 i := bytes.IndexByte(msg, '\n')
117 return 0, nil, nil, errMalformedRecord
119 id, err = strconv.ParseInt(string(msg[:i]), 10, 64)
121 return 0, nil, nil, errMalformedRecord
126 i = bytes.Index(msg, []byte("\n\n"))
128 return 0, nil, nil, errMalformedRecord
130 text, rest = msg[:i+1], msg[i+2:]
131 if !isValidRecordText(text) {
132 return 0, nil, nil, errMalformedRecord
134 return id, text, rest, nil