Coverage Report

Created: 2024-11-21 07:03

/src/cryptopp/files.h
Line
Count
Source (jump to first uncovered line)
1
// files.h - originally written and placed in the public domain by Wei Dai
2
3
/// \file files.h
4
/// \brief Classes providing file-based library services
5
/// \since Crypto++ 1.0
6
7
#ifndef CRYPTOPP_FILES_H
8
#define CRYPTOPP_FILES_H
9
10
#include "cryptlib.h"
11
#include "filters.h"
12
#include "argnames.h"
13
#include "smartptr.h"
14
15
#include <iostream>
16
#include <fstream>
17
18
NAMESPACE_BEGIN(CryptoPP)
19
20
/// \brief Implementation of Store interface
21
/// \details file-based implementation of Store interface
22
class CRYPTOPP_DLL FileStore : public Store, private FilterPutSpaceHelper, public NotCopyable
23
{
24
public:
25
  /// \brief Exception thrown when file-based error is encountered
26
  class Err : public Exception
27
  {
28
  public:
29
0
    Err(const std::string &s) : Exception(IO_ERROR, s) {}
30
  };
31
  /// \brief Exception thrown when file-based open error is encountered
32
0
  class OpenErr : public Err {public: OpenErr(const std::string &filename) : Err("FileStore: error opening file for reading: " + filename) {}};
33
  /// \brief Exception thrown when file-based read error is encountered
34
0
  class ReadErr : public Err {public: ReadErr() : Err("FileStore: error reading file") {}};
35
36
  /// \brief Construct a FileStore
37
0
  FileStore() : m_stream(NULLPTR), m_space(NULLPTR), m_len(0), m_waiting(0) {}
38
39
  /// \brief Construct a FileStore
40
  /// \param in an existing stream
41
  FileStore(std::istream &in) : m_stream(NULLPTR), m_space(NULLPTR), m_len(0), m_waiting(0)
42
0
    {StoreInitialize(MakeParameters(Name::InputStreamPointer(), &in));}
43
44
  /// \brief Construct a FileStore
45
  /// \param filename the narrow name of the file to open
46
  FileStore(const char *filename) : m_stream(NULLPTR), m_space(NULLPTR), m_len(0), m_waiting(0)
47
0
    {StoreInitialize(MakeParameters(Name::InputFileName(), filename ? filename : ""));}
48
49
#if defined(CRYPTOPP_UNIX_AVAILABLE) || defined(CRYPTOPP_DOXYGEN_PROCESSING) || (CRYPTOPP_MSC_VERSION >= 1400)
50
  /// \brief Construct a FileStore
51
  /// \param filename the Unicode name of the file to open
52
  /// \details On non-Windows OS, this function assumes that setlocale() has been called.
53
  FileStore(const wchar_t *filename)
54
0
    {StoreInitialize(MakeParameters(Name::InputFileNameWide(), filename));}
55
#endif
56
57
  /// \brief Retrieves the internal stream
58
  /// \return the internal stream pointer
59
0
  std::istream* GetStream() {return m_stream;}
60
61
  /// \brief Retrieves the internal stream
62
  /// \return the internal stream pointer
63
0
  const std::istream* GetStream() const {return m_stream;}
64
65
  /// \brief Provides the number of bytes ready for retrieval
66
  /// \return the number of bytes ready for retrieval
67
  /// \details All retrieval functions return the actual number of bytes retrieved, which is
68
  ///  the lesser of the request number and  MaxRetrievable()
69
  lword MaxRetrievable() const;
70
  size_t TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true);
71
  size_t CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end=LWORD_MAX, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true) const;
72
  lword Skip(lword skipMax=ULONG_MAX);
73
74
private:
75
  void StoreInitialize(const NameValuePairs &parameters);
76
77
  member_ptr<std::ifstream> m_file;
78
  std::istream *m_stream;
79
  byte *m_space;
80
  size_t m_len;
81
  bool m_waiting;
