intl/uconv/ucvlatin/nsUnicodeToUTF16.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     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/. */
     6 #include "nsUnicodeToUTF16.h"
     7 #include <string.h>
     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;
    19   // Handle BOM if necessary 
    20   if(0!=mBOM)
    21   {
    22      if(destInLen <2)
    23         goto needmoreoutput;
    25      *p++ = mBOM;
    26      mBOM = 0;
    27      destOutLen +=2;
    28   }
    29   // find out the length of copy 
    31   copyCharLen = srcInLen;
    32   if(copyCharLen > (destInLen - destOutLen) / 2) {
    33      copyCharLen = (destInLen - destOutLen) / 2;
    34   }
    36   // copy the data  by swaping 
    37   CopyData((char*)p , aSrc, copyCharLen );
    39   srcOutLen += copyCharLen;
    40   destOutLen += copyCharLen * 2;
    41   if(copyCharLen < srcInLen)
    42       goto needmoreoutput;
    44   *aSrcLength = srcOutLen;
    45   *aDestLength = destOutLen;
    46   return NS_OK;
    48 needmoreoutput:
    49   *aSrcLength = srcOutLen;
    50   *aDestLength = destOutLen;
    51   return NS_OK_UENC_MOREOUTPUT;
    52 }
    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 }
    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 }
    84 NS_IMETHODIMP nsUnicodeToUTF16BE::Reset()
    85 {
    86   mBOM = 0;
    87   return NS_OK;
    88 }
    90 NS_IMETHODIMP nsUnicodeToUTF16BE::SetOutputErrorBehavior(int32_t aBehavior, 
    91       nsIUnicharEncoder * aEncoder, char16_t aChar)
    92 {
    93   return NS_OK;
    94 }
    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 }
   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 }

mercurial