void CPPageFileInfoRes::OnSaveAs() { int i = m_list.GetSelectionMark(); if (i < 0) { return; } CDSMResource& r = m_res.GetAt((POSITION)m_list.GetItemData(i)); CFileDialog fd(FALSE, nullptr, CString(r.name), OFN_EXPLORER | OFN_ENABLESIZING | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_NOCHANGEDIR, _T("All files|*.*||"), this, 0); if (fd.DoModal() == IDOK) { FILE* f = nullptr; if (!_tfopen_s(&f, fd.GetPathName(), _T("wb"))) { fwrite(r.data.GetData(), 1, r.data.GetCount(), f); fclose(f); } } }
void DebugPrint(CString cstr) { static int flag = TRUE; static TCHAR file[MAX_PATH]; static DWORD first = GetTickCount(); CString output; output.Format(_T("%08d "), GetTickCount() - first); output += cstr; output.Append(_T("\n")); output.Replace(_T("\r"), _T("")); if(flag) { TCHAR* ptrEnd; ::GetModuleFileName(NULL, file, MAX_PATH); if((ptrEnd = _tcsrchr(file, '.')) != NULL ) { *ptrEnd = '\0'; _tcscat_s(file, MAX_PATH, _T(".log")); } DeleteFile(file); flag = FALSE; } if(debugMode == DEBUG_MODE_NONE) { return ; } FILE *fp; _tfopen_s(&fp, file, _T("ac")); _ftprintf(fp, _T("%s"), (LPCTSTR)output); fflush(fp); fclose(fp); if(debugMode == DEBUG_MODE_MESSAGE) { AfxMessageBox(output); } }
//============================================================================== // Brief : コンストラクタ // Return : : なし // Arg : const TCHAR* pNameFile : ファイル名 //============================================================================== CFile::CFile( const TCHAR* pNameFile ) { // メンバ変数の初期化 m_pFile = nullptr; m_pBuffer = nullptr; m_cursor = 0; // ファイルを開く _tfopen_s( &m_pFile, pNameFile, "rb" ); if( m_pFile == nullptr ) { ThrowExceptionFunction(); } // ヘッダの読み取り char aBufferHeader[ 32 ] = { 0 }; fread( &aBufferHeader[ 0 ], sizeof( char ), 32, m_pFile ); // ヘッダ情報の格納 memcpy( &m_type[ 0 ], &aBufferHeader[ 0x00 ], sizeof( char ) * 4 ); m_type[ 4 ] = '\0'; memcpy( &m_size, &aBufferHeader[ 0x04 ], sizeof( unsigned long ) ); memcpy( &m_version, &aBufferHeader[ 0x08 ], sizeof( unsigned long ) ); memcpy( &m_form, &aBufferHeader[ 0x0C ], sizeof( unsigned long ) ); memcpy( &m_code, &aBufferHeader[ 0x10 ], sizeof( unsigned short ) ); memcpy( &m_compress, &aBufferHeader[ 0x12 ], sizeof( unsigned short ) ); // バッファ格納領域の確保 m_pBuffer = new char[ m_size ]; if( m_pBuffer == nullptr ) { this->~CFile(); ThrowExceptionBadAllocation(); } // ファイルの読み込み if( fread( &m_pBuffer[ 0 ], sizeof( char ), m_size, m_pFile ) != m_size ) { ThrowExceptionFunction(); } }
bool PreviousImagesManager::LoadFileBytes(const CString& cszFilename, std::vector<BYTE>& vecJpegData) { FILE* fd = nullptr; if (0 != _tfopen_s(&fd, cszFilename, _T("rb")) || fd == nullptr) return false; std::shared_ptr<FILE> spFd(fd, fclose); //std::unique_ptr<FILE> upFd(fd, fclose); fseek(fd, 0, SEEK_END); long lLen = ftell(fd); fseek(fd, 0, SEEK_SET); vecJpegData.resize(static_cast<size_t>(lLen)); size_t uiDataRead = fread(vecJpegData.data(), 1, lLen, fd); if (uiDataRead != static_cast<size_t>(lLen)) return false; // error during loading; file just changed its size return true; }
BOOL CoExtender::SaveConfig() { BOOL fOk = FALSE; FILE *pFile; if( 0 == _tfopen_s( &pFile, m_sConfigFile.GetBuffer(0), _T("w") ) ) { if( 1 == fwrite( m_pConfigBuf, m_uiConfigLen, 1, pFile ) ) { fOk = TRUE; } else { ASSERT( FALSE ); } fclose( pFile ); } return fOk; }
// ヘッダを見て PNG 画像かどうか(一応)チェック BOOL isPng(LPCTSTR fileName) { unsigned char pngHead[] = { 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A }; unsigned char readHead[8]; FILE *fp = NULL; if (0 != _tfopen_s(&fp, fileName, _T("rb")) || 8 != fread(readHead, 1, 8, fp)) { // ファイルが読めない return FALSE; } fclose(fp); // compare for(unsigned int i=0;i<8;i++) if(pngHead[i] != readHead[i]) return FALSE; return TRUE; }
// Open the error file void CNetList::createErrorFile( const TCHAR *filename ) { // Open the filename for the results m_err_filename = filename; int brk = m_err_filename.ReverseFind('\\'); if (brk) { m_err_filename = m_err_filename.Left( brk ); } m_err_filename += _T(".txt"); errno_t err; err = _tfopen_s(&m_err_file, m_err_filename,_T("w")); m_errors = 0; if (!m_err_file) { Message(IDS_CANNOTOPEN); return; } }
int CFastStringArray::AddListFromFile(LPCTSTR fname) { FILE* fp; errno_t err = _tfopen_s(&fp, fname, _T("rt")); if(err) return 0; TCHAR buf[4096]; for(;;){ TCHAR* p = _fgetts(buf, 4096, fp); if(!p) break; int len = _tcslen(p); for(int i=0;i<len;i++){ if(p[i]==_T('\n') || p[i]==_T('\r')){ p[i] = _T('\0'); break; } } if(_tcslen(p)) Add(p); } fclose(fp); return m_count-1; }
bool CMainWindow::SaveFile(LPCTSTR filename) { FILE *fp = NULL; _tfopen_s(&fp, filename, _T("w+b")); if (fp) { int len = SendEditor(SCI_GETTEXT, 0, 0); char * data = new char[len+1]; SendEditor(SCI_GETTEXT, len, (LPARAM)data); fwrite(data, sizeof(char), len-1, fp); fclose(fp); } else { return false; } SendEditor(SCI_SETSAVEPOINT); ::ShowWindow(m_hWndEdit, SW_SHOW); return true; }
int CSmtpClient::Base64EncodeAttachment(CString sFileName, std::string& sEncodedFileData) { strconv_t strconv; int uFileSize = 0; BYTE* uchFileData = NULL; struct _stat st; int nResult = _tstat(sFileName, &st); if(nResult != 0) return 1; // File not found. // Allocate buffer of file size uFileSize = st.st_size; uchFileData = new BYTE[uFileSize]; // Read file data to buffer. FILE* f = NULL; #if _MSC_VER<1400 f = _tfopen(sFileName, _T("rb")); #else /*errno_t err = */_tfopen_s(&f, sFileName, _T("rb")); #endif if(!f || fread(uchFileData, uFileSize, 1, f)!=1) { delete [] uchFileData; uchFileData = NULL; return 2; // Coudln't read file data. } fclose(f); sEncodedFileData = base64_encode(uchFileData, uFileSize); delete [] uchFileData; // OK. return 0; }
void CLanguagesDlg::_LoadLanguage(LPCTSTR szLang) { CPrivateConfigEx cConfig(TRUE); if(_tcscmp(szLang, _T("English")) != 0) { std_string strFile = SU_DriveLetterToUpper(Executable::instance().getPathOnly()); strFile += PWM_DIR_LANGUAGES; strFile += _T("\\"); strFile += szLang; strFile += _T(".lng"); FILE* fp = NULL; _tfopen_s(&fp, strFile.c_str(), _T("rb")); ASSERT(fp != NULL); if(fp == NULL) { MessageBox(TRL("Language file cannot be opened!"), TRL("Loading error"), MB_OK | MB_ICONWARNING); return; } fclose(fp); if(cConfig.Set(PWMKEY_LANG, szLang) == FALSE) { MessageBox(TRL("Language file cannot be activated!"), TRL("Loading error"), MB_OK | MB_ICONWARNING); return; } } else cConfig.Set(PWMKEY_LANG, _T("Standard")); CString str = TRL("The selected language has been activated. KeePass must be restarted in order to load the language."); str += _T("\r\n\r\n"); str += TRL("Do you wish to restart KeePass now?"); int iResult = CVistaTaskDialog::ShowMessageBox(this->m_hWnd, TRL("Restart KeePass?"), str, MTDI_QUESTION, TRL("&Yes"), IDOK, TRL("&No"), IDCANCEL); if(iResult < 0) iResult = MessageBox(str, TRL("Restart KeePass?"), MB_YESNO | MB_ICONQUESTION); if((iResult == IDOK) || (iResult == IDYES)) CDialog::OnOK(); }
bool ReadFromFile(LPCTSTR strFilename,void **data,int *data_size) { FILE *file=NULL; if(strFilename) { _tfopen_s(&file, strFilename, _T("rb")); } // fail if the file couldn't be opened if(file==NULL) { //Log("Couldn't load \"%s\".",strFilename); return false; } fread(data_size,sizeof(int),1,file); *data=malloc(*data_size); fread(*data,*data_size,1,file); fclose(file); return true; }
PUBLIC LPTSTR read_svg(LPCTSTR file_path){ FILE *fp = NULL; int file_length = 0; LPSTR buf = NULL; LPTSTR svg_content = NULL; //现在假设SVG文件是以ANSI格式编码 _tfopen_s(&fp, file_path, _T("rb")); if(!fp){ MessageBox(NULL, _T("Error occurred while reading file!"), _T("Message"), MB_OK|MB_ICONEXCLAMATION); return NULL; } fseek(fp, 0, SEEK_END); file_length = ftell(fp); rewind(fp); buf = (LPSTR)malloc(sizeof(LPSTR)*(file_length + 1)); fread(buf,sizeof(char),file_length,fp); buf[file_length] = 0; fclose(fp); svg_content = utf8_to_unicode(buf); free(buf); return svg_content; }
//REWRITE: this is inefficient. We better read and parse file once bool getConfigValue(TCHAR* basedir, TCHAR* lookupKey, TCHAR* outValue, int buf_size) { TCHAR config[LAUNCHER_MAXPATH] = {0}; TCHAR buffer[LAUNCHER_MAXPATH*2]; TCHAR *value; FILE *fp; *outValue = 0; if (!getFileInPackage(basedir, CONFIG_FILE, config, LAUNCHER_MAXPATH)) { showError(config, _T("Configuration file is not found!")); return false; } //scan file for the key errno_t err = _tfopen_s(&fp, config, _T("r")); if (err) { return false; } while (_fgetts(buffer, LAUNCHER_MAXPATH*2, fp)) { value = _tcschr(buffer, '='); if (value != NULL) { //end key on the '=', value will point to the value string *value = 0; value++; if (!_tcscmp(buffer, lookupKey)) { //found it fclose(fp); strip_endofline(value); _tcscpy_s(outValue, buf_size, value); return true; } } } fclose(fp); return false; }
void WriteLog(const TCHAR * format, ...) { FILE * pFile; va_list va_alist; if (!format) { return; } logbuf[0] = 0; va_start (va_alist, format); _vsntprintf_s(logbuf, _countof(logbuf), _countof(logbuf) - 1, format, va_alist); va_end (va_alist); if (_tfopen_s(&pFile,scannerLog,L"a") == NULL) { _fputts(logbuf,pFile); fclose(pFile); } }
/** * \brief */ bool DbConfig::LoadFromFolder(const CPath& cfgFileFolder) { SetDefaults(); CPath cfgFile(cfgFileFolder); cfgFile += cPluginCfgFileName; if (!cfgFile.FileExists()) return false; FILE* fp; _tfopen_s(&fp, cfgFile.C_str(), _T("rt")); if (fp == NULL) return false; bool success = true; TCHAR line[8192]; while (_fgetts(line, _countof(line), fp)) { // Comment or empty line if (line[0] == _T('#') || line[0] == _T('\n')) continue; // Strip newline from the end of the line line[_tcslen(line) - 1] = 0; if (!ReadOption(line)) { success = false; SetDefaults(); break; } } fclose(fp); return success; }
/* Counts the selected file if the contents have the user specified word. The file name is listed in standard output */ VOID WordInFile(LPWIN32_FIND_DATA fileData, TCHAR *path, VOID *ctx) { MyContext *stats = (MyContext*) ctx; TCHAR buffer[MAX_PATH]; CHAR line[256]; _stprintf_s(buffer, _T("%s%s"), path, fileData->cFileName); FILE *file; int error = _tfopen_s(&file, buffer, _T("r")); if (error != 0) return; while (fgets(line, 256, file) != NULL) { if (strstr(line, stats->match) != NULL) { stats->countFiles++; // show the name in standard output _tprintf(_T(" %s%s\n"), path, fileData->cFileName); break; } } fclose(file); }
bool VdfFlow::Init(const TCHAR* arcname, VdfsIndex::FileInfoPtr& fileinfo) { if(CurrentFileInfo) return false; if(Archive) { CurrentFileInfo = fileinfo; Name = fileinfo->Name; return true; } FILE* archive = NULL; if(!_tfopen_s(&archive, arcname, _T("rb"))) { CurrentFileInfo = fileinfo; Archive = archive; Name = fileinfo->Name; return true; } return false; }
char *CPwImport::FileToMemory(const TCHAR *pszFile, unsigned long *pFileSize) { ASSERT(pszFile != NULL); if(pszFile == NULL) return NULL; if(_tcslen(pszFile) == 0) return NULL; FILE *fp = NULL; _tfopen_s(&fp, pszFile, _T("rb")); if(fp == NULL) return NULL; fseek(fp, 0, SEEK_END); unsigned long uFileSize = ftell(fp); fseek(fp, 0, SEEK_SET); char *pData = new char[uFileSize + 32]; if(pData == NULL) { fclose(fp); fp = NULL; return NULL; } memset(&pData[uFileSize], 0, 32); fread(pData, 1, uFileSize, fp); fclose(fp); fp = NULL; if(pFileSize != NULL) *pFileSize = uFileSize; // Store file size return pData; }
bool CMainWindow::LoadFile(LPCTSTR filename) { InitEditor(); FILE *fp = NULL; _tfopen_s(&fp, filename, L"rb"); if (!fp) return false; //SetTitle(); char data[4096] = { 0 }; size_t lenFile = fread(data, 1, sizeof(data), fp); bool bUTF8 = IsUTF8(data, lenFile); while (lenFile > 0) { SendEditor(SCI_ADDTEXT, lenFile, reinterpret_cast<LPARAM>(static_cast<char *>(data))); lenFile = fread(data, 1, sizeof(data), fp); } fclose(fp); SetupWindow(bUTF8); m_filename = filename; return true; }
/** * Main entry point for MD5Index. * * @author Jacob Hammack * */ int _tmain(int argc, TCHAR *argv[]) { errno_t err; printf("MD5Index v1.0\nCopyright 2008 Jacob Hammack\nhttp://www.hammackj.com\n\n"); err = _tfopen_s(&fp, TEXT("MD5Index.dat"), TEXT("w")); if((err != 0) || (fp == NULL)) { return -1; } printf("[*] Building Index....\n"); EnumerateFiles(); printf("[!] Indexing Complete\n"); return 0; }
static void Hashit(TCHAR *filename) { FILE *FileToHash; MD5_CTX MDContext; int bytes; int i; unsigned char data[1024]; unsigned char hash[32]; unsigned char tmphash[10]; errno_t err; err = _tfopen_s(&FileToHash, filename, TEXT("rb")); if((err != 0) || (FileToHash == NULL)) { return; } MD5Init(&MDContext); while((bytes = fread(data, 1, 1024, FileToHash)) != 0) { MD5Update(&MDContext, data, bytes); } fclose(FileToHash); MD5Final(&MDContext); sprintf(hash, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", MDContext.digest[0], MDContext.digest[1], MDContext.digest[2], MDContext.digest[3], MDContext.digest[4], MDContext.digest[5], MDContext.digest[6], MDContext.digest[7], MDContext.digest[8], MDContext.digest[9], MDContext.digest[10], MDContext.digest[11], MDContext.digest[12], MDContext.digest[13], MDContext.digest[14], MDContext.digest[15]); fprintf(fp,"%s*", hash); _ftprintf(fp, "%s\n", filename); }
std::wstring FindCongfigFile(const std::wstring& configName) { TCHAR szBuf[1024]; GetModuleFileName(nullptr, szBuf, 1024); std::wstring path(szBuf); for (int i = 0; i < 3; ++i) { size_t pos = path.rfind(L'\\'); if (pos != std::wstring::npos) { path.erase(pos); std::wstring filePath(path+ L"\\" + std::wstring(configName)); FILE* fp = nullptr; _tfopen_s(&fp, filePath.c_str(), L"rb"); if (fp) { fclose(fp); return filePath; } } } return L""; }
bool CTortoiseMergeApp::TrySavePatchFromClipboard(std::wstring& resultFile) { resultFile.clear(); UINT cFormat = RegisterClipboardFormat(_T("TSVN_UNIFIEDDIFF")); if (cFormat == 0) return false; if (OpenClipboard(NULL) == 0) return false; HGLOBAL hglb = GetClipboardData(cFormat); LPCSTR lpstr = (LPCSTR)GlobalLock(hglb); DWORD len = GetTempPath(0, NULL); std::unique_ptr<TCHAR[]> path(new TCHAR[len+1]); std::unique_ptr<TCHAR[]> tempF(new TCHAR[len+100]); GetTempPath (len+1, path.get()); GetTempFileName (path.get(), _T("tsm"), 0, tempF.get()); std::wstring sTempFile = std::wstring(tempF.get()); FILE* outFile = 0; _tfopen_s(&outFile, sTempFile.c_str(), _T("wb")); if (outFile != 0) { size_t patchlen = strlen(lpstr); size_t size = fwrite(lpstr, sizeof(char), patchlen, outFile); if (size == patchlen) resultFile = sTempFile; fclose(outFile); } GlobalUnlock(hglb); CloseClipboard(); return !resultFile.empty(); }
int CCrashInfoReader::ParseCrashDescription(CString sFileName, BOOL bParseFileItems, CErrorReportInfo& eri) { strconv_t strconv; FILE* f = NULL; #if _MSC_VER<1400 f = _tfopen(sFileName, _T("rb")); #else _tfopen_s(&f, sFileName, _T("rb")); #endif if(f==NULL) return 1; TiXmlDocument doc; bool bOpen = doc.LoadFile(f); if(!bOpen) return 1; TiXmlHandle hRoot = doc.FirstChild("CrashRpt"); if(hRoot.ToElement()==NULL) { fclose(f); return 1; } { TiXmlHandle hCrashGUID = hRoot.FirstChild("CrashGUID"); if(hCrashGUID.ToElement()!=NULL) { if(hCrashGUID.FirstChild().ToText()!=NULL) { const char* szCrashGUID = hCrashGUID.FirstChild().ToText()->Value(); if(szCrashGUID!=NULL) eri.m_sCrashGUID = strconv.utf82t(szCrashGUID); } } } { TiXmlHandle hAppName = hRoot.FirstChild("AppName"); if(hAppName.ToElement()!=NULL) { const char* szAppName = hAppName.FirstChild().ToText()->Value(); if(szAppName!=NULL) eri.m_sAppName = strconv.utf82t(szAppName); } } { TiXmlHandle hAppVersion = hRoot.FirstChild("AppVersion"); if(hAppVersion.ToElement()!=NULL) { TiXmlText* pText = hAppVersion.FirstChild().ToText(); if(pText!=NULL) { const char* szAppVersion = pText->Value(); if(szAppVersion!=NULL) eri.m_sAppVersion = strconv.utf82t(szAppVersion); } } } { TiXmlHandle hImageName = hRoot.FirstChild("ImageName"); if(hImageName.ToElement()!=NULL) { TiXmlText* pText = hImageName.FirstChild().ToText(); if(pText!=NULL) { const char* szImageName = pText->Value(); if(szImageName!=NULL) eri.m_sImageName = strconv.utf82t(szImageName); } } } { TiXmlHandle hSystemTimeUTC = hRoot.FirstChild("SystemTimeUTC"); if(hSystemTimeUTC.ToElement()!=NULL) { TiXmlText* pText = hSystemTimeUTC.FirstChild().ToText(); if(pText!=NULL) { const char* szSystemTimeUTC = pText->Value(); if(szSystemTimeUTC!=NULL) eri.m_sSystemTimeUTC = strconv.utf82t(szSystemTimeUTC); } } } if(bParseFileItems) { // Get directory name CString sReportDir = sFileName; int pos = sFileName.ReverseFind('\\'); if(pos>=0) sReportDir = sFileName.Left(pos); if(sReportDir.Right(1)!=_T("\\")) sReportDir += _T("\\"); TiXmlHandle fl = hRoot.FirstChild("FileList"); if(fl.ToElement()==0) { fclose(f); return 1; } TiXmlHandle fi = fl.FirstChild("FileItem"); while(fi.ToElement()!=0) { const char* pszDestFile = fi.ToElement()->Attribute("name"); const char* pszDesc = fi.ToElement()->Attribute("description"); const char* pszOptional = fi.ToElement()->Attribute("optional"); if(pszDestFile!=NULL) { CString sDestFile = strconv.utf82t(pszDestFile); ERIFileItem item; item.m_sDestFile = sDestFile; item.m_sSrcFile = sReportDir + sDestFile; if(pszDesc) item.m_sDesc = strconv.utf82t(pszDesc); item.m_bMakeCopy = FALSE; if(pszOptional && strcmp(pszOptional, "1")==0) item.m_bAllowDelete = true; // Check that file really exists DWORD dwAttrs = GetFileAttributes(item.m_sSrcFile); if(dwAttrs!=INVALID_FILE_ATTRIBUTES && (dwAttrs&FILE_ATTRIBUTE_DIRECTORY)==0) { eri.m_FileItems[sDestFile] = item; } } fi = fi.ToElement()->NextSibling("FileItem"); } } fclose(f); return 0; }
BOOL CCrashInfoReader::RemoveFilesFromCrashReport(int nReport, std::vector<CString> FilesToRemove) { strconv_t strconv; TiXmlDocument doc; CString sFileName = m_Reports[nReport].m_sErrorReportDirName + _T("\\crashrpt.xml"); FILE* f = NULL; #if _MSC_VER<1400 f = _tfopen(sFileName, _T("rb")); #else _tfopen_s(&f, sFileName, _T("rb")); #endif if(f==NULL) { return FALSE; } bool bLoad = doc.LoadFile(f); fclose(f); if(!bLoad) { return FALSE; } TiXmlNode* root = doc.FirstChild("CrashRpt"); if(!root) { return FALSE; } TiXmlHandle hFileItems = root->FirstChild("FileList"); if(hFileItems.ToElement()==NULL) { hFileItems = new TiXmlElement("FileList"); root->LinkEndChild(hFileItems.ToNode()); } unsigned i; for(i=0; i<FilesToRemove.size(); i++) { std::map<CString, ERIFileItem>::iterator it = m_Reports[nReport].m_FileItems.find(FilesToRemove[i]); if(it==m_Reports[nReport].m_FileItems.end()) continue; // Such file item name does not exists, skip strconv_t strconv; TiXmlHandle hElem = hFileItems.ToElement()->FirstChild(strconv.t2a(FilesToRemove[i])); if(hElem.ToElement()!=NULL) hFileItems.ToElement()->RemoveChild(hElem.ToElement()); if(it->second.m_bMakeCopy) { Utility::RecycleFile(it->second.m_sSrcFile, TRUE); } m_Reports[nReport].m_FileItems.erase(it); } #if _MSC_VER<1400 f = _tfopen(sFileName, _T("w")); #else _tfopen_s(&f, sFileName, _T("w")); #endif if(f==NULL) return FALSE; bool bSave = doc.SaveFile(f); if(!bSave) return FALSE; fclose(f); return TRUE; }
BOOL CCrashInfoReader::AddFilesToCrashReport(int nReport, std::vector<ERIFileItem> FilesToAdd) { strconv_t strconv; TiXmlDocument doc; CString sFileName = m_Reports[nReport].m_sErrorReportDirName + _T("\\crashrpt.xml"); FILE* f = NULL; #if _MSC_VER<1400 f = _tfopen(sFileName, _T("rb")); #else _tfopen_s(&f, sFileName, _T("rb")); #endif if(f==NULL) { return FALSE; } bool bLoad = doc.LoadFile(f); fclose(f); if(!bLoad) { return FALSE; } TiXmlNode* root = doc.FirstChild("CrashRpt"); if(!root) { return FALSE; } TiXmlHandle hFileItems = root->FirstChild("FileList"); if(hFileItems.ToElement()==NULL) { hFileItems = new TiXmlElement("FileList"); root->LinkEndChild(hFileItems.ToNode()); } unsigned i; for(i=0; i<FilesToAdd.size(); i++) { if(m_Reports[0].m_FileItems.find(FilesToAdd[i].m_sDestFile)!=m_Reports[0].m_FileItems.end()) continue; // Such file item already exists, skip TiXmlHandle hFileItem = new TiXmlElement("FileItem"); hFileItem.ToElement()->SetAttribute("name", strconv.t2utf8(FilesToAdd[i].m_sDestFile)); hFileItem.ToElement()->SetAttribute("description", strconv.t2utf8(FilesToAdd[i].m_sDesc)); if(FilesToAdd[i].m_bAllowDelete) hFileItem.ToElement()->SetAttribute("optional", "1"); hFileItems.ToElement()->LinkEndChild(hFileItem.ToNode()); m_Reports[nReport].m_FileItems[FilesToAdd[i].m_sDestFile] = FilesToAdd[i]; if(FilesToAdd[i].m_bMakeCopy) { CString sDestPath = m_Reports[nReport].m_sErrorReportDirName + _T("\\") + FilesToAdd[i].m_sDestFile; CopyFile(FilesToAdd[i].m_sSrcFile, sDestPath, TRUE); m_Reports[nReport].m_FileItems[FilesToAdd[i].m_sDestFile].m_sSrcFile = sDestPath; } } #if _MSC_VER<1400 f = _tfopen(sFileName, _T("w")); #else _tfopen_s(&f, sFileName, _T("w")); #endif if(f==NULL) return FALSE; bool bSave = doc.SaveFile(f); if(!bSave) return FALSE; fclose(f); return TRUE; }
BOOL CCrashInfoReader::AddUserInfoToCrashDescriptionXML(CString sEmail, CString sDesc) { strconv_t strconv; TiXmlDocument doc; CString sFileName = m_Reports[0].m_sErrorReportDirName + _T("\\crashrpt.xml"); FILE* f = NULL; #if _MSC_VER<1400 f = _tfopen(sFileName, _T("rb")); #else _tfopen_s(&f, sFileName, _T("rb")); #endif if(f==NULL) return FALSE; bool bLoad = doc.LoadFile(f); fclose(f); if(!bLoad) { return FALSE; } TiXmlNode* root = doc.FirstChild("CrashRpt"); if(!root) { return FALSE; } // Write user e-mail TiXmlHandle hEmail = NULL; hEmail = root->FirstChild("UserEmail"); if(hEmail.ToElement()==NULL) { hEmail = new TiXmlElement("UserEmail"); root->LinkEndChild(hEmail.ToElement()); } TiXmlText* email_text = NULL; if(hEmail.FirstChild().ToText()!=NULL) { email_text = hEmail.FirstChild().ToText(); email_text->SetValue(strconv.w2utf8(sEmail)); } else { email_text = new TiXmlText(strconv.t2utf8(sEmail)); hEmail.ToElement()->LinkEndChild(email_text); } // Write problem description TiXmlHandle hDesc = NULL; hDesc = root->FirstChild("ProblemDescription"); if(hDesc.ToElement()==NULL) { hDesc = new TiXmlElement("ProblemDescription"); root->LinkEndChild(hDesc.ToElement()); } TiXmlText* desc_text = NULL; if(hDesc.FirstChild().ToText()!=NULL) { desc_text = hDesc.FirstChild().ToText(); desc_text->SetValue(strconv.w2utf8(sDesc)); } else { desc_text = new TiXmlText(strconv.t2utf8(sDesc)); hDesc.ToElement()->LinkEndChild(desc_text); } #if _MSC_VER<1400 f = _tfopen(sFileName, _T("w")); #else _tfopen_s(&f, sFileName, _T("w")); #endif if(f==NULL) return FALSE; bool bSave = doc.SaveFile(f); fclose(f); if(!bSave) return FALSE; return TRUE; }
BOOL CScreenCapture::PngInit(int nWidth, int nHeight, BOOL bGrayscale, CString sFileName) { m_fp = NULL; m_png_ptr = NULL; m_info_ptr = NULL; #if _MSC_VER>=1400 _tfopen_s(&m_fp, sFileName.GetBuffer(0), _T("wb")); #else m_fp = _tfopen(sFileName.GetBuffer(0), _T("wb")); #endif if (!m_fp) { return FALSE; } m_png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, (png_voidp)NULL, NULL, NULL); if (!m_png_ptr) return FALSE; m_info_ptr = png_create_info_struct(m_png_ptr); if (!m_info_ptr) { png_destroy_write_struct(&m_png_ptr, (png_infopp)NULL); return FALSE; } /* Error handler*/ if (setjmp(png_jmpbuf(m_png_ptr))) { png_destroy_write_struct(&m_png_ptr, &m_info_ptr); fclose(m_fp); return FALSE; } png_init_io(m_png_ptr, m_fp); /* set the zlib compression level */ png_set_compression_level(m_png_ptr, Z_BEST_COMPRESSION); /* set other zlib parameters */ png_set_compression_mem_level(m_png_ptr, 8); png_set_compression_strategy(m_png_ptr, Z_DEFAULT_STRATEGY); png_set_compression_window_bits(m_png_ptr, 15); png_set_compression_method(m_png_ptr, 8); png_set_compression_buffer_size(m_png_ptr, 8192); png_set_IHDR( m_png_ptr, m_info_ptr, nWidth, //width, nHeight, //height, 8, // bit_depth bGrayscale?PNG_COLOR_TYPE_GRAY:PNG_COLOR_TYPE_RGB, // color_type PNG_INTERLACE_NONE, // interlace_type PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); png_set_bgr(m_png_ptr); /* write the file information */ png_write_info(m_png_ptr, m_info_ptr); return TRUE; }
static int HEXDump_FindCallBack(FindData &fd, HDFDCB_Data *pUserParam) { if (!fd.fileMatched) return 0; int argc(pUserParam->argc); TCHAR **argv(pUserParam->argv); BinaryFind &bf(pUserParam->bf); FILE *fp = NULL; _tfopen_s(&fp, fd.fullPath.c_str(), _T("rb")); if (fp != NULL) { int nMatches(0); bf.SetFindBuffer(); _tprintf(_T("%s\n"), fd.fullPath.c_str()); pUserParam->nFiles++; Progress prog; const TCHAR *argStr = FindArgValue(argc, argv, _T("-o=")); if (argStr != NULL) { long long offset(StringUtils::getLLfromStr(argStr)); _fseeki64(fp, offset, offset >= 0 ? SEEK_SET : SEEK_END); } long long fileOffset(_ftelli64(fp)); long long sizeToRead(fd.GetFileSize()); const long long fileSize(sizeToRead); argStr = FindArgValue(argc, argv, _T("-s=")); if (argStr != NULL) { long long szRead = StringUtils::getLLfromStr(argStr); if (fileOffset + szRead > sizeToRead) sizeToRead = sizeToRead - fileOffset; else sizeToRead = szRead; } size_t findDumpSize(0), findDumpOffset(-16); if (bf.HasFindPattern()) { argStr = FindArgValue(argc, argv, _T("-d")); if (argStr != NULL) { if (*argStr == '=') { findDumpSize = StringUtils::getLLfromStr(argStr + 1); STR_SKIP_TILL_CHAR(argStr, ';'); if (*argStr) findDumpOffset = StringUtils::getLLfromStr(argStr + 1); } if (findDumpSize <= 0) findDumpSize = 48; } } prog.SetTask(sizeToRead); BinaryData buffer(NULL, 4 * 1024 * 1024); while (sizeToRead > 0) { buffer.ReadFromFile(fp); if (buffer.DataSize() <= 0) break; sizeToRead -= buffer.DataSize(); if (bf.HasFindPattern()) { bf.SetFindBuffer(buffer); while (true) { long long findPos = bf.FindNext(); if (findPos >= 0) { _tprintf(_T("%08llX=-%08llX\n"), fileOffset + findPos, fileSize - (fileOffset + findPos)); if (findDumpSize > 0) { const long long curPos(_ftelli64(fp)); long long newPos(fileOffset + findPos + findDumpOffset); if (newPos < 0) newPos = 0; newPos &= ~0xf; BinaryData bd(NULL, findDumpSize); bd.ReadFromFile(fp, 0, newPos); HexDump(bd, newPos); _fseeki64(fp, curPos, SEEK_SET); } ++nMatches; } else break; } } else fileOffset = HexDump(buffer, fileOffset); if (prog.UpdateProgress(prog.GetCurrentDone() + buffer.DataSize())) _tprintf(_T("\r%02.02f%%\r"), prog.GetCurrentPercentageDone()); } _tprintf(_T("\r \r")); fclose(fp); if (nMatches > 0) { pUserParam->nFound++; if (nMatches > pUserParam->nMaxMatchPerFile) pUserParam->nMaxMatchPerFile = nMatches; if (nMatches > 1) _tprintf(_T("%d matches\n"), nMatches); } } return 0; }