You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

166 lines
3.0 KiB

  1. package quotedprintable
  2. import "io"
  3. const lineMaxLen = 76
  4. // A Writer is a quoted-printable writer that implements io.WriteCloser.
  5. type Writer struct {
  6. // Binary mode treats the writer's input as pure binary and processes end of
  7. // line bytes as binary data.
  8. Binary bool
  9. w io.Writer
  10. i int
  11. line [78]byte
  12. cr bool
  13. }
  14. // NewWriter returns a new Writer that writes to w.
  15. func NewWriter(w io.Writer) *Writer {
  16. return &Writer{w: w}
  17. }
  18. // Write encodes p using quoted-printable encoding and writes it to the
  19. // underlying io.Writer. It limits line length to 76 characters. The encoded
  20. // bytes are not necessarily flushed until the Writer is closed.
  21. func (w *Writer) Write(p []byte) (n int, err error) {
  22. for i, b := range p {
  23. switch {
  24. // Simple writes are done in batch.
  25. case b >= '!' && b <= '~' && b != '=':
  26. continue
  27. case isWhitespace(b) || !w.Binary && (b == '\n' || b == '\r'):
  28. continue
  29. }
  30. if i > n {
  31. if err := w.write(p[n:i]); err != nil {
  32. return n, err
  33. }
  34. n = i
  35. }
  36. if err := w.encode(b); err != nil {
  37. return n, err
  38. }
  39. n++
  40. }
  41. if n == len(p) {
  42. return n, nil
  43. }
  44. if err := w.write(p[n:]); err != nil {
  45. return n, err
  46. }
  47. return len(p), nil
  48. }
  49. // Close closes the Writer, flushing any unwritten data to the underlying
  50. // io.Writer, but does not close the underlying io.Writer.
  51. func (w *Writer) Close() error {
  52. if err := w.checkLastByte(); err != nil {
  53. return err
  54. }
  55. return w.flush()
  56. }
  57. // write limits text encoded in quoted-printable to 76 characters per line.
  58. func (w *Writer) write(p []byte) error {
  59. for _, b := range p {
  60. if b == '\n' || b == '\r' {
  61. // If the previous byte was \r, the CRLF has already been inserted.
  62. if w.cr && b == '\n' {
  63. w.cr = false
  64. continue
  65. }
  66. if b == '\r' {
  67. w.cr = true
  68. }
  69. if err := w.checkLastByte(); err != nil {
  70. return err
  71. }
  72. if err := w.insertCRLF(); err != nil {
  73. return err
  74. }
  75. continue
  76. }
  77. if w.i == lineMaxLen-1 {
  78. if err := w.insertSoftLineBreak(); err != nil {
  79. return err
  80. }
  81. }
  82. w.line[w.i] = b
  83. w.i++
  84. w.cr = false
  85. }
  86. return nil
  87. }
  88. func (w *Writer) encode(b byte) error {
  89. if lineMaxLen-1-w.i < 3 {
  90. if err := w.insertSoftLineBreak(); err != nil {
  91. return err
  92. }
  93. }
  94. w.line[w.i] = '='
  95. w.line[w.i+1] = upperhex[b>>4]
  96. w.line[w.i+2] = upperhex[b&0x0f]
  97. w.i += 3
  98. return nil
  99. }
  100. // checkLastByte encodes the last buffered byte if it is a space or a tab.
  101. func (w *Writer) checkLastByte() error {
  102. if w.i == 0 {
  103. return nil
  104. }
  105. b := w.line[w.i-1]
  106. if isWhitespace(b) {
  107. w.i--
  108. if err := w.encode(b); err != nil {
  109. return err
  110. }
  111. }
  112. return nil
  113. }
  114. func (w *Writer) insertSoftLineBreak() error {
  115. w.line[w.i] = '='
  116. w.i++
  117. return w.insertCRLF()
  118. }
  119. func (w *Writer) insertCRLF() error {
  120. w.line[w.i] = '\r'
  121. w.line[w.i+1] = '\n'
  122. w.i += 2
  123. return w.flush()
  124. }
  125. func (w *Writer) flush() error {
  126. if _, err := w.w.Write(w.line[:w.i]); err != nil {
  127. return err
  128. }
  129. w.i = 0
  130. return nil
  131. }
  132. func isWhitespace(b byte) bool {
  133. return b == ' ' || b == '\t'
  134. }