File: ec50.cpp

package info (click to toggle)
stardict-tools 3.0.2-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 2,848 kB
  • sloc: cpp: 17,404; sh: 10,701; ansic: 1,985; python: 950; php: 308; makefile: 251; perl: 135
file content (1130 lines) | stat: -rw-r--r-- 20,668 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
//file format crack by Luo XiaoHui <tipyluo@hotmail.com>, this program is written by Hu Zheng <huzheng_001@163.com>
//you can mail us for the demo program which have more comment.
//encoding of this file is Chinese simplified -> GB18030

#include "stdio.h"
#include "stdlib.h"
#include <string.h>
#include <gtk/gtk.h>
#include <glib.h>
#include <string>

//#define MAKEBIG5

#ifdef MAKEBIG5
GIConv gb2big5_converter;
GIConv utf8_converter;
#endif

char dat123[3][4];  //0x0008e000
char *chGrp[26], *chnptr,*buffer, *word;
char chn123[3][4];
short int wordLen;
unsigned short int m1613, m1611;
long m2e15, m2e19;
unsigned char m15_19[8];
char	langMask;	// ӢӢ
FILE *f;

GArray *ec_array, *ce_array;
	
char decrypt[32]={
	0x4c,0x61,0x6e,0x67,0x44,0x61,0x6f,0x20,
	0x45,0x2d,0x43,0x20,0x44,0x69,0x63,0x74,
	0x69,0x6f,0x6e,0x61,0x72,0x79,0x20,0x56,
	0x35,0x2e,0x30,0x20,0x5a,0x48,0x48,0x44

};

struct _worditem
{
	gchar *word;
	gchar *definition;
};

static void incM1613();
static long readData(char *ptr, long len, long pos);
static void readBlock();

gint stardict_strcmp(const gchar *s1, const gchar *s2)
{
	gint a;
	a = g_ascii_strcasecmp(s1, s2);
	if (a == 0)
		return strcmp(s1, s2);
	else
		return a;
}

gint comparefunc(gconstpointer a,gconstpointer b)
{
	if (!(((struct _worditem *)a)->word))
		return -1;
	if (!(((struct _worditem *)b)->word))
		return 1;
	return stardict_strcmp(((struct _worditem *)a)->word,((struct _worditem *)b)->word);
}

gchar *to_utf8_phonetic(gchar *text)
{
	gchar *start = strstr(text, "*[");
	if (!start)
		return NULL;
	if (start!= text) {
		g_print("Warnning, middle phonetic!\n");
		return NULL;
	}
	gchar *end = strchr(start + 2, ']');
	if (!end) {
		printf("Warnning, bad phonetic!\n");
		return NULL;
	}	
	*end = '\0';
	
	std::string return_text = "*[";
	const gchar *s, *old_s;
	s = start+2;
	gchar tmpstr[16];
	gint i = 0;
	gint len = strlen(s);
	gint str_len;
	gboolean processed;	
	while (i < len) {
		processed = true;
		switch (*s) {			
		case 0x18:
			return_text+="æ"; break;
		case 0x10:
			return_text+="ɑ"; break;
		case 0x0F:
			return_text+="ɒ"; break;
		case 0x12:
			return_text+="ʌ"; break;
		case 0x11:
			return_text+="ә"; break;
		case 0x0E:
			return_text+="є"; break;
		case 0x17:
			return_text+="ŋ"; break;
		case 0x13:
			return_text+="θ"; break;
		case 0x14:
			return_text+="ð"; break;
		case 0x15:
			return_text+="ʃ"; break;
		case 0x16:
			return_text+="ʒ"; break;						
		/*case 0x:
			return_text+="ː"; break;						
		case 0x:
			return_text+="ɡ"; break;						
		case 0x:
			return_text+="ˏ"; break;						
		case 0x:
			return_text+="ˋ"; break;
		case 0x:
			return_text+="ˊ"; break;*/
		default:
			processed = false;
		}
		old_s = s;
		s = g_utf8_next_char(s);
		str_len = s - old_s;
		i+= str_len;
		if (!processed) {
			strncpy(tmpstr, old_s, str_len);
			tmpstr[str_len] = '\0';
			return_text += tmpstr;
		}
	}
	return_text+= "]";
	return_text+= (end+1);
	return g_strdup(return_text.c_str());
}

