handle Unicode filenames

pull/2/head
weidai 2009-07-11 01:48:12 +00:00
parent 1f00d79595
commit 92718c4754
4 changed files with 99 additions and 11 deletions

View File

@ -50,9 +50,11 @@ CRYPTOPP_DEFINE_NAME_STRING(SignatureVerificationFilterFlags) //!< word32
CRYPTOPP_DEFINE_NAME_STRING(InputBuffer) //!< ConstByteArrayParameter CRYPTOPP_DEFINE_NAME_STRING(InputBuffer) //!< ConstByteArrayParameter
CRYPTOPP_DEFINE_NAME_STRING(OutputBuffer) //!< ByteArrayParameter CRYPTOPP_DEFINE_NAME_STRING(OutputBuffer) //!< ByteArrayParameter
CRYPTOPP_DEFINE_NAME_STRING(InputFileName) //!< const char * CRYPTOPP_DEFINE_NAME_STRING(InputFileName) //!< const char *
CRYPTOPP_DEFINE_NAME_STRING(InputFileNameWide) //!< const wchar_t *
CRYPTOPP_DEFINE_NAME_STRING(InputStreamPointer) //!< std::istream * CRYPTOPP_DEFINE_NAME_STRING(InputStreamPointer) //!< std::istream *
CRYPTOPP_DEFINE_NAME_STRING(InputBinaryMode) //!< bool CRYPTOPP_DEFINE_NAME_STRING(InputBinaryMode) //!< bool
CRYPTOPP_DEFINE_NAME_STRING(OutputFileName) //!< const char * CRYPTOPP_DEFINE_NAME_STRING(OutputFileName) //!< const char *
CRYPTOPP_DEFINE_NAME_STRING(OutputFileNameWide) //!< const wchar_t *
CRYPTOPP_DEFINE_NAME_STRING(OutputStreamPointer) //!< std::ostream * CRYPTOPP_DEFINE_NAME_STRING(OutputStreamPointer) //!< std::ostream *
CRYPTOPP_DEFINE_NAME_STRING(OutputBinaryMode) //!< bool CRYPTOPP_DEFINE_NAME_STRING(OutputBinaryMode) //!< bool
CRYPTOPP_DEFINE_NAME_STRING(EncodingParameters) //!< ConstByteArrayParameter CRYPTOPP_DEFINE_NAME_STRING(EncodingParameters) //!< ConstByteArrayParameter

View File

