PostgreSQL Source Code git master
Loading...
Searching...
No Matches
varbit.c File Reference
#include "postgres.h"
#include "access/htup_details.h"
#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/nodeFuncs.h"
#include "nodes/supportnodes.h"
#include "port/pg_bitutils.h"
#include "utils/array.h"
#include "utils/fmgrprotos.h"
#include "utils/varbit.h"
Include dependency graph for varbit.c:

Go to the source code of this file.

Macros

#define HEXDIG(z)   ((z)<10 ? ((z)+'0') : ((z)-10+'A'))
 
#define VARBIT_PAD(vb)
 
#define VARBIT_PAD_LAST(vb, ptr)
 
#define VARBIT_CORRECTLY_PADDED(vb)   ((void) 0)
 

Functions

static VarBitbit_catenate (VarBit *arg1, VarBit *arg2)
 
static VarBitbitsubstring (VarBit *arg, int32 s, int32 l, bool length_not_specified)
 
static VarBitbit_overlay (VarBit *t1, VarBit *t2, int sp, int sl)
 
static int32 anybit_typmodin (ArrayType *ta, const char *typename)
 
static charanybit_typmodout (int32 typmod)
 
Datum bit_in (PG_FUNCTION_ARGS)
 
Datum bit_out (PG_FUNCTION_ARGS)
 
Datum bit_recv (PG_FUNCTION_ARGS)
 
Datum bit_send (PG_FUNCTION_ARGS)
 
Datum bit (PG_FUNCTION_ARGS)
 
Datum bittypmodin (PG_FUNCTION_ARGS)
 
Datum bittypmodout (PG_FUNCTION_ARGS)
 
Datum varbit_in (PG_FUNCTION_ARGS)
 
Datum varbit_out (PG_FUNCTION_ARGS)
 
Datum varbit_recv (PG_FUNCTION_ARGS)
 
Datum varbit_send (PG_FUNCTION_ARGS)
 
Datum varbit_support (PG_FUNCTION_ARGS)
 
Datum varbit (PG_FUNCTION_ARGS)
 
Datum varbittypmodin (PG_FUNCTION_ARGS)
 
Datum varbittypmodout (PG_FUNCTION_ARGS)
 
static int32 bit_cmp (VarBit *arg1, VarBit *arg2)
 
Datum biteq (PG_FUNCTION_ARGS)
 
Datum bitne (PG_FUNCTION_ARGS)
 
Datum bitlt (PG_FUNCTION_ARGS)
 
Datum bitle (PG_FUNCTION_ARGS)
 
Datum bitgt (PG_FUNCTION_ARGS)
 
Datum bitge (PG_FUNCTION_ARGS)
 
Datum bitcmp (PG_FUNCTION_ARGS)
 
Datum bitcat (PG_FUNCTION_ARGS)
 
Datum bitsubstr (PG_FUNCTION_ARGS)
 
Datum bitsubstr_no_len (PG_FUNCTION_ARGS)
 
Datum bitoverlay (PG_FUNCTION_ARGS)
 
Datum bitoverlay_no_len (PG_FUNCTION_ARGS)
 
Datum bit_bit_count (PG_FUNCTION_ARGS)
 
Datum bitlength (PG_FUNCTION_ARGS)
 
Datum bitoctetlength (PG_FUNCTION_ARGS)
 
Datum bit_and (PG_FUNCTION_ARGS)
 
Datum bit_or (PG_FUNCTION_ARGS)
 
Datum bitxor (PG_FUNCTION_ARGS)
 
Datum bitnot (PG_FUNCTION_ARGS)
 
Datum bitshiftleft (PG_FUNCTION_ARGS)
 
Datum bitshiftright (PG_FUNCTION_ARGS)
 
Datum bitfromint4 (PG_FUNCTION_ARGS)
 
Datum bittoint4 (PG_FUNCTION_ARGS)
 
Datum bitfromint8 (PG_FUNCTION_ARGS)
 
Datum bittoint8 (PG_FUNCTION_ARGS)
 
Datum bitposition (PG_FUNCTION_ARGS)
 
Datum bitsetbit (PG_FUNCTION_ARGS)
 
Datum bitgetbit (PG_FUNCTION_ARGS)
 

Macro Definition Documentation

◆ HEXDIG

#define HEXDIG (   z)    ((z)<10 ? ((z)+'0') : ((z)-10+'A'))

Definition at line 44 of file varbit.c.

◆ VARBIT_CORRECTLY_PADDED

#define VARBIT_CORRECTLY_PADDED (   vb)    ((void) 0)

Definition at line 77 of file varbit.c.

◆ VARBIT_PAD

#define VARBIT_PAD (   vb)
Value:
do { \
if (pad_ > 0) \
*(VARBITS(vb) + VARBITBYTES(vb) - 1) &= BITMASK << pad_; \
} while (0)
int32_t int32
Definition c.h:679
#define BITS_PER_BYTE
static int fb(int x)
#define VARBITBYTES(PTR)
Definition varbit.h:73
#define VARBITS(PTR)
Definition varbit.h:71
#define BITMASK
Definition varbit.h:87
#define VARBITPAD(PTR)
Definition varbit.h:75

Definition at line 47 of file varbit.c.

48 { \
51 if (pad_ > 0) \
52 *(VARBITS(vb) + VARBITBYTES(vb) - 1) &= BITMASK << pad_; \
53 } while (0)

◆ VARBIT_PAD_LAST

#define VARBIT_PAD_LAST (   vb,
  ptr 
)
Value:
do { \
if (pad_ > 0) \
*((ptr) - 1) &= BITMASK << pad_; \
} while (0)

Definition at line 59 of file varbit.c.

60 { \
63 if (pad_ > 0) \
64 *((ptr) - 1) &= BITMASK << pad_; \
65 } while (0)

Function Documentation

◆ anybit_typmodin()

static int32 anybit_typmodin ( ArrayType ta,
const char typename 
)
static

Definition at line 90 of file varbit.c.

91{
92 int32 typmod;
93 int32 *tl;
94 int n;
95
97
98 /*
99 * we're not too tense about good error message here because grammar
100 * shouldn't allow wrong number of modifiers for BIT
101 */
102 if (n != 1)
105 errmsg("invalid type modifier")));
106
107 if (*tl < 1)
110 errmsg("length for type %s must be at least 1",
111 typename)));
112 if (*tl > (MaxAttrSize * BITS_PER_BYTE))
115 errmsg("length for type %s cannot exceed %d",
116 typename, MaxAttrSize * BITS_PER_BYTE)));
117
118 typmod = *tl;
119
120 return typmod;
121}
int32 * ArrayGetIntegerTypmods(ArrayType *arr, int *n)
Definition arrayutils.c:233
int errcode(int sqlerrcode)
Definition elog.c:875
#define ERROR
Definition elog.h:40
#define ereport(elevel,...)
Definition elog.h:152
#define MaxAttrSize
static char * errmsg

References ArrayGetIntegerTypmods(), BITS_PER_BYTE, ereport, errcode(), errmsg, ERROR, fb(), and MaxAttrSize.

Referenced by bittypmodin(), and varbittypmodin().

◆ anybit_typmodout()

static char * anybit_typmodout ( int32  typmod)
static

Definition at line 127 of file varbit.c.

128{
129 char *res = (char *) palloc(64);
130
131 if (typmod >= 0)
132 snprintf(res, 64, "(%d)", typmod);
133 else
134 *res = '\0';
135
136 return res;
137}
void * palloc(Size size)
Definition mcxt.c:1390
#define snprintf
Definition port.h:261

References palloc(), and snprintf.

Referenced by bittypmodout(), and varbittypmodout().

◆ bit()

Definition at line 391 of file varbit.c.

