PostgreSQL Source Code  git master
varbit.c File Reference
#include "postgres.h"
#include "access/htup_details.h"
#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/nodeFuncs.h"
#include "nodes/supportnodes.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/varbit.h"
Include dependency graph for varbit.c:

Go to the source code of this file.

Macros

#define HEXDIG(z)   ((z)<10 ? ((z)+'0') : ((z)-10+'A'))
 
#define VARBIT_PAD(vb)
 
#define VARBIT_PAD_LAST(vb, ptr)
 
#define VARBIT_CORRECTLY_PADDED(vb)   ((void) 0)
 

Functions

static VarBitbit_catenate (VarBit *arg1, VarBit *arg2)
 
static VarBitbitsubstring (VarBit *arg, int32 s, int32 l, bool length_not_specified)
 
static VarBitbit_overlay (VarBit *t1, VarBit *t2, int sp, int sl)
 
static int32 anybit_typmodin (ArrayType *ta, const char *typename)
 
static char * anybit_typmodout (int32 typmod)
 
Datum bit_in (PG_FUNCTION_ARGS)
 
Datum bit_out (PG_FUNCTION_ARGS)
 
Datum bit_recv (PG_FUNCTION_ARGS)
 
Datum bit_send (PG_FUNCTION_ARGS)
 
Datum bit (PG_FUNCTION_ARGS)
 
Datum bittypmodin (PG_FUNCTION_ARGS)
 
Datum bittypmodout (PG_FUNCTION_ARGS)
 
Datum varbit_in (PG_FUNCTION_ARGS)
 
Datum varbit_out (PG_FUNCTION_ARGS)
 
Datum varbit_recv (PG_FUNCTION_ARGS)
 
Datum varbit_send (PG_FUNCTION_ARGS)
 
Datum varbit_support (PG_FUNCTION_ARGS)
 
Datum varbit (PG_FUNCTION_ARGS)
 
Datum varbittypmodin (PG_FUNCTION_ARGS)
 
Datum varbittypmodout (PG_FUNCTION_ARGS)
 
static int32 bit_cmp (VarBit *arg1, VarBit *arg2)
 
Datum biteq (PG_FUNCTION_ARGS)
 
Datum bitne (PG_FUNCTION_ARGS)
 
Datum bitlt (PG_FUNCTION_ARGS)
 
Datum bitle (PG_FUNCTION_ARGS)
 
Datum bitgt (PG_FUNCTION_ARGS)
 
Datum bitge (PG_FUNCTION_ARGS)
 
Datum bitcmp (PG_FUNCTION_ARGS)
 
Datum bitcat (PG_FUNCTION_ARGS)
 
Datum bitsubstr (PG_FUNCTION_ARGS)
 
Datum bitsubstr_no_len (PG_FUNCTION_ARGS)
 
Datum bitoverlay (PG_FUNCTION_ARGS)
 
Datum bitoverlay_no_len (PG_FUNCTION_ARGS)
 
Datum bitlength (PG_FUNCTION_ARGS)
 
Datum bitoctetlength (PG_FUNCTION_ARGS)
 
Datum bit_and (PG_FUNCTION_ARGS)
 
Datum bit_or (PG_FUNCTION_ARGS)
 
Datum bitxor (PG_FUNCTION_ARGS)
 
Datum bitnot (PG_FUNCTION_ARGS)
 
Datum bitshiftleft (PG_FUNCTION_ARGS)
 
Datum bitshiftright (PG_FUNCTION_ARGS)
 
Datum bitfromint4 (PG_FUNCTION_ARGS)
 
Datum bittoint4 (PG_FUNCTION_ARGS)
 
Datum bitfromint8 (PG_FUNCTION_ARGS)
 
Datum bittoint8 (PG_FUNCTION_ARGS)
 
Datum bitposition (PG_FUNCTION_ARGS)
 
Datum bitsetbit (PG_FUNCTION_ARGS)
 
Datum bitgetbit (PG_FUNCTION_ARGS)
 

Macro Definition Documentation

◆ HEXDIG

#define HEXDIG (   z)    ((z)<10 ? ((z)+'0') : ((z)-10+'A'))

Definition at line 43 of file varbit.c.

Referenced by bit_out().

◆ VARBIT_CORRECTLY_PADDED

#define VARBIT_CORRECTLY_PADDED (   vb)    ((void) 0)

Definition at line 76 of file varbit.c.

Referenced by bit_out(), and varbit_out().

◆ VARBIT_PAD

#define VARBIT_PAD (   vb)
Value:
do { \
int32 pad_ = VARBITPAD(vb); \
Assert(pad_ >= 0 && pad_ < BITS_PER_BYTE); \
if (pad_ > 0) \
*(VARBITS(vb) + VARBITBYTES(vb) - 1) &= BITMASK << pad_; \
} while (0)
#define VARBITS(PTR)
Definition: varbit.h:56
#define BITS_PER_BYTE
#define VARBITBYTES(PTR)
Definition: varbit.h:58
#define VARBITPAD(PTR)
Definition: varbit.h:60
#define BITMASK
Definition: varbit.h:72

Definition at line 46 of file varbit.c.

Referenced by bit(), bit_recv(), bitsubstring(), varbit(), and varbit_recv().

◆ VARBIT_PAD_LAST

#define VARBIT_PAD_LAST (   vb,
  ptr 
)
Value:
do { \
int32 pad_ = VARBITPAD(vb); \
Assert(pad_ >= 0 && pad_ < BITS_PER_BYTE); \
if (pad_ > 0) \
*((ptr) - 1) &= BITMASK << pad_; \
} while (0)
#define BITS_PER_BYTE
#define VARBITPAD(PTR)
Definition: varbit.h:60
#define BITMASK
Definition: varbit.h:72

Definition at line 58 of file varbit.c.

Referenced by bitnot(), and bitshiftright().

Function Documentation

◆ anybit_typmodin()

static int32 anybit_typmodin ( ArrayType ta,
const char *  typename 
)
static

Definition at line 89 of file varbit.c.

References ArrayGetIntegerTypmods(), BITS_PER_BYTE, ereport, errcode(), errmsg(), ERROR, and MaxAttrSize.

Referenced by bittypmodin(), and varbittypmodin().

90 {
91  int32 typmod;
92  int32 *tl;
93  int n;
94 
95  tl = ArrayGetIntegerTypmods(ta, &n);
96 
97  /*
98  * we're not too tense about good error message here because grammar
99  * shouldn't allow wrong number of modifiers for BIT
100  */
101  if (n != 1)
102  ereport(ERROR,
103  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
104  errmsg("invalid type modifier")));
105 
106  if (*tl < 1)
107  ereport(ERROR,
108  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
109  errmsg("length for type %s must be at least 1",
110  typename)));
111  if (*tl > (MaxAttrSize * BITS_PER_BYTE))
112  ereport(ERROR,
113  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
114  errmsg("length for type %s cannot exceed %d",
115  typename, MaxAttrSize * BITS_PER_BYTE)));
116 
117  typmod = *tl;
118 
119  return typmod;
120 }
#define MaxAttrSize
Definition: htup_details.h:585
#define BITS_PER_BYTE
int32 * ArrayGetIntegerTypmods(ArrayType *arr, int *n)
Definition: arrayutils.c:200
int errcode(int sqlerrcode)
Definition: elog.c:608
signed int int32
Definition: c.h:347
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:141
int errmsg(const char *fmt,...)
Definition: elog.c:822

◆ anybit_typmodout()

static char* anybit_typmodout ( int32  typmod)
static

Definition at line 126 of file varbit.c.

References palloc(), and snprintf.

Referenced by bittypmodout(), and varbittypmodout().

127 {
128  char *res = (char *) palloc(64);
129 
130  if (typmod >= 0)
131  snprintf(res, 64, "(%d)", typmod);
132  else
133  *res = '\0';
134 
135  return res;
136 }
void * palloc(Size size)
Definition: mcxt.c:949
#define snprintf
Definition: port.h:192

◆ bit()

Definition at line 390 of file varbit.c.

References arg, ereport, errcode(), errmsg(), ERROR, Min, palloc0(), PG_GETARG_BOOL, PG_GETARG_INT32, PG_GETARG_VARBIT_P, PG_RETURN_VARBIT_P, SET_VARSIZE, VARBIT_PAD, VARBITBYTES, VARBITLEN, VARBITMAXLEN, VARBITS, and VARBITTOTALLEN.

Referenced by _hash_addovflpage(), fls(), hash_bitmap_info(), and leftmostvalue_bit().

391 {
393  int32 len = PG_GETARG_INT32(1);
394  bool isExplicit = PG_GETARG_BOOL(2);
395  VarBit *result;
396  int rlen;
397 
398  /* No work if typmod is invalid or supplied data matches it already */
399  if (len <= 0 || len > VARBITMAXLEN || len == VARBITLEN(arg))
400  PG_RETURN_VARBIT_P(arg);
401 
402  if (!isExplicit)
403  ereport(ERROR,
404  (errcode(ERRCODE_STRING_DATA_LENGTH_MISMATCH),
405  errmsg("bit string length %d does not match type bit(%d)",
406  VARBITLEN(arg), len)));
407 
408  rlen = VARBITTOTALLEN(len);
409  /* set to 0 so that string is zero-padded */
410  result = (VarBit *) palloc0(rlen);
411  SET_VARSIZE(result, rlen);
412  VARBITLEN(result) = len;
413 
414  memcpy(VARBITS(result), VARBITS(arg),
415  Min(VARBITBYTES(result), VARBITBYTES(arg)));
416 
417  /*
418  * Make sure last byte is zero-padded if needed. This is useless but safe
419  * if source data was shorter than target length (we assume the last byte
420  * of the source data was itself correctly zero-padded).
421  */
422  VARBIT_PAD(result);
423 
424  PG_RETURN_VARBIT_P(result);
425 }
#define VARBITS(PTR)
Definition: varbit.h:56
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
#define VARBITBYTES(PTR)
Definition: varbit.h:58
#define Min(x, y)
Definition: c.h:911
int errcode(int sqlerrcode)
Definition: elog.c:608
#define PG_GETARG_VARBIT_P(n)
Definition: varbit.h:47
#define VARBITTOTALLEN(BITLEN)
Definition: varbit.h:62
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:269
#define VARBITLEN(PTR)
Definition: varbit.h:54
#define VARBIT_PAD(vb)
Definition: varbit.c:46
signed int int32
Definition: c.h:347
#define VARBITMAXLEN
Definition: varbit.h:68
#define ERROR
Definition: elog.h:43
#define PG_RETURN_VARBIT_P(x)
Definition: varbit.h:49
Definition: varbit.h:30
#define ereport(elevel, rest)
Definition: elog.h:141
void * palloc0(Size size)
Definition: mcxt.c:980
int errmsg(const char *fmt,...)
Definition: elog.c:822
void * arg
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329

◆ bit_and()

Datum bit_and ( PG_FUNCTION_ARGS  )

Definition at line 1223 of file varbit.c.

References ereport, errcode(), errmsg(), ERROR, i, palloc(), PG_GETARG_VARBIT_P, PG_RETURN_VARBIT_P, SET_VARSIZE, VARBITBYTES, VARBITLEN, VARBITS, and VARSIZE.

1224 {
1225  VarBit *arg1 = PG_GETARG_VARBIT_P(0);
1226  VarBit *arg2 = PG_GETARG_VARBIT_P(1);
1227  VarBit *result;
1228  int len,
1229  bitlen1,
1230  bitlen2,
1231  i;
1232  bits8 *p1,
1233  *p2,
1234  *r;
1235 
1236  bitlen1 = VARBITLEN(arg1);
1237  bitlen2 = VARBITLEN(arg2);
1238  if (bitlen1 != bitlen2)
1239  ereport(ERROR,
1240  (errcode(ERRCODE_STRING_DATA_LENGTH_MISMATCH),
1241  errmsg("cannot AND bit strings of different sizes")));
1242 
1243  len = VARSIZE(arg1);
1244  result = (VarBit *) palloc(len);
1245  SET_VARSIZE(result, len);
1246  VARBITLEN(result) = bitlen1;
1247 
1248  p1 = VARBITS(arg1);
1249  p2 = VARBITS(arg2);
1250  r = VARBITS(result);
1251  for (i = 0; i < VARBITBYTES(arg1); i++)
1252  *r++ = *p1++ & *p2++;
1253 
1254  /* Padding is not needed as & of 0 pads is 0 */
1255 
1256  PG_RETURN_VARBIT_P(result);
1257 }
#define VARBITS(PTR)
Definition: varbit.h:56
#define VARSIZE(PTR)
Definition: postgres.h:303
#define VARBITBYTES(PTR)
Definition: varbit.h:58
int errcode(int sqlerrcode)
Definition: elog.c:608
#define PG_GETARG_VARBIT_P(n)
Definition: varbit.h:47
#define VARBITLEN(PTR)
Definition: varbit.h:54
#define ERROR
Definition: elog.h:43
#define PG_RETURN_VARBIT_P(x)
Definition: varbit.h:49
Definition: varbit.h:30
#define ereport(elevel, rest)
Definition: elog.h:141
uint8 bits8
Definition: c.h:366
void * palloc(Size size)
Definition: mcxt.c:949
int errmsg(const char *fmt,...)
Definition: elog.c:822
int i
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329

