File: 58libpq-string-escaping.patch

package info (click to toggle)
postgresql 7.4.7-6sarge6
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 11,168 kB
  • ctags: 27
  • sloc: sh: 1,903; makefile: 337; ansic: 204; perl: 69; sed: 6; tcl: 1
file content (666 lines) | stat: -rw-r--r-- 24,681 bytes parent folder | download
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
diff -ruN postgresql-7.4.7-old/doc/src/sgml/libpq.sgml postgresql-7.4.7/doc/src/sgml/libpq.sgml
--- postgresql-7.4.7-old/doc/src/sgml/libpq.sgml	2003-11-12 20:05:14.000000000 +0000
+++ postgresql-7.4.7/doc/src/sgml/libpq.sgml	2006-05-24 17:21:38.000000000 +0000
@@ -1957,15 +1957,16 @@
 <sect2 id="libpq-exec-escape-string">
   <title>Escaping Strings for Inclusion in SQL Commands</title>
 
+   <indexterm zone="libpq-exec-escape-string"><primary>PQescapeStringConn</></>
    <indexterm zone="libpq-exec-escape-string"><primary>PQescapeString</></>
    <indexterm zone="libpq-exec-escape-string"><primary>escaping strings</></>
 
 <para>
-<function>PQescapeString</function> escapes a string for use within an SQL
+<function>PQescapeStringConn</function> escapes a string for use within an SQL
 command.  This is useful when inserting data values as literal constants
 in SQL commands.  Certain characters (such as quotes and backslashes) must
 be escaped to prevent them from being interpreted specially by the SQL parser.
-<function>PQescapeString</> performs this operation.
+<function>PQescapeStringConn</> performs this operation.
 </para>
 
 <tip>
@@ -1983,36 +1984,68 @@
 its sibling routines.
 
 <synopsis>
-size_t PQescapeString (char *to, const char *from, size_t length);
+size_t PQescapeStringConn (PGconn *conn,
+                           char *to, const char *from, size_t length,
+                           int *error);
 </synopsis>
 </para>
 
 <para>
+<function>PQescapeStringConn</> writes an escaped
+version of the <parameter>from</> string to the <parameter>to</>
+buffer, escaping special characters so that they cannot cause any
+harm, and adding a terminating zero byte.  The single quotes that
+must surround <productname>PostgreSQL</> string literals are not
+included in the result string; they should be provided in the SQL
+command that the result is inserted into.
 The parameter <parameter>from</> points to the first character of the string
 that is to be escaped, and the <parameter>length</> parameter gives the
-number of characters in this string.  A terminating zero byte is not
+number of bytes in this string.  A terminating zero byte is not
 required, and should not be counted in <parameter>length</>.  (If
 a terminating zero byte is found before <parameter>length</> bytes are
-processed, <function>PQescapeString</> stops at the zero; the behavior
+processed, <function>PQescapeStringConn</> stops at the zero; the behavior
 is thus rather like <function>strncpy</>.)
 <parameter>to</> shall point to a
-buffer that is able to hold at least one more character than twice
+buffer that is able to hold at least one more byte than twice
 the value of <parameter>length</>, otherwise the behavior is
-undefined.  A call to <function>PQescapeString</> writes an escaped
-version of the <parameter>from</> string to the <parameter>to</>
-buffer, replacing special characters so that they cannot cause any
-harm, and adding a terminating zero byte.  The single quotes that
-must surround <productname>PostgreSQL</> string literals are not
-included in the result string; they should be provided in the SQL
-command that the result is inserted into.
+undefined.
+Behavior is likewise undefined if the <parameter>to</> and <parameter>from</>
+strings overlap.
+</para>
+<para>
+If the <parameter>error</> parameter is not NULL, then <literal>*error</>
+is set to zero on success, nonzero on error.  Presently the only possible
+error conditions involve invalid multibyte encoding in the source string.
+The output string is still generated on error, but it can be expected that
+the server will reject it as malformed.  On error, a suitable message is
+stored in the <parameter>conn</> object, whether or not <parameter>error</>
+is NULL.
 </para>
 <para>
