Re: PQunescapebytea not reverse of PQescapebytea?

From: Florian Weimer <fw(at)deneb(dot)enyo(dot)de>
To: Jeff Davis <pgsql(at)j-davis(dot)com>
Cc: Karthik Segpi <karthik(dot)segpi(at)gmail(dot)com>, pgsql-interfaces(at)postgresql(dot)org
Subject: Re: PQunescapebytea not reverse of PQescapebytea?
Date: 2016-09-18 14:02:32
Message-ID: 878tup1f2f.fsf@mid.deneb.enyo.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-interfaces

* Jeff Davis:

> On Wed, 2014-03-19 at 21:28 +0100, Florian Weimer wrote:
>> * Karthik Segpi:
>>
>> > I have a 'bytea' column in the database, onto which my custom C application
>> > is inserting encrypted data. Before inserting, I am calling
>> > 'PQescapebytea()' to escape the ciphertext. However, after SELECT, the data
>> > needs to be 'un-escaped' before attempting to decrypt. I am trying to
>> > 'un-escape' using 'PQunescapebytea'. However, I am finding that
>> > 'PQunescapebytea' is not exact inverse of 'PQescapebytea'. I saw
>> > documentation and posts in the mailing lists alluding to this as well. As a
>> > result, the decryption always fails.
>>
>> Can you show us some example data that shows the inconsistency?
>> PQunescapebytea should give you back the blob you passed to
>> PQescapebytea, but the same blob can have different BYTEA
>> encodings—not everyone uses the \x hexadecimal encoding.
>
> Example:
>
> size_t len1, len2;
> char *str = "\\\\123";
>
> printf("%s\n", str);
> printf("%s\n", PQescapeBytea(str, strlen(str), &len1));
> printf("%s\n", PQunescapeBytea(
> PQescapeBytea(str, strlen(str), &len1),
> &len2));
>
> The reason for this is that PQescapeBytea is designed to escape it to be
> passed into the server via a SQL string (adding two levels of escaping,
> one for the sql string and one for bytea); whereas PQunescapeBytea is
> designed to unescape a result coming back from the server (which only
> has one level of escaping to undo: the bytea escaping).

Ah, right, this is annoying. I think a thin (one-level) escaping
function would make sense to add to libpq.

In response to

Browse pgsql-interfaces by date

  From Date Subject
Next Message Attila Peller 2016-10-07 01:23:47 cursor updatability changed
Previous Message Reynaldo Rodriguez 2016-08-31 00:26:29 Preguntas/Questions