1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
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/
9 : *
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
13 : * License.
14 : *
15 : * The Original Code is Mozilla Communicator client code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * Netscape Communications Corporation.
19 : * Portions created by the Initial Developer are Copyright (C) 1998
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : *
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.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #include "pratom.h"
39 : #include "unicpriv.h"
40 : #include "nsIUnicodeEncoder.h"
41 : #include "nsUnicodeEncodeHelper.h"
42 :
43 : //----------------------------------------------------------------------
44 : // Class nsUnicodeEncodeHelper [implementation]
45 172272 : nsresult nsUnicodeEncodeHelper::ConvertByTable(
46 : const PRUnichar * aSrc,
47 : PRInt32 * aSrcLength,
48 : char * aDest,
49 : PRInt32 * aDestLength,
50 : uScanClassID aScanClass,
51 : uShiftOutTable * aShiftOutTable,
52 : uMappingTable * aMappingTable)
53 : {
54 172272 : const PRUnichar * src = aSrc;
55 172272 : const PRUnichar * srcEnd = aSrc + *aSrcLength;
56 172272 : char * dest = aDest;
57 172272 : PRInt32 destLen = *aDestLength;
58 :
59 : PRUnichar med;
60 : PRInt32 bcw; // byte count for write;
61 172272 : nsresult res = NS_OK;
62 :
63 550275 : while (src < srcEnd) {
64 217890 : if (!uMapCode((uTable*) aMappingTable, static_cast<PRUnichar>(*(src++)), reinterpret_cast<PRUint16*>(&med))) {
65 12167 : if (aScanClass == u1ByteCharset && *(src - 1) < 0x20) {
66 : // some tables are missing the 0x00 - 0x20 part
67 8 : med = *(src - 1);
68 : } else {
69 12159 : res = NS_ERROR_UENC_NOMAPPING;
70 12159 : break;
71 : }
72 : }
73 :
74 : bool charFound;
75 205731 : if (aScanClass == uMultibytesCharset) {
76 11 : NS_ASSERTION(aShiftOutTable, "shift table missing");
77 : charFound = uGenerateShift(aShiftOutTable, 0, med,
78 : (PRUint8 *)dest, destLen,
79 11 : (PRUint32 *)&bcw);
80 : } else {
81 : charFound = uGenerate(aScanClass, 0, med,
82 : (PRUint8 *)dest, destLen,
83 205720 : (PRUint32 *)&bcw);
84 : }
85 205731 : if (!charFound) {
86 0 : src--;
87 0 : res = NS_OK_UENC_MOREOUTPUT;
88 0 : break;
89 : }
90 :
91 205731 : dest += bcw;
92 205731 : destLen -= bcw;
93 : }
94 :
95 172272 : *aSrcLength = src - aSrc;
96 172272 : *aDestLength = dest - aDest;
97 172272 : return res;
98 : }
99 :
100 7 : nsresult nsUnicodeEncodeHelper::ConvertByMultiTable(
101 : const PRUnichar * aSrc,
102 : PRInt32 * aSrcLength,
103 : char * aDest,
104 : PRInt32 * aDestLength,
105 : PRInt32 aTableCount,
106 : uScanClassID * aScanClassArray,
107 : uShiftOutTable ** aShiftOutTable,
108 : uMappingTable ** aMappingTable)
109 : {
110 7 : const PRUnichar * src = aSrc;
111 7 : const PRUnichar * srcEnd = aSrc + *aSrcLength;
112 7 : char * dest = aDest;
113 7 : PRInt32 destLen = *aDestLength;
114 :
115 : PRUnichar med;
116 : PRInt32 bcw; // byte count for write;
117 7 : nsresult res = NS_OK;
118 : PRInt32 i;
119 :
120 91 : while (src < srcEnd) {
121 99 : for (i=0; i<aTableCount; i++)
122 99 : if (uMapCode((uTable*) aMappingTable[i], static_cast<PRUint16>(*src), reinterpret_cast<PRUint16*>(&med))) break;
123 :
124 77 : src++;
125 77 : if (i == aTableCount) {
126 0 : res = NS_ERROR_UENC_NOMAPPING;
127 0 : break;
128 : }
129 :
130 : bool charFound;
131 77 : if (aScanClassArray[i] == uMultibytesCharset) {
132 11 : NS_ASSERTION(aShiftOutTable[i], "shift table missing");
133 11 : charFound = uGenerateShift(aShiftOutTable[i], 0, med,
134 : (PRUint8 *)dest, destLen,
135 22 : (PRUint32 *)&bcw);
136 : }
137 : else
138 66 : charFound = uGenerate(aScanClassArray[i], 0, med,
139 : (PRUint8 *)dest, destLen,
140 132 : (PRUint32 *)&bcw);
141 77 : if (!charFound) {
142 0 : src--;
143 0 : res = NS_OK_UENC_MOREOUTPUT;
144 0 : break;
145 : }
146 :
147 77 : dest += bcw;
148 77 : destLen -= bcw;
149 : }
150 :
151 7 : *aSrcLength = src - aSrc;
152 7 : *aDestLength = dest - aDest;
153 7 : return res;
154 : }
|