diff options
author | Bruce Momjian <bruce@momjian.us> | 2001-09-14 17:46:40 +0000 |
---|---|---|
committer | Bruce Momjian <bruce@momjian.us> | 2001-09-14 17:46:40 +0000 |
commit | c1fbf06654aca97a109a2346d182158e8f14ab01 (patch) | |
tree | c0e4e87bb80d3eca6a8dd5e6194dac791b630a5d /src/interfaces/libpq/fe-exec.c | |
parent | e8d5b8d2901ddf1b4518ec0eb450028678f2082e (diff) | |
download | postgresql-c1fbf06654aca97a109a2346d182158e8f14ab01.tar.gz postgresql-c1fbf06654aca97a109a2346d182158e8f14ab01.zip |
> Here's a revised patch. Changes:
>
> 1. Now outputs '\\' instead of '\134' when using encode(bytea, 'escape')
> Note that I ended up leaving \0 as \000 so that there are no ambiguities
> when decoding something like, for example, \0123.
>
> 2. Fixed bug in byteain which allowed input values which were not valid
> octals (e.g. \789), to be parsed as if they were octals.
>
> Joe
>
Here's rev 2 of the bytea string support patch. Changes:
1. Added missing declaration for MatchBytea function
2. Added PQescapeBytea to fe-exec.c
3. Applies cleanly on cvs tip from this afternoon
I'm hoping that someone can review/approve/apply this before beta starts, so
I guess I'd vote (not that it counts for much) to delay beta a few days :-)
Joe Conway
Diffstat (limited to 'src/interfaces/libpq/fe-exec.c')
-rw-r--r-- | src/interfaces/libpq/fe-exec.c | 74 |
1 files changed, 73 insertions, 1 deletions
diff --git a/src/interfaces/libpq/fe-exec.c b/src/interfaces/libpq/fe-exec.c index e7495dabf7f..2741ec67aa6 100644 --- a/src/interfaces/libpq/fe-exec.c +++ b/src/interfaces/libpq/fe-exec.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-exec.c,v 1.111 2001/09/13 17:00:34 momjian Exp $ + * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-exec.c,v 1.112 2001/09/14 17:46:40 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -104,7 +104,79 @@ PQescapeString (char *to, const char *from, size_t length) return target - to; } +/* + * 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 == \\\\ + */ +unsigned char * +PQescapeBytea(unsigned char *bintext, size_t binlen, size_t *bytealen) +{ + unsigned char *vp; + unsigned char *rp; + unsigned char *result; + size_t i; + size_t len; + + /* + * empty string has 1 char ('\0') + */ + len = 1; + + vp = bintext; + for (i = binlen; i != 0; i--, vp++) + { + if (*vp == 0) + len += 5; + else if (*vp == 39) + len += 2; + else if (*vp == 92) + len += 4; + else + len++; + } + + rp = result = (unsigned char *) malloc(len); + vp = bintext; + *bytealen = len; + for (i = binlen; i != 0; i--, vp++) + { + if (*vp == 0) + { + rp[0] = '\\'; + rp[1] = '\\'; + rp[2] = '0'; + rp[3] = '0'; + rp[4] = '0'; + rp += 5; + } + else if (*vp == 39) + { + rp[0] = '\\'; + rp[1] = '\''; + rp += 2; + } + else if (*vp == 92) + { + rp[0] = '\\'; + rp[1] = '\\'; + rp[2] = '\\'; + rp[3] = '\\'; + rp += 4; + } + else + *rp++ = *vp; + } + *rp = '\0'; + + return result; +} /* ---------------- * Space management for PGresult. |