Blame src/nsEscCharsetProber.cpp

Packit 67f6e7
/* -*- Mode: C; tab-width: 4; 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.org 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
Packit 67f6e7
#include "nsEscCharsetProber.h"
Packit 67f6e7
Packit 67f6e7
nsEscCharSetProber::nsEscCharSetProber(void)
Packit 67f6e7
{
Packit 67f6e7
  mCodingSM[0] = new nsCodingStateMachine(&HZSMModel);
Packit 67f6e7
  mCodingSM[1] = new nsCodingStateMachine(&ISO2022CNSMModel);
Packit 67f6e7
  mCodingSM[2] = new nsCodingStateMachine(&ISO2022JPSMModel);
Packit 67f6e7
  mCodingSM[3] = new nsCodingStateMachine(&ISO2022KRSMModel);
Packit 67f6e7
  mActiveSM = NUM_OF_ESC_CHARSETS;
Packit 67f6e7
  mState = eDetecting;
Packit 67f6e7
  mDetectedCharset = nsnull;
Packit 67f6e7
}
Packit 67f6e7
Packit 67f6e7
nsEscCharSetProber::~nsEscCharSetProber(void)
Packit 67f6e7
{
Packit 67f6e7
  for (PRUint32 i = 0; i < NUM_OF_ESC_CHARSETS; i++)
Packit 67f6e7
    delete mCodingSM[i];
Packit 67f6e7
}
Packit 67f6e7
Packit 67f6e7
void nsEscCharSetProber::Reset(void)
Packit 67f6e7
{
Packit 67f6e7
  mState = eDetecting;
Packit 67f6e7
  for (PRUint32 i = 0; i < NUM_OF_ESC_CHARSETS; i++)
Packit 67f6e7
    mCodingSM[i]->Reset();
Packit 67f6e7
  mActiveSM = NUM_OF_ESC_CHARSETS;
Packit 67f6e7
  mDetectedCharset = nsnull;
Packit 67f6e7
}
Packit 67f6e7
Packit 67f6e7
nsProbingState nsEscCharSetProber::HandleData(const char* aBuf, PRUint32 aLen)
Packit 67f6e7
{
Packit 67f6e7
  nsSMState codingState;
Packit 67f6e7
  PRInt32 j;
Packit 67f6e7
  PRUint32 i;
Packit 67f6e7
Packit 67f6e7
  for ( i = 0; i < aLen && mState == eDetecting; i++)
Packit 67f6e7
  {
Packit 67f6e7
    for (j = mActiveSM-1; j>= 0; j--)
Packit 67f6e7
    {
Packit 67f6e7
      //byte is feed to all active state machine 
Packit 67f6e7
      codingState = mCodingSM[j]->NextState(aBuf[i]);
Packit 67f6e7
      if (codingState == eError)
Packit 67f6e7
      {
Packit 67f6e7
        //got negative answer for this state machine, make it inactive
Packit 67f6e7
        mActiveSM--;
Packit 67f6e7
        if (mActiveSM == 0)
Packit 67f6e7
        {
Packit 67f6e7
          mState = eNotMe;
Packit 67f6e7
          return mState;
Packit 67f6e7
        }
Packit 67f6e7
        else if (j != (PRInt32)mActiveSM)
Packit 67f6e7
        {
Packit 67f6e7
          nsCodingStateMachine* t;
Packit 67f6e7
          t = mCodingSM[mActiveSM];
Packit 67f6e7
          mCodingSM[mActiveSM] = mCodingSM[j];
Packit 67f6e7
          mCodingSM[j] = t;
Packit 67f6e7
        }
Packit 67f6e7
      }
Packit 67f6e7
      else if (codingState == eItsMe)
Packit 67f6e7
      {
Packit 67f6e7
        mState = eFoundIt;
Packit 67f6e7
        mDetectedCharset = mCodingSM[j]->GetCodingStateMachine();
Packit 67f6e7
        return mState;
Packit 67f6e7
      }
Packit 67f6e7
    }
Packit 67f6e7
  }
Packit 67f6e7
Packit 67f6e7
  return mState;
Packit 67f6e7
}
Packit 67f6e7