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 "varatt.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 void *data, int datalen)
 
void pq_sendcountedtext (StringInfo buf, const char *str, int slen)
 
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 88 of file pqformat.c.

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

References buf, and initStringInfo().

Referenced by bbsink_copystream_archive_contents(), bbsink_copystream_begin_archive(), bbsink_copystream_begin_manifest(), bbsink_copystream_end_archive(), NotifyMyFrontEnd(), pgstat_progress_parallel_incr_param(), PostgresMain(), printsimple(), printsimple_startup(), ReadyForQuery(), ReceiveCopyBegin(), ReportGUCOption(), send_message_to_frontend(), sendAuthRequest(), SendCopyBegin(), SendCopyOutResponse(), SendFunctionResult(), SendNegotiateProtocolVersion(), SendTimeLineHistory(), StartLogicalReplication(), StartReplication(), and UploadManifest().

◆ pq_beginmessage_reuse()

void pq_beginmessage_reuse ( StringInfo  buf,
char  msgtype 
)

Definition at line 109 of file pqformat.c.

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

References buf, and resetStringInfo().

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

◆ pq_begintypsend()

void pq_begintypsend ( StringInfo  buf)

◆ pq_copymsgbytes()

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

Definition at line 528 of file pqformat.c.

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

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 296 of file pqformat.c.

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

References buf, pfree(), and pq_putmessage.

Referenced by bbsink_copystream_archive_contents(), bbsink_copystream_begin_archive(), bbsink_copystream_begin_manifest(), bbsink_copystream_end_archive(), HandleParallelMessage(), NotifyMyFrontEnd(), pgstat_progress_parallel_incr_param(), PostgresMain(), printsimple(), printsimple_startup(), ReadyForQuery(), ReceiveCopyBegin(), ReportGUCOption(), send_message_to_frontend(), sendAuthRequest(), SendCopyBegin(), SendCopyOutResponse(), SendFunctionResult(), SendNegotiateProtocolVersion(), SendTimeLineHistory(), StartLogicalReplication(), and StartReplication().

◆ pq_endmessage_reuse()

void pq_endmessage_reuse ( StringInfo  buf)

Definition at line 314 of file pqformat.c.

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

References buf, and pq_putmessage.

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

◆ pq_endtypsend()

bytea* pq_endtypsend ( StringInfo  buf)

◆ pq_getmsgbyte()

◆ pq_getmsgbytes()

const char* pq_getmsgbytes ( StringInfo  msg,
int  datalen 
)

Definition at line 508 of file pqformat.c.

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

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

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

◆ pq_getmsgend()

◆ pq_getmsgfloat4()

float4 pq_getmsgfloat4 ( StringInfo  msg)

Definition at line 469 of file pqformat.c.

470 {
471  union
472  {
473  float4 f;
474  uint32 i;
475  } swap;
476 
477  swap.i = pq_getmsgint(msg, 4);
478  return swap.f;
479 }
unsigned int uint32
Definition: c.h:493
float float4
Definition: c.h:616
int i
Definition: isn.c:73
unsigned int pq_getmsgint(StringInfo msg, int b)
Definition: pqformat.c:415

References i, and pq_getmsgint().

Referenced by float4recv().

◆ pq_getmsgfloat8()

float8 pq_getmsgfloat8 ( StringInfo  msg)

Definition at line 488 of file pqformat.c.

489 {
490  union
491  {
492  float8 f;
493  int64 i;
494  } swap;
495 
496  swap.i = pq_getmsgint64(msg);
497  return swap.f;
498 }
double float8
Definition: c.h:617
int64 pq_getmsgint64(StringInfo msg)
Definition: pqformat.c:453

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 415 of file pqformat.c.

416 {
417  unsigned int result;
418  unsigned char n8;
419  uint16 n16;
420  uint32 n32;
421 
422  switch (b)
423  {
424  case 1:
425  pq_copymsgbytes(msg, (char *) &n8, 1);
426  result = n8;
427  break;
428  case 2:
429  pq_copymsgbytes(msg, (char *) &n16, 2);
430  result = pg_ntoh16(n16);
431  break;
432  case 4:
433  pq_copymsgbytes(msg, (char *) &n32, 4);
434  result = pg_ntoh32(n32);
435  break;
436  default:
437  elog(ERROR, "unsupported integer size %d", b);
438  result = 0; /* keep compiler quiet */
439  break;
440  }
441  return result;
442 }
unsigned short uint16
Definition: c.h:492
#define elog(elevel,...)
Definition: elog.h:224
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:528

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

Referenced by array_agg_array_deserialize(), array_agg_deserialize(), array_recv(), bit_recv(), CheckSASLAuth(), cidrecv(), cube_recv(), date_recv(), exec_bind_message(), handle_streamed_transaction(), HandleFunctionRequest(), HandleParallelMessage(), hstore_recv(), int2recv(), int4recv(), interval_avg_deserialize(), 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(), string_agg_deserialize(), tidrecv(), timetz_recv(), tsqueryrecv(), tsvectorrecv(), varbit_recv(), and xidrecv().

◆ pq_getmsgint64()

◆ pq_getmsgrawstring()

const char* pq_getmsgrawstring ( StringInfo  msg)

Definition at line 608 of file pqformat.c.

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

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 579 of file pqformat.c.

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

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

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 367 of file pqformat.c.

368 {
369  int slen = strlen(str);
370  char *p;
371 
372  p = pg_server_to_client(str, slen);
373  if (p != str) /* actual conversion has been done? */
374  {
375  (void) pq_putmessage(msgtype, p, strlen(p) + 1);
376  pfree(p);
377  return;
378  }
379  (void) pq_putmessage(msgtype, str, slen + 1);
380 }
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_copystream_begin_backup(), and SendXlogRecPtrResult().

◆ pq_send_ascii_string()

void pq_send_ascii_string ( StringInfo  buf,
const char *  str 
)

Definition at line 227 of file pqformat.c.

228 {
229  while (*str)
230  {
231  char ch = *str++;
232 
233  if (IS_HIGHBIT_SET(ch))
234  ch = '?';
236  }
237  appendStringInfoChar(buf, '\0');
238 }
#define IS_HIGHBIT_SET(ch)
Definition: c.h:1142
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:194

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

Referenced by err_sendstring().

◆ pq_sendbytes()

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

Definition at line 126 of file pqformat.c.

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

References appendBinaryStringInfo(), buf, and data.

Referenced by array_agg_array_serialize(), array_agg_serialize(), array_send(), logicalrep_write_message(), logicalrep_write_tuple(), multirange_send(), printtup(), range_send(), record_send(), sendAuthRequest(), SendFunctionResult(), SendTimeLineHistory(), string_agg_serialize(), uuid_send(), and varbit_send().

◆ pq_sendcountedtext()

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

Definition at line 142 of file pqformat.c.

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

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 252 of file pqformat.c.

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

References buf, i, and pq_sendint32().

Referenced by float4send().

◆ pq_sendfloat8()

void pq_sendfloat8 ( StringInfo  buf,
float8  f 
)

Definition at line 276 of file pqformat.c.

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

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 172 of file pqformat.c.

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

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().