mimeintl.c 17.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
/*
 * MIME OLE International interface
 *
 * Copyright 2008 Huw Davies for CodeWeavers
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

#define COBJMACROS
#define NONAMELESSUNION

#include <stdarg.h>
#include <stdio.h>

#include "windef.h"
#include "winbase.h"
#include "winuser.h"
#include "winnls.h"
#include "objbase.h"
#include "ole2.h"
#include "mimeole.h"
34
#include "mlang.h"
35 36 37 38 39 40 41 42

#include "wine/list.h"
#include "wine/debug.h"

#include "inetcomm_private.h"

WINE_DEFAULT_DEBUG_CHANNEL(inetcomm);

43 44 45 46 47 48
typedef struct
{
    struct list entry;
    INETCSETINFO cs_info;
} charset_entry;

49 50
typedef struct
{
51
    IMimeInternational IMimeInternational_iface;
52
    LONG refs;
53 54 55 56
    CRITICAL_SECTION cs;

    struct list charsets;
    LONG next_charset_handle;
57
    HCHARSET default_charset;
58
} internat_impl;
59

60
static inline internat_impl *impl_from_IMimeInternational(IMimeInternational *iface)
61
{
62
    return CONTAINING_RECORD(iface, internat_impl, IMimeInternational_iface);
63 64
}

65 66 67 68 69 70
static inline HRESULT get_mlang(IMultiLanguage **ml)
{
    return CoCreateInstance(&CLSID_CMultiLanguage, NULL,  CLSCTX_INPROC_SERVER | CLSCTX_INPROC_HANDLER,
                            &IID_IMultiLanguage, (void **)ml);
}

71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
static HRESULT WINAPI MimeInternat_QueryInterface( IMimeInternational *iface, REFIID riid, LPVOID *ppobj )
{
    if (IsEqualGUID(riid, &IID_IUnknown) ||
        IsEqualGUID(riid, &IID_IMimeInternational))
    {
        IMimeInternational_AddRef( iface );
        *ppobj = iface;
        return S_OK;
    }

    FIXME("interface %s not implemented\n", debugstr_guid(riid));
    return E_NOINTERFACE;
}

static ULONG WINAPI MimeInternat_AddRef( IMimeInternational *iface )
{
87
    internat_impl *This = impl_from_IMimeInternational( iface );
88 89 90 91 92
    return InterlockedIncrement(&This->refs);
}

static ULONG WINAPI MimeInternat_Release( IMimeInternational *iface )
{
93
    internat_impl *This = impl_from_IMimeInternational( iface );
94 95 96 97 98
    ULONG refs;

    refs = InterlockedDecrement(&This->refs);
    if (!refs)
    {
99 100 101 102 103 104 105
        charset_entry *charset, *cursor2;

        LIST_FOR_EACH_ENTRY_SAFE(charset, cursor2, &This->charsets, charset_entry, entry)
        {
            list_remove(&charset->entry);
            HeapFree(GetProcessHeap(), 0, charset);
        }
106 107
        This->cs.DebugInfo->Spare[0] = 0;
        DeleteCriticalSection(&This->cs);
108 109 110 111 112 113 114 115
        HeapFree(GetProcessHeap(), 0, This);
    }

    return refs;
}

static HRESULT WINAPI MimeInternat_SetDefaultCharset(IMimeInternational *iface, HCHARSET hCharset)
{
116
    internat_impl *This = impl_from_IMimeInternational( iface );
117 118 119 120 121 122 123 124 125

    TRACE("(%p)->(%p)\n", iface, hCharset);

    if(hCharset == NULL) return E_INVALIDARG;
    /* FIXME check hCharset is valid */

    InterlockedExchangePointer(&This->default_charset, hCharset);

    return S_OK;
126 127 128 129
}

static HRESULT WINAPI MimeInternat_GetDefaultCharset(IMimeInternational *iface, LPHCHARSET phCharset)
{
130
    internat_impl *This = impl_from_IMimeInternational( iface );
131 132 133 134 135 136 137 138 139 140 141 142 143 144
    HRESULT hr = S_OK;

    TRACE("(%p)->(%p)\n", iface, phCharset);

    if(This->default_charset == NULL)
    {
        HCHARSET hcs;
        hr = IMimeInternational_GetCodePageCharset(iface, GetACP(), CHARSET_BODY, &hcs);
        if(SUCCEEDED(hr))
            InterlockedCompareExchangePointer(&This->default_charset, hcs, NULL);
    }
    *phCharset = This->default_charset;

    return hr;
145 146
}