392{
395 bool isExplicit = PG_GETARG_BOOL(2);
396 VarBit *result;
397 int rlen;
398
399 /* No work if typmod is invalid or supplied data matches it already */
402
403 if (!isExplicit)
404 ereturn(fcinfo->context, (Datum) 0,
406 errmsg("bit string length %d does not match type bit(%d)",
407 VARBITLEN(arg), len)));
408
410 /* set to 0 so that string is zero-padded */
411 result = (VarBit *) palloc0(rlen);
414
417
418 /*
419 * Make sure last byte is zero-padded if needed. This is useless but safe
420 * if source data was shorter than target length (we assume the last byte
421 * of the source data was itself correctly zero-padded).
422 */
424
426}
#define Min(x, y)
Definition c.h:1131
uint32 result
memcpy(sums, checksumBaseOffsets, sizeof(checksumBaseOffsets))
Datum arg
Definition elog.c:1323
#define ereturn(context, dummy_value,...)
Definition elog.h:280
#define PG_GETARG_INT32(n)
Definition fmgr.h:269
#define PG_GETARG_BOOL(n)
Definition fmgr.h:274
void * palloc0(Size size)
Definition mcxt.c:1420
const void size_t len
uint64_t Datum
Definition postgres.h:70
static void SET_VARSIZE(void *PTR, Size len)
Definition varatt.h:432
#define VARBIT_PAD(vb)
Definition varbit.c:47
#define VARBITLEN(PTR)
Definition varbit.h:69
#define PG_RETURN_VARBIT_P(x)
Definition varbit.h:64
#define PG_GETARG_VARBIT_P(n)
Definition varbit.h:62
#define VARBITMAXLEN
Definition varbit.h:83
#define VARBITTOTALLEN(BITLEN)
Definition varbit.h:77

References arg, ereturn, errcode(), errmsg, fb(), len, memcpy(), Min, palloc0(), PG_GETARG_BOOL, PG_GETARG_INT32, PG_GETARG_VARBIT_P, PG_RETURN_VARBIT_P, result, SET_VARSIZE(), VARBIT_PAD, VARBITBYTES, VARBITLEN, VARBITMAXLEN, VARBITS, and VARBITTOTALLEN.

Referenced by _hash_addovflpage(), bloom_add_value(), bloom_contains_value(), build_test_info_result(), fill_val(), hash_bitmap_info(), heap_fill_tuple(), RT_GROW_NODE_16(), RT_GROW_NODE_48(), and show_incremental_sort_group_info().

◆ bit_and()

Datum bit_and ( PG_FUNCTION_ARGS  )

Definition at line 1243 of file varbit.c.

1244{
1247 VarBit *result;
1248 int len,
1249 bitlen1,
1250 bitlen2;
1251 uint8 *p1,
1252 *p2,
1253 *r;
1254
1257 if (bitlen1 != bitlen2)
1258 ereport(ERROR,
1260 errmsg("cannot AND bit strings of different sizes")));
1261
1262 len = VARSIZE(arg1);
1263 result = (VarBit *) palloc(len);
1266
1267 p1 = VARBITS(arg1);
1268 p2 = VARBITS(arg2);
1269 r = VARBITS(result);
1270 for (size_t i = 0; i < VARBITBYTES(arg1); i++)
1271 *r++ = *p1++ & *p2++;
1272
1273 /* Padding is not needed as & of 0 pads is 0 */
1274
1276}
uint8_t uint8
Definition c.h:681
int i
Definition isn.c:77
static Size VARSIZE(const void *PTR)
Definition varatt.h:298

References ereport, errcode(), errmsg, ERROR, fb(), i, len, palloc(), PG_GETARG_VARBIT_P, PG_RETURN_VARBIT_P, result, SET_VARSIZE(), VARBITBYTES, VARBITLEN, VARBITS, and VARSIZE().

◆ bit_bit_count()

Datum bit_bit_count ( PG_FUNCTION_ARGS  )

Definition at line 1211 of file varbit.c.

1212{
1214
1216}
#define PG_RETURN_INT64(x)
Definition fmgr.h:370
static uint64 pg_popcount(const char *buf, int bytes)

References arg, PG_GETARG_VARBIT_P, pg_popcount(), PG_RETURN_INT64, VARBITBYTES, and VARBITS.

◆ bit_catenate()

static VarBit * bit_catenate ( VarBit arg1,
VarBit arg2 
)
static

Definition at line 977 of file varbit.c.

978{
979 VarBit *result;
980 int bitlen1,
981 bitlen2,
982 bytelen,
983 bit1pad,
984 bit2shift;
985 uint8 *pr,
986 *pa;
987
990
994 errmsg("bit string length exceeds the maximum allowed (%d)",
995 VARBITMAXLEN)));
997
1001
1002 /* Copy the first bitstring in */
1004
1005 /* Copy the second bit string */
1007 if (bit1pad == 0)
1008 {
1010 VARBITBYTES(arg2));
1011 }
1012 else if (bitlen2 > 0)
1013 {
1014 /* We need to shift all the bits to fit */
1016 pr = VARBITS(result) + VARBITBYTES(arg1) - 1;
1017 for (pa = VARBITS(arg2); pa < VARBITEND(arg2); pa++)
1018 {
1019 *pr |= ((*pa >> bit2shift) & BITMASK);
1020 pr++;
1021 if (pr < VARBITEND(result))
1022 *pr = (*pa << bit1pad) & BITMASK;
1023 }
1024 }
1025
1026 /* The pad bits should be already zero at this point */
1027
1028 return result;
1029}
#define VARBITEND(PTR)
Definition varbit.h:85

References BITMASK, BITS_PER_BYTE, ereport, errcode(), errmsg, ERROR, fb(), memcpy(), palloc(), result, SET_VARSIZE(), VARBITBYTES, VARBITEND, VARBITLEN, VARBITMAXLEN, VARBITPAD, VARBITS, and VARBITTOTALLEN.

Referenced by bit_overlay(), and bitcat().

◆ bit_cmp()

static int32 bit_cmp ( VarBit arg1,
VarBit arg2 
)
static

Definition at line 818 of file varbit.c.

819{
820 int bitlen1,
821 bytelen1,
822 bitlen2,
823 bytelen2;
824 int32 cmp;
825
828
830 if (cmp == 0)
831 {
834 if (bitlen1 != bitlen2)
835 cmp = (bitlen1 < bitlen2) ? -1 : 1;
836 }
837 return cmp;
838}
static int cmp(const chr *x, const chr *y, size_t len)

References cmp(), fb(), Min, VARBITBYTES, VARBITLEN, and VARBITS.

Referenced by bitcmp(), biteq(), bitge(), bitgt(), bitle(), bitlt(), and bitne().

◆ bit_in()

Datum bit_in ( PG_FUNCTION_ARGS  )

Definition at line 147 of file varbit.c.

148{
150#ifdef NOT_USED
151 Oid typelem = PG_GETARG_OID(1);
152#endif
153 int32 atttypmod = PG_GETARG_INT32(2);
154 Node *escontext = fcinfo->context;
155 VarBit *result; /* The resulting bit string */
156 char *sp; /* pointer into the character string */
157 uint8 *r; /* pointer into the result */
158 int len, /* Length of the whole data structure */
159 bitlen, /* Number of bits in the bit string */
160 slen; /* Length of the input string */
161 bool bit_not_hex; /* false = hex string true = bit string */
162 int bc;
163 uint8 x = 0;
164
165 /* Check that the first character is a b or an x */
166 if (input_string[0] == 'b' || input_string[0] == 'B')
167 {
168 bit_not_hex = true;
169 sp = input_string + 1;
170 }
171 else if (input_string[0] == 'x' || input_string[0] == 'X')
172 {
173 bit_not_hex = false;
174 sp = input_string + 1;
175 }
176 else
177 {
178 /*
179 * Otherwise it's binary. This allows things like cast('1001' as bit)
180 * to work transparently.
181 */
182 bit_not_hex = true;
184 }
185
186 /*
187 * Determine bitlength from input string. MaxAllocSize ensures a regular
188 * input is small enough, but we must check hex input.
189 */
190 slen = strlen(sp);
191 if (bit_not_hex)
192 bitlen = slen;
193 else
194 {
195 if (slen > VARBITMAXLEN / 4)
196 ereturn(escontext, (Datum) 0,
198 errmsg("bit string length exceeds the maximum allowed (%d)",
199 VARBITMAXLEN)));
200 bitlen = slen * 4;
201 }
202
203 /*
204 * Sometimes atttypmod is not supplied. If it is supplied we need to make
205 * sure that the bitstring fits.
206 */
207 if (atttypmod <= 0)
208 atttypmod = bitlen;
209 else if (bitlen != atttypmod)
210 ereturn(escontext, (Datum) 0,
212 errmsg("bit string length %d does not match type bit(%d)",
213 bitlen, atttypmod)));
214
215 len = VARBITTOTALLEN(atttypmod);
216 /* set to 0 so that *r is always initialised and string is zero-padded */
217 result = (VarBit *) palloc0(len);
219 VARBITLEN(result) = atttypmod;
220
221 r = VARBITS(result);
222 if (bit_not_hex)
223 {
224 /* Parse the bit representation of the string */
225 /* We know it fits, as bitlen was compared to atttypmod */
226 x = HIGHBIT;
227 for (; *sp; sp++)
228 {
229 if (*sp == '1')
230 *r |= x;
231 else if (*sp != '0')
232 ereturn(escontext, (Datum) 0,
234 errmsg("\"%.*s\" is not a valid binary digit",
235 pg_mblen_cstr(sp), sp)));
236
237 x >>= 1;
238 if (x == 0)
239 {
240 x = HIGHBIT;
241 r++;
242 }
243 }
244 }
245 else
246 {
247 /* Parse the hex representation of the string */
248 for (bc = 0; *sp; sp++)
249 {
250 if (*sp >= '0' && *sp <= '9')
251 x = (uint8) (*sp - '0');
252 else if (*sp >= 'A' && *sp <= 'F')
253 x = (uint8) (*sp - 'A') + 10;
254 else if (*sp >= 'a' && *sp <= 'f')
255 x = (uint8) (*sp - 'a') + 10;
256 else
257 ereturn(escontext, (Datum) 0,
259 errmsg("\"%.*s\" is not a valid hexadecimal digit",
260 pg_mblen_cstr(sp), sp)));
261
262 if (bc)
263 {
264 *r++ |= x;
265 bc = 0;
266 }
267 else
268 {
269 *r = x << 4;
270 bc = 1;
271 }
272 }
273 }
274
276}
#define HIGHBIT
Definition c.h:1283
#define PG_GETARG_OID(n)
Definition fmgr.h:275
#define PG_GETARG_CSTRING(n)
Definition fmgr.h:278
int x
Definition isn.c:75
int pg_mblen_cstr(const char *mbstr)
Definition mbutils.c:1045
unsigned int Oid
Definition nodes.h:133

