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