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  * Code originally contributed by Adriaan Joubert.
7  *
8  * Portions Copyright (c) 1996-2018, PostgreSQL Global Development Group
9  * Portions Copyright (c) 1994, Regents of the University of California
10  *
11  * IDENTIFICATION
12  * src/backend/utils/adt/varbit.c
13  *
14  *-------------------------------------------------------------------------
15  */
16 
17 #include "postgres.h"
18 
19 #include "access/htup_details.h"
20 #include "common/int.h"
21 #include "libpq/pqformat.h"
22 #include "nodes/nodeFuncs.h"
23 #include "utils/array.h"
24 #include "utils/builtins.h"
25 #include "utils/varbit.h"
26 
27 #define HEXDIG(z) ((z)<10 ? ((z)+'0') : ((z)-10+'A'))
28 
29 static VarBit *bit_catenate(VarBit *arg1, VarBit *arg2);
30 static VarBit *bitsubstring(VarBit *arg, int32 s, int32 l,
31  bool length_not_specified);
32 static VarBit *bit_overlay(VarBit *t1, VarBit *t2, int sp, int sl);
33 
34 
35 /*
36  * common code for bittypmodin and varbittypmodin
37  */
38 static int32
39 anybit_typmodin(ArrayType *ta, const char *typename)
40 {
41  int32 typmod;
42  int32 *tl;
43  int n;
44 
45  tl = ArrayGetIntegerTypmods(ta, &n);
46 
47  /*
48  * we're not too tense about good error message here because grammar
49  * shouldn't allow wrong number of modifiers for BIT
50  */
51  if (n != 1)
52  ereport(ERROR,
53  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
54  errmsg("invalid type modifier")));
55 
56  if (*tl < 1)
57  ereport(ERROR,
58  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
59  errmsg("length for type %s must be at least 1",
60  typename)));
61  if (*tl > (MaxAttrSize * BITS_PER_BYTE))
62  ereport(ERROR,
63  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
64  errmsg("length for type %s cannot exceed %d",
65  typename, MaxAttrSize * BITS_PER_BYTE)));
66 
67  typmod = *tl;
68 
69  return typmod;
70 }
71 
72 /*
73  * common code for bittypmodout and varbittypmodout
74  */
75 static char *
77 {
78  char *res = (char *) palloc(64);
79 
80  if (typmod >= 0)
81  snprintf(res, 64, "(%d)", typmod);
82  else
83  *res = '\0';
84 
85  return res;
86 }
87 
88 
89 /*----------
90  * attypmod -- contains the length of the bit string in bits, or for
91  * varying bits the maximum length.
92  *
93  * The data structure contains the following elements:
94  * header -- length of the whole data structure (incl header)
95  * in bytes. (as with all varying length datatypes)
96  * data section -- private data section for the bits data structures
97  * bitlength -- length of the bit string in bits
98  * bitdata -- bit string, most significant byte first
99  *
100  * The length of the bitdata vector should always be exactly as many
101  * bytes as are needed for the given bitlength. If the bitlength is
102  * not a multiple of 8, the extra low-order padding bits of the last
103  * byte must be zeroes.
104  *----------
105  */
106 
107 /*
108  * bit_in -
109  * converts a char string to the internal representation of a bitstring.
110  * The length is determined by the number of bits required plus
111  * VARHDRSZ bytes or from atttypmod.
112  */
113 Datum
115 {
116  char *input_string = PG_GETARG_CSTRING(0);
117 
118 #ifdef NOT_USED
119  Oid typelem = PG_GETARG_OID(1);
120 #endif
121  int32 atttypmod = PG_GETARG_INT32(2);
122  VarBit *result; /* The resulting bit string */
123  char *sp; /* pointer into the character string */
124  bits8 *r; /* pointer into the result */
125  int len, /* Length of the whole data structure */
126  bitlen, /* Number of bits in the bit string */
127  slen; /* Length of the input string */
128  bool bit_not_hex; /* false = hex string true = bit string */
129  int bc;
130  bits8 x = 0;
131 
132  /* Check that the first character is a b or an x */
133  if (input_string[0] == 'b' || input_string[0] == 'B')
134  {
135  bit_not_hex = true;
136  sp = input_string + 1;
137  }
138  else if (input_string[0] == 'x' || input_string[0] == 'X')
139  {
140  bit_not_hex = false;
141  sp = input_string + 1;
142  }
143  else
144  {
145  /*
146  * Otherwise it's binary. This allows things like cast('1001' as bit)
147  * to work transparently.
148  */
149  bit_not_hex = true;
150  sp = input_string;
151  }
152 
153  /*
154  * Determine bitlength from input string. MaxAllocSize ensures a regular
155  * input is small enough, but we must check hex input.
156  */
157  slen = strlen(sp);
158  if (bit_not_hex)
159  bitlen = slen;
160  else
161  {
162  if (slen > VARBITMAXLEN / 4)
163  ereport(ERROR,
164  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
165  errmsg("bit string length exceeds the maximum allowed (%d)",
166  VARBITMAXLEN)));
167  bitlen = slen * 4;
168  }
169 
170  /*
171  * Sometimes atttypmod is not supplied. If it is supplied we need to make
172  * sure that the bitstring fits.
173  */
174  if (atttypmod <= 0)
175  atttypmod = bitlen;
176  else if (bitlen != atttypmod)
177  ereport(ERROR,
178  (errcode(ERRCODE_STRING_DATA_LENGTH_MISMATCH),
179  errmsg("bit string length %d does not match type bit(%d)",
180  bitlen, atttypmod)));
181 
182  len = VARBITTOTALLEN(atttypmod);
183  /* set to 0 so that *r is always initialised and string is zero-padded */
184  result = (VarBit *) palloc0(len);
185  SET_VARSIZE(result, len);
186  VARBITLEN(result) = atttypmod;
187 
188  r = VARBITS(result);
189  if (bit_not_hex)
190  {
191  /* Parse the bit representation of the string */
192  /* We know it fits, as bitlen was compared to atttypmod */
193  x = HIGHBIT;
194  for (; *sp; sp++)
195  {
196  if (*sp == '1')
197  *r |= x;
198  else if (*sp != '0')
199  ereport(ERROR,
200  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
201  errmsg("\"%c\" is not a valid binary digit",
202  *sp)));
203 
204  x >>= 1;
205  if (x == 0)
206  {
207  x = HIGHBIT;
208  r++;
209  }
210  }
211  }
212  else
213  {
214  /* Parse the hex representation of the string */
215  for (bc = 0; *sp; sp++)
216  {
217  if (*sp >= '0' && *sp <= '9')
218  x = (bits8) (*sp - '0');
219  else if (*sp >= 'A' && *sp <= 'F')
220  x = (bits8) (*sp - 'A') + 10;
221  else if (*sp >= 'a' && *sp <= 'f')
222  x = (bits8) (*sp - 'a') + 10;
223  else
224  ereport(ERROR,
225  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
226  errmsg("\"%c\" is not a valid hexadecimal digit",
227  *sp)));
228 
229  if (bc)
230  {
231  *r++ |= x;
232  bc = 0;
233  }
234  else
235  {
236  *r = x << 4;
237  bc = 1;
238  }
239  }
240  }
241 
242  PG_RETURN_VARBIT_P(result);
243 }
244 
245 
246 Datum
248 {
249 #if 1
250  /* same as varbit output */
251  return varbit_out(fcinfo);
252 #else
253 
254  /*
255  * This is how one would print a hex string, in case someone wants to
256  * write a formatting function.
257  */
258  VarBit *s = PG_GETARG_VARBIT_P(0);
259  char *result,
260  *r;
261  bits8 *sp;
262  int i,
263  len,
264  bitlen;
265 
266  bitlen = VARBITLEN(s);
267  len = (bitlen + 3) / 4;
268  result = (char *) palloc(len + 2);
269  sp = VARBITS(s);
270  r = result;
271  *r++ = 'X';
272  /* we cheat by knowing that we store full bytes zero padded */
273  for (i = 0; i < len; i += 2, sp++)
274  {
275  *r++ = HEXDIG((*sp) >> 4);
276  *r++ = HEXDIG((*sp) & 0xF);
277  }
278 
279  /*
280  * Go back one step if we printed a hex number that was not part of the
281  * bitstring anymore
282  */
283  if (i > len)
284  r--;
285  *r = '\0';
286 
287  PG_RETURN_CSTRING(result);
288 #endif
289 }
290 
291 /*
292  * bit_recv - converts external binary format to bit
293  */
294 Datum
296 {
298 
299 #ifdef NOT_USED
300  Oid typelem = PG_GETARG_OID(1);
301 #endif
302  int32 atttypmod = PG_GETARG_INT32(2);
303  VarBit *result;
304  int len,
305  bitlen;
306  int ipad;
307  bits8 mask;
308 
309  bitlen = pq_getmsgint(buf, sizeof(int32));
310  if (bitlen < 0 || bitlen > VARBITMAXLEN)
311  ereport(ERROR,
312  (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
313  errmsg("invalid length in external bit string")));
314 
315  /*
316  * Sometimes atttypmod is not supplied. If it is supplied we need to make
317  * sure that the bitstring fits.
318  */
319  if (atttypmod > 0 && bitlen != atttypmod)
320  ereport(ERROR,
321  (errcode(ERRCODE_STRING_DATA_LENGTH_MISMATCH),
322  errmsg("bit string length %d does not match type bit(%d)",
323  bitlen, atttypmod)));
324 
325  len = VARBITTOTALLEN(bitlen);
326  result = (VarBit *) palloc(len);
327  SET_VARSIZE(result, len);
328  VARBITLEN(result) = bitlen;
329 
330  pq_copymsgbytes(buf, (char *) VARBITS(result), VARBITBYTES(result));
331 
332  /* Make sure last byte is zero-padded if needed */
333  ipad = VARBITPAD(result);
334  if (ipad > 0)
335  {
336  mask = BITMASK << ipad;
337  *(VARBITS(result) + VARBITBYTES(result) - 1) &= mask;
338  }
339 
340  PG_RETURN_VARBIT_P(result);
341 }
342 
343 /*
344  * bit_send - converts bit to binary format
345  */
346 Datum
348 {
349  /* Exactly the same as varbit_send, so share code */
350  return varbit_send(fcinfo);
351 }
352 
353 /*
354  * bit()
355  * Converts a bit() type to a specific internal length.
356  * len is the bitlength specified in the column definition.
357  *
358  * If doing implicit cast, raise error when source data is wrong length.
359  * If doing explicit cast, silently truncate or zero-pad to specified length.
360  */
361 Datum
363 {
365  int32 len = PG_GETARG_INT32(1);
366  bool isExplicit = PG_GETARG_BOOL(2);
367  VarBit *result;
368  int rlen;
369  int ipad;
370  bits8 mask;
371 
372  /* No work if typmod is invalid or supplied data matches it already */
373  if (len <= 0 || len > VARBITMAXLEN || len == VARBITLEN(arg))
374  PG_RETURN_VARBIT_P(arg);
375 
376  if (!isExplicit)
377  ereport(ERROR,
378  (errcode(ERRCODE_STRING_DATA_LENGTH_MISMATCH),
379  errmsg("bit string length %d does not match type bit(%d)",
380  VARBITLEN(arg), len)));
381 
382  rlen = VARBITTOTALLEN(len);
383  /* set to 0 so that string is zero-padded */
384  result = (VarBit *) palloc0(rlen);
385  SET_VARSIZE(result, rlen);
386  VARBITLEN(result) = len;
387 
388  memcpy(VARBITS(result), VARBITS(arg),
389  Min(VARBITBYTES(result), VARBITBYTES(arg)));
390 
391  /*
392  * Make sure last byte is zero-padded if needed. This is useless but safe
393  * if source data was shorter than target length (we assume the last byte
394  * of the source data was itself correctly zero-padded).
395  */
396  ipad = VARBITPAD(result);
397  if (ipad > 0)
398  {
399  mask = BITMASK << ipad;
400  *(VARBITS(result) + VARBITBYTES(result) - 1) &= mask;
401  }
402 
403  PG_RETURN_VARBIT_P(result);
404 }
405 
406 Datum
408 {
410 
411  PG_RETURN_INT32(anybit_typmodin(ta, "bit"));
412 }
413 
414 Datum
416 {
417  int32 typmod = PG_GETARG_INT32(0);
418 
420 }
421 
422 
423 /*
424  * varbit_in -
425  * converts a string to the internal representation of a bitstring.
426  * This is the same as bit_in except that atttypmod is taken as
427  * the maximum length, not the exact length to force the bitstring to.
428  */
429 Datum
431 {
432  char *input_string = PG_GETARG_CSTRING(0);
433 
434 #ifdef NOT_USED
435  Oid typelem = PG_GETARG_OID(1);
436 #endif
437  int32 atttypmod = PG_GETARG_INT32(2);
438  VarBit *result; /* The resulting bit string */
439  char *sp; /* pointer into the character string */
440  bits8 *r; /* pointer into the result */
441  int len, /* Length of the whole data structure */
442  bitlen, /* Number of bits in the bit string */
443  slen; /* Length of the input string */
444  bool bit_not_hex; /* false = hex string true = bit string */
445  int bc;
446  bits8 x = 0;
447 
448  /* Check that the first character is a b or an x */
449  if (input_string[0] == 'b' || input_string[0] == 'B')
450  {
451  bit_not_hex = true;
452  sp = input_string + 1;
453  }
454  else if (input_string[0] == 'x' || input_string[0] == 'X')
455  {
456  bit_not_hex = false;
457  sp = input_string + 1;
458  }
459  else
460  {
461  bit_not_hex = true;
462  sp = input_string;
463  }
464 
465  /*
466  * Determine bitlength from input string. MaxAllocSize ensures a regular
467  * input is small enough, but we must check hex input.
468  */
469  slen = strlen(sp);
470  if (bit_not_hex)
471  bitlen = slen;
472  else
473  {
474  if (slen > VARBITMAXLEN / 4)
475  ereport(ERROR,
476  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
477  errmsg("bit string length exceeds the maximum allowed (%d)",
478  VARBITMAXLEN)));
479  bitlen = slen * 4;
480  }
481 
482  /*
483  * Sometimes atttypmod is not supplied. If it is supplied we need to make
484  * sure that the bitstring fits.
485  */
486  if (atttypmod <= 0)
487  atttypmod = bitlen;
488  else if (bitlen > atttypmod)
489  ereport(ERROR,
490  (errcode(ERRCODE_STRING_DATA_RIGHT_TRUNCATION),
491  errmsg("bit string too long for type bit varying(%d)",
492  atttypmod)));
493 
494  len = VARBITTOTALLEN(bitlen);
495  /* set to 0 so that *r is always initialised and string is zero-padded */
496  result = (VarBit *) palloc0(len);
497  SET_VARSIZE(result, len);
498  VARBITLEN(result) = Min(bitlen, atttypmod);
499 
500  r = VARBITS(result);
501  if (bit_not_hex)
502  {
503  /* Parse the bit representation of the string */
504  /* We know it fits, as bitlen was compared to atttypmod */
505  x = HIGHBIT;
506  for (; *sp; sp++)
507  {
508  if (*sp == '1')
509  *r |= x;
510  else if (*sp != '0')
511  ereport(ERROR,
512  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
513  errmsg("\"%c\" is not a valid binary digit",
514  *sp)));
515 
516  x >>= 1;
517  if (x == 0)
518  {
519  x = HIGHBIT;
520  r++;
521  }
522  }
523  }
524  else
525  {
526  /* Parse the hex representation of the string */
527  for (bc = 0; *sp; sp++)
528  {
529  if (*sp >= '0' && *sp <= '9')
530  x = (bits8) (*sp - '0');
531  else if (*sp >= 'A' && *sp <= 'F')
532  x = (bits8) (*sp - 'A') + 10;
533  else if (*sp >= 'a' && *sp <= 'f')
534  x = (bits8) (*sp - 'a') + 10;
535  else
536  ereport(ERROR,
537  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
538  errmsg("\"%c\" is not a valid hexadecimal digit",
539  *sp)));
540 
541  if (bc)
542  {
543  *r++ |= x;
544  bc = 0;
545  }
546  else
547  {
548  *r = x << 4;
549  bc = 1;
550  }
551  }
552  }
553 
554  PG_RETURN_VARBIT_P(result);
555 }
556 
557 /*
558  * varbit_out -
559  * Prints the string as bits to preserve length accurately
560  *
561  * XXX varbit_recv() and hex input to varbit_in() can load a value that this
562  * cannot emit. Consider using hex output for such values.
563  */
564 Datum
566 {
567  VarBit *s = PG_GETARG_VARBIT_P(0);
568  char *result,
569  *r;
570  bits8 *sp;
571  bits8 x;
572  int i,
573  k,
574  len;
575 
576  len = VARBITLEN(s);
577  result = (char *) palloc(len + 1);
578  sp = VARBITS(s);
579  r = result;
580  for (i = 0; i <= len - BITS_PER_BYTE; i += BITS_PER_BYTE, sp++)
581  {
582  /* print full bytes */
583  x = *sp;
584  for (k = 0; k < BITS_PER_BYTE; k++)
585  {
586  *r++ = IS_HIGHBIT_SET(x) ? '1' : '0';
587  x <<= 1;
588  }
589  }
590  if (i < len)
591  {
592  /* print the last partial byte */
593  x = *sp;
594  for (k = i; k < len; k++)
595  {
596  *r++ = IS_HIGHBIT_SET(x) ? '1' : '0';
597  x <<= 1;
598  }
599  }
600  *r = '\0';
601 
602  PG_RETURN_CSTRING(result);
603 }
604 
605 /*
606  * varbit_recv - converts external binary format to varbit
607  *
608  * External format is the bitlen as an int32, then the byte array.
609  */
610 Datum
612 {
614 
615 #ifdef NOT_USED
616  Oid typelem = PG_GETARG_OID(1);
617 #endif
618  int32 atttypmod = PG_GETARG_INT32(2);
619  VarBit *result;
620  int len,
621  bitlen;
622  int ipad;
623  bits8 mask;
624 
625  bitlen = pq_getmsgint(buf, sizeof(int32));
626  if (bitlen < 0 || bitlen > VARBITMAXLEN)
627  ereport(ERROR,
628  (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
629  errmsg("invalid length in external bit string")));
630 
631  /*
632  * Sometimes atttypmod is not supplied. If it is supplied we need to make
633  * sure that the bitstring fits.
634  */
635  if (atttypmod > 0 && bitlen > atttypmod)
636  ereport(ERROR,
637  (errcode(ERRCODE_STRING_DATA_RIGHT_TRUNCATION),
638  errmsg("bit string too long for type bit varying(%d)",
639  atttypmod)));
640 
641  len = VARBITTOTALLEN(bitlen);
642  result = (VarBit *) palloc(len);
643  SET_VARSIZE(result, len);
644  VARBITLEN(result) = bitlen;
645 
646  pq_copymsgbytes(buf, (char *) VARBITS(result), VARBITBYTES(result));
647 
648  /* Make sure last byte is zero-padded if needed */
649  ipad = VARBITPAD(result);
650  if (ipad > 0)
651  {
652  mask = BITMASK << ipad;
653  *(VARBITS(result) + VARBITBYTES(result) - 1) &= mask;
654  }
655 
656  PG_RETURN_VARBIT_P(result);
657 }
658 
659 /*
660  * varbit_send - converts varbit to binary format
661  */
662 Datum
664 {
665  VarBit *s = PG_GETARG_VARBIT_P(0);
667 
668  pq_begintypsend(&buf);
669  pq_sendint32(&buf, VARBITLEN(s));
670  pq_sendbytes(&buf, (char *) VARBITS(s), VARBITBYTES(s));
672 }
673 
674 /*
675  * varbit_transform()
676  * Flatten calls to varbit's length coercion function that set the new maximum
677  * length >= the previous maximum length. We can ignore the isExplicit
678  * argument, since that only affects truncation cases.
679  */
680 Datum
682 {
684  Node *ret = NULL;
685  Node *typmod;
686 
687  Assert(list_length(expr->args) >= 2);
688 
689  typmod = (Node *) lsecond(expr->args);
690 
691  if (IsA(typmod, Const) &&!((Const *) typmod)->constisnull)
692  {
693  Node *source = (Node *) linitial(expr->args);
694  int32 new_typmod = DatumGetInt32(((Const *) typmod)->constvalue);
695  int32 old_max = exprTypmod(source);
696  int32 new_max = new_typmod;
697 
698  /* Note: varbit() treats typmod 0 as invalid, so we do too */
699  if (new_max <= 0 || (old_max > 0 && old_max <= new_max))
700  ret = relabel_to_typmod(source, new_typmod);
701  }
702 
703  PG_RETURN_POINTER(ret);
704 }
705 
706 /*
707  * varbit()
708  * Converts a varbit() type to a specific internal length.
709  * len is the maximum bitlength specified in the column definition.
710  *
711  * If doing implicit cast, raise error when source data is too long.
712  * If doing explicit cast, silently truncate to max length.
713  */
714 Datum
716 {
718  int32 len = PG_GETARG_INT32(1);
719  bool isExplicit = PG_GETARG_BOOL(2);
720  VarBit *result;
721  int rlen;
722  int ipad;
723  bits8 mask;
724 
725  /* No work if typmod is invalid or supplied data matches it already */
726  if (len <= 0 || len >= VARBITLEN(arg))
727  PG_RETURN_VARBIT_P(arg);
728 
729  if (!isExplicit)
730  ereport(ERROR,
731  (errcode(ERRCODE_STRING_DATA_RIGHT_TRUNCATION),
732  errmsg("bit string too long for type bit varying(%d)",
733  len)));
734 
735  rlen = VARBITTOTALLEN(len);
736  result = (VarBit *) palloc(rlen);
737  SET_VARSIZE(result, rlen);
738  VARBITLEN(result) = len;
739 
740  memcpy(VARBITS(result), VARBITS(arg), VARBITBYTES(result));
741 
742  /* Make sure last byte is zero-padded if needed */
743  ipad = VARBITPAD(result);
744  if (ipad > 0)
745  {
746  mask = BITMASK << ipad;
747  *(VARBITS(result) + VARBITBYTES(result) - 1) &= mask;
748  }
749 
750  PG_RETURN_VARBIT_P(result);
751 }
752 
753 Datum
755 {
757 
758  PG_RETURN_INT32(anybit_typmodin(ta, "varbit"));
759 }
760 
761 Datum
763 {
764  int32 typmod = PG_GETARG_INT32(0);
765 
767 }
768 
769 
770 /*
771  * Comparison operators
772  *
773  * We only need one set of comparison operators for bitstrings, as the lengths
774  * are stored in the same way for zero-padded and varying bit strings.
775  *
776  * Note that the standard is not unambiguous about the comparison between
777  * zero-padded bit strings and varying bitstrings. If the same value is written
778  * into a zero padded bitstring as into a varying bitstring, but the zero
779  * padded bitstring has greater length, it will be bigger.
780  *
781  * Zeros from the beginning of a bitstring cannot simply be ignored, as they
782  * may be part of a bit string and may be significant.
783  *
784  * Note: btree indexes need these routines not to leak memory; therefore,
785  * be careful to free working copies of toasted datums. Most places don't
786  * need to be so careful.
787  */
788 
789 /*
790  * bit_cmp
791  *
792  * Compares two bitstrings and returns <0, 0, >0 depending on whether the first
793  * string is smaller, equal, or bigger than the second. All bits are considered
794  * and additional zero bits may make one string smaller/larger than the other,
795  * even if their zero-padded values would be the same.
796  */
797 static int32
798 bit_cmp(VarBit *arg1, VarBit *arg2)
799 {
800  int bitlen1,
801  bytelen1,
802  bitlen2,
803  bytelen2;
804  int32 cmp;
805 
806  bytelen1 = VARBITBYTES(arg1);
807  bytelen2 = VARBITBYTES(arg2);
808 
809  cmp = memcmp(VARBITS(arg1), VARBITS(arg2), Min(bytelen1, bytelen2));
810  if (cmp == 0)
811  {
812  bitlen1 = VARBITLEN(arg1);
813  bitlen2 = VARBITLEN(arg2);
814  if (bitlen1 != bitlen2)
815  cmp = (bitlen1 < bitlen2) ? -1 : 1;
816  }
817  return cmp;
818 }
819 
820 Datum
822 {
823  VarBit *arg1 = PG_GETARG_VARBIT_P(0);
824  VarBit *arg2 = PG_GETARG_VARBIT_P(1);
825  bool result;
826  int bitlen1,
827  bitlen2;
828 
829  bitlen1 = VARBITLEN(arg1);
830  bitlen2 = VARBITLEN(arg2);
831 
832  /* fast path for different-length inputs */
833  if (bitlen1 != bitlen2)
834  result = false;
835  else
836  result = (bit_cmp(arg1, arg2) == 0);
837 
838  PG_FREE_IF_COPY(arg1, 0);
839  PG_FREE_IF_COPY(arg2, 1);
840 
841  PG_RETURN_BOOL(result);
842 }
843 
844 Datum
846 {
847  VarBit *arg1 = PG_GETARG_VARBIT_P(0);
848  VarBit *arg2 = PG_GETARG_VARBIT_P(1);
849  bool result;
850  int bitlen1,
851  bitlen2;
852 
853  bitlen1 = VARBITLEN(arg1);
854  bitlen2 = VARBITLEN(arg2);
855 
856  /* fast path for different-length inputs */
857  if (bitlen1 != bitlen2)
858  result = true;
859  else
860  result = (bit_cmp(arg1, arg2) != 0);
861 
862  PG_FREE_IF_COPY(arg1, 0);
863  PG_FREE_IF_COPY(arg2, 1);
864 
865  PG_RETURN_BOOL(result);
866 }
867 
868 Datum
870 {
871  VarBit *arg1 = PG_GETARG_VARBIT_P(0);
872  VarBit *arg2 = PG_GETARG_VARBIT_P(1);
873  bool result;
874 
875  result = (bit_cmp(arg1, arg2) < 0);
876 
877  PG_FREE_IF_COPY(arg1, 0);
878  PG_FREE_IF_COPY(arg2, 1);
879 
880  PG_RETURN_BOOL(result);
881 }
882 
883 Datum
885 {
886  VarBit *arg1 = PG_GETARG_VARBIT_P(0);
887  VarBit *arg2 = PG_GETARG_VARBIT_P(1);
888  bool result;
889 
890  result = (bit_cmp(arg1, arg2) <= 0);
891 
892  PG_FREE_IF_COPY(arg1, 0);
893  PG_FREE_IF_COPY(arg2, 1);
894 
895  PG_RETURN_BOOL(result);
896 }
897 
898 Datum
900 {
901  VarBit *arg1 = PG_GETARG_VARBIT_P(0);
902  VarBit *arg2 = PG_GETARG_VARBIT_P(1);
903  bool result;
904 
905  result = (bit_cmp(arg1, arg2) > 0);
906 
907  PG_FREE_IF_COPY(arg1, 0);
908  PG_FREE_IF_COPY(arg2, 1);
909 
910  PG_RETURN_BOOL(result);
911 }
912 
913 Datum
915 {
916  VarBit *arg1 = PG_GETARG_VARBIT_P(0);
917  VarBit *arg2 = PG_GETARG_VARBIT_P(1);
918  bool result;
919 
920  result = (bit_cmp(arg1, arg2) >= 0);
921 
922  PG_FREE_IF_COPY(arg1, 0);
923  PG_FREE_IF_COPY(arg2, 1);
924 
925  PG_RETURN_BOOL(result);
926 }
927 
928 Datum
930 {
931  VarBit *arg1 = PG_GETARG_VARBIT_P(0);
932  VarBit *arg2 = PG_GETARG_VARBIT_P(1);
933  int32 result;
934 
935  result = bit_cmp(arg1, arg2);
936 
937  PG_FREE_IF_COPY(arg1, 0);
938  PG_FREE_IF_COPY(arg2, 1);
939 
940  PG_RETURN_INT32(result);
941 }
942 
943 /*
944  * bitcat
945  * Concatenation of bit strings
946  */
947 Datum
949 {
950  VarBit *arg1 = PG_GETARG_VARBIT_P(0);
951  VarBit *arg2 = PG_GETARG_VARBIT_P(1);
952 
953  PG_RETURN_VARBIT_P(bit_catenate(arg1, arg2));
954 }
955 
956 static VarBit *
958 {
959  VarBit *result;
960  int bitlen1,
961  bitlen2,
962  bytelen,
963  bit1pad,
964  bit2shift;
965  bits8 *pr,
966  *pa;
967 
968  bitlen1 = VARBITLEN(arg1);
969  bitlen2 = VARBITLEN(arg2);
970 
971  if (bitlen1 > VARBITMAXLEN - bitlen2)
972  ereport(ERROR,
973  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
974  errmsg("bit string length exceeds the maximum allowed (%d)",
975  VARBITMAXLEN)));
976  bytelen = VARBITTOTALLEN(bitlen1 + bitlen2);
977 
978  result = (VarBit *) palloc(bytelen);
979  SET_VARSIZE(result, bytelen);
980  VARBITLEN(result) = bitlen1 + bitlen2;
981 
982  /* Copy the first bitstring in */
983  memcpy(VARBITS(result), VARBITS(arg1), VARBITBYTES(arg1));
984 
985  /* Copy the second bit string */
986  bit1pad = VARBITPAD(arg1);
987  if (bit1pad == 0)
988  {
989  memcpy(VARBITS(result) + VARBITBYTES(arg1), VARBITS(arg2),
990  VARBITBYTES(arg2));
991  }
992  else if (bitlen2 > 0)
993  {
994  /* We need to shift all the bits to fit */
995  bit2shift = BITS_PER_BYTE - bit1pad;
996  pr = VARBITS(result) + VARBITBYTES(arg1) - 1;
997  for (pa = VARBITS(arg2); pa < VARBITEND(arg2); pa++)
998  {
999  *pr |= ((*pa >> bit2shift) & BITMASK);
1000  pr++;
1001  if (pr < VARBITEND(result))
1002  *pr = (*pa << bit1pad) & BITMASK;
1003  }
1004  }
1005 
1006  return result;
1007 }
1008 
1009 /*
1010  * bitsubstr
1011  * retrieve a substring from the bit string.
1012  * Note, s is 1-based.
1013  * SQL draft 6.10 9)
1014  */
1015 Datum
1017 {
1019  PG_GETARG_INT32(1),
1020  PG_GETARG_INT32(2),
1021  false));
1022 }
1023 
1024 Datum
1026 {
1028  PG_GETARG_INT32(1),
1029  -1, true));
1030 }
1031 
1032 static VarBit *
1033 bitsubstring(VarBit *arg, int32 s, int32 l, bool length_not_specified)
1034 {
1035  VarBit *result;
1036  int bitlen,
1037  rbitlen,
1038  len,
1039  ipad = 0,
1040  ishift,
1041  i;
1042  int e,
1043  s1,
1044  e1;
1045  bits8 mask,
1046  *r,
1047  *ps;
1048 
1049  bitlen = VARBITLEN(arg);
1050  s1 = Max(s, 1);
1051  /* If we do not have an upper bound, use end of string */
1052  if (length_not_specified)
1053  {
1054  e1 = bitlen + 1;
1055  }
1056  else
1057  {
1058  e = s + l;
1059 
1060  /*
1061  * A negative value for L is the only way for the end position to be
1062  * before the start. SQL99 says to throw an error.
1063  */
1064  if (e < s)
1065  ereport(ERROR,
1066  (errcode(ERRCODE_SUBSTRING_ERROR),
1067  errmsg("negative substring length not allowed")));
1068  e1 = Min(e, bitlen + 1);
1069  }
1070  if (s1 > bitlen || e1 <= s1)
1071  {
1072  /* Need to return a zero-length bitstring */
1073  len = VARBITTOTALLEN(0);
1074  result = (VarBit *) palloc(len);
1075  SET_VARSIZE(result, len);
1076  VARBITLEN(result) = 0;
1077  }
1078  else
1079  {
1080  /*
1081  * OK, we've got a true substring starting at position s1-1 and ending
1082  * at position e1-1
1083  */
1084  rbitlen = e1 - s1;
1085  len = VARBITTOTALLEN(rbitlen);
1086  result = (VarBit *) palloc(len);
1087  SET_VARSIZE(result, len);
1088  VARBITLEN(result) = rbitlen;
1089  len -= VARHDRSZ + VARBITHDRSZ;
1090  /* Are we copying from a byte boundary? */
1091  if ((s1 - 1) % BITS_PER_BYTE == 0)
1092  {
1093  /* Yep, we are copying bytes */
1094  memcpy(VARBITS(result), VARBITS(arg) + (s1 - 1) / BITS_PER_BYTE,
1095  len);
1096  }
1097  else
1098  {
1099  /* Figure out how much we need to shift the sequence by */
1100  ishift = (s1 - 1) % BITS_PER_BYTE;
1101  r = VARBITS(result);
1102  ps = VARBITS(arg) + (s1 - 1) / BITS_PER_BYTE;
1103  for (i = 0; i < len; i++)
1104  {
1105  *r = (*ps << ishift) & BITMASK;
1106  if ((++ps) < VARBITEND(arg))
1107  *r |= *ps >> (BITS_PER_BYTE - ishift);
1108  r++;
1109  }
1110  }
1111  /* Do we need to pad at the end? */
1112  ipad = VARBITPAD(result);
1113  if (ipad > 0)
1114  {
1115  mask = BITMASK << ipad;
1116  *(VARBITS(result) + len - 1) &= mask;
1117  }
1118  }
1119 
1120  return result;
1121 }
1122 
1123 /*
1124  * bitoverlay
1125  * Replace specified substring of first string with second
1126  *
1127  * The SQL standard defines OVERLAY() in terms of substring and concatenation.
1128  * This code is a direct implementation of what the standard says.
1129  */
1130 Datum
1132 {
1133  VarBit *t1 = PG_GETARG_VARBIT_P(0);
1134  VarBit *t2 = PG_GETARG_VARBIT_P(1);
1135  int sp = PG_GETARG_INT32(2); /* substring start position */
1136  int sl = PG_GETARG_INT32(3); /* substring length */
1137 
1138  PG_RETURN_VARBIT_P(bit_overlay(t1, t2, sp, sl));
1139 }
1140 
1141 Datum
1143 {
1144  VarBit *t1 = PG_GETARG_VARBIT_P(0);
1145  VarBit *t2 = PG_GETARG_VARBIT_P(1);
1146  int sp = PG_GETARG_INT32(2); /* substring start position */
1147  int sl;
1148 
1149  sl = VARBITLEN(t2); /* defaults to length(t2) */
1150  PG_RETURN_VARBIT_P(bit_overlay(t1, t2, sp, sl));
1151 }
1152 
1153 static VarBit *
1154 bit_overlay(VarBit *t1, VarBit *t2, int sp, int sl)
1155 {
1156  VarBit *result;
1157  VarBit *s1;
1158  VarBit *s2;
1159  int sp_pl_sl;
1160 
1161  /*
1162  * Check for possible integer-overflow cases. For negative sp, throw a
1163  * "substring length" error because that's what should be expected
1164  * according to the spec's definition of OVERLAY().
1165  */
1166  if (sp <= 0)
1167  ereport(ERROR,
1168  (errcode(ERRCODE_SUBSTRING_ERROR),
1169  errmsg("negative substring length not allowed")));
1170  if (pg_add_s32_overflow(sp, sl, &sp_pl_sl))
1171  ereport(ERROR,
1172  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1173  errmsg("integer out of range")));
1174 
1175  s1 = bitsubstring(t1, 1, sp - 1, false);
1176  s2 = bitsubstring(t1, sp_pl_sl, -1, true);
1177  result = bit_catenate(s1, t2);
1178  result = bit_catenate(result, s2);
1179 
1180  return result;
1181 }
1182 
1183 /*
1184  * bitlength, bitoctetlength
1185  * Return the length of a bit string
1186  */
1187 Datum
1189 {
1191 
1192  PG_RETURN_INT32(VARBITLEN(arg));
1193 }
1194 
1195 Datum
1197 {
1199 
1201 }
1202 
1203 /*
1204  * bit_and
1205  * perform a logical AND on two bit strings.
1206  */
1207 Datum
1209 {
1210  VarBit *arg1 = PG_GETARG_VARBIT_P(0);
1211  VarBit *arg2 = PG_GETARG_VARBIT_P(1);
1212  VarBit *result;
1213  int len,
1214  bitlen1,
1215  bitlen2,
1216  i;
1217  bits8 *p1,
1218  *p2,
1219  *r;
1220 
1221  bitlen1 = VARBITLEN(arg1);
1222  bitlen2 = VARBITLEN(arg2);
1223  if (bitlen1 != bitlen2)
1224  ereport(ERROR,
1225  (errcode(ERRCODE_STRING_DATA_LENGTH_MISMATCH),
1226  errmsg("cannot AND bit strings of different sizes")));
1227 
1228  len = VARSIZE(arg1);
1229  result = (VarBit *) palloc(len);
1230  SET_VARSIZE(result, len);
1231  VARBITLEN(result) = bitlen1;
1232 
1233  p1 = VARBITS(arg1);
1234  p2 = VARBITS(arg2);
1235  r = VARBITS(result);
1236  for (i = 0; i < VARBITBYTES(arg1); i++)
1237  *r++ = *p1++ & *p2++;
1238 
1239  /* Padding is not needed as & of 0 pad is 0 */
1240 
1241  PG_RETURN_VARBIT_P(result);
1242 }
1243 
1244 /*
1245  * bit_or
1246  * perform a logical OR on two bit strings.
1247  */
1248 Datum
1250 {
1251  VarBit *arg1 = PG_GETARG_VARBIT_P(0);
1252  VarBit *arg2 = PG_GETARG_VARBIT_P(1);
1253  VarBit *result;
1254  int len,
1255  bitlen1,
1256  bitlen2,
1257  i;
1258  bits8 *p1,
1259  *p2,
1260  *r;
1261  bits8 mask;
1262 
1263  bitlen1 = VARBITLEN(arg1);
1264  bitlen2 = VARBITLEN(arg2);
1265  if (bitlen1 != bitlen2)
1266  ereport(ERROR,
1267  (errcode(ERRCODE_STRING_DATA_LENGTH_MISMATCH),
1268  errmsg("cannot OR bit strings of different sizes")));
1269  len = VARSIZE(arg1);
1270  result = (VarBit *) palloc(len);
1271  SET_VARSIZE(result, len);
1272  VARBITLEN(result) = bitlen1;
1273 
1274  p1 = VARBITS(arg1);
1275  p2 = VARBITS(arg2);
1276  r = VARBITS(result);
1277  for (i = 0; i < VARBITBYTES(arg1); i++)
1278  *r++ = *p1++ | *p2++;
1279 
1280  /* Pad the result */
1281  mask = BITMASK << VARBITPAD(result);
1282  if (mask)
1283  {
1284  r--;
1285  *r &= mask;
1286  }
1287 
1288  PG_RETURN_VARBIT_P(result);
1289 }
1290 
1291 /*
1292  * bitxor
1293  * perform a logical XOR on two bit strings.
1294  */
1295 Datum
1297 {
1298  VarBit *arg1 = PG_GETARG_VARBIT_P(0);
1299  VarBit *arg2 = PG_GETARG_VARBIT_P(1);
1300  VarBit *result;
1301  int len,
1302  bitlen1,
1303  bitlen2,
1304  i;
1305  bits8 *p1,
1306  *p2,
1307  *r;
1308  bits8 mask;
1309 
1310  bitlen1 = VARBITLEN(arg1);
1311  bitlen2 = VARBITLEN(arg2);
1312  if (bitlen1 != bitlen2)
1313  ereport(ERROR,
1314  (errcode(ERRCODE_STRING_DATA_LENGTH_MISMATCH),
1315  errmsg("cannot XOR bit strings of different sizes")));
1316 
1317  len = VARSIZE(arg1);
1318  result = (VarBit *) palloc(len);
1319  SET_VARSIZE(result, len);
1320  VARBITLEN(result) = bitlen1;
1321 
1322  p1 = VARBITS(arg1);
1323  p2 = VARBITS(arg2);
1324  r = VARBITS(result);
1325  for (i = 0; i < VARBITBYTES(arg1); i++)
1326  *r++ = *p1++ ^ *p2++;
1327 
1328  /* Pad the result */
1329  mask = BITMASK << VARBITPAD(result);
1330  if (mask)
1331  {
1332  r--;
1333  *r &= mask;
1334  }
1335 
1336  PG_RETURN_VARBIT_P(result);
1337 }
1338 
1339 /*
1340  * bitnot
1341  * perform a logical NOT on a bit string.
1342  */
1343 Datum
1345 {
1347  VarBit *result;
1348  bits8 *p,
1349  *r;
1350  bits8 mask;
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  /* Pad the result */
1362  mask = BITMASK << VARBITPAD(result);
1363  if (mask)
1364  {
1365  r--;
1366  *r &= mask;
1367  }
1368 
1369  PG_RETURN_VARBIT_P(result);
1370 }
1371 
1372 /*
1373  * bitshiftleft
1374  * do a left shift (i.e. towards the beginning of the string)
1375  */
1376 Datum
1378 {
1380  int32 shft = PG_GETARG_INT32(1);
1381  VarBit *result;
1382  int byte_shift,
1383  ishift,
1384  len;
1385  bits8 *p,
1386  *r;
1387 
1388  /* Negative shift is a shift to the right */
1389  if (shft < 0)
1390  {
1391  /* Prevent integer overflow in negation */
1392  if (shft < -VARBITMAXLEN)
1393  shft = -VARBITMAXLEN;
1395  VarBitPGetDatum(arg),
1396  Int32GetDatum(-shft)));
1397  }
1398 
1399  result = (VarBit *) palloc(VARSIZE(arg));
1400  SET_VARSIZE(result, VARSIZE(arg));
1401  VARBITLEN(result) = VARBITLEN(arg);
1402  r = VARBITS(result);
1403 
1404  /* If we shifted all the bits out, return an all-zero string */
1405  if (shft >= VARBITLEN(arg))
1406  {
1407  MemSet(r, 0, VARBITBYTES(arg));
1408  PG_RETURN_VARBIT_P(result);
1409  }
1410 
1411  byte_shift = shft / BITS_PER_BYTE;
1412  ishift = shft % BITS_PER_BYTE;
1413  p = VARBITS(arg) + byte_shift;
1414 
1415  if (ishift == 0)
1416  {
1417  /* Special case: we can do a memcpy */
1418  len = VARBITBYTES(arg) - byte_shift;
1419  memcpy(r, p, len);
1420  MemSet(r + len, 0, byte_shift);
1421  }
1422  else
1423  {
1424  for (; p < VARBITEND(arg); r++)
1425  {
1426  *r = *p << ishift;
1427  if ((++p) < VARBITEND(arg))
1428  *r |= *p >> (BITS_PER_BYTE - ishift);
1429  }
1430  for (; r < VARBITEND(result); r++)
1431  *r = 0;
1432  }
1433 
1434  PG_RETURN_VARBIT_P(result);
1435 }
1436 
1437 /*
1438  * bitshiftright
1439  * do a right shift (i.e. towards the end of the string)
1440  */
1441 Datum
1443 {
1445  int32 shft = PG_GETARG_INT32(1);
1446  VarBit *result;
1447  int byte_shift,
1448  ishift,
1449  len;
1450  bits8 *p,
1451  *r;
1452 
1453  /* Negative shift is a shift to the left */
1454  if (shft < 0)
1455  {
1456  /* Prevent integer overflow in negation */
1457  if (shft < -VARBITMAXLEN)
1458  shft = -VARBITMAXLEN;
1460  VarBitPGetDatum(arg),
1461  Int32GetDatum(-shft)));
1462  }
1463 
1464  result = (VarBit *) palloc(VARSIZE(arg));
1465  SET_VARSIZE(result, VARSIZE(arg));
1466  VARBITLEN(result) = VARBITLEN(arg);
1467  r = VARBITS(result);
1468 
1469  /* If we shifted all the bits out, return an all-zero string */
1470  if (shft >= VARBITLEN(arg))
1471  {
1472  MemSet(r, 0, VARBITBYTES(arg));
1473  PG_RETURN_VARBIT_P(result);
1474  }
1475 
1476  byte_shift = shft / BITS_PER_BYTE;
1477  ishift = shft % BITS_PER_BYTE;
1478  p = VARBITS(arg);
1479 
1480  /* Set the first part of the result to 0 */
1481  MemSet(r, 0, byte_shift);
1482  r += byte_shift;
1483 
1484  if (ishift == 0)
1485  {
1486  /* Special case: we can do a memcpy */
1487  len = VARBITBYTES(arg) - byte_shift;
1488  memcpy(r, p, len);
1489  }
1490  else
1491  {
1492  if (r < VARBITEND(result))
1493  *r = 0; /* initialize first byte */
1494  for (; r < VARBITEND(result); p++)
1495  {
1496  *r |= *p >> ishift;
1497  if ((++r) < VARBITEND(result))
1498  *r = (*p << (BITS_PER_BYTE - ishift)) & BITMASK;
1499  }
1500  }
1501 
1502  PG_RETURN_VARBIT_P(result);
1503 }
1504 
1505 /*
1506  * This is not defined in any standard. We retain the natural ordering of
1507  * bits here, as it just seems more intuitive.
1508  */
1509 Datum
1511 {
1512  int32 a = PG_GETARG_INT32(0);
1513  int32 typmod = PG_GETARG_INT32(1);
1514  VarBit *result;
1515  bits8 *r;
1516  int rlen;
1517  int destbitsleft,
1518  srcbitsleft;
1519 
1520  if (typmod <= 0 || typmod > VARBITMAXLEN)
1521  typmod = 1; /* default bit length */
1522 
1523  rlen = VARBITTOTALLEN(typmod);
1524  result = (VarBit *) palloc(rlen);
1525  SET_VARSIZE(result, rlen);
1526  VARBITLEN(result) = typmod;
1527 
1528  r = VARBITS(result);
1529  destbitsleft = typmod;
1530  srcbitsleft = 32;
1531  /* drop any input bits that don't fit */
1532  srcbitsleft = Min(srcbitsleft, destbitsleft);
1533  /* sign-fill any excess bytes in output */
1534  while (destbitsleft >= srcbitsleft + 8)
1535  {
1536  *r++ = (bits8) ((a < 0) ? BITMASK : 0);
1537  destbitsleft -= 8;
1538  }
1539  /* store first fractional byte */
1540  if (destbitsleft > srcbitsleft)
1541  {
1542  int val = (int) (a >> (destbitsleft - 8));
1543 
1544  /* Force sign-fill in case the compiler implements >> as zero-fill */
1545  if (a < 0)
1546  val |= (-1) << (srcbitsleft + 8 - destbitsleft);
1547  *r++ = (bits8) (val & BITMASK);
1548  destbitsleft -= 8;
1549  }
1550  /* Now srcbitsleft and destbitsleft are the same, need not track both */
1551  /* store whole bytes */
1552  while (destbitsleft >= 8)
1553  {
1554  *r++ = (bits8) ((a >> (destbitsleft - 8)) & BITMASK);
1555  destbitsleft -= 8;
1556  }
1557  /* store last fractional byte */
1558  if (destbitsleft > 0)
1559  *r = (bits8) ((a << (8 - destbitsleft)) & BITMASK);
1560 
1561  PG_RETURN_VARBIT_P(result);
1562 }
1563 
1564 Datum
1566 {
1568  uint32 result;
1569  bits8 *r;
1570 
1571  /* Check that the bit string is not too long */
1572  if (VARBITLEN(arg) > sizeof(result) * BITS_PER_BYTE)
1573  ereport(ERROR,
1574  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1575  errmsg("integer out of range")));
1576 
1577  result = 0;
1578  for (r = VARBITS(arg); r < VARBITEND(arg); r++)
1579  {
1580  result <<= BITS_PER_BYTE;
1581  result |= *r;
1582  }
1583  /* Now shift the result to take account of the padding at the end */
1584  result >>= VARBITPAD(arg);
1585 
1586  PG_RETURN_INT32(result);
1587 }
1588 
1589 Datum
1591 {
1592  int64 a = PG_GETARG_INT64(0);
1593  int32 typmod = PG_GETARG_INT32(1);
1594  VarBit *result;
1595  bits8 *r;
1596  int rlen;
1597  int destbitsleft,
1598  srcbitsleft;
1599 
1600  if (typmod <= 0 || typmod > VARBITMAXLEN)
1601  typmod = 1; /* default bit length */
1602 
1603  rlen = VARBITTOTALLEN(typmod);
1604  result = (VarBit *) palloc(rlen);
1605  SET_VARSIZE(result, rlen);
1606  VARBITLEN(result) = typmod;
1607 
1608  r = VARBITS(result);
1609  destbitsleft = typmod;
1610  srcbitsleft = 64;
1611  /* drop any input bits that don't fit */
1612  srcbitsleft = Min(srcbitsleft, destbitsleft);
1613  /* sign-fill any excess bytes in output */
1614  while (destbitsleft >= srcbitsleft + 8)
1615  {
1616  *r++ = (bits8) ((a < 0) ? BITMASK : 0);
1617  destbitsleft -= 8;
1618  }
1619  /* store first fractional byte */
1620  if (destbitsleft > srcbitsleft)
1621  {
1622  int val = (int) (a >> (destbitsleft - 8));
1623 
1624  /* Force sign-fill in case the compiler implements >> as zero-fill */
1625  if (a < 0)
1626  val |= (-1) << (srcbitsleft + 8 - destbitsleft);
1627  *r++ = (bits8) (val & BITMASK);
1628  destbitsleft -= 8;
1629  }
1630  /* Now srcbitsleft and destbitsleft are the same, need not track both */
1631  /* store whole bytes */
1632  while (destbitsleft >= 8)
1633  {
1634  *r++ = (bits8) ((a >> (destbitsleft - 8)) & BITMASK);
1635  destbitsleft -= 8;
1636  }
1637  /* store last fractional byte */
1638  if (destbitsleft > 0)
1639  *r = (bits8) ((a << (8 - destbitsleft)) & BITMASK);
1640 
1641  PG_RETURN_VARBIT_P(result);
1642 }
1643 
1644 Datum
1646 {
1648  uint64 result;
1649  bits8 *r;
1650 
1651  /* Check that the bit string is not too long */
1652  if (VARBITLEN(arg) > sizeof(result) * BITS_PER_BYTE)
1653  ereport(ERROR,
1654  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1655  errmsg("bigint out of range")));
1656 
1657  result = 0;
1658  for (r = VARBITS(arg); r < VARBITEND(arg); r++)
1659  {
1660  result <<= BITS_PER_BYTE;
1661  result |= *r;
1662  }
1663  /* Now shift the result to take account of the padding at the end */
1664  result >>= VARBITPAD(arg);
1665 
1666  PG_RETURN_INT64(result);
1667 }
1668 
1669 
1670 /*
1671  * Determines the position of S2 in the bitstring S1 (1-based string).
1672  * If S2 does not appear in S1 this function returns 0.
1673  * If S2 is of length 0 this function returns 1.
1674  * Compatible in usage with POSITION() functions for other data types.
1675  */
1676 Datum
1678 {
1680  VarBit *substr = PG_GETARG_VARBIT_P(1);
1681  int substr_length,
1682  str_length,
1683  i,
1684  is;
1685  bits8 *s, /* pointer into substring */
1686  *p; /* pointer into str */
1687  bits8 cmp, /* shifted substring byte to compare */
1688  mask1, /* mask for substring byte shifted right */
1689  mask2, /* mask for substring byte shifted left */
1690  end_mask, /* pad mask for last substring byte */
1691  str_mask; /* pad mask for last string byte */
1692  bool is_match;
1693 
1694  /* Get the substring length */
1695  substr_length = VARBITLEN(substr);
1696  str_length = VARBITLEN(str);
1697 
1698  /* String has zero length or substring longer than string, return 0 */
1699  if ((str_length == 0) || (substr_length > str_length))
1700  PG_RETURN_INT32(0);
1701 
1702  /* zero-length substring means return 1 */
1703  if (substr_length == 0)
1704  PG_RETURN_INT32(1);
1705 
1706  /* Initialise the padding masks */
1707  end_mask = BITMASK << VARBITPAD(substr);
1708  str_mask = BITMASK << VARBITPAD(str);
1709  for (i = 0; i < VARBITBYTES(str) - VARBITBYTES(substr) + 1; i++)
1710  {
1711  for (is = 0; is < BITS_PER_BYTE; is++)
1712  {
1713  is_match = true;
1714  p = VARBITS(str) + i;
1715  mask1 = BITMASK >> is;
1716  mask2 = ~mask1;
1717  for (s = VARBITS(substr);
1718  is_match && s < VARBITEND(substr); s++)
1719  {
1720  cmp = *s >> is;
1721  if (s == VARBITEND(substr) - 1)
1722  {
1723  mask1 &= end_mask >> is;
1724  if (p == VARBITEND(str) - 1)
1725  {
1726  /* Check that there is enough of str left */
1727  if (mask1 & ~str_mask)
1728  {
1729  is_match = false;
1730  break;
1731  }
1732  mask1 &= str_mask;
1733  }
1734  }
1735  is_match = ((cmp ^ *p) & mask1) == 0;
1736  if (!is_match)
1737  break;
1738  /* Move on to the next byte */
1739  p++;
1740  if (p == VARBITEND(str))
1741  {
1742  mask2 = end_mask << (BITS_PER_BYTE - is);
1743  is_match = mask2 == 0;
1744 #if 0
1745  elog(DEBUG4, "S. %d %d em=%2x sm=%2x r=%d",
1746  i, is, end_mask, mask2, is_match);
1747 #endif
1748  break;
1749  }
1750  cmp = *s << (BITS_PER_BYTE - is);
1751  if (s == VARBITEND(substr) - 1)
1752  {
1753  mask2 &= end_mask << (BITS_PER_BYTE - is);
1754  if (p == VARBITEND(str) - 1)
1755  {
1756  if (mask2 & ~str_mask)
1757  {
1758  is_match = false;
1759  break;
1760  }
1761  mask2 &= str_mask;
1762  }
1763  }
1764  is_match = ((cmp ^ *p) & mask2) == 0;
1765  }
1766  /* Have we found a match? */
1767  if (is_match)
1768  PG_RETURN_INT32(i * BITS_PER_BYTE + is + 1);
1769  }
1770  }
1771  PG_RETURN_INT32(0);
1772 }
1773 
1774 
1775 /*
1776  * bitsetbit
1777  *
1778  * Given an instance of type 'bit' creates a new one with
1779  * the Nth bit set to the given value.
1780  *
1781  * The bit location is specified left-to-right in a zero-based fashion
1782  * consistent with the other get_bit and set_bit functions, but
1783  * inconsistent with the standard substring, position, overlay functions
1784  */
1785 Datum
1787 {
1788  VarBit *arg1 = PG_GETARG_VARBIT_P(0);
1789  int32 n = PG_GETARG_INT32(1);
1790  int32 newBit = PG_GETARG_INT32(2);
1791  VarBit *result;
1792  int len,
1793  bitlen;
1794  bits8 *r,
1795  *p;
1796  int byteNo,
1797  bitNo;
1798 
1799  bitlen = VARBITLEN(arg1);
1800  if (n < 0 || n >= bitlen)
1801  ereport(ERROR,
1802  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
1803  errmsg("bit index %d out of valid range (0..%d)",
1804  n, bitlen - 1)));
1805 
1806  /*
1807  * sanity check!
1808  */
1809  if (newBit != 0 && newBit != 1)
1810  ereport(ERROR,
1811  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1812  errmsg("new bit must be 0 or 1")));
1813 
1814  len = VARSIZE(arg1);
1815  result = (VarBit *) palloc(len);
1816  SET_VARSIZE(result, len);
1817  VARBITLEN(result) = bitlen;
1818 
1819  p = VARBITS(arg1);
1820  r = VARBITS(result);
1821 
1822  memcpy(r, p, VARBITBYTES(arg1));
1823 
1824  byteNo = n / BITS_PER_BYTE;
1825  bitNo = BITS_PER_BYTE - 1 - (n % BITS_PER_BYTE);
1826 
1827  /*
1828  * Update the byte.
1829  */
1830  if (newBit == 0)
1831  r[byteNo] &= (~(1 << bitNo));
1832  else
1833  r[byteNo] |= (1 << bitNo);
1834 
1835  PG_RETURN_VARBIT_P(result);
1836 }
1837 
1838 /*
1839  * bitgetbit
1840  *
1841  * returns the value of the Nth bit of a bit array (0 or 1).
1842  *
1843  * The bit location is specified left-to-right in a zero-based fashion
1844  * consistent with the other get_bit and set_bit functions, but
1845  * inconsistent with the standard substring, position, overlay functions
1846  */
1847 Datum
1849 {
1850  VarBit *arg1 = PG_GETARG_VARBIT_P(0);
1851  int32 n = PG_GETARG_INT32(1);
1852  int bitlen;
1853  bits8 *p;
1854  int byteNo,
1855  bitNo;
1856 
1857  bitlen = VARBITLEN(arg1);
1858  if (n < 0 || n >= bitlen)
1859  ereport(ERROR,
1860  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
1861  errmsg("bit index %d out of valid range (0..%d)",
1862  n, bitlen - 1)));
1863 
1864  p = VARBITS(arg1);
1865 
1866  byteNo = n / BITS_PER_BYTE;
1867  bitNo = BITS_PER_BYTE - 1 - (n % BITS_PER_BYTE);
1868 
1869  if (p[byteNo] & (1 << bitNo))
1870  PG_RETURN_INT32(1);
1871  else
1872  PG_RETURN_INT32(0);
1873 }
Datum bittypmodin(PG_FUNCTION_ARGS)
Definition: varbit.c:407
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:326
#define VARBITS(PTR)
Definition: varbit.h:51
#define PG_GETARG_INT32(n)
Definition: fmgr.h:239
Datum bit_and(PG_FUNCTION_ARGS)
Definition: varbit.c:1208
Datum bitle(PG_FUNCTION_ARGS)
Definition: varbit.c:884
#define IsA(nodeptr, _type_)
Definition: nodes.h:568
#define MaxAttrSize
Definition: htup_details.h:603
Datum varbit_in(PG_FUNCTION_ARGS)
Definition: varbit.c:430
Datum bitgetbit(PG_FUNCTION_ARGS)
Definition: varbit.c:1848
List * args
Definition: primnodes.h:458
Datum bitfromint4(PG_FUNCTION_ARGS)
Definition: varbit.c:1510
#define DatumGetInt32(X)
Definition: postgres.h:455
Datum bitlength(PG_FUNCTION_ARGS)
Definition: varbit.c:1188
#define BITS_PER_BYTE
Datum bittoint4(PG_FUNCTION_ARGS)
Definition: varbit.c:1565
Datum bit_recv(PG_FUNCTION_ARGS)
Definition: varbit.c:295
#define castNode(_type_, nodeptr)
Definition: nodes.h:586
#define VARSIZE(PTR)
Definition: postgres.h:303
#define PG_RETURN_INT64(x)
Definition: fmgr.h:332
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:276
#define VARBITBYTES(PTR)
Definition: varbit.h:53
static void pq_sendint32(StringInfo buf, int32 i)
Definition: pqformat.h:148
Datum varbit_out(PG_FUNCTION_ARGS)
Definition: varbit.c:565
void pq_begintypsend(StringInfo buf)
Definition: pqformat.c:328
#define VARHDRSZ
Definition: c.h:522
int32 * ArrayGetIntegerTypmods(ArrayType *arr, int *n)
Definition: arrayutils.c:200
Datum bitcmp(PG_FUNCTION_ARGS)
Definition: varbit.c:929
StringInfoData * StringInfo
Definition: stringinfo.h:43
#define Min(x, y)
Definition: c.h:857
#define PG_RETURN_INT32(x)
Definition: fmgr.h:319
Definition: nodes.h:517
int errcode(int sqlerrcode)
Definition: elog.c:575
#define MemSet(start, val, len)
Definition: c.h:908
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:246
Datum bitsubstr(PG_FUNCTION_ARGS)
Definition: varbit.c:1016
#define PG_GETARG_VARBIT_P(n)
Definition: varbit.h:42
#define VARBITTOTALLEN(BITLEN)
Definition: varbit.h:57
static int32 anybit_typmodin(ArrayType *ta, const char *typename)
Definition: varbit.c:39
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:244
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:335
unsigned int Oid
Definition: postgres_ext.h:31
Datum bitxor(PG_FUNCTION_ARGS)
Definition: varbit.c:1296
static VarBit * bitsubstring(VarBit *arg, int32 s, int32 l, bool length_not_specified)
Definition: varbit.c:1033
#define DEBUG4
Definition: elog.h:22
bytea * pq_endtypsend(StringInfo buf)
Definition: pqformat.c:348
Datum bitge(PG_FUNCTION_ARGS)
Definition: varbit.c:914
#define VARBITLEN(PTR)
Definition: varbit.h:49
#define lsecond(l)
Definition: pg_list.h:116
static char * anybit_typmodout(int32 typmod)
Definition: varbit.c:76
signed int int32
Definition: c.h:313
static VarBit * bit_catenate(VarBit *arg1, VarBit *arg2)
Definition: varbit.c:957
#define VARBITMAXLEN
Definition: varbit.h:63
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:248
#define IS_HIGHBIT_SET(ch)
Definition: c.h:994
static int32 bit_cmp(VarBit *arg1, VarBit *arg2)
Definition: varbit.c:798
#define linitial(l)
Definition: pg_list.h:111
#define ERROR
Definition: elog.h:43
char * s1
Datum bit_in(PG_FUNCTION_ARGS)
Definition: varbit.c:114
Datum bitlt(PG_FUNCTION_ARGS)
Definition: varbit.c:869
Datum varbit(PG_FUNCTION_ARGS)
Definition: varbit.c:715
#define PG_RETURN_VARBIT_P(x)
Definition: varbit.h:44
static char * buf
Definition: pg_test_fsync.c:67
Datum varbittypmodin(PG_FUNCTION_ARGS)
Definition: varbit.c:754
Datum bittypmodout(PG_FUNCTION_ARGS)
Definition: varbit.c:415
#define PG_GETARG_OID(n)
Definition: fmgr.h:245
Definition: varbit.h:25
Datum bit_out(PG_FUNCTION_ARGS)
Definition: varbit.c:247
Datum bitsetbit(PG_FUNCTION_ARGS)
Definition: varbit.c:1786
unsigned int uint32
Definition: c.h:325
#define ereport(elevel, rest)
Definition: elog.h:122
#define HIGHBIT
Definition: c.h:993
#define VARBITHDRSZ
Definition: varbit.h:47
#define VARBITPAD(PTR)
Definition: varbit.h:55
Datum bitshiftright(PG_FUNCTION_ARGS)
Definition: varbit.c:1442
#define VARBITEND(PTR)
Definition: varbit.h:65
uint8 bits8
Definition: c.h:332
#define BITMASK
Definition: varbit.h:67
void * palloc0(Size size)
Definition: mcxt.c:955
Datum bitgt(PG_FUNCTION_ARGS)
Definition: varbit.c:899
char * s2
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:324
uintptr_t Datum
Definition: postgres.h:365
Datum varbittypmodout(PG_FUNCTION_ARGS)
Definition: varbit.c:762
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:318
Datum bitposition(PG_FUNCTION_ARGS)
Definition: varbit.c:1677
static VarBit * bit_overlay(VarBit *t1, VarBit *t2, int sp, int sl)
Definition: varbit.c:1154
Datum bitcat(PG_FUNCTION_ARGS)
Definition: varbit.c:948
Datum biteq(PG_FUNCTION_ARGS)
Definition: varbit.c:821
Datum bitshiftleft(PG_FUNCTION_ARGS)
Definition: varbit.c:1377
#define Max(x, y)
Definition: c.h:851
Datum bitoverlay(PG_FUNCTION_ARGS)
Definition: varbit.c:1131
Datum varbit_recv(PG_FUNCTION_ARGS)
Definition: varbit.c:611
Datum bit(PG_FUNCTION_ARGS)
Definition: varbit.c:362
#define Assert(condition)
Definition: c.h:699
#define VarBitPGetDatum(X)
Definition: varbit.h:41
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:98
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:327
static int list_length(const List *l)
Definition: pg_list.h:89
Datum bit_send(PG_FUNCTION_ARGS)
Definition: varbit.c:347
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:230
Datum varbit_transform(PG_FUNCTION_ARGS)
Definition: varbit.c:681
Datum bitsubstr_no_len(PG_FUNCTION_ARGS)
Definition: varbit.c:1025
Datum bitoctetlength(PG_FUNCTION_ARGS)
Definition: varbit.c:1196
Datum bittoint8(PG_FUNCTION_ARGS)
Definition: varbit.c:1645
Datum varbit_send(PG_FUNCTION_ARGS)
Definition: varbit.c:663
#define Int32GetDatum(X)
Definition: postgres.h:462
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:924
int errmsg(const char *fmt,...)
Definition: elog.c:797
Datum bit_or(PG_FUNCTION_ARGS)
Definition: varbit.c:1249
int i
Datum bitfromint8(PG_FUNCTION_ARGS)
Definition: varbit.c:1590
void * arg
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:247
Datum bitnot(PG_FUNCTION_ARGS)
Definition: varbit.c:1344
#define PG_FUNCTION_ARGS
Definition: fmgr.h:163
Datum bitoverlay_no_len(PG_FUNCTION_ARGS)
Definition: varbit.c:1142
unsigned int pq_getmsgint(StringInfo msg, int b)
Definition: pqformat.c:417
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:327
#define elog
Definition: elog.h:219
#define HEXDIG(z)
Definition: varbit.c:27
#define PG_GETARG_INT64(n)
Definition: fmgr.h:252
long val
Definition: informix.c:689
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:592
Node * relabel_to_typmod(Node *expr, int32 typmod)
Definition: nodeFuncs.c:588
Datum bitne(PG_FUNCTION_ARGS)
Definition: varbit.c:845
static int cmp(const chr *x, const chr *y, size_t len)
Definition: regc_locale.c:742