References ereturn, errcode(), errmsg, fb(), HIGHBIT, len, palloc0(), PG_GETARG_CSTRING, PG_GETARG_INT32, PG_GETARG_OID, pg_mblen_cstr(), PG_RETURN_VARBIT_P, result, SET_VARSIZE(), VARBITLEN, VARBITMAXLEN, VARBITS, VARBITTOTALLEN, and x.

Referenced by leftmostvalue_bit(), and make_const().

◆ bit_or()

Datum bit_or ( PG_FUNCTION_ARGS  )

Definition at line 1283 of file varbit.c.

1284{
1287 VarBit *result;
1288 int len,
1289 bitlen1,
1290 bitlen2;
1291 uint8 *p1,
1292 *p2,
1293 *r;
1294
1297 if (bitlen1 != bitlen2)
1298 ereport(ERROR,
1300 errmsg("cannot OR bit strings of different sizes")));
1301 len = VARSIZE(arg1);
1302 result = (VarBit *) palloc(len);
1305
1306 p1 = VARBITS(arg1);
1307 p2 = VARBITS(arg2);
1308 r = VARBITS(result);
1309 for (size_t i = 0; i < VARBITBYTES(arg1); i++)
1310 *r++ = *p1++ | *p2++;
1311
1312 /* Padding is not needed as | of 0 pads is 0 */
1313
1315}

References ereport, errcode(), errmsg, ERROR, fb(), i, len, palloc(), PG_GETARG_VARBIT_P, PG_RETURN_VARBIT_P, result, SET_VARSIZE(), VARBITBYTES, VARBITLEN, VARBITS, and VARSIZE().

◆ bit_out()

Datum bit_out ( PG_FUNCTION_ARGS  )

Definition at line 280 of file varbit.c.

281{
282#if 1
283 /* same as varbit output */
284 return varbit_out(fcinfo);
285#else
286
287 /*
288 * This is how one would print a hex string, in case someone wants to
289 * write a formatting function.
290 */
292 char *result,
293 *r;
294 uint8 *sp;
295 int i,
296 len,
297 bitlen;
298
299 /* Assertion to help catch any bit functions that don't pad correctly */
301
302 bitlen = VARBITLEN(s);
303 len = (bitlen + 3) / 4;
304 result = (char *) palloc(len + 2);
305 sp = VARBITS(s);
306 r = result;
307 *r++ = 'X';
308 /* we cheat by knowing that we store full bytes zero padded */
309 for (i = 0; i < len; i += 2, sp++)
310 {
311 *r++ = HEXDIG((*sp) >> 4);
312 *r++ = HEXDIG((*sp) & 0xF);
313 }
314
315 /*
316 * Go back one step if we printed a hex number that was not part of the
317 * bitstring anymore
318 */
319 if (i > len)
320 r--;
321 *r = '\0';
322
324#endif
325}
#define PG_RETURN_CSTRING(x)
Definition fmgr.h:364
#define VARBIT_CORRECTLY_PADDED(vb)
Definition varbit.c:77
Datum varbit_out(PG_FUNCTION_ARGS)
Definition varbit.c:587
#define HEXDIG(z)
Definition varbit.c:44

References fb(), HEXDIG, i, len, palloc(), PG_GETARG_VARBIT_P, PG_RETURN_CSTRING, result, VARBIT_CORRECTLY_PADDED, varbit_out(), VARBITLEN, and VARBITS.

◆ bit_overlay()

static VarBit * bit_overlay ( VarBit t1,
VarBit t2,
int  sp,
int  sl 
)
static

Definition at line 1176 of file varbit.c.

1177{
1178 VarBit *result;
1179 VarBit *s1;
1180 VarBit *s2;
1181 int sp_pl_sl;
1182
1183 /*
1184 * Check for possible integer-overflow cases. For negative sp, throw a
1185 * "substring length" error because that's what should be expected
1186 * according to the spec's definition of OVERLAY().
1187 */
1188 if (sp <= 0)
1189 ereport(ERROR,
1191 errmsg("negative substring length not allowed")));
1193 ereport(ERROR,
1195 errmsg("integer out of range")));
1196
1197 s1 = bitsubstring(t1, 1, sp - 1, false);
1198 s2 = bitsubstring(t1, sp_pl_sl, -1, true);
1201
1202 return result;
1203}
static bool pg_add_s32_overflow(int32 a, int32 b, int32 *result)
Definition int.h:151
char * s1
char * s2
static VarBit * bit_catenate(VarBit *arg1, VarBit *arg2)
Definition varbit.c:977
static VarBit * bitsubstring(VarBit *arg, int32 s, int32 l, bool length_not_specified)
Definition varbit.c:1055

References bit_catenate(), bitsubstring(), ereport, errcode(), errmsg, ERROR, fb(), pg_add_s32_overflow(), result, s1, and s2.

Referenced by bitoverlay(), and bitoverlay_no_len().

◆ bit_recv()

Datum bit_recv ( PG_FUNCTION_ARGS  )

Definition at line 331 of file varbit.c.

332{
334
335#ifdef NOT_USED
336 Oid typelem = PG_GETARG_OID(1);
337#endif
338 int32 atttypmod = PG_GETARG_INT32(2);
339 VarBit *result;
340 int len,
341 bitlen;
342
343 bitlen = pq_getmsgint(buf, sizeof(int32));
347 errmsg("invalid length in external bit string")));
348
349 /*
350 * Sometimes atttypmod is not supplied. If it is supplied we need to make
351 * sure that the bitstring fits.
352 */
353 if (atttypmod > 0 && bitlen != atttypmod)
356 errmsg("bit string length %d does not match type bit(%d)",
357 bitlen, atttypmod)));
358
360 result = (VarBit *) palloc(len);
363
365
366 /* Make sure last byte is correctly zero-padded */
368
370}
#define PG_GETARG_POINTER(n)
Definition fmgr.h:277
static char buf[DEFAULT_XLOG_SEG_SIZE]
unsigned int pq_getmsgint(StringInfo msg, int b)
Definition pqformat.c:414
void pq_copymsgbytes(StringInfo msg, void *buf, int datalen)
Definition pqformat.c:527
struct StringInfoData * StringInfo
Definition string.h:15

References buf, ereport, errcode(), errmsg, ERROR, fb(), len, palloc(), PG_GETARG_INT32, PG_GETARG_OID, PG_GETARG_POINTER, PG_RETURN_VARBIT_P, pq_copymsgbytes(), pq_getmsgint(), result, SET_VARSIZE(), VARBIT_PAD, VARBITBYTES, VARBITLEN, VARBITMAXLEN, VARBITS, and VARBITTOTALLEN.

