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 "nsUnicodeToUCS2BE.h"
39 : #include <string.h>
40 :
41 : inline static void SwapBytes(char *aDest, const PRUnichar* aSrc, PRInt32 aLen);
42 :
43 33 : NS_IMETHODIMP nsUnicodeToUTF16BE::Convert(const PRUnichar * aSrc, PRInt32 * aSrcLength,
44 : char * aDest, PRInt32 * aDestLength)
45 : {
46 33 : PRInt32 srcInLen = *aSrcLength;
47 33 : PRInt32 destInLen = *aDestLength;
48 33 : PRInt32 srcOutLen = 0;
49 33 : PRInt32 destOutLen = 0;
50 : PRInt32 copyCharLen;
51 33 : PRUnichar *p = (PRUnichar*)aDest;
52 :
53 : // Handle BOM if necessary
54 33 : if(0!=mBOM)
55 : {
56 7 : if(destInLen <2)
57 0 : goto needmoreoutput;
58 :
59 7 : *p++ = mBOM;
60 7 : mBOM = 0;
61 7 : destOutLen +=2;
62 : }
63 : // find out the length of copy
64 :
65 33 : copyCharLen = srcInLen;
66 33 : if(copyCharLen > (destInLen - destOutLen) / 2) {
67 0 : copyCharLen = (destInLen - destOutLen) / 2;
68 : }
69 :
70 : // copy the data by swaping
71 33 : CopyData((char*)p , aSrc, copyCharLen );
72 :
73 33 : srcOutLen += copyCharLen;
74 33 : destOutLen += copyCharLen * 2;
75 33 : if(copyCharLen < srcInLen)
76 0 : goto needmoreoutput;
77 :
78 33 : *aSrcLength = srcOutLen;
79 33 : *aDestLength = destOutLen;
80 33 : return NS_OK;
81 :
82 : needmoreoutput:
83 0 : *aSrcLength = srcOutLen;
84 0 : *aDestLength = destOutLen;
85 0 : return NS_OK_UENC_MOREOUTPUT;
86 : }
87 :
88 33 : NS_IMETHODIMP nsUnicodeToUTF16BE::GetMaxLength(const PRUnichar * aSrc, PRInt32 aSrcLength,
89 : PRInt32 * aDestLength)
90 : {
91 33 : if(0 != mBOM)
92 7 : *aDestLength = 2*(aSrcLength+1);
93 : else
94 26 : *aDestLength = 2*aSrcLength;
95 33 : return NS_OK_UENC_EXACTLENGTH;
96 : }
97 :
98 6 : NS_IMETHODIMP nsUnicodeToUTF16BE::Finish(char * aDest, PRInt32 * aDestLength)
99 : {
100 6 : if(0 != mBOM)
101 : {
102 0 : if(*aDestLength >= 2)
103 : {
104 0 : *((PRUnichar*)aDest)= mBOM;
105 0 : mBOM=0;
106 0 : *aDestLength = 2;
107 0 : return NS_OK;
108 : } else {
109 0 : *aDestLength = 0;
110 0 : return NS_OK; // xxx should be error
111 : }
112 : } else {
113 6 : *aDestLength = 0;
114 6 : return NS_OK;
115 : }
116 : }
117 :
118 0 : NS_IMETHODIMP nsUnicodeToUTF16BE::Reset()
119 : {
120 0 : mBOM = 0;
121 0 : return NS_OK;
122 : }
123 :
124 32 : NS_IMETHODIMP nsUnicodeToUTF16BE::SetOutputErrorBehavior(PRInt32 aBehavior,
125 : nsIUnicharEncoder * aEncoder, PRUnichar aChar)
126 : {
127 32 : return NS_OK;
128 : }
129 :
130 13 : NS_IMETHODIMP nsUnicodeToUTF16BE::CopyData(char* aDest, const PRUnichar* aSrc, PRInt32 aLen )
131 : {
132 : #ifdef IS_BIG_ENDIAN
133 : //UnicodeToUTF16SameEndian
134 : ::memcpy(aDest, (void*) aSrc, aLen * 2);
135 : #elif defined(IS_LITTLE_ENDIAN)
136 : //UnicodeToUTF16DiffEndian
137 13 : SwapBytes(aDest, aSrc, aLen);
138 : #else
139 : #error "Unknown endianness"
140 : #endif
141 13 : return NS_OK;
142 : }
143 :
144 20 : NS_IMETHODIMP nsUnicodeToUTF16LE::CopyData(char* aDest, const PRUnichar* aSrc, PRInt32 aLen )
145 : {
146 : #ifdef IS_LITTLE_ENDIAN
147 : //UnicodeToUTF16SameEndian
148 20 : ::memcpy(aDest, (void*) aSrc, aLen * 2);
149 : #elif defined(IS_BIG_ENDIAN)
150 : //UnicodeToUTF16DiffEndian
151 : SwapBytes(aDest, aSrc, aLen);
152 : #else
153 : #error "Unknown endianness"
154 : #endif
155 20 : return NS_OK;
156 : }
157 :
158 13 : inline void SwapBytes(char *aDest, const PRUnichar* aSrc, PRInt32 aLen)
159 : {
160 13 : PRUnichar *p = (PRUnichar*) aDest;
161 : // copy the data by swaping
162 164 : for(PRInt32 i = 0; i < aLen; i++)
163 : {
164 151 : PRUnichar aChar = *aSrc++;
165 151 : *p++ = (0x00FF & (aChar >> 8)) | (0xFF00 & (aChar << 8));
166 : }
167 13 : }
168 :
|