NAMESTORE/JSON: fix parsing exp and flags
[oweals/gnunet.git] / src / sq / sq_result_helper.c
index eaf606aa4436dc2a1f8afc38a8edd3bc31ce2674..aba1eaea539a4d651bc055de07dddd807424d2f1 100644 (file)
@@ -3,16 +3,20 @@
   This file is part of GNUnet
   Copyright (C) 2017 GNUnet e.V.
 
-  GNUnet is free software; you can redistribute it and/or modify it under the
-  terms of the GNU General Public License as published by the Free Software
-  Foundation; either version 3, or (at your option) any later version.
-
-  GNUnet is distributed in the hope that it will be useful, but WITHOUT ANY
-  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-  A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
-
-  You should have received a copy of the GNU General Public License along with
-  GNUnet; see the file COPYING.  If not, If not, see <http://www.gnu.org/licenses/>
+  GNUnet is free software: you can redistribute it and/or modify it
+  under the terms of the GNU Affero General Public License as published
+  by the Free Software Foundation, either version 3 of the License,
+  or (at your option) any later version.
+
+  GNUnet is distributed in the hope that it will be useful, but
+  WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+  Affero General Public License for more details.
+  You should have received a copy of the GNU Affero General Public License
+  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+     SPDX-License-Identifier: AGPL3.0-or-later
 */
 /**
  * @file sq/sq_result_helper.c
@@ -46,6 +50,15 @@ extract_var_blob (void *cls,
   const void *ret;
   void **rdst = (void **) dst;
 
+  if (SQLITE_NULL ==
+      sqlite3_column_type (result,
+                           column))
+  {
+    *rdst = NULL;
+    *dst_size = 0;
+    return GNUNET_YES;
+  }
+
   if (SQLITE_BLOB !=
       sqlite3_column_type (result,
                            column))
@@ -142,6 +155,14 @@ extract_fixed_blob (void *cls,
   int have;
   const void *ret;
 
+  if ( (0 == *dst_size) &&
+       (SQLITE_NULL ==
+        sqlite3_column_type (result,
+                             column)) )
+  {
+    return GNUNET_YES;
+  }
+
   if (SQLITE_BLOB !=
       sqlite3_column_type (result,
                            column))
@@ -211,6 +232,13 @@ extract_utf8_string (void *cls,
   const char *text;
   char **rdst = dst;
 
+  if (SQLITE_NULL ==
+      sqlite3_column_type (result,
+                           column))
+  {
+    *rdst = NULL;
+    return GNUNET_OK;
+  }
   if (SQLITE_TEXT !=
       sqlite3_column_type (result,
                            column))
@@ -458,6 +486,45 @@ GNUNET_SQ_result_spec_rsa_signature (struct GNUNET_CRYPTO_RsaSignature **sig)
 }
 
 
+/**
+ * Extract absolute time value from a Postgres database @a result at row @a row.
+ *
+ * @param cls closure
+ * @param result where to extract data from
+ * @param column column to extract data from
+ * @param[in,out] dst_size where to store size of result, may be NULL
+ * @param[out] dst where to store the result
+ * @return
+ *   #GNUNET_YES if all results could be extracted
+ *   #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
+ */
+static int
+extract_abs_time (void *cls,
+                  sqlite3_stmt *result,
+                  unsigned int column,
+                  size_t *dst_size,
+                  void *dst)
+{
+  struct GNUNET_TIME_Absolute *u = dst;
+  struct GNUNET_TIME_Absolute t;
+
+  GNUNET_assert (sizeof (uint64_t) == *dst_size);
+  if (SQLITE_INTEGER !=
+      sqlite3_column_type (result,
+                           column))
+  {
+    GNUNET_break (0);
+    return GNUNET_SYSERR;
+  }
+  t.abs_value_us = (uint64_t) sqlite3_column_int64 (result,
+                                                    column);
+  if (INT64_MAX == t.abs_value_us)
+    t = GNUNET_TIME_UNIT_FOREVER_ABS;
+  *u = t;
+  return GNUNET_OK;
+}
+
+
 /**
  * Absolute time expected.
  *
@@ -467,7 +534,14 @@ GNUNET_SQ_result_spec_rsa_signature (struct GNUNET_CRYPTO_RsaSignature **sig)
 struct GNUNET_SQ_ResultSpec
 GNUNET_SQ_result_spec_absolute_time (struct GNUNET_TIME_Absolute *at)
 {
-  return GNUNET_SQ_result_spec_uint64 (&at->abs_value_us);
+  struct GNUNET_SQ_ResultSpec rs = {
+    .conv = &extract_abs_time,
+    .dst = at,
+    .dst_size = sizeof (struct GNUNET_TIME_Absolute),
+    .num_params = 1
+  };
+
+  return rs;
 }
 
 
@@ -503,6 +577,8 @@ extract_abs_time_nbo (void *cls,
   }
   t.abs_value_us = (uint64_t) sqlite3_column_int64 (result,
                                                     column);
+  if (INT64_MAX == t.abs_value_us)
+    t = GNUNET_TIME_UNIT_FOREVER_ABS;
   *u = GNUNET_TIME_absolute_hton (t);
   return GNUNET_OK;
 }
@@ -548,7 +624,7 @@ extract_uint16 (void *cls,
                 void *dst)
 {
   uint64_t v;
-  uint32_t *u = dst;
+  uint16_t *u = dst;
 
   GNUNET_assert (sizeof (uint16_t) == *dst_size);
   if (SQLITE_INTEGER !=