PostgreSQL Source Code  git master
pqformat.c File Reference
#include "postgres.h"
#include <sys/param.h>
#include "libpq/libpq.h"
#include "libpq/pqformat.h"
#include "mb/pg_wchar.h"
#include "port/pg_bswap.h"
Include dependency graph for pqformat.c:

Go to the source code of this file.

Functions

void pq_beginmessage (StringInfo buf, char msgtype)
 
void pq_beginmessage_reuse (StringInfo buf, char msgtype)
 
void pq_sendbytes (StringInfo buf, const char *data, int datalen)
 
void pq_sendcountedtext (StringInfo buf, const char *str, int slen, bool countincludesself)
 
void pq_sendtext (StringInfo buf, const char *str, int slen)
 
void pq_sendstring (StringInfo buf, const char *str)
 
void pq_send_ascii_string (StringInfo buf, const char *str)
 
void pq_sendfloat4 (StringInfo buf, float4 f)
 
void pq_sendfloat8 (StringInfo buf, float8 f)
 
void pq_endmessage (StringInfo buf)
 
void pq_endmessage_reuse (StringInfo buf)
 
void pq_begintypsend (StringInfo buf)
 
byteapq_endtypsend (StringInfo buf)
 
void pq_puttextmessage (char msgtype, const char *str)
 
void pq_putemptymessage (char msgtype)
 
int pq_getmsgbyte (StringInfo msg)
 
unsigned int pq_getmsgint (StringInfo msg, int b)
 
int64 pq_getmsgint64 (StringInfo msg)
 
float4 pq_getmsgfloat4 (StringInfo msg)
 
float8 pq_getmsgfloat8 (StringInfo msg)
 
const char * pq_getmsgbytes (StringInfo msg, int datalen)
 
void pq_copymsgbytes (StringInfo msg, char *buf, int datalen)
 
char * pq_getmsgtext (StringInfo msg, int rawbytes, int *nbytes)
 
const char * pq_getmsgstring (StringInfo msg)
 
const char * pq_getmsgrawstring (StringInfo msg)
 
void pq_getmsgend (StringInfo msg)
 

Function Documentation

◆ pq_beginmessage()

void pq_beginmessage ( StringInfo  buf,
char  msgtype 
)

Definition at line 87 of file pqformat.c.

88 {
90 
91  /*
92  * We stash the message type into the buffer's cursor field, expecting
93  * that the pq_sendXXX routines won't touch it. We could alternatively
94  * make it the first byte of the buffer contents, but this seems easier.
95  */
96  buf->cursor = msgtype;
97 }
static char * buf
Definition: pg_test_fsync.c:70
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59

References buf, and initStringInfo().

Referenced by NotifyMyFrontEnd(), ParallelWorkerMain(), PostgresMain(), printsimple(), printsimple_startup(), ReadyForQuery(), ReceiveCopyBegin(), ReportGUCOption(), send_message_to_frontend(), sendAuthRequest(), SendCopyBegin(), SendCopyOutResponse(), SendFunctionResult(), SendNegotiateProtocolVersion(), SendTablespaceList(), SendTimeLineHistory(), SendXlogRecPtrResult(), StartLogicalReplication(), and StartReplication().

◆ pq_beginmessage_reuse()

void pq_beginmessage_reuse ( StringInfo  buf,
char  msgtype 
)

Definition at line 108 of file pqformat.c.

109 {
111 
112  /*
113  * We stash the message type into the buffer's cursor field, expecting
114  * that the pq_sendXXX routines won't touch it. We could alternatively
115  * make it the first byte of the buffer contents, but this seems easier.
116  */
117  buf->cursor = msgtype;
118 }
void resetStringInfo(StringInfo str)
Definition: stringinfo.c:75

References buf, and resetStringInfo().

Referenced by exec_describe_statement_message(), printtup(), and SendRowDescriptionMessage().

◆ pq_begintypsend()

◆ pq_copymsgbytes()

void pq_copymsgbytes ( StringInfo  msg,
char *  buf,
int  datalen 
)

Definition at line 530 of file pqformat.c.