void stripNewLine(gchar *text)
{
	gchar *p = text;
	gchar *n = text;
	while (*p) {
		if (*p == 13 && *(p+1) == 10) {
			*n = 10;
			p+=2;
			n++;
		}
		else {
			if (p != n)
				*n = *p;
			p++;
			n++;
		}
	}
	*n = '\0';
}

int	compare(char *str1, const char *str2, int len)
{
	unsigned char c1=0, c2=0;

	for (int i=0; i<len; i++)
	{
		c1 = *(str1+i);
		c2 = *(str2+i);

		if (c1==0)
		{
			break;
		}

		if (c1<=0x7a && c1>=0x61)
		{
			c1 -= 0x20;
		}
		if (c2<=0x7a && c2>=0x61)
		{
			c2 -= 0x20;
		}
		if (c1!=c2)
		{
			break;
		}
	}

	return c1-c2;
}

void deal1611(unsigned long &cpy1611, int cx)
{
	unsigned short int dl, dh, tmp;

	dl = (cpy1611 & 0x0000ffff);
	dh = (cpy1611 & 0xffff0000)>>16;

	if (cx>=0x10)
	{
		dh = dl;
		dl = 0;
		dh <<= (cx-0x10);

	}
	else
	{
		tmp = dl;
		dl <<= cx;
		dh <<= cx;
		cx = 0x10 - cx;
		tmp >>= cx;
		dh = dh | tmp;
	}

	cpy1611 = dh;
	cpy1611 <<= 16;
	cpy1611 += dl;
}

void getWord(char *wptr)
{
	short int i;
	char *tmptr = word;

	wordLen = 0;
	while (*(wptr+m1613)!=0x00)
	{
		*tmptr = *(wptr+m1613);
		tmptr++;
		incM1613();
		wordLen++;
	}
	*tmptr = 0x00;
	incM1613();

	for (i=0; i<8; i++)
	{
		m15_19[i] = (unsigned char)*(wptr+m1613);
		incM1613();
	}
}

void getWord1(char *wptr)
{
	short int i;
	char *tmptr = word;

	wordLen = 0;
	while (*(wptr+m1613)!=0x00)
	{
		*tmptr = *(wptr+m1613);
		tmptr++;
		m1613++;
		wordLen++;
	}
	*tmptr = 0x00;
	m1613++;

	m2e15 = m2e19 =0;
	i=0;
	while (i<4)
	{
		m2e15 *= 0xc8;
		m2e15 += (unsigned char)(*(wptr+m1613))-0x20;
		m1613++;
		i++;
	}
	i=0;
	while (i<4)
	{
		m2e19 *= 0xc8;
		m2e19 += (unsigned char)(*(wptr+m1613))-0x20;
		m1613++;
		i++;
	}
}

