Обсуждение: PQunescapeBytea
The first part of the following piece of code should, and
does, take an array of 5 characters and convert it to an
escaped string of 10 characters (including the terminating
byte).
However, when going in the other direction PQunescapeBytea
reports (in unescaped_len) that the resulting binary data
has a length of 8 instead of 5 and is different than a[].
Am I using the escape/unescape functions incorrectly? I was
expecting the unescape function to produce an exact
duplicate of a[].
Thanks,
Iker
========================
unsigned char a[5];
a[0] = 'a';
a[1] = 'a';
a[2] = 'a';
a[3] = 'a';
a[4] = 0;
for (unsigned int i = 0; i < 5; ++i)
printf("%c\n", a[i]);
size_t escaped_len;
size_t unescaped_len;
unsigned char* escaped = PQescapeBytea(a, 5, &escaped_len);
unsigned char* unescaped = PQunescapeBytea(escaped, &unescaped_len);
printf("\n");
printf("unescaped_len: %d\n", unescaped_len);
for (unsigned int i = 0; i < unescaped_len; ++i)
printf("%c\n", unescaped[i]);
========================
OUTPUT:
a
a
a
a
@ <== this is an unprintable character
unescaped_len: 8
a
a
a
a
\
0
0
0
Iker Arizmendi wrote: > Am I using the escape/unescape functions incorrectly? I was > expecting the unescape function to produce an exact > duplicate of a[]. The two functions are not, and should not be, precise reciprocals. PQescapeBytea() prepares a string to be used in an SQL statement, meaning it must produce something in the form "\\ooo" (without the quotes) -- notice *two* backslashes before the octal number. This is needed because the SQL string literal parser eats one backslash, and the byteain() function needs the other to recognize that it is receiving an octal sequence. PQunescapeBytea(), on the other hand, takes a string in the form "\ooo" which is the output format produced by byteaout() function. Notice that only *one* backslash is expected before the octal. In your example, "aaaa\0" is fed into PQescapeBytea(), producing "aaaa\\000". When that is fed into PQunescapeBytea(), the two backslashes are seen as a single backslash that has been escaped. Therefore the two backslashes are transformed into a single unescaped backslash, and the octal number is taken as a literal part of the string, producing "aaaa\000". HTH, Joe