1 /*
2    Copyright (c) 2015 Oracle and/or its affiliates. All rights reserved.
3 
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License, version 2.0,
6    as published by the Free Software Foundation.
7 
8    This program is also distributed with certain software (including
9    but not limited to OpenSSL) that is licensed under separate terms,
10    as designated in a particular file or component or in included license
11    documentation.  The authors of MySQL hereby grant you an additional
12    permission to link the program and your derivative works with the
13    separately licensed software that they have included with MySQL.
14 
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License, version 2.0, for more details.
19 
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software Foundation,
22    51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */
23 
24 #include "lex_hash.h"
25 #include "sql_lex_hash.h"
26 #include "lex.h"
27 
28 const Lex_hash Lex_hash::sql_keywords(sql_keywords_map, sql_keywords_max_len);
29 const Lex_hash Lex_hash::sql_keywords_and_funcs(sql_keywords_and_funcs_map,
30                                                 sql_keywords_and_funcs_max_len);
31 
32 const Lex_hash Lex_hash::hint_keywords(hint_keywords_map,
33                                        hint_keywords_max_len);
34 
35 /*
36   The following data is based on the latin1 character set, and is only
37   used when comparing keywords
38 */
39 
40 static const uchar to_upper_lex[]=
41 {
42     0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15,
43    16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
44    32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
45    48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
46    64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
47    80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
48    96, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
49    80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90,123,124,125,126,127,
50   128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
51   144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,
52   160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,
53   176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,
54   192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,
55   208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,
56   192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,
57   208,209,210,211,212,213,214,247,216,217,218,219,220,221,222,255
58 };
59 
60 
lex_casecmp(const char * s,const char * t,uint len)61 inline int lex_casecmp(const char *s, const char *t, uint len)
62 {
63   while (len-- != 0 &&
64          to_upper_lex[(uchar) *s++] == to_upper_lex[(uchar) *t++]) ;
65   return (int) len + 1;
66 }
67 
68 
get_hash_symbol(const char * s,unsigned int len) const69 const SYMBOL *Lex_hash::get_hash_symbol(const char *s, unsigned int len) const
70 {
71   const char *cur_str= s;
72 
73   if (len == 0)
74   {
75     DBUG_PRINT("warning",
76                ("get_hash_symbol() received a request for a zero-length symbol,"
77                 " which is probably a mistake."));
78     return NULL;
79   }
80 
81   if (len > entry_max_len)
82     return NULL;
83 
84   uint32 cur_struct= uint4korr(hash_map + ((len - 1) * 4));
85 
86   for (;;)
87   {
88     uchar first_char= (uchar) cur_struct;
89 
90     if (first_char == 0)
91     {
92       uint16 ires= (uint16) (cur_struct >> 16);
93       if (ires == array_elements(symbols))
94         return NULL;
95       const SYMBOL *res= symbols + ires;
96       uint count= (uint) (cur_str - s);
97       return lex_casecmp(cur_str, res->name + count, len - count) ? NULL : res;
98     }
99 
100     uchar cur_char= (uchar) to_upper_lex[(uchar) *cur_str];
101     if (cur_char < first_char)
102       return NULL;
103     cur_struct>>= 8;
104     if (cur_char > (uchar) cur_struct)
105       return NULL;
106 
107     cur_struct>>= 8;
108     cur_struct= uint4korr(hash_map +
109         (((uint16) cur_struct + cur_char - first_char) * 4));
110     cur_str++;
111   }
112 }
113 
114