-<function>PQescapeString</> returns the number of characters written
+<function>PQescapeStringConn</> returns the number of bytes written
 to <parameter>to</>, not including the terminating zero byte.
 </para>
+
 <para>
-Behavior is undefined if the <parameter>to</> and <parameter>from</>
-strings overlap.
+<synopsis>
+size_t PQescapeString (char *to, const char *from, size_t length);
+</synopsis>
+</para>
+
+<para>
+<function>PQescapeString</> is an older, deprecated version of
+<function>PQescapeStringConn</>; the difference is that it does not
+take <parameter>conn</> or <parameter>error</> parameters.  Because of this,
+it cannot adjust its behavior depending on the connection properties (such as
+character encoding) and therefore <emphasis>it may give the wrong results</>.
+Also, it has no way to report error conditions.
+</para>
+<para>
+<function>PQescapeString</> can be used safely in single-threaded client
+programs that work with only one <productname>PostgreSQL</> connection at
+a time (in this case it can find out what it needs to know <quote>behind the
+scenes</>).  In other contexts it is a security hazard and should be avoided
+in favor of <function>PQescapeStringConn</>.
 </para>
 </sect2>
 
@@ -2027,29 +2060,30 @@
 
   <variablelist>
   <varlistentry>
-  <term><function>PQescapeBytea</function><indexterm><primary>PQescapeBytea</></></term>
+  <term><function>PQescapeByteaConn</function><indexterm><primary>PQescapeByteaConn</></></term>
   <listitem>
   <para>
    Escapes binary data for use within an SQL command with the type
-   <type>bytea</type>.  As with <function>PQescapeString</function>,
+   <type>bytea</type>.  As with <function>PQescapeStringConn</function>,
    this is only used when inserting data directly into an SQL command string.
 <synopsis>
-unsigned char *PQescapeBytea(const unsigned char *from,
-                             size_t from_length,
-                             size_t *to_length);
+unsigned char *PQescapeByteaConn(PGconn *conn,
+                                 const unsigned char *from,
+                                 size_t from_length,
+                                 size_t *to_length);
 </synopsis>
 </para>
 
 <para>
    Certain byte values <emphasis>must</emphasis> be escaped (but all
-   byte values <emphasis>may</emphasis> be escaped) when used as part
+   byte values <emphasis>can</emphasis> be escaped) when used as part
    of a <type>bytea</type> literal in an <acronym>SQL</acronym>
    statement. In general, to escape a byte, it is converted into the
    three digit octal number equal to the octet value, and preceded by
