michael@0: /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: #ifndef nsCodingStateMachine_h__ michael@0: #define nsCodingStateMachine_h__ michael@0: michael@0: #include "mozilla/ArrayUtils.h" michael@0: michael@0: #include "nsPkgInt.h" michael@0: michael@0: typedef enum { michael@0: eStart = 0, michael@0: eError = 1, michael@0: eItsMe = 2 michael@0: } nsSMState; michael@0: michael@0: #define GETCLASS(c) GETFROMPCK(((unsigned char)(c)), mModel->classTable) michael@0: michael@0: //state machine model michael@0: typedef struct michael@0: { michael@0: nsPkgInt classTable; michael@0: uint32_t classFactor; michael@0: nsPkgInt stateTable; michael@0: const uint32_t* charLenTable; michael@0: #ifdef DEBUG michael@0: const size_t charLenTableLength; michael@0: #endif michael@0: const char* name; michael@0: } SMModel; michael@0: michael@0: class nsCodingStateMachine { michael@0: public: michael@0: nsCodingStateMachine(const SMModel* sm) : mModel(sm) { mCurrentState = eStart; } michael@0: nsSMState NextState(char c){ michael@0: //for each byte we get its class , if it is first byte, we also get byte length michael@0: uint32_t byteCls = GETCLASS(c); michael@0: if (mCurrentState == eStart) michael@0: { michael@0: mCurrentBytePos = 0; michael@0: MOZ_ASSERT(byteCls < mModel->charLenTableLength); michael@0: mCurrentCharLen = mModel->charLenTable[byteCls]; michael@0: } michael@0: //from byte's class and stateTable, we get its next state michael@0: mCurrentState=(nsSMState)GETFROMPCK(mCurrentState*(mModel->classFactor)+byteCls, michael@0: mModel->stateTable); michael@0: mCurrentBytePos++; michael@0: return mCurrentState; michael@0: } michael@0: uint32_t GetCurrentCharLen(void) {return mCurrentCharLen;} michael@0: void Reset(void) {mCurrentState = eStart;} michael@0: const char * GetCodingStateMachine() {return mModel->name;} michael@0: michael@0: protected: michael@0: nsSMState mCurrentState; michael@0: uint32_t mCurrentCharLen; michael@0: uint32_t mCurrentBytePos; michael@0: michael@0: const SMModel *mModel; michael@0: }; michael@0: michael@0: extern const SMModel UTF8SMModel; michael@0: extern const SMModel Big5SMModel; michael@0: extern const SMModel EUCJPSMModel; michael@0: extern const SMModel EUCKRSMModel; michael@0: extern const SMModel EUCTWSMModel; michael@0: extern const SMModel GB18030SMModel; michael@0: extern const SMModel SJISSMModel; michael@0: michael@0: michael@0: extern const SMModel HZSMModel; michael@0: extern const SMModel ISO2022CNSMModel; michael@0: extern const SMModel ISO2022JPSMModel; michael@0: extern const SMModel ISO2022KRSMModel; michael@0: michael@0: #undef CHAR_LEN_TABLE michael@0: #ifdef DEBUG michael@0: #define CHAR_LEN_TABLE(x) x, mozilla::ArrayLength(x) michael@0: #else michael@0: #define CHAR_LEN_TABLE(x) x michael@0: #endif michael@0: michael@0: #endif /* nsCodingStateMachine_h__ */ michael@0: