LEFT | RIGHT |
1 // Copyright 2010 The Go Authors. All rights reserved. | 1 // Copyright 2010 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package cipher | 5 package cipher |
6 | 6 |
7 import ( | 7 import ( |
8 "os" | 8 "os" |
9 "io" | 9 "io" |
10 ) | 10 ) |
(...skipping 11 matching lines...) Expand all Loading... |
22 func (r StreamReader) Read(dst []byte) (n int, err os.Error) { | 22 func (r StreamReader) Read(dst []byte) (n int, err os.Error) { |
23 n, err = r.R.Read(dst) | 23 n, err = r.R.Read(dst) |
24 r.S.XORKeyStream(dst[:n], dst[:n]) | 24 r.S.XORKeyStream(dst[:n], dst[:n]) |
25 return | 25 return |
26 } | 26 } |
27 | 27 |
28 // StreamWriter wraps a Stream into an io.Writer. It simply calls XORKeyStream | 28 // StreamWriter wraps a Stream into an io.Writer. It simply calls XORKeyStream |
29 // to process each slice of data which passes through. If any Write call | 29 // to process each slice of data which passes through. If any Write call |
30 // returns short then the StreamWriter is out of sync and must be discarded. | 30 // returns short then the StreamWriter is out of sync and must be discarded. |
31 type StreamWriter struct { | 31 type StreamWriter struct { |
32 » S Stream | 32 » S Stream |
33 » W io.Writer | 33 » W io.Writer |
34 » Desynced os.Error | 34 » Err os.Error |
35 } | 35 } |
36 | 36 |
37 func (w StreamWriter) Write(src []byte) (n int, err os.Error) { | 37 func (w StreamWriter) Write(src []byte) (n int, err os.Error) { |
38 » if w.Desynced != nil { | 38 » if w.Err != nil { |
39 » » return 0, w.Desynced | 39 » » return 0, w.Err |
40 } | 40 } |
41 c := make([]byte, len(src)) | 41 c := make([]byte, len(src)) |
42 w.S.XORKeyStream(c, src) | 42 w.S.XORKeyStream(c, src) |
43 n, err = w.W.Write(c) | 43 n, err = w.W.Write(c) |
44 if n != len(src) { | 44 if n != len(src) { |
45 » » w.Desynced = os.ErrorString("cipher.StreamWriter: stream out of
sync") | 45 » » if err == nil { // should never happen |
| 46 » » » err = io.ErrShortWrite |
| 47 » » } |
| 48 » » w.Err = err |
46 } | 49 } |
47 return | 50 return |
48 } | 51 } |
49 | 52 |
50 func (w StreamWriter) Close() os.Error { | 53 func (w StreamWriter) Close() os.Error { |
51 // This saves us from either requiring a WriteCloser or having a | 54 // This saves us from either requiring a WriteCloser or having a |
52 // StreamWriterCloser. | 55 // StreamWriterCloser. |
53 return w.W.(io.Closer).Close() | 56 return w.W.(io.Closer).Close() |
54 } | 57 } |
LEFT | RIGHT |