annotate librcd/librcd.c @ 2224:4446a9e7bdee trunk

[svn] - memory_pool_alloc_object() - memory_pool_strdup() - controlsocket now uses a memory pool for packet data
author nenolod
date Fri, 29 Dec 2006 21:01:49 -0800
parents 9becbe564217
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1613
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
1 #include <stdio.h>
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
2 #include <string.h>
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
3
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
4 #define _LIBRCD_C
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
5 #include "librcd.h"
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
6
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
7 #define NF_VALUE -2
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
8 #define max(a,b) ((a>b)?a:b)
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
9 #define min(a,b) ((a<b)?a:b)
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
10 #define bit(i) (1<<i)
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
11
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
12 typedef struct lng_stat2 {
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
13 unsigned char a;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
14 unsigned char b;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
15 double rate;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
16 double srate;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
17 double erate;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
18 } lng_stat2;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
19
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
20 #include "russian_table.h"
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
21
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
22
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
23 static int end_symbol(char ch) {
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
24 if (ch=='\r'||ch=='\n'||ch==0||ch==' '||ch=='\t'||ch==','||ch=='.'||ch=='!'||ch=='?'||ch==';'||ch=='-'||ch==':'||ch=='"'||ch=='\''||ch==')') return 1;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
25 return 0;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
26 }
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
27
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
28 static int start_symbol(char ch) {
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
29 if ((ch=='\t')||ch=='\r'||ch=='\n'||(ch==' ')||(ch=='(')||(ch=='"')||(ch=='\'')) return 1;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
30 return 0;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
31 }
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
32
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
33 typedef const struct lng_stat2 *lng_stat2_ptr;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
34
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
35 static void bfind(const unsigned char *a, lng_stat2_ptr *w, lng_stat2_ptr *k, lng_stat2_ptr *al) {
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
36 const struct lng_stat2 *winptr, *koiptr,*altptr;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
37 int ki,wi,ai,d,ws=0,ks=0,as=0;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
38 d=npow2>>1;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
39 wi=d;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
40 ki=d;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
41 ai=d;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
42 winptr=0;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
43 koiptr=0;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
44 altptr=0;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
45 do{
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
46 d>>=1;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
47
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
48 if(!ws){
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
49 if (wi>indexes2) wi-=d;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
50 else {
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
51 winptr=enc_win+wi-1;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
52 if(a[0]==winptr->a){
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
53 if(a[1]==winptr->b){
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
54 ws=1;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
55 }else if(a[1]<winptr->b){
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
56 wi-=d;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
57 }else{ //b>win[wi].b
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
58 wi+=d;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
59 }
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
60 }else if(a[0]<winptr->a){
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
61 wi-=d;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
62 }else{ //a>win[wi].a
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
63 wi+=d;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
64 }
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
65 }
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
66 }
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
67 if(!ks){
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
68 if (ki>indexes2) ki-=d;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
69 else {
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
70 koiptr=enc_koi+ki-1;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
71 if(a[0]==koiptr->a){
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
72 if(a[1]==koiptr->b){
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
73 ks=1;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
74 }else if(a[1]<koiptr->b){
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
75 ki-=d;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
76 }else{ //b>win[wi].b
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
77 ki+=d;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
78 }
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
79 }else if(a[0]<koiptr->a){
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
80 ki-=d;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
81 }else{ //a>win[wi].a
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
82 ki+=d;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
83 }
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
84 }
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
85 }
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
86 if(!as){
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
87 if (ai>indexes2) ai-=d;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
88 else {
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
89 altptr=enc_alt+ai-1;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
90 if(a[0]==altptr->a){
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
91 if(a[1]==altptr->b){
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
92 as=1;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
93 }else if(a[1]<altptr->b){
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
94 ai-=d;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
95 }else{ //b>win[wi].b
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
96 ai+=d;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
97 }
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
98 }else if(a[0]<altptr->a){
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
99 ai-=d;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
100 }else{ //a>win[wi].a
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
101 ai+=d;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
102 }
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
103 }
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
104 }
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
105 }while(d);
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
106 if (ws) *w=winptr;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
107 else *w=NULL;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
108 if (ks) *k=koiptr;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
109 else *k=NULL;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
110 if (as) *al=altptr;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
111 else *al=NULL;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
112 }
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
113
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
114 static double calculate(double s, double m, double e) {
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
115 return s+m+e;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
116 }
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
117
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
118 static int is_win_charset2(const unsigned char *txt, int len){
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
119 const struct lng_stat2 *winptr, *koiptr,*altptr;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
120 double winstep,koistep,altstep,winestep,koiestep,altestep,winsstep,koisstep,altsstep;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
121 double winstat=0,koistat=0,altstat=0,winestat=0,koiestat=0,altestat=0,winsstat=0,koisstat=0,altsstat=0;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
122 long j;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
123
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
124 #ifdef _AUTO_DEBUG
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
125 fprintf(stderr,"Word: %s\n",txt);
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
126 #endif
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
127 for(j=0;j<len-1;j++){
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
128 //skip bottom half of table
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
129 if(txt[j]<128 || txt[j+1]<128) continue;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
130 #ifdef _AUTO_DEBUG
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
131 fprintf(stderr,"Pair: %c%c",txt[j],txt[j+1]);
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
132 #endif
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
133 bfind(txt+j,&winptr,&koiptr,&altptr);
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
134
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
135 if ((j==0)||(start_symbol(txt[j-1]))) {
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
136 if (winptr) winsstep=winptr->srate;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
137 else winsstep=NF_VALUE;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
138 if (koiptr) koisstep=koiptr->srate;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
139 else koisstep=NF_VALUE;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
140 if (altptr) altsstep=altptr->srate;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
141 else altsstep=NF_VALUE;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
142 winestep=0;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
143 koiestep=0;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
144 altestep=0;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
145 winstep=0;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
146 koistep=0;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
147 altstep=0;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
148 #ifdef _AUTO_DEBUG
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
149 fprintf(stderr,", Win %lf, Koi %lf, Alt: %lf\n",winsstep,koisstep,altsstep);
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
150 #endif
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
151 } else if ((j==len-2)||(end_symbol(txt[j+2]))) {
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
152 if (winptr) winestep=winptr->erate;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
153 else winestep=NF_VALUE;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
154 if (koiptr) koiestep=koiptr->erate;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
155 else koiestep=NF_VALUE;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
156 if (altptr) altestep=altptr->erate;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
157 else altestep=NF_VALUE;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
158 winsstep=0;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
159 koisstep=0;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
160 altsstep=0;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
161 winstep=0;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
162 koistep=0;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
163 altstep=0;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
164 #ifdef _AUTO_DEBUG
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
165 fprintf(stderr,", Win %lf, Koi %lf, Alt %lf\n",winestep,koiestep,altestep);
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
166 #endif
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
167 } else {
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
168 if (winptr) winstep=winptr->rate;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
169 else winstep=NF_VALUE;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
170 if (koiptr) koistep=koiptr->rate;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
171 else koistep=NF_VALUE;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
172 if (altptr) altstep=altptr->rate;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
173 else altstep=NF_VALUE;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
174 winsstep=0;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
175 winestep=0;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
176 koisstep=0;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
177 koiestep=0;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
178 altsstep=0;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
179 altestep=0;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
180 #ifdef _AUTO_DEBUG
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
181 fprintf(stderr,", Win %lf, Koi %lf, Alt %lf\n",winstep,koistep,altstep);
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
182 #endif
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
183 }
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
184
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
185 winstat+=winstep;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
186 koistat+=koistep;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
187 altstat+=altstep;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
188 winsstat+=winsstep;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
189 koisstat+=koisstep;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
190 altsstat+=altsstep;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
191 winestat+=winestep;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
192 koiestat+=koiestep;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
193 altestat+=altestep;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
194 }
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
195
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
196 #ifdef _AUTO_DEBUG
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
197 fprintf(stderr,"Start. Win: %lf, Koi: %lf, Alt: %lf\n",winsstat,koisstat,altsstat);
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
198 fprintf(stderr,"Middle. Win: %lf, Koi: %lf, Alt: %lf\n",winstat,koistat,altstat);
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
199 fprintf(stderr,"End. Win: %lf, Koi: %lf, Alt: %lf\n",winestat,koiestat,altestat);
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
200 fprintf(stderr,"Final. Win: %lf, Koi: %lf, Alt: %lf\n",calculate(winsstat,winstat,winestat),calculate(koisstat,koistat,koiestat),calculate(altsstat,altstat,altestat));
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
201 #endif
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
202 if ((calculate(altsstat,altstat,altestat)>calculate(koisstat,koistat,koiestat))&&(calculate(altsstat,altstat,altestat)>calculate(winsstat,winstat,winestat))) return 3;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
203 if (calculate(koisstat,koistat,koiestat)>calculate(winsstat,winstat,winestat)) return 1;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
204 return 0;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
205 }
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
206
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
207
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
208 static int check_utf8(const unsigned char *buf, int len) {
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
209 long i,j;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
210 int bytes=0,rflag=0;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
211 unsigned char tmp;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
212 int res=0;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
213
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
214 for (i=0;i<len;i++) {
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
215 if (buf[i]<128) continue;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
216
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
217 if (bytes>0) {
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
218 if ((buf[i]&0xC0)==0x80) {
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
219 if (rflag) {
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
220 tmp=buf[i]&0x3F;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
221 // Russian is 0x410-0x44F
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
222 if ((rflag==1)&&(tmp>=0x10)) res++;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
223 else if ((rflag==2)&&(tmp<=0x0F)) res++;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
224 }
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
225 bytes--;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
226 } else {
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
227 res--;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
228 bytes=1-bytes;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
229 rflag=0;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
230 }
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
231 } else {
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
232 for (j=6;j>=0;j--)
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
233 if ((buf[i]&bit(j))==0) break;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
234
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
235 if ((j==0)||(j==6)) {
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
236 if ((j==6)&&(bytes<0)) bytes++;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
237 else res--;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
238 continue;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
239 }
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
240 bytes=6-j;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
241 if (bytes==1) {
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
242 // Cyrrilic D0-D3, Russian - D0-D1
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
243 if (buf[i]==0xD0) rflag=1;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
244 else if (buf[i]==0xD1) rflag=2;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
245 }
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
246 }
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
247
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
248 if ((buf[i]==0xD0)||(buf[i]==0xD1)) {
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
249 if (i+1==len) break;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
250
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
251 }
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
252 }
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
253 return res;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
254 }
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
255
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
256
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
257
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
258 rcd_russian_charset rcdGetRussianCharset(const char *buf,int len) {
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
259 long l;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
260
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
261 l = len?len:strlen(buf);
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
262 if (check_utf8((const unsigned char *)buf,l)>1) return RUSSIAN_CHARSET_UTF8;
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
263 return is_win_charset2((const unsigned char *)buf,l);
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
264 }
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
265
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
266 /* Compatibility */
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
267 rcd_russian_charset get_russian_charset(const char *buf,int len) {
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
268 return rcdGetRussianCharset(buf, len);
9becbe564217 [svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
diff changeset
269 }