Code

data: Don't free memory before using it for the last time.
[sysdb.git] / src / core / data.c
index 0f10538fe6313f798eef54dd9c1ce081974f4108..557dfd6a5fb3011d276c22ccb31442edaaf59ff1 100644 (file)
  * private helper functions
  */
 
+/* Calculate the linear function 'd1 + n * d2'. */
+static int
+data_lin(const sdb_data_t *d1, int n, const sdb_data_t *d2, sdb_data_t *res)
+{
+       if (d1->type != d2->type)
+               return -1;
+
+       if (d1->type == SDB_TYPE_INTEGER)
+               res->data.integer = d1->data.integer + (int64_t)n * d2->data.integer;
+       else if (d1->type == SDB_TYPE_DECIMAL)
+               res->data.decimal = d1->data.decimal + (double)n * d2->data.decimal;
+       else if (d1->type ==  SDB_TYPE_DATETIME)
+               res->data.datetime = d1->data.datetime + (sdb_time_t)n * d2->data.datetime;
+       else
+               return -1;
+       res->type = d1->type;
+       return 0;
+} /* data_lin */
+
+/* Multiply d1 with d2. */
+static int
+data_mul(const sdb_data_t *d1, const sdb_data_t *d2, sdb_data_t *res)
+{
+       if (d1->type == SDB_TYPE_INTEGER) {
+               if (d2->type == SDB_TYPE_INTEGER)
+                       res->data.integer = d1->data.integer * d2->data.integer;
+               else if (d2->type == SDB_TYPE_DATETIME) {
+                       res->data.datetime = (sdb_time_t)d1->data.integer
+                               * d2->data.datetime;
+                       res->type = SDB_TYPE_DATETIME;
+                       return 0;
+               }
+               else
+                       return -1;
+       }
+       else if (d1->type == SDB_TYPE_DECIMAL) {
+               if (d2->type == SDB_TYPE_DECIMAL)
+                       res->data.decimal = d1->data.decimal * d2->data.decimal;
+               else if (d2->type == SDB_TYPE_DATETIME) {
+                       res->data.datetime = (sdb_time_t)(d1->data.decimal
+                                       * (double)d2->data.datetime);
+                       res->type = SDB_TYPE_DATETIME;
+                       return 0;
+               }
+               else
+                       return -1;
+       }
+       else if (d1->type == SDB_TYPE_DATETIME) {
+               if (d2->type == SDB_TYPE_DATETIME)
+                       res->data.datetime = d1->data.datetime
+                               * d2->data.datetime;
+               else if (d2->type == SDB_TYPE_INTEGER)
+                       res->data.datetime = d1->data.datetime
+                               * (sdb_time_t)d2->data.integer;
+               else if (d2->type == SDB_TYPE_DECIMAL)
+                       res->data.datetime = (sdb_time_t)((double)d1->data.datetime
+                                       * d2->data.decimal);
+               else
+                       return -1;
+       }
+       else
+               return -1;
+
+       res->type = d1->type;
+       return 0;
+} /* data_mul */
+
+/* Device d1 by d2 and return the result and the remainder. */
+static int
+data_div(const sdb_data_t *d1, const sdb_data_t *d2,
+               sdb_data_t *res, sdb_data_t *rem)
+{
+       if (d1->type == SDB_TYPE_INTEGER) {
+               if (d2->type != SDB_TYPE_INTEGER)
+                       return -1;
+               if (res)
+                       res->data.integer = d1->data.integer / d2->data.integer;
+               if (rem)
+                       rem->data.integer = d1->data.integer % d2->data.integer;
+       }
+       else if (d1->type == SDB_TYPE_DECIMAL) {
+               if (d2->type != SDB_TYPE_DECIMAL)
+                       return -1;
+               if (res)
+                       res->data.decimal = d1->data.decimal / d2->data.decimal;
+               if (rem)
+                       rem->data.decimal = fmod(d1->data.decimal, d2->data.decimal);
+       }
+       else if (d1->type == SDB_TYPE_DATETIME) {
+               if (d2->type == SDB_TYPE_DECIMAL) {
+                       if (res)
+                               res->data.datetime = (sdb_time_t)((double)d1->data.datetime
+                                               / d2->data.decimal);
+                       if (rem) {
+                               double tmp = fmod((double)d1->data.datetime, d2->data.decimal);
+                               rem->data.datetime = (sdb_time_t)tmp;
+                       }
+               }
+               else {
+                       sdb_time_t a, b;
+                       if (d2->type == SDB_TYPE_DATETIME) {
+                               a = d1->data.datetime;
+                               b = d2->data.datetime;
+                       }
+                       else if (d2->type == SDB_TYPE_INTEGER) {
+                               a = d1->data.datetime;
+                               b = (sdb_time_t)d2->data.integer;
+                       }
+                       else
+                               return -1;
+                       if (res)
+                               res->data.datetime = a / b;
+                       if (rem)
+                               rem->data.datetime = a % b;
+               }
+       }
+       else
+               return -1;
+
+       if (res)
+               res->type = d1->type;
+       if (rem)
+               rem->type = d1->type;
+       return 0;
+} /* data_div */
+
+/* Concatenate d1 and d2. */
 static int
 data_concat(const sdb_data_t *d1, const sdb_data_t *d2, sdb_data_t *res)
 {
@@ -55,6 +182,9 @@ data_concat(const sdb_data_t *d1, const sdb_data_t *d2, sdb_data_t *res)
        unsigned char *s1, *s2;
        size_t len1, len2;
 
+       if (d1->type != d2->type)
+               return -1;
+
        if (d1->type == SDB_TYPE_STRING) {
                s1 = (unsigned char *)d1->data.string;
                s2 = (unsigned char *)d2->data.string;
@@ -111,16 +241,36 @@ sdb_data_copy(sdb_data_t *dst, const sdb_data_t *src)
        tmp = *src;
        switch (src->type) {
                case SDB_TYPE_STRING:
-                       tmp.data.string = strdup(src->data.string);
-                       if (! tmp.data.string)
-                               return -1;
+                       if (src->data.string) {
+                               tmp.data.string = strdup(src->data.string);
+                               if (! tmp.data.string)
+                                       return -1;
+                       }
                        break;
                case SDB_TYPE_BINARY:
-                       tmp.data.binary.datum = malloc(src->data.binary.length);
-                       if (! tmp.data.binary.datum)
-                               return -1;
-                       memcpy(tmp.data.binary.datum, src->data.binary.datum,
-                                       src->data.binary.length);
+                       if (src->data.binary.datum) {
+                               tmp.data.binary.datum = malloc(src->data.binary.length);
+                               if (! tmp.data.binary.datum)
+                                       return -1;
+                               memcpy(tmp.data.binary.datum, src->data.binary.datum,
+                                               src->data.binary.length);
+                       }
+                       break;
+               case SDB_TYPE_REGEX:
+                       if (src->data.re.raw) {
+                               tmp.data.re.raw = strdup(src->data.re.raw);
+                               if (! tmp.data.re.raw)
+                                       return -1;
+                               /* we need to recompile because the regex might point to
+                                * dynamically allocated memory */
+                               if (regcomp(&tmp.data.re.regex, tmp.data.re.raw,
+                                                       REG_EXTENDED | REG_ICASE | REG_NOSUB)) {
+                                       free(tmp.data.re.raw);
+                                       return -1;
+                               }
+                       }
+                       else
+                               memset(&tmp.data.re.regex, 0, sizeof(tmp.data.re.regex));
                        break;
        }
 
@@ -147,6 +297,14 @@ sdb_data_free_datum(sdb_data_t *datum)
                        datum->data.binary.datum = NULL;
                        datum->data.binary.length = 0;
                        break;
+               case SDB_TYPE_REGEX:
+                       if (datum->data.re.raw) {
+                               free(datum->data.re.raw);
+                               regfree(&datum->data.re.regex);
+                       }
+                       datum->data.re.raw = NULL;
+                       memset(&datum->data.re.regex, 0, sizeof(datum->data.re.regex));
+                       break;
        }
 } /* sdb_data_free_datum */
 
