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
DigestBuf(DigestEngine & eng)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
DigestBuf(DigestEngine & eng,std::istream & istr)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
DigestBuf(DigestEngine & eng,std::ostream & ostr)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
~DigestBuf()51 DigestBuf::~DigestBuf()
52 {
53 }
54
55
readFromDevice(char * buffer,std::streamsize length)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
writeToDevice(const char * buffer,std::streamsize length)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
close()77 void DigestBuf::close()
78 {
79 sync();
80 if (_pOstr) _pOstr->flush();
81 }
82
83
DigestIOS(DigestEngine & eng)84 DigestIOS::DigestIOS(DigestEngine& eng): _buf(eng)
85 {
86 poco_ios_init(&_buf);
87 }
88
89
DigestIOS(DigestEngine & eng,std::istream & istr)90 DigestIOS::DigestIOS(DigestEngine& eng, std::istream& istr): _buf(eng, istr)
91 {
92 poco_ios_init(&_buf);
93 }
94
95
DigestIOS(DigestEngine & eng,std::ostream & ostr)96 DigestIOS::DigestIOS(DigestEngine& eng, std::ostream& ostr): _buf(eng, ostr)
97 {
98 poco_ios_init(&_buf);
99 }
100
101
~DigestIOS()102 DigestIOS::~DigestIOS()
103 {
104 }
105
106
rdbuf()107 DigestBuf* DigestIOS::rdbuf()
108 {
109 return &_buf;
110 }
111
112
DigestInputStream(DigestEngine & eng,std::istream & istr)113 DigestInputStream::DigestInputStream(DigestEngine& eng, std::istream& istr):
114 DigestIOS(eng, istr),
115 std::istream(&_buf)
116 {
117 }
118
119
~DigestInputStream()120 DigestInputStream::~DigestInputStream()
121 {
122 }
123
124
DigestOutputStream(DigestEngine & eng)125 DigestOutputStream::DigestOutputStream(DigestEngine& eng):
126 DigestIOS(eng),
127 std::ostream(&_buf)
128 {
129 }
130
131
DigestOutputStream(DigestEngine & eng,std::ostream & ostr)132 DigestOutputStream::DigestOutputStream(DigestEngine& eng, std::ostream& ostr):
133 DigestIOS(eng, ostr),
134 std::ostream(&_buf)
135 {
136 }
137
138
~DigestOutputStream()139 DigestOutputStream::~DigestOutputStream()
140 {
141 }
142
143
close()144 void DigestOutputStream::close()
145 {
146 _buf.close();
147 }
148
149
150 } // namespace Poco
151