Bug 468575 - Scrape some gunk off the config/ grout, r=ted
[wine-gecko.git] / intl / uconv / src / nsUTF8ConverterService.cpp
blob0cb916d3e42e32d0f9a288a9b9ed56de74b2076c
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:expandtab:shiftwidth=2:tabstop=4:
3 */
4 /* ***** BEGIN LICENSE BLOCK *****
5 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
7 * The contents of this file are subject to the Mozilla Public License Version
8 * 1.1 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://www.mozilla.org/MPL/
12 * Software distributed under the License is distributed on an "AS IS" basis,
13 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14 * for the specific language governing rights and limitations under the
15 * License.
17 * The Original Code is mozilla.org code.
19 * The Initial Developers of the Original Code are
20 * Naoki Hotta <nhotta@netscape.com> and Jungshik Shin <jshin@mailaps.org>.
21 * Portions created by the Initial Developer are Copyright (C) 2002, 2003
22 * the Initial Developers. All Rights Reserved.
24 * Contributor(s):
26 * Alternatively, the contents of this file may be used under the terms of
27 * either the GNU General Public License Version 2 or later (the "GPL"), or
28 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
39 #include "nsString.h"
40 #include "nsIUnicodeEncoder.h"
41 #include "nsICharsetConverterManager.h"
42 #include "nsReadableUtils.h"
43 #include "nsIServiceManager.h"
44 #include "nsUConvDll.h"
45 #include "prmem.h"
46 #include "nsUTF8ConverterService.h"
47 #include "nsEscape.h"
48 #include "nsAutoPtr.h"
50 NS_IMPL_ISUPPORTS1(nsUTF8ConverterService, nsIUTF8ConverterService)
52 static nsresult
53 ToUTF8(const nsACString &aString, const char *aCharset, nsACString &aResult)
55 nsresult rv;
56 if (!aCharset || !*aCharset)
57 return NS_ERROR_INVALID_ARG;
59 nsCOMPtr<nsICharsetConverterManager> ccm;
61 ccm = do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID, &rv);
62 NS_ENSURE_SUCCESS(rv, rv);
64 nsCOMPtr<nsIUnicodeDecoder> unicodeDecoder;
65 rv = ccm->GetUnicodeDecoder(aCharset,
66 getter_AddRefs(unicodeDecoder));
67 NS_ENSURE_SUCCESS(rv, rv);
69 PRInt32 srcLen = aString.Length();
70 PRInt32 dstLen;
71 const nsAFlatCString& inStr = PromiseFlatCString(aString);
72 rv = unicodeDecoder->GetMaxLength(inStr.get(), srcLen, &dstLen);
73 NS_ENSURE_SUCCESS(rv, rv);
75 nsAutoArrayPtr<PRUnichar> ustr(new PRUnichar[dstLen]);
76 NS_ENSURE_TRUE(ustr, NS_ERROR_OUT_OF_MEMORY);
78 rv = unicodeDecoder->Convert(inStr.get(), &srcLen, ustr, &dstLen);
79 if (NS_SUCCEEDED(rv)){
80 // Tru64 Cxx and IRIX MIPSpro 7.3 need an explicit get()
81 CopyUTF16toUTF8(Substring(ustr.get(), ustr + dstLen), aResult);
83 return rv;
86 NS_IMETHODIMP
87 nsUTF8ConverterService::ConvertStringToUTF8(const nsACString &aString,
88 const char *aCharset,
89 PRBool aSkipCheck,
90 nsACString &aUTF8String)
92 // return if ASCII only or valid UTF-8 providing that the ASCII/UTF-8
93 // check is requested. It may not be asked for if a caller suspects
94 // that the input is in non-ASCII 7bit charset (ISO-2022-xx, HZ) or
95 // it's in a charset other than UTF-8 that can be mistaken for UTF-8.
96 if (!aSkipCheck && (IsASCII(aString) || IsUTF8(aString))) {
97 aUTF8String = aString;
98 return NS_OK;
101 aUTF8String.Truncate();
103 nsresult rv = ToUTF8(aString, aCharset, aUTF8String);
105 // additional protection for cases where check is skipped and the input
106 // is actually in UTF-8 as opposed to aCharset. (i.e. caller's hunch
107 // was wrong.) We don't check ASCIIness assuming there's no charset
108 // incompatible with ASCII (we don't support EBCDIC).
109 if (aSkipCheck && NS_FAILED(rv) && IsUTF8(aString)) {
110 aUTF8String = aString;
111 return NS_OK;
114 return rv;
117 NS_IMETHODIMP
118 nsUTF8ConverterService::ConvertURISpecToUTF8(const nsACString &aSpec,
119 const char *aCharset,
120 nsACString &aUTF8Spec)
122 // assume UTF-8 if the spec contains unescaped non-ASCII characters.
123 // No valid spec in Mozilla would break this assumption.
124 if (!IsASCII(aSpec)) {
125 aUTF8Spec = aSpec;
126 return NS_OK;
129 aUTF8Spec.Truncate();
131 nsCAutoString unescapedSpec;
132 // NS_UnescapeURL does not fill up unescapedSpec unless there's at least
133 // one character to unescape.
134 PRBool written = NS_UnescapeURL(PromiseFlatCString(aSpec).get(), aSpec.Length(),
135 esc_OnlyNonASCII, unescapedSpec);
137 if (!written) {
138 aUTF8Spec = aSpec;
139 return NS_OK;
141 // return if ASCII only or escaped UTF-8
142 if (IsASCII(unescapedSpec) || IsUTF8(unescapedSpec)) {
143 aUTF8Spec = unescapedSpec;
144 return NS_OK;
147 return ToUTF8(unescapedSpec, aCharset, aUTF8Spec);