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