PostgreSQL Source Code  git master
varbit.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * varbit.c
4  * Functions for the SQL datatypes BIT() and BIT VARYING().
5  *
6  * The data structure contains the following elements:
7  * header -- length of the whole data structure (incl header)
8  * in bytes (as with all varying length datatypes)
9  * data section -- private data section for the bits data structures
10  * bitlength -- length of the bit string in bits
11  * bitdata -- bit string, most significant byte first
12  *
13  * The length of the bitdata vector should always be exactly as many
14  * bytes as are needed for the given bitlength. If the bitlength is
15  * not a multiple of 8, the extra low-order padding bits of the last
16  * byte must be zeroes.
17  *
18  * attypmod is defined as the length of the bit string in bits, or for
19  * varying bits the maximum length.
20  *
21  * Code originally contributed by Adriaan Joubert.
22  *
23  * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
24  * Portions Copyright (c) 1994, Regents of the University of California
25  *
26  * IDENTIFICATION
27  * src/backend/utils/adt/varbit.c
28  *
29  *-------------------------------------------------------------------------
30  */
31 
32 #include "postgres.h"
33 
34 #include "access/htup_details.h"
35 #include "common/int.h"
36 #include "libpq/pqformat.h"
37 #include "nodes/nodeFuncs.h"
38 #include "nodes/supportnodes.h"
39 #include "utils/array.h"
40 #include "utils/builtins.h"
41 #include "utils/varbit.h"
42 
43 #define HEXDIG(z) ((z)<10 ? ((z)+'0') : ((z)-10+'A'))
44 
45 /* Mask off any bits that should be zero in the last byte of a bitstring */
46 #define VARBIT_PAD(vb) \
47  do { \
48  int32 pad_ = VARBITPAD(vb); \
49  Assert(pad_ >= 0 && pad_ < BITS_PER_BYTE); \
50  if (pad_ > 0) \
51  *(VARBITS(vb) + VARBITBYTES(vb) - 1) &= BITMASK << pad_; \
52  } while (0)
53 
54 /*
55  * Many functions work byte-by-byte, so they have a pointer handy to the
56  * last-plus-one byte, which saves a cycle or two.
57  */
58 #define VARBIT_PAD_LAST(vb, ptr) \
59  do { \
60  int32 pad_ = VARBITPAD(vb); \
61  Assert(pad_ >= 0 && pad_ < BITS_PER_BYTE); \
62  if (pad_ > 0) \
63  *((ptr) - 1) &= BITMASK << pad_; \
64  } while (0)
65 
66 /* Assert proper padding of a bitstring */
67 #ifdef USE_ASSERT_CHECKING
68 #define VARBIT_CORRECTLY_PADDED(vb) \
69  do { \
70  int32 pad_ = VARBITPAD(vb); \
71  Assert(pad_ >= 0 && pad_ < BITS_PER_BYTE); \
72  Assert(pad_ == 0 || \
73  (*(VARBITS(vb) + VARBITBYTES(vb) - 1) & ~(BITMASK << pad_)) == 0); \
74  } while (0)
75 #else
76 #define VARBIT_CORRECTLY_PADDED(vb) ((void) 0)
77 #endif
78 
79 static VarBit *bit_catenate(VarBit *arg1, VarBit *arg2);
80 static VarBit *bitsubstring(VarBit *arg, int32 s, int32 l,
81  bool length_not_specified);
82 static VarBit *bit_overlay(VarBit *t1, VarBit *t2, int sp, int sl);
83 
84 
85 /*
86  * common code for bittypmodin and varbittypmodin
87  */
88 static int32
89 anybit_typmodin(ArrayType *ta, const char *typename)
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 }
121 
122 /*
123  * common code for bittypmodout and varbittypmodout
124  */
125 static char *
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 }
137 
138 
139 /*
140  * bit_in -
141  * converts a char string to the internal representation of a bitstring.
142  * The length is determined by the number of bits required plus
143  * VARHDRSZ bytes or from atttypmod.
144  */
145 Datum
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 }
276 
277 
278 Datum
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 }
325 
326 /*
327  * bit_recv - converts external binary format to bit
328  */
329 Datum
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 }
370 
371 /*
372  * bit_send - converts bit to binary format
373  */
374 Datum
376 {
377  /* Exactly the same as varbit_send, so share code */
378  return varbit_send(fcinfo);
379 }
380 
381 /*
382  * bit()
383  * Converts a bit() type to a specific internal length.
384  * len is the bitlength specified in the column definition.
385  *
386  * If doing implicit cast, raise error when source data is wrong length.
387  * If doing explicit cast, silently truncate or zero-pad to specified length.
388  */
389 Datum
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 }
426 
427 Datum
429 {
431 
432  PG_RETURN_INT32(anybit_typmodin(ta, "bit"));
433 }
434 
435 Datum
437 {
438  int32 typmod = PG_GETARG_INT32(0);
439 
441 }
442 
443 
444 /*
445  * varbit_in -
446  * converts a string to the internal representation of a bitstring.
447  * This is the same as bit_in except that atttypmod is taken as
448  * the maximum length, not the exact length to force the bitstring to.
449  */
450 Datum
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 }
577 
578 /*
579  * varbit_out -
580  * Prints the string as bits to preserve length accurately
581  *
582  * XXX varbit_recv() and hex input to varbit_in() can load a value that this
583  * cannot emit. Consider using hex output for such values.
584  */
585 Datum
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 }
628 
629 /*
630  * varbit_recv - converts external binary format to varbit
631  *
632  * External format is the bitlen as an int32, then the byte array.
633  */
634 Datum
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 }
675 
676 /*
677  * varbit_send - converts varbit to binary format
678  */
679 Datum
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 }
690 
691 /*
692  * varbit_support()
693  *
694  * Planner support function for the varbit() length coercion function.
695  *
696  * Currently, the only interesting thing we can do is flatten calls that set
697  * the new maximum length >= the previous maximum length. We can ignore the
698  * isExplicit argument, since that only affects truncation cases.
699  */
700 Datum
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 }
731 
732 /*
733  * varbit()
734  * Converts a varbit() type to a specific internal length.
735  * len is the maximum bitlength specified in the column definition.
736  *
737  * If doing implicit cast, raise error when source data is too long.
738  * If doing explicit cast, silently truncate to max length.
739  */
740 Datum
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 }
771 
772 Datum
774 {
776 
777  PG_RETURN_INT32(anybit_typmodin(ta, "varbit"));
778 }
779 
780 Datum
782 {
783  int32 typmod = PG_GETARG_INT32(0);
784 
786 }
787 
788 
789 /*
790  * Comparison operators
791  *
792  * We only need one set of comparison operators for bitstrings, as the lengths
793  * are stored in the same way for zero-padded and varying bit strings.
794  *
795  * Note that the standard is not unambiguous about the comparison between
796  * zero-padded bit strings and varying bitstrings. If the same value is written
797  * into a zero padded bitstring as into a varying bitstring, but the zero
798  * padded bitstring has greater length, it will be bigger.
799  *
800  * Zeros from the beginning of a bitstring cannot simply be ignored, as they
801  * may be part of a bit string and may be significant.
802  *
803  * Note: btree indexes need these routines not to leak memory; therefore,
804  * be careful to free working copies of toasted datums. Most places don't
805  * need to be so careful.
806  */
807 
808 /*
809  * bit_cmp
810  *
811  * Compares two bitstrings and returns <0, 0, >0 depending on whether the first
812  * string is smaller, equal, or bigger than the second. All bits are considered
813  * and additional zero bits may make one string smaller/larger than the other,
814  * even if their zero-padded values would be the same.
815  */
816 static int32
817 bit_cmp(VarBit *arg1, VarBit *arg2)
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 }
838 
839 Datum
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 }
862 
863 Datum
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 }
886 
887 Datum
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 }
901 
902 Datum
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 }
916 
917 Datum
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 }
931 
932 Datum
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 }
946 
947 Datum
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 }
961 
962 /*
963  * bitcat
964  * Concatenation of bit strings
965  */
966 Datum
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 }
974 
975 static VarBit *
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 }
1029 
1030 /*
1031  * bitsubstr
1032  * retrieve a substring from the bit string.
1033  * Note, s is 1-based.
1034  * SQL draft 6.10 9)
1035  */
1036 Datum
1038 {
1040  PG_GETARG_INT32(1),
1041  PG_GETARG_INT32(2),
1042  false));
1043 }
1044 
1045 Datum
1047 {
1049  PG_GETARG_INT32(1),
1050  -1, true));
1051 }
1052 
1053 static VarBit *
1054 bitsubstring(VarBit *arg, int32 s, int32 l, bool length_not_specified)
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 }
1137 
1138 /*
1139  * bitoverlay
1140  * Replace specified substring of first string with second
1141  *
1142  * The SQL standard defines OVERLAY() in terms of substring and concatenation.
1143  * This code is a direct implementation of what the standard says.
1144  */
1145 Datum
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 }
1155 
1156 Datum
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 }
1167 
1168 static VarBit *
1169 bit_overlay(VarBit *t1, VarBit *t2, int sp, int sl)
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 }
1197 
1198 /*
1199  * bitlength, bitoctetlength
1200  * Return the length of a bit string
1201  */
1202 Datum
1204 {
1206 
1207  PG_RETURN_INT32(VARBITLEN(arg));
1208 }
1209 
1210 Datum
1212 {
1214 
1216 }
1217 
1218 /*
1219  * bit_and
1220  * perform a logical AND on two bit strings.
1221  */
1222 Datum
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 }
1258 
1259 /*
1260  * bit_or
1261  * perform a logical OR on two bit strings.
1262  */
1263 Datum
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 }
1298 
1299 /*
1300  * bitxor
1301  * perform a logical XOR on two bit strings.
1302  */
1303 Datum
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 }
1339 
1340 /*
1341  * bitnot
1342  * perform a logical NOT on a bit string.
1343  */
1344 Datum
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 }
1366 
1367 /*
1368  * bitshiftleft
1369  * do a left shift (i.e. towards the beginning of the string)
1370  */
1371 Datum
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 }
1433 
1434 /*
1435  * bitshiftright
1436  * do a right shift (i.e. towards the end of the string)
1437  */
1438 Datum
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 }
1505 
1506 /*
1507  * This is not defined in any standard. We retain the natural ordering of
1508  * bits here, as it just seems more intuitive.
1509  */
1510 Datum
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 }
1564 
1565 Datum
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 }
1589 
1590 Datum
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 }
1644 
1645 Datum
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 }
1669 
1670 
1671 /*
1672  * Determines the position of S2 in the bitstring S1 (1-based string).
1673  * If S2 does not appear in S1 this function returns 0.
1674  * If S2 is of length 0 this function returns 1.
1675  * Compatible in usage with POSITION() functions for other data types.
1676  */
1677 Datum
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 }
1774 
1775 
1776 /*
1777  * bitsetbit
1778  *
1779  * Given an instance of type 'bit' creates a new one with
1780  * the Nth bit set to the given value.
1781  *
1782  * The bit location is specified left-to-right in a zero-based fashion
1783  * consistent with the other get_bit and set_bit functions, but
1784  * inconsistent with the standard substring, position, overlay functions
1785  */
1786 Datum
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 }
1838 
1839 /*
1840  * bitgetbit
1841  *
1842  * returns the value of the Nth bit of a bit array (0 or 1).
1843  *
1844  * The bit location is specified left-to-right in a zero-based fashion
1845  * consistent with the other get_bit and set_bit functions, but
1846  * inconsistent with the standard substring, position, overlay functions
1847  */
1848 Datum
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 VARBIT_PAD_LAST(vb, ptr)
Definition: varbit.c:58
Datum bittypmodin(PG_FUNCTION_ARGS)
Definition: varbit.c:428
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:351
#define VARBITS(PTR)
Definition: varbit.h:56
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
Datum bit_and(PG_FUNCTION_ARGS)
Definition: varbit.c:1223
Datum bitle(PG_FUNCTION_ARGS)
Definition: varbit.c:903
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
#define MaxAttrSize
Definition: htup_details.h:585
Datum varbit_in(PG_FUNCTION_ARGS)
Definition: varbit.c:451
Datum bitgetbit(PG_FUNCTION_ARGS)
Definition: varbit.c:1849
List * args
Definition: primnodes.h:463
Datum bitfromint4(PG_FUNCTION_ARGS)
Definition: varbit.c:1511
#define DatumGetInt32(X)
Definition: postgres.h:472
Datum bitlength(PG_FUNCTION_ARGS)
Definition: varbit.c:1203
#define BITS_PER_BYTE
Datum bittoint4(PG_FUNCTION_ARGS)
Definition: varbit.c:1566
Datum bit_recv(PG_FUNCTION_ARGS)
Definition: varbit.c:330
#define VARSIZE(PTR)
Definition: postgres.h:303
#define PG_RETURN_INT64(x)
Definition: fmgr.h:357
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:275
#define VARBITBYTES(PTR)
Definition: varbit.h:58
Datum varbit_out(PG_FUNCTION_ARGS)
Definition: varbit.c:586
void pq_begintypsend(StringInfo buf)
Definition: pqformat.c:328
#define VARHDRSZ
Definition: c.h:556
int32 * ArrayGetIntegerTypmods(ArrayType *arr, int *n)
Definition: arrayutils.c:200
Datum bitcmp(PG_FUNCTION_ARGS)
Definition: varbit.c:948
StringInfoData * StringInfo
Definition: stringinfo.h:44
#define Min(x, y)
Definition: c.h:905
#define PG_RETURN_INT32(x)
Definition: fmgr.h:344
Definition: nodes.h:525
int errcode(int sqlerrcode)
Definition: elog.c:608
#define MemSet(start, val, len)
Definition: c.h:956
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:271
Datum bitsubstr(PG_FUNCTION_ARGS)
Definition: varbit.c:1037
#define PG_GETARG_VARBIT_P(n)
Definition: varbit.h:47
#define VARBITTOTALLEN(BITLEN)
Definition: varbit.h:62
static int32 anybit_typmodin(ArrayType *ta, const char *typename)
Definition: varbit.c:89
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:269
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:360
unsigned int Oid
Definition: postgres_ext.h:31
Datum bitxor(PG_FUNCTION_ARGS)
Definition: varbit.c:1304
static VarBit * bitsubstring(VarBit *arg, int32 s, int32 l, bool length_not_specified)
Definition: varbit.c:1054
#define DEBUG4
Definition: elog.h:22
bytea * pq_endtypsend(StringInfo buf)
Definition: pqformat.c:348
Datum bitge(PG_FUNCTION_ARGS)
Definition: varbit.c:933
#define VARBITLEN(PTR)
Definition: varbit.h:54
#define lsecond(l)
Definition: pg_list.h:200
static char * anybit_typmodout(int32 typmod)
Definition: varbit.c:126
#define VARBIT_PAD(vb)
Definition: varbit.c:46
signed int int32
Definition: c.h:347
static VarBit * bit_catenate(VarBit *arg1, VarBit *arg2)
Definition: varbit.c:976
#define VARBITMAXLEN
Definition: varbit.h:68
static void pq_sendint32(StringInfo buf, uint32 i)
Definition: pqformat.h:145
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:251
#define IS_HIGHBIT_SET(ch)
Definition: c.h:1100
static int32 bit_cmp(VarBit *arg1, VarBit *arg2)
Definition: varbit.c:817
#define linitial(l)
Definition: pg_list.h:195
#define ERROR
Definition: elog.h:43
char * s1
Datum bit_in(PG_FUNCTION_ARGS)
Definition: varbit.c:146
Datum bitlt(PG_FUNCTION_ARGS)
Definition: varbit.c:888
Datum varbit(PG_FUNCTION_ARGS)
Definition: varbit.c:741
#define PG_RETURN_VARBIT_P(x)
Definition: varbit.h:49
static char * buf
Definition: pg_test_fsync.c:67
Datum varbittypmodin(PG_FUNCTION_ARGS)
Definition: varbit.c:773
Datum bittypmodout(PG_FUNCTION_ARGS)
Definition: varbit.c:436
#define PG_GETARG_OID(n)
Definition: fmgr.h:270
Definition: varbit.h:30
Datum bit_out(PG_FUNCTION_ARGS)
Definition: varbit.c:279
Datum bitsetbit(PG_FUNCTION_ARGS)
Definition: varbit.c:1787
unsigned int uint32
Definition: c.h:359
#define ereport(elevel, rest)
Definition: elog.h:141
#define HIGHBIT
Definition: c.h:1099
#define VARBITHDRSZ
Definition: varbit.h:52
#define VARBITPAD(PTR)
Definition: varbit.h:60
Datum bitshiftright(PG_FUNCTION_ARGS)
Definition: varbit.c:1439
#define VARBITEND(PTR)
Definition: varbit.h:70
uint8 bits8
Definition: c.h:366
#define BITMASK
Definition: varbit.h:72
void * palloc0(Size size)
Definition: mcxt.c:980
Datum bitgt(PG_FUNCTION_ARGS)
Definition: varbit.c:918
char * s2
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
uintptr_t Datum
Definition: postgres.h:367
Datum varbittypmodout(PG_FUNCTION_ARGS)
Definition: varbit.c:781
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:343
Datum bitposition(PG_FUNCTION_ARGS)
Definition: varbit.c:1678
static VarBit * bit_overlay(VarBit *t1, VarBit *t2, int sp, int sl)
Definition: varbit.c:1169
Datum bitcat(PG_FUNCTION_ARGS)
Definition: varbit.c:967
Datum biteq(PG_FUNCTION_ARGS)
Definition: varbit.c:840
Datum bitshiftleft(PG_FUNCTION_ARGS)
Definition: varbit.c:1372
#define Max(x, y)
Definition: c.h:899
Datum bitoverlay(PG_FUNCTION_ARGS)
Definition: varbit.c:1146
Datum varbit_recv(PG_FUNCTION_ARGS)
Definition: varbit.c:635
Datum bit(PG_FUNCTION_ARGS)
Definition: varbit.c:390
#define Assert(condition)
Definition: c.h:733
#define VarBitPGetDatum(X)
Definition: varbit.h:46
void pq_copymsgbytes(StringInfo msg, char *buf, int datalen)
Definition: pqformat.c:530
static bool pg_add_s32_overflow(int32 a, int32 b, int32 *result)
Definition: int.h:104
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:352
static int list_length(const List *l)
Definition: pg_list.h:169
Datum bit_send(PG_FUNCTION_ARGS)
Definition: varbit.c:375
#define VARBIT_CORRECTLY_PADDED(vb)
Definition: varbit.c:76
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:255
Datum bitsubstr_no_len(PG_FUNCTION_ARGS)
Definition: varbit.c:1046
Datum bitoctetlength(PG_FUNCTION_ARGS)
Definition: varbit.c:1211
Datum bittoint8(PG_FUNCTION_ARGS)
Definition: varbit.c:1646
Datum varbit_send(PG_FUNCTION_ARGS)
Definition: varbit.c:680
#define Int32GetDatum(X)
Definition: postgres.h:479
e
Definition: preproc-init.c:82
void pq_sendbytes(StringInfo buf, const char *data, int datalen)
Definition: pqformat.c:125
void * palloc(Size size)
Definition: mcxt.c:949
int errmsg(const char *fmt,...)
Definition: elog.c:822
#define elog(elevel,...)
Definition: elog.h:228
Datum bit_or(PG_FUNCTION_ARGS)
Definition: varbit.c:1264
int i
Datum varbit_support(PG_FUNCTION_ARGS)
Definition: varbit.c:701
Datum bitfromint8(PG_FUNCTION_ARGS)
Definition: varbit.c:1591
void * arg
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:272
Datum bitnot(PG_FUNCTION_ARGS)
Definition: varbit.c:1345
#define PG_FUNCTION_ARGS
Definition: fmgr.h:188
Datum bitoverlay_no_len(PG_FUNCTION_ARGS)
Definition: varbit.c:1157
unsigned int pq_getmsgint(StringInfo msg, int b)
Definition: pqformat.c:417
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329
#define HEXDIG(z)
Definition: varbit.c:43
#define PG_GETARG_INT64(n)
Definition: fmgr.h:277
#define snprintf
Definition: port.h:192
long val
Definition: informix.c:684
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:617
Node * relabel_to_typmod(Node *expr, int32 typmod)
Definition: nodeFuncs.c:587
Datum bitne(PG_FUNCTION_ARGS)
Definition: varbit.c:864
static int cmp(const chr *x, const chr *y, size_t len)
Definition: regc_locale.c:742