1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 2003
20 * the Initial Developer. All Rights Reserved.
24 * Alternatively, the contents of this file may be used under the terms of
25 * either of the GNU General Public License Version 2 or later (the "GPL"),
26 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
39 #ifdef MOZ_USE_NATIVE_UCONV
41 #include "nsIGenericFactory.h"
43 #include "nsINativeUConvService.h"
45 #include "nsIUnicodeDecoder.h"
46 #include "nsIUnicodeEncoder.h"
47 #include "nsICharRepresentable.h"
49 #include "nsNativeUConvService.h"
50 #include "nsAutoPtr.h"
52 #include <nl_types.h> // CODESET
53 #include <langinfo.h> // nl_langinfo
54 #include <iconv.h> // iconv_open, iconv, iconv_close
58 class IConvAdaptor
: public nsIUnicodeDecoder
,
59 public nsIUnicodeEncoder
,
60 public nsICharRepresentable
64 virtual ~IConvAdaptor();
66 nsresult
Init(const char* from
, const char* to
);
72 NS_IMETHOD
Convert(const char * aSrc
,
75 PRInt32
* aDestLength
);
77 NS_IMETHOD
GetMaxLength(const char * aSrc
,
79 PRInt32
* aDestLength
);
84 NS_IMETHOD
Convert(const PRUnichar
* aSrc
,
87 PRInt32
* aDestLength
);
90 NS_IMETHOD
Finish(char * aDest
, PRInt32
* aDestLength
);
92 NS_IMETHOD
GetMaxLength(const PRUnichar
* aSrc
,
94 PRInt32
* aDestLength
);
96 // defined by the Decoder: NS_IMETHOD Reset();
98 NS_IMETHOD
SetOutputErrorBehavior(PRInt32 aBehavior
,
99 nsIUnicharEncoder
* aEncoder
,
102 NS_IMETHOD
FillInfo(PRUint32
* aInfo
);
106 nsresult
ConvertInternal(void * aSrc
,
107 PRInt32
* aSrcLength
,
108 PRInt32 aSrcCharSize
,
110 PRInt32
* aDestLength
,
111 PRInt32 aDestCharSize
);
115 PRBool mReplaceOnError
;
116 PRUnichar mReplaceChar
;
119 nsCString mFrom
, mTo
;
123 NS_IMPL_ISUPPORTS3(IConvAdaptor
,
126 nsICharRepresentable
)
128 IConvAdaptor::IConvAdaptor()
131 mReplaceOnError
= PR_FALSE
;
134 IConvAdaptor::~IConvAdaptor()
137 iconv_close(mConverter
);
141 IConvAdaptor::Init(const char* from
, const char* to
)
148 mConverter
= iconv_open(to
, from
);
149 if (mConverter
== (iconv_t
) -1 )
152 printf(" * IConvAdaptor - FAILED Initing: %s ==> %s\n", from
, to
);
155 return NS_ERROR_FAILURE
;
160 // From some charset to ucs2
162 IConvAdaptor::Convert(const char * aSrc
,
163 PRInt32
* aSrcLength
,
165 PRInt32
* aDestLength
)
167 return ConvertInternal( (void*) aSrc
,
176 IConvAdaptor::GetMaxLength(const char * aSrc
,
178 PRInt32
* aDestLength
)
181 return NS_ERROR_UENC_NOMAPPING
;
183 *aDestLength
= aSrcLength
*4; // sick
185 printf(" * IConvAdaptor - - GetMaxLength %d ( %s -> %s )\n", *aDestLength
, mFrom
.get(), mTo
.get());
192 IConvAdaptor::Reset()
194 const char *zero_char_in_ptr
= NULL
;
195 char *zero_char_out_ptr
= NULL
;
196 size_t zero_size_in
= 0,
200 (char **)&zero_char_in_ptr
,
206 printf(" * IConvAdaptor - - Reset\n");
212 // convert unicode data into some charset.
214 IConvAdaptor::Convert(const PRUnichar
* aSrc
,
215 PRInt32
* aSrcLength
,
217 PRInt32
* aDestLength
)
219 return ConvertInternal( (void*) aSrc
,
229 IConvAdaptor::Finish(char * aDest
, PRInt32
* aDestLength
)
236 IConvAdaptor::GetMaxLength(const PRUnichar
* aSrc
,
238 PRInt32
* aDestLength
)
241 return NS_ERROR_UENC_NOMAPPING
;
243 *aDestLength
= aSrcLength
*4; // sick
250 IConvAdaptor::SetOutputErrorBehavior(PRInt32 aBehavior
,
251 nsIUnicharEncoder
* aEncoder
,
254 if (aBehavior
== nsIUnicodeEncoder::kOnError_Signal
) {
255 mReplaceOnError
= PR_FALSE
;
258 else if (aBehavior
== nsIUnicodeEncoder::kOnError_Replace
) {
259 mReplaceOnError
= PR_TRUE
;
260 mReplaceChar
= aChar
;
264 NS_WARNING("Uconv Error Behavior not support");
265 return NS_ERROR_FAILURE
;
269 IConvAdaptor::FillInfo(PRUint32
* aInfo
)
272 printf(" * IConvAdaptor - FillInfo called\n");
280 IConvAdaptor::ConvertInternal(void * aSrc
,
281 PRInt32
* aSrcLength
,
282 PRInt32 aSrcCharSize
,
284 PRInt32
* aDestLength
,
285 PRInt32 aDestCharSize
)
288 NS_WARNING("Converter Not Initialize");
289 return NS_ERROR_NOT_INITIALIZED
;
292 size_t inLeft
= (size_t) *aSrcLength
* aSrcCharSize
;
293 size_t outLeft
= (size_t) *aDestLength
* aDestCharSize
;
294 size_t outputAvail
= outLeft
;
298 res
= iconv(mConverter
,
304 if (res
== (size_t) -1) {
305 // on some platforms (e.g., linux) iconv will fail with
306 // E2BIG if it cannot convert _all_ of its input. it'll
307 // still adjust all of the in/out params correctly, so we
308 // can ignore this error. the assumption is that we will
309 // be called again to complete the conversion.
310 if ((errno
== E2BIG
) && (outLeft
< outputAvail
)) {
315 if (errno
== EILSEQ
) {
317 if (mReplaceOnError
) {
318 if (aDestCharSize
== 1) {
319 (*(char*)aDest
) = (char)mReplaceChar
;
320 aDest
= (char*)aDest
+ sizeof(char);
324 (*(PRUnichar
*)aDest
) = (PRUnichar
)mReplaceChar
;
325 aDest
= (PRUnichar
*)aDest
+ sizeof(PRUnichar
);
328 inLeft
-= aSrcCharSize
;
329 outLeft
-= aDestCharSize
;
332 printf(" * IConvAdaptor - replacing char in output ( %s -> %s )\n",
333 mFrom
.get(), mTo
.get());
342 printf(" * IConvAdaptor - Bad input ( %s -> %s )\n", mFrom
.get(), mTo
.get());
344 return NS_ERROR_UENC_NOMAPPING
;
348 if (inLeft
<= 0 || outLeft
<= 0 || res
== -1)
353 if (res
!= (size_t) -1) {
355 // xp_iconv deals with how much is remaining in a given buffer
356 // but what uconv wants how much we read/written already. So
357 // we fix it up here.
358 *aSrcLength
-= (inLeft
/ aSrcCharSize
);
359 *aDestLength
-= (outLeft
/ aDestCharSize
);
364 printf(" * IConvAdaptor - - xp_iconv error( %s -> %s )\n", mFrom
.get(), mTo
.get());
367 return NS_ERROR_UENC_NOMAPPING
;
370 NS_IMPL_ISUPPORTS1(NativeUConvService
, nsINativeUConvService
)
373 NativeUConvService::GetNativeConverter(const char* from
,
375 nsISupports
** aResult
)
379 //nsRefPtr<IConvAdaptor> ucl = new IConvAdaptor();
380 IConvAdaptor
*adaptor
=new IConvAdaptor();
381 nsCOMPtr
<nsISupports
> ucl(static_cast<nsIUnicodeDecoder
*>(adaptor
));
383 return NS_ERROR_OUT_OF_MEMORY
;
385 //nsresult rv = ucl->Init(from, to);
386 nsresult rv
=adaptor
->Init(from
,to
);
387 if (NS_SUCCEEDED(rv
))
388 NS_ADDREF(*aResult
= ucl
);