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. 4 5package tlog 6 7import ( 8 "bytes" 9 "encoding/base64" 10 "errors" 11 "fmt" 12 "strconv" 13 "strings" 14 "unicode/utf8" 15) 16 17// A Tree is a tree description, to be signed by a go.sum database server. 18type Tree struct { 19 N int64 20 Hash Hash 21} 22 23// FormatTree formats a tree description for inclusion in a note. 24// 25// The encoded form is three lines, each ending in a newline (U+000A): 26// 27// go.sum database tree 28// N 29// Hash 30// 31// where N is in decimal and Hash is in base64. 32// 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"). 37func FormatTree(tree Tree) []byte { 38 return []byte(fmt.Sprintf("go.sum database tree\n%d\n%s\n", tree.N, tree.Hash)) 39} 40 41var errMalformedTree = errors.New("malformed tree note") 42var treePrefix = []byte("go.sum database tree\n") 43 44// ParseTree parses a formatted tree root description. 45func ParseTree(text []byte) (tree Tree, err error) { 46 // The message looks like: 47 // 48 // go.sum database tree 49 // 2 50 // nND/nri/U0xuHUrYSy0HtMeal2vzD9V4k/BO79C+QeI= 51 // 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 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 73var errMalformedRecord = errors.New("malformed record data") 74 75// FormatRecord formats a record for serving to a client 76// in a lookup response or data tile. 77// 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. 83func 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// isValidRecordText reports whether text is syntactically valid record text. 94func 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// 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. 113func ParseRecord(msg []byte) (id int64, text, rest []byte, err error) { 114 // Leading record id. 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 // Record text. 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