/*
* call-seq:
* PGconn.escape_bytea( obj )
*
* Escapes binary data for use within an SQL command with the type +bytea+.
*
* Certain byte values must be escaped (but all byte values may be escaped)
* when used as part of a +bytea+ literal in an SQL statement. In general, to
* escape a byte, it is converted into the three digit octal number equal to
* the octet value, and preceded by two backslashes. The single quote (') and
* backslash (\) characters have special alternative escape sequences.
* #escape_bytea performs this operation, escaping only the minimally required bytes.
*
* See the PostgreSQL documentation on PQescapeBytea[http://www.postgresql.org/docs/current/interactive/libpq-exec.html#LIBPQ-EXEC-ESCAPE-BYTEA] for more information.
*/
static VALUE
pgconn_escape_bytea(self, obj)
VALUE self;
VALUE obj;
{
unsigned char *from, *to;
size_t from_len, to_len;
VALUE ret;
Check_Type(obj, T_STRING);
from = (unsigned char*)RSTRING_PTR(obj);
from_len = RSTRING_LEN(obj);
to = PQescapeByteaConn(get_pgconn(self),from, from_len, &to_len);
ret = rb_str_new((char*)to, to_len - 1);
OBJ_INFECT(ret, obj);
PQfreemem(to);
return ret;
}