1 /* Copyright (c) 2000, 2021, Oracle and/or its affiliates.
2 
3    This program is free software; you can redistribute it and/or modify
4    it under the terms of the GNU General Public License, version 2.0,
5    as published by the Free Software Foundation.
6 
7    This program is also distributed with certain software (including
8    but not limited to OpenSSL) that is licensed under separate terms,
9    as designated in a particular file or component or in included license
10    documentation.  The authors of MySQL hereby grant you an additional
11    permission to link the program and your derivative works with the
12    separately licensed software that they have included with MySQL.
13 
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License, version 2.0, for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */
22 
23 /* Written by Sergei A. Golubchik, who has a shared copyright to this code */
24 
25 #include "ftdefs.h"
26 #include "my_compare.h"
27 
28 
29 static CHARSET_INFO *ft_stopword_cs= NULL;
30 
31 
32 typedef struct st_ft_stopwords
33 {
34   const char * pos;
35   uint   len;
36 } FT_STOPWORD;
37 
38 static TREE *stopwords3=NULL;
39 
FT_STOPWORD_cmp(void * cmp_arg MY_ATTRIBUTE ((unused)),FT_STOPWORD * w1,FT_STOPWORD * w2)40 static int FT_STOPWORD_cmp(void* cmp_arg MY_ATTRIBUTE((unused)),
41 			   FT_STOPWORD *w1, FT_STOPWORD *w2)
42 {
43   return ha_compare_text(ft_stopword_cs,
44 			 (uchar *)w1->pos,w1->len,
45 			 (uchar *)w2->pos,w2->len,0,0);
46 }
47 
FT_STOPWORD_free(FT_STOPWORD * w,TREE_FREE action,void * arg MY_ATTRIBUTE ((unused)))48 static void FT_STOPWORD_free(FT_STOPWORD *w, TREE_FREE action,
49                              void *arg MY_ATTRIBUTE((unused)))
50 {
51   if (action == free_free)
52     my_free((void*)w->pos);
53 }
54 
ft_add_stopword(const char * w)55 static int ft_add_stopword(const char *w)
56 {
57   FT_STOPWORD sw;
58   return !w ||
59          (((sw.len= (uint) strlen(sw.pos=w)) >= ft_min_word_len) &&
60           (tree_insert(stopwords3, &sw, 0, stopwords3->custom_arg)==NULL));
61 }
62 
ft_init_stopwords()63 int ft_init_stopwords()
64 {
65   if (!stopwords3)
66   {
67     if (!(stopwords3=(TREE *)my_malloc(mi_key_memory_ft_stopwords,
68                                        sizeof(TREE),MYF(0))))
69       return -1;
70     init_tree(stopwords3,0,0,sizeof(FT_STOPWORD),(qsort_cmp2)&FT_STOPWORD_cmp,
71               0,
72               (ft_stopword_file ? (tree_element_free)&FT_STOPWORD_free : 0),
73               NULL);
74     /*
75       Stopword engine currently does not support tricky
76       character sets UCS2, UTF16, UTF32.
77       Use latin1 to compare stopwords in case of these character sets.
78       It's also fine to use latin1 with the built-in stopwords.
79     */
80     ft_stopword_cs= default_charset_info->mbminlen == 1 ?
81                     default_charset_info : &my_charset_latin1;
82   }
83 
84   if (ft_stopword_file)
85   {
86     File fd;
87     size_t len;
88     uchar *buffer, *start, *end;
89     FT_WORD w;
90     int error=-1;
91 
92     if (!*ft_stopword_file)
93       return 0;
94 
95     if ((fd=my_open(ft_stopword_file, O_RDONLY, MYF(MY_WME))) == -1)
96       return -1;
97     len= (size_t)my_seek(fd, 0L, MY_SEEK_END, MYF(0));
98     my_seek(fd, 0L, MY_SEEK_SET, MYF(0));
99     if (!(start=buffer=my_malloc(mi_key_memory_ft_stopwords,
100                                  len+1, MYF(MY_WME))))
101       goto err0;
102     len=my_read(fd, buffer, len, MYF(MY_WME));
103     end=start+len;
104     while (ft_simple_get_word(ft_stopword_cs, &start, end, &w, TRUE))
105     {
106       if (ft_add_stopword(my_strndup(mi_key_memory_ft_stopwords,
107                                      (char*) w.pos, w.len, MYF(0))))
108         goto err1;
109     }
110     error=0;
111 err1:
112     my_free(buffer);
113 err0:
114     my_close(fd, MYF(MY_WME));
115     return error;
116   }
117   else
118   {
119     /* compatibility mode: to be removed */
120     char **sws=(char **)ft_precompiled_stopwords;
121 
122     for (;*sws;sws++)
123     {
124       if (ft_add_stopword(*sws))
125         return -1;
126     }
127     ft_stopword_file="(built-in)"; /* for SHOW VARIABLES */
128   }
129   return 0;
130 }
131 
is_stopword(char * word,uint len)132 int is_stopword(char *word, uint len)
133 {
134   FT_STOPWORD sw;
135   sw.pos=word;
136   sw.len=len;
137   return tree_search(stopwords3,&sw, stopwords3->custom_arg) != NULL;
138 }
139 
140 
ft_free_stopwords()141 void ft_free_stopwords()
142 {
143   if (stopwords3)
144   {
145     delete_tree(stopwords3); /* purecov: inspected */
146     my_free(stopwords3);
147     stopwords3=0;
148   }
149   ft_stopword_file= 0;
150 }
151