1 #include "license.hunspell"
2 #include "license.myspell"
10 #include "hashmgr.hxx"
14 // build a hash table from a munched word list
16 HashMgr::HashMgr(const char * tpath, const char * apath, const char * key)
20 , flag_mode(FLAG_CHAR)
23 , forbiddenword(FORBIDDENWORD) // forbidden word signing flag
35 ignorechars_utf16 = NULL;
36 ignorechars_utf16_len = 0;
37 load_config(apath, key);
38 int ec = load_tables(tpath, key);
40 /* error condition - what should we do here */
41 HUNSPELL_WARNING(stderr, "Hash Manager Error : %d\n",ec);
54 // now pass through hash table freeing up everything
55 // go through column by column of the table
56 for (int i=0; i < tablesize; i++) {
57 struct hentry * pt = tableptr[i];
58 struct hentry * nt = NULL;
61 if (pt->astr && (!aliasf || TESTAFF(pt->astr, ONLYUPCASEFLAG, pt->alen))) free(pt->astr);
71 for (int j = 0; j < (numaliasf); j++) free(aliasf[j]);
80 for (int j = 0; j < (numaliasm); j++) free(aliasm[j]);
86 #ifndef MOZILLA_CLIENT
87 if (utf8) free_utf_tbl();
94 if (ignorechars) free(ignorechars);
95 if (ignorechars_utf16) free(ignorechars_utf16);
102 // lookup a root word in the hashtable
104 struct hentry * HashMgr::lookup(const char *word) const
108 dp = tableptr[hash(word)];
109 if (!dp) return NULL;
110 for ( ; dp != NULL; dp = dp->next) {
111 if (strcmp(word, dp->word) == 0) return dp;
117 // add a word to the hash table (private)
118 int HashMgr::add_word(const char * word, int wbl, int wcl, unsigned short * aff,
119 int al, const char * desc, bool onlyupcase)
121 bool upcasehomonym = false;
122 int descl = desc ? (aliasm ? sizeof(char *) : strlen(desc) + 1) : 0;
123 // variable-length hash record with word and optional fields
125 (struct hentry *) malloc (sizeof(struct hentry) + wbl + descl);
127 char * hpw = hp->word;
129 if (ignorechars != NULL) {
131 remove_ignored_chars_utf(hpw, ignorechars_utf16, ignorechars_utf16_len);
133 remove_ignored_chars(hpw, ignorechars);
136 if (complexprefixes) {
137 if (utf8) reverseword_utf(hpw); else reverseword(hpw);
142 hp->blen = (unsigned char) wbl;
143 hp->clen = (unsigned char) wcl;
144 hp->alen = (short) al;
147 hp->next_homonym = NULL;
149 // store the description string or its pointer
153 hp->var += H_OPT_ALIASM;
154 store_pointer(hpw + wbl + 1, get_aliasm(atoi(desc)));
156 strcpy(hpw + wbl + 1, desc);
157 if (complexprefixes) {
158 if (utf8) reverseword_utf(HENTRY_DATA(hp));
159 else reverseword(HENTRY_DATA(hp));
162 if (strstr(HENTRY_DATA(hp), MORPH_PHON)) hp->var += H_OPT_PHON;
165 struct hentry * dp = tableptr[i];
170 while (dp->next != NULL) {
171 if ((!dp->next_homonym) && (strcmp(hp->word, dp->word) == 0)) {
172 // remove hidden onlyupcase homonym
174 if ((dp->astr) && TESTAFF(dp->astr, ONLYUPCASEFLAG, dp->alen)) {
181 dp->next_homonym = hp;
184 upcasehomonym = true;
189 if (strcmp(hp->word, dp->word) == 0) {
190 // remove hidden onlyupcase homonym
192 if ((dp->astr) && TESTAFF(dp->astr, ONLYUPCASEFLAG, dp->alen)) {
199 dp->next_homonym = hp;
202 upcasehomonym = true;
205 if (!upcasehomonym) {
208 // remove hidden onlyupcase homonym
209 if (hp->astr) free(hp->astr);
215 int HashMgr::add_hidden_capitalized_word(char * word, int wbl, int wcl,
216 unsigned short * flags, int flagslen, char * dp, int captype)
221 // add inner capitalized forms to handle the following allcap forms:
222 // Mixed caps: OpenOffice.org -> OPENOFFICE.ORG
223 // Allcaps with suffixes: CIA's -> CIA'S
224 if (((captype == HUHCAP) || (captype == HUHINITCAP) ||
225 ((captype == ALLCAP) && (flagslen != 0))) &&
226 !((flagslen != 0) && TESTAFF(flags, forbiddenword, flagslen))) {
227 unsigned short * flags2 = (unsigned short *) malloc (sizeof(unsigned short) * (flagslen+1));
228 if (!flags2) return 1;
229 if (flagslen) memcpy(flags2, flags, flagslen * sizeof(unsigned short));
230 flags2[flagslen] = ONLYUPCASEFLAG;
234 int wlen = u8_u16(w, BUFSIZE, word);
235 mkallsmall_utf(w, wlen, langnum);
236 mkallcap_utf(w, 1, langnum);
237 u16_u8(st, BUFSIZE, w, wlen);
238 return add_word(st,wbl,wcl,flags2,flagslen+1,dp, true);
240 mkallsmall(word, csconv);
241 mkinitcap(word, csconv);
242 return add_word(word,wbl,wcl,flags2,flagslen+1,dp, true);
248 // detect captype and modify word length for UTF-8 encoding
249 int HashMgr::get_clen_and_captype(const char * word, int wbl, int * captype) {
252 w_char dest_utf[BUFSIZE];
253 len = u8_u16(dest_utf, BUFSIZE, word);
254 *captype = get_captype_utf8(dest_utf, len, langnum);
257 *captype = get_captype((char *) word, len, csconv);
262 // remove word (personal dictionary function for standalone applications)
263 int HashMgr::remove(const char * word)
265 struct hentry * dp = lookup(word);
267 if (dp->alen == 0 || !TESTAFF(dp->astr, forbiddenword, dp->alen)) {
268 unsigned short * flags =
269 (unsigned short *) malloc(sizeof(short) * (dp->alen + 1));
270 if (!flags) return 1;
271 for (int i = 0; i < dp->alen; i++) flags[i] = dp->astr[i];
272 flags[dp->alen] = forbiddenword;
275 flag_qsort(flags, 0, dp->alen);
277 dp = dp->next_homonym;
282 /* remove forbidden flag to add a personal word to the hash */
283 int HashMgr::remove_forbidden_flag(const char * word) {
284 struct hentry * dp = lookup(word);
287 if (dp->astr && TESTAFF(dp->astr, forbiddenword, dp->alen)) {
288 if (dp->alen == 1) dp->alen = 0; // XXX forbidden words of personal dic.
290 unsigned short * flags2 =
291 (unsigned short *) malloc(sizeof(short) * (dp->alen - 1));
292 if (!flags2) return 1;
294 for (i = 0; i < dp->alen; i++) {
295 if (dp->astr[i] != forbiddenword) flags2[j++] = dp->astr[i];
298 dp->astr = flags2; // XXX allowed forbidden words
301 dp = dp->next_homonym;
306 // add a custom dic. word to the hash table (public)
307 int HashMgr::add(const char * word)
309 unsigned short * flags = NULL;
311 if (remove_forbidden_flag(word)) {
313 int wbl = strlen(word);
314 int wcl = get_clen_and_captype(word, wbl, &captype);
315 add_word(word, wbl, wcl, flags, al, NULL, false);
316 return add_hidden_capitalized_word((char *) word, wbl, wcl, flags, al, NULL, captype);
321 int HashMgr::add_with_affix(const char * word, const char * example)
323 // detect captype and modify word length for UTF-8 encoding
324 struct hentry * dp = lookup(example);
325 remove_forbidden_flag(word);
326 if (dp && dp->astr) {
328 int wbl = strlen(word);
329 int wcl = get_clen_and_captype(word, wbl, &captype);
331 add_word(word, wbl, wcl, dp->astr, dp->alen, NULL, false);
333 unsigned short * flags = (unsigned short *) malloc (dp->alen * sizeof(short));
335 memcpy((void *) flags, (void *) dp->astr, dp->alen * sizeof(short));
336 add_word(word, wbl, wcl, flags, dp->alen, NULL, false);
339 return add_hidden_capitalized_word((char *) word, wbl, wcl, dp->astr, dp->alen, NULL, captype);
344 // walk the hash table entry by entry - null at end
345 // initialize: col=-1; hp = NULL; hp = walk_hashtable(&col, hp);
346 struct hentry * HashMgr::walk_hashtable(int &col, struct hentry * hp) const
348 if (hp && hp->next != NULL) return hp->next;
349 for (col++; col < tablesize; col++) {
350 if (tableptr[col]) return tableptr[col];
352 // null at end and reset to start
357 // load a munched word list and build a hash table on the fly
358 int HashMgr::load_tables(const char * tpath, const char * key)
364 unsigned short * flags;
367 // open dictionary file
368 FileMgr * dict = new FileMgr(tpath, key);
369 if (dict == NULL) return 1;
371 // first read the first line of file to get hash table size */
372 if ((ts = dict->getline()) == NULL) {
373 HUNSPELL_WARNING(stderr, "error: empty dic file %s\n", tpath);
379 /* remove byte order mark */
380 if (strncmp(ts,"\xEF\xBB\xBF",3) == 0) {
381 memmove(ts, ts+3, strlen(ts+3)+1);
382 // warning: dic file begins with byte order mark: possible incompatibility with old Hunspell versions
385 tablesize = atoi(ts);
387 int nExtra = 5 + USERWORD;
389 if (tablesize <= 0 || (tablesize >= (std::numeric_limits<int>::max() - 1 - nExtra) / int(sizeof(struct hentry *)))) {
390 HUNSPELL_WARNING(stderr, "error: line 1: missing or bad word count in the dic file\n");
395 if ((tablesize % 2) == 0) tablesize++;
397 // allocate the hash table
398 tableptr = (struct hentry **) calloc(tablesize, sizeof(struct hentry *));
404 // loop through all words on much list and add to hash
405 // table and create word and affix strings
407 while ((ts = dict->getline()) != NULL) {
409 // split each line into word and morphological description
411 while ((dp = strchr(dp, ':')) != NULL) {
412 if ((dp > ts + 3) && (*(dp - 3) == ' ' || *(dp - 3) == '\t')) {
413 for (dp -= 4; dp >= ts && (*dp == ' ' || *dp == '\t'); dp--);
414 if (dp < ts) { // missing word
425 // tabulator is the old morphological field separator
426 dp2 = strchr(ts, '\t');
427 if (dp2 && (!dp || dp2 < dp)) {
432 // split each line into word and affix char strings
433 // "\/" signs slash in words (not affix separator)
434 // "/" at beginning of the line is word character (not affix separator)
440 } else if (*(ap - 1) != '\\') break;
441 // replace "\/" with "/"
442 for (char * sp = ap - 1; *sp; *sp = *(sp + 1), sp++);
449 int index = atoi(ap + 1);
450 al = get_aliasf(index, &flags, dict);
452 HUNSPELL_WARNING(stderr, "error: line %d: bad flag vector alias\n", dict->getlinenum());
456 al = decode_flags(&flags, ap + 1, dict);
458 HUNSPELL_WARNING(stderr, "Can't allocate memory.\n");
462 flag_qsort(flags, 0, al);
471 int wbl = strlen(ts);
472 int wcl = get_clen_and_captype(ts, wbl, &captype);
473 // add the word and its index plus its capitalized form optionally
474 if (add_word(ts,wbl,wcl,flags,al,dp, false) ||
475 add_hidden_capitalized_word(ts, wbl, wcl, flags, al, dp, captype)) {
485 // the hash function is a simple load and rotate
486 // algorithm borrowed
488 int HashMgr::hash(const char * word) const
491 for (int i=0; i < 4 && *word != 0; i++)
492 hv = (hv << 8) | (*word++);
494 ROTATE(hv,ROTATE_LEN);
497 return (unsigned long) hv % tablesize;
500 int HashMgr::decode_flags(unsigned short ** result, char * flags, FileMgr * af) {
502 if (*flags == '\0') {
507 case FLAG_LONG: { // two-character flags (1x2yZz -> 1x 2y Zz)
509 if (len%2 == 1) HUNSPELL_WARNING(stderr, "error: line %d: bad flagvector\n", af->getlinenum());
511 *result = (unsigned short *) malloc(len * sizeof(short));
512 if (!*result) return -1;
513 for (int i = 0; i < len; i++) {
514 (*result)[i] = (((unsigned short) flags[i * 2]) << 8) + (unsigned short) flags[i * 2 + 1];
518 case FLAG_NUM: { // decimal numbers separated by comma (4521,23,233 -> 4521 23 233)
522 unsigned short * dest;
524 for (p = flags; *p; p++) {
525 if (*p == ',') len++;
527 *result = (unsigned short *) malloc(len * sizeof(short));
528 if (!*result) return -1;
530 for (p = flags; *p; p++) {
533 if (i >= DEFAULTFLAGS) HUNSPELL_WARNING(stderr, "error: line %d: flag id %d is too large (max: %d)\n",
534 af->getlinenum(), i, DEFAULTFLAGS - 1);
535 *dest = (unsigned short) i;
536 if (*dest == 0) HUNSPELL_WARNING(stderr, "error: line %d: 0 is wrong flag id\n", af->getlinenum());
542 if (i >= DEFAULTFLAGS) HUNSPELL_WARNING(stderr, "error: line %d: flag id %d is too large (max: %d)\n",
543 af->getlinenum(), i, DEFAULTFLAGS - 1);
544 *dest = (unsigned short) i;
545 if (*dest == 0) HUNSPELL_WARNING(stderr, "error: line %d: 0 is wrong flag id\n", af->getlinenum());
548 case FLAG_UNI: { // UTF-8 characters
550 len = u8_u16(w, BUFSIZE/2, flags);
551 *result = (unsigned short *) malloc(len * sizeof(short));
552 if (!*result) return -1;
553 memcpy(*result, w, len * sizeof(short));
556 default: { // Ispell's one-character flags (erfg -> e r f g)
557 unsigned short * dest;
559 *result = (unsigned short *) malloc(len * sizeof(short));
560 if (!*result) return -1;
562 for (unsigned char * p = (unsigned char *) flags; *p; p++) {
563 *dest = (unsigned short) *p;
571 unsigned short HashMgr::decode_flag(const char * f) {
572 unsigned short s = 0;
576 s = ((unsigned short) f[0] << 8) + (unsigned short) f[1];
580 if (i >= DEFAULTFLAGS) HUNSPELL_WARNING(stderr, "error: flag id %d is too large (max: %d)\n", i, DEFAULTFLAGS - 1);
581 s = (unsigned short) i;
584 u8_u16((w_char *) &s, 1, f);
587 s = (unsigned short) *((unsigned char *)f);
589 if (s == 0) HUNSPELL_WARNING(stderr, "error: 0 is wrong flag id\n");
593 char * HashMgr::encode_flag(unsigned short f) {
594 unsigned char ch[10];
595 if (f==0) return mystrdup("(NULL)");
596 if (flag_mode == FLAG_LONG) {
597 ch[0] = (unsigned char) (f >> 8);
598 ch[1] = (unsigned char) (f - ((f >> 8) << 8));
600 } else if (flag_mode == FLAG_NUM) {
601 sprintf((char *) ch, "%d", f);
602 } else if (flag_mode == FLAG_UNI) {
603 u16_u8((char *) &ch, 10, (w_char *) &f, 1);
605 ch[0] = (unsigned char) (f);
608 return mystrdup((char *) ch);
611 // read in aff file and set flag mode
612 int HashMgr::load_config(const char * affpath, const char * key)
614 char * line; // io buffers
617 // open the affix file
618 FileMgr * afflst = new FileMgr(affpath, key);
620 HUNSPELL_WARNING(stderr, "Error - could not open affix description file %s\n",affpath);
624 // read in each line ignoring any that do not
625 // start with a known line type indicator
627 while ((line = afflst->getline()) != NULL) {
630 /* remove byte order mark */
633 if (strncmp(line,"\xEF\xBB\xBF",3) == 0) memmove(line, line+3, strlen(line+3)+1);
636 /* parse in the try string */
637 if ((strncmp(line,"FLAG",4) == 0) && isspace(line[4])) {
638 if (flag_mode != FLAG_CHAR) {
639 HUNSPELL_WARNING(stderr, "error: line %d: multiple definitions of the FLAG affix file parameter\n", afflst->getlinenum());
641 if (strstr(line, "long")) flag_mode = FLAG_LONG;
642 if (strstr(line, "num")) flag_mode = FLAG_NUM;
643 if (strstr(line, "UTF-8")) flag_mode = FLAG_UNI;
644 if (flag_mode == FLAG_CHAR) {
645 HUNSPELL_WARNING(stderr, "error: line %d: FLAG needs `num', `long' or `UTF-8' parameter\n", afflst->getlinenum());
648 if (strncmp(line,"FORBIDDENWORD",13) == 0) {
650 if (parse_string(line, &st, afflst->getlinenum())) {
654 forbiddenword = decode_flag(st);
657 if (strncmp(line, "SET", 3) == 0) {
658 if (parse_string(line, &enc, afflst->getlinenum())) {
662 if (strcmp(enc, "UTF-8") == 0) {
664 #ifndef OPENOFFICEORG
665 #ifndef MOZILLA_CLIENT
666 initialize_utf_tbl();
669 } else csconv = get_current_cs(enc);
671 if (strncmp(line, "LANG", 4) == 0) {
672 if (parse_string(line, &lang, afflst->getlinenum())) {
676 langnum = get_lang_num(lang);
679 /* parse in the ignored characters (for example, Arabic optional diacritics characters */
680 if (strncmp(line,"IGNORE",6) == 0) {
681 if (parse_array(line, &ignorechars, &ignorechars_utf16,
682 &ignorechars_utf16_len, utf8, afflst->getlinenum())) {
688 if ((strncmp(line,"AF",2) == 0) && isspace(line[2])) {
689 if (parse_aliasf(line, afflst)) {
695 if ((strncmp(line,"AM",2) == 0) && isspace(line[2])) {
696 if (parse_aliasm(line, afflst)) {
702 if (strncmp(line,"COMPLEXPREFIXES",15) == 0) complexprefixes = 1;
703 if (((strncmp(line,"SFX",3) == 0) || (strncmp(line,"PFX",3) == 0)) && isspace(line[3])) break;
705 if (csconv == NULL) csconv = get_current_cs(SPELL_ENCODING);
710 /* parse in the ALIAS table */
711 int HashMgr::parse_aliasf(char * line, FileMgr * af)
713 if (numaliasf != 0) {
714 HUNSPELL_WARNING(stderr, "error: line %d: multiple table definitions\n", af->getlinenum());
721 piece = mystrsep(&tp, 0);
723 if (*piece != '\0') {
725 case 0: { np++; break; }
727 numaliasf = atoi(piece);
732 HUNSPELL_WARNING(stderr, "error: line %d: bad entry number\n", af->getlinenum());
735 aliasf = (unsigned short **) malloc(numaliasf * sizeof(unsigned short *));
736 aliasflen = (unsigned short *) malloc(numaliasf * sizeof(short));
737 if (!aliasf || !aliasflen) {
739 if (aliasf) free(aliasf);
740 if (aliasflen) free(aliasflen);
752 piece = mystrsep(&tp, 0);
760 HUNSPELL_WARNING(stderr, "error: line %d: missing data\n", af->getlinenum());
764 /* now parse the numaliasf lines to read in the remainder of the table */
766 for (int j=0; j < numaliasf; j++) {
767 if ((nl = af->getline()) == NULL) return 1;
773 piece = mystrsep(&tp, 0);
775 if (*piece != '\0') {
778 if (strncmp(piece,"AF",2) != 0) {
784 HUNSPELL_WARNING(stderr, "error: line %d: table is corrupt\n", af->getlinenum());
790 aliasflen[j] = (unsigned short) decode_flags(&(aliasf[j]), piece, af);
791 flag_qsort(aliasf[j], 0, aliasflen[j]);
798 piece = mystrsep(&tp, 0);
806 HUNSPELL_WARNING(stderr, "error: line %d: table is corrupt\n", af->getlinenum());
813 int HashMgr::is_aliasf() {
814 return (aliasf != NULL);
817 int HashMgr::get_aliasf(int index, unsigned short ** fvec, FileMgr * af) {
818 if ((index > 0) && (index <= numaliasf)) {
819 *fvec = aliasf[index - 1];
820 return aliasflen[index - 1];
822 HUNSPELL_WARNING(stderr, "error: line %d: bad flag alias index: %d\n", af->getlinenum(), index);
827 /* parse morph alias definitions */
828 int HashMgr::parse_aliasm(char * line, FileMgr * af)
830 if (numaliasm != 0) {
831 HUNSPELL_WARNING(stderr, "error: line %d: multiple table definitions\n", af->getlinenum());
838 piece = mystrsep(&tp, 0);
840 if (*piece != '\0') {
842 case 0: { np++; break; }
844 numaliasm = atoi(piece);
846 HUNSPELL_WARNING(stderr, "error: line %d: bad entry number\n", af->getlinenum());
849 aliasm = (char **) malloc(numaliasm * sizeof(char *));
861 piece = mystrsep(&tp, 0);
867 HUNSPELL_WARNING(stderr, "error: line %d: missing data\n", af->getlinenum());
871 /* now parse the numaliasm lines to read in the remainder of the table */
873 for (int j=0; j < numaliasm; j++) {
874 if ((nl = af->getline()) == NULL) return 1;
879 piece = mystrsep(&tp, ' ');
881 if (*piece != '\0') {
884 if (strncmp(piece,"AM",2) != 0) {
885 HUNSPELL_WARNING(stderr, "error: line %d: table is corrupt\n", af->getlinenum());
894 // add the remaining of the line
897 tp = tp + strlen(tp);
899 if (complexprefixes) {
900 if (utf8) reverseword_utf(piece);
901 else reverseword(piece);
903 aliasm[j] = mystrdup(piece);
915 piece = mystrsep(&tp, ' ');
921 HUNSPELL_WARNING(stderr, "error: line %d: table is corrupt\n", af->getlinenum());
928 int HashMgr::is_aliasm() {
929 return (aliasm != NULL);
932 char * HashMgr::get_aliasm(int index) {
933 if ((index > 0) && (index <= numaliasm)) return aliasm[index - 1];
934 HUNSPELL_WARNING(stderr, "error: bad morph. alias index: %d\n", index);