147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
static HRESULT mlang_getcodepageinfo(UINT cp, MIMECPINFO *mlang_cp_info)
{
    HRESULT hr;
    IMultiLanguage *ml;

    hr = get_mlang(&ml);

    if(SUCCEEDED(hr))
    {
        hr = IMultiLanguage_GetCodePageInfo(ml, cp, mlang_cp_info);
        IMultiLanguage_Release(ml);
    }
    return hr;
}

162 163 164 165
static HRESULT WINAPI MimeInternat_GetCodePageCharset(IMimeInternational *iface, CODEPAGEID cpiCodePage,
                                                      CHARSETTYPE ctCsetType,
                                                      LPHCHARSET phCharset)
{
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
    HRESULT hr;
    MIMECPINFO mlang_cp_info;

    TRACE("(%p)->(%d, %d, %p)\n", iface, cpiCodePage, ctCsetType, phCharset);

    *phCharset = NULL;

    hr = mlang_getcodepageinfo(cpiCodePage, &mlang_cp_info);
    if(SUCCEEDED(hr))
    {
        const WCHAR *charset_nameW = NULL;
        char *charset_name;
        DWORD len;

        switch(ctCsetType)
        {
        case CHARSET_BODY:
            charset_nameW = mlang_cp_info.wszBodyCharset;
            break;
        case CHARSET_HEADER:
            charset_nameW = mlang_cp_info.wszHeaderCharset;
            break;
        case CHARSET_WEB:
            charset_nameW = mlang_cp_info.wszWebCharset;
            break;
191 192
        default:
            return MIME_E_INVALID_CHARSET_TYPE;
193 194 195 196 197 198 199 200
        }
        len = WideCharToMultiByte(CP_ACP, 0, charset_nameW, -1, NULL, 0, NULL, NULL);
        charset_name = HeapAlloc(GetProcessHeap(), 0, len);
        WideCharToMultiByte(CP_ACP, 0, charset_nameW, -1, charset_name, len, NULL, NULL);
        hr = IMimeInternational_FindCharset(iface, charset_name, phCharset);
        HeapFree(GetProcessHeap(), 0, charset_name);
    }
    return hr;
201 202
}

203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
static HRESULT mlang_getcsetinfo(const char *charset, MIMECSETINFO *mlang_info)
{
    DWORD len = MultiByteToWideChar(CP_ACP, 0, charset, -1, NULL, 0);
    BSTR bstr = SysAllocStringLen(NULL, len - 1);
    HRESULT hr;
    IMultiLanguage *ml;

    MultiByteToWideChar(CP_ACP, 0, charset, -1, bstr, len);

    hr = get_mlang(&ml);

    if(SUCCEEDED(hr))
    {
        hr = IMultiLanguage_GetCharsetInfo(ml, bstr, mlang_info);
        IMultiLanguage_Release(ml);
    }
    SysFreeString(bstr);
    if(FAILED(hr)) hr = MIME_E_NOT_FOUND;
    return hr;
}

static HCHARSET add_charset(struct list *list, MIMECSETINFO *mlang_info, HCHARSET handle)
{
    charset_entry *charset = HeapAlloc(GetProcessHeap(), 0, sizeof(*charset));

    WideCharToMultiByte(CP_ACP, 0, mlang_info->wszCharset, -1,
                        charset->cs_info.szName, sizeof(charset->cs_info.szName), NULL, NULL);
    charset->cs_info.cpiWindows = mlang_info->uiCodePage;
    charset->cs_info.cpiInternet = mlang_info->uiInternetEncoding;
    charset->cs_info.hCharset = handle;
    charset->cs_info.dwReserved1 = 0;
    list_add_head(list, &charset->entry);

    return charset->cs_info.hCharset;
}

