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