| 1 |
package object |
| 2 |
|
| 3 |
import ( |
| 4 |
"bufio" |
| 5 |
"bytes" |
| 6 |
"fmt" |
| 7 |
"io" |
| 8 |
|
| 9 |
"github.com/go-git/go-git/v5/plumbing" |
| 10 |
) |
| 11 |
|
| 12 |
// tagScanner holds the working state of the tag decoder driven by the |
| 13 |
// stateFn loop in (*Tag).Decode. Each tagState reads one or more lines |
| 14 |
// from r, updates the in-progress *Tag and the scanner's bookkeeping, |
| 15 |
// and returns the state that should run next (or nil to stop). |
| 16 |
type tagScanner struct { |
| 17 |
r *bufio.Reader |
| 18 |
t *Tag |
| 19 |
msgbuf bytes.Buffer |
| 20 |
|
| 21 |
// pending holds a line that was read but the current state decided to |
| 22 |
// hand back to the next state, paired with the io.EOF flag returned |
| 23 |
// when the line was originally read. |
| 24 |
pending []byte |
| 25 |
pendingErr error |
| 26 |
|
| 27 |
// First-occurrence tracking: once the corresponding canonical |
| 28 |
// header has been decoded at its expected position, subsequent |
| 29 |
// occurrences (or out-of-position lines) are silently dropped, |
| 30 |
// matching the strict layout enforced by upstream's |
| 31 |
// parse_tag_buffer (tag.c:130). |
| 32 |
// |
| 33 |
// gpgsig-sha256 is recognized and skipped without exposing a new field |
| 34 |
// in v5. |
| 35 |
sawObject, sawType, sawName, sawTagger bool |
| 36 |
} |
| 37 |
|
| 38 |
// tagState is one step of the decoder state machine. Each function reads |
| 39 |
// the lines it needs, mutates *Tag via s.t, and returns the next state |
| 40 |
// to run (or nil to terminate the loop). |
| 41 |
type tagState func(*tagScanner) (tagState, error) |
| 42 |
|
| 43 |
// readLine returns the next line from the buffer, transparently |
| 44 |
// consuming any line that was previously pushed back by a state that |
| 45 |
// decided not to handle it. |
| 46 |
func (s *tagScanner) readLine() ([]byte, error) { |
| 47 |
if s.pending != nil { |
| 48 |
line, err := s.pending, s.pendingErr |
| 49 |
s.pending, s.pendingErr = nil, nil |
| 50 |
return line, err |
| 51 |
} |
| 52 |
return s.r.ReadBytes('\n') |
| 53 |
} |
| 54 |
|
| 55 |
// pushBack stashes an unconsumed line so the next state's readLine call |
| 56 |
// sees it. Only one line can be pushed back at a time. |
| 57 |
func (s *tagScanner) pushBack(line []byte, err error) { |
| 58 |
s.pending = line |
| 59 |
s.pendingErr = err |
| 60 |
} |
| 61 |
|
| 62 |
// scanTagObject requires the first line to be `object HASH`, mirroring |
| 63 |
// upstream's strict parse_tag_buffer (tag.c:151-156). Anything else |
| 64 |
// returns ErrMalformedTag. |
| 65 |
func scanTagObject(s *tagScanner) (tagState, error) { |
| 66 |
line, err := s.readLine() |
| 67 |
if err != nil && err != io.EOF { |
| 68 |
return nil, err |
| 69 |
} |
| 70 |
if len(line) == 0 || isBlankLine(line) { |
| 71 |
return nil, fmt.Errorf("%w: missing object header", ErrMalformedTag) |
| 72 |
} |
| 73 |
|
| 74 |
key, data := splitHeader(line) |
| 75 |
if key != "object" { |
| 76 |
return nil, fmt.Errorf("%w: object header must be first", ErrMalformedTag) |
| 77 |
} |
| 78 |
h, herr := parseObjectIDHex(data, ErrMalformedTag, "object") |
| 79 |
if herr != nil { |
| 80 |
return nil, herr |
| 81 |
} |
| 82 |
s.t.Target = h |
| 83 |
s.sawObject = true |
| 84 |
if err == io.EOF { |
| 85 |
return nil, nil |
| 86 |
} |
| 87 |
return scanTagType, nil |
| 88 |
} |
| 89 |
|
| 90 |
// scanTagType requires a `type` line immediately after the object header, |
| 91 |
// mirroring upstream's parse_tag_buffer (tag.c:158-166). |
| 92 |
func scanTagType(s *tagScanner) (tagState, error) { |
| 93 |
line, err := s.readLine() |
| 94 |
if err != nil && err != io.EOF { |
| 95 |
return nil, err |
| 96 |
} |
| 97 |
if len(line) == 0 || isBlankLine(line) { |
| 98 |
return nil, fmt.Errorf("%w: missing type header", ErrMalformedTag) |
| 99 |
} |
| 100 |
|
| 101 |
key, data := splitHeader(line) |
| 102 |
if key != "type" { |
| 103 |
return nil, fmt.Errorf("%w: type header must follow object", ErrMalformedTag) |
| 104 |
} |
| 105 |
ot, perr := plumbing.ParseObjectType(string(data)) |
| 106 |
if perr != nil { |
| 107 |
return nil, perr |
| 108 |
} |
| 109 |
s.t.TargetType = ot |
| 110 |
s.sawType = true |
| 111 |
if err == io.EOF { |
| 112 |
return nil, nil |
| 113 |
} |
| 114 |
return scanTagName, nil |
| 115 |
} |
| 116 |
|
| 117 |
// scanTagName requires a `tag` line immediately after the type header, |
| 118 |
// mirroring upstream's parse_tag_buffer (tag.c:186-194). |
| 119 |
func scanTagName(s *tagScanner) (tagState, error) { |
| 120 |
line, err := s.readLine() |
| 121 |
if err != nil && err != io.EOF { |
| 122 |
return nil, err |
| 123 |
} |
| 124 |
if len(line) == 0 || isBlankLine(line) { |
| 125 |
return nil, fmt.Errorf("%w: missing tag header", ErrMalformedTag) |
| 126 |
} |
| 127 |
|
| 128 |
key, data := splitHeader(line) |
| 129 |
if key != "tag" { |
| 130 |
return nil, fmt.Errorf("%w: tag header must follow type", ErrMalformedTag) |
| 131 |
} |
| 132 |
s.t.Name = string(data) |
| 133 |
s.sawName = true |
| 134 |
if err == io.EOF { |
| 135 |
return nil, nil |
| 136 |
} |
| 137 |
return scanTagTagger, nil |
| 138 |
} |
| 139 |
|
| 140 |
// scanTagTagger accepts a `tagger` line at its canonical position. Any |
| 141 |
// other header is pushed back for scanTagHeaders. |
| 142 |
func scanTagTagger(s *tagScanner) (tagState, error) { |
| 143 |
line, err := s.readLine() |
| 144 |
if err != nil && err != io.EOF { |
| 145 |
return nil, err |
| 146 |
} |
| 147 |
if len(line) == 0 { |
| 148 |
return nil, nil |
| 149 |
} |
| 150 |
if isBlankLine(line) { |
| 151 |
return scanTagMessage, nil |
| 152 |
} |
| 153 |
|
| 154 |
key, data := splitHeader(line) |
| 155 |
if key == "tagger" { |
| 156 |
s.t.Tagger.Decode(data) |
| 157 |
s.sawTagger = true |
| 158 |
if err == io.EOF { |
| 159 |
return nil, nil |
| 160 |
} |
| 161 |
return scanTagHeaders, nil |
| 162 |
} |
| 163 |
s.pushBack(line, err) |
| 164 |
return scanTagHeaders, nil |
| 165 |
} |
| 166 |
|
| 167 |
// scanTagHeaders dispatches one header line. gpgsig-sha256 hands off to |
| 168 |
// scanTagSkipCont so the continuation block can be consumed; out-of-position |
| 169 |
// canonical fields and unknown headers are silently dropped. |
| 170 |
func scanTagHeaders(s *tagScanner) (tagState, error) { |
| 171 |
line, err := s.readLine() |
| 172 |
if err != nil && err != io.EOF { |
| 173 |
return nil, err |
| 174 |
} |
| 175 |
if len(line) == 0 { |
| 176 |
return nil, nil |
| 177 |
} |
| 178 |
if isBlankLine(line) { |
| 179 |
return scanTagMessage, nil |
| 180 |
} |
| 181 |
|
| 182 |
key, _ := splitHeader(line) |
| 183 |
next := scanTagHeaders |
| 184 |
switch key { |
| 185 |
case "object", "type", "tag", "tagger": |
| 186 |
// Out-of-canonical-position duplicates are dropped, mirroring the |
| 187 |
// strict ordering of upstream's parse_tag_buffer. |
| 188 |
case headerpgp256: |
| 189 |
next = scanTagSkipCont |
| 190 |
default: |
| 191 |
// Unknown header: silently dropped (the Tag struct does not |
| 192 |
// expose ExtraHeaders). |
| 193 |
} |
| 194 |
|
| 195 |
if err == io.EOF { |
| 196 |
return nil, nil |
| 197 |
} |
| 198 |
return next, nil |
| 199 |
} |
| 200 |
|
| 201 |
// scanTagSkipCont discards continuation lines for a header scanTagHeaders chose |
| 202 |
// to drop. The first non-continuation line is pushed back so scanTagHeaders can |
| 203 |
// dispatch it. |
| 204 |
func scanTagSkipCont(s *tagScanner) (tagState, error) { |
| 205 |
line, err := s.readLine() |
| 206 |
if err != nil && err != io.EOF { |
| 207 |
return nil, err |
| 208 |
} |
| 209 |
if len(line) > 0 && line[0] == ' ' { |
| 210 |
if err == io.EOF { |
| 211 |
return nil, nil |
| 212 |
} |
| 213 |
return scanTagSkipCont, nil |
| 214 |
} |
| 215 |
if len(line) > 0 { |
| 216 |
s.pushBack(line, err) |
| 217 |
} |
| 218 |
return scanTagHeaders, nil |
| 219 |
} |
| 220 |
|
| 221 |
// scanTagMessage drains the remaining bytes into the message buffer. |
| 222 |
// (*Tag).Decode then runs parseSignedBytes over those bytes to peel off |
| 223 |
// the optional inline trailing PGP signature. |
| 224 |
func scanTagMessage(s *tagScanner) (tagState, error) { |
| 225 |
for { |
| 226 |
line, err := s.readLine() |
| 227 |
if err != nil && err != io.EOF { |
| 228 |
return nil, err |
| 229 |
} |
| 230 |
if len(line) > 0 { |
| 231 |
s.msgbuf.Write(line) |
| 232 |
} |
| 233 |
if err == io.EOF { |
| 234 |
return nil, nil |
| 235 |
} |
| 236 |
} |
| 237 |
} |