forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoogle.patch
1354 lines (1274 loc) · 42.4 KB
/
google.patch
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
Index: src/hunspell/affixmgr.cxx
===================================================================
RCS file: /cvsroot/hunspell/hunspell/src/hunspell/affixmgr.cxx,v
retrieving revision 1.41
diff -u -r1.41 affixmgr.cxx
--- src/hunspell/affixmgr.cxx 16 Dec 2011 09:15:34 -0000 1.41
+++ src/hunspell/affixmgr.cxx 29 May 2014 01:05:07 -0000
@@ -14,8 +14,14 @@
#include "csutil.hxx"
+#ifdef HUNSPELL_CHROME_CLIENT
+AffixMgr::AffixMgr(hunspell::BDictReader* reader, HashMgr** ptr, int * md)
+{
+ bdict_reader = reader;
+#else
AffixMgr::AffixMgr(const char * affpath, HashMgr** ptr, int * md, const char * key)
{
+#endif
// register hash manager and load affix data from aff file
pHMgr = ptr[0];
alldic = ptr;
@@ -107,9 +113,17 @@
sFlag[i] = NULL;
}
+#ifdef HUNSPELL_CHROME_CLIENT
+ // Define dummy parameters for parse_file() to avoid changing the parameters
+ // of parse_file(). This may make it easier to merge the changes of the
+ // original hunspell.
+ const char* affpath = NULL;
+ const char* key = NULL;
+#else
for (int j=0; j < CONTSIZE; j++) {
contclasses[j] = 0;
}
+#endif
if (parse_file(affpath, key)) {
HUNSPELL_WARNING(stderr, "Failure loading aff file %s\n",affpath);
@@ -269,6 +283,43 @@
char * line; // io buffers
char ft; // affix type
+#ifdef HUNSPELL_CHROME_CLIENT
+ // open the affix file
+ // We're always UTF-8
+ utf8 = 1;
+
+ // A BDICT file stores PFX and SFX lines in a special section and it provides
+ // a special line iterator for reading PFX and SFX lines.
+ // We create a FileMgr object from this iterator and parse PFX and SFX lines
+ // before parsing other lines.
+ hunspell::LineIterator affix_iterator = bdict_reader->GetAffixLineIterator();
+ FileMgr* iterator = new FileMgr(&affix_iterator);
+ if (!iterator) {
+ HUNSPELL_WARNING(stderr,
+ "error: could not create a FileMgr from an affix line iterator.\n");
+ return 1;
+ }
+
+ while ((line = iterator->getline())) {
+ ft = ' ';
+ if (strncmp(line,"PFX",3) == 0) ft = complexprefixes ? 'S' : 'P';
+ if (strncmp(line,"SFX",3) == 0) ft = complexprefixes ? 'P' : 'S';
+ if (ft != ' ')
+ parse_affix(line, ft, iterator, NULL);
+ }
+ delete iterator;
+
+ // Create a FileMgr object for reading lines except PFX and SFX lines.
+ // We don't need to change the loop below since our FileMgr emulates the
+ // original one.
+ hunspell::LineIterator other_iterator = bdict_reader->GetOtherLineIterator();
+ FileMgr * afflst = new FileMgr(&other_iterator);
+ if (!afflst) {
+ HUNSPELL_WARNING(stderr,
+ "error: could not create a FileMgr from an other line iterator.\n");
+ return 1;
+ }
+#else
// checking flag duplication
char dupflags[CONTSIZE];
char dupflags_ini = 1;
@@ -282,6 +333,7 @@
HUNSPELL_WARNING(stderr, "error: could not open affix description file %s\n",affpath);
return 1;
}
+#endif
// step one is to parse the affix file building up the internal
// affix data structures
@@ -291,6 +343,7 @@
while ((line = afflst->getline()) != NULL) {
mychomp(line);
+#ifndef HUNSPELL_CHROME_CLIENT
/* remove byte order mark */
if (firstline) {
firstline = 0;
@@ -299,6 +352,7 @@
memmove(line, line+3, strlen(line+3)+1);
}
}
+#endif
/* parse in the keyboard string */
if (strncmp(line,"KEY",3) == 0) {
@@ -545,6 +599,7 @@
}
}
+#ifndef HUNSPELL_CHROME_CLIENT
/* parse in the typical fault correcting table */
if (strncmp(line,"REP",3) == 0) {
if (parse_reptable(line, afflst)) {
@@ -552,6 +607,7 @@
return 1;
}
}
+#endif
/* parse in the input conversion table */
if (strncmp(line,"ICONV",5) == 0) {
@@ -699,6 +755,7 @@
checksharps=1;
}
+#ifndef HUNSPELL_CHROME_CLIENT
/* parse this affix: P - prefix, S - suffix */
ft = ' ';
if (strncmp(line,"PFX",3) == 0) ft = complexprefixes ? 'S' : 'P';
@@ -713,6 +770,7 @@
return 1;
}
}
+#endif
}
finishFileMgr(afflst);
@@ -1307,6 +1365,26 @@
const char * r;
int lenr, lenp;
+#ifdef HUNSPELL_CHROME_CLIENT
+ const char *pattern, *pattern2;
+ hunspell::ReplacementIterator iterator = bdict_reader->GetReplacementIterator();
+ while (iterator.GetNext(&pattern, &pattern2)) {
+ r = word;
+ lenr = strlen(pattern2);
+ lenp = strlen(pattern);
+
+ // search every occurence of the pattern in the word
+ while ((r=strstr(r, pattern)) != NULL) {
+ strcpy(candidate, word);
+ if (r-word + lenr + strlen(r+lenp) >= MAXLNLEN) break;
+ strcpy(candidate+(r-word), pattern2);
+ strcpy(candidate+(r-word)+lenr, r+lenp);
+ if (candidate_check(candidate,strlen(candidate))) return 1;
+ r++; // search for the next letter
+ }
+ }
+
+#else
if ((wl < 2) || !numrep) return 0;
for (int i=0; i < numrep; i++ ) {
@@ -1323,6 +1401,7 @@
r++; // search for the next letter
}
}
+#endif
return 0;
}
@@ -4219,6 +4298,7 @@
case 1: {
np++;
aflag = pHMgr->decode_flag(piece);
+#ifndef HUNSPELL_CHROME_CLIENT // We don't check for duplicates.
if (((at == 'S') && (dupflags[aflag] & dupSFX)) ||
((at == 'P') && (dupflags[aflag] & dupPFX))) {
HUNSPELL_WARNING(stderr, "error: line %d: multiple definitions of an affix flag\n",
@@ -4226,6 +4306,7 @@
// return 1; XXX permissive mode for bad dictionaries
}
dupflags[aflag] += (char) ((at == 'S') ? dupSFX : dupPFX);
+#endif
break;
}
// piece 3 - is cross product indicator
Index: src/hunspell/affixmgr.hxx
===================================================================
RCS file: /cvsroot/hunspell/hunspell/src/hunspell/affixmgr.hxx,v
retrieving revision 1.15
diff -u -r1.15 affixmgr.hxx
--- src/hunspell/affixmgr.hxx 13 Oct 2011 13:41:54 -0000 1.15
+++ src/hunspell/affixmgr.hxx 29 May 2014 01:05:07 -0000
@@ -18,6 +18,40 @@
class PfxEntry;
class SfxEntry;
+#ifdef HUNSPELL_CHROME_CLIENT
+
+#include <vector>
+
+// This class provides an implementation of the contclasses array in AffixMgr
+// that is normally a large static array. We should almost never need more than
+// 256 elements, so this class only allocates that much to start off with. If
+// elements higher than that are actually used, we'll automatically expand.
+class ContClasses {
+ public:
+ ContClasses() {
+ // Pre-allocate a buffer so that typically, we'll never have to resize.
+ EnsureSizeIs(256);
+ }
+
+ char& operator[](size_t index) {
+ EnsureSizeIs(index + 1);
+ return data[index];
+ }
+
+ void EnsureSizeIs(size_t new_size) {
+ if (data.size() >= new_size)
+ return; // Nothing to do.
+
+ size_t old_size = data.size();
+ data.resize(new_size);
+ memset(&data[old_size], 0, new_size - old_size);
+ }
+
+ std::vector<char> data;
+};
+
+#endif // HUNSPELL_CHROME_CLIENT
+
class LIBHUNSPELL_DLL_EXPORTED AffixMgr
{
@@ -106,12 +140,20 @@
int fullstrip;
int havecontclass; // boolean variable
+#ifdef HUNSPELL_CHROME_CLIENT
+ ContClasses contclasses;
+#else
char contclasses[CONTSIZE]; // flags of possible continuing classes (twofold affix)
+#endif
public:
+#ifdef HUNSPELL_CHROME_CLIENT
+ AffixMgr(hunspell::BDictReader* reader, HashMgr** ptr, int * md);
+#else
AffixMgr(const char * affpath, HashMgr** ptr, int * md,
const char * key = NULL);
+#endif
~AffixMgr();
struct hentry * affix_check(const char * word, int len,
const unsigned short needflag = (unsigned short) 0,
@@ -218,6 +260,10 @@
int get_fullstrip() const;
private:
+#ifdef HUNSPELL_CHROME_CLIENT
+ // Not owned by us, owned by the Hunspell object.
+ hunspell::BDictReader* bdict_reader;
+#endif
int parse_file(const char * affpath, const char * key);
int parse_flag(char * line, unsigned short * out, FileMgr * af);
int parse_num(char * line, int * out, FileMgr * af);
@@ -249,4 +295,3 @@
};
#endif
-
Index: src/hunspell/filemgr.cxx
===================================================================
RCS file: /cvsroot/hunspell/hunspell/src/hunspell/filemgr.cxx,v
retrieving revision 1.5
diff -u -r1.5 filemgr.cxx
--- src/hunspell/filemgr.cxx 23 Jun 2011 09:21:50 -0000 1.5
+++ src/hunspell/filemgr.cxx 29 May 2014 01:05:07 -0000
@@ -7,6 +7,32 @@
#include "filemgr.hxx"
+#ifdef HUNSPELL_CHROME_CLIENT
+#include "third_party/hunspell/google/bdict_reader.h"
+
+FileMgr::FileMgr(hunspell::LineIterator* iterator) : iterator_(iterator) {
+}
+
+FileMgr::~FileMgr() {
+}
+
+char * FileMgr::getline() {
+ // Read one line from a BDICT file and store the line to our line buffer.
+ // To emulate the original FileMgr::getline(), this function returns
+ // the pointer to our line buffer if we can read a line without errors.
+ // Otherwise, this function returns NULL.
+ bool result = iterator_->AdvanceAndCopy(line_, BUFSIZE - 1);
+ return result ? line_ : NULL;
+}
+
+int FileMgr::getlinenum() {
+ // This function is used only for displaying a line number that causes a
+ // parser error. For a BDICT file, providing a line number doesn't help
+ // identifying the place where causes a parser error so much since it is a
+ // binary file. So, we just return 0.
+ return 0;
+}
+#else
int FileMgr::fail(const char * err, const char * par) {
fprintf(stderr, err, par);
return -1;
@@ -47,3 +73,4 @@
int FileMgr::getlinenum() {
return linenum;
}
+#endif
Index: src/hunspell/filemgr.hxx
===================================================================
RCS file: /cvsroot/hunspell/hunspell/src/hunspell/filemgr.hxx,v
retrieving revision 1.3
diff -u -r1.3 filemgr.hxx
--- src/hunspell/filemgr.hxx 15 Apr 2010 11:22:08 -0000 1.3
+++ src/hunspell/filemgr.hxx 29 May 2014 01:05:07 -0000
@@ -7,6 +7,30 @@
#include "hunzip.hxx"
#include <stdio.h>
+#ifdef HUNSPELL_CHROME_CLIENT
+namespace hunspell {
+class LineIterator;
+} // namespace hunspell
+
+// A class which encapsulates operations of reading a BDICT file.
+// Chrome uses a BDICT file to compress hunspell dictionaries. A BDICT file is
+// a binary file converted from a DIC file and an AFF file. (See
+// "bdict_reader.h" for its format.)
+// This class encapsulates the operations of reading a BDICT file and emulates
+// the original FileMgr operations for AffixMgr so that it can read a BDICT
+// file without so many changes.
+class FileMgr {
+ public:
+ FileMgr(hunspell::LineIterator* iterator);
+ ~FileMgr();
+ char* getline();
+ int getlinenum();
+
+ protected:
+ hunspell::LineIterator* iterator_;
+ char line_[BUFSIZE + 50]; // input buffer
+};
+#else
class LIBHUNSPELL_DLL_EXPORTED FileMgr
{
protected:
@@ -23,3 +47,4 @@
int getlinenum();
};
#endif
+#endif
Index: src/hunspell/hashmgr.cxx
===================================================================
RCS file: /cvsroot/hunspell/hunspell/src/hunspell/hashmgr.cxx,v
retrieving revision 1.12
diff -u -r1.12 hashmgr.cxx
--- src/hunspell/hashmgr.cxx 23 Jun 2011 09:21:50 -0000 1.12
+++ src/hunspell/hashmgr.cxx 29 May 2014 01:05:07 -0000
@@ -12,8 +12,14 @@
// build a hash table from a munched word list
+#ifdef HUNSPELL_CHROME_CLIENT
+HashMgr::HashMgr(hunspell::BDictReader* reader)
+{
+ bdict_reader = reader;
+#else
HashMgr::HashMgr(const char * tpath, const char * apath, const char * key)
{
+#endif
tablesize = 0;
tableptr = NULL;
flag_mode = FLAG_CHAR;
@@ -31,8 +37,14 @@
numaliasm = 0;
aliasm = NULL;
forbiddenword = FORBIDDENWORD; // forbidden word signing flag
+#ifdef HUNSPELL_CHROME_CLIENT
+ // No tables to load, just the AF lines.
+ load_config(NULL, NULL);
+ int ec = LoadAFLines();
+#else
load_config(apath, key);
int ec = load_tables(tpath, key);
+#endif
if (ec) {
/* error condition - what should we do here */
HUNSPELL_WARNING(stderr, "Hash Manager Error : %d\n",ec);
@@ -91,15 +103,58 @@
if (ignorechars) free(ignorechars);
if (ignorechars_utf16) free(ignorechars_utf16);
+#ifdef HUNSPELL_CHROME_CLIENT
+ EmptyHentryCache();
+ for (std::vector<std::string*>::iterator it = pointer_to_strings_.begin();
+ it != pointer_to_strings_.end(); ++it) {
+ delete *it;
+ }
+#endif
#ifdef MOZILLA_CLIENT
delete [] csconv;
#endif
}
+#ifdef HUNSPELL_CHROME_CLIENT
+void HashMgr::EmptyHentryCache() {
+ // We need to delete each cache entry, and each additional one in the linked
+ // list of homonyms.
+ for (HEntryCache::iterator i = hentry_cache.begin();
+ i != hentry_cache.end(); ++i) {
+ hentry* cur = i->second;
+ while (cur) {
+ hentry* next = cur->next_homonym;
+ DeleteHashEntry(cur);
+ cur = next;
+ }
+ }
+ hentry_cache.clear();
+}
+#endif
+
// lookup a root word in the hashtable
struct hentry * HashMgr::lookup(const char *word) const
{
+#ifdef HUNSPELL_CHROME_CLIENT
+ int affix_ids[hunspell::BDict::MAX_AFFIXES_PER_WORD];
+ int affix_count = bdict_reader->FindWord(word, affix_ids);
+ if (affix_count == 0) { // look for custom added word
+ std::map<base::StringPiece, int>::const_iterator iter =
+ custom_word_to_affix_id_map_.find(word);
+ if (iter != custom_word_to_affix_id_map_.end()) {
+ affix_count = 1;
+ affix_ids[0] = iter->second;
+ }
+ }
+
+ static const int kMaxWordLen = 128;
+ static char word_buf[kMaxWordLen];
+ // To take account of null-termination, we use upto 127.
+ strncpy(word_buf, word, kMaxWordLen - 1);
+
+ return AffixIDsToHentry(word_buf, affix_ids, affix_count);
+#else
struct hentry * dp;
if (tableptr) {
dp = tableptr[hash(word)];
@@ -109,12 +164,14 @@
}
}
return NULL;
+#endif
}
// add a word to the hash table (private)
int HashMgr::add_word(const char * word, int wbl, int wcl, unsigned short * aff,
int al, const char * desc, bool onlyupcase)
{
+#ifndef HUNSPELL_CHROME_CLIENT
bool upcasehomonym = false;
int descl = desc ? (aliasm ? sizeof(short) : strlen(desc) + 1) : 0;
// variable-length hash record with word and optional fields
@@ -206,6 +263,17 @@
if (hp->astr) free(hp->astr);
free(hp);
}
+#else
+ std::map<base::StringPiece, int>::iterator iter =
+ custom_word_to_affix_id_map_.find(word);
+ if(iter == custom_word_to_affix_id_map_.end()) { // word needs to be added
+ std::string* new_string_word = new std::string(word);
+ pointer_to_strings_.push_back(new_string_word);
+ base::StringPiece sp(*(new_string_word));
+ custom_word_to_affix_id_map_[sp] = 0; // no affixes for custom words
+ return 1;
+ }
+#endif
return 0;
}
@@ -256,6 +324,12 @@
// remove word (personal dictionary function for standalone applications)
int HashMgr::remove(const char * word)
{
+#ifdef HUNSPELL_CHROME_CLIENT
+ std::map<base::StringPiece, int>::iterator iter =
+ custom_word_to_affix_id_map_.find(word);
+ if (iter != custom_word_to_affix_id_map_.end())
+ custom_word_to_affix_id_map_.erase(iter);
+#else
struct hentry * dp = lookup(word);
while (dp) {
if (dp->alen == 0 || !TESTAFF(dp->astr, forbiddenword, dp->alen)) {
@@ -270,6 +344,7 @@
}
dp = dp->next_homonym;
}
+#endif
return 0;
}
@@ -339,6 +414,44 @@
// initialize: col=-1; hp = NULL; hp = walk_hashtable(&col, hp);
struct hentry * HashMgr::walk_hashtable(int &col, struct hentry * hp) const
{
+#ifdef HUNSPELL_CHROME_CLIENT
+ // Return NULL if dictionary is not valid.
+ if (!bdict_reader->IsValid())
+ return NULL;
+
+ // This function is only ever called by one place and not nested. We can
+ // therefore keep static state between calls and use |col| as a "reset" flag
+ // to avoid changing the API. It is set to -1 for the first call.
+ // Allocate the iterator on the heap to prevent an exit time destructor.
+ static hunspell::WordIterator& word_iterator =
+ *new hunspell::WordIterator(bdict_reader->GetAllWordIterator());
+ if (col < 0) {
+ col = 1;
+ word_iterator = bdict_reader->GetAllWordIterator();
+ }
+
+ int affix_ids[hunspell::BDict::MAX_AFFIXES_PER_WORD];
+ static const int kMaxWordLen = 128;
+ static char word[kMaxWordLen];
+ int affix_count = word_iterator.Advance(word, kMaxWordLen, affix_ids);
+ if (affix_count == 0)
+ return NULL;
+ short word_len = static_cast<short>(strlen(word));
+
+ // Since hunspell 1.2.8, an hentry struct becomes a variable-length struct,
+ // i.e. a struct which uses its array 'word[1]' as a variable-length array.
+ // As noted above, this function is not nested. So, we just use a static
+ // struct which consists of an hentry and a char[kMaxWordLen], and initialize
+ // the static struct and return it for now.
+ // No need to create linked lists for the extra affixes.
+ static struct {
+ hentry entry;
+ char word[kMaxWordLen];
+ } hash_entry;
+
+ return InitHashEntry(&hash_entry.entry, sizeof(hash_entry),
+ &word[0], word_len, affix_ids[0]);
+#else
if (hp && hp->next != NULL) return hp->next;
for (col++; col < tablesize; col++) {
if (tableptr[col]) return tableptr[col];
@@ -346,11 +459,13 @@
// null at end and reset to start
col = -1;
return NULL;
+#endif
}
// load a munched word list and build a hash table on the fly
int HashMgr::load_tables(const char * tpath, const char * key)
{
+#ifndef HUNSPELL_CHROME_CLIENT
int al;
char * ap;
char * dp;
@@ -471,6 +586,7 @@
}
delete dict;
+#endif
return 0;
}
@@ -479,6 +595,9 @@
int HashMgr::hash(const char * word) const
{
+#ifdef HUNSPELL_CHROME_CLIENT
+ return 0;
+#else
long hv = 0;
for (int i=0; i < 4 && *word != 0; i++)
hv = (hv << 8) | (*word++);
@@ -487,6 +606,7 @@
hv ^= (*word++);
}
return (unsigned long) hv % tablesize;
+#endif
}
int HashMgr::decode_flags(unsigned short ** result, char * flags, FileMgr * af) {
@@ -607,7 +727,12 @@
int firstline = 1;
// open the affix file
+#ifdef HUNSPELL_CHROME_CLIENT
+ hunspell::LineIterator iterator = bdict_reader->GetOtherLineIterator();
+ FileMgr * afflst = new FileMgr(&iterator);
+#else
FileMgr * afflst = new FileMgr(affpath, key);
+#endif
if (!afflst) {
HUNSPELL_WARNING(stderr, "Error - could not open affix description file %s\n",affpath);
return 1;
@@ -802,6 +927,121 @@
return 0;
}
+#ifdef HUNSPELL_CHROME_CLIENT
+int HashMgr::LoadAFLines()
+{
+ utf8 = 1; // We always use UTF-8.
+
+ // Read in all the AF lines which tell us the rules for each affix group ID.
+ hunspell::LineIterator iterator = bdict_reader->GetAfLineIterator();
+ FileMgr afflst(&iterator);
+ while (char* line = afflst.getline()) {
+ int rv = parse_aliasf(line, &afflst);
+ if (rv)
+ return rv;
+ }
+
+ return 0;
+}
+
+hentry* HashMgr::InitHashEntry(hentry* entry,
+ size_t item_size,
+ const char* word,
+ int word_length,
+ int affix_index) const {
+ // Return if the given buffer doesn't have enough space for a hentry struct
+ // or the given word is too long.
+ // Our BDICT cannot handle words longer than (128 - 1) bytes. So, it is
+ // better to return an error if the given word is too long and prevent
+ // an unexpected result caused by a long word.
+ const int kMaxWordLen = 128;
+ if (item_size < sizeof(hentry) + word_length + 1 ||
+ word_length >= kMaxWordLen)
+ return NULL;
+
+ // Initialize a hentry struct with the given parameters, and
+ // append the given string at the end of this hentry struct.
+ memset(entry, 0, item_size);
+ FileMgr af(NULL);
+ entry->alen = static_cast<short>(
+ const_cast<HashMgr*>(this)->get_aliasf(affix_index, &entry->astr, &af));
+ entry->blen = static_cast<unsigned char>(word_length);
+ memcpy(&entry->word, word, word_length);
+
+ return entry;
+}
+
+hentry* HashMgr::CreateHashEntry(const char* word,
+ int word_length,
+ int affix_index) const {
+ // Return if the given word is too long.
+ // (See the comment in HashMgr::InitHashEntry().)
+ const int kMaxWordLen = 128;
+ if (word_length >= kMaxWordLen)
+ return NULL;
+
+ const size_t kEntrySize = sizeof(hentry) + word_length + 1;
+ struct hentry* entry = reinterpret_cast<hentry*>(malloc(kEntrySize));
+ if (entry)
+ InitHashEntry(entry, kEntrySize, word, word_length, affix_index);
+
+ return entry;
+}
+
+void HashMgr::DeleteHashEntry(hentry* entry) const {
+ free(entry);
+}
+
+hentry* HashMgr::AffixIDsToHentry(char* word,
+ int* affix_ids,
+ int affix_count) const
+{
+ if (affix_count == 0)
+ return NULL;
+
+ HEntryCache& cache = const_cast<HashMgr*>(this)->hentry_cache;
+ std::string std_word(word);
+ HEntryCache::iterator found = cache.find(std_word);
+ if (found != cache.end()) {
+ // We must return an existing hentry for the same word if we've previously
+ // handed one out. Hunspell will compare pointers in some cases to see if
+ // two words it has found are the same.
+ return found->second;
+ }
+
+ short word_len = static_cast<short>(strlen(word));
+
+ // We can get a number of prefixes per word. There will normally be only one,
+ // but if not, there will be a linked list of "hentry"s for the "homonym"s
+ // for the word.
+ struct hentry* first_he = NULL;
+ struct hentry* prev_he = NULL; // For making linked list.
+ for (int i = 0; i < affix_count; i++) {
+ struct hentry* he = CreateHashEntry(word, word_len, affix_ids[i]);
+ if (!he)
+ break;
+ if (i == 0)
+ first_he = he;
+ if (prev_he)
+ prev_he->next_homonym = he;
+ prev_he = he;
+ }
+
+ cache[std_word] = first_he; // Save this word in the cache for later.
+ return first_he;
+}
+
+hentry* HashMgr::GetHentryFromHEntryCache(char* word) {
+ HEntryCache& cache = const_cast<HashMgr*>(this)->hentry_cache;
+ std::string std_word(word);
+ HEntryCache::iterator found = cache.find(std_word);
+ if (found != cache.end())
+ return found->second;
+ else
+ return NULL;
+}
+#endif
+
int HashMgr::is_aliasf() {
return (aliasf != NULL);
}
Index: src/hunspell/hashmgr.hxx
===================================================================
RCS file: /cvsroot/hunspell/hunspell/src/hunspell/hashmgr.hxx,v
retrieving revision 1.3
diff -u -r1.3 hashmgr.hxx
--- src/hunspell/hashmgr.hxx 15 Apr 2010 11:22:08 -0000 1.3
+++ src/hunspell/hashmgr.hxx 29 May 2014 01:05:07 -0000
@@ -8,10 +8,25 @@
#include "htypes.hxx"
#include "filemgr.hxx"
+#ifdef HUNSPELL_CHROME_CLIENT
+#include <string>
+#include <map>
+
+#include "base/stl_util.h"
+#include "base/strings/string_piece.h"
+#include "third_party/hunspell/google/bdict_reader.h"
+#endif
+
enum flag { FLAG_CHAR, FLAG_LONG, FLAG_NUM, FLAG_UNI };
class LIBHUNSPELL_DLL_EXPORTED HashMgr
{
+#ifdef HUNSPELL_CHROME_CLIENT
+ // Not owned by this class, owned by the Hunspell object.
+ hunspell::BDictReader* bdict_reader;
+ std::map<base::StringPiece, int> custom_word_to_affix_id_map_;
+ std::vector<std::string*> pointer_to_strings_;
+#endif
int tablesize;
struct hentry ** tableptr;
int userword;
@@ -34,7 +49,23 @@
public:
+#ifdef HUNSPELL_CHROME_CLIENT
+ HashMgr(hunspell::BDictReader* reader);
+
+ // Return the hentry corresponding to the given word. Returns NULL if the
+ // word is not there in the cache.
+ hentry* GetHentryFromHEntryCache(char* word);
+
+ // Called before we do a new operation. This will empty the cache of pointers
+ // to hentries that we have cached. In Chrome, we make these on-demand, but
+ // they must live as long as the single spellcheck operation that they're part
+ // of since Hunspell will save pointers to various ones as it works.
+ //
+ // This function allows that cache to be emptied and not grow infinitely.
+ void EmptyHentryCache();
+#else
HashMgr(const char * tpath, const char * apath, const char * key = NULL);
+#endif
~HashMgr();
struct hentry * lookup(const char *) const;
@@ -59,6 +90,40 @@
int al, const char * desc, bool onlyupcase);
int load_config(const char * affpath, const char * key);
int parse_aliasf(char * line, FileMgr * af);
+
+#ifdef HUNSPELL_CHROME_CLIENT
+ // Loads the AF lines from a BDICT.
+ // A BDICT file compresses its AF lines to save memory.
+ // This function decompresses each AF line and call parse_aliasf().
+ int LoadAFLines();
+
+ // Helper functions that create a new hentry struct, initialize it, and
+ // delete it.
+ // These functions encapsulate non-trivial operations in creating and
+ // initializing a hentry struct from BDICT data to avoid changing code so much
+ // even when a hentry struct is changed.
+ hentry* InitHashEntry(hentry* entry,
+ size_t item_size,
+ const char* word,
+ int word_length,
+ int affix_index) const;
+ hentry* CreateHashEntry(const char* word,
+ int word_length,
+ int affix_index) const;
+ void DeleteHashEntry(hentry* entry) const;
+
+ // Converts the list of affix IDs to a linked list of hentry structures. The
+ // hentry structures will point to the given word. The returned pointer will
+ // be a statically allocated variable that will change for the next call. The
+ // |word| buffer must be the same.
+ hentry* AffixIDsToHentry(char* word, int* affix_ids, int affix_count) const;
+
+ // See EmptyHentryCache above. Note that each one is actually a linked list
+ // followed by the homonym pointer.
+ typedef std::map<std::string, hentry*> HEntryCache;
+ HEntryCache hentry_cache;
+#endif
+
int add_hidden_capitalized_word(char * word, int wbl, int wcl,
unsigned short * flags, int al, char * dp, int captype);
int parse_aliasm(char * line, FileMgr * af);
Index: src/hunspell/htypes.hxx
===================================================================
RCS file: /cvsroot/hunspell/hunspell/src/hunspell/htypes.hxx,v
retrieving revision 1.3
diff -u -r1.3 htypes.hxx
--- src/hunspell/htypes.hxx 6 Sep 2010 07:58:53 -0000 1.3
+++ src/hunspell/htypes.hxx 29 May 2014 01:05:07 -0000
@@ -1,6 +1,16 @@
#ifndef _HTYPES_HXX_
#define _HTYPES_HXX_
+#ifdef HUNSPELL_CHROME_CLIENT
+// This is a workaround for preventing errors in parsing Turkish BDICs, which
+// contain very long AF lines (~ 12,000 chars).
+// TODO(hbono) change the HashMgr::parse_aliasf() function to be able to parse
+// longer lines than MAXDELEN.
+#define MAXDELEN (8192 * 2)
+#else
+#define MAXDELEN 8192
+#endif // HUNSPELL_CHROME_CLIENT
+
#define ROTATE_LEN 5
#define ROTATE(v,q) \
Index: src/hunspell/hunspell.cxx
===================================================================
RCS file: /cvsroot/hunspell/hunspell/src/hunspell/hunspell.cxx,v
retrieving revision 1.29
diff -u -r1.29 hunspell.cxx
--- src/hunspell/hunspell.cxx 23 Jun 2011 09:21:50 -0000 1.29
+++ src/hunspell/hunspell.cxx 29 May 2014 01:05:07 -0000
@@ -7,20 +7,37 @@
#include "hunspell.hxx"
#include "hunspell.h"
+#ifndef HUNSPELL_CHROME_CLIENT
#ifndef MOZILLA_CLIENT
# include "config.h"
#endif
+#endif
#include "csutil.hxx"
+#ifdef HUNSPELL_CHROME_CLIENT
+Hunspell::Hunspell(const unsigned char* bdict_data, size_t bdict_length)
+#else
Hunspell::Hunspell(const char * affpath, const char * dpath, const char * key)
+#endif
{
encoding = NULL;
csconv = NULL;
utf8 = 0;
complexprefixes = 0;
+#ifndef HUNSPELL_CHROME_CLIENT
affixpath = mystrdup(affpath);
+#endif
maxdic = 0;
+#ifdef HUNSPELL_CHROME_CLIENT
+ bdict_reader = new hunspell::BDictReader;
+ bdict_reader->Init(bdict_data, bdict_length);
+
+ pHMgr[0] = new HashMgr(bdict_reader);
+ if (pHMgr[0]) maxdic = 1;
+
+ pAMgr = new AffixMgr(bdict_reader, pHMgr, &maxdic);
+#else
/* first set up the hash manager */
pHMgr[0] = new HashMgr(dpath, affpath, key);
if (pHMgr[0]) maxdic = 1;
@@ -28,6 +45,7 @@
/* next set up the affix manager */
/* it needs access to the hash manager lookup methods */
pAMgr = new AffixMgr(affpath, pHMgr, &maxdic, key);
+#endif
/* get the preferred try string and the dictionary */
/* encoding from the Affix Manager for that dictionary */
@@ -41,7 +59,11 @@
wordbreak = pAMgr->get_breaktable();
/* and finally set up the suggestion manager */
+#ifdef HUNSPELL_CHROME_CLIENT
+ pSMgr = new SuggestMgr(bdict_reader, try_string, MAXSUGGESTION, pAMgr);
+#else
pSMgr = new SuggestMgr(try_string, MAXSUGGESTION, pAMgr);
+#endif
if (try_string) free(try_string);
}
@@ -59,10 +81,16 @@
csconv= NULL;
if (encoding) free(encoding);
encoding = NULL;
+#ifdef HUNSPELL_CHROME_CLIENT
+ if (bdict_reader) delete bdict_reader;
+ bdict_reader = NULL;
+#else
if (affixpath) free(affixpath);
affixpath = NULL;
+#endif
}
+#ifndef HUNSPELL_CHROME_CLIENT
// load extra dictionaries
int Hunspell::add_dic(const char * dpath, const char * key) {
if (maxdic == MAXDIC || !affixpath) return 1;
@@ -70,6 +98,7 @@
if (pHMgr[maxdic]) maxdic++; else return 1;
return 0;
}
+#endif
// make a copy of src at destination while removing all leading
// blanks and removing any trailing periods after recording
@@ -322,6 +351,9 @@
int Hunspell::spell(const char * word, int * info, char ** root)
{
+#ifdef HUNSPELL_CHROME_CLIENT
+ if (pHMgr[0]) pHMgr[0]->EmptyHentryCache();
+#endif
struct hentry * rv=NULL;
// need larger vector. For example, Turkish capital letter I converted a
// 2-byte UTF-8 character (dotless i) by mkallsmall.
@@ -586,6 +618,13 @@
if (!len)
return NULL;
+#ifdef HUNSPELL_CHROME_CLIENT
+ // We need to check if the word length is valid to make coverity (Event
+ // fixed_size_dest: Possible overrun of N byte fixed size buffer) happy.
+ if ((utf8 && strlen(word) >= MAXWORDUTF8LEN) || (!utf8 && strlen(word) >= MAXWORDLEN))
+ return NULL;
+#endif
+
// word reversing wrapper for complex prefixes
if (complexprefixes) {
if (word != w2) {
@@ -675,6 +714,9 @@
int Hunspell::suggest(char*** slst, const char * word)
{
+#ifdef HUNSPELL_CHROME_CLIENT
+ if (pHMgr[0]) pHMgr[0]->EmptyHentryCache();
+#endif
int onlycmpdsug = 0;
char cw[MAXWORDUTF8LEN];
char wspace[MAXWORDUTF8LEN];
@@ -1921,13 +1963,21 @@
Hunhandle *Hunspell_create(const char * affpath, const char * dpath)
{
+#ifdef HUNSPELL_CHROME_CLIENT
+ return NULL;
+#else
return (Hunhandle*)(new Hunspell(affpath, dpath));
+#endif
}
Hunhandle *Hunspell_create_key(const char * affpath, const char * dpath,
const char * key)
{
+#ifdef HUNSPELL_CHROME_CLIENT
+ return NULL;
+#else
return (Hunhandle*)(new Hunspell(affpath, dpath, key));
+#endif
}