531 {
532  if (datalen < 0 || datalen > (msg->len - msg->cursor))
533  ereport(ERROR,
534  (errcode(ERRCODE_PROTOCOL_VIOLATION),
535  errmsg("insufficient data left in message")));
536  memcpy(buf, &msg->data[msg->cursor], datalen);
537  msg->cursor += datalen;
538 }
int errcode(int sqlerrcode)
Definition: elog.c:698
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define ERROR
Definition: elog.h:33
#define ereport(elevel,...)
Definition: elog.h:143

References buf, StringInfoData::cursor, StringInfoData::data, ereport, errcode(), errmsg(), ERROR, and StringInfoData::len.

Referenced by bit_recv(), bytearecv(), CopyGetData(), logicalrep_read_tuple(), pq_getmsgint(), pq_getmsgint64(), and varbit_recv().

◆ pq_endmessage()

void pq_endmessage ( StringInfo  buf)

Definition at line 298 of file pqformat.c.

299 {
300  /* msgtype was saved in cursor field */
301  (void) pq_putmessage(buf->cursor, buf->data, buf->len);
302  /* no need to complain about any failure, since pqcomm.c already did */
303  pfree(buf->data);
304  buf->data = NULL;
305 }
#define pq_putmessage(msgtype, s, len)
Definition: libpq.h:49
void pfree(void *pointer)
Definition: mcxt.c:1169

References buf, pfree(), and pq_putmessage.

Referenced by HandleParallelMessage(), NotifyMyFrontEnd(), ParallelWorkerMain(), PostgresMain(), printsimple(), printsimple_startup(), ReadyForQuery(), ReceiveCopyBegin(), ReportGUCOption(), send_message_to_frontend(), sendAuthRequest(), SendCopyBegin(), SendCopyOutResponse(), SendFunctionResult(), SendNegotiateProtocolVersion(), SendTablespaceList(), SendTimeLineHistory(), SendXlogRecPtrResult(), StartLogicalReplication(), and StartReplication().

◆ pq_endmessage_reuse()

void pq_endmessage_reuse ( StringInfo  buf)

Definition at line 316 of file pqformat.c.

317 {
318  /* msgtype was saved in cursor field */
319  (void) pq_putmessage(buf->cursor, buf->data, buf->len);
320 }

References buf, and pq_putmessage.

Referenced by exec_describe_statement_message(), printtup(), and SendRowDescriptionMessage().

◆ pq_endtypsend()

◆ pq_getmsgbyte()

◆ pq_getmsgbytes()

const char* pq_getmsgbytes ( StringInfo  msg,
int  datalen 
)

Definition at line 510 of file pqformat.c.

511 {
512  const char *result;
513 
514  if (datalen < 0 || datalen > (msg->len - msg->cursor))
515  ereport(ERROR,
516  (errcode(ERRCODE_PROTOCOL_VIOLATION),
517  errmsg("insufficient data left in message")));
518  result = &msg->data[msg->cursor];
519  msg->cursor += datalen;
520  return result;
521 }

References StringInfoData::cursor, StringInfoData::data, ereport, errcode(), errmsg(), ERROR, and StringInfoData::len.

Referenced by CheckSASLAuth(), exec_bind_message(), multirange_recv(), parse_fcall_arguments(), range_recv(), uuid_recv(), and xml_recv().

◆ pq_getmsgend()

void pq_getmsgend ( StringInfo  msg)

◆ pq_getmsgfloat4()

float4 pq_getmsgfloat4 ( StringInfo  msg)

Definition at line 471 of file pqformat.c.

472 {
473  union
474  {
475  float4 f;
476  uint32 i;
477  } swap;
478 
479  swap.i = pq_getmsgint(msg, 4);
480  return swap.f;
481 }
unsigned int uint32
Definition: c.h:441
float float4
Definition: c.h:564
int i
Definition: isn.c:73
unsigned int pq_getmsgint(StringInfo msg, int b)
Definition: pqformat.c:417

References i, and pq_getmsgint().

Referenced by float4recv().

◆ pq_getmsgfloat8()

float8 pq_getmsgfloat8 ( StringInfo  msg)

Definition at line 490 of file pqformat.c.

