blob: c13304e0a204494bb428628864c46279fdb9e3c3 (
plain) (
blame)
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
|
/*
* PQescapeString implementation is from
* <URL:http://cert.uni-stuttgart.de/doc/postgresql/escape/>
* It will be available in a later release of PostGreSQL.
*/
#if !defined(HAVE_PQESCAPESTRING)
#include <sys/types.h>
/* Quoting strings before inclusion in queries. */
size_t PQescapeString (char *to, const char *from, size_t length);
/* ---------------
* Escaping arbitrary strings to get valid SQL strings/identifiers.
*
* Replaces "\\" with "\\\\", "\0" with "\\0", and "'" with "''".
* length is the length of the buffer pointed to by
* from. The buffer at to must be at least 2*length + 1 characters
* long. A terminating NUL character is written.
* ---------------
*/
size_t
PQescapeString (char *to, const char *from, size_t length)
{
const char *source = from;
char *target = to;
unsigned int remaining = length;
while (remaining > 0) {
switch (*source) {
case '\0':
*target = '\\';
target++;
*target = '0';
/* target and remaining are updated below. */
break;
case '\\':
*target = '\\';
target++;
*target = '\\';
/* target and remaining are updated below. */
break;
case '\'':
*target = '\'';
target++;
*target = '\'';
/* target and remaining are updated below. */
break;
default:
*target = *source;
/* target and remaining are updated below. */
}
source++;
target++;
remaining--;
}
/* Write the terminating NUL character. */
*target = '\0';
return target - to;
}
#endif /* !defined(HAVE_PQESCAPESTRING) */
|