FILE * platform_fopen(const char *path, const char *mode) { #ifdef _WIN32 struct gc_arena gc = gc_new(); FILE *f = _wfopen(wide_string(path, &gc), wide_string(mode, &gc)); gc_free(&gc); return f; #else return fopen(path, mode); #endif }
wide_string CUnicodeUtils::StdGetUnicode(const std::string& multibyte) { int len = (int)multibyte.size(); if (len==0) return wide_string(); int size = len*4; wchar_t * wide = new wchar_t[size]; int ret = MultiByteToWideChar(CP_UTF8, 0, multibyte.c_str(), len, wide, size - 1); wide[ret] = 0; wide_string sRet = wide_string(wide); delete [] wide; return sRet; }
wide_string UTF8ToWide(const std::string& multibyte) { size_t length = multibyte.length(); if (length == 0) return wide_string(); wchar_t * wide = new wchar_t[length*2+2]; if (wide == NULL) return wide_string(); int ret = (int)MultiByteToWideChar(CP_UTF8, 0, multibyte.c_str(), (int)multibyte.size(), wide, (int)length*2 - 1); wide[ret] = 0; wide_string str = wide; delete[] wide; return str; }
/* * Attempt to simulate fork/execve on Windows */ int openvpn_execve (const struct argv *a, const struct env_set *es, const unsigned int flags) { int ret = -1; static bool exec_warn = false; if (a && a->argv[0]) { if (openvpn_execve_allowed (flags)) { struct gc_arena gc = gc_new (); STARTUPINFOW start_info; PROCESS_INFORMATION proc_info; char *env = env_block (es); WCHAR *cl = wide_cmd_line (a, &gc); WCHAR *cmd = wide_string (a->argv[0], &gc); /* this allows console programs to run, and is ignored otherwise */ DWORD proc_flags = CREATE_NO_WINDOW; CLEAR (start_info); CLEAR (proc_info); /* fill in STARTUPINFO struct */ GetStartupInfoW(&start_info); start_info.cb = sizeof(start_info); start_info.dwFlags = STARTF_USESHOWWINDOW; start_info.wShowWindow = SW_HIDE; if (CreateProcessW (cmd, cl, NULL, NULL, FALSE, proc_flags, env, NULL, &start_info, &proc_info)) { DWORD exit_status = 0; CloseHandle (proc_info.hThread); WaitForSingleObject (proc_info.hProcess, INFINITE); if (GetExitCodeProcess (proc_info.hProcess, &exit_status)) ret = (int)exit_status; else msg (M_WARN|M_ERRNO, "openvpn_execve: GetExitCodeProcess %S failed", cmd); CloseHandle (proc_info.hProcess); } else { msg (M_WARN|M_ERRNO, "openvpn_execve: CreateProcess %S failed", cmd); } free (env); gc_free (&gc); } else if (!exec_warn && (script_security < SSEC_SCRIPTS)) { msg (M_WARN, SCRIPT_SECURITY_WARNING); exec_warn = true; } } else { msg (M_WARN, "openvpn_execve: called with empty argv"); } return ret; }
static void speak_dataset_comments (const dataset *dset) { int i; ISpVoice *v = NULL; HRESULT hr; hr = CoInitialize(NULL); if (!SUCCEEDED(hr)) { fprintf(stderr, "CoInitialize failed\n"); return; } hr = CoCreateInstance(&CLSID_SpVoice, NULL, CLSCTX_ALL, &IID_ISpVoice, (void *) &v); if (SUCCEEDED(hr)) { for (i=0; i<N_COMMENTS; i++) { if (dset->comments[i] != NULL) { WCHAR *w = wide_string(dset->comments[i]); ISpVoice_Speak(v, w, 0, NULL); free(w); } } ISpVoice_Release(v); } CoUninitialize(); }
static int speak_line (const char *line) { static ISpVoice *v = NULL; if (line == NULL) { if (v != NULL) { release_sapi_voice(v); } return 0; } if (v == NULL) { v = get_sapi_voice(); } if (v != NULL) { WCHAR *w = wide_string(line); ISpVoice_Speak(v, w, 0, NULL); free(w); } else { return 1; } return 0; }
static int speak_buffer (const char *buf, int (*should_stop)()) { ISpVoice *v = NULL; WCHAR *w; char line[2048]; v = get_sapi_voice(); if (v == NULL) return 1; bufgets_init(buf); while (bufgets(line, sizeof line, buf)) { if (should_stop()) { ISpVoice_Speak(v, L"OK, stopping", 0, NULL); break; } tailstrip(line); w = wide_string(line); ISpVoice_Speak(v, w, 0, NULL); free(w); } bufgets_finalize(buf); release_sapi_voice(v); return 0; }
std::string get_computer_name() { TCHAR name[512]; DWORD len = 512; GetComputerName(name, &len); std::wstring wide_string(&name[0]); //TODO figure this out return std::string(wide_string.begin(), wide_string.end()); };
int platform_access(const char *path, int mode) { #ifdef _WIN32 struct gc_arena gc = gc_new(); int ret = _waccess(wide_string(path, &gc), mode & ~X_OK); gc_free(&gc); return ret; #else return access(path, mode); #endif }
int platform_stat(const char *path, platform_stat_t *buf) { #ifdef _WIN32 struct gc_arena gc = gc_new(); int res = _wstat(wide_string(path, &gc), buf); gc_free(&gc); return res; #else return stat(path, buf); #endif }
int platform_open(const char *path, int flags, int mode) { #ifdef _WIN32 struct gc_arena gc = gc_new(); int fd = _wopen(wide_string(path, &gc), flags, mode); gc_free(&gc); return fd; #else return open(path, flags, mode); #endif }
static WCHAR * wide_cmd_line(const struct argv *a, struct gc_arena *gc) { size_t nchars = 1; size_t maxlen = 0; size_t i; struct buffer buf; char *work = NULL; if (!a) { return NULL; } for (i = 0; i < a->argc; ++i) { const char *arg = a->argv[i]; const size_t len = strlen(arg); nchars += len + 3; if (len > maxlen) { maxlen = len; } } work = gc_malloc(maxlen + 1, false, gc); check_malloc_return(work); buf = alloc_buf_gc(nchars, gc); for (i = 0; i < a->argc; ++i) { const char *arg = a->argv[i]; strcpy(work, arg); string_mod(work, CC_PRINT, CC_DOUBLE_QUOTE|CC_CRLF, '_'); if (i) { buf_printf(&buf, " "); } if (string_class(work, CC_ANY, CC_SPACE)) { buf_printf(&buf, "%s", work); } else { buf_printf(&buf, "\"%s\"", work); } } return wide_string(BSTR(&buf), gc); }
/* delete a file, return true if succeeded */ bool platform_unlink(const char *filename) { #if defined(_WIN32) struct gc_arena gc = gc_new(); BOOL ret = DeleteFileW(wide_string(filename, &gc)); gc_free(&gc); return (ret != 0); #elif defined(HAVE_UNLINK) return (unlink(filename) == 0); #else /* if defined(_WIN32) */ return false; #endif }
/* * Wrapper for chdir library function */ int platform_chdir(const char *dir) { #ifdef HAVE_CHDIR #ifdef _WIN32 int res; struct gc_arena gc = gc_new(); res = _wchdir(wide_string(dir, &gc)); gc_free(&gc); return res; #else /* ifdef _WIN32 */ return chdir(dir); #endif #else /* ifdef HAVE_CHDIR */ return -1; #endif }
void ttext_::copy_selection(const bool mouse) { int length = selection_length_; unsigned start = selection_start_; if(length == 0) { return; } if(length < 0) { length = - length; start -= length; } const wide_string& wtext = utils::string_to_wstring(text_.text()); const std::string& text = utils::wstring_to_string( wide_string(wtext.begin() + start, wtext.begin() + start + length)); copy_to_clipboard(text, mouse); }
int main() { // \u263A is "Unicode WHITE SMILING FACE = have a nice day!" std::string narrow_string ("smile2"); std::wstring wide_string (L"smile2\u263A"); std::list<char> narrow_list; narrow_list.push_back('s'); narrow_list.push_back('m'); narrow_list.push_back('i'); narrow_list.push_back('l'); narrow_list.push_back('e'); narrow_list.push_back('3'); std::list<wchar_t> wide_list; wide_list.push_back(L's'); wide_list.push_back(L'm'); wide_list.push_back(L'i'); wide_list.push_back(L'l'); wide_list.push_back(L'e'); wide_list.push_back(L'3'); wide_list.push_back(L'\u263A'); { fs::ofstream f("smile"); } { fs::ofstream f(L"smile\u263A"); } { fs::ofstream f(narrow_string); } { fs::ofstream f(wide_string); } { fs::ofstream f(narrow_list); } { fs::ofstream f(wide_list); } narrow_list.pop_back(); narrow_list.push_back('4'); wide_list.pop_back(); wide_list.pop_back(); wide_list.push_back(L'4'); wide_list.push_back(L'\u263A'); { fs::ofstream f(fs::path(narrow_list.begin(), narrow_list.end())); } { fs::ofstream f(fs::path(wide_list.begin(), wide_list.end())); } return 0; }
static void audio_graph_error (const char *msg) { #ifdef HAVE_FLITE cst_voice *v; #endif #ifdef WIN32_SAPI ISpVoice *v = NULL; HRESULT hr; #endif fprintf(stderr, "%s\n", msg); #ifdef HAVE_FLITE flite_init(); v = register_cmu_us_kal(); flite_text_to_speech(msg, v, "play"); #endif #ifdef WIN32_SAPI hr = CoInitialize(NULL); if (!SUCCEEDED(hr)) return; hr = CoCreateInstance(&CLSID_SpVoice, NULL, CLSCTX_ALL, &IID_ISpVoice, (void *) &v); if (SUCCEEDED(hr)) { wchar_t *w = wide_string(msg); ISpVoice_Speak(v, w, 0, NULL); free(w); ISpVoice_Release(v); } #endif }
void textbox::handle_event(const SDL_Event& event) { scrollarea::handle_event(event); if(hidden()) return; bool changed = false; const int old_selstart = selstart_; const int old_selend = selend_; //Sanity check: verify that selection start and end are within text //boundaries if(is_selection() && !(size_t(selstart_) <= text_.size() && size_t(selend_) <= text_.size())) { WRN_DP << "out-of-boundary selection\n"; selstart_ = selend_ = -1; } int mousex, mousey; const Uint8 mousebuttons = SDL_GetMouseState(&mousex,&mousey); if(!(mousebuttons & SDL_BUTTON(1))) { grabmouse_ = false; } SDL_Rect const &loc = inner_location(); bool clicked_inside = !mouse_locked() && (event.type == SDL_MOUSEBUTTONDOWN && (mousebuttons & SDL_BUTTON(1)) && point_in_rect(mousex, mousey, loc)); if(clicked_inside) { set_focus(true); } if ((grabmouse_ && (!mouse_locked() && event.type == SDL_MOUSEMOTION)) || clicked_inside) { const int x = mousex - loc.x + text_pos_; const int y = mousey - loc.y; int pos = 0; int distance = x; for(unsigned int i = 1; i < char_x_.size(); ++i) { if(static_cast<int>(yscroll_) + y < char_y_[i]) { break; } // Check individually each distance (if, one day, we support // RTL languages, char_x_[c] may not be monotonous.) if(abs(x - char_x_[i]) < distance && yscroll_ + y < char_y_[i] + line_height_) { pos = i; distance = abs(x - char_x_[i]); } } cursor_ = pos; if(grabmouse_) selend_ = cursor_; update_text_cache(false); if(!grabmouse_ && mousebuttons & SDL_BUTTON(1)) { grabmouse_ = true; selstart_ = selend_ = cursor_; } else if (! (mousebuttons & SDL_BUTTON(1))) { grabmouse_ = false; } set_dirty(); } if(editable_ == false) { return; } //if we don't have the focus, then see if we gain the focus, //otherwise return if(focus(&event) == false) { if (!mouse_locked() && event.type == SDL_MOUSEMOTION && point_in_rect(mousex, mousey, loc)) events::focus_handler(this); return; } if(event.type != SDL_KEYDOWN || focus(&event) != true) { draw(); return; } const SDL_keysym& key = reinterpret_cast<const SDL_KeyboardEvent&>(event).keysym; const SDLMod modifiers = SDL_GetModState(); const int c = key.sym; const int old_cursor = cursor_; if(c == SDLK_LEFT && cursor_ > 0) --cursor_; if(c == SDLK_RIGHT && cursor_ < static_cast<int>(text_.size())) ++cursor_; // ctrl-a, ctrl-e and ctrl-u are readline style shortcuts, even on Macs if(c == SDLK_END || (c == SDLK_e && (modifiers & KMOD_CTRL))) cursor_ = text_.size(); if(c == SDLK_HOME || (c == SDLK_a && (modifiers & KMOD_CTRL))) cursor_ = 0; if((old_cursor != cursor_) && (modifiers & KMOD_SHIFT)) { if(selstart_ == -1) selstart_ = old_cursor; selend_ = cursor_; } if(c == SDLK_BACKSPACE) { changed = true; if(is_selection()) { erase_selection(); } else if(cursor_ > 0) { --cursor_; text_.erase(text_.begin()+cursor_); } } if(c == SDLK_u && (modifiers & KMOD_CTRL)) { // clear line changed = true; cursor_ = 0; text_.resize(0); } if(c == SDLK_DELETE && !text_.empty()) { changed = true; if(is_selection()) { erase_selection(); } else { if(cursor_ < static_cast<int>(text_.size())) { text_.erase(text_.begin()+cursor_); } } } wchar_t character = key.unicode; //movement characters may have a "Unicode" field on some platforms, so ignore it. if(!(c == SDLK_UP || c == SDLK_DOWN || c == SDLK_LEFT || c == SDLK_RIGHT || c == SDLK_DELETE || c == SDLK_BACKSPACE || c == SDLK_END || c == SDLK_HOME || c == SDLK_PAGEUP || c == SDLK_PAGEDOWN)) { if(character != 0) { DBG_G << "Char: " << character << ", c = " << c << "\n"; } if(event.key.keysym.mod & copypaste_modifier) { switch(c) { case SDLK_v: // paste { changed = true; if(is_selection()) erase_selection(); std::string str = copy_from_clipboard(false); //cut off anything after the first newline str.erase(std::find_if(str.begin(),str.end(),utils::isnewline),str.end()); wide_string s = utils::string_to_wstring(str); if(text_.size() < max_size_) { if(s.size() + text_.size() > max_size_) { s.resize(max_size_ - text_.size()); } text_.insert(text_.begin()+cursor_, s.begin(), s.end()); cursor_ += s.size(); } } break; case SDLK_c: // copy { const size_t beg = std::min<size_t>(size_t(selstart_),size_t(selend_)); const size_t end = std::max<size_t>(size_t(selstart_),size_t(selend_)); wide_string ws = wide_string(text_.begin() + beg, text_.begin() + end); std::string s = utils::wstring_to_string(ws); copy_to_clipboard(s, false); } break; } } else { if(character >= 32 && character != 127) { changed = true; if(is_selection()) erase_selection(); if(text_.size() + 1 <= max_size_) { text_.insert(text_.begin()+cursor_,character); ++cursor_; } } } } if(is_selection() && (selend_ != cursor_)) selstart_ = selend_ = -1; //since there has been cursor activity, make the cursor appear for //at least the next 500ms. show_cursor_ = true; show_cursor_at_ = SDL_GetTicks(); if(changed || old_cursor != cursor_ || old_selstart != selstart_ || old_selend != selend_) { text_image_ = NULL; handle_text_changed(text_); } set_dirty(true); }
static void plugin_init_item(struct plugin *p, const struct plugin_option *o) { struct gc_arena gc = gc_new(); bool rel = false; p->so_pathname = o->so_pathname; p->plugin_type_mask = plugin_supported_types(); #ifndef _WIN32 p->handle = NULL; /* If the plug-in filename is not an absolute path, * or beginning with '.', it should use the PLUGIN_LIBDIR * as the base directory for loading the plug-in. * * This means the following scenarios are loaded from these places: * --plugin fancyplug.so -> $PLUGIN_LIBDIR/fancyplug.so * --plugin my/fancyplug.so -> $PLUGIN_LIBDIR/my/fancyplug.so * --plugin ./fancyplug.so -> $CWD/fancyplug.so * --plugin /usr/lib/my/fancyplug.so -> /usr/lib/my/fancyplug.so * * Please note that $CWD means the directory OpenVPN is either started from * or the directory OpenVPN have changed into using --cd before --plugin * was parsed. * */ if (!platform_absolute_pathname(p->so_pathname) && p->so_pathname[0] != '.') { char full[PATH_MAX]; openvpn_snprintf(full, sizeof(full), "%s/%s", PLUGIN_LIBDIR, p->so_pathname); p->handle = dlopen(full, RTLD_NOW); } else { rel = !platform_absolute_pathname(p->so_pathname); p->handle = dlopen(p->so_pathname, RTLD_NOW); } if (!p->handle) { msg(M_ERR, "PLUGIN_INIT: could not load plugin shared object %s: %s", p->so_pathname, dlerror()); } #define PLUGIN_SYM(var, name, flags) libdl_resolve_symbol(p->handle, (void *)&p->var, name, p->so_pathname, flags) #else /* ifndef _WIN32 */ rel = !platform_absolute_pathname(p->so_pathname); p->module = LoadLibraryW(wide_string(p->so_pathname, &gc)); if (!p->module) { msg(M_ERR, "PLUGIN_INIT: could not load plugin DLL: %s", p->so_pathname); } #define PLUGIN_SYM(var, name, flags) dll_resolve_symbol(p->module, (void *)&p->var, name, p->so_pathname, flags) #endif /* ifndef _WIN32 */ PLUGIN_SYM(open1, "openvpn_plugin_open_v1", 0); PLUGIN_SYM(open2, "openvpn_plugin_open_v2", 0); PLUGIN_SYM(open3, "openvpn_plugin_open_v3", 0); PLUGIN_SYM(func1, "openvpn_plugin_func_v1", 0); PLUGIN_SYM(func2, "openvpn_plugin_func_v2", 0); PLUGIN_SYM(func3, "openvpn_plugin_func_v3", 0); PLUGIN_SYM(close, "openvpn_plugin_close_v1", PLUGIN_SYMBOL_REQUIRED); PLUGIN_SYM(abort, "openvpn_plugin_abort_v1", 0); PLUGIN_SYM(client_constructor, "openvpn_plugin_client_constructor_v1", 0); PLUGIN_SYM(client_destructor, "openvpn_plugin_client_destructor_v1", 0); PLUGIN_SYM(min_version_required, "openvpn_plugin_min_version_required_v1", 0); PLUGIN_SYM(initialization_point, "openvpn_plugin_select_initialization_point_v1", 0); if (!p->open1 && !p->open2 && !p->open3) { msg(M_FATAL, "PLUGIN: symbol openvpn_plugin_open_vX is undefined in plugin: %s", p->so_pathname); } if (!p->func1 && !p->func2 && !p->func3) { msg(M_FATAL, "PLUGIN: symbol openvpn_plugin_func_vX is undefined in plugin: %s", p->so_pathname); } /* * Verify that we are sufficiently up-to-date to handle the plugin */ if (p->min_version_required) { const int plugin_needs_version = (*p->min_version_required)(); if (plugin_needs_version > OPENVPN_PLUGIN_VERSION) { msg(M_FATAL, "PLUGIN_INIT: plugin needs interface version %d, but this version of OpenVPN only supports version %d: %s", plugin_needs_version, OPENVPN_PLUGIN_VERSION, p->so_pathname); } } if (p->initialization_point) { p->requested_initialization_point = (*p->initialization_point)(); } else { p->requested_initialization_point = OPENVPN_PLUGIN_INIT_PRE_DAEMON; } if (rel) { msg(M_WARN, "WARNING: plugin '%s' specified by a relative pathname -- using an absolute pathname would be more secure", p->so_pathname); } p->initialized = true; gc_free(&gc); }
void redirect_stdout_stderr (const char *file, bool append) { #if defined(WIN32) if (!std_redir) { struct gc_arena gc = gc_new (); HANDLE log_handle; int log_fd; SECURITY_ATTRIBUTES saAttr; saAttr.nLength = sizeof(SECURITY_ATTRIBUTES); saAttr.bInheritHandle = TRUE; saAttr.lpSecurityDescriptor = NULL; log_handle = CreateFileW (wide_string (file, &gc), GENERIC_WRITE, FILE_SHARE_READ, &saAttr, append ? OPEN_ALWAYS : CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); gc_free (&gc); if (log_handle == INVALID_HANDLE_VALUE) { msg (M_WARN|M_ERRNO, "Warning: cannot open --log file: %s", file); return; } /* append to logfile? */ if (append) { if (SetFilePointer (log_handle, 0, NULL, FILE_END) == INVALID_SET_FILE_POINTER) msg (M_ERR, "Error: cannot seek to end of --log file: %s", file); } /* save original stderr for password prompts */ orig_stderr = GetStdHandle (STD_ERROR_HANDLE); #if 0 /* seems not be necessary with stdout/stderr redirection below*/ /* set up for redirection */ if (!SetStdHandle (STD_OUTPUT_HANDLE, log_handle) || !SetStdHandle (STD_ERROR_HANDLE, log_handle)) msg (M_ERR, "Error: cannot redirect stdout/stderr to --log file: %s", file); #endif /* direct stdout/stderr to point to log_handle */ log_fd = _open_osfhandle ((intptr_t)log_handle, _O_TEXT); if (log_fd == -1) msg (M_ERR, "Error: --log redirect failed due to _open_osfhandle failure"); /* open log_handle as FILE stream */ ASSERT (msgfp == NULL); msgfp = _fdopen (log_fd, "wt"); if (msgfp == NULL) msg (M_ERR, "Error: --log redirect failed due to _fdopen"); /* redirect C-library stdout/stderr to log file */ if (_dup2 (log_fd, 1) == -1 || _dup2 (log_fd, 2) == -1) msg (M_WARN, "Error: --log redirect of stdout/stderr failed"); std_redir = true; } #elif defined(HAVE_DUP2) if (!std_redir) { int out = open (file, O_CREAT | O_WRONLY | (append ? O_APPEND : O_TRUNC), S_IRUSR | S_IWUSR); if (out < 0) { msg (M_WARN|M_ERRNO, "Warning: Error redirecting stdout/stderr to --log file: %s", file); return; } if (dup2 (out, 1) == -1) msg (M_ERR, "--log file redirection error on stdout"); if (dup2 (out, 2) == -1) msg (M_ERR, "--log file redirection error on stderr"); if (out > 2) close (out); std_redir = true; } #else msg (M_WARN, "WARNING: The --log option is not supported on this OS because it lacks the dup2 function"); #endif }
TEST(fxcrt, WideStringOperatorEQ) { CFX_WideString null_string; EXPECT_TRUE(null_string == null_string); CFX_WideString empty_string(L""); EXPECT_TRUE(empty_string == empty_string); EXPECT_TRUE(empty_string == null_string); EXPECT_TRUE(null_string == empty_string); CFX_WideString deleted_string(L"hello"); deleted_string.Delete(0, 5); EXPECT_TRUE(deleted_string == deleted_string); EXPECT_TRUE(deleted_string == null_string); EXPECT_TRUE(deleted_string == empty_string); EXPECT_TRUE(null_string == deleted_string); EXPECT_TRUE(null_string == empty_string); CFX_WideString wide_string(L"hello"); EXPECT_TRUE(wide_string == wide_string); EXPECT_FALSE(wide_string == null_string); EXPECT_FALSE(wide_string == empty_string); EXPECT_FALSE(wide_string == deleted_string); EXPECT_FALSE(null_string == wide_string); EXPECT_FALSE(empty_string == wide_string); EXPECT_FALSE(deleted_string == wide_string); CFX_WideString wide_string_same1(L"hello"); EXPECT_TRUE(wide_string == wide_string_same1); EXPECT_TRUE(wide_string_same1 == wide_string); CFX_WideString wide_string_same2(wide_string); EXPECT_TRUE(wide_string == wide_string_same2); EXPECT_TRUE(wide_string_same2 == wide_string); CFX_WideString wide_string1(L"he"); CFX_WideString wide_string2(L"hellp"); CFX_WideString wide_string3(L"hellod"); EXPECT_FALSE(wide_string == wide_string1); EXPECT_FALSE(wide_string == wide_string2); EXPECT_FALSE(wide_string == wide_string3); EXPECT_FALSE(wide_string1 == wide_string); EXPECT_FALSE(wide_string2 == wide_string); EXPECT_FALSE(wide_string3 == wide_string); CFX_WideStringC null_string_c; CFX_WideStringC empty_string_c(L""); EXPECT_TRUE(null_string == null_string_c); EXPECT_TRUE(null_string == empty_string_c); EXPECT_TRUE(empty_string == null_string_c); EXPECT_TRUE(empty_string == empty_string_c); EXPECT_TRUE(deleted_string == null_string_c); EXPECT_TRUE(deleted_string == empty_string_c); EXPECT_TRUE(null_string_c == null_string); EXPECT_TRUE(empty_string_c == null_string); EXPECT_TRUE(null_string_c == empty_string); EXPECT_TRUE(empty_string_c == empty_string); EXPECT_TRUE(null_string_c == deleted_string); EXPECT_TRUE(empty_string_c == deleted_string); CFX_WideStringC wide_string_c_same1(L"hello"); EXPECT_TRUE(wide_string == wide_string_c_same1); EXPECT_TRUE(wide_string_c_same1 == wide_string); CFX_WideStringC wide_string_c1(L"he"); CFX_WideStringC wide_string_c2(L"hellp"); CFX_WideStringC wide_string_c3(L"hellod"); EXPECT_FALSE(wide_string == wide_string_c1); EXPECT_FALSE(wide_string == wide_string_c2); EXPECT_FALSE(wide_string == wide_string_c3); EXPECT_FALSE(wide_string_c1 == wide_string); EXPECT_FALSE(wide_string_c2 == wide_string); EXPECT_FALSE(wide_string_c3 == wide_string); const wchar_t* c_null_string = nullptr; const wchar_t* c_empty_string = L""; EXPECT_TRUE(null_string == c_null_string); EXPECT_TRUE(null_string == c_empty_string); EXPECT_TRUE(empty_string == c_null_string); EXPECT_TRUE(empty_string == c_empty_string); EXPECT_TRUE(deleted_string == c_null_string); EXPECT_TRUE(deleted_string == c_empty_string); EXPECT_TRUE(c_null_string == null_string); EXPECT_TRUE(c_empty_string == null_string); EXPECT_TRUE(c_null_string == empty_string); EXPECT_TRUE(c_empty_string == empty_string); EXPECT_TRUE(c_null_string == deleted_string); EXPECT_TRUE(c_empty_string == deleted_string); const wchar_t* c_string_same1 = L"hello"; EXPECT_TRUE(wide_string == c_string_same1); EXPECT_TRUE(c_string_same1 == wide_string); const wchar_t* c_string1 = L"he"; const wchar_t* c_string2 = L"hellp"; const wchar_t* c_string3 = L"hellod"; EXPECT_FALSE(wide_string == c_string1); EXPECT_FALSE(wide_string == c_string2); EXPECT_FALSE(wide_string == c_string3); EXPECT_FALSE(c_string1 == wide_string); EXPECT_FALSE(c_string2 == wide_string); EXPECT_FALSE(c_string3 == wide_string); }
static void plugin_init_item (struct plugin *p, const struct plugin_option *o) { struct gc_arena gc = gc_new (); bool rel = false; p->so_pathname = o->so_pathname; p->plugin_type_mask = plugin_supported_types (); #ifndef WIN32 p->handle = NULL; #if defined(PLUGIN_LIBDIR) if (!absolute_pathname (p->so_pathname)) { char full[PATH_MAX]; openvpn_snprintf (full, sizeof(full), "%s/%s", PLUGIN_LIBDIR, p->so_pathname); p->handle = dlopen (full, RTLD_NOW); #if defined(ENABLE_PLUGIN_SEARCH) if (!p->handle) { rel = true; p->handle = dlopen (p->so_pathname, RTLD_NOW); } #endif } else #endif { rel = !absolute_pathname (p->so_pathname); p->handle = dlopen (p->so_pathname, RTLD_NOW); } if (!p->handle) msg (M_ERR, "PLUGIN_INIT: could not load plugin shared object %s: %s", p->so_pathname, dlerror()); # define PLUGIN_SYM(var, name, flags) libdl_resolve_symbol (p->handle, (void*)&p->var, name, p->so_pathname, flags) #else rel = !absolute_pathname (p->so_pathname); p->module = LoadLibraryW (wide_string (p->so_pathname, &gc)); if (!p->module) msg (M_ERR, "PLUGIN_INIT: could not load plugin DLL: %s", p->so_pathname); # define PLUGIN_SYM(var, name, flags) dll_resolve_symbol (p->module, (void*)&p->var, name, p->so_pathname, flags) #endif PLUGIN_SYM (open1, "openvpn_plugin_open_v1", 0); PLUGIN_SYM (open2, "openvpn_plugin_open_v2", 0); PLUGIN_SYM (open3, "openvpn_plugin_open_v3", 0); PLUGIN_SYM (func1, "openvpn_plugin_func_v1", 0); PLUGIN_SYM (func2, "openvpn_plugin_func_v2", 0); PLUGIN_SYM (func3, "openvpn_plugin_func_v3", 0); PLUGIN_SYM (close, "openvpn_plugin_close_v1", PLUGIN_SYMBOL_REQUIRED); PLUGIN_SYM (abort, "openvpn_plugin_abort_v1", 0); PLUGIN_SYM (client_constructor, "openvpn_plugin_client_constructor_v1", 0); PLUGIN_SYM (client_destructor, "openvpn_plugin_client_destructor_v1", 0); PLUGIN_SYM (min_version_required, "openvpn_plugin_min_version_required_v1", 0); PLUGIN_SYM (initialization_point, "openvpn_plugin_select_initialization_point_v1", 0); if (!p->open1 && !p->open2 && !p->open3) msg (M_FATAL, "PLUGIN: symbol openvpn_plugin_open_vX is undefined in plugin: %s", p->so_pathname); if (!p->func1 && !p->func2 && !p->func3) msg (M_FATAL, "PLUGIN: symbol openvpn_plugin_func_vX is undefined in plugin: %s", p->so_pathname); /* * Verify that we are sufficiently up-to-date to handle the plugin */ if (p->min_version_required) { const int plugin_needs_version = (*p->min_version_required)(); if (plugin_needs_version > OPENVPN_PLUGIN_VERSION) msg (M_FATAL, "PLUGIN_INIT: plugin needs interface version %d, but this version of OpenVPN only supports version %d: %s", plugin_needs_version, OPENVPN_PLUGIN_VERSION, p->so_pathname); } if (p->initialization_point) p->requested_initialization_point = (*p->initialization_point)(); else p->requested_initialization_point = OPENVPN_PLUGIN_INIT_PRE_DAEMON; if (rel) msg (M_WARN, "WARNING: plugin '%s' specified by a relative pathname -- using an absolute pathname would be more secure", p->so_pathname); p->initialized = true; gc_free (&gc); }
bool ttext::insert_unicode(const unsigned offset, const wchar_t unicode) { return (insert_unicode(offset, wide_string(1, unicode)) == 1); }
static wide_string markov_generate_name(const markov_prefix_map& prefixes, size_t chain_size, size_t max_len, rand_rng::simple_rng* rng) { if(chain_size == 0) return wide_string(); wide_string prefix, res; // Since this function is called in the name description in a MP game it // uses the local locale. The locale between players can be different and // thus the markov_prefix_map can be different. This resulted in // get_random() getting called a different number of times for the // generation in different locales (due to the bail out at 'if(c == 0)'). // // This causes a problem since the random state is no longer in sync. The // following calls to get_random() return different results, which caused // traits to be different. To avoid that problem we call get_random() // the maximum number of times and store the result in a lookup table. std::vector<int> random(max_len); size_t j = 0; for(; j < max_len; ++j) { random[j] = rng ? rng->get_next_random() : get_random_nocheck(); } j = 0; while(res.size() < max_len) { const markov_prefix_map::const_iterator i = prefixes.find(prefix); if(i == prefixes.end() || i->second.empty()) { return res; } const wchar_t c = i->second[random[j++]%i->second.size()]; if(c == 0) { return res; } res.resize(res.size()+1); res[res.size()-1] = c; prefix.resize(prefix.size()+1); prefix[prefix.size()-1] = c; while(prefix.size() > chain_size) { prefix.erase(prefix.begin()); } } // Getting here means that the maximum length was reached when // generating the name, hence the ending of the name has to be // made valid. Otherwise weird names like Unárierini- and // Thramboril-G may occur. // Strip characters from the end until the last prefix of the // name has end-of-string as a possible next character in the // markov prefix map. If no valid ending is found, use the // originally generated name. wide_string originalRes = res; int prefixLen; while(!res.empty()) { prefixLen = chain_size < res.size() ? chain_size : res.size(); prefix = wide_string(res.end() - prefixLen, res.end()); const markov_prefix_map::const_iterator i = prefixes.find(prefix); if (i == prefixes.end() || i->second.empty()) { return res; } if (std::find(i->second.begin(), i->second.end(), static_cast<wchar_t>(0)) != i->second.end()) { // This ending is valid. return res; } // The current ending is invalid, remove the last character // and retry. res.pop_back(); } // No valid ending at all could be found. This generally should // not happen, unless the chain length is very long or the // maximum length is very small. Return the originally generated // name, it's not much we can do about it. return originalRes; }