summaryrefslogtreecommitdiff
path: root/src/backend/tsearch/ts_utils.c
blob: bb0a75ca85aa348f11302d5c60a1efee46ecce2f (plain)
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
/*-------------------------------------------------------------------------
 *
 * ts_utils.c
 *		various support functions
 *
 * Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
 *
 *
 * IDENTIFICATION
 *	  $PostgreSQL: pgsql/src/backend/tsearch/ts_utils.c,v 1.1 2007/08/21 01:11:18 tgl Exp $
 *
 *-------------------------------------------------------------------------
 */

#include "postgres.h"

#include <ctype.h>

#include "miscadmin.h"
#include "storage/fd.h"
#include "tsearch/ts_locale.h"
#include "tsearch/ts_public.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"


#define CS_WAITKEY	0
#define CS_INKEY	1
#define CS_WAITEQ	2
#define CS_WAITVALUE	3
#define CS_INVALUE	4
#define CS_IN2VALUE 5
#define CS_WAITDELIM	6
#define CS_INESC	7
#define CS_IN2ESC	8

static char *
nstrdup(char *ptr, int len)
{
	char	   *res = palloc(len + 1),
			   *cptr;

	memcpy(res, ptr, len);
	res[len] = '\0';
	cptr = ptr = res;
	while (*ptr)
	{
		if (t_iseq(ptr, '\\'))
			ptr++;
		COPYCHAR(cptr, ptr);
		cptr += pg_mblen(ptr);
		ptr += pg_mblen(ptr);
	}
	*cptr = '\0';

	return res;
}

/*
 * Parse a parameter string consisting of key = value clauses
 */
void
parse_keyvalpairs(text *in, Map ** m)
{
	Map		   *mptr;
	char	   *ptr = VARDATA(in),
			   *begin = NULL;
	char		num = 0;
	int			state = CS_WAITKEY;

	while (ptr - VARDATA(in) < VARSIZE(in) - VARHDRSZ)
	{
		if (t_iseq(ptr, ','))
			num++;
		ptr += pg_mblen(ptr);
	}

	*m = mptr = (Map *) palloc(sizeof(Map) * (num + 2));
	memset(mptr, 0, sizeof(Map) * (num + 2));
	ptr = VARDATA(in);
	while (ptr - VARDATA(in) < VARSIZE(in) - VARHDRSZ)
	{
		if (state == CS_WAITKEY)
		{
			if (t_isalpha(ptr))
			{
				begin = ptr;
				state = CS_INKEY;
			}
			else if (!t_isspace(ptr))
				ereport(ERROR,
						(errcode(ERRCODE_SYNTAX_ERROR),
						 errmsg("invalid parameter list format: \"%s\"",
								TextPGetCString(in))));
		}
		else if (state == CS_INKEY)
		{
			if (t_isspace(ptr))
			{
				mptr->key = nstrdup(begin, ptr - begin);
				state = CS_WAITEQ;
			}
			else if (t_iseq(ptr, '='))
			{
				mptr->key = nstrdup(begin, ptr - begin);
				state = CS_WAITVALUE;
			}
			else if (!t_isalpha(ptr))
				ereport(ERROR,
						(errcode(ERRCODE_SYNTAX_ERROR),
						 errmsg("invalid parameter list format: \"%s\"",
								TextPGetCString(in))));
		}
		else if (state == CS_WAITEQ)
		{
			if (t_iseq(ptr, '='))
				state = CS_WAITVALUE;
			else if (!t_isspace(ptr))
				ereport(ERROR,
						(errcode(ERRCODE_SYNTAX_ERROR),
						 errmsg("invalid parameter list format: \"%s\"",
								TextPGetCString(in))));
		}
		else if (state == CS_WAITVALUE)
		{
			if (t_iseq(ptr, '"'))
			{
				begin = ptr + 1;
				state = CS_INVALUE;
			}
			else if (!t_isspace(ptr))
			{
				begin = ptr;
				state = CS_IN2VALUE;
			}
		}
		else if (state == CS_INVALUE)
		{
			if (t_iseq(ptr, '"'))
			{
				mptr->value = nstrdup(begin, ptr - begin);
				mptr++;
				state = CS_WAITDELIM;
			}
			else if (t_iseq(ptr, '\\'))
				state = CS_INESC;
		}
		else if (state == CS_IN2VALUE)
		{
			if (t_isspace(ptr) || t_iseq(ptr, ','))
			{
				mptr->value = nstrdup(begin, ptr - begin);
				mptr++;
				state = (t_iseq(ptr, ',')) ? CS_WAITKEY : CS_WAITDELIM;
			}
			else if (t_iseq(ptr, '\\'))
				state = CS_INESC;
		}
		else if (state == CS_WAITDELIM)
		{
			if (t_iseq(ptr, ','))
				state = CS_WAITKEY;
			else if (!t_isspace(ptr))
				ereport(ERROR,
						(errcode(ERRCODE_SYNTAX_ERROR),
						 errmsg("invalid parameter list format: \"%s\"",
								TextPGetCString(in))));
		}
		else if (state == CS_INESC)
			state = CS_INVALUE;
		else if (state == CS_IN2ESC)
			state = CS_IN2VALUE;
		else
			elog(ERROR, "unrecognized parse_keyvalpairs state: %d", state);
		ptr += pg_mblen(ptr);
	}

	if (state == CS_IN2VALUE)
	{
		mptr->value = nstrdup(begin, ptr - begin);
		mptr++;
	}
	else if (!(state == CS_WAITDELIM || state == CS_WAITKEY))
		ereport(ERROR,
				(errcode(ERRCODE_SYNTAX_ERROR),
				 errmsg("invalid parameter list format: \"%s\"",
						TextPGetCString(in))));
}

