BOOL read_appkey(LPCWSTR lpappname, /* 区段名 */ LPCWSTR lpkey, /* 键名 */ LPWSTR prefstring, /* 保存值缓冲区 */ DWORD bufsize /* 缓冲区大小 */ ) { DWORD res = 0; LPWSTR lpstring; if ( profile_path[1] != L':' ) { if (!ini_ready(profile_path,MAX_PATH)) { return res; } } lpstring = (LPWSTR)SYS_MALLOC(bufsize); res = GetPrivateProfileStringW(lpappname, lpkey ,L"", lpstring, bufsize, profile_path); if (res == 0 && GetLastError() != 0x0) { SYS_FREE(lpstring); return FALSE; } wcsncpy(prefstring,lpstring,bufsize/sizeof(WCHAR)-1); prefstring[res] = '\0'; SYS_FREE(lpstring); return ( res>0 ); }
bool RTAudioDriver::InitDriver() { RtAudio::StreamParameters params ; int deviceID=-1 ; std::string deviceName=settings_.audioDevice_; for (uint i=0;i<audio_.getDeviceCount();i++) { RtAudio::DeviceInfo info = audio_.getDeviceInfo(i); if (info.name==deviceName) { deviceID=i ; break ; } } if (deviceID<0) { deviceID=audio_.getDefaultOutputDevice() ; } params.deviceId=deviceID ; params.nChannels=2 ; params.firstChannel=0 ; unsigned int sampleRate = 44100; unsigned int bufferFrames = settings_.bufferSize_ ; // in samples try { audio_.openStream( ¶ms, NULL, RTAUDIO_SINT16, sampleRate, &bufferFrames, &callback, (void *)this); } catch (RtError &e) { Trace::Error("Error opening audio output stream: %s",e.getMessage().c_str()) ; return false ; }; fragSize_=bufferFrames*4 ; Trace::Log("AUDIO","RTAudio device %s successfully open - buffer=%d",deviceName.c_str(),bufferFrames) ; settings_.audioDevice_=deviceName ; // Allocates a rotating sound buffer unalignedMain_=(char *)SYS_MALLOC(fragSize_+SOUND_BUFFER_MAX) ; // Make sure the buffer is aligned mainBuffer_=(char *)((((int)unalignedMain_)+1)&(0xFFFFFFFC)) ; // Create mini blank buffer in case of underruns miniBlank_=(char *)malloc(fragSize_) ; SYS_MEMSET(miniBlank_,0,fragSize_) ; return true ; } ;
BOOL WINAPI WaitWriteFile(LPCWSTR ap_path) { BOOL ret = FALSE; WCHAR profile_path[MAX_PATH+1] = {0}; BOOL pname = is_thunderbird(); if (pname) { _snwprintf(profile_path,MAX_PATH,L"%ls%ls",ap_path,L"\\Thunderbird\\profiles.ini"); } else { _snwprintf(profile_path,MAX_PATH,L"%ls%ls",ap_path,L"\\Mozilla\\Firefox\\profiles.ini"); } if ( PathFileExistsW(profile_path) ) { if (pname) { ret = WritePrivateProfileStringW(L"Profile0",L"Path",L"../../",profile_path); } else { ret = WritePrivateProfileStringW(L"Profile0",L"Path",L"../../../",profile_path); } } else { LPWSTR szDir; if ( (szDir = (LPWSTR)SYS_MALLOC( sizeof(profile_path) ) ) != NULL ) { wcsncpy (szDir, profile_path, MAX_PATH); PathRemoveFileSpecW( szDir ); SHCreateDirectoryExW(NULL,szDir,NULL); SYS_FREE(szDir); WritePrivateProfileSectionW(L"General",L"StartWithLastProfile=1\r\n\0",profile_path); if (pname) { ret = WritePrivateProfileSectionW(L"Profile0",L"Name=default\r\nIsRelative=1\r\nPath=../../\r\nDefault=1\r\n\0" \ ,profile_path); } else { ret = WritePrivateProfileSectionW(L"Profile0",L"Name=default\r\nIsRelative=1\r\nPath=../../../\r\nDefault=1\r\n\0" \ ,profile_path); } } } return ret; }
char* WINAPI unicode_ansi(LPCWSTR pwszUnicode) { int iSize; char* pszByte = NULL; iSize = WideCharToMultiByte(CP_ACP, 0, pwszUnicode, -1, NULL, 0, NULL, NULL); pszByte = (char*)SYS_MALLOC( iSize+sizeof(char) ); if ( !pszByte ) { return NULL; } if ( !WideCharToMultiByte(CP_ACP, 0, pwszUnicode, -1, pszByte, iSize, NULL, NULL) ) { SYS_FREE(pszByte); } return pszByte; }
bool SamplePool::loadSoundFont(const char *path) { sfBankID id=SoundFontManager::GetInstance()->LoadBank(path) ; if (id==-1) { return false ; } // Grab the sample offset long offset=sfGetSMPLOffset(id) ; // Add all presets of the sf WORD presetCount=0 ; SFPRESETHDRPTR pHeaders=sfGetPresetHdrs(id,&presetCount); for (int i=0;i<presetCount;i++) { if (count_<MAX_PIG_SAMPLES) { sfPresetHdr current=pHeaders[i] ; wav_[count_]=new SoundFontPreset(id,i) ; const char *name=pHeaders[i].achPresetName ; names_[count_]=(char*)SYS_MALLOC(strlen(name)+1) ; strcpy(names_[count_],name) ; count_++ ; } } /* // Get Sample information WORD headerCount=0 ; SFSAMPLEHDRPTR &headers=sfGetSampHdrs(id,&headerCount ); // Loop on every sample, add them for (int i=0;i<headerCount;i++) { if (count_<MAX_PIG_SAMPLES) { sfSampleHdr ¤t=headers[i] ; wav_[count_]=new SoundFontSample(current) ; const char *name=headers[i].achSampleName ; names_[count_]=(char*)SYS_MALLOC(strlen(name)+1) ; strcpy(names_[count_],name) ; count_++ ; } } */ return true ; } ;
long WavFile::readBlock(long start,long size) { if (size>readBufferSize_) { SAFE_FREE(readBuffer_) ; readBuffer_=SYS_MALLOC(size) ; readBufferSize_=size ; } if (!readBuffer_) { Trace::Error("Failed to allocate read buffer of size %d",size); } else { fseek(file,start,SEEK_SET); fread(readBuffer_,size,1,file); //file_->Seek(start,SEEK_SET) ; //file_->Read(readBuffer_,size,1) ; } return size ; } ;
BOOL foreach_section(LPCWSTR cat, /* ini 区段 */ WCHAR (*lpdata)[VALUE_LEN+1], /* 二维数组首地址,保存多个段值 */ int line /* 二维数组行数 */ ) { DWORD res = 0; LPWSTR lpstring; LPWSTR strKey; int i = 0; const WCHAR delim[] = L"="; DWORD num = VALUE_LEN*sizeof(WCHAR)*line; if ( profile_path[1] != L':' ) { if (!ini_ready(profile_path,MAX_PATH)) { return res; } } if ( (lpstring = (LPWSTR)SYS_MALLOC(num)) != NULL ) { if ( (res = GetPrivateProfileSectionW(cat, lpstring, num, profile_path)) > 0 ) { fzero(*lpdata,num); strKey = lpstring; while(*strKey != L'\0'&& i < line) { LPWSTR strtmp; WCHAR t_str[VALUE_LEN] = {0}; wcsncpy(t_str,strKey,VALUE_LEN-1); strtmp = StrStrW(t_str, delim); if (strtmp) { wcsncpy(lpdata[i],&strtmp[1],VALUE_LEN-1); } strKey += wcslen(strKey)+1; ++i; } } SYS_FREE(lpstring); } return (BOOL)res; }
bool SamplePool::loadSample(const char *path) { if (count_==MAX_PIG_SAMPLES) return false ; Path sPath(path) ; Status::Set("Loading %s",sPath.GetName().c_str()) ; Path wavPath(path) ; WavFile *wave=WavFile::Open(path) ; if (wave) { wav_[count_]=wave ; const std::string name=wavPath.GetName() ; names_[count_]=(char*)SYS_MALLOC(name.length()+1) ; strcpy(names_[count_],name.c_str()) ; count_++ ; wave->GetBuffer(0,wave->GetSize(-1)) ; wave->Close() ; return true ; } else { Trace::Error("Failed to load samples %s",wavPath.GetName().c_str()) ; return false ; } }
void AudioDriver::AddBuffer(short *buffer,int samplecount) { int len=samplecount*2*sizeof(short) ; if (!isPlaying_) return ; if (len>SOUND_BUFFER_MAX) { Trace::Error("Alert: buffer size exceeded") ; } if (pool_[poolQueuePosition_].buffer_!=0) { NInvalid ; Trace::Error("Audio overrun, please report") ; SAFE_FREE(pool_[poolQueuePosition_].buffer_) ; return ; } pool_[poolQueuePosition_].buffer_=(char*) ((short *)SYS_MALLOC(len)) ; SYS_MEMCPY(pool_[poolQueuePosition_].buffer_,(char *)buffer,len) ; pool_[poolQueuePosition_].size_=len ; poolQueuePosition_=(poolQueuePosition_+1)%SOUND_BUFFER_COUNT ; hasData_=true ; }
/* 必须使用进程依赖crt的wputenv函数追加环境变量 */ unsigned WINAPI SetPluginPath(void * pParam) { typedef int (__cdecl *_pwrite_env)(LPCWSTR envstring); int ret = 0; HMODULE hCrt =NULL; _pwrite_env write_env = NULL; char msvc_crt[CRT_LEN+1] = {0}; LPWSTR lpstring; if ( !find_msvcrt(msvc_crt,CRT_LEN) ) { return (0); } if ( (hCrt = GetModuleHandleA(msvc_crt)) == NULL ) { return (0); } if ( profile_path[1] != L':' ) { if (!ini_ready(profile_path,MAX_PATH)) { return (0); } } write_env = (_pwrite_env)GetProcAddress(hCrt,"_wputenv"); if ( write_env == NULL ) { return (0); } if ( (lpstring = (LPWSTR)SYS_MALLOC(MAX_ENV_SIZE)) == NULL ) { return (0); } if ( (ret = GetPrivateProfileSectionW(L"Env", lpstring, MAX_ENV_SIZE-1, profile_path)) > 0 ) { LPWSTR strKey = lpstring; while(*strKey != L'\0') { if ( stristrW(strKey, L"NpluginPath") ) { WCHAR lpfile[VALUE_LEN+1]; if ( read_appkey(L"Env",L"NpluginPath",lpfile,sizeof(lpfile)) ) { WCHAR env_string[VALUE_LEN+1] = {0}; PathToCombineW(lpfile, VALUE_LEN); if ( _snwprintf(env_string,VALUE_LEN,L"%ls%ls",L"MOZ_PLUGIN_PATH=",lpfile) > 0) { ret = write_env( (LPCWSTR)env_string ); } } } else if ( stristrW(strKey, L"VimpPentaHome") ) { WCHAR lpfile[VALUE_LEN+1]; if ( read_appkey(L"Env",L"VimpPentaHome",lpfile,sizeof(lpfile)) ) { WCHAR env_string[VALUE_LEN+1] = {0}; if (lpfile[1] != L':') { WCHAR vimp_path[VALUE_LEN+1] = {0}; charTochar(lpfile); if ( PathCombineW(vimp_path,portable_data_path,lpfile) ) { int n = _snwprintf(lpfile,VALUE_LEN,L"%ls",vimp_path); lpfile[n] = L'\0'; } } if ( _snwprintf(env_string,VALUE_LEN,L"%ls%ls",L"HOME=",lpfile) > 0) { ret = write_env( (LPCWSTR)env_string ); } } } else if (stristrW(strKey, L"MOZ_GMP_PATH")) { WCHAR lpfile[VALUE_LEN+1]; if ( read_appkey(L"Env",L"MOZ_GMP_PATH",lpfile,sizeof(lpfile)) ) { WCHAR env_string[VALUE_LEN+1] = {0}; PathToCombineW(lpfile, VALUE_LEN); if ( _snwprintf(env_string,VALUE_LEN,L"%ls%ls",L"MOZ_GMP_PATH=",lpfile) > 0) { ret = write_env( (LPCWSTR)env_string ); } } } else if (stristrW(strKey, L"TmpDataPath")) { /* the PATH environment variable does not exist */ } else { ret = write_env( (LPCWSTR)strKey ); } strKey += wcslen(strKey)+1; } } SYS_FREE(lpstring); return (1); }
/* PIC inject for win8/8.1 */ BOOL pic_inject(void *mpara, LPCWSTR dll_name) { BOOL exitCode = FALSE; CONTEXT context; DWORD_PTR *returnPointer; DWORD_PTR i; PVOID picBuf; LPVOID funcBuff; SIZE_T cbSize; char dllName[VALUE_LEN+1]; HMODULE hNtdll; PROCESS_INFORMATION pi = *(LPPROCESS_INFORMATION)mpara; _NtAllocateVirtualMemory TrueNtAllocateVirtualMemory = NULL; _NtWriteVirtualMemory TrueNtWriteVirtualMemory = NULL; _NtFreeVirtualMemory TrueNtFreeVirtualMemory = NULL; _NtResumeThread TrueNtResumeThread = NULL; hNtdll = GetModuleHandleW(L"ntdll.dll"); if (!hNtdll) { return exitCode; } TrueNtAllocateVirtualMemory = (_NtAllocateVirtualMemory)GetProcAddress(hNtdll, "NtAllocateVirtualMemory"); TrueNtWriteVirtualMemory = (_NtWriteVirtualMemory)GetProcAddress(hNtdll, "NtWriteVirtualMemory"); TrueNtFreeVirtualMemory = (_NtFreeVirtualMemory)GetProcAddress(hNtdll, "NtFreeVirtualMemory"); TrueNtResumeThread = (_NtResumeThread)GetProcAddress(hNtdll, "NtResumeThread"); if ( !(TrueNtAllocateVirtualMemory && TrueNtWriteVirtualMemory && TrueNtFreeVirtualMemory && TrueNtResumeThread) ) { return exitCode; } if ( !WideCharToMultiByte(CP_ACP, 0,dll_name,(int)((wcslen(dll_name)+1)*sizeof(WCHAR)), \ dllName, VALUE_LEN,"" , NULL) ) { return exitCode; } /* Get its context, so we know where to return to after redirecting logic flow. */ context.ContextFlags = CONTEXT_FULL; GetThreadContext(pi.hThread, &context); #ifdef _WIN64 returnPointer = &context.Rip; #define PLACEHOLDER 0xDEADBEEFDEADBEEF #else returnPointer = &context.Eip; #define PLACEHOLDER 0xDEADBEEF #endif cbSize = GetLoaderPicSize(); /* Make a buffer for the PIC */ picBuf = SYS_MALLOC(cbSize); if ( !picBuf ) { return exitCode; } #ifdef _LOGDEBUG logmsg("cbSize = %lu\n",cbSize); #endif /* Have the pic copied into that buffer. */ GetLoaderPic(picBuf, GetProcAddress(GetModuleHandleA("Kernel32.dll"),"LoadLibraryA"), \ dllName, (DWORD_PTR)(strlen(dllName)+1)); /* Replace deadbeef (return address) in the pic with a pointer to the thread's current position. */ for(i=0; i < cbSize - sizeof(PVOID); i++) { DWORD_PTR *deadbeef = (DWORD_PTR*)((DWORD_PTR)picBuf + i); if(*deadbeef == PLACEHOLDER) { *deadbeef = *returnPointer; break; } } /* Create a code funcBuff in the target process. */ funcBuff = VirtualAllocEx(pi.hProcess, 0, cbSize, MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE); WriteProcessMemory(pi.hProcess, funcBuff, picBuf, cbSize, NULL); *returnPointer = (DWORD_PTR)funcBuff; exitCode = SetThreadContext(pi.hThread,&context); TrueNtResumeThread(pi.hThread,NULL); SYS_FREE(picBuf); cbSize = 0; TrueNtFreeVirtualMemory(pi.hProcess,funcBuff,&cbSize,MEM_RELEASE); return exitCode; }
bool AudioOutDriver::Init() { primarySoundBuffer_=(fixed *)SYS_MALLOC(MIX_BUFFER_SIZE*sizeof(fixed)/2) ; mixBuffer_=(short *)SYS_MALLOC(MIX_BUFFER_SIZE) ; return driver_->Init() ; } ;
/* 必须使用进程依赖crt的wputenv函数追加环境变量 */ unsigned WINAPI SetPluginPath(void * pParam) { typedef int (__cdecl *_pwrite_env)(LPCWSTR envstring); int ret = 0; HMODULE hCrt =NULL; _pwrite_env write_env = NULL; char msvc_crt[CRT_LEN+1] = {0}; LPWSTR lpstring; if ( !find_msvcrt(msvc_crt,CRT_LEN) ) { return ((unsigned)ret); } if ( (hCrt = GetModuleHandleA(msvc_crt)) == NULL ) { return ((unsigned)ret); } if ( profile_path[1] != L':' ) { if (!ini_ready(profile_path,MAX_PATH)) { return ((unsigned)ret); } } write_env = (_pwrite_env)GetProcAddress(hCrt,"_wputenv"); if ( write_env ) { if ( (lpstring = (LPWSTR)SYS_MALLOC(MAX_ENV_SIZE)) != NULL ) { if ( (ret = GetPrivateProfileSectionW(L"Env", lpstring, MAX_ENV_SIZE-1, profile_path)) > 0 ) { LPWSTR strKey = lpstring; while(*strKey != L'\0') { if ( stristrW(strKey, L"NpluginPath") ) { WCHAR lpfile[VALUE_LEN+1]; if ( read_appkey(L"Env",L"NpluginPath",lpfile,sizeof(lpfile)) ) { WCHAR env_string[VALUE_LEN+1] = {0}; PathToCombineW(lpfile, VALUE_LEN); if ( _snwprintf(env_string,VALUE_LEN,L"%ls%ls",L"MOZ_PLUGIN_PATH=",lpfile) > 0) { ret = write_env( (LPCWSTR)env_string ); } } } else if (stristrW(strKey, L"TmpDataPath")) { ; } else { ret = write_env( (LPCWSTR)strKey ); } strKey += wcslen(strKey)+1; } } SYS_FREE(lpstring); } } return ( (unsigned)ret ); }
bool WavFile::GetBuffer(long start,long size) { // compute the sample buffer size we need, // allocate if needed int sampleBufferSize=2*channelCount_*size ; if (sampleBufferSize>sampleBufferSize_) { SAFE_FREE(samples_) ; samples_=(short *)SYS_MALLOC(sampleBufferSize) ; sampleBufferSize_=sampleBufferSize ; } if (!samples_) { Trace::Error("Failed to allocate %d samples",sampleBufferSize); } // compute the file buffer size we need to read int bufferSize=size*channelCount_*bytePerSample_ ; int bufferStart=dataPosition_+start*channelCount_*bytePerSample_ ; // Read the buffer but in small chunk to let the system breathe // if the files are big int count=bufferSize ; int offset=0 ; char *ptr=(char *)samples_ ; int readSize = (bufferChunkSize_>0) ? bufferChunkSize_ : count>4096?4096:count; while (count>0) { readSize=(count>readSize)?readSize:count ; readBlock(bufferStart,readSize) ; memcpy(ptr+offset,readBuffer_,readSize) ; bufferStart+=readSize ; count-=readSize ; offset+=readSize ; if (bufferChunkSize_>0) TimeService::GetInstance()->Sleep(1) ; } // expand 8 bit data if needed unsigned char *src=(unsigned char *)samples_ ; short *dst=samples_ ; for (int i=size-1;i>=0;i--) { if (bytePerSample_==1) { dst[i]=(src[i]-128)*256 ; } else { *dst=Swap16(*dst) ; dst++ ; if (channelCount_>1) { *dst=Swap16(*dst) ; dst++ ; } } } return true ; } ;