239 240 241
static HRESULT WINAPI MimeInternat_FindCharset(IMimeInternational *iface, LPCSTR pszCharset,
                                               LPHCHARSET phCharset)
{
242
    internat_impl *This = impl_from_IMimeInternational( iface );
243 244 245 246 247 248 249 250 251 252 253
    HRESULT hr = MIME_E_NOT_FOUND;
    charset_entry *charset;

    TRACE("(%p)->(%s, %p)\n", iface, debugstr_a(pszCharset), phCharset);

    *phCharset = NULL;

    EnterCriticalSection(&This->cs);

    LIST_FOR_EACH_ENTRY(charset, &This->charsets, charset_entry, entry)
    {
254
        if(!lstrcmpiA(charset->cs_info.szName, pszCharset))
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
        {
            *phCharset = charset->cs_info.hCharset;
            hr = S_OK;
            break;
        }
    }

    if(hr == MIME_E_NOT_FOUND)
    {
        MIMECSETINFO mlang_info;

        LeaveCriticalSection(&This->cs);
        hr = mlang_getcsetinfo(pszCharset, &mlang_info);
        EnterCriticalSection(&This->cs);

        if(SUCCEEDED(hr))
            *phCharset = add_charset(&This->charsets, &mlang_info,
272
                                     UlongToHandle(InterlockedIncrement(&This->next_charset_handle)));
273 274 275 276
    }

    LeaveCriticalSection(&This->cs);
    return hr;
277 278 279 280 281
}

static HRESULT WINAPI MimeInternat_GetCharsetInfo(IMimeInternational *iface, HCHARSET hCharset,
                                                  LPINETCSETINFO pCsetInfo)
{
282
    internat_impl *This = impl_from_IMimeInternational( iface );
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
    HRESULT hr = MIME_E_INVALID_HANDLE;
    charset_entry *charset;

    TRACE("(%p)->(%p, %p)\n", iface, hCharset, pCsetInfo);

    EnterCriticalSection(&This->cs);

    LIST_FOR_EACH_ENTRY(charset, &This->charsets, charset_entry, entry)
    {
        if(charset->cs_info.hCharset ==  hCharset)
        {
            *pCsetInfo = charset->cs_info;
            hr = S_OK;
            break;
        }
    }

    LeaveCriticalSection(&This->cs);

    return hr;
303 304 305 306 307 308 309 310 311
}

static HRESULT WINAPI MimeInternat_GetCodePageInfo(IMimeInternational *iface, CODEPAGEID cpiCodePage,
                                                   LPCODEPAGEINFO pCodePageInfo)
{
    FIXME("stub\n");
    return E_NOTIMPL;
}

312 313 314
static HRESULT WINAPI MimeInternat_CanConvertCodePages(IMimeInternational *iface, CODEPAGEID cpiSource,
                                                       CODEPAGEID cpiDest)
{
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
    HRESULT hr;
    IMultiLanguage *ml;

    TRACE("(%p)->(%d, %d)\n", iface, cpiSource, cpiDest);

    /* Could call mlang.IsConvertINetStringAvailable() to avoid the COM overhead if need be. */

    hr = get_mlang(&ml);
    if(SUCCEEDED(hr))
    {
        hr = IMultiLanguage_IsConvertible(ml, cpiSource, cpiDest);
        IMultiLanguage_Release(ml);
    }

    return hr;
330 331
}

332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
static HRESULT WINAPI MimeInternat_DecodeHeader(IMimeInternational *iface, HCHARSET hCharset,
                                                LPCSTR pszData,
                                                LPPROPVARIANT pDecoded,
                                                LPRFC1522INFO pRfc1522Info)
{
    FIXME("stub\n");
    return E_NOTIMPL;
}

static HRESULT WINAPI MimeInternat_EncodeHeader(IMimeInternational *iface, HCHARSET hCharset,
                                                LPPROPVARIANT pData,
                                                LPSTR *ppszEncoded,
                                                LPRFC1522INFO pRfc1522Info)
{
    FIXME("stub\n");
    return E_NOTIMPL;
}

