diff options
author | George Hazan <george.hazan@gmail.com> | 2013-08-16 12:48:20 +0000 |
---|---|---|
committer | George Hazan <george.hazan@gmail.com> | 2013-08-16 12:48:20 +0000 |
commit | 517890bdff3c0e5bdf9fbdb5894e37b073b16bc5 (patch) | |
tree | 80dfc06f8ae06a96b32577e8f46b0a3d05d49f8c | |
parent | 49fc1a8b9b5858974f90b9d2ed14ba5442c8cbfc (diff) |
built-in base64 removed from uinfoex
git-svn-id: http://svn.miranda-ng.org/main/trunk@5717 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c
3 files changed, 53 insertions, 278 deletions
diff --git a/plugins/UserInfoEx/src/ex_import/classExImContactBase.cpp b/plugins/UserInfoEx/src/ex_import/classExImContactBase.cpp index ffbce296e9..6a7851d374 100644 --- a/plugins/UserInfoEx/src/ex_import/classExImContactBase.cpp +++ b/plugins/UserInfoEx/src/ex_import/classExImContactBase.cpp @@ -454,15 +454,13 @@ LPSTR CExImContactBase::uid2String(BYTE bPrependType) case DBVT_BLOB: //'n' cpbVal and pbVal are valid
{
if (bPrependType) { //True = XML
- INT_PTR baselen = Base64EncodeGetRequiredLength(_dbvUID.cpbVal, BASE64_FLAG_NOCRLF);
+ INT_PTR baselen = mir_base64_encode_bufsize(_dbvUID.cpbVal);
LPSTR t = (LPSTR)mir_alloc(baselen + 5 + bPrependType);
assert(t != NULL);
- if (Base64Encode(_dbvUID.pbVal, _dbvUID.cpbVal, t + bPrependType, &baselen, BASE64_FLAG_NOCRLF)) {
- if (baselen){
- t[baselen + bPrependType] = 0;
- if (bPrependType) t[0] = 'n';
- return t;
- }
+ if ( mir_base64_encodebuf(_dbvUID.pbVal, _dbvUID.cpbVal, t + bPrependType, baselen)) {
+ t[baselen + bPrependType] = 0;
+ if (bPrependType) t[0] = 'n';
+ return t;
}
mir_free(t);
return NULL;
diff --git a/plugins/UserInfoEx/src/ex_import/classExImContactXML.cpp b/plugins/UserInfoEx/src/ex_import/classExImContactXML.cpp index 78d81fdbf6..b10d99b840 100644 --- a/plugins/UserInfoEx/src/ex_import/classExImContactXML.cpp +++ b/plugins/UserInfoEx/src/ex_import/classExImContactXML.cpp @@ -412,16 +412,14 @@ int CExImContactXML::ExportSetting(TiXmlElement *xmlModule, LPCSTR pszModule, LP case DBVT_BLOB: //'n' cpbVal and pbVal are valid
{
// new buffer for base64 encoded data
- INT_PTR baselen = Base64EncodeGetRequiredLength(dbv.cpbVal, BASE64_FLAG_NOCRLF);
+ INT_PTR baselen = mir_base64_encode_bufsize(dbv.cpbVal);
str = (LPSTR)mir_alloc(baselen + 6);
assert(str != NULL);
// encode data
- if (Base64Encode(dbv.pbVal, dbv.cpbVal, str+1, &baselen, BASE64_FLAG_NOCRLF)) {
- if (baselen){
- str[baselen+1] = 0;
- str[0] = 'n';
- xmlValue = new TiXmlText(str);
- }
+ if ( mir_base64_encodebuf(dbv.pbVal, dbv.cpbVal, str+1, baselen)) {
+ str[baselen+1] = 0;
+ str[0] = 'n';
+ xmlValue = new TiXmlText(str);
}
mir_free(str);
break;
@@ -463,7 +461,6 @@ BYTE CExImContactXML::ExportEvents() PBYTE pbEventBuf = NULL;
DWORD cbEventBuf = 0,
dwNumEventsAdded = 0;
- LPSTR pBase64Data = NULL;
int dwNumEvents = db_event_count(_hContact);
if (dwNumEvents == 0)
@@ -477,20 +474,9 @@ BYTE CExImContactXML::ExportEvents() // read out all events for the current contact
for (HANDLE hDbEvent = db_event_first(_hContact); hDbEvent != NULL; hDbEvent = db_event_next(hDbEvent)) {
if (!DB::Event::GetInfoWithData(hDbEvent, &dbei)) {
- // new buffer for base64 encoded data
- INT_PTR cbNewBase64Data = Base64EncodeGetRequiredLength(dbei.cbBlob, BASE64_FLAG_NOCRLF);
- if (cbNewBase64Data > cbBase64Data) {
- pBase64Data = (LPSTR)mir_realloc(pBase64Data, cbNewBase64Data + 5);
- if (pBase64Data == NULL) {
- MessageBoxA(NULL, "mir_realloc(cbNewBase64Data + 5) == NULL", "Error", 0);
- break;
- }
- cbBase64Data = cbNewBase64Data;
- }
-
// encode data
- if (Base64Encode(dbei.pBlob, dbei.cbBlob, pBase64Data, &cbNewBase64Data, BASE64_FLAG_NOCRLF)) {
- pBase64Data[cbNewBase64Data] = 0;
+ LPSTR pBase64Data = mir_base64_encode(dbei.pBlob, dbei.cbBlob);
+ if (pBase64Data) {
TiXmlElement *xmlEvent = new TiXmlElement("evt");
if (xmlEvent) {
xmlEvent->SetAttribute("type", dbei.eventType);
@@ -529,7 +515,6 @@ BYTE CExImContactXML::ExportEvents() }
mir_free(pbEventBuf);
- mir_free(pBase64Data);
return dwNumEventsAdded == dwNumEvents;
}
@@ -631,43 +616,34 @@ int CExImContactXML::LoadXmlElemnt(TiXmlElement *xContact) LPCSTR pUID = xContact->Attribute("uidv");
if (pUID != NULL) {
- size_t len = 0;
- INT_PTR baselen = NULL;
+ unsigned valLen;
PBYTE pbVal = NULL;
switch (*(pUID++)) {
- case 'b':
- uid((BYTE)atoi(pUID));
- break;
- case 'w':
- uid((WORD)atoi(pUID));
- break;
- case 'd':
- uid((DWORD)_atoi64(pUID));
- break;
- case 's':
- // utf8 -> asci
- uida(pUID);
- break;
- case 'u':
- uidu(pUID);
- break;
- case 'n':
- len = strlen(pUID);
- baselen = Base64DecodeGetRequiredLength(len);
- pbVal = (PBYTE)mir_alloc(baselen /*+1*/);
- if (pbVal != NULL){
- if (Base64Decode(pUID, len, pbVal, &baselen)) {
- uidn(pbVal, baselen);
- }
- else {
- assert(pUID != NULL);
- }
- }
- break;
- default:
- uidu((LPCSTR)NULL);
- break;
+ case 'b':
+ uid((BYTE)atoi(pUID));
+ break;
+ case 'w':
+ uid((WORD)atoi(pUID));
+ break;
+ case 'd':
+ uid((DWORD)_atoi64(pUID));
+ break;
+ case 's':
+ // utf8 -> asci
+ uida(pUID);
+ break;
+ case 'u':
+ uidu(pUID);
+ break;
+ case 'n':
+ pbVal = (PBYTE)mir_base64_decode(pUID, &valLen);
+ if (pbVal != NULL)
+ uidn(pbVal, valLen);
+ break;
+ default:
+ uidu((LPCSTR)NULL);
+ break;
}
}
}
@@ -977,8 +953,7 @@ int CExImContactXML::ImportSetting(LPCSTR pszModule, TiXmlElement *xmlEntry) DBVARIANT dbv = { 0 };
// convert data
- size_t len = 0;
- INT_PTR baselen = NULL;
+ unsigned baselen;
switch (value[0]) {
case 'b': //'b' bVal and cVal are valid
@@ -1002,17 +977,13 @@ int CExImContactXML::ImportSetting(LPCSTR pszModule, TiXmlElement *xmlEntry) dbv.pszVal = (LPSTR)mir_strdup((LPSTR)(value + 1));
break;
case 'n':
- len = strlen(value + 1);
- baselen = Base64DecodeGetRequiredLength(len);
dbv.type = DBVT_BLOB;
- dbv.pbVal = (PBYTE)mir_alloc(baselen +1);
- if (dbv.pbVal != NULL){
- if (Base64Decode((value + 1), len, dbv.pbVal, &baselen))
- dbv.cpbVal = baselen;
- else {
- mir_free(dbv.pbVal);
- return ERROR_NOT_ADDED;
- }
+ dbv.pbVal = (PBYTE)mir_base64_decode(value+1, &baselen);
+ if (dbv.pbVal != NULL)
+ dbv.cpbVal = baselen;
+ else {
+ mir_free(dbv.pbVal);
+ return ERROR_NOT_ADDED;
}
break;
default:
@@ -1067,14 +1038,13 @@ int CExImContactXML::ImportEvent(LPCSTR pszModule, TiXmlElement *xmlEvent) if (!tmp || tmp[0] == 0)
return ERROR_INVALID_VALUE;
- size_t cbSrc = strlen(tmp);
- INT_PTR baselen = Base64DecodeGetRequiredLength(cbSrc);
-
- dbei.pBlob = (PBYTE)_alloca(baselen+1);
- if (Base64Decode(tmp, cbSrc, dbei.pBlob, &baselen)) {
+ unsigned baselen;
+ mir_ptr<BYTE> tmpVal((PBYTE)mir_base64_decode(tmp, &baselen));
+ if (tmpVal != NULL) {
// event owning module
- dbei.szModule = (LPSTR)pszModule;
- dbei.cbBlob = baselen;
+ dbei.pBlob = tmpVal;
+ dbei.cbBlob = baselen;
+ dbei.szModule = (LPSTR)pszModule;
xmlEvent->Attribute("type", (LPINT)&dbei.eventType);
xmlEvent->Attribute("flag", (LPINT)&dbei.flags);
@@ -1082,10 +1052,8 @@ int CExImContactXML::ImportEvent(LPCSTR pszModule, TiXmlElement *xmlEvent) dbei.flags = DBEF_READ;
// search in new and existing contact for existing event to avoid duplicates
- if (/*!_isNewContact && */DB::Event::Exists(_hContact, _hEvent, &dbei)) {
- mir_free(dbei.pBlob);
+ if (/*!_isNewContact && */DB::Event::Exists(_hContact, _hEvent, &dbei))
return ERROR_DUPLICATED;
- }
if ((_hEvent = db_event_add(_hContact, &dbei)) != 0)
return ERROR_OK;
diff --git a/plugins/UserInfoEx/src/ex_import/mir_rfcCodecs.h b/plugins/UserInfoEx/src/ex_import/mir_rfcCodecs.h index d30fddca4a..1a8445c9fe 100644 --- a/plugins/UserInfoEx/src/ex_import/mir_rfcCodecs.h +++ b/plugins/UserInfoEx/src/ex_import/mir_rfcCodecs.h @@ -28,197 +28,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #define TSSMTPMAX_UUENCODE_LINE_LENGTH 45
//=======================================================================
-// Base64Encode/Base64Decode
-// compliant with RFC 2045
-//=======================================================================
-//
-#define BASE64_FLAG_NONE 0
-#define BASE64_FLAG_NOPAD 1
-#define BASE64_FLAG_NOCRLF 2
-
-inline INT_PTR Base64EncodeGetRequiredLength(INT_PTR nSrcLen, DWORD dwFlags = BASE64_FLAG_NONE)
-{
- INT_PTR nRet = nSrcLen*4/3;
-
- if ((dwFlags & BASE64_FLAG_NOPAD) == 0)
- nRet += nSrcLen % 3;
-
- INT_PTR nCRLFs = nRet / 76 + 3;
- INT_PTR nOnLastLine = nRet % 76;
-
- if (nOnLastLine) {
- if (nOnLastLine % 4)
- nRet += 4 - (nOnLastLine % 4);
- }
-
- nCRLFs *= 2;
-
- if ((dwFlags & BASE64_FLAG_NOCRLF) == 0)
- nRet += nCRLFs;
-
- return nRet;
-}
-
-inline INT_PTR Base64DecodeGetRequiredLength(INT_PTR nSrcLen)
-{
- return nSrcLen;
-}
-
-inline BOOL Base64Encode(
- const BYTE *pbSrcData,
- INT_PTR nSrcLen,
- LPSTR szDest,
- INT_PTR *pnDestLen,
- DWORD dwFlags = BASE64_FLAG_NONE)
-{
- static const char s_chBase64EncodingTable[64] = {
- 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q',
- 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
- 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
- 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' };
-
- if (!pbSrcData || !szDest || !pnDestLen)
- return FALSE;
-
- INT_PTR nWritten(0);
- INT_PTR nLen1((nSrcLen / 3) * 4);
- INT_PTR nLen2(nLen1 / 76);
- INT_PTR nLen3(19);
- INT_PTR i, j, k, n;
-
- for (i = 0; i <= nLen2; i++) {
- if (i == nLen2)
- nLen3 = (nLen1 % 76) / 4;
-
- for (j = 0; j < nLen3; j++) {
- DWORD dwCurr(0);
- for (INT_PTR n = 0; n < 3; n++) {
- dwCurr |= *pbSrcData++;
- dwCurr <<= 8;
- }
- for (k = 0; k < 4; k++) {
- BYTE b = (BYTE)(dwCurr >> 26);
- *szDest++ = s_chBase64EncodingTable[b];
- dwCurr <<= 6;
- }
- }
- nWritten += nLen3 * 4;
-
- if ((dwFlags & BASE64_FLAG_NOCRLF) == 0) {
- *szDest++ = '\r';
- *szDest++ = '\n';
- *szDest++ = '\t'; // as vcards have tabs in second line of binary data
- nWritten += 3;
- }
- }
-
- if (nWritten && (dwFlags & BASE64_FLAG_NOCRLF) == 0) {
- szDest -= 2;
- nWritten -= 2;
- }
-
- nLen2 = nSrcLen % 3 ? nSrcLen % 3 + 1 : 0;
- if (nLen2) {
- DWORD dwCurr(0);
- for (n = 0; n < 3; n++)
- {
- if (n < (nSrcLen % 3))
- dwCurr |= *pbSrcData++;
- dwCurr <<= 8;
- }
- for (k = 0; k < nLen2; k++) {
- BYTE b = (BYTE)(dwCurr >> 26);
- *szDest++ = s_chBase64EncodingTable[b];
- dwCurr <<= 6;
- }
- nWritten+= nLen2;
- if ((dwFlags & BASE64_FLAG_NOPAD) == 0) {
- nLen3 = nLen2 ? 4 - nLen2 : 0;
- for (j = 0; j < nLen3; j++) {
- *szDest++ = '=';
- }
- nWritten+= nLen3;
- }
- }
-
- *pnDestLen = nWritten;
- return TRUE;
-}
-
-inline INT_PTR DecodeBase64Char(UINT ch) throw()
-{
- // returns -1 if the character is invalid
- // or should be skipped
- // otherwise, returns the 6-bit code for the character
- // from the encoding table
- if (ch >= 'A' && ch <= 'Z')
- return ch - 'A' + 0; // 0 range starts at 'A'
- if (ch >= 'a' && ch <= 'z')
- return ch - 'a' + 26; // 26 range starts at 'a'
- if (ch >= '0' && ch <= '9')
- return ch - '0' + 52; // 52 range starts at '0'
- if (ch == '+')
- return 62;
- if (ch == '/')
- return 63;
- return -1;
-}
-
-inline BOOL Base64Decode(LPCSTR szSrc, INT_PTR nSrcLen, BYTE *pbDest, INT_PTR *pnDestLen) throw()
-{
- // walk the source buffer
- // each four character sequence is converted to 3 bytes
- // CRLFs and =, and any characters not in the encoding table
- // are skiped
-
- if (szSrc == NULL || pnDestLen == NULL) {
- return FALSE;
- }
-
- LPCSTR szSrcEnd = szSrc + nSrcLen;
- INT_PTR nWritten = 0;
-
- BOOL bOverflow = (pbDest == NULL) ? TRUE : FALSE;
-
- while (szSrc < szSrcEnd) {
- DWORD dwCurr = 0;
- INT_PTR i;
- INT_PTR nBits = 0;
- for (i=0; i<4; i++) {
- if (szSrc >= szSrcEnd)
- break;
- INT_PTR nCh = DecodeBase64Char(*szSrc);
- szSrc++;
- if (nCh == -1) {
- // skip this char
- i--;
- continue;
- }
- dwCurr <<= 6;
- dwCurr |= nCh;
- nBits += 6;
- }
-
- if (!bOverflow && nWritten + (nBits/8) > (*pnDestLen))
- bOverflow = TRUE;
-
- // dwCurr has the 3 bytes to write to the output buffer
- // left to right
- dwCurr <<= 24-nBits;
- for (i=0; i<nBits/8; i++) {
- if (!bOverflow) {
- *pbDest = (BYTE) ((dwCurr & 0x00ff0000) >> 16);
- pbDest++;
- }
- dwCurr <<= 8;
- nWritten++;
- }
- }
- *pnDestLen = nWritten;
- return bOverflow ? FALSE:TRUE;
-}
-
-//=======================================================================
// Quoted Printable encode/decode
// compliant with RFC 2045
//=======================================================================
@@ -242,7 +51,7 @@ inline INT_PTR QPDecodeGetRequiredLength(INT_PTR nSrcLen) inline BOOL QPEncode(BYTE* pbSrcData, INT_PTR nSrcLen, LPSTR szDest, INT_PTR* pnDestLen, BYTE *bEncoded, DWORD dwFlags = 0)
{
//The hexadecimal character set
- static const CHAR s_chHexChars[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
+ static const CHAR s_chHexChars[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F'};
INT_PTR nRead = 0, nWritten = 0, nLineLen = 0;
CHAR ch;
@@ -272,7 +81,7 @@ inline BOOL QPEncode(BYTE* pbSrcData, INT_PTR nSrcLen, LPSTR szDest, INT_PTR* pn *szDest++ = ch;
nWritten++;
nLineLen++;
- }
+ }
else {
*szDest++ = '=';
*szDest++ = s_chHexChars[(ch >> 4) & 0x0F];
|