monotone

monotone Mtn Source Tree

Root/cryptopp/files.cpp

1// files.cpp - written and placed in the public domain by Wei Dai
2
3#include "pch.h"
4#include "files.h"
5
6NAMESPACE_BEGIN(CryptoPP)
7
8using namespace std;
9
10void Files_TestInstantiations()
11{
12FileStore f0;
13FileSource f1;
14FileSink f2;
15}
16
17void FileStore::StoreInitialize(const NameValuePairs &parameters)
18{
19const char *fileName;
20if (parameters.GetValue("InputFileName", fileName))
21{
22ios::openmode binary = parameters.GetValueWithDefault("InputBinaryMode", true) ? ios::binary : ios::openmode(0);
23m_file.open(fileName, ios::in | binary);
24if (!m_file)
25throw OpenErr(fileName);
26m_stream = &m_file;
27}
28else
29{
30m_stream = NULL;
31parameters.GetValue("InputStreamPointer", m_stream);
32}
33m_waiting = false;
34}
35
36unsigned long FileStore::MaxRetrievable() const
37{
38if (!m_stream)
39return 0;
40
41streampos current = m_stream->tellg();
42streampos end = m_stream->seekg(0, ios::end).tellg();
43m_stream->seekg(current);
44return end-current;
45}
46
47unsigned int FileStore::TransferTo2(BufferedTransformation &target, unsigned long &transferBytes, const std::string &channel, bool blocking)
48{
49if (!m_stream)
50{
51transferBytes = 0;
52return 0;
53}
54
55unsigned long size=transferBytes;
56transferBytes = 0;
57
58if (m_waiting)
59goto output;
60
61while (size && m_stream->good())
62{
63{
64unsigned int spaceSize = 1024;
65m_space = HelpCreatePutSpace(target, channel, 1, (unsigned int)STDMIN(size, (unsigned long)UINT_MAX), spaceSize);
66
67m_stream->read((char *)m_space, STDMIN(size, (unsigned long)spaceSize));
68}
69m_len = m_stream->gcount();
70unsigned int blockedBytes;
71output:
72blockedBytes = target.ChannelPutModifiable2(channel, m_space, m_len, 0, blocking);
73m_waiting = blockedBytes > 0;
74if (m_waiting)
75return blockedBytes;
76size -= m_len;
77transferBytes += m_len;
78}
79
80if (!m_stream->good() && !m_stream->eof())
81throw ReadErr();
82
83return 0;
84}
85
86unsigned int FileStore::CopyRangeTo2(BufferedTransformation &target, unsigned long &begin, unsigned long end, const std::string &channel, bool blocking) const
87{
88if (!m_stream)
89return 0;
90
91if (begin == 0 && end == 1)
92{
93int result = m_stream->peek();
94if (result == EOF)// GCC workaround: 2.95.2 doesn't have char_traits<char>::eof()
95return 0;
96else
97{
98unsigned int blockedBytes = target.ChannelPut(channel, byte(result), blocking);
99begin += 1-blockedBytes;
100return blockedBytes;
101}
102}
103
104// TODO: figure out what happens on cin
105streampos current = m_stream->tellg();
106streampos endPosition = m_stream->seekg(0, ios::end).tellg();
107streampos newPosition = current + (streamoff)begin;
108
109if (newPosition >= endPosition)
110{
111m_stream->seekg(current);
112return 0;// don't try to seek beyond the end of file
113}
114m_stream->seekg(newPosition);
115unsigned long total = 0;
116try
117{
118assert(!m_waiting);
119unsigned long copyMax = end-begin;
120unsigned int blockedBytes = const_cast<FileStore *>(this)->TransferTo2(target, copyMax, channel, blocking);
121begin += copyMax;
122if (blockedBytes)
123{
124const_cast<FileStore *>(this)->m_waiting = false;
125return blockedBytes;
126}
127}
128catch(...)
129{
130m_stream->clear();
131m_stream->seekg(current);
132throw;
133}
134m_stream->clear();
135m_stream->seekg(current);
136
137return 0;
138}
139
140void FileSink::IsolatedInitialize(const NameValuePairs &parameters)
141{
142const char *fileName;
143if (parameters.GetValue("OutputFileName", fileName))
144{
145ios::openmode binary = parameters.GetValueWithDefault("OutputBinaryMode", true) ? ios::binary : ios::openmode(0);
146m_file.open(fileName, ios::out | ios::trunc | binary);
147if (!m_file)
148throw OpenErr(fileName);
149m_stream = &m_file;
150}
151else
152{
153m_stream = NULL;
154parameters.GetValue("OutputStreamPointer", m_stream);
155}
156}
157
158bool FileSink::IsolatedFlush(bool hardFlush, bool blocking)
159{
160if (!m_stream)
161throw Err("FileSink: output stream not opened");
162
163m_stream->flush();
164if (!m_stream->good())
165throw WriteErr();
166
167return false;
168}
169
170unsigned int FileSink::Put2(const byte *inString, unsigned int length, int messageEnd, bool blocking)
171{
172if (!m_stream)
173throw Err("FileSink: output stream not opened");
174
175m_stream->write((const char *)inString, length);
176
177if (messageEnd)
178m_stream->flush();
179
180if (!m_stream->good())
181throw WriteErr();
182
183return 0;
184}
185
186NAMESPACE_END

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status