1 /*-------------------------------------------------------------------------
2  *
3  * kwlookup.c
4  *	  Key word lookup for PostgreSQL
5  *
6  *
7  * Portions Copyright (c) 2003-2020, PgPool Global Development Group
8  * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
9  * Portions Copyright (c) 1994, Regents of the University of California
10  *
11  *
12  * IDENTIFICATION
13  *	  src/common/kwlookup.c
14  *
15  *-------------------------------------------------------------------------
16  */
17 
18 #include <string.h>
19 #include "kwlookup.h"
20 
21 
22 /*
23  * ScanKeywordLookup - see if a given word is a keyword
24  *
25  * The list of keywords to be matched against is passed as a ScanKeywordList.
26  *
27  * Returns the keyword number (0..N-1) of the keyword, or -1 if no match.
28  * Callers typically use the keyword number to index into information
29  * arrays, but that is no concern of this code.
30  *
31  * The match is done case-insensitively.  Note that we deliberately use a
32  * dumbed-down case conversion that will only translate 'A'-'Z' into 'a'-'z',
33  * even if we are in a locale where tolower() would produce more or different
34  * translations.  This is to conform to the SQL99 spec, which says that
35  * keywords are to be matched in this way even though non-keyword identifiers
36  * receive a different case-normalization mapping.
37  */
38 int
ScanKeywordLookup(const char * str,const ScanKeywordList * keywords)39 ScanKeywordLookup(const char *str,
40 				  const ScanKeywordList *keywords)
41 {
42 	size_t		len;
43 	int			h;
44 	const char *kw;
45 
46 	/*
47 	 * Reject immediately if too long to be any keyword.  This saves useless
48 	 * hashing and downcasing work on long strings.
49 	 */
50 	len = strlen(str);
51 	if (len > keywords->max_kw_len)
52 		return -1;
53 
54 	/*
55 	 * Compute the hash function.  We assume it was generated to produce
56 	 * case-insensitive results.  Since it's a perfect hash, we need only
57 	 * match to the specific keyword it identifies.
58 	 */
59 	h = keywords->hash(str, len);
60 
61 	/* An out-of-range result implies no match */
62 	if (h < 0 || h >= keywords->num_keywords)
63 		return -1;
64 
65 	/*
66 	 * Compare character-by-character to see if we have a match, applying an
67 	 * ASCII-only downcasing to the input characters.  We must not use
68 	 * tolower() since it may produce the wrong translation in some locales
69 	 * (eg, Turkish).
70 	 */
71 	kw = GetScanKeyword(h, keywords);
72 	while (*str != '\0')
73 	{
74 		char		ch = *str++;
75 
76 		if (ch >= 'A' && ch <= 'Z')
77 			ch += 'a' - 'A';
78 		if (ch != *kw++)
79 			return -1;
80 	}
81 	if (*kw != '\0')
82 		return -1;
83 
84 	/* Success! */
85 	return h;
86 }
87