Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit294f0d4

Browse files
committed
Add PQunescapeBytea libpq function.
Everyone using libpq and bytea is probably having to invent this wheel..Patrick Welche
1 parentb2aade0 commit294f0d4

File tree

4 files changed

+137
-25
lines changed

4 files changed

+137
-25
lines changed

‎doc/src/sgml/libpq.sgml

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$Header: /cvsroot/pgsql/doc/src/sgml/libpq.sgml,v 1.87 2002/01/18 20:39:04 momjian Exp $
2+
$Header: /cvsroot/pgsql/doc/src/sgml/libpq.sgml,v 1.88 2002/03/04 23:59:11 momjian Exp $
33
-->
44

55
<chapter id="libpq">
@@ -955,6 +955,25 @@ strings overlap.
955955
byte is also added. The single quotes that must surround
956956
PostgreSQL string literals are not part of the result string.
957957
</para>
958+
959+
<para>
960+
<function>PQunescapeBytea</function>
961+
Converts an escaped string representation of binary data into binary
962+
data - the reverse of <function>PQescapeBytea</function>.
963+
<synopsis>
964+
unsigned char *PQunescapeBytea(unsigned char *from, size_t *to_length);
965+
</synopsis>
966+
967+
The <paramater>from</parameter> parameter points to an escaped string
968+
such as might be returned by <function>PQgetvalue</function> of a
969+
<type>BYTEA</type> column. <function>PQunescapeBytea</function> converts
970+
this NUL terminated string representation into binary, filling a buffer.
971+
It returns a pointer to the buffer which is NULL on error, and the size
972+
of the buffer in <parameter>to_length</parameter>. The pointer may
973+
subsequently be used as an argument to the function
974+
<function>free(3)</function>.
975+
</para>
976+
958977
</sect2>
959978

960979

‎src/include/utils/elog.h

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,36 @@
77
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $Id: elog.h,v 1.32 2002/03/0401:46:04 tgl Exp $
10+
* $Id: elog.h,v 1.33 2002/03/0423:59:14 momjian Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
1414
#ifndefELOG_H
1515
#defineELOG_H
1616

1717
/* Error level codes */
18-
#defineDEBUG510/* Debugging messages, in categories
19-
* of decreasing detail. */
20-
#defineDEBUG411
21-
#defineDEBUG312
22-
#defineDEBUG213
23-
#defineDEBUG114
24-
#defineLOG15/* Server operational history messages;
25-
* sent only to server log by default. */
26-
#defineCOMMERROR 16/* Client communication problems; same as
27-
* LOG for server reporting, but never ever
28-
* try to send to client. */
29-
#defineINFO17/* Informative messages that are part of
30-
* normal query operation; sent only to
31-
* client by default. */
32-
#defineNOTICE18/* Important messages, for unusual cases that
33-
* should be reported but are not serious
34-
* enough to abort the query. Sent to client
35-
* and server log by default. */
36-
#defineERROR19/* user error - return to known state */
37-
#defineFATAL20/* fatal error - abort process */
38-
#definePANIC21/* take down the other backends with me */
18+
#defineDEBUG510/* Debugging messages, in categories
19+
* of decreasing detail. */
20+
#defineDEBUG411
21+
#defineDEBUG312
22+
#defineDEBUG213
23+
#defineDEBUG114
24+
#defineLOG15/* Server operational history messages;
25+
* sent only to server log by default. */
26+
#defineCOMMERROR 16/* Client communication problems; same as
27+
* LOG for server reporting, but never ever
28+
* try to send to client. */
29+
#defineINFO17/* Informative messages that are part of
30+
* normal query operation; sent only to
31+
* client by default. */
32+
#defineINFOALWAYS18/* Like INFO, but always prints to client */
33+
#defineNOTICE19/* Important messages, for unusual cases that
34+
* should be reported but are not serious
35+
* enough to abort the query. Sent to client
36+
* and server log by default. */
37+
#defineERROR20/* user error - return to known state */
38+
#defineFATAL21/* fatal error - abort process */
39+
#definePANIC22/* take down the other backends with me */
3940

