| 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] = std::make_unique<nsCodingStateMachine>(args: &ISO2022JPSMModel); |
| 14 | mCodingSM[1] = std::make_unique<nsCodingStateMachine>(args: &HZSMModel); |
| 15 | } |
| 16 | |
| 17 | nsEscCharSetProber::~nsEscCharSetProber(void) = default; |
| 18 | |
| 19 | void nsEscCharSetProber::Reset(void) |
| 20 | { |
| 21 | mState = eDetecting; |
| 22 | for (auto &codingSM : mCodingSM) { |
| 23 | codingSM->Reset(); |
| 24 | } |
| 25 | mDetectedCharset = nullptr; |
| 26 | } |
| 27 | |
| 28 | nsProbingState nsEscCharSetProber::HandleData(const char *aBuf, unsigned int aLen) |
| 29 | { |
| 30 | if (mState != eDetecting) { |
| 31 | return mState; |
| 32 | } |
| 33 | |
| 34 | int activeSM = mCodingSM.size(); |
| 35 | |
| 36 | for (auto &codingSM : mCodingSM) { |
| 37 | for (unsigned int i = 0; i < aLen; i++) { |
| 38 | // byte is feed to all active state machine |
| 39 | auto codingState = codingSM->NextState(c: aBuf[i]); |
| 40 | if (codingState == eError) { |
| 41 | // got negative answer for this state machine, make it inactive |
| 42 | activeSM--; |
| 43 | if (activeSM == 0) { |
| 44 | mState = eNotMe; |
| 45 | return mState; |
| 46 | } |
| 47 | } else if (codingState == eItsMe) { |
| 48 | mState = eFoundIt; |
| 49 | mDetectedCharset = codingSM->GetCodingStateMachine(); |
| 50 | return mState; |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | return mState; |
| 56 | } |
| 57 | } |
| 58 |
