Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:55:02

0001 // files.h - originally written and placed in the public domain by Wei Dai

0002 
0003 /// \file files.h

0004 /// \brief Classes providing file-based library services

0005 /// \since Crypto++ 1.0

0006 
0007 #ifndef CRYPTOPP_FILES_H
0008 #define CRYPTOPP_FILES_H
0009 
0010 #include "cryptlib.h"
0011 #include "filters.h"
0012 #include "argnames.h"
0013 #include "smartptr.h"
0014 
0015 #include <iostream>
0016 #include <fstream>
0017 
0018 NAMESPACE_BEGIN(CryptoPP)
0019 
0020 /// \brief Implementation of Store interface

0021 /// \details file-based implementation of Store interface

0022 class CRYPTOPP_DLL FileStore : public Store, private FilterPutSpaceHelper, public NotCopyable
0023 {
0024 public:
0025     /// \brief Exception thrown when file-based error is encountered

0026     class Err : public Exception
0027     {
0028     public:
0029         Err(const std::string &s) : Exception(IO_ERROR, s) {}
0030     };
0031     /// \brief Exception thrown when file-based open error is encountered

0032     class OpenErr : public Err {public: OpenErr(const std::string &filename) : Err("FileStore: error opening file for reading: " + filename) {}};
0033     /// \brief Exception thrown when file-based read error is encountered

0034     class ReadErr : public Err {public: ReadErr() : Err("FileStore: error reading file") {}};
0035 
0036     /// \brief Construct a FileStore

0037     FileStore() : m_stream(NULLPTR), m_space(NULLPTR), m_len(0), m_waiting(0) {}
0038 
0039     /// \brief Construct a FileStore

0040     /// \param in an existing stream

0041     FileStore(std::istream &in) : m_stream(NULLPTR), m_space(NULLPTR), m_len(0), m_waiting(0)
0042         {StoreInitialize(MakeParameters(Name::InputStreamPointer(), &in));}
0043 
0044     /// \brief Construct a FileStore

0045     /// \param filename the narrow name of the file to open

0046     FileStore(const char *filename) : m_stream(NULLPTR), m_space(NULLPTR), m_len(0), m_waiting(0)
0047         {StoreInitialize(MakeParameters(Name::InputFileName(), filename ? filename : ""));}
0048 
0049 #if defined(CRYPTOPP_UNIX_AVAILABLE) || defined(CRYPTOPP_DOXYGEN_PROCESSING) || (CRYPTOPP_MSC_VERSION >= 1400)
0050     /// \brief Construct a FileStore

0051     /// \param filename the Unicode name of the file to open

0052     /// \details On non-Windows OS, this function assumes that setlocale() has been called.

0053     FileStore(const wchar_t *filename)
0054         {StoreInitialize(MakeParameters(Name::InputFileNameWide(), filename));}
0055 #endif
0056 
0057     /// \brief Retrieves the internal stream

0058     /// \return the internal stream pointer

0059     std::istream* GetStream() {return m_stream;}
0060 
0061     /// \brief Retrieves the internal stream

0062     /// \return the internal stream pointer

0063     const std::istream* GetStream() const {return m_stream;}
0064 
0065     /// \brief Provides the number of bytes ready for retrieval

0066     /// \return the number of bytes ready for retrieval

0067     /// \details All retrieval functions return the actual number of bytes retrieved, which is

0068     ///  the lesser of the request number and  MaxRetrievable()

0069     lword MaxRetrievable() const;
0070     size_t TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true);
0071     size_t CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end=LWORD_MAX, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true) const;
0072     lword Skip(lword skipMax=ULONG_MAX);
0073 
0074 private:
0075     void StoreInitialize(const NameValuePairs &parameters);
0076 
0077     member_ptr<std::ifstream> m_file;
0078     std::istream *m_stream;
0079     byte *m_space;
0080     size_t m_len;
0081     bool m_waiting;
0082 };
0083 
0084 /// \brief Implementation of Store interface

0085 /// \details file-based implementation of Store interface

