* Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
- * $PostgreSQL: pgsql/src/bin/pg_dump/dumputils.c,v 1.47 2009/07/14 20:24:10 tgl Exp $
+ * $PostgreSQL: pgsql/src/bin/pg_dump/dumputils.c,v 1.48 2009/08/04 21:56:08 tgl Exp $
*
*-------------------------------------------------------------------------
*/
}
+/*
+ * Convert a bytea value (presented as raw bytes) to an SQL string literal
+ * and append it to the given buffer. We assume the specified
+ * standard_conforming_strings setting.
+ *
+ * This is needed in situations where we do not have a PGconn available.
+ * Where we do, PQescapeByteaConn is a better choice.
+ */
+void
+appendByteaLiteral(PQExpBuffer buf, const unsigned char *str, size_t length,
+ bool std_strings)
+{
+ const unsigned char *source = str;
+ char *target;
+
+ static const char hextbl[] = "0123456789abcdef";
+
+ /*
+ * This implementation is hard-wired to produce hex-format output.
+ * We do not know the server version the output will be loaded into,
+ * so making an intelligent format choice is impossible. It might be
+ * better to always use the old escaped format.
+ */
+ if (!enlargePQExpBuffer(buf, 2 * length + 5))
+ return;
+
+ target = buf->data + buf->len;
+ *target++ = '\'';
+ if (!std_strings)
+ *target++ = '\\';
+ *target++ = '\\';
+ *target++ = 'x';
+
+ while (length-- > 0)
+ {
+ unsigned char c = *source++;
+
+ *target++ = hextbl[(c >> 4) & 0xF];
+ *target++ = hextbl[c & 0xF];
+ }
+
+ /* Write the terminating quote and NUL character. */
+ *target++ = '\'';
+ *target = '\0';
+
+ buf->len = target - buf->data;
+}
+
+
/*
* Convert backend's version string into a number.
*/
* Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
- * $PostgreSQL: pgsql/src/bin/pg_dump/dumputils.h,v 1.24 2009/03/11 03:33:29 adunstan Exp $
+ * $PostgreSQL: pgsql/src/bin/pg_dump/dumputils.h,v 1.25 2009/08/04 21:56:08 tgl Exp $
*
*-------------------------------------------------------------------------
*/
PGconn *conn);
extern void appendStringLiteralDQ(PQExpBuffer buf, const char *str,
const char *dqprefix);
+extern void appendByteaLiteral(PQExpBuffer buf,
+ const unsigned char *str, size_t length,
+ bool std_strings);
extern int parse_version(const char *versionString);
extern bool parsePGArray(const char *atext, char ***itemarray, int *nitems);
extern bool buildACLCommands(const char *name, const char *subname,
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_archiver.c,v 1.173 2009/07/21 21:46:10 tgl Exp $
+ * $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_archiver.c,v 1.174 2009/08/04 21:56:08 tgl Exp $
*
*-------------------------------------------------------------------------
*/
}
else
{
- unsigned char *str;
- size_t len;
+ PQExpBuffer buf = createPQExpBuffer();
- str = PQescapeBytea((const unsigned char *) AH->lo_buf,
- AH->lo_buf_used, &len);
- if (!str)
- die_horribly(AH, modulename, "out of memory\n");
+ appendByteaLiteralAHX(buf,
+ (const unsigned char *) AH->lo_buf,
+ AH->lo_buf_used,
+ AH);
/* Hack: turn off writingBlob so ahwrite doesn't recurse to here */
AH->writingBlob = 0;
- ahprintf(AH, "SELECT pg_catalog.lowrite(0, '%s');\n", str);
+ ahprintf(AH, "SELECT pg_catalog.lowrite(0, %s);\n", buf->data);
AH->writingBlob = 1;
- free(str);
+ destroyPQExpBuffer(buf);
}
AH->lo_buf_used = 0;
}
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_archiver.h,v 1.80 2009/07/21 21:46:10 tgl Exp $
+ * $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_archiver.h,v 1.81 2009/08/04 21:56:09 tgl Exp $
*
*-------------------------------------------------------------------------
*/
#define appendStringLiteralAHX(buf,str,AH) \
appendStringLiteral(buf, str, (AH)->public.encoding, (AH)->public.std_strings)
+#define appendByteaLiteralAHX(buf,str,len,AH) \
+ appendByteaLiteral(buf, str, len, (AH)->public.std_strings)
+
/*
* Mandatory routines for each supported format
*/
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_null.c,v 1.21 2009/07/21 21:46:10 tgl Exp $
+ * $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_null.c,v 1.22 2009/08/04 21:56:09 tgl Exp $
*
*-------------------------------------------------------------------------
*/
#include "pg_backup_archiver.h"
+#include "dumputils.h"
#include <unistd.h> /* for dup */
{
if (dLen > 0)
{
- unsigned char *str;
- size_t len;
+ PQExpBuffer buf = createPQExpBuffer();
- str = PQescapeBytea((const unsigned char *) data, dLen, &len);
- if (!str)
- die_horribly(AH, NULL, "out of memory\n");
+ appendByteaLiteralAHX(buf,
+ (const unsigned char *) data,
+ dLen,
+ AH);
- ahprintf(AH, "SELECT pg_catalog.lowrite(0, '%s');\n", str);
+ ahprintf(AH, "SELECT pg_catalog.lowrite(0, %s);\n", buf->data);
- free(str);
+ destroyPQExpBuffer(buf);
}
return dLen;
}