1999-04-25 05:19:27 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* pqformat.c
|
|
|
|
* Routines for formatting and parsing frontend/backend messages
|
|
|
|
*
|
|
|
|
* Outgoing messages are built up in a StringInfo buffer (which is expansible)
|
|
|
|
* and then sent in a single call to pq_putmessage. This module provides data
|
|
|
|
* formatting/conversion routines that are needed to produce valid messages.
|
|
|
|
* Note in particular the distinction between "raw data" and "text"; raw data
|
|
|
|
* is message protocol characters and binary values that are not subject to
|
|
|
|
* MULTIBYTE conversion, while text is converted by MULTIBYTE rules.
|
|
|
|
*
|
|
|
|
* Incoming messages are read directly off the wire, as it were, but there
|
|
|
|
* are still data-conversion tasks to be performed.
|
|
|
|
*
|
2002-06-20 22:29:54 +02:00
|
|
|
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
|
2000-01-26 06:58:53 +01:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1999-04-25 05:19:27 +02:00
|
|
|
*
|
2002-08-08 08:32:26 +02:00
|
|
|
* $Id: pqformat.c,v 1.22 2002/08/08 06:32:26 ishii Exp $
|
1999-04-25 05:19:27 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* INTERFACE ROUTINES
|
1999-04-25 23:50:58 +02:00
|
|
|
* Message assembly and output:
|
1999-05-25 18:15:34 +02:00
|
|
|
* pq_beginmessage - initialize StringInfo buffer
|
1999-04-25 05:19:27 +02:00
|
|
|
* pq_sendbyte - append a raw byte to a StringInfo buffer
|
|
|
|
* pq_sendint - append a binary integer to a StringInfo buffer
|
|
|
|
* pq_sendbytes - append raw data to a StringInfo buffer
|
1999-04-25 21:27:47 +02:00
|
|
|
* pq_sendcountedtext - append a text string (with MULTIBYTE conversion)
|
1999-04-25 05:19:27 +02:00
|
|
|
* pq_sendstring - append a null-terminated text string (with MULTIBYTE)
|
|
|
|
* pq_endmessage - send the completed message to the frontend
|
|
|
|
* Note: it is also possible to append data to the StringInfo buffer using
|
|
|
|
* the regular StringInfo routines, but this is discouraged since required
|
|
|
|
* MULTIBYTE conversion may not occur.
|
|
|
|
*
|
1999-04-25 23:50:58 +02:00
|
|
|
* Special-case message output:
|
|
|
|
* pq_puttextmessage - generate a MULTIBYTE-converted message in one step
|
|
|
|
*
|
1999-04-25 05:19:27 +02:00
|
|
|
* Message input:
|
|
|
|
* pq_getint - get an integer from connection
|
|
|
|
* pq_getstr - get a null terminated string from connection
|
1999-04-25 21:27:47 +02:00
|
|
|
* pq_getstr performs MULTIBYTE conversion on the collected string.
|
|
|
|
* Use the raw pqcomm.c routines pq_getstring or pq_getbytes
|
1999-04-25 05:19:27 +02:00
|
|
|
* to fetch data without conversion.
|
|
|
|
*/
|
2000-07-08 07:30:33 +02:00
|
|
|
|
1999-04-25 05:19:27 +02:00
|
|
|
#include "postgres.h"
|
|
|
|
|
2000-07-08 07:30:33 +02:00
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/param.h>
|
|
|
|
|
1999-04-25 05:19:27 +02:00
|
|
|
#include "libpq/libpq.h"
|
1999-07-16 07:00:38 +02:00
|
|
|
#include "libpq/pqformat.h"
|
|
|
|
#include "mb/pg_wchar.h"
|
1999-04-25 05:19:27 +02:00
|
|
|
#ifdef HAVE_ENDIAN_H
|
2000-07-13 00:59:15 +02:00
|
|
|
#include <endian.h>
|
1999-04-25 05:19:27 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef BYTE_ORDER
|
|
|
|
#error BYTE_ORDER must be defined as LITTLE_ENDIAN, BIG_ENDIAN or PDP_ENDIAN
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if BYTE_ORDER == LITTLE_ENDIAN
|
|
|
|
|
|
|
|
#define ntoh_s(n) n
|
|
|
|
#define ntoh_l(n) n
|
|
|
|
#define hton_s(n) n
|
|
|
|
#define hton_l(n) n
|
|
|
|
|
|
|
|
#else
|
|
|
|
#if BYTE_ORDER == BIG_ENDIAN
|
|
|
|
|
|
|
|
#define ntoh_s(n) (uint16)((((uint16)n & 0x00ff) << 8) | \
|
|
|
|
(((uint16)n & 0xff00) >> 8))
|
|
|
|
#define ntoh_l(n) (uint32)((((uint32)n & 0x000000ff) << 24) | \
|
|
|
|
(((uint32)n & 0x0000ff00) << 8) | \
|
|
|
|
(((uint32)n & 0x00ff0000) >> 8) | \
|
|
|
|
(((uint32)n & 0xff000000) >> 24))
|
|
|
|
#define hton_s(n) (ntoh_s(n))
|
|
|
|
#define hton_l(n) (ntoh_l(n))
|
|
|
|
|
|
|
|
#else
|
|
|
|
#if BYTE_ORDER == PDP_ENDIAN
|
|
|
|
|
|
|
|
#error PDP_ENDIAN macros not written yet
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#error BYTE_ORDER not defined as anything understood
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/* --------------------------------
|
|
|
|
* pq_sendbyte - append a raw byte to a StringInfo buffer
|
|
|
|
* --------------------------------
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
pq_sendbyte(StringInfo buf, int byt)
|
|
|
|
{
|
2001-12-04 21:57:22 +01:00
|
|
|
appendStringInfoCharMacro(buf, byt);
|
1999-04-25 05:19:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------
|
|
|
|
* pq_sendbytes - append raw data to a StringInfo buffer
|
|
|
|
* --------------------------------
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
pq_sendbytes(StringInfo buf, const char *data, int datalen)
|
|
|
|
{
|
|
|
|
appendBinaryStringInfo(buf, data, datalen);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------
|
1999-04-25 21:27:47 +02:00
|
|
|
* pq_sendcountedtext - append a text string (with MULTIBYTE conversion)
|
1999-04-25 05:19:27 +02:00
|
|
|
*
|
1999-04-25 21:27:47 +02:00
|
|
|
* The data sent to the frontend by this routine is a 4-byte count field
|
|
|
|
* (the count includes itself, by convention) followed by the string.
|
|
|
|
* The passed text string need not be null-terminated, and the data sent
|
|
|
|
* to the frontend isn't either.
|
1999-04-25 05:19:27 +02:00
|
|
|
* --------------------------------
|
|
|
|
*/
|
|
|
|
void
|
1999-04-25 21:27:47 +02:00
|
|
|
pq_sendcountedtext(StringInfo buf, const char *str, int slen)
|
1999-04-25 05:19:27 +02:00
|
|
|
{
|
1999-09-12 00:28:11 +02:00
|
|
|
char *p;
|
1999-05-25 18:15:34 +02:00
|
|
|
|
1999-09-12 00:28:11 +02:00
|
|
|
p = (char *) pg_server_to_client((unsigned char *) str, slen);
|
1999-04-25 21:27:47 +02:00
|
|
|
if (p != str) /* actual conversion has been done? */
|
|
|
|
{
|
1999-09-12 00:28:11 +02:00
|
|
|
slen = strlen(p);
|
|
|
|
pq_sendint(buf, slen + 4, 4);
|
|
|
|
appendBinaryStringInfo(buf, p, slen);
|
|
|
|
pfree(p);
|
|
|
|
return;
|
1999-04-25 21:27:47 +02:00
|
|
|
}
|
|
|
|
pq_sendint(buf, slen + 4, 4);
|
1999-04-25 05:19:27 +02:00
|
|
|
appendBinaryStringInfo(buf, str, slen);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------
|
|
|
|
* pq_sendstring - append a null-terminated text string (with MULTIBYTE)
|
|
|
|
*
|
1999-04-25 21:27:47 +02:00
|
|
|
* NB: passed text string must be null-terminated, and so is the data
|
|
|
|
* sent to the frontend.
|
1999-04-25 05:19:27 +02:00
|
|
|
* --------------------------------
|
|
|
|
*/
|
|
|
|
void
|
1999-04-25 21:27:47 +02:00
|
|
|
pq_sendstring(StringInfo buf, const char *str)
|
1999-04-25 05:19:27 +02:00
|
|
|
{
|
1999-05-25 18:15:34 +02:00
|
|
|
int slen = strlen(str);
|
2000-04-12 19:17:23 +02:00
|
|
|
|
1999-09-12 00:28:11 +02:00
|
|
|
char *p;
|
1999-05-25 18:15:34 +02:00
|
|
|
|
1999-09-12 00:28:11 +02:00
|
|
|
p = (char *) pg_server_to_client((unsigned char *) str, slen);
|
1999-04-25 21:27:47 +02:00
|
|
|
if (p != str) /* actual conversion has been done? */
|
|
|
|
{
|
1999-09-12 00:28:11 +02:00
|
|
|
slen = strlen(p);
|
|
|
|
appendBinaryStringInfo(buf, p, slen + 1);
|
|
|
|
pfree(p);
|
|
|
|
return;
|
1999-04-25 21:27:47 +02:00
|
|
|
}
|
1999-05-25 18:15:34 +02:00
|
|
|
appendBinaryStringInfo(buf, str, slen + 1);
|
1999-04-25 05:19:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------
|
|
|
|
* pq_sendint - append a binary integer to a StringInfo buffer
|
|
|
|
* --------------------------------
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
pq_sendint(StringInfo buf, int i, int b)
|
|
|
|
{
|
1999-05-25 18:15:34 +02:00
|
|
|
unsigned char n8;
|
|
|
|
uint16 n16;
|
|
|
|
uint32 n32;
|
1999-04-25 05:19:27 +02:00
|
|
|
|
|
|
|
switch (b)
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
n8 = (unsigned char) i;
|
|
|
|
appendBinaryStringInfo(buf, (char *) &n8, 1);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
n16 = ((PG_PROTOCOL_MAJOR(FrontendProtocol) == 0) ? hton_s(i) : htons((uint16) i));
|
|
|
|
appendBinaryStringInfo(buf, (char *) &n16, 2);
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
n32 = ((PG_PROTOCOL_MAJOR(FrontendProtocol) == 0) ? hton_l(i) : htonl((uint32) i));
|
|
|
|
appendBinaryStringInfo(buf, (char *) &n32, 4);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
elog(ERROR, "pq_sendint: unsupported size %d", b);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------
|
|
|
|
* pq_endmessage - send the completed message to the frontend
|
|
|
|
*
|
|
|
|
* The data buffer is pfree()d, but if the StringInfo was allocated with
|
|
|
|
* makeStringInfo then the caller must still pfree it.
|
|
|
|
* --------------------------------
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
pq_endmessage(StringInfo buf)
|
|
|
|
{
|
2001-04-16 03:46:57 +02:00
|
|
|
(void) pq_putmessage('\0', buf->data, buf->len);
|
|
|
|
/* no need to complain about any failure, since pqcomm.c already did */
|
1999-04-25 05:19:27 +02:00
|
|
|
pfree(buf->data);
|
|
|
|
buf->data = NULL;
|
|
|
|
}
|
|
|
|
|
1999-04-25 23:50:58 +02:00
|
|
|
/* --------------------------------
|
|
|
|
* pq_puttextmessage - generate a MULTIBYTE-converted message in one step
|
|
|
|
*
|
|
|
|
* This is the same as the pqcomm.c routine pq_putmessage, except that
|
|
|
|
* the message body is a null-terminated string to which MULTIBYTE
|
|
|
|
* conversion applies.
|
|
|
|
*
|
|
|
|
* returns 0 if OK, EOF if trouble
|
|
|
|
* --------------------------------
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
pq_puttextmessage(char msgtype, const char *str)
|
|
|
|
{
|
1999-05-25 18:15:34 +02:00
|
|
|
int slen = strlen(str);
|
1999-09-12 00:28:11 +02:00
|
|
|
char *p;
|
1999-05-25 18:15:34 +02:00
|
|
|
|
1999-09-12 00:28:11 +02:00
|
|
|
p = (char *) pg_server_to_client((unsigned char *) str, slen);
|
1999-04-25 23:50:58 +02:00
|
|
|
if (p != str) /* actual conversion has been done? */
|
|
|
|
{
|
2000-04-12 19:17:23 +02:00
|
|
|
int result = pq_putmessage(msgtype, p, strlen(p) + 1);
|
|
|
|
|
1999-09-12 00:28:11 +02:00
|
|
|
pfree(p);
|
|
|
|
return result;
|
1999-04-25 23:50:58 +02:00
|
|
|
}
|
1999-05-25 18:15:34 +02:00
|
|
|
return pq_putmessage(msgtype, str, slen + 1);
|
1999-04-25 23:50:58 +02:00
|
|
|
}
|
|
|
|
|
1999-04-25 05:19:27 +02:00
|
|
|
/* --------------------------------
|
|
|
|
* pq_getint - get an integer from connection
|
|
|
|
*
|
|
|
|
* returns 0 if OK, EOF if trouble
|
|
|
|
* --------------------------------
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
pq_getint(int *result, int b)
|
|
|
|
{
|
1999-05-25 18:15:34 +02:00
|
|
|
int status;
|
|
|
|
unsigned char n8;
|
|
|
|
uint16 n16;
|
|
|
|
uint32 n32;
|
1999-04-25 05:19:27 +02:00
|
|
|
|
|
|
|
switch (b)
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
status = pq_getbytes((char *) &n8, 1);
|
|
|
|
*result = (int) n8;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
status = pq_getbytes((char *) &n16, 2);
|
|
|
|
*result = (int) ((PG_PROTOCOL_MAJOR(FrontendProtocol) == 0) ?
|
|
|
|
ntoh_s(n16) : ntohs(n16));
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
status = pq_getbytes((char *) &n32, 4);
|
|
|
|
*result = (int) ((PG_PROTOCOL_MAJOR(FrontendProtocol) == 0) ?
|
|
|
|
ntoh_l(n32) : ntohl(n32));
|
|
|
|
break;
|
|
|
|
default:
|
1999-05-25 18:15:34 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* if we elog(ERROR) here, we will lose sync with the
|
|
|
|
* frontend, so just complain to postmaster log instead...
|
1999-04-25 05:19:27 +02:00
|
|
|
*/
|
2002-03-04 02:46:04 +01:00
|
|
|
elog(COMMERROR, "pq_getint: unsupported size %d", b);
|
1999-04-25 05:19:27 +02:00
|
|
|
status = EOF;
|
|
|
|
*result = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------
|
|
|
|
* pq_getstr - get a null terminated string from connection
|
|
|
|
*
|
1999-08-31 06:26:40 +02:00
|
|
|
* The return value is placed in an expansible StringInfo.
|
|
|
|
* Note that space allocation comes from the current memory context!
|
1999-04-25 05:19:27 +02:00
|
|
|
*
|
|
|
|
* returns 0 if OK, EOF if trouble
|
|
|
|
* --------------------------------
|
|
|
|
*/
|
|
|
|
int
|
1999-08-31 06:26:40 +02:00
|
|
|
pq_getstr(StringInfo s)
|
1999-04-25 05:19:27 +02:00
|
|
|
{
|
1999-09-12 00:28:11 +02:00
|
|
|
int result;
|
1999-04-25 05:19:27 +02:00
|
|
|
char *p;
|
|
|
|
|
1999-09-12 00:28:11 +02:00
|
|
|
result = pq_getstring(s);
|
1999-04-25 05:19:27 +02:00
|
|
|
|
1999-08-31 06:26:40 +02:00
|
|
|
p = (char *) pg_client_to_server((unsigned char *) s->data, s->len);
|
|
|
|
if (p != s->data) /* actual conversion has been done? */
|
1999-04-25 21:27:47 +02:00
|
|
|
{
|
1999-08-31 06:26:40 +02:00
|
|
|
/* reset s to empty, and append the new string p */
|
|
|
|
s->len = 0;
|
|
|
|
s->data[0] = '\0';
|
|
|
|
appendBinaryStringInfo(s, p, strlen(p));
|
1999-09-12 00:28:11 +02:00
|
|
|
pfree(p);
|
1999-04-25 21:27:47 +02:00
|
|
|
}
|
1999-04-25 05:19:27 +02:00
|
|
|
|
1999-09-12 00:28:11 +02:00
|
|
|
return result;
|
1999-04-25 05:19:27 +02:00
|
|
|
}
|