static HRESULT WINAPI MimeInternat_ConvertBuffer(IMimeInternational *iface, CODEPAGEID cpiSource,
351
                                                 CODEPAGEID cpiDest, LPBLOB pIn, LPBLOB pOut,
352 353
                                                 ULONG *pcbRead)
{
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
    HRESULT hr;
    IMultiLanguage *ml;

    TRACE("(%p)->(%d, %d, %p, %p, %p)\n", iface, cpiSource, cpiDest, pIn, pOut, pcbRead);

    *pcbRead = 0;
    pOut->cbSize = 0;

    /* Could call mlang.ConvertINetString() to avoid the COM overhead if need be. */

    hr = get_mlang(&ml);
    if(SUCCEEDED(hr))
    {
        DWORD mode = 0;
        UINT in_size = pIn->cbSize, out_size;

        hr = IMultiLanguage_ConvertString(ml, &mode, cpiSource, cpiDest, pIn->pBlobData, &in_size,
                                          NULL, &out_size);
        if(hr == S_OK) /* S_FALSE means the conversion could not be performed */
        {
            pOut->pBlobData = CoTaskMemAlloc(out_size);
            if(!pOut->pBlobData)
                hr = E_OUTOFMEMORY;
            else
            {
                mode = 0;
                in_size = pIn->cbSize;
                hr = IMultiLanguage_ConvertString(ml, &mode, cpiSource, cpiDest, pIn->pBlobData, &in_size,
                                                  pOut->pBlobData, &out_size);

                if(hr == S_OK)
                {
                    *pcbRead = in_size;
                    pOut->cbSize = out_size;
                }
                else
                    CoTaskMemFree(pOut->pBlobData);
            }
        }
        IMultiLanguage_Release(ml);
    }

    return hr;
397 398 399
}

static HRESULT WINAPI MimeInternat_ConvertString(IMimeInternational *iface, CODEPAGEID cpiSource,
400
                                                 CODEPAGEID cpiDest, LPPROPVARIANT pIn,
401 402
                                                 LPPROPVARIANT pOut)
{
403 404 405 406 407 408 409 410 411 412
    HRESULT hr;
    int src_len;
    IMultiLanguage *ml;

    TRACE("(%p)->(%d, %d, %p %p)\n", iface, cpiSource, cpiDest, pIn, pOut);

    switch(pIn->vt)
    {
    case VT_LPSTR:
        if(cpiSource == CP_UNICODE) cpiSource = GetACP();
413
        src_len = strlen(pIn->pszVal);
414 415 416
        break;
    case VT_LPWSTR:
        cpiSource = CP_UNICODE;
417
        src_len = lstrlenW(pIn->pwszVal) * sizeof(WCHAR);
418 419 420 421 422 423 424 425 426 427 428
        break;
    default:
        return E_INVALIDARG;
    }

    hr = get_mlang(&ml);
    if(SUCCEEDED(hr))
    {
        DWORD mode = 0;
        UINT in_size = src_len, out_size;

429
        hr = IMultiLanguage_ConvertString(ml, &mode, cpiSource, cpiDest, (BYTE*)pIn->pszVal, &in_size,
430 431 432 433 434
                                          NULL, &out_size);
        if(hr == S_OK) /* S_FALSE means the conversion could not be performed */
        {
            out_size += (cpiDest == CP_UNICODE) ? sizeof(WCHAR) : sizeof(char);

435 436
            pOut->pszVal = CoTaskMemAlloc(out_size);
            if(!pOut->pszVal)
437 438 439 440 441
                hr = E_OUTOFMEMORY;
            else
            {
                mode = 0;
                in_size = src_len;
442 443
                hr = IMultiLanguage_ConvertString(ml, &mode, cpiSource, cpiDest, (BYTE*)pIn->pszVal, &in_size,
                                                  (BYTE*)pOut->pszVal, &out_size);
444 445 446 447 448

                if(hr == S_OK)
                {
                    if(cpiDest == CP_UNICODE)
                    {
449
                        pOut->pwszVal[out_size / sizeof(WCHAR)] = 0;
450 451 452 453
                        pOut->vt = VT_LPWSTR;
                    }
                    else
                    {
454
                        pOut->pszVal[out_size] = '\0';
455 456 457 458
                        pOut->vt = VT_LPSTR;
                    }
                }
                else
459
                    CoTaskMemFree(pOut->pszVal);
460 461 462 463 464
            }
        }
        IMultiLanguage_Release(ml);
    }
    return hr;
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
}

static HRESULT WINAPI MimeInternat_MLANG_ConvertInetReset(IMimeInternational *iface)
{
    FIXME("stub\n");
    return E_NOTIMPL;
}

static HRESULT WINAPI MimeInternat_MLANG_ConvertInetString(IMimeInternational *iface, CODEPAGEID cpiSource,
                                                           CODEPAGEID cpiDest,
                                                           LPCSTR pSource,
                                                           int *pnSizeOfSource,
                                                           LPSTR pDestination,
                                                           int *pnDstSize)
{
    FIXME("stub\n");
    return E_NOTIMPL;
}

