Blame src/CharDistribution.h

Packit 67f6e7
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
Packit 67f6e7
/* ***** BEGIN LICENSE BLOCK *****
Packit 67f6e7
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
Packit 67f6e7
 *
Packit 67f6e7
 * The contents of this file are subject to the Mozilla Public License Version
Packit 67f6e7
 * 1.1 (the "License"); you may not use this file except in compliance with
Packit 67f6e7
 * the License. You may obtain a copy of the License at
Packit 67f6e7
 * http://www.mozilla.org/MPL/
Packit 67f6e7
 *
Packit 67f6e7
 * Software distributed under the License is distributed on an "AS IS" basis,
Packit 67f6e7
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
Packit 67f6e7
 * for the specific language governing rights and limitations under the
Packit 67f6e7
 * License.
Packit 67f6e7
 *
Packit 67f6e7
 * The Original Code is Mozilla Communicator client code.
Packit 67f6e7
 *
Packit 67f6e7
 * The Initial Developer of the Original Code is
Packit 67f6e7
 * Netscape Communications Corporation.
Packit 67f6e7
 * Portions created by the Initial Developer are Copyright (C) 1998
Packit 67f6e7
 * the Initial Developer. All Rights Reserved.
Packit 67f6e7
 *
Packit 67f6e7
 * Contributor(s):
Packit 67f6e7
 *
Packit 67f6e7
 * Alternatively, the contents of this file may be used under the terms of
Packit 67f6e7
 * either the GNU General Public License Version 2 or later (the "GPL"), or
Packit 67f6e7
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
Packit 67f6e7
 * in which case the provisions of the GPL or the LGPL are applicable instead
Packit 67f6e7
 * of those above. If you wish to allow use of your version of this file only
Packit 67f6e7
 * under the terms of either the GPL or the LGPL, and not to allow others to
Packit 67f6e7
 * use your version of this file under the terms of the MPL, indicate your
Packit 67f6e7
 * decision by deleting the provisions above and replace them with the notice
Packit 67f6e7
 * and other provisions required by the GPL or the LGPL. If you do not delete
Packit 67f6e7
 * the provisions above, a recipient may use your version of this file under
Packit 67f6e7
 * the terms of any one of the MPL, the GPL or the LGPL.
Packit 67f6e7
 *
Packit 67f6e7
 * ***** END LICENSE BLOCK ***** */
Packit 67f6e7
Packit 67f6e7
#ifndef CharDistribution_h__
Packit 67f6e7
#define CharDistribution_h__
Packit 67f6e7
Packit 67f6e7
#include "nscore.h"
Packit 67f6e7
Packit 67f6e7
#define ENOUGH_DATA_THRESHOLD 1024
Packit 67f6e7
 
