CFG_RW *NewCfgRwEx2W(FOLDER **root, wchar_t *cfg_name, bool dont_backup, wchar_t *template_name) { CFG_RW *rw; FOLDER *f; bool loaded_from_template = false; // Validate arguments if (cfg_name == NULL || root == NULL) { return NULL; } f = CfgReadW(cfg_name); if (f == NULL) { // Load from template if (UniIsEmptyStr(template_name) == false) { f = CfgReadW(template_name); if (f != NULL) { loaded_from_template = true; goto LABEL_CONTIUNE; } } rw = ZeroMalloc(sizeof(CFG_RW)); rw->lock = NewLock(); rw->FileNameW = CopyUniStr(cfg_name); rw->FileName = CopyUniToStr(cfg_name); rw->Io = FileCreateW(cfg_name); *root = NULL; rw->DontBackup = dont_backup; return rw; } LABEL_CONTIUNE: rw = ZeroMalloc(sizeof(CFG_RW)); rw->FileNameW = CopyUniStr(cfg_name); rw->FileName = CopyUniToStr(cfg_name); if (loaded_from_template == false) { rw->Io = FileOpenW(cfg_name, false); } else { rw->Io = FileCreateW(cfg_name); } rw->lock = NewLock(); *root = f; rw->DontBackup = dont_backup; return rw; }
// Save the Unicode cache void SaveUnicodeCache(wchar_t *strfilename, UINT strfilesize, UCHAR *hash) { UNICODE_CACHE c; BUF *b; UINT i; IO *io; wchar_t name[MAX_PATH]; UCHAR binhash[MD5_SIZE]; // Validate arguments if (strfilename == NULL || hash == NULL) { return; } Zero(&c, sizeof(c)); UniToStr(c.StrFileName, sizeof(c.StrFileName), strfilename); c.StrFileSize = strfilesize; GetMachineName(c.MachineName, sizeof(c.MachineName)); c.OsType = GetOsInfo()->OsType; Copy(c.hash, hash, MD5_SIZE); #ifdef OS_UNIX GetCurrentCharSet(c.CharSet, sizeof(c.CharSet)); #else // OS_UNIX { UINT id = MsGetThreadLocale(); Copy(c.CharSet, &id, sizeof(id)); } #endif // OS_UNIX b = NewBuf(); WriteBuf(b, &c, sizeof(c)); WriteBufInt(b, LIST_NUM(TableList)); for (i = 0;i < LIST_NUM(TableList);i++) { TABLE *t = LIST_DATA(TableList, i); WriteBufInt(b, StrLen(t->name)); WriteBuf(b, t->name, StrLen(t->name)); WriteBufInt(b, StrLen(t->str)); WriteBuf(b, t->str, StrLen(t->str)); WriteBufInt(b, UniStrLen(t->unistr)); WriteBuf(b, t->unistr, UniStrLen(t->unistr) * sizeof(wchar_t)); } Hash(binhash, b->Buf, b->Size, false); WriteBuf(b, binhash, MD5_SIZE); GenerateUnicodeCacheFileName(name, sizeof(name), strfilename, strfilesize, hash); io = FileCreateW(name); if (io != NULL) { SeekBuf(b, 0, 0); BufToFile(io, b); FileClose(io); } FreeBuf(b); }
// Download thread void ViDownloadThread(THREAD *thread, void *param) { VI_INSTALL_DLG *d; VI_SETTING_ARCH *a; HWND hWnd; UINT num_files = 2; VI_DOWNLOAD_FILE files[2]; VI_DOWNLOAD_FILE *f; UINT i; // Validate arguments if (thread == NULL || param == NULL) { return; } d = (VI_INSTALL_DLG *)param; hWnd = d->hWnd; Zero(files, sizeof(files)); a = ViGetSuitableArchForCpu(); // File body f = &files[0]; StrCpy(f->SrcPath, sizeof(f->SrcPath), a->Path); // Configuration file if (IsEmptyStr(setting.SettingPath) == false) { f = &files[1]; StrCpy(f->SrcPath, sizeof(f->SrcPath), setting.SettingPath); } else { // No configuration file num_files = 1; } for (i = 0;i < num_files;i++) { bool b = true; if (i == 0 && setting.DownloadNotRequired) { b = false; } if (b) { wchar_t tmp[MAX_SIZE]; IO *dest = NULL; VI_FILE *down; UINT ret; UINT totalsize; UINT currentsize; wchar_t filename_w[MAX_PATH]; f = &files[i]; GetFileNameFromFilePath(f->FileName, sizeof(f->FileName), f->SrcPath); MakeSafeFileName(f->FileName, sizeof(f->FileName), f->FileName); StrToUni(filename_w, sizeof(filename_w), f->FileName); ConbinePathW(f->DestPathW, sizeof(f->DestPathW), MsGetMyTempDirW(), filename_w); ViInstallDlgSetPos(hWnd, 0); UniFormat(tmp, sizeof(tmp), _U(IDS_DOWNLOADSTART+skip), f->FileName); ViInstallDlgSetText(d, hWnd, S_STATUS, tmp); down = ViOpenFile(f->SrcPath); if (down == NULL) { MsgBoxEx(hWnd, MB_ICONSTOP, _U(IDS_DOWNLOAD_ERROR+skip), f->FileName); ViInstallDlgCancel(hWnd); return; } dest = FileCreateW(f->DestPathW); if (dest == NULL) { MsgBoxEx(hWnd, MB_ICONSTOP, _U(IDS_TEMP_ERROR+skip), f->DestPathW); ViCloseFile(down); ViInstallDlgCancel(hWnd); return; } totalsize = ViGetFileSize(down); currentsize = 0; UniFormat(tmp, sizeof(tmp), _U(IDS_DOWNLOADING3+skip), f->FileName); ViInstallDlgSetText(d, hWnd, S_STATUS, tmp); while (true) { UINT pos = 0; if (d->Halt) { // User cancel FileClose(dest); ViCloseFile(down); return; } UniFormat(tmp, sizeof(tmp), _U(IDS_DOWNLOADING3+skip), f->FileName); ViInstallDlgSetText(d, hWnd, IDS_DOWNLOADING3+skip, tmp); ret = ViReadFile(down, d->Buf, d->BufSize); if (ret == INFINITE) { // Communication error MsgBoxEx(hWnd, MB_ICONSTOP, _U(IDS_DOWNLOAD_ERROR+skip), f->FileName); FileClose(dest); ViCloseFile(down); ViInstallDlgCancel(hWnd); return; } // Draw progress currentsize += ret; if (totalsize != 0) { UniFormat(tmp, sizeof(tmp), _U(IDS_DOWNLOADING+skip), ((float)totalsize) / 1024.0f / 1024.0f, ((float)currentsize) / 1024.0f / 1024.0f); pos = (UINT)(((float)currentsize) * 100.0f / ((float)totalsize)); } else { UniFormat(tmp, sizeof(tmp), _U(IDS_DOWNLOADING2+skip), ((float)currentsize) / 1024.0f / 1024.0f); pos = (UINT)(((float)currentsize) * 100.0f / (1024.0f * 1024.0f * 10.0f)); } ViInstallDlgSetText(d, hWnd, S_SIZEINFO, tmp); ViInstallDlgSetPos(hWnd, pos); if (ret == 0) { // Download Complete break; } else { FileWrite(dest, d->Buf, ret); } } ViCloseFile(down); FileClose(dest); } } UniStrCpy(setting.DownloadedInstallerPathW, sizeof(setting.DownloadedInstallerPathW), files[0].DestPathW); if (num_files >= 2) { UniStrCpy(setting.DownloadedSettingPathW, sizeof(setting.DownloadedSettingPathW), files[1].DestPathW); } PostMessageA(hWnd, WM_VI_DOWNLOAD_FINISHED, 0, 0); }
bool CfgSaveExW3(CFG_RW *rw, FOLDER *f, wchar_t *name, UINT *written_size, bool write_binary) { wchar_t tmp[MAX_SIZE]; bool text = !write_binary; UCHAR hash[SHA1_SIZE]; BUF *b; IO *o; bool ret = true; UINT dummy_int = 0; // Validate arguments if (name == NULL || f == NULL) { return false; } if (written_size == NULL) { written_size = &dummy_int; } // Convert to buffer b = CfgFolderToBuf(f, text); if (b == NULL) { return false; } // Hash the contents Hash(hash, b->Buf, b->Size, true); // Compare the contents to be written with the content which was written last if (rw != NULL) { if (Cmp(hash, rw->LashHash, SHA1_SIZE) == 0) { // Contents are not changed ret = false; } else { Copy(rw->LashHash, hash, SHA1_SIZE); } } if (ret || OS_IS_UNIX(GetOsInfo()->OsType)) { // Generate a temporary file name UniFormat(tmp, sizeof(tmp), L"%s.log", name); // Copy the file that currently exist to a temporary file FileCopyW(name, tmp); // Save the new file o = FileCreateW(name); if (o != NULL) { if (FileWrite(o, b->Buf, b->Size) == false) { // File saving failure FileClose(o); FileDeleteW(name); FileRenameW(tmp, name); if (rw != NULL) { Zero(rw->LashHash, sizeof(rw->LashHash)); } } else { // Successful saving file FileClose(o); // Delete the temporary file FileDeleteW(tmp); } } else { // File saving failure FileRenameW(tmp, name); if (rw != NULL) { Zero(rw->LashHash, sizeof(rw->LashHash)); } } } *written_size = b->Size; // Release memory FreeBuf(b); return ret; }
// Installation of WinPcap void EmInstallWinPcap(HWND hWnd, RPC *r) { wchar_t temp_name[MAX_SIZE]; HGLOBAL g; HINSTANCE h; HRSRC hr; UINT size; void *data; IO *io; // Ask whether the user want to start the installation if (MsgBox(hWnd, MB_ICONQUESTION | MB_YESNO, _UU("EM_WPCAP_INSTALL")) == IDNO) { return; } // Generate a temporary file name UniFormat(temp_name, sizeof(temp_name), L"%s\\winpcap_installer.exe", MsGetTempDirW()); // Read from the resource h = GetUiDll(); hr = FindResource(h, MAKEINTRESOURCE(BIN_WINPCAP), "BIN"); if (hr == NULL) { RES_ERROR: MsgBox(hWnd, MB_ICONSTOP, _UU("EM_RESOURCE")); return; } g = LoadResource(h, hr); if (g == NULL) { goto RES_ERROR; } size = SizeofResource(h, hr); data = LockResource(g); if (data == NULL) { goto RES_ERROR; } // Write to a temporary file io = FileCreateW(temp_name); if (io == NULL) { goto RES_ERROR; } FileWrite(io, data, size); FileClose(io); // Run if (RunW(temp_name, NULL, false, true) == false) { // Failure FileDeleteW(temp_name); goto RES_ERROR; } FileDeleteW(temp_name); if (r == NULL) { return; } // Message after the end if (OS_IS_WINDOWS_NT(GetOsInfo()->OsType) == false) { // Need to restart the computer MsgBox(hWnd, MB_ICONINFORMATION, _UU("EM_WPCAP_REBOOT1")); } else { // Need to restart the service if (MsgBox(hWnd, MB_ICONQUESTION | MB_YESNO, _UU("EM_WPCAP_REBOOT2")) == IDNO) { // Not restart } else { // Restart RPC_TEST t; RPC_BRIDGE_SUPPORT t2; Zero(&t, sizeof(t)); EcRebootServer(r, &t); SleepThread(500); Zero(&t2, sizeof(t2)); CALL(hWnd, EcGetBridgeSupport(r, &t2)); } } }
// Creating a new local console CONSOLE *NewLocalConsole(wchar_t *infile, wchar_t *outfile) { IO *in_io = NULL, *out_io = NULL; CONSOLE *c = ZeroMalloc(sizeof(CONSOLE)); LOCAL_CONSOLE_PARAM *p; UINT old_size = 0; #ifdef OS_WIN32 if (MsGetConsoleWidth() == 80) { //old_size = MsSetConsoleWidth(WIN32_DEFAULT_CONSOLE_WIDTH); } #endif // OS_WIN32 c->ConsoleType = CONSOLE_LOCAL; c->Free = ConsoleLocalFree; c->ReadLine = ConsoleLocalReadLine; c->ReadPassword = ConsoleLocalReadPassword; c->Write = ConsoleLocalWrite; c->GetWidth = ConsoleLocalGetWidth; c->OutputLock = NewLock(); if (UniIsEmptyStr(infile) == false) { // Input file is specified in_io = FileOpenW(infile, false); if (in_io == NULL) { wchar_t tmp[MAX_SIZE]; UniFormat(tmp, sizeof(tmp), _UU("CON_INFILE_ERROR"), infile); c->Write(c, tmp); Free(c); return NULL; } else { wchar_t tmp[MAX_SIZE]; UniFormat(tmp, sizeof(tmp), _UU("CON_INFILE_START"), infile); c->Write(c, tmp); } } if (UniIsEmptyStr(outfile) == false) { // Output file is specified out_io = FileCreateW(outfile); if (out_io == NULL) { wchar_t tmp[MAX_SIZE]; UniFormat(tmp, sizeof(tmp), _UU("CON_OUTFILE_ERROR"), outfile); c->Write(c, tmp); Free(c); if (in_io != NULL) { FileClose(in_io); } return NULL; } else { wchar_t tmp[MAX_SIZE]; UniFormat(tmp, sizeof(tmp), _UU("CON_OUTFILE_START"), outfile); c->Write(c, tmp); } } p = ZeroMalloc(sizeof(LOCAL_CONSOLE_PARAM)); c->Param = p; p->InFile = in_io; p->OutFile = out_io; p->Win32_OldConsoleWidth = old_size; if (in_io != NULL) { UINT size; void *buf; size = FileSize(in_io); buf = ZeroMalloc(size + 1); FileRead(in_io, buf, size); p->InBuf = NewBuf(); WriteBuf(p->InBuf, buf, size); Free(buf); p->InBuf->Current = 0; } return c; }