-   two backslashes. The single quote (<literal>'</>) and backslash
+   one or two backslashes. The single quote (<literal>'</>) and backslash
    (<literal>\</>) characters have special alternative escape
    sequences. See <xref linkend="datatype-binary"> for more
-   information. <function>PQescapeBytea</function> performs this
+   information. <function>PQescapeByteaConn</function> performs this
    operation, escaping only the minimally required bytes.
   </para>
 
@@ -2060,22 +2094,60 @@
    bytes in this binary string.  (A terminating zero byte is
    neither necessary nor counted.)  The <parameter>to_length</parameter>
    parameter points to a variable that will hold the resultant
-   escaped string length. The result string length includes the terminating
+   escaped string length. This result string length includes the terminating
    zero byte of the result.
   </para>
 
   <para>
-   <function>PQescapeBytea</> returns an escaped version of the
+   <function>PQescapeByteaConn</> returns an escaped version of the
    <parameter>from</parameter> parameter binary string in memory
-   allocated with <function>malloc()</>.  This memory must be freed
-   using <function>PQfreemem</> when the result is no longer needed.
-   The return string has all special characters replaced so that they
-   can be properly processed by the
-   <productname>PostgreSQL</productname> string literal parser, and
-   the <type>bytea</type> input function. A terminating zero byte is
-   also added.  The single quotes that must surround
-   <productname>PostgreSQL</productname> string literals are not part
-   of the result string.
+   allocated with <function>malloc()</>.  This memory must be freed using
+   <function>PQfreemem()</> when the result is no longer needed.  The
+   return string has all special characters replaced so that they can
+   be properly processed by the <productname>PostgreSQL</productname>
+   string literal parser, and the <type>bytea</type> input function. A
+   terminating zero byte is also added.  The single quotes that must
+   surround <productname>PostgreSQL</productname> string literals are
+   not part of the result string.
+  </para>
+
+  <para>
+   On error, a NULL pointer is returned, and a suitable error message
+   is stored in the <parameter>conn</> object.  Currently, the only
+   possible error is insufficient memory for the result string.
+  </para>
+  </listitem>
+  </varlistentry>
+
+  <varlistentry>
+  <term><function>PQescapeBytea</function><indexterm><primary>PQescapeBytea</></></term>
+  <listitem>
+  <para>
+   <function>PQescapeBytea</> is an older, deprecated version of
+   <function>PQescapeByteaConn</>.
+<synopsis>
+unsigned char *PQescapeBytea(const unsigned char *from,
+                             size_t from_length,
+                             size_t *to_length);
+</synopsis>
+</para>
+
+<para>
+   The only difference from <function>PQescapeByteaConn</> is that
+   <function>PQescapeBytea</> does not
+   take a <structname>PGconn</> parameter.  Because of this, it cannot adjust
+   its behavior depending on the connection properties (in particular,
+   whether standard-conforming strings are enabled)
+   and therefore <emphasis>it may give the wrong results</>.  Also, it
+   has no way to return an error message on failure.
+  </para>
+
+  <para>
+   <function>PQescapeBytea</> can be used safely in single-threaded client
+   programs that work with only one <productname>PostgreSQL</> connection at
+   a time (in this case it can find out what it needs to know <quote>behind the
+   scenes</>).  In other contexts it is a security hazard and should be
+   avoided in favor of <function>PQescapeByteaConn</>.
   </para>
   </listitem>
   </varlistentry>
@@ -2084,7 +2156,7 @@
   <term><function>PQunescapeBytea</function><indexterm><primary>PQunescapeBytea</></></term>
   <listitem>
   <para>
-   Converts an escaped string representation of binary data into binary
+   Converts a string representation of binary data into binary
    data --- the reverse of <function>PQescapeBytea</function>.
    This is needed when retrieving <type>bytea</type> data in text format,
    but not when retrieving it in binary format.
@@ -2094,16 +2166,24 @@
 </synopsis>
 </para>
 
-<para>
-   The <parameter>from</parameter> parameter points to an escaped string
-   such as might be returned by <function>PQgetvalue</function> when applied to a
-   <type>bytea</type> column. <function>PQunescapeBytea</function> converts
-   this string representation into its binary representation.
+  <para>
+   The <parameter>from</parameter> parameter points to a string
+   such as might be returned by <function>PQgetvalue</function> when applied
+   to a <type>bytea</type> column. <function>PQunescapeBytea</function>
+   converts this string representation into its binary representation.
    It returns a pointer to a buffer allocated with
    <function>malloc()</function>, or null on error, and puts the size of
    the buffer in <parameter>to_length</parameter>. The result must be
    freed using <function>PQfreemem</> when it is no longer needed.
   </para>
+
+  <para>
+   This conversion is not exactly the inverse of
+   <function>PQescapeBytea</function>, because the string is not expected
+   to be <quote>escaped</> when received from <function>PQgetvalue</function>.
+   In particular this means there is no need for string quoting considerations,
+   and so no need for a <structname>PGconn</> parameter.
+  </para>
   </listitem>
   </varlistentry>
 
@@ -2119,6 +2199,7 @@
 
 <para>
    Frees memory allocated by <application>libpq</>, particularly
+   <function>PQescapeByteaConn</function>,
    <function>PQescapeBytea</function>,
    <function>PQunescapeBytea</function>,
    and <function>PQnotifies</function>.
diff -ruN postgresql-7.4.7-old/src/interfaces/libpq/blibpqdll.def postgresql-7.4.7/src/interfaces/libpq/blibpqdll.def
--- postgresql-7.4.7-old/src/interfaces/libpq/blibpqdll.def	2003-08-13 16:29:03.000000000 +0000
+++ postgresql-7.4.7/src/interfaces/libpq/blibpqdll.def	2006-05-24 17:21:38.000000000 +0000
@@ -113,6 +113,8 @@
     _PQfformat               @ 109
     _PQexecPrepared          @ 110
     _PQsendQueryPrepared     @ 111
+    _PQescapeStringConn      @ 126
+    _PQescapeByteaConn       @ 127
 
 ; Aliases for MS compatible names
     PQconnectdb             = _PQconnectdb            
@@ -226,3 +228,5 @@
     PQfformat               = _PQfformat
     PQexecPrepared          = _PQexecPrepared
     PQsendQueryPrepared     = _PQsendQueryPrepared
+    PQescapeStringConn      = _PQescapeStringConn
+    PQescapeByteaConn       = _PQescapeByteaConn
diff -ruN postgresql-7.4.7-old/src/interfaces/libpq/fe-connect.c postgresql-7.4.7/src/interfaces/libpq/fe-connect.c
--- postgresql-7.4.7-old/src/interfaces/libpq/fe-connect.c	2003-12-19 23:32:36.000000000 +0000
+++ postgresql-7.4.7/src/interfaces/libpq/fe-connect.c	2006-05-24 17:21:38.000000000 +0000
@@ -1956,6 +1956,7 @@
 	conn->xactStatus = PQTRANS_IDLE;
 	conn->setenv_state = SETENV_STATE_IDLE;
 	conn->client_encoding = PG_SQL_ASCII;
+	conn->std_strings = false;	/* unless server says differently */
 	conn->verbosity = PQERRORS_DEFAULT;
 	conn->notifyList = DLNewList();
 	conn->sock = -1;
@@ -2921,8 +2922,14 @@
 		status = -1;
 	else
 	{
-		/* change libpq internal encoding */
-		conn->client_encoding = pg_char_to_encoding(encoding);
+		/*
+		 * In protocol 2 we have to assume the setting will stick, and
+		 * adjust our state immediately.  In protocol 3 and up we can
+		 * rely on the backend to report the parameter value, and we'll
+		 * change state at that time.
+		 */
+		if (PG_PROTOCOL_MAJOR(conn->pversion) < 3)
+			pqSaveParameterStatus(conn, "client_encoding", encoding);
 		status = 0;				/* everything is ok */
 	}
 	PQclear(res);
diff -ruN postgresql-7.4.7-old/src/interfaces/libpq/fe-exec.c postgresql-7.4.7/src/interfaces/libpq/fe-exec.c
--- postgresql-7.4.7-old/src/interfaces/libpq/fe-exec.c	2006-05-24 17:21:24.000000000 +0000
+++ postgresql-7.4.7/src/interfaces/libpq/fe-exec.c	2006-05-24 17:21:38.000000000 +0000
@@ -41,6 +41,12 @@
 	"PGRES_FATAL_ERROR"
 };
 
+/*
+ * static state needed by PQescapeString and PQescapeBytea; initialize to
+ * values that result in backward-compatible behavior
+ */
+static int	static_client_encoding = PG_SQL_ASCII;
+static bool	static_std_strings = false;
 
 
 static bool PQsendQueryStart(PGconn *conn);
@@ -608,11 +614,22 @@
 	}
 
 	/*
-	 * Special hacks: remember client_encoding as a numeric value, and
-	 * convert server version to a numeric form as well.
+	 * Special hacks: remember client_encoding and standard_conforming_strings,
+	 * and convert server version to a numeric form.  We keep the first two of
+	 * these in static variables as well, so that PQescapeString and
+	 * PQescapeBytea can behave somewhat sanely (at least in single-
+	 * connection-using programs).
 	 */
 	if (strcmp(name, "client_encoding") == 0)