491 {
492  union
493  {
494  float8 f;
495  int64 i;
496  } swap;
497 
498  swap.i = pq_getmsgint64(msg);
499  return swap.f;
500 }
double float8
Definition: c.h:565
int64 pq_getmsgint64(StringInfo msg)
Definition: pqformat.c:455

References i, and pq_getmsgint64().

Referenced by box_recv(), circle_recv(), complex_recv(), cube_recv(), float8recv(), line_recv(), lseg_recv(), path_recv(), point_recv(), and poly_recv().

◆ pq_getmsgint()

unsigned int pq_getmsgint ( StringInfo  msg,
int  b 
)

Definition at line 417 of file pqformat.c.

418 {
419  unsigned int result;
420  unsigned char n8;
421  uint16 n16;
422  uint32 n32;
423 
424  switch (b)
425  {
426  case 1:
427  pq_copymsgbytes(msg, (char *) &n8, 1);
428  result = n8;
429  break;
430  case 2:
431  pq_copymsgbytes(msg, (char *) &n16, 2);
432  result = pg_ntoh16(n16);
433  break;
434  case 4:
435  pq_copymsgbytes(msg, (char *) &n32, 4);
436  result = pg_ntoh32(n32);
437  break;
438  default:
439  elog(ERROR, "unsupported integer size %d", b);
440  result = 0; /* keep compiler quiet */
441  break;
442  }
443  return result;
444 }
unsigned short uint16
Definition: c.h:440
#define elog(elevel,...)
Definition: elog.h:218
int b
Definition: isn.c:70
#define pg_ntoh32(x)
Definition: pg_bswap.h:125
#define pg_ntoh16(x)
Definition: pg_bswap.h:124
void pq_copymsgbytes(StringInfo msg, char *buf, int datalen)
Definition: pqformat.c:530

References b, elog, ERROR, pg_ntoh16, pg_ntoh32, and pq_copymsgbytes().

Referenced by array_recv(), bit_recv(), CheckSASLAuth(), cidrecv(), cube_recv(), date_recv(), exec_bind_message(), handle_streamed_transaction(), HandleFunctionRequest(), HandleParallelMessage(), hstore_recv(), int2recv(), int4recv(), interval_recv(), jsonb_recv(), jsonpath_recv(), logicalrep_read_attrs(), logicalrep_read_begin(), logicalrep_read_begin_prepare(), logicalrep_read_commit_prepared(), logicalrep_read_delete(), logicalrep_read_insert(), logicalrep_read_prepare_common(), logicalrep_read_rel(), logicalrep_read_rollback_prepared(), logicalrep_read_stream_abort(), logicalrep_read_stream_commit(), logicalrep_read_stream_start(), logicalrep_read_truncate(), logicalrep_read_tuple(), logicalrep_read_typ(), logicalrep_read_update(), lquery_recv(), ltree_recv(), ltxtq_recv(), multirange_recv(), numeric_avg_deserialize(), numeric_deserialize(), numeric_recv(), numericvar_deserialize(), oidrecv(), parse_fcall_arguments(), path_recv(), pg_snapshot_recv(), poly_recv(), PostgresMain(), pq_getmsgfloat4(), ProcessStandbyHSFeedbackMessage(), range_recv(), ReadArrayBinary(), record_recv(), tidrecv(), timetz_recv(), tsqueryrecv(), tsvectorrecv(), varbit_recv(), and xidrecv().

◆ pq_getmsgint64()

◆ pq_getmsgrawstring()

const char* pq_getmsgrawstring ( StringInfo  msg)

Definition at line 610 of file pqformat.c.

611 {
612  char *str;
613  int slen;
614 
615  str = &msg->data[msg->cursor];
616 
617  /*
618  * It's safe to use strlen() here because a StringInfo is guaranteed to
619  * have a trailing null byte. But check we found a null inside the
620  * message.
621  */
622  slen = strlen(str);
623  if (msg->cursor + slen >= msg->len)
624  ereport(ERROR,
625  (errcode(ERRCODE_PROTOCOL_VIOLATION),
626  errmsg("invalid string in message")));
627  msg->cursor += slen + 1;
628 
629  return str;
630 }

References StringInfoData::cursor, StringInfoData::data, ereport, errcode(), errmsg(), ERROR, StringInfoData::len, and generate_unaccent_rules::str.