bool doSearch(const gchar *searchWord)
{
	short int len, i, j;
	unsigned long offset;
	unsigned char frch;  //
	char m2e1d[6];
	char *cptr;	//cptr
	const char *wptr;	//wptr

	wptr = searchWord;
	frch = *wptr;

	if (frch>=0xb0)
	{//
		unsigned short int si=0;
		langMask = 'c';
		si = (unsigned char)frch;
		si -= 0xb0;
		si <<= 9;
		cptr = chnptr+si;
		i = (unsigned char)*(cptr+1);
		j = (unsigned char)*(cptr);
		m1611 = i*0x100;
		m1611 += j;
		m1613 = 0;
		cptr += 2;

		for (; cptr<=(chnptr+si+0x0200); cptr+=3)
		{
			if (compare(cptr, wptr+1, 3)>=0)
			{
				break;
			}
			m1611++;
			continue;
		}
		readBlock();
		while (m1613<0x800)
		{
			if (*(buffer+m1613)==0x01 || *(buffer+m1613)==0x1a)
			{
				break;
			}
			incM1613();
		}

		do
		{
			if (*(buffer+m1613)==0x1a)
			{
				return false;
			}
			incM1613();
			getWord(buffer);

		}while ((compare(word, wptr, wordLen+1))<0);
		
		/* convent m15_19[] to m2e15 and m2e19 */
		i=0;
		m2e15=m2e19=0;

		while (i<4)
		{
			m2e15 *= 0xc8;
			m2e15 += m15_19[i]-0x20;
			i++;
		}
		while (i<8)
		{
			m2e19 *= 0xc8;
			m2e19 += m15_19[i]-0x20;
			i++;
		}

		*(word+wordLen) = 0x0d;
		*(word+wordLen+1) = 0x0a;

		offset = m2e19;
		offset += (unsigned char)(*chn123[1]);
		offset += (unsigned char)(*(chn123[1]+1)) * 0x100;
		offset += (unsigned char)(*(chn123[1]+2)) * 0x10000;
		offset += (unsigned char)(*(chn123[1]+3)) * 0x1000000;
		fseek(f, offset, SEEK_SET);
		if ((len=readData(m2e1d, 2, offset))<=0)
		{
			return false;
		}
		i = 0x0c00;
		i -= wordLen;
		i -= 4;
		j = (unsigned char)(*(m2e1d));
		j += (unsigned char)(*(m2e1d+1)) * 0x100;

		if (i>j)
		{
			i = j;
		}

		if ((len=readData(word+wordLen+2, i, offset+2))<=0)
		{
			return false;
		}
	}
	else
	{//С0xb0,
		langMask = 'e';
		if (frch<=0x5a && frch>=0x41)
		{
			frch -= 0x41;
		}
		else if (frch<=0x7a && frch>=0x61)
		{
			frch -= 0x61;
		}
		else
		{
			return false;
		}


		cptr = chGrp[frch];
		i = (unsigned char)*(cptr+1);
		j = (unsigned char)(*cptr);
		m1611 = i*0x100;
		m1611 += j;
		m1613 = 0;

		cptr += 2;
		for (; cptr<(chGrp[frch]+0x0c00); cptr+=5)
		{
			if (compare(cptr, wptr+1, 5)>=0)
			{
				break;
			}
			m1611++;
			continue;
		}

		readBlock();
		
		while (m1613<0x800)
		{
			if (*(buffer+m1613)==0x01 || *(buffer+m1613)==0x1a)
			{

				break;
			}
			incM1613();			
		}

		do
		{
			if (*(buffer+m1613)==0x1a)
			{
				return false;
			}
			incM1613();
			getWord(buffer);

		}while ((compare(word, wptr, wordLen+1))<0);

		/* convent m15_19[] to m2e15 and m2e19 */
		i=0;
		m2e15=m2e19=0;

		while (i<4)
		{
			m2e15 *= 0xc8;
			m2e15 += m15_19[i]-0x20;
			i++;
		}
		while (i<8)
		{
			m2e19 *= 0xc8;
			m2e19 += m15_19[i]-0x20;
			i++;
		}

		*(word+wordLen) = 0x0d;
		*(word+wordLen+1) = 0x0a;

		offset = m2e19;
		offset += (unsigned char)(*dat123[1]);
		offset += (unsigned char)(*(dat123[1]+1)) * 0x100;
		offset += (unsigned char)(*(dat123[1]+2)) * 0x10000;
		offset += (unsigned char)(*(dat123[1]+3)) * 0x1000000;

		fseek(f, offset, SEEK_SET);

		if ((len=readData(m2e1d, 4, offset))<=0)
		{
			return false;
		}

		if ((len=readData(m2e1d+4, 2, offset+4))<=0)
		{
			return false;
		}

		i = 0x0c00;
		i -= wordLen;
		i -= 4;
		j = (unsigned char)(*(m2e1d+4));
		j += (unsigned char)(*(m2e1d+5)) * 0x100;

		if (i>j)
		{
			i = j;
		}

		if ((len=readData(word+wordLen+2, i, offset+6))<=0)
		{
			return false;
		}
	}


	*(word+wordLen+2+len)=0;

	return true;
}