@ -12,31 +12,58 @@ NAMESPACE_BEGIN(CryptoPP)
using namespace std; using namespace std;
#ifndef NDEBUG
void Files_TestInstantiations() void Files_TestInstantiations()
{ {
FileStore f0; FileStore f0;
FileSource f1; FileSource f1;
FileSink f2; FileSink f2;
} }
#endif
void FileStore::StoreInitialize(const NameValuePairs &parameters) void FileStore::StoreInitialize(const NameValuePairs &parameters)
{ {
m_file.reset(new std::ifstream); m_waiting = false;
m_stream = NULL;
m_file.release();
const char *fileName; const char *fileName;
const wchar_t *fileNameWide;
#ifdef CRYPTOPP_UNIX_AVAILABLE
std::string narrowed;
#endif
if (parameters.GetValue(Name::InputFileName(), fileName)) if (parameters.GetValue(Name::InputFileName(), fileName))
{ {
#ifdef CRYPTOPP_UNIX_AVAILABLE
narrowName:
#endif
ios::openmode binary = parameters.GetValueWithDefault(Name::InputBinaryMode(), true) ? ios::binary : ios::openmode(0); ios::openmode binary = parameters.GetValueWithDefault(Name::InputBinaryMode(), true) ? ios::binary : ios::openmode(0);
m_file.reset(new std::ifstream);
m_file->open(fileName, ios::in | binary); m_file->open(fileName, ios::in | binary);
if (!*m_file) if (!*m_file)
throw OpenErr(fileName); throw OpenErr(fileName);
m_stream = m_file.get(); m_stream = m_file.get();
} }
else #if defined(CRYPTOPP_UNIX_AVAILABLE) || _MSC_VER >= 1400
else if (parameters.GetValue(Name::InputFileNameWide(), fileNameWide))
{ {
m_stream = NULL; #if _MSC_VER >= 1400
parameters.GetValue(Name::InputStreamPointer(), m_stream); ios::openmode binary = parameters.GetValueWithDefault(Name::InputBinaryMode(), true) ? ios::binary : ios::openmode(0);
m_file.reset(new std::ifstream);
m_file->open(fileNameWide, ios::in | binary);
if (!*m_file)
throw OpenErr(StringNarrow(fileNameWide));
m_stream = m_file.get();
#else
narrowed = StringNarrow(fileNameWide);
fileName = narrowed.c_str();
goto narrowName;
#endif
} }
m_waiting = false; #endif
else
parameters.GetValue(Name::InputStreamPointer(), m_stream);
} }
lword FileStore::MaxRetrievable() const lword FileStore::MaxRetrievable() const
@ -144,6 +171,9 @@ size_t FileStore::CopyRangeTo2(BufferedTransformation &target, lword &begin, lwo
lword FileStore::Skip(lword skipMax) lword FileStore::Skip(lword skipMax)
{ {
if (!m_stream)
return 0;
lword oldPos = m_stream->tellg(); lword oldPos = m_stream->tellg();
std::istream::off_type offset; std::istream::off_type offset;
if (!SafeConvert(skipMax, offset)) if (!SafeConvert(skipMax, offset))
@ -154,21 +184,46 @@ lword FileStore::Skip(lword skipMax)
void FileSink::IsolatedInitialize(const NameValuePairs &parameters) void FileSink::IsolatedInitialize(const NameValuePairs &parameters)
{ {
m_file.reset(new std::ofstream); m_stream = NULL;
m_file.release();
const char *fileName; const char *fileName;
const wchar_t *fileNameWide;
#ifdef CRYPTOPP_UNIX_AVAILABLE
std::string narrowed;
#endif
if (parameters.GetValue(Name::OutputFileName(), fileName)) if (parameters.GetValue(Name::OutputFileName(), fileName))
{ {
#ifdef CRYPTOPP_UNIX_AVAILABLE
narrowName:
#endif
ios::openmode binary = parameters.GetValueWithDefault(Name::OutputBinaryMode(), true) ? ios::binary : ios::openmode(0); ios::openmode binary = parameters.GetValueWithDefault(Name::OutputBinaryMode(), true) ? ios::binary : ios::openmode(0);
m_file.reset(new std::ofstream);
m_file->open(fileName, ios::out | ios::trunc | binary); m_file->open(fileName, ios::out | ios::trunc | binary);
if (!*m_file) if (!*m_file)
throw OpenErr(fileName); throw OpenErr(fileName);
m_stream = m_file.get(); m_stream = m_file.get();
} }
else #if defined(CRYPTOPP_UNIX_AVAILABLE) || _MSC_VER >= 1400
else if (parameters.GetValue(Name::OutputFileNameWide(), fileNameWide))
{ {
m_stream = NULL; #if _MSC_VER >= 1400
parameters.GetValue(Name::OutputStreamPointer(), m_stream); ios::openmode binary = parameters.GetValueWithDefault(Name::OutputBinaryMode(), true) ? ios::binary : ios::openmode(0);
m_file.reset(new std::ofstream);
m_file->open(fileNameWide, ios::out | ios::trunc | binary);
if (!*m_file)
throw OpenErr(StringNarrow(fileNameWide));
m_stream = m_file.get();
#else
narrowed = StringNarrow(fileNameWide);
fileName = narrowed.c_str();
goto narrowName;
#endif
} }
#endif
else
parameters.GetValue(Name::OutputStreamPointer(), m_stream);
} }
bool FileSink::IsolatedFlush(bool hardFlush, bool blocking) bool FileSink::IsolatedFlush(bool hardFlush, bool blocking)

17
files.h
View File

@ -27,6 +27,11 @@ public:
{StoreInitialize(MakeParameters(Name::InputStreamPointer(), &in));} {StoreInitialize(MakeParameters(Name::InputStreamPointer(), &in));}
FileStore(const char *filename) FileStore(const char *filename)
{StoreInitialize(MakeParameters(Name::InputFileName(), filename));} {StoreInitialize(MakeParameters(Name::InputFileName(), filename));}
#if defined(CRYPTOPP_UNIX_AVAILABLE) || _MSC_VER >= 1400
//! specify file with Unicode name. On non-Windows OS, this function assumes that setlocale() has been called.
FileStore(const wchar_t *filename)
{StoreInitialize(MakeParameters(Name::InputFileNameWide(), filename));}
#endif
std::istream* GetStream() {return m_stream;} std::istream* GetStream() {return m_stream;}
@ -59,6 +64,11 @@ public:
: SourceTemplate<FileStore>(attachment) {SourceInitialize(pumpAll, MakeParameters(Name::InputStreamPointer(), &in));} : SourceTemplate<FileStore>(attachment) {SourceInitialize(pumpAll, MakeParameters(Name::InputStreamPointer(), &in));}
FileSource(const char *filename, bool pumpAll, BufferedTransformation *attachment = NULL, bool binary=true) FileSource(const char *filename, bool pumpAll, BufferedTransformation *attachment = NULL, bool binary=true)
: SourceTemplate<FileStore>(attachment) {SourceInitialize(pumpAll, MakeParameters(Name::InputFileName(), filename)(Name::InputBinaryMode(), binary));} : SourceTemplate<FileStore>(attachment) {SourceInitialize(pumpAll, MakeParameters(Name::InputFileName(), filename)(Name::InputBinaryMode(), binary));}
#if defined(CRYPTOPP_UNIX_AVAILABLE) || _MSC_VER >= 1400
//! specify file with Unicode name. On non-Windows OS, this function assumes that setlocale() has been called.
FileSource(const wchar_t *filename, bool pumpAll, BufferedTransformation *attachment = NULL, bool binary=true)
: SourceTemplate<FileStore>(attachment) {SourceInitialize(pumpAll, MakeParameters(Name::InputFileNameWide(), filename)(Name::InputBinaryMode(), binary));}
#endif
std::istream* GetStream() {return m_store.GetStream();} std::istream* GetStream() {return m_store.GetStream();}
}; };
@ -79,7 +89,12 @@ public:
FileSink(std::ostream &out) FileSink(std::ostream &out)
{IsolatedInitialize(MakeParameters(Name::OutputStreamPointer(), &out));} {IsolatedInitialize(MakeParameters(Name::OutputStreamPointer(), &out));}
FileSink(const char *filename, bool binary=true) FileSink(const char *filename, bool binary=true)
{IsolatedInitialize(MakeParameters(Name::OutputFileName(), filename)("OutputBinaryMode", binary));} {IsolatedInitialize(MakeParameters(Name::OutputFileName(), filename)(Name::OutputBinaryMode(), binary));}
#if defined(CRYPTOPP_UNIX_AVAILABLE) || _MSC_VER >= 1400
//! specify file with Unicode name. On non-Windows OS, this function assumes that setlocale() has been called.
FileSink(const wchar_t *filename, bool binary=true)
{IsolatedInitialize(MakeParameters(Name::OutputFileNameWide(), filename)(Name::OutputBinaryMode(), binary));}
#endif
std::ostream* GetStream() {return m_stream;} std::ostream* GetStream() {return m_stream;}

18
misc.h
View File

@ -122,7 +122,7 @@ template <class T, class F, int instance>
const T & Singleton<T, F, instance>::Ref(CRYPTOPP_NOINLINE_DOTDOTDOT) const const T & Singleton<T, F, instance>::Ref(CRYPTOPP_NOINLINE_DOTDOTDOT) const
{ {
static simple_ptr<T> s_pObject; static simple_ptr<T> s_pObject;
static char s_objectState = 0; static volatile char s_objectState = 0;
retry: retry:
switch (s_objectState) switch (s_objectState)
@ -547,6 +547,22 @@ inline void SecureWipeArray(T *buf, size_t n)
SecureWipeBuffer((byte *)buf, n * sizeof(T)); SecureWipeBuffer((byte *)buf, n * sizeof(T));
} }
// this function uses wcstombs(), which assumes that setlocale() has been called
static std::string StringNarrow(const wchar_t *str)
{
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable: 4996) // 'wcstombs': This function or variable may be unsafe.
#endif
size_t size = wcstombs(NULL, str, 0);
std::string result(size, 0);
wcstombs(&result[0], str, size);
return result;
#ifdef _MSC_VER
#pragma warning(pop)
#endif
}
// ************** rotate functions *************** // ************** rotate functions ***************
template <class T> inline T rotlFixed(T x, unsigned int y) template <class T> inline T rotlFixed(T x, unsigned int y)