strings are distinguished from characters strings by two
characteristics: First, binary strings specifically allow storing
octets of value zero and other non-printable
- octets. Second, operations on binary strings process the actual
- bytes, whereas the encoding and processing of character strings
- depends on locale settings.
+ octets (defined as octets outside the range 32 to 126).
+ Second, operations on binary strings process the actual bytes,
+ whereas the encoding and processing of character strings depends
+ on locale settings.
\\
+ |
+ 0 to 31 and 127 to 255
+ non-printable
octets
+ '\\xxx'> (octal value)
+ SELECT '\\001'::bytea;
+ \001
+
+
- Note that the result in each of the examples in was exactly one
- octet in length, even though the output representation of the zero
- octet and backslash are more than one character.
+ The requirement to escape non-printable
octets actually
+ varies depending on locale settings. In some instances you can get away
+ with leaving them unescaped. Note that the result in each of the examples
+ in was exactly one octet in
+ length, even though the output representation of the zero octet and
+ backslash are more than one character.
|
32 to 126
printable
octets
- ASCII representation
+ client character set representation
SELECT '\\176'::bytea;
~
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/utils/adt/varlena.c,v 1.107 2003/11/29 19:51:59 pgsql Exp $
+ * $PostgreSQL: pgsql/src/backend/utils/adt/varlena.c,v 1.108 2003/11/30 20:55:09 joe Exp $
*
*-------------------------------------------------------------------------
*/
{
if (*vp == '\\')
len += 2;
- else if (isprint((unsigned char) *vp))
- len++;
- else
+ else if ((unsigned char) *vp < 0x20 || (unsigned char) *vp > 0x7e)
len += 4;
+ else
+ len++;
}
rp = result = (char *) palloc(len);
vp = VARDATA(vlena);
*rp++ = '\\';
*rp++ = '\\';
}
- else if (isprint((unsigned char) *vp))
- *rp++ = *vp;
- else
+ else if ((unsigned char) *vp < 0x20 || (unsigned char) *vp > 0x7e)
{
val = *vp;
rp[0] = '\\';
rp[1] = DIG(val & 03);
rp += 4;
}
+ else
+ *rp++ = *vp;
}
*rp = '\0';
PG_RETURN_CSTRING(result);
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/interfaces/libpq/fe-exec.c,v 1.154 2003/11/29 19:52:11 pgsql Exp $
+ * $PostgreSQL: pgsql/src/interfaces/libpq/fe-exec.c,v 1.155 2003/11/30 20:55:09 joe Exp $
*
*-------------------------------------------------------------------------
*/
* '\0' == ASCII 0 == \\000
* '\'' == ASCII 39 == \'
* '\\' == ASCII 92 == \\\\
- * anything >= 0x80 ---> \\ooo (where ooo is an octal expression)
+ * anything < 0x20, or > 0x7e ---> \\ooo
+ * (where ooo is an octal expression)
*/
unsigned char *
PQescapeBytea(const unsigned char *bintext, size_t binlen, size_t *bytealen)
vp = bintext;
for (i = binlen; i > 0; i--, vp++)
{
- if (*vp == 0 || *vp >= 0x80)
+ if (*vp < 0x20 || *vp > 0x7e)
len += 5; /* '5' is for '\\ooo' */
else if (*vp == '\'')
len += 2;
for (i = binlen; i > 0; i--, vp++)
{
- if (*vp == 0 || *vp >= 0x80)
+ if (*vp < 0x20 || *vp > 0x7e)
{
(void) sprintf(rp, "\\\\%03o", *vp);
rp += 5;