void readBlock()
{
	char *ptr;	
	unsigned long offset;

	if (m1611>=0x8000)
	{
		offset = 0xffff0000 + m1611;
	}
	else
	{
		offset = 0x00000000 + m1611;
	}

	deal1611(offset, 0x0b);  

	if (langMask=='e')
	{
		ptr = dat123[0];
	}
	else if (langMask=='c')
	{
		ptr = chn123[0];
	}
	else
	{
		return;
	}

	offset += (unsigned char)(*ptr);
	offset += (unsigned char)((*(ptr+1)))<<8;
	offset += (unsigned char)((*(ptr+2)))<<16;
	offset += (unsigned char)((*(ptr+3)))<<24;

	fseek(f, offset, SEEK_SET);

	if ((readData(buffer, 0x800, offset))<=0)
	{
		return;
	}
}

void incM1613()
{
	m1613++;
	if (m1613==0x800)
	{
		m1611++;
		readBlock();
		m1613=0;
	}
}

long readData(char *ptr, long len, long pos)
{
	unsigned short int i1, i2, dec;
	long count;
	char c;

	i1 = pos & 0x000003ff;  //ȡ pos 10λ
	i2 = i1 + (pos>>10);			//ȥ pos 10λ i1 

	fread(ptr, 1, len, f);

	//²Ϊܹ
	count = 0;
	dec = i1;
	while(count<len)
	{
		dec = i1;
		dec = dec & 0x01f;

		c = decrypt[dec];
		c = c ^ i2;
		*ptr = *ptr ^ c;
		ptr++;

		i2++;
		i1++;
		if (i1==0x400)
		{
			i1 = 0;	
			i2++;
		}
		count++;
	}

	return len;
}

void captureAllChn(int chnum)
{
	struct _worditem worditem;
#ifdef MAKEBIG5
	gchar *big5word;
#endif
	
	short int len, i, j;
	unsigned long offset;
	char *cptr, *ptr;	//cptr
	bool end = false;
	const char const_ff[5]={0x0ff,0x0ff,0x0ff,0x0ff,0x0ff};

	langMask = 'c';
	unsigned short int si=0;
	si = chnum;
	si <<= 9;
	cptr = chnptr+si;

	i = (unsigned char)*(cptr+1);
	j = (unsigned char)(*cptr);
	m1611 = i*0x100;
	m1611 += j;

	cptr += 2;
	m1611--;
	for (; (cptr<(chnptr+si+0x0200) && !end); cptr+=3)
	{
		m1613 = 0;
		if (compare(cptr, const_ff, 3)>=0)
		{
			end = true;
		}
		m1611++;

		if (m1611>=0x8000)
		{
			offset = 0xffff0000 + m1611;
		}
		else
		{
			offset = 0x00000000 + m1611;
		}

		deal1611(offset, 0x0b);  //

		ptr = chn123[0];
		offset += (unsigned char)(*ptr);
		offset += (unsigned char)((*(ptr+1)))<<8;
		offset += (unsigned char)((*(ptr+2)))<<16;
		offset += (unsigned char)((*(ptr+3)))<<24;

		fseek(f, offset, SEEK_SET);

		if ((len=readData(buffer, 0x800, offset))<=0)
		{
			g_print("error in %d! readData\n", chnum);
			return;
		}

		while (m1613<0x800)
		{
			if (*(buffer+m1613)==0x01 || *(buffer+m1613)==0x1a)
			{
				break;
			}
			m1613++;
		}

		do
		{
			if (*(buffer+m1613)==0x1a)
			{
				g_print("error in %d! 0x1a\n", chnum);
				return;
			}
			m1613++;
			getWord1(buffer);

			word[wordLen] = '\0';
			
			if (word[0]) {
#ifdef MAKEBIG5
				big5word = g_convert_with_iconv(word, wordLen,gb2big5_converter,NULL,NULL,NULL);
				if (big5word) {
					worditem.word = g_convert_with_iconv(big5word, -1, utf8_converter,NULL,NULL,NULL);
					g_free(big5word);
				}
				else {
					worditem.word = NULL;
					//printf("convert to big5 failed1: %s\n", word);
				}
#else
				worditem.word = g_locale_to_utf8(word, wordLen,NULL, NULL, NULL);
#endif
				if (worditem.word) {
					g_free(worditem.word);
					worditem.word = g_strdup(word);
					g_array_append_val(ce_array, worditem); //success
				}
				else {
					// here have about 1700 words that are invalid :(, this is the only space that are not perfect!!!
					//printf("bad chinese word: %s\n", word);					
				}
			}
			else {
				//printf("empty chinese word\n");
			}
			
		}while (m1613<0x800);
	}	
}

