Header And Logo

PostgreSQL
| The world's most advanced open source database.

Functions

pqformat.c File Reference

#include "postgres.h"
#include <sys/param.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "libpq/libpq.h"
#include "libpq/pqformat.h"
#include "mb/pg_wchar.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_sendbyte (StringInfo buf, int byt)
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_sendint (StringInfo buf, int i, int b)
void pq_sendint64 (StringInfo buf, int64 i)
void pq_sendfloat4 (StringInfo buf, float4 f)
void pq_sendfloat8 (StringInfo buf, float8 f)
void pq_endmessage (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)
void pq_getmsgend (StringInfo msg)

Function Documentation

void pq_beginmessage ( StringInfo  buf,
char  msgtype 
)

Definition at line 87 of file pqformat.c.

References StringInfoData::cursor, and initStringInfo().

Referenced by exec_describe_statement_message(), IdentifySystem(), NotifyMyFrontEnd(), perform_base_backup(), PostgresMain(), printtup(), printtup_20(), printtup_internal_20(), ReadyForQuery(), ReceiveCopyBegin(), ReportGUCOption(), send_message_to_frontend(), sendAuthRequest(), SendBackupHeader(), SendCopyBegin(), SendFunctionResult(), SendRowDescriptionMessage(), SendTimeLineHistory(), SendXlogRecPtrResult(), and StartReplication().

{
    initStringInfo(buf);

    /*
     * We stash the message type into the buffer's cursor field, expecting
     * that the pq_sendXXX routines won't touch it.  We could alternatively
     * make it the first byte of the buffer contents, but this seems easier.
     */
    buf->cursor = msgtype;
}

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

Definition at line 569 of file pqformat.c.

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

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

{
    if (datalen < 0 || datalen > (msg->len - msg->cursor))
        ereport(ERROR,
                (errcode(ERRCODE_PROTOCOL_VIOLATION),
                 errmsg("insufficient data left in message")));
    memcpy(buf, &msg->data[msg->cursor], datalen);
    msg->cursor += datalen;
}

void pq_endmessage ( StringInfo  buf  ) 
bytea* pq_endtypsend ( StringInfo  buf  ) 
int pq_getmsgbyte ( StringInfo  msg  ) 

Definition at line 431 of file pqformat.c.

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

Referenced by boolrecv(), charrecv(), macaddr_recv(), network_recv(), path_recv(), PostgresMain(), ProcessStandbyMessage(), ProcessStandbyReplyMessage(), range_recv(), and XLogWalRcvProcessMsg().

{
    if (msg->cursor >= msg->len)
        ereport(ERROR,
                (errcode(ERRCODE_PROTOCOL_VIOLATION),
                 errmsg("no data left in message")));
    return (unsigned char) msg->data[msg->cursor++];
}

const char* pq_getmsgbytes ( StringInfo  msg,
int  datalen 
)

Definition at line 549 of file pqformat.c.

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

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

{
    const char *result;

    if (datalen < 0 || datalen > (msg->len - msg->cursor))
        ereport(ERROR,
                (errcode(ERRCODE_PROTOCOL_VIOLATION),
                 errmsg("insufficient data left in message")));
    result = &msg->data[msg->cursor];
    msg->cursor += datalen;
    return result;
}

void pq_getmsgend ( StringInfo  msg  ) 

Definition at line 647 of file pqformat.c.

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

Referenced by exec_bind_message(), HandleFunctionRequest(), PostgresMain(), and range_recv().

{
    if (msg->cursor != msg->len)
        ereport(ERROR,
                (errcode(ERRCODE_PROTOCOL_VIOLATION),
                 errmsg("invalid message format")));
}

float4 pq_getmsgfloat4 ( StringInfo  msg  ) 

Definition at line 510 of file pqformat.c.

References i, pq_getmsgint(), and swap.

Referenced by float4recv().

{
    union
    {
        float4      f;
        uint32      i;
    }           swap;

    swap.i = pq_getmsgint(msg, 4);
    return swap.f;
}

float8 pq_getmsgfloat8 ( StringInfo  msg  ) 
unsigned int pq_getmsgint ( StringInfo  msg,
int  b 
)

Definition at line 447 of file pqformat.c.

References elog, ERROR, and pq_copymsgbytes().

