pqformat.c 8.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*-------------------------------------------------------------------------
 *
 * 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.
 *
B
Add:  
Bruce Momjian 已提交
16 17
 * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
 * Portions Copyright (c) 1994, Regents of the University of California
18
 *
B
Add:  
Bruce Momjian 已提交
19
 *	$Id: pqformat.c,v 1.12 2000/01/26 05:56:29 momjian Exp $
20 21 22 23 24
 *
 *-------------------------------------------------------------------------
 */
/*
 * INTERFACE ROUTINES
25
 * Message assembly and output:
B
Bruce Momjian 已提交
26
 *		pq_beginmessage - initialize StringInfo buffer
27 28 29
 *		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
30
 *		pq_sendcountedtext - append a text string (with MULTIBYTE conversion)
31 32 33 34 35 36
 *		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.
 *
37 38 39
 * Special-case message output:
 *		pq_puttextmessage - generate a MULTIBYTE-converted message in one step
 *
40 41 42
 * Message input:
 *		pq_getint		- get an integer from connection
 *		pq_getstr		- get a null terminated string from connection
43 44
 * pq_getstr performs MULTIBYTE conversion on the collected string.
 * Use the raw pqcomm.c routines pq_getstring or pq_getbytes
45 46 47 48 49
 * to fetch data without conversion.
 */
#include "postgres.h"

#include "libpq/libpq.h"
B
Bruce Momjian 已提交
50 51
#include "libpq/pqformat.h"

52
#ifdef MULTIBYTE
B
Bruce Momjian 已提交
53
#include "mb/pg_wchar.h"
54 55
#endif
#ifdef HAVE_ENDIAN_H
56
#include "endian.h"
57
#endif
M
 
Marc G. Fournier 已提交
58 59 60
#ifdef HAVE_SYS_PARAM_H
#include <sys/param.h>
#endif
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119

#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)
{
	appendStringInfoChar(buf, byt);
}

/* --------------------------------
 *		pq_sendbytes	- append raw data to a StringInfo buffer
 * --------------------------------
 */
void
pq_sendbytes(StringInfo buf, const char *data, int datalen)
{
	appendBinaryStringInfo(buf, data, datalen);
}

/* --------------------------------
120
 *		pq_sendcountedtext - append a text string (with MULTIBYTE conversion)
121
 *
122 123 124 125
 * 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.
126 127 128
 * --------------------------------
 */
void
129
pq_sendcountedtext(StringInfo buf, const char *str, int slen)
130 131
{
#ifdef MULTIBYTE
132
	char	   *p;
B
Bruce Momjian 已提交
133

134
	p = (char *) pg_server_to_client((unsigned char *) str, slen);
135 136
	if (p != str)				/* actual conversion has been done? */
	{
137 138 139 140 141
		slen = strlen(p);
		pq_sendint(buf, slen + 4, 4);
		appendBinaryStringInfo(buf, p, slen);
		pfree(p);
		return;
142
	}
143
#endif
144
	pq_sendint(buf, slen + 4, 4);
145 146 147 148 149 150
	appendBinaryStringInfo(buf, str, slen);
}

/* --------------------------------
 *		pq_sendstring	- append a null-terminated text string (with MULTIBYTE)
 *
151 152
 * NB: passed text string must be null-terminated, and so is the data
 * sent to the frontend.
153 154 155
 * --------------------------------
 */
void
156
pq_sendstring(StringInfo buf, const char *str)
157
{
B
Bruce Momjian 已提交
158
	int			slen = strlen(str);
159
#ifdef MULTIBYTE
160
	char	   *p;
B
Bruce Momjian 已提交
161

162
	p = (char *) pg_server_to_client((unsigned char *) str, slen);
163 164
	if (p != str)				/* actual conversion has been done? */
	{
165 166 167 168
		slen = strlen(p);
		appendBinaryStringInfo(buf, p, slen + 1);
		pfree(p);
		return;
169
	}
170
#endif
B
Bruce Momjian 已提交
171
	appendBinaryStringInfo(buf, str, slen + 1);
172 173 174 175 176 177 178 179 180
}

/* --------------------------------
 *		pq_sendint		- append a binary integer to a StringInfo buffer
 * --------------------------------
 */
void
pq_sendint(StringInfo buf, int i, int b)
{
B
Bruce Momjian 已提交
181 182 183
	unsigned char n8;
	uint16		n16;
	uint32		n32;
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216

	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)
{
	if (pq_putmessage('\0', buf->data, buf->len))
	{
217
		snprintf(PQerrormsg, PQERRORMSG_LENGTH,
218 219 220 221 222 223 224 225
				 "FATAL: pq_endmessage failed: errno=%d\n", errno);
		fputs(PQerrormsg, stderr);
		pqdebug("%s", PQerrormsg);
	}
	pfree(buf->data);
	buf->data = NULL;
}

226 227 228 229 230 231 232 233 234 235 236 237 238
/* --------------------------------
 *		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)
{
B
Bruce Momjian 已提交
239
	int			slen = strlen(str);
240
#ifdef MULTIBYTE
241
	char	   *p;
B
Bruce Momjian 已提交
242

243
	p = (char *) pg_server_to_client((unsigned char *) str, slen);
244 245
	if (p != str)				/* actual conversion has been done? */
	{
246 247 248
		int result = pq_putmessage(msgtype, p, strlen(p) + 1);
		pfree(p);
		return result;
249 250
	}
#endif
B
Bruce Momjian 已提交
251
	return pq_putmessage(msgtype, str, slen + 1);
252 253
}

254 255 256 257 258 259 260 261 262
/* --------------------------------
 *		pq_getint - get an integer from connection
 *
 *		returns 0 if OK, EOF if trouble
 * --------------------------------
 */
int
pq_getint(int *result, int b)
{
B
Bruce Momjian 已提交
263 264 265 266
	int			status;
	unsigned char n8;
	uint16		n16;
	uint32		n32;
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284

	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:
B
Bruce Momjian 已提交
285 286 287 288

			/*
			 * if we elog(ERROR) here, we will lose sync with the
			 * frontend, so just complain to postmaster log instead...
289 290 291 292 293 294 295 296 297 298 299 300
			 */
			fprintf(stderr, "pq_getint: unsupported size %d\n", b);
			status = EOF;
			*result = 0;
			break;
	}
	return status;
}

/* --------------------------------
 *		pq_getstr - get a null terminated string from connection
 *
301 302
 *		The return value is placed in an expansible StringInfo.
 *		Note that space allocation comes from the current memory context!
303 304 305 306 307
 *
 *		returns 0 if OK, EOF if trouble
 * --------------------------------
 */
int
308
pq_getstr(StringInfo s)
309
{
310
	int			result;
311 312 313 314
#ifdef MULTIBYTE
	char	   *p;
#endif

315
	result = pq_getstring(s);
316 317

#ifdef MULTIBYTE
318 319
	p = (char *) pg_client_to_server((unsigned char *) s->data, s->len);
	if (p != s->data)			/* actual conversion has been done? */
320
	{
321 322 323 324
		/* reset s to empty, and append the new string p */
		s->len = 0;
		s->data[0] = '\0';
		appendBinaryStringInfo(s, p, strlen(p));
325
		pfree(p);
326
	}
327 328
#endif

329
	return result;
330
}