void searchAllChn()
{
	gint tmpwordlen;
	struct _worditem *pworditem;
	gchar *tmpword, *tmp_utf8, *p;
#ifdef MAKEBIG5
	gchar *big5word;
#endif
	
	g_print("Searching...\n");
	for (guint i = 0; i < ce_array->len; i++) {
		pworditem = &g_array_index(ce_array, struct _worditem, i);
		if (doSearch(pworditem->word) && word[0]) {
			stripNewLine(word);
			tmpwordlen = strlen(pworditem->word);
			if (strncmp(word, pworditem->word, tmpwordlen) == 0 && word[tmpwordlen] == 10) {				
			}
			else {
				printf("fix wrong head: %s", pworditem->word);
				g_free(pworditem->word);
				p = strchr(word, '\n');
				*p='\0';
				tmpwordlen = strlen(word);				
				pworditem->word = g_strdup(word);
				g_strstrip(pworditem->word);
				printf(" | %s\n", pworditem->word);				
			}
			g_strstrip(word + tmpwordlen +1);
#ifdef MAKEBIG5
			big5word = g_convert_with_iconv(word + tmpwordlen+1, -1,gb2big5_converter,NULL,NULL,NULL);
			if (big5word) {
				tmp_utf8 = g_convert_with_iconv(big5word, -1, utf8_converter,NULL,NULL,NULL);
				g_free(big5word);
			}
			else {
				tmp_utf8 = NULL;
				//printf("convert to big5 failed: %s\n", pworditem->word);
			}				
#else
			tmp_utf8 = g_locale_to_utf8(word + tmpwordlen +1, -1,NULL, NULL, NULL);
#endif
			if (tmp_utf8) {
				pworditem->definition = tmp_utf8;
				tmpword = pworditem->word;
#ifdef MAKEBIG5
				big5word = g_convert_with_iconv(tmpword, -1,gb2big5_converter,NULL,NULL,NULL);
				pworditem->word = g_convert_with_iconv(big5word, -1, utf8_converter,NULL,NULL,NULL);
				g_free(big5word);
#else
				pworditem->word = g_locale_to_utf8(tmpword, -1,NULL, NULL, NULL);;
#endif
				g_free(tmpword);
			}
			else {
				//printf("chinese word's definition invalid: %s\n", pworditem->word);
				g_free(pworditem->word);
				pworditem->word= NULL;
			}
		}
		else {
			printf("chinese word's definition not found: %s\n", pworditem->word);
			g_free(pworditem->word);
			pworditem->word= NULL;
		}
		if (i %10000 == 0)
			printf(".");
	}
}

void captureAllWord(int chnum)
{	
	struct _worditem worditem;
	
	short int len, i, j;
	unsigned long offset;
	char *cptr, *ptr;
	bool end = false;
	const char const_ff[5]={0x0ff,0x0ff,0x0ff,0x0ff,0x0ff};

	langMask = 'e';
	
	cptr = chGrp[chnum];
	i = (unsigned char)*(cptr+1);
	j = (unsigned char)(*cptr);
	m1611 = i*0x100;
	m1611 += j;
	

	cptr += 2;
	m1611--;
	for (; (cptr<(chGrp[chnum]+0x0c00) && !end); cptr+=5)
	{
		m1613 = 0;
		if (compare(cptr, const_ff, 5)>=0)
		{
			end = true;
		}
		m1611++;
	
		if (m1611>=0x8000)
		{
			offset = 0xffff0000 + m1611;
		}
		else
		{
			offset = 0x00000000 + m1611;
		}

		deal1611(offset, 0x0b);  //cable -> 0x0017e800

		ptr = dat123[0];
		offset += (unsigned char)(*ptr);
		offset += (unsigned char)((*(ptr+1)))<<8;
		offset += (unsigned char)((*(ptr+2)))<<16;
		offset += (unsigned char)((*(ptr+3)))<<24;

		fseek(f, offset, SEEK_SET);

		if ((len=readData(buffer, 0x800, offset))<=0)
		{
			g_print("error in %d! readData\n", chnum);
			return;
		}
		
		while (m1613<0x800)
		{
			if (*(buffer+m1613)==0x01 || *(buffer+m1613)==0x1a)
			{
				break;
			}
			m1613++;
		}

		do
		{
			if (*(buffer+m1613)==0x1a)
			{
				g_print("error in %d! 0x1a\n", chnum);
				return;
			}
			m1613++;
			getWord1(buffer);	
			
			word[wordLen] = '\0';
			g_strstrip(word);
			if (word[0]) {
				if (g_utf8_validate(word, -1,NULL)) {
					worditem.word = g_strdup(word);
					g_array_append_val(ec_array, worditem); //success
				}
				else {
					printf("bad english word: %s\n", word);
				}			
			}
			else {
				//printf("empty english word!\n");
			}
		}while (m1613<0x800);
	}
}