+	{
 		conn->client_encoding = pg_char_to_encoding(value);
+		static_client_encoding = conn->client_encoding;
+	}
+	else if (strcmp(name, "standard_conforming_strings") == 0)
+	{
+		conn->std_strings = (strcmp(value, "on") == 0);
+		static_std_strings = conn->std_strings;
+	}
 	else if (strcmp(name, "server_version") == 0)
 	{
 		int			cnt;
@@ -2227,7 +2244,7 @@
 /*
  * Escaping arbitrary strings to get valid SQL literal strings.
  *
- * Replaces "\\" with "\\\\" and "'" with "''".
+ * Replaces "'" with "''", and if not std_strings, replaces "\" with "\\".
  *
  * length is the length of the source string.  (Note: if a terminating NUL
  * is encountered sooner, PQescapeString stops short of "length"; the behavior
@@ -2239,33 +2256,74 @@
  *
  * Returns the actual length of the output (not counting the terminating NUL).
  */
-size_t
-PQescapeString(char *to, const char *from, size_t length)
+static size_t
+PQescapeStringInternal(PGconn *conn,
+					   char *to, const char *from, size_t length,
+					   int *error,
+					   int encoding, bool std_strings)
 {
 	const char *source = from;
 	char	   *target = to;
 	size_t		remaining = length;
 
+	if (error)
+		*error = 0;
+
 	while (remaining > 0 && *source != '\0')
 	{
-		switch (*source)
-		{
-			case '\\':
-				*target++ = '\\';
-				*target++ = '\\';
-				break;
+		char	c = *source;
+		int		len;
+		int		i;
+
+		/* Fast path for plain ASCII */
+		if (!IS_HIGHBIT_SET(c))
+		{
+			/* Apply quoting if needed */
+			if (c == '\'' ||
+				(c == '\\' && !std_strings))
+				*target++ = c;
+			/* Copy the character */
+			*target++ = c;
+			source++;
+			remaining--;
+			continue;
+		}
 
-			case '\'':
-				*target++ = '\'';
-				*target++ = '\'';
-				break;
+		/* Slow path for possible multibyte characters */
+		len = pg_encoding_mblen(encoding, source);
 
-			default:
-				*target++ = *source;
+		/* Copy the character */
+		for (i = 0; i < len; i++)
+		{
+			if (remaining == 0 || *source == '\0')
 				break;
+			*target++ = *source++;
+			remaining--;
+		}
+
+		/*
+		 * If we hit premature end of string (ie, incomplete multibyte
+		 * character), try to pad out to the correct length with spaces.
+		 * We may not be able to pad completely, but we will always be able
+		 * to insert at least one pad space (since we'd not have quoted a
+		 * multibyte character).  This should be enough to make a string that
+		 * the server will error out on.
+		 */
+		if (i < len)
+		{
+			if (error)
+				*error = 1;
+			if (conn)
+				printfPQExpBuffer(&conn->errorMessage,
+						libpq_gettext("incomplete multibyte character\n"));
+			for (; i < len; i++)
+			{
+				if (((size_t) (target - to)) / 2 >= length)
+					break;
+				*target++ = ' ';
+			}
+			break;
 		}
-		source++;
-		remaining--;
 	}
 
 	/* Write the terminating NUL character. */
@@ -2274,72 +2332,109 @@
 	return target - to;
 }
 
+size_t
+PQescapeStringConn(PGconn *conn,
+				   char *to, const char *from, size_t length,
+				   int *error)
+{
+	if (!conn)
+	{
+		/* force empty-string result */
+		*to = '\0';
+		if (error)
+			*error = 1;
+		return 0;
+	}
+	return PQescapeStringInternal(conn, to, from, length, error,
+								  conn->client_encoding,
+								  conn->std_strings);
+}
+
+size_t
+PQescapeString(char *to, const char *from, size_t length)
+{
+	return PQescapeStringInternal(NULL, to, from, length, NULL,
+								  static_client_encoding,
+								  static_std_strings);
+}
+
 /*
  *		PQescapeBytea	- converts from binary string to the
  *		minimal encoding necessary to include the string in an SQL
  *		INSERT statement with a bytea type column as the target.
  *
  *		The following transformations are applied
- *		'\0' == ASCII  0 == \\000
- *		'\'' == ASCII 39 == \'
- *		'\\' == ASCII 92 == \\\\
- *		anything < 0x20, or > 0x7e ---> \\ooo
- *                                      (where ooo is an octal expression)
- */
-unsigned char *
-PQescapeBytea(const unsigned char *bintext, size_t binlen, size_t *bytealen)
+ *		'\0' == ASCII  0 == \000
+ *		'\'' == ASCII 39 == ''
+ *		'\\' == ASCII 92 == \\
+ *		anything < 0x20, or > 0x7e ---> \ooo
+ *										(where ooo is an octal expression)
+ *		If not std_strings, all backslashes sent to the output are doubled.
+ */
+static unsigned char *
+PQescapeByteaInternal(PGconn *conn,
+					  const unsigned char *from, size_t from_length,
+					  size_t *to_length, bool std_strings)
 {
 	const unsigned char *vp;
 	unsigned char *rp;
 	unsigned char *result;
 	size_t		i;
 	size_t		len;
+	size_t		bslash_len = (std_strings ? 1 : 2);
 
 	/*
 	 * empty string has 1 char ('\0')
 	 */
 	len = 1;
 
-	vp = bintext;
-	for (i = binlen; i > 0; i--, vp++)
+	vp = from;
+	for (i = from_length; i > 0; i--, vp++)
 	{
 		if (*vp < 0x20 || *vp > 0x7e)
-			len += 5;			/* '5' is for '\\ooo' */
+			len += bslash_len + 3;
 		else if (*vp == '\'')
 			len += 2;
 		else if (*vp == '\\')
-			len += 4;
+			len += bslash_len + bslash_len;
 		else
 			len++;
 	}
 
+	*to_length = len;
 	rp = result = (unsigned char *) malloc(len);
 	if (rp == NULL)
+	{
+		if (conn)
+			printfPQExpBuffer(&conn->errorMessage,
+							  libpq_gettext("out of memory\n"));
 		return NULL;
+	}
 
-	vp = bintext;
-	*bytealen = len;
-
-	for (i = binlen; i > 0; i--, vp++)
+	vp = from;
+	for (i = from_length; i > 0; i--, vp++)
 	{
 		if (*vp < 0x20 || *vp > 0x7e)
 		{
-			(void) sprintf(rp, "\\\\%03o", *vp);
-			rp += 5;
+			if (!std_strings)
+				*rp++ = '\\';
+			(void) sprintf((char *) rp, "\\%03o", *vp);
+			rp += 4;
 		}
 		else if (*vp == '\'')
 		{
-			rp[0] = '\'';
-			rp[1] = '\'';
-			rp += 2;
+			*rp++ = '\'';
+			*rp++ = '\'';
 		}
 		else if (*vp == '\\')
 		{
-			rp[0] = '\\';
-			rp[1] = '\\';
-			rp[2] = '\\';
-			rp[3] = '\\';
-			rp += 4;
+			if (!std_strings)
+			{
+				*rp++ = '\\';
+				*rp++ = '\\';
+			}
+			*rp++ = '\\';
+			*rp++ = '\\';
 		}
 		else
 			*rp++ = *vp;
@@ -2349,6 +2444,25 @@
 	return result;
 }
 
+unsigned char *
+PQescapeByteaConn(PGconn *conn,
+				  const unsigned char *from, size_t from_length,
+				  size_t *to_length)
+{
+	if (!conn)
+		return NULL;
+	return PQescapeByteaInternal(conn, from, from_length, to_length,
+								 conn->std_strings);
+}
+
+unsigned char *
+PQescapeBytea(const unsigned char *from, size_t from_length, size_t *to_length)
+{
+	return PQescapeByteaInternal(NULL, from, from_length, to_length,
+								 static_std_strings);
+}
+
+
 #define ISFIRSTOCTDIGIT(CH) ((CH) >= '0' && (CH) <= '3')
 #define ISOCTDIGIT(CH) ((CH) >= '0' && (CH) <= '7')
 #define OCTVAL(CH) ((CH) - '0')
@@ -2358,7 +2472,7 @@
  *		of a bytea, strtext, into binary, filling a buffer. It returns a
  *		pointer to the buffer (or NULL on error), and the size of the
  *		buffer in retbuflen. The pointer may subsequently be used as an
- *		argument to the function free(3). It is the reverse of PQescapeBytea.
+ *		argument to the function PQfreemem.
  *
  *		The following transformations are made:
  *		\\	 == ASCII 92 == \
diff -ruN postgresql-7.4.7-old/src/interfaces/libpq/libpqdll.def postgresql-7.4.7/src/interfaces/libpq/libpqdll.def
--- postgresql-7.4.7-old/src/interfaces/libpq/libpqdll.def	2003-08-13 16:29:03.000000000 +0000
+++ postgresql-7.4.7/src/interfaces/libpq/libpqdll.def	2006-05-24 17:21:38.000000000 +0000
@@ -113,3 +113,5 @@
     PQfformat               @ 109
     PQexecPrepared          @ 110
     PQsendQueryPrepared     @ 111
+    PQescapeStringConn      @ 126
+    PQescapeByteaConn       @ 127
diff -ruN postgresql-7.4.7-old/src/interfaces/libpq/libpq-fe.h postgresql-7.4.7/src/interfaces/libpq/libpq-fe.h
--- postgresql-7.4.7-old/src/interfaces/libpq/libpq-fe.h	2003-08-27 00:33:34.000000000 +0000
+++ postgresql-7.4.7/src/interfaces/libpq/libpq-fe.h	2006-05-24 17:21:38.000000000 +0000
@@ -393,11 +393,18 @@
 
 
 /* Quoting strings before inclusion in queries. */
-extern size_t PQescapeString(char *to, const char *from, size_t length);
-extern unsigned char *PQescapeBytea(const unsigned char *bintext, size_t binlen,
-			  size_t *bytealen);
+extern size_t PQescapeStringConn(PGconn *conn,
+								 char *to, const char *from, size_t length,
+								 int *error);
+extern unsigned char *PQescapeByteaConn(PGconn *conn,
+				  const unsigned char *from, size_t from_length,
+				  size_t *to_length);
 extern unsigned char *PQunescapeBytea(const unsigned char *strtext,
 				size_t *retbuflen);
+/* These forms are deprecated! */
+extern size_t PQescapeString(char *to, const char *from, size_t length);
+extern unsigned char *PQescapeBytea(const unsigned char *from, size_t from_length,
+			  size_t *to_length);
 
 
 
diff -ruN postgresql-7.4.7-old/src/interfaces/libpq/libpq-int.h postgresql-7.4.7/src/interfaces/libpq/libpq-int.h
--- postgresql-7.4.7-old/src/interfaces/libpq/libpq-int.h	2004-03-05 01:54:13.000000000 +0000
+++ postgresql-7.4.7/src/interfaces/libpq/libpq-int.h	2006-05-24 17:21:38.000000000 +0000
@@ -293,6 +293,7 @@
 	char		cryptSalt[2];	/* password salt received from backend */
 	pgParameterStatus *pstatus; /* ParameterStatus data */
 	int			client_encoding;	/* encoding id */
+	bool		std_strings;	/* standard_conforming_strings */
 	PGVerbosity verbosity;		/* error/notice message verbosity */
 	PGlobjfuncs *lobjfuncs;		/* private state for large-object access
 								 * fns */