0086 class CRYPTOPP_DLL FileSource : public SourceTemplate<FileStore>
0087 {
0088 public:
0089     typedef FileStore::Err Err;
0090     typedef FileStore::OpenErr OpenErr;
0091     typedef FileStore::ReadErr ReadErr;
0092 
0093     /// \brief Construct a FileSource

0094     FileSource(BufferedTransformation *attachment = NULLPTR)
0095         : SourceTemplate<FileStore>(attachment) {}
0096 
0097     /// \brief Construct a FileSource

0098     /// \param in an existing stream

0099     /// \param pumpAll flag indicating if source data should be pumped to its attached transformation

0100     /// \param attachment an optional attached transformation

0101     FileSource(std::istream &in, bool pumpAll, BufferedTransformation *attachment = NULLPTR)
0102         : SourceTemplate<FileStore>(attachment) {SourceInitialize(pumpAll, MakeParameters(Name::InputStreamPointer(), &in));}
0103 
0104     /// \brief Construct a FileSource

0105     /// \param filename the narrow name of the file to open

0106     /// \param pumpAll flag indicating if source data should be pumped to its attached transformation

0107     /// \param attachment an optional attached transformation

0108     /// \param binary flag indicating if the file is binary

0109     FileSource(const char *filename, bool pumpAll, BufferedTransformation *attachment = NULLPTR, bool binary=true)
0110         : SourceTemplate<FileStore>(attachment) {SourceInitialize(pumpAll, MakeParameters(Name::InputFileName(), filename)(Name::InputBinaryMode(), binary));}
0111 
0112 #if defined(CRYPTOPP_UNIX_AVAILABLE) || defined(CRYPTOPP_DOXYGEN_PROCESSING) || (CRYPTOPP_MSC_VERSION >= 1400)
0113     /// \brief Construct a FileSource

0114     /// \param filename the Unicode name of the file to open

0115     /// \param pumpAll flag indicating if source data should be pumped to its attached transformation

0116     /// \param attachment an optional attached transformation

0117     /// \param binary flag indicating if the file is binary

0118     /// \details On non-Windows OS, this function assumes that setlocale() has been called.

0119     FileSource(const wchar_t *filename, bool pumpAll, BufferedTransformation *attachment = NULLPTR, bool binary=true)
0120         : SourceTemplate<FileStore>(attachment) {SourceInitialize(pumpAll, MakeParameters(Name::InputFileNameWide(), filename)(Name::InputBinaryMode(), binary));}
0121 #endif
0122 
0123     /// \brief Retrieves the internal stream

0124     /// \return the internal stream pointer

0125     std::istream* GetStream() {return m_store.GetStream();}
0126 };
0127 
0128 /// \brief Implementation of Store interface

0129 /// \details file-based implementation of Sink interface

0130 class CRYPTOPP_DLL FileSink : public Sink, public NotCopyable
0131 {
0132 public:
0133     /// \brief Exception thrown when file-based error is encountered

0134     class Err : public Exception
0135     {
0136     public:
0137         Err(const std::string &s) : Exception(IO_ERROR, s) {}
0138     };
0139     /// \brief Exception thrown when file-based open error is encountered

0140     class OpenErr : public Err {public: OpenErr(const std::string &filename) : Err("FileSink: error opening file for writing: " + filename) {}};
0141     /// \brief Exception thrown when file-based write error is encountered

0142     class WriteErr : public Err {public: WriteErr() : Err("FileSink: error writing file") {}};
0143 
0144     /// \brief Construct a FileSink

0145     FileSink() : m_stream(NULLPTR) {}
0146 
0147     /// \brief Construct a FileSink

0148     /// \param out an existing stream

0149     FileSink(std::ostream &out)
0150         {IsolatedInitialize(MakeParameters(Name::OutputStreamPointer(), &out));}
0151 
0152     /// \brief Construct a FileSink

0153     /// \param filename the narrow name of the file to open

0154     /// \param binary flag indicating if the file is binary

0155     FileSink(const char *filename, bool binary=true)
0156         {IsolatedInitialize(MakeParameters(Name::OutputFileName(), filename)(Name::OutputBinaryMode(), binary));}
0157 
0158 #if defined(CRYPTOPP_UNIX_AVAILABLE) || (CRYPTOPP_MSC_VERSION >= 1400)
0159     /// \brief Construct a FileSink

0160     /// \param filename the Unicode name of the file to open

0161     /// \details On non-Windows OS, this function assumes that setlocale() has been called.

0162     FileSink(const wchar_t *filename, bool binary=true)
0163         {IsolatedInitialize(MakeParameters(Name::OutputFileNameWide(), filename)(Name::OutputBinaryMode(), binary));}
0164 #endif
0165 
0166     /// \brief Retrieves the internal stream

0167     /// \return the internal stream pointer

0168     std::ostream* GetStream() {return m_stream;}
0169 
0170     void IsolatedInitialize(const NameValuePairs &parameters);
0171     size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking);
0172     bool IsolatedFlush(bool hardFlush, bool blocking);
0173 
0174 private:
0175     member_ptr<std::ofstream> m_file;
0176     std::ostream *m_stream;
0177 };
0178 
0179 NAMESPACE_END
0180 
0181 #endif