DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Untracked file

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:expandtab:shiftwidth=2:tabstop=2: 
 */
/* ***** BEGIN LICENSE BLOCK *****
 * Version: NPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Netscape Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/NPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is Mozilla Communicator client code.
 *
 * The Initial Developer of the Original Code is 
 * Netscape Communications Corporation.
 * Portions created by the Initial Developer are Copyright (C) 1998
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s): Jungshik Shin <jshin@mailaps.org>
 *
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the NPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the NPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */

#include "nsUCSupport.h"
#include "nsUTF32ToUnicode.h"
#include <string.h>

//----------------------------------------------------------------------
// static functions and macro definition common to nsUTF32(BE|LE)ToUnicode

#ifdef IS_BIG_ENDIAN
#define LE_STRING_TO_UCS4(s)                                       \
        (PRUint8(*(s)) | (PRUint8(*((s) + 1)) << 8) |              \
         (PRUint8(*((s) + 2)) << 16) | (PRUint8(*((s) + 3)) << 24))
#else
#define LE_STRING_TO_UCS4(s) (*(PRUint32*) (s))
#endif

#ifdef IS_BIG_ENDIAN
#define BE_STRING_TO_UCS4(s) (*(PRUint32*) (s))
#else
#define BE_STRING_TO_UCS4(s)                                       \
        (PRUint8(*((s) + 3)) | (PRUint8(*((s) + 2)) << 8) |         \
         (PRUint8(*((s) + 1)) << 16) | (PRUint8(*(s)) << 24))
#endif
 
static nsresult ConvertCommon(const char * aSrc, 
                              PRInt32 * aSrcLength, 
                              PRUnichar * aDest, 
                              PRInt32 * aDestLength,
                              PRUint16 * aState,
                              PRUint8  * aBuffer,
                              PRBool aIsLE)
{
   
  NS_ENSURE_TRUE(*aState < 4, NS_ERROR_INVALID_ARG);
  NS_ENSURE_TRUE(*aDestLength > 0, NS_ERROR_INVALID_ARG);

  const char *src = aSrc;
  const char *srcEnd = aSrc + *aSrcLength;
   
  PRUnichar *dest = aDest;
  PRUnichar *destEnd = aDest + *aDestLength;

  if (*aState > *aSrcLength) 
  {
    memcpy(aBuffer + 4 - *aState, src, *aSrcLength);
    *aDestLength = 0;
    *aState -= *aSrcLength;
    return NS_OK_UDEC_MOREINPUT;
  }

  PRUint32 ucs4;

  // prev. run left a partial UTF-32 seq. 
  if (*aState > 0)
  {
    memcpy(aBuffer + 4 - *aState, src, *aState);
    ucs4 =  aIsLE ? LE_STRING_TO_UCS4(aBuffer) : BE_STRING_TO_UCS4(aBuffer); 
    if (ucs4 < 0x10000L)  // BMP
    {
      // XXX Do we have to convert surrogate code points to the replacement
      // character (0xfffd)?  
      *dest++= PRUnichar(ucs4);
    }
    else if (ucs4 < 0x110000L)  // plane 1 through plane 16 
    {
      if (destEnd - dest < 2) 
      {
        *aSrcLength = 0;
        *aDestLength = 0;
        return NS_OK_UDEC_MOREOUTPUT;
      }
      // ((ucs4 - 0x10000) >> 10) + 0xd800;
      *dest++= PRUnichar((ucs4 >> 10) + 0xd7c0);  // high surrogate
      *dest++= PRUnichar(ucs4 & 0x3ffL | 0xdc00); // low surrogate
    }       
    // Codepoints in plane 17 and higher (> 0x10ffff)
    // are not representable in UTF-16 we use for the internal
    // character representation. This is not a problem
    // because Unicode/ISO 10646 will never assign characters
    // in plane 17 and higher. Therefore, we convert them
    // to Unicode replacement character (0xfffd).
    else                   
      *dest++ = 0xfffd;
    src += *aState;
    *aState = 0;
  }

  nsresult rv = NS_OK;  // conversion result

  for ( ; src < srcEnd && dest < destEnd; src += 4)
  {
    if (srcEnd - src < 4) 
    {
      // fill up aBuffer until src buffer gets exhausted.
      memcpy(aBuffer, src, srcEnd - src);
      *aState = 4 - (srcEnd - src); // set add. char to read in next run
      src = srcEnd;
      rv = NS_OK_UDEC_MOREINPUT;
      break;
    }

    ucs4 =  aIsLE ? LE_STRING_TO_UCS4(src) : BE_STRING_TO_UCS4(src); 
    if (ucs4 < 0x10000L)  // BMP
    {
      // XXX Do we have to convert surrogate code points to the replacement
      // character (0xfffd)?  
      *dest++= PRUnichar(ucs4);
    }
    else if (ucs4 < 0x110000L)  // plane 1 through plane 16 
    {
      if (destEnd - dest < 2) 
        break;
      // ((ucs4 - 0x10000) >> 10) + 0xd800;
      *dest++= PRUnichar((ucs4 >> 10) + 0xd7c0); 
      *dest++= PRUnichar(ucs4 & 0x3ffL | 0xdc00);
    }       
    else                       // plane 17 and higher
      *dest++ = 0xfffd;
  }

  //output not finished, output buffer too short
  if((NS_OK == rv) && (src < srcEnd) && (dest >= destEnd)) 
    rv = NS_OK_UDEC_MOREOUTPUT;

  *aSrcLength = src - aSrc;
  *aDestLength  = dest - aDest;

  return rv;
}


