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