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_p.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 : mModel(sm)
40 {
41 }
42 nsSMState NextState(char c)
43 {
44 // for each byte we get its class, if it is first byte, we also get byte length
45 unsigned int byteCls = GETCLASS(c);
46 if (mCurrentState == eStart) {
47 mCurrentCharLen = mModel->charLenTable[byteCls];
48 }
49 // from byte's class and stateTable, we get its next state
50 mCurrentState = GETFROMPCK(index: mCurrentState * (mModel->classFactor) + byteCls, table: mModel->stateTable);
51 return mCurrentState;
52 }
53 unsigned int GetCurrentCharLen(void)
54 {
55 return mCurrentCharLen;
56 }
57 void Reset(void)
58 {
59 mCurrentState = eStart;
60 }
61 const char *GetCodingStateMachine()
62 {
63 return mModel->name;
64 }
65#ifdef DEBUG_PROBE
66 const char *DumpCurrentState()
67 {
68 switch (mCurrentState) {
69 case eStart:
70 return "eStart";
71 case eError:
72 return "eError";
73 case eItsMe:
74 return "eItsMe";
75 default:
76 return "OK";
77 }
78 }
79#endif
80
81protected:
82 int mCurrentState = eStart;
83 unsigned int mCurrentCharLen = 0;
84
85 const SMModel *mModel = nullptr;
86};
87
88extern KCODECS_NO_EXPORT const SMModel UTF8SMModel;
89extern KCODECS_NO_EXPORT const SMModel Big5SMModel;
90extern KCODECS_NO_EXPORT const SMModel EUCJPSMModel;
91extern KCODECS_NO_EXPORT const SMModel EUCKRSMModel;
92extern KCODECS_NO_EXPORT const SMModel GB18030SMModel;
93extern KCODECS_NO_EXPORT const SMModel SJISSMModel;
94extern KCODECS_NO_EXPORT const SMModel UCS2LESMModel;
95extern KCODECS_NO_EXPORT const SMModel UCS2BESMModel;
96
97extern KCODECS_NO_EXPORT const SMModel HZSMModel;
98extern KCODECS_NO_EXPORT const SMModel ISO2022JPSMModel;
99}
100#endif /* nsCodingStateMachine_h__ */
101

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