1 | // |
---|
2 | // DigestStream.cpp |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/Foundation/src/DigestStream.cpp#4 $ |
---|
5 | // |
---|
6 | // Library: Foundation |
---|
7 | // Package: Crypt |
---|
8 | // Module: DigestStream |
---|
9 | // |
---|
10 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
---|
11 | // and Contributors. |
---|
12 | // |
---|
13 | // Permission is hereby granted, free of charge, to any person or organization |
---|
14 | // obtaining a copy of the software and accompanying documentation covered by |
---|
15 | // this license (the "Software") to use, reproduce, display, distribute, |
---|
16 | // execute, and transmit the Software, and to prepare derivative works of the |
---|
17 | // Software, and to permit third-parties to whom the Software is furnished to |
---|
18 | // do so, all subject to the following: |
---|
19 | // |
---|
20 | // The copyright notices in the Software and this entire statement, including |
---|
21 | // the above license grant, this restriction and the following disclaimer, |
---|
22 | // must be included in all copies of the Software, in whole or in part, and |
---|
23 | // all derivative works of the Software, unless such copies or derivative |
---|
24 | // works are solely in the form of machine-executable object code generated by |
---|
25 | // a source language processor. |
---|
26 | // |
---|
27 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
28 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
29 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
---|
30 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
---|
31 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
---|
32 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
33 | // DEALINGS IN THE SOFTWARE. |
---|
34 | // |
---|
35 | |
---|
36 | |
---|
37 | #include "Poco/DigestStream.h" |
---|
38 | |
---|
39 | |
---|
40 | namespace Poco { |
---|
41 | |
---|
42 | |
---|
43 | const int DigestBuf::BUFFER_SIZE = 256; |
---|
44 | |
---|
45 | |
---|
46 | DigestBuf::DigestBuf(DigestEngine& eng): |
---|
47 | BufferedStreamBuf(BUFFER_SIZE, std::ios::out), |
---|
48 | _eng(eng), |
---|
49 | _pIstr(0), |
---|
50 | _pOstr(0) |
---|
51 | { |
---|
52 | } |
---|
53 | |
---|
54 | |
---|
55 | DigestBuf::DigestBuf(DigestEngine& eng, std::istream& istr): |
---|
56 | BufferedStreamBuf(BUFFER_SIZE, std::ios::in), |
---|
57 | _eng(eng), |
---|
58 | _pIstr(&istr), |
---|
59 | _pOstr(0) |
---|
60 | { |
---|
61 | } |
---|
62 | |
---|
63 | |
---|
64 | DigestBuf::DigestBuf(DigestEngine& eng, std::ostream& ostr): |
---|
65 | BufferedStreamBuf(BUFFER_SIZE, std::ios::out), |
---|
66 | _eng(eng), |
---|
67 | _pIstr(0), |
---|
68 | _pOstr(&ostr) |
---|
69 | { |
---|
70 | } |
---|
71 | |
---|
72 | |
---|
73 | DigestBuf::~DigestBuf() |
---|
74 | { |
---|
75 | } |
---|
76 | |
---|
77 | |
---|
78 | int DigestBuf::readFromDevice(char* buffer, std::streamsize length) |
---|
79 | { |
---|
80 | if (_pIstr && _pIstr->good()) |
---|
81 | { |
---|
82 | _pIstr->read(buffer, length); |
---|
83 | std::streamsize n = _pIstr->gcount(); |
---|
84 | if (n > 0) _eng.update(buffer, static_cast<unsigned>(n)); |
---|
85 | return static_cast<int>(n); |
---|
86 | } |
---|
87 | return -1; |
---|
88 | } |
---|
89 | |
---|
90 | |
---|
91 | int DigestBuf::writeToDevice(const char* buffer, std::streamsize length) |
---|
92 | { |
---|
93 | _eng.update(buffer, (unsigned) length); |
---|
94 | if (_pOstr) _pOstr->write(buffer, length); |
---|
95 | return static_cast<int>(length); |
---|
96 | } |
---|
97 | |
---|
98 | |
---|
99 | void DigestBuf::close() |
---|
100 | { |
---|
101 | sync(); |
---|
102 | if (_pOstr) _pOstr->flush(); |
---|
103 | } |
---|
104 | |
---|
105 | |
---|
106 | DigestIOS::DigestIOS(DigestEngine& eng): _buf(eng) |
---|
107 | { |
---|
108 | poco_ios_init(&_buf); |
---|
109 | } |
---|
110 | |
---|
111 | |
---|
112 | DigestIOS::DigestIOS(DigestEngine& eng, std::istream& istr): _buf(eng, istr) |
---|
113 | { |
---|
114 | poco_ios_init(&_buf); |
---|
115 | } |
---|
116 | |
---|
117 | |
---|
118 | DigestIOS::DigestIOS(DigestEngine& eng, std::ostream& ostr): _buf(eng, ostr) |
---|
119 | { |
---|
120 | poco_ios_init(&_buf); |
---|
121 | } |
---|
122 | |
---|
123 | |
---|
124 | DigestIOS::~DigestIOS() |
---|
125 | { |
---|
126 | } |
---|
127 | |
---|
128 | |
---|
129 | DigestBuf* DigestIOS::rdbuf() |
---|
130 | { |
---|
131 | return &_buf; |
---|
132 | } |
---|
133 | |
---|
134 | |
---|
135 | DigestInputStream::DigestInputStream(DigestEngine& eng, std::istream& istr): |
---|
136 | DigestIOS(eng, istr), |
---|
137 | std::istream(&_buf) |
---|
138 | { |
---|
139 | } |
---|
140 | |
---|
141 | |
---|
142 | DigestInputStream::~DigestInputStream() |
---|
143 | { |
---|
144 | } |
---|
145 | |
---|
146 | |
---|
147 | DigestOutputStream::DigestOutputStream(DigestEngine& eng): |
---|
148 | DigestIOS(eng), |
---|
149 | std::ostream(&_buf) |
---|
150 | { |
---|
151 | } |
---|
152 | |
---|
153 | |
---|
154 | DigestOutputStream::DigestOutputStream(DigestEngine& eng, std::ostream& ostr): |
---|
155 | DigestIOS(eng, ostr), |
---|
156 | std::ostream(&_buf) |
---|
157 | { |
---|
158 | } |
---|
159 | |
---|
160 | |
---|
161 | DigestOutputStream::~DigestOutputStream() |
---|
162 | { |
---|
163 | } |
---|
164 | |
---|
165 | |
---|
166 | void DigestOutputStream::close() |
---|
167 | { |
---|
168 | _buf.close(); |
---|
169 | } |
---|
170 | |
---|
171 | |
---|
172 | } // namespace Poco |
---|