82
};
83
84
/// \brief Implementation of Store interface
85
/// \details file-based implementation of Store interface
86
class CRYPTOPP_DLL FileSource : public SourceTemplate<FileStore>
87
{
88
public:
89
  typedef FileStore::Err Err;
90
  typedef FileStore::OpenErr OpenErr;
91
  typedef FileStore::ReadErr ReadErr;
92
93
  /// \brief Construct a FileSource
94
  FileSource(BufferedTransformation *attachment = NULLPTR)
95
0
    : SourceTemplate<FileStore>(attachment) {}
96
97
  /// \brief Construct a FileSource
98
  /// \param in an existing stream
99
  /// \param pumpAll flag indicating if source data should be pumped to its attached transformation
100
  /// \param attachment an optional attached transformation
101
  FileSource(std::istream &in, bool pumpAll, BufferedTransformation *attachment = NULLPTR)
102
0
    : SourceTemplate<FileStore>(attachment) {SourceInitialize(pumpAll, MakeParameters(Name::InputStreamPointer(), &in));}
103
104
  /// \brief Construct a FileSource
105
  /// \param filename the narrow name of the file to open
106
  /// \param pumpAll flag indicating if source data should be pumped to its attached transformation
107
  /// \param attachment an optional attached transformation
108
  /// \param binary flag indicating if the file is binary
109
  FileSource(const char *filename, bool pumpAll, BufferedTransformation *attachment = NULLPTR, bool binary=true)
110
0
    : SourceTemplate<FileStore>(attachment) {SourceInitialize(pumpAll, MakeParameters(Name::InputFileName(), filename)(Name::InputBinaryMode(), binary));}
111
112
#if defined(CRYPTOPP_UNIX_AVAILABLE) || defined(CRYPTOPP_DOXYGEN_PROCESSING) || (CRYPTOPP_MSC_VERSION >= 1400)
113
  /// \brief Construct a FileSource
114
  /// \param filename the Unicode name of the file to open
115
  /// \param pumpAll flag indicating if source data should be pumped to its attached transformation
116
  /// \param attachment an optional attached transformation
117
  /// \param binary flag indicating if the file is binary
118
  /// \details On non-Windows OS, this function assumes that setlocale() has been called.
119
  FileSource(const wchar_t *filename, bool pumpAll, BufferedTransformation *attachment = NULLPTR, bool binary=true)
120
0
    : SourceTemplate<FileStore>(attachment) {SourceInitialize(pumpAll, MakeParameters(Name::InputFileNameWide(), filename)(Name::InputBinaryMode(), binary));}
121
#endif
122
123
  /// \brief Retrieves the internal stream
124
  /// \return the internal stream pointer
125
0
  std::istream* GetStream() {return m_store.GetStream();}
126
};
127
128
/// \brief Implementation of Store interface
129
/// \details file-based implementation of Sink interface
130
class CRYPTOPP_DLL FileSink : public Sink, public NotCopyable
131
{
132
public:
133
  /// \brief Exception thrown when file-based error is encountered
134
  class Err : public Exception
135
  {
136
  public:
137
0
    Err(const std::string &s) : Exception(IO_ERROR, s) {}
138
  };
139
  /// \brief Exception thrown when file-based open error is encountered
140
0
  class OpenErr : public Err {public: OpenErr(const std::string &filename) : Err("FileSink: error opening file for writing: " + filename) {}};
141
  /// \brief Exception thrown when file-based write error is encountered
142
0
  class WriteErr : public Err {public: WriteErr() : Err("FileSink: error writing file") {}};
143
144
  /// \brief Construct a FileSink
145
0
  FileSink() : m_stream(NULLPTR) {}
146
147
  /// \brief Construct a FileSink
148
  /// \param out an existing stream
149
  FileSink(std::ostream &out)
150
0
    {IsolatedInitialize(MakeParameters(Name::OutputStreamPointer(), &out));}
151
152
  /// \brief Construct a FileSink
153
  /// \param filename the narrow name of the file to open
154
  /// \param binary flag indicating if the file is binary
155
  FileSink(const char *filename, bool binary=true)
156
0
    {IsolatedInitialize(MakeParameters(Name::OutputFileName(), filename)(Name::OutputBinaryMode(), binary));}
157
158
#if defined(CRYPTOPP_UNIX_AVAILABLE) || (CRYPTOPP_MSC_VERSION >= 1400)
159
  /// \brief Construct a FileSink
160
  /// \param filename the Unicode name of the file to open
161
  /// \details On non-Windows OS, this function assumes that setlocale() has been called.
162
  FileSink(const wchar_t *filename, bool binary=true)
163
0
    {IsolatedInitialize(MakeParameters(Name::OutputFileNameWide(), filename)(Name::OutputBinaryMode(), binary));}
164
#endif
165
166
  /// \brief Retrieves the internal stream
167
  /// \return the internal stream pointer
168
0
  std::ostream* GetStream() {return m_stream;}
169
170
  void IsolatedInitialize(const NameValuePairs &parameters);
171
  size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking);
172
  bool IsolatedFlush(bool hardFlush, bool blocking);
173
174
private:
175
  member_ptr<std::ofstream> m_file;
176
  std::ostream *m_stream;
177
};
178
179
NAMESPACE_END
180
181
#endif