aboutsummaryrefslogtreecommitdiff
path: root/src/backend/utils/adt/varbit.c
blob: cd2839c6929f285cca434095bb0264be7c00a804 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
/*-------------------------------------------------------------------------
 *
 * varbit.c
 *	  Functions for the built-in type bit() and varying bit().
 *
 * IDENTIFICATION
 *	  $Header: /cvsroot/pgsql/src/backend/utils/adt/varbit.c,v 1.2 2000/04/12 17:15:52 momjian Exp $
 *
 *-------------------------------------------------------------------------
 */

/* Include file list stolen from float.c.
 * Can probably get rid of some of these.
 * - thomas 2000-04-07
 */
#include <ctype.h>
#include <errno.h>

#include <float.h>				/* faked on sunos4 */

#include <math.h>

#include "postgres.h"
#ifdef HAVE_LIMITS_H
#include <limits.h>
#ifndef MAXINT
#define MAXINT		  INT_MAX
#endif
#else
#ifdef HAVE_VALUES_H
#include <values.h>
#endif
#endif
#include "fmgr.h"
#include "utils/builtins.h"
#include "access/htup.h"

/*
   Prefixes:
	 zp    -- zero-padded fixed length bit string
	 var   -- varying bit string

   attypmod -- contains the length of the bit string in bits, or for
			   varying bits the maximum length.

   The data structure contains the following elements:
	  header  -- length of the whole data structure (incl header)
				 in bytes. (as with all varying length datatypes)
	  data section -- private data section for the bits data structures
		 bitlength -- lenght of the bit string in bits
	 bitdata   -- least significant byte first string
*/

char *
varbit_out(bits8 *s)
{
	return zpbits_out(s);
}

/*
 * zpbit_in -
 *	  converts a string to the internal representation of a bitstring.
 *		  The length is determined by the number of bits required plus
 *		  VARHDRSZ bytes or from atttypmod.
 *	  (XXX dummy is here because we pass typelem as the second argument
 *		  for array_in. copied this, no idea what it means??)
 */
bits8 *
zpbit_in(char *s, int dummy, int32 atttypmod)
{
	bits8	   *result;			/* the bits string that was read in   */
	char	   *sp;				/* pointer into the character string  */
	bits8	   *r;
	int			len,			/* Length of the whole data structure */
				bitlen,			/* Number of bits in the bit string   */
				slen;			/* Length of the input string		  */
	int			bit_not_hex = 0;/* 0 = hex string  1=bit string		  */
	int			bc,
				ipad;
	bits8		x = 0;


	if (s == NULL)
		return (bits8 *) NULL;

	/* Check that the first character is a b or an x */
	if (s[0] == 'b' || s[0] == 'B')
		bit_not_hex = 1;
	else if (s[0] == 'x' || s[0] == 'X')
		bit_not_hex = 0;
	else
		elog(ERROR, "zpbit_in: %s is not a valid bitstring", s);

	slen = strlen(s) - 1;
	/* Determine bitlength from input string */
	bitlen = slen;
	if (!bit_not_hex)
		bitlen *= 4;

	/*
	 * Sometimes atttypmod is not supplied. If it is supplied we need to
	 * make sure that the bitstring fits. Note that the number of infered
	 * bits can be larger than the number of actual bits needed, but only
	 * if we are reading a hex string and not by more than 3 bits, as a
	 * hex string gives and accurate length upto 4 bits
	 */
	if (atttypmod == -1)
		atttypmod = bitlen;
	else if ((bitlen > atttypmod && bit_not_hex) ||
			 (bitlen > atttypmod + 3 && !bit_not_hex))
		elog(ERROR, "zpbit_in: bit string of size %d cannot be written into bits(%d)",
			 bitlen, atttypmod);


	len = VARBITDATALEN(atttypmod);

	if (len > MaxAttrSize)
		elog(ERROR, "zpbit_in: length of bit() must be less than %ld",
			 (MaxAttrSize - VARHDRSZ - VARBITHDRSZ) * BITSPERBYTE);

	result = (bits8 *) palloc(len);
	/* set to 0 so that *r is always initialised and strin is zero-padded */
	memset(result, 0, len);
	VARSIZE(result) = len;
	VARBITLEN(result) = atttypmod;

	/*
	 * We need to read the bitstring from the end, as we store it least
	 * significant byte first. s points to the byte before the beginning
	 * of the bitstring
	 */
	sp = s + 1;
	r = VARBITS(result);
	if (bit_not_hex)
	{
		/* Parse the bit representation of the string */
		/* We know it fits, as bitlen was compared to atttypmod */
		x = BITHIGH;
		for (bc = 0; sp != s + slen + 1; sp++, bc++)
		{
			if (*sp == '1')
				*r |= x;
			if (bc == 7)
			{
				bc = 0;
				x = BITHIGH;
				r++;
			}
			else
				x >>= 1;
		}
	}
	else
	{
		/* Parse the hex representation of the string */
		for (bc = 0; sp != s + slen + 1; sp++)
		{
			if (*sp >= '0' && *sp <= '9')
				x = (bits8) (*sp - '0');
			else if (*sp >= 'A' && *sp <= 'F')
				x = (bits8) (*sp - 'A') + 10;
			else if (*sp >= 'a' && *sp <= 'f')
				x = (bits8) (*sp - 'a') + 10;
			else
				elog(ERROR, "Cannot parse %c as a hex digit", *sp);
			if (bc)
			{
				bc = 0;
				*r++ |= x;
			}
			else
			{
				bc++;
				*r = x << 4;
			}
		}
	}

	if (bitlen > atttypmod)
	{
		/* Check that this fitted */
		r = (bits8 *) (result + len - 1);
		ipad = VARBITPAD(result);

		/*
		 * The bottom ipad bits of the byte pointed to by r need to be
		 * zero
		 */
		if (((*r << (BITSPERBYTE - ipad)) & BITMASK) > 0)
			elog(ERROR, "zpbit_in: bit string too large for bit(%d) data type",
				 atttypmod);
	}

	return result;
}

