LCOV - code coverage report
Current view: top level - src/backend/utils/adt - varbit.c (source / functions) Hit Total Coverage
Test: PostgreSQL 13beta1 Lines: 570 672 84.8 %
Date: 2020-06-01 10:07:15 Functions: 43 48 89.6 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.13