Referenced by abstimerecv(), array_recv(), bit_recv(), cidrecv(), date_recv(), exec_bind_message(), HandleFunctionRequest(), hstore_recv(), int2recv(), int4recv(), interval_recv(), numeric_recv(), oidrecv(), parse_fcall_arguments(), parse_fcall_arguments_20(), path_recv(), poly_recv(), PostgresMain(), pq_getmsgfloat4(), ProcessStandbyHSFeedbackMessage(), range_recv(), ReadArrayBinary(), record_recv(), reltimerecv(), tidrecv(), timetz_recv(), tintervalrecv(), tsqueryrecv(), tsvectorrecv(), txid_snapshot_recv(), varbit_recv(), and xidrecv().

{
    unsigned int result;
    unsigned char n8;
    uint16      n16;
    uint32      n32;

    switch (b)
    {
        case 1:
            pq_copymsgbytes(msg, (char *) &n8, 1);
            result = n8;
            break;
        case 2:
            pq_copymsgbytes(msg, (char *) &n16, 2);
            result = ntohs(n16);
            break;
        case 4:
            pq_copymsgbytes(msg, (char *) &n32, 4);
            result = ntohl(n32);
            break;
        default:
            elog(ERROR, "unsupported integer size %d", b);
            result = 0;         /* keep compiler quiet */
            break;
    }
    return result;
}

int64 pq_getmsgint64 ( StringInfo  msg  ) 

Definition at line 485 of file pqformat.c.

References pq_copymsgbytes().

Referenced by cash_recv(), int8recv(), interval_recv(), pq_getmsgfloat8(), ProcessStandbyHSFeedbackMessage(), ProcessStandbyReplyMessage(), time_recv(), timestamp_recv(), timestamptz_recv(), timetz_recv(), txid_snapshot_recv(), and XLogWalRcvProcessMsg().

{
    int64       result;
    uint32      h32;
    uint32      l32;

    pq_copymsgbytes(msg, (char *) &h32, 4);
    pq_copymsgbytes(msg, (char *) &l32, 4);
    h32 = ntohl(h32);
    l32 = ntohl(l32);

    result = h32;
    result <<= 32;
    result |= l32;

    return result;
}

const char* pq_getmsgstring ( StringInfo  msg  ) 

Definition at line 620 of file pqformat.c.

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

Referenced by CopyGetData(), exec_bind_message(), HandleFunctionRequest(), PostgresMain(), tsqueryrecv(), and tsvectorrecv().

{
    char       *str;
    int         slen;

    str = &msg->data[msg->cursor];

    /*
     * It's safe to use strlen() here because a StringInfo is guaranteed to
     * have a trailing null byte.  But check we found a null inside the
     * message.
     */
    slen = strlen(str);
    if (msg->cursor + slen >= msg->len)
        ereport(ERROR,
                (errcode(ERRCODE_PROTOCOL_VIOLATION),
                 errmsg("invalid string in message")));
    msg->cursor += slen + 1;

    return pg_client_to_server(str, slen);
}

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

Definition at line 587 of file pqformat.c.

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

Referenced by bpcharrecv(), cstring_recv(), enum_recv(), hstore_recv(), json_recv(), namerecv(), textrecv(), unknownrecv(), and varcharrecv().

{
    char       *str;
    char       *p;

    if (rawbytes < 0 || rawbytes > (msg->len - msg->cursor))
        ereport(ERROR,
                (errcode(ERRCODE_PROTOCOL_VIOLATION),
                 errmsg("insufficient data left in message")));
    str = &msg->data[msg->cursor];
    msg->cursor += rawbytes;

    p = pg_client_to_server(str, rawbytes);
    if (p != str)               /* actual conversion has been done? */
        *nbytes = strlen(p);
    else
    {
        p = (char *) palloc(rawbytes + 1);
        memcpy(p, str, rawbytes);
        p[rawbytes] = '\0';
        *nbytes = rawbytes;
    }
    return p;
}

void pq_putemptymessage ( char  msgtype  ) 
void pq_puttextmessage ( char  msgtype,
const char *  str 
)

Definition at line 399 of file pqformat.c.

References pfree(), pg_server_to_client(), and pq_putmessage().

Referenced by printtup_startup(), SendBackupHeader(), SendXlogRecPtrResult(), and StartReplication().

{
    int         slen = strlen(str);
    char       *p;

    p = pg_server_to_client(str, slen);
    if (p != str)               /* actual conversion has been done? */
    {
        (void) pq_putmessage(msgtype, p, strlen(p) + 1);
        pfree(p);
        return;
    }
    (void) pq_putmessage(msgtype, str, slen + 1);
}