/* zpbit_out -
 *	  for the time being we print everything as hex strings, as this is likely
 *	  to be more compact than bit strings, and consequently much more efficient
 *	  for long strings
 */
char *
zpbit_out(bits8 *s)
{
	char	   *result,
			   *r;
	bits8	   *sp;
	int			i,
				len,
				bitlen;

	if (s == NULL)
	{
		result = (char *) palloc(2);
		result[0] = '-';
		result[1] = '\0';
	}
	else
	{
		bitlen = VARBITLEN(s);
		len = bitlen / 4 + (bitlen % 4 > 0 ? 1 : 0);
		result = (char *) palloc(len + 4);
		sp = VARBITS(s);
		r = result;
		*r++ = 'X';
		*r++ = '\'';
		/* we cheat by knowing that we store full bytes zero padded */
		for (i = 0; i < len; i += 2, sp++)
		{
			*r++ = HEXDIG((*sp) >> 4);
			*r++ = HEXDIG((*sp) & 0xF);
		}

		/*
		 * Go back one step if we printed a hex number that was not part
		 * of the bitstring anymore
		 */
		if (i == len + 1)
			r--;
		*r++ = '\'';
		*r = '\0';
	}
	return result;
}

/* zpbits_out -
 *	  Prints the string a bits
 */
char *
zpbits_out(bits8 *s)
{
	char	   *result,
			   *r;
	bits8	   *sp;
	bits8		x;
	int			i,
				k,
				len;

	if (s == NULL)
	{
		result = (char *) palloc(2);
		result[0] = '-';
		result[1] = '\0';
	}
	else
	{
		len = VARBITLEN(s);
		result = (char *) palloc(len + 4);
		sp = VARBITS(s);
		r = result;
		*r++ = 'B';
		*r++ = '\'';
		for (i = 0; i < len - BITSPERBYTE; i += BITSPERBYTE, sp++)
		{
			x = *sp;
			for (k = 0; k < BITSPERBYTE; k++)
			{
				*r++ = (x & BITHIGH) ? '1' : '0';
				x <<= 1;
			}
		}
		x = *sp;
		for (k = i; k < len; k++)
		{
			*r++ = (x & BITHIGH) ? '1' : '0';
			x <<= 1;
		}
		*r++ = '\'';
		*r = '\0';
	}
	return result;
}


