// locale mbs -> Unicode -> UTF-8 (without free when use _malloca) string MBCS2UTF8(string mbs) { if(mbs == "") return ""; int wlen = MultiByteToWideChar(CP_ACP, 0, mbs.c_str(), -1, NULL, 0); WCHAR *wbuf = (WCHAR *)_malloca((wlen + 1) * sizeof(WCHAR)); if(wbuf == NULL){ throw "_malloca failed for MBCS to UNICODE"; return ""; } *wbuf = L'\0'; if(MultiByteToWideChar(CP_ACP, 0, mbs.c_str(), -1, wbuf, wlen) <= 0){ throw "can't convert MBCS to UNICODE"; return ""; } int ulen = WideCharToMultiByte(CP_UTF8, 0, wbuf, -1, NULL, 0, NULL, NULL); char *ubuf = (char *)_malloca((ulen + 1) * sizeof(char)); if(ubuf == NULL){ throw "_malloca failed for UNICODE to UTF8"; return ""; } *ubuf = '\0'; if(WideCharToMultiByte(CP_UTF8, 0, wbuf, -1, ubuf, ulen, NULL, NULL) <= 0){ throw "can't convert UNICODE to UTF8"; return ""; } ubuf[ulen] = '\0'; return ubuf; }
BOOL convertSetting(HANDLE hContact, char* module, char* setting, int toType) // 0 = byte, 1 = word, 2 = dword, 3 = string, 4 = unicode { DBVARIANT dbv = {0}; BOOL Result = 0; if (!GetSetting(hContact, module, setting, &dbv)) { switch (dbv.type) { case DBVT_BYTE: Result = Convert(hContact, module, setting, dbv.bVal, toType); break; case DBVT_WORD: Result = Convert(hContact, module, setting, dbv.wVal, toType); break; case DBVT_DWORD: Result = Convert(hContact, module, setting, dbv.dVal, toType); break; case DBVT_ASCIIZ: if (toType == 4) // convert to UNICODE { int len = (int)strlen(dbv.pszVal) + 1; WCHAR *wc = (WCHAR*)_malloca(len*sizeof(WCHAR)); MultiByteToWideChar(CP_ACP, 0, dbv.pszVal, -1, wc, len); Result = !DBWriteContactSettingWString(hContact, module, setting, wc); } else if (strlen(dbv.pszVal) < 11 && toType != 3) { int val = atoi(dbv.pszVal); if (val == 0 && dbv.pszVal[0] != '0') break; Result = Convert(hContact, module, setting, val, toType); } break; case DBVT_UTF8: if (toType == 3 && UOS) // convert to ANSI { int len = (int)strlen(dbv.pszVal) + 1; char *sz = (char*)_malloca(len*3); WCHAR *wc = (WCHAR*)_malloca(len*sizeof(WCHAR)); MultiByteToWideChar(CP_UTF8, 0, dbv.pszVal, -1, wc, len); WideCharToMultiByte(CP_ACP, 0, wc, -1, sz, len, NULL, NULL); Result = !DBWriteContactSettingString(hContact, module, setting, sz); } break; } if (!Result) msg(Translate("Cannot Convert!"), modFullname); DBFreeVariant(&dbv); } return Result; }
// UTF8 -> Unicode -> locale mbs (without free when use _malloca) string UTF82MBCS(string utf8) { if(utf8 == "") return ""; int wlen = MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(), -1, NULL, 0); WCHAR *wbuf = (WCHAR *)_malloca((wlen + 1) * sizeof(WCHAR)); if(wbuf == NULL){ throw "_malloca failed for UTF8 to UNICODE"; return ""; } *wbuf = L'\0'; if(MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(), -1, wbuf, wlen) <= 0){ throw "can't convert UTF8 to UNICODE"; return ""; } int slen = WideCharToMultiByte(CP_ACP, 0, wbuf, -1, NULL, 0, NULL, NULL); char *sbuf = (char *)_malloca((slen + 1) * sizeof(char)); if(sbuf == NULL){ throw "_malloca failed for UNICODE to MBCS"; return ""; } *sbuf = '\0'; if(WideCharToMultiByte(CP_ACP, 0, wbuf, -1, sbuf, slen, NULL, NULL) <= 0){ throw "can't convert UNICODE to MBCS"; return ""; } sbuf[slen] = '\0'; return sbuf; }
void XPath::ProcessPath(LookupInfo &info, bool bCreate) { if (!info.nodeName) return; TCHAR *nodeName = (TCHAR *)_malloca(sizeof(TCHAR) * (info.nodeName.length+1)); lstrcpyn(nodeName, info.nodeName.p, info.nodeName.length+1); if (info.attrName && info.attrValue) { TCHAR *attrName = (TCHAR *)_malloca(sizeof(TCHAR) * (info.attrName.length+1)); lstrcpyn(attrName, info.attrName.p, info.attrName.length+1); TCHAR *attrValue = (TCHAR *)_malloca(sizeof(TCHAR) * (info.attrValue.length+1)); lstrcpyn(attrValue, info.attrValue.p, info.attrValue.length+1); HXML hXml = xmlGetChildByTag(m_hXml, nodeName, attrName, attrValue); m_hXml = (hXml || !bCreate) ? hXml : (m_hXml << XCHILD(nodeName) << XATTR(attrName, attrValue)); } else if (info.nodeIndex) { int idx = _ttoi(info.nodeIndex.p); m_hXml = lstrcmp(nodeName, _T("*")) ? xmlGetNthChild(m_hXml, nodeName, idx) : xmlGetChild(m_hXml, idx-1); // no support for such creation mode } else { HXML hXml = xmlGetChild(m_hXml, nodeName); m_hXml = (hXml || !bCreate) ? hXml : (m_hXml << XCHILD(nodeName)); } info.Reset(); }
// Avatars support void gg_getavatarfilename(GGPROTO *gg, HANDLE hContact, char *pszDest, int cbLen) { int tPathLen; char *path = (char *)_malloca(cbLen); char *avatartype = NULL; if (gg->hAvatarsFolder == NULL || FoldersGetCustomPath(gg->hAvatarsFolder, path, cbLen, "")) { char *tmpPath = Utils_ReplaceVars("%miranda_avatarcache%"); tPathLen = mir_snprintf(pszDest, cbLen, "%s\\%s", tmpPath, GG_PROTO); mir_free(tmpPath); } else { strcpy(pszDest, path); tPathLen = (int)strlen(pszDest); } if (_access(pszDest, 0)) CallService(MS_UTILS_CREATEDIRTREE, 0, (LPARAM)pszDest); switch (DBGetContactSettingByte(hContact, GG_PROTO, GG_KEY_AVATARTYPE, GG_KEYDEF_AVATARTYPE)) { case PA_FORMAT_JPEG: avatartype = "jpg"; break; case PA_FORMAT_GIF: avatartype = "gif"; break; case PA_FORMAT_PNG: avatartype = "png"; break; } if (hContact != NULL) { DBVARIANT dbv; if (!DBGetContactSettingString(hContact, GG_PROTO, GG_KEY_AVATARHASH, &dbv)) { mir_snprintf(pszDest + tPathLen, cbLen - tPathLen, "\\%s.%s", dbv.pszVal, avatartype); DBFreeVariant(&dbv); } } else mir_snprintf(pszDest + tPathLen, cbLen - tPathLen, "\\%s avatar.%s", GG_PROTO, avatartype); }
int CAimProto::get_avatar_filename(HANDLE hContact, TCHAR* pszDest, size_t cbLen, const TCHAR *ext) { size_t tPathLen; bool found = false; init_custom_folders(); TCHAR* path = (TCHAR*)_malloca(cbLen * sizeof(TCHAR)); if (hAvatarsFolder == NULL || FoldersGetCustomPathT(hAvatarsFolder, path, (int)cbLen, _T(""))) { TCHAR *tmpPath = Utils_ReplaceVarsT(_T("%miranda_avatarcache%")); TCHAR *tszModuleName = mir_a2t(m_szModuleName); tPathLen = mir_sntprintf(pszDest, cbLen, _T("%s\\%s"), tmpPath, tszModuleName); mir_free(tszModuleName); mir_free(tmpPath); } else { _tcscpy(pszDest, path); tPathLen = _tcslen(pszDest); } if (ext && _taccess(pszDest, 0)) CallService(MS_UTILS_CREATEDIRTREET, 0, (LPARAM)pszDest); size_t tPathLen2 = tPathLen; DBVARIANT dbv; if (getTString(hContact, AIM_KEY_AH, &dbv)) return GAIR_NOAVATAR; tPathLen += mir_sntprintf(pszDest + tPathLen, cbLen - tPathLen, _T("\\%s"), dbv.ptszVal); DBFreeVariant(&dbv); if (ext == NULL) { mir_sntprintf(pszDest + tPathLen, cbLen - tPathLen, _T(".*")); _tfinddata_t c_file; long hFile = _tfindfirst(pszDest, &c_file); if (hFile > -1L) { do { if (_tcsrchr(c_file.name, '.')) { mir_sntprintf(pszDest + tPathLen2, cbLen - tPathLen2, _T("\\%s"), c_file.name); found = true; } } while (_tfindnext(hFile, &c_file) == 0); _findclose( hFile ); } if (!found) pszDest[0] = 0; } else { mir_sntprintf(pszDest + tPathLen, cbLen - tPathLen, ext); found = _taccess(pszDest, 0) == 0; } return found ? GAIR_SUCCESS : GAIR_WAITFOR; }
static void DbgPrint(LPCWSTR format, ...) { if (g_DebugMode) { const WCHAR *outputString; WCHAR *buffer = NULL; size_t length; va_list argp; va_start(argp, format); length = _vscwprintf(format, argp) + 1; buffer = _malloca(length * sizeof(WCHAR)); if (buffer) { vswprintf_s(buffer, length, format, argp); outputString = buffer; } else { outputString = format; } if (g_UseStdErr) fputws(outputString, stderr); else OutputDebugStringW(outputString); if (buffer) _freea(buffer); va_end(argp); } }
void _DBGPRINT( LPCWSTR kwszFunction, INT iLineNumber, LPCWSTR kwszDebugFormatString, ... ) { INT cbFormatString = 0; va_list args; PWCHAR wszDebugString = NULL; size_t st_Offset = 0; va_start( args, kwszDebugFormatString ); cbFormatString = _scwprintf( L"[%s:%d] ", kwszFunction, iLineNumber ) * sizeof( WCHAR ); cbFormatString += _vscwprintf( kwszDebugFormatString, args ) * sizeof( WCHAR ) + 2; /* Depending on the size of the format string, allocate space on the stack or the heap. */ wszDebugString = (PWCHAR)_malloca( cbFormatString ); /* Populate the buffer with the contents of the format string. */ StringCbPrintfW( wszDebugString, cbFormatString, L"[%s:%d] ", kwszFunction, iLineNumber ); StringCbLengthW( wszDebugString, cbFormatString, &st_Offset ); StringCbVPrintfW( &wszDebugString[st_Offset / sizeof(WCHAR)], cbFormatString - st_Offset, kwszDebugFormatString, args ); OutputDebugStringW( wszDebugString ); _freea( wszDebugString ); va_end( args ); }
VOID Console::FormatV( LPCWSTR wstrFormat, va_list pArgList ) { // Count the required length of the string DWORD dwStrLen = _vscwprintf( wstrFormat, pArgList ) + 1; // +1 = null terminator WCHAR* strMessage = ( WCHAR* )_malloca( dwStrLen * sizeof( WCHAR ) ); vswprintf_s( strMessage, dwStrLen, wstrFormat, pArgList ); // Output the string to the console for( DWORD i = 0; i < wcslen( strMessage ); i++ ) { Add( strMessage[i] ); } // Output the string to the debug channel, if requested if( m_bOutputToDebugChannel ) { OutputDebugStringW( strMessage ); } // Render the new output Render(); _freea( strMessage ); }
BOOL ConvertMultiByteToUnicode(UINT CodePage) { // Get required buffer size int cbMultiByte = WideCharToMultiByte(CP_ACP, 0, lpSource, -1, NULL, 0, NULL, NULL); LPSTR lpTemp = (LPSTR)_malloca(cbMultiByte); WideCharToMultiByte(CP_ACP, 0, lpSource, -1, lpTemp, cbMultiByte, NULL, NULL); // Convert the CodePage encoded str to unicode int cchWideChar = MultiByteToWideChar(CodePage, 0, lpTemp, -1, NULL, 0); int cbUnicode = cchWideChar * sizeof(WCHAR); LPWSTR lpTarget = (LPWSTR)_malloca(cbUnicode); MultiByteToWideChar(CodePage, 0, lpTemp, -1, lpTarget, cchWideChar); BOOL fRenamed = MoveFile(lpSource, lpTarget) != 0; _freea(lpTemp); _freea(lpTarget); return fRenamed; }
static void Initialize(void* map, Allocator* allocator) { (*reinterpret_cast< std::map< const std::string, trr::ResourceLoader* >* >(map)) [((new(_malloca(sizeof(First))) First())->GetExtension())] = new(allocator->FlatAllocate(sizeof(First))) First(); return Impl_contentManager< Args... >::Initialize(map, allocator); }
void *FixedSizeAllocator::AllocatePage(void) { switch (m_arena) { case MEMORY_ARENA_STACK: return _malloca(m_pageSize); case MEMORY_ARENA_HEAP: return malloc(m_pageSize); } return nullptr; }
BOOL ConvertTextFileToUnicode(UINT CodePage) { int const cbMax = 1024 * 1024; // Unable to process files larger than 1MB HANDLE hFile = CreateFile(lpSource, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, NULL, NULL); if (hFile == INVALID_HANDLE_VALUE) return FALSE; LPSTR lpSourceText = (LPSTR)_malloca(cbMax); DWORD NumberOfBytesRead; ReadFile(hFile, lpSourceText, cbMax, &NumberOfBytesRead, NULL); CloseHandle(hFile); // Convert the CodePage encoded text to unicode int cchWideChar = MultiByteToWideChar(CodePage, 0, lpSourceText, NumberOfBytesRead, NULL, 0); int cbUnicode = cchWideChar * sizeof(WCHAR); LPWSTR lpTarget = (LPWSTR)_malloca(cbUnicode); int nWideCharsWritten = MultiByteToWideChar(CodePage, 0, lpSourceText, NumberOfBytesRead, lpTarget, cchWideChar); _freea(lpSourceText); // Backup the source file TCHAR szDefaultExt[5] = TEXT(".bak"); //int nLength = _tcslen(lpSource) + _tcslen(szDefaultExt); //LPTSTR lpBakFileName = (LPTSTR)malloc(nLength * sizeof(TCHAR)); //*lpBakFileName = NULL; TCHAR lpBakFileName[MAX_PATH] = TEXT(""); _tcscat_s(lpBakFileName, MAX_PATH, lpSource); _tcscat_s(lpBakFileName, MAX_PATH, szDefaultExt); if (MoveFile(lpSource, lpBakFileName) == 0) return FALSE; // Write the unicode text HANDLE hNewFile = CreateFile(lpSource, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if (hNewFile == INVALID_HANDLE_VALUE) return FALSE; DWORD NumberOfBytesWritten; // Write the unicode signature const WORD bom = 0xfeff; WriteFile(hFile, &bom, sizeof(bom), &NumberOfBytesWritten, NULL); WriteFile(hNewFile, lpTarget, nWideCharsWritten * sizeof(WCHAR), &NumberOfBytesWritten, NULL); // Treat lpTarget as ansi type to get the length in byte _freea(lpTarget); CloseHandle(hNewFile); return TRUE; }
int WorkSettingsChain(DWORD ofsContact,DBContact *dbc,int firstTime) { DBContactSettings *dbcsNew,dbcsOld; DWORD ofsDestThis; int ret; if(firstTime) { ofsDestPrevSettings=0; ofsThisSettings=dbc->ofsFirstSettings; dbc->ofsFirstSettings=0; } if(ofsThisSettings==0) return ERROR_NO_MORE_ITEMS; if(!SignatureValid(ofsThisSettings,DBCONTACTSETTINGS_SIGNATURE)) { AddToStatus(STATUS_ERROR,TranslateT("Settings chain corrupted, further entries ignored")); return ERROR_NO_MORE_ITEMS; } if(PeekSegment(ofsThisSettings,&dbcsOld,sizeof(dbcsOld))!=ERROR_SUCCESS) return ERROR_NO_MORE_ITEMS; if(dbcsOld.cbBlob>256*1024 || dbcsOld.cbBlob==0) { AddToStatus(STATUS_ERROR,TranslateT("Infeasibly large settings blob: skipping")); ofsThisSettings=dbcsOld.ofsNext; return ERROR_SUCCESS; } dbcsNew=(DBContactSettings*)_malloca(offsetof(DBContactSettings,blob)+dbcsOld.cbBlob); if((ret=ReadSegment(ofsThisSettings,dbcsNew,offsetof(DBContactSettings,blob)+dbcsOld.cbBlob))!=ERROR_SUCCESS) { if(ret!=ERROR_HANDLE_EOF) { //eof is OK because blank space at the end doesn't matter return ERROR_NO_MORE_ITEMS; } } if((dbcsNew->ofsModuleName=ConvertModuleNameOfs(dbcsOld.ofsModuleName))==0) { ofsThisSettings=dbcsOld.ofsNext; return ERROR_SUCCESS; } if(dbcsNew->blob[0]==0) { AddToStatus(STATUS_MESSAGE,TranslateT("Empty settings group at %08X: deleting"),ofsThisSettings); ofsThisSettings=dbcsOld.ofsNext; return ERROR_SUCCESS; } dbcsNew->ofsNext=0; //TODO? validate all settings in blob/compact if necessary if((ofsDestThis=WriteSegment(WSOFS_END,dbcsNew,offsetof(DBContactSettings,blob)+dbcsNew->cbBlob))==WS_ERROR) { return ERROR_HANDLE_DISK_FULL; } if(ofsDestPrevSettings) WriteSegment(ofsDestPrevSettings+offsetof(DBContactSettings,ofsNext),&ofsDestThis,sizeof(DWORD)); else dbc->ofsFirstSettings=ofsDestThis; ofsDestPrevSettings=ofsDestThis; ofsThisSettings=dbcsOld.ofsNext; return ERROR_SUCCESS; }
void EbmlIO::Write1UTF8( ISequentialStream* pStream, const wchar_t* str) { assert(pStream); assert(str); const int cb = WideCharToMultiByte( CP_UTF8, 0, //flags (must be 0 for UTF-8 conversion) str, -1, //assume NUL-terminated, and calculate length 0, //buf 0, //count 0, 0); assert(cb > 0); char* const buf = (char*)_malloca(cb); const int n = WideCharToMultiByte( CP_UTF8, 0, str, -1, buf, cb, 0, 0); assert(n == cb); assert(n > 0); const size_t nn = n - 1; assert(nn <= 255); #ifdef _DEBUG const size_t strlen_ = strlen(buf); assert(strlen_ == nn); assert(buf[nn] == '\0'); #endif const BYTE size = static_cast<BYTE>(nn); Write1UInt(pStream, size); Write(pStream, buf, size); }
HANDLE MemoryCallEntryPoint(HMEMORYMODULE mod, LPTSTR cmdLine) { PMEMORYMODULE module = (PMEMORYMODULE)mod; if (module == NULL || module->isDLL || module->exeEntry == NULL || !module->isRelocated) { return 0; } LPTSTR acmd = GetCommandLine(); LPTSTR bkpcmd = (LPTSTR)_malloca((_tcslen(acmd) + 1) * sizeof(TCHAR)); _tcscpy(bkpcmd, acmd); _tcscpy(acmd, cmdLine); HANDLE hThread = (HANDLE)_beginthreadex(NULL, 0, (unsigned(__stdcall *)(void *)) module->exeEntry, NULL, 0, 0); WaitForSingleObject(hThread, 100); _tcscpy(acmd, bkpcmd); _freea(bkpcmd); return hThread; }
int proxy_initialize_send(HANDLE connection, char* sn, char* cookie) { const char sn_length = (char)strlen(sn); const int len = sn_length + 21 + TLV_HEADER_SIZE + AIM_CAPS_LENGTH; char* buf= (char*)_malloca(len); unsigned short offset=0; aim_writeshort(len-2, offset, buf); aim_writegeneric(10, "\x04\x4a\0\x02\0\0\0\0\0\0", offset, buf); aim_writechar((unsigned char)sn_length, offset, buf); // screen name len aim_writegeneric(sn_length, sn, offset, buf); // screen name aim_writegeneric(8, cookie, offset, buf); // icbm cookie aim_writetlv(1, AIM_CAPS_LENGTH, AIM_CAP_FILE_TRANSFER, offset, buf); return Netlib_Send(connection, buf, offset, 0) >= 0 ? 0 : -1; }
// Unicode -> locale mbs (not free wcs, without free when use _malloca) string WCS2MBCS(WCHAR *wbuf) { if(wbuf == NULL || wbuf[0] == L'\0') return ""; int slen = WideCharToMultiByte(CP_ACP, 0, wbuf, -1, NULL, 0, NULL, NULL); char *sbuf = (char *)_malloca((slen + 1) * sizeof(char)); if(sbuf == NULL){ throw "_malloca failed for UNICODE to MBCS"; return ""; } *sbuf = '\0'; if(WideCharToMultiByte(CP_ACP, 0, wbuf, -1, sbuf, slen, NULL, NULL) <= 0){ throw "can't convert UNICODE to MBCS"; return ""; } sbuf[slen] = '\0'; return sbuf; }
static void RebuildGroupCombo(HWND hwndDlg) { DBVARIANT dbv = {0}; char caGroupId[33]; int bHasGroups = !DBGetContactSettingT(NULL, "CListGroups", "0", &dbv); HWND hGroupsCombo = GetDlgItem(hwndDlg, IDC_GROUPS); DBFreeVariant(&dbv); if (bHasGroups) { int curs = SendMessageT(hGroupsCombo, CB_GETCURSEL, 0, 0); TCHAR* curst; EnableDlgItem(hwndDlg, IDC_ENABLEGROUPS, TRUE); EnableGroupCombo(hwndDlg); if (curs != CB_ERR) { curst = (char*)_malloca((SendMessageT(hGroupsCombo, CB_GETLBTEXTLEN, curs, 0) + 1) * sizeof(WCHAR)); SendMessageT(hGroupsCombo, CB_GETLBTEXT, curs, (LPARAM)curst); } SendMessageT(hGroupsCombo, CB_RESETCONTENT, 0, 0); for (int groupId=0; ; groupId++) { itoa(groupId, caGroupId, 10); TCHAR* szGroup = DBGetContactSettingStringT(NULL, "CListGroups", caGroupId, NULL); int nPrefix = g_UnicodeCore ? sizeof(WCHAR) : sizeof(char); if (!szGroup) break; int nIndex = SendMessageT(hGroupsCombo, CB_ADDSTRING, 0, (LPARAM)szGroup + nPrefix); SendMessageT(hGroupsCombo, CB_SETITEMDATA, nIndex, groupId+1); SAFE_FREE((void**)&szGroup); } if (curs != CB_ERR) SendMessageT(hGroupsCombo, CB_SELECTSTRING, -1, (LPARAM)curst); else SendMessageT(hGroupsCombo, CB_SETCURSEL, 0, 0); } else { // no groups available EnableDlgItem(hwndDlg, IDC_ENABLEGROUPS, FALSE); EnableDlgItem(hwndDlg, IDC_GROUPS, FALSE); } }
static char* GetCachedSetting(const char *szModuleName,const char *szSettingName,int settingNameLen) { static char *lastsetting = NULL; int moduleNameLen = (int)strlen(szModuleName),index; char *szFullName = (char*)_malloca(moduleNameLen+settingNameLen+3); strcpy(szFullName+1,szModuleName); szFullName[moduleNameLen+1]='/'; strcpy(szFullName+moduleNameLen+2,szSettingName); if (lastsetting && strcmp(szFullName+1,lastsetting) == 0) return lastsetting; if ( li.List_GetIndex(&lSettings, szFullName, &index)) lastsetting = (char*)lSettings.items[index] + 1; else lastsetting = InsertCachedSetting( szFullName, moduleNameLen+settingNameLen+3, index )+1; return lastsetting; }
int CMsnProto::AuthDeny(HANDLE hDbEvent, const TCHAR* szReason) { if (!msnLoggedIn) return 1; DBEVENTINFO dbei = { 0 }; dbei.cbSize = sizeof(dbei); if ((int)(dbei.cbBlob = MSN_CallService(MS_DB_EVENT_GETBLOBSIZE, (WPARAM)hDbEvent, 0)) == -1) return 1; dbei.pBlob = (PBYTE)_malloca(dbei.cbBlob); if (MSN_CallService(MS_DB_EVENT_GET, (WPARAM)hDbEvent, (LPARAM)&dbei)) return 1; if (dbei.eventType != EVENTTYPE_AUTHREQUEST) return 1; if (strcmp(dbei.szModule, m_szModuleName)) return 1; char* nick = (char*)(dbei.pBlob + sizeof(DWORD) + sizeof(HANDLE)); char* firstName = nick + strlen(nick) + 1; char* lastName = firstName + strlen(firstName) + 1; char* email = lastName + strlen(lastName) + 1; MsnContact* msc = Lists_Get(email); if (msc == NULL) return 0; MSN_AddUser(NULL, email, msc->netId, LIST_PL + LIST_REMOVE); MSN_AddUser(NULL, email, msc->netId, LIST_BL); MSN_AddUser(NULL, email, msc->netId, LIST_RL); if (!(msc->list & (LIST_FL | LIST_LL))) { if (msc->hContact) MSN_CallService(MS_DB_CONTACT_DELETE, (WPARAM)msc->hContact, 0); msc->hContact = NULL; HANDLE hContact = MSN_HContactFromEmail(email); if (hContact) MSN_CallService(MS_DB_CONTACT_DELETE, (WPARAM)hContact, 0); } return 0; }
HANDLE __cdecl CMsnProto::AddToListByEvent(int flags, int iContact, HANDLE hDbEvent) { DBEVENTINFO dbei = {0}; dbei.cbSize = sizeof(dbei); if ((dbei.cbBlob = MSN_CallService(MS_DB_EVENT_GETBLOBSIZE, (WPARAM)hDbEvent, 0)) == (DWORD)(-1)) return NULL; dbei.pBlob=(PBYTE) _malloca(dbei.cbBlob); if (MSN_CallService(MS_DB_EVENT_GET, (WPARAM)hDbEvent, (LPARAM)&dbei)) return NULL; if (strcmp(dbei.szModule, m_szModuleName)) return NULL; if (dbei.eventType != EVENTTYPE_AUTHREQUEST) return NULL; char* nick = (char *) (dbei.pBlob + sizeof(DWORD) + sizeof(HANDLE)); char* firstName = nick + strlen(nick) + 1; char* lastName = firstName + strlen(firstName) + 1; char* email = lastName + strlen(lastName) + 1; return AddToListByEmail(email, nick, flags); }
static INT_PTR SetSettingResident(WPARAM wParam,LPARAM lParam) { char* szSetting; size_t cbSettingNameLen = strlen(( char* )lParam ); int idx; char* szTemp = ( char* )_malloca( cbSettingNameLen+2 ); strcpy( szTemp+1, ( char* )lParam ); EnterCriticalSection(&csDbAccess); if ( !li.List_GetIndex( &lSettings, szTemp, &idx )) szSetting = InsertCachedSetting( szTemp, cbSettingNameLen+2, idx ); else szSetting = lSettings.items[ idx ]; *szSetting = (char)wParam; LeaveCriticalSection(&csDbAccess); return 0; }
std::wstring KRtoWide(const char* pKrStr, int len) { // Trim Right while (len > 0) { int ch = pKrStr[len - 1]; if (ch == ' ' || ch == 0) --len; else break; } int nReq = MultiByteToWideChar(CP_949, 0, pKrStr, len, NULL, 0); int cbAlloc = ++nReq * sizeof(WCHAR); WCHAR* pBuff = (WCHAR*)_malloca(cbAlloc); int wlen = MultiByteToWideChar(CP_949, 0, pKrStr, len, pBuff, nReq); ASSERT(wlen >= 0); std::wstring ret(pBuff, &pBuff[wlen]); _freea(pBuff); return ret; }
bool SmileyCType::CreateTriggerText(char* text) { UrlDecode(text); int len = (int)strlen(text); if (len == 0) return false; int reslen = Netlib_GetBase64DecodedBufferSize(len)+1; char* res = (char*)_malloca(reslen); NETLIBBASE64 nlb = { text, len, ( PBYTE )res, reslen }; if (!CallService(MS_NETLIB_BASE64DECODE, 0, LPARAM( &nlb ))) return false; res[nlb.cbDecoded] = 0; TCHAR *txt = mir_utf8decodeT(res); if (txt == NULL) return false; m_TriggerText = txt; mir_free(txt); return true; }
void __cdecl CAimProto::avatar_request_thread(void* param) { HANDLE hContact = (HANDLE)param; char *sn = getSetting(hContact, AIM_KEY_SN); LOG("Starting avatar request thread for %s)", sn); if (wait_conn(hAvatarConn, hAvatarEvent, 0x10)) { char *hash_str = getSetting(hContact, AIM_KEY_AH); char type = getByte(hContact, AIM_KEY_AHT, 1); size_t len = (strlen(hash_str) + 1) / 2; char* hash = (char*)_malloca(len); string_to_bytes(hash_str, hash); LOG("Requesting an Avatar: %s (Hash: %s)", sn, hash_str); aim_request_avatar(hAvatarConn, avatar_seqno, sn, type, hash, (unsigned short)len); mir_free(hash_str); } mir_free(sn); }
int CMsnProto::Authorize(HANDLE hDbEvent) { if (!msnLoggedIn) return 1; DBEVENTINFO dbei = { 0 }; dbei.cbSize = sizeof(dbei); if ((int)(dbei.cbBlob = MSN_CallService(MS_DB_EVENT_GETBLOBSIZE, (WPARAM)hDbEvent, 0)) == -1) return 1; dbei.pBlob = (PBYTE)_malloca(dbei.cbBlob); if (MSN_CallService(MS_DB_EVENT_GET, (WPARAM)hDbEvent, (LPARAM)&dbei)) return 1; if (dbei.eventType != EVENTTYPE_AUTHREQUEST) return 1; if (strcmp(dbei.szModule, m_szModuleName)) return 1; char* nick = (char*)(dbei.pBlob + sizeof(DWORD) + sizeof(HANDLE)); char* firstName = nick + strlen(nick) + 1; char* lastName = firstName + strlen(firstName) + 1; char* email = lastName + strlen(lastName) + 1; HANDLE hContact = MSN_HContactFromEmail(email, nick, true, 0); int netId = Lists_GetNetId(email); MSN_AddUser(hContact, email, netId, LIST_AL); MSN_AddUser(hContact, email, netId, LIST_BL + LIST_REMOVE); MSN_AddUser(hContact, email, netId, LIST_PL + LIST_REMOVE); MSN_SetContactDb(hContact, email); return 0; }
//-------------------------------------------------------------------------------------- // Name: FormatV() // Desc: Output a va_list using a format string //-------------------------------------------------------------------------------------- VOID Console::FormatV( _In_z_ _Printf_format_string_ LPCSTR strFormat, va_list pArgList ) { // Count the required length of the string DWORD dwStrLen = _vscprintf( strFormat, pArgList ) + 1; // +1 = null terminator CHAR* strMessage = ( CHAR* )_malloca( dwStrLen ); vsprintf_s( strMessage, dwStrLen, strFormat, pArgList ); // Output the string to the console for( DWORD i = 0; i < strlen( strMessage ); i++ ) { Add( strMessage[i] ); } // Output the string to the debug channel, if requested if( m_bOutputToDebugChannel ) { OutputDebugStringA( strMessage ); } // Render the new output Render(); _freea( strMessage ); }
static INT_PTR WriteContactSetting(WPARAM wParam, LPARAM lParam) { DBCONTACTWRITESETTING *dbcws=(DBCONTACTWRITESETTING*)lParam; DBCONTACTWRITESETTING tmp; struct DBContact dbc; DWORD ofsModuleName; struct DBContactSettings dbcs; PBYTE pBlob; int settingNameLen=0; int moduleNameLen=0; int settingDataLen=0; int bytesRequired,bytesRemaining; DWORD ofsContact,ofsSettingsGroup,ofsBlobPtr; if (dbcws == NULL || dbcws->szSetting==NULL || dbcws->szModule==NULL ) return 1; // the db format can't tolerate more than 255 bytes of space (incl. null) for settings+module name settingNameLen=(int)strlen(dbcws->szSetting); moduleNameLen=(int)strlen(dbcws->szModule); if ( settingNameLen > 0xFE ) { #ifdef _DEBUG OutputDebugStringA("WriteContactSetting() got a > 255 setting name length. \n"); #endif return 1; } if ( moduleNameLen > 0xFE ) { #ifdef _DEBUG OutputDebugStringA("WriteContactSetting() got a > 255 module name length. \n"); #endif return 1; } tmp = *dbcws; if (tmp.value.type == DBVT_WCHAR) { if (tmp.value.pszVal != NULL) { char* val = mir_utf8encodeW(tmp.value.pwszVal); if ( val == NULL ) return 1; tmp.value.pszVal = ( char* )_malloca( strlen( val )+1 ); strcpy( tmp.value.pszVal, val ); mir_free(val); tmp.value.type = DBVT_UTF8; } else return 1; } if(tmp.value.type!=DBVT_BYTE && tmp.value.type!=DBVT_WORD && tmp.value.type!=DBVT_DWORD && tmp.value.type!=DBVT_ASCIIZ && tmp.value.type!=DBVT_UTF8 && tmp.value.type!=DBVT_BLOB) return 1; if ((!tmp.szModule) || (!tmp.szSetting) || ((tmp.value.type == DBVT_ASCIIZ || tmp.value.type == DBVT_UTF8 )&& tmp.value.pszVal == NULL) || (tmp.value.type == DBVT_BLOB && tmp.value.pbVal == NULL) ) return 1; // the db can not tolerate strings/blobs longer than 0xFFFF since the format writes 2 lengths switch( tmp.value.type ) { case DBVT_ASCIIZ: case DBVT_BLOB: case DBVT_UTF8: { size_t len = ( tmp.value.type != DBVT_BLOB ) ? (int)strlen(tmp.value.pszVal) : tmp.value.cpbVal; if ( len >= 0xFFFF ) { #ifdef _DEBUG OutputDebugStringA("WriteContactSetting() writing huge string/blob, rejecting ( >= 0xFFFF ) \n"); #endif return 1; } } } EnterCriticalSection(&csDbAccess); { char* szCachedSettingName = GetCachedSetting(tmp.szModule, tmp.szSetting, moduleNameLen, settingNameLen); if ( tmp.value.type != DBVT_BLOB ) { DBVARIANT* pCachedValue = GetCachedValuePtr((HANDLE)wParam, szCachedSettingName, 1); if ( pCachedValue != NULL ) { BOOL bIsIdentical = FALSE; if ( pCachedValue->type == tmp.value.type ) { switch(tmp.value.type) { case DBVT_BYTE: bIsIdentical = pCachedValue->bVal == tmp.value.bVal; break; case DBVT_WORD: bIsIdentical = pCachedValue->wVal == tmp.value.wVal; break; case DBVT_DWORD: bIsIdentical = pCachedValue->dVal == tmp.value.dVal; break; case DBVT_UTF8: case DBVT_ASCIIZ: bIsIdentical = strcmp( pCachedValue->pszVal, tmp.value.pszVal ) == 0; break; } if ( bIsIdentical ) { LeaveCriticalSection(&csDbAccess); return 0; } } SetCachedVariant(&tmp.value, pCachedValue); } if ( szCachedSettingName[-1] != 0 ) { LeaveCriticalSection(&csDbAccess); NotifyEventHooks(hSettingChangeEvent,wParam,(LPARAM)&tmp); return 0; } } else GetCachedValuePtr((HANDLE)wParam, szCachedSettingName, -1); } ofsModuleName=GetModuleNameOfs(tmp.szModule); if(wParam==0) ofsContact=dbHeader.ofsUser; else ofsContact=wParam; dbc=*(struct DBContact*)DBRead(ofsContact,sizeof(struct DBContact),NULL); if(dbc.signature!=DBCONTACT_SIGNATURE) { LeaveCriticalSection(&csDbAccess); return 1; } log0("write setting"); //make sure the module group exists ofsSettingsGroup=GetSettingsGroupOfsByModuleNameOfs(&dbc,ofsModuleName); if(ofsSettingsGroup==0) { //module group didn't exist - make it if(tmp.value.type&DBVTF_VARIABLELENGTH) { if(tmp.value.type==DBVT_ASCIIZ || tmp.value.type==DBVT_UTF8) bytesRequired = (int)strlen(tmp.value.pszVal)+2; else if(tmp.value.type==DBVT_BLOB) bytesRequired=tmp.value.cpbVal+2; } else bytesRequired=tmp.value.type; bytesRequired+=2+settingNameLen; bytesRequired+=(DB_SETTINGS_RESIZE_GRANULARITY-(bytesRequired%DB_SETTINGS_RESIZE_GRANULARITY))%DB_SETTINGS_RESIZE_GRANULARITY; ofsSettingsGroup=CreateNewSpace(bytesRequired+offsetof(struct DBContactSettings,blob)); dbcs.signature=DBCONTACTSETTINGS_SIGNATURE; dbcs.ofsNext=dbc.ofsFirstSettings; dbcs.ofsModuleName=ofsModuleName; dbcs.cbBlob=bytesRequired; dbcs.blob[0]=0; dbc.ofsFirstSettings=ofsSettingsGroup; DBWrite(ofsContact,&dbc,sizeof(struct DBContact)); DBWrite(ofsSettingsGroup,&dbcs,sizeof(struct DBContactSettings)); ofsBlobPtr=ofsSettingsGroup+offsetof(struct DBContactSettings,blob); pBlob=(PBYTE)DBRead(ofsBlobPtr,1,&bytesRemaining); } else {
int WorkModuleChain(int firstTime) { DBModuleName moduleName,*newModName; if(firstTime) { AddToStatus(STATUS_MESSAGE,TranslateT("Processing module name chain")); modChainCount=0; last_mod = 0; if(modChain!=NULL) free(modChain); modChain = (ModChainEntry*)malloc(sizeof(ModChainEntry)); phase=0; ofsCurrent=dbhdr.ofsFirstModuleName; } switch(phase) { case 0: if(ofsCurrent==0) { phase++; return ERROR_SUCCESS; } if(!SignatureValid(ofsCurrent,DBMODULENAME_SIGNATURE)) { AddToStatus(STATUS_ERROR,TranslateT("Module chain corrupted, further entries ignored")); phase++; return ERROR_SUCCESS; } if(PeekSegment(ofsCurrent,&moduleName,offsetof(DBModuleName,name))!=ERROR_SUCCESS) { phase++; return ERROR_SUCCESS; } if(moduleName.cbName>256) AddToStatus(STATUS_WARNING,TranslateT("Unreasonably long module name, skipping")); else { modChain=(ModChainEntry*)realloc(modChain,sizeof(ModChainEntry)*++modChainCount); modChain[modChainCount-1].ofsOld=ofsCurrent; modChain[modChainCount-1].size=offsetof(DBModuleName,name)+moduleName.cbName; modChain[modChainCount-1].ofsNew=0; if (moduleName.cbName) PeekSegment(ofsCurrent+offsetof(DBModuleName,name),&modChain[modChainCount-1].name,moduleName.cbName); modChain[modChainCount-1].name[moduleName.cbName]=0; } ofsCurrent=moduleName.ofsNext; break; case 1: ofsLast = 0; iCurrentModName=0; dbhdr.ofsFirstModuleName=0; phase++; case 2: if(iCurrentModName>=modChainCount) { DWORD dw = 0; if(ofsLast) WriteSegment(ofsLast+offsetof(DBModuleName,ofsNext),&dw,sizeof(DWORD)); return ERROR_NO_MORE_ITEMS; } if(modChain[iCurrentModName].ofsNew==0) { newModName=(DBModuleName*)_malloca(modChain[iCurrentModName].size); if(ReadSegment(modChain[iCurrentModName].ofsOld,newModName,modChain[iCurrentModName].size)!=ERROR_SUCCESS) return ERROR_NO_MORE_ITEMS; if((modChain[iCurrentModName].ofsNew=WriteSegment(WSOFS_END,newModName,modChain[iCurrentModName].size))==WS_ERROR) return ERROR_HANDLE_DISK_FULL; { // check duplicated modulenames int i, n=0; for(i=iCurrentModName+1;i<modChainCount;i++) if(!strcmp(modChain[i].name, modChain[iCurrentModName].name)) { modChain[i].ofsNew = modChain[iCurrentModName].ofsNew; n++; } if (n) AddToStatus(STATUS_WARNING,TranslateT("Module name '%s' is not unique: %d duplicates found)"), modChain[iCurrentModName].name,n); } if(iCurrentModName==0) dbhdr.ofsFirstModuleName=modChain[iCurrentModName].ofsNew; else if(WriteSegment(ofsLast+offsetof(DBModuleName,ofsNext),&modChain[iCurrentModName].ofsNew,sizeof(DWORD))==WS_ERROR) return ERROR_HANDLE_DISK_FULL; ofsLast = modChain[iCurrentModName].ofsNew; } iCurrentModName++; break; } return ERROR_SUCCESS; }