From 48540940b6c28bb4378abfeb500ec45a625b37b6 Mon Sep 17 00:00:00 2001 From: Vadim Dashevskiy Date: Tue, 15 May 2012 10:38:20 +0000 Subject: initial commit git-svn-id: http://svn.miranda-ng.org/main/trunk@2 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- plugins/helpers/commonheaders.h | 65 ++++++ plugins/helpers/db_helpers.cpp | 71 ++++++ plugins/helpers/db_helpers.h | 489 ++++++++++++++++++++++++++++++++++++++++ plugins/helpers/gen_helpers.cpp | 463 +++++++++++++++++++++++++++++++++++++ plugins/helpers/gen_helpers.h | 125 ++++++++++ plugins/helpers/stshelpers.cpp | 303 +++++++++++++++++++++++++ plugins/helpers/stshelpers.h | 35 +++ plugins/helpers/xmlhelpers.cpp | 418 ++++++++++++++++++++++++++++++++++ plugins/helpers/xmlhelpers.h | 17 ++ 9 files changed, 1986 insertions(+) create mode 100644 plugins/helpers/commonheaders.h create mode 100644 plugins/helpers/db_helpers.cpp create mode 100644 plugins/helpers/db_helpers.h create mode 100644 plugins/helpers/gen_helpers.cpp create mode 100644 plugins/helpers/gen_helpers.h create mode 100644 plugins/helpers/stshelpers.cpp create mode 100644 plugins/helpers/stshelpers.h create mode 100644 plugins/helpers/xmlhelpers.cpp create mode 100644 plugins/helpers/xmlhelpers.h (limited to 'plugins/helpers') diff --git a/plugins/helpers/commonheaders.h b/plugins/helpers/commonheaders.h new file mode 100644 index 0000000000..7603131670 --- /dev/null +++ b/plugins/helpers/commonheaders.h @@ -0,0 +1,65 @@ +/* + +Miranda IM: the free IM client for Microsoft* Windows* + +Copyright 2000-2003 Miranda ICQ/IM project, +all portions of this codebase are copyrighted to the people +listed in contributors.txt. + +This program is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License +as published by the Free Software Foundation; either version 2 +of the License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*/ + +#if defined( UNICODE ) && !defined( _UNICODE ) +# define _UNICODE +#endif + +#include +#include + +//#define _WIN32_WINNT 0x0501 +#include +#include +#include +//#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include diff --git a/plugins/helpers/db_helpers.cpp b/plugins/helpers/db_helpers.cpp new file mode 100644 index 0000000000..cf58b4f3b2 --- /dev/null +++ b/plugins/helpers/db_helpers.cpp @@ -0,0 +1,71 @@ +/* + Helper functions for Miranda-IM (www.miranda-im.org) + Copyright 2006 P. Boon + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ +#include "db_helpers.h" + +struct RemoveSettings{ + char *szPrefix; + int count; + char **szSettings; +}; + +static int DBRemoveEnumProc(const char *szSetting, LPARAM lParam) { + + struct RemoveSettings *rs; + + rs = (struct RemoveSettings *)lParam; + if (!strncmp(szSetting, rs->szPrefix, strlen(rs->szPrefix))) { + rs->szSettings = ( char** )realloc(rs->szSettings, (rs->count+1)*sizeof(char *)); + rs->szSettings[rs->count] = _strdup(szSetting); + rs->count += 1; + } + + return 0; +} + +int Hlp_RemoveDatabaseSettings(HANDLE hContact, char *szModule, char *szPrefix) { + + DBCONTACTENUMSETTINGS dbces; + struct RemoveSettings rs; + int i, count; + + ZeroMemory(&rs, sizeof(struct RemoveSettings)); + rs.szPrefix = szPrefix; + ZeroMemory(&dbces, sizeof(DBCONTACTENUMSETTINGS)); + dbces.pfnEnumProc = DBRemoveEnumProc; + dbces.lParam = (LPARAM)&rs; + dbces.szModule = szModule; + if (CallService(MS_DB_CONTACT_ENUMSETTINGS, (WPARAM)(HANDLE)hContact, (LPARAM)&dbces) == -1) { + + return -1; + } + count = 0; + if (rs.szSettings != NULL) { + for (i=0;i> 6 ) & 0x3F ); + *d++ = 0x80 + ( U & 0x003F ); + } + else { + *d++ = 0xE0 + ( U >> 12 ); + *d++ = 0x80 + (( U >> 6 ) & 0x3F ); + *d++ = 0x80 + ( U & 0x3F ); + } } + + *d = 0; + } + + return result; +} + +///////////////////////////////////////////////////////////////////////////////////////// +// Utf8Encode - converts UCS2 string to the UTF8-encoded format + +char* Utf8EncodeUcs2( const wchar_t* src ) +{ + int len = wcslen( src ); + char* result = ( char* )malloc( len*3 + 1 ); + if ( result == NULL ) + return NULL; + + { const wchar_t* s = src; + BYTE* d = ( BYTE* )result; + + while( *s ) { + int U = *s++; + + if ( U < 0x80 ) { + *d++ = ( BYTE )U; + } + else if ( U < 0x800 ) { + *d++ = 0xC0 + (( U >> 6 ) & 0x3F ); + *d++ = 0x80 + ( U & 0x003F ); + } + else { + *d++ = 0xE0 + ( U >> 12 ); + *d++ = 0x80 + (( U >> 6 ) & 0x3F ); + *d++ = 0x80 + ( U & 0x3F ); + } } + + *d = 0; + } + + return result; +} + +// Helper functions that need MODULENAME +#define SETTING_NOENCODINGCHECK "NoEncodingCheck" + +int Hlp_UnicodeCheck(char *szPluginName, BOOL bForce, const char *szModule) { + +#ifndef _DEBUG + char *ptr; + char szVersionText[256]; + + if (!CallService(MS_SYSTEM_GETVERSIONTEXT, (WPARAM)sizeof(szVersionText), (LPARAM)szVersionText)) { + ptr = strstr(szVersionText, "Unicode"); +#ifdef UNICODE + if ( (ptr == NULL) && (!DBGetContactSettingByte(NULL, szModule, SETTING_NOENCODINGCHECK, 0)) ) { + if (bForce) { + MessageBoxA(NULL, "You are running the ANSI version Miranda. Please use the ANSI build of this plugin.", szPluginName, MB_OK); + + return -1; + } + else { + MessageBoxA(NULL, "You are running the ANSI version Miranda. It's recommened to use the ANSI build of this plugin.", szPluginName, MB_OK); + DBWriteContactSettingByte(NULL, szModule, SETTING_NOENCODINGCHECK, 1); + + return 0; + } + } +#else + if ( (ptr != NULL) && (!DBGetContactSettingByte(NULL, szModule, SETTING_NOENCODINGCHECK, 0)) ) { + if (bForce) { + MessageBoxA(NULL, "You are running the UNICODE version Miranda. Please use the UNICODE build of this plugin.", szPluginName, MB_OK); + + return -1; + } + else { + MessageBoxA(NULL, "You are running the UNICODE version Miranda. It's recommened to use the UNICODE build of this plugin.", szPluginName, MB_OK); + DBWriteContactSettingByte(NULL, szModule, SETTING_NOENCODINGCHECK, 1); + + return 0; + } + } +#endif + } +#endif + + return 0; +} \ No newline at end of file diff --git a/plugins/helpers/gen_helpers.h b/plugins/helpers/gen_helpers.h new file mode 100644 index 0000000000..8f4d91b940 --- /dev/null +++ b/plugins/helpers/gen_helpers.h @@ -0,0 +1,125 @@ +/* + Helper functions for Miranda-IM (www.miranda-im.org) + Copyright 2006 P. Boon + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ +#ifndef __GEN_HELPERS_H +#define __GEN_HELPERS_H + +int ProtoServiceExists(const char *szModule, const char *szService); +char *Hlp_GetProtocolNameA(const char *proto); +TCHAR *Hlp_GetProtocolName(const char *proto); + +char *Hlp_GetDlgItemTextA(HWND hwndDlg, int nIDDlgItem); +TCHAR *Hlp_GetDlgItemText(HWND hwndDlg, int nIDDlgItem); +char *Hlp_GetWindowTextA(HWND hwndDlg); +TCHAR *Hlp_GetWindowText(HWND hwndDlg); + +char* u2a( wchar_t* src ); +wchar_t* a2u( char* src ); +int ttoi(TCHAR *string); +TCHAR *itot(int num); + +char* Utf8EncodeUcs2( const wchar_t* src ); +char* Utf8Encode( const char* src ); +void Utf8Decode( char* str, wchar_t** ucs2 ); + +#define MAX_DEBUG 1024 + +#define __LOGLEVEL_DEBUG 10 +#define __LOGLEVEL_INFO 9 +#define __LOGLEVEL_WARN 8 +#define __LOGLEVEL_ERROR 7 +#define __LOGLEVEL_FATAL 6 + +#ifndef LOGLEVEL + #ifdef _DEBUG + #define LOGLEVEL __LOGLEVEL_DEBUG + #else + #define LOGLEVEL __LOGLEVEL_INFO + #endif +#endif + +int AddDebugLogMessageA(const char* fmt, ...); +int AddDebugLogMessage(const TCHAR* fmt, ...); +int AddErrorLogMessageA(const char* fmt, ...); +int AddErrorLogMessage(const TCHAR* fmt, ...); + +#if LOGLEVEL >= __LOGLEVEL_DEBUG +#define log_debugA AddDebugLogMessageA +#define log_debug AddDebugLogMessage +#else +static __inline int log_debugA(const char* fmt, ...) { return 0; } +static __inline int log_debug(const TCHAR* fmt, ...) { return 0; } +#endif + +#if LOGLEVEL >= __LOGLEVEL_INFO +#define log_infoA AddDebugLogMessageA +#define log_info AddDebugLogMessage +#else +static __inline int log_infoA(const char* fmt, ...) { return 0; } +static __inline int log_info(const TCHAR* fmt, ...) { return 0; } +#endif + +#if LOGLEVEL >= __LOGLEVEL_WARN +#define log_warnA AddDebugLogMessageA +#define log_warn AddDebugLogMessage +#else +static __inline int log_warnA(const char* fmt, ...) { return 0; } +static __inline int log_warn(const TCHAR* fmt, ...) { return 0; } +#endif + +#if LOGLEVEL >= __LOGLEVEL_ERROR +#define log_errorA AddErrorLogMessageA +#define log_error AddErrorLogMessage +#else +static __inline int log_errorA(const char* fmt, ...) { return 0; } +static __inline int log_error(const TCHAR* fmt, ...) { return 0; } +#endif + +#if LOGLEVEL >= __LOGLEVEL_FATAL +#define log_fatalA AddErrorLogMessageA +#define log_fatal AddErrorMessage +#else +static __inline int log_fatalA(const char* fmt, ...) { return 0; } +static __inline int log_fatal(const TCHAR* fmt, ...) { return 0; } +#endif + +int Hlp_UnicodeCheck(char *szPluginName, BOOL bFore, const char *szModule); +#define UnicodeCheck(x, y) Hlp_UnicodeCheck(x, y, MODULENAME) + +static __inline char *variables_parsedupA(char *szFormat, char *szExtraText, HANDLE hContact) { + + if (ServiceExists(MS_VARS_FORMATSTRING)) { + FORMATINFO fi; + char *szParsed, *szResult; + + ZeroMemory(&fi, sizeof(fi)); + fi.cbSize = sizeof(fi); + fi.szFormat = szFormat; + fi.szExtraText = szExtraText; + fi.hContact = hContact; + szParsed = (char *)CallService(MS_VARS_FORMATSTRING, (WPARAM)&fi, 0); + if (szParsed) { + szResult = _strdup(szParsed); + CallService(MS_VARS_FREEMEMORY, (WPARAM)szParsed, 0); + return szResult; + } + } + return szFormat?_strdup(szFormat):szFormat; +} + +#endif diff --git a/plugins/helpers/stshelpers.cpp b/plugins/helpers/stshelpers.cpp new file mode 100644 index 0000000000..abea62b3ba --- /dev/null +++ b/plugins/helpers/stshelpers.cpp @@ -0,0 +1,303 @@ +#include "commonheaders.h" +#include "gen_helpers.h" +#include +#include "stshelpers.h" + +static struct ProtoInfo { + const char *szProto; + UINT idEvent; +}; + +// note: accesses to pi are not synchronized... 'cause I'm a lazy SOB (and it's not necessary ATM) +static struct ProtoInfo *pi = NULL; +static int piCount = 0; +static UINT g_idEvent = 0; +static HWND hMessageWindow = NULL; +static HANDLE hProtoAckHook = NULL; + +static VOID CALLBACK EnableProtoTimer(HWND hwnd, UINT message, UINT idEvent, DWORD dwTime) { + + int i; + + KillTimer(hwnd, idEvent); + log_debugA("EnableProtoTimer"); + for (i=0;i 1) { + pi = realloc(pi, (piCount-1)*sizeof(struct ProtoInfo)); + } + else { + free(pi); + pi = NULL; + log_debugA("(3) pi freed"); + } + piCount -= 1; + } + } +} + +static int ProtoAck(WPARAM wParam, LPARAM lParam) { + + int status; + ACKDATA *ack; + + ack = (ACKDATA*)lParam; + if (ack->type != ACKTYPE_STATUS) return 0; + if (ack->result != ACKRESULT_SUCCESS) return 0; + log_debugA("StatusHandling ProtoAck"); + status = CallProtoService(ack->szModule, PS_GETSTATUS, 0, 0); + log_debugA("StatusHandling ProtoAck status=%d", status); + if ( (status < MIN_STATUS) || (status > MAX_STATUS) ) { + return 0; + } + pi = realloc(pi, (piCount+1)*sizeof(struct ProtoInfo)); + g_idEvent += 1; + pi[piCount].idEvent = g_idEvent; + pi[piCount].szProto = ack->szModule; + piCount += 1; + log_debugA("added timer %d for pi[%d] %s (%x)", pi[piCount-1].idEvent, piCount-1, ack->szModule, ack->szModule); + SetTimer(hMessageWindow, g_idEvent, CONNECTIONTIMEOUT, EnableProtoTimer); + + return 0; +} + +int InitProtoStatusChangedHandling() { + + if (hMessageWindow == NULL) { + int i, count; + PROTOCOLDESCRIPTOR **protos; + + CallService(MS_PROTO_ENUMPROTOCOLS, (WPARAM)&count, (LPARAM)&protos); + for(i=0;itype!=PROTOTYPE_PROTOCOL || CallProtoService(protos[i]->szName,PS_GETCAPS,PFLAGNUM_2,0)==0) continue; + //pi = realloc(pi, (piCount + 1)*sizeof(struct ProtoInfo)); + //ZeroMemory(&pi[piCount], sizeof(struct ProtoInfo)); + //pi[piCount].szProto = protos[i].szName; + log_debugA("InitProtoStatusChangedHandling: %s added", protos[i]->szName); + } + hMessageWindow = CreateWindowExA(0, "STATIC", NULL, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL); + hProtoAckHook = HookEvent(ME_PROTO_ACK, ProtoAck); + + return 0; + } + + return 1; +} + +int ProtoChangedStatus(char *szProto) { + + int i; + + log_debugA("ProtoChangedStatus %s", szProto); + if (szProto != NULL) { + for (i=0;istatus == ID_STATUS_CURRENT) || (ssi->flags&SSIF_DONTSETSTATUS) ) { + return CallProtoService(ssi->szProto, PS_GETSTATUS, 0, 0); + } + + return ssi->status; +} + +static int EqualsGlobalStatus(SETSTATUSINFO *ssi, int count) { + + PROTOCOLDESCRIPTOR **protos; + int i, j, protoCount, pstatus, gstatus; + + if ( (count == 1) && (ssi[0].flags&SSIF_USEAWAYSYS) && (ssi[0].szProto == NULL) ) { + return ssi[0].status; + } + pstatus = gstatus = 0; + CallService(MS_PROTO_ENUMPROTOCOLS,(WPARAM)&protoCount,(LPARAM)&protos); + for (i=0;itype != PROTOTYPE_PROTOCOL) || (CallProtoService(ssi[i].szProto, PS_GETCAPS, PFLAGNUM_2, 0) == 0)) continue; + pstatus = 0; + for (j=0;jszName, ssi[j].szProto)) { + if (ssi[j].flags&SSIF_DONTSETSTATUS) { + // not all proto's will be set + return 0; + } + if (!(ssi[j].flags&SSIF_USEAWAYSYS)) { + // not all proto's handled by awaysys + return 0; + } + pstatus = GetActualStatus(&ssi[j]); + } + else { + // not all proto's will be set + return 0; + } + } + if (pstatus == 0) { + pstatus = CallProtoService(protos[i]->szName, PS_GETSTATUS, 0, 0); + } + if (gstatus == 0) { + gstatus = pstatus; + } + if (pstatus != gstatus) { + return 0; + } + } + + return gstatus; +} + +// helper, from core +static char *GetDefaultMessage(int status) +{ + switch(status) { + case ID_STATUS_AWAY: return Translate("I've been away since %time%."); + case ID_STATUS_NA: return Translate("Give it up, I'm not in!"); + case ID_STATUS_OCCUPIED: return Translate("Not right now."); + case ID_STATUS_DND: return Translate("Give a guy some peace, would ya?"); + case ID_STATUS_FREECHAT: return Translate("I'm a chatbot!"); + case ID_STATUS_ONLINE: return Translate("Yep, I'm here."); + case ID_STATUS_OFFLINE: return Translate("Nope, not here."); + case ID_STATUS_INVISIBLE: return Translate("I'm hiding from the mafia."); + case ID_STATUS_ONTHEPHONE: return Translate("That'll be the phone."); + case ID_STATUS_OUTTOLUNCH: return Translate("Mmm...food."); + case ID_STATUS_IDLE: return Translate("idleeeeeeee"); + } + return NULL; +} + +static char *GetDefaultStatusMessage(int newstatus) { + + char *sMsg, *tMsg; + + sMsg = NULL; + if (ServiceExists(MS_AWAYMSG_GETSTATUSMSG)) { + tMsg = (char*)CallService(MS_AWAYMSG_GETSTATUSMSG, (WPARAM)newstatus, 0); + if (tMsg != NULL) { + sMsg = _strdup(tMsg); + mir_free(tMsg); + } + } + else { + tMsg = GetDefaultMessage(newstatus); /* awaysys doesn't define the service above */ + if (tMsg != NULL) { + sMsg = _strdup(tMsg); + } + } + + return sMsg; +} + +int Hlp_SetStatus(SETSTATUSINFO *ssi, int count) { + + int i, status; + char *szMsg; + + status = EqualsGlobalStatus(ssi, count); + if (status != 0) { + log_debugA("Hlp_SetStatus: Setting global status"); + CallService(MS_CLIST_SETSTATUSMODE, (WPARAM)status, 0); + return 0; + } + for (i=0;i'): + len = 1; + tszRep = _T(">"); + break; + case _T('&'): + len = 1; + tszRep = _T("&"); + break; + case _T('\''): + len = 1; + tszRep = _T("'"); + break; + case _T('"'): + len = 1; + tszRep = _T("""); + break; + case _T('\r'): + if (tszEsc[cur+1] == _T('\n')) { + len = 2; + tszRep = _T(" "); + } + break; + case _T('\n'): + len = 1; + tszRep = _T(" "); + break; + } + // tszRep > len !!! + if (tszRep != NULL) { + tszEsc = realloc(tszEsc, (_tcslen(tszEsc) + _tcslen(tszRep) - len + 1)*sizeof(TCHAR)); + MoveMemory(&tszEsc[cur+_tcslen(tszRep) - len], &tszEsc[cur], _tcslen(&tszEsc[cur]) + 1); + CopyMemory(&tszEsc[cur], tszRep, _tcslen(tszRep)*sizeof(TCHAR)); + cur += _tcslen(tszRep); + } + else { + cur++; + } + } +#ifdef UNICODE + szRes = Utf8EncodeUcs2(tszEsc); +#else + szRes = Utf8Encode(tszEsc); +#endif + free(tszEsc); + + return szRes; +} + +char *XMLEncodeStringA(char *szSrc) { + + int cur, len; + char *szEsc, *szRep; + + if (szSrc == NULL) { + return NULL; + } + szEsc = _strdup(szSrc); + cur = len = 0; + while (szEsc[cur] != '\0') { + szRep = NULL; + switch (szEsc[cur]) { + case '<': + len = 1; + szRep = "<"; + break; + case '>': + len = 1; + szRep = ">"; + break; + case '&': + len = 1; + szRep = "&"; + break; + case '\'': + len = 1; + szRep = "'"; + break; + case '"': + len = 1; + szRep = """; + break; + case '\r': + if (szEsc[cur+1] == '\n') { + len = 2; + szRep = " "; + } + break; + case '\n': + len = 1; + szRep = " "; + break; + } + if (szRep != NULL) { + szEsc = realloc(szEsc, (strlen(szEsc) + strlen(szRep) - len + 1)); + MoveMemory(&szEsc[cur+strlen(szRep) - len], &szEsc[cur], strlen(&szEsc[cur]) + 1); + CopyMemory(&szEsc[cur], szRep, strlen(szRep)); + cur+=strlen(szRep); + } + else { + cur++; + } + } + + return szEsc; +} + +TCHAR *DecodeXMLString(char *szSrc) { + + int cur; + TCHAR *tszEsc, *tszRep, *tszTrans; + char *szEsc; +#ifdef UNICODE + wchar_t *wszEsc; +#endif + + if (szSrc == NULL) { + return NULL; + } + szEsc = _strdup(szSrc); +#ifdef UNICODE + Utf8Decode(szEsc, &wszEsc); + tszEsc = wszEsc; + free(szEsc); +#else + Utf8Decode(szEsc, NULL); + tszEsc = szEsc; +#endif + cur= 0; + while (tszEsc[cur] != _T('\0')) { + tszRep = NULL; + if (!_tcsncmp(&tszEsc[cur], _T("<"), 4)) { + tszRep = _T("<"); + tszTrans = _T("<"); + } + else if (!_tcsncmp(&tszEsc[cur], _T(">"), 4)) { + tszRep = _T(">"); + tszTrans = _T(">"); + } + else if (!_tcsncmp(&tszEsc[cur], _T("&"), 5)) { + tszRep = _T("&"); + tszTrans = _T("&"); + } + else if (!_tcsncmp(&tszEsc[cur], _T("'"), 6)) { + tszRep = _T("'"); + tszTrans = _T("\'"); + } + else if (!_tcsncmp(&tszEsc[cur], _T("""), 6)) { + tszRep = _T("""); + tszTrans = _T("&"); + } + else if (!_tcsncmp(&tszEsc[cur], _T(" "), 5)) { + tszRep = _T(" "); + tszTrans = _T("\r\n"); + } + if (tszRep != NULL) { + tszEsc = realloc(tszEsc, (_tcslen(tszEsc) - _tcslen(tszRep) + _tcslen(tszTrans) + 1)*sizeof(TCHAR)); + MoveMemory(&tszEsc[cur+_tcslen(tszTrans)], &tszEsc[cur+_tcslen(tszRep)], _tcslen(&tszEsc[cur+_tcslen(tszRep)]) + 1); + CopyMemory(&tszEsc[cur], tszTrans, _tcslen(tszTrans)*sizeof(TCHAR)); + cur += _tcslen(tszTrans); + } + else { + cur++; + } + } + + return tszEsc; +} + +char *DecodeXMLStringA(char *szSrc, BOOL bUtfDecode) { + + int cur; + char *szEsc, *szRep, *szTrans; + + if (szSrc == NULL) { + return NULL; + } + szEsc = _strdup(szSrc); + if (bUtfDecode) { + Utf8Decode(szEsc, NULL); + } + cur= 0; + while (szEsc[cur] != '\0') { + szRep = NULL; + if (!strncmp(&szEsc[cur], "<", 4)) { + szRep = "<"; + szTrans = "<"; + } + else if (!strncmp(&szEsc[cur], ">", 4)) { + szRep = ">"; + szTrans = ">"; + } + else if (!strncmp(&szEsc[cur], "&", 5)) { + szRep = "&"; + szTrans = "&"; + } + else if (!strncmp(&szEsc[cur], "'", 6)) { + szRep = "'"; + szTrans = "'"; + } + else if (!strncmp(&szEsc[cur], """, 6)) { + szRep = """; + szTrans = "&"; + } + else if (!strncmp(&szEsc[cur], " ", 5)) { + szRep = " "; + szTrans = "\r\n"; + } + if (szRep != NULL) { + szEsc = realloc(szEsc, (strlen(szEsc) - strlen(szRep) + strlen(szTrans) + 1)); + MoveMemory(&szEsc[cur+strlen(szTrans)], &szEsc[cur+strlen(szRep)], strlen(&szEsc[cur+strlen(szRep)]) + 1); + CopyMemory(&szEsc[cur], szTrans, strlen(szTrans)); + cur += strlen(szTrans); + } + else { + cur++; + } + } + + return szEsc; +} + +void AppendXMLOpeningTag(char **szBuf, char *szTag, int *depth) { + + char *cur; + + *depth += 1; + *szBuf = realloc(*szBuf, strlen(*szBuf) + strlen(szTag) + *depth + 4); + cur = *szBuf+strlen(*szBuf); + *cur = '\n'; + if (*depth > 0) { + memset(cur+1, ' ', *depth); + } + *(cur+1+*depth) = '\0'; + sprintf(*szBuf+strlen(*szBuf), "<%s>", szTag); +} + +void AppendXMLClosingTag(char **szBuf, char *szTag, int *depth) { + + char *cur; + + *depth -= 1; + *szBuf = realloc(*szBuf, strlen(*szBuf) + strlen(szTag) + *depth + 5); + cur = *szBuf+strlen(*szBuf); + *cur = '\n'; + if (*depth > 0) { + memset(cur+1, ' ', *depth); + } + *(cur+1+*depth) = '\0'; + sprintf(*szBuf+strlen(*szBuf), "", szTag); +} + +void AppendXMLTag(char **szBuf, char *szTag, char *szAtts, int *depth) { + + char *cur; + + *depth += 1; + if (szAtts != NULL) { + *szBuf = realloc(*szBuf, strlen(*szBuf) + strlen(szTag) + strlen(szAtts) + *depth + 6); + } + else { + *szBuf = realloc(*szBuf, strlen(*szBuf) + strlen(szTag) + *depth + 5); + } + cur = *szBuf+strlen(*szBuf); + *cur = '\n'; + if (*depth > 0) { + memset(cur+1, ' ', *depth); + } + *(cur+1+*depth) = '\0'; + if (szAtts != NULL) { + sprintf(*szBuf+strlen(*szBuf), "<%s %s/>", szTag, szAtts); + } + else { + sprintf(*szBuf+strlen(*szBuf), "<%s/>", szTag); + } + *depth -= 1; +} + +char *GetSettingType(BYTE type) { + + switch (type) { + case DBVT_BYTE: + return "BYTE"; + case DBVT_WORD: + return "WORD"; + case DBVT_DWORD: + return "DWORD"; + case DBVT_ASCIIZ: + return "ASCIIZ"; + case DBVT_BLOB: + return "BLOB"; + case DBVT_UTF8: + return "UTF8"; + case DBVT_WCHAR: + return "WCHAR"; + } + return "UNKNOWN"; +} + +void AppendXMLTagTString(char **szBuf, char *szTag, TCHAR *tszVal, int *depth) { + + char *szVal, *szAtts, *szType; + + szVal = XMLEncodeString(tszVal); +#ifdef UNICODE + szType = GetSettingType(DBVT_WCHAR); +#else + szType = GetSettingType(DBVT_ASCIIZ); +#endif + szAtts = malloc(strlen(szVal) + strlen(szType) + 17); + sprintf(szAtts, "type=\"%s\" value=\"%s\"", szType, szVal); + AppendXMLTag(szBuf, szTag, szAtts, depth); + free(szAtts); + free(szVal); +} + +void AppendXMLTagString(char **szBuf, char *szTag, char *szVal, int *depth) { + + char *szEnc, *szAtts, *szType; + + szEnc = XMLEncodeStringA(szVal); + szType = GetSettingType(DBVT_ASCIIZ); + szAtts = malloc(strlen(szEnc) + strlen(szType) + 17); + sprintf(szAtts, "type=\"%s\" value=\"%s\"", szType, szEnc); + AppendXMLTag(szBuf, szTag, szAtts, depth); + free(szAtts); + free(szEnc); +} + +void AppendXMLTagUtfString(char **szBuf, char *szTag, char *szVal, int *depth) { + + char *szEnc, *szAtts, *szType; + + szEnc = XMLEncodeStringA(szVal); + szType = GetSettingType(DBVT_UTF8); + szAtts = malloc(strlen(szEnc) + strlen(szType) + 17); + sprintf(szAtts, "type=\"%s\" value=\"%s\"", szType, szEnc); + AppendXMLTag(szBuf, szTag, szAtts, depth); + free(szAtts); + free(szEnc); +} + +void AppendXMLTagByte(char **szBuf, char *szTag, BYTE bVal, int *depth) { + + char szVal[64], *szAtts, *szType; + + mir_snprintf(szVal, sizeof(szVal), "0x%x", bVal); + szType = GetSettingType(DBVT_BYTE); + szAtts = malloc(strlen(szVal) + strlen(szType) + 17); + sprintf(szAtts, "type=\"%s\" value=\"%s\"", szType, szVal); + AppendXMLTag(szBuf, szTag, szAtts, depth); + free(szAtts); +} + +void AppendXMLTagWord(char **szBuf, char *szTag, WORD wVal, int *depth) { + + char szVal[64], *szAtts, *szType; + + mir_snprintf(szVal, sizeof(szVal), "0x%x", wVal); + szType = GetSettingType(DBVT_WORD); + szAtts = malloc(strlen(szVal) + strlen(szType) + 17); + sprintf(szAtts, "type=\"%s\" value=\"%s\"", szType, szVal); + AppendXMLTag(szBuf, szTag, szAtts, depth); + free(szAtts); +} + +void AppendXMLTagDword(char **szBuf, char *szTag, DWORD dVal, int *depth) { + + char szVal[64], *szAtts, *szType; + + mir_snprintf(szVal, sizeof(szVal), "0x%x", dVal); + szType = GetSettingType(DBVT_DWORD); + szAtts = malloc(strlen(szVal) + strlen(szType) + 17); + sprintf(szAtts, "type=\"%s\" value=\"%s\"", szType, szVal); + AppendXMLTag(szBuf, szTag, szAtts, depth); + free(szAtts); +} + +void AppendXMLTagBlob(char **szBuf, char *szTag, BYTE *pBlob, int cbBlob, int *depth) { + + int maxLen; + char *szType, *szVal, *szAtts; + NETLIBBASE64 nlb; + + maxLen = Netlib_GetBase64EncodedBufferSize(cbBlob); + szVal = malloc(maxLen + 1); + ZeroMemory(&nlb, sizeof(NETLIBBASE64)); + nlb.cbDecoded = cbBlob; + nlb.pbDecoded = pBlob; + nlb.cchEncoded = maxLen; + nlb.pszEncoded = szVal; + CallService(MS_NETLIB_BASE64ENCODE, 0, (LPARAM)&nlb); + szType = GetSettingType(DBVT_BLOB); + szAtts = malloc(strlen(szVal) + strlen(szType) + 17); + sprintf(szAtts, "type=\"%s\" value=\"%s\"", szType, szVal); + AppendXMLTag(szBuf, szTag, szAtts, depth); + free(szAtts); +} diff --git a/plugins/helpers/xmlhelpers.h b/plugins/helpers/xmlhelpers.h new file mode 100644 index 0000000000..dd0cc822a7 --- /dev/null +++ b/plugins/helpers/xmlhelpers.h @@ -0,0 +1,17 @@ +void AppendXMLOpeningTag(char **szBuf, char *szTag, int *depth); +void AppendXMLClosingTag(char **szBuf, char *szTag, int *depth); +void AppendXMLTag(char **szBuf, char *szTag, char *szAtts, int *depth); + +char *GetSettingType(BYTE type); +void AppendXMLTagTString(char **szBuf, char *szTag, TCHAR *tszVal, int *depth); +void AppendXMLTagString(char **szBuf, char *szTag, char *szVal, int *depth); +void AppendXMLTagUtfString(char **szBuf, char *szTag, char *szVal, int *depth); +void AppendXMLTagByte(char **szBuf, char *szTag, BYTE bVal, int *depth); +void AppendXMLTagWord(char **szBuf, char *szTag, WORD wVal, int *depth); +void AppendXMLTagDword(char **szBuf, char *szTag, DWORD dVal, int *depth); +void AppendXMLTagBlob(char **szBuf, char *szTag, BYTE *pBlob, int cbBlob, int *depth); + +char *XMLEncodeString(TCHAR *szSrc); +char *XMLEncodeStringA(char *tszSrc); +TCHAR *DecodeXMLString(char *szSrc); +char *DecodeXMLStringA(char *szSrc, BOOL bUtfDecode); -- cgit v1.2.3