◆ bit_send()

Datum bit_send ( PG_FUNCTION_ARGS  )

Definition at line 376 of file varbit.c.

377{
378 /* Exactly the same as varbit_send, so share code */
379 return varbit_send(fcinfo);
380}
Datum varbit_send(PG_FUNCTION_ARGS)
Definition varbit.c:681

References varbit_send().

◆ bitcat()

Datum bitcat ( PG_FUNCTION_ARGS  )

Definition at line 968 of file varbit.c.

References bit_catenate(), fb(), PG_GETARG_VARBIT_P, and PG_RETURN_VARBIT_P.

◆ bitcmp()

Datum bitcmp ( PG_FUNCTION_ARGS  )

Definition at line 949 of file varbit.c.

950{
954
956
959
961}
#define PG_FREE_IF_COPY(ptr, n)
Definition fmgr.h:260
#define PG_RETURN_INT32(x)
Definition fmgr.h:355
static int32 bit_cmp(VarBit *arg1, VarBit *arg2)
Definition varbit.c:818

References bit_cmp(), fb(), PG_FREE_IF_COPY, PG_GETARG_VARBIT_P, PG_RETURN_INT32, and result.

Referenced by gbt_bit_ssup_cmp().

◆ biteq()

Datum biteq ( PG_FUNCTION_ARGS  )

Definition at line 841 of file varbit.c.

842{
845 bool result;
846 int bitlen1,
847 bitlen2;
848
851
852 /* fast path for different-length inputs */
853 if (bitlen1 != bitlen2)
854 result = false;
855 else
856 result = (bit_cmp(arg1, arg2) == 0);
857
860
862}
#define PG_RETURN_BOOL(x)
Definition fmgr.h:360

References bit_cmp(), fb(), PG_FREE_IF_COPY, PG_GETARG_VARBIT_P, PG_RETURN_BOOL, result, and VARBITLEN.

Referenced by gbt_biteq().

◆ bitfromint4()

Datum bitfromint4 ( PG_FUNCTION_ARGS  )

Definition at line 1528 of file varbit.c.

1529{
1530 int32 a = PG_GETARG_INT32(0);
1531 int32 typmod = PG_GETARG_INT32(1);
1532 VarBit *result;
1533 uint8 *r;
1534 int rlen;
1535 int destbitsleft,
1537
1539 typmod = 1; /* default bit length */
1540
1541 rlen = VARBITTOTALLEN(typmod);
1542 result = (VarBit *) palloc(rlen);
1544 VARBITLEN(result) = typmod;
1545
1546 r = VARBITS(result);
1547 destbitsleft = typmod;
1548 srcbitsleft = 32;
1549 /* drop any input bits that don't fit */
1551 /* sign-fill any excess bytes in output */
1552 while (destbitsleft >= srcbitsleft + 8)
1553 {
1554 *r++ = (uint8) ((a < 0) ? BITMASK : 0);
1555 destbitsleft -= 8;
1556 }
1557 /* store first fractional byte */
1559 {
1560 unsigned int val = (unsigned int) (a >> (destbitsleft - 8));
1561
1562 /* Force sign-fill in case the compiler implements >> as zero-fill */
1563 if (a < 0)
1564 val |= ((unsigned int) -1) << (srcbitsleft + 8 - destbitsleft);
1565 *r++ = (uint8) (val & BITMASK);
1566 destbitsleft -= 8;
1567 }
1568 /* Now srcbitsleft and destbitsleft are the same, need not track both */
1569 /* store whole bytes */
1570 while (destbitsleft >= 8)
1571 {
1572 *r++ = (uint8) ((a >> (destbitsleft - 8)) & BITMASK);
1573 destbitsleft -= 8;
1574 }
1575 /* store last fractional byte */
1576 if (destbitsleft > 0)
1577 *r = (uint8) ((a << (8 - destbitsleft)) & BITMASK);
1578
1580}
long val
Definition informix.c:689
int a
Definition isn.c:73

References a, BITMASK, fb(), Min, palloc(), PG_GETARG_INT32, PG_RETURN_VARBIT_P, result, SET_VARSIZE(), val, VARBITLEN, VARBITMAXLEN, VARBITS, and VARBITTOTALLEN.

◆ bitfromint8()

Datum bitfromint8 ( PG_FUNCTION_ARGS  )

Definition at line 1608 of file varbit.c.

1609{
1610 int64 a = PG_GETARG_INT64(0);
1611 int32 typmod = PG_GETARG_INT32(1);
1612 VarBit *result;
1613 uint8 *r;
1614 int rlen;
1615 int destbitsleft,
1617
1619 typmod = 1; /* default bit length */
1620
1621 rlen = VARBITTOTALLEN(typmod);
1622 result = (VarBit *) palloc(rlen);
1624 VARBITLEN(result) = typmod;
1625
1626 r = VARBITS(result);
1627 destbitsleft = typmod;
1628 srcbitsleft = 64;
1629 /* drop any input bits that don't fit */
1631 /* sign-fill any excess bytes in output */
1632 while (destbitsleft >= srcbitsleft + 8)
1633 {
1634 *r++ = (uint8) ((a < 0) ? BITMASK : 0);
1635 destbitsleft -= 8;
1636 }
1637 /* store first fractional byte */
1639 {
1640 unsigned int val = (unsigned int) (a >> (destbitsleft - 8));
1641
1642 /* Force sign-fill in case the compiler implements >> as zero-fill */
1643 if (a < 0)
1644 val |= ((unsigned int) -1) << (srcbitsleft + 8 - destbitsleft);
1645 *r++ = (uint8) (val & BITMASK);
1646 destbitsleft -= 8;
1647 }
1648 /* Now srcbitsleft and destbitsleft are the same, need not track both */
1649 /* store whole bytes */
1650 while (destbitsleft >= 8)
1651 {
1652 *r++ = (uint8) ((a >> (destbitsleft - 8)) & BITMASK);
1653 destbitsleft -= 8;
1654 }
1655 /* store last fractional byte */
1656 if (destbitsleft > 0)
1657 *r = (uint8) ((a << (8 - destbitsleft)) & BITMASK);
1658
1660}
int64_t int64
Definition c.h:680
#define PG_GETARG_INT64(n)
Definition fmgr.h:284

References a, BITMASK, fb(), Min, palloc(), PG_GETARG_INT32, PG_GETARG_INT64, PG_RETURN_VARBIT_P, result, SET_VARSIZE(), val, VARBITLEN, VARBITMAXLEN, VARBITS, and VARBITTOTALLEN.

◆ bitge()

Datum bitge ( PG_FUNCTION_ARGS  )

Definition at line 934 of file varbit.c.

935{
938 bool result;
939
940 result = (bit_cmp(arg1, arg2) >= 0);
941
944
946}

References bit_cmp(), fb(), PG_FREE_IF_COPY, PG_GETARG_VARBIT_P, PG_RETURN_BOOL, and result.

Referenced by gbt_bitge().

◆ bitgetbit()

Datum bitgetbit ( PG_FUNCTION_ARGS  )

Definition at line 1865 of file varbit.c.

1866{
1868 int32 n = PG_GETARG_INT32(1);
1869 int bitlen;
1870 uint8 *p;
1871 int byteNo,
1872 bitNo;
1873
1875 if (n < 0 || n >= bitlen)
1876 ereport(ERROR,
1878 errmsg("bit index %d out of valid range (0..%d)",
1879 n, bitlen - 1)));
1880
1881 p = VARBITS(arg1);
1882
1883 byteNo = n / BITS_PER_BYTE;
1884 bitNo = BITS_PER_BYTE - 1 - (n % BITS_PER_BYTE);
1885
1886 if (p[byteNo] & (1 << bitNo))
1887 PG_RETURN_INT32(1);
1888 else
1889 PG_RETURN_INT32(0);
1890}

References BITS_PER_BYTE, ereport, errcode(), errmsg, ERROR, fb(), PG_GETARG_INT32, PG_GETARG_VARBIT_P, PG_RETURN_INT32, VARBITLEN, and VARBITS.

◆ bitgt()

Datum bitgt ( PG_FUNCTION_ARGS  )

Definition at line 919 of file varbit.c.

920{
923 bool result;
924
925 result = (bit_cmp(arg1, arg2) > 0);
926
929
931}

References bit_cmp(), fb(), PG_FREE_IF_COPY, PG_GETARG_VARBIT_P, PG_RETURN_BOOL, and result.