@@ -163,7 +321,7 @@ sdb_data_cmp(const sdb_data_t *d1, const sdb_data_t *d2)
        CMP_NULL(d1, d2);
 
        if (d1->type != d2->type)
-               return -1;
+               return SDB_CMP(d1->type, d2->type);
 
        switch (d1->type) {
                case SDB_TYPE_INTEGER:
@@ -198,12 +356,67 @@ sdb_data_cmp(const sdb_data_t *d1, const sdb_data_t *d2)
 
                        return diff;
                }
-               default:
-                       return -1;
+               case SDB_TYPE_REGEX:
+                       CMP_NULL(d1->data.re.raw, d2->data.re.raw);
+                       return strcmp(d1->data.re.raw, d2->data.re.raw);
        }
-#undef CMP_NULL
+       return -1;
 } /* sdb_data_cmp */
 
+int
+sdb_data_strcmp(const sdb_data_t *d1, const sdb_data_t *d2)
+{
+       char d1_str[sdb_data_strlen(d1) + 1];
+       char d2_str[sdb_data_strlen(d2) + 1];
+
+       if (sdb_data_isnull(d1))
+               d1 = NULL;
+       if (sdb_data_isnull(d2))
+               d2 = NULL;
+
+       CMP_NULL(d1, d2);
+
+       if (sdb_data_format(d1, d1_str, sizeof(d1_str), SDB_UNQUOTED) < 0)
+               return SDB_CMP(sizeof(d1_str), sizeof(d2_str));
+       if (sdb_data_format(d2, d2_str, sizeof(d2_str), SDB_UNQUOTED) < 0)
+               return SDB_CMP(sizeof(d1_str), sizeof(d2_str));
+
+       return strcasecmp(d1_str, d2_str);
+#undef CMP_NULL
+} /* sdb_data_strcmp */
+
+_Bool
+sdb_data_isnull(const sdb_data_t *datum)
+{
+       if (! datum)
+               return 1;
+       if ((datum->type == SDB_TYPE_STRING) && (! datum->data.string))
+               return 1;
+       if ((datum->type == SDB_TYPE_BINARY) && (! datum->data.binary.datum))
+               return 1;
+       if ((datum->type == SDB_TYPE_REGEX) && (! datum->data.re.raw))
+               return 1;
+       return 0;
+} /* sdb_data_isnull */
+
+int
+sdb_data_parse_op(const char *op)
+{
+       if (! strcmp(op, "+"))
+               return SDB_DATA_ADD;
+       else if (! strcmp(op, "-"))
+               return SDB_DATA_SUB;
+       else if (! strcmp(op, "*"))
+               return SDB_DATA_MUL;
+       else if (! strcmp(op, "/"))
+               return SDB_DATA_DIV;
+       else if (! strcmp(op, "%"))
+               return SDB_DATA_MOD;
+       else if (! strcmp(op, "||"))
+               return SDB_DATA_CONCAT;
+       return -1;
+} /* sdb_data_parse_op */
+
 int
 sdb_data_expr_eval(int op, const sdb_data_t *d1, const sdb_data_t *d2,
                sdb_data_t *res)
