Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/intl/chardet/nsCyrillicDetector.cpp
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
#include "nscore.h"
6
#include "nsCyrillicProb.h"
7
#include <stdio.h>
8
9
#include "nsCOMPtr.h"
10
#include "nsISupports.h"
11
#include "nsICharsetDetector.h"
12
#include "nsICharsetDetectionObserver.h"
13
#include "nsIStringCharsetDetector.h"
14
#include "nsCyrillicDetector.h"
15
16
//----------------------------------------------------------------------
17
// Interface nsISupports [implementation]
18
NS_IMPL_ISUPPORTS(nsCyrXPCOMDetector, nsICharsetDetector)
19
20
void nsCyrillicDetector::HandleData(const char* aBuf, uint32_t aLen)
21
0
{
22
0
   uint8_t cls;
23
0
   const char* b;
24
0
   uint32_t i;
25
0
   if(mDone)
26
0
      return;
27
0
   for(i=0, b=aBuf;i<aLen;i++,b++)
28
0
   {
29
0
     for(unsigned j=0;j<mItems;j++)
30
0
     {
31
0
        if( 0x80 & *b)
32
0
           cls = mCyrillicClass[j][(*b) & 0x7F];
33
0
        else
34
0
           cls = 0;
35
0
        NS_ASSERTION( cls <= 32 , "illegal character class");
36
0
        mProb[j] += gCyrillicProb[mLastCls[j]][cls];
37
0
        mLastCls[j] = cls;
38
0
     }
39
0
   }
40
0
   // We now only based on the first block we receive
41
0
   DataEnd();
42
0
}
43
44
//---------------------------------------------------------------------
45
#define THRESHOLD_RATIO 1.5f
46
void nsCyrillicDetector::DataEnd()
47
0
{
48
0
   uint32_t max=0;
49
0
   uint8_t  maxIdx=0;
50
0
   uint8_t j;
51
0
   if(mDone)
52
0
      return;
53
0
   for(j=0;j<mItems;j++) {
54
0
      if(mProb[j] > max)
55
0
      {
56
0
           max = mProb[j];
57
0
           maxIdx= j;
58
0
      }
59
0
   }
60
0
61
0
   if( 0 == max ) // if we didn't get any 8 bits data
62
0
     return;
63
0
64
#ifdef DEBUG
65
   for(j=0;j<mItems;j++)
66
      printf("Charset %s->\t%d\n", mCharsets[j], mProb[j]);
67
#endif
68
0
   this->Report(mCharsets[maxIdx]);
69
0
   mDone = true;
70
0
}
71
72
//---------------------------------------------------------------------
73
nsCyrXPCOMDetector:: nsCyrXPCOMDetector(uint8_t aItems,
74
                      const uint8_t ** aCyrillicClass,
75
                      const char **aCharsets)
76
       : nsCyrillicDetector(aItems, aCyrillicClass, aCharsets)
77
0
{
78
0
    mObserver = nullptr;
79
0
}
80
81
//---------------------------------------------------------------------
82
nsCyrXPCOMDetector::~nsCyrXPCOMDetector()
83
0
{
84
0
}
85
86
//---------------------------------------------------------------------
87
NS_IMETHODIMP nsCyrXPCOMDetector::Init(
88
  nsICharsetDetectionObserver* aObserver)
89
0
{
90
0
  NS_ASSERTION(mObserver == nullptr , "Init twice");
91
0
  if(nullptr == aObserver)
92
0
     return NS_ERROR_ILLEGAL_VALUE;
93
0
94
0
  mObserver = aObserver;
95
0
  return NS_OK;
96
0
}
97
98
//----------------------------------------------------------
99
NS_IMETHODIMP nsCyrXPCOMDetector::DoIt(
100
  const char* aBuf, uint32_t aLen, bool* oDontFeedMe)
101
0
{
102
0
  NS_ASSERTION(mObserver != nullptr , "have not init yet");
103
0
104
0
  if((nullptr == aBuf) || (nullptr == oDontFeedMe))
105
0
     return NS_ERROR_ILLEGAL_VALUE;
106
0
107
0
  this->HandleData(aBuf, aLen);
108
0
  *oDontFeedMe = false;
109
0
  return NS_OK;
110
0
}
111
112
//----------------------------------------------------------
113
NS_IMETHODIMP nsCyrXPCOMDetector::Done()
114
0
{
115
0
  NS_ASSERTION(mObserver != nullptr , "have not init yet");
116
0
  this->DataEnd();
117
0
  return NS_OK;
118
0
}
119
120
//----------------------------------------------------------
121
void nsCyrXPCOMDetector::Report(const char* aCharset)
122
0
{
123
0
  NS_ASSERTION(mObserver != nullptr , "have not init yet");
124
0
  mObserver->Notify(aCharset, eBestAnswer);
125
0
}