Packit 67f6e7
class CharDistributionAnalysis
Packit 67f6e7
{
Packit 67f6e7
public:
Packit 67f6e7
  CharDistributionAnalysis() {Reset();};
Packit 67f6e7
Packit 67f6e7
  //feed a block of data and do distribution analysis
Packit 67f6e7
  void HandleData(const char* aBuf, PRUint32 aLen) {};
Packit 67f6e7
  
Packit 67f6e7
  //Feed a character with known length
Packit 67f6e7
  void HandleOneChar(const char* aStr, PRUint32 aCharLen)
Packit 67f6e7
  {
Packit 67f6e7
    PRInt32 order;
Packit 67f6e7
Packit 67f6e7
    //we only care about 2-bytes character in our distribution analysis
Packit 67f6e7
    order = (aCharLen == 2) ? GetOrder(aStr) : -1;
Packit 67f6e7
Packit 67f6e7
    if (order >= 0)
Packit 67f6e7
    {
Packit 67f6e7
      mTotalChars++;
Packit 67f6e7
      //order is valid
Packit 67f6e7
      if ((PRUint32)order < mTableSize)
Packit 67f6e7
      {
Packit 67f6e7
        if (512 > mCharToFreqOrder[order])
Packit 67f6e7
          mFreqChars++;
Packit 67f6e7
      }
Packit 67f6e7
    }
Packit 67f6e7
  };
Packit 67f6e7
Packit 67f6e7
  //return confidence base on existing data
Packit 67f6e7
  float GetConfidence();
Packit 67f6e7
Packit 67f6e7
  //Reset analyser, clear any state 
Packit 67f6e7
  void      Reset(void) 
Packit 67f6e7
  {
Packit 67f6e7
    mDone = PR_FALSE;
Packit 67f6e7
    mTotalChars = 0;
Packit 67f6e7
    mFreqChars = 0;
Packit 67f6e7
  };
Packit 67f6e7
Packit 67f6e7
  //This function is for future extension. Caller can use this function to control
Packit 67f6e7
  //analyser's behavior
Packit 67f6e7
  void      SetOpion(){};
Packit 67f6e7
Packit 67f6e7
  //It is not necessary to receive all data to draw conclusion. For charset detection,
Packit 67f6e7
  // certain amount of data is enough
Packit 67f6e7
  PRBool GotEnoughData() {return mTotalChars > ENOUGH_DATA_THRESHOLD;};
Packit 67f6e7
Packit 67f6e7
protected:
Packit 67f6e7
  //we do not handle character base on its original encoding string, but 
Packit 67f6e7
  //convert this encoding string to a number, here called order.
Packit 67f6e7
  //This allow multiple encoding of a language to share one frequency table 
Packit 67f6e7
  virtual PRInt32 GetOrder(const char* str) {return -1;};
Packit 67f6e7
  
Packit 67f6e7
  //If this flag is set to PR_TRUE, detection is done and conclusion has been made
Packit 67f6e7
  PRBool   mDone;
Packit 67f6e7
Packit 67f6e7
  //The number of characters whose frequency order is less than 512
Packit 67f6e7
  PRUint32 mFreqChars;
Packit 67f6e7
Packit 67f6e7
  //Total character encounted.
Packit 67f6e7
  PRUint32 mTotalChars;
Packit 67f6e7
Packit 67f6e7
  //Mapping table to get frequency order from char order (get from GetOrder())
Packit 67f6e7
  const PRInt16  *mCharToFreqOrder;
Packit 67f6e7
Packit 67f6e7
  //Size of above table
Packit 67f6e7
  PRUint32 mTableSize;
Packit 67f6e7
Packit 67f6e7
  //This is a constant value varies from language to language, it is used in 
Packit 67f6e7
  //calculating confidence. See my paper for further detail.
Packit 67f6e7
  float    mTypicalDistributionRatio;
Packit 67f6e7
};
Packit 67f6e7
Packit 67f6e7
Packit 67f6e7
class EUCTWDistributionAnalysis: public CharDistributionAnalysis
Packit 67f6e7
{
Packit 67f6e7
public:
Packit 67f6e7
  EUCTWDistributionAnalysis();
Packit 67f6e7
protected:
Packit 67f6e7
Packit 67f6e7
  //for euc-TW encoding, we are interested 
Packit 67f6e7
  //  first  byte range: 0xc4 -- 0xfe
Packit 67f6e7
  //  second byte range: 0xa1 -- 0xfe
Packit 67f6e7
  //no validation needed here. State machine has done that
Packit 67f6e7
  PRInt32 GetOrder(const char* str) 
Packit 67f6e7
  { if ((unsigned char)*str >= (unsigned char)0xc4)  
Packit 67f6e7
      return 94*((unsigned char)str[0]-(unsigned char)0xc4) + (unsigned char)str[1] - (unsigned char)0xa1;
Packit 67f6e7
    else
Packit 67f6e7
      return -1;
Packit 67f6e7
  };
Packit 67f6e7
};
Packit 67f6e7
Packit 67f6e7
Packit 67f6e7
class EUCKRDistributionAnalysis : public CharDistributionAnalysis
Packit 67f6e7
{
Packit 67f6e7
public:
Packit 67f6e7
  EUCKRDistributionAnalysis();
Packit 67f6e7
protected:
Packit 67f6e7
  //for euc-KR encoding, we are interested 
Packit 67f6e7
  //  first  byte range: 0xb0 -- 0xfe
Packit 67f6e7
  //  second byte range: 0xa1 -- 0xfe
Packit 67f6e7
  //no validation needed here. State machine has done that
Packit 67f6e7
  PRInt32 GetOrder(const char* str) 
Packit 67f6e7
  { if ((unsigned char)*str >= (unsigned char)0xb0)  
Packit 67f6e7
      return 94*((unsigned char)str[0]-(unsigned char)0xb0) + (unsigned char)str[1] - (unsigned char)0xa1;
Packit 67f6e7
    else
Packit 67f6e7
      return -1;
Packit 67f6e7
  };
Packit 67f6e7
};
Packit 67f6e7
Packit 67f6e7
class GB2312DistributionAnalysis : public CharDistributionAnalysis
Packit 67f6e7
{
Packit 67f6e7
public:
Packit 67f6e7
  GB2312DistributionAnalysis();
Packit 67f6e7
protected:
Packit 67f6e7
  //for GB2312 encoding, we are interested 
Packit 67f6e7
  //  first  byte range: 0xb0 -- 0xfe
Packit 67f6e7
  //  second byte range: 0xa1 -- 0xfe
Packit 67f6e7
  //no validation needed here. State machine has done that
Packit 67f6e7
  PRInt32 GetOrder(const char* str) 
Packit 67f6e7
  { if ((unsigned char)*str >= (unsigned char)0xb0 && (unsigned char)str[1] >= (unsigned char)0xa1)  
Packit 67f6e7
      return 94*((unsigned char)str[0]-(unsigned char)0xb0) + (unsigned char)str[1] - (unsigned char)0xa1;
Packit 67f6e7
    else
Packit 67f6e7
      return -1;
Packit 67f6e7
  };
Packit 67f6e7
};
Packit 67f6e7
Packit 67f6e7
Packit 67f6e7
class Big5DistributionAnalysis : public CharDistributionAnalysis
Packit 67f6e7
{
Packit 67f6e7
public:
Packit 67f6e7
  Big5DistributionAnalysis();
Packit 67f6e7
protected:
Packit 67f6e7
  //for big5 encoding, we are interested 
Packit 67f6e7
  //  first  byte range: 0xa4 -- 0xfe
Packit 67f6e7
  //  second byte range: 0x40 -- 0x7e , 0xa1 -- 0xfe
Packit 67f6e7
  //no validation needed here. State machine has done that
Packit 67f6e7
  PRInt32 GetOrder(const char* str) 
Packit 67f6e7
  { if ((unsigned char)*str >= (unsigned char)0xa4)  
Packit 67f6e7
      if ((unsigned char)str[1] >= (unsigned char)0xa1)
Packit 67f6e7
        return 157*((unsigned char)str[0]-(unsigned char)0xa4) + (unsigned char)str[1] - (unsigned char)0xa1 +63;
Packit 67f6e7
      else
Packit 67f6e7
        return 157*((unsigned char)str[0]-(unsigned char)0xa4) + (unsigned char)str[1] - (unsigned char)0x40;
Packit 67f6e7
    else
Packit 67f6e7
      return -1;
Packit 67f6e7
  };
Packit 67f6e7
};
Packit 67f6e7
Packit 67f6e7
class SJISDistributionAnalysis : public CharDistributionAnalysis
Packit 67f6e7
{
Packit 67f6e7
public:
Packit 67f6e7
  SJISDistributionAnalysis();
Packit 67f6e7
protected:
Packit 67f6e7
  //for sjis encoding, we are interested 
Packit 67f6e7
  //  first  byte range: 0x81 -- 0x9f , 0xe0 -- 0xfe
Packit 67f6e7
  //  second byte range: 0x40 -- 0x7e,  0x81 -- oxfe
Packit 67f6e7
  //no validation needed here. State machine has done that
Packit 67f6e7
  PRInt32 GetOrder(const char* str) 
Packit 67f6e7
  { 
Packit 67f6e7
    PRInt32 order;
Packit 67f6e7
    if ((unsigned char)*str >= (unsigned char)0x81 && (unsigned char)*str <= (unsigned char)0x9f)  
Packit 67f6e7
      order = 188 * ((unsigned char)str[0]-(unsigned char)0x81);
Packit 67f6e7
    else if ((unsigned char)*str >= (unsigned char)0xe0 && (unsigned char)*str <= (unsigned char)0xef)  
Packit 67f6e7
      order = 188 * ((unsigned char)str[0]-(unsigned char)0xe0 + 31);
Packit 67f6e7
    else
Packit 67f6e7
      return -1;
Packit 67f6e7
    order += (unsigned char)*(str+1) - 0x40;
Packit 67f6e7
    if ((unsigned char)str[1] > (unsigned char)0x7f)
Packit 67f6e7
      order--;
Packit 67f6e7
    return order;
Packit 67f6e7
  };
Packit 67f6e7
};
Packit 67f6e7
Packit 67f6e7
class EUCJPDistributionAnalysis : public CharDistributionAnalysis
Packit 67f6e7
{
Packit 67f6e7
public:
Packit 67f6e7
  EUCJPDistributionAnalysis();
Packit 67f6e7
protected:
Packit 67f6e7
  //for euc-JP encoding, we are interested 
Packit 67f6e7
  //  first  byte range: 0xa0 -- 0xfe
Packit 67f6e7
  //  second byte range: 0xa1 -- 0xfe
Packit 67f6e7
  //no validation needed here. State machine has done that
Packit 67f6e7
  PRInt32 GetOrder(const char* str) 
Packit 67f6e7
  { if ((unsigned char)*str >= (unsigned char)0xa0)  
Packit 67f6e7
      return 94*((unsigned char)str[0]-(unsigned char)0xa1) + (unsigned char)str[1] - (unsigned char)0xa1;
Packit 67f6e7
    else
Packit 67f6e7
      return -1;
Packit 67f6e7
  };
Packit 67f6e7
};
Packit 67f6e7
Packit 67f6e7
#endif //CharDistribution_h__
Packit 67f6e7