/* Just like strncasecmp, but compare two UTF8 strings. Limited to 4096 chars. */ int win_utf8ncasecmp(const char *s1, const char *s2, size_t n) { wchar_t ws1[4096], ws2[4096]; int converted; /* Make sure input is valid UTF-8 */ if (!isLegalUTF8String(s1)) { logmsg(LOG_CRIT, "win_utf8ncasecmp: Illegal UTF-8 string:%s", s1); return -1; } if (!isLegalUTF8String(s2)) { logmsg(LOG_CRIT, "win_utf8ncasecmp: Illegal UTF-8 string:%s", s2); return -1; } /* Convert both strings to wide chars */ converted = MultiByteToWideChar(CP_UTF8, 0, s1, n, ws1, wsizeof(ws1)); if (!converted) { logmsg(LOG_CRIT, "win_utf8ncasecmp: MultiByteToWideChar failed"); return -1; } ws1[converted] = '\0'; converted = MultiByteToWideChar(CP_UTF8, 0, s2, n, ws2, wsizeof(ws2)); if (!converted) { logmsg(LOG_CRIT, "win_utf8ncasecmp: MultiByteToWideChar failed"); return 1; } ws2[converted] = '\0'; /* compare */ return _wcsicmp(ws1, ws2); }
BOOL TRegistry::DeleteChildTreeW(const WCHAR *subKey) { WCHAR wbuf[256]; BOOL ret = TRUE; if (subKey && !OpenKeyW(subKey)) { return FALSE; } while (EnumKeyW(0, wbuf, wsizeof(wbuf))) { if (!(ret = DeleteChildTreeW(wbuf))) break; } if (subKey) { CloseKey(); ret = DeleteKeyW(subKey) ? ret : FALSE; } else { while (EnumValueW(0, wbuf, wsizeof(wbuf))) { if (!DeleteValueW(wbuf)) { ret = FALSE; break; } } } return ret; }
void print_help(const int debuglvl, char *part) { struct vrmr_list HelpList; int max_height = 0, max_width = 0, height = 0, width = 0, startx = 0, starty = 0; #ifdef USE_WIDEC wchar_t wpart[32] = L""; #endif /* USE_WIDEC */ /* get screensize */ getmaxyx(stdscr, max_height, max_width); width = 72; height = max_height - 6; startx = max_width - width - 5; starty = 3; #ifdef USE_WIDEC if(utf8_mode == FALSE) { #endif /* USE_WIDEC */ /* read the helpfile */ if(read_helpfile(debuglvl, &HelpList, part) < 0) return; set_lines(debuglvl, &HelpList, (size_t)(width - 4)); print_list(debuglvl, &HelpList, gettext("Help"), height, width, starty, startx, UTF8_FALSE); vrmr_list_cleanup(debuglvl, &HelpList); #ifdef USE_WIDEC } else { /* convert the part name to a wchar_t string */ mbstowcs(wpart, part, wsizeof(wpart)); if(debuglvl >= LOW) vrmr_debug(__FUNC__, "part: %s, wpart %ls, %u", part, wpart, wsizeof(wpart)); /* read the helpfile */ if(read_wide_helpfile(debuglvl, &HelpList, wpart) < 0) return; set_wide_lines(debuglvl, &HelpList, width - 4); print_list(debuglvl, &HelpList, gettext("Help"), height, width, starty, startx, UTF8_TRUE); vrmr_list_cleanup(debuglvl, &HelpList); } #endif /* USE_WIDEC */ }
static BOOL GetSelfSid(SID *sid, DWORD *sid_size) { WCHAR user[128]={}, sys[128]={}, domain[128]={}; DWORD user_size = wsizeof(user); DWORD domain_size = wsizeof(domain); if (!::GetUserNameW(user, &user_size)) return FALSE; SID_NAME_USE snu = SidTypeUser; return ::LookupAccountNameW(sys, user, sid, sid_size, domain, &domain_size, &snu); }
TApp::TApp(HINSTANCE _hI, LPSTR _cmdLine, int _nCmdShow) { hInstance = _hI; cmdLine = _cmdLine; nCmdShow = _nCmdShow; mainWnd = NULL; preWnd = NULL; result = 0; WCHAR cname[MAX_PATH]; snwprintfz(cname, wsizeof(cname), L"tapp_%d", ::GetCurrentProcessId()); defaultClassW = wcsdup(cname); defaultClass = WtoA(cname); tapp = this; hash = new TWinHashTbl(MAX_TAPPWIN_HASH); twinId = 1; InitInstanceForLoadStr(hInstance); #if ENGLISH_TEST TSetDefaultLCID(0x409); // for English Dialog Test #else TSetDefaultLCID(); #endif ::CoInitialize(NULL); ::InitCommonControls(); }
/* リンクの解決 あらかじめ、CoInitialize(NULL); を実行しておくこと */ BOOL ReadLinkU8(LPCSTR src, LPSTR dest, LPSTR arg) { IShellLink *shellLink; IPersistFile *persistFile; WCHAR wbuf[MAX_PATH]; BOOL ret = FALSE; if (SUCCEEDED(CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLinkW, (void **)&shellLink))) { if (SUCCEEDED(shellLink->QueryInterface(IID_IPersistFile, (void **)&persistFile))) { U8toW(src, wbuf, wsizeof(wbuf)); if (SUCCEEDED(persistFile->Load(wbuf, STGM_READ))) { if (SUCCEEDED(shellLink->GetPath((char *)wbuf, MAX_PATH, NULL, SLGP_SHORTPATH))) { WtoU8(wbuf, dest, MAX_PATH_U8); shellLink->GetArguments((char *)wbuf, MAX_PATH); WtoU8(wbuf, arg, MAX_PATH_U8); ret = TRUE; } } persistFile->Release(); } shellLink->Release(); } return ret; }
BOOL TInstDlg::AppKick() { WCHAR setupDir[MAX_PATH], setupPath[MAX_PATH]; GetDlgItemTextW(FILE_EDIT, setupDir, wsizeof(setupDir)); SetCurrentDirectoryW(setupDir); MakePathW(setupPath, setupDir, IPMSG_EXENAME_W); return (int)ShellExecuteW(NULL, NULL, setupPath, L"/SHOW_HISTORY", 0, SW_SHOW) > 32; }
int read_wide_helpfile(const int debuglvl, struct vrmr_list *help_list, wchar_t *part) { wchar_t line[128] = L""; FILE *fp = NULL; char inrange = 0; char helpfile[256] = ""; /* safety */ if(!help_list || !part) { vrmr_error(-1, VR_INTERR, "parameter problem " "(in: %s:%d).", __FUNC__, __LINE__); return(-1); } /* setup the list */ if(vrmr_list_setup(debuglvl, help_list, free_helpword) < 0) { vrmr_error(-1, VR_INTERR, "vrmr_list_setup failed " "(in: %s:%d).", __FUNC__, __LINE__); return(-1); } if(utf8_mode == 1) { /* TRANSLATORS: translate this to you language code: so for 'ru' use 'vuurmuur-ru.UTF-8.hlp', for 'pt_BR' use 'vuurmuur-pt_BR.UTF-8.hlp' */ if(snprintf(helpfile, sizeof(helpfile), "%s/%s", vccnf.helpfile_location, gettext("vuurmuur.UTF-8.hlp") ) >= (int)sizeof(helpfile)) { vrmr_error(-1, VR_INTERR, "buffer too small " "for helpfile supplied at compile-time " "(in: %s:%d).", __FUNC__, __LINE__); return(-1); } vrmr_sanitize_path(debuglvl, helpfile, sizeof(helpfile)); /* open the file */ fp = fopen(helpfile, "r"); } if(fp == NULL) { if(utf8_mode == 1) vrmr_debug(__FUNC__, "opening '%s' failed: " "%s, falling back to non UTF-8 language file.", helpfile, strerror(errno)); /* UTF-8 language helpfile does not exist, try to fall back to default */ /* TRANSLATORS: translate this to you language code: so for 'ru' use 'vuurmuur-ru.hlp', for 'pt_BR' use 'vuurmuur-pt_BR.hlp' */ if(snprintf(helpfile, sizeof(helpfile), "%s/%s", vccnf.helpfile_location, gettext("vuurmuur.hlp")) >= (int)sizeof(helpfile)) { vrmr_error(-1, "Error", "buffer too small for " "helpfile supplied at compile-time " "(in: %s:%d).", __FUNC__, __LINE__); return(-1); } vrmr_sanitize_path(debuglvl, helpfile, sizeof(helpfile)); } /* open the file */ fp = fopen(helpfile, "r"); if(fp == NULL) { vrmr_debug(__FUNC__, "opening '%s' failed: %s, " "falling back to default.", helpfile, strerror(errno)); /* language helpfile does not exist, try to fall back to default */ if(snprintf(helpfile, sizeof(helpfile), "%s/vuurmuur.hlp", vccnf.helpfile_location) >= (int)sizeof(helpfile)) { vrmr_error(-1, "Error", "buffer too small for " "helpfile supplied at compile-time " "(in: %s:%d).", __FUNC__, __LINE__); return(-1); } vrmr_sanitize_path(debuglvl, helpfile, sizeof(helpfile)); if(!(fp = fopen(helpfile, "r"))) { vrmr_error(-1, VR_ERR, "%s %s: %s", STR_OPENING_FILE_FAILED, helpfile, strerror(errno)); return(-1); } } while(fgetws(line, wsizeof(line), fp) != NULL) { if(inrange) { if(wcscmp(line, L":[END]:\n") == 0) { /* implied inrange = 0; */ break; } } if(inrange) { if(read_wide_helpline(debuglvl, help_list, line) < 0) return(-1); } else { if(wcsncmp(line, part, wcslen(part)) == 0) inrange = 1; } } fclose(fp); return(0); }
/* Wrapper for Windows stat function, which provides st_dev and st_ino. These are calculated as follows: st_dev is set to the drive number (0=A 1=B ...). Our virtual root "/" gets a st_dev of 0xff. st_ino is hashed from the full file path. Each half produces a 32 bit hash. These are concatenated to a 64 bit value. The risk that st_ino is the same for two files on the system is, if I'm not mistaken, b=pigeon(2**32, f)**2. For f=1000, b=1e-08. By using a 64 bit hash function this risk can be lowered. Possible future enhancement. pigeon() can be calculated in Python with: def pigeon(m, n): res = 1.0 for i in range(m - n + 1, m): res = res * i / m return 1 - res */ int win_stat(const char *file_name, backend_statstruct * buf) { wchar_t *winpath; int ret; wchar_t pathbuf[4096]; int retval; size_t namelen; wchar_t *splitpoint; char savedchar; struct _stat win_statbuf; /* Special case: Our top-level virtual root, containing each drive represented as a directory. Compare with "My Computer" etc. This virtual root has a hardcoded hash value of 1, to simplify debugging etc. */ if (!strcmp(file_name, "/")) { buf->st_mode = S_IFDIR | S_IRUSR | S_IWUSR; buf->st_nlink = MAX_NUM_DRIVES + 3; /* 3 extra for: . .. / */ buf->st_uid = 1; buf->st_gid = 1; buf->st_rdev = 0; buf->st_size = 4096; buf->st_atime = 0; buf->st_mtime = 0; buf->st_ctime = 0; buf->st_dev = 0xff; buf->st_ino = 1; return 0; } winpath = intpath2winpath(file_name); if (!winpath) { errno = EINVAL; return -1; } ret = _wstat(winpath, &win_statbuf); if (ret < 0) { free(winpath); return ret; } /* Copy values to our struct */ buf->st_mode = win_statbuf.st_mode; buf->st_nlink = win_statbuf.st_nlink; buf->st_uid = win_statbuf.st_uid; buf->st_gid = win_statbuf.st_gid; buf->st_rdev = win_statbuf.st_rdev; buf->st_size = win_statbuf.st_size; buf->st_atime = win_statbuf.st_atime; buf->st_mtime = win_statbuf.st_mtime; buf->st_ctime = win_statbuf.st_ctime; buf->st_blocks = win_statbuf.st_size / 512; retval = GetFullPathNameW(winpath, wsizeof(pathbuf), pathbuf, NULL); if (!retval) { errno = ENOENT; return -1; } /* Set st_dev to the drive number */ buf->st_dev = tolower(pathbuf[0]) - 'a'; /* GetLongPathName fails if called with only x:\, and drive x is not ready. So, only call it for other paths. */ if (pathbuf[0] && wcscmp(pathbuf + 1, L":\\")) { retval = GetLongPathNameW(pathbuf, pathbuf, wsizeof(pathbuf)); if (!retval || (unsigned) retval > wsizeof(pathbuf)) { /* Strangely enough, GetLongPathName returns ERROR_SHARING_VIOLATION for locked files, such as hiberfil.sys */ if (GetLastError() != ERROR_SHARING_VIOLATION) { errno = ENAMETOOLONG; return -1; } } } /* Hash st_ino, by splitting in two halves */ namelen = wcslen(pathbuf); splitpoint = &pathbuf[namelen / 2]; savedchar = *splitpoint; *splitpoint = '\0'; buf->st_ino = wfnv1a_32(pathbuf, 0); assert(sizeof(buf->st_ino) == 8); buf->st_ino = buf->st_ino << 32; *splitpoint = savedchar; buf->st_ino |= wfnv1a_32(splitpoint, 0); #if 0 fprintf(stderr, "win_stat: file=%s, ret=%d, st_dev=0x%x, st_ino=0x%I64x\n", file_name, ret, buf->st_dev, buf->st_ino); #endif free(winpath); return ret; }
// need thread safe (from WriteLogToFileProc) int LogMng::MakeMsgHead(LogMsg *msg, WCHAR *wbuf, int max_len, BOOL with_self) { int len = 0; BOOL is_memo = (msg->flags & DB_FLAG_MEMO) ? TRUE : FALSE; const WCHAR *from_to = (msg->flags & DB_FLAG_FROM) ? L" From: " : L" To: "; const WCHAR *next_to = (msg->flags & DB_FLAG_FROM) ? L" Cc: " : NULL; len = U8toW(LOGMSG_HEAD_TOP, wbuf, max_len); if (is_memo) { len += wcsncpyz(wbuf + len, msg->host[0].nick.s(), max_len - len); len += wcsncpyz(wbuf + len, L"\r\n", max_len - len); } else { for (auto host=msg->host.begin(); host != msg->host.end(); host++) { WCHAR host_str[MAX_LISTBUF]; if (host == msg->host.begin() || !with_self || !cfg->selfHost.IsSameUidHost(*host)) { len += wcsncpyz(wbuf + len, from_to, max_len - len); MakeHostFormat(&(*host), host_str, wsizeof(host_str)); len += wcsncpyz(wbuf + len, host_str, max_len - len); len += wcsncpyz(wbuf + len, L"\r\n", max_len - len); } if (next_to) { from_to = next_to; } } if (with_self) { const WCHAR *self_from_to = (msg->flags & DB_FLAG_FROM) ? L" To: " : L" From: "; WCHAR host_str[MAX_LISTBUF]; len += wcsncpyz(wbuf + len, self_from_to, max_len - len); MakeHostFormat(&cfg->selfHost, host_str, wsizeof(host_str)); len += wcsncpyz(wbuf + len, host_str, max_len - len); len += wcsncpyz(wbuf + len, L"\r\n", max_len - len); } } len += wcsncpyz(wbuf + len, L" at ", max_len - len); len += wcsncpyz(wbuf + len, CtimeW(msg->date), max_len - len); #ifdef IPMSG_PRO #define LOGMNG_MAKEMSGHEAD #include "miscext.dat" #undef LOGMNG_MAKEMSGHEAD #endif len += wcsncpyz(wbuf + len, L"\r\n", max_len - len); if (msg->clip.size() || msg->files.size()) { len += wcsncpyz(wbuf + len, msg->clip.size() ? msg->files.size() ? L" (files/image)" : L" (image)" : L" (files)", max_len - len); for (auto &f: msg->files) { len += wcsncpyz(wbuf + len, L" ", max_len - len); len += wcsncpyz(wbuf + len, f.s(), max_len - len); } for (auto &c: msg->clip) { len += wcsncpyz(wbuf + len, L" ", max_len - len); len += wcsncpyz(wbuf + len, c.fname.s(), max_len - len); } len += wcsncpyz(wbuf + len, L"\r\n", max_len - len); } len += U8toW(LOGMSG_HEAD_END, wbuf + len, max_len - len); return len; }