1 | /* -*- C++ -*- |
---|---|
2 | SPDX-FileCopyrightText: 1998 Netscape Communications Corporation <developer@mozilla.org> |
3 | |
4 | SPDX-License-Identifier: MIT |
5 | */ |
6 | |
7 | #include "nsEscCharsetProber.h" |
8 | |
9 | namespace kencodingprober |
10 | { |
11 | nsEscCharSetProber::nsEscCharSetProber(void) |
12 | { |
13 | mCodingSM[0] = new nsCodingStateMachine(&HZSMModel); |
14 | mCodingSM[1] = new nsCodingStateMachine(&ISO2022CNSMModel); |
15 | mCodingSM[2] = new nsCodingStateMachine(&ISO2022JPSMModel); |
16 | mCodingSM[3] = new nsCodingStateMachine(&ISO2022KRSMModel); |
17 | mActiveSM = NUM_OF_ESC_CHARSETS; |
18 | mState = eDetecting; |
19 | mDetectedCharset = nullptr; |
20 | } |
21 | |
22 | nsEscCharSetProber::~nsEscCharSetProber(void) |
23 | { |
24 | for (unsigned int i = 0; i < NUM_OF_ESC_CHARSETS; i++) { |
25 | delete mCodingSM[i]; |
26 | } |
27 | } |
28 | |
29 | void nsEscCharSetProber::Reset(void) |
30 | { |
31 | mState = eDetecting; |
32 | for (unsigned int i = 0; i < NUM_OF_ESC_CHARSETS; i++) { |
33 | mCodingSM[i]->Reset(); |
34 | } |
35 | mActiveSM = NUM_OF_ESC_CHARSETS; |
36 | mDetectedCharset = nullptr; |
37 | } |
38 | |
39 | nsProbingState nsEscCharSetProber::HandleData(const char *aBuf, unsigned int aLen) |
40 | { |
41 | nsSMState codingState; |
42 | int j; |
43 | unsigned int i; |
44 | |
45 | for (i = 0; i < aLen && mState == eDetecting; i++) { |
46 | for (j = mActiveSM - 1; j >= 0; j--) { |
47 | // byte is feed to all active state machine |
48 | codingState = mCodingSM[j]->NextState(c: aBuf[i]); |
49 | if (codingState == eError) { |
50 | // got negative answer for this state machine, make it inactive |
51 | mActiveSM--; |
52 | if (mActiveSM == 0) { |
53 | mState = eNotMe; |
54 | return mState; |
55 | } else if (j != (int)mActiveSM) { |
56 | nsCodingStateMachine *t; |
57 | t = mCodingSM[mActiveSM]; |
58 | mCodingSM[mActiveSM] = mCodingSM[j]; |
59 | mCodingSM[j] = t; |
60 | } |
61 | } else if (codingState == eItsMe) { |
62 | mState = eFoundIt; |
63 | mDetectedCharset = mCodingSM[j]->GetCodingStateMachine(); |
64 | return mState; |
65 | } |
66 | } |
67 | } |
68 | |
69 | return mState; |
70 | } |
71 | } |
72 |