summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorweidai <weidai@57ff6487-cd31-0410-9ec3-f628ee90f5f0>2009-07-11 22:50:18 +0000
committerweidai <weidai@57ff6487-cd31-0410-9ec3-f628ee90f5f0>2009-07-11 22:50:18 +0000
commit14e7017ab6366f088aa3b2dba794847bb22a16d4 (patch)
tree3be29c508762394ff9a52d6077c5edc3c72f16d0
parent4b53fa6f2956ccf4b0cdb98adeceeaf66b67612b (diff)
downloadcryptopp-14e7017ab6366f088aa3b2dba794847bb22a16d4.tar.gz
improve Unicode filename handling
git-svn-id: svn://svn.code.sf.net/p/cryptopp/code/trunk/c5@473 57ff6487-cd31-0410-9ec3-f628ee90f5f0
-rw-r--r--files.cpp104
-rw-r--r--misc.h9
2 files changed, 56 insertions, 57 deletions
diff --git a/files.cpp b/files.cpp
index 4d4f936..6a29ed5 100644
--- a/files.cpp
+++ b/files.cpp
@@ -27,43 +27,39 @@ void FileStore::StoreInitialize(const NameValuePairs &parameters)
m_stream = NULL;
m_file.release();
- const char *fileName;
- const wchar_t *fileNameWide;
-#ifdef CRYPTOPP_UNIX_AVAILABLE
- std::string narrowed;
+ const char *fileName = NULL;
+#if defined(CRYPTOPP_UNIX_AVAILABLE) || _MSC_VER >= 1400
+ const wchar_t *fileNameWide = NULL;
+ if (!parameters.GetValue(Name::InputFileNameWide(), fileNameWide))
#endif
+ if (!parameters.GetValue(Name::InputFileName(), fileName))
+ {
+ parameters.GetValue(Name::InputStreamPointer(), m_stream);
+ return;
+ }
- if (parameters.GetValue(Name::InputFileName(), fileName))
- {
+ ios::openmode binary = parameters.GetValueWithDefault(Name::InputBinaryMode(), true) ? ios::binary : ios::openmode(0);
+ m_file.reset(new std::ifstream);
#ifdef CRYPTOPP_UNIX_AVAILABLE
-narrowName:
+ std::string narrowed;
+ if (fileNameWide)
+ fileName = (narrowed = StringNarrow(fileNameWide)).c_str();
#endif
- 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);
- if (!*m_file)
- throw OpenErr(fileName);
- m_stream = m_file.get();
- }
-#if defined(CRYPTOPP_UNIX_AVAILABLE) || _MSC_VER >= 1400
- else if (parameters.GetValue(Name::InputFileNameWide(), fileNameWide))
+#if _MSC_VER >= 1400
+ if (fileNameWide)
{
- #if _MSC_VER >= 1400
- 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
+ throw OpenErr(StringNarrow(fileNameWide, false));
}
#endif
- else
- parameters.GetValue(Name::InputStreamPointer(), m_stream);
+ if (fileName)
+ {
+ m_file->open(fileName, ios::in | binary);
+ if (!*m_file)
+ throw OpenErr(fileName);
+ }
+ m_stream = m_file.get();
}
lword FileStore::MaxRetrievable() const
@@ -187,43 +183,39 @@ void FileSink::IsolatedInitialize(const NameValuePairs &parameters)
m_stream = NULL;
m_file.release();
- const char *fileName;
- const wchar_t *fileNameWide;
-#ifdef CRYPTOPP_UNIX_AVAILABLE
- std::string narrowed;
+ const char *fileName = NULL;
+#if defined(CRYPTOPP_UNIX_AVAILABLE) || _MSC_VER >= 1400
+ const wchar_t *fileNameWide = NULL;
+ if (!parameters.GetValue(Name::OutputFileNameWide(), fileNameWide))
#endif
+ if (!parameters.GetValue(Name::OutputFileName(), fileName))
+ {
+ parameters.GetValue(Name::OutputStreamPointer(), m_stream);
+ return;
+ }
- if (parameters.GetValue(Name::OutputFileName(), fileName))
- {
+ ios::openmode binary = parameters.GetValueWithDefault(Name::OutputBinaryMode(), true) ? ios::binary : ios::openmode(0);
+ m_file.reset(new std::ofstream);
#ifdef CRYPTOPP_UNIX_AVAILABLE
-narrowName:
+ std::string narrowed;
+ if (fileNameWide)
+ fileName = (narrowed = StringNarrow(fileNameWide)).c_str();
#endif
- 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);
- if (!*m_file)
- throw OpenErr(fileName);
- m_stream = m_file.get();
- }
-#if defined(CRYPTOPP_UNIX_AVAILABLE) || _MSC_VER >= 1400
- else if (parameters.GetValue(Name::OutputFileNameWide(), fileNameWide))
+#if _MSC_VER >= 1400
+ if (fileNameWide)
{
- #if _MSC_VER >= 1400
- 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
+ throw OpenErr(StringNarrow(fileNameWide, false));
}
#endif
- else
- parameters.GetValue(Name::OutputStreamPointer(), m_stream);
+ if (fileName)
+ {
+ m_file->open(fileName, ios::out | ios::trunc | binary);
+ if (!*m_file)
+ throw OpenErr(fileName);
+ }
+ m_stream = m_file.get();
}
bool FileSink::IsolatedFlush(bool hardFlush, bool blocking)
diff --git a/misc.h b/misc.h
index e8b8013..ac1cbda 100644
--- a/misc.h
+++ b/misc.h
@@ -548,13 +548,20 @@ inline void SecureWipeArray(T *buf, size_t n)
}
// this function uses wcstombs(), which assumes that setlocale() has been called
-static std::string StringNarrow(const wchar_t *str)
+static std::string StringNarrow(const wchar_t *str, bool throwOnError = true)
{
#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);
+ if (size == -1)
+ {
+ if (throwOnError)
+ throw InvalidArgument("StringNarrow: wcstombs() call failed");
+ else
+ return std::string();
+ }
std::string result(size, 0);
wcstombs(&result[0], str, size);
return result;