◆ bit_catenate()

static VarBit * bit_catenate ( VarBit arg1,
VarBit arg2 
)
static

Definition at line 976 of file varbit.c.

References BITMASK, BITS_PER_BYTE, ereport, errcode(), errmsg(), ERROR, palloc(), SET_VARSIZE, VARBITBYTES, VARBITEND, VARBITLEN, VARBITMAXLEN, VARBITPAD, VARBITS, and VARBITTOTALLEN.

Referenced by bit_overlay(), and bitcat().

977 {
978  VarBit *result;
979  int bitlen1,
980  bitlen2,
981  bytelen,
982  bit1pad,
983  bit2shift;
984  bits8 *pr,
985  *pa;
986 
987  bitlen1 = VARBITLEN(arg1);
988  bitlen2 = VARBITLEN(arg2);
989 
990  if (bitlen1 > VARBITMAXLEN - bitlen2)
991  ereport(ERROR,
992  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
993  errmsg("bit string length exceeds the maximum allowed (%d)",
994  VARBITMAXLEN)));
995  bytelen = VARBITTOTALLEN(bitlen1 + bitlen2);
996 
997  result = (VarBit *) palloc(bytelen);
998  SET_VARSIZE(result, bytelen);
999  VARBITLEN(result) = bitlen1 + bitlen2;
1000 
1001  /* Copy the first bitstring in */
1002  memcpy(VARBITS(result), VARBITS(arg1), VARBITBYTES(arg1));
1003 
1004  /* Copy the second bit string */
1005  bit1pad = VARBITPAD(arg1);
1006  if (bit1pad == 0)
1007  {
1008  memcpy(VARBITS(result) + VARBITBYTES(arg1), VARBITS(arg2),
1009  VARBITBYTES(arg2));
1010  }
1011  else if (bitlen2 > 0)
1012  {
1013  /* We need to shift all the bits to fit */
1014  bit2shift = BITS_PER_BYTE - bit1pad;
1015  pr = VARBITS(result) + VARBITBYTES(arg1) - 1;
1016  for (pa = VARBITS(arg2); pa < VARBITEND(arg2); pa++)
1017  {
1018  *pr |= ((*pa >> bit2shift) & BITMASK);
1019  pr++;
1020  if (pr < VARBITEND(result))
1021  *pr = (*pa << bit1pad) & BITMASK;
1022  }
1023  }
1024 
1025  /* The pad bits should be already zero at this point */
1026 
1027  return result;
1028 }
#define VARBITS(PTR)
Definition: varbit.h:56
#define BITS_PER_BYTE
#define VARBITBYTES(PTR)
Definition: varbit.h:58
int errcode(int sqlerrcode)
Definition: elog.c:608
#define VARBITTOTALLEN(BITLEN)
Definition: varbit.h:62
#define VARBITLEN(PTR)
Definition: varbit.h:54
#define VARBITMAXLEN
Definition: varbit.h:68
#define ERROR
Definition: elog.h:43
Definition: varbit.h:30
#define ereport(elevel, rest)
Definition: elog.h:141
#define VARBITPAD(PTR)
Definition: varbit.h:60
#define VARBITEND(PTR)
Definition: varbit.h:70
uint8 bits8
Definition: c.h:366
#define BITMASK
Definition: varbit.h:72
void * palloc(Size size)
Definition: mcxt.c:949
int errmsg(const char *fmt,...)
Definition: elog.c:822
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329

◆ bit_cmp()

static int32 bit_cmp ( VarBit arg1,
VarBit arg2 
)
static

Definition at line 817 of file varbit.c.

References cmp(), Min, VARBITBYTES, VARBITLEN, and VARBITS.

Referenced by bitcmp(), biteq(), bitge(), bitgt(), bitle(), bitlt(), and bitne().

818 {
819  int bitlen1,
820  bytelen1,
821  bitlen2,
822  bytelen2;
823  int32 cmp;
824 
825  bytelen1 = VARBITBYTES(arg1);
826  bytelen2 = VARBITBYTES(arg2);
827 
828  cmp = memcmp(VARBITS(arg1), VARBITS(arg2), Min(bytelen1, bytelen2));
829  if (cmp == 0)
830  {
831  bitlen1 = VARBITLEN(arg1);
832  bitlen2 = VARBITLEN(arg2);
833  if (bitlen1 != bitlen2)
834  cmp = (bitlen1 < bitlen2) ? -1 : 1;
835  }
836  return cmp;
837 }
#define VARBITS(PTR)
Definition: varbit.h:56
#define VARBITBYTES(PTR)
Definition: varbit.h:58
#define Min(x, y)
Definition: c.h:911
#define VARBITLEN(PTR)
Definition: varbit.h:54
signed int int32
Definition: c.h:347
static int cmp(const chr *x, const chr *y, size_t len)
Definition: regc_locale.c:742

◆ bit_in()

Datum bit_in ( PG_FUNCTION_ARGS  )

Definition at line 146 of file varbit.c.

References ereport, errcode(), errmsg(), ERROR, HIGHBIT, palloc0(), PG_GETARG_CSTRING, PG_GETARG_INT32, PG_GETARG_OID, PG_RETURN_VARBIT_P, SET_VARSIZE, VARBITLEN, VARBITMAXLEN, VARBITS, and VARBITTOTALLEN.

Referenced by leftmostvalue_bit(), and make_const().

147 {
148  char *input_string = PG_GETARG_CSTRING(0);
149 
150 #ifdef NOT_USED
151  Oid typelem = PG_GETARG_OID(1);
152 #endif
153  int32 atttypmod = PG_GETARG_INT32(2);
154  VarBit *result; /* The resulting bit string */
155  char *sp; /* pointer into the character string */
156  bits8 *r; /* pointer into the result */
157  int len, /* Length of the whole data structure */
158  bitlen, /* Number of bits in the bit string */
159  slen; /* Length of the input string */
160  bool bit_not_hex; /* false = hex string true = bit string */
161  int bc;
162  bits8 x = 0;
163 
164  /* Check that the first character is a b or an x */
165  if (input_string[0] == 'b' || input_string[0] == 'B')
166  {
167  bit_not_hex = true;
168  sp = input_string + 1;
169  }
170  else if (input_string[0] == 'x' || input_string[0] == 'X')
171  {
172  bit_not_hex = false;
173  sp = input_string + 1;
174  }
175  else
176  {
177  /*
178  * Otherwise it's binary. This allows things like cast('1001' as bit)
179  * to work transparently.
180  */
181  bit_not_hex = true;
182  sp = input_string;
183  }
184 
185  /*
186  * Determine bitlength from input string. MaxAllocSize ensures a regular
187  * input is small enough, but we must check hex input.
188  */
189  slen = strlen(sp);
190  if (bit_not_hex)
191  bitlen = slen;
192  else
193  {
194  if (slen > VARBITMAXLEN / 4)
195  ereport(ERROR,
196  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
197  errmsg("bit string length exceeds the maximum allowed (%d)",
198  VARBITMAXLEN)));
199  bitlen = slen * 4;
200  }
201 
202  /*
203  * Sometimes atttypmod is not supplied. If it is supplied we need to make
204  * sure that the bitstring fits.
205  */
206  if (atttypmod <= 0)
207  atttypmod = bitlen;
208  else if (bitlen != atttypmod)
209  ereport(ERROR,
210  (errcode(ERRCODE_STRING_DATA_LENGTH_MISMATCH),
211  errmsg("bit string length %d does not match type bit(%d)",
212  bitlen, atttypmod)));
213 
214  len = VARBITTOTALLEN(atttypmod);
215  /* set to 0 so that *r is always initialised and string is zero-padded */
216  result = (VarBit *) palloc0(len);
217  SET_VARSIZE(result, len);
218  VARBITLEN(result) = atttypmod;
219 
220  r = VARBITS(result);
221  if (bit_not_hex)
222  {
223  /* Parse the bit representation of the string */
224  /* We know it fits, as bitlen was compared to atttypmod */
225  x = HIGHBIT;
226  for (; *sp; sp++)
227  {
228  if (*sp == '1')
229  *r |= x;
230  else if (*sp != '0')
231  ereport(ERROR,
232  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
233  errmsg("\"%c\" is not a valid binary digit",
234  *sp)));
235 
236  x >>= 1;
237  if (x == 0)
238  {
239  x = HIGHBIT;
240  r++;
241  }
242  }
243  }
244  else
245  {
246  /* Parse the hex representation of the string */
247  for (bc = 0; *sp; sp++)
248  {
249  if (*sp >= '0' && *sp <= '9')
250  x = (bits8) (*sp - '0');
251  else if (*sp >= 'A' && *sp <= 'F')
252  x = (bits8) (*sp - 'A') + 10;
253  else if (*sp >= 'a' && *sp <= 'f')
254  x = (bits8) (*sp - 'a') + 10;
255  else
256  ereport(ERROR,
257  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
258  errmsg("\"%c\" is not a valid hexadecimal digit",
259  *sp)));
260 
261  if (bc)
262  {
263  *r++ |= x;
264  bc = 0;
265  }
266  else
267  {
268  *r = x << 4;
269  bc = 1;
270  }
271  }
272  }
273 
274  PG_RETURN_VARBIT_P(result);
275 }
#define VARBITS(PTR)
Definition: varbit.h:56
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
int errcode(int sqlerrcode)
Definition: elog.c:608
#define VARBITTOTALLEN(BITLEN)
Definition: varbit.h:62
unsigned int Oid
Definition: postgres_ext.h:31
#define VARBITLEN(PTR)
Definition: varbit.h:54
signed int int32
Definition: c.h:347
#define VARBITMAXLEN
Definition: varbit.h:68
#define ERROR
Definition: elog.h:43
#define PG_RETURN_VARBIT_P(x)
Definition: varbit.h:49
#define PG_GETARG_OID(n)
Definition: fmgr.h:270
Definition: varbit.h:30
#define ereport(elevel, rest)
Definition: elog.h:141
#define HIGHBIT
Definition: c.h:1105
uint8 bits8
Definition: c.h:366
void * palloc0(Size size)
Definition: mcxt.c:980
int errmsg(const char *fmt,...)
Definition: elog.c:822
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:272
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329

◆ bit_or()

Datum bit_or ( PG_FUNCTION_ARGS  )

Definition at line 1264 of file varbit.c.

References ereport, errcode(), errmsg(), ERROR, i, palloc(), PG_GETARG_VARBIT_P, PG_RETURN_VARBIT_P, SET_VARSIZE, VARBITBYTES, VARBITLEN, VARBITS, and VARSIZE.

1265 {
1266  VarBit *arg1 = PG_GETARG_VARBIT_P(0);
1267  VarBit *arg2 = PG_GETARG_VARBIT_P(1);
1268  VarBit *result;
1269  int len,
1270  bitlen1,
1271  bitlen2,
1272  i;
1273  bits8 *p1,
1274  *p2,
1275  *r;
1276 
1277  bitlen1 = VARBITLEN(arg1);
1278  bitlen2 = VARBITLEN(arg2);
1279  if (bitlen1 != bitlen2)
1280  ereport(ERROR,
1281  (errcode(ERRCODE_STRING_DATA_LENGTH_MISMATCH),
1282  errmsg("cannot OR bit strings of different sizes")));
1283  len = VARSIZE(arg1);
1284  result = (VarBit *) palloc(len);
1285  SET_VARSIZE(result, len);
1286  VARBITLEN(result) = bitlen1;
1287 
1288  p1 = VARBITS(arg1);
1289  p2 = VARBITS(arg2);
1290  r = VARBITS(result);
1291  for (i = 0; i < VARBITBYTES(arg1); i++)
1292  *r++ = *p1++ | *p2++;
1293 
1294  /* Padding is not needed as | of 0 pads is 0 */
1295 
1296  PG_RETURN_VARBIT_P(result);
1297 }
#define VARBITS(PTR)
Definition: varbit.h:56
#define VARSIZE(PTR)
Definition: postgres.h:303
#define VARBITBYTES(PTR)
Definition: varbit.h:58
int errcode(int sqlerrcode)
Definition: elog.c:608
#define PG_GETARG_VARBIT_P(n)
Definition: varbit.h:47
#define VARBITLEN(PTR)
Definition: varbit.h:54
#define ERROR
Definition: elog.h:43
#define PG_RETURN_VARBIT_P(x)
Definition: varbit.h:49
Definition: varbit.h:30
#define ereport(elevel, rest)
Definition: elog.h:141
uint8 bits8
Definition: c.h:366
void * palloc(Size size)
Definition: mcxt.c:949
int errmsg(const char *fmt,...)
Definition: elog.c:822
int i
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329

