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, 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 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(), ParallelWorkerMain(), 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 531 of file pqformat.c.

532 {
533  if (datalen < 0 || datalen > (msg->len - msg->cursor))
534  ereport(ERROR,
535  (errcode(ERRCODE_PROTOCOL_VIOLATION),
536  errmsg("insufficient data left in message")));
537  memcpy(buf, &msg->data[msg->cursor], datalen);
538  msg->cursor += datalen;
539 }
int errcode(int sqlerrcode)
Definition: elog.c:860
int errmsg(const char *fmt,...)
Definition: elog.c:1075
#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)

◆ pq_endmessage_reuse()

void pq_endmessage_reuse ( StringInfo  buf)

Definition at line 317 of file pqformat.c.

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

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

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

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

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

References i, and pq_getmsgint().

Referenced by float4recv().

◆ pq_getmsgfloat8()

float8 pq_getmsgfloat8 ( StringInfo  msg)

Definition at line 491 of file pqformat.c.

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

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

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

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

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

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

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

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

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

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

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

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

231 {
232  while (*str)
233  {
234  char ch = *str++;
235 
236  if (IS_HIGHBIT_SET(ch))
237  ch = '?';
239  }
240  appendStringInfoChar(buf, '\0');
241 }
#define IS_HIGHBIT_SET(ch)
Definition: c.h:1144
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,
bool  countincludesself 
)

Definition at line 143 of file pqformat.c.

145 {
146  int extra = countincludesself ? 4 : 0;
147  char *p;
148 
149  p = pg_server_to_client(str, slen);
150  if (p != str) /* actual conversion has been done? */
151  {
152  slen = strlen(p);
153  pq_sendint32(buf, slen + extra);
154  appendBinaryStringInfoNT(buf, p, slen);
155  pfree(p);
156  }
157  else
158  {
159  pq_sendint32(buf, slen + extra);
161  }
162 }
static void pq_sendint32(StringInfo buf, uint32 i)
Definition: pqformat.h:145
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 255 of file pqformat.c.

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

References buf, i, and pq_sendint32().

Referenced by float4send().

◆ pq_sendfloat8()

void pq_sendfloat8 ( StringInfo  buf,
float8  f 
)

Definition at line 279 of file pqformat.c.

280 {
281  union
282  {
283  float8 f;
284  int64 i;
285  } swap;
286 
287  swap.f = f;
288  pq_sendint64(buf, swap.i);
289 }
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 175 of file pqformat.c.

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

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