|
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 #ifndef nsCodingStateMachine_h__ |
|
6 #define nsCodingStateMachine_h__ |
|
7 |
|
8 #include "mozilla/ArrayUtils.h" |
|
9 |
|
10 #include "nsPkgInt.h" |
|
11 |
|
12 typedef enum { |
|
13 eStart = 0, |
|
14 eError = 1, |
|
15 eItsMe = 2 |
|
16 } nsSMState; |
|
17 |
|
18 #define GETCLASS(c) GETFROMPCK(((unsigned char)(c)), mModel->classTable) |
|
19 |
|
20 //state machine model |
|
21 typedef struct |
|
22 { |
|
23 nsPkgInt classTable; |
|
24 uint32_t classFactor; |
|
25 nsPkgInt stateTable; |
|
26 const uint32_t* charLenTable; |
|
27 #ifdef DEBUG |
|
28 const size_t charLenTableLength; |
|
29 #endif |
|
30 const char* name; |
|
31 } SMModel; |
|
32 |
|
33 class nsCodingStateMachine { |
|
34 public: |
|
35 nsCodingStateMachine(const SMModel* sm) : mModel(sm) { mCurrentState = eStart; } |
|
36 nsSMState NextState(char c){ |
|
37 //for each byte we get its class , if it is first byte, we also get byte length |
|
38 uint32_t byteCls = GETCLASS(c); |
|
39 if (mCurrentState == eStart) |
|
40 { |
|
41 mCurrentBytePos = 0; |
|
42 MOZ_ASSERT(byteCls < mModel->charLenTableLength); |
|
43 mCurrentCharLen = mModel->charLenTable[byteCls]; |
|
44 } |
|
45 //from byte's class and stateTable, we get its next state |
|
46 mCurrentState=(nsSMState)GETFROMPCK(mCurrentState*(mModel->classFactor)+byteCls, |
|
47 mModel->stateTable); |
|
48 mCurrentBytePos++; |
|
49 return mCurrentState; |
|
50 } |
|
51 uint32_t GetCurrentCharLen(void) {return mCurrentCharLen;} |
|
52 void Reset(void) {mCurrentState = eStart;} |
|
53 const char * GetCodingStateMachine() {return mModel->name;} |
|
54 |
|
55 protected: |
|
56 nsSMState mCurrentState; |
|
57 uint32_t mCurrentCharLen; |
|
58 uint32_t mCurrentBytePos; |
|
59 |
|
60 const SMModel *mModel; |
|
61 }; |
|
62 |
|
63 extern const SMModel UTF8SMModel; |
|
64 extern const SMModel Big5SMModel; |
|
65 extern const SMModel EUCJPSMModel; |
|
66 extern const SMModel EUCKRSMModel; |
|
67 extern const SMModel EUCTWSMModel; |
|
68 extern const SMModel GB18030SMModel; |
|
69 extern const SMModel SJISSMModel; |
|
70 |
|
71 |
|
72 extern const SMModel HZSMModel; |
|
73 extern const SMModel ISO2022CNSMModel; |
|
74 extern const SMModel ISO2022JPSMModel; |
|
75 extern const SMModel ISO2022KRSMModel; |
|
76 |
|
77 #undef CHAR_LEN_TABLE |
|
78 #ifdef DEBUG |
|
79 #define CHAR_LEN_TABLE(x) x, mozilla::ArrayLength(x) |
|
80 #else |
|
81 #define CHAR_LEN_TABLE(x) x |
|
82 #endif |
|
83 |
|
84 #endif /* nsCodingStateMachine_h__ */ |
|
85 |