// 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 quotedprintableimport ()// Reader is a quoted-printable decoder.typeReaderstruct { 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 fromHex( byte) (byte, error) {switch {case >= '0' && <= '9':return - '0', nilcase >= 'A' && <= 'F':return - 'A' + 10, nil// Accept badly encoded bytes.case >= 'a' && <= 'f':return - 'a' + 10, nil }return0, fmt.Errorf("quotedprintable: invalid hex byte 0x%02x", )}func readHexByte( []byte) ( byte, error) {iflen() < 2 {return0, io.ErrUnexpectedEOF }var , byteif , = fromHex([0]); != nil {return0, }if , = fromHex([1]); != nil {return0, }return <<4 | , nil}func isQPDiscardWhitespace( rune) bool {switch {case'\n', '\r', ' ', '\t':returntrue }returnfalse}var ( crlf = []byte("\r\n") lf = []byte("\n") softSuffix = []byte("="))// Read reads and decodes quoted-printable data from the underlying reader.func ( *Reader) ( []byte) ( int, 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. // 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).forlen() > 0 {iflen(.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)ifbytes.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", ) } } elseif {if { .line = append(.line, '\r', '\n') } else { .line = append(.line, '\n') } }continue } := .line[0]switch {case == '=': , = readHexByte(.line[1:])if != nil {iflen(.line) >= 2 && .line[1] != '\r' && .line[1] != '\n' {// Take the = as a literal =. = '='break }return , } .line = .line[2:] // 2 of the 3; other 1 is done belowcase == '\t' || == '\r' || == '\n':breakcase >= 0x80:// As an extension to RFC 2045, we accept // values >= 0x80 without complaint. Issue 22597.breakcase < ' ' || > '~':return , fmt.Errorf("quotedprintable: invalid unescaped byte 0x%02x in body", ) } [0] = = [1:] .line = .line[1:] ++ }return , nil}
The pages are generated with Goldsv0.7.0-preview. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.