-rps: merge duplicate functions
[oweals/gnunet.git] / src / pq / pq_result_helper.c
index 48f073cda50e934a7642e2cec14fe510a99e7eae..bb6a8fa9bef127b0d8ec4023fee6bc936f6e0b61 100644 (file)
@@ -55,9 +55,8 @@ clean_varsize_blob (void *cls,
  * @param[out] dst where to store the result
  * @return
  *   #GNUNET_YES if all results could be extracted
- *   #GNUNET_NO if at least one result was NULL
  *   #GNUNET_SYSERR if a result was invalid (non-existing field)
- */ 
+ */
 static int
 extract_varsize_blob (void *cls,
                      PGresult *result,
@@ -70,7 +69,7 @@ extract_varsize_blob (void *cls,
   const char *res;
   void *idst;
   int fnum;
-  
+
   fnum = PQfnumber (result,
                    fname);
   if (fnum < 0)
@@ -83,8 +82,8 @@ extract_varsize_blob (void *cls,
   if (PQgetisnull (result,
                   row,
                   fnum))
-    return GNUNET_NO;
-  
+    return GNUNET_SYSERR;
+
   /* if a field is null, continue but
    * remember that we now return a different result */
   len = PQgetlength (result,
@@ -119,7 +118,7 @@ GNUNET_PQ_result_spec_variable_size (const char *name,
 {
   struct GNUNET_PQ_ResultSpec res =
     { &extract_varsize_blob,
-      &clean_varsize_blob, NULL, 
+      &clean_varsize_blob, NULL,
       (void *) (dst), 0, name, sptr };
   return res;
 }
@@ -136,9 +135,8 @@ GNUNET_PQ_result_spec_variable_size (const char *name,
  * @param[out] dst where to store the result
  * @return
  *   #GNUNET_YES if all results could be extracted
- *   #GNUNET_NO if at least one result was NULL
- *   #GNUNET_SYSERR if a result was invalid (non-existing field)
- */ 
+ *   #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
+ */
 static int
 extract_fixed_blob (void *cls,
                    PGresult *result,
@@ -150,7 +148,7 @@ extract_fixed_blob (void *cls,
   size_t len;
   const char *res;
   int fnum;
-  
+
   fnum = PQfnumber (result,
                    fname);
   if (fnum < 0)
@@ -163,14 +161,14 @@ extract_fixed_blob (void *cls,
   if (PQgetisnull (result,
                   row,
                   fnum))
-    return GNUNET_NO;
-  
+    return GNUNET_SYSERR;
+
   /* if a field is null, continue but
    * remember that we now return a different result */
   len = PQgetlength (result,
                     row,
                     fnum);
-  if (*dst_size != len) 
+  if (*dst_size != len)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Field `%s' has wrong size (got %u, expected %u)\n",
@@ -205,7 +203,7 @@ GNUNET_PQ_result_spec_fixed_size (const char *name,
 {
   struct GNUNET_PQ_ResultSpec res =
     { &extract_fixed_blob,
-      NULL, NULL, 
+      NULL, NULL,
       (dst), dst_size, name, NULL };
   return res;
 }
@@ -222,9 +220,8 @@ GNUNET_PQ_result_spec_fixed_size (const char *name,
  * @param[out] dst where to store the result
  * @return
  *   #GNUNET_YES if all results could be extracted
- *   #GNUNET_NO if at least one result was NULL
- *   #GNUNET_SYSERR if a result was invalid (non-existing field)
- */ 
+ *   #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
+ */
 static int
 extract_rsa_public_key (void *cls,
                        PGresult *result,
@@ -233,7 +230,7 @@ extract_rsa_public_key (void *cls,
                        size_t *dst_size,
                        void *dst)
 {
-  struct GNUNET_CRYPTO_rsa_PublicKey **pk = dst;
+  struct GNUNET_CRYPTO_RsaPublicKey **pk = dst;
   size_t len;
   const char *res;
   int fnum;
@@ -251,7 +248,7 @@ extract_rsa_public_key (void *cls,
   if (PQgetisnull (result,
                   row,
                   fnum))
-    return GNUNET_NO;
+    return GNUNET_SYSERR;
 
   /* if a field is null, continue but
    * remember that we now return a different result */
@@ -285,8 +282,8 @@ static void
 clean_rsa_public_key (void *cls,
                      void *rd)
 {
-  struct GNUNET_CRYPTO_rsa_PublicKey **pk = rd;
-  
+  struct GNUNET_CRYPTO_RsaPublicKey **pk = rd;
+
   if (NULL != *pk)
   {
     GNUNET_CRYPTO_rsa_public_key_free (*pk);
@@ -304,7 +301,7 @@ clean_rsa_public_key (void *cls,
  */
 struct GNUNET_PQ_ResultSpec
 GNUNET_PQ_result_spec_rsa_public_key (const char *name,
-                                     struct GNUNET_CRYPTO_rsa_PublicKey **rsa)
+                                     struct GNUNET_CRYPTO_RsaPublicKey **rsa)
 {
   struct GNUNET_PQ_ResultSpec res =
     { &extract_rsa_public_key,
@@ -326,9 +323,8 @@ GNUNET_PQ_result_spec_rsa_public_key (const char *name,
  * @param[out] dst where to store the result
  * @return
  *   #GNUNET_YES if all results could be extracted
- *   #GNUNET_NO if at least one result was NULL
- *   #GNUNET_SYSERR if a result was invalid (non-existing field)
- */ 
+ *   #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
+ */
 static int
 extract_rsa_signature (void *cls,
                       PGresult *result,
@@ -337,11 +333,11 @@ extract_rsa_signature (void *cls,
                       size_t *dst_size,
                       void *dst)
 {
-  struct GNUNET_CRYPTO_rsa_Signature **sig = dst;
+  struct GNUNET_CRYPTO_RsaSignature **sig = dst;
   size_t len;
   const char *res;
   int fnum;
-  
+
   *sig = NULL;
   fnum = PQfnumber (result,
                    fname);
@@ -355,7 +351,7 @@ extract_rsa_signature (void *cls,
   if (PQgetisnull (result,
                   row,
                   fnum))
-    return GNUNET_NO;
+    return GNUNET_SYSERR;
 
   /* if a field is null, continue but
    * remember that we now return a different result */
@@ -389,7 +385,7 @@ static void
 clean_rsa_signature (void *cls,
                     void *rd)
 {
-  struct GNUNET_CRYPTO_rsa_Signature **sig = rd;
+  struct GNUNET_CRYPTO_RsaSignature **sig = rd;
 
   if (NULL != *sig)
   {
@@ -408,7 +404,7 @@ clean_rsa_signature (void *cls,
  */
 struct GNUNET_PQ_ResultSpec
 GNUNET_PQ_result_spec_rsa_signature (const char *name,
-                                   struct GNUNET_CRYPTO_rsa_Signature **sig)
+                                   struct GNUNET_CRYPTO_RsaSignature **sig)
 {
   struct GNUNET_PQ_ResultSpec res =
     { &extract_rsa_signature,
@@ -419,6 +415,109 @@ GNUNET_PQ_result_spec_rsa_signature (const char *name,
 }
 
 
+/**
+ * Extract data from a Postgres database @a result at row @a row.
+ *
+ * @param cls closure
+ * @param result where to extract data from
+ * @param int row to extract data from
+ * @param fname name (or prefix) of the fields to extract 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_string (void *cls,
+                PGresult *result,
+                int row,
+                const char *fname,
+                size_t *dst_size,
+                void *dst)
+{
+  char **str = dst;
+  size_t len;
+  const char *res;
+  int fnum;
+
+  *str = NULL;
+  fnum = PQfnumber (result,
+                   fname);
+  if (fnum < 0)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+               "Field `%s' does not exist in result\n",
+               fname);
+    return GNUNET_SYSERR;
+  }
+  if (PQgetisnull (result,
+                  row,
+                  fnum))
+    return GNUNET_SYSERR;
+
+  /* if a field is null, continue but
+   * remember that we now return a different result */
+  len = PQgetlength (result,
+                    row,
+                    fnum);
+  res = PQgetvalue (result,
+                   row,
+                   fnum);
+  *str = GNUNET_strndup (res,
+                         len);
+  if (NULL == *str)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+               "Field `%s' contains bogus value (fails to decode)\n",
+               fname);
+    return GNUNET_SYSERR;
+  }
+  return GNUNET_OK;
+}
+
+
+/**
+ * Function called to clean up memory allocated
+ * by a #GNUNET_PQ_ResultConverter.
+ *
+ * @param cls closure
+ * @param rd result data to clean up
+ */
+static void
+clean_string (void *cls,
+              void *rd)
+{
+  char **str = rd;
+
+  if (NULL != *str)
+  {
+    GNUNET_free (*str);
+    *str = NULL;
+  }
+}
+
+
+/**
+ * 0-terminated string expected.
+ *
+ * @param name name of the field in the table
+ * @param[out] dst where to store the result, allocated
+ * @return array entry for the result specification to use
+ */
+struct GNUNET_PQ_ResultSpec
+GNUNET_PQ_result_spec_string (const char *name,
+                              char **dst)
+{
+  struct GNUNET_PQ_ResultSpec res =
+    { &extract_string,
+      &clean_string,
+      NULL,
+      (void *) dst, 0, (name), NULL };
+  return res;
+}
+
+
 /**
  * Absolute time expected.
  *
@@ -463,9 +562,8 @@ GNUNET_PQ_result_spec_absolute_time_nbo (const char *name,
  * @param[out] dst where to store the result
  * @return
  *   #GNUNET_YES if all results could be extracted
- *   #GNUNET_NO if at least one result was NULL
- *   #GNUNET_SYSERR if a result was invalid (non-existing field)
- */ 
+ *   #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
+ */
 static int
 extract_uint16 (void *cls,
                PGresult *result,
@@ -477,7 +575,7 @@ extract_uint16 (void *cls,
   uint16_t *udst = dst;
   const uint16_t *res;
   int fnum;
-  
+
   fnum = PQfnumber (result,
                    fname);
   if (fnum < 0)
@@ -490,7 +588,7 @@ extract_uint16 (void *cls,
   if (PQgetisnull (result,
                   row,
                   fnum))
-    return GNUNET_NO;
+    return GNUNET_SYSERR;
   GNUNET_assert (NULL != dst);
   if (sizeof (uint16_t) != *dst_size)
   {
@@ -536,9 +634,8 @@ GNUNET_PQ_result_spec_uint16 (const char *name,
  * @param[out] dst where to store the result
  * @return
  *   #GNUNET_YES if all results could be extracted
- *   #GNUNET_NO if at least one result was NULL
- *   #GNUNET_SYSERR if a result was invalid (non-existing field)
- */ 
+ *   #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
+ */
 static int
 extract_uint32 (void *cls,
                PGresult *result,
@@ -550,7 +647,7 @@ extract_uint32 (void *cls,
   uint32_t *udst = dst;
   const uint32_t *res;
   int fnum;
-  
+
   fnum = PQfnumber (result,
                    fname);
   if (fnum < 0)
@@ -563,7 +660,7 @@ extract_uint32 (void *cls,
   if (PQgetisnull (result,
                   row,
                   fnum))
-    return GNUNET_NO;
+    return GNUNET_SYSERR;
   GNUNET_assert (NULL != dst);
   if (sizeof (uint32_t) != *dst_size)
   {
@@ -590,7 +687,7 @@ GNUNET_PQ_result_spec_uint32 (const char *name,
                              uint32_t *u32)
 {
   struct GNUNET_PQ_ResultSpec res =
-    { &extract_uint32, 
+    { &extract_uint32,
       NULL,
       NULL,
       (void *) u32, sizeof (*u32), (name), NULL };
@@ -609,9 +706,8 @@ GNUNET_PQ_result_spec_uint32 (const char *name,
  * @param[out] dst where to store the result
  * @return
  *   #GNUNET_YES if all results could be extracted
- *   #GNUNET_NO if at least one result was NULL
- *   #GNUNET_SYSERR if a result was invalid (non-existing field)
- */ 
+ *   #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
+ */
 static int
 extract_uint64 (void *cls,
                PGresult *result,
@@ -623,7 +719,7 @@ extract_uint64 (void *cls,
   uint64_t *udst = dst;
   const uint64_t *res;
   int fnum;
-  
+
   fnum = PQfnumber (result,
                    fname);
   if (fnum < 0)
@@ -636,7 +732,7 @@ extract_uint64 (void *cls,
   if (PQgetisnull (result,
                   row,
                   fnum))
-    return GNUNET_NO;
+    return GNUNET_SYSERR;
   GNUNET_assert (NULL != dst);
   if (sizeof (uint64_t) != *dst_size)
   {