File indexing completed on 2024-04-28 03:53:03

0001 /*  -*- C++ -*-
0002     SPDX-FileCopyrightText: 1998 Netscape Communications Corporation <developer@mozilla.org>
0003 
0004     SPDX-License-Identifier: MIT
0005 */
0006 
0007 #ifndef nsCodingStateMachine_h__
0008 #define nsCodingStateMachine_h__
0009 
0010 #include "kencodingprober.h"
0011 
0012 #include "kcodecs_export.h"
0013 
0014 #include "nsPkgInt.h"
0015 namespace kencodingprober
0016 {
0017 enum {
0018     eStart = 0,
0019     eError = 1,
0020     eItsMe = 2,
0021 };
0022 using nsSMState = int;
0023 
0024 #define GETCLASS(c) GETFROMPCK(((unsigned char)(c)), mModel->classTable)
0025 
0026 // state machine model
0027 typedef struct {
0028     nsPkgInt classTable;
0029     unsigned int classFactor;
0030     nsPkgInt stateTable;
0031     const unsigned int *charLenTable;
0032     const char *name;
0033 } SMModel;
0034 
0035 class KCODECS_NO_EXPORT nsCodingStateMachine
0036 {
0037 public:
0038     nsCodingStateMachine(const SMModel *sm)
0039     {
0040         mCurrentState = eStart;
0041         mModel = sm;
0042     }
0043     nsSMState NextState(char c)
0044     {
0045         // for each byte we get its class KCODECS_NO_EXPORT , if it is first byte, we also get byte length
0046         unsigned int byteCls = GETCLASS(c);
0047         if (mCurrentState == eStart) {
0048             mCurrentBytePos = 0;
0049             mCurrentCharLen = mModel->charLenTable[byteCls];
0050         }
0051         // from byte's class KCODECS_NO_EXPORT and stateTable, we get its next state
0052         mCurrentState = GETFROMPCK(mCurrentState * (mModel->classFactor) + byteCls, mModel->stateTable);
0053         mCurrentBytePos++;
0054         return mCurrentState;
0055     }
0056     unsigned int GetCurrentCharLen(void)
0057     {
0058         return mCurrentCharLen;
0059     }
0060     void Reset(void)
0061     {
0062         mCurrentState = eStart;
0063     }
0064     const char *GetCodingStateMachine()
0065     {
0066         return mModel->name;
0067     }
0068 #ifdef DEBUG_PROBE
0069     const char *DumpCurrentState()
0070     {
0071         switch (mCurrentState) {
0072         case eStart:
0073             return "eStart";
0074         case eError:
0075             return "eError";
0076         case eItsMe:
0077             return "eItsMe";
0078         default:
0079             return "OK";
0080         }
0081     }
0082 #endif
0083 
0084 protected:
0085     int mCurrentState;
0086     unsigned int mCurrentCharLen;
0087     unsigned int mCurrentBytePos;
0088 
0089     const SMModel *mModel;
0090 };
0091 
0092 extern KCODECS_NO_EXPORT const SMModel UTF8SMModel;
0093 extern KCODECS_NO_EXPORT const SMModel Big5SMModel;
0094 extern KCODECS_NO_EXPORT const SMModel EUCJPSMModel;
0095 extern KCODECS_NO_EXPORT const SMModel EUCKRSMModel;
0096 extern KCODECS_NO_EXPORT const SMModel GB18030SMModel;
0097 extern KCODECS_NO_EXPORT const SMModel SJISSMModel;
0098 extern KCODECS_NO_EXPORT const SMModel UCS2LESMModel;
0099 extern KCODECS_NO_EXPORT const SMModel UCS2BESMModel;
0100 
0101 extern KCODECS_NO_EXPORT const SMModel HZSMModel;
0102 extern KCODECS_NO_EXPORT const SMModel ISO2022CNSMModel;
0103 extern KCODECS_NO_EXPORT const SMModel ISO2022JPSMModel;
0104 extern KCODECS_NO_EXPORT const SMModel ISO2022KRSMModel;
0105 }
0106 #endif /* nsCodingStateMachine_h__ */