diff options
author | Özgür Kesim <oec-taler@kesim.org> | 2023-06-25 16:42:02 +0200 |
---|---|---|
committer | Özgür Kesim <oec-taler@kesim.org> | 2023-06-25 16:42:02 +0200 |
commit | 7011143dc4a04272c8d7a7744e8e674509b1c727 (patch) | |
tree | 234c8b7c7c11019634d57e4854e9d564b66ba8fc /src | |
parent | 4b3ff0e1e18c2bba72e36dcc97c50cf50fcdb9a1 (diff) | |
download | gnunet-7011143dc4a04272c8d7a7744e8e674509b1c727.tar.gz gnunet-7011143dc4a04272c8d7a7744e8e674509b1c727.zip |
[pq] unified/simplified switch-logic
Diffstat (limited to 'src')
-rw-r--r-- | src/pq/pq_result_helper.c | 281 |
1 files 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 @@ | |||
22 | * @brief functions to extract result values | 22 | * @brief functions to extract result values |
23 | * @author Christian Grothoff | 23 | * @author Christian Grothoff |
24 | */ | 24 | */ |
25 | #include "gnunet_time_lib.h" | ||
25 | #include "platform.h" | 26 | #include "platform.h" |
26 | #include "gnunet_common.h" | 27 | #include "gnunet_common.h" |
27 | #include "gnunet_util_lib.h" | 28 | #include "gnunet_util_lib.h" |
@@ -1216,151 +1217,181 @@ extract_array_generic ( | |||
1216 | if (NULL != info->num) | 1217 | if (NULL != info->num) |
1217 | *info->num = header.dim; | 1218 | *info->num = header.dim; |
1218 | 1219 | ||
1219 | switch (info->typ) | ||
1220 | { | ||
1221 | case array_of_bool: | ||
1222 | if (NULL != dst_size) | ||
1223 | *dst_size = sizeof(bool) * (header.dim); | ||
1224 | out = GNUNET_new_array (header.dim, bool); | ||
1225 | break; | ||
1226 | case array_of_uint16: | ||
1227 | if (NULL != dst_size) | ||
1228 | *dst_size = sizeof(uint16_t) * (header.dim); | ||
1229 | out = GNUNET_new_array (header.dim, uint16_t); | ||
1230 | break; | ||
1231 | case array_of_uint32: | ||
1232 | if (NULL != dst_size) | ||
1233 | *dst_size = sizeof(uint32_t) * (header.dim); | ||
1234 | out = GNUNET_new_array (header.dim, uint32_t); | ||
1235 | break; | ||
1236 | case array_of_uint64: | ||
1237 | if (NULL != dst_size) | ||
1238 | *dst_size = sizeof(uint64_t) * (header.dim); | ||
1239 | out = GNUNET_new_array (header.dim, uint64_t); | ||
1240 | break; | ||
1241 | case array_of_abs_time: | ||
1242 | if (NULL != dst_size) | ||
1243 | *dst_size = sizeof(struct GNUNET_TIME_Absolute) * (header.dim); | ||
1244 | out = GNUNET_new_array (header.dim, struct GNUNET_TIME_Absolute); | ||
1245 | break; | ||
1246 | case array_of_rel_time: | ||
1247 | if (NULL != dst_size) | ||
1248 | *dst_size = sizeof(struct GNUNET_TIME_Relative) * (header.dim); | ||
1249 | out = GNUNET_new_array (header.dim, struct GNUNET_TIME_Relative); | ||
1250 | break; | ||
1251 | case array_of_timestamp: | ||
1252 | if (NULL != dst_size) | ||
1253 | *dst_size = sizeof(struct GNUNET_TIME_Timestamp) * (header.dim); | ||
1254 | out = GNUNET_new_array (header.dim, struct GNUNET_TIME_Timestamp); | ||
1255 | break; | ||
1256 | case array_of_byte: | ||
1257 | if (0 == info->same_size) | ||
1258 | *info->sizes = GNUNET_new_array (header.dim, size_t); | ||
1259 | /* fallthrough */ | ||
1260 | case array_of_string: | ||
1261 | { | ||
1262 | size_t total = 0; | ||
1263 | bool is_string = (array_of_string == info->typ); | ||
1264 | |||
1265 | /* first, calculate total size required for allocation */ | ||
1266 | { | ||
1267 | char *ptr = data + sizeof(header); | ||
1268 | for (uint32_t i = 0; i < header.dim; i++) | ||
1269 | { | ||
1270 | uint32_t sz; | ||
1271 | |||
1272 | sz = ntohl (*(uint32_t *) ptr); | ||
1273 | sz += is_string ? 1 : 0; | ||
1274 | total += sz; | ||
1275 | ptr += sizeof(uint32_t); | ||
1276 | ptr += sz; | ||
1277 | |||
1278 | if ((! is_string) && | ||
1279 | (0 == info->same_size)) | ||
1280 | (*info->sizes)[i] = sz; | ||
1281 | |||
1282 | FAIL_IF ((0 != info->same_size) && | ||
1283 | (sz != info->same_size)); | ||
1284 | FAIL_IF (total < sz); | ||
1285 | } | ||
1286 | } | ||
1287 | |||
1288 | if (NULL != dst_size) | ||
1289 | *dst_size = total; | ||
1290 | |||
1291 | if (0 < total) | ||
1292 | out = GNUNET_malloc (total); | ||
1293 | |||
1294 | break; | ||
1295 | } | ||
1296 | default: | ||
1297 | FAIL_IF (1 != 0); | ||
1298 | } | ||
1299 | |||
1300 | *((void **) dst) = out; | ||
1301 | if (NULL == out) | ||
1302 | return GNUNET_OK; /* array length must be 0 */ | ||
1303 | |||
1304 | /* copy data */ | ||
1305 | { | 1220 | { |
1306 | char *in = data + sizeof(header); | 1221 | char *in = data + sizeof(header); |
1307 | 1222 | ||
1308 | for (uint32_t i = 0; i < header.dim; i++) | 1223 | switch (info->typ) |
1309 | { | 1224 | { |
1310 | size_t sz = ntohl (*(uint32_t *) in); | 1225 | case array_of_bool: |
1311 | in += sizeof(uint32_t); | 1226 | if (NULL != dst_size) |
1312 | 1227 | *dst_size = sizeof(bool) * (header.dim); | |
1313 | switch (info->typ) | 1228 | out = GNUNET_new_array (header.dim, bool); |
1229 | *((void **) dst) = out; | ||
1230 | for (uint32_t i = 0; i < header.dim; i++) | ||
1314 | { | 1231 | { |
1315 | case array_of_bool: | 1232 | size_t sz = ntohl (*(uint32_t *) in); |
1316 | FAIL_IF (sz != sizeof(bool)); | 1233 | FAIL_IF (sz != sizeof(bool)); |
1234 | in += sizeof(uint32_t); | ||
1317 | *(bool *) out = *(bool *) in; | 1235 | *(bool *) out = *(bool *) in; |
1318 | break; | 1236 | in += sz; |
1319 | case array_of_uint16: | 1237 | out += sz; |
1238 | } | ||
1239 | break; | ||
1240 | |||
1241 | case array_of_uint16: | ||
1242 | if (NULL != dst_size) | ||
1243 | *dst_size = sizeof(uint16_t) * (header.dim); | ||
1244 | out = GNUNET_new_array (header.dim, uint16_t); | ||
1245 | *((void **) dst) = out; | ||
1246 | for (uint32_t i = 0; i < header.dim; i++) | ||
1247 | { | ||
1248 | size_t sz = ntohl (*(uint32_t *) in); | ||
1320 | FAIL_IF (sz != sizeof(uint16_t)); | 1249 | FAIL_IF (sz != sizeof(uint16_t)); |
1250 | in += sizeof(uint32_t); | ||
1321 | *(uint16_t *) out = ntohs (*(uint16_t *) in); | 1251 | *(uint16_t *) out = ntohs (*(uint16_t *) in); |
1322 | break; | 1252 | in += sz; |
1323 | case array_of_uint32: | 1253 | out += sz; |
1254 | } | ||
1255 | break; | ||
1256 | |||
1257 | case array_of_uint32: | ||
1258 | if (NULL != dst_size) | ||
1259 | *dst_size = sizeof(uint32_t) * (header.dim); | ||
1260 | out = GNUNET_new_array (header.dim, uint32_t); | ||
1261 | *((void **) dst) = out; | ||
1262 | for (uint32_t i = 0; i < header.dim; i++) | ||
1263 | { | ||
1264 | size_t sz = ntohl (*(uint32_t *) in); | ||
1324 | FAIL_IF (sz != sizeof(uint32_t)); | 1265 | FAIL_IF (sz != sizeof(uint32_t)); |
1266 | in += sizeof(uint32_t); | ||
1325 | *(uint32_t *) out = ntohl (*(uint32_t *) in); | 1267 | *(uint32_t *) out = ntohl (*(uint32_t *) in); |
1326 | break; | 1268 | in += sz; |
1327 | case array_of_uint64: | 1269 | out += sz; |
1270 | } | ||
1271 | break; | ||
1272 | |||
1273 | case array_of_uint64: | ||
1274 | if (NULL != dst_size) | ||
1275 | *dst_size = sizeof(uint64_t) * (header.dim); | ||
1276 | out = GNUNET_new_array (header.dim, uint64_t); | ||
1277 | *((void **) dst) = out; | ||
1278 | for (uint32_t i = 0; i < header.dim; i++) | ||
1279 | { | ||
1280 | size_t sz = ntohl (*(uint32_t *) in); | ||
1328 | FAIL_IF (sz != sizeof(uint64_t)); | 1281 | FAIL_IF (sz != sizeof(uint64_t)); |
1282 | in += sizeof(uint32_t); | ||
1329 | *(uint64_t *) out = GNUNET_ntohll (*(uint64_t *) in); | 1283 | *(uint64_t *) out = GNUNET_ntohll (*(uint64_t *) in); |
1330 | break; | 1284 | in += sz; |
1331 | case array_of_abs_time: | 1285 | out += sz; |
1332 | case array_of_rel_time: | 1286 | } |
1333 | case array_of_timestamp: | 1287 | break; |
1288 | |||
1289 | case array_of_abs_time: | ||
1290 | if (NULL != dst_size) | ||
1291 | *dst_size = sizeof(struct GNUNET_TIME_Absolute) * (header.dim); | ||
1292 | out = GNUNET_new_array (header.dim, struct GNUNET_TIME_Absolute); | ||
1293 | *((void **) dst) = out; | ||
1294 | for (uint32_t i = 0; i < header.dim; i++) | ||
1295 | { | ||
1296 | size_t sz = ntohl (*(uint32_t *) in); | ||
1297 | FAIL_IF (sz != sizeof(uint64_t)); | ||
1298 | in += sizeof(uint32_t); | ||
1299 | ((struct GNUNET_TIME_Absolute *) out)->abs_value_us = | ||
1300 | GNUNET_ntohll (*(uint64_t *) in); | ||
1301 | in += sz; | ||
1302 | out += sz; | ||
1303 | } | ||
1304 | break; | ||
1305 | |||
1306 | case array_of_rel_time: | ||
1307 | if (NULL != dst_size) | ||
1308 | *dst_size = sizeof(struct GNUNET_TIME_Relative) * (header.dim); | ||
1309 | out = GNUNET_new_array (header.dim, struct GNUNET_TIME_Relative); | ||
1310 | *((void **) dst) = out; | ||
1311 | for (uint32_t i = 0; i < header.dim; i++) | ||
1312 | { | ||
1313 | size_t sz = ntohl (*(uint32_t *) in); | ||
1314 | FAIL_IF (sz != sizeof(uint64_t)); | ||
1315 | in += sizeof(uint32_t); | ||
1316 | ((struct GNUNET_TIME_Relative *) out)->rel_value_us = | ||
1317 | GNUNET_ntohll (*(uint64_t *) in); | ||
1318 | in += sz; | ||
1319 | out += sz; | ||
1320 | } | ||
1321 | break; | ||
1322 | |||
1323 | case array_of_timestamp: | ||
1324 | if (NULL != dst_size) | ||
1325 | *dst_size = sizeof(struct GNUNET_TIME_Timestamp) * (header.dim); | ||
1326 | out = GNUNET_new_array (header.dim, struct GNUNET_TIME_Timestamp); | ||
1327 | *((void **) dst) = out; | ||
1328 | for (uint32_t i = 0; i < header.dim; i++) | ||
1329 | { | ||
1330 | size_t sz = ntohl (*(uint32_t *) in); | ||
1334 | FAIL_IF (sz != sizeof(uint64_t)); | 1331 | FAIL_IF (sz != sizeof(uint64_t)); |
1332 | in += sizeof(uint32_t); | ||
1333 | ((struct GNUNET_TIME_Timestamp *) out)->abs_time.abs_value_us = | ||
1334 | GNUNET_ntohll (*(uint64_t *) in); | ||
1335 | in += sz; | ||
1336 | out += sz; | ||
1337 | } | ||
1338 | break; | ||
1339 | |||
1340 | case array_of_byte: | ||
1341 | if (0 == info->same_size) | ||
1342 | *info->sizes = GNUNET_new_array (header.dim, size_t); | ||
1343 | /* fallthrough */ | ||
1344 | case array_of_string: | ||
1345 | { | ||
1346 | size_t total = 0; | ||
1347 | bool is_string = (array_of_string == info->typ); | ||
1348 | |||
1349 | /* first, calculate total size required for allocation */ | ||
1335 | { | 1350 | { |
1336 | uint64_t val = GNUNET_ntohll (*(uint64_t *) in); | 1351 | char *ptr = data + sizeof(header); |
1337 | switch (info->typ) | 1352 | for (uint32_t i = 0; i < header.dim; i++) |
1338 | { | 1353 | { |
1339 | case array_of_abs_time: | 1354 | uint32_t sz; |
1340 | ((struct GNUNET_TIME_Absolute *) out)->abs_value_us = val; | 1355 | |
1341 | break; | 1356 | sz = ntohl (*(uint32_t *) ptr); |
1342 | case array_of_rel_time: | 1357 | sz += is_string ? 1 : 0; |
1343 | ((struct GNUNET_TIME_Relative *) out)->rel_value_us = val; | 1358 | total += sz; |
1344 | break; | 1359 | ptr += sizeof(uint32_t); |
1345 | case array_of_timestamp: | 1360 | ptr += sz; |
1346 | ((struct GNUNET_TIME_Timestamp *) out)->abs_time.abs_value_us = val; | 1361 | |
1347 | break; | 1362 | if ((! is_string) && |
1348 | default: | 1363 | (0 == info->same_size)) |
1349 | FAIL_IF (1 != 0); | 1364 | (*info->sizes)[i] = sz; |
1365 | |||
1366 | FAIL_IF ((0 != info->same_size) && | ||
1367 | (sz != info->same_size)); | ||
1368 | FAIL_IF (total < sz); | ||
1350 | } | 1369 | } |
1351 | } | 1370 | } |
1371 | |||
1372 | if (NULL != dst_size) | ||
1373 | *dst_size = total; | ||
1374 | |||
1375 | if (0 < total) | ||
1376 | out = GNUNET_malloc (total); | ||
1377 | |||
1378 | *((void **) dst) = out; | ||
1379 | |||
1380 | /* copy data */ | ||
1381 | for (uint32_t i = 0; i < header.dim; i++) | ||
1382 | { | ||
1383 | size_t sz = ntohl (*(uint32_t *) in); | ||
1384 | in += sizeof(uint32_t); | ||
1385 | GNUNET_memcpy (out, in, sz); | ||
1386 | |||
1387 | in += sz; | ||
1388 | out += sz; | ||
1389 | out += (array_of_string == info->typ) ? 1 : 0; | ||
1390 | } | ||
1352 | break; | 1391 | break; |
1353 | case array_of_byte: | ||
1354 | case array_of_string: | ||
1355 | GNUNET_memcpy (out, in, sz); | ||
1356 | break; | ||
1357 | default: | ||
1358 | FAIL_IF (1 != 0); | ||
1359 | } | 1392 | } |
1360 | 1393 | default: | |
1361 | in += sz; | 1394 | FAIL_IF (1 != 0); |
1362 | out += sz; | ||
1363 | out += (array_of_string == info->typ) ? 1 : 0; | ||
1364 | } | 1395 | } |
1365 | } | 1396 | } |
1366 | 1397 | ||