Referenced by gbt_bitgt().

◆ bitle()

Datum bitle ( PG_FUNCTION_ARGS  )

Definition at line 904 of file varbit.c.

905{
908 bool result;
909
910 result = (bit_cmp(arg1, arg2) <= 0);
911
914
916}

References bit_cmp(), fb(), PG_FREE_IF_COPY, PG_GETARG_VARBIT_P, PG_RETURN_BOOL, and result.

Referenced by gbt_bitle().

◆ bitlength()

Datum bitlength ( PG_FUNCTION_ARGS  )

Definition at line 1223 of file varbit.c.

1224{
1226
1228}

References arg, PG_GETARG_VARBIT_P, PG_RETURN_INT32, and VARBITLEN.

◆ bitlt()

Datum bitlt ( PG_FUNCTION_ARGS  )

Definition at line 889 of file varbit.c.

890{
893 bool result;
894
895 result = (bit_cmp(arg1, arg2) < 0);
896
899
901}

References bit_cmp(), fb(), PG_FREE_IF_COPY, PG_GETARG_VARBIT_P, PG_RETURN_BOOL, and result.

Referenced by gbt_bitlt().

◆ bitne()

Datum bitne ( PG_FUNCTION_ARGS  )

Definition at line 865 of file varbit.c.

866{
869 bool result;
870 int bitlen1,
871 bitlen2;
872
875
876 /* fast path for different-length inputs */
877 if (bitlen1 != bitlen2)
878 result = true;
879 else
880 result = (bit_cmp(arg1, arg2) != 0);
881
884
886}

References bit_cmp(), fb(), PG_FREE_IF_COPY, PG_GETARG_VARBIT_P, PG_RETURN_BOOL, result, and VARBITLEN.

◆ bitnot()

Datum bitnot ( PG_FUNCTION_ARGS  )

Definition at line 1362 of file varbit.c.

1363{
1365 VarBit *result;
1366 uint8 *p,
1367 *r;
1368
1369 result = (VarBit *) palloc(VARSIZE(arg));
1372
1373 p = VARBITS(arg);
1374 r = VARBITS(result);
1375 for (; p < VARBITEND(arg); p++)
1376 *r++ = ~*p;
1377
1378 /* Must zero-pad the result, because extra bits are surely 1's here */
1380
1382}
#define VARBIT_PAD_LAST(vb, ptr)
Definition varbit.c:59

References arg, palloc(), PG_GETARG_VARBIT_P, PG_RETURN_VARBIT_P, result, SET_VARSIZE(), VARBIT_PAD_LAST, VARBITEND, VARBITLEN, VARBITS, and VARSIZE().

◆ bitoctetlength()

Datum bitoctetlength ( PG_FUNCTION_ARGS  )

Definition at line 1231 of file varbit.c.

1232{
1234
1236}

References arg, PG_GETARG_VARBIT_P, PG_RETURN_INT32, and VARBITBYTES.

◆ bitoverlay()

Datum bitoverlay ( PG_FUNCTION_ARGS  )

Definition at line 1153 of file varbit.c.

1154{
1157 int sp = PG_GETARG_INT32(2); /* substring start position */
1158 int sl = PG_GETARG_INT32(3); /* substring length */
1159
1161}
static VarBit * bit_overlay(VarBit *t1, VarBit *t2, int sp, int sl)
Definition varbit.c:1176

References bit_overlay(), fb(), PG_GETARG_INT32, PG_GETARG_VARBIT_P, and PG_RETURN_VARBIT_P.

◆ bitoverlay_no_len()

Datum bitoverlay_no_len ( PG_FUNCTION_ARGS  )

Definition at line 1164 of file varbit.c.

1165{
1168 int sp = PG_GETARG_INT32(2); /* substring start position */
1169 int sl;
1170
1171 sl = VARBITLEN(t2); /* defaults to length(t2) */
1173}

References bit_overlay(), fb(), PG_GETARG_INT32, PG_GETARG_VARBIT_P, PG_RETURN_VARBIT_P, and VARBITLEN.

◆ bitposition()

Datum bitposition ( PG_FUNCTION_ARGS  )

Definition at line 1695 of file varbit.c.

1696{
1699 int substr_length,
1700 str_length,
1701 is;
1702 uint8 *s, /* pointer into substring */
1703 *p; /* pointer into str */
1704 uint8 cmp, /* shifted substring byte to compare */
1705 mask1, /* mask for substring byte shifted right */
1706 mask2, /* mask for substring byte shifted left */
1707 end_mask, /* pad mask for last substring byte */
1708 str_mask; /* pad mask for last string byte */
1709 bool is_match;
1710
1711 /* Get the substring length */
1714
1715 /* String has zero length or substring longer than string, return 0 */
1716 if ((str_length == 0) || (substr_length > str_length))
1717 PG_RETURN_INT32(0);
1718
1719 /* zero-length substring means return 1 */
1720 if (substr_length == 0)
1721 PG_RETURN_INT32(1);
1722
1723 /* Initialise the padding masks */
1726 for (size_t i = 0; i < VARBITBYTES(str) - VARBITBYTES(substr) + 1; i++)
1727 {
1728 for (is = 0; is < BITS_PER_BYTE; is++)
1729 {
1730 is_match = true;
1731 p = VARBITS(str) + i;
1732 mask1 = BITMASK >> is;
1733 mask2 = ~mask1;
1734 for (s = VARBITS(substr);
1735 is_match && s < VARBITEND(substr); s++)
1736 {
1737 cmp = *s >> is;
1738 if (s == VARBITEND(substr) - 1)
1739 {
1740 mask1 &= end_mask >> is;
1741 if (p == VARBITEND(str) - 1)
1742 {
1743 /* Check that there is enough of str left */
1744 if (mask1 & ~str_mask)
1745 {
1746 is_match = false;
1747 break;
1748 }
1749 mask1 &= str_mask;
1750 }
1751 }
1752 is_match = ((cmp ^ *p) & mask1) == 0;
1753 if (!is_match)
1754 break;
1755 /* Move on to the next byte */
1756 p++;
1757 if (p == VARBITEND(str))
1758 {
1759 mask2 = end_mask << (BITS_PER_BYTE - is);
1760 is_match = mask2 == 0;
1761#if 0
1762 elog(DEBUG4, "S. %d %d em=%2x sm=%2x r=%d",
1763 i, is, end_mask, mask2, is_match);
1764#endif
1765 break;
1766 }
1767 cmp = *s << (BITS_PER_BYTE - is);
1768 if (s == VARBITEND(substr) - 1)
1769 {
1770 mask2 &= end_mask << (BITS_PER_BYTE - is);
1771 if (p == VARBITEND(str) - 1)
1772 {
1773 if (mask2 & ~str_mask)
1774 {
1775 is_match = false;
1776 break;
1777 }
1778 mask2 &= str_mask;
1779 }
1780 }
1781 is_match = ((cmp ^ *p) & mask2) == 0;
1782 }
1783 /* Have we found a match? */
1784 if (is_match)
1786 }
1787 }
1788 PG_RETURN_INT32(0);
1789}
#define elog(elevel,...)
Definition elog.h:228
#define DEBUG4
Definition elog.h:28
const char * str

References BITMASK, BITS_PER_BYTE, cmp(), DEBUG4, elog, fb(), i, PG_GETARG_VARBIT_P, PG_RETURN_INT32, str, VARBITBYTES, VARBITEND, VARBITLEN, VARBITPAD, and VARBITS.

◆ bitsetbit()

Datum bitsetbit ( PG_FUNCTION_ARGS  )

Definition at line 1803 of file varbit.c.

1804{
1806 int32 n = PG_GETARG_INT32(1);
1808 VarBit *result;
1809 int len,
1810 bitlen;
1811 uint8 *r,
1812 *p;
1813 int byteNo,
1814 bitNo;
1815
1817 if (n < 0 || n >= bitlen)
1818 ereport(ERROR,
1820 errmsg("bit index %d out of valid range (0..%d)",
1821 n, bitlen - 1)));
1822
1823 /*
1824 * sanity check!
1825 */
1826 if (newBit != 0 && newBit != 1)
1827 ereport(ERROR,
1829 errmsg("new bit must be 0 or 1")));
1830
1831 len = VARSIZE(arg1);
1832 result = (VarBit *) palloc(len);
1835
1836 p = VARBITS(arg1);
1837 r = VARBITS(result);
1838
1839 memcpy(r, p, VARBITBYTES(arg1));
1840
1841 byteNo = n / BITS_PER_BYTE;
1842 bitNo = BITS_PER_BYTE - 1 - (n % BITS_PER_BYTE);
1843
1844 /*
1845 * Update the byte.
1846 */
1847 if (newBit == 0)
1848 r[byteNo] &= (~(1 << bitNo));
1849 else
1850 r[byteNo] |= (1 << bitNo);
1851
1853}

