Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/extensions/universalchardet/src/base/nsCodingStateMachine.h
Line
Count
Source (jump to first uncovered line)
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
/* Apart from these 3 generic states, machine states are specific to
13
 * each charset prober.
14
 */
15
0
#define eStart 0
16
#define eError 1
17
0
#define eItsMe 2
18
19
0
#define GETCLASS(c) GETFROMPCK(((unsigned char)(c)), mModel->classTable)
20
21
//state machine model
22
typedef struct
23
{
24
  nsPkgInt classTable;
25
  uint32_t classFactor;
26
  nsPkgInt stateTable;
27
  const uint32_t* charLenTable;
28
#ifdef DEBUG
29
  const size_t charLenTableLength;
30
#endif
31
  const char* name;
32
} SMModel;
33
34
class nsCodingStateMachine {
35
public:
36
0
  explicit nsCodingStateMachine(const SMModel* sm) : mModel(sm) { mCurrentState = eStart; }
37
0
  uint32_t NextState(char c){
38
0
    //for each byte we get its class , if it is first byte, we also get byte length
39
0
    uint32_t byteCls = GETCLASS(c);
40
0
    if (mCurrentState == eStart)
41
0
    {
42
0
      mCurrentBytePos = 0;
43
0
      MOZ_ASSERT(byteCls < mModel->charLenTableLength);
44
0
      mCurrentCharLen = mModel->charLenTable[byteCls];
45
0
    }
46
0
    //from byte's class and stateTable, we get its next state
47
0
    mCurrentState = GETFROMPCK(mCurrentState * mModel->classFactor + byteCls,
48
0
                               mModel->stateTable);
49
0
    mCurrentBytePos++;
50
0
    return mCurrentState;
51
0
  }
52
0
  uint32_t  GetCurrentCharLen(void) {return mCurrentCharLen;}
53
0
  void      Reset(void) {mCurrentState = eStart;}
54
0
  const char * GetCodingStateMachine() {return mModel->name;}
55
56
protected:
57
  uint32_t mCurrentState;
58
  uint32_t mCurrentCharLen;
59
  uint32_t mCurrentBytePos;
60
61
  const SMModel *mModel;
62
};
63
64
extern const SMModel UTF8SMModel;
65
extern const SMModel Big5SMModel;
66
extern const SMModel EUCJPSMModel;
67
extern const SMModel EUCKRSMModel;
68
extern const SMModel EUCTWSMModel;
69
extern const SMModel GB18030SMModel;
70
extern const SMModel SJISSMModel;
71
72
73
extern const SMModel HZSMModel;
74
extern const SMModel ISO2022CNSMModel;
75
extern const SMModel ISO2022JPSMModel;
76
extern const SMModel ISO2022KRSMModel;
77
78
#undef CHAR_LEN_TABLE
79
#ifdef DEBUG
80
#define CHAR_LEN_TABLE(x) x, mozilla::ArrayLength(x)
81
#else
82
#define CHAR_LEN_TABLE(x) x
83
#endif
84
85
#endif /* nsCodingStateMachine_h__ */
86