/*
 * varbit_in -
 *	  converts a string to the internal representation of a bitstring.
*/
bits8 *
varbit_in(char *s, int dummy, int32 atttypmod)
{
	bits8	   *result;			/* The resulting bit string			  */
	char	   *sp;				/* pointer into the character string  */
	bits8	   *r;
	int			len,			/* Length of the whole data structure */
				bitlen,			/* Number of bits in the bit string   */
				slen;			/* Length of the input string		  */
	int			bit_not_hex = 0;
	int			bc,
				ipad;
	bits8		x = 0;


	if (s == NULL)
		return (bits8 *) NULL;

	/* Check that the first character is a b or an x */
	if (s[0] == 'b' || s[0] == 'B')
		bit_not_hex = 1;
	else if (s[0] == 'x' || s[0] == 'X')
		bit_not_hex = 0;
	else
		elog(ERROR, "zpbit_in: %s is not a valid bitstring", s);

	slen = strlen(s) - 1;
	/* Determine bitlength from input string */
	bitlen = slen;
	if (!bit_not_hex)
		bitlen *= 4;

	/*
	 * Sometimes atttypmod is not supplied. If it is supplied we need to
	 * make sure that the bitstring fits. Note that the number of infered
	 * bits can be larger than the number of actual bits needed, but only
	 * if we are reading a hex string and not by more than 3 bits, as a
	 * hex string gives and accurate length upto 4 bits
	 */
	if (atttypmod > -1)
		if ((bitlen > atttypmod && bit_not_hex) ||
			(bitlen > atttypmod + 3 && !bit_not_hex))
			elog(ERROR, "varbit_in: bit string of size %d cannot be written into varying bits(%d)",
				 bitlen, atttypmod);


	len = VARBITDATALEN(bitlen);

	if (len > MaxAttrSize)
		elog(ERROR, "varbit_in: length of bit() must be less than %ld",
			 (MaxAttrSize - VARHDRSZ - VARBITHDRSZ) * BITSPERBYTE);

	result = (bits8 *) palloc(len);
	/* set to 0 so that *r is always initialised and strin is zero-padded */
	memset(result, 0, len);
	VARSIZE(result) = len;
	VARBITLEN(result) = bitlen;

	/*
	 * We need to read the bitstring from the end, as we store it least
	 * significant byte first. s points to the byte before the beginning
	 * of the bitstring
	 */
	sp = s + 1;
	r = VARBITS(result);
	if (bit_not_hex)
	{
		/* Parse the bit representation of the string */
		x = BITHIGH;
		for (bc = 0; sp != s + slen + 1; sp++, bc++)
		{
			if (*sp == '1')
				*r |= x;
			if (bc == 7)
			{
				bc = 0;
				x = BITHIGH;
				r++;
			}
			else
				x >>= 1;
		}
	}
	else
	{
		for (bc = 0; sp != s + slen + 1; sp++)
		{
			if (*sp >= '0' && *sp <= '9')
				x = (bits8) (*sp - '0');
			else if (*sp >= 'A' && *sp <= 'F')
				x = (bits8) (*sp - 'A') + 10;
			else if (*sp >= 'a' && *sp <= 'f')
				x = (bits8) (*sp - 'a') + 10;
			else
				elog(ERROR, "Cannot parse %c as a hex digit", *sp);
			if (bc)
			{
				bc = 0;
				*r++ |= x;
			}
			else
			{
				bc++;
				*r = x << 4;
			}
		}
	}

	if (bitlen > atttypmod)
	{
		/* Check that this fitted */
		r = (bits8 *) (result + len - 1);
		ipad = VARBITPAD(result);

		/*
		 * The bottom ipad bits of the byte pointed to by r need to be
		 * zero
		 */
		if (((*r << (BITSPERBYTE - ipad)) & BITMASK) > 0)
			elog(ERROR, "varbit_in: bit string too large for varying bit(%d) data type",
				 atttypmod);
	}

	return result;
}

