From b85bccbbe61447d2bede9df50f8d5e8f2a04b9d9 Mon Sep 17 00:00:00 2001 From: thunder2 Date: Mon, 3 Dec 2012 15:58:44 +0000 Subject: [PATCH] Added function "ops_open" to openpgpsdk to open files with utf8 characters on Windows. git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@5934 b45a01b8-16f6-495d-af2f-9b41ad6348cc --- openpgpsdk/src/openpgpsdk.pro | 9 ++-- openpgpsdk/src/openpgpsdk/create.c | 7 +-- openpgpsdk/src/openpgpsdk/crypto.c | 5 ++- openpgpsdk/src/openpgpsdk/keyring.c | 3 +- openpgpsdk/src/openpgpsdk/readerwriter.c | 7 +-- openpgpsdk/src/openpgpsdk/signature.c | 3 +- openpgpsdk/src/util/opsdir.c | 22 ++++++++++ openpgpsdk/src/util/opsdir.h | 6 +++ openpgpsdk/src/util/opsstring.c | 55 ++++++++++++++++++++++++ openpgpsdk/src/util/opsstring.h | 17 ++++++++ 10 files changed, 121 insertions(+), 13 deletions(-) create mode 100644 openpgpsdk/src/util/opsdir.c create mode 100644 openpgpsdk/src/util/opsdir.h create mode 100644 openpgpsdk/src/util/opsstring.c create mode 100644 openpgpsdk/src/util/opsstring.h diff --git a/openpgpsdk/src/openpgpsdk.pro b/openpgpsdk/src/openpgpsdk.pro index 8575b8e21..536f808fd 100644 --- a/openpgpsdk/src/openpgpsdk.pro +++ b/openpgpsdk/src/openpgpsdk.pro @@ -67,9 +67,10 @@ HEADERS += openpgpsdk/writer.h \ openpgpsdk/accumulate.h \ openpgpsdk/armour.h \ openpgpsdk/parse_local.h \ - openpgpsdk/keyring_local.h + openpgpsdk/keyring_local.h \ + util/opsdir.h \ + util/opsstring.h - SOURCES += openpgpsdk/accumulate.c \ openpgpsdk/compress.c \ openpgpsdk/create.c \ @@ -106,4 +107,6 @@ SOURCES += openpgpsdk/accumulate.c \ openpgpsdk/writer_fd.c \ openpgpsdk/writer_memory.c \ openpgpsdk/writer_skey_checksum.c \ - openpgpsdk/writer_stream_encrypt_se_ip.c + openpgpsdk/writer_stream_encrypt_se_ip.c \ + util/opsdir.c \ + util/opsstring.c diff --git a/openpgpsdk/src/openpgpsdk/create.c b/openpgpsdk/src/openpgpsdk/create.c index 3b767d3bc..ed36067f5 100644 --- a/openpgpsdk/src/openpgpsdk/create.c +++ b/openpgpsdk/src/openpgpsdk/create.c @@ -40,6 +40,7 @@ #ifndef WIN32 #include #endif +#include #include #include @@ -1197,7 +1198,7 @@ ops_write_literal_data_from_file(const char *filename, ops_memory_t* mem=NULL; size_t len=0; - fd=open(filename,O_RDONLY | O_BINARY); + fd=ops_open(filename,O_RDONLY | O_BINARY, 0); if (fd < 0) return ops_false; @@ -1251,7 +1252,7 @@ ops_memory_t* ops_write_mem_from_file(const char *filename, int* errnum) *errnum=0; - fd=open(filename,O_RDONLY | O_BINARY); + fd=ops_open(filename,O_RDONLY | O_BINARY, 0); if (fd < 0) { @@ -1305,7 +1306,7 @@ int ops_write_file_from_buf(const char *filename, const char* buf, flags |= O_BINARY; - fd=open(filename,flags, 0600); + fd=ops_open(filename,flags, 0600); if (fd < 0) { perror(NULL); diff --git a/openpgpsdk/src/openpgpsdk/crypto.c b/openpgpsdk/src/openpgpsdk/crypto.c index 199122893..3218fc507 100644 --- a/openpgpsdk/src/openpgpsdk/crypto.c +++ b/openpgpsdk/src/openpgpsdk/crypto.c @@ -33,6 +33,7 @@ #include #include +#include /** \ingroup Core_MPI @@ -180,9 +181,9 @@ ops_boolean_t ops_encrypt_file(const char* input_filename, ops_create_info_t *cinfo; #ifdef WINDOWS_SYS - fd_in=open(input_filename, O_RDONLY | O_BINARY); + fd_in=ops_open(input_filename, O_RDONLY | O_BINARY); #else - fd_in=open(input_filename, O_RDONLY ); + fd_in=ops_open(input_filename, O_RDONLY, 0); #endif if(fd_in < 0) { diff --git a/openpgpsdk/src/openpgpsdk/keyring.c b/openpgpsdk/src/openpgpsdk/keyring.c index e98783b5e..ae705928d 100644 --- a/openpgpsdk/src/openpgpsdk/keyring.c +++ b/openpgpsdk/src/openpgpsdk/keyring.c @@ -46,6 +46,7 @@ #include #include +#include /** \ingroup HighLevel_Keyring @@ -738,7 +739,7 @@ ops_boolean_t ops_keyring_read_from_file(ops_keyring_t *keyring, const ops_boole // ops_parse_options(pinfo,OPS_PTAG_SS_ALL,OPS_PARSE_RAW); ops_parse_options(pinfo,OPS_PTAG_SS_ALL,OPS_PARSE_PARSED); - fd=open(filename,O_RDONLY | O_BINARY); + fd=ops_open(filename,O_RDONLY | O_BINARY, 0); if(fd < 0) { diff --git a/openpgpsdk/src/openpgpsdk/readerwriter.c b/openpgpsdk/src/openpgpsdk/readerwriter.c index f29341cc7..a5434a0e7 100644 --- a/openpgpsdk/src/openpgpsdk/readerwriter.c +++ b/openpgpsdk/src/openpgpsdk/readerwriter.c @@ -32,6 +32,7 @@ #include #include +#include #include "parse_local.h" @@ -183,7 +184,7 @@ int ops_setup_file_write(ops_create_info_t **cinfo, const char* filename, ops_bo flags |= O_EXCL; flags |= O_BINARY; - fd=open(filename, flags, 0600); + fd=ops_open(filename, flags, 0600); if(fd < 0) { perror(filename); @@ -221,7 +222,7 @@ int ops_setup_file_append(ops_create_info_t **cinfo, const char* filename) * initialise needed structures for writing to file */ - fd=open(filename,O_WRONLY | O_APPEND | O_BINARY | O_CREAT, 0600); + fd=ops_open(filename,O_WRONLY | O_APPEND | O_BINARY | O_CREAT, 0600); if(fd < 0) { @@ -267,7 +268,7 @@ int ops_setup_file_read(ops_parse_info_t **pinfo, const char *filename, * initialise needed structures for reading */ - fd=open(filename,O_RDONLY | O_BINARY); + fd=ops_open(filename,O_RDONLY | O_BINARY, 0); if (fd < 0) { diff --git a/openpgpsdk/src/openpgpsdk/signature.c b/openpgpsdk/src/openpgpsdk/signature.c index 1ec268762..875d3dde3 100644 --- a/openpgpsdk/src/openpgpsdk/signature.c +++ b/openpgpsdk/src/openpgpsdk/signature.c @@ -37,6 +37,7 @@ #include #include +#include #include @@ -1001,7 +1002,7 @@ ops_boolean_t ops_sign_file_as_cleartext(const char* input_filename, // open file to sign - fd_in=open(input_filename, O_RDONLY | O_BINARY); + fd_in=ops_open(input_filename, O_RDONLY | O_BINARY, 0); if(fd_in < 0) { diff --git a/openpgpsdk/src/util/opsdir.c b/openpgpsdk/src/util/opsdir.c new file mode 100644 index 000000000..df7ce76c2 --- /dev/null +++ b/openpgpsdk/src/util/opsdir.c @@ -0,0 +1,22 @@ +#include "opsdir.h" +#include "opsstring.h" + +#include + +int ops_open(const char* filename, int flag, int pmode) +{ +#ifdef WIN32 + wchar_t *wfilename = ConvertUtf8ToUtf16(filename); + if (!wfilename) + { + return -1; + } + + int result = _wopen(wfilename, flag, pmode); + free(wfilename); + + return result; +#else + return open(filename, flag, pmode); +#endif +} diff --git a/openpgpsdk/src/util/opsdir.h b/openpgpsdk/src/util/opsdir.h new file mode 100644 index 000000000..78f7ad8e8 --- /dev/null +++ b/openpgpsdk/src/util/opsdir.h @@ -0,0 +1,6 @@ +#ifndef OPUTIL_H +#define OPUTIL_H + +int ops_open(const char* filename, int flag, int pmode); + +#endif diff --git a/openpgpsdk/src/util/opsstring.c b/openpgpsdk/src/util/opsstring.c new file mode 100644 index 000000000..6a0e2b072 --- /dev/null +++ b/openpgpsdk/src/util/opsstring.c @@ -0,0 +1,55 @@ +#include "opsstring.h" + +#ifdef WIN32 +wchar_t *ConvertUtf8ToUtf16(const char* source) +{ + if (!source) { + return NULL; + } + +#ifdef WIN32 + int nbChars = MultiByteToWideChar(CP_UTF8, 0, source, -1, 0, 0); + if (nbChars == 0) { + return NULL; + } + + wchar_t* utf16Name = (wchar_t*) malloc(nbChars * sizeof(wchar_t)); + if (MultiByteToWideChar(CP_UTF8, 0, source, -1, utf16Name, nbChars) == 0) { + free(utf16Name); + return NULL; + } + + return utf16Name; +#else + // currently only for WIN32 + // convert code from rsstring.cc + return NULL; +#endif +} + +char* ConvertUtf16ToUtf8(const wchar_t *source) +{ + if (!source) { + return NULL; + } + +#ifdef WIN32 + int nbChars = WideCharToMultiByte(CP_UTF8, 0, source, -1, 0, 0, 0, 0); + if (nbChars == 0) { + return NULL; + } + + char* utf8Name = (char*) malloc(nbChars * sizeof(char)); + if (WideCharToMultiByte(CP_UTF8, 0, source, -1, utf8Name, nbChars, 0, 0) == 0) { + free(utf8Name); + return NULL; + } + + return utf8Name; +#else + // currently only for WIN32 + // convert code from rsstring.cc + return NULL; +#endif +} +#endif diff --git a/openpgpsdk/src/util/opsstring.h b/openpgpsdk/src/util/opsstring.h new file mode 100644 index 000000000..3bbaed494 --- /dev/null +++ b/openpgpsdk/src/util/opsstring.h @@ -0,0 +1,17 @@ +#ifndef OPSSTRING_H +#define OPSSTRING_H + +#ifdef WIN32 +#include +#endif + +#ifdef WIN32 +// currently only for WIN32 + +// Convert strings between UTF8 and UTF16 +// Don't forget to free the returned string. +wchar_t* ConvertUtf8ToUtf16(const char *source); +char* ConvertUtf16ToUtf8(const wchar_t* source); +#endif + +#endif // OPSSTRING_H