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