static HRESULT WINAPI MimeInternat_Rfc1522Decode(IMimeInternational *iface, LPCSTR pszValue,
485
                                                 LPSTR pszCharset,
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
                                                 ULONG cchmax,
                                                 LPSTR *ppszDecoded)
{
    FIXME("stub\n");
    return E_NOTIMPL;
}

static HRESULT WINAPI MimeInternat_Rfc1522Encode(IMimeInternational *iface, LPCSTR pszValue,
                                                 HCHARSET hCharset,
                                                 LPSTR *ppszEncoded)
{
    FIXME("stub\n");
    return E_NOTIMPL;
}

static IMimeInternationalVtbl mime_internat_vtbl =
{
    MimeInternat_QueryInterface,
    MimeInternat_AddRef,
    MimeInternat_Release,
    MimeInternat_SetDefaultCharset,
    MimeInternat_GetDefaultCharset,
    MimeInternat_GetCodePageCharset,
    MimeInternat_FindCharset,
    MimeInternat_GetCharsetInfo,
    MimeInternat_GetCodePageInfo,
512
    MimeInternat_CanConvertCodePages,
513 514 515 516 517 518 519 520 521 522
    MimeInternat_DecodeHeader,
    MimeInternat_EncodeHeader,
    MimeInternat_ConvertBuffer,
    MimeInternat_ConvertString,
    MimeInternat_MLANG_ConvertInetReset,
    MimeInternat_MLANG_ConvertInetString,
    MimeInternat_Rfc1522Decode,
    MimeInternat_Rfc1522Encode
};

523
static internat_impl *global_internat;
524 525 526 527

HRESULT MimeInternational_Construct(IMimeInternational **internat)
{
    global_internat = HeapAlloc(GetProcessHeap(), 0, sizeof(*global_internat));
528
    global_internat->IMimeInternational_iface.lpVtbl = &mime_internat_vtbl;
529
    global_internat->refs = 0;
530
    InitializeCriticalSection(&global_internat->cs);
531
    global_internat->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": global_internat.cs");
532 533 534

    list_init(&global_internat->charsets);
    global_internat->next_charset_handle = 0;
535
    global_internat->default_charset = NULL;
536

537
    *internat = &global_internat->IMimeInternational_iface;
538 539 540 541 542 543 544 545 546

    IMimeInternational_AddRef(*internat);
    return S_OK;
}

HRESULT WINAPI MimeOleGetInternat(IMimeInternational **internat)
{
    TRACE("(%p)\n", internat);

547
    *internat = &global_internat->IMimeInternational_iface;
548 549 550
    IMimeInternational_AddRef(*internat);
    return S_OK;
}
551

552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
HRESULT WINAPI MimeOleFindCharset(LPCSTR name, LPHCHARSET charset)
{
    IMimeInternational *internat;
    HRESULT hr;

    TRACE("(%s, %p)\n", debugstr_a(name), charset);

    hr = MimeOleGetInternat(&internat);
    if(SUCCEEDED(hr))
    {
        hr = IMimeInternational_FindCharset(internat, name, charset);
        IMimeInternational_Release(internat);
    }
    return hr;
}

568 569 570 571 572 573 574 575 576 577 578 579 580
HRESULT WINAPI MimeOleGetCharsetInfo(HCHARSET hCharset, LPINETCSETINFO pCsetInfo)
{
    IMimeInternational *internat;
    HRESULT hr;

    TRACE("(%p, %p)\n", hCharset, pCsetInfo);

    hr = MimeOleGetInternat(&internat);
    if(SUCCEEDED(hr))
    {
        hr = IMimeInternational_GetCharsetInfo(internat, hCharset, pCsetInfo);
        IMimeInternational_Release(internat);
    }
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
    return hr;
}

HRESULT WINAPI MimeOleGetDefaultCharset(LPHCHARSET charset)
{
    IMimeInternational *internat;
    HRESULT hr;

    TRACE("(%p)\n", charset);

    hr = MimeOleGetInternat(&internat);
    if(SUCCEEDED(hr))
    {
        hr = IMimeInternational_GetDefaultCharset(internat, charset);
        IMimeInternational_Release(internat);
    }
    return hr;
598
}