4041
/*#define DEBUGDEBUG1*//* Backward compatibility with pre-7.3 */
4142

‎src/interfaces/libpq/fe-exec.c

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-exec.c,v 1.113 2001/10/25 05:50:13 momjian Exp $
11+
* $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-exec.c,v 1.114 2002/03/04 23:59:14 momjian Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -180,6 +180,95 @@ PQescapeBytea(unsigned char *bintext, size_t binlen, size_t *bytealen)
180180
returnresult;
181181
}
182182

183+
/*
184+
*PQunescapeBytea- converts the null terminated string representation
185+
*of a bytea, strtext, into binary, filling a buffer. It returns a
186+
*pointer to the buffer which is NULL on error, and the size of the
187+
*buffer in retbuflen. The pointer may subsequently be used as an
188+
*argument to the function free(3). It is the reverse of PQescapeBytea.
189+
*
190+
*The following transformations are reversed:
191+
*'\0' == ASCII 0 == \000
192+
*'\'' == ASCII 39 == \'
193+
*'\\' == ASCII 92 == \\
194+
*
195+
*States:
196+
*0normal0->1->2->3->4
197+
*1\ 1->5
198+
*2\0 1->6
199+
*3\00
200+
*4\000
201+
*5\'
202+
*6\\
203+
*/
204+
unsignedchar*
205+
PQunescapeBytea(unsignedchar*strtext,size_t*retbuflen)
206+
{
207+
size_tbuflen;
208+
unsignedchar*buffer,*sp,*bp;
209+
unsignedintstate=0;
210+
211+
if(strtext==NULL)returnNULL;
212+
buflen=strlen(strtext);/* will shrink, also we discover if strtext */
213+
buffer= (unsignedchar*)malloc(buflen);/* isn't NULL terminated */
214+
if(buffer==NULL)returnNULL;
215+
for(bp=buffer,sp=strtext;*sp!='\0';bp++,sp++)
216+
{
217+
switch(state)
218+
{
219+
case0:
220+
if(*sp=='\\')state=1;
221+
*bp=*sp;
222+
break;
223+
case1:
224+
if(*sp=='\'')/* state=5 */
225+
{/* replace \' with 39 */
226+
bp--;
227+
*bp=39;
228+
buflen--;
229+
state=0;
230+
}
231+
elseif(*sp=='\\')/* state=6 */
232+
{/* replace \\ with 92 */
233+
bp--;
234+
*bp=92;
235+
buflen--;
236+
state=0;
237+
}
238+
else
239+
{
240+
if(*sp=='0')state=2;
241+
elsestate=0;
242+
*bp=*sp;
243+
}
244+
break;
245+
case2:
246+
if(*sp=='0')state=3;
247+
elsestate=0;
248+
*bp=*sp;
249+
break;
250+
case3:
251+
if(*sp=='0')/* state=4 */
252+
{
253+
bp-=3;
254+
*bp=0;
255+
buflen-=3;
256+
state=0;
257+
}
258+
else
259+
{
260+
*bp=*sp;
261+
state=0;
262+
}
263+
break;
264+
}
265+
}
266+
realloc(buffer,buflen);
267+
268+
*retbuflen=buflen;
269+
returnbuffer;
270+
}
271+
183272
/* ----------------
184273
* Space management for PGresult.
185274
*

‎src/interfaces/libpq/libpq-fe.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $Id: libpq-fe.h,v 1.80 2001/11/08 20:37:52 momjian Exp $
10+
* $Id: libpq-fe.h,v 1.81 2002/03/04 23:59:14 momjian Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -252,6 +252,9 @@ extern PQnoticeProcessor PQsetNoticeProcessor(PGconn *conn,
252252
externsize_tPQescapeString(char*to,constchar*from,size_tlength);
253253
externunsignedchar*PQescapeBytea(unsignedchar*bintext,size_tbinlen,
254254
size_t*bytealen);
255+
externunsignedchar*PQunescapeBytea(unsignedchar*strtext,
256+
size_t*retbuflen);
257+
255258

256259
/* Simple synchronous query */
257260
externPGresult*PQexec(PGconn*conn,constchar*query);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp