PostgreSQL Source Code  git master
pqformat.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * pqformat.h
4  * Definitions for formatting and parsing frontend/backend messages
5  *
6  * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  * src/include/libpq/pqformat.h
10  *
11  *-------------------------------------------------------------------------
12  */
13 #ifndef PQFORMAT_H
14 #define PQFORMAT_H
15 
16 #include "lib/stringinfo.h"
17 #include "mb/pg_wchar.h"
18 #include "port/pg_bswap.h"
19 
20 extern void pq_beginmessage(StringInfo buf, char msgtype);
21 extern void pq_beginmessage_reuse(StringInfo buf, char msgtype);
22 extern void pq_endmessage(StringInfo buf);
24 
25 extern void pq_sendbytes(StringInfo buf, const char *data, int datalen);
26 extern void pq_sendcountedtext(StringInfo buf, const char *str, int slen,
27  bool countincludesself);
28 extern void pq_sendtext(StringInfo buf, const char *str, int slen);
29 extern void pq_sendstring(StringInfo buf, const char *str);
30 extern void pq_send_ascii_string(StringInfo buf, const char *str);
31 extern void pq_sendfloat4(StringInfo buf, float4 f);
32 extern void pq_sendfloat8(StringInfo buf, float8 f);
33 
34 extern void pq_sendfloat4(StringInfo buf, float4 f);
35 extern void pq_sendfloat8(StringInfo buf, float8 f);
36 
37 /*
38  * Append an int8 to a StringInfo buffer, which already has enough space
39  * preallocated.
40  *
41  * The use of pg_restrict allows the compiler to optimize the code based on
42  * the assumption that buf, buf->len, buf->data and *buf->data don't
43  * overlap. Without the annotation buf->len etc cannot be kept in a register
44  * over subsequent pq_writeintN calls.
45  *
46  * The use of StringInfoData * rather than StringInfo is due to MSVC being
47  * overly picky and demanding a * before a restrict.
48  */
49 static inline void
51 {
52  int8 ni = i;
53 
54  Assert(buf->len + sizeof(int8) <= buf->maxlen);
55  memcpy((char *pg_restrict) (buf->data + buf->len), &ni, sizeof(int8));
56  buf->len += sizeof(int8);
57 }
58 
59 /*
60  * Append an int16 to a StringInfo buffer, which already has enough space
61  * preallocated.
62  */
63 static inline void
65 {
66  int16 ni = pg_hton16(i);
67 
68  Assert(buf->len + sizeof(int16) <= buf->maxlen);
69  memcpy((char *pg_restrict) (buf->data + buf->len), &ni, sizeof(int16));
70  buf->len += sizeof(int16);
71 }
72 
73 /*
74  * Append an int32 to a StringInfo buffer, which already has enough space
75  * preallocated.
76  */
77 static inline void
79 {
80  int32 ni = pg_hton32(i);
81 
82  Assert(buf->len + sizeof(int32) <= buf->maxlen);
83  memcpy((char *pg_restrict) (buf->data + buf->len), &ni, sizeof(int32));
84  buf->len += sizeof(int32);
85 }
86 
87 /*
88  * Append an int64 to a StringInfo buffer, which already has enough space
89  * preallocated.
90  */
91 static inline void
92 pq_writeint64(StringInfoData *pg_restrict buf, int64 i)
93 {
94  int64 ni = pg_hton64(i);
95 
96  Assert(buf->len + sizeof(int64) <= buf->maxlen);
97  memcpy((char *pg_restrict) (buf->data + buf->len), &ni, sizeof(int64));
98  buf->len += sizeof(int64);
99 }
100 
101 /*
102  * Append a null-terminated text string (with conversion) to a buffer with
103  * preallocated space.
104  *
105  * NB: The pre-allocated space needs to be sufficient for the string after
106  * converting to client encoding.
107  *
108  * NB: passed text string must be null-terminated, and so is the data
109  * sent to the frontend.
110  */
111 static inline void
112 pq_writestring(StringInfoData *pg_restrict buf, const char *pg_restrict str)
113 {
114  int slen = strlen(str);
115  char *p;
116 
117  p = pg_server_to_client(str, slen);
118  if (p != str) /* actual conversion has been done? */
119  slen = strlen(p);
120 
121  Assert(buf->len + slen + 1 <= buf->maxlen);
122 
123  memcpy(((char *pg_restrict) buf->data + buf->len), p, slen + 1);
124  buf->len += slen + 1;
125 
126  if (p != str)
127  pfree(p);
128 }
129 
130 /* append a binary int8 to a StringInfo buffer */
131 static inline void
133 {
134  enlargeStringInfo(buf, sizeof(int8));
135  pq_writeint8(buf, i);
136 }
137 
138 /* append a binary int16 to a StringInfo buffer */
139 static inline void
141 {
142  enlargeStringInfo(buf, sizeof(int16));
143  pq_writeint16(buf, i);
144 }
145 
146 /* append a binary int32 to a StringInfo buffer */
147 static inline void
149 {
150  enlargeStringInfo(buf, sizeof(int32));
151  pq_writeint32(buf, i);
152 }
153 
154 /* append a binary int64 to a StringInfo buffer */
155 static inline void
157 {
158  enlargeStringInfo(buf, sizeof(int64));
159  pq_writeint64(buf, i);
160 }
161 
162 /* append a binary byte to a StringInfo buffer */
163 static inline void
165 {
166  pq_sendint8(buf, byt);
167 }
168 
169 /*
170  * Append a binary integer to a StringInfo buffer
171  *
172  * This function is deprecated; prefer use of the functions above.
173  */
174 static inline void
176 {
177  switch (b)
178  {
179  case 1:
180  pq_sendint8(buf, (int8) i);
181  break;
182  case 2:
183  pq_sendint16(buf, (int16) i);
184  break;
185  case 4:
186  pq_sendint32(buf, (int32) i);
187  break;
188  default:
189  elog(ERROR, "unsupported integer size %d", b);
190  break;
191  }
192 }
193 
194 
195 extern void pq_begintypsend(StringInfo buf);
196 extern bytea *pq_endtypsend(StringInfo buf);
197 
198 extern void pq_puttextmessage(char msgtype, const char *str);
199 extern void pq_putemptymessage(char msgtype);
200 
201 extern int pq_getmsgbyte(StringInfo msg);
202 extern unsigned int pq_getmsgint(StringInfo msg, int b);
203 extern int64 pq_getmsgint64(StringInfo msg);
204 extern float4 pq_getmsgfloat4(StringInfo msg);
205 extern float8 pq_getmsgfloat8(StringInfo msg);
206 extern const char *pq_getmsgbytes(StringInfo msg, int datalen);
207 extern void pq_copymsgbytes(StringInfo msg, char *buf, int datalen);
208 extern char *pq_getmsgtext(StringInfo msg, int rawbytes, int *nbytes);
209 extern const char *pq_getmsgstring(StringInfo msg);
210 extern const char *pq_getmsgrawstring(StringInfo msg);
211 extern void pq_getmsgend(StringInfo msg);
212 
213 #endif /* PQFORMAT_H */
signed short int16
Definition: c.h:283
void pq_sendstring(StringInfo buf, const char *str)
Definition: pqformat.c:197
void pq_beginmessage_reuse(StringInfo buf, char msgtype)
Definition: pqformat.c:108
static void pq_writestring(StringInfoData *pg_restrict buf, const char *pg_restrict str)
Definition: pqformat.h:112
bytea * pq_endtypsend(StringInfo buf)
Definition: pqformat.c:348
void pq_endmessage(StringInfo buf)
Definition: pqformat.c:298
void pq_sendcountedtext(StringInfo buf, const char *str, int slen, bool countincludesself)
Definition: pqformat.c:142
#define pg_hton64(x)
Definition: pg_bswap.h:122
unsigned int pq_getmsgint(StringInfo msg, int b)
Definition: pqformat.c:417
float8 pq_getmsgfloat8(StringInfo msg)
Definition: pqformat.c:490
static void pq_sendint32(StringInfo buf, int32 i)
Definition: pqformat.h:148
char * pg_server_to_client(const char *s, int len)
Definition: mbutils.c:623
#define pg_hton16(x)
Definition: pg_bswap.h:120
void pq_sendfloat4(StringInfo buf, float4 f)
Definition: pqformat.c:254
const char * pq_getmsgstring(StringInfo msg)
Definition: pqformat.c:581
static void pq_sendint64(StringInfo buf, int64 i)
Definition: pqformat.h:156
const char * pq_getmsgrawstring(StringInfo msg)
Definition: pqformat.c:610
void pq_sendfloat8(StringInfo buf, float8 f)
Definition: pqformat.c:278
static void pq_sendbyte(StringInfo buf, int8 byt)
Definition: pqformat.h:164
int64 pq_getmsgint64(StringInfo msg)
Definition: pqformat.c:455
int pq_getmsgbyte(StringInfo msg)
Definition: pqformat.c:401
void pq_begintypsend(StringInfo buf)
Definition: pqformat.c:328
void pq_send_ascii_string(StringInfo buf, const char *str)
Definition: pqformat.c:229
static void pq_sendint8(StringInfo buf, int8 i)
Definition: pqformat.h:132
signed int int32
Definition: c.h:284
void pfree(void *pointer)
Definition: mcxt.c:949
#define ERROR
Definition: elog.h:43
double float8
Definition: c.h:429
#define pg_hton32(x)
Definition: pg_bswap.h:121
char * pq_getmsgtext(StringInfo msg, int rawbytes, int *nbytes)
Definition: pqformat.c:548
const char * pq_getmsgbytes(StringInfo msg, int datalen)
Definition: pqformat.c:510
void pq_putemptymessage(char msgtype)
Definition: pqformat.c:390
static char * buf
Definition: pg_test_fsync.c:67
void enlargeStringInfo(StringInfo str, int needed)
Definition: stringinfo.c:264
static void pq_writeint64(StringInfoData *pg_restrict buf, int64 i)
Definition: pqformat.h:92
static void pq_writeint16(StringInfoData *pg_restrict buf, int16 i)
Definition: pqformat.h:64
signed char int8
Definition: c.h:282
float float4
Definition: c.h:428
void pq_copymsgbytes(StringInfo msg, char *buf, int datalen)
Definition: pqformat.c:530
static void pq_writeint8(StringInfoData *pg_restrict buf, int8 i)
Definition: pqformat.h:50
#define Assert(condition)
Definition: c.h:670
void pq_beginmessage(StringInfo buf, char msgtype)
Definition: pqformat.c:87
void pq_getmsgend(StringInfo msg)
Definition: pqformat.c:637
void pq_puttextmessage(char msgtype, const char *str)
Definition: pqformat.c:369
static void pq_sendint16(StringInfo buf, int16 i)
Definition: pqformat.h:140
void pq_sendtext(StringInfo buf, const char *str, int slen)
Definition: pqformat.c:174
int i
void pq_sendbytes(StringInfo buf, const char *data, int datalen)
Definition: pqformat.c:125
Definition: c.h:487
static void pq_writeint32(StringInfoData *pg_restrict buf, int32 i)
Definition: pqformat.h:78
#define elog
Definition: elog.h:219
float4 pq_getmsgfloat4(StringInfo msg)
Definition: pqformat.c:471
static void pq_sendint(StringInfo buf, int i, int b)
Definition: pqformat.h:175
void pq_endmessage_reuse(StringInfo buf)
Definition: pqformat.c:316