michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nsUnicodeToUTF16.h" michael@0: #include michael@0: michael@0: NS_IMETHODIMP nsUnicodeToUTF16BE::Convert(const char16_t * aSrc, int32_t * aSrcLength, michael@0: char * aDest, int32_t * aDestLength) michael@0: { michael@0: int32_t srcInLen = *aSrcLength; michael@0: int32_t destInLen = *aDestLength; michael@0: int32_t srcOutLen = 0; michael@0: int32_t destOutLen = 0; michael@0: int32_t copyCharLen; michael@0: char16_t *p = (char16_t*)aDest; michael@0: michael@0: // Handle BOM if necessary michael@0: if(0!=mBOM) michael@0: { michael@0: if(destInLen <2) michael@0: goto needmoreoutput; michael@0: michael@0: *p++ = mBOM; michael@0: mBOM = 0; michael@0: destOutLen +=2; michael@0: } michael@0: // find out the length of copy michael@0: michael@0: copyCharLen = srcInLen; michael@0: if(copyCharLen > (destInLen - destOutLen) / 2) { michael@0: copyCharLen = (destInLen - destOutLen) / 2; michael@0: } michael@0: michael@0: // copy the data by swaping michael@0: CopyData((char*)p , aSrc, copyCharLen ); michael@0: michael@0: srcOutLen += copyCharLen; michael@0: destOutLen += copyCharLen * 2; michael@0: if(copyCharLen < srcInLen) michael@0: goto needmoreoutput; michael@0: michael@0: *aSrcLength = srcOutLen; michael@0: *aDestLength = destOutLen; michael@0: return NS_OK; michael@0: michael@0: needmoreoutput: michael@0: *aSrcLength = srcOutLen; michael@0: *aDestLength = destOutLen; michael@0: return NS_OK_UENC_MOREOUTPUT; michael@0: } michael@0: michael@0: NS_IMETHODIMP nsUnicodeToUTF16BE::GetMaxLength(const char16_t * aSrc, int32_t aSrcLength, michael@0: int32_t * aDestLength) michael@0: { michael@0: if(0 != mBOM) michael@0: *aDestLength = 2*(aSrcLength+1); michael@0: else michael@0: *aDestLength = 2*aSrcLength; michael@0: return NS_OK_UENC_EXACTLENGTH; michael@0: } michael@0: michael@0: NS_IMETHODIMP nsUnicodeToUTF16BE::Finish(char * aDest, int32_t * aDestLength) michael@0: { michael@0: if(0 != mBOM) michael@0: { michael@0: if(*aDestLength >= 2) michael@0: { michael@0: *((char16_t*)aDest)= mBOM; michael@0: mBOM=0; michael@0: *aDestLength = 2; michael@0: return NS_OK; michael@0: } else { michael@0: *aDestLength = 0; michael@0: return NS_OK; // xxx should be error michael@0: } michael@0: } else { michael@0: *aDestLength = 0; michael@0: return NS_OK; michael@0: } michael@0: } michael@0: michael@0: NS_IMETHODIMP nsUnicodeToUTF16BE::Reset() michael@0: { michael@0: mBOM = 0; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP nsUnicodeToUTF16BE::SetOutputErrorBehavior(int32_t aBehavior, michael@0: nsIUnicharEncoder * aEncoder, char16_t aChar) michael@0: { michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP nsUnicodeToUTF16BE::CopyData(char* aDest, const char16_t* aSrc, int32_t aLen ) michael@0: { michael@0: mozilla::NativeEndian::copyAndSwapToBigEndian(aDest, aSrc, aLen); michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP nsUnicodeToUTF16LE::CopyData(char* aDest, const char16_t* aSrc, int32_t aLen ) michael@0: { michael@0: mozilla::NativeEndian::copyAndSwapToLittleEndian(aDest, aSrc, aLen); michael@0: return NS_OK; michael@0: }