LCOV - code coverage report
Current view: top level - gdk - gdk_atoms.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 590 794 74.3 %
Date: 2024-11-15 19:37:45 Functions: 79 92 85.9 %

          Line data    Source code
       1             : /*
       2             :  * SPDX-License-Identifier: MPL-2.0
       3             :  *
       4             :  * This Source Code Form is subject to the terms of the Mozilla Public
       5             :  * License, v. 2.0.  If a copy of the MPL was not distributed with this
       6             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       7             :  *
       8             :  * Copyright 2024 MonetDB Foundation;
       9             :  * Copyright August 2008 - 2023 MonetDB B.V.;
      10             :  * Copyright 1997 - July 2008 CWI.
      11             :  */
      12             : 
      13             : /*
      14             :  * @a M. L. Kersten, P. Boncz
      15             :  * @* Atomic types
      16             :  * The Binary Association Table library assumes efficient
      17             :  * implementation of the atoms making up the binary association.  This
      18             :  * section describes the preliminaries for handling both built-in and
      19             :  * user-defined atomic types.
      20             :  * New types, such as point and polygons, can be readily added to this
      21             :  * collection.
      22             :  */
      23             : /*
      24             :  * @- inline comparison routines
      25             :  * Return 0 on l==r, < 0 iff l < r, >0 iff l > r
      26             :  */
      27             : #include "monetdb_config.h"
      28             : #include "gdk.h"
      29             : #include "gdk_time.h"
      30             : #include "gdk_private.h"
      31             : 
      32             : /* the *Cmp functions return a value less than zero if the first
      33             :  * argument is less than the second; they return zero if the two
      34             :  * values are equal; and they return a value greater than zero if the
      35             :  * first argument is greater than the second.  Remember that in all
      36             :  * cases, nil is considered smaller than any other value and nil is
      37             :  * equal to itself (this has repercussions for the floating point
      38             :  * implementation if and when its NIL value is the floating point
      39             :  * NaN). */
      40             : 
      41             : static int
      42         200 : mskCmp(const msk *l, const msk *r)
      43             : {
      44         200 :         return (*l > *r) - (*l < *r);
      45             : }
      46             : 
      47             : static int
      48   433134989 : bteCmp(const bte *l, const bte *r)
      49             : {
      50   433134989 :         return (*l > *r) - (*l < *r);
      51             : }
      52             : 
      53             : static int
      54    60050063 : shtCmp(const sht *l, const sht *r)
      55             : {
      56    60050063 :         return (*l > *r) - (*l < *r);
      57             : }
      58             : 
      59             : static int
      60  1563144558 : intCmp(const int *l, const int *r)
      61             : {
      62  1563144558 :         return (*l > *r) - (*l < *r);
      63             : }
      64             : 
      65             : static int
      66   408859131 : fltCmp(const flt *l, const flt *r)
      67             : {
      68   408859131 :         return is_flt_nil(*l) ? -!is_flt_nil(*r) : is_flt_nil(*r) ? 1 : (*l > *r) - (*l < *r);
      69             : }
      70             : 
      71             : static int
      72  1442550702 : lngCmp(const lng *l, const lng *r)
      73             : {
      74  1442550702 :         return (*l > *r) - (*l < *r);
      75             : }
      76             : 
      77             : #ifdef HAVE_HGE
      78             : static int
      79   138246444 : hgeCmp(const hge *l, const hge *r)
      80             : {
      81   138246444 :         return (*l > *r) - (*l < *r);
      82             : }
      83             : #endif
      84             : 
      85             : static int
      86    38704642 : dblCmp(const dbl *l, const dbl *r)
      87             : {
      88    38704642 :         return is_dbl_nil(*l) ? -!is_dbl_nil(*r) : is_dbl_nil(*r) ? 1 : (*l > *r) - (*l < *r);
      89             : }
      90             : 
      91             : /*
      92             :  * @- inline hash routines
      93             :  * Return some positive integer derived from one atom value.
      94             :  */
      95             : static BUN
      96           0 : bteHash(const bte *v)
      97             : {
      98           0 :         return (BUN) mix_bte(*(const unsigned char *) v);
      99             : }
     100             : 
     101             : static BUN
     102           0 : shtHash(const sht *v)
     103             : {
     104           0 :         return (BUN) mix_sht(*(const unsigned short *) v);
     105             : }
     106             : 
     107             : static BUN
     108     1109255 : intHash(const int *v)
     109             : {
     110     1109255 :         return (BUN) mix_int(*(const unsigned int *) v);
     111             : }
     112             : 
     113             : static BUN
     114         868 : lngHash(const lng *v)
     115             : {
     116         868 :         return (BUN) mix_lng(*(const ulng *) v);
     117             : }
     118             : 
     119             : #ifdef HAVE_HGE
     120             : static BUN
     121           0 : hgeHash(const hge *v)
     122             : {
     123           0 :         return (BUN) mix_hge(*(const uhge *) v);
     124             : }
     125             : #endif
     126             : 
     127             : static BUN
     128      151851 : fltHash(const flt *v)
     129             : {
     130      151851 :         if (is_flt_nil(*v))
     131             :                 return (BUN) mix_int(GDK_int_min);
     132      151716 :         if (*v == 0)
     133             :                 return (BUN) mix_int(0);
     134      147739 :         return (BUN) mix_int(*(const unsigned int *) v);
     135             : }
     136             : 
     137             : static BUN
     138       41090 : dblHash(const dbl *v)
     139             : {
     140       41090 :         if (is_dbl_nil(*v))
     141             :                 return (BUN) mix_lng(GDK_lng_min);
     142       39128 :         if (*v == 0)
     143             :                 return (BUN) mix_lng(0);
     144       33786 :         return (BUN) mix_lng(*(const ulng *) v);
     145             : }
     146             : 
     147             : /*
     148             :  * @+ Standard Atoms
     149             :  */
     150             : 
     151             : /*
     152             :  * @+ Atomic Type Interface
     153             :  * The collection of built-in types supported for BATs can be extended
     154             :  * easily.  In essence, the user should specify conversion routines
     155             :  * from values stored anywhere in memory to its equivalent in the BAT,
     156             :  * and vice verse.  Some routines are required for coercion and to
     157             :  * support the BAT administration.
     158             :  *
     159             :  * A new type is incrementally build using the routine
     160             :  * ATOMallocate(id).  The parameter id denotes the type name; an entry
     161             :  * is created if the type is so far unknown.
     162             :  *
     163             :  * The size describes the amount of space to be reserved in the BUN.
     164             :  *
     165             :  * The routine put takes a pointer to a memory resident copy and
     166             :  * prepares a persistent copy in the BAT passed.  The inverse
     167             :  * operation is get.  A new value can be directly included into the
     168             :  * BAT using new, which should prepare a null-value representation.  A
     169             :  * value is removed from the BAT store using del, which can take care
     170             :  * of garbage collection and BAT administration.
     171             :  *
     172             :  * The pair tostr and fromstr should convert a reference to a
     173             :  * persistent value to a memory resident string equivalent. FromStr
     174             :  * takes a string and applies a put to store it within a BAT.  They
     175             :  * are used to prepare for readable output/input and to support
     176             :  * coercion.
     177             :  *
     178             :  * The routines cmp and eq are comparison routines used to build
     179             :  * access structures. The null returns a reference to a null value
     180             :  * representation.
     181             :  *
     182             :  * The incremental atom construction uses hardwired properties.  This
     183             :  * should be improved later on.
     184             :  */
     185             : static MT_Lock GDKatomLock = MT_LOCK_INITIALIZER(GDKatomLock);
     186             : 
     187             : int
     188        3748 : ATOMallocate(const char *id)
     189             : {
     190        3748 :         int t;
     191             : 
     192        3748 :         if (strlen(id) >= IDLENGTH) {
     193           0 :                 GDKerror("name too long");
     194           0 :                 return int_nil;
     195             :         }
     196             : 
     197        3748 :         MT_lock_set(&GDKatomLock);
     198        3748 :         t = ATOMindex(id);
     199        3748 :         if (t < 0) {
     200        3748 :                 t = -t;
     201        3748 :                 if (t == GDKatomcnt) {
     202        3748 :                         if (GDKatomcnt == MAXATOMS) {
     203           0 :                                 MT_lock_unset(&GDKatomLock);
     204           0 :                                 GDKerror("too many types");
     205           0 :                                 return int_nil;
     206             :                         }
     207        3748 :                         GDKatomcnt++;
     208             :                 }
     209        3748 :                 BATatoms[t] = (atomDesc) {
     210             :                         .size = sizeof(int),    /* default */
     211             :                         .linear = true,         /* default */
     212             :                         .storage = t,           /* default */
     213             :                 };
     214        3748 :                 strcpy(BATatoms[t].name, id);
     215             :         }
     216        3748 :         MT_lock_unset(&GDKatomLock);
     217        3748 :         return t;
     218             : }
     219             : 
     220             : int
     221       58417 : ATOMindex(const char *nme)
     222             : {
     223       58417 :         int t, j = GDKatomcnt;
     224             : 
     225      702224 :         for (t = 0; t < GDKatomcnt; t++) {
     226      698015 :                 if (!BATatoms[t].name[0]) {
     227           0 :                         if (j == GDKatomcnt)
     228      643807 :                                 j = t;
     229      698015 :                 } else if (strcmp(nme, BATatoms[t].name) == 0) {
     230       54208 :                         return t;
     231             :                 }
     232             : 
     233             :         }
     234        4209 :         return -j;
     235             : }
     236             : 
     237             : const char *
     238      360047 : ATOMname(int t)
     239             : {
     240      360047 :         return t >= 0 && t < GDKatomcnt && *BATatoms[t].name ? BATatoms[t].name : "null";
     241             : }
     242             : 
     243             : bool
     244      244510 : ATOMisdescendant(int tpe, int parent)
     245             : {
     246      244510 :         int cur = -1;
     247             : 
     248      496332 :         while (cur != tpe) {
     249      251822 :                 cur = tpe;
     250      251822 :                 if (cur == parent)
     251             :                         return true;
     252      251822 :                 tpe = ATOMstorage(tpe);
     253             :         }
     254             :         return false;
     255             : }
     256             : 
     257             : 
     258             : const bte bte_nil = GDK_bte_min-1;
     259             : const sht sht_nil = GDK_sht_min-1;
     260             : const int int_nil = GDK_int_min-1;
     261             : #ifdef NAN_CANNOT_BE_USED_AS_INITIALIZER
     262             : /* Definition of NAN is seriously broken on Intel compiler (at least
     263             :  * in some versions), so we work around it. */
     264             : const union _flt_nil_t _flt_nil_ = {
     265             :         .l = UINT32_C(0x7FC00000)
     266             : };
     267             : const union _dbl_nil_t _dbl_nil_ = {
     268             :         .l = UINT64_C(0x7FF8000000000000)
     269             : };
     270             : #else
     271             : const flt flt_nil = NAN;
     272             : const dbl dbl_nil = NAN;
     273             : #endif
     274             : const lng lng_nil = GDK_lng_min-1;
     275             : #ifdef HAVE_HGE
     276             : const hge hge_nil = GDK_hge_min-1;
     277             : #endif
     278             : const oid oid_nil = (oid) 1 << (sizeof(oid) * 8 - 1);
     279             : const ptr ptr_nil = NULL;
     280             : const uuid uuid_nil = {0};
     281             : 
     282             : ptr
     283          21 : ATOMnil(int t)
     284             : {
     285          21 :         const void *src = ATOMnilptr(t);
     286          21 :         size_t len = ATOMlen(ATOMtype(t), src);
     287          22 :         ptr dst = GDKmalloc(len);
     288             : 
     289          22 :         if (dst)
     290          22 :                 memcpy(dst, src, len);
     291          22 :         return dst;
     292             : }
     293             : 
     294             : /*
     295             :  * @- Atomic ADT functions
     296             :  */
     297             : size_t
     298     6327528 : ATOMlen(int t, const void *src)
     299             : {
     300     6327528 :         size_t (*l)(const void *) = BATatoms[t].atomLen;
     301             : 
     302     6327528 :         return l ? (*l) (src) : ATOMsize(t);
     303             : }
     304             : 
     305             : gdk_return
     306      861332 : ATOMheap(int t, Heap *hp, size_t cap)
     307             : {
     308      861332 :         gdk_return (*h) (Heap *, size_t) = BATatoms[t].atomHeap;
     309             : 
     310      861332 :         if (h) {
     311      861332 :                 return (*h) (hp, cap);
     312             :         }
     313             :         return GDK_SUCCEED;
     314             : }
     315             : 
     316             : /*
     317             :  * Atom print avoids coercion to strings for built-in types.
     318             :  * The comparison against the NULL value is hard coded for speed.
     319             :  */
     320             : #define LINE_LEN        60
     321             : 
     322             : int
     323         717 : ATOMprint(int t, const void *p, stream *s)
     324             : {
     325         717 :         ssize_t (*tostr) (char **, size_t *, const void *, bool);
     326         717 :         ssize_t res;
     327             : 
     328        1434 :         if (p && t >= 0 && t < GDKatomcnt && (tostr = BATatoms[t].atomToStr)) {
     329         717 :                 size_t sz;
     330             : 
     331         717 :                 if (t < TYPE_date) {
     332         247 :                         char buf[dblStrlen], *addr = buf;       /* use memory from stack */
     333             : 
     334         247 :                         sz = dblStrlen;
     335         247 :                         res = (*tostr) (&addr, &sz, p, true);
     336         247 :                         if (res > 0)
     337         247 :                                 res = mnstr_write(s, buf, (size_t) res, 1);
     338             :                 } else {
     339         470 :                         str buf = NULL;
     340             : 
     341         470 :                         sz = 0;
     342         470 :                         res = (*tostr) (&buf, &sz, p, true);
     343         470 :                         if (res > 0)
     344         470 :                                 res = mnstr_write(s, buf, (size_t) res, 1);
     345         470 :                         GDKfree(buf);
     346             :                 }
     347             :         } else {
     348           0 :                 res = mnstr_write(s, "nil", 1, 3);
     349             :         }
     350         717 :         if (res < 0)
     351           0 :                 GDKsyserror("ATOMprint: write failure\n");
     352         717 :         return (int) res;
     353             : }
     354             : 
     355             : 
     356             : char *
     357       24462 : ATOMformat(int t, const void *p)
     358             : {
     359       24462 :         ssize_t (*tostr) (char **, size_t *, const void *, bool);
     360             : 
     361       24462 :         if (p && 0 <= t && t < GDKatomcnt && (tostr = BATatoms[t].atomToStr)) {
     362       24469 :                 size_t sz = 0;
     363       24469 :                 char *buf = NULL;
     364       24469 :                 ssize_t res = (*tostr) (&buf, &sz, p, true);
     365       24473 :                 if (res < 0 && buf) {
     366           0 :                         GDKfree(buf);
     367           0 :                         buf = NULL;
     368             :                 }
     369       24473 :                 return buf;
     370             :         }
     371           0 :         return GDKstrdup("nil");
     372             : }
     373             : 
     374             : ptr
     375      115022 : ATOMdup(int t, const void *p)
     376             : {
     377      115022 :         size_t len = ATOMlen(t, p);
     378      115022 :         ptr n = GDKmalloc(len);
     379             : 
     380      115022 :         if (n)
     381      115022 :                 memcpy(n, p, len);
     382      115022 :         return n;
     383             : }
     384             : 
     385             : /*
     386             :  * @* Builtin Atomic Operator Implementations
     387             :  *
     388             :  * @+ Atom-from-String Conversions
     389             :  * These routines convert from string to atom. They are used during
     390             :  * conversion and BAT import. In order to avoid unnecessary
     391             :  * malloc()/free() sequences, the conversion functions have a meta
     392             :  * 'dst' pointer to a destination region, and an integer* 'len'
     393             :  * parameter, that denotes the length of that region (a char region
     394             :  * for ToStr functions, an atom region from FromStr conversions). Only
     395             :  * if necessary will the conversion routine do a GDKfree()/GDKmalloc()
     396             :  * sequence, and increment the 'len'.  Passing a pointer to a nil-ptr
     397             :  * as 'dst' and/or a *len==0 is valid; the conversion function will
     398             :  * then alloc some region for you.
     399             :  */
     400             : #define atommem(size)                                   \
     401             :         do {                                            \
     402             :                 if (*dst == NULL || *len < (size)) { \
     403             :                         GDKfree(*dst);                  \
     404             :                         *len = (size);                  \
     405             :                         *dst = GDKmalloc(*len);         \
     406             :                         if (*dst == NULL) {             \
     407             :                                 *len = 0;               \
     408             :                                 return -1;              \
     409             :                         }                               \
     410             :                 }                                       \
     411             :         } while (0)
     412             : 
     413             : #define is_ptr_nil(val)         ((val) == ptr_nil)
     414             : 
     415             : #define atomtostr(TYPE, FMT, FMTCAST)                                   \
     416             : ssize_t                                                                 \
     417             : TYPE##ToStr(char **dst, size_t *len, const TYPE *src, bool external)    \
     418             : {                                                                       \
     419             :         atommem(TYPE##Strlen);                                          \
     420             :         if (is_##TYPE##_nil(*src)) {                                    \
     421             :                 if (external) {                                         \
     422             :                         strcpy(*dst, "nil");                          \
     423             :                         return 3;                                       \
     424             :                 }                                                       \
     425             :                 strcpy(*dst, str_nil);                                  \
     426             :                 return 1;                                               \
     427             :         }                                                               \
     428             :         return snprintf(*dst, *len, FMT, FMTCAST *src);                 \
     429             : }
     430             : 
     431             : #define base10(x)       ((x) - '0')
     432             : 
     433             : #define base16(x)       (((x) >= 'a' && (x) <= 'f') ? ((x) - 'a' + 10) : ((x) >= 'A' && (x) <= 'F') ? ((x) - 'A' + 10) : (x) - '0')
     434             : #define mult16(x)       ((x) << 4)
     435             : 
     436             : static void *
     437           0 : voidRead(void *a, size_t *dstlen, stream *s, size_t cnt)
     438             : {
     439           0 :         (void) dstlen;
     440           0 :         (void) s;
     441           0 :         (void) cnt;
     442           0 :         return a;
     443             : }
     444             : 
     445             : static gdk_return
     446           0 : voidWrite(const void *a, stream *s, size_t cnt)
     447             : {
     448           0 :         (void) a;
     449           0 :         (void) s;
     450           0 :         (void) cnt;
     451           0 :         return GDK_SUCCEED;
     452             : }
     453             : 
     454             : /*
     455             :  * Converts string values such as TRUE/FALSE/true/false etc to 1/0/NULL.
     456             :  * Switched from byte-to-byte compare to library function strncasecmp,
     457             :  * experiments showed that library function is even slightly faster and we
     458             :  * now also support True/False (and trUe/FAlSE should this become a thing).
     459             :  */
     460             : static ssize_t
     461           0 : mskFromStr(const char *src, size_t *len, msk **dst, bool external)
     462             : {
     463           0 :         const char *p = src;
     464             : 
     465           0 :         (void) external;
     466           0 :         atommem(sizeof(msk));
     467             : 
     468           0 :         if (strNil(src))
     469             :                 return -1;
     470             : 
     471           0 :         while (GDKisspace(*p))
     472           0 :                 p++;
     473           0 :         if (*p == '0') {
     474           0 :                 **dst = 0;
     475           0 :                 p++;
     476           0 :         } else if (*p == '1') {
     477           0 :                 **dst = 1;
     478           0 :                 p++;
     479             :         } else {
     480             :                 return -1;
     481             :         }
     482           0 :         while (GDKisspace(*p))
     483           0 :                 p++;
     484           0 :         return (ssize_t) (p - src);
     485             : }
     486             : 
     487             : static ssize_t
     488           0 : mskToStr(char **dst, size_t *len, const msk *src, bool external)
     489             : {
     490           0 :         (void) external;
     491           0 :         atommem(2);
     492           0 :         strcpy(*dst, *src ? "1" : "0");
     493           0 :         return 1;
     494             : }
     495             : 
     496             : ssize_t
     497       15590 : bitFromStr(const char *src, size_t *len, bit **dst, bool external)
     498             : {
     499       15590 :         const char *p = src;
     500             : 
     501       15590 :         atommem(sizeof(bit));
     502             : 
     503       15590 :         **dst = bit_nil;
     504             : 
     505       15590 :         if (strNil(src))
     506             :                 return 1;
     507             : 
     508       15590 :         while (GDKisspace(*p))
     509           0 :                 p++;
     510       15590 :         if (*p == '0') {
     511         294 :                 **dst = FALSE;
     512         294 :                 p++;
     513       15296 :         } else if (*p == '1') {
     514          60 :                 **dst = TRUE;
     515          60 :                 p++;
     516       15236 :         } else if (strncasecmp(p, "true",  4) == 0) {
     517       10272 :                 **dst = TRUE;
     518       10272 :                 p += 4;
     519        4964 :         } else if (strncasecmp(p, "false", 5) == 0) {
     520        4932 :                 **dst = FALSE;
     521        4932 :                 p += 5;
     522          32 :         } else if (external && strncasecmp(p, "nil",   3) == 0) {
     523           0 :                 p += 3;
     524             :         } else {
     525             :                 return -1;
     526             :         }
     527       15558 :         while (GDKisspace(*p))
     528           0 :                 p++;
     529       15558 :         return (ssize_t) (p - src);
     530             : }
     531             : 
     532             : ssize_t
     533       19306 : bitToStr(char **dst, size_t *len, const bit *src, bool external)
     534             : {
     535       19306 :         atommem(6);
     536             : 
     537       19306 :         if (is_bit_nil(*src)) {
     538          25 :                 if (external) {
     539          25 :                         strcpy(*dst, "nil");
     540          25 :                         return 3;
     541             :                 }
     542           0 :                 strcpy(*dst, str_nil);
     543           0 :                 return 1;
     544             :         }
     545       19281 :         if (*src) {
     546        8563 :                 strcpy(*dst, "true");
     547        8563 :                 return 4;
     548             :         }
     549       10718 :         strcpy(*dst, "false");
     550       10718 :         return 5;
     551             : }
     552             : 
     553             : ssize_t
     554           0 : batFromStr(const char *src, size_t *len, bat **dst, bool external)
     555             : {
     556           0 :         char *s;
     557           0 :         const char *t, *r = src;
     558           0 :         int c;
     559           0 :         bat bid = 0;
     560             : 
     561           0 :         atommem(sizeof(bat));
     562             : 
     563           0 :         if (strNil(src)) {
     564           0 :                 **dst = bat_nil;
     565           0 :                 return 1;
     566             :         }
     567             : 
     568           0 :         while (GDKisspace(*r))
     569           0 :                 r++;
     570             : 
     571           0 :         if (external && strcmp(r, "nil") == 0) {
     572           0 :                 **dst = bat_nil;
     573           0 :                 return (ssize_t) (r - src) + 3;
     574             :         }
     575             : 
     576           0 :         if (*r == '<')
     577           0 :                 r++;
     578           0 :         t = r;
     579           0 :         while ((c = *t) && (c == '_' || GDKisalnum(c)))
     580           0 :                 t++;
     581             : 
     582           0 :         s = GDKstrndup(r, t - r);
     583           0 :         if (s == NULL)
     584             :                 return -1;
     585           0 :         bid = BBPindex(s);
     586           0 :         GDKfree(s);
     587           0 :         **dst = bid == 0 ? bat_nil : bid;
     588           0 :         return (ssize_t) (t + (c == '>') - src);
     589             : }
     590             : 
     591             : ssize_t
     592           0 : batToStr(char **dst, size_t *len, const bat *src, bool external)
     593             : {
     594           0 :         bat b = *src;
     595           0 :         size_t i;
     596           0 :         str s;
     597             : 
     598           0 :         if (is_bat_nil(b) || !BBPcheck(b) || (s = BBP_logical(b)) == NULL || *s == 0) {
     599           0 :                 atommem(4);
     600           0 :                 if (external) {
     601           0 :                         strcpy(*dst, "nil");
     602           0 :                         return 3;
     603             :                 }
     604           0 :                 strcpy(*dst, str_nil);
     605           0 :                 return 1;
     606             :         }
     607           0 :         i = strlen(s) + 3;
     608           0 :         atommem(i);
     609           0 :         return (ssize_t) strconcat_len(*dst, *len, "<", s, ">", NULL);
     610             : }
     611             : 
     612             : 
     613             : /*
     614             :  * numFromStr parses the head of the string for a number, accepting an
     615             :  * optional sign. The code has been prepared to continue parsing by
     616             :  * returning the number of characters read.  Both overflow and
     617             :  * incorrect syntax (not a number) result in the function returning 0
     618             :  * and setting the destination to nil.
     619             :  */
     620             : struct maxdiv {
     621             :         /* if we want to multiply a value with scale, the value must
     622             :          * be no larger than maxval for there to not be overflow */
     623             : #ifdef HAVE_HGE
     624             :         hge scale, maxval;
     625             : #else
     626             :         lng scale, maxval;
     627             : #endif
     628             : };
     629             : static const struct maxdiv maxdiv[] = {
     630             : #ifdef HAVE_HGE
     631             :         /* maximum hge value: 170141183460469231731687303715884105727 (2**127-1)
     632             :          * GCC doesn't currently support integer constants that don't
     633             :          * fit in 8 bytes, so we split large values up*/
     634             :         {(hge) LL_CONSTANT(1), (hge) LL_CONSTANT(17014118346046923173U) * LL_CONSTANT(10000000000000000000U)+ (hge) LL_CONSTANT(1687303715884105727)},
     635             :         {(hge) LL_CONSTANT(10), (hge) LL_CONSTANT(17014118346046923173U) * LL_CONSTANT(1000000000000000000) + (hge) LL_CONSTANT(168730371588410572)},
     636             :         {(hge) LL_CONSTANT(100), (hge) LL_CONSTANT(17014118346046923173U) * LL_CONSTANT(100000000000000000) + (hge) LL_CONSTANT(16873037158841057)},
     637             :         {(hge) LL_CONSTANT(1000), (hge) LL_CONSTANT(17014118346046923173U) * LL_CONSTANT(10000000000000000) + (hge) LL_CONSTANT(1687303715884105)},
     638             :         {(hge) LL_CONSTANT(10000), (hge) LL_CONSTANT(17014118346046923173U) * LL_CONSTANT(1000000000000000) + (hge) LL_CONSTANT(168730371588410)},
     639             :         {(hge) LL_CONSTANT(100000), (hge) LL_CONSTANT(17014118346046923173U) * LL_CONSTANT(100000000000000) + (hge) LL_CONSTANT(16873037158841)},
     640             :         {(hge) LL_CONSTANT(1000000), (hge) LL_CONSTANT(17014118346046923173U) * LL_CONSTANT(10000000000000) + (hge) LL_CONSTANT(1687303715884)},
     641             :         {(hge) LL_CONSTANT(10000000), (hge) LL_CONSTANT(17014118346046923173U) * LL_CONSTANT(1000000000000) + (hge) LL_CONSTANT(168730371588)},
     642             :         {(hge) LL_CONSTANT(100000000), (hge) LL_CONSTANT(17014118346046923173U) * LL_CONSTANT(100000000000) + (hge) LL_CONSTANT(16873037158)},
     643             :         {(hge) LL_CONSTANT(1000000000), (hge) LL_CONSTANT(17014118346046923173U) * LL_CONSTANT(10000000000) + (hge) LL_CONSTANT(1687303715)},
     644             :         {(hge) LL_CONSTANT(10000000000), (hge) LL_CONSTANT(17014118346046923173U) * LL_CONSTANT(1000000000) + (hge) LL_CONSTANT(168730371)},
     645             :         {(hge) LL_CONSTANT(100000000000), (hge) LL_CONSTANT(17014118346046923173U) * LL_CONSTANT(100000000) + (hge) LL_CONSTANT(16873037)},
     646             :         {(hge) LL_CONSTANT(1000000000000), (hge) LL_CONSTANT(17014118346046923173U) * LL_CONSTANT(10000000) + (hge) LL_CONSTANT(1687303)},
     647             :         {(hge) LL_CONSTANT(10000000000000), (hge) LL_CONSTANT(17014118346046923173U) * LL_CONSTANT(1000000) + (hge) LL_CONSTANT(168730)},
     648             :         {(hge) LL_CONSTANT(100000000000000), (hge) LL_CONSTANT(17014118346046923173U) * LL_CONSTANT(100000) + (hge) LL_CONSTANT(16873)},
     649             :         {(hge) LL_CONSTANT(1000000000000000), (hge) LL_CONSTANT(17014118346046923173U) * LL_CONSTANT(10000) + (hge) LL_CONSTANT(1687)},
     650             :         {(hge) LL_CONSTANT(10000000000000000), (hge) LL_CONSTANT(17014118346046923173U) * LL_CONSTANT(1000) + (hge) LL_CONSTANT(168)},
     651             :         {(hge) LL_CONSTANT(100000000000000000), (hge) LL_CONSTANT(17014118346046923173U) * LL_CONSTANT(100) + (hge) LL_CONSTANT(16)},
     652             :         {(hge) LL_CONSTANT(1000000000000000000), (hge) LL_CONSTANT(17014118346046923173U) * LL_CONSTANT(10) + (hge) LL_CONSTANT(1)},
     653             :         {(hge) LL_CONSTANT(10000000000000000000U) * LL_CONSTANT(1), (hge) LL_CONSTANT(17014118346046923173U)},
     654             :         {(hge) LL_CONSTANT(10000000000000000000U) * LL_CONSTANT(10), (hge) LL_CONSTANT(1701411834604692317)},
     655             :         {(hge) LL_CONSTANT(10000000000000000000U) * LL_CONSTANT(100), (hge) LL_CONSTANT(170141183460469231)},
     656             :         {(hge) LL_CONSTANT(10000000000000000000U) * LL_CONSTANT(1000), (hge) LL_CONSTANT(17014118346046923)},
     657             :         {(hge) LL_CONSTANT(10000000000000000000U) * LL_CONSTANT(10000), (hge) LL_CONSTANT(1701411834604692)},
     658             :         {(hge) LL_CONSTANT(10000000000000000000U) * LL_CONSTANT(100000), (hge) LL_CONSTANT(170141183460469)},
     659             :         {(hge) LL_CONSTANT(10000000000000000000U) * LL_CONSTANT(1000000), (hge) LL_CONSTANT(17014118346046)},
     660             :         {(hge) LL_CONSTANT(10000000000000000000U) * LL_CONSTANT(10000000), (hge) LL_CONSTANT(1701411834604)},
     661             :         {(hge) LL_CONSTANT(10000000000000000000U) * LL_CONSTANT(100000000), (hge) LL_CONSTANT(170141183460)},
     662             :         {(hge) LL_CONSTANT(10000000000000000000U) * LL_CONSTANT(1000000000), (hge) LL_CONSTANT(17014118346)},
     663             :         {(hge) LL_CONSTANT(10000000000000000000U) * LL_CONSTANT(10000000000), (hge) LL_CONSTANT(1701411834)},
     664             :         {(hge) LL_CONSTANT(10000000000000000000U) * LL_CONSTANT(100000000000), (hge) LL_CONSTANT(170141183)},
     665             :         {(hge) LL_CONSTANT(10000000000000000000U) * LL_CONSTANT(1000000000000), (hge) LL_CONSTANT(17014118)},
     666             :         {(hge) LL_CONSTANT(10000000000000000000U) * LL_CONSTANT(10000000000000), (hge) LL_CONSTANT(1701411)},
     667             :         {(hge) LL_CONSTANT(10000000000000000000U) * LL_CONSTANT(100000000000000), (hge) LL_CONSTANT(170141)},
     668             :         {(hge) LL_CONSTANT(10000000000000000000U) * LL_CONSTANT(1000000000000000), (hge) LL_CONSTANT(17014)},
     669             :         {(hge) LL_CONSTANT(10000000000000000000U) * LL_CONSTANT(10000000000000000), (hge) LL_CONSTANT(1701)},
     670             :         {(hge) LL_CONSTANT(10000000000000000000U) * LL_CONSTANT(100000000000000000), (hge) LL_CONSTANT(170)},
     671             :         {(hge) LL_CONSTANT(10000000000000000000U) * LL_CONSTANT(1000000000000000000), (hge) LL_CONSTANT(17)},
     672             :         {(hge) LL_CONSTANT(10000000000000000000U) * LL_CONSTANT(10000000000000000000U),(hge) LL_CONSTANT(1)},
     673             : #else
     674             :         /* maximum lng value: 9223372036854775807 (2**63-1) */
     675             :         {LL_CONSTANT(1), LL_CONSTANT(9223372036854775807)},
     676             :         {LL_CONSTANT(10), LL_CONSTANT(922337203685477580)},
     677             :         {LL_CONSTANT(100), LL_CONSTANT(92233720368547758)},
     678             :         {LL_CONSTANT(1000), LL_CONSTANT(9223372036854775)},
     679             :         {LL_CONSTANT(10000), LL_CONSTANT(922337203685477)},
     680             :         {LL_CONSTANT(100000), LL_CONSTANT(92233720368547)},
     681             :         {LL_CONSTANT(1000000), LL_CONSTANT(9223372036854)},
     682             :         {LL_CONSTANT(10000000), LL_CONSTANT(922337203685)},
     683             :         {LL_CONSTANT(100000000), LL_CONSTANT(92233720368)},
     684             :         {LL_CONSTANT(1000000000), LL_CONSTANT(9223372036)},
     685             :         {LL_CONSTANT(10000000000), LL_CONSTANT(922337203)},
     686             :         {LL_CONSTANT(100000000000), LL_CONSTANT(92233720)},
     687             :         {LL_CONSTANT(1000000000000), LL_CONSTANT(9223372)},
     688             :         {LL_CONSTANT(10000000000000), LL_CONSTANT(922337)},
     689             :         {LL_CONSTANT(100000000000000), LL_CONSTANT(92233)},
     690             :         {LL_CONSTANT(1000000000000000), LL_CONSTANT(9223)},
     691             :         {LL_CONSTANT(10000000000000000), LL_CONSTANT(922)},
     692             :         {LL_CONSTANT(100000000000000000), LL_CONSTANT(92)},
     693             :         {LL_CONSTANT(1000000000000000000), LL_CONSTANT(9)},
     694             : #endif
     695             : };
     696             : static const int maxmod10 = 7;  /* (int) (maxdiv[0].maxval % 10) */
     697             : 
     698             : static ssize_t
     699   236707010 : numFromStr(const char *src, size_t *len, void **dst, int tp, bool external)
     700             : {
     701   236707010 :         const char *p = src;
     702   236707010 :         size_t sz = ATOMsize(tp);
     703             : #ifdef HAVE_HGE
     704   236707010 :         hge base = 0;
     705             : #else
     706             :         lng base = 0;
     707             : #endif
     708   236707010 :         int sign = 1;
     709             : 
     710             :         /* a valid number has the following syntax:
     711             :          * [-+]?[0-9]+([eE][0-9]+)?(LL)? -- PCRE syntax, or in other words
     712             :          * optional sign, one or more digits, optional exponent, optional LL
     713             :          * the exponent has the following syntax:
     714             :          * lower or upper case letter E, one or more digits
     715             :          * embedded spaces are not allowed
     716             :          * the optional LL at the end are only allowed for lng and hge
     717             :          * values */
     718   236707010 :         atommem(sz);
     719             : 
     720   236707010 :         if (strNil(src)) {
     721           0 :                 memcpy(*dst, ATOMnilptr(tp), sz);
     722           0 :                 return 1;
     723             :         }
     724             : 
     725   236707108 :         while (GDKisspace(*p))
     726          98 :                 p++;
     727   236707010 :         if (!GDKisdigit(*p)) {
     728       11665 :                 switch (*p) {
     729           5 :                 case 'n':
     730           5 :                         if (external) {
     731           0 :                                 memcpy(*dst, ATOMnilptr(tp), sz);
     732           0 :                                 if (p[1] == 'i' && p[2] == 'l') {
     733           0 :                                         p += 3;
     734           0 :                                         return (ssize_t) (p - src);
     735             :                                 }
     736             :                         }
     737           5 :                         GDKerror("not a number");
     738           5 :                         goto bailout;
     739       11476 :                 case '-':
     740       11476 :                         sign = -1;
     741       11476 :                         p++;
     742       11476 :                         break;
     743           1 :                 case '+':
     744           1 :                         p++;
     745           1 :                         break;
     746             :                 }
     747       11660 :                 if (!GDKisdigit(*p)) {
     748         189 :                         GDKerror("not a number");
     749         189 :                         goto bailout;
     750             :                 }
     751             :         }
     752   575807553 :         do {
     753   575807553 :                 int dig = base10(*p);
     754   575807553 :                 if (base > maxdiv[1].maxval ||
     755           1 :                     (base == maxdiv[1].maxval && dig > maxmod10)) {
     756             :                         /* overflow */
     757           0 :                         goto overflow;
     758             :                 }
     759   575807553 :                 base = 10 * base + dig;
     760   575807553 :                 p++;
     761   575807553 :         } while (GDKisdigit(*p));
     762   236706816 :         if ((*p == 'e' || *p == 'E') && GDKisdigit(p[1])) {
     763           7 :                 p++;
     764           7 :                 if (base == 0) {
     765             :                         /* if base is 0, any exponent will do, the
     766             :                          * result is still 0 */
     767           0 :                         while (GDKisdigit(*p))
     768           0 :                                 p++;
     769             :                 } else {
     770             :                         int exp = 0;
     771          11 :                         do {
     772             :                                 /* this calculation cannot overflow */
     773          11 :                                 exp = exp * 10 + base10(*p);
     774          11 :                                 if (exp >= (int) (sizeof(maxdiv) / sizeof(maxdiv[0]))) {
     775             :                                         /* overflow */
     776           0 :                                         goto overflow;
     777             :                                 }
     778          11 :                                 p++;
     779          11 :                         } while (GDKisdigit(*p));
     780           7 :                         if (base > maxdiv[exp].maxval) {
     781             :                                 /* overflow */
     782           0 :                                 goto overflow;
     783             :                         }
     784           7 :                         base *= maxdiv[exp].scale;
     785             :                 }
     786             :         }
     787   236706816 :         base *= sign;
     788   236706816 :         switch (sz) {
     789       53371 :         case 1: {
     790       53371 :                 bte **dstbte = (bte **) dst;
     791       53371 :                 if (base < GDK_bte_min || base > GDK_bte_max) {
     792           0 :                         goto overflow;
     793             :                 }
     794       53371 :                 **dstbte = (bte) base;
     795       53371 :                 break;
     796             :         }
     797      386913 :         case 2: {
     798      386913 :                 sht **dstsht = (sht **) dst;
     799      386913 :                 if (base < GDK_sht_min || base > GDK_sht_max) {
     800           3 :                         goto overflow;
     801             :                 }
     802      386910 :                 **dstsht = (sht) base;
     803      386910 :                 break;
     804             :         }
     805   232891891 :         case 4: {
     806   232891891 :                 int **dstint = (int **) dst;
     807   232891891 :                 if (base < GDK_int_min || base > GDK_int_max) {
     808           2 :                         goto overflow;
     809             :                 }
     810   232891889 :                 **dstint = (int) base;
     811   232891889 :                 break;
     812             :         }
     813     1911982 :         case 8: {
     814     1911982 :                 lng **dstlng = (lng **) dst;
     815             : #ifdef HAVE_HGE
     816     1911982 :                 if (base < GDK_lng_min || base > GDK_lng_max) {
     817           4 :                         goto overflow;
     818             :                 }
     819             : #endif
     820     1911978 :                 **dstlng = (lng) base;
     821     1911978 :                 if (p[0] == 'L' && p[1] == 'L')
     822           0 :                         p += 2;
     823             :                 break;
     824             :         }
     825             : #ifdef HAVE_HGE
     826     1462659 :         case 16: {
     827     1462659 :                 hge **dsthge = (hge **) dst;
     828     1462659 :                 **dsthge = (hge) base;
     829     1462659 :                 if (p[0] == 'L' && p[1] == 'L')
     830           0 :                         p += 2;
     831             :                 break;
     832             :         }
     833             : #endif
     834             :         }
     835   236706923 :         while (GDKisspace(*p))
     836         116 :                 p++;
     837   236706807 :         return (ssize_t) (p - src);
     838             : 
     839             :   overflow:
     840           9 :         while (GDKisdigit(*p))
     841           0 :                 p++;
     842           9 :         GDKerror("overflow: \"%.*s\" does not fit in %s\n",
     843             :                  (int) (p - src), src, ATOMname(tp));
     844         203 :   bailout:
     845         203 :         memcpy(*dst, ATOMnilptr(tp), sz);
     846         203 :         return -1;
     847             : }
     848             : 
     849             : ssize_t
     850       52871 : bteFromStr(const char *src, size_t *len, bte **dst, bool external)
     851             : {
     852       52871 :         return numFromStr(src, len, (void **) dst, TYPE_bte, external);
     853             : }
     854             : 
     855             : ssize_t
     856      377755 : shtFromStr(const char *src, size_t *len, sht **dst, bool external)
     857             : {
     858      377755 :         return numFromStr(src, len, (void **) dst, TYPE_sht, external);
     859             : }
     860             : 
     861             : ssize_t
     862   223390182 : intFromStr(const char *src, size_t *len, int **dst, bool external)
     863             : {
     864   223390182 :         return numFromStr(src, len, (void **) dst, TYPE_int, external);
     865             : }
     866             : 
     867             : ssize_t
     868     1912046 : lngFromStr(const char *src, size_t *len, lng **dst, bool external)
     869             : {
     870     1912046 :         return numFromStr(src, len, (void **) dst, TYPE_lng, external);
     871             : }
     872             : 
     873             : #ifdef HAVE_HGE
     874             : ssize_t
     875     1462578 : hgeFromStr(const char *src, size_t *len, hge **dst, bool external)
     876             : {
     877     1462578 :         return numFromStr(src, len, (void **) dst, TYPE_hge, external);
     878             : }
     879             : #endif
     880             : 
     881             : #define atom_io(TYPE, NAME, CAST)                                       \
     882             : static TYPE *                                                           \
     883             : TYPE##Read(TYPE *A, size_t *dstlen, stream *s, size_t cnt)              \
     884             : {                                                                       \
     885             :         TYPE *a = A;                                                    \
     886             :         if (a == NULL || *dstlen < cnt * sizeof(TYPE)) {             \
     887             :                 if ((a = GDKrealloc(a, cnt * sizeof(TYPE))) == NULL)    \
     888             :                         return NULL;                                    \
     889             :                 *dstlen = cnt * sizeof(TYPE);                           \
     890             :         }                                                               \
     891             :         if (mnstr_read##NAME##Array(s, (CAST *) a, cnt) == 0 ||         \
     892             :             mnstr_errnr(s) != MNSTR_NO__ERROR) {                        \
     893             :                 if (a != A)                                             \
     894             :                         GDKfree(a);                                     \
     895             :                 return NULL;                                            \
     896             :         }                                                               \
     897             :         return a;                                                       \
     898             : }                                                                       \
     899             : static gdk_return                                                       \
     900             : TYPE##Write(const TYPE *a, stream *s, size_t cnt)                       \
     901             : {                                                                       \
     902             :         return mnstr_write##NAME##Array(s, (const CAST *) a, cnt) ?     \
     903             :                 GDK_SUCCEED : GDK_FAIL;                                 \
     904             : }
     905             : 
     906             : static gdk_return
     907      125642 : mskWrite(const msk *a, stream *s, size_t cnt)
     908             : {
     909      125642 :         if (cnt == 0)
     910             :                 return GDK_SUCCEED;
     911      125642 :         if (cnt == 1)
     912      125642 :                 return mnstr_writeBte(s, (int8_t) *a) ? GDK_SUCCEED : GDK_FAIL;
     913             :         return GDK_FAIL;
     914             : }
     915             : 
     916             : static void *
     917      123173 : mskRead(msk *A, size_t *dstlen, stream *s, size_t cnt)
     918             : {
     919      123173 :         int8_t v;
     920      123173 :         msk *a = A;
     921      123173 :         if (cnt != 1)
     922             :                 return NULL;
     923      123173 :         if (a == NULL || *dstlen == 0) {
     924           0 :                 if ((a = GDKrealloc(a, 1)) == NULL)
     925             :                         return NULL;
     926           0 :                 *dstlen = 1;
     927             :         }
     928      123173 :         if (mnstr_readBte(s, &v) != 1) {
     929           0 :                 if (a != A)
     930           0 :                         GDKfree(a);
     931           0 :                 return NULL;
     932             :         }
     933      123173 :         *a = v != 0;
     934      123173 :         return a;
     935             : }
     936             : 
     937        7248 : atom_io(bit, Bte, bte)
     938             : 
     939       42925 : atomtostr(bte, "%hhd", )
     940      256596 : atom_io(bte, Bte, bte)
     941             : 
     942     1070000 : atomtostr(sht, "%hd", )
     943        8366 : atom_io(sht, Sht, sht)
     944             : 
     945    15223268 : atomtostr(int, "%d", )
     946      222252 : atom_io(int, Int, int)
     947             : 
     948      165807 : atomtostr(lng, LLFMT, )
     949       18817 : atom_io(lng, Lng, lng)
     950             : 
     951             : #ifdef HAVE_HGE
     952             : #define HGE_LL018FMT "%018" PRId64
     953             : #define HGE_LL18DIGITS LL_CONSTANT(1000000000000000000)
     954             : #define HGE_ABS(a) (((a) < 0) ? -(a) : (a))
     955             : ssize_t
     956        3295 : hgeToStr(char **dst, size_t *len, const hge *src, bool external)
     957             : {
     958        3295 :         atommem(hgeStrlen);
     959        3295 :         if (is_hge_nil(*src)) {
     960           1 :                 if (external) {
     961           1 :                         assert(*len >= strlen("nil") + 1);
     962           1 :                         strcpy(*dst, "nil");
     963           1 :                         return 3;
     964             :                 }
     965           0 :                 assert(*len >= strlen(str_nil) + 1);
     966           0 :                 strcpy(*dst, str_nil);
     967           0 :                 return 1;
     968             :         }
     969        3294 :         if ((hge) GDK_lng_min <= *src && *src <= (hge) GDK_lng_max) {
     970        3177 :                 lng s = (lng) *src;
     971        3177 :                 return lngToStr(dst, len, &s, external);
     972             :         } else {
     973         117 :                 hge s = *src / HGE_LL18DIGITS;
     974         117 :                 ssize_t llen = hgeToStr(dst, len, &s, external);
     975         117 :                 if (llen < 0)
     976             :                         return llen;
     977         117 :                 snprintf(*dst + llen, *len - llen, HGE_LL018FMT,
     978         117 :                          (lng) HGE_ABS(*src % HGE_LL18DIGITS));
     979         117 :                 return strlen(*dst);
     980             :         }
     981             : }
     982         954 : atom_io(hge, Hge, hge)
     983             : #endif
     984             : 
     985             : ssize_t
     986           0 : ptrFromStr(const char *src, size_t *len, ptr **dst, bool external)
     987             : {
     988           0 :         size_t base = 0;
     989           0 :         const char *p = src;
     990             : 
     991           0 :         atommem(sizeof(ptr));
     992             : 
     993           0 :         **dst = ptr_nil;
     994           0 :         if (strNil(src))
     995             :                 return 1;
     996             : 
     997           0 :         while (GDKisspace(*p))
     998           0 :                 p++;
     999           0 :         if (external && strncmp(p, "nil", 3) == 0) {
    1000           0 :                 p += 3;
    1001             :         } else {
    1002           0 :                 if (p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) {
    1003           0 :                         p += 2;
    1004             :                 }
    1005           0 :                 if (!GDKisxdigit(*p)) {
    1006           0 :                         GDKerror("not a number\n");
    1007           0 :                         return -1;
    1008             :                 }
    1009           0 :                 while (GDKisxdigit(*p)) {
    1010           0 :                         if (base >= ((size_t) 1 << (8 * sizeof(size_t) - 4))) {
    1011           0 :                                 GDKerror("overflow\n");
    1012           0 :                                 return -1;
    1013             :                         }
    1014           0 :                         base = mult16(base) + base16(*p);
    1015           0 :                         p++;
    1016             :                 }
    1017           0 :                 **dst = (ptr) base;
    1018             :         }
    1019           0 :         while (GDKisspace(*p))
    1020           0 :                 p++;
    1021           0 :         return (ssize_t) (p - src);
    1022             : }
    1023             : 
    1024             : #ifdef _MSC_VER
    1025             : /* Windows doesn't put 0x in front whereas Linux does, so we do it ourselves */
    1026             : atomtostr(ptr, "0x%p", )
    1027             : #else
    1028           1 : atomtostr(ptr, "%p", )
    1029             : #endif
    1030             : 
    1031             : #if SIZEOF_VOID_P == SIZEOF_INT
    1032             : atom_io(ptr, Int, int)
    1033             : #else /* SIZEOF_VOID_P == SIZEOF_LNG */
    1034           0 : atom_io(ptr, Lng, lng)
    1035             : #endif
    1036             : 
    1037             : ssize_t
    1038      253702 : dblFromStr(const char *src, size_t *len, dbl **dst, bool external)
    1039             : {
    1040      253702 :         const char *p = src;
    1041      253702 :         ssize_t n = 0;
    1042      253702 :         double d;
    1043             : 
    1044             :         /* alloc memory */
    1045      253702 :         atommem(sizeof(dbl));
    1046             : 
    1047      253702 :         if (strNil(src)) {
    1048           0 :                 **dst = dbl_nil;
    1049           0 :                 return 1;
    1050             :         }
    1051             : 
    1052      253821 :         while (GDKisspace(*p))
    1053         119 :                 p++;
    1054      253702 :         if (external && strncmp(p, "nil", 3) == 0) {
    1055           0 :                 **dst = dbl_nil;
    1056           0 :                 p += 3;
    1057           0 :                 n = (ssize_t) (p - src);
    1058             :         } else {
    1059             :                 /* on overflow, strtod returns HUGE_VAL and sets
    1060             :                  * errno to ERANGE; on underflow, it returns a value
    1061             :                  * whose magnitude is no greater than the smallest
    1062             :                  * normalized double, and may or may not set errno to
    1063             :                  * ERANGE.  We accept underflow, but not overflow. */
    1064      253702 :                 char *pe;
    1065      253702 :                 errno = 0;
    1066      253702 :                 d = strtod(p, &pe);
    1067      284056 :                 if (p == pe)
    1068             :                         p = src; /* nothing converted */
    1069             :                 else
    1070      284022 :                         p = pe;
    1071      284056 :                 n = (ssize_t) (p - src);
    1072      284056 :                 if (n == 0 || (errno == ERANGE && (d < -1 || d > 1))
    1073      284014 :                     || !isfinite(d) /* no NaN or Infinite */
    1074             :                     ) {
    1075         104 :                         GDKerror("overflow or not a number\n");
    1076         104 :                         return -1;
    1077             :                 } else {
    1078      284002 :                         while (src[n] && GDKisspace(src[n]))
    1079          50 :                                 n++;
    1080      283952 :                         **dst = (dbl) d;
    1081             :                 }
    1082             :         }
    1083             :         return n;
    1084             : }
    1085             : 
    1086             : ssize_t
    1087       13927 : dblToStr(char **dst, size_t *len, const dbl *src, bool external)
    1088             : {
    1089       13927 :         int i;
    1090             : 
    1091       13927 :         atommem(dblStrlen);
    1092       13927 :         if (is_dbl_nil(*src)) {
    1093           7 :                 if (external) {
    1094           7 :                         strcpy(*dst, "nil");
    1095           7 :                         return 3;
    1096             :                 }
    1097           0 :                 strcpy(*dst, str_nil);
    1098           0 :                 return 1;
    1099             :         }
    1100       50115 :         for (i = 4; i < 18; i++) {
    1101       50117 :                 snprintf(*dst, *len, "%.*g", i, *src);
    1102       50117 :                 if (strtod(*dst, NULL) == *src)
    1103             :                         break;
    1104             :         }
    1105       13917 :         return (ssize_t) strlen(*dst);
    1106             : }
    1107             : 
    1108         767 : atom_io(dbl, Lng, lng)
    1109             : 
    1110             : ssize_t
    1111     1826592 : fltFromStr(const char *src, size_t *len, flt **dst, bool external)
    1112             : {
    1113     1826592 :         const char *p = src;
    1114     1826592 :         ssize_t n = 0;
    1115     1826592 :         float f;
    1116             : 
    1117             :         /* alloc memory */
    1118     1826592 :         atommem(sizeof(flt));
    1119             : 
    1120     1826592 :         if (strNil(src)) {
    1121           0 :                 **dst = flt_nil;
    1122           0 :                 return 1;
    1123             :         }
    1124             : 
    1125     1826657 :         while (GDKisspace(*p))
    1126          65 :                 p++;
    1127     1826592 :         if (external && strncmp(p, "nil", 3) == 0) {
    1128           0 :                 **dst = flt_nil;
    1129           0 :                 p += 3;
    1130           0 :                 n = (ssize_t) (p - src);
    1131             :         } else {
    1132             :                 /* on overflow, strtof returns HUGE_VALF and sets
    1133             :                  * errno to ERANGE; on underflow, it returns a value
    1134             :                  * whose magnitude is no greater than the smallest
    1135             :                  * normalized float, and may or may not set errno to
    1136             :                  * ERANGE.  We accept underflow, but not overflow. */
    1137     1826592 :                 char *pe;
    1138     1826592 :                 errno = 0;
    1139     1826592 :                 f = strtof(p, &pe);
    1140     1832449 :                 if (p == pe)
    1141             :                         p = src; /* nothing converted */
    1142             :                 else
    1143     1832441 :                         p = pe;
    1144     1832449 :                 n = (ssize_t) (p - src);
    1145     1832449 :                 if (n == 0 || (errno == ERANGE && (f < -1 || f > 1))
    1146     1832433 :                     || !isfinite(f) /* no NaN or infinite */) {
    1147          58 :                         GDKerror("overflow or not a number\n");
    1148          58 :                         return -1;
    1149             :                 } else {
    1150     1832430 :                         while (src[n] && GDKisspace(src[n]))
    1151          39 :                                 n++;
    1152     1832391 :                         if (f == -0)
    1153        6574 :                                 f = 0;
    1154     1832391 :                         **dst = (flt) f;
    1155             :                 }
    1156             :         }
    1157             :         return n;
    1158             : }
    1159             : 
    1160             : ssize_t
    1161        1840 : fltToStr(char **dst, size_t *len, const flt *src, bool external)
    1162             : {
    1163        1840 :         int i;
    1164             : 
    1165        1840 :         atommem(fltStrlen);
    1166        1840 :         if (is_flt_nil(*src)) {
    1167           5 :                 if (external) {
    1168           5 :                         strcpy(*dst, "nil");
    1169           5 :                         return 3;
    1170             :                 }
    1171           0 :                 strcpy(*dst, str_nil);
    1172           0 :                 return 1;
    1173             :         }
    1174        3293 :         for (i = 4; i < 10; i++) {
    1175        3293 :                 snprintf(*dst, *len, "%.*g", i, *src);
    1176        3293 :                 if (strtof(*dst, NULL) == *src)
    1177             :                         break;
    1178             :         }
    1179        1835 :         return (ssize_t) strlen(*dst);
    1180             : }
    1181             : 
    1182         217 : atom_io(flt, Int, int)
    1183             : 
    1184             : 
    1185             : /*
    1186             :  * String conversion routines.
    1187             :  */
    1188             : ssize_t
    1189          26 : OIDfromStr(const char *src, size_t *len, oid **dst, bool external)
    1190             : {
    1191             : #if SIZEOF_OID == SIZEOF_INT
    1192             :         int ui = 0, *uip = &ui;
    1193             : #else
    1194          26 :         lng ui = 0, *uip = &ui;
    1195             : #endif
    1196          26 :         size_t l = sizeof(ui);
    1197          26 :         ssize_t pos = 0;
    1198          26 :         const char *p = src;
    1199             : 
    1200          26 :         atommem(sizeof(oid));
    1201             : 
    1202          26 :         **dst = oid_nil;
    1203          26 :         if (strNil(src))
    1204             :                 return 1;
    1205             : 
    1206          26 :         while (GDKisspace(*p))
    1207           0 :                 p++;
    1208             : 
    1209          26 :         if (external && strncmp(p, "nil", 3) == 0)
    1210           0 :                 return (ssize_t) (p - src) + 3;
    1211             : 
    1212          26 :         if (*p >= '0' && *p <= '9') {
    1213             : #if SIZEOF_OID == SIZEOF_INT
    1214             :                 pos = intFromStr(p, &l, &uip, external);
    1215             : #else
    1216          25 :                 pos = lngFromStr(p, &l, &uip, external);
    1217             : #endif
    1218          25 :                 if (pos < 0)
    1219             :                         return pos;
    1220          25 :                 if (p[pos] == '@') {
    1221           7 :                         pos++;
    1222          14 :                         while (p[pos] >= '0' && p[pos] <= '9')
    1223           7 :                                 pos++;
    1224             :                 }
    1225          25 :                 if (ui >= 0) {
    1226          25 :                         **dst = ui;
    1227             :                 }
    1228          25 :                 p += pos;
    1229             :         } else {
    1230           1 :                 GDKerror("not an OID\n");
    1231           1 :                 return -1;
    1232             :         }
    1233          25 :         while (GDKisspace(*p))
    1234           0 :                 p++;
    1235          25 :         return (ssize_t) (p - src);
    1236             : }
    1237             : 
    1238             : ssize_t
    1239        4216 : OIDtoStr(char **dst, size_t *len, const oid *src, bool external)
    1240             : {
    1241        4216 :         atommem(oidStrlen);
    1242             : 
    1243        4216 :         if (is_oid_nil(*src)) {
    1244          16 :                 if (external) {
    1245          16 :                         strcpy(*dst, "nil");
    1246          16 :                         return 3;
    1247             :                 }
    1248           0 :                 strcpy(*dst, str_nil);
    1249           0 :                 return 1;
    1250             :         }
    1251        4200 :         return snprintf(*dst, *len, OIDFMT "@0", *src);
    1252             : }
    1253             : 
    1254             : static int
    1255     3494790 : UUIDcompare(const void *L, const void *R)
    1256             : {
    1257     3494790 :         const uuid *l = L, *r = R;
    1258     3494790 :         if (is_uuid_nil(*r))
    1259      439459 :                 return !is_uuid_nil(*l);
    1260     3055331 :         if (is_uuid_nil(*l))
    1261             :                 return -1;
    1262      401255 :         return memcmp(l->u, r->u, UUID_SIZE);
    1263             : }
    1264             : 
    1265             : static ssize_t
    1266         278 : UUIDfromString(const char *svalue, size_t *len, void **RETVAL, bool external)
    1267             : {
    1268         278 :         uuid **retval = (uuid **) RETVAL;
    1269         278 :         const char *s = svalue;
    1270             : 
    1271         278 :         if (*len < UUID_SIZE || *retval == NULL) {
    1272          54 :                 GDKfree(*retval);
    1273          54 :                 if ((*retval = GDKmalloc(UUID_SIZE)) == NULL)
    1274             :                         return -1;
    1275          54 :                 *len = UUID_SIZE;
    1276             :         }
    1277         278 :         if (external && strcmp(svalue, "nil") == 0) {
    1278           0 :                 **retval = uuid_nil;
    1279           0 :                 return 3;
    1280             :         }
    1281         278 :         if (strNil(svalue)) {
    1282          33 :                 **retval = uuid_nil;
    1283          33 :                 return 1;
    1284             :         }
    1285         247 :         while (GDKisspace(*s))
    1286           2 :                 s++;
    1287             :         /* we don't use uuid_parse since we accept UUIDs without hyphens */
    1288             :         uuid u;
    1289        3371 :         for (int i = 0, j = 0; i < UUID_SIZE; i++) {
    1290             :                 /* on select locations we allow a '-' in the source string */
    1291        3176 :                 if (j == 8 || j == 12 || j == 16 || j == 20) {
    1292         781 :                         if (*s == '-')
    1293         728 :                                 s++;
    1294             :                 }
    1295        3176 :                 if (*s >= '0' && *s <= '9')
    1296        1609 :                         u.u[i] = *s - '0';
    1297        1567 :                 else if ('a' <= *s && *s <= 'f')
    1298        1133 :                         u.u[i] = *s - 'a' + 10;
    1299         434 :                 else if ('A' <= *s && *s <= 'F')
    1300         402 :                         u.u[i] = *s - 'A' + 10;
    1301             :                 else
    1302          32 :                         goto bailout;
    1303        3144 :                 s++;
    1304        3144 :                 j++;
    1305        3144 :                 u.u[i] <<= 4;
    1306        3144 :                 if (*s >= '0' && *s <= '9')
    1307        1510 :                         u.u[i] |= *s - '0';
    1308        1634 :                 else if ('a' <= *s && *s <= 'f')
    1309        1264 :                         u.u[i] |= *s - 'a' + 10;
    1310         370 :                 else if ('A' <= *s && *s <= 'F')
    1311         352 :                         u.u[i] |= *s - 'A' + 10;
    1312             :                 else
    1313          18 :                         goto bailout;
    1314        3126 :                 s++;
    1315        3126 :                 j++;
    1316             :         }
    1317         201 :         while (GDKisspace(*s))
    1318           6 :                 s++;
    1319         195 :         if (*s != 0)
    1320          12 :                 goto bailout;
    1321         183 :         **retval = u;
    1322         183 :         return (ssize_t) (s - svalue);
    1323             : 
    1324          62 :   bailout:
    1325          62 :         **retval = uuid_nil;
    1326          62 :         return -1;
    1327             : }
    1328             : 
    1329             : static BUN
    1330           0 : UUIDhash(const void *v)
    1331             : {
    1332           0 :         return mix_uuid((const uuid *) v);
    1333             : }
    1334             : 
    1335             : static void *
    1336          22 : UUIDread(void *U, size_t *dstlen, stream *s, size_t cnt)
    1337             : {
    1338          22 :         uuid *u = U;
    1339          22 :         if (u == NULL || *dstlen < cnt * sizeof(uuid)) {
    1340           0 :                 if ((u = GDKrealloc(u, cnt * sizeof(uuid))) == NULL)
    1341             :                         return NULL;
    1342           0 :                 *dstlen = cnt * sizeof(uuid);
    1343             :         }
    1344          22 :         if (mnstr_read(s, u, UUID_SIZE, cnt) < (ssize_t) cnt) {
    1345           0 :                 if (u != U)
    1346           0 :                         GDKfree(u);
    1347           0 :                 return NULL;
    1348             :         }
    1349             :         return u;
    1350             : }
    1351             : 
    1352             : static gdk_return
    1353          23 : UUIDwrite(const void *u, stream *s, size_t cnt)
    1354             : {
    1355          23 :         return mnstr_write(s, u, UUID_SIZE, cnt) ? GDK_SUCCEED : GDK_FAIL;
    1356             : }
    1357             : 
    1358             : static ssize_t
    1359     1704624 : UUIDtoString(str *retval, size_t *len, const void *VALUE, bool external)
    1360             : {
    1361     1704624 :         const uuid *value = VALUE;
    1362     1704624 :         if (*len <= UUID_STRLEN || *retval == NULL) {
    1363         105 :                 if (*retval)
    1364           0 :                         GDKfree(*retval);
    1365         105 :                 if ((*retval = GDKmalloc(UUID_STRLEN + 1)) == NULL)
    1366             :                         return -1;
    1367           0 :                 *len = UUID_STRLEN + 1;
    1368             :         }
    1369     1704060 :         if (is_uuid_nil(*value)) {
    1370           0 :                 if (external) {
    1371           0 :                         assert(*len >= strlen("nil") + 1);
    1372           0 :                         strcpy(*retval, "nil");
    1373           0 :                         return 3;
    1374             :                 }
    1375           0 :                 assert(*len >= strlen(str_nil) + 1);
    1376           0 :                 strcpy(*retval, str_nil);
    1377           0 :                 return 1;
    1378             :         }
    1379     1704060 :         snprintf(*retval, *len,
    1380             :                          "%02x%02x%02x%02x-%02x%02x-%02x%02x"
    1381             :                          "-%02x%02x-%02x%02x%02x%02x%02x%02x",
    1382     1704060 :                          value->u[0], value->u[1], value->u[2], value->u[3],
    1383     1704060 :                          value->u[4], value->u[5], value->u[6], value->u[7],
    1384     1704060 :                          value->u[8], value->u[9], value->u[10], value->u[11],
    1385     1704060 :                          value->u[12], value->u[13], value->u[14], value->u[15]);
    1386     1704060 :         assert(strlen(*retval) == UUID_STRLEN);
    1387             :         return UUID_STRLEN;
    1388             : }
    1389             : 
    1390             : static const blob blob_nil = {
    1391             :         ~(size_t) 0
    1392             : };
    1393             : 
    1394             : size_t
    1395    14806101 : blobsize(size_t nitems)
    1396             : {
    1397    14806101 :         if (nitems == ~(size_t) 0)
    1398     5334265 :                 nitems = 0;
    1399    14806101 :         assert(offsetof(blob, data) + nitems <= VAR_MAX);
    1400    14806101 :         return (size_t) (offsetof(blob, data) + nitems);
    1401             : }
    1402             : 
    1403             : static int
    1404    14941197 : BLOBcmp(const void *L, const void *R)
    1405             : {
    1406    14941197 :         const blob *l = L, *r = R;
    1407    14941197 :         int c;
    1408    14941197 :         if (is_blob_nil(r))
    1409    10712649 :                 return !is_blob_nil(l);
    1410     4228548 :         if (is_blob_nil(l))
    1411             :                 return -1;
    1412        6621 :         if (l->nitems < r->nitems) {
    1413        2553 :                 c = memcmp(l->data, r->data, l->nitems);
    1414        2553 :                 if (c == 0)
    1415        2273 :                         return -1;
    1416             :         } else {
    1417        4068 :                 c = memcmp(l->data, r->data, r->nitems);
    1418        4068 :                 if (c == 0)
    1419        3554 :                         return l->nitems > r->nitems;
    1420             :         }
    1421             :         return c;
    1422             : }
    1423             : 
    1424             : static void
    1425          35 : BLOBdel(Heap *h, var_t *idx)
    1426             : {
    1427          35 :         HEAP_free(h, *idx);
    1428          35 : }
    1429             : 
    1430             : static BUN
    1431       11264 : BLOBhash(const void *B)
    1432             : {
    1433       11264 :         const blob *b = B;
    1434       11264 :         return (BUN) b->nitems;
    1435             : }
    1436             : 
    1437             : static void *
    1438          65 : BLOBread(void *A, size_t *dstlen, stream *s, size_t cnt)
    1439             : {
    1440          65 :         blob *a = A;
    1441          65 :         int len;
    1442             : 
    1443          65 :         (void) cnt;
    1444          65 :         assert(cnt == 1);
    1445          65 :         if (mnstr_readInt(s, &len) != 1 || len < 0)
    1446             :                 return NULL;
    1447          65 :         if (a == NULL || *dstlen < (size_t) len) {
    1448           0 :                 if ((a = GDKrealloc(a, (size_t) len)) == NULL)
    1449             :                         return NULL;
    1450           0 :                 *dstlen = (size_t) len;
    1451             :         }
    1452          65 :         if (mnstr_read(s, (char *) a, (size_t) len, 1) != 1) {
    1453           0 :                 GDKfree(a);
    1454           0 :                 return NULL;
    1455             :         }
    1456             :         return a;
    1457             : }
    1458             : 
    1459             : static gdk_return
    1460          65 : BLOBwrite(const void *A, stream *s, size_t cnt)
    1461             : {
    1462          65 :         const blob *a = A;
    1463          65 :         size_t len = blobsize(a->nitems);
    1464             : 
    1465          65 :         (void) cnt;
    1466          65 :         assert(cnt == 1);
    1467         130 :         if (!mnstr_writeInt(s, (int) len) /* 64bit: check for overflow */ ||
    1468          65 :                 mnstr_write(s, a, len, 1) < 0)
    1469           0 :                 return GDK_FAIL;
    1470             :         return GDK_SUCCEED;
    1471             : }
    1472             : 
    1473             : static size_t
    1474        1471 : BLOBlength(const void *P)
    1475             : {
    1476        1471 :         const blob *p = P;
    1477        1471 :         size_t l = blobsize(p->nitems); /* 64bit: check for overflow */
    1478        1471 :         assert(l <= (size_t) GDK_int_max);
    1479        1471 :         return l;
    1480             : }
    1481             : 
    1482             : static gdk_return
    1483         878 : BLOBheap(Heap *heap, size_t capacity)
    1484             : {
    1485         878 :         return HEAP_initialize(heap, capacity, 0, (int) sizeof(var_t));
    1486             : }
    1487             : 
    1488             : static var_t
    1489     7401085 : BLOBput(BAT *b, var_t *bun, const void *VAL)
    1490             : {
    1491     7401085 :         const blob *val = VAL;
    1492     7401085 :         char *base = NULL;
    1493             : 
    1494     7401085 :         *bun = HEAP_malloc(b, blobsize(val->nitems));
    1495     7401084 :         base = b->tvheap->base;
    1496     7401084 :         if (*bun != (var_t) -1) {
    1497     7401084 :                 memcpy(&base[*bun], val, blobsize(val->nitems));
    1498     7401084 :                 b->tvheap->dirty = true;
    1499             :         }
    1500     7401084 :         return *bun;
    1501             : }
    1502             : 
    1503             : static ssize_t
    1504         360 : BLOBtostr(str *tostr, size_t *l, const void *P, bool external)
    1505             : {
    1506         360 :         static const char hexit[] = "0123456789ABCDEF";
    1507         360 :         const blob *p = P;
    1508         360 :         char *s;
    1509         360 :         size_t i;
    1510         360 :         size_t expectedlen;
    1511             : 
    1512         360 :         if (is_blob_nil(p))
    1513           3 :                 expectedlen = external ? 4 : 2;
    1514             :         else
    1515         357 :                 expectedlen = p->nitems * 2 + 1;
    1516         360 :         if (*l < expectedlen || *tostr == NULL) {
    1517          39 :                 GDKfree(*tostr);
    1518          39 :                 *tostr = GDKmalloc(expectedlen);
    1519          39 :                 if (*tostr == NULL)
    1520             :                         return -1;
    1521          39 :                 *l = expectedlen;
    1522             :         }
    1523         360 :         if (is_blob_nil(p)) {
    1524           3 :                 if (external) {
    1525           3 :                         strcpy(*tostr, "nil");
    1526           3 :                         return 3;
    1527             :                 }
    1528           0 :                 strcpy(*tostr, str_nil);
    1529           0 :                 return 1;
    1530             :         }
    1531             : 
    1532         357 :         s = *tostr;
    1533             : 
    1534    10192172 :         for (i = 0; i < p->nitems; i++) {
    1535    10191815 :                 *s++ = hexit[(p->data[i] >> 4) & 15];
    1536    10191815 :                 *s++ = hexit[p->data[i] & 15];
    1537             :         }
    1538         357 :         *s = '\0';
    1539         357 :         return (ssize_t) (s - *tostr);
    1540             : }
    1541             : 
    1542             : static ssize_t
    1543        2393 : BLOBfromstr(const char *instr, size_t *l, void **VAL, bool external)
    1544             : {
    1545        2393 :         blob **val = (blob **) VAL;
    1546        2393 :         size_t i;
    1547        2393 :         size_t nitems;
    1548        2393 :         size_t nbytes;
    1549        2393 :         blob *result;
    1550        2393 :         const char *s = instr;
    1551             : 
    1552        4786 :         if (strNil(instr) || (external && strncmp(instr, "nil", 3) == 0)) {
    1553           0 :                 nbytes = blobsize(0);
    1554           0 :                 if (*l < nbytes || *val == NULL) {
    1555           0 :                         GDKfree(*val);
    1556           0 :                         if ((*val = GDKmalloc(nbytes)) == NULL)
    1557             :                                 return -1;
    1558             :                 }
    1559           0 :                 **val = blob_nil;
    1560           0 :                 return strNil(instr) ? 1 : 3;
    1561             :         }
    1562             : 
    1563             :         /* count hexits and check for hexits/space */
    1564    21957765 :         for (i = nitems = 0; instr[i]; i++) {
    1565    21955375 :                 if (GDKisxdigit(instr[i]))
    1566    21955372 :                         nitems++;
    1567           3 :                 else if (!GDKisspace(instr[i])) {
    1568           3 :                         GDKerror("Illegal char in blob\n");
    1569           3 :                         return -1;
    1570             :                 }
    1571             :         }
    1572        2390 :         if (nitems % 2 != 0) {
    1573           0 :                 GDKerror("Illegal blob length '%zu' (should be even)\n", nitems);
    1574           0 :                 return -1;
    1575             :         }
    1576        2390 :         nitems /= 2;
    1577        2390 :         nbytes = blobsize(nitems);
    1578             : 
    1579        2390 :         if (*l < nbytes || *val == NULL) {
    1580        2292 :                 GDKfree(*val);
    1581        2292 :                 *val = GDKmalloc(nbytes);
    1582        2292 :                 if( *val == NULL)
    1583             :                         return -1;
    1584        2292 :                 *l = nbytes;
    1585             :         }
    1586        2390 :         result = *val;
    1587        2390 :         result->nitems = nitems;
    1588             : 
    1589             :         /*
    1590             :            // Read the values of the blob.
    1591             :          */
    1592    10980076 :         for (i = 0; i < nitems; ++i) {
    1593    10977686 :                 int res = 0;
    1594             : 
    1595    10977686 :                 for (;;) {
    1596    10977686 :                         if (*s >= '0' && *s <= '9') {
    1597     5900546 :                                 res = *s - '0';
    1598     5077140 :                         } else if (*s >= 'A' && *s <= 'F') {
    1599     2651908 :                                 res = 10 + *s - 'A';
    1600     2425232 :                         } else if (*s >= 'a' && *s <= 'f') {
    1601     2425232 :                                 res = 10 + *s - 'a';
    1602             :                         } else {
    1603           0 :                                 assert(GDKisspace(*s));
    1604           0 :                                 s++;
    1605           0 :                                 continue;
    1606             :                         }
    1607    10977686 :                         break;
    1608             :                 }
    1609    10977686 :                 s++;
    1610    10977686 :                 res <<= 4;
    1611    10977686 :                 for (;;) {
    1612    10977686 :                         if (*s >= '0' && *s <= '9') {
    1613     5899417 :                                 res += *s - '0';
    1614     5078269 :                         } else if (*s >= 'A' && *s <= 'F') {
    1615     2654281 :                                 res += 10 + *s - 'A';
    1616     2423988 :                         } else if (*s >= 'a' && *s <= 'f') {
    1617     2423988 :                                 res += 10 + *s - 'a';
    1618             :                         } else {
    1619           0 :                                 assert(GDKisspace(*s));
    1620           0 :                                 s++;
    1621           0 :                                 continue;
    1622             :                         }
    1623    10977686 :                         break;
    1624             :                 }
    1625    10977686 :                 s++;
    1626             : 
    1627    10977686 :                 result->data[i] = (uint8_t) res;
    1628             :         }
    1629        2390 :         while (GDKisspace(*s))
    1630           0 :                 s++;
    1631             : 
    1632        2390 :         return (ssize_t) (s - instr);
    1633             : }
    1634             : 
    1635             : atomDesc BATatoms[MAXATOMS] = {
    1636             :         [TYPE_void] = {
    1637             :                 .name = "void",
    1638             :                 .storage = TYPE_void,
    1639             :                 .linear = true,
    1640             : #if SIZEOF_OID == SIZEOF_INT
    1641             :                 .atomNull = (void *) &int_nil,
    1642             :                 .atomCmp = (int (*)(const void *, const void *)) intCmp,
    1643             :                 .atomHash = (BUN (*)(const void *)) intHash,
    1644             : #else
    1645             :                 .atomNull = (void *) &lng_nil,
    1646             :                 .atomCmp = (int (*)(const void *, const void *)) lngCmp,
    1647             :                 .atomHash = (BUN (*)(const void *)) lngHash,
    1648             : #endif
    1649             :                 .atomFromStr = (ssize_t (*)(const char *, size_t *, void **, bool)) OIDfromStr,
    1650             :                 .atomToStr = (ssize_t (*)(char **, size_t *, const void *, bool)) OIDtoStr,
    1651             :                 .atomRead = (void *(*)(void *, size_t *, stream *, size_t)) voidRead,
    1652             :                 .atomWrite = (gdk_return (*)(const void *, stream *, size_t)) voidWrite,
    1653             :         },
    1654             :         [TYPE_bit] = {
    1655             :                 .name = "bit",
    1656             :                 .storage = TYPE_bte,
    1657             :                 .linear = true,
    1658             :                 .size = sizeof(bit),
    1659             :                 .atomNull = (void *) &bte_nil,
    1660             :                 .atomFromStr = (ssize_t (*)(const char *, size_t *, void **, bool)) bitFromStr,
    1661             :                 .atomToStr = (ssize_t (*)(char **, size_t *, const void *, bool)) bitToStr,
    1662             :                 .atomRead = (void *(*)(void *, size_t *, stream *, size_t)) bitRead,
    1663             :                 .atomWrite = (gdk_return (*)(const void *, stream *, size_t)) bitWrite,
    1664             :                 .atomCmp = (int (*)(const void *, const void *)) bteCmp,
    1665             :                 .atomHash = (BUN (*)(const void *)) bteHash,
    1666             :         },
    1667             :         [TYPE_msk] = {
    1668             :                 .name = "msk",
    1669             :                 .storage = TYPE_msk,
    1670             :                 .linear = false,
    1671             :                 .size = 1,      /* really 1/8 */
    1672             :                 .atomFromStr = (ssize_t (*)(const char *, size_t *, void **, bool)) mskFromStr,
    1673             :                 .atomToStr = (ssize_t (*)(char **, size_t *, const void *, bool)) mskToStr,
    1674             :                 .atomRead = (void *(*)(void *, size_t *, stream *, size_t)) mskRead,
    1675             :                 .atomWrite = (gdk_return (*)(const void *, stream *, size_t)) mskWrite,
    1676             :                 .atomCmp = (int (*)(const void *, const void *)) mskCmp,
    1677             :         },
    1678             :         [TYPE_bte] = {
    1679             :                 .name = "bte",
    1680             :                 .storage = TYPE_bte,
    1681             :                 .linear = true,
    1682             :                 .size = sizeof(bte),
    1683             :                 .atomNull = (void *) &bte_nil,
    1684             :                 .atomFromStr = (ssize_t (*)(const char *, size_t *, void **, bool)) bteFromStr,
    1685             :                 .atomToStr = (ssize_t (*)(char **, size_t *, const void *, bool)) bteToStr,
    1686             :                 .atomRead = (void *(*)(void *, size_t *, stream *, size_t)) bteRead,
    1687             :                 .atomWrite = (gdk_return (*)(const void *, stream *, size_t)) bteWrite,
    1688             :                 .atomCmp = (int (*)(const void *, const void *)) bteCmp,
    1689             :                 .atomHash = (BUN (*)(const void *)) bteHash,
    1690             :         },
    1691             :         [TYPE_sht] = {
    1692             :                 .name = "sht",
    1693             :                 .storage = TYPE_sht,
    1694             :                 .linear = true,
    1695             :                 .size = sizeof(sht),
    1696             :                 .atomNull = (void *) &sht_nil,
    1697             :                 .atomFromStr = (ssize_t (*)(const char *, size_t *, void **, bool)) shtFromStr,
    1698             :                 .atomToStr = (ssize_t (*)(char **, size_t *, const void *, bool)) shtToStr,
    1699             :                 .atomRead = (void *(*)(void *, size_t *, stream *, size_t)) shtRead,
    1700             :                 .atomWrite = (gdk_return (*)(const void *, stream *, size_t)) shtWrite,
    1701             :                 .atomCmp = (int (*)(const void *, const void *)) shtCmp,
    1702             :                 .atomHash = (BUN (*)(const void *)) shtHash,
    1703             :         },
    1704             :         [TYPE_int] = {
    1705             :                 .name = "int",
    1706             :                 .storage = TYPE_int,
    1707             :                 .linear = true,
    1708             :                 .size = sizeof(int),
    1709             :                 .atomNull = (void *) &int_nil,
    1710             :                 .atomFromStr = (ssize_t (*)(const char *, size_t *, void **, bool)) intFromStr,
    1711             :                 .atomToStr = (ssize_t (*)(char **, size_t *, const void *, bool)) intToStr,
    1712             :                 .atomRead = (void *(*)(void *, size_t *, stream *, size_t)) intRead,
    1713             :                 .atomWrite = (gdk_return (*)(const void *, stream *, size_t)) intWrite,
    1714             :                 .atomCmp = (int (*)(const void *, const void *)) intCmp,
    1715             :                 .atomHash = (BUN (*)(const void *)) intHash,
    1716             :         },
    1717             :         [TYPE_oid] = {
    1718             :                 .name = "oid",
    1719             :                 .linear = true,
    1720             :                 .size = sizeof(oid),
    1721             : #if SIZEOF_OID == SIZEOF_INT
    1722             :                 .storage = TYPE_int,
    1723             :                 .atomNull = (void *) &int_nil,
    1724             :                 .atomRead = (void *(*)(void *, size_t *, stream *, size_t)) intRead,
    1725             :                 .atomWrite = (gdk_return (*)(const void *, stream *, size_t)) intWrite,
    1726             :                 .atomCmp = (int (*)(const void *, const void *)) intCmp,
    1727             :                 .atomHash = (BUN (*)(const void *)) intHash,
    1728             : #else
    1729             :                 .storage = TYPE_lng,
    1730             :                 .atomNull = (void *) &lng_nil,
    1731             :                 .atomRead = (void *(*)(void *, size_t *, stream *, size_t)) lngRead,
    1732             :                 .atomWrite = (gdk_return (*)(const void *, stream *, size_t)) lngWrite,
    1733             :                 .atomCmp = (int (*)(const void *, const void *)) lngCmp,
    1734             :                 .atomHash = (BUN (*)(const void *)) lngHash,
    1735             : #endif
    1736             :                 .atomFromStr = (ssize_t (*)(const char *, size_t *, void **, bool)) OIDfromStr,
    1737             :                 .atomToStr = (ssize_t (*)(char **, size_t *, const void *, bool)) OIDtoStr,
    1738             :         },
    1739             :         [TYPE_ptr] = {
    1740             :                 .name = "ptr",
    1741             :                 .storage = TYPE_ptr,
    1742             :                 .linear = true,
    1743             :                 .size = sizeof(void *),
    1744             :                 .atomNull = (void *) &ptr_nil,
    1745             :                 .atomFromStr = (ssize_t (*)(const char *, size_t *, void **, bool)) ptrFromStr,
    1746             :                 .atomToStr = (ssize_t (*)(char **, size_t *, const void *, bool)) ptrToStr,
    1747             :                 .atomRead = (void *(*)(void *, size_t *, stream *, size_t)) ptrRead,
    1748             :                 .atomWrite = (gdk_return (*)(const void *, stream *, size_t)) ptrWrite,
    1749             : #if SIZEOF_VOID_P == SIZEOF_INT
    1750             :                 .atomCmp = (int (*)(const void *, const void *)) intCmp,
    1751             :                 .atomHash = (BUN (*)(const void *)) intHash,
    1752             : #else /* SIZEOF_VOID_P == SIZEOF_LNG */
    1753             :                 .atomCmp = (int (*)(const void *, const void *)) lngCmp,
    1754             :                 .atomHash = (BUN (*)(const void *)) lngHash,
    1755             : #endif
    1756             :         },
    1757             :         [TYPE_flt] = {
    1758             :                 .name = "flt",
    1759             :                 .storage = TYPE_flt,
    1760             :                 .linear = true,
    1761             :                 .size = sizeof(flt),
    1762             :                 .atomNull = (void *) &flt_nil,
    1763             :                 .atomFromStr = (ssize_t (*)(const char *, size_t *, void **, bool)) fltFromStr,
    1764             :                 .atomToStr = (ssize_t (*)(char **, size_t *, const void *, bool)) fltToStr,
    1765             :                 .atomRead = (void *(*)(void *, size_t *, stream *, size_t)) fltRead,
    1766             :                 .atomWrite = (gdk_return (*)(const void *, stream *, size_t)) fltWrite,
    1767             :                 .atomCmp = (int (*)(const void *, const void *)) fltCmp,
    1768             :                 .atomHash = (BUN (*)(const void *)) fltHash,
    1769             :         },
    1770             :         [TYPE_dbl] = {
    1771             :                 .name = "dbl",
    1772             :                 .storage = TYPE_dbl,
    1773             :                 .linear = true,
    1774             :                 .size = sizeof(dbl),
    1775             :                 .atomNull = (void *) &dbl_nil,
    1776             :                 .atomFromStr = (ssize_t (*)(const char *, size_t *, void **, bool)) dblFromStr,
    1777             :                 .atomToStr = (ssize_t (*)(char **, size_t *, const void *, bool)) dblToStr,
    1778             :                 .atomRead = (void *(*)(void *, size_t *, stream *, size_t)) dblRead,
    1779             :                 .atomWrite = (gdk_return (*)(const void *, stream *, size_t)) dblWrite,
    1780             :                 .atomCmp = (int (*)(const void *, const void *)) dblCmp,
    1781             :                 .atomHash = (BUN (*)(const void *)) dblHash,
    1782             :         },
    1783             :         [TYPE_lng] = {
    1784             :                 .name = "lng",
    1785             :                 .storage = TYPE_lng,
    1786             :                 .linear = true,
    1787             :                 .size = sizeof(lng),
    1788             :                 .atomNull = (void *) &lng_nil,
    1789             :                 .atomFromStr = (ssize_t (*)(const char *, size_t *, void **, bool)) lngFromStr,
    1790             :                 .atomToStr = (ssize_t (*)(char **, size_t *, const void *, bool)) lngToStr,
    1791             :                 .atomRead = (void *(*)(void *, size_t *, stream *, size_t)) lngRead,
    1792             :                 .atomWrite = (gdk_return (*)(const void *, stream *, size_t)) lngWrite,
    1793             :                 .atomCmp = (int (*)(const void *, const void *)) lngCmp,
    1794             :                 .atomHash = (BUN (*)(const void *)) lngHash,
    1795             :         },
    1796             : #ifdef HAVE_HGE
    1797             :         [TYPE_hge] = {
    1798             :                 .name = "hge",
    1799             :                 .storage = TYPE_hge,
    1800             :                 .linear = true,
    1801             :                 .size = sizeof(hge),
    1802             :                 .atomNull = (void *) &hge_nil,
    1803             :                 .atomFromStr = (ssize_t (*)(const char *, size_t *, void **, bool)) hgeFromStr,
    1804             :                 .atomToStr = (ssize_t (*)(char **, size_t *, const void *, bool)) hgeToStr,
    1805             :                 .atomRead = (void *(*)(void *, size_t *, stream *, size_t)) hgeRead,
    1806             :                 .atomWrite = (gdk_return (*)(const void *, stream *, size_t)) hgeWrite,
    1807             :                 .atomCmp = (int (*)(const void *, const void *)) hgeCmp,
    1808             :                 .atomHash = (BUN (*)(const void *)) hgeHash,
    1809             :         },
    1810             : #endif
    1811             :         [TYPE_date] = {
    1812             :                 .name = "date",
    1813             :                 .storage = TYPE_int,
    1814             :                 .linear = true,
    1815             :                 .size = sizeof(int),
    1816             :                 .atomNull = (void *) &int_nil,
    1817             :                 .atomFromStr = (ssize_t (*)(const char *, size_t *, void **, bool)) date_fromstr,
    1818             :                 .atomToStr = (ssize_t (*)(char **, size_t *, const void *, bool)) date_tostr,
    1819             :                 .atomRead = (void *(*)(void *, size_t *, stream *, size_t)) intRead,
    1820             :                 .atomWrite = (gdk_return (*)(const void *, stream *, size_t)) intWrite,
    1821             :                 .atomCmp = (int (*)(const void *, const void *)) intCmp,
    1822             :                 .atomHash = (BUN (*)(const void *)) intHash,
    1823             :         },
    1824             :         [TYPE_daytime] = {
    1825             :                 .name = "daytime",
    1826             :                 .storage = TYPE_lng,
    1827             :                 .linear = true,
    1828             :                 .size = sizeof(lng),
    1829             :                 .atomNull = (void *) &lng_nil,
    1830             :                 .atomFromStr = (ssize_t (*)(const char *, size_t *, void **, bool)) daytime_tz_fromstr,
    1831             :                 .atomToStr = (ssize_t (*)(char **, size_t *, const void *, bool)) daytime_tostr,
    1832             :                 .atomRead = (void *(*)(void *, size_t *, stream *, size_t)) lngRead,
    1833             :                 .atomWrite = (gdk_return (*)(const void *, stream *, size_t)) lngWrite,
    1834             :                 .atomCmp = (int (*)(const void *, const void *)) lngCmp,
    1835             :                 .atomHash = (BUN (*)(const void *)) lngHash,
    1836             :         },
    1837             :         [TYPE_timestamp] = {
    1838             :                 .name = "timestamp",
    1839             :                 .storage = TYPE_lng,
    1840             :                 .linear = true,
    1841             :                 .size = sizeof(lng),
    1842             :                 .atomNull = (void *) &lng_nil,
    1843             :                 .atomFromStr = (ssize_t (*)(const char *, size_t *, void **, bool)) timestamp_fromstr,
    1844             :                 .atomToStr = (ssize_t (*)(char **, size_t *, const void *, bool)) timestamp_tostr,
    1845             :                 .atomRead = (void *(*)(void *, size_t *, stream *, size_t)) lngRead,
    1846             :                 .atomWrite = (gdk_return (*)(const void *, stream *, size_t)) lngWrite,
    1847             :                 .atomCmp = (int (*)(const void *, const void *)) lngCmp,
    1848             :                 .atomHash = (BUN (*)(const void *)) lngHash,
    1849             :         },
    1850             :         [TYPE_uuid] = {
    1851             :                 .name = "uuid",
    1852             :                 .storage = TYPE_uuid,
    1853             :                 .linear = true,
    1854             :                 .size = sizeof(uuid),
    1855             :                 .atomNull = (void *) &uuid_nil,
    1856             :                 .atomFromStr = UUIDfromString,
    1857             :                 .atomToStr = UUIDtoString,
    1858             :                 .atomRead = UUIDread,
    1859             :                 .atomWrite = UUIDwrite,
    1860             :                 .atomCmp = UUIDcompare,
    1861             :                 .atomHash = UUIDhash,
    1862             :         },
    1863             :         [TYPE_str] = {
    1864             :                 .name = "str",
    1865             :                 .storage = TYPE_str,
    1866             :                 .linear = true,
    1867             :                 .size = sizeof(var_t),
    1868             :                 .atomNull = (void *) str_nil,
    1869             :                 .atomFromStr = (ssize_t (*)(const char *, size_t *, void **, bool)) strFromStr,
    1870             :                 .atomToStr = (ssize_t (*)(char **, size_t *, const void *, bool)) strToStr,
    1871             :                 .atomRead = (void *(*)(void *, size_t *, stream *, size_t)) strRead,
    1872             :                 .atomWrite = (gdk_return (*)(const void *, stream *, size_t)) strWrite,
    1873             :                 .atomCmp = (int (*)(const void *, const void *)) strCmp,
    1874             :                 .atomHash = (BUN (*)(const void *)) strHash,
    1875             :                 .atomPut = strPut,
    1876             :                 .atomLen = (size_t (*)(const void *)) strLen,
    1877             :                 .atomHeap = strHeap,
    1878             :         },
    1879             :         [TYPE_blob] = {
    1880             :                 .name = "blob",
    1881             :                 .storage = TYPE_blob,
    1882             :                 .linear = true,
    1883             :                 .size = sizeof(var_t),
    1884             :                 .atomNull = (void *) &blob_nil,
    1885             :                 .atomFromStr = BLOBfromstr,
    1886             :                 .atomToStr = BLOBtostr,
    1887             :                 .atomRead = BLOBread,
    1888             :                 .atomWrite = BLOBwrite,
    1889             :                 .atomCmp = BLOBcmp,
    1890             :                 .atomHash = BLOBhash,
    1891             :                 .atomPut = BLOBput,
    1892             :                 .atomDel = BLOBdel,
    1893             :                 .atomLen = BLOBlength,
    1894             :                 .atomHeap = BLOBheap,
    1895             :         },
    1896             : };
    1897             : 
    1898             : int GDKatomcnt = TYPE_blob + 1;
    1899             : 
    1900             : /*
    1901             :  * Sometimes a bat descriptor is loaded before the dynamic module
    1902             :  * defining the atom is loaded. To support this an extra set of
    1903             :  * unknown atoms is kept.  These can be accessed via the ATOMunknown
    1904             :  * interface. Finding an (negative) atom index can be done via
    1905             :  * ATOMunknown_find, which simply adds the atom if it's not in the
    1906             :  * unknown set. The index can be used to find the name of an unknown
    1907             :  * ATOM via ATOMunknown_name.
    1908             :  */
    1909             : static str unknown[MAXATOMS] = { NULL };
    1910             : 
    1911             : int
    1912         129 : ATOMunknown_find(const char *nme)
    1913             : {
    1914         129 :         int i, j = 0;
    1915             : 
    1916             :         /* first try to find the atom */
    1917         129 :         MT_lock_set(&GDKatomLock);
    1918        4112 :         for (i = 1; i < MAXATOMS; i++) {
    1919        3953 :                 if (unknown[i]) {
    1920         178 :                         if (strcmp(unknown[i], nme) == 0) {
    1921          99 :                                 MT_lock_unset(&GDKatomLock);
    1922          99 :                                 return -i;
    1923             :                         }
    1924        3775 :                 } else if (j == 0)
    1925        3854 :                         j = i;
    1926             :         }
    1927          30 :         if (j == 0) {
    1928             :                 /* no space for new atom (shouldn't happen) */
    1929           0 :                 MT_lock_unset(&GDKatomLock);
    1930           0 :                 return 0;
    1931             :         }
    1932          30 :         if ((unknown[j] = GDKstrdup(nme)) == NULL) {
    1933           0 :                 MT_lock_unset(&GDKatomLock);
    1934           0 :                 return 0;
    1935             :         }
    1936          30 :         MT_lock_unset(&GDKatomLock);
    1937          30 :         return -j;
    1938             : }
    1939             : 
    1940             : const char *
    1941         185 : ATOMunknown_name(int i)
    1942             : {
    1943         185 :         assert(i < 0);
    1944         185 :         assert(-i < MAXATOMS);
    1945         185 :         assert(unknown[-i]);
    1946         185 :         return unknown[-i];
    1947             : }
    1948             : 
    1949             : void
    1950         297 : ATOMunknown_clean(void)
    1951             : {
    1952         297 :         int i;
    1953             : 
    1954         297 :         MT_lock_set(&GDKatomLock);
    1955         600 :         for (i = 1; i < MAXATOMS; i++) {
    1956         303 :                 if(unknown[i]) {
    1957           6 :                         GDKfree(unknown[i]);
    1958           6 :                         unknown[i] = NULL;
    1959             :                 } else {
    1960             :                         break;
    1961             :                 }
    1962             :         }
    1963         297 :         MT_lock_unset(&GDKatomLock);
    1964         297 : }

Generated by: LCOV version 1.14