Commit 6b045d9a authored by Alexandre Cesaro's avatar Alexandre Cesaro Committed by Brad Fitzpatrick

mime/quotedprintable: Return a Reader instead of an io.Reader

It is not needed right now, but it will allow more flexibility in
the future.

Fixes #10472

Change-Id: I2eaea70abeca5ed10f89b0b2dfdabdac376a0a41
Reviewed-on: https://go-review.googlesource.com/8964Reviewed-by: 's avatarBrad Fitzpatrick <bradfitz@golang.org>
parent 2f0828ef
...@@ -13,19 +13,16 @@ import ( ...@@ -13,19 +13,16 @@ import (
"io" "io"
) )
// Deviations from RFC 2045: // Reader is a quoted-printable decoder.
// 1. in addition to "=\r\n", "=\n" is also treated as soft line break. type Reader struct {
// 2. it will pass through a '\r' or '\n' not preceded by '=', consistent
// with other broken QP encoders & decoders.
type reader struct {
br *bufio.Reader br *bufio.Reader
rerr error // last read error rerr error // last read error
line []byte // to be consumed before more of br line []byte // to be consumed before more of br
} }
// NewReader returns a quoted-printable reader, decoding from r. // NewReader returns a quoted-printable reader, decoding from r.
func NewReader(r io.Reader) io.Reader { func NewReader(r io.Reader) *Reader {
return &reader{ return &Reader{
br: bufio.NewReader(r), br: bufio.NewReader(r),
} }
} }
...@@ -43,7 +40,7 @@ func fromHex(b byte) (byte, error) { ...@@ -43,7 +40,7 @@ func fromHex(b byte) (byte, error) {
return 0, fmt.Errorf("quotedprintable: invalid hex byte 0x%02x", b) return 0, fmt.Errorf("quotedprintable: invalid hex byte 0x%02x", b)
} }
func (q *reader) readHexByte(v []byte) (b byte, err error) { func readHexByte(v []byte) (b byte, err error) {
if len(v) < 2 { if len(v) < 2 {
return 0, io.ErrUnexpectedEOF return 0, io.ErrUnexpectedEOF
} }
...@@ -71,43 +68,48 @@ var ( ...@@ -71,43 +68,48 @@ var (
softSuffix = []byte("=") softSuffix = []byte("=")
) )
func (q *reader) Read(p []byte) (n int, err error) { // Read reads and decodes quoted-printable data from the underlying reader.
func (r *Reader) Read(p []byte) (n int, err error) {
// Deviations from RFC 2045:
// 1. in addition to "=\r\n", "=\n" is also treated as soft line break.
// 2. it will pass through a '\r' or '\n' not preceded by '=', consistent
// with other broken QP encoders & decoders.
for len(p) > 0 { for len(p) > 0 {
if len(q.line) == 0 { if len(r.line) == 0 {
if q.rerr != nil { if r.rerr != nil {
return n, q.rerr return n, r.rerr
} }
q.line, q.rerr = q.br.ReadSlice('\n') r.line, r.rerr = r.br.ReadSlice('\n')
// Does the line end in CRLF instead of just LF? // Does the line end in CRLF instead of just LF?
hasLF := bytes.HasSuffix(q.line, lf) hasLF := bytes.HasSuffix(r.line, lf)
hasCR := bytes.HasSuffix(q.line, crlf) hasCR := bytes.HasSuffix(r.line, crlf)
wholeLine := q.line wholeLine := r.line
q.line = bytes.TrimRightFunc(wholeLine, isQPDiscardWhitespace) r.line = bytes.TrimRightFunc(wholeLine, isQPDiscardWhitespace)
if bytes.HasSuffix(q.line, softSuffix) { if bytes.HasSuffix(r.line, softSuffix) {
rightStripped := wholeLine[len(q.line):] rightStripped := wholeLine[len(r.line):]
q.line = q.line[:len(q.line)-1] r.line = r.line[:len(r.line)-1]
if !bytes.HasPrefix(rightStripped, lf) && !bytes.HasPrefix(rightStripped, crlf) { if !bytes.HasPrefix(rightStripped, lf) && !bytes.HasPrefix(rightStripped, crlf) {
q.rerr = fmt.Errorf("quotedprintable: invalid bytes after =: %q", rightStripped) r.rerr = fmt.Errorf("quotedprintable: invalid bytes after =: %q", rightStripped)
} }
} else if hasLF { } else if hasLF {
if hasCR { if hasCR {
q.line = append(q.line, '\r', '\n') r.line = append(r.line, '\r', '\n')
} else { } else {
q.line = append(q.line, '\n') r.line = append(r.line, '\n')
} }
} }
continue continue
} }
b := q.line[0] b := r.line[0]
switch { switch {
case b == '=': case b == '=':
b, err = q.readHexByte(q.line[1:]) b, err = readHexByte(r.line[1:])
if err != nil { if err != nil {
return n, err return n, err
} }
q.line = q.line[2:] // 2 of the 3; other 1 is done below r.line = r.line[2:] // 2 of the 3; other 1 is done below
case b == '\t' || b == '\r' || b == '\n': case b == '\t' || b == '\r' || b == '\n':
break break
case b < ' ' || b > '~': case b < ' ' || b > '~':
...@@ -115,7 +117,7 @@ func (q *reader) Read(p []byte) (n int, err error) { ...@@ -115,7 +117,7 @@ func (q *reader) Read(p []byte) (n int, err error) {
} }
p[0] = b p[0] = b
p = p[1:] p = p[1:]
q.line = q.line[1:] r.line = r.line[1:]
n++ n++
} }
return n, nil return n, nil
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment