kernel32: Do not omit mandatory argument to DeviceIoControl.
[wine.git] / dlls / inetcomm / mimeintl.c
blob07d3ae5f038af049ae08c839fa99cfb8588de43e
1 /*
2 * MIME OLE International interface
4 * Copyright 2008 Huw Davies for CodeWeavers
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.
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.
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
21 #define COBJMACROS
22 #define NONAMELESSUNION
24 #include <stdarg.h>
25 #include <stdio.h>
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"
36 #include "wine/list.h"
37 #include "wine/unicode.h"
38 #include "wine/debug.h"
40 #include "inetcomm_private.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(inetcomm);
44 typedef struct
46 struct list entry;
47 INETCSETINFO cs_info;
48 } charset_entry;
50 typedef struct
52 IMimeInternational IMimeInternational_iface;
53 LONG refs;
54 CRITICAL_SECTION cs;
56 struct list charsets;
57 LONG next_charset_handle;
58 HCHARSET default_charset;
59 } internat_impl;
61 static inline internat_impl *impl_from_IMimeInternational(IMimeInternational *iface)
63 return CONTAINING_RECORD(iface, internat_impl, IMimeInternational_iface);
66 static inline HRESULT get_mlang(IMultiLanguage **ml)
68 return CoCreateInstance(&CLSID_CMultiLanguage, NULL, CLSCTX_INPROC_SERVER | CLSCTX_INPROC_HANDLER,
69 &IID_IMultiLanguage, (void **)ml);
72 static HRESULT WINAPI MimeInternat_QueryInterface( IMimeInternational *iface, REFIID riid, LPVOID *ppobj )
74 if (IsEqualGUID(riid, &IID_IUnknown) ||
75 IsEqualGUID(riid, &IID_IMimeInternational))
77 IMimeInternational_AddRef( iface );
78 *ppobj = iface;
79 return S_OK;
82 FIXME("interface %s not implemented\n", debugstr_guid(riid));
83 return E_NOINTERFACE;
86 static ULONG WINAPI MimeInternat_AddRef( IMimeInternational *iface )
88 internat_impl *This = impl_from_IMimeInternational( iface );
89 return InterlockedIncrement(&This->refs);
92 static ULONG WINAPI MimeInternat_Release( IMimeInternational *iface )
94 internat_impl *This = impl_from_IMimeInternational( iface );
95 ULONG refs;
97 refs = InterlockedDecrement(&This->refs);
98 if (!refs)
100 charset_entry *charset, *cursor2;
102 LIST_FOR_EACH_ENTRY_SAFE(charset, cursor2, &This->charsets, charset_entry, entry)
104 list_remove(&charset->entry);
105 HeapFree(GetProcessHeap(), 0, charset);
107 This->cs.DebugInfo->Spare[0] = 0;
108 DeleteCriticalSection(&This->cs);
109 HeapFree(GetProcessHeap(), 0, This);
112 return refs;
115 static HRESULT WINAPI MimeInternat_SetDefaultCharset(IMimeInternational *iface, HCHARSET hCharset)
117 internat_impl *This = impl_from_IMimeInternational( iface );
119 TRACE("(%p)->(%p)\n", iface, hCharset);
121 if(hCharset == NULL) return E_INVALIDARG;
122 /* FIXME check hCharset is valid */
124 InterlockedExchangePointer(&This->default_charset, hCharset);
126 return S_OK;
129 static HRESULT WINAPI MimeInternat_GetDefaultCharset(IMimeInternational *iface, LPHCHARSET phCharset)
131 internat_impl *This = impl_from_IMimeInternational( iface );
132 HRESULT hr = S_OK;
134 TRACE("(%p)->(%p)\n", iface, phCharset);
136 if(This->default_charset == NULL)
138 HCHARSET hcs;
139 hr = IMimeInternational_GetCodePageCharset(iface, GetACP(), CHARSET_BODY, &hcs);
140 if(SUCCEEDED(hr))
141 InterlockedCompareExchangePointer(&This->default_charset, hcs, NULL);
143 *phCharset = This->default_charset;
145 return hr;
148 static HRESULT mlang_getcodepageinfo(UINT cp, MIMECPINFO *mlang_cp_info)
150 HRESULT hr;
151 IMultiLanguage *ml;
153 hr = get_mlang(&ml);
155 if(SUCCEEDED(hr))
157 hr = IMultiLanguage_GetCodePageInfo(ml, cp, mlang_cp_info);
158 IMultiLanguage_Release(ml);
160 return hr;
163 static HRESULT WINAPI MimeInternat_GetCodePageCharset(IMimeInternational *iface, CODEPAGEID cpiCodePage,
164 CHARSETTYPE ctCsetType,
165 LPHCHARSET phCharset)
167 HRESULT hr;
168 MIMECPINFO mlang_cp_info;
170 TRACE("(%p)->(%d, %d, %p)\n", iface, cpiCodePage, ctCsetType, phCharset);
172 *phCharset = NULL;
174 hr = mlang_getcodepageinfo(cpiCodePage, &mlang_cp_info);
175 if(SUCCEEDED(hr))
177 const WCHAR *charset_nameW = NULL;
178 char *charset_name;
179 DWORD len;
181 switch(ctCsetType)
183 case CHARSET_BODY:
184 charset_nameW = mlang_cp_info.wszBodyCharset;
185 break;
186 case CHARSET_HEADER:
187 charset_nameW = mlang_cp_info.wszHeaderCharset;
188 break;
189 case CHARSET_WEB:
190 charset_nameW = mlang_cp_info.wszWebCharset;
191 break;
192 default:
193 return MIME_E_INVALID_CHARSET_TYPE;
195 len = WideCharToMultiByte(CP_ACP, 0, charset_nameW, -1, NULL, 0, NULL, NULL);
196 charset_name = HeapAlloc(GetProcessHeap(), 0, len);
197 WideCharToMultiByte(CP_ACP, 0, charset_nameW, -1, charset_name, len, NULL, NULL);
198 hr = IMimeInternational_FindCharset(iface, charset_name, phCharset);
199 HeapFree(GetProcessHeap(), 0, charset_name);
201 return hr;
204 static HRESULT mlang_getcsetinfo(const char *charset, MIMECSETINFO *mlang_info)
206 DWORD len = MultiByteToWideChar(CP_ACP, 0, charset, -1, NULL, 0);
207 BSTR bstr = SysAllocStringLen(NULL, len - 1);
208 HRESULT hr;
209 IMultiLanguage *ml;
211 MultiByteToWideChar(CP_ACP, 0, charset, -1, bstr, len);
213 hr = get_mlang(&ml);
215 if(SUCCEEDED(hr))
217 hr = IMultiLanguage_GetCharsetInfo(ml, bstr, mlang_info);
218 IMultiLanguage_Release(ml);
220 SysFreeString(bstr);
221 if(FAILED(hr)) hr = MIME_E_NOT_FOUND;
222 return hr;
225 static HCHARSET add_charset(struct list *list, MIMECSETINFO *mlang_info, HCHARSET handle)
227 charset_entry *charset = HeapAlloc(GetProcessHeap(), 0, sizeof(*charset));
229 WideCharToMultiByte(CP_ACP, 0, mlang_info->wszCharset, -1,
230 charset->cs_info.szName, sizeof(charset->cs_info.szName), NULL, NULL);
231 charset->cs_info.cpiWindows = mlang_info->uiCodePage;
232 charset->cs_info.cpiInternet = mlang_info->uiInternetEncoding;
233 charset->cs_info.hCharset = handle;
234 charset->cs_info.dwReserved1 = 0;
235 list_add_head(list, &charset->entry);
237 return charset->cs_info.hCharset;
240 static HRESULT WINAPI MimeInternat_FindCharset(IMimeInternational *iface, LPCSTR pszCharset,
241 LPHCHARSET phCharset)
243 internat_impl *This = impl_from_IMimeInternational( iface );
244 HRESULT hr = MIME_E_NOT_FOUND;
245 charset_entry *charset;
247 TRACE("(%p)->(%s, %p)\n", iface, debugstr_a(pszCharset), phCharset);
249 *phCharset = NULL;
251 EnterCriticalSection(&This->cs);
253 LIST_FOR_EACH_ENTRY(charset, &This->charsets, charset_entry, entry)
255 if(!lstrcmpiA(charset->cs_info.szName, pszCharset))
257 *phCharset = charset->cs_info.hCharset;
258 hr = S_OK;
259 break;
263 if(hr == MIME_E_NOT_FOUND)
265 MIMECSETINFO mlang_info;
267 LeaveCriticalSection(&This->cs);
268 hr = mlang_getcsetinfo(pszCharset, &mlang_info);
269 EnterCriticalSection(&This->cs);
271 if(SUCCEEDED(hr))
272 *phCharset = add_charset(&This->charsets, &mlang_info,
273 UlongToHandle(InterlockedIncrement(&This->next_charset_handle)));
276 LeaveCriticalSection(&This->cs);
277 return hr;
280 static HRESULT WINAPI MimeInternat_GetCharsetInfo(IMimeInternational *iface, HCHARSET hCharset,
281 LPINETCSETINFO pCsetInfo)
283 internat_impl *This = impl_from_IMimeInternational( iface );
284 HRESULT hr = MIME_E_INVALID_HANDLE;
285 charset_entry *charset;
287 TRACE("(%p)->(%p, %p)\n", iface, hCharset, pCsetInfo);
289 EnterCriticalSection(&This->cs);
291 LIST_FOR_EACH_ENTRY(charset, &This->charsets, charset_entry, entry)
293 if(charset->cs_info.hCharset == hCharset)
295 *pCsetInfo = charset->cs_info;
296 hr = S_OK;
297 break;
301 LeaveCriticalSection(&This->cs);
303 return hr;
306 static HRESULT WINAPI MimeInternat_GetCodePageInfo(IMimeInternational *iface, CODEPAGEID cpiCodePage,
307 LPCODEPAGEINFO pCodePageInfo)
309 FIXME("stub\n");
310 return E_NOTIMPL;
313 static HRESULT WINAPI MimeInternat_CanConvertCodePages(IMimeInternational *iface, CODEPAGEID cpiSource,
314 CODEPAGEID cpiDest)
316 HRESULT hr;
317 IMultiLanguage *ml;
319 TRACE("(%p)->(%d, %d)\n", iface, cpiSource, cpiDest);
321 /* Could call mlang.IsConvertINetStringAvailable() to avoid the COM overhead if need be. */
323 hr = get_mlang(&ml);
324 if(SUCCEEDED(hr))
326 hr = IMultiLanguage_IsConvertible(ml, cpiSource, cpiDest);
327 IMultiLanguage_Release(ml);
330 return hr;
333 static HRESULT WINAPI MimeInternat_DecodeHeader(IMimeInternational *iface, HCHARSET hCharset,
334 LPCSTR pszData,
335 LPPROPVARIANT pDecoded,
336 LPRFC1522INFO pRfc1522Info)
338 FIXME("stub\n");
339 return E_NOTIMPL;
342 static HRESULT WINAPI MimeInternat_EncodeHeader(IMimeInternational *iface, HCHARSET hCharset,
343 LPPROPVARIANT pData,
344 LPSTR *ppszEncoded,
345 LPRFC1522INFO pRfc1522Info)
347 FIXME("stub\n");
348 return E_NOTIMPL;
351 static HRESULT WINAPI MimeInternat_ConvertBuffer(IMimeInternational *iface, CODEPAGEID cpiSource,
352 CODEPAGEID cpiDest, LPBLOB pIn, LPBLOB pOut,
353 ULONG *pcbRead)
355 HRESULT hr;
356 IMultiLanguage *ml;
358 TRACE("(%p)->(%d, %d, %p, %p, %p)\n", iface, cpiSource, cpiDest, pIn, pOut, pcbRead);
360 *pcbRead = 0;
361 pOut->cbSize = 0;
363 /* Could call mlang.ConvertINetString() to avoid the COM overhead if need be. */
365 hr = get_mlang(&ml);
366 if(SUCCEEDED(hr))
368 DWORD mode = 0;
369 UINT in_size = pIn->cbSize, out_size;
371 hr = IMultiLanguage_ConvertString(ml, &mode, cpiSource, cpiDest, pIn->pBlobData, &in_size,
372 NULL, &out_size);
373 if(hr == S_OK) /* S_FALSE means the conversion could not be performed */
375 pOut->pBlobData = CoTaskMemAlloc(out_size);
376 if(!pOut->pBlobData)
377 hr = E_OUTOFMEMORY;
378 else
380 mode = 0;
381 in_size = pIn->cbSize;
382 hr = IMultiLanguage_ConvertString(ml, &mode, cpiSource, cpiDest, pIn->pBlobData, &in_size,
383 pOut->pBlobData, &out_size);
385 if(hr == S_OK)
387 *pcbRead = in_size;
388 pOut->cbSize = out_size;
390 else
391 CoTaskMemFree(pOut->pBlobData);
394 IMultiLanguage_Release(ml);
397 return hr;
400 static HRESULT WINAPI MimeInternat_ConvertString(IMimeInternational *iface, CODEPAGEID cpiSource,
401 CODEPAGEID cpiDest, LPPROPVARIANT pIn,
402 LPPROPVARIANT pOut)
404 HRESULT hr;
405 int src_len;
406 IMultiLanguage *ml;
408 TRACE("(%p)->(%d, %d, %p %p)\n", iface, cpiSource, cpiDest, pIn, pOut);
410 switch(pIn->vt)
412 case VT_LPSTR:
413 if(cpiSource == CP_UNICODE) cpiSource = GetACP();
414 src_len = strlen(pIn->u.pszVal);
415 break;
416 case VT_LPWSTR:
417 cpiSource = CP_UNICODE;
418 src_len = strlenW(pIn->u.pwszVal) * sizeof(WCHAR);
419 break;
420 default:
421 return E_INVALIDARG;
424 hr = get_mlang(&ml);
425 if(SUCCEEDED(hr))
427 DWORD mode = 0;
428 UINT in_size = src_len, out_size;
430 hr = IMultiLanguage_ConvertString(ml, &mode, cpiSource, cpiDest, (BYTE*)pIn->u.pszVal, &in_size,
431 NULL, &out_size);
432 if(hr == S_OK) /* S_FALSE means the conversion could not be performed */
434 out_size += (cpiDest == CP_UNICODE) ? sizeof(WCHAR) : sizeof(char);
436 pOut->u.pszVal = CoTaskMemAlloc(out_size);
437 if(!pOut->u.pszVal)
438 hr = E_OUTOFMEMORY;
439 else
441 mode = 0;
442 in_size = src_len;
443 hr = IMultiLanguage_ConvertString(ml, &mode, cpiSource, cpiDest, (BYTE*)pIn->u.pszVal, &in_size,
444 (BYTE*)pOut->u.pszVal, &out_size);
446 if(hr == S_OK)
448 if(cpiDest == CP_UNICODE)
450 pOut->u.pwszVal[out_size / sizeof(WCHAR)] = 0;
451 pOut->vt = VT_LPWSTR;
453 else
455 pOut->u.pszVal[out_size] = '\0';
456 pOut->vt = VT_LPSTR;
459 else
460 CoTaskMemFree(pOut->u.pszVal);
463 IMultiLanguage_Release(ml);
465 return hr;
468 static HRESULT WINAPI MimeInternat_MLANG_ConvertInetReset(IMimeInternational *iface)
470 FIXME("stub\n");
471 return E_NOTIMPL;
474 static HRESULT WINAPI MimeInternat_MLANG_ConvertInetString(IMimeInternational *iface, CODEPAGEID cpiSource,
475 CODEPAGEID cpiDest,
476 LPCSTR pSource,
477 int *pnSizeOfSource,
478 LPSTR pDestination,
479 int *pnDstSize)
481 FIXME("stub\n");
482 return E_NOTIMPL;
485 static HRESULT WINAPI MimeInternat_Rfc1522Decode(IMimeInternational *iface, LPCSTR pszValue,
486 LPSTR pszCharset,
487 ULONG cchmax,
488 LPSTR *ppszDecoded)
490 FIXME("stub\n");
491 return E_NOTIMPL;
494 static HRESULT WINAPI MimeInternat_Rfc1522Encode(IMimeInternational *iface, LPCSTR pszValue,
495 HCHARSET hCharset,
496 LPSTR *ppszEncoded)
498 FIXME("stub\n");
499 return E_NOTIMPL;
502 static IMimeInternationalVtbl mime_internat_vtbl =
504 MimeInternat_QueryInterface,
505 MimeInternat_AddRef,
506 MimeInternat_Release,
507 MimeInternat_SetDefaultCharset,
508 MimeInternat_GetDefaultCharset,
509 MimeInternat_GetCodePageCharset,
510 MimeInternat_FindCharset,
511 MimeInternat_GetCharsetInfo,
512 MimeInternat_GetCodePageInfo,
513 MimeInternat_CanConvertCodePages,
514 MimeInternat_DecodeHeader,
515 MimeInternat_EncodeHeader,
516 MimeInternat_ConvertBuffer,
517 MimeInternat_ConvertString,
518 MimeInternat_MLANG_ConvertInetReset,
519 MimeInternat_MLANG_ConvertInetString,
520 MimeInternat_Rfc1522Decode,
521 MimeInternat_Rfc1522Encode
524 static internat_impl *global_internat;
526 HRESULT MimeInternational_Construct(IMimeInternational **internat)
528 global_internat = HeapAlloc(GetProcessHeap(), 0, sizeof(*global_internat));
529 global_internat->IMimeInternational_iface.lpVtbl = &mime_internat_vtbl;
530 global_internat->refs = 0;
531 InitializeCriticalSection(&global_internat->cs);
532 global_internat->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": global_internat.cs");
534 list_init(&global_internat->charsets);
535 global_internat->next_charset_handle = 0;
536 global_internat->default_charset = NULL;
538 *internat = &global_internat->IMimeInternational_iface;
540 IMimeInternational_AddRef(*internat);
541 return S_OK;
544 HRESULT WINAPI MimeOleGetInternat(IMimeInternational **internat)
546 TRACE("(%p)\n", internat);
548 *internat = &global_internat->IMimeInternational_iface;
549 IMimeInternational_AddRef(*internat);
550 return S_OK;
553 HRESULT WINAPI MimeOleFindCharset(LPCSTR name, LPHCHARSET charset)
555 IMimeInternational *internat;
556 HRESULT hr;
558 TRACE("(%s, %p)\n", debugstr_a(name), charset);
560 hr = MimeOleGetInternat(&internat);
561 if(SUCCEEDED(hr))
563 hr = IMimeInternational_FindCharset(internat, name, charset);
564 IMimeInternational_Release(internat);
566 return hr;
569 HRESULT WINAPI MimeOleGetCharsetInfo(HCHARSET hCharset, LPINETCSETINFO pCsetInfo)
571 IMimeInternational *internat;
572 HRESULT hr;
574 TRACE("(%p, %p)\n", hCharset, pCsetInfo);
576 hr = MimeOleGetInternat(&internat);
577 if(SUCCEEDED(hr))
579 hr = IMimeInternational_GetCharsetInfo(internat, hCharset, pCsetInfo);
580 IMimeInternational_Release(internat);
582 return hr;
585 HRESULT WINAPI MimeOleGetDefaultCharset(LPHCHARSET charset)
587 IMimeInternational *internat;
588 HRESULT hr;
590 TRACE("(%p)\n", charset);
592 hr = MimeOleGetInternat(&internat);
593 if(SUCCEEDED(hr))
595 hr = IMimeInternational_GetDefaultCharset(internat, charset);
596 IMimeInternational_Release(internat);
598 return hr;