References BITS_PER_BYTE, ereport, errcode(), errmsg, ERROR, fb(), len, memcpy(), palloc(), PG_GETARG_INT32, PG_GETARG_VARBIT_P, PG_RETURN_VARBIT_P, result, SET_VARSIZE(), VARBITBYTES, VARBITLEN, VARBITS, and VARSIZE().

◆ bitshiftleft()

Datum bitshiftleft ( PG_FUNCTION_ARGS  )

Definition at line 1389 of file varbit.c.

1390{
1393 VarBit *result;
1394 int byte_shift,
1395 ishift,
1396 len;
1397 uint8 *p,
1398 *r;
1399
1400 /* Negative shift is a shift to the right */
1401 if (shft < 0)
1402 {
1403 /* Prevent integer overflow in negation */
1404 if (shft < -VARBITMAXLEN)
1405 shft = -VARBITMAXLEN;
1408 Int32GetDatum(-shft)));
1409 }
1410
1411 result = (VarBit *) palloc(VARSIZE(arg));
1414 r = VARBITS(result);
1415
1416 /* If we shifted all the bits out, return an all-zero string */
1417 if (shft >= VARBITLEN(arg))
1418 {
1419 MemSet(r, 0, VARBITBYTES(arg));
1421 }
1422
1425 p = VARBITS(arg) + byte_shift;
1426
1427 if (ishift == 0)
1428 {
1429 /* Special case: we can do a memcpy */
1431 memcpy(r, p, len);
1432 MemSet(r + len, 0, byte_shift);
1433 }
1434 else
1435 {
1436 for (; p < VARBITEND(arg); r++)
1437 {
1438 *r = *p << ishift;
1439 if ((++p) < VARBITEND(arg))
1440 *r |= *p >> (BITS_PER_BYTE - ishift);
1441 }
1442 for (; r < VARBITEND(result); r++)
1443 *r = 0;
1444 }
1445
1446 /* The pad bits should be already zero at this point */
1447
1449}
#define MemSet(start, val, len)
Definition c.h:1147
#define DirectFunctionCall2(func, arg1, arg2)
Definition fmgr.h:690
#define PG_RETURN_DATUM(x)
Definition fmgr.h:354
static Datum Int32GetDatum(int32 X)
Definition postgres.h:212
Datum bitshiftright(PG_FUNCTION_ARGS)
Definition varbit.c:1456
static Datum VarBitPGetDatum(const VarBit *X)
Definition varbit.h:57

References arg, BITS_PER_BYTE, bitshiftright(), DirectFunctionCall2, fb(), Int32GetDatum(), len, memcpy(), MemSet, palloc(), PG_GETARG_INT32, PG_GETARG_VARBIT_P, PG_RETURN_DATUM, PG_RETURN_VARBIT_P, result, SET_VARSIZE(), VARBITBYTES, VARBITEND, VARBITLEN, VARBITMAXLEN, VarBitPGetDatum(), VARBITS, and VARSIZE().

Referenced by bitshiftright().

◆ bitshiftright()

Datum bitshiftright ( PG_FUNCTION_ARGS  )

Definition at line 1456 of file varbit.c.

1457{
1460 VarBit *result;
1461 int byte_shift,
1462 ishift,
1463 len;
1464 uint8 *p,
1465 *r;
1466
1467 /* Negative shift is a shift to the left */
1468 if (shft < 0)
1469 {
1470 /* Prevent integer overflow in negation */
1471 if (shft < -VARBITMAXLEN)
1472 shft = -VARBITMAXLEN;
1475 Int32GetDatum(-shft)));
1476 }
1477
1478 result = (VarBit *) palloc(VARSIZE(arg));
1481 r = VARBITS(result);
1482
1483 /* If we shifted all the bits out, return an all-zero string */
1484 if (shft >= VARBITLEN(arg))
1485 {
1486 MemSet(r, 0, VARBITBYTES(arg));
1488 }
1489
1492 p = VARBITS(arg);
1493
1494 /* Set the first part of the result to 0 */
1495 MemSet(r, 0, byte_shift);
1496 r += byte_shift;
1497
1498 if (ishift == 0)
1499 {
1500 /* Special case: we can do a memcpy */
1502 memcpy(r, p, len);
1503 r += len;
1504 }
1505 else
1506 {
1507 if (r < VARBITEND(result))
1508 *r = 0; /* initialize first byte */
1509 for (; r < VARBITEND(result); p++)
1510 {
1511 *r |= *p >> ishift;
1512 if ((++r) < VARBITEND(result))
1513 *r = (*p << (BITS_PER_BYTE - ishift)) & BITMASK;
1514 }
1515 }
1516
1517 /* We may have shifted 1's into the pad bits, so fix that */
1519
1521}
Datum bitshiftleft(PG_FUNCTION_ARGS)
Definition varbit.c:1389

References arg, BITMASK, BITS_PER_BYTE, bitshiftleft(), DirectFunctionCall2, fb(), Int32GetDatum(), len, memcpy(), MemSet, palloc(), PG_GETARG_INT32, PG_GETARG_VARBIT_P, PG_RETURN_DATUM, PG_RETURN_VARBIT_P, result, SET_VARSIZE(), VARBIT_PAD_LAST, VARBITBYTES, VARBITEND, VARBITLEN, VARBITMAXLEN, VarBitPGetDatum(), VARBITS, and VARSIZE().

Referenced by bitshiftleft().

◆ bitsubstr()

Datum bitsubstr ( PG_FUNCTION_ARGS  )

Definition at line 1038 of file varbit.c.

1039{
1041 PG_GETARG_INT32(1),
1042 PG_GETARG_INT32(2),
1043 false));
1044}

References bitsubstring(), PG_GETARG_INT32, PG_GETARG_VARBIT_P, and PG_RETURN_VARBIT_P.

◆ bitsubstr_no_len()

Datum bitsubstr_no_len ( PG_FUNCTION_ARGS  )

Definition at line 1047 of file varbit.c.

1048{
1050 PG_GETARG_INT32(1),
1051 -1, true));
1052}

References bitsubstring(), PG_GETARG_INT32, PG_GETARG_VARBIT_P, and PG_RETURN_VARBIT_P.

◆ bitsubstring()

static VarBit * bitsubstring ( VarBit arg,
int32  s,
int32  l,
bool  length_not_specified 
)
static

Definition at line 1055 of file varbit.c.

1056{
1057 VarBit *result;
1058 int bitlen,
1059 rbitlen,
1060 len,
1061 ishift,
1062 i;
1063 int32 e,
1064 s1,
1065 e1;
1066 uint8 *r,
1067 *ps;
1068
1069 bitlen = VARBITLEN(arg);
1070 s1 = Max(s, 1);
1071 /* If we do not have an upper bound, use end of string */
1073 {
1074 e1 = bitlen + 1;
1075 }
1076 else if (l < 0)
1077 {
1078 /* SQL99 says to throw an error for E < S, i.e., negative length */
1079 ereport(ERROR,
1081 errmsg("negative substring length not allowed")));
1082 e1 = -1; /* silence stupider compilers */
1083 }
1084 else if (pg_add_s32_overflow(s, l, &e))
1085 {
1086 /*
1087 * L could be large enough for S + L to overflow, in which case the
1088 * substring must run to end of string.
1089 */
1090 e1 = bitlen + 1;
1091 }
1092 else
1093 {
1094 e1 = Min(e, bitlen + 1);
1095 }
1096 if (s1 > bitlen || e1 <= s1)
1097 {
1098 /* Need to return a zero-length bitstring */
1099 len = VARBITTOTALLEN(0);
1100 result = (VarBit *) palloc(len);
1102 VARBITLEN(result) = 0;
1103 }
1104 else
1105 {
1106 /*
1107 * OK, we've got a true substring starting at position s1-1 and ending
1108 * at position e1-1
1109 */
1110 rbitlen = e1 - s1;
1112 result = (VarBit *) palloc(len);
1116 /* Are we copying from a byte boundary? */
1117 if ((s1 - 1) % BITS_PER_BYTE == 0)
1118 {
1119 /* Yep, we are copying bytes */
1121 len);
1122 }
1123 else
1124 {
1125 /* Figure out how much we need to shift the sequence by */
1126 ishift = (s1 - 1) % BITS_PER_BYTE;
1127 r = VARBITS(result);
1128 ps = VARBITS(arg) + (s1 - 1) / BITS_PER_BYTE;
1129 for (i = 0; i < len; i++)
1130 {
1131 *r = (*ps << ishift) & BITMASK;
1132 if ((++ps) < VARBITEND(arg))
1133 *r |= *ps >> (BITS_PER_BYTE - ishift);
1134 r++;
1135 }
1136 }
1137
1138 /* Make sure last byte is correctly zero-padded */
1140 }
1141
1142 return result;
1143}
#define Max(x, y)
Definition c.h:1125
#define VARHDRSZ
Definition c.h:840
struct parser_state ps
e
#define VARBITHDRSZ
Definition varbit.h:67