Referenced by CheckSASLAuth(), HandleParallelMessage(), and pq_parse_errornotice().

◆ pq_getmsgstring()

const char* pq_getmsgstring ( StringInfo  msg)

Definition at line 581 of file pqformat.c.

582 {
583  char *str;
584  int slen;
585 
586  str = &msg->data[msg->cursor];
587 
588  /*
589  * It's safe to use strlen() here because a StringInfo is guaranteed to
590  * have a trailing null byte. But check we found a null inside the
591  * message.
592  */
593  slen = strlen(str);
594  if (msg->cursor + slen >= msg->len)
595  ereport(ERROR,
596  (errcode(ERRCODE_PROTOCOL_VIOLATION),
597  errmsg("invalid string in message")));
598  msg->cursor += slen + 1;
599 
600  return pg_client_to_server(str, slen);
601 }
char * pg_client_to_server(const char *s, int len)
Definition: mbutils.c:660

References StringInfoData::cursor, StringInfoData::data, ereport, errcode(), errmsg(), ERROR, StringInfoData::len, pg_client_to_server(), and generate_unaccent_rules::str.

Referenced by CopyGetData(), exec_bind_message(), logicalrep_read_attrs(), logicalrep_read_begin_prepare(), logicalrep_read_commit_prepared(), logicalrep_read_namespace(), logicalrep_read_origin(), logicalrep_read_prepare_common(), logicalrep_read_rel(), logicalrep_read_rollback_prepared(), logicalrep_read_typ(), PostgresMain(), tsqueryrecv(), and tsvectorrecv().

◆ pq_getmsgtext()

char* pq_getmsgtext ( StringInfo  msg,
int  rawbytes,
int *  nbytes 
)

Definition at line 548 of file pqformat.c.

549 {
550  char *str;
551  char *p;
552 
553  if (rawbytes < 0 || rawbytes > (msg->len - msg->cursor))
554  ereport(ERROR,
555  (errcode(ERRCODE_PROTOCOL_VIOLATION),
556  errmsg("insufficient data left in message")));
557  str = &msg->data[msg->cursor];
558  msg->cursor += rawbytes;
559 
560  p = pg_client_to_server(str, rawbytes);
561  if (p != str) /* actual conversion has been done? */
562  *nbytes = strlen(p);
563  else
564  {
565  p = (char *) palloc(rawbytes + 1);
566  memcpy(p, str, rawbytes);
567  p[rawbytes] = '\0';
568  *nbytes = rawbytes;
569  }
570  return p;
571 }
void * palloc(Size size)
Definition: mcxt.c:1062

References StringInfoData::cursor, StringInfoData::data, ereport, errcode(), errmsg(), ERROR, StringInfoData::len, palloc(), pg_client_to_server(), and generate_unaccent_rules::str.

Referenced by bpcharrecv(), cstring_recv(), enum_recv(), hstore_recv(), json_recv(), jsonb_recv(), jsonpath_recv(), lquery_recv(), ltree_recv(), ltxtq_recv(), namerecv(), textrecv(), unknownrecv(), and varcharrecv().

◆ pq_putemptymessage()

void pq_putemptymessage ( char  msgtype)

◆ pq_puttextmessage()

void pq_puttextmessage ( char  msgtype,
const char *  str 
)

Definition at line 369 of file pqformat.c.

370 {
371  int slen = strlen(str);
372  char *p;
373 
374  p = pg_server_to_client(str, slen);
375  if (p != str) /* actual conversion has been done? */
376  {
377  (void) pq_putmessage(msgtype, p, strlen(p) + 1);
378  pfree(p);
379  return;
380  }
381  (void) pq_putmessage(msgtype, str, slen + 1);
382 }
char * pg_server_to_client(const char *s, int len)
Definition: mbutils.c:738

References pfree(), pg_server_to_client(), pq_putmessage, and generate_unaccent_rules::str.

Referenced by bbsink_copytblspc_begin_backup(), and SendXlogRecPtrResult().

◆ pq_send_ascii_string()

void pq_send_ascii_string ( StringInfo  buf,
const char *  str 
)

Definition at line 229 of file pqformat.c.

