1 | // |
2 | // DigestStream.cpp |
3 | // |
4 | // Library: Foundation |
5 | // Package: Crypt |
6 | // Module: DigestStream |
7 | // |
8 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
9 | // and Contributors. |
10 | // |
11 | // SPDX-License-Identifier: BSL-1.0 |
12 | // |
13 | |
14 | |
15 | #include "Poco/DigestStream.h" |
16 | |
17 | |
18 | namespace Poco { |
19 | |
20 | |
21 | const int DigestBuf::BUFFER_SIZE = 256; |
22 | |
23 | |
24 | DigestBuf::DigestBuf(DigestEngine& eng): |
25 | BufferedStreamBuf(BUFFER_SIZE, std::ios::out), |
26 | _eng(eng), |
27 | _pIstr(0), |
28 | _pOstr(0) |
29 | { |
30 | } |
31 | |
32 | |
33 | DigestBuf::DigestBuf(DigestEngine& eng, std::istream& istr): |
34 | BufferedStreamBuf(BUFFER_SIZE, std::ios::in), |
35 | _eng(eng), |
36 | _pIstr(&istr), |
37 | _pOstr(0) |
38 | { |
39 | } |
40 | |
41 | |
42 | DigestBuf::DigestBuf(DigestEngine& eng, std::ostream& ostr): |
43 | BufferedStreamBuf(BUFFER_SIZE, std::ios::out), |
44 | _eng(eng), |
45 | _pIstr(0), |
46 | _pOstr(&ostr) |
47 | { |
48 | } |
49 | |
50 | |
51 | DigestBuf::~DigestBuf() |
52 | { |
53 | } |
54 | |
55 | |
56 | int DigestBuf::readFromDevice(char* buffer, std::streamsize length) |
57 | { |
58 | if (_pIstr && _pIstr->good()) |
59 | { |
60 | _pIstr->read(buffer, length); |
61 | std::streamsize n = _pIstr->gcount(); |
62 | if (n > 0) _eng.update(buffer, static_cast<unsigned>(n)); |
63 | return static_cast<int>(n); |
64 | } |
65 | return -1; |
66 | } |
67 | |
68 | |
69 | int DigestBuf::writeToDevice(const char* buffer, std::streamsize length) |
70 | { |
71 | _eng.update(buffer, (unsigned) length); |
72 | if (_pOstr) _pOstr->write(buffer, length); |
73 | return static_cast<int>(length); |
74 | } |
75 | |
76 | |
77 | void DigestBuf::close() |
78 | { |
79 | sync(); |
80 | if (_pOstr) _pOstr->flush(); |
81 | } |
82 | |
83 | |
84 | DigestIOS::DigestIOS(DigestEngine& eng): _buf(eng) |
85 | { |
86 | poco_ios_init(&_buf); |
87 | } |
88 | |
89 | |
90 | DigestIOS::DigestIOS(DigestEngine& eng, std::istream& istr): _buf(eng, istr) |
91 | { |
92 | poco_ios_init(&_buf); |
93 | } |
94 | |
95 | |
96 | DigestIOS::DigestIOS(DigestEngine& eng, std::ostream& ostr): _buf(eng, ostr) |
97 | { |
98 | poco_ios_init(&_buf); |
99 | } |
100 | |
101 | |
102 | DigestIOS::~DigestIOS() |
103 | { |
104 | } |
105 | |
106 | |
107 | DigestBuf* DigestIOS::rdbuf() |
108 | { |
109 | return &_buf; |
110 | } |
111 | |
112 | |
113 | DigestInputStream::DigestInputStream(DigestEngine& eng, std::istream& istr): |
114 | DigestIOS(eng, istr), |
115 | std::istream(&_buf) |
116 | { |
117 | } |
118 | |
119 | |
120 | DigestInputStream::~DigestInputStream() |
121 | { |
122 | } |
123 | |
124 | |
125 | DigestOutputStream::DigestOutputStream(DigestEngine& eng): |
126 | DigestIOS(eng), |
127 | std::ostream(&_buf) |
128 | { |
129 | } |
130 | |
131 | |
132 | DigestOutputStream::DigestOutputStream(DigestEngine& eng, std::ostream& ostr): |
133 | DigestIOS(eng, ostr), |
134 | std::ostream(&_buf) |
135 | { |
136 | } |
137 | |
138 | |
139 | DigestOutputStream::~DigestOutputStream() |
140 | { |
141 | } |
142 | |
143 | |
144 | void DigestOutputStream::close() |
145 | { |
146 | _buf.close(); |
147 | } |
148 | |
149 | |
150 | } // namespace Poco |
151 | |