/******************************************************************************* * * 函 数 名 : BackupUDD * 功能描述 : 备份udd文件 * 参数列表 : 无 * 说 明 : * 返回结果 : 如果成功返回TRUE,失败返回FALSE * *******************************************************************************/ BOOL BackupUDD(char *pDestPath) { NULLVALUE_CHECK(pDestPath, BackupUDD) ; char szSourcePath[MAX_PATH] = {0} ; char szDestPath[MAX_PATH] = {0} ; char szProcessName[MAX_PATH] = {0} ; strcpy_s(szDestPath, MAX_PATH, pDestPath) ; if (FALSE == GetDebugPragramUDDPath(szSourcePath)) { OutputDebugString(_T("BackupUDD GetDebugPragramUDDPath failed")) ; return FALSE ; } if (FALSE == GetDebugProcessName(szProcessName)) { OutputDebugString(_T("BackupUDD GetDebugProcessName failed")) ; return FALSE ; } PathAddExtensionA(szProcessName, ".udd") ; PathAppendA(szSourcePath, szProcessName) ; PathAppendA(szDestPath,szProcessName) ; if(FALSE == CopyFile(szSourcePath, szDestPath, FALSE)) { MessageBox(NULL, _T("备份UDD文件错误"), _T("Tips"), MB_OK) ; return FALSE ; } return TRUE ; }
CKcsLogging::~CKcsLogging() { CStringA strFormat; CStringA strPath; char szPath[MAX_PATH * 2] = { 0 }; char szTime[64] = { 0 }; HANDLE fileHandle = INVALID_HANDLE_VALUE; time_t tNow = 0; time(&tNow); strftime(szTime, 64, "%Y-%m-%d", localtime(&tNow)); strFormat.Format(CLEAR_LOG_FILE, _GetLevelName(m_eLevel), szTime); m_stream << std::endl; std::string str_newLine(m_stream.str()); if (str_newLine.empty() || str_newLine.length() <= 10) goto Clear0; OutputDebugStringA(str_newLine.c_str()); SHGetSpecialFolderPathA(NULL, szPath, CSIDL_LOCAL_APPDATA, FALSE); PathAppendA(szPath, "KSafe\\KClear\\Logs"); if (!_CreateDirectoryNested(szPath)) goto Clear0; PathAppendA(szPath, strFormat); fileHandle = CreateFileA(szPath, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_ALWAYS,// | CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL); if (fileHandle == INVALID_HANDLE_VALUE || fileHandle == NULL) goto Clear0; SetFilePointer(fileHandle, 0, 0, FILE_END); DWORD num_written; WriteFile(fileHandle, static_cast<const void*>(str_newLine.c_str()), static_cast<DWORD>(str_newLine.length()), &num_written, NULL); Clear0: if (fileHandle != INVALID_HANDLE_VALUE) { CloseHandle(fileHandle); fileHandle = INVALID_HANDLE_VALUE; } return; }
/** * Builds a list of predefined paths for the User folder * according to the running system. * @return List of data paths. */ std::vector<std::string> findUserFolders() { std::vector<std::string> list; #ifdef _WIN32 char path[MAX_PATH]; // Get Documents folder if (SUCCEEDED(SHGetFolderPathA(NULL, CSIDL_PERSONAL, NULL, SHGFP_TYPE_CURRENT, path))) { PathAppendA(path, "OpenXcom\\"); list.push_back(path); } // Get binary directory if (GetModuleFileNameA(NULL, path, MAX_PATH) != 0) { PathRemoveFileSpecA(path); PathAppendA(path, "user\\"); list.push_back(path); } // Get working directory if (GetCurrentDirectoryA(MAX_PATH, path) != 0) { PathAppendA(path, "user\\"); list.push_back(path); } #else #ifdef __HAIKU__ list.push_back("/boot/apps/OpenXcom/"); #endif char const *home = getHome(); char path[MAXPATHLEN]; // Get user folders if (char const *const xdg_data_home = getenv("XDG_DATA_HOME")) { snprintf(path, MAXPATHLEN, "%s/openxcom/", xdg_data_home); } else { #ifdef __APPLE__ snprintf(path, MAXPATHLEN, "%s/Library/Application Support/OpenXcom/", home); #else snprintf(path, MAXPATHLEN, "%s/.local/share/openxcom/", home); #endif } list.push_back(path); // Get old-style folder snprintf(path, MAXPATHLEN, "%s/.openxcom/", home); list.push_back(path); // Get working directory list.push_back("./user/"); #endif return list; }
void CKcsLogging::ClearLogFileByDays(int nDay /* = 7 */) { CHAR szLogFileName[MAX_PATH * 2] = { 0 }; WIN32_FIND_DATAA fd = { 0 }; HANDLE hFile = INVALID_HANDLE_VALUE; SHGetSpecialFolderPathA(NULL, szLogFileName, CSIDL_LOCAL_APPDATA, FALSE); PathAppendA(szLogFileName, "KSafe\\KClear\\Logs"); PathAppendA(szLogFileName, "*.*"); hFile = FindFirstFileA(szLogFileName, &fd); if (INVALID_HANDLE_VALUE == hFile) { goto Clear0; } do { if (0 == stricmp(".", fd.cFileName) || 0 == stricmp("..", fd.cFileName)) continue; time_t lNow = 0; time(&lNow); FILETIME localfiletime; FileTimeToLocalFileTime(&(fd.ftLastWriteTime), &localfiletime); CTime stSys(lNow); CTime stFile(localfiletime); CTimeSpan stSPan; stSPan = stSys - stFile; if (stSPan.GetDays() >= nDay) { CStringA strtemp = szLogFileName ; CStringA strFileName = strtemp.Left(strtemp.ReverseFind(L'\\') + 1); strFileName += fd.cFileName; if (::PathFileExistsA(strFileName)) { ::DeleteFileA(strFileName); } } } while (FindNextFileA(hFile, &fd)); Clear0: if (hFile != INVALID_HANDLE_VALUE) { FindClose(hFile); hFile = INVALID_HANDLE_VALUE; } }
bool CheckCommonDirectory(std::string* outpath, const std::string& dirname) { std::unique_ptr<char[]> path(new char[MAX_PATH]); if (SHGetFolderPathA(NULL, CSIDL_COMMON_APPDATA, NULL, SHGFP_TYPE_CURRENT, path.get()) == S_OK) { PathAppendA(path.get(), dirname.c_str()); PathAppendA(path.get(), outpath->c_str()); if (FileExist(path.get())) { *outpath = path.get(); return true; } } return false; }
void CWE785_Path_Manipulation_Function_Without_Max_Sized_Buffer__w32_04_bad() { if(STATIC_CONST_TRUE) { { char path[BAD_PATH_SIZE]; DWORD length; length = GetCurrentDirectoryA(BAD_PATH_SIZE, path); if (length == 0 || length >= BAD_PATH_SIZE) /* failure conditions for this API call */ { exit(1); } /* FLAW: PathAppend assumes the 'path' parameter is MAX_PATH */ /* INCIDENTAL: CWE 121 stack based buffer overflow, which is intrinsic to * this example identified on the CWE webpage */ if (!PathAppendA(path, "AAAAAAAAAAAA")) { exit(1); } printSizeTLine(strlen(path)); printIntLine(BAD_PATH_SIZE); printLine(path); } } }
/* good1() uses if(STATIC_CONST_FALSE) instead of if(STATIC_CONST_TRUE) */ static void good1() { if(STATIC_CONST_FALSE) { /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */ printLine("Benign, fixed string"); } else { { /* FIX: ensure MAX_PATH allocated in 'path' */ char path[MAX_PATH]; DWORD length; length = GetCurrentDirectoryA(MAX_PATH, path); if (length == 0 || length >= MAX_PATH) { exit(1); /* failure conditions for this API call */ } if (!PathAppendA(path, "AAAAAAAAAAAA")) { exit(1); } printLine(path); } } }
/* Returns a full path for a filename in userspace (i.e., where we'll have read/write access). * For Windows, this is in %APPDATA%/application_name/ * For Linux (including Maemo and Meego), this is in user's home/.config/application_name/ (note the '.', to make it a hidden folder) * If the folder can't be accessed (or running on a new operating system), the program folder is used. * For Qt platforms, we use QDesktopServices::storageLocation(QDesktopServices::DataLocation). */ char *getApplicationFilename(const char *name) { // not safe to use LOG here, as logfile may not have been initialised! //printf("getApplicationFilename: %s\n", name); //printf("application_path: %s\n", application_path); // Maemo/Meego treated as Linux as far as paths are concerned #if _WIN32 char *filename = new char[MAX_PATH]; strcpy(filename, application_path); PathAppendA(filename, name); #elif __linux char *filename = NULL; int application_path_len = strlen(application_path); if( application_path_len == 0 || application_path[application_path_len-1] == '/' ) { // shouldn't add path separator int len = application_path_len + strlen(name); filename = new char[len+1]; sprintf(filename, "%s%s", application_path, name); } else { // should add path separator int len = application_path_len + 1 + strlen(name); filename = new char[len+1]; sprintf(filename, "%s/%s", application_path, name); } #else char *filename = new char[strlen(name)+1]; strcpy(filename, name); #endif //printf("getApplicationFilename returns: %s\n", filename); return filename; }
/************************************************************************* * PathAppend [SHELL32.36] */ BOOL WINAPI PathAppendAW( LPVOID lpszPath1, LPCVOID lpszPath2) { if (SHELL_OsIsUnicode()) return PathAppendW(lpszPath1, lpszPath2); return PathAppendA(lpszPath1, lpszPath2); }
static HMODULE DelayLoadLibraryA(LPCSTR dllname) { CHAR curfile[_MAX_PATH]; GetModuleFileNameA(hInstance, curfile, MAX_PATH); PathRemoveFileSpecA(curfile); PathAppendA(curfile, dllname); HMODULE hdll = LoadLibraryA(curfile); if (hdll == nullptr) return LoadLibraryA(dllname); return hdll; }
static bool GetSavePathFunc(int, char* buffer, const char* fileName) { // get the base directory std::wstring savePath = GetCitizenSavePath(); // and the save path wcstombs(buffer, savePath.c_str(), MAX_PATH - 1); buffer[MAX_PATH] = '\0'; // and PathAppend to it PathAppendA(buffer, fileName); return true; }
std::string local::savedgamesdir(std::string name) { #if defined(_WIN32) char szPath[MAX_PATH]; auto result = SHGetFolderPathA(NULL, CSIDL_PERSONAL | CSIDL_FLAG_CREATE, NULL, 0, szPath); if(FAILED(result)) { debugmanager()->fatal("CSIDL_PERSONAL: failed to retrieve path"); } PathAppendA(szPath, "My Games"); PathAppendA(szPath, name.data()); return std::string(szPath); #elif defined(__linux__) struct passwd *pwd = getpwuid(getuid()); return std::string(pwd->pw_dir) + "/mygames/" + name; #elif defined(__APPLE__) struct passwd *pwd = getpwuid(getuid()); return std::string(pwd->pw_dir) + "/Documents/My Games/" + name; #else debugmanager()->fatal( "polar::fs::local::savedgamesdir: not implemented"); return ""; #endif }
std::string get_sysinfo() { #ifdef _WIN32 std::ostringstream oss; LPSTR filePath = new char[MAX_PATH]; UINT blockSize; VS_FIXEDFILEINFO *fixedFileInfo; GetSystemDirectoryA(filePath, MAX_PATH); PathAppendA(filePath, "kernel32.dll"); DWORD dwVersionSize = GetFileVersionInfoSizeA(filePath, NULL); LPBYTE lpVersionInfo = new BYTE[dwVersionSize]; GetFileVersionInfoA(filePath, 0, dwVersionSize, lpVersionInfo); VerQueryValueA(lpVersionInfo, "\\", (LPVOID *)&fixedFileInfo, &blockSize); oss << "Windows/" << HIWORD(fixedFileInfo->dwProductVersionMS) << '.' // Major << LOWORD(fixedFileInfo->dwProductVersionMS) << '.' // Minor << HIWORD(fixedFileInfo->dwProductVersionLS) << ' '; // Build #ifdef _WIN64 oss << "x86_64"; #else BOOL is64 = FALSE; if (IsWow64Process(GetCurrentProcess(), &is64) && is64) oss << "x86_64"; // 32-bit app on 64-bit OS else oss << "x86"; #endif delete[] lpVersionInfo; delete[] filePath; return oss.str(); #else struct utsname osinfo; uname(&osinfo); return std::string(osinfo.sysname) + "/" + osinfo.release + " " + osinfo.machine; #endif }
bool WcxArchive::pExtract( const char *lpDestPath ) { int nProcessed = 0; int nResult = 0; m_files.clear(); while ( nResult == 0 ) { tHeaderData HeaderData; memset (&HeaderData, 0, sizeof (HeaderData)); nResult = m_pModule->m_pfnReadHeader (m_hArchive, &HeaderData); if( nResult!=0 ) continue; char szDestPath[MAX_PATH] = {0}; strcpy(szDestPath, lpDestPath); PathAppendA(szDestPath, HeaderData.FileName); int nProcessResult = 0; if ( (HeaderData.FileAttr & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY ) { CreateDirEx (szDestPath); nProcessResult = m_pModule->m_pfnProcessFile (m_hArchive, PK_SKIP, NULL, NULL); ATLASSERT(FALSE); } else { CreateDirs( szDestPath ); SetFileApisToANSI(); nProcessResult = m_pModule->m_pfnProcessFile (m_hArchive, PK_EXTRACT, NULL, szDestPath); SetFileApisToOEM(); if(!nProcessResult) m_files.push_back( szDestPath ); } if ( !nProcessResult ) nProcessed++; } return nProcessed!=0; }
/******************************************************************************* * * 函 数 名 : GetDebugPragramUDDPath * 功能描述 : 取得od udd目录 * 参数列表 : pOutBuffer -- 取出buffer * 说 明 : * 返回结果 : 如果成功返回TRUE,失败返回FALSE * *******************************************************************************/ BOOL GetDebugPragramUDDPath(char *pOutBuffer) { NULLVALUE_CHECK(pOutBuffer, BackupUDD) ; char szOdPath[MAX_PATH] = {0} ; // 取得od路径 if(0 == GetModuleFileNameA(NULL, szOdPath, MAX_PATH)) { OutputDebugString(_T("GetDebugPragramUDDPath GetModuleFileNameA failed")) ; return FALSE ; } // 构造UDD路径 // 处理中路径,去文件名 PathRemoveFileSpecA(szOdPath) ; PathAppendA(szOdPath, "UDD") ; strcpy_s(pOutBuffer, MAX_PATH, szOdPath) ; return TRUE ; }
/* good1() reverses the blocks on the goto statement */ static void good1() { goto sink; sink: { /* FIX: ensure MAX_PATH allocated in 'path' */ char path[MAX_PATH]; DWORD length; length = GetCurrentDirectoryA(MAX_PATH, path); if (length == 0 || length >= MAX_PATH) { exit(1); /* failure conditions for this API call */ } if (!PathAppendA(path, "AAAAAAAAAAAA")) { exit(1); } printLine(path); } }
int thcrap_inject_into_running(HANDLE hProcess, const char *run_cfg_fn) { int ret = -1; HMODULE inj_mod = NULL; if(GetModuleHandleEx( GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT | GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPTSTR)thcrap_inject_into_running, &inj_mod )) { size_t cur_dir_len = GetCurrentDirectory(0, NULL) + 1; size_t inj_dir_len = GetModuleFileNameU(inj_mod, NULL, 0) + 1; VLA(char, inj_dll, inj_dir_len); VLA(char, inj_dir, inj_dir_len); STRLEN_DEC(run_cfg_fn); size_t param_len = cur_dir_len + run_cfg_fn_len; VLA(char, abs_run_cfg_fn, param_len); const char *param; GetModuleFileNameU(inj_mod, inj_dir, inj_dir_len); strncpy(inj_dll, inj_dir, inj_dir_len); PathRemoveFileSpec(inj_dir); PathAddBackslashA(inj_dir); // Allow for relative directory names if(PathIsRelativeA(run_cfg_fn)) { GetCurrentDirectory(cur_dir_len, abs_run_cfg_fn); PathAppendA(abs_run_cfg_fn, run_cfg_fn); param = abs_run_cfg_fn; } else { param = run_cfg_fn; param_len = run_cfg_fn_len; } ret = Inject(hProcess, inj_dir, inj_dll, "thcrap_init", param, param_len); VLA_FREE(abs_run_cfg_fn); VLA_FREE(inj_dir); VLA_FREE(inj_dll); } return ret; }
/************************************************************************* Constructor *************************************************************************/ CHelperSystem::CHelperSystem() : m_pXMLParser(0) { s_pMe = this; //Set work dir char szDir[MAX_PATH] = {0}; GetCurrentDirectoryA(MAX_PATH, szDir); PathAppendA(szDir, "..\\Helper"); PathAddBackslashA(szDir); m_strWorkDictory = szDir; // create TinyXML Parser. m_pXMLParser = new CHelperTinyXMLParser; // load all helper items. m_pHelpItemSet = new CHelperItemSet(); //debug begin //m_pHelpItemSet->ShowPaihang(true); //m_pHelpItemSet->InitPaihangTree("104"); //enddebug m_pHelpItemSet->LoadItemSet(CHelperSystem::GetMe()->GetWorkDictory() + "HelperItems.xml"); }
bool FullPathFromPath(std::string* path, const std::string& in_path) { if (PathIsRelativeA(in_path.c_str())) { std::unique_ptr<char[]> buffer(new char[MAX_PATH]); if (GetModuleFileNameA(CurrentModule(), buffer.get(), MAX_PATH) && PathRemoveFileSpecA(buffer.get())) { PathAppendA(buffer.get(), in_path.c_str()); *path = buffer.get(); } } else { *path = in_path; } if (FileExist(*path)) return true; return false; }
HRESULT __stdcall CArchiveExtractCallback::GetStream ( unsigned int index, ISequentialOutStream **outStream, int askExtractMode ) { CPropVariant value; IInArchive *archive = m_pArchive->m_pArchive; if ( askExtractMode == 0 ) //extract { if ( archive->GetProperty (index, kpidPath, &value) != S_OK ) return S_OK; //!!! to return error char szArcFileName[MAX_PATH]; char szFullName[MAX_PATH]; if ( value.vt == VT_BSTR ) WideCharToMultiByte (CP_OEMCP, 0, value.bstrVal, -1, szArcFileName, MAX_PATH, NULL, NULL); else { //strcpy (szArcFileName, FSF.PointToName (m_pArchive->m_lpFileName)); //CutTo (szArcFileName, '.', true); ATLASSERT(FALSE); } strcpy (szFullName, m_lpDestPath.c_str()); PathAppendA(szFullName, szArcFileName); if ( (int)m_nLastProcessed == -1 ) m_nLastProcessed = 0; FILETIME ftCreationTime, ftLastAccessTime, ftLastWriteTime; DWORD dwFileAttributes = 0; memset (&ftCreationTime, 0, sizeof (FILETIME)); memset (&ftLastAccessTime, 0, sizeof (FILETIME)); memset (&ftLastWriteTime, 0, sizeof (FILETIME)); if ( archive->GetProperty (index, kpidAttrib, &value) == S_OK ) { if ( value.vt == VT_UI4 ) dwFileAttributes = value.ulVal; } if ( archive->GetProperty (index, kpidCTime, &value) == S_OK ) { if ( value.vt == VT_FILETIME ) memcpy (&ftCreationTime, &value.filetime, sizeof (FILETIME)); } if ( archive->GetProperty (index, kpidATime, &value) == S_OK ) { if ( value.vt == VT_FILETIME ) memcpy (&ftLastAccessTime, &value.filetime, sizeof (FILETIME)); } if ( archive->GetProperty (index, kpidMTime, &value) == S_OK ) { if ( value.vt == VT_FILETIME ) memcpy (&ftLastWriteTime, &value.filetime, sizeof (FILETIME)); } bool bIsFolder = false; if ( archive->GetProperty (index, kpidIsDir, &value) == S_OK ) { if (value.vt == VT_BOOL) bIsFolder = (value.boolVal == VARIANT_TRUE); } if ( bIsFolder || dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY ) { *outStream = NULL; CreateDirEx (szFullName, dwFileAttributes); } else { CreateDirs (szFullName); COutFile *file = new COutFile (szFullName); if ( file->Open () ) { file->SetAttributes (dwFileAttributes); file->SetTime (&ftCreationTime, &ftLastAccessTime, &ftLastWriteTime); *outStream = file; m_files.push_back( szFullName ); } else delete file; } } else *outStream = NULL; return S_OK; }
/** * Builds a list of predefined paths for the Data folder * according to the running system. * @return List of data paths. */ std::vector<std::string> findDataFolders() { std::vector<std::string> list; #ifdef _WIN32 char path[MAX_PATH]; // Get Documents folder if (SUCCEEDED(SHGetFolderPathA(NULL, CSIDL_PERSONAL, NULL, SHGFP_TYPE_CURRENT, path))) { PathAppendA(path, "OpenXcom\\data\\"); list.push_back(path); } // Get binary directory if (GetModuleFileNameA(NULL, path, MAX_PATH) != 0) { PathRemoveFileSpecA(path); PathAppendA(path, "data\\"); list.push_back(path); } // Get working directory if (GetCurrentDirectoryA(MAX_PATH, path) != 0) { PathAppendA(path, "data\\"); list.push_back(path); } #else #ifdef __HAIKU__ list.push_back("/boot/apps/OpenXcom/data/"); #endif char const *home = getHome(); char path[MAXPATHLEN]; // Get user-specific data folders if (char const *const xdg_data_home = getenv("XDG_DATA_HOME")) { snprintf(path, MAXPATHLEN, "%s/openxcom/data/", xdg_data_home); } else { #ifdef __APPLE__ snprintf(path, MAXPATHLEN, "%s/Library/Application Support/OpenXcom/data/", home); #else snprintf(path, MAXPATHLEN, "%s/.local/share/openxcom/data/", home); #endif } list.push_back(path); // Get global data folders if (char *xdg_data_dirs = getenv("XDG_DATA_DIRS")) { char *dir = strtok(xdg_data_dirs, ":"); while (dir != 0) { snprintf(path, MAXPATHLEN, "%s/openxcom/data/", dir); list.push_back(path); dir = strtok(0, ":"); } } else { #ifdef __APPLE__ snprintf(path, MAXPATHLEN, "%s/Users/Shared/OpenXcom/data/", home); list.push_back(path); #else list.push_back("/usr/local/share/openxcom/data/"); list.push_back("/usr/share/openxcom/data/"); #endif } // Get working directory list.push_back("./data/"); #endif return list; }
bool CSusie::Mount(CArcFile* pclArc) { // Get header const u8* pbtHeader = pclArc->GetHeader(); // Mount for (auto& pstsiTarget : m_stsiMain) { if (pstsiTarget.bValidity == 0) { // Invalid Susie plugin continue; } // Get IsSupported() IsSupportedProc IsSupported = reinterpret_cast<IsSupportedProc>(pstsiTarget.cllPlugin.GetProcAddress(_T("IsSupported"))); if (IsSupported == nullptr) { // IsSupported function is not implemented continue; } // Copy the archive file path char szPathToArc[MAX_PATH]; YCStringA clsPathToArc = pclArc->GetArcPath(); strcpy(szPathToArc, clsPathToArc); // Call IsSupported() BYTE abtHeader[2048]; memcpy(abtHeader, pbtHeader, sizeof(abtHeader)); if (IsSupported(szPathToArc, (DWORD) abtHeader) == 0) { // Archive file not supported continue; } // Get GetArchiveInfo() GetArchiveInfoProc GetArchiveInfo = reinterpret_cast<GetArchiveInfoProc>(pstsiTarget.cllPlugin.GetProcAddress(_T("GetArchiveInfo"))); if (GetArchiveInfo == nullptr) { // We trust IsSupported() and attempt to mount return pclArc->Mount(); } // Copy archive path (Since there is a chance that IsSupported() can be overwritten) strcpy(szPathToArc, clsPathToArc); // Call GetArchiveInfo() YCLocalMemory cllmFileInfo; if (GetArchiveInfo(szPathToArc, 0, 0, &cllmFileInfo.GetHandle()) != 0) { // Error in function GetArchiveInfo() cllmFileInfo.Free(); // We will trust IsSupported() and attempt to mount return pclArc->Mount(); } // Error in the function GetArchiveInfo() if (cllmFileInfo.GetHandle() == nullptr) { // We will trust IsSupported() and attempt to mount return pclArc->Mount(); } // Get file information fileInfo* pstFileInfo = (fileInfo*) cllmFileInfo.Lock(); size_t uSusieFileInfoSize = cllmFileInfo.GetSize(); size_t uFileCount = uSusieFileInfoSize / sizeof(fileInfo); for (size_t uIndex = 0; uIndex < uFileCount; uIndex++) { if (pstFileInfo->method[0] == '\0') { // Exit break; } char szFileName[MAX_PATH]; if (pstFileInfo->path[0] != '\0') { // Path exists strcpy(szFileName, pstFileInfo->path); PathAppendA(szFileName, pstFileInfo->filename); } else { strcpy(szFileName, pstFileInfo->filename); } // Set the file information SFileInfo stFileInfo; stFileInfo.name = szFileName; stFileInfo.sizeCmp = (pstFileInfo->compsize == 0) ? pstFileInfo->filesize : pstFileInfo->compsize; stFileInfo.sizeOrg = pstFileInfo->filesize; stFileInfo.start = pstFileInfo->position; stFileInfo.end = stFileInfo.start + stFileInfo.sizeCmp; stFileInfo.format.Append((LPTSTR)pstFileInfo->method, 8); pclArc->AddFileInfo(stFileInfo); pstFileInfo++; } // Release resources cllmFileInfo.Free(); return true; } return false; }
/** * Builds a list of predefined paths for the Data folder * according to the running system. * @return List of data paths. */ std::vector<std::string> findDataFolders() { std::vector<std::string> list; #ifdef __MORPHOS__ list.push_back("PROGDIR:"); return list; #endif #ifdef _WIN32 char path[MAX_PATH]; // Get Documents folder if (SUCCEEDED(SHGetFolderPathA(NULL, CSIDL_PERSONAL, NULL, SHGFP_TYPE_CURRENT, path))) { PathAppendA(path, "OpenXcom\\"); list.push_back(path); } // Get binary directory if (GetModuleFileNameA(NULL, path, MAX_PATH) != 0) { PathRemoveFileSpecA(path); list.push_back(path); } // Get working directory if (GetCurrentDirectoryA(MAX_PATH, path) != 0) { list.push_back(path); } #else char const *home = getHome(); #ifdef __HAIKU__ char data_path[B_PATH_NAME_LENGTH]; find_directory(B_SYSTEM_SETTINGS_DIRECTORY, 0, true, data_path, sizeof(data_path)-strlen("/OpenXcom/")); strcat(data_path,"/OpenXcom/"); list.push_back(data_path); #endif char path[MAXPATHLEN]; // Get user-specific data folders if (char const *const xdg_data_home = getenv("XDG_DATA_HOME")) { snprintf(path, MAXPATHLEN, "%s/openxcom/", xdg_data_home); } else { #ifdef __APPLE__ snprintf(path, MAXPATHLEN, "%s/Library/Application Support/OpenXcom/", home); #else snprintf(path, MAXPATHLEN, "%s/.local/share/openxcom/", home); #endif } list.push_back(path); // Get global data folders if (char *xdg_data_dirs = getenv("XDG_DATA_DIRS")) { char *dir = strtok(xdg_data_dirs, ":"); while (dir != 0) { snprintf(path, MAXPATHLEN, "%s/openxcom/", dir); list.push_back(path); dir = strtok(0, ":"); } } #ifdef __APPLE__ list.push_back("/Users/Shared/OpenXcom/"); #else list.push_back("/usr/local/share/openxcom/"); #ifndef __FreeBSD__ list.push_back("/usr/share/openxcom/"); #endif #ifdef DATADIR snprintf(path, MAXPATHLEN, "%s/", DATADIR); list.push_back(path); #endif #endif // Get working directory list.push_back("./"); #endif return list; }
void initLogFile() { // first need to establish full path, and create folder if necessary // Maemo/Meego treated as Linux as far as paths are concerned LOG("initLogFile()\n"); // n.b., at this stage logging will only go to console output, not to log file #if _WIN32 bool ok = true; WCHAR logfilename_w[MAX_PATH]; if ( SUCCEEDED( SHGetFolderPathW( NULL, CSIDL_APPDATA, NULL, 0, logfilename_w ) ) ) { { // handle unicode (e.g., for unicode user accounts) int shortpath_length_w = GetShortPathNameW(logfilename_w,0,0); LPWSTR shortpath_w = new WCHAR[shortpath_length_w]; GetShortPathNameW(logfilename_w,shortpath_w,shortpath_length_w); int shortpath_length = WideCharToMultiByte(CP_OEMCP, WC_NO_BEST_FIT_CHARS, shortpath_w, shortpath_length_w, 0, 0, 0, 0); WideCharToMultiByte(CP_OEMCP, WC_NO_BEST_FIT_CHARS, shortpath_w, shortpath_length_w, logfilename, MAX_PATH, 0, 0); delete [] shortpath_w; } PathAppendA(logfilename, application_name); if( access(logfilename, 0) != 0 ) { // folder doesn't seem to exist - try creating it int res = mkdir(logfilename); //int res = 1; // test if( res != 0 ) { printf("Failed to create folder for application data!\n"); MessageBoxA(NULL, "Failed to create folder for application data - storing in local folder instead.\n", "Warning", MB_OK|MB_ICONEXCLAMATION); ok = false; } } } else { printf("Failed to obtain path for application folder!\n"); MessageBoxA(NULL, "Failed to obtain path for application folder - storing in local folder instead.\n", "Warning", MB_OK|MB_ICONEXCLAMATION); ok = false; } if( ok ) { strcpy(application_path, logfilename); strcpy(oldlogfilename, logfilename); PathAppendA(logfilename, "log.txt"); PathAppendA(oldlogfilename, "log_old.txt"); } else { // just save in local directory and hope for the best! strcpy(application_path, ""); strcpy(logfilename, "log.txt"); strcpy(oldlogfilename, "log_old.txt"); } #elif defined(__ANDROID__) // create the folder if it doesn't already exist bool ok = true; if( access(application_path, 0) != 0 ) { __android_log_print(ANDROID_LOG_INFO, "Gigalomania", "try to create data folder"); int res = mkdir(application_path, S_IRWXU | S_IRWXG | S_IRWXO); if( res != 0 ) { __android_log_print(ANDROID_LOG_INFO, "Gigalomania", "failed to create data folder"); ok = false; } } if( ok ) { logfilename = getApplicationFilename("log.txt"); oldlogfilename = getApplicationFilename("log_old.txt"); } else { // just save in local directory and hope for the best! strcpy(application_path, ""); logfilename = getApplicationFilename("log.txt"); oldlogfilename = getApplicationFilename("log_old.txt"); } #elif __linux char *homedir = getenv("HOME"); //const char *subdir = "/.gigalomania"; const char *subdir = "/.config/gigalomania"; int len = strlen(homedir) + strlen(subdir); application_path = new char[len+1]; sprintf(application_path, "%s%s", homedir, subdir); // create the folder if it doesn't already exist bool ok = true; if( access(application_path, 0) != 0 ) { int res = mkdir(application_path, S_IRWXU | S_IRWXG | S_IRWXO); if( res != 0 ) { ok = false; } } if( ok ) { logfilename = getApplicationFilename("log.txt"); oldlogfilename = getApplicationFilename("log_old.txt"); } else { // just save in local directory and hope for the best! strcpy(application_path, ""); logfilename = getApplicationFilename("log.txt"); oldlogfilename = getApplicationFilename("log_old.txt"); } #else // no need to do anything #endif remove(oldlogfilename); rename(logfilename, oldlogfilename); remove(logfilename); LOG("Initialising Log File...\n"); LOG("Version %d.%d\n", majorVersion, minorVersion); #ifdef _DEBUG LOG("Running in Debug mode\n"); #else LOG("Running in Release mode\n"); #endif #if defined(_WIN32) LOG("Platform: Windows\n"); #elif defined(__ANDROID__) // must be before __linux, as Android also defines __linux LOG("Platform: Android\n"); #elif __linux LOG("Platform: Linux\n"); #elif defined(__APPLE__) && defined(__MACH__) LOG("Platform: MacOS X\n"); #elif __amigaos4__ // must be before AROS, as the AmigaOS 4 makefile defines AROS too LOG("Platform: AmigaOS 4\n"); #elif AROS LOG("Platform: AROS\n"); #elif defined(__MORPHOS__) LOG("Platform: MorphOS\n"); #else LOG("Platform: UNKNOWN\n"); #endif LOG("Application path: %s\n", application_path); LOG("logfilename: %s\n", logfilename); LOG("oldlogfilename: %s\n", oldlogfilename); }
int main(int argc, char* argv[]) { #ifdef _DEBUG // Setup the debug options _CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF //Check for memory leaks on app exit );//| _CRTDBG_CHECK_ALWAYS_DF); _CrtSetAllocHook(YourAllocHook); #endif // Read in Version Infomation CFileVersionInfo ver; ver.Open(GetModuleHandle(NULL)); TCHAR displayName[256]; _sntprintf(displayName, 255, _T("Munin Node for Windows %i.%i.%i"), ver.GetFileVersionMajor(), ver.GetFileVersionMinor(), ver.GetFileVersionQFE()); // Load Configuration // Get the executable file path char szConfigFilePath[MAX_PATH]; ::GetModuleFileNameA(NULL, szConfigFilePath, MAX_PATH); PathRemoveFileSpecA(szConfigFilePath); PathAppendA(szConfigFilePath, "\\munin-node.ini"); g_Config.SetPath(szConfigFilePath); g_Config.ReadFile(); // Prepare Service modules _Module.Init(_T("munin-node"), _T("Munin Node for Windows")); _Module.m_bService = TRUE; // Parse arguments if (argc > 1) { char seps[] = "-/"; char *pToken; pToken = strtok(argv[1], seps); while (pToken) { if (!stricmp(pToken, "install")) { return !_Module.Install(); } else if (!stricmp(pToken, "uninstall")) { return !_Module.Uninstall(); } else if (!stricmp(pToken, "quiet")) { FreeConsole(); } else if (!stricmp(pToken, "unattended")) { _Module.SetQuiet(true); } else if (!stricmp(pToken, "run")) { _Module.m_bService = FALSE; } else if (!stricmp(pToken, "help") || !stricmp(pToken, "h") || !stricmp(pToken, "?")) { printf("%s\n", _Module.GetServiceDisplayName()); printf("Usage:\n"); printf(" -install Install the 'Munin Node' service.\n"); printf(" -uninstall Removes the 'Munin Node' service.\n"); printf(" -quiet Close the console window, running in the background.\n"); printf(" -run Run as a normal program, rather than a service.\n"); return 1; } pToken = strtok(NULL, seps); } } _Module.Start(); return 0; }
bool CSusie::Decode(CArcFile* pclArc) { YCStringA clsPathToArc = pclArc->GetArcPath(); YCStringA clsFileName = pclArc->GetOpenFileInfo()->name; // Get header const u8* pbtHeader = pclArc->GetHeader(); // GetPicture() file input std::vector<YCLibrary*> vtSupportPlugin; for (auto& pstsiTarget : m_stsiMain) { if (pstsiTarget.bValidity == 0) { // Invalid Susie plugin continue; } // Get IsSupported() IsSupportedProc IsSupported = reinterpret_cast<IsSupportedProc>(pstsiTarget.cllPlugin.GetProcAddress(_T("IsSupported"))); if (IsSupported == nullptr) { // IsSupported() function is not implemented continue; } // Copy the archive's file path char szPathToArc[MAX_PATH]; strcpy(szPathToArc, clsPathToArc); BYTE abtHeader[2048]; memcpy(abtHeader, pbtHeader, sizeof(abtHeader)); if (!IsSupported(szPathToArc, (DWORD) abtHeader)) { // Archive file is not supported continue; } // Archive file is supported vtSupportPlugin.push_back(&pstsiTarget.cllPlugin); // Copy the archive file path strcpy(szPathToArc, clsPathToArc); // Get GetPicture() GetPictureProc GetPicture = reinterpret_cast<GetPictureProc>(pstsiTarget.cllPlugin.GetProcAddress(_T("GetPicture"))); if (GetPicture == nullptr) { // GetPicture() function is not implemented continue; } YCLocalMemory cllmBitmapInfo; YCLocalMemory cllmBitmapData; // Call GetPicture() if (GetPicture(szPathToArc, pclArc->GetOpenFileInfo()->start, 0x0000, &cllmBitmapInfo.GetHandle(), &cllmBitmapData.GetHandle(), nullptr, 0) != 0) { // GetPicture() has failed continue; } // Get image information LPBITMAPINFO pBMPInfo = (LPBITMAPINFO) cllmBitmapInfo.Lock(); LPBYTE pHBm = (LPBYTE) cllmBitmapData.Lock(); size_t HBmSize = cllmBitmapData.GetSize(); // Image output CImage image; image.Init(pclArc, pBMPInfo->bmiHeader.biWidth, pBMPInfo->bmiHeader.biHeight, pBMPInfo->bmiHeader.biBitCount, (LPBYTE) pBMPInfo->bmiColors); image.Write(pHBm, HBmSize); image.Close(); // Exit (Free resources) cllmBitmapInfo.Free(); cllmBitmapData.Free(); return true; } // File input - GetFile() YCLocalMemory cllmSrc; for (auto& supportPlugin : vtSupportPlugin) { // Copy archive file path char szPathToArc[MAX_PATH]; strcpy(szPathToArc, clsPathToArc); // Get GetFile() GetFileProc GetFile = reinterpret_cast<GetFileProc>(supportPlugin->GetProcAddress(_T("GetFile"))); if (GetFile == nullptr) { // GetFile() function is not supported continue; } // Call GetFile() if (GetFile(szPathToArc, pclArc->GetOpenFileInfo()->start, (LPSTR)&cllmSrc.GetHandle(), 0x0100, nullptr, 0) != 0) { // GetFile has failed cllmSrc.Free(); continue; } // Successful completion break; } // Lock the memory for reading bool bGetFileSuccess = true; LPBYTE pbtSrc = nullptr; DWORD dwSrcSize; if (cllmSrc.GetHandle() != nullptr) { // Successful GetFile() pbtSrc = (LPBYTE) cllmSrc.Lock(); dwSrcSize = cllmSrc.GetSize(); } else { // GetFile() has failed bGetFileSuccess = false; // Memory allocation dwSrcSize = pclArc->GetOpenFileInfo()->sizeCmp; cllmSrc.Alloc(LHND, dwSrcSize); pbtSrc = (LPBYTE) cllmSrc.Lock(); // Reading pclArc->Read(pbtSrc, dwSrcSize); pclArc->SeekCur(-(INT64)dwSrcSize); } // Memory Input - GetPicture() for (auto& pstsiTarget : m_stsiMain) { if (pstsiTarget.bValidity == 0) { // Invalid Susie plugin continue; } // Get IsSupported() IsSupportedProc IsSupported = reinterpret_cast<IsSupportedProc>(pstsiTarget.cllPlugin.GetProcAddress(_T("IsSupported"))); if (IsSupported == nullptr) { // IsSupported() function is not implemented continue; } // Copy file path // Plugin fails if the full path does not exist char szPathToFile[MAX_PATH]; strcpy(szPathToFile, clsPathToArc.GetDirPath()); PathAppendA(szPathToFile, clsFileName); // Needs 2KB BYTE abtSrcHeader[2048]; DWORD dwCopySize = (dwSrcSize <= sizeof(abtSrcHeader)) ? dwSrcSize : sizeof(abtSrcHeader); ZeroMemory(abtSrcHeader, sizeof(abtSrcHeader)); memcpy(abtSrcHeader, pbtSrc, dwCopySize); // Call IsSupported() if (!IsSupported(szPathToFile, (DWORD) abtSrcHeader)) { // File is not supported continue; } // Get GetPicture() GetPictureProc GetPicture = reinterpret_cast<GetPictureProc>(pstsiTarget.cllPlugin.GetProcAddress(_T("GetPicture"))); if (GetPicture == nullptr) { // GetPicture() function is not implemented continue; } YCLocalMemory cllmBitmapInfo; YCLocalMemory cllmBitmapData; // Call GetPicture() if (GetPicture((LPSTR)pbtSrc, dwSrcSize, 0x0001, &cllmBitmapInfo.GetHandle(), &cllmBitmapData.GetHandle(), nullptr, 0) != 0) { // GetPicture() has failed continue; } // Get image info LPBITMAPINFO pBMPInfo = (LPBITMAPINFO) cllmBitmapInfo.Lock(); LPBYTE pHBm = (LPBYTE) cllmBitmapData.Lock(); size_t HBmSize = cllmBitmapData.GetSize(); // Image output CImage image; image.Init(pclArc, pBMPInfo->bmiHeader.biWidth, pBMPInfo->bmiHeader.biHeight, pBMPInfo->bmiHeader.biBitCount, (BYTE*) pBMPInfo->bmiColors); image.Write(pHBm, HBmSize); image.Close(); // Exit cllmBitmapInfo.Free(); cllmBitmapData.Free(); cllmSrc.Free(); return true; } // Exit if the file could not be obtained in the function GetFile() if (!bGetFileSuccess) { cllmSrc.Free(); return false; } // Output file obtained from GetFile() pclArc->OpenFile(); pclArc->WriteFile(pbtSrc, dwSrcSize, pclArc->GetOpenFileInfo()->sizeCmp); pclArc->CloseFile(); // Exit (Free resources) cllmSrc.Free(); return true; }