//----------------------------------------------------------------------
// Class nsUTF32ToUnicode [implementation]

nsUTF32ToUnicode::nsUTF32ToUnicode() : nsBasicDecoderSupport()
{
  Reset();
}

//----------------------------------------------------------------------
// Subclassing of nsDecoderSupport class [implementation]

NS_IMETHODIMP nsUTF32ToUnicode::GetMaxLength(const char * aSrc, 
                                            PRInt32 aSrcLength, 
                                            PRInt32 * aDestLength)
{
  // Non-BMP characters take two PRUnichars(a pair of surrogate codepoints)
  // so that we have to divide by 2 instead of 4 for the worst case.
  *aDestLength = aSrcLength / 2;
  return NS_OK;
}


//----------------------------------------------------------------------
// Subclassing of nsBasicDecoderSupport class [implementation]

NS_IMETHODIMP nsUTF32ToUnicode::Reset()
{
  // the number of additional bytes to read to complete UTF-32 4byte seq.
  mState = 0;  
  memset(mBufferInc, 0, 4);
  return NS_OK;

}


//----------------------------------------------------------------------
// Class nsUTF32BEToUnicode [implementation]

//----------------------------------------------------------------------
// Subclassing of nsUTF32ToUnicode class [implementation]

NS_IMETHODIMP nsUTF32BEToUnicode::Convert(const char * aSrc, 
                                          PRInt32 * aSrcLength, 
                                          PRUnichar * aDest, 
                                          PRInt32 * aDestLength)
{
  return ConvertCommon(aSrc, aSrcLength, aDest, aDestLength, &mState, 
                       mBufferInc, PR_FALSE);
}

//----------------------------------------------------------------------
// Class nsUTF32LEToUnicode [implementation]
  
//----------------------------------------------------------------------
// Subclassing of nsUTF32ToUnicode class [implementation]

NS_IMETHODIMP nsUTF32LEToUnicode::Convert(const char * aSrc, 
                                          PRInt32 * aSrcLength, 
                                          PRUnichar * aDest, 
                                          PRInt32 * aDestLength)
{
  return ConvertCommon(aSrc, aSrcLength, aDest, aDestLength, &mState, 
                       mBufferInc, PR_TRUE);
}

// XXX : What to do with 'unflushed' mBufferInc?? : Finish()