@@ -214,170 +427,17 @@ sdb_data_expr_eval(int op, const sdb_data_t *d1, const sdb_data_t *d2,
                case SDB_DATA_CONCAT:
                        return data_concat(d1, d2, res);
                case SDB_DATA_ADD:
-                       if (d1->type != d2->type)
-                               return -1;
-                       switch (d1->type) {
-                               case SDB_TYPE_INTEGER:
-                                       res->data.integer = d1->data.integer + d2->data.integer;
-                                       break;
-                               case SDB_TYPE_DECIMAL:
-                                       res->data.decimal = d1->data.decimal + d2->data.decimal;
-                                       break;
-                               case SDB_TYPE_DATETIME:
-                                       res->data.datetime = d1->data.datetime + d2->data.datetime;
-                                       break;
-                               default:
-                                       return -1;
-                       }
-                       break;
+                       return data_lin(d1, 1, d2, res);
                case SDB_DATA_SUB:
-                       if (d1->type != d2->type)
-                               return -1;
-                       switch (d1->type) {
-                               case SDB_TYPE_INTEGER:
-                                       res->data.integer = d1->data.integer - d2->data.integer;
-                                       break;
-                               case SDB_TYPE_DECIMAL:
-                                       res->data.decimal = d1->data.decimal - d2->data.decimal;
-                                       break;
-                               case SDB_TYPE_DATETIME:
-                                       res->data.datetime = d1->data.datetime - d2->data.datetime;
-                                       break;
-                               default:
-                                       return -1;
-                       }
-                       break;
+                       return data_lin(d1, -1, d2, res);
                case SDB_DATA_MUL:
-                       switch (d1->type) {
-                               case SDB_TYPE_INTEGER:
-                                       if (d2->type == SDB_TYPE_INTEGER)
-                                               res->data.integer = d1->data.integer
-                                                       * d2->data.integer;
-                                       else if (d2->type == SDB_TYPE_DATETIME) {
-                                               res->data.datetime = (sdb_time_t)d1->data.integer
-                                                       * d2->data.datetime;
-                                               res->type = SDB_TYPE_DATETIME;
-                                               return 0;
-                                       }
-                                       else
-                                               return -1;
-                                       break;
-                               case SDB_TYPE_DECIMAL:
-                                       if (d2->type == SDB_TYPE_DECIMAL)
-                                               res->data.decimal = d1->data.decimal
-                                                       * d2->data.decimal;
-                                       else if (d2->type == SDB_TYPE_DATETIME) {
-                                               double tmp = d1->data.decimal
-                                                       * (double)d2->data.datetime;
-                                               res->data.datetime = (sdb_time_t)tmp;
-                                               res->type = SDB_TYPE_DATETIME;
-                                               return 0;
-                                       }
-                                       else
-                                               return -1;
-                                       break;
-                               case SDB_TYPE_DATETIME:
-                                       if (d2->type == SDB_TYPE_DATETIME)
-                                               res->data.datetime = d1->data.datetime
-                                                       * d2->data.datetime;
-                                       else if (d2->type == SDB_TYPE_INTEGER) {
-                                               res->data.datetime = d1->data.datetime
-                                                       * (sdb_time_t)d2->data.integer;
-                                               res->type = SDB_TYPE_DATETIME;
-                                               return 0;
-                                       }
-                                       else if (d2->type == SDB_TYPE_DECIMAL) {
-                                               double tmp = (double)d1->data.datetime
-                                                       * d2->data.decimal;
-                                               res->data.datetime = (sdb_time_t)tmp;
-                                               res->type = SDB_TYPE_DATETIME;
-                                               return 0;
-                                       }
-                                       else
-                                               return -1;
-                                       break;
-                               default:
-                                       return -1;
-                       }
-                       break;
+                       return data_mul(d1, d2, res);
                case SDB_DATA_DIV:
-                       switch (d1->type) {
-                               case SDB_TYPE_INTEGER:
-                                       if (d2->type != SDB_TYPE_INTEGER)
-                                               return -1;
-                                       res->data.integer = d1->data.integer / d2->data.integer;
-                                       break;
-                               case SDB_TYPE_DECIMAL:
-                                       if (d2->type != SDB_TYPE_DECIMAL)
-                                               return -1;
-                                       res->data.decimal = d1->data.decimal / d2->data.decimal;
-                                       break;
-                               case SDB_TYPE_DATETIME:
-                                       if (d2->type == SDB_TYPE_DATETIME)
-                                               res->data.datetime = d1->data.datetime
-                                                       / d2->data.datetime;
-                                       else if (d2->type == SDB_TYPE_INTEGER) {
-                                               res->data.datetime = d1->data.datetime
-                                                       / (sdb_time_t)d2->data.integer;
-                                               res->type = SDB_TYPE_DATETIME;
-                                               return 0;
-                                       }
-                                       else if (d2->type == SDB_TYPE_DECIMAL) {
-                                               double tmp = (double)d1->data.datetime
-                                                       / d2->data.decimal;
-                                               res->data.datetime = (sdb_time_t)tmp;
-                                               res->type = SDB_TYPE_DATETIME;
-                                               return 0;
-                                       }
-                                       else
-                                               return -1;
-                                       break;
-                               default:
-                                       return -1;
-                       }
-                       break;
+                       return data_div(d1, d2, res, NULL);
                case SDB_DATA_MOD:
-                       switch (d1->type) {
-                               case SDB_TYPE_INTEGER:
-                                       if (d2->type != SDB_TYPE_INTEGER)
-                                               return -1;
-                                       res->data.integer = d1->data.integer % d2->data.integer;
-                                       break;
-                               case SDB_TYPE_DECIMAL:
-                                       if (d2->type != SDB_TYPE_DECIMAL)
-                                               return -1;
-                                       res->data.decimal = fmod(d1->data.decimal, d2->data.decimal);
-                                       break;
-                               case SDB_TYPE_DATETIME:
-                                       if (d2->type == SDB_TYPE_DATETIME)
-                                               res->data.datetime = d1->data.datetime
-                                                       % d2->data.datetime;
-                                       else if (d2->type == SDB_TYPE_INTEGER) {
-                                               res->data.datetime = d1->data.datetime
-                                                       % (sdb_time_t)d2->data.integer;
-                                               res->type = SDB_TYPE_DATETIME;
-                                               return 0;
-                                       }
-                                       else if (d2->type == SDB_TYPE_DECIMAL) {
-                                               double tmp = fmod((double)d1->data.datetime,
-                                                       d2->data.decimal);
-                                               res->data.datetime = (sdb_time_t)tmp;
-                                               res->type = SDB_TYPE_DATETIME;
-                                               return 0;
-                                       }
-                                       else
-                                               return -1;
-                                       break;
-                               default:
-                                       return -1;
-                       }
-                       break;
-               default:
-                       return -1;
+                       return data_div(d1, d2, NULL, res);
        }