/*
 * Given the base name and extension of a tsearch config file, return
 * its full path name.  The base name is assumed to be user-supplied,
 * and is checked to prevent pathname attacks.  The extension is assumed
 * to be safe.
 *
 * The result is a palloc'd string.
 */
char *
get_tsearch_config_filename(const char *basename,
							const char *extension)
{
	char		sharepath[MAXPGPATH];
	char	   *result;
	const char *p;

	/*
	 * We enforce that the basename is all alpha characters.  This may be
	 * overly restrictive, but we don't want to allow access to anything
	 * outside the tsearch_data directory, so for instance '/' *must* be
	 * rejected.  This is the same test used for timezonesets names.
	 */
	for (p = basename; *p; p++)
	{
		if (!isalpha((unsigned char) *p))
			ereport(ERROR,
					(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
					 errmsg("invalid text search configuration file name \"%s\"",
							basename)));
	}

	get_share_path(my_exec_path, sharepath);
	result = palloc(MAXPGPATH);
	snprintf(result, MAXPGPATH, "%s/tsearch_data/%s.%s",
			 sharepath, basename, extension);

	return result;
}

#define STOPBUFLEN	4096

void
readstoplist(char *in, StopList * s)
{
	char	  **stop = NULL;

	s->len = 0;
	if (in && *in)
	{
		char	   *filename = get_tsearch_config_filename(in, "stop");
		FILE	   *hin;
		char		buf[STOPBUFLEN];
		int			reallen = 0;
		int			line = 0;

		if ((hin = AllocateFile(filename, "r")) == NULL)
			ereport(ERROR,
					(errcode(ERRCODE_CONFIG_FILE_ERROR),
					 errmsg("could not open stopword file \"%s\": %m",
							filename)));

		while (fgets(buf, STOPBUFLEN, hin))
		{
			char	   *pbuf = buf;

			line++;
			while (*pbuf && !isspace(*pbuf))
				pbuf++;
			*pbuf = '\0';

			if (*buf == '\0')
				continue;

			if (!pg_verifymbstr(buf, strlen(buf), true))
			{
				FreeFile(hin);
				ereport(ERROR,
						(errcode(ERRCODE_CHARACTER_NOT_IN_REPERTOIRE),
						 errmsg("invalid multibyte encoding at line %d in file \"%s\"",
								line, filename)));
			}

			if (s->len >= reallen)
			{
				if (reallen == 0)
				{
					reallen = 16;
					stop = (char **) palloc(sizeof(char *) * reallen);
				}
				else
				{
					reallen *= 2;
					stop = (char **) repalloc((void *) stop, sizeof(char *) * reallen);
				}
			}


			if (s->wordop)
				stop[s->len] = s->wordop(buf);
			else
				stop[s->len] = pstrdup(buf);

			(s->len)++;
		}
		FreeFile(hin);
		pfree(filename);
	}

	s->stop = stop;
}

static int
comparestr(const void *a, const void *b)
{
	return strcmp(*(char **) a, *(char **) b);
}

void
sortstoplist(StopList * s)
{
	if (s->stop && s->len > 0)
		qsort(s->stop, s->len, sizeof(char *), comparestr);
}

bool
searchstoplist(StopList * s, char *key)
{
	return (s->stop && s->len > 0 &&
			bsearch(&key, s->stop, s->len,
					sizeof(char *), comparestr)) ? true : false;
}

char *
pnstrdup(const char *in, int len)
{
	char	   *out = palloc(len + 1);

	memcpy(out, in, len);
	out[len] = '\0';
	return out;
}