/*
  the zpbit_out routines are fine for varying bits as well
*/


/*
 * Comparison operators
 *
 * We only need one set of comparison operators for bitstrings, as the lengths
 * are stored in the same way for zero-padded and varying bit strings.
 *
 * Note that the standard is not unambiguous about the comparison between
 * zero-padded bit strings and varying bitstrings. If the same value is written
 * into a zero padded bitstring as into a varying bitstring, but the zero
 * padded bitstring has greater length, it will be bigger.
 *
 * Zeros from the beginning of a bitstring cannot simply be ignored, as they
 * may be part of a bit string and may be significant.
 */

bool
biteq(bits8 *arg1, bits8 *arg2)
{
	int			bitlen1,
				bitlen2;

	if (!PointerIsValid(arg1) || !PointerIsValid(arg2))
		return (bool) 0;
	bitlen1 = VARBITLEN(arg1);
	bitlen2 = VARBITLEN(arg2);
	if (bitlen1 != bitlen2)
		return (bool) 0;

	/* bit strings are always stored in a full number of bytes */
	return memcmp((void *) VARBITS(arg1), (void *) VARBITS(arg2),
				  VARBITBYTES(arg1)) == 0;
}

bool
bitne(bits8 *arg1, bits8 *arg2)
{
	int			bitlen1,
				bitlen2;

	if (!PointerIsValid(arg1) || !PointerIsValid(arg2))
		return (bool) 0;
	bitlen1 = VARBITLEN(arg1);
	bitlen2 = VARBITLEN(arg2);
	if (bitlen1 != bitlen2)
		return (bool) 1;

	/* bit strings are always stored in a full number of bytes */
	return memcmp((void *) VARBITS(arg1), (void *) VARBITS(arg2),
				  VARBITBYTES(arg1)) != 0;
}

/* bitcmp
 *
 * Compares two bitstrings and returns -1, 0, 1 depending on whether the first
 * string is smaller, equal, or bigger than the second. All bits are considered
 * and additional zero bits may make one string smaller/larger than the other,
 * even if their zero-padded values would be the same.
 *	 Anything is equal to undefined.
 */
int
bitcmp(bits8 *arg1, bits8 *arg2)
{
	int			bitlen1,
				bytelen1,
				bitlen2,
				bytelen2;
	int			cmp;

	if (!PointerIsValid(arg1) || !PointerIsValid(arg2))
		return (bool) 0;
	bytelen1 = VARBITBYTES(arg1);
	bytelen2 = VARBITBYTES(arg2);

	cmp = memcmp(VARBITS(arg1), VARBITS(arg2), Min(bytelen1, bytelen2));
	if (cmp == 0)
	{
		bitlen1 = VARBITLEN(arg1);
		bitlen2 = VARBITLEN(arg2);
		if (bitlen1 != bitlen2)
			return bitlen1 < bitlen2 ? -1 : 1;
	}
	return cmp;
}

bool
bitlt(bits8 *arg1, bits8 *arg2)
{
	return (bool) (bitcmp(arg1, arg2) == -1);
}

bool
bitle(bits8 *arg1, bits8 *arg2)
{
	return (bool) (bitcmp(arg1, arg2) <= 0);
}

bool
bitge(bits8 *arg1, bits8 *arg2)
{
	return (bool) (bitcmp(arg1, arg2) >= 0);
}

bool
bitgt(bits8 *arg1, bits8 *arg2)
{
	return (bool) (bitcmp(arg1, arg2) == 1);
}

/* bitcat
 * Concatenation of bit strings
 */