◆ bit_out()

Datum bit_out ( PG_FUNCTION_ARGS  )

Definition at line 279 of file varbit.c.

References HEXDIG, i, palloc(), PG_GETARG_VARBIT_P, PG_RETURN_CSTRING, VARBIT_CORRECTLY_PADDED, varbit_out(), VARBITLEN, and VARBITS.

280 {
281 #if 1
282  /* same as varbit output */
283  return varbit_out(fcinfo);
284 #else
285 
286  /*
287  * This is how one would print a hex string, in case someone wants to
288  * write a formatting function.
289  */
290  VarBit *s = PG_GETARG_VARBIT_P(0);
291  char *result,
292  *r;
293  bits8 *sp;
294  int i,
295  len,
296  bitlen;
297 
298  /* Assertion to help catch any bit functions that don't pad correctly */
300 
301  bitlen = VARBITLEN(s);
302  len = (bitlen + 3) / 4;
303  result = (char *) palloc(len + 2);
304  sp = VARBITS(s);
305  r = result;
306  *r++ = 'X';
307  /* we cheat by knowing that we store full bytes zero padded */
308  for (i = 0; i < len; i += 2, sp++)
309  {
310  *r++ = HEXDIG((*sp) >> 4);
311  *r++ = HEXDIG((*sp) & 0xF);
312  }
313 
314  /*
315  * Go back one step if we printed a hex number that was not part of the
316  * bitstring anymore
317  */
318  if (i > len)
319  r--;
320  *r = '\0';
321 
322  PG_RETURN_CSTRING(result);
323 #endif
324 }
#define VARBITS(PTR)
Definition: varbit.h:56
Datum varbit_out(PG_FUNCTION_ARGS)
Definition: varbit.c:586
#define PG_GETARG_VARBIT_P(n)
Definition: varbit.h:47
#define VARBITLEN(PTR)
Definition: varbit.h:54
Definition: varbit.h:30
uint8 bits8
Definition: c.h:366
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:352
#define VARBIT_CORRECTLY_PADDED(vb)
Definition: varbit.c:76
void * palloc(Size size)
Definition: mcxt.c:949
int i
#define HEXDIG(z)
Definition: varbit.c:43

◆ bit_overlay()

static VarBit * bit_overlay ( VarBit t1,
VarBit t2,
int  sp,
int  sl 
)
static

Definition at line 1169 of file varbit.c.

References bit_catenate(), bitsubstring(), ereport, errcode(), errmsg(), ERROR, pg_add_s32_overflow(), s1, and s2.

Referenced by bitoverlay(), and bitoverlay_no_len().

1170 {
1171  VarBit *result;
1172  VarBit *s1;
1173  VarBit *s2;
1174  int sp_pl_sl;
1175 
1176  /*
1177  * Check for possible integer-overflow cases. For negative sp, throw a
1178  * "substring length" error because that's what should be expected
1179  * according to the spec's definition of OVERLAY().
1180  */
1181  if (sp <= 0)
1182  ereport(ERROR,
1183  (errcode(ERRCODE_SUBSTRING_ERROR),
1184  errmsg("negative substring length not allowed")));
1185  if (pg_add_s32_overflow(sp, sl, &sp_pl_sl))
1186  ereport(ERROR,
1187  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1188  errmsg("integer out of range")));
1189 
1190  s1 = bitsubstring(t1, 1, sp - 1, false);
1191  s2 = bitsubstring(t1, sp_pl_sl, -1, true);
1192  result = bit_catenate(s1, t2);
1193  result = bit_catenate(result, s2);
1194 
1195  return result;
1196 }
int errcode(int sqlerrcode)
Definition: elog.c:608
static VarBit * bitsubstring(VarBit *arg, int32 s, int32 l, bool length_not_specified)
Definition: varbit.c:1054
static VarBit * bit_catenate(VarBit *arg1, VarBit *arg2)
Definition: varbit.c:976
#define ERROR
Definition: elog.h:43
char * s1
Definition: varbit.h:30
#define ereport(elevel, rest)
Definition: elog.h:141
char * s2
static bool pg_add_s32_overflow(int32 a, int32 b, int32 *result)
Definition: int.h:104
int errmsg(const char *fmt,...)
Definition: elog.c:822

◆ bit_recv()

Datum bit_recv ( PG_FUNCTION_ARGS  )

Definition at line 330 of file varbit.c.

References buf, ereport, errcode(), errmsg(), ERROR, palloc(), PG_GETARG_INT32, PG_GETARG_OID, PG_GETARG_POINTER, PG_RETURN_VARBIT_P, pq_copymsgbytes(), pq_getmsgint(), SET_VARSIZE, VARBIT_PAD, VARBITBYTES, VARBITLEN, VARBITMAXLEN, VARBITS, and VARBITTOTALLEN.

331 {
333 
334 #ifdef NOT_USED
335  Oid typelem = PG_GETARG_OID(1);
336 #endif
337  int32 atttypmod = PG_GETARG_INT32(2);
338  VarBit *result;
339  int len,
340  bitlen;
341 
342  bitlen = pq_getmsgint(buf, sizeof(int32));
343  if (bitlen < 0 || bitlen > VARBITMAXLEN)
344  ereport(ERROR,
345  (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
346  errmsg("invalid length in external bit string")));
347 
348  /*
349  * Sometimes atttypmod is not supplied. If it is supplied we need to make
350  * sure that the bitstring fits.
351  */
352  if (atttypmod > 0 && bitlen != atttypmod)
353  ereport(ERROR,
354  (errcode(ERRCODE_STRING_DATA_LENGTH_MISMATCH),
355  errmsg("bit string length %d does not match type bit(%d)",
356  bitlen, atttypmod)));
357 
358  len = VARBITTOTALLEN(bitlen);
359  result = (VarBit *) palloc(len);
360  SET_VARSIZE(result, len);
361  VARBITLEN(result) = bitlen;
362 
363  pq_copymsgbytes(buf, (char *) VARBITS(result), VARBITBYTES(result));
364 
365  /* Make sure last byte is correctly zero-padded */
366  VARBIT_PAD(result);
367 
368  PG_RETURN_VARBIT_P(result);
369 }
#define VARBITS(PTR)
Definition: varbit.h:56
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
#define VARBITBYTES(PTR)
Definition: varbit.h:58
StringInfoData * StringInfo
Definition: stringinfo.h:44
int errcode(int sqlerrcode)
Definition: elog.c:608
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:271
#define VARBITTOTALLEN(BITLEN)
Definition: varbit.h:62
unsigned int Oid
Definition: postgres_ext.h:31
#define VARBITLEN(PTR)
Definition: varbit.h:54
#define VARBIT_PAD(vb)
Definition: varbit.c:46
signed int int32
Definition: c.h:347
#define VARBITMAXLEN
Definition: varbit.h:68
#define ERROR
Definition: elog.h:43
#define PG_RETURN_VARBIT_P(x)
Definition: varbit.h:49
static char * buf
Definition: pg_test_fsync.c:67
#define PG_GETARG_OID(n)
Definition: fmgr.h:270
Definition: varbit.h:30
#define ereport(elevel, rest)
Definition: elog.h:141
void pq_copymsgbytes(StringInfo msg, char *buf, int datalen)
Definition: pqformat.c:530
void * palloc(Size size)
Definition: mcxt.c:949
int errmsg(const char *fmt,...)
Definition: elog.c:822
unsigned int pq_getmsgint(StringInfo msg, int b)
Definition: pqformat.c:417
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329

◆ bit_send()

Datum bit_send ( PG_FUNCTION_ARGS  )

Definition at line 375 of file varbit.c.

References varbit_send().

376 {
377  /* Exactly the same as varbit_send, so share code */
378  return varbit_send(fcinfo);
379 }
Datum varbit_send(PG_FUNCTION_ARGS)
Definition: varbit.c:680

◆ bitcat()

Datum bitcat ( PG_FUNCTION_ARGS  )

Definition at line 967 of file varbit.c.

References bit_catenate(), PG_GETARG_VARBIT_P, and PG_RETURN_VARBIT_P.

968 {
969  VarBit *arg1 = PG_GETARG_VARBIT_P(0);
970  VarBit *arg2 = PG_GETARG_VARBIT_P(1);
971 
972  PG_RETURN_VARBIT_P(bit_catenate(arg1, arg2));
973 }
#define PG_GETARG_VARBIT_P(n)
Definition: varbit.h:47
static VarBit * bit_catenate(VarBit *arg1, VarBit *arg2)
Definition: varbit.c:976
#define PG_RETURN_VARBIT_P(x)
Definition: varbit.h:49
Definition: varbit.h:30

◆ bitcmp()

Datum bitcmp ( PG_FUNCTION_ARGS  )

Definition at line 948 of file varbit.c.

References bit_cmp(), PG_FREE_IF_COPY, PG_GETARG_VARBIT_P, and PG_RETURN_INT32.

Referenced by leftmostvalue_bit(), and leftmostvalue_varbit().

949 {
950  VarBit *arg1 = PG_GETARG_VARBIT_P(0);
951  VarBit *arg2 = PG_GETARG_VARBIT_P(1);
952  int32 result;
953 
954  result = bit_cmp(arg1, arg2);
955 
956  PG_FREE_IF_COPY(arg1, 0);
957  PG_FREE_IF_COPY(arg2, 1);
958 
959  PG_RETURN_INT32(result);
960 }
#define PG_RETURN_INT32(x)
Definition: fmgr.h:344
#define PG_GETARG_VARBIT_P(n)
Definition: varbit.h:47
signed int int32
Definition: c.h:347
static int32 bit_cmp(VarBit *arg1, VarBit *arg2)
Definition: varbit.c:817
Definition: varbit.h:30
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:255

◆ biteq()

Datum biteq ( PG_FUNCTION_ARGS  )

Definition at line 840 of file varbit.c.

References bit_cmp(), PG_FREE_IF_COPY, PG_GETARG_VARBIT_P, PG_RETURN_BOOL, and VARBITLEN.

Referenced by gbt_biteq().

841 {
842  VarBit *arg1 = PG_GETARG_VARBIT_P(0);
843  VarBit *arg2 = PG_GETARG_VARBIT_P(1);
844  bool result;
845  int bitlen1,
846  bitlen2;
847 
848  bitlen1 = VARBITLEN(arg1);
849  bitlen2 = VARBITLEN(arg2);
850 
851  /* fast path for different-length inputs */
852  if (bitlen1 != bitlen2)
853  result = false;
854  else
855  result = (bit_cmp(arg1, arg2) == 0);
856 
857  PG_FREE_IF_COPY(arg1, 0);
858  PG_FREE_IF_COPY(arg2, 1);
859 
860  PG_RETURN_BOOL(result);
861 }
#define PG_GETARG_VARBIT_P(n)
Definition: varbit.h:47
#define VARBITLEN(PTR)
Definition: varbit.h:54
static int32 bit_cmp(VarBit *arg1, VarBit *arg2)
Definition: varbit.c:817
Definition: varbit.h:30
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:255

◆ bitfromint4()

Datum bitfromint4 ( PG_FUNCTION_ARGS  )

Definition at line 1511 of file varbit.c.

References BITMASK, Min, palloc(), PG_GETARG_INT32, PG_RETURN_VARBIT_P, SET_VARSIZE, val, VARBITLEN, VARBITMAXLEN, VARBITS, and VARBITTOTALLEN.

