diff options
Diffstat (limited to 'llgo/third_party/gofrontend/libgo/go/encoding/pem/pem.go')
| -rw-r--r-- | llgo/third_party/gofrontend/libgo/go/encoding/pem/pem.go | 35 |
1 files changed, 26 insertions, 9 deletions
diff --git a/llgo/third_party/gofrontend/libgo/go/encoding/pem/pem.go b/llgo/third_party/gofrontend/libgo/go/encoding/pem/pem.go index 8ff7ee8c33a..506196b1db9 100644 --- a/llgo/third_party/gofrontend/libgo/go/encoding/pem/pem.go +++ b/llgo/third_party/gofrontend/libgo/go/encoding/pem/pem.go @@ -10,8 +10,10 @@ package pem import ( "bytes" "encoding/base64" + "errors" "io" "sort" + "strings" ) // A Block represents a PEM encoded structure. @@ -110,27 +112,37 @@ func Decode(data []byte) (p *Block, rest []byte) { } // TODO(agl): need to cope with values that spread across lines. - key, val := line[0:i], line[i+1:] + key, val := line[:i], line[i+1:] key = bytes.TrimSpace(key) val = bytes.TrimSpace(val) p.Headers[string(key)] = string(val) rest = next } - i := bytes.Index(rest, pemEnd) - if i < 0 { + var endIndex int + // If there were no headers, the END line might occur + // immediately, without a leading newline. + if len(p.Headers) == 0 && bytes.HasPrefix(rest, pemEnd[1:]) { + endIndex = 0 + } else { + endIndex = bytes.Index(rest, pemEnd) + } + + if endIndex < 0 { return decodeError(data, rest) } - base64Data := removeWhitespace(rest[0:i]) + base64Data := removeWhitespace(rest[:endIndex]) p.Bytes = make([]byte, base64.StdEncoding.DecodedLen(len(base64Data))) n, err := base64.StdEncoding.Decode(p.Bytes, base64Data) if err != nil { return decodeError(data, rest) } - p.Bytes = p.Bytes[0:n] + p.Bytes = p.Bytes[:n] - _, rest = getLine(rest[i+len(pemEnd):]) + // the -1 is because we might have only matched pemEnd without the + // leading newline if the PEM block was empty. + _, rest = getLine(rest[endIndex+len(pemEnd)-1:]) return } @@ -171,6 +183,8 @@ type lineBreaker struct { out io.Writer } +var nl = []byte{'\n'} + func (l *lineBreaker) Write(b []byte) (n int, err error) { if l.used+len(b) < pemLineLength { copy(l.line[l.used:], b) @@ -190,7 +204,7 @@ func (l *lineBreaker) Write(b []byte) (n int, err error) { return } - n, err = l.out.Write([]byte{'\n'}) + n, err = l.out.Write(nl) if err != nil { return } @@ -204,7 +218,7 @@ func (l *lineBreaker) Close() (err error) { if err != nil { return } - _, err = l.out.Write([]byte{'\n'}) + _, err = l.out.Write(nl) } return @@ -244,11 +258,14 @@ func Encode(out io.Writer, b *Block) error { // For consistency of output, write other headers sorted by key. sort.Strings(h) for _, k := range h { + if strings.Contains(k, ":") { + return errors.New("pem: cannot encode a header key that contains a colon") + } if err := writeHeader(out, k, b.Headers[k]); err != nil { return err } } - if _, err := out.Write([]byte{'\n'}); err != nil { + if _, err := out.Write(nl); err != nil { return err } } |