bits8 *
bitcat(bits8 *arg1, bits8 *arg2)
{
	int			bitlen1,
				bitlen2,
				bytelen,
				bit1pad,
				bit2shift;
	bits8	   *result;
	bits8	   *pr,
			   *pa;

	if (!PointerIsValid(arg1) || !PointerIsValid(arg2))
		return NULL;

	bitlen1 = VARBITLEN(arg1);
	bitlen2 = VARBITLEN(arg2);

	bytelen = VARBITDATALEN(bitlen1 + bitlen2);

	result = (bits8 *) palloc(bytelen * sizeof(bits8));
	VARSIZE(result) = bytelen;
	VARBITLEN(result) = bitlen1 + bitlen2;
	/* Copy the first bitstring in */
	memcpy(VARBITS(result), VARBITS(arg1), VARBITBYTES(arg1));
	/* Copy the second bit string */
	bit1pad = VARBITPAD(arg1);
	if (bit1pad == 0)
	{
		memcpy(VARBITS(result) + VARBITBYTES(arg1), VARBITS(arg2),
			   VARBITBYTES(arg2));
	}
	else if (bitlen2 > 0)
	{
		/* We need to shift all the results to fit */
		bit2shift = BITSPERBYTE - bit1pad;
		pa = VARBITS(arg2);
		pr = VARBITS(result) + VARBITBYTES(arg1) - 1;
		for (; pa < VARBITEND(arg2); pa++)
		{
			*pr |= ((*pa >> bit2shift) & BITMASK);
			pr++;
			if (pr < VARBITEND(result))
				*pr = (*pa << bit1pad) & BITMASK;
		}
	}

	return result;
}

/* bitsubstr
 * retrieve a substring from the bit string.
 * Note, s is 1-based.
 * SQL draft 6.10 9)
 */
bits8 *
bitsubstr(bits8 *arg, int32 s, int32 l)
{
	int			bitlen,
				rbitlen,
				len,
				ipad = 0,
				ishift,
				i;
	int			e,
				s1,
				e1;
	bits8	   *result;
	bits8		mask,
			   *r,
			   *ps;

	if (!PointerIsValid(arg))
		return NULL;

	bitlen = VARBITLEN(arg);
	e = s + l;
	s1 = Max(s, 1);
	e1 = Min(e, bitlen + 1);
	if (s1 > bitlen || e1 < 1)
	{
		/* Need to return a null string */
		len = VARBITDATALEN(0);
		result = (bits8 *) palloc(len);
		VARBITLEN(result) = 0;
		VARSIZE(result) = len;
	}
	else
	{

		/*
		 * OK, we've got a true substring starting at position s1-1 and
		 * ending at position e1-1
		 */
		rbitlen = e1 - s1;
		len = VARBITDATALEN(rbitlen);
		result = (bits8 *) palloc(len);
		VARBITLEN(result) = rbitlen;
		VARSIZE(result) = len;
		len -= VARHDRSZ + VARBITHDRSZ;
		/* Are we copying from a byte boundary? */
		if ((s1 - 1) % BITSPERBYTE == 0)
		{
			/* Yep, we are copying bytes */
			memcpy(VARBITS(result), VARBITS(arg) + (s1 - 1) / BITSPERBYTE, len);
		}
		else
		{
			/* Figure out how much we need to shift the sequence by */
			ishift = (s1 - 1) % BITSPERBYTE;
			r = VARBITS(result);
			ps = VARBITS(arg) + (s1 - 1) / BITSPERBYTE;
			for (i = 0; i < len; i++)
			{
				*r = (*ps << ishift) & BITMASK;
				if ((++ps) < VARBITEND(arg))
					*r |= *ps >> (BITSPERBYTE - ishift);
				r++;
			}
		}
		/* Do we need to pad at the end? */
		ipad = VARBITPAD(result);
		if (ipad > 0)
		{
			mask = BITMASK << ipad;
			*(VARBITS(result) + len - 1) &= mask;
		}
	}

	return result;
}

/* bitand
 * perform a logical AND on two bit strings. The result is automatically
 * truncated to the shorter bit string
 */
