Copyright 2012 The Go Authors. All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
Package quotedprintable implements quoted-printable encoding as specified by RFC 2045.
package quotedprintable

import (
	
	
	
	
)
Reader is a quoted-printable decoder.
type Reader struct {
	br   *bufio.Reader
	rerr error  // last read error
	line []byte // to be consumed before more of br
}
NewReader returns a quoted-printable reader, decoding from r.
func ( io.Reader) *Reader {
	return &Reader{
		br: bufio.NewReader(),
	}
}

func ( byte) (byte, error) {
	switch {
	case  >= '0' &&  <= '9':
		return  - '0', nil
	case  >= 'A' &&  <= 'F':
Accept badly encoded bytes.
	case  >= 'a' &&  <= 'f':
		return  - 'a' + 10, nil
	}
	return 0, fmt.Errorf("quotedprintable: invalid hex byte 0x%02x", )
}

func ( []byte) ( byte,  error) {
	if len() < 2 {
		return 0, io.ErrUnexpectedEOF
	}
	var ,  byte
	if ,  = fromHex([0]);  != nil {
		return 0, 
	}
	if ,  = fromHex([1]);  != nil {
		return 0, 
	}
	return <<4 | , nil
}

func ( rune) bool {
	switch  {
	case '\n', '\r', ' ', '\t':
		return true
	}
	return false
}

var (
	crlf       = []byte("\r\n")
	lf         = []byte("\n")
	softSuffix = []byte("=")
)
Read reads and decodes quoted-printable data from the underlying reader.
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. 3. it accepts soft line-break (=) at end of message (issue 15486); i.e. the final byte read from the underlying reader is allowed to be '=', and it will be silently ignored. 4. it takes = as literal = if not followed by two hex digits but not at end of line (issue 13219).
	for len() > 0 {
		if len(.line) == 0 {
			if .rerr != nil {
				return , .rerr
			}
			.line, .rerr = .br.ReadSlice('\n')
Does the line end in CRLF instead of just LF?
			 := bytes.HasSuffix(.line, lf)
			 := bytes.HasSuffix(.line, crlf)
			 := .line
			.line = bytes.TrimRightFunc(, isQPDiscardWhitespace)
			if bytes.HasSuffix(.line, softSuffix) {
				 := [len(.line):]
				.line = .line[:len(.line)-1]
				if !bytes.HasPrefix(, lf) && !bytes.HasPrefix(, crlf) &&
					!(len() == 0 && len(.line) > 0 && .rerr == io.EOF) {
					.rerr = fmt.Errorf("quotedprintable: invalid bytes after =: %q", )
				}
			} else if  {
				if  {
					.line = append(.line, '\r', '\n')
				} else {
					.line = append(.line, '\n')
				}
			}
			continue
		}
		 := .line[0]

		switch {
		case  == '=':
			,  = readHexByte(.line[1:])
			if  != nil {
Take the = as a literal =.
					 = '='
					break
				}
				return , 
			}
			.line = .line[2:] // 2 of the 3; other 1 is done below
		case  == '\t' ||  == '\r' ||  == '\n':
			break
As an extension to RFC 2045, we accept values >= 0x80 without complaint. Issue 22597.
			break
		case  < ' ' ||  > '~':
			return , fmt.Errorf("quotedprintable: invalid unescaped byte 0x%02x in body", )
		}
		[0] = 
		 = [1:]
		.line = .line[1:]
		++
	}
	return , nil