1 | /* -*- C++ -*- |
2 | SPDX-FileCopyrightText: 1998 Netscape Communications Corporation <developer@mozilla.org> |
3 | |
4 | SPDX-License-Identifier: MIT |
5 | */ |
6 | |
7 | #ifndef nsCodingStateMachine_h__ |
8 | #define nsCodingStateMachine_h__ |
9 | |
10 | #include "kencodingprober.h" |
11 | |
12 | #include "kcodecs_export.h" |
13 | |
14 | #include "nsPkgInt.h" |
15 | namespace kencodingprober |
16 | { |
17 | enum { |
18 | eStart = 0, |
19 | eError = 1, |
20 | eItsMe = 2, |
21 | }; |
22 | using nsSMState = int; |
23 | |
24 | #define GETCLASS(c) GETFROMPCK(((unsigned char)(c)), mModel->classTable) |
25 | |
26 | // state machine model |
27 | typedef struct { |
28 | nsPkgInt classTable; |
29 | unsigned int classFactor; |
30 | nsPkgInt stateTable; |
31 | const unsigned int *charLenTable; |
32 | const char *name; |
33 | } SMModel; |
34 | |
35 | class KCODECS_NO_EXPORT nsCodingStateMachine |
36 | { |
37 | public: |
38 | nsCodingStateMachine(const SMModel *sm) |
39 | { |
40 | mCurrentState = eStart; |
41 | mModel = sm; |
42 | } |
43 | nsSMState NextState(char c) |
44 | { |
45 | // for each byte we get its class KCODECS_NO_EXPORT , if it is first byte, we also get byte length |
46 | unsigned int byteCls = GETCLASS(c); |
47 | if (mCurrentState == eStart) { |
48 | mCurrentBytePos = 0; |
49 | mCurrentCharLen = mModel->charLenTable[byteCls]; |
50 | } |
51 | // from byte's class KCODECS_NO_EXPORT and stateTable, we get its next state |
52 | mCurrentState = GETFROMPCK(mCurrentState * (mModel->classFactor) + byteCls, mModel->stateTable); |
53 | mCurrentBytePos++; |
54 | return mCurrentState; |
55 | } |
56 | unsigned int GetCurrentCharLen(void) |
57 | { |
58 | return mCurrentCharLen; |
59 | } |
60 | void Reset(void) |
61 | { |
62 | mCurrentState = eStart; |
63 | } |
64 | const char *GetCodingStateMachine() |
65 | { |
66 | return mModel->name; |
67 | } |
68 | #ifdef DEBUG_PROBE |
69 | const char *DumpCurrentState() |
70 | { |
71 | switch (mCurrentState) { |
72 | case eStart: |
73 | return "eStart" ; |
74 | case eError: |
75 | return "eError" ; |
76 | case eItsMe: |
77 | return "eItsMe" ; |
78 | default: |
79 | return "OK" ; |
80 | } |
81 | } |
82 | #endif |
83 | |
84 | protected: |
85 | int mCurrentState; |
86 | unsigned int mCurrentCharLen; |
87 | unsigned int mCurrentBytePos; |
88 | |
89 | const SMModel *mModel; |
90 | }; |
91 | |
92 | extern KCODECS_NO_EXPORT const SMModel UTF8SMModel; |
93 | extern KCODECS_NO_EXPORT const SMModel Big5SMModel; |
94 | extern KCODECS_NO_EXPORT const SMModel EUCJPSMModel; |
95 | extern KCODECS_NO_EXPORT const SMModel EUCKRSMModel; |
96 | extern KCODECS_NO_EXPORT const SMModel GB18030SMModel; |
97 | extern KCODECS_NO_EXPORT const SMModel SJISSMModel; |
98 | extern KCODECS_NO_EXPORT const SMModel UCS2LESMModel; |
99 | extern KCODECS_NO_EXPORT const SMModel UCS2BESMModel; |
100 | |
101 | extern KCODECS_NO_EXPORT const SMModel HZSMModel; |
102 | extern KCODECS_NO_EXPORT const SMModel ISO2022CNSMModel; |
103 | extern KCODECS_NO_EXPORT const SMModel ISO2022JPSMModel; |
104 | extern KCODECS_NO_EXPORT const SMModel ISO2022KRSMModel; |
105 | } |
106 | #endif /* nsCodingStateMachine_h__ */ |
107 | |