HANDLE createThreadEvent(char* uuid, char* suffix) { LPSTR event_name = NULL; HANDLE hEvent = NULL; PSECURITY_ATTRIBUTES psa = 0; // Everything having to do with SECURITY_ATTRIBUTES sa = { 0 }; // sa, psa, security is copied DWORD status = 0; // from the previous implementation. psa = isNT() ? &sa : 0; if (isNT()) { sa.nLength = sizeof(sa); status = alloc_own_security_descriptor_NT(&sa.lpSecurityDescriptor); cci_check_error(status); } if (!status) { event_name = allocEventName(uuid, suffix); if (!event_name) status = cci_check_error(ccErrNoMem); } #if 0 cci_debug_printf("%s event_name:%s", __FUNCTION__, event_name); #endif if (!status) { hEvent = CreateEvent(psa, FALSE, FALSE, event_name); if (!hEvent) status = cci_check_error(GetLastError()); } if (!status) ResetEvent(hEvent); if (event_name) free(event_name); if (isNT()) free(sa.lpSecurityDescriptor); return hEvent; }
int sqlite3WinDelete(const char *zFilename){ int cnt = 0; int rc; void *zConverted = convertUtf8Filename(zFilename); if( zConverted==0 ){ return SQLITE_NOMEM; } if( isNT() ){ do{ rc = DeleteFileW(zConverted); }while( rc==0 && GetFileAttributesW(zConverted)!=0xffffffff && cnt++ < MX_DELETION_ATTEMPTS && (Sleep(100), 1) ); }else{ #if OS_WINCE return SQLITE_NOMEM; #else do{ rc = DeleteFileA(zConverted); }while( rc==0 && GetFileAttributesA(zConverted)!=0xffffffff && cnt++ < MX_DELETION_ATTEMPTS && (Sleep(100), 1) ); #endif } sqliteFree(zConverted); TRACE2("DELETE \"%s\"\n", zFilename); return rc!=0 ? SQLITE_OK : SQLITE_IOERR; }
void skipStub(LauncherProperties * props) { if(props->isOnlyStub) { WCHAR * os; props->status = EXIT_CODE_STUB; os = appendStringW(NULL, L"It`s only the launcher stub.\nOS: "); if(is9x()) os = appendStringW(os, L"Windows 9x"); if(isNT()) os = appendStringW(os, L"Windows NT"); if(is2k()) os = appendStringW(os, L"Windows 2000"); if(isXP()) os = appendStringW(os, L"Windows XP"); if(is2003()) os = appendStringW(os, L"Windows 2003"); if(isVista()) os = appendStringW(os, L"Windows Vista"); if(is2008()) os = appendStringW(os, L"Windows 2008"); if(is7()) os = appendStringW(os, L"Windows 7"); if(IsWow64) os = appendStringW(os, L" x64"); showMessageW(props, os , 0); FREE(os); } else { skipLauncherStub(props, STUB_FILL_SIZE); if(!isOK(props)) { writeMessageA(props, OUTPUT_LEVEL_NORMAL, 1, "Error! Can`t process launcher stub", 1); showErrorW(props, INTEGRITY_ERROR_PROP, 1, props->exeName); } } }
/* ** Attempt to open a new file for exclusive access by this process. ** The file will be opened for both reading and writing. To avoid ** a potential security problem, we do not allow the file to have ** previously existed. Nor do we allow the file to be a symbolic ** link. ** ** If delFlag is true, then make arrangements to automatically delete ** the file when it is closed. ** ** On success, write the file handle into *id and return SQLITE_OK. ** ** On failure, return SQLITE_CANTOPEN. ** ** Sometimes if we have just deleted a prior journal file, windows ** will fail to open a new one because there is a "pending delete". ** To work around this bug, we pause for 100 milliseconds and attempt ** a second open after the first one fails. The whole operation only ** fails if both open attempts are unsuccessful. */ int sqlite3WinOpenExclusive(const char *zFilename, OsFile **pId, int delFlag){ winFile f; HANDLE h; DWORD fileflags; void *zConverted = convertUtf8Filename(zFilename); if( zConverted==0 ){ return SQLITE_NOMEM; } assert( *pId == 0 ); fileflags = FILE_FLAG_RANDOM_ACCESS; #if !OS_WINCE if( delFlag ){ fileflags |= FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE; } #endif if( isNT() ){ int cnt = 0; do{ h = CreateFileW((WCHAR*)zConverted, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, fileflags, NULL ); }while( h==INVALID_HANDLE_VALUE && cnt++ < 2 && (Sleep(100), 1) ); }else{ #if OS_WINCE return SQLITE_NOMEM; #else int cnt = 0; do{ h = CreateFileA((char*)zConverted, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, fileflags, NULL ); }while( h==INVALID_HANDLE_VALUE && cnt++ < 2 && (Sleep(100), 1) ); #endif /* OS_WINCE */ } #if OS_WINCE if( delFlag && h!=INVALID_HANDLE_VALUE ){ f.zDeleteOnClose = zConverted; zConverted = 0; } f.hMutex = NULL; #endif sqliteFree(zConverted); if( h==INVALID_HANDLE_VALUE ){ return SQLITE_CANTOPEN; } f.h = h; TRACE3("OPEN EX %d \"%s\"\n", h, zFilename); return allocateWinFile(&f, pId); }
/* ** Convert a UTF-8 filename into whatever form the underlying ** operating system wants filenames in. Space to hold the result ** is obtained from sqliteMalloc and must be freed by the calling ** function. */ static void *convertUtf8Filename(const char *zFilename){ void *zConverted = 0; if( isNT() ){ zConverted = utf8ToUnicode(zFilename); }else{ zConverted = utf8ToMbcs(zFilename); } /* caller will handle out of memory */ return zConverted; }
/* ** Create a temporary file name in zBuf. zBuf must be big enough to ** hold at least SQLITE_TEMPNAME_SIZE characters. */ int sqlite3WinTempFileName(char *zBuf){ static char zChars[] = "abcdefghijklmnopqrstuvwxyz" "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "0123456789"; int i, j; char zTempPath[SQLITE_TEMPNAME_SIZE]; if( sqlite3_temp_directory ){ strncpy(zTempPath, sqlite3_temp_directory, SQLITE_TEMPNAME_SIZE-30); zTempPath[SQLITE_TEMPNAME_SIZE-30] = 0; }else if( isNT() ){ char *zMulti; WCHAR zWidePath[SQLITE_TEMPNAME_SIZE]; GetTempPathW(SQLITE_TEMPNAME_SIZE-30, zWidePath); zMulti = unicodeToUtf8(zWidePath); if( zMulti ){ strncpy(zTempPath, zMulti, SQLITE_TEMPNAME_SIZE-30); zTempPath[SQLITE_TEMPNAME_SIZE-30] = 0; sqliteFree(zMulti); }else{ return SQLITE_NOMEM; } }else{ char *zUtf8; char zMbcsPath[SQLITE_TEMPNAME_SIZE]; GetTempPathA(SQLITE_TEMPNAME_SIZE-30, zMbcsPath); zUtf8 = mbcsToUtf8(zMbcsPath); if( zUtf8 ){ strncpy(zTempPath, zUtf8, SQLITE_TEMPNAME_SIZE-30); zTempPath[SQLITE_TEMPNAME_SIZE-30] = 0; sqliteFree(zUtf8); }else{ return SQLITE_NOMEM; } } for(i=strlen(zTempPath); i>0 && zTempPath[i-1]=='\\'; i--){} zTempPath[i] = 0; for(;;){ sprintf(zBuf, "%s\\"TEMP_FILE_PREFIX, zTempPath); j = strlen(zBuf); sqlite3Randomness(15, &zBuf[j]); for(i=0; i<15; i++, j++){ zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ]; } zBuf[j] = 0; if( !sqlite3OsFileExists(zBuf) ) break; } TRACE2("TEMP FILENAME: %s\n", zBuf); return SQLITE_OK; }
/* ** Return TRUE if the named file exists. */ int sqlite3WinFileExists(const char *zFilename){ int exists = 0; void *zConverted = convertUtf8Filename(zFilename); if( zConverted==0 ){ return SQLITE_NOMEM; } if( isNT() ){ exists = GetFileAttributesW((WCHAR*)zConverted) != 0xffffffff; }else{ #if OS_WINCE return SQLITE_NOMEM; #else exists = GetFileAttributesA((char*)zConverted) != 0xffffffff; #endif } sqliteFree(zConverted); return exists; }
/* ** Attempt to open a new file for read-only access. ** ** On success, write the file handle into *id and return SQLITE_OK. ** ** On failure, return SQLITE_CANTOPEN. */ int sqlite3WinOpenReadOnly(const char *zFilename, OsFile **pId){ winFile f; HANDLE h; void *zConverted = convertUtf8Filename(zFilename); if( zConverted==0 ){ return SQLITE_NOMEM; } assert( *pId==0 ); if( isNT() ){ h = CreateFileW((WCHAR*)zConverted, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS, NULL ); }else{ #if OS_WINCE return SQLITE_NOMEM; #else h = CreateFileA((char*)zConverted, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS, NULL ); #endif } sqliteFree(zConverted); if( h==INVALID_HANDLE_VALUE ){ return SQLITE_CANTOPEN; } f.h = h; #if OS_WINCE f.zDeleteOnClose = 0; f.hMutex = NULL; #endif TRACE3("OPEN RO %d \"%s\"\n", h, zFilename); return allocateWinFile(&f, pId); }
/* ** Attempt to open a file for both reading and writing. If that ** fails, try opening it read-only. If the file does not exist, ** try to create it. ** ** On success, a handle for the open file is written to *id ** and *pReadonly is set to 0 if the file was opened for reading and ** writing or 1 if the file was opened read-only. The function returns ** SQLITE_OK. ** ** On failure, the function returns SQLITE_CANTOPEN and leaves ** *id and *pReadonly unchanged. */ int sqlite3WinOpenReadWrite( const char *zFilename, OsFile **pId, int *pReadonly ){ winFile f; HANDLE h; void *zConverted = convertUtf8Filename(zFilename); if( zConverted==0 ){ return SQLITE_NOMEM; } assert( *pId==0 ); if( isNT() ){ h = CreateFileW((WCHAR*)zConverted, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS, NULL ); if( h==INVALID_HANDLE_VALUE ){ h = CreateFileW((WCHAR*)zConverted, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS, NULL ); if( h==INVALID_HANDLE_VALUE ){ sqliteFree(zConverted); return SQLITE_CANTOPEN; } *pReadonly = 1; }else{ *pReadonly = 0; } #if OS_WINCE if (!winceCreateLock(zFilename, &f)){ CloseHandle(h); sqliteFree(zConverted); return SQLITE_CANTOPEN; } #endif }else{ #if OS_WINCE return SQLITE_NOMEM; #else h = CreateFileA((char*)zConverted, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS, NULL ); if( h==INVALID_HANDLE_VALUE ){ h = CreateFileA((char*)zConverted, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS, NULL ); if( h==INVALID_HANDLE_VALUE ){ sqliteFree(zConverted); return SQLITE_CANTOPEN; } *pReadonly = 1; }else{ *pReadonly = 0; } #endif /* OS_WINCE */ } sqliteFree(zConverted); f.h = h; #if OS_WINCE f.zDeleteOnClose = 0; #endif TRACE3("OPEN R/W %d \"%s\"\n", h, zFilename); return allocateWinFile(&f, pId); }
PDWORD GetProcAddressByHash(WCHAR* dll, ULONGLONG hash) { PPEB Peb; UINT i; PPEB_LDR_DATA Ldr; PLDR_DATA_TABLE_ENTRY Module; PLIST_ENTRY ModuleListEntry, ModuleListHead; /* get PEB */ __asm { mov eax, FS:[0x30]; mov Peb, eax } Ldr = (PPEB_LDR_DATA)Peb->Ldr; ModuleListHead = &Ldr->InMemoryOrderModuleList; ModuleListEntry = ModuleListHead->Flink; while (ModuleListEntry != ModuleListHead) { Module = (PLDR_DATA_TABLE_ENTRY)ModuleListEntry; ModuleListEntry = ModuleListEntry->Flink; if (!wcscmp(Module->FullDllName.Buffer, dll)) break; } DWORD imageBaseAddr = (DWORD)Module->Reserved2[0]; #if DEBUG printf("PEB location : 0x%p\n", Peb); printf("Dll: %ls imageBaseAddr: 0x%p\n", Module->FullDllName.Buffer, Module->Reserved2[0]); #endif // DEBUG PIMAGE_DOS_HEADER hDOS = MapPE_DOS(GetCurrentProcess(), (PVOID)imageBaseAddr); if (!isDOS(hDOS)) return NULL; PIMAGE_NT_HEADERS hNT = MapPE_NT(GetCurrentProcess(), (PVOID)(imageBaseAddr + hDOS->e_lfanew)); if (!isNT(hNT)) return NULL; IMAGE_SECTION_HEADER** hSection = MapPE_SECTIONS( GetCurrentProcess(), (PVOID)(imageBaseAddr + hDOS->e_lfanew + sizeof(IMAGE_NT_HEADERS)), hNT->FileHeader.NumberOfSections); #if DEBUG for (i = 0; i < hNT->FileHeader.NumberOfSections; i++) printf("%s\n", hSection[i]->Name); #endif // DEBUG PIMAGE_EXPORT_DIRECTORY hExport = MapPE_DD_EXPORT(GetCurrentProcess(), (PVOID)(imageBaseAddr + hNT->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress)); EAT hEAT; hEAT.function = (PDWORD)(imageBaseAddr + hExport->AddressOfFunctions); hEAT.name = (PDWORD)(imageBaseAddr + hExport->AddressOfNames); hEAT.ordinal = (PDWORD)(imageBaseAddr + hExport->AddressOfNameOrdinals); #if DEBUG printf("Dll: %s\n", imageBaseAddr + hExport->Name); for (i = 0; i < hExport->NumberOfNames; i++) printf("%s <> 0x%p\n", (char*)(imageBaseAddr + hEAT.name[i]), (PDWORD)(imageBaseAddr + hEAT.function[i])); #endif // DEBUG for (i = 0; i < hExport->NumberOfNames; i++) { if (hashkey((PCHAR)(imageBaseAddr + hEAT.name[i])) == hash) break; } free(hSection); free(hExport); free(hNT); free(hDOS); return (PDWORD)(imageBaseAddr + hEAT.function[i]); }