[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[gnunet] 02/02: [pq] unified/simplified switch-logic
From: |
gnunet |
Subject: |
[gnunet] 02/02: [pq] unified/simplified switch-logic |
Date: |
Sun, 25 Jun 2023 16:42:08 +0200 |
This is an automated email from the git hooks/post-receive script.
oec pushed a commit to branch master
in repository gnunet.
commit 7011143dc4a04272c8d7a7744e8e674509b1c727
Author: Özgür Kesim <oec-taler@kesim.org>
AuthorDate: Sun Jun 25 16:42:02 2023 +0200
[pq] unified/simplified switch-logic
---
src/pq/pq_result_helper.c | 281 +++++++++++++++++++++++++---------------------
1 file changed, 156 insertions(+), 125 deletions(-)
diff --git a/src/pq/pq_result_helper.c b/src/pq/pq_result_helper.c
index cdb7fcbc4..624263812 100644
--- a/src/pq/pq_result_helper.c
+++ b/src/pq/pq_result_helper.c
@@ -22,6 +22,7 @@
* @brief functions to extract result values
* @author Christian Grothoff
*/
+#include "gnunet_time_lib.h"
#include "platform.h"
#include "gnunet_common.h"
#include "gnunet_util_lib.h"
@@ -1216,151 +1217,181 @@ extract_array_generic (
if (NULL != info->num)
*info->num = header.dim;
- switch (info->typ)
- {
- case array_of_bool:
- if (NULL != dst_size)
- *dst_size = sizeof(bool) * (header.dim);
- out = GNUNET_new_array (header.dim, bool);
- break;
- case array_of_uint16:
- if (NULL != dst_size)
- *dst_size = sizeof(uint16_t) * (header.dim);
- out = GNUNET_new_array (header.dim, uint16_t);
- break;
- case array_of_uint32:
- if (NULL != dst_size)
- *dst_size = sizeof(uint32_t) * (header.dim);
- out = GNUNET_new_array (header.dim, uint32_t);
- break;
- case array_of_uint64:
- if (NULL != dst_size)
- *dst_size = sizeof(uint64_t) * (header.dim);
- out = GNUNET_new_array (header.dim, uint64_t);
- break;
- case array_of_abs_time:
- if (NULL != dst_size)
- *dst_size = sizeof(struct GNUNET_TIME_Absolute) * (header.dim);
- out = GNUNET_new_array (header.dim, struct GNUNET_TIME_Absolute);
- break;
- case array_of_rel_time:
- if (NULL != dst_size)
- *dst_size = sizeof(struct GNUNET_TIME_Relative) * (header.dim);
- out = GNUNET_new_array (header.dim, struct GNUNET_TIME_Relative);
- break;
- case array_of_timestamp:
- if (NULL != dst_size)
- *dst_size = sizeof(struct GNUNET_TIME_Timestamp) * (header.dim);
- out = GNUNET_new_array (header.dim, struct GNUNET_TIME_Timestamp);
- break;
- case array_of_byte:
- if (0 == info->same_size)
- *info->sizes = GNUNET_new_array (header.dim, size_t);
- /* fallthrough */
- case array_of_string:
- {
- size_t total = 0;
- bool is_string = (array_of_string == info->typ);
-
- /* first, calculate total size required for allocation */
- {
- char *ptr = data + sizeof(header);
- for (uint32_t i = 0; i < header.dim; i++)
- {
- uint32_t sz;
-
- sz = ntohl (*(uint32_t *) ptr);
- sz += is_string ? 1 : 0;
- total += sz;
- ptr += sizeof(uint32_t);
- ptr += sz;
-
- if ((! is_string) &&
- (0 == info->same_size))
- (*info->sizes)[i] = sz;
-
- FAIL_IF ((0 != info->same_size) &&
- (sz != info->same_size));
- FAIL_IF (total < sz);
- }
- }
-
- if (NULL != dst_size)
- *dst_size = total;
-
- if (0 < total)
- out = GNUNET_malloc (total);
-
- break;
- }
- default:
- FAIL_IF (1 != 0);
- }
-
- *((void **) dst) = out;
- if (NULL == out)
- return GNUNET_OK; /* array length must be 0 */
-
- /* copy data */
{
char *in = data + sizeof(header);
- for (uint32_t i = 0; i < header.dim; i++)
+ switch (info->typ)
{
- size_t sz = ntohl (*(uint32_t *) in);
- in += sizeof(uint32_t);
-
- switch (info->typ)
+ case array_of_bool:
+ if (NULL != dst_size)
+ *dst_size = sizeof(bool) * (header.dim);
+ out = GNUNET_new_array (header.dim, bool);
+ *((void **) dst) = out;
+ for (uint32_t i = 0; i < header.dim; i++)
{
- case array_of_bool:
+ size_t sz = ntohl (*(uint32_t *) in);
FAIL_IF (sz != sizeof(bool));
+ in += sizeof(uint32_t);
*(bool *) out = *(bool *) in;
- break;
- case array_of_uint16:
+ in += sz;
+ out += sz;
+ }
+ break;
+
+ case array_of_uint16:
+ if (NULL != dst_size)
+ *dst_size = sizeof(uint16_t) * (header.dim);
+ out = GNUNET_new_array (header.dim, uint16_t);
+ *((void **) dst) = out;
+ for (uint32_t i = 0; i < header.dim; i++)
+ {
+ size_t sz = ntohl (*(uint32_t *) in);
FAIL_IF (sz != sizeof(uint16_t));
+ in += sizeof(uint32_t);
*(uint16_t *) out = ntohs (*(uint16_t *) in);
- break;
- case array_of_uint32:
+ in += sz;
+ out += sz;
+ }
+ break;
+
+ case array_of_uint32:
+ if (NULL != dst_size)
+ *dst_size = sizeof(uint32_t) * (header.dim);
+ out = GNUNET_new_array (header.dim, uint32_t);
+ *((void **) dst) = out;
+ for (uint32_t i = 0; i < header.dim; i++)
+ {
+ size_t sz = ntohl (*(uint32_t *) in);
FAIL_IF (sz != sizeof(uint32_t));
+ in += sizeof(uint32_t);
*(uint32_t *) out = ntohl (*(uint32_t *) in);
- break;
- case array_of_uint64:
+ in += sz;
+ out += sz;
+ }
+ break;
+
+ case array_of_uint64:
+ if (NULL != dst_size)
+ *dst_size = sizeof(uint64_t) * (header.dim);
+ out = GNUNET_new_array (header.dim, uint64_t);
+ *((void **) dst) = out;
+ for (uint32_t i = 0; i < header.dim; i++)
+ {
+ size_t sz = ntohl (*(uint32_t *) in);
FAIL_IF (sz != sizeof(uint64_t));
+ in += sizeof(uint32_t);
*(uint64_t *) out = GNUNET_ntohll (*(uint64_t *) in);
- break;
- case array_of_abs_time:
- case array_of_rel_time:
- case array_of_timestamp:
+ in += sz;
+ out += sz;
+ }
+ break;
+
+ case array_of_abs_time:
+ if (NULL != dst_size)
+ *dst_size = sizeof(struct GNUNET_TIME_Absolute) * (header.dim);
+ out = GNUNET_new_array (header.dim, struct GNUNET_TIME_Absolute);
+ *((void **) dst) = out;
+ for (uint32_t i = 0; i < header.dim; i++)
+ {
+ size_t sz = ntohl (*(uint32_t *) in);
+ FAIL_IF (sz != sizeof(uint64_t));
+ in += sizeof(uint32_t);
+ ((struct GNUNET_TIME_Absolute *) out)->abs_value_us =
+ GNUNET_ntohll (*(uint64_t *) in);
+ in += sz;
+ out += sz;
+ }
+ break;
+
+ case array_of_rel_time:
+ if (NULL != dst_size)
+ *dst_size = sizeof(struct GNUNET_TIME_Relative) * (header.dim);
+ out = GNUNET_new_array (header.dim, struct GNUNET_TIME_Relative);
+ *((void **) dst) = out;
+ for (uint32_t i = 0; i < header.dim; i++)
+ {
+ size_t sz = ntohl (*(uint32_t *) in);
+ FAIL_IF (sz != sizeof(uint64_t));
+ in += sizeof(uint32_t);
+ ((struct GNUNET_TIME_Relative *) out)->rel_value_us =
+ GNUNET_ntohll (*(uint64_t *) in);
+ in += sz;
+ out += sz;
+ }
+ break;
+
+ case array_of_timestamp:
+ if (NULL != dst_size)
+ *dst_size = sizeof(struct GNUNET_TIME_Timestamp) * (header.dim);
+ out = GNUNET_new_array (header.dim, struct GNUNET_TIME_Timestamp);
+ *((void **) dst) = out;
+ for (uint32_t i = 0; i < header.dim; i++)
+ {
+ size_t sz = ntohl (*(uint32_t *) in);
FAIL_IF (sz != sizeof(uint64_t));
+ in += sizeof(uint32_t);
+ ((struct GNUNET_TIME_Timestamp *) out)->abs_time.abs_value_us =
+ GNUNET_ntohll (*(uint64_t *) in);
+ in += sz;
+ out += sz;
+ }
+ break;
+
+ case array_of_byte:
+ if (0 == info->same_size)
+ *info->sizes = GNUNET_new_array (header.dim, size_t);
+ /* fallthrough */
+ case array_of_string:
+ {
+ size_t total = 0;
+ bool is_string = (array_of_string == info->typ);
+
+ /* first, calculate total size required for allocation */
{
- uint64_t val = GNUNET_ntohll (*(uint64_t *) in);
- switch (info->typ)
+ char *ptr = data + sizeof(header);
+ for (uint32_t i = 0; i < header.dim; i++)
{
- case array_of_abs_time:
- ((struct GNUNET_TIME_Absolute *) out)->abs_value_us = val;
- break;
- case array_of_rel_time:
- ((struct GNUNET_TIME_Relative *) out)->rel_value_us = val;
- break;
- case array_of_timestamp:
- ((struct GNUNET_TIME_Timestamp *) out)->abs_time.abs_value_us =
val;
- break;
- default:
- FAIL_IF (1 != 0);
+ uint32_t sz;
+
+ sz = ntohl (*(uint32_t *) ptr);
+ sz += is_string ? 1 : 0;
+ total += sz;
+ ptr += sizeof(uint32_t);
+ ptr += sz;
+
+ if ((! is_string) &&
+ (0 == info->same_size))
+ (*info->sizes)[i] = sz;
+
+ FAIL_IF ((0 != info->same_size) &&
+ (sz != info->same_size));
+ FAIL_IF (total < sz);
}
}
+
+ if (NULL != dst_size)
+ *dst_size = total;
+
+ if (0 < total)
+ out = GNUNET_malloc (total);
+
+ *((void **) dst) = out;
+
+ /* copy data */
+ for (uint32_t i = 0; i < header.dim; i++)
+ {
+ size_t sz = ntohl (*(uint32_t *) in);
+ in += sizeof(uint32_t);
+ GNUNET_memcpy (out, in, sz);
+
+ in += sz;
+ out += sz;
+ out += (array_of_string == info->typ) ? 1 : 0;
+ }
break;
- case array_of_byte:
- case array_of_string:
- GNUNET_memcpy (out, in, sz);
- break;
- default:
- FAIL_IF (1 != 0);
}
-
- in += sz;
- out += sz;
- out += (array_of_string == info->typ) ? 1 : 0;
+ default:
+ FAIL_IF (1 != 0);
}
}
--
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.