1512 {
1513  int32 a = PG_GETARG_INT32(0);
1514  int32 typmod = PG_GETARG_INT32(1);
1515  VarBit *result;
1516  bits8 *r;
1517  int rlen;
1518  int destbitsleft,
1519  srcbitsleft;
1520 
1521  if (typmod <= 0 || typmod > VARBITMAXLEN)
1522  typmod = 1; /* default bit length */
1523 
1524  rlen = VARBITTOTALLEN(typmod);
1525  result = (VarBit *) palloc(rlen);
1526  SET_VARSIZE(result, rlen);
1527  VARBITLEN(result) = typmod;
1528 
1529  r = VARBITS(result);
1530  destbitsleft = typmod;
1531  srcbitsleft = 32;
1532  /* drop any input bits that don't fit */
1533  srcbitsleft = Min(srcbitsleft, destbitsleft);
1534  /* sign-fill any excess bytes in output */
1535  while (destbitsleft >= srcbitsleft + 8)
1536  {
1537  *r++ = (bits8) ((a < 0) ? BITMASK : 0);
1538  destbitsleft -= 8;
1539  }
1540  /* store first fractional byte */
1541  if (destbitsleft > srcbitsleft)
1542  {
1543  unsigned int val = (unsigned int) (a >> (destbitsleft - 8));
1544 
1545  /* Force sign-fill in case the compiler implements >> as zero-fill */
1546  if (a < 0)
1547  val |= ((unsigned int) -1) << (srcbitsleft + 8 - destbitsleft);
1548  *r++ = (bits8) (val & BITMASK);
1549  destbitsleft -= 8;
1550  }
1551  /* Now srcbitsleft and destbitsleft are the same, need not track both */
1552  /* store whole bytes */
1553  while (destbitsleft >= 8)
1554  {
1555  *r++ = (bits8) ((a >> (destbitsleft - 8)) & BITMASK);
1556  destbitsleft -= 8;
1557  }
1558  /* store last fractional byte */
1559  if (destbitsleft > 0)
1560  *r = (bits8) ((a << (8 - destbitsleft)) & BITMASK);
1561 
1562  PG_RETURN_VARBIT_P(result);
1563 }
#define VARBITS(PTR)
Definition: varbit.h:56
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
#define Min(x, y)
Definition: c.h:911
#define VARBITTOTALLEN(BITLEN)
Definition: varbit.h:62
#define VARBITLEN(PTR)
Definition: varbit.h:54
signed int int32
Definition: c.h:347
#define VARBITMAXLEN
Definition: varbit.h:68
#define PG_RETURN_VARBIT_P(x)
Definition: varbit.h:49
Definition: varbit.h:30
uint8 bits8
Definition: c.h:366
#define BITMASK
Definition: varbit.h:72
void * palloc(Size size)
Definition: mcxt.c:949
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329
long val
Definition: informix.c:664

◆ bitfromint8()

Datum bitfromint8 ( PG_FUNCTION_ARGS  )

Definition at line 1591 of file varbit.c.

References BITMASK, Min, palloc(), PG_GETARG_INT32, PG_GETARG_INT64, PG_RETURN_VARBIT_P, SET_VARSIZE, val, VARBITLEN, VARBITMAXLEN, VARBITS, and VARBITTOTALLEN.

1592 {
1593  int64 a = PG_GETARG_INT64(0);
1594  int32 typmod = PG_GETARG_INT32(1);
1595  VarBit *result;
1596  bits8 *r;
1597  int rlen;
1598  int destbitsleft,
1599  srcbitsleft;
1600 
1601  if (typmod <= 0 || typmod > VARBITMAXLEN)
1602  typmod = 1; /* default bit length */
1603 
1604  rlen = VARBITTOTALLEN(typmod);
1605  result = (VarBit *) palloc(rlen);
1606  SET_VARSIZE(result, rlen);
1607  VARBITLEN(result) = typmod;
1608 
1609  r = VARBITS(result);
1610  destbitsleft = typmod;
1611  srcbitsleft = 64;
1612  /* drop any input bits that don't fit */
1613  srcbitsleft = Min(srcbitsleft, destbitsleft);
1614  /* sign-fill any excess bytes in output */
1615  while (destbitsleft >= srcbitsleft + 8)
1616  {
1617  *r++ = (bits8) ((a < 0) ? BITMASK : 0);
1618  destbitsleft -= 8;
1619  }
1620  /* store first fractional byte */
1621  if (destbitsleft > srcbitsleft)
1622  {
1623  unsigned int val = (unsigned int) (a >> (destbitsleft - 8));
1624 
1625  /* Force sign-fill in case the compiler implements >> as zero-fill */
1626  if (a < 0)
1627  val |= ((unsigned int) -1) << (srcbitsleft + 8 - destbitsleft);
1628  *r++ = (bits8) (val & BITMASK);
1629  destbitsleft -= 8;
1630  }
1631  /* Now srcbitsleft and destbitsleft are the same, need not track both */
1632  /* store whole bytes */
1633  while (destbitsleft >= 8)
1634  {
1635  *r++ = (bits8) ((a >> (destbitsleft - 8)) & BITMASK);
1636  destbitsleft -= 8;
1637  }
1638  /* store last fractional byte */
1639  if (destbitsleft > 0)
1640  *r = (bits8) ((a << (8 - destbitsleft)) & BITMASK);
1641 
1642  PG_RETURN_VARBIT_P(result);
1643 }
#define VARBITS(PTR)
Definition: varbit.h:56
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
#define Min(x, y)
Definition: c.h:911
#define VARBITTOTALLEN(BITLEN)
Definition: varbit.h:62
#define VARBITLEN(PTR)
Definition: varbit.h:54
signed int int32
Definition: c.h:347
#define VARBITMAXLEN
Definition: varbit.h:68
#define PG_RETURN_VARBIT_P(x)
Definition: varbit.h:49
Definition: varbit.h:30
uint8 bits8
Definition: c.h:366
#define BITMASK
Definition: varbit.h:72
void * palloc(Size size)
Definition: mcxt.c:949
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329
#define PG_GETARG_INT64(n)
Definition: fmgr.h:277
long val
Definition: informix.c:664

◆ bitge()

Datum bitge ( PG_FUNCTION_ARGS  )

Definition at line 933 of file varbit.c.

References bit_cmp(), PG_FREE_IF_COPY, PG_GETARG_VARBIT_P, and PG_RETURN_BOOL.

Referenced by gbt_bitge().

934 {
935  VarBit *arg1 = PG_GETARG_VARBIT_P(0);
936  VarBit *arg2 = PG_GETARG_VARBIT_P(1);
937  bool result;
938 
939  result = (bit_cmp(arg1, arg2) >= 0);
940 
941  PG_FREE_IF_COPY(arg1, 0);
942  PG_FREE_IF_COPY(arg2, 1);
943 
944  PG_RETURN_BOOL(result);
945 }
#define PG_GETARG_VARBIT_P(n)
Definition: varbit.h:47
static int32 bit_cmp(VarBit *arg1, VarBit *arg2)
Definition: varbit.c:817
Definition: varbit.h:30
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:255

◆ bitgetbit()

Datum bitgetbit ( PG_FUNCTION_ARGS  )

Definition at line 1849 of file varbit.c.

References BITS_PER_BYTE, ereport, errcode(), errmsg(), ERROR, PG_GETARG_INT32, PG_GETARG_VARBIT_P, PG_RETURN_INT32, VARBITLEN, and VARBITS.

1850 {
1851  VarBit *arg1 = PG_GETARG_VARBIT_P(0);
1852  int32 n = PG_GETARG_INT32(1);
1853  int bitlen;
1854  bits8 *p;
1855  int byteNo,
1856  bitNo;
1857 
1858  bitlen = VARBITLEN(arg1);
1859  if (n < 0 || n >= bitlen)
1860  ereport(ERROR,
1861  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
1862  errmsg("bit index %d out of valid range (0..%d)",
1863  n, bitlen - 1)));
1864 
1865  p = VARBITS(arg1);
1866 
1867  byteNo = n / BITS_PER_BYTE;
1868  bitNo = BITS_PER_BYTE - 1 - (n % BITS_PER_BYTE);
1869 
1870  if (p[byteNo] & (1 << bitNo))
1871  PG_RETURN_INT32(1);
1872  else
1873  PG_RETURN_INT32(0);
1874 }
#define VARBITS(PTR)
Definition: varbit.h:56
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
#define BITS_PER_BYTE
#define PG_RETURN_INT32(x)
Definition: fmgr.h:344
int errcode(int sqlerrcode)
Definition: elog.c:608
#define PG_GETARG_VARBIT_P(n)
Definition: varbit.h:47
#define VARBITLEN(PTR)
Definition: varbit.h:54
signed int int32
Definition: c.h:347
#define ERROR
Definition: elog.h:43
Definition: varbit.h:30
#define ereport(elevel, rest)
Definition: elog.h:141
uint8 bits8
Definition: c.h:366
int errmsg(const char *fmt,...)
Definition: elog.c:822

◆ bitgt()

Datum bitgt ( PG_FUNCTION_ARGS  )

Definition at line 918 of file varbit.c.

References bit_cmp(), PG_FREE_IF_COPY, PG_GETARG_VARBIT_P, and PG_RETURN_BOOL.

Referenced by gbt_bitgt().

919 {
920  VarBit *arg1 = PG_GETARG_VARBIT_P(0);
921  VarBit *arg2 = PG_GETARG_VARBIT_P(1);
922  bool result;
923 
924  result = (bit_cmp(arg1, arg2) > 0);
925 
926  PG_FREE_IF_COPY(arg1, 0);
927  PG_FREE_IF_COPY(arg2, 1);
928 
929  PG_RETURN_BOOL(result);
930 }
#define PG_GETARG_VARBIT_P(n)
Definition: varbit.h:47
static int32 bit_cmp(VarBit *arg1, VarBit *arg2)
Definition: varbit.c:817
Definition: varbit.h:30
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:255

◆ bitle()

Datum bitle ( PG_FUNCTION_ARGS  )

Definition at line 903 of file varbit.c.

References bit_cmp(), PG_FREE_IF_COPY, PG_GETARG_VARBIT_P, and PG_RETURN_BOOL.

Referenced by gbt_bitle().

904 {
905  VarBit *arg1 = PG_GETARG_VARBIT_P(0);
906  VarBit *arg2 = PG_GETARG_VARBIT_P(1);
907  bool result;
908 
909  result = (bit_cmp(arg1, arg2) <= 0);
910 
911  PG_FREE_IF_COPY(arg1, 0);
912  PG_FREE_IF_COPY(arg2, 1);
913 
914  PG_RETURN_BOOL(result);
915 }
#define PG_GETARG_VARBIT_P(n)
Definition: varbit.h:47
static int32 bit_cmp(VarBit *arg1, VarBit *arg2)
Definition: varbit.c:817
Definition: varbit.h:30
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:255

◆ bitlength()

Datum bitlength ( PG_FUNCTION_ARGS  )

Definition at line 1203 of file varbit.c.

References arg, PG_GETARG_VARBIT_P, PG_RETURN_INT32, and VARBITLEN.

1204 {
1206 
1207  PG_RETURN_INT32(VARBITLEN(arg));
1208 }
#define PG_RETURN_INT32(x)
Definition: fmgr.h:344
#define PG_GETARG_VARBIT_P(n)
Definition: varbit.h:47
#define VARBITLEN(PTR)
Definition: varbit.h:54
Definition: varbit.h:30
void * arg

◆ bitlt()

Datum bitlt ( PG_FUNCTION_ARGS  )

Definition at line 888 of file varbit.c.

References bit_cmp(), PG_FREE_IF_COPY, PG_GETARG_VARBIT_P, and PG_RETURN_BOOL.

Referenced by gbt_bitlt().

889 {
890  VarBit *arg1 = PG_GETARG_VARBIT_P(0);
891  VarBit *arg2 = PG_GETARG_VARBIT_P(1);
892  bool result;
893 
894  result = (bit_cmp(arg1, arg2) < 0);
895 
896  PG_FREE_IF_COPY(arg1, 0);
897  PG_FREE_IF_COPY(arg2, 1);
898 
899  PG_RETURN_BOOL(result);
900 }
#define PG_GETARG_VARBIT_P(n)
Definition: varbit.h:47
static int32 bit_cmp(VarBit *arg1, VarBit *arg2)
Definition: varbit.c:817
Definition: varbit.h:30
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:255

◆ bitne()

Datum bitne ( PG_FUNCTION_ARGS  )

Definition at line 864 of file varbit.c.

References bit_cmp(), PG_FREE_IF_COPY, PG_GETARG_VARBIT_P, PG_RETURN_BOOL, and VARBITLEN.

865 {
866  VarBit *arg1 = PG_GETARG_VARBIT_P(0);
867  VarBit *arg2 = PG_GETARG_VARBIT_P(1);
868  bool result;
869  int bitlen1,
870  bitlen2;
871 
872  bitlen1 = VARBITLEN(arg1);
873  bitlen2 = VARBITLEN(arg2);
874 
875  /* fast path for different-length inputs */
876  if (bitlen1 != bitlen2)
877  result = true;
878  else
879  result = (bit_cmp(arg1, arg2) != 0);
880 
881  PG_FREE_IF_COPY(arg1, 0);
882  PG_FREE_IF_COPY(arg2, 1);
883 
884  PG_RETURN_BOOL(result);
885 }
#define PG_GETARG_VARBIT_P(n)
Definition: varbit.h:47
#define VARBITLEN(PTR)
Definition: varbit.h:54
static int32 bit_cmp(VarBit *arg1, VarBit *arg2)
Definition: varbit.c:817
Definition: varbit.h:30
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:255