230 {
231  while (*str)
232  {
233  char ch = *str++;
234 
235  if (IS_HIGHBIT_SET(ch))
236  ch = '?';
238  }
239  appendStringInfoChar(buf, '\0');
240 }
#define IS_HIGHBIT_SET(ch)
Definition: c.h:1153
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188

References appendStringInfoChar(), appendStringInfoCharMacro, buf, IS_HIGHBIT_SET, and generate_unaccent_rules::str.

Referenced by err_sendstring().

◆ pq_sendbytes()

void pq_sendbytes ( StringInfo  buf,
const char *  data,
int  datalen 
)

Definition at line 125 of file pqformat.c.

126 {
127  /* use variant that maintains a trailing null-byte, out of caution */
128  appendBinaryStringInfo(buf, data, datalen);
129 }
const void * data
void appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
Definition: stringinfo.c:227

References appendBinaryStringInfo(), buf, and data.

Referenced by array_send(), logicalrep_write_message(), logicalrep_write_tuple(), multirange_send(), printtup(), range_send(), record_send(), send_int8_string(), sendAuthRequest(), SendFunctionResult(), SendTablespaceList(), SendTimeLineHistory(), SendXlogRecPtrResult(), uuid_send(), and varbit_send().

◆ pq_sendcountedtext()

void pq_sendcountedtext ( StringInfo  buf,
const char *  str,
int  slen,
bool  countincludesself 
)

Definition at line 142 of file pqformat.c.

144 {
145  int extra = countincludesself ? 4 : 0;
146  char *p;
147 
148  p = pg_server_to_client(str, slen);
149  if (p != str) /* actual conversion has been done? */
150  {
151  slen = strlen(p);
152  pq_sendint32(buf, slen + extra);
153  appendBinaryStringInfoNT(buf, p, slen);
154  pfree(p);
155  }
156  else
157  {
158  pq_sendint32(buf, slen + extra);
160  }
161 }
static void pq_sendint32(StringInfo buf, uint32 i)
Definition: pqformat.h:145
void appendBinaryStringInfoNT(StringInfo str, const char *data, int datalen)
Definition: stringinfo.c:253

References appendBinaryStringInfoNT(), buf, pfree(), pg_server_to_client(), pq_sendint32(), and generate_unaccent_rules::str.

Referenced by logicalrep_write_tuple(), printsimple(), printtup(), and SendFunctionResult().

◆ pq_sendfloat4()

void pq_sendfloat4 ( StringInfo  buf,
float4  f 
)

Definition at line 254 of file pqformat.c.

255 {
256  union
257  {
258  float4 f;
259  uint32 i;
260  } swap;
261 
262  swap.f = f;
263  pq_sendint32(buf, swap.i);
264 }

References buf, i, and pq_sendint32().

Referenced by float4send().

◆ pq_sendfloat8()

void pq_sendfloat8 ( StringInfo  buf,
float8  f 
)

Definition at line 278 of file pqformat.c.

279 {
280  union
281  {
282  float8 f;
283  int64 i;
284  } swap;
285 
286  swap.f = f;
287  pq_sendint64(buf, swap.i);
288 }
static void pq_sendint64(StringInfo buf, uint64 i)
Definition: pqformat.h:153

References buf, i, and pq_sendint64().

Referenced by box_send(), circle_send(), complex_send(), cube_send(), float8send(), line_send(), lseg_send(), path_send(), point_send(), and poly_send().

◆ pq_sendstring()

◆ pq_sendtext()

void pq_sendtext ( StringInfo  buf,
const char *  str,
int  slen 
)

Definition at line 174 of file pqformat.c.

175 {
176  char *p;
177 
178  p = pg_server_to_client(str, slen);
179  if (p != str) /* actual conversion has been done? */
180  {
181  slen = strlen(p);
182  appendBinaryStringInfo(buf, p, slen);
183  pfree(p);
184  }
185  else
187 }

References appendBinaryStringInfo(), buf, pfree(), pg_server_to_client(), and generate_unaccent_rules::str.

Referenced by cstring_send(), enum_send(), hstore_send(), json_send(), jsonb_send(), jsonpath_send(), lquery_send(), ltree_send(), ltxtq_send(), namesend(), textsend(), tsvectorsend(), unknownsend(), and xml_send().