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