◆ bitnot()

Datum bitnot ( PG_FUNCTION_ARGS  )

Definition at line 1345 of file varbit.c.

References arg, palloc(), PG_GETARG_VARBIT_P, PG_RETURN_VARBIT_P, SET_VARSIZE, VARBIT_PAD_LAST, VARBITEND, VARBITLEN, VARBITS, and VARSIZE.

1346 {
1348  VarBit *result;
1349  bits8 *p,
1350  *r;
1351 
1352  result = (VarBit *) palloc(VARSIZE(arg));
1353  SET_VARSIZE(result, VARSIZE(arg));
1354  VARBITLEN(result) = VARBITLEN(arg);
1355 
1356  p = VARBITS(arg);
1357  r = VARBITS(result);
1358  for (; p < VARBITEND(arg); p++)
1359  *r++ = ~*p;
1360 
1361  /* Must zero-pad the result, because extra bits are surely 1's here */
1362  VARBIT_PAD_LAST(result, r);
1363 
1364  PG_RETURN_VARBIT_P(result);
1365 }
#define VARBIT_PAD_LAST(vb, ptr)
Definition: varbit.c:58
#define VARBITS(PTR)
Definition: varbit.h:56
#define VARSIZE(PTR)
Definition: postgres.h:303
#define PG_GETARG_VARBIT_P(n)
Definition: varbit.h:47
#define VARBITLEN(PTR)
Definition: varbit.h:54
#define PG_RETURN_VARBIT_P(x)
Definition: varbit.h:49
Definition: varbit.h:30
#define VARBITEND(PTR)
Definition: varbit.h:70
uint8 bits8
Definition: c.h:366
void * palloc(Size size)
Definition: mcxt.c:949
void * arg
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329

◆ bitoctetlength()

Datum bitoctetlength ( PG_FUNCTION_ARGS  )

Definition at line 1211 of file varbit.c.

References arg, PG_GETARG_VARBIT_P, PG_RETURN_INT32, and VARBITBYTES.

1212 {
1214 
1216 }
#define VARBITBYTES(PTR)
Definition: varbit.h:58
#define PG_RETURN_INT32(x)
Definition: fmgr.h:344
#define PG_GETARG_VARBIT_P(n)
Definition: varbit.h:47
Definition: varbit.h:30
void * arg

◆ bitoverlay()

Datum bitoverlay ( PG_FUNCTION_ARGS  )

Definition at line 1146 of file varbit.c.

References bit_overlay(), PG_GETARG_INT32, PG_GETARG_VARBIT_P, and PG_RETURN_VARBIT_P.

1147 {
1148  VarBit *t1 = PG_GETARG_VARBIT_P(0);
1149  VarBit *t2 = PG_GETARG_VARBIT_P(1);
1150  int sp = PG_GETARG_INT32(2); /* substring start position */
1151  int sl = PG_GETARG_INT32(3); /* substring length */
1152 
1153  PG_RETURN_VARBIT_P(bit_overlay(t1, t2, sp, sl));
1154 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
#define PG_GETARG_VARBIT_P(n)
Definition: varbit.h:47
#define PG_RETURN_VARBIT_P(x)
Definition: varbit.h:49
Definition: varbit.h:30
static VarBit * bit_overlay(VarBit *t1, VarBit *t2, int sp, int sl)
Definition: varbit.c:1169

◆ bitoverlay_no_len()

Datum bitoverlay_no_len ( PG_FUNCTION_ARGS  )

Definition at line 1157 of file varbit.c.

References bit_overlay(), PG_GETARG_INT32, PG_GETARG_VARBIT_P, PG_RETURN_VARBIT_P, and VARBITLEN.

1158 {
1159  VarBit *t1 = PG_GETARG_VARBIT_P(0);
1160  VarBit *t2 = PG_GETARG_VARBIT_P(1);
1161  int sp = PG_GETARG_INT32(2); /* substring start position */
1162  int sl;
1163 
1164  sl = VARBITLEN(t2); /* defaults to length(t2) */
1165  PG_RETURN_VARBIT_P(bit_overlay(t1, t2, sp, sl));
1166 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
#define PG_GETARG_VARBIT_P(n)
Definition: varbit.h:47
#define VARBITLEN(PTR)
Definition: varbit.h:54
#define PG_RETURN_VARBIT_P(x)
Definition: varbit.h:49
Definition: varbit.h:30
static VarBit * bit_overlay(VarBit *t1, VarBit *t2, int sp, int sl)
Definition: varbit.c:1169

◆ bitposition()

Datum bitposition ( PG_FUNCTION_ARGS  )

Definition at line 1678 of file varbit.c.

References BITMASK, BITS_PER_BYTE, cmp(), DEBUG4, elog, i, PG_GETARG_VARBIT_P, PG_RETURN_INT32, generate_unaccent_rules::str, VARBITBYTES, VARBITEND, VARBITLEN, VARBITPAD, and VARBITS.

1679 {
1681  VarBit *substr = PG_GETARG_VARBIT_P(1);
1682  int substr_length,
1683  str_length,
1684  i,
1685  is;
1686  bits8 *s, /* pointer into substring */
1687  *p; /* pointer into str */
1688  bits8 cmp, /* shifted substring byte to compare */
1689  mask1, /* mask for substring byte shifted right */
1690  mask2, /* mask for substring byte shifted left */
1691  end_mask, /* pad mask for last substring byte */
1692  str_mask; /* pad mask for last string byte */
1693  bool is_match;
1694 
1695  /* Get the substring length */
1696  substr_length = VARBITLEN(substr);
1697  str_length = VARBITLEN(str);
1698 
1699  /* String has zero length or substring longer than string, return 0 */
1700  if ((str_length == 0) || (substr_length > str_length))
1701  PG_RETURN_INT32(0);
1702 
1703  /* zero-length substring means return 1 */
1704  if (substr_length == 0)
1705  PG_RETURN_INT32(1);
1706 
1707  /* Initialise the padding masks */
1708  end_mask = BITMASK << VARBITPAD(substr);
1709  str_mask = BITMASK << VARBITPAD(str);
1710  for (i = 0; i < VARBITBYTES(str) - VARBITBYTES(substr) + 1; i++)
1711  {
1712  for (is = 0; is < BITS_PER_BYTE; is++)
1713  {
1714  is_match = true;
1715  p = VARBITS(str) + i;
1716  mask1 = BITMASK >> is;
1717  mask2 = ~mask1;
1718  for (s = VARBITS(substr);
1719  is_match && s < VARBITEND(substr); s++)
1720  {
1721  cmp = *s >> is;
1722  if (s == VARBITEND(substr) - 1)
1723  {
1724  mask1 &= end_mask >> is;
1725  if (p == VARBITEND(str) - 1)
1726  {
1727  /* Check that there is enough of str left */
1728  if (mask1 & ~str_mask)
1729  {
1730  is_match = false;
1731  break;
1732  }
1733  mask1 &= str_mask;
1734  }
1735  }
1736  is_match = ((cmp ^ *p) & mask1) == 0;
1737  if (!is_match)
1738  break;
1739  /* Move on to the next byte */
1740  p++;
1741  if (p == VARBITEND(str))
1742  {
1743  mask2 = end_mask << (BITS_PER_BYTE - is);
1744  is_match = mask2 == 0;
1745 #if 0
1746  elog(DEBUG4, "S. %d %d em=%2x sm=%2x r=%d",
1747  i, is, end_mask, mask2, is_match);
1748 #endif
1749  break;
1750  }
1751  cmp = *s << (BITS_PER_BYTE - is);
1752  if (s == VARBITEND(substr) - 1)
1753  {
1754  mask2 &= end_mask << (BITS_PER_BYTE - is);
1755  if (p == VARBITEND(str) - 1)
1756  {
1757  if (mask2 & ~str_mask)
1758  {
1759  is_match = false;
1760  break;
1761  }
1762  mask2 &= str_mask;
1763  }
1764  }
1765  is_match = ((cmp ^ *p) & mask2) == 0;
1766  }
1767  /* Have we found a match? */
1768  if (is_match)
1769  PG_RETURN_INT32(i * BITS_PER_BYTE + is + 1);
1770  }
1771  }
1772  PG_RETURN_INT32(0);
1773 }
#define VARBITS(PTR)
Definition: varbit.h:56
#define BITS_PER_BYTE
#define VARBITBYTES(PTR)
Definition: varbit.h:58
#define PG_RETURN_INT32(x)
Definition: fmgr.h:344
#define PG_GETARG_VARBIT_P(n)
Definition: varbit.h:47
#define DEBUG4
Definition: elog.h:22
#define VARBITLEN(PTR)
Definition: varbit.h:54
Definition: varbit.h:30
#define VARBITPAD(PTR)
Definition: varbit.h:60
#define VARBITEND(PTR)
Definition: varbit.h:70
uint8 bits8
Definition: c.h:366
#define BITMASK
Definition: varbit.h:72
#define elog(elevel,...)
Definition: elog.h:228
int i
static int cmp(const chr *x, const chr *y, size_t len)
Definition: regc_locale.c:742

◆ bitsetbit()

Datum bitsetbit ( PG_FUNCTION_ARGS  )

Definition at line 1787 of file varbit.c.

References BITS_PER_BYTE, ereport, errcode(), errmsg(), ERROR, palloc(), PG_GETARG_INT32, PG_GETARG_VARBIT_P, PG_RETURN_VARBIT_P, SET_VARSIZE, VARBITBYTES, VARBITLEN, VARBITS, and VARSIZE.

1788 {
1789  VarBit *arg1 = PG_GETARG_VARBIT_P(0);
1790  int32 n = PG_GETARG_INT32(1);
1791  int32 newBit = PG_GETARG_INT32(2);
1792  VarBit *result;
1793  int len,
1794  bitlen;
1795  bits8 *r,
1796  *p;
1797  int byteNo,
1798  bitNo;
1799 
1800  bitlen = VARBITLEN(arg1);
1801  if (n < 0 || n >= bitlen)
1802  ereport(ERROR,
1803  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
1804  errmsg("bit index %d out of valid range (0..%d)",
1805  n, bitlen - 1)));
1806 
1807  /*
1808  * sanity check!
1809  */
1810  if (newBit != 0 && newBit != 1)
1811  ereport(ERROR,
1812  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1813  errmsg("new bit must be 0 or 1")));
1814 
1815  len = VARSIZE(arg1);
1816  result = (VarBit *) palloc(len);
1817  SET_VARSIZE(result, len);
1818  VARBITLEN(result) = bitlen;
1819 
1820  p = VARBITS(arg1);
1821  r = VARBITS(result);
1822 
1823  memcpy(r, p, VARBITBYTES(arg1));
1824 
1825  byteNo = n / BITS_PER_BYTE;
1826  bitNo = BITS_PER_BYTE - 1 - (n % BITS_PER_BYTE);
1827 
1828  /*
1829  * Update the byte.
1830  */
1831  if (newBit == 0)
1832  r[byteNo] &= (~(1 << bitNo));
1833  else
1834  r[byteNo] |= (1 << bitNo);
1835 
1836  PG_RETURN_VARBIT_P(result);
1837 }
#define VARBITS(PTR)
Definition: varbit.h:56
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
#define BITS_PER_BYTE
#define VARSIZE(PTR)
Definition: postgres.h:303
#define VARBITBYTES(PTR)
Definition: varbit.h:58
int errcode(int sqlerrcode)
Definition: elog.c:608
#define PG_GETARG_VARBIT_P(n)
Definition: varbit.h:47
#define VARBITLEN(PTR)
Definition: varbit.h:54
signed int int32
Definition: c.h:347
#define ERROR
Definition: elog.h:43
#define PG_RETURN_VARBIT_P(x)
Definition: varbit.h:49
Definition: varbit.h:30
#define ereport(elevel, rest)
Definition: elog.h:141
uint8 bits8
Definition: c.h:366
void * palloc(Size size)
Definition: mcxt.c:949
int errmsg(const char *fmt,...)
Definition: elog.c:822
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329

◆ bitshiftleft()

Datum bitshiftleft ( PG_FUNCTION_ARGS  )

Definition at line 1372 of file varbit.c.

