Back to index

lightning-sunbird  0.9+nobinonly
nsUTF7ToUnicode.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 "nsUTF7ToUnicode.h"
00039 
00040 #define ENC_DIRECT      0
00041 #define ENC_BASE64      1
00042 
00043 //----------------------------------------------------------------------
00044 // Class nsBasicUTF7Decoder [implementation]
00045 
00046 nsBasicUTF7Decoder::nsBasicUTF7Decoder(char aLastChar, char aEscChar) 
00047 : nsBufferDecoderSupport(1)
00048 {
00049   mLastChar = aLastChar;
00050   mEscChar = aEscChar;
00051   Reset();
00052 }
00053 
00054 nsresult nsBasicUTF7Decoder::DecodeDirect(
00055                              const char * aSrc, 
00056                              PRInt32 * aSrcLength, 
00057                              PRUnichar * aDest, 
00058                              PRInt32 * aDestLength)
00059 {
00060   const char * srcEnd = aSrc + *aSrcLength;
00061   const char * src = aSrc;
00062   PRUnichar * destEnd = aDest + *aDestLength;
00063   PRUnichar * dest = aDest;
00064   nsresult res = NS_OK;
00065   char ch;
00066 
00067   while (src < srcEnd) {
00068     ch = *src;
00069 
00070     // stop when we meet other chars or end of direct encoded seq.
00071     // if (!(DirectEncodable(ch)) || (ch == mEscChar)) {
00072     // but we are decoding; so we should be lax; pass everything until escchar
00073     if (ch == mEscChar) {
00074       res = NS_ERROR_UDEC_ILLEGALINPUT;
00075       break;
00076     }
00077 
00078     if (dest >= destEnd) {
00079       res = NS_OK_UDEC_MOREOUTPUT;
00080       break;
00081     } else {
00082       *dest++ = ch;
00083       src++;
00084     }
00085   }
00086 
00087   *aSrcLength = src - aSrc;
00088   *aDestLength = dest - aDest;
00089   return res;
00090 }
00091 
00092 nsresult nsBasicUTF7Decoder::DecodeBase64(
00093                              const char * aSrc, 
00094                              PRInt32 * aSrcLength, 
00095                              PRUnichar * aDest, 
00096                              PRInt32 * aDestLength)
00097 {
00098   const char * srcEnd = aSrc + *aSrcLength;
00099   const char * src = aSrc;
00100   PRUnichar * destEnd = aDest + *aDestLength;
00101   PRUnichar * dest = aDest;
00102   nsresult res = NS_OK;
00103   char ch;
00104   PRUint32 value;
00105 
00106   while (src < srcEnd) {
00107     ch = *src;
00108 
00109     // stop when we meet other chars or end of direct encoded seq.
00110     value = CharToValue(ch);
00111     if (value > 0xff) {
00112       res = NS_ERROR_UDEC_ILLEGALINPUT;
00113       break;
00114     }
00115 
00116     switch (mEncStep) {
00117       case 0:
00118         mEncBits = value << 10;
00119         break;
00120       case 1:
00121         mEncBits += value << 4;
00122         break;
00123       case 2:
00124         if (dest >= destEnd) {
00125           res = NS_OK_UDEC_MOREOUTPUT;
00126           break;
00127         }
00128         mEncBits += value >> 2;
00129         *(dest++) = (PRUnichar) mEncBits;
00130         mEncBits = (value & 0x03) << 14;
00131         break;
00132       case 3:
00133         mEncBits += value << 8;
00134         break;
00135       case 4:
00136         mEncBits += value << 2;
00137         break;
00138       case 5:
00139         if (dest >= destEnd) {
00140           res = NS_OK_UDEC_MOREOUTPUT;
00141           break;
00142         }
00143         mEncBits += value >> 4;
00144         *(dest++) = (PRUnichar) mEncBits;
00145         mEncBits = (value & 0x0f) << 12;
00146         break;
00147       case 6:
00148         mEncBits += value << 6;
00149         break;
00150       case 7:
00151         if (dest >= destEnd) {
00152           res = NS_OK_UDEC_MOREOUTPUT;
00153           break;
00154         }
00155         mEncBits += value;
00156         *(dest++) = (PRUnichar) mEncBits;
00157         mEncBits = 0;
00158         break;
00159     }
00160 
00161     if (res != NS_OK) break;
00162 
00163     src++;
00164     (++mEncStep)%=8;
00165   }
00166 
00167   *aSrcLength = src - aSrc;
00168   *aDestLength = dest - aDest;
00169   return res;
00170 }
00171 
00172 PRUint32 nsBasicUTF7Decoder::CharToValue(char aChar) {
00173   if ((aChar>='A')&&(aChar<='Z'))
00174     return (PRUint8)(aChar-'A');
00175   else if ((aChar>='a')&&(aChar<='z'))
00176     return (PRUint8)(26+aChar-'a');
00177   else if ((aChar>='0')&&(aChar<='9'))
00178     return (PRUint8)(26+26+aChar-'0');
00179   else if (aChar=='+')
00180     return (PRUint8)(26+26+10);
00181   else if (aChar==mLastChar)
00182     return (PRUint8)(26+26+10+1);
00183   else
00184     return 0xffff;
00185 }
00186 
00187 //----------------------------------------------------------------------
00188 // Subclassing of nsBufferDecoderSupport class [implementation]
00189 
00190 NS_IMETHODIMP nsBasicUTF7Decoder::ConvertNoBuff(const char * aSrc, 
00191                                                 PRInt32 * aSrcLength, 
00192                                                 PRUnichar * aDest, 
00193                                                 PRInt32 * aDestLength)
00194 {
00195   const char * srcEnd = aSrc + *aSrcLength;
00196   const char * src = aSrc;
00197   PRUnichar * destEnd = aDest + *aDestLength;
00198   PRUnichar * dest = aDest;
00199   PRInt32 bcr,bcw;
00200   nsresult res = NS_OK;
00201   char ch;
00202 
00203   while (src < srcEnd) {
00204     ch = *src;
00205 
00206     // fist, attept to decode in the current mode
00207     bcr = srcEnd - src;
00208     bcw = destEnd - dest;
00209     if (mEncoding == ENC_DIRECT) 
00210       res = DecodeDirect(src, &bcr, dest, &bcw);
00211     else if ((mFreshBase64) && (*src == '-')) {
00212       *dest = mEscChar;
00213       bcr = 0;
00214       bcw = 1;
00215       res = NS_ERROR_UDEC_ILLEGALINPUT;
00216     } else {
00217       mFreshBase64 = PR_FALSE;
00218       res = DecodeBase64(src, &bcr, dest, &bcw);
00219     }
00220     src += bcr;
00221     dest += bcw;
00222 
00223     // if an illegal char was encountered, test if it is an escape seq.
00224     if (res == NS_ERROR_UDEC_ILLEGALINPUT) {
00225       if (mEncoding == ENC_DIRECT) {
00226         if (*src == mEscChar) {
00227           mEncoding = ENC_BASE64;
00228           mFreshBase64 = PR_TRUE;
00229           mEncBits = 0;
00230           mEncStep = 0;
00231           src++;
00232           res = NS_OK;
00233         } else break;
00234       } else {
00235         mEncoding = ENC_DIRECT;
00236         res = NS_OK;
00237         // absorbe end of escape sequence
00238         if (*src == '-') src++;
00239       }
00240     } else if (res != NS_OK) break;
00241   }
00242 
00243   *aSrcLength = src - aSrc;
00244   *aDestLength = dest - aDest;
00245   return res;
00246 }
00247 
00248 NS_IMETHODIMP nsBasicUTF7Decoder::Reset()
00249 {
00250   mEncoding = ENC_DIRECT;
00251   mEncBits = 0;
00252   mEncStep = 0;
00253   return nsBufferDecoderSupport::Reset();
00254 }
00255 
00256 //----------------------------------------------------------------------
00257 // Class nsUTF7ToUnicode [implementation]
00258 
00259 nsUTF7ToUnicode::nsUTF7ToUnicode() 
00260 : nsBasicUTF7Decoder('/', '+')
00261 {
00262 }