rpcrt4: Move the full pointer table init to after a potential call to NdrServerInitia...
[wine] / dlls / inetcomm / mimeintl.c
1 /*
2  * MIME OLE International interface
3  *
4  * Copyright 2008 Huw Davies for CodeWeavers
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #define COBJMACROS
22 #define NONAMELESSUNION
23
24 #include <stdarg.h>
25 #include <stdio.h>
26
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winuser.h"
30 #include "winnls.h"
31 #include "objbase.h"
32 #include "ole2.h"
33 #include "mimeole.h"
34 #include "mlang.h"
35
36 #include "wine/list.h"
37 #include "wine/unicode.h"
38 #include "wine/debug.h"
39
40 #include "inetcomm_private.h"
41
42 WINE_DEFAULT_DEBUG_CHANNEL(inetcomm);
43
44 typedef struct
45 {
46     struct list entry;
47     INETCSETINFO cs_info;
48 } charset_entry;
49
50 typedef struct
51 {
52     const IMimeInternationalVtbl *lpVtbl;
53     LONG refs;
54     CRITICAL_SECTION cs;
55
56     struct list charsets;
57     LONG next_charset_handle;
58     HCHARSET default_charset;
59 } internat;
60
61 static inline internat *impl_from_IMimeInternational( IMimeInternational *iface )
62 {
63     return (internat *)((char*)iface - FIELD_OFFSET(internat, lpVtbl));
64 }
65
66 static inline HRESULT get_mlang(IMultiLanguage **ml)
67 {
68     return CoCreateInstance(&CLSID_CMultiLanguage, NULL,  CLSCTX_INPROC_SERVER | CLSCTX_INPROC_HANDLER,
69                             &IID_IMultiLanguage, (void **)ml);
70 }
71
72 static HRESULT WINAPI MimeInternat_QueryInterface( IMimeInternational *iface, REFIID riid, LPVOID *ppobj )
73 {
74     if (IsEqualGUID(riid, &IID_IUnknown) ||
75         IsEqualGUID(riid, &IID_IMimeInternational))
76     {
77         IMimeInternational_AddRef( iface );
78         *ppobj = iface;
79         return S_OK;
80     }
81
82     FIXME("interface %s not implemented\n", debugstr_guid(riid));
83     return E_NOINTERFACE;
84 }
85
86 static ULONG WINAPI MimeInternat_AddRef( IMimeInternational *iface )
87 {
88     internat *This = impl_from_IMimeInternational( iface );
89     return InterlockedIncrement(&This->refs);
90 }
91
92 static ULONG WINAPI MimeInternat_Release( IMimeInternational *iface )
93 {
94     internat *This = impl_from_IMimeInternational( iface );
95     ULONG refs;
96
97     refs = InterlockedDecrement(&This->refs);
98     if (!refs)
99     {
100         charset_entry *charset, *cursor2;
101
102         LIST_FOR_EACH_ENTRY_SAFE(charset, cursor2, &This->charsets, charset_entry, entry)
103         {
104             list_remove(&charset->entry);
105             HeapFree(GetProcessHeap(), 0, charset);
106         }
107         HeapFree(GetProcessHeap(), 0, This);
108     }
109
110     return refs;
111 }
112
113 static HRESULT WINAPI MimeInternat_SetDefaultCharset(IMimeInternational *iface, HCHARSET hCharset)
114 {
115     internat *This = impl_from_IMimeInternational( iface );
116
117     TRACE("(%p)->(%p)\n", iface, hCharset);
118
119     if(hCharset == NULL) return E_INVALIDARG;
120     /* FIXME check hCharset is valid */
121
122     InterlockedExchangePointer(&This->default_charset, hCharset);
123
124     return S_OK;
125 }
126
127 static HRESULT WINAPI MimeInternat_GetDefaultCharset(IMimeInternational *iface, LPHCHARSET phCharset)
128 {
129     internat *This = impl_from_IMimeInternational( iface );
130     HRESULT hr = S_OK;
131
132     TRACE("(%p)->(%p)\n", iface, phCharset);
133
134     if(This->default_charset == NULL)
135     {
136         HCHARSET hcs;
137         hr = IMimeInternational_GetCodePageCharset(iface, GetACP(), CHARSET_BODY, &hcs);
138         if(SUCCEEDED(hr))
139             InterlockedCompareExchangePointer(&This->default_charset, hcs, NULL);
140     }
141     *phCharset = This->default_charset;
142
143     return hr;
144 }
145
146 static HRESULT mlang_getcodepageinfo(UINT cp, MIMECPINFO *mlang_cp_info)
147 {
148     HRESULT hr;
149     IMultiLanguage *ml;
150
151     hr = get_mlang(&ml);
152
153     if(SUCCEEDED(hr))
154     {
155         hr = IMultiLanguage_GetCodePageInfo(ml, cp, mlang_cp_info);
156         IMultiLanguage_Release(ml);
157     }
158     return hr;
159 }
160
161 static HRESULT WINAPI MimeInternat_GetCodePageCharset(IMimeInternational *iface, CODEPAGEID cpiCodePage,
162                                                       CHARSETTYPE ctCsetType,
163                                                       LPHCHARSET phCharset)
164 {
165     HRESULT hr;
166     MIMECPINFO mlang_cp_info;
167
168     TRACE("(%p)->(%d, %d, %p)\n", iface, cpiCodePage, ctCsetType, phCharset);
169
170     *phCharset = NULL;
171
172     hr = mlang_getcodepageinfo(cpiCodePage, &mlang_cp_info);
173     if(SUCCEEDED(hr))
174     {
175         const WCHAR *charset_nameW = NULL;
176         char *charset_name;
177         DWORD len;
178
179         switch(ctCsetType)
180         {
181         case CHARSET_BODY:
182             charset_nameW = mlang_cp_info.wszBodyCharset;
183             break;
184         case CHARSET_HEADER:
185             charset_nameW = mlang_cp_info.wszHeaderCharset;
186             break;
187         case CHARSET_WEB:
188             charset_nameW = mlang_cp_info.wszWebCharset;
189             break;
190         default:
191             return MIME_E_INVALID_CHARSET_TYPE;
192         }
193         len = WideCharToMultiByte(CP_ACP, 0, charset_nameW, -1, NULL, 0, NULL, NULL);
194         charset_name = HeapAlloc(GetProcessHeap(), 0, len);
195         WideCharToMultiByte(CP_ACP, 0, charset_nameW, -1, charset_name, len, NULL, NULL);
196         hr = IMimeInternational_FindCharset(iface, charset_name, phCharset);
197         HeapFree(GetProcessHeap(), 0, charset_name);
198     }
199     return hr;
200 }
201
202 static HRESULT mlang_getcsetinfo(const char *charset, MIMECSETINFO *mlang_info)
203 {
204     DWORD len = MultiByteToWideChar(CP_ACP, 0, charset, -1, NULL, 0);
205     BSTR bstr = SysAllocStringLen(NULL, len - 1);
206     HRESULT hr;
207     IMultiLanguage *ml;
208
209     MultiByteToWideChar(CP_ACP, 0, charset, -1, bstr, len);
210
211     hr = get_mlang(&ml);
212
213     if(SUCCEEDED(hr))
214     {
215         hr = IMultiLanguage_GetCharsetInfo(ml, bstr, mlang_info);
216         IMultiLanguage_Release(ml);
217     }
218     SysFreeString(bstr);
219     if(FAILED(hr)) hr = MIME_E_NOT_FOUND;
220     return hr;
221 }
222
223 static HCHARSET add_charset(struct list *list, MIMECSETINFO *mlang_info, HCHARSET handle)
224 {
225     charset_entry *charset = HeapAlloc(GetProcessHeap(), 0, sizeof(*charset));
226
227     WideCharToMultiByte(CP_ACP, 0, mlang_info->wszCharset, -1,
228                         charset->cs_info.szName, sizeof(charset->cs_info.szName), NULL, NULL);
229     charset->cs_info.cpiWindows = mlang_info->uiCodePage;
230     charset->cs_info.cpiInternet = mlang_info->uiInternetEncoding;
231     charset->cs_info.hCharset = handle;
232     charset->cs_info.dwReserved1 = 0;
233     list_add_head(list, &charset->entry);
234
235     return charset->cs_info.hCharset;
236 }
237
238 static HRESULT WINAPI MimeInternat_FindCharset(IMimeInternational *iface, LPCSTR pszCharset,
239                                                LPHCHARSET phCharset)
240 {
241     internat *This = impl_from_IMimeInternational( iface );
242     HRESULT hr = MIME_E_NOT_FOUND;
243     charset_entry *charset;
244
245     TRACE("(%p)->(%s, %p)\n", iface, debugstr_a(pszCharset), phCharset);
246
247     *phCharset = NULL;
248
249     EnterCriticalSection(&This->cs);
250
251     LIST_FOR_EACH_ENTRY(charset, &This->charsets, charset_entry, entry)
252     {
253         if(!lstrcmpiA(charset->cs_info.szName, pszCharset))
254         {
255             *phCharset = charset->cs_info.hCharset;
256             hr = S_OK;
257             break;
258         }
259     }
260
261     if(hr == MIME_E_NOT_FOUND)
262     {
263         MIMECSETINFO mlang_info;
264
265         LeaveCriticalSection(&This->cs);
266         hr = mlang_getcsetinfo(pszCharset, &mlang_info);
267         EnterCriticalSection(&This->cs);
268
269         if(SUCCEEDED(hr))
270             *phCharset = add_charset(&This->charsets, &mlang_info,
271                                      (HCHARSET)InterlockedIncrement(&This->next_charset_handle));
272     }
273
274     LeaveCriticalSection(&This->cs);
275     return hr;
276 }
277
278 static HRESULT WINAPI MimeInternat_GetCharsetInfo(IMimeInternational *iface, HCHARSET hCharset,
279                                                   LPINETCSETINFO pCsetInfo)
280 {
281     internat *This = impl_from_IMimeInternational( iface );
282     HRESULT hr = MIME_E_INVALID_HANDLE;
283     charset_entry *charset;
284
285     TRACE("(%p)->(%p, %p)\n", iface, hCharset, pCsetInfo);
286
287     EnterCriticalSection(&This->cs);
288
289     LIST_FOR_EACH_ENTRY(charset, &This->charsets, charset_entry, entry)
290     {
291         if(charset->cs_info.hCharset ==  hCharset)
292         {
293             *pCsetInfo = charset->cs_info;
294             hr = S_OK;
295             break;
296         }
297     }
298
299     LeaveCriticalSection(&This->cs);
300
301     return hr;
302 }
303
304 static HRESULT WINAPI MimeInternat_GetCodePageInfo(IMimeInternational *iface, CODEPAGEID cpiCodePage,
305                                                    LPCODEPAGEINFO pCodePageInfo)
306 {
307     FIXME("stub\n");
308     return E_NOTIMPL;
309 }
310
311 static HRESULT WINAPI MimeInternat_CanConvertCodePages(IMimeInternational *iface, CODEPAGEID cpiSource,
312                                                        CODEPAGEID cpiDest)
313 {
314     HRESULT hr;
315     IMultiLanguage *ml;
316
317     TRACE("(%p)->(%d, %d)\n", iface, cpiSource, cpiDest);
318
319     /* Could call mlang.IsConvertINetStringAvailable() to avoid the COM overhead if need be. */
320
321     hr = get_mlang(&ml);
322     if(SUCCEEDED(hr))
323     {
324         hr = IMultiLanguage_IsConvertible(ml, cpiSource, cpiDest);
325         IMultiLanguage_Release(ml);
326     }
327
328     return hr;
329 }
330
331 static HRESULT WINAPI MimeInternat_DecodeHeader(IMimeInternational *iface, HCHARSET hCharset,
332                                                 LPCSTR pszData,
333                                                 LPPROPVARIANT pDecoded,
334                                                 LPRFC1522INFO pRfc1522Info)
335 {
336     FIXME("stub\n");
337     return E_NOTIMPL;
338 }
339
340 static HRESULT WINAPI MimeInternat_EncodeHeader(IMimeInternational *iface, HCHARSET hCharset,
341                                                 LPPROPVARIANT pData,
342                                                 LPSTR *ppszEncoded,
343                                                 LPRFC1522INFO pRfc1522Info)
344 {
345     FIXME("stub\n");
346     return E_NOTIMPL;
347 }
348
349 static HRESULT WINAPI MimeInternat_ConvertBuffer(IMimeInternational *iface, CODEPAGEID cpiSource,
350                                                  CODEPAGEID cpiDest, LPBLOB pIn, LPBLOB pOut,
351                                                  ULONG *pcbRead)
352 {
353     HRESULT hr;
354     IMultiLanguage *ml;
355
356     TRACE("(%p)->(%d, %d, %p, %p, %p)\n", iface, cpiSource, cpiDest, pIn, pOut, pcbRead);
357
358     *pcbRead = 0;
359     pOut->cbSize = 0;
360
361     /* Could call mlang.ConvertINetString() to avoid the COM overhead if need be. */
362
363     hr = get_mlang(&ml);
364     if(SUCCEEDED(hr))
365     {
366         DWORD mode = 0;
367         UINT in_size = pIn->cbSize, out_size;
368
369         hr = IMultiLanguage_ConvertString(ml, &mode, cpiSource, cpiDest, pIn->pBlobData, &in_size,
370                                           NULL, &out_size);
371         if(hr == S_OK) /* S_FALSE means the conversion could not be performed */
372         {
373             pOut->pBlobData = CoTaskMemAlloc(out_size);
374             if(!pOut->pBlobData)
375                 hr = E_OUTOFMEMORY;
376             else
377             {
378                 mode = 0;
379                 in_size = pIn->cbSize;
380                 hr = IMultiLanguage_ConvertString(ml, &mode, cpiSource, cpiDest, pIn->pBlobData, &in_size,
381                                                   pOut->pBlobData, &out_size);
382
383                 if(hr == S_OK)
384                 {
385                     *pcbRead = in_size;
386                     pOut->cbSize = out_size;
387                 }
388                 else
389                     CoTaskMemFree(pOut->pBlobData);
390             }
391         }
392         IMultiLanguage_Release(ml);
393     }
394
395     return hr;
396 }
397
398 static HRESULT WINAPI MimeInternat_ConvertString(IMimeInternational *iface, CODEPAGEID cpiSource,
399                                                  CODEPAGEID cpiDest, LPPROPVARIANT pIn,
400                                                  LPPROPVARIANT pOut)
401 {
402     HRESULT hr;
403     int src_len;
404     IMultiLanguage *ml;
405
406     TRACE("(%p)->(%d, %d, %p %p)\n", iface, cpiSource, cpiDest, pIn, pOut);
407
408     switch(pIn->vt)
409     {
410     case VT_LPSTR:
411         if(cpiSource == CP_UNICODE) cpiSource = GetACP();
412         src_len = strlen(pIn->u.pszVal);
413         break;
414     case VT_LPWSTR:
415         cpiSource = CP_UNICODE;
416         src_len = strlenW(pIn->u.pwszVal) * sizeof(WCHAR);
417         break;
418     default:
419         return E_INVALIDARG;
420     }
421
422     hr = get_mlang(&ml);
423     if(SUCCEEDED(hr))
424     {
425         DWORD mode = 0;
426         UINT in_size = src_len, out_size;
427
428         hr = IMultiLanguage_ConvertString(ml, &mode, cpiSource, cpiDest, (BYTE*)pIn->u.pszVal, &in_size,
429                                           NULL, &out_size);
430         if(hr == S_OK) /* S_FALSE means the conversion could not be performed */
431         {
432             out_size += (cpiDest == CP_UNICODE) ? sizeof(WCHAR) : sizeof(char);
433
434             pOut->u.pszVal = CoTaskMemAlloc(out_size);
435             if(!pOut->u.pszVal)
436                 hr = E_OUTOFMEMORY;
437             else
438             {
439                 mode = 0;
440                 in_size = src_len;
441                 hr = IMultiLanguage_ConvertString(ml, &mode, cpiSource, cpiDest, (BYTE*)pIn->u.pszVal, &in_size,
442                                                   (BYTE*)pOut->u.pszVal, &out_size);
443
444                 if(hr == S_OK)
445                 {
446                     if(cpiDest == CP_UNICODE)
447                     {
448                         pOut->u.pwszVal[out_size / sizeof(WCHAR)] = 0;
449                         pOut->vt = VT_LPWSTR;
450                     }
451                     else
452                     {
453                         pOut->u.pszVal[out_size] = '\0';
454                         pOut->vt = VT_LPSTR;
455                     }
456                 }
457                 else
458                     CoTaskMemFree(pOut->u.pszVal);
459             }
460         }
461         IMultiLanguage_Release(ml);
462     }
463     return hr;
464 }
465
466 static HRESULT WINAPI MimeInternat_MLANG_ConvertInetReset(IMimeInternational *iface)
467 {
468     FIXME("stub\n");
469     return E_NOTIMPL;
470 }
471
472 static HRESULT WINAPI MimeInternat_MLANG_ConvertInetString(IMimeInternational *iface, CODEPAGEID cpiSource,
473                                                            CODEPAGEID cpiDest,
474                                                            LPCSTR pSource,
475                                                            int *pnSizeOfSource,
476                                                            LPSTR pDestination,
477                                                            int *pnDstSize)
478 {
479     FIXME("stub\n");
480     return E_NOTIMPL;
481 }
482
483 static HRESULT WINAPI MimeInternat_Rfc1522Decode(IMimeInternational *iface, LPCSTR pszValue,
484                                                  LPSTR pszCharset,
485                                                  ULONG cchmax,
486                                                  LPSTR *ppszDecoded)
487 {
488     FIXME("stub\n");
489     return E_NOTIMPL;
490 }
491
492 static HRESULT WINAPI MimeInternat_Rfc1522Encode(IMimeInternational *iface, LPCSTR pszValue,
493                                                  HCHARSET hCharset,
494                                                  LPSTR *ppszEncoded)
495 {
496     FIXME("stub\n");
497     return E_NOTIMPL;
498 }
499
500 static IMimeInternationalVtbl mime_internat_vtbl =
501 {
502     MimeInternat_QueryInterface,
503     MimeInternat_AddRef,
504     MimeInternat_Release,
505     MimeInternat_SetDefaultCharset,
506     MimeInternat_GetDefaultCharset,
507     MimeInternat_GetCodePageCharset,
508     MimeInternat_FindCharset,
509     MimeInternat_GetCharsetInfo,
510     MimeInternat_GetCodePageInfo,
511     MimeInternat_CanConvertCodePages,
512     MimeInternat_DecodeHeader,
513     MimeInternat_EncodeHeader,
514     MimeInternat_ConvertBuffer,
515     MimeInternat_ConvertString,
516     MimeInternat_MLANG_ConvertInetReset,
517     MimeInternat_MLANG_ConvertInetString,
518     MimeInternat_Rfc1522Decode,
519     MimeInternat_Rfc1522Encode
520 };
521
522 static internat *global_internat;
523
524 HRESULT MimeInternational_Construct(IMimeInternational **internat)
525 {
526     global_internat = HeapAlloc(GetProcessHeap(), 0, sizeof(*global_internat));
527     global_internat->lpVtbl = &mime_internat_vtbl;
528     global_internat->refs = 0;
529     InitializeCriticalSection(&global_internat->cs);
530
531     list_init(&global_internat->charsets);
532     global_internat->next_charset_handle = 0;
533     global_internat->default_charset = NULL;
534
535     *internat = (IMimeInternational*)&global_internat->lpVtbl;
536
537     IMimeInternational_AddRef(*internat);
538     return S_OK;
539 }
540
541 HRESULT WINAPI MimeOleGetInternat(IMimeInternational **internat)
542 {
543     TRACE("(%p)\n", internat);
544
545     *internat = (IMimeInternational *)&global_internat->lpVtbl;
546     IMimeInternational_AddRef(*internat);
547     return S_OK;
548 }
549
550 HRESULT WINAPI MimeOleFindCharset(LPCSTR name, LPHCHARSET charset)
551 {
552     IMimeInternational *internat;
553     HRESULT hr;
554
555     TRACE("(%s, %p)\n", debugstr_a(name), charset);
556
557     hr = MimeOleGetInternat(&internat);
558     if(SUCCEEDED(hr))
559     {
560         hr = IMimeInternational_FindCharset(internat, name, charset);
561         IMimeInternational_Release(internat);
562     }
563     return hr;
564 }
565
566 HRESULT WINAPI MimeOleGetCharsetInfo(HCHARSET hCharset, LPINETCSETINFO pCsetInfo)
567 {
568     IMimeInternational *internat;
569     HRESULT hr;
570
571     TRACE("(%p, %p)\n", hCharset, pCsetInfo);
572
573     hr = MimeOleGetInternat(&internat);
574     if(SUCCEEDED(hr))
575     {
576         hr = IMimeInternational_GetCharsetInfo(internat, hCharset, pCsetInfo);
577         IMimeInternational_Release(internat);
578     }
579     return hr;
580 }
581
582 HRESULT WINAPI MimeOleGetDefaultCharset(LPHCHARSET charset)
583 {
584     IMimeInternational *internat;
585     HRESULT hr;
586
587     TRACE("(%p)\n", charset);
588
589     hr = MimeOleGetInternat(&internat);
590     if(SUCCEEDED(hr))
591     {
592         hr = IMimeInternational_GetDefaultCharset(internat, charset);
593         IMimeInternational_Release(internat);
594     }
595     return hr;
596 }