References arg, BITS_PER_BYTE, bitshiftright(), DirectFunctionCall2, Int32GetDatum, MemSet, palloc(), PG_GETARG_INT32, PG_GETARG_VARBIT_P, PG_RETURN_DATUM, PG_RETURN_VARBIT_P, SET_VARSIZE, VARBITBYTES, VARBITEND, VARBITLEN, VARBITMAXLEN, VarBitPGetDatum, VARBITS, and VARSIZE.

Referenced by bitshiftright().

1373 {
1375  int32 shft = PG_GETARG_INT32(1);
1376  VarBit *result;
1377  int byte_shift,
1378  ishift,
1379  len;
1380  bits8 *p,
1381  *r;
1382 
1383  /* Negative shift is a shift to the right */
1384  if (shft < 0)
1385  {
1386  /* Prevent integer overflow in negation */
1387  if (shft < -VARBITMAXLEN)
1388  shft = -VARBITMAXLEN;
1390  VarBitPGetDatum(arg),
1391  Int32GetDatum(-shft)));
1392  }
1393 
1394  result = (VarBit *) palloc(VARSIZE(arg));
1395  SET_VARSIZE(result, VARSIZE(arg));
1396  VARBITLEN(result) = VARBITLEN(arg);
1397  r = VARBITS(result);
1398 
1399  /* If we shifted all the bits out, return an all-zero string */
1400  if (shft >= VARBITLEN(arg))
1401  {
1402  MemSet(r, 0, VARBITBYTES(arg));
1403  PG_RETURN_VARBIT_P(result);
1404  }
1405 
1406  byte_shift = shft / BITS_PER_BYTE;
1407  ishift = shft % BITS_PER_BYTE;
1408  p = VARBITS(arg) + byte_shift;
1409 
1410  if (ishift == 0)
1411  {
1412  /* Special case: we can do a memcpy */
1413  len = VARBITBYTES(arg) - byte_shift;
1414  memcpy(r, p, len);
1415  MemSet(r + len, 0, byte_shift);
1416  }
1417  else
1418  {
1419  for (; p < VARBITEND(arg); r++)
1420  {
1421  *r = *p << ishift;
1422  if ((++p) < VARBITEND(arg))
1423  *r |= *p >> (BITS_PER_BYTE - ishift);
1424  }
1425  for (; r < VARBITEND(result); r++)
1426  *r = 0;
1427  }
1428 
1429  /* The pad bits should be already zero at this point */
1430 
1431  PG_RETURN_VARBIT_P(result);
1432 }
#define VARBITS(PTR)
Definition: varbit.h:56
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
#define BITS_PER_BYTE
#define VARSIZE(PTR)
Definition: postgres.h:303
#define VARBITBYTES(PTR)
Definition: varbit.h:58
#define MemSet(start, val, len)
Definition: c.h:962
#define PG_GETARG_VARBIT_P(n)
Definition: varbit.h:47
#define VARBITLEN(PTR)
Definition: varbit.h:54
signed int int32
Definition: c.h:347
#define VARBITMAXLEN
Definition: varbit.h:68
#define PG_RETURN_VARBIT_P(x)
Definition: varbit.h:49
Definition: varbit.h:30
Datum bitshiftright(PG_FUNCTION_ARGS)
Definition: varbit.c:1439
#define VARBITEND(PTR)
Definition: varbit.h:70
uint8 bits8
Definition: c.h:366
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:343
#define VarBitPGetDatum(X)
Definition: varbit.h:46
#define Int32GetDatum(X)
Definition: postgres.h:479
void * palloc(Size size)
Definition: mcxt.c:949
void * arg
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:617

◆ bitshiftright()

Datum bitshiftright ( PG_FUNCTION_ARGS  )

Definition at line 1439 of file varbit.c.

References arg, BITMASK, BITS_PER_BYTE, bitshiftleft(), DirectFunctionCall2, Int32GetDatum, MemSet, palloc(), PG_GETARG_INT32, PG_GETARG_VARBIT_P, PG_RETURN_DATUM, PG_RETURN_VARBIT_P, SET_VARSIZE, VARBIT_PAD_LAST, VARBITBYTES, VARBITEND, VARBITLEN, VARBITMAXLEN, VarBitPGetDatum, VARBITS, and VARSIZE.

Referenced by bitshiftleft().

1440 {
1442  int32 shft = PG_GETARG_INT32(1);
1443  VarBit *result;
1444  int byte_shift,
1445  ishift,
1446  len;
1447  bits8 *p,
1448  *r;
1449 
1450  /* Negative shift is a shift to the left */
1451  if (shft < 0)
1452  {
1453  /* Prevent integer overflow in negation */
1454  if (shft < -VARBITMAXLEN)
1455  shft = -VARBITMAXLEN;
1457  VarBitPGetDatum(arg),
1458  Int32GetDatum(-shft)));
1459  }
1460 
1461  result = (VarBit *) palloc(VARSIZE(arg));
1462  SET_VARSIZE(result, VARSIZE(arg));
1463  VARBITLEN(result) = VARBITLEN(arg);
1464  r = VARBITS(result);
1465 
1466  /* If we shifted all the bits out, return an all-zero string */
1467  if (shft >= VARBITLEN(arg))
1468  {
1469  MemSet(r, 0, VARBITBYTES(arg));
1470  PG_RETURN_VARBIT_P(result);
1471  }
1472 
1473  byte_shift = shft / BITS_PER_BYTE;
1474  ishift = shft % BITS_PER_BYTE;
1475  p = VARBITS(arg);
1476 
1477  /* Set the first part of the result to 0 */
1478  MemSet(r, 0, byte_shift);
1479  r += byte_shift;
1480 
1481  if (ishift == 0)
1482  {
1483  /* Special case: we can do a memcpy */
1484  len = VARBITBYTES(arg) - byte_shift;
1485  memcpy(r, p, len);
1486  r += len;
1487  }
1488  else
1489  {
1490  if (r < VARBITEND(result))
1491  *r = 0; /* initialize first byte */
1492  for (; r < VARBITEND(result); p++)
1493  {
1494  *r |= *p >> ishift;
1495  if ((++r) < VARBITEND(result))
1496  *r = (*p << (BITS_PER_BYTE - ishift)) & BITMASK;
1497  }
1498  }
1499 
1500  /* We may have shifted 1's into the pad bits, so fix that */
1501  VARBIT_PAD_LAST(result, r);
1502 
1503  PG_RETURN_VARBIT_P(result);
1504 }
#define VARBIT_PAD_LAST(vb, ptr)
Definition: varbit.c:58
#define VARBITS(PTR)
Definition: varbit.h:56
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
#define BITS_PER_BYTE
#define VARSIZE(PTR)
Definition: postgres.h:303
#define VARBITBYTES(PTR)
Definition: varbit.h:58
#define MemSet(start, val, len)
Definition: c.h:962
#define PG_GETARG_VARBIT_P(n)
Definition: varbit.h:47
#define VARBITLEN(PTR)
Definition: varbit.h:54
signed int int32
Definition: c.h:347
#define VARBITMAXLEN
Definition: varbit.h:68
#define PG_RETURN_VARBIT_P(x)
Definition: varbit.h:49
Definition: varbit.h:30
#define VARBITEND(PTR)
Definition: varbit.h:70
uint8 bits8
Definition: c.h:366
#define BITMASK
Definition: varbit.h:72
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:343
Datum bitshiftleft(PG_FUNCTION_ARGS)
Definition: varbit.c:1372
#define VarBitPGetDatum(X)
Definition: varbit.h:46
#define Int32GetDatum(X)
Definition: postgres.h:479
void * palloc(Size size)
Definition: mcxt.c:949
void * arg
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:617

◆ bitsubstr()

Datum bitsubstr ( PG_FUNCTION_ARGS  )

Definition at line 1037 of file varbit.c.

References bitsubstring(), PG_GETARG_INT32, PG_GETARG_VARBIT_P, and PG_RETURN_VARBIT_P.

1038 {
1040  PG_GETARG_INT32(1),
1041  PG_GETARG_INT32(2),
1042  false));
1043 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
#define PG_GETARG_VARBIT_P(n)
Definition: varbit.h:47
static VarBit * bitsubstring(VarBit *arg, int32 s, int32 l, bool length_not_specified)
Definition: varbit.c:1054
#define PG_RETURN_VARBIT_P(x)
Definition: varbit.h:49

◆ bitsubstr_no_len()

Datum bitsubstr_no_len ( PG_FUNCTION_ARGS  )

Definition at line 1046 of file varbit.c.

References bitsubstring(), PG_GETARG_INT32, PG_GETARG_VARBIT_P, and PG_RETURN_VARBIT_P.

1047 {
1049  PG_GETARG_INT32(1),
1050  -1, true));
1051 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
#define PG_GETARG_VARBIT_P(n)
Definition: varbit.h:47
static VarBit * bitsubstring(VarBit *arg, int32 s, int32 l, bool length_not_specified)
Definition: varbit.c:1054
#define PG_RETURN_VARBIT_P(x)
Definition: varbit.h:49

◆ bitsubstring()

static VarBit * bitsubstring ( VarBit arg,
int32  s,
int32  l,
bool  length_not_specified 
)
static

Definition at line 1054 of file varbit.c.

References BITMASK, BITS_PER_BYTE, ereport, errcode(), errmsg(), ERROR, i, Max, Min, palloc(), s1, SET_VARSIZE, VARBIT_PAD, VARBITEND, VARBITHDRSZ, VARBITLEN, VARBITS, VARBITTOTALLEN, and VARHDRSZ.

Referenced by bit_overlay(), bitsubstr(), and bitsubstr_no_len().

1055 {
1056  VarBit *result;
1057  int bitlen,
1058  rbitlen,
1059  len,
1060  ishift,
1061  i;
1062  int e,
1063  s1,
1064  e1;
1065  bits8 *r,
1066  *ps;
1067 
1068  bitlen = VARBITLEN(arg);
1069  s1 = Max(s, 1);
1070  /* If we do not have an upper bound, use end of string */
1071  if (length_not_specified)
1072  {
1073  e1 = bitlen + 1;
1074  }
1075  else
1076  {
1077  e = s + l;
1078 
1079  /*
1080  * A negative value for L is the only way for the end position to be
1081  * before the start. SQL99 says to throw an error.
1082  */
1083  if (e < s)
1084  ereport(ERROR,
1085  (errcode(ERRCODE_SUBSTRING_ERROR),
1086  errmsg("negative substring length not allowed")));
1087  e1 = Min(e, bitlen + 1);
1088  }
1089  if (s1 > bitlen || e1 <= s1)
1090  {
1091  /* Need to return a zero-length bitstring */
1092  len = VARBITTOTALLEN(0);
1093  result = (VarBit *) palloc(len);
1094  SET_VARSIZE(result, len);
1095  VARBITLEN(result) = 0;
1096  }
1097  else
1098  {
1099  /*
1100  * OK, we've got a true substring starting at position s1-1 and ending
1101  * at position e1-1
1102  */
1103  rbitlen = e1 - s1;
1104  len = VARBITTOTALLEN(rbitlen);
1105  result = (VarBit *) palloc(len);
1106  SET_VARSIZE(result, len);
1107  VARBITLEN(result) = rbitlen;
1108  len -= VARHDRSZ + VARBITHDRSZ;
1109  /* Are we copying from a byte boundary? */
1110  if ((s1 - 1) % BITS_PER_BYTE == 0)
1111  {
1112  /* Yep, we are copying bytes */
1113  memcpy(VARBITS(result), VARBITS(arg) + (s1 - 1) / BITS_PER_BYTE,
1114  len);
1115  }
1116  else
1117  {
1118  /* Figure out how much we need to shift the sequence by */
1119  ishift = (s1 - 1) % BITS_PER_BYTE;
1120  r = VARBITS(result);
1121  ps = VARBITS(arg) + (s1 - 1) / BITS_PER_BYTE;
1122  for (i = 0; i < len; i++)
1123  {
1124  *r = (*ps << ishift) & BITMASK;
1125  if ((++ps) < VARBITEND(arg))
1126  *r |= *ps >> (BITS_PER_BYTE - ishift);
1127  r++;
1128  }
1129  }
1130 
1131  /* Make sure last byte is correctly zero-padded */
1132  VARBIT_PAD(result);
1133  }
1134 
1135  return result;
1136 }
#define VARBITS(PTR)
Definition: varbit.h:56
#define BITS_PER_BYTE
#define VARHDRSZ
Definition: c.h:562
#define Min(x, y)
Definition: c.h:911
int errcode(int sqlerrcode)
Definition: elog.c:608
#define VARBITTOTALLEN(BITLEN)
Definition: varbit.h:62
#define VARBITLEN(PTR)
Definition: varbit.h:54
#define VARBIT_PAD(vb)
Definition: varbit.c:46
#define ERROR
Definition: elog.h:43
char * s1
Definition: varbit.h:30
#define ereport(elevel, rest)
Definition: elog.h:141
#define VARBITHDRSZ
Definition: varbit.h:52
#define VARBITEND(PTR)
Definition: varbit.h:70
uint8 bits8
Definition: c.h:366
#define BITMASK
Definition: varbit.h:72
#define Max(x, y)
Definition: c.h:905
e
Definition: preproc-init.c:82
void * palloc(Size size)
Definition: mcxt.c:949
int errmsg(const char *fmt,...)
Definition: elog.c:822
int i
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329

