1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/extensions/universalchardet/src/base/nsCodingStateMachine.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,85 @@ 1.4 +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 +#ifndef nsCodingStateMachine_h__ 1.9 +#define nsCodingStateMachine_h__ 1.10 + 1.11 +#include "mozilla/ArrayUtils.h" 1.12 + 1.13 +#include "nsPkgInt.h" 1.14 + 1.15 +typedef enum { 1.16 + eStart = 0, 1.17 + eError = 1, 1.18 + eItsMe = 2 1.19 +} nsSMState; 1.20 + 1.21 +#define GETCLASS(c) GETFROMPCK(((unsigned char)(c)), mModel->classTable) 1.22 + 1.23 +//state machine model 1.24 +typedef struct 1.25 +{ 1.26 + nsPkgInt classTable; 1.27 + uint32_t classFactor; 1.28 + nsPkgInt stateTable; 1.29 + const uint32_t* charLenTable; 1.30 +#ifdef DEBUG 1.31 + const size_t charLenTableLength; 1.32 +#endif 1.33 + const char* name; 1.34 +} SMModel; 1.35 + 1.36 +class nsCodingStateMachine { 1.37 +public: 1.38 + nsCodingStateMachine(const SMModel* sm) : mModel(sm) { mCurrentState = eStart; } 1.39 + nsSMState NextState(char c){ 1.40 + //for each byte we get its class , if it is first byte, we also get byte length 1.41 + uint32_t byteCls = GETCLASS(c); 1.42 + if (mCurrentState == eStart) 1.43 + { 1.44 + mCurrentBytePos = 0; 1.45 + MOZ_ASSERT(byteCls < mModel->charLenTableLength); 1.46 + mCurrentCharLen = mModel->charLenTable[byteCls]; 1.47 + } 1.48 + //from byte's class and stateTable, we get its next state 1.49 + mCurrentState=(nsSMState)GETFROMPCK(mCurrentState*(mModel->classFactor)+byteCls, 1.50 + mModel->stateTable); 1.51 + mCurrentBytePos++; 1.52 + return mCurrentState; 1.53 + } 1.54 + uint32_t GetCurrentCharLen(void) {return mCurrentCharLen;} 1.55 + void Reset(void) {mCurrentState = eStart;} 1.56 + const char * GetCodingStateMachine() {return mModel->name;} 1.57 + 1.58 +protected: 1.59 + nsSMState mCurrentState; 1.60 + uint32_t mCurrentCharLen; 1.61 + uint32_t mCurrentBytePos; 1.62 + 1.63 + const SMModel *mModel; 1.64 +}; 1.65 + 1.66 +extern const SMModel UTF8SMModel; 1.67 +extern const SMModel Big5SMModel; 1.68 +extern const SMModel EUCJPSMModel; 1.69 +extern const SMModel EUCKRSMModel; 1.70 +extern const SMModel EUCTWSMModel; 1.71 +extern const SMModel GB18030SMModel; 1.72 +extern const SMModel SJISSMModel; 1.73 + 1.74 + 1.75 +extern const SMModel HZSMModel; 1.76 +extern const SMModel ISO2022CNSMModel; 1.77 +extern const SMModel ISO2022JPSMModel; 1.78 +extern const SMModel ISO2022KRSMModel; 1.79 + 1.80 +#undef CHAR_LEN_TABLE 1.81 +#ifdef DEBUG 1.82 +#define CHAR_LEN_TABLE(x) x, mozilla::ArrayLength(x) 1.83 +#else 1.84 +#define CHAR_LEN_TABLE(x) x 1.85 +#endif 1.86 + 1.87 +#endif /* nsCodingStateMachine_h__ */ 1.88 +