From 1ef4b90ce07ccc4e372f0b8179c69438bff24ae2 Mon Sep 17 00:00:00 2001 From: George Hazan Date: Fri, 3 Feb 2017 16:08:38 +0300 Subject: mime type standardization --- bin10/lib/mir_app.lib | Bin 102750 -> 103298 bytes bin10/lib/mir_app64.lib | Bin 98158 -> 98670 bytes bin12/lib/mir_app.lib | Bin 102750 -> 103298 bytes bin12/lib/mir_app64.lib | Bin 98158 -> 98670 bytes bin14/lib/mir_app.lib | Bin 102750 -> 103298 bytes bin14/lib/mir_app64.lib | Bin 98158 -> 98670 bytes include/m_core.h | 6 ++++++ protocols/JabberG/src/jabber_iq_handlers.cpp | 11 +++------- protocols/JabberG/src/jabber_iqid.cpp | 19 ++++++---------- protocols/JabberG/src/jabber_util.cpp | 14 ++---------- protocols/JabberG/src/jabber_vcard.cpp | 10 ++------- protocols/MRA/src/MraAvatars.cpp | 23 ++------------------ protocols/Tlen/src/tlen_avatar.cpp | 17 ++------------- src/mir_app/src/mir_app.def | 2 ++ src/mir_app/src/mir_app64.def | 2 ++ src/mir_app/src/proto_utils.cpp | 31 +++++++++++++++++++++++++++ 16 files changed, 59 insertions(+), 76 deletions(-) diff --git a/bin10/lib/mir_app.lib b/bin10/lib/mir_app.lib index a7beab2991..c2a0c3e080 100644 Binary files a/bin10/lib/mir_app.lib and b/bin10/lib/mir_app.lib differ diff --git a/bin10/lib/mir_app64.lib b/bin10/lib/mir_app64.lib index e7fa88fb0e..c9c6976d56 100644 Binary files a/bin10/lib/mir_app64.lib and b/bin10/lib/mir_app64.lib differ diff --git a/bin12/lib/mir_app.lib b/bin12/lib/mir_app.lib index a7beab2991..c2a0c3e080 100644 Binary files a/bin12/lib/mir_app.lib and b/bin12/lib/mir_app.lib differ diff --git a/bin12/lib/mir_app64.lib b/bin12/lib/mir_app64.lib index e7fa88fb0e..c9c6976d56 100644 Binary files a/bin12/lib/mir_app64.lib and b/bin12/lib/mir_app64.lib differ diff --git a/bin14/lib/mir_app.lib b/bin14/lib/mir_app.lib index a7beab2991..c2a0c3e080 100644 Binary files a/bin14/lib/mir_app.lib and b/bin14/lib/mir_app.lib differ diff --git a/bin14/lib/mir_app64.lib b/bin14/lib/mir_app64.lib index e7fa88fb0e..c9c6976d56 100644 Binary files a/bin14/lib/mir_app64.lib and b/bin14/lib/mir_app64.lib differ diff --git a/include/m_core.h b/include/m_core.h index e02d594c66..8b834dbb62 100644 --- a/include/m_core.h +++ b/include/m_core.h @@ -335,6 +335,12 @@ MIR_APP_DLL(int) ProtoGetAvatarFormat(const wchar_t *ptszFileName); // detects image format by its contents MIR_APP_DLL(int) ProtoGetAvatarFileFormat(const wchar_t *ptszFileName); +// returns the mime type according to a picture type (PA_*) passed +MIR_APP_DLL(const wchar_t*) ProtoGetAvatarMimeType(int iFileType); + +// returns the picture type (PA_*) according to a mime type passed +MIR_APP_DLL(int) ProtoGetAvatarFormatByMimeType(const wchar_t *pwszMimeType); + // returns the image format and extension by the first bytes of picture // ptszExtension might be NULL #if defined( __cplusplus ) diff --git a/protocols/JabberG/src/jabber_iq_handlers.cpp b/protocols/JabberG/src/jabber_iq_handlers.cpp index 924b335afc..e532340acc 100644 --- a/protocols/JabberG/src/jabber_iq_handlers.cpp +++ b/protocols/JabberG/src/jabber_iq_handlers.cpp @@ -151,14 +151,9 @@ BOOL CJabberProto::OnIqRequestAvatar(HXML, CJabberIqInfo *pInfo) if (pictureType == PA_FORMAT_UNKNOWN) return TRUE; - wchar_t *szMimeType; - switch (pictureType) { - case PA_FORMAT_JPEG: szMimeType = L"image/jpeg"; break; - case PA_FORMAT_GIF: szMimeType = L"image/gif"; break; - case PA_FORMAT_PNG: szMimeType = L"image/png"; break; - case PA_FORMAT_BMP: szMimeType = L"image/bmp"; break; - default: return TRUE; - } + const wchar_t *szMimeType = ProtoGetAvatarMimeType(pictureType); + if (szMimeType == NULL) + return TRUE; wchar_t szFileName[MAX_PATH]; GetAvatarFileName(NULL, szFileName, _countof(szFileName)); diff --git a/protocols/JabberG/src/jabber_iqid.cpp b/protocols/JabberG/src/jabber_iqid.cpp index a91cd1d3d4..df2fd4cf42 100644 --- a/protocols/JabberG/src/jabber_iqid.cpp +++ b/protocols/JabberG/src/jabber_iqid.cpp @@ -1405,19 +1405,14 @@ void CJabberProto::OnIqResultGotAvatar(MCONTACT hContact, HXML n, const wchar_t return; int pictureType; - if (mimeType != NULL) { - if (!mir_wstrcmp(mimeType, L"image/jpeg")) pictureType = PA_FORMAT_JPEG; - else if (!mir_wstrcmp(mimeType, L"image/png")) pictureType = PA_FORMAT_PNG; - else if (!mir_wstrcmp(mimeType, L"image/gif")) pictureType = PA_FORMAT_GIF; - else if (!mir_wstrcmp(mimeType, L"image/bmp")) pictureType = PA_FORMAT_BMP; - else { -LBL_ErrFormat: - debugLogW(L"Invalid mime type specified for picture: %s", mimeType); - return; - } + if (mimeType != NULL) + pictureType = ProtoGetAvatarFormatByMimeType(mimeType); + else + pictureType = ProtoGetBufferFormat(body, 0); + if (pictureType == PA_FORMAT_UNKNOWN) { + debugLogW(L"Invalid mime type specified for picture: %s", mimeType); + return; } - else if ((pictureType = ProtoGetBufferFormat(body, 0)) == PA_FORMAT_UNKNOWN) - goto LBL_ErrFormat; PROTO_AVATAR_INFORMATION ai; ai.format = pictureType; diff --git a/protocols/JabberG/src/jabber_util.cpp b/protocols/JabberG/src/jabber_util.cpp index c1fb18a4e2..19de86f090 100644 --- a/protocols/JabberG/src/jabber_util.cpp +++ b/protocols/JabberG/src/jabber_util.cpp @@ -587,20 +587,10 @@ wchar_t* __stdcall JabberStripJid(const wchar_t *jid, wchar_t *dest, size_t dest LPCTSTR __stdcall JabberGetPictureType(HXML node, const char *picBuf) { if (LPCTSTR ptszType = XmlGetText(XmlGetChild(node, "TYPE"))) - if (!mir_wstrcmp(ptszType, L"image/jpeg") || - !mir_wstrcmp(ptszType, L"image/png") || - !mir_wstrcmp(ptszType, L"image/gif") || - !mir_wstrcmp(ptszType, L"image/bmp")) + if (ProtoGetAvatarFormatByMimeType(ptszType) != PA_FORMAT_UNKNOWN) return ptszType; - switch (ProtoGetBufferFormat(picBuf)) { - case PA_FORMAT_GIF: return L"image/gif"; - case PA_FORMAT_BMP: return L"image/bmp"; - case PA_FORMAT_PNG: return L"image/png"; - case PA_FORMAT_JPEG: return L"image/jpeg"; - } - - return NULL; + return ProtoGetAvatarMimeType(ProtoGetBufferFormat(picBuf)); } ///////////////////////////////////////////////////////////////////////////////////////// diff --git a/protocols/JabberG/src/jabber_vcard.cpp b/protocols/JabberG/src/jabber_vcard.cpp index 2150161afd..16e57c3817 100644 --- a/protocols/JabberG/src/jabber_vcard.cpp +++ b/protocols/JabberG/src/jabber_vcard.cpp @@ -1132,15 +1132,9 @@ void CJabberProto::SetServerVcard(BOOL bPhotoChanged, wchar_t* szPhotoFileName) DWORD nRead; if (ReadFile(hFile, buffer, st.st_size, &nRead, NULL)) { ptrA str(mir_base64_encode((PBYTE)(LPSTR)buffer, nRead)); - if (str != NULL) { + const wchar_t *szFileType = ProtoGetAvatarMimeType(ProtoGetBufferFormat(buffer)); + if (str != NULL && szFileType != NULL) { n = v << XCHILD(L"PHOTO"); - wchar_t *szFileType; - switch (ProtoGetBufferFormat(buffer)) { - case PA_FORMAT_PNG: szFileType = L"image/png"; break; - case PA_FORMAT_GIF: szFileType = L"image/gif"; break; - case PA_FORMAT_BMP: szFileType = L"image/bmp"; break; - default: szFileType = L"image/jpeg"; break; - } n << XCHILD(L"TYPE", szFileType); n << XCHILD(L"BINVAL", _A2T(str)); diff --git a/protocols/MRA/src/MraAvatars.cpp b/protocols/MRA/src/MraAvatars.cpp index 7c80a1230a..a61094c670 100644 --- a/protocols/MRA/src/MraAvatars.cpp +++ b/protocols/MRA/src/MraAvatars.cpp @@ -3,19 +3,6 @@ #define PA_FORMAT_MAX 7 -const LPSTR lpcszContentType[9] = -{ - "", // PA_FORMAT_UNKNOWN - "image/png", // PA_FORMAT_PNG - "image/jpeg", // PA_FORMAT_JPEG - "image/icon", // PA_FORMAT_ICON - "image/x-xbitmap", // PA_FORMAT_BMP - "image/gif", // PA_FORMAT_GIF - "", // PA_FORMAT_SWF - "", // PA_FORMAT_XML - NULL -}; - struct MRA_AVATARS_QUEUE : public FIFO_MT { HNETLIBUSER hNetlibUser; @@ -405,14 +392,8 @@ DWORD MraAvatarsHttpTransaction(HNETLIBCONN hConnection, DWORD dwRequestType, LP *pbKeepAlive = !_strnicmp(pnlhr->headers[i].szValue, "keep-alive", 10); } else if (!_strnicmp(pnlhr->headers[i].szName, "Content-Type", 12)) { - if (pdwFormat) { - for (DWORD j = 0; j < PA_FORMAT_MAX; j++) { - if (!_stricmp(pnlhr->headers[i].szValue, lpcszContentType[j])) { - *pdwFormat = j; - break; - } - } - } + if (pdwFormat) + *pdwFormat = ProtoGetAvatarFormatByMimeType(_A2T(pnlhr->headers[i].szValue)); } else if (!_strnicmp(pnlhr->headers[i].szName, "Content-Length", 14)) { if (pdwAvatarSize) diff --git a/protocols/Tlen/src/tlen_avatar.cpp b/protocols/Tlen/src/tlen_avatar.cpp index bd9c031b3a..3689a8bb75 100644 --- a/protocols/Tlen/src/tlen_avatar.cpp +++ b/protocols/Tlen/src/tlen_avatar.cpp @@ -274,22 +274,9 @@ static void TlenGetAvatarThread(void *ptr) if (resp != NULL) { if (resp->resultCode/100 == 2) { if (resp->dataLength > 0) { - int i; - for (i=0; iheadersCount; i++ ) { + for (int i=0; iheadersCount; i++ ) { if (!strcmpi(resp->headers[i].szName, "Content-Type")) { - if (!strcmpi(resp->headers[i].szValue, "image/png")) - format = PA_FORMAT_PNG; - else if (!strcmpi(resp->headers[i].szValue, "image/x-png")) - format = PA_FORMAT_PNG; - else if (!strcmpi(resp->headers[i].szValue, "image/jpeg")) - format = PA_FORMAT_JPEG; - else if (!strcmpi(resp->headers[i].szValue, "image/jpg")) - format = PA_FORMAT_JPEG; - else if (!strcmpi(resp->headers[i].szValue, "image/gif")) - format = PA_FORMAT_GIF; - else if (!strcmpi(resp->headers[i].szValue, "image/bmp")) - format = PA_FORMAT_BMP; - + format = ProtoGetAvatarFormatByMimeType(_A2T(resp->headers[i].szValue)); break; } } diff --git a/src/mir_app/src/mir_app.def b/src/mir_app/src/mir_app.def index e3106f9927..473761ff35 100644 --- a/src/mir_app/src/mir_app.def +++ b/src/mir_app/src/mir_app.def @@ -396,3 +396,5 @@ Netlib_NtlmCreateResponse @383 ?makeBlob@DB_AUTH_BLOB@@AAEPAEXZ @397 NONAME ?size@DB_AUTH_BLOB@@QBEKXZ @398 NONAME Chat_UnescapeTags @399 NONAME +ProtoGetAvatarFormatByMimeType @400 +ProtoGetAvatarMimeType @401 diff --git a/src/mir_app/src/mir_app64.def b/src/mir_app/src/mir_app64.def index da54dec2d5..044ee43a95 100644 --- a/src/mir_app/src/mir_app64.def +++ b/src/mir_app/src/mir_app64.def @@ -396,3 +396,5 @@ Netlib_NtlmCreateResponse @383 ?makeBlob@DB_AUTH_BLOB@@AEAAPEAEXZ @397 NONAME ?size@DB_AUTH_BLOB@@QEBAKXZ @398 NONAME Chat_UnescapeTags @399 NONAME +ProtoGetAvatarFormatByMimeType @400 +ProtoGetAvatarMimeType @401 diff --git a/src/mir_app/src/proto_utils.cpp b/src/mir_app/src/proto_utils.cpp index 94e54a0ed3..32d8c10f8c 100644 --- a/src/mir_app/src/proto_utils.cpp +++ b/src/mir_app/src/proto_utils.cpp @@ -234,6 +234,37 @@ MIR_APP_DLL(int) ProtoGetAvatarFileFormat(const wchar_t *ptszFileName) return (res && dwBytes == _countof(buf)) ? ProtoGetBufferFormat(buf) : PA_FORMAT_UNKNOWN; } +///////////////////////////////////////////////////////////////////////////////////////// +// mime type functions + +static wchar_t *wszMimeTypes[] = +{ + L"binary", // PA_FORMAT_UNKNOWN + L"image/png", // PA_FORMAT_PNG + L"image/jpeg", // PA_FORMAT_JPEG + L"image/icon", // PA_FORMAT_ICON + L"image/bmp", // PA_FORMAT_BMP + L"image/gif", // PA_FORMAT_GIF + L"image/swf", // PA_FORMAT_SWF + L"application/xml" // PA_FORMAT_XML +}; + +MIR_APP_DLL(const wchar_t*) ProtoGetAvatarMimeType(int iFileType) +{ + if (iFileType >= 0 && iFileType <= _countof(wszMimeTypes)) + return wszMimeTypes[iFileType]; + return NULL; +} + +MIR_APP_DLL(int) ProtoGetAvatarFormatByMimeType(const wchar_t *pwszMimeType) +{ + for (int i = 0; i < _countof(wszMimeTypes); i++) + if (!mir_wstrcmp(pwszMimeType, wszMimeTypes[i])) + return i; + + return PA_FORMAT_UNKNOWN; +} + ///////////////////////////////////////////////////////////////////////////////////////// // default PROTO_INTERFACE method implementations -- cgit v1.2.3