void pq_send_ascii_string ( StringInfo  buf,
const char *  str 
)

Definition at line 217 of file pqformat.c.

References appendStringInfoChar(), appendStringInfoCharMacro, and IS_HIGHBIT_SET.

Referenced by err_sendstring().

{
    while (*str)
    {
        char        ch = *str++;

        if (IS_HIGHBIT_SET(ch))
            ch = '?';
        appendStringInfoCharMacro(buf, ch);
    }
    appendStringInfoChar(buf, '\0');
}

void pq_sendbyte ( StringInfo  buf,
int  byt 
)
void pq_sendbytes ( StringInfo  buf,
const char *  data,
int  datalen 
)
void pq_sendcountedtext ( StringInfo  buf,
const char *  str,
int  slen,
bool  countincludesself 
)

Definition at line 130 of file pqformat.c.

References appendBinaryStringInfo(), pfree(), pg_server_to_client(), and pq_sendint().

Referenced by printtup(), printtup_20(), and SendFunctionResult().

{
    int         extra = countincludesself ? 4 : 0;
    char       *p;

    p = pg_server_to_client(str, slen);
    if (p != str)               /* actual conversion has been done? */
    {
        slen = strlen(p);
        pq_sendint(buf, slen + extra, 4);
        appendBinaryStringInfo(buf, p, slen);
        pfree(p);
    }
    else
    {
        pq_sendint(buf, slen + extra, 4);
        appendBinaryStringInfo(buf, str, slen);
    }
}

void pq_sendfloat4 ( StringInfo  buf,
float4  f 
)

Definition at line 297 of file pqformat.c.

References appendBinaryStringInfo(), i, and swap.

Referenced by float4send().

{
    union
    {
        float4      f;
        uint32      i;
    }           swap;

    swap.f = f;
    swap.i = htonl(swap.i);

    appendBinaryStringInfo(buf, (char *) &swap.i, 4);
}

void pq_sendfloat8 ( StringInfo  buf,
float8  f 
)
void pq_sendint ( StringInfo  buf,
int  i,
int  b 
)
void pq_sendint64 ( StringInfo  buf,
int64  i 
)

Definition at line 270 of file pqformat.c.

References appendBinaryStringInfo().

Referenced by cash_send(), int8send(), interval_send(), pq_sendfloat8(), time_send(), timestamp_send(), timestamptz_send(), timetz_send(), txid_snapshot_send(), WalSndKeepalive(), XLogSend(), XLogWalRcvSendHSFeedback(), and XLogWalRcvSendReply().

{
    uint32      n32;

    /* High order half first, since we're doing MSB-first */
    n32 = (uint32) (i >> 32);
    n32 = htonl(n32);
    appendBinaryStringInfo(buf, (char *) &n32, 4);

    /* Now the low order half */
    n32 = (uint32) i;
    n32 = htonl(n32);
    appendBinaryStringInfo(buf, (char *) &n32, 4);
}

void pq_sendstring ( StringInfo  buf,
const char *  str 
)

Definition at line 185 of file pqformat.c.

References appendBinaryStringInfo(), pfree(), and pg_server_to_client().

Referenced by err_sendstring(), IdentifySystem(), NotifyMyFrontEnd(), ReportGUCOption(), SendBackupHeader(), SendRowDescriptionMessage(), SendTimeLineHistory(), SendXlogRecPtrResult(), StartReplication(), and tsquerysend().

{
    int         slen = strlen(str);
    char       *p;

    p = pg_server_to_client(str, slen);
    if (p != str)               /* actual conversion has been done? */
    {
        slen = strlen(p);
        appendBinaryStringInfo(buf, p, slen + 1);
        pfree(p);
    }
    else
        appendBinaryStringInfo(buf, str, slen + 1);
}

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

Definition at line 162 of file pqformat.c.

References appendBinaryStringInfo(), pfree(), and pg_server_to_client().

Referenced by cstring_send(), enum_send(), hstore_send(), json_send(), namesend(), textsend(), tsvectorsend(), unknownsend(), and xml_send().

{
    char       *p;

    p = pg_server_to_client(str, slen);
    if (p != str)               /* actual conversion has been done? */
    {
        slen = strlen(p);
        appendBinaryStringInfo(buf, p, slen);
        pfree(p);
    }
    else
        appendBinaryStringInfo(buf, str, slen);
}