summaryrefslogtreecommitdiff
path: root/contrib/string/string_io.c
blob: 03b7169a54a68bf5c0a056f6634b9094a7c6864a (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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*
 * string_io.c --
 *
 * This file defines C-like input/output conversion routines for strings.
 *
 * Copyright (c) 1998, Massimo Dal Zotto <dz@cs.unitn.it>
 *
 * This file is distributed under the GNU General Public License
 * either version 2, or (at your option) any later version.
 */

#include <ctype.h>
#include <string.h>

#include "postgres.h"
#include "utils/elog.h"
#include "utils/palloc.h"
#include "utils/builtins.h"

#include "string_io.h"

/* define this if you want to see iso-8859 characters */
#define ISO8859

#define MIN(x, y)	((x) < (y) ? (x) : (y))
#define VALUE(char)		((char) - '0')
#define DIGIT(val)	((val) + '0')
#define ISOCTAL(c)	(((c) >= '0') && ((c) <= '7'))
#ifndef ISO8859
#define NOTPRINTABLE(c) (!isprint(c))
#else
#define NOTPRINTABLE(c) (!isprint(c) && ((c) < 0xa0))
#endif

/*
 * string_output() --
 *
 * This function takes a pointer to a string data and an optional
 * data size and returns a printable representation of the string
 * translating all escape sequences to C-like \nnn or \c escapes.
 * The function is used by output methods of various string types.
 *
 * Arguments:
 *	data -		input data (can be NULL)
 *	size -		optional size of data. A negative value indicates
 *				that data is a null terminated string.
 *
 * Returns:
 *	a pointer to a new string containing the printable
 *	representation of data.
 */

char *
string_output(char *data, int size)
{
	register unsigned char c,
			   *p,
			   *r,
			   *result;
	register int l,
				len;

	if (data == NULL)
	{
		result = (char *) palloc(2);
		result[0] = '-';
		result[1] = '\0';
		return (result);
	}

	if (size < 0)
		size = strlen(data);

	/* adjust string length for escapes */
	len = size;
	for (p = data, l = size; l > 0; p++, l--)
	{
		switch (*p)
		{
			case '\\':
			case '"':
			case '{':
			case '}':
			case '\b':
			case '\f':
			case '\n':
			case '\r':
			case '\t':
			case '\v':
				len++;
				break;
			default:
				if (NOTPRINTABLE(*p))
					len += 3;
		}
	}
	len++;

	result = (char *) palloc(len);

	for (p = data, r = result, l = size; (l > 0) && (c = *p); p++, l--)
	{
		switch (c)
		{
			case '\\':
			case '"':
			case '{':
			case '}':
				*r++ = '\\';
				*r++ = c;
				break;
			case '\b':
				*r++ = '\\';
				*r++ = 'b';
				break;
			case '\f':
				*r++ = '\\';
				*r++ = 'f';
				break;
			case '\n':
				*r++ = '\\';
				*r++ = 'n';
				break;
			case '\r':
				*r++ = '\\';
				*r++ = 'r';
				break;
			case '\t':
				*r++ = '\\';
				*r++ = 't';
				break;
			case '\v':
				*r++ = '\\';
				*r++ = 'v';
				break;
			default:
				if (NOTPRINTABLE(c))
				{
					*r = '\\';
					r += 3;
					*r-- = DIGIT(c & 07);
					c >>= 3;
					*r-- = DIGIT(c & 07);
					c >>= 3;
					*r = DIGIT(c & 03);
					r += 3;
				}
				else
					*r++ = c;
		}
	}
	*r = '\0';

	return ((char *) result);
}

/*
 * string_input() --
 *
 * This function accepts a C string in input and copies it into a new
 * object allocated with palloc() translating all escape sequences.
 * An optional header can be allocatd before the string, for example
 * to hold the length of a varlena object.
 * This function is not necessary for input from sql commands because
 * the parser already does escape translation, all data input routines
 * receive strings in internal form.
 *
 * Arguments:
 *	str -		input string possibly with escapes
 *	size -		the required size of new data. A value of 0
 *				indicates a variable size string, while a
 *				negative value indicates a variable size string
 *				of size not greater than this absolute value.
 *	hdrsize -	size of an optional header to be allocated before
 *				the data. It must then be filled by the caller.
 *	rtn_size -	an optional pointer to an int variable where the
 *				size of the new string is stored back.
 *
 * Returns:
 *	a pointer to the new string or the header.
 */

char *
string_input(char *str, int size, int hdrsize, int *rtn_size)
{
	register unsigned char *p,
			   *r;
	unsigned char *result;
	int			len;

	if ((str == NULL) || (hdrsize < 0))
		return (char *) NULL;

	/* Compute result size */
	len = strlen(str);
	for (p = str; *p;)
	{
		if (*p++ == '\\')
		{
			if (ISOCTAL(*p))
			{
				if (ISOCTAL(*(p + 1)))
				{
					p++;
					len--;
				}
				if (ISOCTAL(*(p + 1)))
				{
					p++;
					len--;
				}
			}
			if (*p)
				p++;
			len--;
		}
	}

	/* result has variable length */
	if (size == 0)
		size = len + 1;
	else
		/* result has variable length with maximum size */
	if (size < 0)
		size = MIN(len, -size) + 1;

	result = (char *) palloc(hdrsize + size);
	memset(result, 0, hdrsize + size);
	if (rtn_size)
		*rtn_size = size;

	r = result + hdrsize;
	for (p = str; *p;)
	{
		register unsigned char c;

		if ((c = *p++) == '\\')
		{
			switch (c = *p++)
			{
				case '\0':
					p--;
					break;
				case '0':
				case '1':
				case '2':
				case '3':
				case '4':
				case '5':
				case '6':
				case '7':
					c = VALUE(c);
					if (isdigit(*p))
						c = (c << 3) + VALUE(*p++);
					if (isdigit(*p))
						c = (c << 3) + VALUE(*p++);
					*r++ = c;
					break;
				case 'b':
					*r++ = '\b';
					break;
				case 'f':
					*r++ = '\f';
					break;
				case 'n':
					*r++ = '\n';
					break;
				case 'r':
					*r++ = '\r';
					break;
				case 't':
					*r++ = '\t';
					break;
				case 'v':
					*r++ = '\v';
					break;
				default:
					*r++ = c;
			}
		}
		else
			*r++ = c;
	}

	return ((char *) result);
}

char *
c_charout(int32 c)
{
	char		str[2];

	str[0] = (char) c;
	str[1] = '\0';

	return (string_output(str, 1));
}

/*
 * This can be used for SET, bytea, text and unknown data types
 */

char *
c_textout(struct varlena * vlena)
{
	int			len = 0;
	char	   *s = NULL;

	if (vlena)
	{
		len = VARSIZE(vlena) - VARHDRSZ;
		s = VARDATA(vlena);
	}
	return (string_output(s, len));
}

/*
 * This can be used for varchar and bpchar strings
 */

char *
c_varcharout(char *s)
{
	int			len = 0;

	if (s)
	{
		len = *(int32 *) s - 4;
		s += 4;
	}
	return (string_output(s, len));
}

#if 0
struct varlena *
c_textin(char *str)
{
	struct varlena *result;
	int			len;

	if (str == NULL)
		return ((struct varlena *) NULL);

	result = (struct varlena *) string_input(str, 0, VARHDRSZ, &len);
	VARSIZE(result) = len;

	return (result);
}

int32 *
c_charin(char *str)
{
	return (string_input(str, 1, 0, NULL));
}
#endif

/* end of file */

/*
 * Local variables:
 *  tab-width: 4
 *  c-indent-level: 4
 *  c-basic-offset: 4
 * End:
 */