bits8 *
bitand(bits8 *arg1, bits8 *arg2)
{
	int			len,
				i;
	bits8	   *result;
	bits8	   *p1,
			   *p2,
			   *r;

	if (!PointerIsValid(arg1) || !PointerIsValid(arg2))
		return (bool) 0;

	len = Min(VARSIZE(arg1), VARSIZE(arg2));
	result = (bits8 *) palloc(len);
	VARSIZE(result) = len;
	VARBITLEN(result) = Min(VARBITLEN(arg1), VARBITLEN(arg2));

	p1 = (bits8 *) VARBITS(arg1);
	p2 = (bits8 *) VARBITS(arg2);
	r = (bits8 *) VARBITS(result);
	for (i = 0; i < Min(VARBITBYTES(arg1), VARBITBYTES(arg2)); i++)
		*r++ = *p1++ & *p2++;

	/* Padding is not needed as & of 0 pad is 0 */

	return result;
}

/* bitor
 * perform a logical OR on two bit strings. The result is automatically
 * truncated to the shorter bit string.
 */
bits8 *
bitor(bits8 *arg1, bits8 *arg2)
{
	int			len,
				i;
	bits8	   *result;
	bits8	   *p1,
			   *p2,
			   *r;
	bits8		mask;

	if (!PointerIsValid(arg1) || !PointerIsValid(arg2))
		return (bool) 0;

	len = Min(VARSIZE(arg1), VARSIZE(arg2));
	result = (bits8 *) palloc(len);
	VARSIZE(result) = len;
	VARBITLEN(result) = Min(VARBITLEN(arg1), VARBITLEN(arg2));

	p1 = (bits8 *) VARBITS(arg1);
	p2 = (bits8 *) VARBITS(arg2);
	r = (bits8 *) VARBITS(result);
	for (i = 0; i < Min(VARBITBYTES(arg1), VARBITBYTES(arg2)); i++)
		*r++ = *p1++ | *p2++;

	/* Pad the result */
	mask = BITMASK << VARBITPAD(result);
	*r &= mask;

	return result;
}

/* bitxor
 * perform a logical XOR on two bit strings. The result is automatically
 * truncated to the shorter bit string.
 */
bits8 *
bitxor(bits8 *arg1, bits8 *arg2)
{
	int			len,
				i;
	bits8	   *result;
	bits8	   *p1,
			   *p2,
			   *r;
	bits8		mask;

	if (!PointerIsValid(arg1) || !PointerIsValid(arg2))
		return (bool) 0;

	len = Min(VARSIZE(arg1), VARSIZE(arg2));
	result = (bits8 *) palloc(len);
	VARSIZE(result) = len;
	VARBITLEN(result) = Min(VARBITLEN(arg1), VARBITLEN(arg2));

	p1 = (bits8 *) VARBITS(arg1);
	p2 = (bits8 *) VARBITS(arg2);
	r = (bits8 *) VARBITS(result);
	for (i = 0; i < Min(VARBITBYTES(arg1), VARBITBYTES(arg2)); i++)
		*r++ = *p1++ ^ *p2++;

	/* Pad the result */
	mask = BITMASK << VARBITPAD(result);
	*r &= mask;

	return result;
}

/* bitnot
 * perform a logical NOT on a bit strings.
 */
bits8 *
bitnot(bits8 *arg)
{
	bits8	   *result;
	bits8	   *p,
			   *r;
	bits8		mask;

	if (!PointerIsValid(arg))
		return (bool) 0;

	result = (bits8 *) palloc(VARSIZE(arg));
	VARSIZE(result) = VARSIZE(arg);
	VARBITLEN(result) = VARBITLEN(arg);

	p = (bits8 *) VARBITS(arg);
	r = (bits8 *) VARBITS(result);
	for (; p < VARBITEND(arg); p++, r++)
		*r = ~*p;

	/* Pad the result */
	mask = BITMASK << VARBITPAD(result);
	*r &= mask;

	return result;
}

/* bitshiftleft
 * do a left shift (i.e. to the beginning of the string) of the bit string
 */
