28 #ifndef DOXYGEN_SHOULD_SKIP_THIS 30 #define INT4VAR_HDR 'V' 31 #define INT8VAR_HDR '8' 34 #define BITMAP_HDR 'B' 36 #define BITMAP_HDR 'M' 38 #define BITMAP_ARRAY_HDR 'A' 39 #define BITMAP_HASH_HDR 'H' 40 #define INT4_ARRAY_HDR 'I' 41 #define BITMAP_HASH_MORE '>' 42 #define BITMAP_HASH_DONE '.' 47 #define INT32SIZE_B64 7 49 #define INT64SIZE_B64 12 55 static const char _base64[] =
56 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
58 static const int8 b64lookup[128] = {
59 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
60 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
61 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
62 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
63 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
64 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
65 -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
66 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1,
70 b64_encode(
const char *src,
unsigned len,
char *dst)
84 buf |= (
unsigned char) *s << (pos << 3);
91 *p++ = _base64[(buf >> 18) & 0x3f];
92 *p++ = _base64[(buf >> 12) & 0x3f];
93 *p++ = _base64[(buf >> 6) & 0x3f];
94 *p++ = _base64[buf & 0x3f];
107 *p++ = _base64[(buf >> 18) & 0x3f];
108 *p++ = _base64[(buf >> 12) & 0x3f];
109 *p++ = (pos == 0) ? _base64[(buf >> 6) & 0x3f] :
'=';
117 b64_decode(
const char *src,
unsigned len,
char *dst)
119 const char *srcend = src + len,
132 if (c ==
' ' || c ==
'\t' || c ==
'\n' || c ==
'\r')
146 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
147 errmsg(
"unexpected \"=\"")));
154 if (c > 0 && c < 127)
155 b = b64lookup[(
unsigned char) c];
158 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
159 errmsg(
"invalid symbol")));
162 buf = (buf << 6) + b;
166 *p++ = (buf >> 16) & 255;
167 if (end == 0 || end > 1)
168 *p++ = (buf >> 8) & 255;
169 if (end == 0 || end > 2)
178 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
179 errmsg(
"invalid end sequence")));
199 return (4 * ((bytes + 2) / 3));
214 return HDRLEN + INT32SIZE_B64 + strlen(name);
231 int len = b64_encode((
char *) &value,
sizeof(int32), *p_stream);
232 (*p_stream) += (len - 1);
250 char *endpos = (*p_stream) + INT32SIZE_B64;
251 char endchar = *endpos;
253 b64_decode(*p_stream, INT32SIZE_B64 + 1, (
char *) &value);
255 (*p_stream) += INT32SIZE_B64;
259 #ifdef UNUSED_BUT_WORKS 270 serialise_int8(
char **p_stream, int64 value)
272 int len = b64_encode((
char *) &value,
sizeof(int64), *p_stream);
288 deserialise_int8(
char **p_stream)
291 fprintf(stderr,
"Deserialise %s\n", *p_stream);
292 b64_decode(*p_stream, INT64SIZE_B64, (
char *) &value);
293 (*p_stream) += INT64SIZE_B64;
311 int len = b64_encode(instream, bytes, *p_stream);
312 (*p_stream)[len] =
'\0';
330 b64_decode(*p_stream, len, outstream);
347 (**p_stream) = value?
'T':
'F';
363 bool result = (**p_stream) ==
'T';
381 (**p_stream) = value;
397 char result = **p_stream;
415 static char *blank_name =
"";
421 safe_name = blank_name;
425 strcpy((*p_stream), safe_name);
426 (*p_stream) += strlen(safe_name);
442 char *result = palloc((name_len + 1) *
sizeof(
char));
443 strncpy(result, *p_stream, name_len);
444 result[name_len] =
'\0';
445 (*p_stream) += name_len;
460 int stream_len =
hdrlen(name) + BOOLSIZE + INT32SIZE_B64 + 1;
461 char *stream = palloc(stream_len *
sizeof(
char));
462 char *streamstart = stream;
487 if (i4v->
type != OBJ_INT4) {
513 int stream_len =
hdrlen(name) + (2 * INT32SIZE_B64) +
515 char *stream = palloc(stream_len *
sizeof(
char));
516 char *streamstart = stream;
523 (
char *) &(array->
array[0]));
548 elems = 1 + arraymax - arrayzero;
551 if (array->
type != OBJ_INT4_ARRAY) {
559 (
char *) &(array->
array[0]));
575 int stream_len =
hdrlen(name) + (INT32SIZE_B64 * 2) + 1;
576 char *stream = palloc(stream_len *
sizeof(
char));
577 char *streamstart = stream;
602 if (range->
type != OBJ_RANGE) {
631 (
char *) &(bitmap->
bitset));
647 int stream_len =
hdrlen(name) + (INT32SIZE_B64 * 2) +
649 char *stream = palloc(stream_len *
sizeof(
char));
650 char *streamstart = stream;
672 bool shared,
char **p_stream)
674 Bitmap *bitmap = *p_bitmap;
684 if (bitmap->
type != OBJ_BITMAP) {
693 (
char *) &(bitmap->
bitset[0]));
731 int bitmap_len = (INT32SIZE_B64 * 2) +
732 streamlen(
sizeof(bm_int) * bitset_elems);
733 int stream_len =
hdrlen(name) + (INT32SIZE_B64 * 4) +
734 (bitmap_len * array_elems) + 1;
736 char *stream = palloc(stream_len *
sizeof(
char));
737 char *streamstart = stream;
745 for (idx = 0; idx < array_elems; idx++) {
778 if (bmarray->
type != OBJ_BITMAP_ARRAY) {
784 arraymax, bitzero, bitmax);
787 array_elems = 1 + arraymax - arrayzero;
788 for (idx = 0; idx < array_elems; idx++) {
815 size += 1 + bitset_size +
hdrlen(var->
key);
834 int bitset_size = (INT32SIZE_B64 * 2) +
837 int stream_len =
hdrlen(name) + (INT32SIZE_B64 * 2) +
838 all_bitmaps_size + 1;
839 char *stream = palloc(stream_len *
sizeof(
char));
840 char *streamstart = stream;
874 Bitmap *tmp_bitmap = NULL;
881 if (bmhash->
type != OBJ_BITMAP_HASH) {
917 if (var && var->
obj) {
931 case OBJ_BITMAP_ARRAY:
934 case OBJ_BITMAP_HASH:
939 (errcode(ERRCODE_INTERNAL_ERROR),
940 errmsg(
"Unsupported type for variable serialisation"),
941 errdetail(
"Cannot serialise objects of type %d.",
961 if ((**p_stream) !=
'\0') {
978 (errcode(ERRCODE_INTERNAL_ERROR),
979 errmsg(
"Unsupported type for variable deserialisation"),
980 errdetail(
"Cannot deserialise objects of type %c.",
999 while ((**p_stream) !=
'\0') {
Subtype of Object for storing arrays of integers.
Subtype of Object for storing bitmaps.
static VarEntry * deserialise_bitmap_hash(char **p_stream)
De-serialise a veil bitmap hash variable.
int32 value
the integer value of the variable
void vl_NewBitmap(Bitmap **p_bitmap, bool shared, int32 min, int32 max)
Return a newly initialised (empty) Bitmap.
int32 bitzero
The index of the lowest bit each bitmap can store.
int32 bitmax
The index of the highest bit each bitmap can store.
Subtype of Object for storing simple int4 values.
ObjType type
This must have the value OBJ_BITMAP.
void vl_BitmapUnion(Bitmap *target, Bitmap *source)
Create the union of two bitmaps, updating the first with the result.
static char * serialise_bitmap_array(BitmapArray *bmarray, char *name)
Serialise a veil bitmap array variable into a dynamically allocated string.
ObjType type
This must have the value OBJ_BITMAP_ARRAY.
static int32 deserialise_int4(char **p_stream)
De-serialise an int4 value from a base64 character stream.
ObjType type
This must have the value OBJ_RANGE.
int32 bitmax
The index of the highest bit the bitmap can store.
static char deserialise_char(char **p_stream)
De-serialise a character value.
static int hdrlen(char *name)
Return the length of the header part of a serialised data stream for the given named variable...
ObjType type
This must have the value OBJ_INT4_ARRAY.
VarEntry * vl_lookup_variable(char *name)
Lookup a variable by name, creating it as as a session variable if it does not already exist...
int32 vl_deserialise(char **p_stream)
De-serialise a base64 string containing, possibly many, derialised veil variables.
static VarEntry * deserialise_range(char **p_stream)
De-serialise a veil range variable.
#define ARRAYELEMS(min, max)
Gives the number of array elements in a Bitmap that runs from element min to element max...
static void serialise_char(char **p_stream, char value)
Serialise a character value into *p_stream.
static char * deserialise_name(char **p_stream)
De-serialise a string returning a dynamically allocated string.
static void deserialise_one_bitmap(Bitmap **p_bitmap, char *name, bool shared, char **p_stream)
De-serialise a single bitmap into a veil bitmap array or bitmap hash.
static VarEntry * deserialise_bitmap(char **p_stream)
De-serialise a veil bitmap variable.
void vl_NewBitmapHash(BitmapHash **p_bmhash, char *name, int32 bitzero, int32 bitmax)
Return a newly initialised (empty) BitmapHash.
int32 arrayzero
The index of array element zero: the index of the lowest numbered bitmap in the array.
int32 arraymax
The index of the lowest numbered bitmap in the array.
static int sizeof_bitmaps_in_hash(BitmapHash *bmhash, int bitset_size)
Calculate the size needed for a base64 stream to contain all of the bitmaps in a bitmap hash includin...
char * vl_serialise_var(char *name)
Serialise a veil variable.
Provide definitions for all non-local C-callable Veil functions.
static char * serialise_bitmap(Bitmap *bitmap, char *name)
Serialise a veil bitmap variable into a dynamically allocated string.
bool shared
Whether this is a shared variable.
General purpose object-type.
Provides version information for veil.
int32 bitzero
The index of the lowest bit the bitmap can store.
Subtype of Object for storing bitmap hashes.
void vl_type_mismatch(char *name, ObjType expected, ObjType got)
Report, by raising an error, a type mismatch between the expected and actual type of a VarEntry varia...
static VarEntry * deserialise_int4var(char **p_stream)
De-serialise a veil integer variable.
HTAB * hash
Pointer to the (Postgresql dynahash) hash table.
static char * serialise_int4var(Int4Var *var, char *name)
Serialise a veil integer variable into a dynamically allocated string.
bm_int bitset[1]
Element zero of the array of int4 values comprising the bitmap.
Bitmap * vl_AddBitmapToHash(BitmapHash *bmhash, char *hashelem)
Create a newly allocated empty Bitmap to a BitmapHash.
VarEntry * vl_NextHashEntry(HTAB *hash, VarEntry *prev)
Utility function for scanning the hash table of a BitmapHash.
Int4Array * vl_NewInt4Array(Int4Array *current, bool shared, int32 min, int32 max)
Return a newly initialised (zeroed) Int4Array.
static int streamlen(int bytes)
Return the length of a base64 encoded stream for a binary stream of ::bytes length.
static char * serialise_int4array(Int4Array *array, char *name)
Serialise a veil integer array variable into a dynamically allocated string.
int32 arrayzero
The index of array element zero: the index of the lowest numbered bitmap in the array.
static void serialise_name(char **p_stream, char *name)
Serialise a string (containing a name) into *p_stream.
Subtype of Object for storing range values.
int32 max
Upper limit for range.
void vl_NewBitmapArray(BitmapArray **p_bmarray, bool shared, int32 arrayzero, int32 arraymax, int32 bitzero, int32 bitmax)
Return a newly initialised (empty) BitmapArray.
int32 bitzero
The index of the lowest bit each bitmap can store.
static char * serialise_range(Range *range, char *name)
Serialise a veil range variable into a dynamically allocated string.
static void serialise_one_bitmap(char **p_stream, Bitmap *bitmap)
Serialise a single bitmap from a veil bitmap array or bitmap hash.
ObjType type
Identifies the type of the object.
static bool deserialise_bool(char **p_stream)
De-serialise a boolean value.
int32 bitmax
The index of the highest bit each bitmap can store.
int32 arraymax
The index of the lowest numbered bitmap in the array.
static void serialise_stream(char **p_stream, int32 bytes, char *instream)
Serialise a binary stream as a base64 stream into *p_stream.
ObjType type
This must have the value OBJ_BITMAP_HASH.
Object * obj
Pointer to the contents of the variable.
static char * serialise_bitmap_hash(BitmapHash *bmhash, char *name)
Serialise a veil bitmap hash variable into a dynamically allocated string.
Subtype of Object for storing bitmap arrays.
Int4Var * vl_NewInt4(bool shared)
Create a new session or shared Int4Var object.
ObjType type
This must have the value OBJ_INT4.
int32 min
Lower limit for range.
static void serialise_bool(char **p_stream, bool value)
Serialise a boolean value into *p_stream.
bool isnull
if true, the value is null
int32 array[0]
Element zero of the array of integers.
Bitmap * bitmap[1]
Element zero of the array of Bitmap pointers comprising the array.
static VarEntry * deserialise_int4array(char **p_stream)
De-serialise a veil integer array variable.
static VarEntry * deserialise_bitmap_array(char **p_stream)
De-serialise a veil bitmap array variable.
static void serialise_int4(char **p_stream, int32 value)
Serialise an int4 value as a base64 stream (truncated to save a byte) into *p_stream.
VarEntry * vl_deserialise_next(char **p_stream)
De-serialise the next veil variable from *p_stream.
char key[60]
String containing variable name.
static void deserialise_stream(char **p_stream, int32 bytes, char *outstream)
De-serialise a binary stream.
Range * vl_NewRange(bool shared)
Create a new session or shared Range object.
Define all Veil public datatypes.