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"
15namespace kencodingprober
16{
17enum {
18 eStart = 0,
19 eError = 1,
20 eItsMe = 2,
21};
22using nsSMState = int;
23
24#define GETCLASS(c) GETFROMPCK(((unsigned char)(c)), mModel->classTable)
25
26// state machine model
27typedef struct {
28 nsPkgInt classTable;
29 unsigned int classFactor;
30 nsPkgInt stateTable;
31 const unsigned int *charLenTable;
32 const char *name;
33} SMModel;
34
35class KCODECS_NO_EXPORT nsCodingStateMachine
36{
37public:
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
84protected:
85 int mCurrentState;
86 unsigned int mCurrentCharLen;
87 unsigned int mCurrentBytePos;
88
89 const SMModel *mModel;
90};
91
92extern KCODECS_NO_EXPORT const SMModel UTF8SMModel;
93extern KCODECS_NO_EXPORT const SMModel Big5SMModel;
94extern KCODECS_NO_EXPORT const SMModel EUCJPSMModel;
95extern KCODECS_NO_EXPORT const SMModel EUCKRSMModel;
96extern KCODECS_NO_EXPORT const SMModel GB18030SMModel;
97extern KCODECS_NO_EXPORT const SMModel SJISSMModel;
98extern KCODECS_NO_EXPORT const SMModel UCS2LESMModel;
99extern KCODECS_NO_EXPORT const SMModel UCS2BESMModel;
100
101extern KCODECS_NO_EXPORT const SMModel HZSMModel;
102extern KCODECS_NO_EXPORT const SMModel ISO2022CNSMModel;
103extern KCODECS_NO_EXPORT const SMModel ISO2022JPSMModel;
104extern KCODECS_NO_EXPORT const SMModel ISO2022KRSMModel;
105}
106#endif /* nsCodingStateMachine_h__ */
107

source code of kcodecs/src/probers/nsCodingStateMachine.h