|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 #include "nsUnicodeToUTF16.h" |
|
7 #include <string.h> |
|
8 |
|
9 NS_IMETHODIMP nsUnicodeToUTF16BE::Convert(const char16_t * aSrc, int32_t * aSrcLength, |
|
10 char * aDest, int32_t * aDestLength) |
|
11 { |
|
12 int32_t srcInLen = *aSrcLength; |
|
13 int32_t destInLen = *aDestLength; |
|
14 int32_t srcOutLen = 0; |
|
15 int32_t destOutLen = 0; |
|
16 int32_t copyCharLen; |
|
17 char16_t *p = (char16_t*)aDest; |
|
18 |
|
19 // Handle BOM if necessary |
|
20 if(0!=mBOM) |
|
21 { |
|
22 if(destInLen <2) |
|
23 goto needmoreoutput; |
|
24 |
|
25 *p++ = mBOM; |
|
26 mBOM = 0; |
|
27 destOutLen +=2; |
|
28 } |
|
29 // find out the length of copy |
|
30 |
|
31 copyCharLen = srcInLen; |
|
32 if(copyCharLen > (destInLen - destOutLen) / 2) { |
|
33 copyCharLen = (destInLen - destOutLen) / 2; |
|
34 } |
|
35 |
|
36 // copy the data by swaping |
|
37 CopyData((char*)p , aSrc, copyCharLen ); |
|
38 |
|
39 srcOutLen += copyCharLen; |
|
40 destOutLen += copyCharLen * 2; |
|
41 if(copyCharLen < srcInLen) |
|
42 goto needmoreoutput; |
|
43 |
|
44 *aSrcLength = srcOutLen; |
|
45 *aDestLength = destOutLen; |
|
46 return NS_OK; |
|
47 |
|
48 needmoreoutput: |
|
49 *aSrcLength = srcOutLen; |
|
50 *aDestLength = destOutLen; |
|
51 return NS_OK_UENC_MOREOUTPUT; |
|
52 } |
|
53 |
|
54 NS_IMETHODIMP nsUnicodeToUTF16BE::GetMaxLength(const char16_t * aSrc, int32_t aSrcLength, |
|
55 int32_t * aDestLength) |
|
56 { |
|
57 if(0 != mBOM) |
|
58 *aDestLength = 2*(aSrcLength+1); |
|
59 else |
|
60 *aDestLength = 2*aSrcLength; |
|
61 return NS_OK_UENC_EXACTLENGTH; |
|
62 } |
|
63 |
|
64 NS_IMETHODIMP nsUnicodeToUTF16BE::Finish(char * aDest, int32_t * aDestLength) |
|
65 { |
|
66 if(0 != mBOM) |
|
67 { |
|
68 if(*aDestLength >= 2) |
|
69 { |
|
70 *((char16_t*)aDest)= mBOM; |
|
71 mBOM=0; |
|
72 *aDestLength = 2; |
|
73 return NS_OK; |
|
74 } else { |
|
75 *aDestLength = 0; |
|
76 return NS_OK; // xxx should be error |
|
77 } |
|
78 } else { |
|
79 *aDestLength = 0; |
|
80 return NS_OK; |
|
81 } |
|
82 } |
|
83 |
|
84 NS_IMETHODIMP nsUnicodeToUTF16BE::Reset() |
|
85 { |
|
86 mBOM = 0; |
|
87 return NS_OK; |
|
88 } |
|
89 |
|
90 NS_IMETHODIMP nsUnicodeToUTF16BE::SetOutputErrorBehavior(int32_t aBehavior, |
|
91 nsIUnicharEncoder * aEncoder, char16_t aChar) |
|
92 { |
|
93 return NS_OK; |
|
94 } |
|
95 |
|
96 NS_IMETHODIMP nsUnicodeToUTF16BE::CopyData(char* aDest, const char16_t* aSrc, int32_t aLen ) |
|
97 { |
|
98 mozilla::NativeEndian::copyAndSwapToBigEndian(aDest, aSrc, aLen); |
|
99 return NS_OK; |
|
100 } |
|
101 |
|
102 NS_IMETHODIMP nsUnicodeToUTF16LE::CopyData(char* aDest, const char16_t* aSrc, int32_t aLen ) |
|
103 { |
|
104 mozilla::NativeEndian::copyAndSwapToLittleEndian(aDest, aSrc, aLen); |
|
105 return NS_OK; |
|
106 } |