-
-       res->type = d1->type;
-       return 0;
+       return -1;
 } /* sdb_data_expr_eval */
 
 size_t
@@ -391,19 +451,26 @@ sdb_data_strlen(const sdb_data_t *datum)
                        /* log(64) */
                        return 20;
                case SDB_TYPE_DECIMAL:
-                       /* XXX: -0xN.NNNNNNp+NNN */
+                       /* XXX: -d.dddddde+dd or -ddddd.dddddd */
                        return 42;
                case SDB_TYPE_STRING:
                        if (! datum->data.string)
-                               return 6; /* "NULL" */
+                               return 8; /* "<NULL>" */
                        /* in the worst case, each character needs to be escaped */
                        return 2 * strlen(datum->data.string) + 2;
                case SDB_TYPE_DATETIME:
                        /* "YYYY-MM-DD HH:MM:SS +zzzz" */
                        return 27;
                case SDB_TYPE_BINARY:
+                       if (! datum->data.binary.datum)
+                               return 8; /* "<NULL>" */
                        /* "\xNN" */
                        return 4 * datum->data.binary.length + 2;
+               case SDB_TYPE_REGEX:
+                       if (! datum->data.re.raw)
+                               return 8; /* "<NULL>" */
+                       /* "/.../" */
+                       return strlen(datum->data.re.raw) + 4;
        }
        return 0;
 } /* sdb_data_strlen */
