File indexing completed on 2024-04-28 03:53:03
0001 /* -*- C++ -*- 0002 SPDX-FileCopyrightText: 1998 Netscape Communications Corporation <developer@mozilla.org> 0003 0004 SPDX-License-Identifier: MIT 0005 */ 0006 0007 #include "nsEscCharsetProber.h" 0008 0009 namespace kencodingprober 0010 { 0011 nsEscCharSetProber::nsEscCharSetProber(void) 0012 { 0013 mCodingSM[0] = new nsCodingStateMachine(&HZSMModel); 0014 mCodingSM[1] = new nsCodingStateMachine(&ISO2022CNSMModel); 0015 mCodingSM[2] = new nsCodingStateMachine(&ISO2022JPSMModel); 0016 mCodingSM[3] = new nsCodingStateMachine(&ISO2022KRSMModel); 0017 mActiveSM = NUM_OF_ESC_CHARSETS; 0018 mState = eDetecting; 0019 mDetectedCharset = nullptr; 0020 } 0021 0022 nsEscCharSetProber::~nsEscCharSetProber(void) 0023 { 0024 for (unsigned int i = 0; i < NUM_OF_ESC_CHARSETS; i++) { 0025 delete mCodingSM[i]; 0026 } 0027 } 0028 0029 void nsEscCharSetProber::Reset(void) 0030 { 0031 mState = eDetecting; 0032 for (unsigned int i = 0; i < NUM_OF_ESC_CHARSETS; i++) { 0033 mCodingSM[i]->Reset(); 0034 } 0035 mActiveSM = NUM_OF_ESC_CHARSETS; 0036 mDetectedCharset = nullptr; 0037 } 0038 0039 nsProbingState nsEscCharSetProber::HandleData(const char *aBuf, unsigned int aLen) 0040 { 0041 nsSMState codingState; 0042 int j; 0043 unsigned int i; 0044 0045 for (i = 0; i < aLen && mState == eDetecting; i++) { 0046 for (j = mActiveSM - 1; j >= 0; j--) { 0047 // byte is feed to all active state machine 0048 codingState = mCodingSM[j]->NextState(aBuf[i]); 0049 if (codingState == eError) { 0050 // got negative answer for this state machine, make it inactive 0051 mActiveSM--; 0052 if (mActiveSM == 0) { 0053 mState = eNotMe; 0054 return mState; 0055 } else if (j != (int)mActiveSM) { 0056 nsCodingStateMachine *t; 0057 t = mCodingSM[mActiveSM]; 0058 mCodingSM[mActiveSM] = mCodingSM[j]; 0059 mCodingSM[j] = t; 0060 } 0061 } else if (codingState == eItsMe) { 0062 mState = eFoundIt; 0063 mDetectedCharset = mCodingSM[j]->GetCodingStateMachine(); 0064 return mState; 0065 } 0066 } 0067 } 0068 0069 return mState; 0070 } 0071 }