improve Unicode filename handling
parent
92718c4754
commit
80aaec3131
104
files.cpp
104
files.cpp
|
|
@ -27,43 +27,39 @@ void FileStore::StoreInitialize(const NameValuePairs ¶meters)
|
||||||
m_stream = NULL;
|
m_stream = NULL;
|
||||||
m_file.release();
|
m_file.release();
|
||||||
|
|
||||||
const char *fileName;
|
const char *fileName = NULL;
|
||||||
const wchar_t *fileNameWide;
|
#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;
|
||||||
|
}
|
||||||
|
|
||||||
|
ios::openmode binary = parameters.GetValueWithDefault(Name::InputBinaryMode(), true) ? ios::binary : ios::openmode(0);
|
||||||
|
m_file.reset(new std::ifstream);
|
||||||
#ifdef CRYPTOPP_UNIX_AVAILABLE
|
#ifdef CRYPTOPP_UNIX_AVAILABLE
|
||||||
std::string narrowed;
|
std::string narrowed;
|
||||||
|
if (fileNameWide)
|
||||||
|
fileName = (narrowed = StringNarrow(fileNameWide)).c_str();
|
||||||
#endif
|
#endif
|
||||||
|
#if _MSC_VER >= 1400
|
||||||
if (parameters.GetValue(Name::InputFileName(), fileName))
|
if (fileNameWide)
|
||||||
{
|
{
|
||||||
#ifdef CRYPTOPP_UNIX_AVAILABLE
|
m_file->open(fileNameWide, ios::in | binary);
|
||||||
narrowName:
|
if (!*m_file)
|
||||||
|
throw OpenErr(StringNarrow(fileNameWide, false));
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
ios::openmode binary = parameters.GetValueWithDefault(Name::InputBinaryMode(), true) ? ios::binary : ios::openmode(0);
|
if (fileName)
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
#if defined(CRYPTOPP_UNIX_AVAILABLE) || _MSC_VER >= 1400
|
m_stream = m_file.get();
|
||||||
else if (parameters.GetValue(Name::InputFileNameWide(), 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
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
else
|
|
||||||
parameters.GetValue(Name::InputStreamPointer(), m_stream);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
lword FileStore::MaxRetrievable() const
|
lword FileStore::MaxRetrievable() const
|
||||||
|
|
@ -187,43 +183,39 @@ void FileSink::IsolatedInitialize(const NameValuePairs ¶meters)
|
||||||
m_stream = NULL;
|
m_stream = NULL;
|
||||||
m_file.release();
|
m_file.release();
|
||||||
|
|
||||||
const char *fileName;
|
const char *fileName = NULL;
|
||||||
const wchar_t *fileNameWide;
|
#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;
|
||||||
|
}
|
||||||
|
|
||||||
|
ios::openmode binary = parameters.GetValueWithDefault(Name::OutputBinaryMode(), true) ? ios::binary : ios::openmode(0);
|
||||||
|
m_file.reset(new std::ofstream);
|
||||||
#ifdef CRYPTOPP_UNIX_AVAILABLE
|
#ifdef CRYPTOPP_UNIX_AVAILABLE
|
||||||
std::string narrowed;
|
std::string narrowed;
|
||||||
|
if (fileNameWide)
|
||||||
|
fileName = (narrowed = StringNarrow(fileNameWide)).c_str();
|
||||||
#endif
|
#endif
|
||||||
|
#if _MSC_VER >= 1400
|
||||||
if (parameters.GetValue(Name::OutputFileName(), fileName))
|
if (fileNameWide)
|
||||||
{
|
{
|
||||||
#ifdef CRYPTOPP_UNIX_AVAILABLE
|
m_file->open(fileNameWide, ios::out | ios::trunc | binary);
|
||||||
narrowName:
|
if (!*m_file)
|
||||||
|
throw OpenErr(StringNarrow(fileNameWide, false));
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
ios::openmode binary = parameters.GetValueWithDefault(Name::OutputBinaryMode(), true) ? ios::binary : ios::openmode(0);
|
if (fileName)
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
#if defined(CRYPTOPP_UNIX_AVAILABLE) || _MSC_VER >= 1400
|
m_stream = m_file.get();
|
||||||
else if (parameters.GetValue(Name::OutputFileNameWide(), 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
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
else
|
|
||||||
parameters.GetValue(Name::OutputStreamPointer(), m_stream);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool FileSink::IsolatedFlush(bool hardFlush, bool blocking)
|
bool FileSink::IsolatedFlush(bool hardFlush, bool blocking)
|
||||||
|
|
|
||||||
9
misc.h
9
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
|
// 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
|
#ifdef _MSC_VER
|
||||||
#pragma warning(push)
|
#pragma warning(push)
|
||||||
#pragma warning(disable: 4996) // 'wcstombs': This function or variable may be unsafe.
|
#pragma warning(disable: 4996) // 'wcstombs': This function or variable may be unsafe.
|
||||||
#endif
|
#endif
|
||||||
size_t size = wcstombs(NULL, str, 0);
|
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);
|
std::string result(size, 0);
|
||||||
wcstombs(&result[0], str, size);
|
wcstombs(&result[0], str, size);
|
||||||
return result;
|
return result;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue