| 1 |
// Copyright 2011 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package v2 |
| 6 |
|
| 7 |
import ( |
| 8 |
"bytes" |
| 9 |
"hash" |
| 10 |
"io" |
| 11 |
) |
| 12 |
|
| 13 |
// NewCanonicalTextHash reformats text written to it into the canonical |
| 14 |
// form and then applies the hash h. See RFC 4880, section 5.2.1. |
| 15 |
func NewCanonicalTextHash(h hash.Hash) hash.Hash { |
| 16 |
return &canonicalTextHash{h, 0} |
| 17 |
} |
| 18 |
|
| 19 |
// NewCanonicalTextWriteCloser reformats text written to it into the canonical |
| 20 |
// form. See RFC 4880, section 5.2.1. |
| 21 |
func NewCanonicalTextWriteCloser(w io.WriteCloser) io.WriteCloser { |
| 22 |
return &canonicalTextWriteCloser{w, 0} |
| 23 |
} |
| 24 |
|
| 25 |
// NewCanonicalTextReader reformats text read from it into the canonical |
| 26 |
// form. See RFC 4880, section 5.2.1. |
| 27 |
func NewCanonicalTextReader(r io.Reader) io.Reader { |
| 28 |
return &canonicalTextReader{r, bytes.NewBuffer(nil), 0} |
| 29 |
} |
| 30 |
|
| 31 |
type canonicalTextHash struct { |
| 32 |
h hash.Hash |
| 33 |
s int |
| 34 |
} |
| 35 |
|
| 36 |
var newline = []byte{'\r', '\n'} |
| 37 |
|
| 38 |
func writeCanonical(cw io.Writer, buf []byte, s *int) (int, error) { |
| 39 |
start := 0 |
| 40 |
for i, c := range buf { |
| 41 |
switch *s { |
| 42 |
case 0: |
| 43 |
if c == '\r' { |
| 44 |
*s = 1 |
| 45 |
} else if c == '\n' { |
| 46 |
if _, err := cw.Write(buf[start:i]); err != nil { |
| 47 |
return 0, err |
| 48 |
} |
| 49 |
if _, err := cw.Write(newline); err != nil { |
| 50 |
return 0, err |
| 51 |
} |
| 52 |
start = i + 1 |
| 53 |
} |
| 54 |
case 1: |
| 55 |
*s = 0 |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
if _, err := cw.Write(buf[start:]); err != nil { |
| 60 |
return 0, err |
| 61 |
} |
| 62 |
return len(buf), nil |
| 63 |
} |
| 64 |
|
| 65 |
func (cth *canonicalTextHash) Write(buf []byte) (int, error) { |
| 66 |
return writeCanonical(cth.h, buf, &cth.s) |
| 67 |
} |
| 68 |
|
| 69 |
func (cth *canonicalTextHash) Sum(in []byte) []byte { |
| 70 |
return cth.h.Sum(in) |
| 71 |
} |
| 72 |
|
| 73 |
func (cth *canonicalTextHash) Reset() { |
| 74 |
cth.h.Reset() |
| 75 |
cth.s = 0 |
| 76 |
} |
| 77 |
|
| 78 |
func (cth *canonicalTextHash) Size() int { |
| 79 |
return cth.h.Size() |
| 80 |
} |
| 81 |
|
| 82 |
func (cth *canonicalTextHash) BlockSize() int { |
| 83 |
return cth.h.BlockSize() |
| 84 |
} |
| 85 |
|
| 86 |
type canonicalTextWriteCloser struct { |
| 87 |
w io.WriteCloser |
| 88 |
s int |
| 89 |
} |
| 90 |
|
| 91 |
func (tw *canonicalTextWriteCloser) Write(buf []byte) (int, error) { |
| 92 |
return writeCanonical(tw.w, buf, &tw.s) |
| 93 |
} |
| 94 |
|
| 95 |
func (tw *canonicalTextWriteCloser) Close() error { |
| 96 |
return tw.w.Close() |
| 97 |
} |
| 98 |
|
| 99 |
type canonicalTextReader struct { |
| 100 |
r io.Reader |
| 101 |
buffer *bytes.Buffer |
| 102 |
s int |
| 103 |
} |
| 104 |
|
| 105 |
func (tr *canonicalTextReader) Read(buf []byte) (int, error) { |
| 106 |
if tr.buffer.Len() > 0 { |
| 107 |
return tr.buffer.Read(buf) |
| 108 |
} |
| 109 |
n, err := tr.r.Read(buf) |
| 110 |
if err != nil { |
| 111 |
return n, err |
| 112 |
} |
| 113 |
if _, err = writeCanonical(tr.buffer, buf[:n], &tr.s); err != nil { |
| 114 |
return n, err |
| 115 |
} |
| 116 |
return tr.buffer.Read(buf) |
| 117 |
} |