◆ bittoint4()

Datum bittoint4 ( PG_FUNCTION_ARGS  )

Definition at line 1566 of file varbit.c.

References arg, BITS_PER_BYTE, ereport, errcode(), errmsg(), ERROR, PG_GETARG_VARBIT_P, PG_RETURN_INT32, VARBITEND, VARBITLEN, VARBITPAD, and VARBITS.

1567 {
1569  uint32 result;
1570  bits8 *r;
1571 
1572  /* Check that the bit string is not too long */
1573  if (VARBITLEN(arg) > sizeof(result) * BITS_PER_BYTE)
1574  ereport(ERROR,
1575  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1576  errmsg("integer out of range")));
1577 
1578  result = 0;
1579  for (r = VARBITS(arg); r < VARBITEND(arg); r++)
1580  {
1581  result <<= BITS_PER_BYTE;
1582  result |= *r;
1583  }
1584  /* Now shift the result to take account of the padding at the end */
1585  result >>= VARBITPAD(arg);
1586 
1587  PG_RETURN_INT32(result);
1588 }
#define VARBITS(PTR)
Definition: varbit.h:56
#define BITS_PER_BYTE
#define PG_RETURN_INT32(x)
Definition: fmgr.h:344
int errcode(int sqlerrcode)
Definition: elog.c:608
#define PG_GETARG_VARBIT_P(n)
Definition: varbit.h:47
#define VARBITLEN(PTR)
Definition: varbit.h:54
#define ERROR
Definition: elog.h:43
Definition: varbit.h:30
unsigned int uint32
Definition: c.h:359
#define ereport(elevel, rest)
Definition: elog.h:141
#define VARBITPAD(PTR)
Definition: varbit.h:60
#define VARBITEND(PTR)
Definition: varbit.h:70
uint8 bits8
Definition: c.h:366
int errmsg(const char *fmt,...)
Definition: elog.c:822
void * arg

◆ bittoint8()

Datum bittoint8 ( PG_FUNCTION_ARGS  )

Definition at line 1646 of file varbit.c.

References arg, BITS_PER_BYTE, ereport, errcode(), errmsg(), ERROR, PG_GETARG_VARBIT_P, PG_RETURN_INT64, VARBITEND, VARBITLEN, VARBITPAD, and VARBITS.

1647 {
1649  uint64 result;
1650  bits8 *r;
1651 
1652  /* Check that the bit string is not too long */
1653  if (VARBITLEN(arg) > sizeof(result) * BITS_PER_BYTE)
1654  ereport(ERROR,
1655  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1656  errmsg("bigint out of range")));
1657 
1658  result = 0;
1659  for (r = VARBITS(arg); r < VARBITEND(arg); r++)
1660  {
1661  result <<= BITS_PER_BYTE;
1662  result |= *r;
1663  }
1664  /* Now shift the result to take account of the padding at the end */
1665  result >>= VARBITPAD(arg);
1666 
1667  PG_RETURN_INT64(result);
1668 }
#define VARBITS(PTR)
Definition: varbit.h:56
#define BITS_PER_BYTE
#define PG_RETURN_INT64(x)
Definition: fmgr.h:357
int errcode(int sqlerrcode)
Definition: elog.c:608
#define PG_GETARG_VARBIT_P(n)
Definition: varbit.h:47
#define VARBITLEN(PTR)
Definition: varbit.h:54
#define ERROR
Definition: elog.h:43
Definition: varbit.h:30
#define ereport(elevel, rest)
Definition: elog.h:141
#define VARBITPAD(PTR)
Definition: varbit.h:60
#define VARBITEND(PTR)
Definition: varbit.h:70
uint8 bits8
Definition: c.h:366
int errmsg(const char *fmt,...)
Definition: elog.c:822
void * arg

◆ bittypmodin()

Datum bittypmodin ( PG_FUNCTION_ARGS  )

Definition at line 428 of file varbit.c.

References anybit_typmodin(), PG_GETARG_ARRAYTYPE_P, and PG_RETURN_INT32.

429 {
431 
432  PG_RETURN_INT32(anybit_typmodin(ta, "bit"));
433 }
#define PG_RETURN_INT32(x)
Definition: fmgr.h:344
static int32 anybit_typmodin(ArrayType *ta, const char *typename)
Definition: varbit.c:89
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:251

◆ bittypmodout()

Datum bittypmodout ( PG_FUNCTION_ARGS  )

Definition at line 436 of file varbit.c.

References anybit_typmodout(), PG_GETARG_INT32, and PG_RETURN_CSTRING.

437 {
438  int32 typmod = PG_GETARG_INT32(0);
439 
441 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
static char * anybit_typmodout(int32 typmod)
Definition: varbit.c:126
signed int int32
Definition: c.h:347
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:352

◆ bitxor()

Datum bitxor ( PG_FUNCTION_ARGS  )

Definition at line 1304 of file varbit.c.

References ereport, errcode(), errmsg(), ERROR, i, palloc(), PG_GETARG_VARBIT_P, PG_RETURN_VARBIT_P, SET_VARSIZE, VARBITBYTES, VARBITLEN, VARBITS, and VARSIZE.

1305 {
1306  VarBit *arg1 = PG_GETARG_VARBIT_P(0);
1307  VarBit *arg2 = PG_GETARG_VARBIT_P(1);
1308  VarBit *result;
1309  int len,
1310  bitlen1,
1311  bitlen2,
1312  i;
1313  bits8 *p1,
1314  *p2,
1315  *r;
1316 
1317  bitlen1 = VARBITLEN(arg1);
1318  bitlen2 = VARBITLEN(arg2);
1319  if (bitlen1 != bitlen2)
1320  ereport(ERROR,
1321  (errcode(ERRCODE_STRING_DATA_LENGTH_MISMATCH),
1322  errmsg("cannot XOR bit strings of different sizes")));
1323 
1324  len = VARSIZE(arg1);
1325  result = (VarBit *) palloc(len);
1326  SET_VARSIZE(result, len);
1327  VARBITLEN(result) = bitlen1;
1328 
1329  p1 = VARBITS(arg1);
1330  p2 = VARBITS(arg2);
1331  r = VARBITS(result);
1332  for (i = 0; i < VARBITBYTES(arg1); i++)
1333  *r++ = *p1++ ^ *p2++;
1334 
1335  /* Padding is not needed as ^ of 0 pads is 0 */
1336 
1337  PG_RETURN_VARBIT_P(result);
1338 }
#define VARBITS(PTR)
Definition: varbit.h:56
#define VARSIZE(PTR)
Definition: postgres.h:303
#define VARBITBYTES(PTR)
Definition: varbit.h:58
int errcode(int sqlerrcode)
Definition: elog.c:608
#define PG_GETARG_VARBIT_P(n)
Definition: varbit.h:47
#define VARBITLEN(PTR)
Definition: varbit.h:54
#define ERROR
Definition: elog.h:43
#define PG_RETURN_VARBIT_P(x)
Definition: varbit.h:49
Definition: varbit.h:30
#define ereport(elevel, rest)
Definition: elog.h:141
uint8 bits8
Definition: c.h:366
void * palloc(Size size)
Definition: mcxt.c:949
int errmsg(const char *fmt,...)
Definition: elog.c:822
int i
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329

◆ varbit()

Datum varbit ( PG_FUNCTION_ARGS  )

Definition at line 741 of file varbit.c.

References arg, ereport, errcode(), errmsg(), ERROR, palloc(), PG_GETARG_BOOL, PG_GETARG_INT32, PG_GETARG_VARBIT_P, PG_RETURN_VARBIT_P, SET_VARSIZE, VARBIT_PAD, VARBITBYTES, VARBITLEN, VARBITS, and VARBITTOTALLEN.

Referenced by leftmostvalue_varbit().

742 {
744  int32 len = PG_GETARG_INT32(1);
745  bool isExplicit = PG_GETARG_BOOL(2);
746  VarBit *result;
747  int rlen;
748 
749  /* No work if typmod is invalid or supplied data matches it already */
750  if (len <= 0 || len >= VARBITLEN(arg))
751  PG_RETURN_VARBIT_P(arg);
752 
753  if (!isExplicit)
754  ereport(ERROR,
755  (errcode(ERRCODE_STRING_DATA_RIGHT_TRUNCATION),
756  errmsg("bit string too long for type bit varying(%d)",
757  len)));
758 
759  rlen = VARBITTOTALLEN(len);
760  result = (VarBit *) palloc(rlen);
761  SET_VARSIZE(result, rlen);
762  VARBITLEN(result) = len;
763 
764  memcpy(VARBITS(result), VARBITS(arg), VARBITBYTES(result));
765 
766  /* Make sure last byte is correctly zero-padded */
767  VARBIT_PAD(result);
768 
769  PG_RETURN_VARBIT_P(result);
770 }
#define VARBITS(PTR)
Definition: varbit.h:56
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
#define VARBITBYTES(PTR)
Definition: varbit.h:58
int errcode(int sqlerrcode)
Definition: elog.c:608
#define PG_GETARG_VARBIT_P(n)
Definition: varbit.h:47
#define VARBITTOTALLEN(BITLEN)
Definition: varbit.h:62
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:269
#define VARBITLEN(PTR)
Definition: varbit.h:54
#define VARBIT_PAD(vb)
Definition: varbit.c:46
signed int int32
Definition: c.h:347
#define ERROR
Definition: elog.h:43
#define PG_RETURN_VARBIT_P(x)
Definition: varbit.h:49
Definition: varbit.h:30
#define ereport(elevel, rest)
Definition: elog.h:141
void * palloc(Size size)
Definition: mcxt.c:949
int errmsg(const char *fmt,...)
Definition: elog.c:822
void * arg
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329

◆ varbit_in()

Datum varbit_in ( PG_FUNCTION_ARGS  )

Definition at line 451 of file varbit.c.

References ereport, errcode(), errmsg(), ERROR, HIGHBIT, Min, palloc0(), PG_GETARG_CSTRING, PG_GETARG_INT32, PG_GETARG_OID, PG_RETURN_VARBIT_P, SET_VARSIZE, VARBITLEN, VARBITMAXLEN, VARBITS, and VARBITTOTALLEN.

Referenced by leftmostvalue_varbit().

452 {
453  char *input_string = PG_GETARG_CSTRING(0);
454 
455 #ifdef NOT_USED
456  Oid typelem = PG_GETARG_OID(1);
457 #endif
458  int32 atttypmod = PG_GETARG_INT32(2);
459  VarBit *result; /* The resulting bit string */
460  char *sp; /* pointer into the character string */
461  bits8 *r; /* pointer into the result */
462  int len, /* Length of the whole data structure */
463  bitlen, /* Number of bits in the bit string */
464  slen; /* Length of the input string */
465  bool bit_not_hex; /* false = hex string true = bit string */
466  int bc;
467  bits8 x = 0;
468 
469  /* Check that the first character is a b or an x */
470  if (input_string[0] == 'b' || input_string[0] == 'B')
471  {
472  bit_not_hex = true;
473  sp = input_string + 1;
474  }
475  else if (input_string[0] == 'x' || input_string[0] == 'X')
476  {
477  bit_not_hex = false;
478  sp = input_string + 1;
479  }
480  else
481  {
482  bit_not_hex = true;
483  sp = input_string;
484  }
485 
486  /*
487  * Determine bitlength from input string. MaxAllocSize ensures a regular
488  * input is small enough, but we must check hex input.
489  */
490  slen = strlen(sp);
491  if (bit_not_hex)
492  bitlen = slen;
493  else
494  {
495  if (slen > VARBITMAXLEN / 4)
496  ereport(ERROR,
497  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
498  errmsg("bit string length exceeds the maximum allowed (%d)",
499  VARBITMAXLEN)));
500  bitlen = slen * 4;
501  }
502 
503  /*
504  * Sometimes atttypmod is not supplied. If it is supplied we need to make
505  * sure that the bitstring fits.
506  */
507  if (atttypmod <= 0)
508  atttypmod = bitlen;
509  else if (bitlen > atttypmod)
510  ereport(ERROR,
511  (errcode(ERRCODE_STRING_DATA_RIGHT_TRUNCATION),
512  errmsg("bit string too long for type bit varying(%d)",
513  atttypmod)));
514 
515  len = VARBITTOTALLEN(bitlen);
516  /* set to 0 so that *r is always initialised and string is zero-padded */
517  result = (VarBit *) palloc0(len);
518  SET_VARSIZE(result, len);
519  VARBITLEN(result) = Min(bitlen, atttypmod);
520 
521  r = VARBITS(result);
522  if (bit_not_hex)
523  {
524  /* Parse the bit representation of the string */
525  /* We know it fits, as bitlen was compared to atttypmod */
526  x = HIGHBIT;
527  for (; *sp; sp++)
528  {
529  if (*sp == '1')
530  *r |= x;
531  else if (*sp != '0')
532  ereport(ERROR,
533  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
534  errmsg("\"%c\" is not a valid binary digit",
535  *sp)));
536 
537  x >>= 1;
538  if (x == 0)
539  {
540  x = HIGHBIT;
541  r++;
542  }
543  }
544  }
545  else
546  {
547  /* Parse the hex representation of the string */
548  for (bc = 0; *sp; sp++)
549  {
550  if (*sp >= '0' && *sp <= '9')
551  x = (bits8) (*sp - '0');
552  else if (*sp >= 'A' && *sp <= 'F')
553  x = (bits8) (*sp - 'A') + 10;
554  else if (*sp >= 'a' && *sp <= 'f')
555  x = (bits8) (*sp - 'a') + 10;
556  else
557  ereport(ERROR,
558  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
559  errmsg("\"%c\" is not a valid hexadecimal digit",
560  *sp)));
561 
562  if (bc)
563  {
564  *r++ |= x;
565  bc = 0;
566  }
567  else
568  {
569  *r = x << 4;
570  bc = 1;
571  }
572  }
573  }
574 
575  PG_RETURN_VARBIT_P(result);
576 }
#define VARBITS(PTR)
Definition: varbit.h:56
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
#define Min(x, y)
Definition: c.h:911
int errcode(int sqlerrcode)
Definition: elog.c:608
#define VARBITTOTALLEN(BITLEN)
Definition: varbit.h:62
unsigned int Oid
Definition: postgres_ext.h:31
#define VARBITLEN(PTR)
Definition: varbit.h:54
signed int int32
Definition: c.h:347
#define VARBITMAXLEN
Definition: varbit.h:68
#define ERROR
Definition: elog.h:43
#define PG_RETURN_VARBIT_P(x)
Definition: varbit.h:49
#define PG_GETARG_OID(n)
Definition: fmgr.h:270
Definition: varbit.h:30
#define ereport(elevel, rest)
Definition: elog.h:141
#define HIGHBIT
Definition: c.h:1105
uint8 bits8
Definition: c.h:366
void * palloc0(Size size)
Definition: mcxt.c:980
int errmsg(const char *fmt,...)
Definition: elog.c:822
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:272
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329

