Back to index

lightning-sunbird  0.9+nobinonly
nsUnicodeToUCS2BE.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is Mozilla Communicator client code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "nsUnicodeToUCS2BE.h"
00039 #include <string.h>
00040 
00041 inline static void SwapBytes(char *aDest, const PRUnichar* aSrc, PRInt32 aLen);
00042 
00043 NS_IMETHODIMP nsUnicodeToUTF16BE::Convert(const PRUnichar * aSrc, PRInt32 * aSrcLength, 
00044       char * aDest, PRInt32 * aDestLength)
00045 {
00046   PRInt32 srcInLen = *aSrcLength;
00047   PRInt32 destInLen = *aDestLength;
00048   PRInt32 srcOutLen = 0;
00049   PRInt32 destOutLen = 0;
00050   PRInt32 copyCharLen;
00051   PRUnichar *p = (PRUnichar*)aDest;
00052  
00053   // Handle BOM if necessary 
00054   if(0!=mBOM)
00055   {
00056      if(destInLen <2)
00057         goto needmoreoutput;
00058   
00059      *p++ = mBOM;
00060      mBOM = 0;
00061      destOutLen +=2;
00062   }
00063   // find out the length of copy 
00064 
00065   copyCharLen = srcInLen;
00066   if(copyCharLen > (destInLen - destOutLen) / 2) {
00067      copyCharLen = (destInLen - destOutLen) / 2;
00068   }
00069 
00070   // copy the data  by swaping 
00071   CopyData((char*)p , aSrc, copyCharLen );
00072 
00073   srcOutLen += copyCharLen;
00074   destOutLen += copyCharLen * 2;
00075   if(copyCharLen < srcInLen)
00076       goto needmoreoutput;
00077   
00078   *aSrcLength = srcOutLen;
00079   *aDestLength = destOutLen;
00080   return NS_OK;
00081 
00082 needmoreoutput:
00083   *aSrcLength = srcOutLen;
00084   *aDestLength = destOutLen;
00085   return NS_OK_UENC_MOREOUTPUT;
00086 }
00087 
00088 NS_IMETHODIMP nsUnicodeToUTF16BE::GetMaxLength(const PRUnichar * aSrc, PRInt32 aSrcLength, 
00089       PRInt32 * aDestLength)
00090 {
00091   if(0 != mBOM)
00092     *aDestLength = 2*(aSrcLength+1);
00093   else 
00094     *aDestLength = 2*aSrcLength;
00095   return NS_OK_UENC_EXACTLENGTH;
00096 }
00097 
00098 NS_IMETHODIMP nsUnicodeToUTF16BE::Finish(char * aDest, PRInt32 * aDestLength)
00099 {
00100   if(0 != mBOM)
00101   {
00102      if(*aDestLength >= 2)
00103      {
00104         *((PRUnichar*)aDest)= mBOM;
00105         mBOM=0;
00106         *aDestLength = 2;
00107         return NS_OK;  
00108      } else {
00109         *aDestLength = 0;
00110         return NS_OK;  // xxx should be error
00111      }
00112   } else { 
00113      *aDestLength = 0;
00114      return NS_OK;
00115   } 
00116 }
00117 
00118 NS_IMETHODIMP nsUnicodeToUTF16BE::Reset()
00119 {
00120   mBOM = 0;
00121   return NS_OK;
00122 }
00123 
00124 NS_IMETHODIMP nsUnicodeToUTF16BE::SetOutputErrorBehavior(PRInt32 aBehavior, 
00125       nsIUnicharEncoder * aEncoder, PRUnichar aChar)
00126 {
00127   return NS_OK;
00128 }
00129 
00130 NS_IMETHODIMP nsUnicodeToUTF16BE::FillInfo(PRUint32 *aInfo)
00131 {
00132   ::memset(aInfo, 0xFF, (0x10000L >> 3));
00133   return NS_OK;
00134 }
00135 
00136 NS_IMETHODIMP nsUnicodeToUTF16BE::CopyData(char* aDest, const PRUnichar* aSrc, PRInt32 aLen  )
00137 {
00138 #ifdef IS_BIG_ENDIAN
00139   //UnicodeToUTF16SameEndian
00140   ::memcpy(aDest, (void*) aSrc, aLen * 2);
00141 #elif defined(IS_LITTLE_ENDIAN)
00142   //UnicodeToUTF16DiffEndian
00143   SwapBytes(aDest, aSrc, aLen);
00144 #else
00145   #error "Unknown endianness"
00146 #endif
00147   return NS_OK;
00148 }
00149 
00150 NS_IMETHODIMP nsUnicodeToUTF16LE::CopyData(char* aDest, const PRUnichar* aSrc, PRInt32 aLen  )
00151 {
00152 #ifdef IS_LITTLE_ENDIAN
00153   //UnicodeToUTF16SameEndian
00154   ::memcpy(aDest, (void*) aSrc, aLen * 2);
00155 #elif defined(IS_BIG_ENDIAN)
00156   //UnicodeToUTF16DiffEndian
00157   SwapBytes(aDest, aSrc, aLen);
00158 #else
00159   #error "Unknown endianness"
00160 #endif
00161   return NS_OK;
00162 }
00163 
00164 inline void SwapBytes(char *aDest, const PRUnichar* aSrc, PRInt32 aLen)
00165 {
00166   PRUnichar *p = (PRUnichar*) aDest;
00167   // copy the data  by swaping 
00168   for(PRInt32 i = 0; i < aLen; i++)
00169   {
00170     PRUnichar aChar = *aSrc++;
00171     *p++ = (0x00FF & (aChar >> 8)) | (0xFF00 & (aChar << 8));
00172   }
00173 }
00174