bits8 *
bitshiftleft(bits8 *arg, int shft)
{
	int			byte_shift,
				ishift,
				len;
	bits8	   *result;
	bits8	   *p,
			   *r;

	if (!PointerIsValid(arg))
		return (bool) 0;

	/* Negative shift is a shift to the right */
	if (shft < 0)
		return bitshiftright(arg, -shft);

	result = (bits8 *) palloc(VARSIZE(arg));
	VARSIZE(result) = VARSIZE(arg);
	VARBITLEN(result) = VARBITLEN(arg);
	r = (bits8 *) VARBITS(result);

	byte_shift = shft / BITSPERBYTE;
	ishift = shft % BITSPERBYTE;
	p = ((bits8 *) VARBITS(arg)) + byte_shift;

	if (ishift == 0)
	{
		/* Special case: we can do a memcpy */
		len = VARBITBYTES(arg) - byte_shift;
		memcpy(r, p, len);
		memset(r + len, 0, byte_shift);
	}
	else
	{
		for (; p < VARBITEND(arg); r++)
		{
			*r = *p << ishift;
			if ((++p) < VARBITEND(arg))
				*r |= *p >> (BITSPERBYTE - ishift);
		}
		for (; r < VARBITEND(result); r++)
			*r = (bits8) 0;
	}

	return result;
}

/* bitshiftright
 * do a right shift (i.e. to the beginning of the string) of the bit string
 */
bits8 *
bitshiftright(bits8 *arg, int shft)
{
	int			byte_shift,
				ishift,
				len;
	bits8	   *result;
	bits8	   *p,
			   *r;

	if (!PointerIsValid(arg))
		return (bits8 *) 0;

	/* Negative shift is a shift to the left */
	if (shft < 0)
		return bitshiftleft(arg, -shft);

	result = (bits8 *) palloc(VARSIZE(arg));
	VARSIZE(result) = VARSIZE(arg);
	VARBITLEN(result) = VARBITLEN(arg);
	r = (bits8 *) VARBITS(result);

	byte_shift = shft / BITSPERBYTE;
	ishift = shft % BITSPERBYTE;
	p = (bits8 *) VARBITS(arg);

	/* Set the first part of the result to 0 */
	memset(r, 0, byte_shift);

	if (ishift == 0)
	{
		/* Special case: we can do a memcpy */
		len = VARBITBYTES(arg) - byte_shift;
		memcpy(r + byte_shift, p, len);
	}
	else
	{
		r += byte_shift;
		*r = 0;					/* Initialise first byte */
		for (; r < VARBITEND(result); p++)
		{
			*r |= *p >> ishift;
			if ((++r) < VARBITEND(result))
				*r = (*p << (BITSPERBYTE - ishift)) & BITMASK;
		}
	}

	return result;
}

bool
varbiteq(bits8 *arg1, bits8 *arg2)
{
	return biteq(arg1, arg2);
}

bool
varbitne(bits8 *arg1, bits8 *arg2)
{
	return bitne(arg1, arg2);
}

bool
varbitge(bits8 *arg1, bits8 *arg2)
{
	return bitge(arg1, arg2);
}

bool
varbitgt(bits8 *arg1, bits8 *arg2)
{
	return bitgt(arg1, arg2);
}

bool
varbitle(bits8 *arg1, bits8 *arg2)
{
	return bitle(arg1, arg2);
}

bool
varbitlt(bits8 *arg1, bits8 *arg2)
{
	return bitlt(arg1, arg2);
}

int
varbitcmp(bits8 *arg1, bits8 *arg2)
{
	return bitcmp(arg1, arg2);
}

bits8 *
varbitand(bits8 *arg1, bits8 *arg2)
{
	return bitand(arg1, arg2);
}

bits8 *
varbitor(bits8 *arg1, bits8 *arg2)
{
	return bitor(arg1, arg2);
}

bits8 *
varbitxor(bits8 *arg1, bits8 *arg2)
{
	return bitxor(arg1, arg2);
}

bits8 *
varbitnot(bits8 *arg)
{
	return bitnot(arg);
}

bits8 *
varbitshiftright(bits8 *arg, int shft)
{
	return bitshiftright(arg, shft);
}

bits8 *
varbitshiftleft(bits8 *arg, int shft)
{
	return bitshiftleft(arg, shft);
}

bits8 *
varbitcat(bits8 *arg1, bits8 *arg2)
{
	return bitcat(arg1, arg2);
}

bits8 *
varbitsubstr(bits8 *arg, int32 s, int32 l)
{
	return bitsubstr(arg, s, l);
}