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-2020, PostgreSQL Global Development Group
24  * Portions Copyright (c) 1994, Regents of the University of California
25  *
26  * IDENTIFICATION
27  *	  src/backend/utils/adt/varbit.c
28  *
29  *-------------------------------------------------------------------------
30  */
31 
32 #include "postgres.h"
33 
34 #include "access/htup_details.h"
35 #include "common/int.h"
36 #include "libpq/pqformat.h"
37 #include "nodes/nodeFuncs.h"
38 #include "nodes/supportnodes.h"
39 #include "utils/array.h"
40 #include "utils/builtins.h"
41 #include "utils/varbit.h"
42 
43 #define HEXDIG(z)	 ((z)<10 ? ((z)+'0') : ((z)-10+'A'))
44 
45 /* Mask off any bits that should be zero in the last byte of a bitstring */
46 #define VARBIT_PAD(vb) \
47 	do { \
48 		int32	pad_ = VARBITPAD(vb); \
49 		Assert(pad_ >= 0 && pad_ < BITS_PER_BYTE); \
50 		if (pad_ > 0) \
51 			*(VARBITS(vb) + VARBITBYTES(vb) - 1) &= BITMASK << pad_; \
52 	} while (0)
53 
54 /*
55  * Many functions work byte-by-byte, so they have a pointer handy to the
56  * last-plus-one byte, which saves a cycle or two.
57  */
58 #define VARBIT_PAD_LAST(vb, ptr) \
59 	do { \
60 		int32	pad_ = VARBITPAD(vb); \
61 		Assert(pad_ >= 0 && pad_ < BITS_PER_BYTE); \
62 		if (pad_ > 0) \
63 			*((ptr) - 1) &= BITMASK << pad_; \
64 	} while (0)
65 
66 /* Assert proper padding of a bitstring */
67 #ifdef USE_ASSERT_CHECKING
68 #define VARBIT_CORRECTLY_PADDED(vb) \
69 	do { \
70 		int32	pad_ = VARBITPAD(vb); \
71 		Assert(pad_ >= 0 && pad_ < BITS_PER_BYTE); \
72 		Assert(pad_ == 0 || \
73 			   (*(VARBITS(vb) + VARBITBYTES(vb) - 1) & ~(BITMASK << pad_)) == 0); \
74 	} while (0)
75 #else
76 #define VARBIT_CORRECTLY_PADDED(vb) ((void) 0)
77 #endif
78 
79 static VarBit *bit_catenate(VarBit *arg1, VarBit *arg2);
80 static VarBit *bitsubstring(VarBit *arg, int32 s, int32 l,
81 							bool length_not_specified);
82 static VarBit *bit_overlay(VarBit *t1, VarBit *t2, int sp, int sl);
83 
84 
85 /*
86  * common code for bittypmodin and varbittypmodin
87  */
88 static int32
anybit_typmodin(ArrayType * ta,const char * typename)89 anybit_typmodin(ArrayType *ta, const char *typename)
90 {
91 	int32		typmod;
92 	int32	   *tl;
93 	int			n;
94 
95 	tl = ArrayGetIntegerTypmods(ta, &n);
96 
97 	/*
98 	 * we're not too tense about good error message here because grammar
99 	 * shouldn't allow wrong number of modifiers for BIT
100 	 */
101 	if (n != 1)
102 		ereport(ERROR,
103 				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
104 				 errmsg("invalid type modifier")));
105 
106 	if (*tl < 1)
107 		ereport(ERROR,
108 				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
109 				 errmsg("length for type %s must be at least 1",
110 						typename)));
111 	if (*tl > (MaxAttrSize * BITS_PER_BYTE))
112 		ereport(ERROR,
113 				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
114 				 errmsg("length for type %s cannot exceed %d",
115 						typename, MaxAttrSize * BITS_PER_BYTE)));
116 
117 	typmod = *tl;
118 
119 	return typmod;
120 }
121 
122 /*
123  * common code for bittypmodout and varbittypmodout
124  */
125 static char *
anybit_typmodout(int32 typmod)126 anybit_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  */
145 Datum
bit_in(PG_FUNCTION_ARGS)146 bit_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 
278 Datum
bit_out(PG_FUNCTION_ARGS)279 bit_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  */
329 Datum
bit_recv(PG_FUNCTION_ARGS)330 bit_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  */
374 Datum
bit_send(PG_FUNCTION_ARGS)375 bit_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  */
389 Datum
bit(PG_FUNCTION_ARGS)390 bit(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 
427 Datum
bittypmodin(PG_FUNCTION_ARGS)428 bittypmodin(PG_FUNCTION_ARGS)
429 {
430 	ArrayType  *ta = PG_GETARG_ARRAYTYPE_P(0);
431 
432 	PG_RETURN_INT32(anybit_typmodin(ta, "bit"));
433 }
434 
435 Datum
bittypmodout(PG_FUNCTION_ARGS)436 bittypmodout(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  */
450 Datum
varbit_in(PG_FUNCTION_ARGS)451 varbit_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  */
585 Datum
varbit_out(PG_FUNCTION_ARGS)586 varbit_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  */
634 Datum
varbit_recv(PG_FUNCTION_ARGS)635 varbit_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  */
679 Datum
varbit_send(PG_FUNCTION_ARGS)680 varbit_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  */
700 Datum
varbit_support(PG_FUNCTION_ARGS)701 varbit_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  */
740 Datum
varbit(PG_FUNCTION_ARGS)741 varbit(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 
772 Datum
varbittypmodin(PG_FUNCTION_ARGS)773 varbittypmodin(PG_FUNCTION_ARGS)
774 {
775 	ArrayType  *ta = PG_GETARG_ARRAYTYPE_P(0);
776 
777 	PG_RETURN_INT32(anybit_typmodin(ta, "varbit"));
778 }
779 
780 Datum
varbittypmodout(PG_FUNCTION_ARGS)781 varbittypmodout(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  */
816 static int32
bit_cmp(VarBit * arg1,VarBit * arg2)817 bit_cmp(VarBit *arg1, VarBit *arg2)
818 {
819 	int			bitlen1,
820 				bytelen1,
821 				bitlen2,
822 				bytelen2;
823 	int32		cmp;
824 
825 	bytelen1 = VARBITBYTES(arg1);
826 	bytelen2 = VARBITBYTES(arg2);
827 
828 	cmp = memcmp(VARBITS(arg1), VARBITS(arg2), Min(bytelen1, bytelen2));
829 	if (cmp == 0)
830 	{
831 		bitlen1 = VARBITLEN(arg1);
832 		bitlen2 = VARBITLEN(arg2);
833 		if (bitlen1 != bitlen2)
834 			cmp = (bitlen1 < bitlen2) ? -1 : 1;
835 	}
836 	return cmp;
837 }
838 
839 Datum
biteq(PG_FUNCTION_ARGS)840 biteq(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 
863 Datum
bitne(PG_FUNCTION_ARGS)864 bitne(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 
887 Datum
bitlt(PG_FUNCTION_ARGS)888 bitlt(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 
902 Datum
bitle(PG_FUNCTION_ARGS)903 bitle(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 
917 Datum
bitgt(PG_FUNCTION_ARGS)918 bitgt(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 
932 Datum
bitge(PG_FUNCTION_ARGS)933 bitge(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 
947 Datum
bitcmp(PG_FUNCTION_ARGS)948 bitcmp(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  */
966 Datum
bitcat(PG_FUNCTION_ARGS)967 bitcat(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 
975 static VarBit *
bit_catenate(VarBit * arg1,VarBit * arg2)976 bit_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  */
1036 Datum
bitsubstr(PG_FUNCTION_ARGS)1037 bitsubstr(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 
1045 Datum
bitsubstr_no_len(PG_FUNCTION_ARGS)1046 bitsubstr_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 
1053 static VarBit *
bitsubstring(VarBit * arg,int32 s,int32 l,bool length_not_specified)1054 bitsubstring(VarBit *arg, int32 s, int32 l, bool length_not_specified)
1055 {
1056 	VarBit	   *result;
1057 	int			bitlen,
1058 				rbitlen,
1059 				len,
1060 				ishift,
1061 				i;
1062 	int32		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 if (l < 0)
1076 	{
1077 		/* SQL99 says to throw an error for E < S, i.e., negative length */
1078 		ereport(ERROR,
1079 				(errcode(ERRCODE_SUBSTRING_ERROR),
1080 				 errmsg("negative substring length not allowed")));
1081 		e1 = -1;				/* silence stupider compilers */
1082 	}
1083 	else if (pg_add_s32_overflow(s, l, &e))
1084 	{
1085 		/*
1086 		 * L could be large enough for S + L to overflow, in which case the
1087 		 * substring must run to end of string.
1088 		 */
1089 		e1 = bitlen + 1;
1090 	}
1091 	else
1092 	{
1093 		e1 = Min(e, bitlen + 1);
1094 	}
1095 	if (s1 > bitlen || e1 <= s1)
1096 	{
1097 		/* Need to return a zero-length bitstring */
1098 		len = VARBITTOTALLEN(0);
1099 		result = (VarBit *) palloc(len);
1100 		SET_VARSIZE(result, len);
1101 		VARBITLEN(result) = 0;
1102 	}
1103 	else
1104 	{
1105 		/*
1106 		 * OK, we've got a true substring starting at position s1-1 and ending
1107 		 * at position e1-1
1108 		 */
1109 		rbitlen = e1 - s1;
1110 		len = VARBITTOTALLEN(rbitlen);
1111 		result = (VarBit *) palloc(len);
1112 		SET_VARSIZE(result, len);
1113 		VARBITLEN(result) = rbitlen;
1114 		len -= VARHDRSZ + VARBITHDRSZ;
1115 		/* Are we copying from a byte boundary? */
1116 		if ((s1 - 1) % BITS_PER_BYTE == 0)
1117 		{
1118 			/* Yep, we are copying bytes */
1119 			memcpy(VARBITS(result), VARBITS(arg) + (s1 - 1) / BITS_PER_BYTE,
1120 				   len);
1121 		}
1122 		else
1123 		{
1124 			/* Figure out how much we need to shift the sequence by */
1125 			ishift = (s1 - 1) % BITS_PER_BYTE;
1126 			r = VARBITS(result);
1127 			ps = VARBITS(arg) + (s1 - 1) / BITS_PER_BYTE;
1128 			for (i = 0; i < len; i++)
1129 			{
1130 				*r = (*ps << ishift) & BITMASK;
1131 				if ((++ps) < VARBITEND(arg))
1132 					*r |= *ps >> (BITS_PER_BYTE - ishift);
1133 				r++;
1134 			}
1135 		}
1136 
1137 		/* Make sure last byte is correctly zero-padded */
1138 		VARBIT_PAD(result);
1139 	}
1140 
1141 	return result;
1142 }
1143 
1144 /*
1145  * bitoverlay
1146  *	Replace specified substring of first string with second
1147  *
1148  * The SQL standard defines OVERLAY() in terms of substring and concatenation.
1149  * This code is a direct implementation of what the standard says.
1150  */
1151 Datum
bitoverlay(PG_FUNCTION_ARGS)1152 bitoverlay(PG_FUNCTION_ARGS)
1153 {
1154 	VarBit	   *t1 = PG_GETARG_VARBIT_P(0);
1155 	VarBit	   *t2 = PG_GETARG_VARBIT_P(1);
1156 	int			sp = PG_GETARG_INT32(2);	/* substring start position */
1157 	int			sl = PG_GETARG_INT32(3);	/* substring length */
1158 
1159 	PG_RETURN_VARBIT_P(bit_overlay(t1, t2, sp, sl));
1160 }
1161 
1162 Datum
bitoverlay_no_len(PG_FUNCTION_ARGS)1163 bitoverlay_no_len(PG_FUNCTION_ARGS)
1164 {
1165 	VarBit	   *t1 = PG_GETARG_VARBIT_P(0);
1166 	VarBit	   *t2 = PG_GETARG_VARBIT_P(1);
1167 	int			sp = PG_GETARG_INT32(2);	/* substring start position */
1168 	int			sl;
1169 
1170 	sl = VARBITLEN(t2);			/* defaults to length(t2) */
1171 	PG_RETURN_VARBIT_P(bit_overlay(t1, t2, sp, sl));
1172 }
1173 
1174 static VarBit *
bit_overlay(VarBit * t1,VarBit * t2,int sp,int sl)1175 bit_overlay(VarBit *t1, VarBit *t2, int sp, int sl)
1176 {
1177 	VarBit	   *result;
1178 	VarBit	   *s1;
1179 	VarBit	   *s2;
1180 	int			sp_pl_sl;
1181 
1182 	/*
1183 	 * Check for possible integer-overflow cases.  For negative sp, throw a
1184 	 * "substring length" error because that's what should be expected
1185 	 * according to the spec's definition of OVERLAY().
1186 	 */
1187 	if (sp <= 0)
1188 		ereport(ERROR,
1189 				(errcode(ERRCODE_SUBSTRING_ERROR),
1190 				 errmsg("negative substring length not allowed")));
1191 	if (pg_add_s32_overflow(sp, sl, &sp_pl_sl))
1192 		ereport(ERROR,
1193 				(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1194 				 errmsg("integer out of range")));
1195 
1196 	s1 = bitsubstring(t1, 1, sp - 1, false);
1197 	s2 = bitsubstring(t1, sp_pl_sl, -1, true);
1198 	result = bit_catenate(s1, t2);
1199 	result = bit_catenate(result, s2);
1200 
1201 	return result;
1202 }
1203 
1204 /*
1205  * bitlength, bitoctetlength
1206  * Return the length of a bit string
1207  */
1208 Datum
bitlength(PG_FUNCTION_ARGS)1209 bitlength(PG_FUNCTION_ARGS)
1210 {
1211 	VarBit	   *arg = PG_GETARG_VARBIT_P(0);
1212 
1213 	PG_RETURN_INT32(VARBITLEN(arg));
1214 }
1215 
1216 Datum
bitoctetlength(PG_FUNCTION_ARGS)1217 bitoctetlength(PG_FUNCTION_ARGS)
1218 {
1219 	VarBit	   *arg = PG_GETARG_VARBIT_P(0);
1220 
1221 	PG_RETURN_INT32(VARBITBYTES(arg));
1222 }
1223 
1224 /*
1225  * bit_and
1226  * perform a logical AND on two bit strings.
1227  */
1228 Datum
bit_and(PG_FUNCTION_ARGS)1229 bit_and(PG_FUNCTION_ARGS)
1230 {
1231 	VarBit	   *arg1 = PG_GETARG_VARBIT_P(0);
1232 	VarBit	   *arg2 = PG_GETARG_VARBIT_P(1);
1233 	VarBit	   *result;
1234 	int			len,
1235 				bitlen1,
1236 				bitlen2,
1237 				i;
1238 	bits8	   *p1,
1239 			   *p2,
1240 			   *r;
1241 
1242 	bitlen1 = VARBITLEN(arg1);
1243 	bitlen2 = VARBITLEN(arg2);
1244 	if (bitlen1 != bitlen2)
1245 		ereport(ERROR,
1246 				(errcode(ERRCODE_STRING_DATA_LENGTH_MISMATCH),
1247 				 errmsg("cannot AND bit strings of different sizes")));
1248 
1249 	len = VARSIZE(arg1);
1250 	result = (VarBit *) palloc(len);
1251 	SET_VARSIZE(result, len);
1252 	VARBITLEN(result) = bitlen1;
1253 
1254 	p1 = VARBITS(arg1);
1255 	p2 = VARBITS(arg2);
1256 	r = VARBITS(result);
1257 	for (i = 0; i < VARBITBYTES(arg1); i++)
1258 		*r++ = *p1++ & *p2++;
1259 
1260 	/* Padding is not needed as & of 0 pads is 0 */
1261 
1262 	PG_RETURN_VARBIT_P(result);
1263 }
1264 
1265 /*
1266  * bit_or
1267  * perform a logical OR on two bit strings.
1268  */
1269 Datum
bit_or(PG_FUNCTION_ARGS)1270 bit_or(PG_FUNCTION_ARGS)
1271 {
1272 	VarBit	   *arg1 = PG_GETARG_VARBIT_P(0);
1273 	VarBit	   *arg2 = PG_GETARG_VARBIT_P(1);
1274 	VarBit	   *result;
1275 	int			len,
1276 				bitlen1,
1277 				bitlen2,
1278 				i;
1279 	bits8	   *p1,
1280 			   *p2,
1281 			   *r;
1282 
1283 	bitlen1 = VARBITLEN(arg1);
1284 	bitlen2 = VARBITLEN(arg2);
1285 	if (bitlen1 != bitlen2)
1286 		ereport(ERROR,
1287 				(errcode(ERRCODE_STRING_DATA_LENGTH_MISMATCH),
1288 				 errmsg("cannot OR bit strings of different sizes")));
1289 	len = VARSIZE(arg1);
1290 	result = (VarBit *) palloc(len);
1291 	SET_VARSIZE(result, len);
1292 	VARBITLEN(result) = bitlen1;
1293 
1294 	p1 = VARBITS(arg1);
1295 	p2 = VARBITS(arg2);
1296 	r = VARBITS(result);
1297 	for (i = 0; i < VARBITBYTES(arg1); i++)
1298 		*r++ = *p1++ | *p2++;
1299 
1300 	/* Padding is not needed as | of 0 pads is 0 */
1301 
1302 	PG_RETURN_VARBIT_P(result);
1303 }
1304 
1305 /*
1306  * bitxor
1307  * perform a logical XOR on two bit strings.
1308  */
1309 Datum
bitxor(PG_FUNCTION_ARGS)1310 bitxor(PG_FUNCTION_ARGS)
1311 {
1312 	VarBit	   *arg1 = PG_GETARG_VARBIT_P(0);
1313 	VarBit	   *arg2 = PG_GETARG_VARBIT_P(1);
1314 	VarBit	   *result;
1315 	int			len,
1316 				bitlen1,
1317 				bitlen2,
1318 				i;
1319 	bits8	   *p1,
1320 			   *p2,
1321 			   *r;
1322 
1323 	bitlen1 = VARBITLEN(arg1);
1324 	bitlen2 = VARBITLEN(arg2);
1325 	if (bitlen1 != bitlen2)
1326 		ereport(ERROR,
1327 				(errcode(ERRCODE_STRING_DATA_LENGTH_MISMATCH),
1328 				 errmsg("cannot XOR bit strings of different sizes")));
1329 
1330 	len = VARSIZE(arg1);
1331 	result = (VarBit *) palloc(len);
1332 	SET_VARSIZE(result, len);
1333 	VARBITLEN(result) = bitlen1;
1334 
1335 	p1 = VARBITS(arg1);
1336 	p2 = VARBITS(arg2);
1337 	r = VARBITS(result);
1338 	for (i = 0; i < VARBITBYTES(arg1); i++)
1339 		*r++ = *p1++ ^ *p2++;
1340 
1341 	/* Padding is not needed as ^ of 0 pads is 0 */
1342 
1343 	PG_RETURN_VARBIT_P(result);
1344 }
1345 
1346 /*
1347  * bitnot
1348  * perform a logical NOT on a bit string.
1349  */
1350 Datum
bitnot(PG_FUNCTION_ARGS)1351 bitnot(PG_FUNCTION_ARGS)
1352 {
1353 	VarBit	   *arg = PG_GETARG_VARBIT_P(0);
1354 	VarBit	   *result;
1355 	bits8	   *p,
1356 			   *r;
1357 
1358 	result = (VarBit *) palloc(VARSIZE(arg));
1359 	SET_VARSIZE(result, VARSIZE(arg));
1360 	VARBITLEN(result) = VARBITLEN(arg);
1361 
1362 	p = VARBITS(arg);
1363 	r = VARBITS(result);
1364 	for (; p < VARBITEND(arg); p++)
1365 		*r++ = ~*p;
1366 
1367 	/* Must zero-pad the result, because extra bits are surely 1's here */
1368 	VARBIT_PAD_LAST(result, r);
1369 
1370 	PG_RETURN_VARBIT_P(result);
1371 }
1372 
1373 /*
1374  * bitshiftleft
1375  * do a left shift (i.e. towards the beginning of the string)
1376  */
1377 Datum
bitshiftleft(PG_FUNCTION_ARGS)1378 bitshiftleft(PG_FUNCTION_ARGS)
1379 {
1380 	VarBit	   *arg = PG_GETARG_VARBIT_P(0);
1381 	int32		shft = PG_GETARG_INT32(1);
1382 	VarBit	   *result;
1383 	int			byte_shift,
1384 				ishift,
1385 				len;
1386 	bits8	   *p,
1387 			   *r;
1388 
1389 	/* Negative shift is a shift to the right */
1390 	if (shft < 0)
1391 	{
1392 		/* Prevent integer overflow in negation */
1393 		if (shft < -VARBITMAXLEN)
1394 			shft = -VARBITMAXLEN;
1395 		PG_RETURN_DATUM(DirectFunctionCall2(bitshiftright,
1396 											VarBitPGetDatum(arg),
1397 											Int32GetDatum(-shft)));
1398 	}
1399 
1400 	result = (VarBit *) palloc(VARSIZE(arg));
1401 	SET_VARSIZE(result, VARSIZE(arg));
1402 	VARBITLEN(result) = VARBITLEN(arg);
1403 	r = VARBITS(result);
1404 
1405 	/* If we shifted all the bits out, return an all-zero string */
1406 	if (shft >= VARBITLEN(arg))
1407 	{
1408 		MemSet(r, 0, VARBITBYTES(arg));
1409 		PG_RETURN_VARBIT_P(result);
1410 	}
1411 
1412 	byte_shift = shft / BITS_PER_BYTE;
1413 	ishift = shft % BITS_PER_BYTE;
1414 	p = VARBITS(arg) + byte_shift;
1415 
1416 	if (ishift == 0)
1417 	{
1418 		/* Special case: we can do a memcpy */
1419 		len = VARBITBYTES(arg) - byte_shift;
1420 		memcpy(r, p, len);
1421 		MemSet(r + len, 0, byte_shift);
1422 	}
1423 	else
1424 	{
1425 		for (; p < VARBITEND(arg); r++)
1426 		{
1427 			*r = *p << ishift;
1428 			if ((++p) < VARBITEND(arg))
1429 				*r |= *p >> (BITS_PER_BYTE - ishift);
1430 		}
1431 		for (; r < VARBITEND(result); r++)
1432 			*r = 0;
1433 	}
1434 
1435 	/* The pad bits should be already zero at this point */
1436 
1437 	PG_RETURN_VARBIT_P(result);
1438 }
1439 
1440 /*
1441  * bitshiftright
1442  * do a right shift (i.e. towards the end of the string)
1443  */
1444 Datum
bitshiftright(PG_FUNCTION_ARGS)1445 bitshiftright(PG_FUNCTION_ARGS)
1446 {
1447 	VarBit	   *arg = PG_GETARG_VARBIT_P(0);
1448 	int32		shft = PG_GETARG_INT32(1);
1449 	VarBit	   *result;
1450 	int			byte_shift,
1451 				ishift,
1452 				len;
1453 	bits8	   *p,
1454 			   *r;
1455 
1456 	/* Negative shift is a shift to the left */
1457 	if (shft < 0)
1458 	{
1459 		/* Prevent integer overflow in negation */
1460 		if (shft < -VARBITMAXLEN)
1461 			shft = -VARBITMAXLEN;
1462 		PG_RETURN_DATUM(DirectFunctionCall2(bitshiftleft,
1463 											VarBitPGetDatum(arg),
1464 											Int32GetDatum(-shft)));
1465 	}
1466 
1467 	result = (VarBit *) palloc(VARSIZE(arg));
1468 	SET_VARSIZE(result, VARSIZE(arg));
1469 	VARBITLEN(result) = VARBITLEN(arg);
1470 	r = VARBITS(result);
1471 
1472 	/* If we shifted all the bits out, return an all-zero string */
1473 	if (shft >= VARBITLEN(arg))
1474 	{
1475 		MemSet(r, 0, VARBITBYTES(arg));
1476 		PG_RETURN_VARBIT_P(result);
1477 	}
1478 
1479 	byte_shift = shft / BITS_PER_BYTE;
1480 	ishift = shft % BITS_PER_BYTE;
1481 	p = VARBITS(arg);
1482 
1483 	/* Set the first part of the result to 0 */
1484 	MemSet(r, 0, byte_shift);
1485 	r += byte_shift;
1486 
1487 	if (ishift == 0)
1488 	{
1489 		/* Special case: we can do a memcpy */
1490 		len = VARBITBYTES(arg) - byte_shift;
1491 		memcpy(r, p, len);
1492 		r += len;
1493 	}
1494 	else
1495 	{
1496 		if (r < VARBITEND(result))
1497 			*r = 0;				/* initialize first byte */
1498 		for (; r < VARBITEND(result); p++)
1499 		{
1500 			*r |= *p >> ishift;
1501 			if ((++r) < VARBITEND(result))
1502 				*r = (*p << (BITS_PER_BYTE - ishift)) & BITMASK;
1503 		}
1504 	}
1505 
1506 	/* We may have shifted 1's into the pad bits, so fix that */
1507 	VARBIT_PAD_LAST(result, r);
1508 
1509 	PG_RETURN_VARBIT_P(result);
1510 }
1511 
1512 /*
1513  * This is not defined in any standard. We retain the natural ordering of
1514  * bits here, as it just seems more intuitive.
1515  */
1516 Datum
bitfromint4(PG_FUNCTION_ARGS)1517 bitfromint4(PG_FUNCTION_ARGS)
1518 {
1519 	int32		a = PG_GETARG_INT32(0);
1520 	int32		typmod = PG_GETARG_INT32(1);
1521 	VarBit	   *result;
1522 	bits8	   *r;
1523 	int			rlen;
1524 	int			destbitsleft,
1525 				srcbitsleft;
1526 
1527 	if (typmod <= 0 || typmod > VARBITMAXLEN)
1528 		typmod = 1;				/* default bit length */
1529 
1530 	rlen = VARBITTOTALLEN(typmod);
1531 	result = (VarBit *) palloc(rlen);
1532 	SET_VARSIZE(result, rlen);
1533 	VARBITLEN(result) = typmod;
1534 
1535 	r = VARBITS(result);
1536 	destbitsleft = typmod;
1537 	srcbitsleft = 32;
1538 	/* drop any input bits that don't fit */
1539 	srcbitsleft = Min(srcbitsleft, destbitsleft);
1540 	/* sign-fill any excess bytes in output */
1541 	while (destbitsleft >= srcbitsleft + 8)
1542 	{
1543 		*r++ = (bits8) ((a < 0) ? BITMASK : 0);
1544 		destbitsleft -= 8;
1545 	}
1546 	/* store first fractional byte */
1547 	if (destbitsleft > srcbitsleft)
1548 	{
1549 		unsigned int val = (unsigned int) (a >> (destbitsleft - 8));
1550 
1551 		/* Force sign-fill in case the compiler implements >> as zero-fill */
1552 		if (a < 0)
1553 			val |= ((unsigned int) -1) << (srcbitsleft + 8 - destbitsleft);
1554 		*r++ = (bits8) (val & BITMASK);
1555 		destbitsleft -= 8;
1556 	}
1557 	/* Now srcbitsleft and destbitsleft are the same, need not track both */
1558 	/* store whole bytes */
1559 	while (destbitsleft >= 8)
1560 	{
1561 		*r++ = (bits8) ((a >> (destbitsleft - 8)) & BITMASK);
1562 		destbitsleft -= 8;
1563 	}
1564 	/* store last fractional byte */
1565 	if (destbitsleft > 0)
1566 		*r = (bits8) ((a << (8 - destbitsleft)) & BITMASK);
1567 
1568 	PG_RETURN_VARBIT_P(result);
1569 }
1570 
1571 Datum
bittoint4(PG_FUNCTION_ARGS)1572 bittoint4(PG_FUNCTION_ARGS)
1573 {
1574 	VarBit	   *arg = PG_GETARG_VARBIT_P(0);
1575 	uint32		result;
1576 	bits8	   *r;
1577 
1578 	/* Check that the bit string is not too long */
1579 	if (VARBITLEN(arg) > sizeof(result) * BITS_PER_BYTE)
1580 		ereport(ERROR,
1581 				(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1582 				 errmsg("integer out of range")));
1583 
1584 	result = 0;
1585 	for (r = VARBITS(arg); r < VARBITEND(arg); r++)
1586 	{
1587 		result <<= BITS_PER_BYTE;
1588 		result |= *r;
1589 	}
1590 	/* Now shift the result to take account of the padding at the end */
1591 	result >>= VARBITPAD(arg);
1592 
1593 	PG_RETURN_INT32(result);
1594 }
1595 
1596 Datum
bitfromint8(PG_FUNCTION_ARGS)1597 bitfromint8(PG_FUNCTION_ARGS)
1598 {
1599 	int64		a = PG_GETARG_INT64(0);
1600 	int32		typmod = PG_GETARG_INT32(1);
1601 	VarBit	   *result;
1602 	bits8	   *r;
1603 	int			rlen;
1604 	int			destbitsleft,
1605 				srcbitsleft;
1606 
1607 	if (typmod <= 0 || typmod > VARBITMAXLEN)
1608 		typmod = 1;				/* default bit length */
1609 
1610 	rlen = VARBITTOTALLEN(typmod);
1611 	result = (VarBit *) palloc(rlen);
1612 	SET_VARSIZE(result, rlen);
1613 	VARBITLEN(result) = typmod;
1614 
1615 	r = VARBITS(result);
1616 	destbitsleft = typmod;
1617 	srcbitsleft = 64;
1618 	/* drop any input bits that don't fit */
1619 	srcbitsleft = Min(srcbitsleft, destbitsleft);
1620 	/* sign-fill any excess bytes in output */
1621 	while (destbitsleft >= srcbitsleft + 8)
1622 	{
1623 		*r++ = (bits8) ((a < 0) ? BITMASK : 0);
1624 		destbitsleft -= 8;
1625 	}
1626 	/* store first fractional byte */
1627 	if (destbitsleft > srcbitsleft)
1628 	{
1629 		unsigned int val = (unsigned int) (a >> (destbitsleft - 8));
1630 
1631 		/* Force sign-fill in case the compiler implements >> as zero-fill */
1632 		if (a < 0)
1633 			val |= ((unsigned int) -1) << (srcbitsleft + 8 - destbitsleft);
1634 		*r++ = (bits8) (val & BITMASK);
1635 		destbitsleft -= 8;
1636 	}
1637 	/* Now srcbitsleft and destbitsleft are the same, need not track both */
1638 	/* store whole bytes */
1639 	while (destbitsleft >= 8)
1640 	{
1641 		*r++ = (bits8) ((a >> (destbitsleft - 8)) & BITMASK);
1642 		destbitsleft -= 8;
1643 	}
1644 	/* store last fractional byte */
1645 	if (destbitsleft > 0)
1646 		*r = (bits8) ((a << (8 - destbitsleft)) & BITMASK);
1647 
1648 	PG_RETURN_VARBIT_P(result);
1649 }
1650 
1651 Datum
bittoint8(PG_FUNCTION_ARGS)1652 bittoint8(PG_FUNCTION_ARGS)
1653 {
1654 	VarBit	   *arg = PG_GETARG_VARBIT_P(0);
1655 	uint64		result;
1656 	bits8	   *r;
1657 
1658 	/* Check that the bit string is not too long */
1659 	if (VARBITLEN(arg) > sizeof(result) * BITS_PER_BYTE)
1660 		ereport(ERROR,
1661 				(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1662 				 errmsg("bigint out of range")));
1663 
1664 	result = 0;
1665 	for (r = VARBITS(arg); r < VARBITEND(arg); r++)
1666 	{
1667 		result <<= BITS_PER_BYTE;
1668 		result |= *r;
1669 	}
1670 	/* Now shift the result to take account of the padding at the end */
1671 	result >>= VARBITPAD(arg);
1672 
1673 	PG_RETURN_INT64(result);
1674 }
1675 
1676 
1677 /*
1678  * Determines the position of S2 in the bitstring S1 (1-based string).
1679  * If S2 does not appear in S1 this function returns 0.
1680  * If S2 is of length 0 this function returns 1.
1681  * Compatible in usage with POSITION() functions for other data types.
1682  */
1683 Datum
bitposition(PG_FUNCTION_ARGS)1684 bitposition(PG_FUNCTION_ARGS)
1685 {
1686 	VarBit	   *str = PG_GETARG_VARBIT_P(0);
1687 	VarBit	   *substr = PG_GETARG_VARBIT_P(1);
1688 	int			substr_length,
1689 				str_length,
1690 				i,
1691 				is;
1692 	bits8	   *s,				/* pointer into substring */
1693 			   *p;				/* pointer into str */
1694 	bits8		cmp,			/* shifted substring byte to compare */
1695 				mask1,			/* mask for substring byte shifted right */
1696 				mask2,			/* mask for substring byte shifted left */
1697 				end_mask,		/* pad mask for last substring byte */
1698 				str_mask;		/* pad mask for last string byte */
1699 	bool		is_match;
1700 
1701 	/* Get the substring length */
1702 	substr_length = VARBITLEN(substr);
1703 	str_length = VARBITLEN(str);
1704 
1705 	/* String has zero length or substring longer than string, return 0 */
1706 	if ((str_length == 0) || (substr_length > str_length))
1707 		PG_RETURN_INT32(0);
1708 
1709 	/* zero-length substring means return 1 */
1710 	if (substr_length == 0)
1711 		PG_RETURN_INT32(1);
1712 
1713 	/* Initialise the padding masks */
1714 	end_mask = BITMASK << VARBITPAD(substr);
1715 	str_mask = BITMASK << VARBITPAD(str);
1716 	for (i = 0; i < VARBITBYTES(str) - VARBITBYTES(substr) + 1; i++)
1717 	{
1718 		for (is = 0; is < BITS_PER_BYTE; is++)
1719 		{
1720 			is_match = true;
1721 			p = VARBITS(str) + i;
1722 			mask1 = BITMASK >> is;
1723 			mask2 = ~mask1;
1724 			for (s = VARBITS(substr);
1725 				 is_match && s < VARBITEND(substr); s++)
1726 			{
1727 				cmp = *s >> is;
1728 				if (s == VARBITEND(substr) - 1)
1729 				{
1730 					mask1 &= end_mask >> is;
1731 					if (p == VARBITEND(str) - 1)
1732 					{
1733 						/* Check that there is enough of str left */
1734 						if (mask1 & ~str_mask)
1735 						{
1736 							is_match = false;
1737 							break;
1738 						}
1739 						mask1 &= str_mask;
1740 					}
1741 				}
1742 				is_match = ((cmp ^ *p) & mask1) == 0;
1743 				if (!is_match)
1744 					break;
1745 				/* Move on to the next byte */
1746 				p++;
1747 				if (p == VARBITEND(str))
1748 				{
1749 					mask2 = end_mask << (BITS_PER_BYTE - is);
1750 					is_match = mask2 == 0;
1751 #if 0
1752 					elog(DEBUG4, "S. %d %d em=%2x sm=%2x r=%d",
1753 						 i, is, end_mask, mask2, is_match);
1754 #endif
1755 					break;
1756 				}
1757 				cmp = *s << (BITS_PER_BYTE - is);
1758 				if (s == VARBITEND(substr) - 1)
1759 				{
1760 					mask2 &= end_mask << (BITS_PER_BYTE - is);
1761 					if (p == VARBITEND(str) - 1)
1762 					{
1763 						if (mask2 & ~str_mask)
1764 						{
1765 							is_match = false;
1766 							break;
1767 						}
1768 						mask2 &= str_mask;
1769 					}
1770 				}
1771 				is_match = ((cmp ^ *p) & mask2) == 0;
1772 			}
1773 			/* Have we found a match? */
1774 			if (is_match)
1775 				PG_RETURN_INT32(i * BITS_PER_BYTE + is + 1);
1776 		}
1777 	}
1778 	PG_RETURN_INT32(0);
1779 }
1780 
1781 
1782 /*
1783  * bitsetbit
1784  *
1785  * Given an instance of type 'bit' creates a new one with
1786  * the Nth bit set to the given value.
1787  *
1788  * The bit location is specified left-to-right in a zero-based fashion
1789  * consistent with the other get_bit and set_bit functions, but
1790  * inconsistent with the standard substring, position, overlay functions
1791  */
1792 Datum
bitsetbit(PG_FUNCTION_ARGS)1793 bitsetbit(PG_FUNCTION_ARGS)
1794 {
1795 	VarBit	   *arg1 = PG_GETARG_VARBIT_P(0);
1796 	int32		n = PG_GETARG_INT32(1);
1797 	int32		newBit = PG_GETARG_INT32(2);
1798 	VarBit	   *result;
1799 	int			len,
1800 				bitlen;
1801 	bits8	   *r,
1802 			   *p;
1803 	int			byteNo,
1804 				bitNo;
1805 
1806 	bitlen = VARBITLEN(arg1);
1807 	if (n < 0 || n >= bitlen)
1808 		ereport(ERROR,
1809 				(errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
1810 				 errmsg("bit index %d out of valid range (0..%d)",
1811 						n, bitlen - 1)));
1812 
1813 	/*
1814 	 * sanity check!
1815 	 */
1816 	if (newBit != 0 && newBit != 1)
1817 		ereport(ERROR,
1818 				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1819 				 errmsg("new bit must be 0 or 1")));
1820 
1821 	len = VARSIZE(arg1);
1822 	result = (VarBit *) palloc(len);
1823 	SET_VARSIZE(result, len);
1824 	VARBITLEN(result) = bitlen;
1825 
1826 	p = VARBITS(arg1);
1827 	r = VARBITS(result);
1828 
1829 	memcpy(r, p, VARBITBYTES(arg1));
1830 
1831 	byteNo = n / BITS_PER_BYTE;
1832 	bitNo = BITS_PER_BYTE - 1 - (n % BITS_PER_BYTE);
1833 
1834 	/*
1835 	 * Update the byte.
1836 	 */
1837 	if (newBit == 0)
1838 		r[byteNo] &= (~(1 << bitNo));
1839 	else
1840 		r[byteNo] |= (1 << bitNo);
1841 
1842 	PG_RETURN_VARBIT_P(result);
1843 }
1844 
1845 /*
1846  * bitgetbit
1847  *
1848  * returns the value of the Nth bit of a bit array (0 or 1).
1849  *
1850  * The bit location is specified left-to-right in a zero-based fashion
1851  * consistent with the other get_bit and set_bit functions, but
1852  * inconsistent with the standard substring, position, overlay functions
1853  */
1854 Datum
bitgetbit(PG_FUNCTION_ARGS)1855 bitgetbit(PG_FUNCTION_ARGS)
1856 {
1857 	VarBit	   *arg1 = PG_GETARG_VARBIT_P(0);
1858 	int32		n = PG_GETARG_INT32(1);
1859 	int			bitlen;
1860 	bits8	   *p;
1861 	int			byteNo,
1862 				bitNo;
1863 
1864 	bitlen = VARBITLEN(arg1);
1865 	if (n < 0 || n >= bitlen)
1866 		ereport(ERROR,
1867 				(errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
1868 				 errmsg("bit index %d out of valid range (0..%d)",
1869 						n, bitlen - 1)));
1870 
1871 	p = VARBITS(arg1);
1872 
1873 	byteNo = n / BITS_PER_BYTE;
1874 	bitNo = BITS_PER_BYTE - 1 - (n % BITS_PER_BYTE);
1875 
1876 	if (p[byteNo] & (1 << bitNo))
1877 		PG_RETURN_INT32(1);
1878 	else
1879 		PG_RETURN_INT32(0);
1880 }
1881