void searchAllWord()
{
	g_print("Searching...\n");
#ifdef MAKEBIG5
	gchar *big5word;
#endif
	gint tmpwordlen;
	struct _worditem *pworditem;
	gchar *tmp_utf8;
	gchar *p;
	for (guint i = 0; i < ec_array->len; i++) {
		pworditem = &g_array_index(ec_array, struct _worditem, i);
		if (doSearch(pworditem->word) && word[0]) {
			stripNewLine(word);
			tmpwordlen = strlen(pworditem->word);
			if (strncmp(word, pworditem->word, tmpwordlen) == 0 && word[tmpwordlen] == 10) {
			}
			else {
				//printf("fix wrong head: %s", pworditem->word);
				g_free(pworditem->word);
				p = strchr(word, '\n');
				*p='\0';
				tmpwordlen = strlen(word);				
				pworditem->word = g_strdup(word);
				g_strstrip(pworditem->word);
				//printf(" | %s\n", pworditem->word);				
			}
			g_strstrip(word + tmpwordlen +1);
#ifdef MAKEBIG5
			big5word = g_convert_with_iconv(word + tmpwordlen+1, -1,gb2big5_converter,NULL,NULL,NULL);
			if (big5word) {
				tmp_utf8 = g_convert_with_iconv(big5word, -1, utf8_converter,NULL,NULL,NULL);
				g_free(big5word);
			}
			else {
				tmp_utf8 = NULL;
				//printf("convert to big5 failed: %s\n", pworditem->word);
			}				
#else
			tmp_utf8 = g_locale_to_utf8(word + tmpwordlen+1, -1,NULL, NULL, NULL);
#endif			
			if (tmp_utf8) {
				pworditem->definition = to_utf8_phonetic(tmp_utf8);
				if (pworditem->definition) {
					g_free(tmp_utf8);
				}
				else
					pworditem->definition = tmp_utf8;
			}
			else {
				//g_print("english word's definition invalid: %s\n", pworditem->word);
				g_free(pworditem->word);
				pworditem->word= NULL;
			}
		}
		else {
			g_print("english word's definition not found: %s\n", pworditem->word);
			g_free(pworditem->word);
			pworditem->word= NULL;
		}
	}
}

void initVari()
{
	buffer = new char[0x800];
	word = new char[0x0c00];
}

void closeVari()
{
	for(int i=0; i<26; i++)
	{
		delete []chGrp[i]; 
	}
	delete []buffer;
	delete []word;
}

void initFile()
{
	unsigned long len;
	unsigned short int i;
	
	f = fopen("ec50.dat", "r");

	fseek(f, 0x0008e000, SEEK_SET);
	len = 4;
	i = 0;
	while (i<3 && len==4)
	{
		len = readData(dat123[i], 4, 0x0008e000+4*i);
		i++;
	}
	
	if (len<4)
	{
		return;
	}

	fseek(f, 0x000c0000, SEEK_SET);

	len = 4;
	i = 0;
	while (i<3 &&len==4)
	{
		len = readData(chn123[i], 4, 0x000c0000+4*i);
		i++;
	}
	if (len<4)
	{
		return;
	}

	fseek(f, 0x00105800, SEEK_SET);
	chnptr = new char[0x9000];
	len = readData(chnptr, 0x9000, 0x00105800);
	
	//λ ec50.dat  0x000f2000жȡ26СΪ0x0c00С
	fseek(f, 0x000f2000, SEEK_SET);
	len = 0x0c00;
	i = 0;
	while (i<26 && len==0x0c00)
	{		
		chGrp[i] = new char[0x0c00];
		len = readData(chGrp[i], 0x0c00, 0x000f2000+0x0c00*i); 
		i++;
	}
	if (len<0x0c00)
	{
		return;
	}
}