References arg, BITMASK, BITS_PER_BYTE, ereport, errcode(), errmsg, ERROR, fb(), i, len, Max, memcpy(), Min, palloc(), pg_add_s32_overflow(), ps, result, s1, SET_VARSIZE(), VARBIT_PAD, VARBITEND, VARBITHDRSZ, VARBITLEN, VARBITS, VARBITTOTALLEN, and VARHDRSZ.

Referenced by bit_overlay(), bitsubstr(), and bitsubstr_no_len().

◆ bittoint4()

Datum bittoint4 ( PG_FUNCTION_ARGS  )

Definition at line 1583 of file varbit.c.

1584{
1586 uint32 result;
1587 uint8 *r;
1588
1589 /* Check that the bit string is not too long */
1590 if (VARBITLEN(arg) > sizeof(result) * BITS_PER_BYTE)
1591 ereturn(fcinfo->context, (Datum) 0,
1593 errmsg("integer out of range")));
1594
1595 result = 0;
1596 for (r = VARBITS(arg); r < VARBITEND(arg); r++)
1597 {
1599 result |= *r;
1600 }
1601 /* Now shift the result to take account of the padding at the end */
1602 result >>= VARBITPAD(arg);
1603
1605}
uint32_t uint32
Definition c.h:683

References arg, BITS_PER_BYTE, ereturn, errcode(), errmsg, fb(), PG_GETARG_VARBIT_P, PG_RETURN_INT32, result, VARBITEND, VARBITLEN, VARBITPAD, and VARBITS.

◆ bittoint8()

Datum bittoint8 ( PG_FUNCTION_ARGS  )

Definition at line 1663 of file varbit.c.

1664{
1666 uint64 result;
1667 uint8 *r;
1668
1669 /* Check that the bit string is not too long */
1670 if (VARBITLEN(arg) > sizeof(result) * BITS_PER_BYTE)
1671 ereturn(fcinfo->context, (Datum) 0,
1673 errmsg("bigint out of range")));
1674
1675 result = 0;
1676 for (r = VARBITS(arg); r < VARBITEND(arg); r++)
1677 {
1679 result |= *r;
1680 }
1681 /* Now shift the result to take account of the padding at the end */
1682 result >>= VARBITPAD(arg);
1683
1685}
uint64_t uint64
Definition c.h:684

References arg, BITS_PER_BYTE, ereturn, errcode(), errmsg, fb(), PG_GETARG_VARBIT_P, PG_RETURN_INT64, result, VARBITEND, VARBITLEN, VARBITPAD, and VARBITS.

◆ bittypmodin()

Datum bittypmodin ( PG_FUNCTION_ARGS  )

Definition at line 429 of file varbit.c.

430{
432
434}
#define PG_GETARG_ARRAYTYPE_P(n)
Definition array.h:263
static int32 anybit_typmodin(ArrayType *ta, const char *typename)
Definition varbit.c:90

References anybit_typmodin(), fb(), PG_GETARG_ARRAYTYPE_P, and PG_RETURN_INT32.

◆ bittypmodout()

Datum bittypmodout ( PG_FUNCTION_ARGS  )

Definition at line 437 of file varbit.c.

438{
439 int32 typmod = PG_GETARG_INT32(0);
440
442}
static char * anybit_typmodout(int32 typmod)
Definition varbit.c:127

References anybit_typmodout(), PG_GETARG_INT32, and PG_RETURN_CSTRING.

◆ bitxor()

Datum bitxor ( PG_FUNCTION_ARGS  )

Definition at line 1322 of file varbit.c.

1323{
1326 VarBit *result;
1327 int len,
1328 bitlen1,
1329 bitlen2;
1330 uint8 *p1,
1331 *p2,
1332 *r;
1333
1336 if (bitlen1 != bitlen2)
1337 ereport(ERROR,
1339 errmsg("cannot XOR bit strings of different sizes")));
1340
1341 len = VARSIZE(arg1);
1342 result = (VarBit *) palloc(len);
1345
1346 p1 = VARBITS(arg1);
1347 p2 = VARBITS(arg2);
1348 r = VARBITS(result);
1349 for (size_t i = 0; i < VARBITBYTES(arg1); i++)
1350 *r++ = *p1++ ^ *p2++;
1351
1352 /* Padding is not needed as ^ of 0 pads is 0 */
1353
1355}

References ereport, errcode(), errmsg, ERROR, fb(), i, len, palloc(), PG_GETARG_VARBIT_P, PG_RETURN_VARBIT_P, result, SET_VARSIZE(), VARBITBYTES, VARBITLEN, VARBITS, and VARSIZE().

◆ varbit()

Datum varbit ( PG_FUNCTION_ARGS  )

Definition at line 742 of file varbit.c.

743{
746 bool isExplicit = PG_GETARG_BOOL(2);
747 VarBit *result;
748 int rlen;
749
750 /* No work if typmod is invalid or supplied data matches it already */
753
754 if (!isExplicit)
755 ereturn(fcinfo->context, (Datum) 0,
757 errmsg("bit string too long for type bit varying(%d)",
758 len)));
759
761 result = (VarBit *) palloc(rlen);
764
766
767 /* Make sure last byte is correctly zero-padded */
769
771}

References arg, ereturn, errcode(), errmsg, fb(), len, memcpy(), palloc(), PG_GETARG_BOOL, PG_GETARG_INT32, PG_GETARG_VARBIT_P, PG_RETURN_VARBIT_P, result, SET_VARSIZE(), VARBIT_PAD, VARBITBYTES, VARBITLEN, VARBITS, and VARBITTOTALLEN.

◆ varbit_in()

Datum varbit_in ( PG_FUNCTION_ARGS  )

Definition at line 452 of file varbit.c.

