1 /*****************************************************************************
2  *
3  * Copyright (c) 2008-2010, CoreCodec, Inc.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *     * Redistributions of source code must retain the above copyright
9  *       notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above copyright
11  *       notice, this list of conditions and the following disclaimer in the
12  *       documentation and/or other materials provided with the distribution.
13  *     * Neither the name of CoreCodec, Inc. nor the
14  *       names of its contributors may be used to endorse or promote products
15  *       derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY CoreCodec, Inc. ``AS IS'' AND ANY
18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL CoreCodec, Inc. BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  ****************************************************************************/
29 
30 #include "charconvert.h"
31 #include "corec/str/str.h"
32 
33 #if defined(TARGET_WIN)
34 
35 #ifndef STRICT
36 #define STRICT
37 #endif
38 #include <windows.h>
39 
40 #ifndef CP_UTF8
41 #define CP_UTF8 65001
42 #endif
43 
44 #ifndef CP_UTF7
45 #define CP_UTF7 65000
46 #endif
47 
CharConvSS(charconv * CC,char * Out,size_t OutLen,const char * In)48 void CharConvSS(charconv* CC, char* Out, size_t OutLen, const char* In)
49 {
50 	if (OutLen>0)
51 	{
52 		WCHAR Temp[1024];
53 		UINT OutCode = LOWORD((DWORD)CC);
54 		UINT InCode = HIWORD((DWORD)CC);
55 
56 		if (InCode == OutCode ||
57 			!MultiByteToWideChar(InCode,0,In,-1,Temp,512) ||
58 			!WideCharToMultiByte(OutCode,0,Temp,-1,Out,OutLen,0,0))
59 		{
60 			size_t n = min(strlen(In),OutLen-1);
61 			memcpy(Out,In,n*sizeof(char));
62 			Out[n] = 0;
63 		}
64 	}
65 }
66 
CharConvWS(charconv * CC,wchar_t * Out,size_t OutLen,const char * In)67 void CharConvWS(charconv* CC, wchar_t* Out, size_t OutLen, const char* In)
68 {
69 	UINT InCode = HIWORD((DWORD)CC);
70 	if (!MultiByteToWideChar(InCode,0,In,-1,Out,OutLen))
71 	{
72 		for (;OutLen>1 && *In;++In,--OutLen,++Out)
73 			*Out = (wchar_t)*In;
74 		*Out = 0;
75 	}
76 }
77 
CharConvSW(charconv * CC,char * Out,size_t OutLen,const wchar_t * In)78 void CharConvSW(charconv* CC, char* Out, size_t OutLen, const wchar_t* In)
79 {
80 	UINT OutCode = LOWORD((DWORD)CC);
81 	if (!WideCharToMultiByte(OutCode,0,In,-1,Out,OutLen,0,0))
82 	{
83 		for (;OutLen>1 && *In;++In,--OutLen,++Out)
84 			*Out = (char)(*In>255?'*':*In);
85 		*Out = 0;
86 	}
87 }
88 
CharConvWW(charconv * UNUSED_PARAM (CC),wchar_t * Out,size_t OutLen,const wchar_t * In)89 void CharConvWW(charconv* UNUSED_PARAM(CC), wchar_t* Out, size_t OutLen, const wchar_t* In)
90 {
91 #ifdef UNICODE
92 	tcscpy_s(Out,OutLen,In);
93 #else
94 	if (OutLen>0)
95 	{
96 		size_t n = min(wcslen(In),OutLen-1);
97 		memcpy(Out,In,n*sizeof(wchar_t));
98 		Out[n] = 0;
99 	}
100 #endif
101 }
102 
103 typedef struct codepage
104 {
105     const tchar_t* Name;
106     uint16_t CodePage;
107     uint16_t CodePage2;
108 
109 } codepage;
110 
111 static const codepage CodePage[] =
112 {
113     {T("UTF-7"),        CP_UTF7,0},
114     {T("UTF-8"),        CP_UTF8,0},
115     {T("Shift_JIS"),    932,0},
116     {T("GB2312"),       936,0},
117     {T("BIG5"),         950,0},
118     {T("ISO-8859-1"),   28591,1252},
119     {T("ISO-8859-2"),   28592,1250},
120     {T("ISO-8859-3"),   28593,1254},
121     {T("ISO-8859-4"),   28594,1257},
122     {T("ISO-8859-5"),   28595,1251},
123     {T("ISO-8859-6"),   28596,1256},
124     {T("ISO-8859-7"),   28597,1253},
125     {T("ISO-8859-8"),   28598,1255},
126     {NULL,0,0}
127 };
128 
GetCodePage(const tchar_t * Name,bool_t To)129 static NOINLINE UINT GetCodePage(const tchar_t* Name, bool_t To)
130 {
131     if (Name && Name[0])
132     {
133         const codepage* i;
134 
135     	int CP;
136 	    if (stscanf(Name,T("CP%d"),&CP)==1 ||
137             stscanf(Name,T("windows-%d"),&CP)==1)
138 		    return CP;
139 
140         for (i=CodePage;i->Name;++i)
141 	        if (tcsisame_ascii(Name,i->Name))
142             {
143                 if (IsValidCodePage(i->CodePage))
144     		        return i->CodePage;
145                 if (i->CodePage2 && IsValidCodePage(i->CodePage2))
146     		        return i->CodePage2;
147                 break;
148             }
149     }
150     if (To)
151         return GetOEMCP();
152     else
153         return CP_ACP;
154 }
155 
CharConvOpen(const tchar_t * From,const tchar_t * To)156 charconv* CharConvOpen(const tchar_t* From, const tchar_t* To)
157 {
158 	return (charconv*)MAKELONG(GetCodePage(To,1),GetCodePage(From,0));
159 }
160 
CharConvClose(charconv * UNUSED_PARAM (p))161 void CharConvClose(charconv* UNUSED_PARAM(p))
162 {
163 }
164 
CharConvDefault(tchar_t * UNUSED_PARAM (Out),size_t UNUSED_PARAM (OutLen))165 void CharConvDefault(tchar_t* UNUSED_PARAM(Out), size_t UNUSED_PARAM(OutLen))
166 {
167     *Out = 0;
168 }
169 
170 #endif
171