◆ varbit_out()

Datum varbit_out ( PG_FUNCTION_ARGS  )

Definition at line 586 of file varbit.c.

References BITS_PER_BYTE, i, IS_HIGHBIT_SET, palloc(), PG_GETARG_VARBIT_P, PG_RETURN_CSTRING, VARBIT_CORRECTLY_PADDED, VARBITLEN, and VARBITS.

Referenced by bit_out().

587 {
588  VarBit *s = PG_GETARG_VARBIT_P(0);
589  char *result,
590  *r;
591  bits8 *sp;
592  bits8 x;
593  int i,
594  k,
595  len;
596 
597  /* Assertion to help catch any bit functions that don't pad correctly */
599 
600  len = VARBITLEN(s);
601  result = (char *) palloc(len + 1);
602  sp = VARBITS(s);
603  r = result;
604  for (i = 0; i <= len - BITS_PER_BYTE; i += BITS_PER_BYTE, sp++)
605  {
606  /* print full bytes */
607  x = *sp;
608  for (k = 0; k < BITS_PER_BYTE; k++)
609  {
610  *r++ = IS_HIGHBIT_SET(x) ? '1' : '0';
611  x <<= 1;
612  }
613  }
614  if (i < len)
615  {
616  /* print the last partial byte */
617  x = *sp;
618  for (k = i; k < len; k++)
619  {
620  *r++ = IS_HIGHBIT_SET(x) ? '1' : '0';
621  x <<= 1;
622  }
623  }
624  *r = '\0';
625 
626  PG_RETURN_CSTRING(result);
627 }
#define VARBITS(PTR)
Definition: varbit.h:56
#define BITS_PER_BYTE
#define PG_GETARG_VARBIT_P(n)
Definition: varbit.h:47
#define VARBITLEN(PTR)
Definition: varbit.h:54
#define IS_HIGHBIT_SET(ch)
Definition: c.h:1106
Definition: varbit.h:30
uint8 bits8
Definition: c.h:366
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:352
#define VARBIT_CORRECTLY_PADDED(vb)
Definition: varbit.c:76
void * palloc(Size size)
Definition: mcxt.c:949
int i

◆ varbit_recv()

Datum varbit_recv ( PG_FUNCTION_ARGS  )

Definition at line 635 of file varbit.c.

References buf, ereport, errcode(), errmsg(), ERROR, palloc(), PG_GETARG_INT32, PG_GETARG_OID, PG_GETARG_POINTER, PG_RETURN_VARBIT_P, pq_copymsgbytes(), pq_getmsgint(), SET_VARSIZE, VARBIT_PAD, VARBITBYTES, VARBITLEN, VARBITMAXLEN, VARBITS, and VARBITTOTALLEN.

636 {
638 
639 #ifdef NOT_USED
640  Oid typelem = PG_GETARG_OID(1);
641 #endif
642  int32 atttypmod = PG_GETARG_INT32(2);
643  VarBit *result;
644  int len,
645  bitlen;
646 
647  bitlen = pq_getmsgint(buf, sizeof(int32));
648  if (bitlen < 0 || bitlen > VARBITMAXLEN)
649  ereport(ERROR,
650  (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
651  errmsg("invalid length in external bit string")));
652 
653  /*
654  * Sometimes atttypmod is not supplied. If it is supplied we need to make
655  * sure that the bitstring fits.
656  */
657  if (atttypmod > 0 && bitlen > atttypmod)
658  ereport(ERROR,
659  (errcode(ERRCODE_STRING_DATA_RIGHT_TRUNCATION),
660  errmsg("bit string too long for type bit varying(%d)",
661  atttypmod)));
662 
663  len = VARBITTOTALLEN(bitlen);
664  result = (VarBit *) palloc(len);
665  SET_VARSIZE(result, len);
666  VARBITLEN(result) = bitlen;
667 
668  pq_copymsgbytes(buf, (char *) VARBITS(result), VARBITBYTES(result));
669 
670  /* Make sure last byte is correctly zero-padded */
671  VARBIT_PAD(result);
672 
673  PG_RETURN_VARBIT_P(result);
674 }
#define VARBITS(PTR)
Definition: varbit.h:56
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
#define VARBITBYTES(PTR)
Definition: varbit.h:58
StringInfoData * StringInfo
Definition: stringinfo.h:44
int errcode(int sqlerrcode)
Definition: elog.c:608
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:271
#define VARBITTOTALLEN(BITLEN)
Definition: varbit.h:62
unsigned int Oid
Definition: postgres_ext.h:31
#define VARBITLEN(PTR)
Definition: varbit.h:54
#define VARBIT_PAD(vb)
Definition: varbit.c:46
signed int int32
Definition: c.h:347
#define VARBITMAXLEN
Definition: varbit.h:68
#define ERROR
Definition: elog.h:43
#define PG_RETURN_VARBIT_P(x)
Definition: varbit.h:49
static char * buf
Definition: pg_test_fsync.c:67
#define PG_GETARG_OID(n)
Definition: fmgr.h:270
Definition: varbit.h:30
#define ereport(elevel, rest)
Definition: elog.h:141
void pq_copymsgbytes(StringInfo msg, char *buf, int datalen)
Definition: pqformat.c:530
void * palloc(Size size)
Definition: mcxt.c:949
int errmsg(const char *fmt,...)
Definition: elog.c:822
unsigned int pq_getmsgint(StringInfo msg, int b)
Definition: pqformat.c:417
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329

◆ varbit_send()

Datum varbit_send ( PG_FUNCTION_ARGS  )

Definition at line 680 of file varbit.c.

References buf, PG_GETARG_VARBIT_P, PG_RETURN_BYTEA_P, pq_begintypsend(), pq_endtypsend(), pq_sendbytes(), pq_sendint32(), VARBITBYTES, VARBITLEN, and VARBITS.

Referenced by bit_send().

681 {
682  VarBit *s = PG_GETARG_VARBIT_P(0);
684 
685  pq_begintypsend(&buf);
686  pq_sendint32(&buf, VARBITLEN(s));
687  pq_sendbytes(&buf, (char *) VARBITS(s), VARBITBYTES(s));
689 }
#define VARBITS(PTR)
Definition: varbit.h:56
#define VARBITBYTES(PTR)
Definition: varbit.h:58
void pq_begintypsend(StringInfo buf)
Definition: pqformat.c:328
#define PG_GETARG_VARBIT_P(n)
Definition: varbit.h:47
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:360
bytea * pq_endtypsend(StringInfo buf)
Definition: pqformat.c:348
#define VARBITLEN(PTR)
Definition: varbit.h:54
static void pq_sendint32(StringInfo buf, uint32 i)
Definition: pqformat.h:145
static char * buf
Definition: pg_test_fsync.c:67
Definition: varbit.h:30
void pq_sendbytes(StringInfo buf, const char *data, int datalen)
Definition: pqformat.c:125

◆ varbit_support()

Datum varbit_support ( PG_FUNCTION_ARGS  )

Definition at line 701 of file varbit.c.

References FuncExpr::args, Assert, DatumGetInt32, exprTypmod(), SupportRequestSimplify::fcall, IsA, linitial, list_length(), lsecond, PG_GETARG_POINTER, PG_RETURN_POINTER, and relabel_to_typmod().

702 {
703  Node *rawreq = (Node *) PG_GETARG_POINTER(0);
704  Node *ret = NULL;
705 
706  if (IsA(rawreq, SupportRequestSimplify))
707  {
709  FuncExpr *expr = req->fcall;
710  Node *typmod;
711 
712  Assert(list_length(expr->args) >= 2);
713 
714  typmod = (Node *) lsecond(expr->args);
715 
716  if (IsA(typmod, Const) &&!((Const *) typmod)->constisnull)
717  {
718  Node *source = (Node *) linitial(expr->args);
719  int32 new_typmod = DatumGetInt32(((Const *) typmod)->constvalue);
720  int32 old_max = exprTypmod(source);
721  int32 new_max = new_typmod;
722 
723  /* Note: varbit() treats typmod 0 as invalid, so we do too */
724  if (new_max <= 0 || (old_max > 0 && old_max <= new_max))
725  ret = relabel_to_typmod(source, new_typmod);
726  }
727  }
728 
729  PG_RETURN_POINTER(ret);
730 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:351
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
List * args
Definition: primnodes.h:463
#define DatumGetInt32(X)
Definition: postgres.h:472
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:275
Definition: nodes.h:525
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:271
#define lsecond(l)
Definition: pg_list.h:200
signed int int32
Definition: c.h:347
#define linitial(l)
Definition: pg_list.h:195
#define Assert(condition)
Definition: c.h:739
static int list_length(const List *l)
Definition: pg_list.h:169
Node * relabel_to_typmod(Node *expr, int32 typmod)
Definition: nodeFuncs.c:587

◆ varbittypmodin()

Datum varbittypmodin ( PG_FUNCTION_ARGS  )

Definition at line 773 of file varbit.c.

References anybit_typmodin(), PG_GETARG_ARRAYTYPE_P, and PG_RETURN_INT32.

774 {
776 
777  PG_RETURN_INT32(anybit_typmodin(ta, "varbit"));
778 }
#define PG_RETURN_INT32(x)
Definition: fmgr.h:344
static int32 anybit_typmodin(ArrayType *ta, const char *typename)
Definition: varbit.c:89
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:251

◆ varbittypmodout()

Datum varbittypmodout ( PG_FUNCTION_ARGS  )

Definition at line 781 of file varbit.c.

References anybit_typmodout(), PG_GETARG_INT32, and PG_RETURN_CSTRING.

782 {
783  int32 typmod = PG_GETARG_INT32(0);
784 
786 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
static char * anybit_typmodout(int32 typmod)
Definition: varbit.c:126
signed int int32
Definition: c.h:347
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:352