@@ -425,11 +492,11 @@ sdb_data_format(const sdb_data_t *datum, char *buf, size_t buflen, int quoted)
                        ret = snprintf(buf, buflen, "%"PRIi64, datum->data.integer);
                        break;
                case SDB_TYPE_DECIMAL:
-                       ret = snprintf(buf, buflen, "%a", datum->data.decimal);
+                       ret = snprintf(buf, buflen, "%g", datum->data.decimal);
                        break;
                case SDB_TYPE_STRING:
                        if (! datum->data.string)
-                               data = "NULL";
+                               data = "<NULL>";
                        else {
                                pos = 0;
                                for (i = 0; i < strlen(datum->data.string); ++i) {
@@ -471,8 +538,20 @@ sdb_data_format(const sdb_data_t *datum, char *buf, size_t buflen, int quoted)
                                tmp[pos] = hex[byte & 0xf];
                                ++pos;
                        }
-                       tmp[pos] = '\0';
-                       data = tmp;
+                       if (datum->data.binary.datum) {
+                               tmp[pos] = '\0';
+                               data = tmp;
+                       }
+                       else
+                               data = "<NULL>";
+                       break;
+               case SDB_TYPE_REGEX:
+                       if (! datum->data.re.raw)
+                               data = "<NULL>";
+                       else {
+                               snprintf(tmp, sizeof(tmp), "/%s/", datum->data.re.raw);
+                               data = tmp;
+                       }
                        break;
        }
 
@@ -517,6 +596,22 @@ sdb_data_parse(char *str, int type, sdb_data_t *data)
                        tmp.data.binary.length = strlen(str);
                        tmp.data.binary.datum = (unsigned char *)str;
                        break;
+               case SDB_TYPE_REGEX:
+                       tmp.data.re.raw = strdup(str);
+                       if (! tmp.data.re.raw)
+                               return -1;
+                       if (regcomp(&tmp.data.re.regex, tmp.data.re.raw,
+                                               REG_EXTENDED | REG_ICASE | REG_NOSUB)) {
+                               sdb_log(SDB_LOG_ERR, "core: Failed to compile regular "
+                                               "expression '%s'", tmp.data.re.raw);
+                               free(tmp.data.re.raw);
+                               return -1;
+                       }
+                       if (! data) {
+                               tmp.type = SDB_TYPE_REGEX;
+                               sdb_data_free_datum(&tmp);
+                       }
+                       break;
                default:
                        errno = EINVAL;
                        return -1;