Back to index

lightning-sunbird  0.9+nobinonly
nsUnicodeDecodeHelper.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 "pratom.h"
00039 #include "unicpriv.h"
00040 #include "nsUConvDll.h"
00041 #include "nsIMappingCache.h"
00042 #include "nsMappingCache.h"
00043 #include "nsIUnicodeDecodeHelper.h"
00044 #include "nsIUnicodeDecoder.h"
00045 #include "nsUnicodeDecodeHelper.h"
00046 
00047 //----------------------------------------------------------------------
00048 // Class nsUnicodeDecodeHelper [implementation]
00049 
00050 NS_IMPL_ISUPPORTS1(nsUnicodeDecodeHelper, nsIUnicodeDecodeHelper)
00051 
00052 nsUnicodeDecodeHelper::nsUnicodeDecodeHelper() 
00053 {
00054 }
00055 
00056 nsUnicodeDecodeHelper::~nsUnicodeDecodeHelper() 
00057 {
00058 }
00059 
00060 //----------------------------------------------------------------------
00061 // Interface nsIUnicodeDecodeHelper [implementation]
00062 
00063 NS_IMETHODIMP nsUnicodeDecodeHelper::ConvertByTable(
00064                                      const char * aSrc, 
00065                                      PRInt32 * aSrcLength, 
00066                                      PRUnichar * aDest, 
00067                                      PRInt32 * aDestLength, 
00068                                      uShiftTable * aShiftTable, 
00069                                      uMappingTable  * aMappingTable)
00070 {
00071   const char * src = aSrc;
00072   PRInt32 srcLen = *aSrcLength;
00073   PRUnichar * dest = aDest;
00074   PRUnichar * destEnd = aDest + *aDestLength;
00075 
00076   PRUnichar med;
00077   PRInt32 bcr; // byte count for read
00078   nsresult res = NS_OK;
00079 
00080   while ((srcLen > 0) && (dest < destEnd)) {
00081     if (!uScan(aShiftTable, NULL, (PRUint8 *)src, NS_REINTERPRET_CAST(PRUint16*, &med), srcLen, 
00082     (PRUint32 *)&bcr)) {
00083       res = NS_OK_UDEC_MOREINPUT;
00084       break;
00085     }
00086 
00087     if (!uMapCode((uTable*) aMappingTable, NS_STATIC_CAST(PRUint16, med), NS_REINTERPRET_CAST(PRUint16*, dest))) {
00088       if (med < 0x20) {
00089         // somehow some table miss the 0x00 - 0x20 part
00090         *dest = med;
00091       } else {
00092         // Unicode replacement value for unmappable chars
00093         *dest = 0xfffd;
00094       }
00095     }
00096 
00097     src += bcr;
00098     srcLen -= bcr;
00099     dest++;
00100   }
00101 
00102   if ((srcLen > 0) && (res == NS_OK)) res = NS_OK_UDEC_MOREOUTPUT;
00103 
00104   *aSrcLength = src - aSrc;
00105   *aDestLength  = dest - aDest;
00106   return res;
00107 }
00108 
00109 NS_IMETHODIMP nsUnicodeDecodeHelper::ConvertByMultiTable(
00110                                      const char * aSrc, 
00111                                      PRInt32 * aSrcLength, 
00112                                      PRUnichar * aDest, 
00113                                      PRInt32 * aDestLength, 
00114                                      PRInt32 aTableCount, 
00115                                      const uRange * aRangeArray, 
00116                                      uShiftTable ** aShiftTable, 
00117                                      uMappingTable ** aMappingTable)
00118 {
00119   PRUint8 * src = (PRUint8 *)aSrc;
00120   PRInt32 srcLen = *aSrcLength;
00121   PRUnichar * dest = aDest;
00122   PRUnichar * destEnd = aDest + *aDestLength;
00123 
00124   PRUnichar med;
00125   PRInt32 bcr; // byte count for read
00126   nsresult res = NS_OK;
00127   PRInt32 i;
00128 
00129   while ((srcLen > 0) && (dest < destEnd)) 
00130   {
00131     PRBool done= PR_FALSE;
00132     PRBool passRangeCheck = PR_FALSE;
00133     PRBool passScan = PR_FALSE;
00134     for (i=0; (!done) && (i<aTableCount); i++)  
00135     {
00136       if ((aRangeArray[i].min <= *src) && (*src <= aRangeArray[i].max)) 
00137       {
00138         passRangeCheck = PR_TRUE;
00139         if (uScan(aShiftTable[i], NULL, src, 
00140                    NS_REINTERPRET_CAST(PRUint16*, &med), srcLen, 
00141                    (PRUint32 *)&bcr)) 
00142         {
00143           passScan = PR_TRUE;
00144           done = uMapCode((uTable*) aMappingTable[i], 
00145                           NS_STATIC_CAST(PRUint16, med), 
00146                           NS_REINTERPRET_CAST(PRUint16*, dest)); 
00147         } // if (uScan ... )
00148       } // if Range
00149     } // for loop
00150 
00151     if(passRangeCheck && (! passScan))
00152     {
00153       res = NS_OK_UDEC_MOREINPUT;
00154       break;
00155     }
00156     if(! done)
00157     {
00158       bcr = 1;
00159       if ((PRUint8)*src < 0x20) {
00160         // somehow some table miss the 0x00 - 0x20 part
00161         *dest = *src;
00162       } else if(*src == (PRUint8) 0xa0) {
00163         // handle nbsp
00164         *dest = 0x00a0;
00165       } else {
00166         // we need to decide how many byte we skip. We can use uScan to do this
00167         for (i=0; i<aTableCount; i++)  
00168         {
00169           if ((aRangeArray[i].min <= *src) && (*src <= aRangeArray[i].max)) 
00170           {
00171             if (uScan(aShiftTable[i], NULL, src, 
00172                    NS_REINTERPRET_CAST(PRUint16*, &med), srcLen, 
00173                    (PRUint32*)&bcr)) 
00174             { 
00175                // match the patten
00176               
00177                PRInt32 k; 
00178                for(k = 1; k < bcr; k++)
00179                {
00180                  if(0 == (src[k]  & 0x80))
00181                  { // only skip if all bytes > 0x80
00182                    // if we hit bytes <= 0x80, skip only one byte
00183                    bcr = 1;
00184                    break; 
00185                  }
00186                }
00187                break;
00188             }
00189           }
00190         }
00191         // treat it as NSBR if bcr == 1 and it is 0xa0
00192         *dest = ((1==bcr)&&(*src == (PRUint8)0xa0 )) ? 0x00a0 : 0xfffd;
00193       }
00194     }
00195 
00196     src += bcr;
00197     srcLen -= bcr;
00198     dest++;
00199   } // while
00200 
00201   if ((srcLen > 0) && (res == NS_OK)) res = NS_OK_UDEC_MOREOUTPUT;
00202 
00203   *aSrcLength = src - (PRUint8 *)aSrc;
00204   *aDestLength  = dest - aDest;
00205   return res;
00206 }
00207 
00208 NS_IMETHODIMP nsUnicodeDecodeHelper::CreateCache(nsMappingCacheType aType, nsIMappingCache* aResult)
00209 {
00210    return nsMappingCache::CreateCache(aType, aResult);
00211 }
00212 
00213 NS_IMETHODIMP nsUnicodeDecodeHelper::DestroyCache(nsIMappingCache aCache)
00214 {
00215    return nsMappingCache::DestroyCache(aCache);
00216 }
00217 
00218 NS_IMETHODIMP nsUnicodeDecodeHelper::ConvertByFastTable(
00219                                      const char * aSrc, 
00220                                      PRInt32 * aSrcLength, 
00221                                      PRUnichar * aDest, 
00222                                      PRInt32 * aDestLength, 
00223                                      const PRUnichar * aFastTable, 
00224                                      PRInt32 aTableSize)
00225 {
00226   PRUint8 * src = (PRUint8 *)aSrc;
00227   PRUint8 * srcEnd = src;
00228   PRUnichar * dest = aDest;
00229 
00230   nsresult res;
00231   if (*aSrcLength > *aDestLength) {
00232     srcEnd += (*aDestLength);
00233     res = NS_PARTIAL_MORE_OUTPUT;
00234   } else {
00235     srcEnd += (*aSrcLength);
00236     res = NS_OK;
00237   }
00238 
00239   for (; src<srcEnd;) *dest++ = aFastTable[*src++];
00240 
00241   *aSrcLength = src - (PRUint8 *)aSrc;
00242   *aDestLength  = dest - aDest;
00243   return res;
00244 }
00245 
00246 NS_IMETHODIMP nsUnicodeDecodeHelper::CreateFastTable(
00247                                      uShiftTable * aShiftTable, 
00248                                      uMappingTable  * aMappingTable,
00249                                      PRUnichar * aFastTable, 
00250                                      PRInt32 aTableSize)
00251 {
00252   PRInt32 tableSize = aTableSize;
00253   PRInt32 buffSize = aTableSize;
00254   char * buff = new char [buffSize];
00255   if (buff == NULL) return NS_ERROR_OUT_OF_MEMORY;
00256 
00257   char * p = buff;
00258   for (PRInt32 i=0; i<aTableSize; i++) *(p++) = i;
00259   nsresult res = ConvertByTable(buff, &buffSize, aFastTable, &tableSize, 
00260       aShiftTable, aMappingTable);
00261 
00262   delete [] buff;
00263   return res;
00264 }
00265