1
/*-------------------------------------------------------------------------
4
* Functions for the SQL datatypes BIT() and BIT VARYING().
6
* Code originally contributed by Adriaan Joubert.
8
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
9
* Portions Copyright (c) 1994, Regents of the University of California
12
* $PostgreSQL: pgsql/src/backend/utils/adt/varbit.c,v 1.44 2004-12-31 22:01:22 pgsql Exp $
14
*-------------------------------------------------------------------------
19
#include "catalog/pg_type.h"
20
#include "libpq/pqformat.h"
21
#include "utils/array.h"
22
#include "utils/fmgroids.h"
23
#include "utils/memutils.h"
24
#include "utils/varbit.h"
26
#define HEXDIG(z) ((z)<10 ? ((z)+'0') : ((z)-10+'A'))
30
* attypmod -- contains the length of the bit string in bits, or for
31
* varying bits the maximum length.
33
* The data structure contains the following elements:
34
* header -- length of the whole data structure (incl header)
35
* in bytes. (as with all varying length datatypes)
36
* data section -- private data section for the bits data structures
37
* bitlength -- length of the bit string in bits
38
* bitdata -- bit string, most significant byte first
40
* The length of the bitdata vector should always be exactly as many
41
* bytes as are needed for the given bitlength. If the bitlength is
42
* not a multiple of 8, the extra low-order padding bits of the last
43
* byte must be zeroes.
49
* converts a char string to the internal representation of a bitstring.
50
* The length is determined by the number of bits required plus
51
* VARHDRSZ bytes or from atttypmod.
54
bit_in(PG_FUNCTION_ARGS)
56
char *input_string = PG_GETARG_CSTRING(0);
59
Oid typelem = PG_GETARG_OID(1);
61
int32 atttypmod = PG_GETARG_INT32(2);
62
VarBit *result; /* The resulting bit string */
63
char *sp; /* pointer into the character string */
64
bits8 *r; /* pointer into the result */
65
int len, /* Length of the whole data structure */
66
bitlen, /* Number of bits in the bit string */
67
slen; /* Length of the input string */
68
bool bit_not_hex; /* false = hex string true = bit string */
72
/* Check that the first character is a b or an x */
73
if (input_string[0] == 'b' || input_string[0] == 'B')
76
sp = input_string + 1;
78
else if (input_string[0] == 'x' || input_string[0] == 'X')
81
sp = input_string + 1;
86
* Otherwise it's binary. This allows things like cast('1001' as
87
* bit) to work transparently.
94
/* Determine bitlength from input string */
101
* Sometimes atttypmod is not supplied. If it is supplied we need to
102
* make sure that the bitstring fits.
106
else if (bitlen != atttypmod)
108
(errcode(ERRCODE_STRING_DATA_LENGTH_MISMATCH),
109
errmsg("bit string length %d does not match type bit(%d)",
110
bitlen, atttypmod)));
112
len = VARBITTOTALLEN(atttypmod);
113
/* set to 0 so that *r is always initialised and string is zero-padded */
114
result = (VarBit *) palloc0(len);
115
VARATT_SIZEP(result) = len;
116
VARBITLEN(result) = atttypmod;
121
/* Parse the bit representation of the string */
122
/* We know it fits, as bitlen was compared to atttypmod */
130
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
131
errmsg("\"%c\" is not a valid binary digit",
144
/* Parse the hex representation of the string */
145
for (bc = 0; *sp; sp++)
147
if (*sp >= '0' && *sp <= '9')
148
x = (bits8) (*sp - '0');
149
else if (*sp >= 'A' && *sp <= 'F')
150
x = (bits8) (*sp - 'A') + 10;
151
else if (*sp >= 'a' && *sp <= 'f')
152
x = (bits8) (*sp - 'a') + 10;
155
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
156
errmsg("\"%c\" is not a valid hexadecimal digit",
172
PG_RETURN_VARBIT_P(result);
177
bit_out(PG_FUNCTION_ARGS)
180
/* same as varbit output */
181
return varbit_out(fcinfo);
183
/* This is how one would print a hex string, in case someone wants to
184
write a formatting function. */
185
VarBit *s = PG_GETARG_VARBIT_P(0);
193
bitlen = VARBITLEN(s);
194
len = (bitlen + 3) / 4;
195
result = (char *) palloc(len + 2);
199
/* we cheat by knowing that we store full bytes zero padded */
200
for (i = 0; i < len; i += 2, sp++)
202
*r++ = HEXDIG((*sp) >> 4);
203
*r++ = HEXDIG((*sp) & 0xF);
207
* Go back one step if we printed a hex number that was not part of
208
* the bitstring anymore
214
PG_RETURN_CSTRING(result);
219
* bit_recv - converts external binary format to bit
222
bit_recv(PG_FUNCTION_ARGS)
224
/* Exactly the same as varbit_recv, so share code */
225
return varbit_recv(fcinfo);
229
* bit_send - converts bit to binary format
232
bit_send(PG_FUNCTION_ARGS)
234
/* Exactly the same as varbit_send, so share code */
235
return varbit_send(fcinfo);
239
* Converts a bit() type to a specific internal length.
240
* len is the bitlength specified in the column definition.
242
* If doing implicit cast, raise error when source data is wrong length.
243
* If doing explicit cast, silently truncate or zero-pad to specified length.
246
bit(PG_FUNCTION_ARGS)
248
VarBit *arg = PG_GETARG_VARBIT_P(0);
249
int32 len = PG_GETARG_INT32(1);
250
bool isExplicit = PG_GETARG_BOOL(2);
256
/* No work if typmod is invalid or supplied data matches it already */
257
if (len <= 0 || len == VARBITLEN(arg))
258
PG_RETURN_VARBIT_P(arg);
262
(errcode(ERRCODE_STRING_DATA_LENGTH_MISMATCH),
263
errmsg("bit string length %d does not match type bit(%d)",
264
VARBITLEN(arg), len)));
266
rlen = VARBITTOTALLEN(len);
267
/* set to 0 so that string is zero-padded */
268
result = (VarBit *) palloc0(rlen);
269
VARATT_SIZEP(result) = rlen;
270
VARBITLEN(result) = len;
272
memcpy(VARBITS(result), VARBITS(arg),
273
Min(VARBITBYTES(result), VARBITBYTES(arg)));
276
* Make sure last byte is zero-padded if needed. This is useless but
277
* safe if source data was shorter than target length (we assume the
278
* last byte of the source data was itself correctly zero-padded).
280
ipad = VARBITPAD(result);
283
mask = BITMASK << ipad;
284
*(VARBITS(result) + VARBITBYTES(result) - 1) &= mask;
287
PG_RETURN_VARBIT_P(result);
292
* converts a string to the internal representation of a bitstring.
293
* This is the same as bit_in except that atttypmod is taken as
294
* the maximum length, not the exact length to force the bitstring to.
297
varbit_in(PG_FUNCTION_ARGS)
299
char *input_string = PG_GETARG_CSTRING(0);
302
Oid typelem = PG_GETARG_OID(1);
304
int32 atttypmod = PG_GETARG_INT32(2);
305
VarBit *result; /* The resulting bit string */
306
char *sp; /* pointer into the character string */
307
bits8 *r; /* pointer into the result */
308
int len, /* Length of the whole data structure */
309
bitlen, /* Number of bits in the bit string */
310
slen; /* Length of the input string */
311
bool bit_not_hex; /* false = hex string true = bit string */
315
/* Check that the first character is a b or an x */
316
if (input_string[0] == 'b' || input_string[0] == 'B')
319
sp = input_string + 1;
321
else if (input_string[0] == 'x' || input_string[0] == 'X')
324
sp = input_string + 1;
333
/* Determine bitlength from input string */
340
* Sometimes atttypmod is not supplied. If it is supplied we need to
341
* make sure that the bitstring fits.
345
else if (bitlen > atttypmod)
347
(errcode(ERRCODE_STRING_DATA_RIGHT_TRUNCATION),
348
errmsg("bit string too long for type bit varying(%d)",
351
len = VARBITTOTALLEN(bitlen);
352
/* set to 0 so that *r is always initialised and string is zero-padded */
353
result = (VarBit *) palloc0(len);
354
VARATT_SIZEP(result) = len;
355
VARBITLEN(result) = Min(bitlen, atttypmod);
360
/* Parse the bit representation of the string */
361
/* We know it fits, as bitlen was compared to atttypmod */
369
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
370
errmsg("\"%c\" is not a valid binary digit",
383
/* Parse the hex representation of the string */
384
for (bc = 0; *sp; sp++)
386
if (*sp >= '0' && *sp <= '9')
387
x = (bits8) (*sp - '0');
388
else if (*sp >= 'A' && *sp <= 'F')
389
x = (bits8) (*sp - 'A') + 10;
390
else if (*sp >= 'a' && *sp <= 'f')
391
x = (bits8) (*sp - 'a') + 10;
394
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
395
errmsg("\"%c\" is not a valid hexadecimal digit",
411
PG_RETURN_VARBIT_P(result);
415
* Prints the string as bits to preserve length accurately
418
varbit_out(PG_FUNCTION_ARGS)
420
VarBit *s = PG_GETARG_VARBIT_P(0);
430
result = (char *) palloc(len + 1);
433
for (i = 0; i < len - BITS_PER_BYTE; i += BITS_PER_BYTE, sp++)
436
for (k = 0; k < BITS_PER_BYTE; k++)
438
*r++ = (x & BITHIGH) ? '1' : '0';
443
for (k = i; k < len; k++)
445
*r++ = (x & BITHIGH) ? '1' : '0';
450
PG_RETURN_CSTRING(result);
454
* varbit_recv - converts external binary format to varbit
456
* External format is the bitlen as an int32, then the byte array.
459
varbit_recv(PG_FUNCTION_ARGS)
461
StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
468
bitlen = pq_getmsgint(buf, sizeof(int32));
471
(errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
472
errmsg("invalid length in external bit string")));
474
len = VARBITTOTALLEN(bitlen);
475
result = (VarBit *) palloc(len);
476
VARATT_SIZEP(result) = len;
477
VARBITLEN(result) = bitlen;
479
pq_copymsgbytes(buf, (char *) VARBITS(result), VARBITBYTES(result));
481
/* Make sure last byte is zero-padded if needed */
482
ipad = VARBITPAD(result);
485
mask = BITMASK << ipad;
486
*(VARBITS(result) + VARBITBYTES(result) - 1) &= mask;
489
PG_RETURN_VARBIT_P(result);
493
* varbit_send - converts varbit to binary format
496
varbit_send(PG_FUNCTION_ARGS)
498
VarBit *s = PG_GETARG_VARBIT_P(0);
501
pq_begintypsend(&buf);
502
pq_sendint(&buf, VARBITLEN(s), sizeof(int32));
503
pq_sendbytes(&buf, VARBITS(s), VARBITBYTES(s));
504
PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
508
* Converts a varbit() type to a specific internal length.
509
* len is the maximum bitlength specified in the column definition.
511
* If doing implicit cast, raise error when source data is too long.
512
* If doing explicit cast, silently truncate to max length.
515
varbit(PG_FUNCTION_ARGS)
517
VarBit *arg = PG_GETARG_VARBIT_P(0);
518
int32 len = PG_GETARG_INT32(1);
519
bool isExplicit = PG_GETARG_BOOL(2);
525
/* No work if typmod is invalid or supplied data matches it already */
526
if (len <= 0 || len >= VARBITLEN(arg))
527
PG_RETURN_VARBIT_P(arg);
531
(errcode(ERRCODE_STRING_DATA_RIGHT_TRUNCATION),
532
errmsg("bit string too long for type bit varying(%d)",
535
rlen = VARBITTOTALLEN(len);
536
result = (VarBit *) palloc(rlen);
537
VARATT_SIZEP(result) = rlen;
538
VARBITLEN(result) = len;
540
memcpy(VARBITS(result), VARBITS(arg), VARBITBYTES(result));
542
/* Make sure last byte is zero-padded if needed */
543
ipad = VARBITPAD(result);
546
mask = BITMASK << ipad;
547
*(VARBITS(result) + VARBITBYTES(result) - 1) &= mask;
550
PG_RETURN_VARBIT_P(result);
555
* Comparison operators
557
* We only need one set of comparison operators for bitstrings, as the lengths
558
* are stored in the same way for zero-padded and varying bit strings.
560
* Note that the standard is not unambiguous about the comparison between
561
* zero-padded bit strings and varying bitstrings. If the same value is written
562
* into a zero padded bitstring as into a varying bitstring, but the zero
563
* padded bitstring has greater length, it will be bigger.
565
* Zeros from the beginning of a bitstring cannot simply be ignored, as they
566
* may be part of a bit string and may be significant.
568
* Note: btree indexes need these routines not to leak memory; therefore,
569
* be careful to free working copies of toasted datums. Most places don't
570
* need to be so careful.
575
* Compares two bitstrings and returns <0, 0, >0 depending on whether the first
576
* string is smaller, equal, or bigger than the second. All bits are considered
577
* and additional zero bits may make one string smaller/larger than the other,
578
* even if their zero-padded values would be the same.
581
bit_cmp(VarBit *arg1, VarBit *arg2)
589
bytelen1 = VARBITBYTES(arg1);
590
bytelen2 = VARBITBYTES(arg2);
592
cmp = memcmp(VARBITS(arg1), VARBITS(arg2), Min(bytelen1, bytelen2));
595
bitlen1 = VARBITLEN(arg1);
596
bitlen2 = VARBITLEN(arg2);
597
if (bitlen1 != bitlen2)
598
cmp = (bitlen1 < bitlen2) ? -1 : 1;
604
biteq(PG_FUNCTION_ARGS)
606
VarBit *arg1 = PG_GETARG_VARBIT_P(0);
607
VarBit *arg2 = PG_GETARG_VARBIT_P(1);
612
bitlen1 = VARBITLEN(arg1);
613
bitlen2 = VARBITLEN(arg2);
615
/* fast path for different-length inputs */
616
if (bitlen1 != bitlen2)
619
result = (bit_cmp(arg1, arg2) == 0);
621
PG_FREE_IF_COPY(arg1, 0);
622
PG_FREE_IF_COPY(arg2, 1);
624
PG_RETURN_BOOL(result);
628
bitne(PG_FUNCTION_ARGS)
630
VarBit *arg1 = PG_GETARG_VARBIT_P(0);
631
VarBit *arg2 = PG_GETARG_VARBIT_P(1);
636
bitlen1 = VARBITLEN(arg1);
637
bitlen2 = VARBITLEN(arg2);
639
/* fast path for different-length inputs */
640
if (bitlen1 != bitlen2)
643
result = (bit_cmp(arg1, arg2) != 0);
645
PG_FREE_IF_COPY(arg1, 0);
646
PG_FREE_IF_COPY(arg2, 1);
648
PG_RETURN_BOOL(result);
652
bitlt(PG_FUNCTION_ARGS)
654
VarBit *arg1 = PG_GETARG_VARBIT_P(0);
655
VarBit *arg2 = PG_GETARG_VARBIT_P(1);
658
result = (bit_cmp(arg1, arg2) < 0);
660
PG_FREE_IF_COPY(arg1, 0);
661
PG_FREE_IF_COPY(arg2, 1);
663
PG_RETURN_BOOL(result);
667
bitle(PG_FUNCTION_ARGS)
669
VarBit *arg1 = PG_GETARG_VARBIT_P(0);
670
VarBit *arg2 = PG_GETARG_VARBIT_P(1);
673
result = (bit_cmp(arg1, arg2) <= 0);
675
PG_FREE_IF_COPY(arg1, 0);
676
PG_FREE_IF_COPY(arg2, 1);
678
PG_RETURN_BOOL(result);
682
bitgt(PG_FUNCTION_ARGS)
684
VarBit *arg1 = PG_GETARG_VARBIT_P(0);
685
VarBit *arg2 = PG_GETARG_VARBIT_P(1);
688
result = (bit_cmp(arg1, arg2) > 0);
690
PG_FREE_IF_COPY(arg1, 0);
691
PG_FREE_IF_COPY(arg2, 1);
693
PG_RETURN_BOOL(result);
697
bitge(PG_FUNCTION_ARGS)
699
VarBit *arg1 = PG_GETARG_VARBIT_P(0);
700
VarBit *arg2 = PG_GETARG_VARBIT_P(1);
703
result = (bit_cmp(arg1, arg2) >= 0);
705
PG_FREE_IF_COPY(arg1, 0);
706
PG_FREE_IF_COPY(arg2, 1);
708
PG_RETURN_BOOL(result);
712
bitcmp(PG_FUNCTION_ARGS)
714
VarBit *arg1 = PG_GETARG_VARBIT_P(0);
715
VarBit *arg2 = PG_GETARG_VARBIT_P(1);
718
result = bit_cmp(arg1, arg2);
720
PG_FREE_IF_COPY(arg1, 0);
721
PG_FREE_IF_COPY(arg2, 1);
723
PG_RETURN_INT32(result);
727
* Concatenation of bit strings
730
bitcat(PG_FUNCTION_ARGS)
732
VarBit *arg1 = PG_GETARG_VARBIT_P(0);
733
VarBit *arg2 = PG_GETARG_VARBIT_P(1);
743
bitlen1 = VARBITLEN(arg1);
744
bitlen2 = VARBITLEN(arg2);
746
bytelen = VARBITTOTALLEN(bitlen1 + bitlen2);
748
result = (VarBit *) palloc(bytelen);
749
VARATT_SIZEP(result) = bytelen;
750
VARBITLEN(result) = bitlen1 + bitlen2;
752
/* Copy the first bitstring in */
753
memcpy(VARBITS(result), VARBITS(arg1), VARBITBYTES(arg1));
755
/* Copy the second bit string */
756
bit1pad = VARBITPAD(arg1);
759
memcpy(VARBITS(result) + VARBITBYTES(arg1), VARBITS(arg2),
762
else if (bitlen2 > 0)
764
/* We need to shift all the bits to fit */
765
bit2shift = BITS_PER_BYTE - bit1pad;
766
pr = VARBITS(result) + VARBITBYTES(arg1) - 1;
767
for (pa = VARBITS(arg2); pa < VARBITEND(arg2); pa++)
769
*pr |= ((*pa >> bit2shift) & BITMASK);
771
if (pr < VARBITEND(result))
772
*pr = (*pa << bit1pad) & BITMASK;
776
PG_RETURN_VARBIT_P(result);
780
* retrieve a substring from the bit string.
781
* Note, s is 1-based.
785
bitsubstr(PG_FUNCTION_ARGS)
787
VarBit *arg = PG_GETARG_VARBIT_P(0);
788
int32 s = PG_GETARG_INT32(1);
789
int32 l = PG_GETARG_INT32(2);
804
bitlen = VARBITLEN(arg);
805
/* If we do not have an upper bound, set bitlen */
810
e1 = Min(e, bitlen + 1);
811
if (s1 > bitlen || e1 < 1)
813
/* Need to return a zero-length bitstring */
814
len = VARBITTOTALLEN(0);
815
result = (VarBit *) palloc(len);
816
VARATT_SIZEP(result) = len;
817
VARBITLEN(result) = 0;
822
* OK, we've got a true substring starting at position s1-1 and
823
* ending at position e1-1
826
len = VARBITTOTALLEN(rbitlen);
827
result = (VarBit *) palloc(len);
828
VARATT_SIZEP(result) = len;
829
VARBITLEN(result) = rbitlen;
830
len -= VARHDRSZ + VARBITHDRSZ;
831
/* Are we copying from a byte boundary? */
832
if ((s1 - 1) % BITS_PER_BYTE == 0)
834
/* Yep, we are copying bytes */
835
memcpy(VARBITS(result), VARBITS(arg) + (s1 - 1) / BITS_PER_BYTE,
840
/* Figure out how much we need to shift the sequence by */
841
ishift = (s1 - 1) % BITS_PER_BYTE;
843
ps = VARBITS(arg) + (s1 - 1) / BITS_PER_BYTE;
844
for (i = 0; i < len; i++)
846
*r = (*ps << ishift) & BITMASK;
847
if ((++ps) < VARBITEND(arg))
848
*r |= *ps >> (BITS_PER_BYTE - ishift);
852
/* Do we need to pad at the end? */
853
ipad = VARBITPAD(result);
856
mask = BITMASK << ipad;
857
*(VARBITS(result) + len - 1) &= mask;
861
PG_RETURN_VARBIT_P(result);
864
/* bitlength, bitoctetlength
865
* Return the length of a bit string
868
bitlength(PG_FUNCTION_ARGS)
870
VarBit *arg = PG_GETARG_VARBIT_P(0);
872
PG_RETURN_INT32(VARBITLEN(arg));
876
bitoctetlength(PG_FUNCTION_ARGS)
878
VarBit *arg = PG_GETARG_VARBIT_P(0);
880
PG_RETURN_INT32(VARBITBYTES(arg));
884
* perform a logical AND on two bit strings.
887
bitand(PG_FUNCTION_ARGS)
889
VarBit *arg1 = PG_GETARG_VARBIT_P(0);
890
VarBit *arg2 = PG_GETARG_VARBIT_P(1);
900
bitlen1 = VARBITLEN(arg1);
901
bitlen2 = VARBITLEN(arg2);
902
if (bitlen1 != bitlen2)
904
(errcode(ERRCODE_STRING_DATA_LENGTH_MISMATCH),
905
errmsg("cannot AND bit strings of different sizes")));
908
result = (VarBit *) palloc(len);
909
VARATT_SIZEP(result) = len;
910
VARBITLEN(result) = bitlen1;
915
for (i = 0; i < VARBITBYTES(arg1); i++)
916
*r++ = *p1++ & *p2++;
918
/* Padding is not needed as & of 0 pad is 0 */
920
PG_RETURN_VARBIT_P(result);
924
* perform a logical OR on two bit strings.
927
bitor(PG_FUNCTION_ARGS)
929
VarBit *arg1 = PG_GETARG_VARBIT_P(0);
930
VarBit *arg2 = PG_GETARG_VARBIT_P(1);
941
bitlen1 = VARBITLEN(arg1);
942
bitlen2 = VARBITLEN(arg2);
943
if (bitlen1 != bitlen2)
945
(errcode(ERRCODE_STRING_DATA_LENGTH_MISMATCH),
946
errmsg("cannot OR bit strings of different sizes")));
948
result = (VarBit *) palloc(len);
949
VARATT_SIZEP(result) = len;
950
VARBITLEN(result) = bitlen1;
955
for (i = 0; i < VARBITBYTES(arg1); i++)
956
*r++ = *p1++ | *p2++;
959
mask = BITMASK << VARBITPAD(result);
966
PG_RETURN_VARBIT_P(result);
970
* perform a logical XOR on two bit strings.
973
bitxor(PG_FUNCTION_ARGS)
975
VarBit *arg1 = PG_GETARG_VARBIT_P(0);
976
VarBit *arg2 = PG_GETARG_VARBIT_P(1);
987
bitlen1 = VARBITLEN(arg1);
988
bitlen2 = VARBITLEN(arg2);
989
if (bitlen1 != bitlen2)
991
(errcode(ERRCODE_STRING_DATA_LENGTH_MISMATCH),
992
errmsg("cannot XOR bit strings of different sizes")));
995
result = (VarBit *) palloc(len);
996
VARATT_SIZEP(result) = len;
997
VARBITLEN(result) = bitlen1;
1001
r = VARBITS(result);
1002
for (i = 0; i < VARBITBYTES(arg1); i++)
1003
*r++ = *p1++ ^ *p2++;
1005
/* Pad the result */
1006
mask = BITMASK << VARBITPAD(result);
1013
PG_RETURN_VARBIT_P(result);
1017
* perform a logical NOT on a bit string.
1020
bitnot(PG_FUNCTION_ARGS)
1022
VarBit *arg = PG_GETARG_VARBIT_P(0);
1028
result = (VarBit *) palloc(VARSIZE(arg));
1029
VARATT_SIZEP(result) = VARSIZE(arg);
1030
VARBITLEN(result) = VARBITLEN(arg);
1033
r = VARBITS(result);
1034
for (; p < VARBITEND(arg); p++)
1037
/* Pad the result */
1038
mask = BITMASK << VARBITPAD(result);
1045
PG_RETURN_VARBIT_P(result);
1049
* do a left shift (i.e. towards the beginning of the string)
1052
bitshiftleft(PG_FUNCTION_ARGS)
1054
VarBit *arg = PG_GETARG_VARBIT_P(0);
1055
int32 shft = PG_GETARG_INT32(1);
1063
/* Negative shift is a shift to the right */
1065
PG_RETURN_DATUM(DirectFunctionCall2(bitshiftright,
1066
VarBitPGetDatum(arg),
1067
Int32GetDatum(-shft)));
1069
result = (VarBit *) palloc(VARSIZE(arg));
1070
VARATT_SIZEP(result) = VARSIZE(arg);
1071
VARBITLEN(result) = VARBITLEN(arg);
1072
r = VARBITS(result);
1074
/* If we shifted all the bits out, return an all-zero string */
1075
if (shft >= VARBITLEN(arg))
1077
MemSet(r, 0, VARBITBYTES(arg));
1078
PG_RETURN_VARBIT_P(result);
1081
byte_shift = shft / BITS_PER_BYTE;
1082
ishift = shft % BITS_PER_BYTE;
1083
p = VARBITS(arg) + byte_shift;
1087
/* Special case: we can do a memcpy */
1088
len = VARBITBYTES(arg) - byte_shift;
1090
MemSet(r + len, 0, byte_shift);
1094
for (; p < VARBITEND(arg); r++)
1097
if ((++p) < VARBITEND(arg))
1098
*r |= *p >> (BITS_PER_BYTE - ishift);
1100
for (; r < VARBITEND(result); r++)
1104
PG_RETURN_VARBIT_P(result);
1108
* do a right shift (i.e. towards the end of the string)
1111
bitshiftright(PG_FUNCTION_ARGS)
1113
VarBit *arg = PG_GETARG_VARBIT_P(0);
1114
int32 shft = PG_GETARG_INT32(1);
1122
/* Negative shift is a shift to the left */
1124
PG_RETURN_DATUM(DirectFunctionCall2(bitshiftleft,
1125
VarBitPGetDatum(arg),
1126
Int32GetDatum(-shft)));
1128
result = (VarBit *) palloc(VARSIZE(arg));
1129
VARATT_SIZEP(result) = VARSIZE(arg);
1130
VARBITLEN(result) = VARBITLEN(arg);
1131
r = VARBITS(result);
1133
/* If we shifted all the bits out, return an all-zero string */
1134
if (shft >= VARBITLEN(arg))
1136
MemSet(r, 0, VARBITBYTES(arg));
1137
PG_RETURN_VARBIT_P(result);
1140
byte_shift = shft / BITS_PER_BYTE;
1141
ishift = shft % BITS_PER_BYTE;
1144
/* Set the first part of the result to 0 */
1145
MemSet(r, 0, byte_shift);
1150
/* Special case: we can do a memcpy */
1151
len = VARBITBYTES(arg) - byte_shift;
1156
if (r < VARBITEND(result))
1157
*r = 0; /* initialize first byte */
1158
for (; r < VARBITEND(result); p++)
1161
if ((++r) < VARBITEND(result))
1162
*r = (*p << (BITS_PER_BYTE - ishift)) & BITMASK;
1166
PG_RETURN_VARBIT_P(result);
1170
* This is not defined in any standard. We retain the natural ordering of
1171
* bits here, as it just seems more intuitive.
1174
bitfromint4(PG_FUNCTION_ARGS)
1176
int32 a = PG_GETARG_INT32(0);
1177
int32 typmod = PG_GETARG_INT32(1);
1185
typmod = 1; /* default bit length */
1187
rlen = VARBITTOTALLEN(typmod);
1188
result = (VarBit *) palloc(rlen);
1189
VARATT_SIZEP(result) = rlen;
1190
VARBITLEN(result) = typmod;
1192
r = VARBITS(result);
1193
destbitsleft = typmod;
1195
/* drop any input bits that don't fit */
1196
srcbitsleft = Min(srcbitsleft, destbitsleft);
1197
/* sign-fill any excess bytes in output */
1198
while (destbitsleft >= srcbitsleft + 8)
1200
*r++ = (bits8) ((a < 0) ? BITMASK : 0);
1203
/* store first fractional byte */
1204
if (destbitsleft > srcbitsleft)
1206
*r++ = (bits8) ((a >> (srcbitsleft - 8)) & BITMASK);
1209
/* Now srcbitsleft and destbitsleft are the same, need not track both */
1210
/* store whole bytes */
1211
while (destbitsleft >= 8)
1213
*r++ = (bits8) ((a >> (destbitsleft - 8)) & BITMASK);
1216
/* store last fractional byte */
1217
if (destbitsleft > 0)
1218
*r = (bits8) ((a << (8 - destbitsleft)) & BITMASK);
1220
PG_RETURN_VARBIT_P(result);
1224
bittoint4(PG_FUNCTION_ARGS)
1226
VarBit *arg = PG_GETARG_VARBIT_P(0);
1230
/* Check that the bit string is not too long */
1231
if (VARBITLEN(arg) > sizeof(result) * BITS_PER_BYTE)
1233
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1234
errmsg("integer out of range")));
1237
for (r = VARBITS(arg); r < VARBITEND(arg); r++)
1239
result <<= BITS_PER_BYTE;
1242
/* Now shift the result to take account of the padding at the end */
1243
result >>= VARBITPAD(arg);
1245
PG_RETURN_INT32(result);
1249
bitfromint8(PG_FUNCTION_ARGS)
1251
int64 a = PG_GETARG_INT64(0);
1252
int32 typmod = PG_GETARG_INT32(1);
1260
typmod = 1; /* default bit length */
1262
rlen = VARBITTOTALLEN(typmod);
1263
result = (VarBit *) palloc(rlen);
1264
VARATT_SIZEP(result) = rlen;
1265
VARBITLEN(result) = typmod;
1267
r = VARBITS(result);
1268
destbitsleft = typmod;
1269
#ifndef INT64_IS_BUSTED
1272
srcbitsleft = 32; /* don't try to shift more than 32 */
1274
/* drop any input bits that don't fit */
1275
srcbitsleft = Min(srcbitsleft, destbitsleft);
1276
/* sign-fill any excess bytes in output */
1277
while (destbitsleft >= srcbitsleft + 8)
1279
*r++ = (bits8) ((a < 0) ? BITMASK : 0);
1282
/* store first fractional byte */
1283
if (destbitsleft > srcbitsleft)
1285
*r++ = (bits8) ((a >> (srcbitsleft - 8)) & BITMASK);
1288
/* Now srcbitsleft and destbitsleft are the same, need not track both */
1289
/* store whole bytes */
1290
while (destbitsleft >= 8)
1292
*r++ = (bits8) ((a >> (destbitsleft - 8)) & BITMASK);
1295
/* store last fractional byte */
1296
if (destbitsleft > 0)
1297
*r = (bits8) ((a << (8 - destbitsleft)) & BITMASK);
1299
PG_RETURN_VARBIT_P(result);
1303
bittoint8(PG_FUNCTION_ARGS)
1305
VarBit *arg = PG_GETARG_VARBIT_P(0);
1309
/* Check that the bit string is not too long */
1310
if (VARBITLEN(arg) > sizeof(result) * BITS_PER_BYTE)
1312
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1313
errmsg("bigint out of range")));
1316
for (r = VARBITS(arg); r < VARBITEND(arg); r++)
1318
result <<= BITS_PER_BYTE;
1321
/* Now shift the result to take account of the padding at the end */
1322
result >>= VARBITPAD(arg);
1324
PG_RETURN_INT64(result);
1328
/* Determines the position of S2 in the bitstring S1 (1-based string).
1329
* If S2 does not appear in S1 this function returns 0.
1330
* If S2 is of length 0 this function returns 1.
1331
* Compatible in usage with POSITION() functions for other data types.
1334
bitposition(PG_FUNCTION_ARGS)
1336
VarBit *str = PG_GETARG_VARBIT_P(0);
1337
VarBit *substr = PG_GETARG_VARBIT_P(1);
1342
bits8 *s, /* pointer into substring */
1343
*p; /* pointer into str */
1344
bits8 cmp, /* shifted substring byte to compare */
1345
mask1, /* mask for substring byte shifted right */
1346
mask2, /* mask for substring byte shifted left */
1347
end_mask, /* pad mask for last substring byte */
1348
str_mask; /* pad mask for last string byte */
1351
/* Get the substring length */
1352
substr_length = VARBITLEN(substr);
1353
str_length = VARBITLEN(str);
1355
/* String has zero length or substring longer than string, return 0 */
1356
if ((str_length == 0) || (substr_length > str_length))
1359
/* zero-length substring means return 1 */
1360
if (substr_length == 0)
1363
/* Initialise the padding masks */
1364
end_mask = BITMASK << VARBITPAD(substr);
1365
str_mask = BITMASK << VARBITPAD(str);
1366
for (i = 0; i < VARBITBYTES(str) - VARBITBYTES(substr) + 1; i++)
1368
for (is = 0; is < BITS_PER_BYTE; is++)
1371
p = VARBITS(str) + i;
1372
mask1 = BITMASK >> is;
1374
for (s = VARBITS(substr);
1375
is_match && s < VARBITEND(substr); s++)
1378
if (s == VARBITEND(substr) - 1)
1380
mask1 &= end_mask >> is;
1381
if (p == VARBITEND(str) - 1)
1383
/* Check that there is enough of str left */
1384
if (mask1 & ~str_mask)
1392
is_match = ((cmp ^ *p) & mask1) == 0;
1395
/* Move on to the next byte */
1397
if (p == VARBITEND(str))
1399
mask2 = end_mask << (BITS_PER_BYTE - is);
1400
is_match = mask2 == 0;
1402
elog(DEBUG4, "S. %d %d em=%2x sm=%2x r=%d",
1403
i, is, end_mask, mask2, is_match);
1407
cmp = *s << (BITS_PER_BYTE - is);
1408
if (s == VARBITEND(substr) - 1)
1410
mask2 &= end_mask << (BITS_PER_BYTE - is);
1411
if (p == VARBITEND(str) - 1)
1413
if (mask2 & ~str_mask)
1421
is_match = ((cmp ^ *p) & mask2) == 0;
1423
/* Have we found a match? */
1425
PG_RETURN_INT32(i * BITS_PER_BYTE + is + 1);