453{
455#ifdef NOT_USED
456 Oid typelem = PG_GETARG_OID(1);
457#endif
458 int32 atttypmod = PG_GETARG_INT32(2);
459 Node *escontext = fcinfo->context;
460 VarBit *result; /* The resulting bit string */
461 char *sp; /* pointer into the character string */
462 uint8 *r; /* pointer into the result */
463 int len, /* Length of the whole data structure */
464 bitlen, /* Number of bits in the bit string */
465 slen; /* Length of the input string */
466 bool bit_not_hex; /* false = hex string true = bit string */
467 int bc;
468 uint8 x = 0;
469
470 /* Check that the first character is a b or an x */
471 if (input_string[0] == 'b' || input_string[0] == 'B')
472 {
473 bit_not_hex = true;
474 sp = input_string + 1;
475 }
476 else if (input_string[0] == 'x' || input_string[0] == 'X')
477 {
478 bit_not_hex = false;
479 sp = input_string + 1;
480 }
481 else
482 {
483 bit_not_hex = true;
485 }
486
487 /*
488 * Determine bitlength from input string. MaxAllocSize ensures a regular
489 * input is small enough, but we must check hex input.
490 */
491 slen = strlen(sp);
492 if (bit_not_hex)
493 bitlen = slen;
494 else
495 {
496 if (slen > VARBITMAXLEN / 4)
497 ereturn(escontext, (Datum) 0,
499 errmsg("bit string length exceeds the maximum allowed (%d)",
500 VARBITMAXLEN)));
501 bitlen = slen * 4;
502 }
503
504 /*
505 * Sometimes atttypmod is not supplied. If it is supplied we need to make
506 * sure that the bitstring fits.
507 */
508 if (atttypmod <= 0)
509 atttypmod = bitlen;
510 else if (bitlen > atttypmod)
511 ereturn(escontext, (Datum) 0,
513 errmsg("bit string too long for type bit varying(%d)",
514 atttypmod)));
515
517 /* set to 0 so that *r is always initialised and string is zero-padded */
518 result = (VarBit *) palloc0(len);
520 VARBITLEN(result) = Min(bitlen, atttypmod);
521
522 r = VARBITS(result);
523 if (bit_not_hex)
524 {
525 /* Parse the bit representation of the string */
526 /* We know it fits, as bitlen was compared to atttypmod */
527 x = HIGHBIT;
528 for (; *sp; sp++)
529 {
530 if (*sp == '1')
531 *r |= x;
532 else if (*sp != '0')
533 ereturn(escontext, (Datum) 0,
535 errmsg("\"%.*s\" is not a valid binary digit",
536 pg_mblen_cstr(sp), sp)));
537
538 x >>= 1;
539 if (x == 0)
540 {
541 x = HIGHBIT;
542 r++;
543 }
544 }
545 }
546 else
547 {
548 /* Parse the hex representation of the string */
549 for (bc = 0; *sp; sp++)
550 {
551 if (*sp >= '0' && *sp <= '9')
552 x = (uint8) (*sp - '0');
553 else if (*sp >= 'A' && *sp <= 'F')
554 x = (uint8) (*sp - 'A') + 10;
555 else if (*sp >= 'a' && *sp <= 'f')
556 x = (uint8) (*sp - 'a') + 10;
557 else
558 ereturn(escontext, (Datum) 0,
560 errmsg("\"%.*s\" is not a valid hexadecimal digit",
561 pg_mblen_cstr(sp), sp)));
562
563 if (bc)
564 {
565 *r++ |= x;
566 bc = 0;
567 }
568 else
569 {
570 *r = x << 4;
571 bc = 1;
572 }
573 }
574 }
575
577}

References ereturn, errcode(), errmsg, fb(), HIGHBIT, len, Min, palloc0(), PG_GETARG_CSTRING, PG_GETARG_INT32, PG_GETARG_OID, pg_mblen_cstr(), PG_RETURN_VARBIT_P, result, SET_VARSIZE(), VARBITLEN, VARBITMAXLEN, VARBITS, VARBITTOTALLEN, and x.

Referenced by leftmostvalue_varbit().

◆ varbit_out()

Datum varbit_out ( PG_FUNCTION_ARGS  )

Definition at line 587 of file varbit.c.

588{
590 char *result,
591 *r;
592 uint8 *sp;
593 uint8 x;
594 int i,
595 k,
596 len;
597
598 /* Assertion to help catch any bit functions that don't pad correctly */
600
601 len = VARBITLEN(s);
602 result = (char *) palloc(len + 1);
603 sp = VARBITS(s);
604 r = result;
605 for (i = 0; i <= len - BITS_PER_BYTE; i += BITS_PER_BYTE, sp++)
606 {
607 /* print full bytes */
608 x = *sp;
609 for (k = 0; k < BITS_PER_BYTE; k++)
610 {
611 *r++ = IS_HIGHBIT_SET(x) ? '1' : '0';
612 x <<= 1;
613 }
614 }
615 if (i < len)
616 {
617 /* print the last partial byte */
618 x = *sp;
619 for (k = i; k < len; k++)
620 {
621 *r++ = IS_HIGHBIT_SET(x) ? '1' : '0';
622 x <<= 1;
623 }
624 }
625 *r = '\0';
626
628}
#define IS_HIGHBIT_SET(ch)
Definition c.h:1284

References BITS_PER_BYTE, fb(), i, IS_HIGHBIT_SET, len, palloc(), PG_GETARG_VARBIT_P, PG_RETURN_CSTRING, result, VARBIT_CORRECTLY_PADDED, VARBITLEN, VARBITS, and x.

Referenced by bit_out().

◆ varbit_recv()

Datum varbit_recv ( PG_FUNCTION_ARGS  )

Definition at line 636 of file varbit.c.

637{
639
640#ifdef NOT_USED
641 Oid typelem = PG_GETARG_OID(1);
642#endif
643 int32 atttypmod = PG_GETARG_INT32(2);
644 VarBit *result;
645 int len,
646 bitlen;
647
648 bitlen = pq_getmsgint(buf, sizeof(int32));
652 errmsg("invalid length in external bit string")));
653
654 /*
655 * Sometimes atttypmod is not supplied. If it is supplied we need to make
656 * sure that the bitstring fits.
657 */
658 if (atttypmod > 0 && bitlen > atttypmod)
661 errmsg("bit string too long for type bit varying(%d)",
662 atttypmod)));
663
665 result = (VarBit *) palloc(len);
668
670
671 /* Make sure last byte is correctly zero-padded */
673
675}

References buf, ereport, errcode(), errmsg, ERROR, fb(), len, palloc(), PG_GETARG_INT32, PG_GETARG_OID, PG_GETARG_POINTER, PG_RETURN_VARBIT_P, pq_copymsgbytes(), pq_getmsgint(), result, SET_VARSIZE(), VARBIT_PAD, VARBITBYTES, VARBITLEN, VARBITMAXLEN, VARBITS, and VARBITTOTALLEN.

◆ varbit_send()

Datum varbit_send ( PG_FUNCTION_ARGS  )

Definition at line 681 of file varbit.c.

682{
685
690}
#define PG_RETURN_BYTEA_P(x)
Definition fmgr.h:373
void pq_sendbytes(StringInfo buf, const void *data, int datalen)
Definition pqformat.c:126
void pq_begintypsend(StringInfo buf)
Definition pqformat.c:325
bytea * pq_endtypsend(StringInfo buf)
Definition pqformat.c:345
static void pq_sendint32(StringInfo buf, uint32 i)
Definition pqformat.h:144

References buf, PG_GETARG_VARBIT_P, PG_RETURN_BYTEA_P, pq_begintypsend(), pq_endtypsend(), pq_sendbytes(), pq_sendint32(), VARBITBYTES, VARBITLEN, and VARBITS.

Referenced by bit_send().

◆ varbit_support()

Datum varbit_support ( PG_FUNCTION_ARGS  )

Definition at line 702 of file varbit.c.

703{
705 Node *ret = NULL;
706
708 {
710 FuncExpr *expr = req->fcall;
711 Node *typmod;
712
713 Assert(list_length(expr->args) >= 2);
714
715 typmod = (Node *) lsecond(expr->args);
716
717 if (IsA(typmod, Const) && !((Const *) typmod)->constisnull)
718 {
719 Node *source = (Node *) linitial(expr->args);
723
724 /* Note: varbit() treats typmod 0 as invalid, so we do too */
725 if (new_max <= 0 || (old_max > 0 && old_max <= new_max))
727 }
728 }
729
731}
#define Assert(condition)
Definition c.h:1002
#define PG_RETURN_POINTER(x)
Definition fmgr.h:363
int32 exprTypmod(const Node *expr)
Definition nodeFuncs.c:304
Node * relabel_to_typmod(Node *expr, int32 typmod)
Definition nodeFuncs.c:694
#define IsA(nodeptr, _type_)
Definition nodes.h:162
static int list_length(const List *l)
Definition pg_list.h:152
#define linitial(l)
Definition pg_list.h:178
#define lsecond(l)
Definition pg_list.h:183
static rewind_source * source
Definition pg_rewind.c:89
static int32 DatumGetInt32(Datum X)
Definition postgres.h:202
List * args
Definition primnodes.h:788

References FuncExpr::args, Assert, DatumGetInt32(), exprTypmod(), fb(), IsA, linitial, list_length(), lsecond, PG_GETARG_POINTER, PG_RETURN_POINTER, relabel_to_typmod(), and source.

◆ varbittypmodin()

Datum varbittypmodin ( PG_FUNCTION_ARGS  )

Definition at line 774 of file varbit.c.

775{
777
779}

References anybit_typmodin(), fb(), PG_GETARG_ARRAYTYPE_P, and PG_RETURN_INT32.

◆ varbittypmodout()

Datum varbittypmodout ( PG_FUNCTION_ARGS  )

Definition at line 782 of file varbit.c.

783{
784 int32 typmod = PG_GETARG_INT32(0);
785
787}

References anybit_typmodout(), PG_GETARG_INT32, and PG_RETURN_CSTRING.