void closeFile()
{
	fclose(f);
}

void save_array_to_file(GArray *array, const gchar *basefilename)
{
	g_array_sort(array,comparefunc);
		
	gchar idxfilename[256];
	gchar dicfilename[256];
	sprintf(idxfilename, "%s.idxdata", basefilename);
	sprintf(dicfilename, "%s.dict", basefilename);
	FILE *idxfile = fopen(idxfilename,"w");
	FILE *dicfile = fopen(dicfilename,"w");

	glong tmpglong = 0;
	fwrite(&(tmpglong),sizeof(glong),1,idxfile);		
	
	guint wordcount = array->len;

	long offset_old;
	const gchar *previous_word = "";
	struct _worditem *pworditem;	
	gint definition_len;
	gulong i;
	for (i= 0;i < array->len; i++) {
		pworditem = &g_array_index(array, struct _worditem, i);
		if (pworditem->word) {			
			if (strcmp(previous_word,pworditem->word)==0) {
				//printf("Double! %s\n",previous_word);
				wordcount--;
			}
			else {
				previous_word = pworditem->word;
				offset_old = ftell(dicfile);
				definition_len = strlen(pworditem->definition);
				fwrite(pworditem->definition, 1 ,definition_len,dicfile);
						
				fwrite(pworditem->word,sizeof(gchar),strlen(pworditem->word)+1,idxfile);
				tmpglong = g_htonl(offset_old);
				fwrite(&(tmpglong),sizeof(glong),1,idxfile);
				tmpglong = g_htonl(definition_len);
				fwrite(&(tmpglong),sizeof(glong),1,idxfile);	
			}
		}
		else {			
			wordcount--;
		}
	}	
	for (i= 0;i < array->len; i++) {
		pworditem = &g_array_index(array, struct _worditem, i);
		if (pworditem->word) {
			g_free(pworditem->word);
			g_free(pworditem->definition);
		}
	}	
		
	fseek(idxfile, 0,SEEK_SET);
	tmpglong = g_htonl(wordcount);
	fwrite(&(tmpglong),sizeof(glong),1,idxfile);
	
	g_print("%s wordcount: %u\n", basefilename, wordcount);

	g_free(buffer);
    g_array_free(array,TRUE);
	
	fclose(idxfile);
	fclose(dicfile);
}

void convert()
{
	initVari();
	initFile();

#ifdef MAKEBIG5
gb2big5_converter = g_iconv_open("BIG5","GB2312");
utf8_converter = g_iconv_open("UTF-8","BIG5");
#endif
	
	ec_array = g_array_sized_new(FALSE,FALSE, sizeof(struct _worditem),500000);			
	for (int i=0; i<26; i++) {
		g_print("captureAllWord%d...\n", i);
		captureAllWord(i);
		g_print("%u\n", ec_array->len);
	}
	searchAllWord();
#ifdef MAKEBIG5
	save_array_to_file(ec_array, "langdao-ec-big5");
#else
	save_array_to_file(ec_array, "langdao-ec-gb");
#endif
	
	
	
	/*ce_array = g_array_sized_new(FALSE,FALSE, sizeof(struct _worditem),450000);			
	for (int i=0; i<72; i++) {
		g_print("captureAllChn%d...\n", i);
		captureAllChn(i);
		g_print("%u\n", ce_array->len);
	}
	searchAllChn();
#ifdef MAKEBIG5
	save_array_to_file(ce_array, "langdao-ce-big5");
#else
	save_array_to_file(ce_array, "langdao-ce-gb");
#endif*/

#ifdef MAKEBIG5
	g_iconv_close(gb2big5_converter);
	g_iconv_close(utf8_converter);
#endif
	
	closeVari();
	closeFile();
};

int
main(int argc,char * argv [])
{
	gtk_set_locale ();
	g_type_init ();
	
	convert();	
	
	return FALSE;	
}