1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/intl/uconv/ucvlatin/nsUnicodeToUTF16.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,106 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#include "nsUnicodeToUTF16.h" 1.10 +#include <string.h> 1.11 + 1.12 +NS_IMETHODIMP nsUnicodeToUTF16BE::Convert(const char16_t * aSrc, int32_t * aSrcLength, 1.13 + char * aDest, int32_t * aDestLength) 1.14 +{ 1.15 + int32_t srcInLen = *aSrcLength; 1.16 + int32_t destInLen = *aDestLength; 1.17 + int32_t srcOutLen = 0; 1.18 + int32_t destOutLen = 0; 1.19 + int32_t copyCharLen; 1.20 + char16_t *p = (char16_t*)aDest; 1.21 + 1.22 + // Handle BOM if necessary 1.23 + if(0!=mBOM) 1.24 + { 1.25 + if(destInLen <2) 1.26 + goto needmoreoutput; 1.27 + 1.28 + *p++ = mBOM; 1.29 + mBOM = 0; 1.30 + destOutLen +=2; 1.31 + } 1.32 + // find out the length of copy 1.33 + 1.34 + copyCharLen = srcInLen; 1.35 + if(copyCharLen > (destInLen - destOutLen) / 2) { 1.36 + copyCharLen = (destInLen - destOutLen) / 2; 1.37 + } 1.38 + 1.39 + // copy the data by swaping 1.40 + CopyData((char*)p , aSrc, copyCharLen ); 1.41 + 1.42 + srcOutLen += copyCharLen; 1.43 + destOutLen += copyCharLen * 2; 1.44 + if(copyCharLen < srcInLen) 1.45 + goto needmoreoutput; 1.46 + 1.47 + *aSrcLength = srcOutLen; 1.48 + *aDestLength = destOutLen; 1.49 + return NS_OK; 1.50 + 1.51 +needmoreoutput: 1.52 + *aSrcLength = srcOutLen; 1.53 + *aDestLength = destOutLen; 1.54 + return NS_OK_UENC_MOREOUTPUT; 1.55 +} 1.56 + 1.57 +NS_IMETHODIMP nsUnicodeToUTF16BE::GetMaxLength(const char16_t * aSrc, int32_t aSrcLength, 1.58 + int32_t * aDestLength) 1.59 +{ 1.60 + if(0 != mBOM) 1.61 + *aDestLength = 2*(aSrcLength+1); 1.62 + else 1.63 + *aDestLength = 2*aSrcLength; 1.64 + return NS_OK_UENC_EXACTLENGTH; 1.65 +} 1.66 + 1.67 +NS_IMETHODIMP nsUnicodeToUTF16BE::Finish(char * aDest, int32_t * aDestLength) 1.68 +{ 1.69 + if(0 != mBOM) 1.70 + { 1.71 + if(*aDestLength >= 2) 1.72 + { 1.73 + *((char16_t*)aDest)= mBOM; 1.74 + mBOM=0; 1.75 + *aDestLength = 2; 1.76 + return NS_OK; 1.77 + } else { 1.78 + *aDestLength = 0; 1.79 + return NS_OK; // xxx should be error 1.80 + } 1.81 + } else { 1.82 + *aDestLength = 0; 1.83 + return NS_OK; 1.84 + } 1.85 +} 1.86 + 1.87 +NS_IMETHODIMP nsUnicodeToUTF16BE::Reset() 1.88 +{ 1.89 + mBOM = 0; 1.90 + return NS_OK; 1.91 +} 1.92 + 1.93 +NS_IMETHODIMP nsUnicodeToUTF16BE::SetOutputErrorBehavior(int32_t aBehavior, 1.94 + nsIUnicharEncoder * aEncoder, char16_t aChar) 1.95 +{ 1.96 + return NS_OK; 1.97 +} 1.98 + 1.99 +NS_IMETHODIMP nsUnicodeToUTF16BE::CopyData(char* aDest, const char16_t* aSrc, int32_t aLen ) 1.100 +{ 1.101 + mozilla::NativeEndian::copyAndSwapToBigEndian(aDest, aSrc, aLen); 1.102 + return NS_OK; 1.103 +} 1.104 + 1.105 +NS_IMETHODIMP nsUnicodeToUTF16LE::CopyData(char* aDest, const char16_t* aSrc, int32_t aLen ) 1.106 +{ 1.107 + mozilla::NativeEndian::copyAndSwapToLittleEndian(aDest, aSrc, aLen); 1.108 + return NS_OK; 1.109 +}