Back to index

lightning-sunbird  0.9+nobinonly
nsHZToUnicode.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C; tab-width: 4; 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.org 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 ***** */
00061 #include "nsUCvCnDll.h"
00062 #include "nsHZToUnicode.h"
00063 #include "gbku.h"
00064 
00065 //----------------------------------------------------------------------
00066 // Class nsHZToUnicode [implementation]
00067 
00068 //----------------------------------------------------------------------
00069 // Subclassing of nsTablesDecoderSupport class [implementation]
00070 
00071 #define HZ_STATE_GB         1
00072 #define HZ_STATE_ASCII      2
00073 #define HZ_STATE_TILD       3
00074 #define HZLEAD1 '~'
00075 #define HZLEAD2 '{'
00076 #define HZLEAD3 '}'
00077 #define HZLEAD4 '\n'
00078 
00079 nsHZToUnicode::nsHZToUnicode() : nsBufferDecoderSupport(1)
00080 {
00081   mHZState = HZ_STATE_ASCII;       // per HZ spec, default to ASCII state 
00082   mRunLength = 0;
00083 }
00084 //Overwriting the ConvertNoBuff() in nsUCvCnSupport.cpp.
00085 NS_IMETHODIMP nsHZToUnicode::ConvertNoBuff(
00086   const char* aSrc, 
00087   PRInt32 * aSrcLength, 
00088   PRUnichar *aDest, 
00089   PRInt32 * aDestLength)
00090 {
00091   PRInt32 i=0;
00092   PRInt32 iSrcLength = *aSrcLength;
00093   PRInt32 iDestlen = 0;
00094   PRUint8 ch1, ch2;
00095   nsresult res = NS_OK;
00096   *aSrcLength=0;
00097   for (i=0;i<iSrcLength;i++)
00098   {
00099     if ( iDestlen >= (*aDestLength) )
00100     {
00101       res = NS_OK_UDEC_MOREOUTPUT;
00102       break;
00103     }
00104     if ( *aSrc & 0x80 ) // if it is a 8-bit byte
00105     {
00106       if (UINT8_IN_RANGE(0x81, aSrc[0], 0xFE) &&
00107           UINT8_IN_RANGE(0x40, aSrc[1], 0xFE)) {
00108         // The source is a 8-bit GBCode
00109         *aDest = mUtil.GBKCharToUnicode(aSrc[0], aSrc[1]);
00110       } else {
00111         *aDest = UCS2_NO_MAPPING;
00112       }
00113       aSrc += 2;
00114       i++;
00115       iDestlen++;
00116       aDest++;
00117       *aSrcLength = i+1;
00118       continue;
00119     }
00120     // otherwise, it is a 7-bit byte 
00121     // The source will be an ASCII or a 7-bit HZ code depending on ch1
00122     ch1 = *aSrc;
00123     ch2       = *(aSrc+1);
00124     if (ch1 == HZLEAD1 )  // if it is lead by '~'
00125     {
00126       switch (ch2)
00127       {
00128         case HZLEAD2: 
00129           // we got a '~{'
00130           // we are switching to HZ state
00131           mHZState = HZ_STATE_GB;
00132           mRunLength = 0;
00133           aSrc += 2;
00134           i++;
00135           break;
00136         case HZLEAD3: 
00137           // we got a '~}'
00138           // we are switching to ASCII state
00139           mHZState = HZ_STATE_ASCII;
00140           aSrc += 2;
00141           i++;
00142           if (mRunLength == 0) {
00143             *aDest = UCS2_NO_MAPPING;
00144             iDestlen++;
00145             aDest++;
00146           }
00147           mRunLength = 0;
00148           break;
00149         case HZLEAD1: 
00150           // we got a '~~', process like an ASCII, but no state change
00151           aSrc++;
00152           *aDest = CAST_CHAR_TO_UNICHAR(*aSrc);
00153           aSrc++;
00154           i++;
00155           iDestlen++;
00156           aDest++;
00157           mRunLength++;
00158           break;
00159         case HZLEAD4:       
00160           // we got a "~\n", it means maintain double byte mode cross lines, ignore the '~' itself
00161           //  mHZState = HZ_STATE_GB; 
00162           // I find that "~\n" should interpreted as line continuation without mode change
00163           // It should not be interpreted as line continuation with double byte mode on
00164           aSrc++;
00165           break;
00166         default:
00167           // undefined ESC sequence '~X' are ignored since this is a illegal combination 
00168           aSrc += 2;
00169           *aDest = UCS2_NO_MAPPING;
00170           iDestlen++;
00171           aDest++;
00172           break;
00173       };
00174       continue;// go for next loop
00175     }
00176     // ch1 != '~'
00177     switch (mHZState)
00178     {
00179       case HZ_STATE_GB:
00180         // the following chars are HZ
00181         *aDest = mUtil.GBKCharToUnicode(aSrc[0]|0x80, aSrc[1]|0x80);
00182         aSrc += 2;
00183         i++;
00184         iDestlen++;
00185         aDest++;
00186         mRunLength++;
00187         break;
00188       case HZ_STATE_ASCII:
00189       default:
00190         // default behavior also like an ASCII
00191         // when the source is an ASCII
00192         *aDest = CAST_CHAR_TO_UNICHAR(*aSrc);
00193         aSrc++;
00194         iDestlen++;
00195         aDest++;
00196         break;
00197     }
00198     *aSrcLength = i+1;
00199   }// for loop
00200   *aDestLength = iDestlen;
00201   return NS_OK;
00202 }
00203 
00204