summaryrefslogtreecommitdiff
path: root/src/pq
diff options
context:
space:
mode:
Diffstat (limited to 'src/pq')
-rw-r--r--src/pq/pq.c156
-rw-r--r--src/pq/pq_connect.c108
-rw-r--r--src/pq/pq_eval.c294
-rw-r--r--src/pq/pq_exec.c88
-rw-r--r--src/pq/pq_prepare.c88
-rw-r--r--src/pq/pq_query_helper.c282
-rw-r--r--src/pq/pq_result_helper.c838
-rw-r--r--src/pq/test_pq.c382
8 files changed, 1126 insertions, 1110 deletions
diff --git a/src/pq/pq.c b/src/pq/pq.c
index df5ef17ad..a581b4277 100644
--- a/src/pq/pq.c
+++ b/src/pq/pq.c
@@ -1,22 +1,22 @@
1/* 1/*
2 This file is part of GNUnet 2 This file is part of GNUnet
3 Copyright (C) 2014, 2015, 2016 GNUnet e.V. 3 Copyright (C) 2014, 2015, 2016 GNUnet e.V.
4 4
5 GNUnet is free software: you can redistribute it and/or modify it 5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published 6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License, 7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version. 8 or (at your option) any later version.
9 9
10 GNUnet is distributed in the hope that it will be useful, but 10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20/** 20/**
21 * @file pq/pq.c 21 * @file pq/pq.c
22 * @brief helper functions for libpq (PostGres) interactions 22 * @brief helper functions for libpq (PostGres) interactions
@@ -38,20 +38,20 @@
38 * @return postgres result 38 * @return postgres result
39 */ 39 */
40PGresult * 40PGresult *
41GNUNET_PQ_exec_prepared (PGconn *db_conn, 41GNUNET_PQ_exec_prepared(PGconn *db_conn,
42 const char *name, 42 const char *name,
43 const struct GNUNET_PQ_QueryParam *params) 43 const struct GNUNET_PQ_QueryParam *params)
44{ 44{
45 unsigned int len; 45 unsigned int len;
46 unsigned int i; 46 unsigned int i;
47 47
48 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 48 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
49 "Running prepared statement `%s' on %p\n", 49 "Running prepared statement `%s' on %p\n",
50 name, 50 name,
51 db_conn); 51 db_conn);
52 /* count the number of parameters */ 52 /* count the number of parameters */
53 len = 0; 53 len = 0;
54 for (i=0;0 != params[i].num_params;i++) 54 for (i = 0; 0 != params[i].num_params; i++)
55 len += params[i].num_params; 55 len += params[i].num_params;
56 56
57 /* new scope to allow stack allocation without alloca */ 57 /* new scope to allow stack allocation without alloca */
@@ -70,42 +70,42 @@ GNUNET_PQ_exec_prepared (PGconn *db_conn,
70 70
71 off = 0; 71 off = 0;
72 soff = 0; 72 soff = 0;
73 for (i=0;0 != params[i].num_params;i++) 73 for (i = 0; 0 != params[i].num_params; i++)
74 {
75 const struct GNUNET_PQ_QueryParam *x = &params[i];
76
77 ret = x->conv (x->conv_cls,
78 x->data,
79 x->size,
80 &param_values[off],
81 &param_lengths[off],
82 &param_formats[off],
83 x->num_params,
84 &scratch[soff],
85 len - soff);
86 if (ret < 0)
87 { 74 {
88 for (off = 0; off < soff; off++) 75 const struct GNUNET_PQ_QueryParam *x = &params[i];
89 GNUNET_free (scratch[off]); 76
90 return NULL; 77 ret = x->conv(x->conv_cls,
78 x->data,
79 x->size,
80 &param_values[off],
81 &param_lengths[off],
82 &param_formats[off],
83 x->num_params,
84 &scratch[soff],
85 len - soff);
86 if (ret < 0)
87 {
88 for (off = 0; off < soff; off++)
89 GNUNET_free(scratch[off]);
90 return NULL;
91 }
92 soff += ret;
93 off += x->num_params;
91 } 94 }
92 soff += ret; 95 GNUNET_assert(off == len);
93 off += x->num_params; 96 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG,
94 } 97 "pq",
95 GNUNET_assert (off == len); 98 "Executing prepared SQL statement `%s'\n",
96 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, 99 name);
97 "pq", 100 res = PQexecPrepared(db_conn,
98 "Executing prepared SQL statement `%s'\n", 101 name,
99 name); 102 len,
100 res = PQexecPrepared (db_conn, 103 (const char **)param_values,
101 name, 104 param_lengths,
102 len, 105 param_formats,
103 (const char **) param_values, 106 1);
104 param_lengths,
105 param_formats,
106 1);
107 for (off = 0; off < soff; off++) 107 for (off = 0; off < soff; off++)
108 GNUNET_free (scratch[off]); 108 GNUNET_free(scratch[off]);
109 return res; 109 return res;
110 } 110 }
111} 111}
@@ -118,14 +118,14 @@ GNUNET_PQ_exec_prepared (PGconn *db_conn,
118 * @param rs reult specification to clean up 118 * @param rs reult specification to clean up
119 */ 119 */
120void 120void
121GNUNET_PQ_cleanup_result (struct GNUNET_PQ_ResultSpec *rs) 121GNUNET_PQ_cleanup_result(struct GNUNET_PQ_ResultSpec *rs)
122{ 122{
123 unsigned int i; 123 unsigned int i;
124 124
125 for (i=0; NULL != rs[i].conv; i++) 125 for (i = 0; NULL != rs[i].conv; i++)
126 if (NULL != rs[i].cleaner) 126 if (NULL != rs[i].cleaner)
127 rs[i].cleaner (rs[i].cls, 127 rs[i].cleaner(rs[i].cls,
128 rs[i].dst); 128 rs[i].dst);
129} 129}
130 130
131 131
@@ -141,32 +141,32 @@ GNUNET_PQ_cleanup_result (struct GNUNET_PQ_ResultSpec *rs)
141 * #GNUNET_SYSERR if a result was invalid (non-existing field) 141 * #GNUNET_SYSERR if a result was invalid (non-existing field)
142 */ 142 */
143int 143int
144GNUNET_PQ_extract_result (PGresult *result, 144GNUNET_PQ_extract_result(PGresult *result,
145 struct GNUNET_PQ_ResultSpec *rs, 145 struct GNUNET_PQ_ResultSpec *rs,
146 int row) 146 int row)
147{ 147{
148 unsigned int i; 148 unsigned int i;
149 int ret; 149 int ret;
150 150
151 for (i=0; NULL != rs[i].conv; i++) 151 for (i = 0; NULL != rs[i].conv; i++)
152 {
153 struct GNUNET_PQ_ResultSpec *spec;
154
155 spec = &rs[i];
156 ret = spec->conv (spec->cls,
157 result,
158 row,
159 spec->fname,
160 &spec->dst_size,
161 spec->dst);
162 if (GNUNET_OK != ret)
163 { 152 {
164 GNUNET_PQ_cleanup_result (rs); 153 struct GNUNET_PQ_ResultSpec *spec;
165 return GNUNET_SYSERR; 154
155 spec = &rs[i];
156 ret = spec->conv(spec->cls,
157 result,
158 row,
159 spec->fname,
160 &spec->dst_size,
161 spec->dst);
162 if (GNUNET_OK != ret)
163 {
164 GNUNET_PQ_cleanup_result(rs);
165 return GNUNET_SYSERR;
166 }
167 if (NULL != spec->result_size)
168 *spec->result_size = spec->dst_size;
166 } 169 }
167 if (NULL != spec->result_size)
168 *spec->result_size = spec->dst_size;
169 }
170 return GNUNET_OK; 170 return GNUNET_OK;
171} 171}
172 172
diff --git a/src/pq/pq_connect.c b/src/pq/pq_connect.c
index a295eb864..2ab049724 100644
--- a/src/pq/pq_connect.c
+++ b/src/pq/pq_connect.c
@@ -1,22 +1,22 @@
1/* 1/*
2 This file is part of GNUnet 2 This file is part of GNUnet
3 Copyright (C) 2017 GNUnet e.V. 3 Copyright (C) 2017 GNUnet e.V.
4 4
5 GNUnet is free software: you can redistribute it and/or modify it 5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published 6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License, 7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version. 8 or (at your option) any later version.
9 9
10 GNUnet is distributed in the hope that it will be useful, but 10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20/** 20/**
21 * @file pq/pq_connect.c 21 * @file pq/pq_connect.c
22 * @brief functions to connect to libpq (PostGres) 22 * @brief functions to connect to libpq (PostGres)
@@ -36,8 +36,8 @@
36 * @param res information about some libpq event 36 * @param res information about some libpq event
37 */ 37 */
38static void 38static void
39pq_notice_receiver_cb (void *arg, 39pq_notice_receiver_cb(void *arg,
40 const PGresult *res) 40 const PGresult *res)
41{ 41{
42 /* do nothing, intentionally */ 42 /* do nothing, intentionally */
43} 43}
@@ -51,13 +51,13 @@ pq_notice_receiver_cb (void *arg,
51 * @param message information about some libpq event 51 * @param message information about some libpq event
52 */ 52 */
53static void 53static void
54pq_notice_processor_cb (void *arg, 54pq_notice_processor_cb(void *arg,
55 const char *message) 55 const char *message)
56{ 56{
57 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, 57 GNUNET_log_from(GNUNET_ERROR_TYPE_INFO,
58 "pq", 58 "pq",
59 "%s", 59 "%s",
60 message); 60 message);
61} 61}
62 62
63 63
@@ -70,32 +70,32 @@ pq_notice_processor_cb (void *arg,
70 * @return NULL on error 70 * @return NULL on error
71 */ 71 */
72PGconn * 72PGconn *
73GNUNET_PQ_connect (const char *config_str) 73GNUNET_PQ_connect(const char *config_str)
74{ 74{
75 PGconn *conn; 75 PGconn *conn;
76 76
77 conn = PQconnectdb (config_str); 77 conn = PQconnectdb(config_str);
78 if ( (NULL == conn) || 78 if ((NULL == conn) ||
79 (CONNECTION_OK != 79 (CONNECTION_OK !=
80 PQstatus (conn)) ) 80 PQstatus(conn)))
81 { 81 {
82 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, 82 GNUNET_log_from(GNUNET_ERROR_TYPE_ERROR,
83 "pq", 83 "pq",
84 "Database connection to '%s' failed: %s\n", 84 "Database connection to '%s' failed: %s\n",
85 config_str, 85 config_str,
86 (NULL != conn) ? 86 (NULL != conn) ?
87 PQerrorMessage (conn) 87 PQerrorMessage(conn)
88 : "PQconnectdb returned NULL"); 88 : "PQconnectdb returned NULL");
89 if (NULL != conn) 89 if (NULL != conn)
90 PQfinish (conn); 90 PQfinish(conn);
91 return NULL; 91 return NULL;
92 } 92 }
93 PQsetNoticeReceiver (conn, 93 PQsetNoticeReceiver(conn,
94 &pq_notice_receiver_cb, 94 &pq_notice_receiver_cb,
95 conn);
96 PQsetNoticeProcessor(conn,
97 &pq_notice_processor_cb,
95 conn); 98 conn);
96 PQsetNoticeProcessor (conn,
97 &pq_notice_processor_cb,
98 conn);
99 return conn; 99 return conn;
100} 100}
101 101
@@ -109,21 +109,21 @@ GNUNET_PQ_connect (const char *config_str)
109 * @return the postgres handle, NULL on error 109 * @return the postgres handle, NULL on error
110 */ 110 */
111PGconn * 111PGconn *
112GNUNET_PQ_connect_with_cfg (const struct GNUNET_CONFIGURATION_Handle * cfg, 112GNUNET_PQ_connect_with_cfg(const struct GNUNET_CONFIGURATION_Handle * cfg,
113 const char *section) 113 const char *section)
114{ 114{
115 PGconn *dbh; 115 PGconn *dbh;
116 char *conninfo; 116 char *conninfo;
117 117
118 /* Open database and precompile statements */ 118 /* Open database and precompile statements */
119 if (GNUNET_OK != 119 if (GNUNET_OK !=
120 GNUNET_CONFIGURATION_get_value_string (cfg, 120 GNUNET_CONFIGURATION_get_value_string(cfg,
121 section, 121 section,
122 "CONFIG", 122 "CONFIG",
123 &conninfo)) 123 &conninfo))
124 conninfo = NULL; 124 conninfo = NULL;
125 dbh = GNUNET_PQ_connect (conninfo == NULL ? "" : conninfo); 125 dbh = GNUNET_PQ_connect(conninfo == NULL ? "" : conninfo);
126 GNUNET_free_non_null (conninfo); 126 GNUNET_free_non_null(conninfo);
127 return dbh; 127 return dbh;
128} 128}
129 129
diff --git a/src/pq/pq_eval.c b/src/pq/pq_eval.c
index 2beb3475b..0df71e672 100644
--- a/src/pq/pq_eval.c
+++ b/src/pq/pq_eval.c
@@ -1,22 +1,22 @@
1/* 1/*
2 This file is part of GNUnet 2 This file is part of GNUnet
3 Copyright (C) 2017 GNUnet e.V. 3 Copyright (C) 2017 GNUnet e.V.
4 4
5 GNUnet is free software: you can redistribute it and/or modify it 5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published 6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License, 7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version. 8 or (at your option) any later version.
9 9
10 GNUnet is distributed in the hope that it will be useful, but 10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20/** 20/**
21 * @file pq/pq_eval.c 21 * @file pq/pq_eval.c
22 * @brief functions to execute SQL statements with arguments and/or results (PostGres) 22 * @brief functions to execute SQL statements with arguments and/or results (PostGres)
@@ -57,76 +57,76 @@
57 * @deprecated (low level, let's see if we can do with just the high-level functions) 57 * @deprecated (low level, let's see if we can do with just the high-level functions)
58 */ 58 */
59enum GNUNET_DB_QueryStatus 59enum GNUNET_DB_QueryStatus
60GNUNET_PQ_eval_result (PGconn *connection, 60GNUNET_PQ_eval_result(PGconn *connection,
61 const char *statement_name, 61 const char *statement_name,
62 PGresult *result) 62 PGresult *result)
63{ 63{
64 ExecStatusType est; 64 ExecStatusType est;
65 65
66 est = PQresultStatus (result); 66 est = PQresultStatus(result);
67 if ( (PGRES_COMMAND_OK != est) && 67 if ((PGRES_COMMAND_OK != est) &&
68 (PGRES_TUPLES_OK != est) ) 68 (PGRES_TUPLES_OK != est))
69 {
70 const char *sqlstate;
71
72 sqlstate = PQresultErrorField (result,
73 PG_DIAG_SQLSTATE);
74 if (NULL == sqlstate)
75 { 69 {
76 /* very unexpected... */ 70 const char *sqlstate;
77 GNUNET_break (0); 71
78 return GNUNET_DB_STATUS_HARD_ERROR; 72 sqlstate = PQresultErrorField(result,
79 } 73 PG_DIAG_SQLSTATE);
80 if ( (0 == strcmp (sqlstate, 74 if (NULL == sqlstate)
75 {
76 /* very unexpected... */
77 GNUNET_break(0);
78 return GNUNET_DB_STATUS_HARD_ERROR;
79 }
80 if ((0 == strcmp(sqlstate,
81 PQ_DIAG_SQLSTATE_DEADLOCK)) || 81 PQ_DIAG_SQLSTATE_DEADLOCK)) ||
82 (0 == strcmp (sqlstate, 82 (0 == strcmp(sqlstate,
83 PQ_DIAG_SQLSTATE_SERIALIZATION_FAILURE)) ) 83 PQ_DIAG_SQLSTATE_SERIALIZATION_FAILURE)))
84 { 84 {
85 /* These two can be retried and have a fair chance of working 85 /* These two can be retried and have a fair chance of working
86 the next time */ 86 the next time */
87 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, 87 GNUNET_log_from(GNUNET_ERROR_TYPE_INFO,
88 "pq", 88 "pq",
89 "Query `%s' failed with result: %s/%s/%s/%s/%s\n", 89 "Query `%s' failed with result: %s/%s/%s/%s/%s\n",
90 statement_name, 90 statement_name,
91 PQresultErrorField (result, 91 PQresultErrorField(result,
92 PG_DIAG_MESSAGE_PRIMARY), 92 PG_DIAG_MESSAGE_PRIMARY),
93 PQresultErrorField (result, 93 PQresultErrorField(result,
94 PG_DIAG_MESSAGE_DETAIL), 94 PG_DIAG_MESSAGE_DETAIL),
95 PQresultErrorMessage (result), 95 PQresultErrorMessage(result),
96 PQresStatus (PQresultStatus (result)), 96 PQresStatus(PQresultStatus(result)),
97 PQerrorMessage (connection)); 97 PQerrorMessage(connection));
98 return GNUNET_DB_STATUS_SOFT_ERROR; 98 return GNUNET_DB_STATUS_SOFT_ERROR;
99 } 99 }
100 if (0 == strcmp (sqlstate, 100 if (0 == strcmp(sqlstate,
101 PQ_DIAG_SQLSTATE_UNIQUE_VIOLATION)) 101 PQ_DIAG_SQLSTATE_UNIQUE_VIOLATION))
102 { 102 {
103 /* Likely no need to retry, INSERT of "same" data. */ 103 /* Likely no need to retry, INSERT of "same" data. */
104 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, 104 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG,
105 "pq", 105 "pq",
106 "Query `%s' failed with unique violation: %s/%s/%s/%s/%s\n", 106 "Query `%s' failed with unique violation: %s/%s/%s/%s/%s\n",
107 statement_name, 107 statement_name,
108 PQresultErrorField (result, 108 PQresultErrorField(result,
109 PG_DIAG_MESSAGE_PRIMARY), 109 PG_DIAG_MESSAGE_PRIMARY),
110 PQresultErrorField (result, 110 PQresultErrorField(result,
111 PG_DIAG_MESSAGE_DETAIL), 111 PG_DIAG_MESSAGE_DETAIL),
112 PQresultErrorMessage (result), 112 PQresultErrorMessage(result),
113 PQresStatus (PQresultStatus (result)), 113 PQresStatus(PQresultStatus(result)),
114 PQerrorMessage (connection)); 114 PQerrorMessage(connection));
115 return GNUNET_DB_STATUS_SUCCESS_NO_RESULTS; 115 return GNUNET_DB_STATUS_SUCCESS_NO_RESULTS;
116 } 116 }
117 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, 117 GNUNET_log_from(GNUNET_ERROR_TYPE_ERROR,
118 "pq", 118 "pq",
119 "Query `%s' failed with result: %s/%s/%s/%s/%s\n", 119 "Query `%s' failed with result: %s/%s/%s/%s/%s\n",
120 statement_name, 120 statement_name,
121 PQresultErrorField (result, 121 PQresultErrorField(result,
122 PG_DIAG_MESSAGE_PRIMARY), 122 PG_DIAG_MESSAGE_PRIMARY),
123 PQresultErrorField (result, 123 PQresultErrorField(result,
124 PG_DIAG_MESSAGE_DETAIL), 124 PG_DIAG_MESSAGE_DETAIL),
125 PQresultErrorMessage (result), 125 PQresultErrorMessage(result),
126 PQresStatus (PQresultStatus (result)), 126 PQresStatus(PQresultStatus(result)),
127 PQerrorMessage (connection)); 127 PQerrorMessage(connection));
128 return GNUNET_DB_STATUS_HARD_ERROR; 128 return GNUNET_DB_STATUS_HARD_ERROR;
129 } 129 }
130 return GNUNET_DB_STATUS_SUCCESS_NO_RESULTS; 130 return GNUNET_DB_STATUS_SUCCESS_NO_RESULTS;
131} 131}
132 132
@@ -148,29 +148,29 @@ GNUNET_PQ_eval_result (PGconn *connection,
148 * zero; if INSERT was successful, we return one. 148 * zero; if INSERT was successful, we return one.
149 */ 149 */
150enum GNUNET_DB_QueryStatus 150enum GNUNET_DB_QueryStatus
151GNUNET_PQ_eval_prepared_non_select (PGconn *connection, 151GNUNET_PQ_eval_prepared_non_select(PGconn *connection,
152 const char *statement_name, 152 const char *statement_name,
153 const struct GNUNET_PQ_QueryParam *params) 153 const struct GNUNET_PQ_QueryParam *params)
154{ 154{
155 PGresult *result; 155 PGresult *result;
156 enum GNUNET_DB_QueryStatus qs; 156 enum GNUNET_DB_QueryStatus qs;
157 157
158 result = GNUNET_PQ_exec_prepared (connection, 158 result = GNUNET_PQ_exec_prepared(connection,
159 statement_name, 159 statement_name,
160 params); 160 params);
161 qs = GNUNET_PQ_eval_result (connection, 161 qs = GNUNET_PQ_eval_result(connection,
162 statement_name, 162 statement_name,
163 result); 163 result);
164 if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs) 164 if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs)
165 { 165 {
166 const char *tuples; 166 const char *tuples;
167 167
168 /* What an awful API, this function really does return a string */ 168 /* What an awful API, this function really does return a string */
169 tuples = PQcmdTuples (result); 169 tuples = PQcmdTuples(result);
170 if (NULL != tuples) 170 if (NULL != tuples)
171 qs = strtol (tuples, NULL, 10); 171 qs = strtol(tuples, NULL, 10);
172 } 172 }
173 PQclear (result); 173 PQclear(result);
174 return qs; 174 return qs;
175} 175}
176 176
@@ -191,33 +191,33 @@ GNUNET_PQ_eval_prepared_non_select (PGconn *connection,
191 * codes to `enum GNUNET_DB_QueryStatus`. 191 * codes to `enum GNUNET_DB_QueryStatus`.
192 */ 192 */
193enum GNUNET_DB_QueryStatus 193enum GNUNET_DB_QueryStatus
194GNUNET_PQ_eval_prepared_multi_select (PGconn *connection, 194GNUNET_PQ_eval_prepared_multi_select(PGconn *connection,
195 const char *statement_name, 195 const char *statement_name,
196 const struct GNUNET_PQ_QueryParam *params, 196 const struct GNUNET_PQ_QueryParam *params,
197 GNUNET_PQ_PostgresResultHandler rh, 197 GNUNET_PQ_PostgresResultHandler rh,
198 void *rh_cls) 198 void *rh_cls)
199{ 199{
200 PGresult *result; 200 PGresult *result;
201 enum GNUNET_DB_QueryStatus qs; 201 enum GNUNET_DB_QueryStatus qs;
202 unsigned int ret; 202 unsigned int ret;
203 203
204 result = GNUNET_PQ_exec_prepared (connection, 204 result = GNUNET_PQ_exec_prepared(connection,
205 statement_name, 205 statement_name,
206 params); 206 params);
207 qs = GNUNET_PQ_eval_result (connection, 207 qs = GNUNET_PQ_eval_result(connection,
208 statement_name, 208 statement_name,
209 result); 209 result);
210 if (qs < 0) 210 if (qs < 0)
211 { 211 {
212 PQclear (result); 212 PQclear(result);
213 return qs; 213 return qs;
214 } 214 }
215 ret = PQntuples (result); 215 ret = PQntuples(result);
216 if (NULL != rh) 216 if (NULL != rh)
217 rh (rh_cls, 217 rh(rh_cls,
218 result, 218 result,
219 ret); 219 ret);
220 PQclear (result); 220 PQclear(result);
221 return ret; 221 return ret;
222} 222}
223 223
@@ -238,46 +238,46 @@ GNUNET_PQ_eval_prepared_multi_select (PGconn *connection,
238 * codes to `enum GNUNET_DB_QueryStatus`. 238 * codes to `enum GNUNET_DB_QueryStatus`.
239 */ 239 */
240enum GNUNET_DB_QueryStatus 240enum GNUNET_DB_QueryStatus
241GNUNET_PQ_eval_prepared_singleton_select (PGconn *connection, 241GNUNET_PQ_eval_prepared_singleton_select(PGconn *connection,
242 const char *statement_name, 242 const char *statement_name,
243 const struct GNUNET_PQ_QueryParam *params, 243 const struct GNUNET_PQ_QueryParam *params,
244 struct GNUNET_PQ_ResultSpec *rs) 244 struct GNUNET_PQ_ResultSpec *rs)
245{ 245{
246 PGresult *result; 246 PGresult *result;
247 enum GNUNET_DB_QueryStatus qs; 247 enum GNUNET_DB_QueryStatus qs;
248 248
249 result = GNUNET_PQ_exec_prepared (connection, 249 result = GNUNET_PQ_exec_prepared(connection,
250 statement_name, 250 statement_name,
251 params); 251 params);
252 qs = GNUNET_PQ_eval_result (connection, 252 qs = GNUNET_PQ_eval_result(connection,
253 statement_name, 253 statement_name,
254 result); 254 result);
255 if (qs < 0) 255 if (qs < 0)
256 { 256 {
257 PQclear (result); 257 PQclear(result);
258 return qs; 258 return qs;
259 } 259 }
260 if (0 == PQntuples (result)) 260 if (0 == PQntuples(result))
261 { 261 {
262 PQclear (result); 262 PQclear(result);
263 return GNUNET_DB_STATUS_SUCCESS_NO_RESULTS; 263 return GNUNET_DB_STATUS_SUCCESS_NO_RESULTS;
264 } 264 }
265 if (1 != PQntuples (result)) 265 if (1 != PQntuples(result))
266 { 266 {
267 /* more than one result, but there must be at most one */ 267 /* more than one result, but there must be at most one */
268 GNUNET_break (0); 268 GNUNET_break(0);
269 PQclear (result); 269 PQclear(result);
270 return GNUNET_DB_STATUS_HARD_ERROR; 270 return GNUNET_DB_STATUS_HARD_ERROR;
271 } 271 }
272 if (GNUNET_OK != 272 if (GNUNET_OK !=
273 GNUNET_PQ_extract_result (result, 273 GNUNET_PQ_extract_result(result,
274 rs, 274 rs,
275 0)) 275 0))
276 { 276 {
277 PQclear (result); 277 PQclear(result);
278 return GNUNET_DB_STATUS_HARD_ERROR; 278 return GNUNET_DB_STATUS_HARD_ERROR;
279 } 279 }
280 PQclear (result); 280 PQclear(result);
281 return GNUNET_DB_STATUS_SUCCESS_ONE_RESULT; 281 return GNUNET_DB_STATUS_SUCCESS_ONE_RESULT;
282} 282}
283 283
diff --git a/src/pq/pq_exec.c b/src/pq/pq_exec.c
index 99eb2b270..6c8002aff 100644
--- a/src/pq/pq_exec.c
+++ b/src/pq/pq_exec.c
@@ -1,22 +1,22 @@
1/* 1/*
2 This file is part of GNUnet 2 This file is part of GNUnet
3 Copyright (C) 2017 GNUnet e.V. 3 Copyright (C) 2017 GNUnet e.V.
4 4
5 GNUnet is free software: you can redistribute it and/or modify it 5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published 6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License, 7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version. 8 or (at your option) any later version.
9 9
10 GNUnet is distributed in the hope that it will be useful, but 10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20/** 20/**
21 * @file pq/pq_exec.c 21 * @file pq/pq_exec.c
22 * @brief functions to execute plain SQL statements (PostGres) 22 * @brief functions to execute plain SQL statements (PostGres)
@@ -34,7 +34,7 @@
34 * @return initialized struct 34 * @return initialized struct
35 */ 35 */
36struct GNUNET_PQ_ExecuteStatement 36struct GNUNET_PQ_ExecuteStatement
37GNUNET_PQ_make_execute (const char *sql) 37GNUNET_PQ_make_execute(const char *sql)
38{ 38{
39 struct GNUNET_PQ_ExecuteStatement es = { 39 struct GNUNET_PQ_ExecuteStatement es = {
40 .sql = sql, 40 .sql = sql,
@@ -53,7 +53,7 @@ GNUNET_PQ_make_execute (const char *sql)
53 * @return initialized struct 53 * @return initialized struct
54 */ 54 */
55struct GNUNET_PQ_ExecuteStatement 55struct GNUNET_PQ_ExecuteStatement
56GNUNET_PQ_make_try_execute (const char *sql) 56GNUNET_PQ_make_try_execute(const char *sql)
57{ 57{
58 struct GNUNET_PQ_ExecuteStatement es = { 58 struct GNUNET_PQ_ExecuteStatement es = {
59 .sql = sql, 59 .sql = sql,
@@ -74,38 +74,38 @@ GNUNET_PQ_make_try_execute (const char *sql)
74 * #GNUNET_SYSERR on error 74 * #GNUNET_SYSERR on error
75 */ 75 */
76int 76int
77GNUNET_PQ_exec_statements (PGconn *connection, 77GNUNET_PQ_exec_statements(PGconn *connection,
78 const struct GNUNET_PQ_ExecuteStatement *es) 78 const struct GNUNET_PQ_ExecuteStatement *es)
79{ 79{
80 for (unsigned int i=0; NULL != es[i].sql; i++) 80 for (unsigned int i = 0; NULL != es[i].sql; i++)
81 {
82 PGresult *result;
83
84 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
85 "Running statement `%s' on %p\n",
86 es[i].sql,
87 connection);
88 result = PQexec (connection,
89 es[i].sql);
90 if ( (GNUNET_NO == es[i].ignore_errors) &&
91 (PGRES_COMMAND_OK != PQresultStatus (result)) )
92 { 81 {
93 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, 82 PGresult *result;
94 "pq", 83
95 "Failed to execute `%s': %s/%s/%s/%s/%s", 84 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
96 es[i].sql, 85 "Running statement `%s' on %p\n",
97 PQresultErrorField (result, 86 es[i].sql,
98 PG_DIAG_MESSAGE_PRIMARY), 87 connection);
99 PQresultErrorField (result, 88 result = PQexec(connection,
100 PG_DIAG_MESSAGE_DETAIL), 89 es[i].sql);
101 PQresultErrorMessage (result), 90 if ((GNUNET_NO == es[i].ignore_errors) &&
102 PQresStatus (PQresultStatus (result)), 91 (PGRES_COMMAND_OK != PQresultStatus(result)))
103 PQerrorMessage (connection)); 92 {
104 PQclear (result); 93 GNUNET_log_from(GNUNET_ERROR_TYPE_ERROR,
105 return GNUNET_SYSERR; 94 "pq",
95 "Failed to execute `%s': %s/%s/%s/%s/%s",
96 es[i].sql,
97 PQresultErrorField(result,
98 PG_DIAG_MESSAGE_PRIMARY),
99 PQresultErrorField(result,
100 PG_DIAG_MESSAGE_DETAIL),
101 PQresultErrorMessage(result),
102 PQresStatus(PQresultStatus(result)),
103 PQerrorMessage(connection));
104 PQclear(result);
105 return GNUNET_SYSERR;
106 }
107 PQclear(result);
106 } 108 }
107 PQclear (result);
108 }
109 return GNUNET_OK; 109 return GNUNET_OK;
110} 110}
111 111
diff --git a/src/pq/pq_prepare.c b/src/pq/pq_prepare.c
index 9a8e4e76f..9899b65bb 100644
--- a/src/pq/pq_prepare.c
+++ b/src/pq/pq_prepare.c
@@ -1,22 +1,22 @@
1/* 1/*
2 This file is part of GNUnet 2 This file is part of GNUnet
3 Copyright (C) 2017 GNUnet e.V. 3 Copyright (C) 2017 GNUnet e.V.
4 4
5 GNUnet is free software: you can redistribute it and/or modify it 5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published 6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License, 7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version. 8 or (at your option) any later version.
9 9
10 GNUnet is distributed in the hope that it will be useful, but 10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20/** 20/**
21 * @file pq/pq_prepare.c 21 * @file pq/pq_prepare.c
22 * @brief functions to connect to libpq (PostGres) 22 * @brief functions to connect to libpq (PostGres)
@@ -36,9 +36,9 @@
36 * @return initialized struct 36 * @return initialized struct
37 */ 37 */
38struct GNUNET_PQ_PreparedStatement 38struct GNUNET_PQ_PreparedStatement
39GNUNET_PQ_make_prepare (const char *name, 39GNUNET_PQ_make_prepare(const char *name,
40 const char *sql, 40 const char *sql,
41 unsigned int num_args) 41 unsigned int num_args)
42{ 42{
43 struct GNUNET_PQ_PreparedStatement ps = { 43 struct GNUNET_PQ_PreparedStatement ps = {
44 .name = name, 44 .name = name,
@@ -60,36 +60,36 @@ GNUNET_PQ_make_prepare (const char *name,
60 * #GNUNET_SYSERR on error 60 * #GNUNET_SYSERR on error
61 */ 61 */
62int 62int
63GNUNET_PQ_prepare_statements (PGconn *connection, 63GNUNET_PQ_prepare_statements(PGconn *connection,
64 const struct GNUNET_PQ_PreparedStatement *ps) 64 const struct GNUNET_PQ_PreparedStatement *ps)
65{ 65{
66 for (unsigned int i=0;NULL != ps[i].name;i++) 66 for (unsigned int i = 0; NULL != ps[i].name; i++)
67 {
68 PGresult *ret;
69
70 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
71 "pq",
72 "Preparing SQL statement `%s' as `%s'\n",
73 ps[i].sql,
74 ps[i].name);
75 ret = PQprepare (connection,
76 ps[i].name,
77 ps[i].sql,
78 ps[i].num_arguments,
79 NULL);
80 if (PGRES_COMMAND_OK != PQresultStatus (ret))
81 { 67 {
82 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 68 PGresult *ret;
83 "pq", 69
84 _("PQprepare (`%s' as `%s') failed with error: %s\n"), 70 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG,
85 ps[i].sql, 71 "pq",
86 ps[i].name, 72 "Preparing SQL statement `%s' as `%s'\n",
87 PQerrorMessage (connection)); 73 ps[i].sql,
88 PQclear (ret); 74 ps[i].name);
89 return GNUNET_SYSERR; 75 ret = PQprepare(connection,
76 ps[i].name,
77 ps[i].sql,
78 ps[i].num_arguments,
79 NULL);
80 if (PGRES_COMMAND_OK != PQresultStatus(ret))
81 {
82 GNUNET_log_from(GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
83 "pq",
84 _("PQprepare (`%s' as `%s') failed with error: %s\n"),
85 ps[i].sql,
86 ps[i].name,
87 PQerrorMessage(connection));
88 PQclear(ret);
89 return GNUNET_SYSERR;
90 }
91 PQclear(ret);
90 } 92 }
91 PQclear (ret);
92 }
93 return GNUNET_OK; 93 return GNUNET_OK;
94} 94}
95 95
diff --git a/src/pq/pq_query_helper.c b/src/pq/pq_query_helper.c
index a6ce3c5d8..c3b86ee3d 100644
--- a/src/pq/pq_query_helper.c
+++ b/src/pq/pq_query_helper.c
@@ -1,22 +1,22 @@
1 /* 1/*
2 This file is part of GNUnet 2 This file is part of GNUnet
3 Copyright (C) 2014, 2015, 2016 GNUnet e.V. 3 Copyright (C) 2014, 2015, 2016 GNUnet e.V.
4 4
5 GNUnet is free software: you can redistribute it and/or modify it 5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published 6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License, 7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version. 8 or (at your option) any later version.
9 9
10 GNUnet is distributed in the hope that it will be useful, but 10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20/** 20/**
21 * @file pq/pq_query_helper.c 21 * @file pq/pq_query_helper.c
22 * @brief functions to initialize parameter arrays 22 * @brief functions to initialize parameter arrays
@@ -42,22 +42,22 @@
42 * @return -1 on error, number of offsets used in @a scratch otherwise 42 * @return -1 on error, number of offsets used in @a scratch otherwise
43 */ 43 */
44static int 44static int
45qconv_fixed (void *cls, 45qconv_fixed(void *cls,
46 const void *data, 46 const void *data,
47 size_t data_len, 47 size_t data_len,
48 void *param_values[], 48 void *param_values[],
49 int param_lengths[], 49 int param_lengths[],
50 int param_formats[], 50 int param_formats[],
51 unsigned int param_length, 51 unsigned int param_length,
52 void *scratch[], 52 void *scratch[],
53 unsigned int scratch_length) 53 unsigned int scratch_length)
54{ 54{
55 (void) scratch; 55 (void)scratch;
56 (void) scratch_length; 56 (void)scratch_length;
57 GNUNET_break (NULL == cls); 57 GNUNET_break(NULL == cls);
58 if (1 != param_length) 58 if (1 != param_length)
59 return -1; 59 return -1;
60 param_values[0] = (void *) data; 60 param_values[0] = (void *)data;
61 param_lengths[0] = data_len; 61 param_lengths[0] = data_len;
62 param_formats[0] = 1; 62 param_formats[0] = 1;
63 return 0; 63 return 0;
@@ -72,11 +72,12 @@ qconv_fixed (void *cls,
72 * @oaran ptr_size number of bytes in @a ptr 72 * @oaran ptr_size number of bytes in @a ptr
73 */ 73 */
74struct GNUNET_PQ_QueryParam 74struct GNUNET_PQ_QueryParam
75GNUNET_PQ_query_param_fixed_size (const void *ptr, 75GNUNET_PQ_query_param_fixed_size(const void *ptr,
76 size_t ptr_size) 76 size_t ptr_size)
77{ 77{
78 struct GNUNET_PQ_QueryParam res = 78 struct GNUNET_PQ_QueryParam res =
79 { &qconv_fixed, NULL, ptr, ptr_size, 1 }; 79 { &qconv_fixed, NULL, ptr, ptr_size, 1 };
80
80 return res; 81 return res;
81} 82}
82 83
@@ -87,9 +88,9 @@ GNUNET_PQ_query_param_fixed_size (const void *ptr,
87 * @param ptr pointer to the string query parameter to pass 88 * @param ptr pointer to the string query parameter to pass
88 */ 89 */
89struct GNUNET_PQ_QueryParam 90struct GNUNET_PQ_QueryParam
90GNUNET_PQ_query_param_string (const char *ptr) 91GNUNET_PQ_query_param_string(const char *ptr)
91{ 92{
92 return GNUNET_PQ_query_param_fixed_size (ptr, strlen (ptr)); 93 return GNUNET_PQ_query_param_fixed_size(ptr, strlen(ptr));
93} 94}
94 95
95 96
@@ -108,29 +109,29 @@ GNUNET_PQ_query_param_string (const char *ptr)
108 * @return -1 on error, number of offsets used in @a scratch otherwise 109 * @return -1 on error, number of offsets used in @a scratch otherwise
109 */ 110 */
110static int 111static int
111qconv_uint16 (void *cls, 112qconv_uint16(void *cls,
112 const void *data, 113 const void *data,
113 size_t data_len, 114 size_t data_len,
114 void *param_values[], 115 void *param_values[],
115 int param_lengths[], 116 int param_lengths[],
116 int param_formats[], 117 int param_formats[],
117 unsigned int param_length, 118 unsigned int param_length,
118 void *scratch[], 119 void *scratch[],
119 unsigned int scratch_length) 120 unsigned int scratch_length)
120{ 121{
121 const uint16_t *u_hbo = data; 122 const uint16_t *u_hbo = data;
122 uint16_t *u_nbo; 123 uint16_t *u_nbo;
123 124
124 (void) scratch; 125 (void)scratch;
125 (void) scratch_length; 126 (void)scratch_length;
126 GNUNET_break (NULL == cls); 127 GNUNET_break(NULL == cls);
127 if (1 != param_length) 128 if (1 != param_length)
128 return -1; 129 return -1;
129 u_nbo = GNUNET_new (uint16_t); 130 u_nbo = GNUNET_new(uint16_t);
130 scratch[0] = u_nbo; 131 scratch[0] = u_nbo;
131 *u_nbo = htons (*u_hbo); 132 *u_nbo = htons(*u_hbo);
132 param_values[0] = (void *) u_nbo; 133 param_values[0] = (void *)u_nbo;
133 param_lengths[0] = sizeof (uint16_t); 134 param_lengths[0] = sizeof(uint16_t);
134 param_formats[0] = 1; 135 param_formats[0] = 1;
135 return 1; 136 return 1;
136} 137}
@@ -142,10 +143,11 @@ qconv_uint16 (void *cls,
142 * @param x pointer to the query parameter to pass 143 * @param x pointer to the query parameter to pass
143 */ 144 */
144struct GNUNET_PQ_QueryParam 145struct GNUNET_PQ_QueryParam
145GNUNET_PQ_query_param_uint16 (const uint16_t *x) 146GNUNET_PQ_query_param_uint16(const uint16_t *x)
146{ 147{
147 struct GNUNET_PQ_QueryParam res = 148 struct GNUNET_PQ_QueryParam res =
148 { &qconv_uint16, NULL, x, sizeof (*x), 1 }; 149 { &qconv_uint16, NULL, x, sizeof(*x), 1 };
150
149 return res; 151 return res;
150} 152}
151 153
@@ -165,29 +167,29 @@ GNUNET_PQ_query_param_uint16 (const uint16_t *x)
165 * @return -1 on error, number of offsets used in @a scratch otherwise 167 * @return -1 on error, number of offsets used in @a scratch otherwise
166 */ 168 */
167static int 169static int
168qconv_uint32 (void *cls, 170qconv_uint32(void *cls,
169 const void *data, 171 const void *data,
170 size_t data_len, 172 size_t data_len,
171 void *param_values[], 173 void *param_values[],
172 int param_lengths[], 174 int param_lengths[],
173 int param_formats[], 175 int param_formats[],
174 unsigned int param_length, 176 unsigned int param_length,
175 void *scratch[], 177 void *scratch[],
176 unsigned int scratch_length) 178 unsigned int scratch_length)
177{ 179{
178 const uint32_t *u_hbo = data; 180 const uint32_t *u_hbo = data;
179 uint32_t *u_nbo; 181 uint32_t *u_nbo;
180 182
181 (void) scratch; 183 (void)scratch;
182 (void) scratch_length; 184 (void)scratch_length;
183 GNUNET_break (NULL == cls); 185 GNUNET_break(NULL == cls);
184 if (1 != param_length) 186 if (1 != param_length)
185 return -1; 187 return -1;
186 u_nbo = GNUNET_new (uint32_t); 188 u_nbo = GNUNET_new(uint32_t);
187 scratch[0] = u_nbo; 189 scratch[0] = u_nbo;
188 *u_nbo = htonl (*u_hbo); 190 *u_nbo = htonl(*u_hbo);
189 param_values[0] = (void *) u_nbo; 191 param_values[0] = (void *)u_nbo;
190 param_lengths[0] = sizeof (uint32_t); 192 param_lengths[0] = sizeof(uint32_t);
191 param_formats[0] = 1; 193 param_formats[0] = 1;
192 return 1; 194 return 1;
193} 195}
@@ -199,10 +201,11 @@ qconv_uint32 (void *cls,
199 * @param x pointer to the query parameter to pass 201 * @param x pointer to the query parameter to pass
200 */ 202 */
201struct GNUNET_PQ_QueryParam 203struct GNUNET_PQ_QueryParam
202GNUNET_PQ_query_param_uint32 (const uint32_t *x) 204GNUNET_PQ_query_param_uint32(const uint32_t *x)
203{ 205{
204 struct GNUNET_PQ_QueryParam res = 206 struct GNUNET_PQ_QueryParam res =
205 { &qconv_uint32, NULL, x, sizeof (*x), 1 }; 207 { &qconv_uint32, NULL, x, sizeof(*x), 1 };
208
206 return res; 209 return res;
207} 210}
208 211
@@ -222,29 +225,29 @@ GNUNET_PQ_query_param_uint32 (const uint32_t *x)
222 * @return -1 on error, number of offsets used in @a scratch otherwise 225 * @return -1 on error, number of offsets used in @a scratch otherwise
223 */ 226 */
224static int 227static int
225qconv_uint64 (void *cls, 228qconv_uint64(void *cls,
226 const void *data, 229 const void *data,
227 size_t data_len, 230 size_t data_len,
228 void *param_values[], 231 void *param_values[],
229 int param_lengths[], 232 int param_lengths[],
230 int param_formats[], 233 int param_formats[],
231 unsigned int param_length, 234 unsigned int param_length,
232 void *scratch[], 235 void *scratch[],
233 unsigned int scratch_length) 236 unsigned int scratch_length)
234{ 237{
235 const uint64_t *u_hbo = data; 238 const uint64_t *u_hbo = data;
236 uint64_t *u_nbo; 239 uint64_t *u_nbo;
237 240
238 (void) scratch; 241 (void)scratch;
239 (void) scratch_length; 242 (void)scratch_length;
240 GNUNET_break (NULL == cls); 243 GNUNET_break(NULL == cls);
241 if (1 != param_length) 244 if (1 != param_length)
242 return -1; 245 return -1;
243 u_nbo = GNUNET_new (uint64_t); 246 u_nbo = GNUNET_new(uint64_t);
244 scratch[0] = u_nbo; 247 scratch[0] = u_nbo;
245 *u_nbo = GNUNET_htonll (*u_hbo); 248 *u_nbo = GNUNET_htonll(*u_hbo);
246 param_values[0] = (void *) u_nbo; 249 param_values[0] = (void *)u_nbo;
247 param_lengths[0] = sizeof (uint64_t); 250 param_lengths[0] = sizeof(uint64_t);
248 param_formats[0] = 1; 251 param_formats[0] = 1;
249 return 1; 252 return 1;
250} 253}
@@ -256,10 +259,11 @@ qconv_uint64 (void *cls,
256 * @param x pointer to the query parameter to pass 259 * @param x pointer to the query parameter to pass
257 */ 260 */
258struct GNUNET_PQ_QueryParam 261struct GNUNET_PQ_QueryParam
259GNUNET_PQ_query_param_uint64 (const uint64_t *x) 262GNUNET_PQ_query_param_uint64(const uint64_t *x)
260{ 263{
261 struct GNUNET_PQ_QueryParam res = 264 struct GNUNET_PQ_QueryParam res =
262 { &qconv_uint64, NULL, x, sizeof (*x), 1 }; 265 { &qconv_uint64, NULL, x, sizeof(*x), 1 };
266
263 return res; 267 return res;
264} 268}
265 269
@@ -279,27 +283,27 @@ GNUNET_PQ_query_param_uint64 (const uint64_t *x)
279 * @return -1 on error, number of offsets used in @a scratch otherwise 283 * @return -1 on error, number of offsets used in @a scratch otherwise
280 */ 284 */
281static int 285static int
282qconv_rsa_public_key (void *cls, 286qconv_rsa_public_key(void *cls,
283 const void *data, 287 const void *data,
284 size_t data_len, 288 size_t data_len,
285 void *param_values[], 289 void *param_values[],
286 int param_lengths[], 290 int param_lengths[],
287 int param_formats[], 291 int param_formats[],
288 unsigned int param_length, 292 unsigned int param_length,
289 void *scratch[], 293 void *scratch[],
290 unsigned int scratch_length) 294 unsigned int scratch_length)
291{ 295{
292 const struct GNUNET_CRYPTO_RsaPublicKey *rsa = data; 296 const struct GNUNET_CRYPTO_RsaPublicKey *rsa = data;
293 char *buf; 297 char *buf;
294 size_t buf_size; 298 size_t buf_size;
295 299
296 GNUNET_break (NULL == cls); 300 GNUNET_break(NULL == cls);
297 if (1 != param_length) 301 if (1 != param_length)
298 return -1; 302 return -1;
299 buf_size = GNUNET_CRYPTO_rsa_public_key_encode (rsa, 303 buf_size = GNUNET_CRYPTO_rsa_public_key_encode(rsa,
300 &buf); 304 &buf);
301 scratch[0] = buf; 305 scratch[0] = buf;
302 param_values[0] = (void *) buf; 306 param_values[0] = (void *)buf;
303 param_lengths[0] = buf_size - 1; /* DB doesn't like the trailing \0 */ 307 param_lengths[0] = buf_size - 1; /* DB doesn't like the trailing \0 */
304 param_formats[0] = 1; 308 param_formats[0] = 1;
305 return 1; 309 return 1;
@@ -314,10 +318,11 @@ qconv_rsa_public_key (void *cls,
314 * @return array entry for the query parameters to use 318 * @return array entry for the query parameters to use
315 */ 319 */
316struct GNUNET_PQ_QueryParam 320struct GNUNET_PQ_QueryParam
317GNUNET_PQ_query_param_rsa_public_key (const struct GNUNET_CRYPTO_RsaPublicKey *x) 321GNUNET_PQ_query_param_rsa_public_key(const struct GNUNET_CRYPTO_RsaPublicKey *x)
318{ 322{
319 struct GNUNET_PQ_QueryParam res = 323 struct GNUNET_PQ_QueryParam res =
320 { &qconv_rsa_public_key, NULL, (x), 0, 1 }; 324 { &qconv_rsa_public_key, NULL, (x), 0, 1 };
325
321 return res; 326 return res;
322} 327}
323 328
@@ -337,27 +342,27 @@ GNUNET_PQ_query_param_rsa_public_key (const struct GNUNET_CRYPTO_RsaPublicKey *x
337 * @return -1 on error, number of offsets used in @a scratch otherwise 342 * @return -1 on error, number of offsets used in @a scratch otherwise
338 */ 343 */
339static int 344static int
340qconv_rsa_signature (void *cls, 345qconv_rsa_signature(void *cls,
341 const void *data, 346 const void *data,
342 size_t data_len, 347 size_t data_len,
343 void *param_values[], 348 void *param_values[],
344 int param_lengths[], 349 int param_lengths[],
345 int param_formats[], 350 int param_formats[],
346 unsigned int param_length, 351 unsigned int param_length,
347 void *scratch[], 352 void *scratch[],
348 unsigned int scratch_length) 353 unsigned int scratch_length)
349{ 354{
350 const struct GNUNET_CRYPTO_RsaSignature *sig = data; 355 const struct GNUNET_CRYPTO_RsaSignature *sig = data;
351 char *buf; 356 char *buf;
352 size_t buf_size; 357 size_t buf_size;
353 358
354 GNUNET_break (NULL == cls); 359 GNUNET_break(NULL == cls);
355 if (1 != param_length) 360 if (1 != param_length)
356 return -1; 361 return -1;
357 buf_size = GNUNET_CRYPTO_rsa_signature_encode (sig, 362 buf_size = GNUNET_CRYPTO_rsa_signature_encode(sig,
358 &buf); 363 &buf);
359 scratch[0] = buf; 364 scratch[0] = buf;
360 param_values[0] = (void *) buf; 365 param_values[0] = (void *)buf;
361 param_lengths[0] = buf_size - 1; /* DB doesn't like the trailing \0 */ 366 param_lengths[0] = buf_size - 1; /* DB doesn't like the trailing \0 */
362 param_formats[0] = 1; 367 param_formats[0] = 1;
363 return 1; 368 return 1;
@@ -372,10 +377,11 @@ qconv_rsa_signature (void *cls,
372 * @return array entry for the query parameters to use 377 * @return array entry for the query parameters to use
373 */ 378 */
374struct GNUNET_PQ_QueryParam 379struct GNUNET_PQ_QueryParam
375GNUNET_PQ_query_param_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *x) 380GNUNET_PQ_query_param_rsa_signature(const struct GNUNET_CRYPTO_RsaSignature *x)
376{ 381{
377 struct GNUNET_PQ_QueryParam res = 382 struct GNUNET_PQ_QueryParam res =
378 { &qconv_rsa_signature, NULL, (x), 0, 1 }; 383 { &qconv_rsa_signature, NULL, (x), 0, 1 };
384
379 return res; 385 return res;
380} 386}
381 387
@@ -395,31 +401,31 @@ GNUNET_PQ_query_param_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *x)
395 * @return -1 on error, number of offsets used in @a scratch otherwise 401 * @return -1 on error, number of offsets used in @a scratch otherwise
396 */ 402 */
397static int 403static int
398qconv_abs_time (void *cls, 404qconv_abs_time(void *cls,
399 const void *data, 405 const void *data,
400 size_t data_len, 406 size_t data_len,
401 void *param_values[], 407 void *param_values[],
402 int param_lengths[], 408 int param_lengths[],
403 int param_formats[], 409 int param_formats[],
404 unsigned int param_length, 410 unsigned int param_length,
405 void *scratch[], 411 void *scratch[],
406 unsigned int scratch_length) 412 unsigned int scratch_length)
407{ 413{
408 const struct GNUNET_TIME_Absolute *u = data; 414 const struct GNUNET_TIME_Absolute *u = data;
409 struct GNUNET_TIME_Absolute abs; 415 struct GNUNET_TIME_Absolute abs;
410 uint64_t *u_nbo; 416 uint64_t *u_nbo;
411 417
412 GNUNET_break (NULL == cls); 418 GNUNET_break(NULL == cls);
413 if (1 != param_length) 419 if (1 != param_length)
414 return -1; 420 return -1;
415 abs = *u; 421 abs = *u;
416 if (abs.abs_value_us > INT64_MAX) 422 if (abs.abs_value_us > INT64_MAX)
417 abs.abs_value_us = INT64_MAX; 423 abs.abs_value_us = INT64_MAX;
418 u_nbo = GNUNET_new (uint64_t); 424 u_nbo = GNUNET_new(uint64_t);
419 scratch[0] = u_nbo; 425 scratch[0] = u_nbo;
420 *u_nbo = GNUNET_htonll (abs.abs_value_us); 426 *u_nbo = GNUNET_htonll(abs.abs_value_us);
421 param_values[0] = (void *) u_nbo; 427 param_values[0] = (void *)u_nbo;
422 param_lengths[0] = sizeof (uint64_t); 428 param_lengths[0] = sizeof(uint64_t);
423 param_formats[0] = 1; 429 param_formats[0] = 1;
424 return 1; 430 return 1;
425} 431}
@@ -433,10 +439,10 @@ qconv_abs_time (void *cls,
433 * @return array entry for the query parameters to use 439 * @return array entry for the query parameters to use
434 */ 440 */
435struct GNUNET_PQ_QueryParam 441struct GNUNET_PQ_QueryParam
436GNUNET_PQ_query_param_absolute_time (const struct GNUNET_TIME_Absolute *x) 442GNUNET_PQ_query_param_absolute_time(const struct GNUNET_TIME_Absolute *x)
437{ 443{
438 struct GNUNET_PQ_QueryParam res = 444 struct GNUNET_PQ_QueryParam res =
439 { &qconv_abs_time, NULL, x, sizeof (*x), 1 }; 445 { &qconv_abs_time, NULL, x, sizeof(*x), 1 };
440 446
441 return res; 447 return res;
442} 448}
@@ -451,7 +457,7 @@ GNUNET_PQ_query_param_absolute_time (const struct GNUNET_TIME_Absolute *x)
451struct GNUNET_PQ_QueryParam 457struct GNUNET_PQ_QueryParam
452GNUNET_PQ_query_param_absolute_time_nbo(const struct GNUNET_TIME_AbsoluteNBO *x) 458GNUNET_PQ_query_param_absolute_time_nbo(const struct GNUNET_TIME_AbsoluteNBO *x)
453{ 459{
454 return GNUNET_PQ_query_param_auto_from_type (&x->abs_value_us__); 460 return GNUNET_PQ_query_param_auto_from_type(&x->abs_value_us__);
455} 461}
456 462
457 463
diff --git a/src/pq/pq_result_helper.c b/src/pq/pq_result_helper.c
index 336ca8c42..89c0207f7 100644
--- a/src/pq/pq_result_helper.c
+++ b/src/pq/pq_result_helper.c
@@ -1,22 +1,22 @@
1 /* 1/*
2 This file is part of GNUnet 2 This file is part of GNUnet
3 Copyright (C) 2014, 2015, 2016 GNUnet e.V. 3 Copyright (C) 2014, 2015, 2016 GNUnet e.V.
4 4
5 GNUnet is free software: you can redistribute it and/or modify it 5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published 6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License, 7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version. 8 or (at your option) any later version.
9 9
10 GNUnet is distributed in the hope that it will be useful, but 10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20/** 20/**
21 * @file pq/pq_result_helper.c 21 * @file pq/pq_result_helper.c
22 * @brief functions to extract result values 22 * @brief functions to extract result values
@@ -35,17 +35,17 @@
35 * @param rd result data to clean up 35 * @param rd result data to clean up
36 */ 36 */
37static void 37static void
38clean_varsize_blob (void *cls, 38clean_varsize_blob(void *cls,
39 void *rd) 39 void *rd)
40{ 40{
41 void **dst = rd; 41 void **dst = rd;
42 42
43 (void) cls; 43 (void)cls;
44 if (NULL != *dst) 44 if (NULL != *dst)
45 { 45 {
46 GNUNET_free (*dst); 46 GNUNET_free(*dst);
47 *dst = NULL; 47 *dst = NULL;
48 } 48 }
49} 49}
50 50
51 51
@@ -63,51 +63,51 @@ clean_varsize_blob (void *cls,
63 * #GNUNET_SYSERR if a result was invalid (non-existing field) 63 * #GNUNET_SYSERR if a result was invalid (non-existing field)
64 */ 64 */
65static int 65static int
66extract_varsize_blob (void *cls, 66extract_varsize_blob(void *cls,
67 PGresult *result, 67 PGresult *result,
68 int row, 68 int row,
69 const char *fname, 69 const char *fname,
70 size_t *dst_size, 70 size_t *dst_size,
71 void *dst) 71 void *dst)
72{ 72{
73 size_t len; 73 size_t len;
74 const char *res; 74 const char *res;
75 void *idst; 75 void *idst;
76 int fnum; 76 int fnum;
77 77
78 (void) cls; 78 (void)cls;
79 *dst_size = 0; 79 *dst_size = 0;
80 *((void **) dst) = NULL; 80 *((void **)dst) = NULL;
81 81
82 fnum = PQfnumber (result, 82 fnum = PQfnumber(result,
83 fname); 83 fname);
84 if (fnum < 0) 84 if (fnum < 0)
85 { 85 {
86 GNUNET_break (0); 86 GNUNET_break(0);
87 return GNUNET_SYSERR; 87 return GNUNET_SYSERR;
88 } 88 }
89 if (PQgetisnull (result, 89 if (PQgetisnull(result,
90 row, 90 row,
91 fnum)) 91 fnum))
92 { 92 {
93 /* Let's allow this for varsize */ 93 /* Let's allow this for varsize */
94 return GNUNET_OK; 94 return GNUNET_OK;
95 } 95 }
96 /* if a field is null, continue but 96 /* if a field is null, continue but
97 * remember that we now return a different result */ 97 * remember that we now return a different result */
98 len = PQgetlength (result, 98 len = PQgetlength(result,
99 row, 99 row,
100 fnum); 100 fnum);
101 res = PQgetvalue (result, 101 res = PQgetvalue(result,
102 row, 102 row,
103 fnum); 103 fnum);
104 GNUNET_assert (NULL != res); 104 GNUNET_assert(NULL != res);
105 *dst_size = len; 105 *dst_size = len;
106 idst = GNUNET_malloc (len); 106 idst = GNUNET_malloc(len);
107 *((void **) dst) = idst; 107 *((void **)dst) = idst;
108 GNUNET_memcpy (idst, 108 GNUNET_memcpy(idst,
109 res, 109 res,
110 len); 110 len);
111 return GNUNET_OK; 111 return GNUNET_OK;
112} 112}
113 113
@@ -121,14 +121,15 @@ extract_varsize_blob (void *cls,
121 * @return array entry for the result specification to use 121 * @return array entry for the result specification to use
122 */ 122 */
123struct GNUNET_PQ_ResultSpec 123struct GNUNET_PQ_ResultSpec
124GNUNET_PQ_result_spec_variable_size (const char *name, 124GNUNET_PQ_result_spec_variable_size(const char *name,
125 void **dst, 125 void **dst,
126 size_t *sptr) 126 size_t *sptr)
127{ 127{
128 struct GNUNET_PQ_ResultSpec res = 128 struct GNUNET_PQ_ResultSpec res =
129 { &extract_varsize_blob, 129 { &extract_varsize_blob,
130 &clean_varsize_blob, NULL, 130 &clean_varsize_blob, NULL,
131 (void *) (dst), 0, name, sptr }; 131 (void *)(dst), 0, name, sptr };
132
132 return res; 133 return res;
133} 134}
134 135
@@ -147,50 +148,50 @@ GNUNET_PQ_result_spec_variable_size (const char *name,
147 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) 148 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
148 */ 149 */
149static int 150static int
150extract_fixed_blob (void *cls, 151extract_fixed_blob(void *cls,
151 PGresult *result, 152 PGresult *result,
152 int row, 153 int row,
153 const char *fname, 154 const char *fname,
154 size_t *dst_size, 155 size_t *dst_size,
155 void *dst) 156 void *dst)
156{ 157{
157 size_t len; 158 size_t len;
158 const char *res; 159 const char *res;
159 int fnum; 160 int fnum;
160 161
161 (void) cls; 162 (void)cls;
162 fnum = PQfnumber (result, 163 fnum = PQfnumber(result,
163 fname); 164 fname);
164 if (fnum < 0) 165 if (fnum < 0)
165 { 166 {
166 GNUNET_break (0); 167 GNUNET_break(0);
167 return GNUNET_SYSERR; 168 return GNUNET_SYSERR;
168 } 169 }
169 if (PQgetisnull (result, 170 if (PQgetisnull(result,
170 row, 171 row,
171 fnum)) 172 fnum))
172 { 173 {
173 GNUNET_break (0); 174 GNUNET_break(0);
174 return GNUNET_SYSERR; 175 return GNUNET_SYSERR;
175 } 176 }
176 177
177 /* if a field is null, continue but 178 /* if a field is null, continue but
178 * remember that we now return a different result */ 179 * remember that we now return a different result */
179 len = PQgetlength (result, 180 len = PQgetlength(result,
180 row, 181 row,
181 fnum); 182 fnum);
182 if (*dst_size != len) 183 if (*dst_size != len)
183 { 184 {
184 GNUNET_break (0); 185 GNUNET_break(0);
185 return GNUNET_SYSERR; 186 return GNUNET_SYSERR;
186 } 187 }
187 res = PQgetvalue (result, 188 res = PQgetvalue(result,
188 row, 189 row,
189 fnum); 190 fnum);
190 GNUNET_assert (NULL != res); 191 GNUNET_assert(NULL != res);
191 GNUNET_memcpy (dst, 192 GNUNET_memcpy(dst,
192 res, 193 res,
193 len); 194 len);
194 return GNUNET_OK; 195 return GNUNET_OK;
195} 196}
196 197
@@ -204,14 +205,15 @@ extract_fixed_blob (void *cls,
204 * @return array entry for the result specification to use 205 * @return array entry for the result specification to use
205 */ 206 */
206struct GNUNET_PQ_ResultSpec 207struct GNUNET_PQ_ResultSpec
207GNUNET_PQ_result_spec_fixed_size (const char *name, 208GNUNET_PQ_result_spec_fixed_size(const char *name,
208 void *dst, 209 void *dst,
209 size_t dst_size) 210 size_t dst_size)
210{ 211{
211 struct GNUNET_PQ_ResultSpec res = 212 struct GNUNET_PQ_ResultSpec res =
212 { &extract_fixed_blob, 213 { &extract_fixed_blob,
213 NULL, NULL, 214 NULL, NULL,
214 (dst), dst_size, name, NULL }; 215 (dst), dst_size, name, NULL };
216
215 return res; 217 return res;
216} 218}
217 219
@@ -230,49 +232,49 @@ GNUNET_PQ_result_spec_fixed_size (const char *name,
230 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) 232 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
231 */ 233 */
232static int 234static int
233extract_rsa_public_key (void *cls, 235extract_rsa_public_key(void *cls,
234 PGresult *result, 236 PGresult *result,
235 int row, 237 int row,
236 const char *fname, 238 const char *fname,
237 size_t *dst_size, 239 size_t *dst_size,
238 void *dst) 240 void *dst)
239{ 241{
240 struct GNUNET_CRYPTO_RsaPublicKey **pk = dst; 242 struct GNUNET_CRYPTO_RsaPublicKey **pk = dst;
241 size_t len; 243 size_t len;
242 const char *res; 244 const char *res;
243 int fnum; 245 int fnum;
244 246
245 (void) cls; 247 (void)cls;
246 *pk = NULL; 248 *pk = NULL;
247 fnum = PQfnumber (result, 249 fnum = PQfnumber(result,
248 fname); 250 fname);
249 if (fnum < 0) 251 if (fnum < 0)
250 { 252 {
251 GNUNET_break (0); 253 GNUNET_break(0);
252 return GNUNET_SYSERR; 254 return GNUNET_SYSERR;
253 } 255 }
254 if (PQgetisnull (result, 256 if (PQgetisnull(result,
255 row, 257 row,
256 fnum)) 258 fnum))
257 { 259 {
258 GNUNET_break (0); 260 GNUNET_break(0);
259 return GNUNET_SYSERR; 261 return GNUNET_SYSERR;
260 } 262 }
261 /* if a field is null, continue but 263 /* if a field is null, continue but
262 * remember that we now return a different result */ 264 * remember that we now return a different result */
263 len = PQgetlength (result, 265 len = PQgetlength(result,
264 row, 266 row,
265 fnum); 267 fnum);
266 res = PQgetvalue (result, 268 res = PQgetvalue(result,
267 row, 269 row,
268 fnum); 270 fnum);
269 *pk = GNUNET_CRYPTO_rsa_public_key_decode (res, 271 *pk = GNUNET_CRYPTO_rsa_public_key_decode(res,
270 len); 272 len);
271 if (NULL == *pk) 273 if (NULL == *pk)
272 { 274 {
273 GNUNET_break (0); 275 GNUNET_break(0);
274 return GNUNET_SYSERR; 276 return GNUNET_SYSERR;
275 } 277 }
276 return GNUNET_OK; 278 return GNUNET_OK;
277} 279}
278 280
@@ -285,17 +287,17 @@ extract_rsa_public_key (void *cls,
285 * @param rd result data to clean up 287 * @param rd result data to clean up
286 */ 288 */
287static void 289static void
288clean_rsa_public_key (void *cls, 290clean_rsa_public_key(void *cls,
289 void *rd) 291 void *rd)
290{ 292{
291 struct GNUNET_CRYPTO_RsaPublicKey **pk = rd; 293 struct GNUNET_CRYPTO_RsaPublicKey **pk = rd;
292 294
293 (void) cls; 295 (void)cls;
294 if (NULL != *pk) 296 if (NULL != *pk)
295 { 297 {
296 GNUNET_CRYPTO_rsa_public_key_free (*pk); 298 GNUNET_CRYPTO_rsa_public_key_free(*pk);
297 *pk = NULL; 299 *pk = NULL;
298 } 300 }
299} 301}
300 302
301 303
@@ -307,14 +309,15 @@ clean_rsa_public_key (void *cls,
307 * @return array entry for the result specification to use 309 * @return array entry for the result specification to use
308 */ 310 */
309struct GNUNET_PQ_ResultSpec 311struct GNUNET_PQ_ResultSpec
310GNUNET_PQ_result_spec_rsa_public_key (const char *name, 312GNUNET_PQ_result_spec_rsa_public_key(const char *name,
311 struct GNUNET_CRYPTO_RsaPublicKey **rsa) 313 struct GNUNET_CRYPTO_RsaPublicKey **rsa)
312{ 314{
313 struct GNUNET_PQ_ResultSpec res = 315 struct GNUNET_PQ_ResultSpec res =
314 { &extract_rsa_public_key, 316 { &extract_rsa_public_key,
315 &clean_rsa_public_key, 317 &clean_rsa_public_key,
316 NULL, 318 NULL,
317 (void *) rsa, 0, name, NULL }; 319 (void *)rsa, 0, name, NULL };
320
318 return res; 321 return res;
319} 322}
320 323
@@ -333,49 +336,49 @@ GNUNET_PQ_result_spec_rsa_public_key (const char *name,
333 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) 336 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
334 */ 337 */
335static int 338static int
336extract_rsa_signature (void *cls, 339extract_rsa_signature(void *cls,
337 PGresult *result, 340 PGresult *result,
338 int row, 341 int row,
339 const char *fname, 342 const char *fname,
340 size_t *dst_size, 343 size_t *dst_size,
341 void *dst) 344 void *dst)
342{ 345{
343 struct GNUNET_CRYPTO_RsaSignature **sig = dst; 346 struct GNUNET_CRYPTO_RsaSignature **sig = dst;
344 size_t len; 347 size_t len;
345 const char *res; 348 const char *res;
346 int fnum; 349 int fnum;
347 350
348 (void) cls; 351 (void)cls;
349 *sig = NULL; 352 *sig = NULL;
350 fnum = PQfnumber (result, 353 fnum = PQfnumber(result,
351 fname); 354 fname);
352 if (fnum < 0) 355 if (fnum < 0)
353 { 356 {
354 GNUNET_break (0); 357 GNUNET_break(0);
355 return GNUNET_SYSERR; 358 return GNUNET_SYSERR;
356 } 359 }
357 if (PQgetisnull (result, 360 if (PQgetisnull(result,
358 row, 361 row,
359 fnum)) 362 fnum))
360 { 363 {
361 GNUNET_break (0); 364 GNUNET_break(0);
362 return GNUNET_SYSERR; 365 return GNUNET_SYSERR;
363 } 366 }
364 /* if a field is null, continue but 367 /* if a field is null, continue but
365 * remember that we now return a different result */ 368 * remember that we now return a different result */
366 len = PQgetlength (result, 369 len = PQgetlength(result,
367 row, 370 row,
368 fnum); 371 fnum);
369 res = PQgetvalue (result, 372 res = PQgetvalue(result,
370 row, 373 row,
371 fnum); 374 fnum);
372 *sig = GNUNET_CRYPTO_rsa_signature_decode (res, 375 *sig = GNUNET_CRYPTO_rsa_signature_decode(res,
373 len); 376 len);
374 if (NULL == *sig) 377 if (NULL == *sig)
375 { 378 {
376 GNUNET_break (0); 379 GNUNET_break(0);
377 return GNUNET_SYSERR; 380 return GNUNET_SYSERR;
378 } 381 }
379 return GNUNET_OK; 382 return GNUNET_OK;
380} 383}
381 384
@@ -388,17 +391,17 @@ extract_rsa_signature (void *cls,
388 * @param rd result data to clean up 391 * @param rd result data to clean up
389 */ 392 */
390static void 393static void
391clean_rsa_signature (void *cls, 394clean_rsa_signature(void *cls,
392 void *rd) 395 void *rd)
393{ 396{
394 struct GNUNET_CRYPTO_RsaSignature **sig = rd; 397 struct GNUNET_CRYPTO_RsaSignature **sig = rd;
395 398
396 (void) cls; 399 (void)cls;
397 if (NULL != *sig) 400 if (NULL != *sig)
398 { 401 {
399 GNUNET_CRYPTO_rsa_signature_free (*sig); 402 GNUNET_CRYPTO_rsa_signature_free(*sig);
400 *sig = NULL; 403 *sig = NULL;
401 } 404 }
402} 405}
403 406
404 407
@@ -410,14 +413,15 @@ clean_rsa_signature (void *cls,
410 * @return array entry for the result specification to use 413 * @return array entry for the result specification to use
411 */ 414 */
412struct GNUNET_PQ_ResultSpec 415struct GNUNET_PQ_ResultSpec
413GNUNET_PQ_result_spec_rsa_signature (const char *name, 416GNUNET_PQ_result_spec_rsa_signature(const char *name,
414 struct GNUNET_CRYPTO_RsaSignature **sig) 417 struct GNUNET_CRYPTO_RsaSignature **sig)
415{ 418{
416 struct GNUNET_PQ_ResultSpec res = 419 struct GNUNET_PQ_ResultSpec res =
417 { &extract_rsa_signature, 420 { &extract_rsa_signature,
418 &clean_rsa_signature, 421 &clean_rsa_signature,
419 NULL, 422 NULL,
420 (void *) sig, 0, (name), NULL }; 423 (void *)sig, 0, (name), NULL };
424
421 return res; 425 return res;
422} 426}
423 427
@@ -436,49 +440,49 @@ GNUNET_PQ_result_spec_rsa_signature (const char *name,
436 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) 440 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
437 */ 441 */
438static int 442static int
439extract_string (void *cls, 443extract_string(void *cls,
440 PGresult *result, 444 PGresult *result,
441 int row, 445 int row,
442 const char *fname, 446 const char *fname,
443 size_t *dst_size, 447 size_t *dst_size,
444 void *dst) 448 void *dst)
445{ 449{
446 char **str = dst; 450 char **str = dst;
447 size_t len; 451 size_t len;
448 const char *res; 452 const char *res;
449 int fnum; 453 int fnum;
450 454
451 (void) cls; 455 (void)cls;
452 *str = NULL; 456 *str = NULL;
453 fnum = PQfnumber (result, 457 fnum = PQfnumber(result,
454 fname); 458 fname);
455 if (fnum < 0) 459 if (fnum < 0)
456 { 460 {
457 GNUNET_break (0); 461 GNUNET_break(0);
458 return GNUNET_SYSERR; 462 return GNUNET_SYSERR;
459 } 463 }
460 if (PQgetisnull (result, 464 if (PQgetisnull(result,
461 row, 465 row,
462 fnum)) 466 fnum))
463 { 467 {
464 GNUNET_break (0); 468 GNUNET_break(0);
465 return GNUNET_SYSERR; 469 return GNUNET_SYSERR;
466 } 470 }
467 /* if a field is null, continue but 471 /* if a field is null, continue but
468 * remember that we now return a different result */ 472 * remember that we now return a different result */
469 len = PQgetlength (result, 473 len = PQgetlength(result,
470 row, 474 row,
471 fnum); 475 fnum);
472 res = PQgetvalue (result, 476 res = PQgetvalue(result,
473 row, 477 row,
474 fnum); 478 fnum);
475 *str = GNUNET_strndup (res, 479 *str = GNUNET_strndup(res,
476 len); 480 len);
477 if (NULL == *str) 481 if (NULL == *str)
478 { 482 {
479 GNUNET_break (0); 483 GNUNET_break(0);
480 return GNUNET_SYSERR; 484 return GNUNET_SYSERR;
481 } 485 }
482 return GNUNET_OK; 486 return GNUNET_OK;
483} 487}
484 488
@@ -491,17 +495,17 @@ extract_string (void *cls,
491 * @param rd result data to clean up 495 * @param rd result data to clean up
492 */ 496 */
493static void 497static void
494clean_string (void *cls, 498clean_string(void *cls,
495 void *rd) 499 void *rd)
496{ 500{
497 char **str = rd; 501 char **str = rd;
498 502
499 (void) cls; 503 (void)cls;
500 if (NULL != *str) 504 if (NULL != *str)
501 { 505 {
502 GNUNET_free (*str); 506 GNUNET_free(*str);
503 *str = NULL; 507 *str = NULL;
504 } 508 }
505} 509}
506 510
507 511
@@ -513,14 +517,15 @@ clean_string (void *cls,
513 * @return array entry for the result specification to use 517 * @return array entry for the result specification to use
514 */ 518 */
515struct GNUNET_PQ_ResultSpec 519struct GNUNET_PQ_ResultSpec
516GNUNET_PQ_result_spec_string (const char *name, 520GNUNET_PQ_result_spec_string(const char *name,
517 char **dst) 521 char **dst)
518{ 522{
519 struct GNUNET_PQ_ResultSpec res = 523 struct GNUNET_PQ_ResultSpec res =
520 { &extract_string, 524 { &extract_string,
521 &clean_string, 525 &clean_string,
522 NULL, 526 NULL,
523 (void *) dst, 0, (name), NULL }; 527 (void *)dst, 0, (name), NULL };
528
524 return res; 529 return res;
525} 530}
526 531
@@ -539,53 +544,53 @@ GNUNET_PQ_result_spec_string (const char *name,
539 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) 544 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
540 */ 545 */
541static int 546static int
542extract_abs_time (void *cls, 547extract_abs_time(void *cls,
543 PGresult *result, 548 PGresult *result,
544 int row, 549 int row,
545 const char *fname, 550 const char *fname,
546 size_t *dst_size, 551 size_t *dst_size,
547 void *dst) 552 void *dst)
548{ 553{
549 struct GNUNET_TIME_Absolute *udst = dst; 554 struct GNUNET_TIME_Absolute *udst = dst;
550 const int64_t *res; 555 const int64_t *res;
551 int fnum; 556 int fnum;
552 557
553 (void) cls; 558 (void)cls;
554 fnum = PQfnumber (result, 559 fnum = PQfnumber(result,
555 fname); 560 fname);
556 if (fnum < 0) 561 if (fnum < 0)
557 { 562 {
558 GNUNET_break (0); 563 GNUNET_break(0);
559 return GNUNET_SYSERR; 564 return GNUNET_SYSERR;
560 } 565 }
561 if (PQgetisnull (result, 566 if (PQgetisnull(result,
562 row, 567 row,
563 fnum)) 568 fnum))
564 { 569 {
565 GNUNET_break (0); 570 GNUNET_break(0);
566 return GNUNET_SYSERR; 571 return GNUNET_SYSERR;
567 } 572 }
568 GNUNET_assert (NULL != dst); 573 GNUNET_assert(NULL != dst);
569 if (sizeof (struct GNUNET_TIME_Absolute) != *dst_size) 574 if (sizeof(struct GNUNET_TIME_Absolute) != *dst_size)
570 { 575 {
571 GNUNET_break (0); 576 GNUNET_break(0);
572 return GNUNET_SYSERR; 577 return GNUNET_SYSERR;
573 } 578 }
574 if (sizeof (int64_t) != 579 if (sizeof(int64_t) !=
575 PQgetlength (result, 580 PQgetlength(result,
576 row, 581 row,
577 fnum)) 582 fnum))
578 { 583 {
579 GNUNET_break (0); 584 GNUNET_break(0);
580 return GNUNET_SYSERR; 585 return GNUNET_SYSERR;
581 } 586 }
582 res = (int64_t *) PQgetvalue (result, 587 res = (int64_t *)PQgetvalue(result,
583 row, 588 row,
584 fnum); 589 fnum);
585 if (INT64_MAX == *res) 590 if (INT64_MAX == *res)
586 *udst = GNUNET_TIME_UNIT_FOREVER_ABS; 591 *udst = GNUNET_TIME_UNIT_FOREVER_ABS;
587 else 592 else
588 udst->abs_value_us = GNUNET_ntohll ((uint64_t) *res); 593 udst->abs_value_us = GNUNET_ntohll((uint64_t)*res);
589 return GNUNET_OK; 594 return GNUNET_OK;
590} 595}
591 596
@@ -598,14 +603,15 @@ extract_abs_time (void *cls,
598 * @return array entry for the result specification to use 603 * @return array entry for the result specification to use
599 */ 604 */
600struct GNUNET_PQ_ResultSpec 605struct GNUNET_PQ_ResultSpec
601GNUNET_PQ_result_spec_absolute_time (const char *name, 606GNUNET_PQ_result_spec_absolute_time(const char *name,
602 struct GNUNET_TIME_Absolute *at) 607 struct GNUNET_TIME_Absolute *at)
603{ 608{
604 struct GNUNET_PQ_ResultSpec res = 609 struct GNUNET_PQ_ResultSpec res =
605 { &extract_abs_time, 610 { &extract_abs_time,
606 NULL, 611 NULL,
607 NULL, 612 NULL,
608 (void *) at, sizeof (*at), (name), NULL }; 613 (void *)at, sizeof(*at), (name), NULL };
614
609 return res; 615 return res;
610} 616}
611 617
@@ -618,11 +624,12 @@ GNUNET_PQ_result_spec_absolute_time (const char *name,
618 * @return array entry for the result specification to use 624 * @return array entry for the result specification to use
619 */ 625 */
620struct GNUNET_PQ_ResultSpec 626struct GNUNET_PQ_ResultSpec
621GNUNET_PQ_result_spec_absolute_time_nbo (const char *name, 627GNUNET_PQ_result_spec_absolute_time_nbo(const char *name,
622 struct GNUNET_TIME_AbsoluteNBO *at) 628 struct GNUNET_TIME_AbsoluteNBO *at)
623{ 629{
624 struct GNUNET_PQ_ResultSpec res = 630 struct GNUNET_PQ_ResultSpec res =
625 GNUNET_PQ_result_spec_auto_from_type(name, &at->abs_value_us__); 631 GNUNET_PQ_result_spec_auto_from_type(name, &at->abs_value_us__);
632
626 return res; 633 return res;
627} 634}
628 635
@@ -641,50 +648,50 @@ GNUNET_PQ_result_spec_absolute_time_nbo (const char *name,
641 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) 648 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
642 */ 649 */
643static int 650static int
644extract_uint16 (void *cls, 651extract_uint16(void *cls,
645 PGresult *result, 652 PGresult *result,
646 int row, 653 int row,
647 const char *fname, 654 const char *fname,
648 size_t *dst_size, 655 size_t *dst_size,
649 void *dst) 656 void *dst)
650{ 657{
651 uint16_t *udst = dst; 658 uint16_t *udst = dst;
652 const uint16_t *res; 659 const uint16_t *res;
653 int fnum; 660 int fnum;
654 661
655 (void) cls; 662 (void)cls;
656 fnum = PQfnumber (result, 663 fnum = PQfnumber(result,
657 fname); 664 fname);
658 if (fnum < 0) 665 if (fnum < 0)
659 { 666 {
660 GNUNET_break (0); 667 GNUNET_break(0);
661 return GNUNET_SYSERR; 668 return GNUNET_SYSERR;
662 } 669 }
663 if (PQgetisnull (result, 670 if (PQgetisnull(result,
664 row, 671 row,
665 fnum)) 672 fnum))
666 { 673 {
667 GNUNET_break (0); 674 GNUNET_break(0);
668 return GNUNET_SYSERR; 675 return GNUNET_SYSERR;
669 } 676 }
670 GNUNET_assert (NULL != dst); 677 GNUNET_assert(NULL != dst);
671 if (sizeof (uint16_t) != *dst_size) 678 if (sizeof(uint16_t) != *dst_size)
672 { 679 {
673 GNUNET_break (0); 680 GNUNET_break(0);
674 return GNUNET_SYSERR; 681 return GNUNET_SYSERR;
675 } 682 }
676 if (sizeof (uint16_t) != 683 if (sizeof(uint16_t) !=
677 PQgetlength (result, 684 PQgetlength(result,
678 row, 685 row,
679 fnum)) 686 fnum))
680 { 687 {
681 GNUNET_break (0); 688 GNUNET_break(0);
682 return GNUNET_SYSERR; 689 return GNUNET_SYSERR;
683 } 690 }
684 res = (uint16_t *) PQgetvalue (result, 691 res = (uint16_t *)PQgetvalue(result,
685 row, 692 row,
686 fnum); 693 fnum);
687 *udst = ntohs (*res); 694 *udst = ntohs(*res);
688 return GNUNET_OK; 695 return GNUNET_OK;
689} 696}
690 697
@@ -697,14 +704,15 @@ extract_uint16 (void *cls,
697 * @return array entry for the result specification to use 704 * @return array entry for the result specification to use
698 */ 705 */
699struct GNUNET_PQ_ResultSpec 706struct GNUNET_PQ_ResultSpec
700GNUNET_PQ_result_spec_uint16 (const char *name, 707GNUNET_PQ_result_spec_uint16(const char *name,
701 uint16_t *u16) 708 uint16_t *u16)
702{ 709{
703 struct GNUNET_PQ_ResultSpec res = 710 struct GNUNET_PQ_ResultSpec res =
704 { &extract_uint16, 711 { &extract_uint16,
705 NULL, 712 NULL,
706 NULL, 713 NULL,
707 (void *) u16, sizeof (*u16), (name), NULL }; 714 (void *)u16, sizeof(*u16), (name), NULL };
715
708 return res; 716 return res;
709} 717}
710 718
@@ -723,50 +731,50 @@ GNUNET_PQ_result_spec_uint16 (const char *name,
723 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) 731 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
724 */ 732 */
725static int 733static int
726extract_uint32 (void *cls, 734extract_uint32(void *cls,
727 PGresult *result, 735 PGresult *result,
728 int row, 736 int row,
729 const char *fname, 737 const char *fname,
730 size_t *dst_size, 738 size_t *dst_size,
731 void *dst) 739 void *dst)
732{ 740{
733 uint32_t *udst = dst; 741 uint32_t *udst = dst;
734 const uint32_t *res; 742 const uint32_t *res;
735 int fnum; 743 int fnum;
736 744
737 (void) cls; 745 (void)cls;
738 fnum = PQfnumber (result, 746 fnum = PQfnumber(result,
739 fname); 747 fname);
740 if (fnum < 0) 748 if (fnum < 0)
741 { 749 {
742 GNUNET_break (0); 750 GNUNET_break(0);
743 return GNUNET_SYSERR; 751 return GNUNET_SYSERR;
744 } 752 }
745 if (PQgetisnull (result, 753 if (PQgetisnull(result,
746 row, 754 row,
747 fnum)) 755 fnum))
748 { 756 {
749 GNUNET_break (0); 757 GNUNET_break(0);
750 return GNUNET_SYSERR; 758 return GNUNET_SYSERR;
751 } 759 }
752 GNUNET_assert (NULL != dst); 760 GNUNET_assert(NULL != dst);
753 if (sizeof (uint32_t) != *dst_size) 761 if (sizeof(uint32_t) != *dst_size)
754 { 762 {
755 GNUNET_break (0); 763 GNUNET_break(0);
756 return GNUNET_SYSERR; 764 return GNUNET_SYSERR;
757 } 765 }
758 if (sizeof (uint32_t) != 766 if (sizeof(uint32_t) !=
759 PQgetlength (result, 767 PQgetlength(result,
760 row, 768 row,
761 fnum)) 769 fnum))
762 { 770 {
763 GNUNET_break (0); 771 GNUNET_break(0);
764 return GNUNET_SYSERR; 772 return GNUNET_SYSERR;
765 } 773 }
766 res = (uint32_t *) PQgetvalue (result, 774 res = (uint32_t *)PQgetvalue(result,
767 row, 775 row,
768 fnum); 776 fnum);
769 *udst = ntohl (*res); 777 *udst = ntohl(*res);
770 return GNUNET_OK; 778 return GNUNET_OK;
771} 779}
772 780
@@ -779,14 +787,15 @@ extract_uint32 (void *cls,
779 * @return array entry for the result specification to use 787 * @return array entry for the result specification to use
780 */ 788 */
781struct GNUNET_PQ_ResultSpec 789struct GNUNET_PQ_ResultSpec
782GNUNET_PQ_result_spec_uint32 (const char *name, 790GNUNET_PQ_result_spec_uint32(const char *name,
783 uint32_t *u32) 791 uint32_t *u32)
784{ 792{
785 struct GNUNET_PQ_ResultSpec res = 793 struct GNUNET_PQ_ResultSpec res =
786 { &extract_uint32, 794 { &extract_uint32,
787 NULL, 795 NULL,
788 NULL, 796 NULL,
789 (void *) u32, sizeof (*u32), (name), NULL }; 797 (void *)u32, sizeof(*u32), (name), NULL };
798
790 return res; 799 return res;
791} 800}
792 801
@@ -805,50 +814,50 @@ GNUNET_PQ_result_spec_uint32 (const char *name,
805 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) 814 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
806 */ 815 */
807static int 816static int
808extract_uint64 (void *cls, 817extract_uint64(void *cls,
809 PGresult *result, 818 PGresult *result,
810 int row, 819 int row,
811 const char *fname, 820 const char *fname,
812 size_t *dst_size, 821 size_t *dst_size,
813 void *dst) 822 void *dst)
814{ 823{
815 uint64_t *udst = dst; 824 uint64_t *udst = dst;
816 const uint64_t *res; 825 const uint64_t *res;
817 int fnum; 826 int fnum;
818 827
819 (void) cls; 828 (void)cls;
820 fnum = PQfnumber (result, 829 fnum = PQfnumber(result,
821 fname); 830 fname);
822 if (fnum < 0) 831 if (fnum < 0)
823 { 832 {
824 GNUNET_break (0); 833 GNUNET_break(0);
825 return GNUNET_SYSERR; 834 return GNUNET_SYSERR;
826 } 835 }
827 if (PQgetisnull (result, 836 if (PQgetisnull(result,
828 row, 837 row,
829 fnum)) 838 fnum))
830 { 839 {
831 GNUNET_break (0); 840 GNUNET_break(0);
832 return GNUNET_SYSERR; 841 return GNUNET_SYSERR;
833 } 842 }
834 GNUNET_assert (NULL != dst); 843 GNUNET_assert(NULL != dst);
835 if (sizeof (uint64_t) != *dst_size) 844 if (sizeof(uint64_t) != *dst_size)
836 { 845 {
837 GNUNET_break (0); 846 GNUNET_break(0);
838 return GNUNET_SYSERR; 847 return GNUNET_SYSERR;
839 } 848 }
840 if (sizeof (uint64_t) != 849 if (sizeof(uint64_t) !=
841 PQgetlength (result, 850 PQgetlength(result,
842 row, 851 row,
843 fnum)) 852 fnum))
844 { 853 {
845 GNUNET_break (0); 854 GNUNET_break(0);
846 return GNUNET_SYSERR; 855 return GNUNET_SYSERR;
847 } 856 }
848 res = (uint64_t *) PQgetvalue (result, 857 res = (uint64_t *)PQgetvalue(result,
849 row, 858 row,
850 fnum); 859 fnum);
851 *udst = GNUNET_ntohll (*res); 860 *udst = GNUNET_ntohll(*res);
852 return GNUNET_OK; 861 return GNUNET_OK;
853} 862}
854 863
@@ -861,14 +870,15 @@ extract_uint64 (void *cls,
861 * @return array entry for the result specification to use 870 * @return array entry for the result specification to use
862 */ 871 */
863struct GNUNET_PQ_ResultSpec 872struct GNUNET_PQ_ResultSpec
864GNUNET_PQ_result_spec_uint64 (const char *name, 873GNUNET_PQ_result_spec_uint64(const char *name,
865 uint64_t *u64) 874 uint64_t *u64)
866{ 875{
867 struct GNUNET_PQ_ResultSpec res = 876 struct GNUNET_PQ_ResultSpec res =
868 { &extract_uint64, 877 { &extract_uint64,
869 NULL, 878 NULL,
870 NULL, 879 NULL,
871 (void *) u64, sizeof (*u64), (name), NULL }; 880 (void *)u64, sizeof(*u64), (name), NULL };
881
872 return res; 882 return res;
873} 883}
874 884
diff --git a/src/pq/test_pq.c b/src/pq/test_pq.c
index 67cf32733..df54580d2 100644
--- a/src/pq/test_pq.c
+++ b/src/pq/test_pq.c
@@ -1,22 +1,22 @@
1/* 1/*
2 This file is part of GNUnet 2 This file is part of GNUnet
3 (C) 2015, 2016 GNUnet e.V. 3 (C) 2015, 2016 GNUnet e.V.
4 4
5 GNUnet is free software: you can redistribute it and/or modify it 5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published 6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License, 7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version. 8 or (at your option) any later version.
9 9
10 GNUnet is distributed in the hope that it will be useful, but 10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20/** 20/**
21 * @file pq/test_pq.c 21 * @file pq/test_pq.c
22 * @brief Tests for Postgres convenience API 22 * @brief Tests for Postgres convenience API
@@ -34,52 +34,52 @@
34 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure 34 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
35 */ 35 */
36static int 36static int
37postgres_prepare (PGconn *db_conn) 37postgres_prepare(PGconn *db_conn)
38{ 38{
39 PGresult *result; 39 PGresult *result;
40 40
41#define PREPARE(name, sql, ...) \ 41#define PREPARE(name, sql, ...) \
42 do { \ 42 do { \
43 result = PQprepare (db_conn, name, sql, __VA_ARGS__); \ 43 result = PQprepare(db_conn, name, sql, __VA_ARGS__); \
44 if (PGRES_COMMAND_OK != PQresultStatus (result)) \ 44 if (PGRES_COMMAND_OK != PQresultStatus(result)) \
45 { \ 45 { \
46 GNUNET_break (0); \ 46 GNUNET_break(0); \
47 PQclear (result); result = NULL; \ 47 PQclear(result); result = NULL; \
48 return GNUNET_SYSERR; \ 48 return GNUNET_SYSERR; \
49 } \ 49 } \
50 PQclear (result); result = NULL; \ 50 PQclear(result); result = NULL; \
51 } while (0); 51 } while (0);
52 52
53 PREPARE ("test_insert", 53 PREPARE("test_insert",
54 "INSERT INTO test_pq (" 54 "INSERT INTO test_pq ("
55 " pub" 55 " pub"
56 ",sig" 56 ",sig"
57 ",abs_time" 57 ",abs_time"
58 ",forever" 58 ",forever"
59 ",hash" 59 ",hash"
60 ",vsize" 60 ",vsize"
61 ",u16" 61 ",u16"
62 ",u32" 62 ",u32"
63 ",u64" 63 ",u64"
64 ") VALUES " 64 ") VALUES "
65 "($1, $2, $3, $4, $5, $6," 65 "($1, $2, $3, $4, $5, $6,"
66 "$7, $8, $9);", 66 "$7, $8, $9);",
67 9, NULL); 67 9, NULL);
68 PREPARE ("test_select", 68 PREPARE("test_select",
69 "SELECT" 69 "SELECT"
70 " pub" 70 " pub"
71 ",sig" 71 ",sig"
72 ",abs_time" 72 ",abs_time"
73 ",forever" 73 ",forever"
74 ",hash" 74 ",hash"
75 ",vsize" 75 ",vsize"
76 ",u16" 76 ",u16"
77 ",u32" 77 ",u32"
78 ",u64" 78 ",u64"
79 " FROM test_pq" 79 " FROM test_pq"
80 " ORDER BY abs_time DESC " 80 " ORDER BY abs_time DESC "
81 " LIMIT 1;", 81 " LIMIT 1;",
82 0, NULL); 82 0, NULL);
83 return GNUNET_OK; 83 return GNUNET_OK;
84#undef PREPARE 84#undef PREPARE
85} 85}
@@ -91,13 +91,13 @@ postgres_prepare (PGconn *db_conn)
91 * @return 0 on success 91 * @return 0 on success
92 */ 92 */
93static int 93static int
94run_queries (PGconn *conn) 94run_queries(PGconn *conn)
95{ 95{
96 struct GNUNET_CRYPTO_RsaPublicKey *pub; 96 struct GNUNET_CRYPTO_RsaPublicKey *pub;
97 struct GNUNET_CRYPTO_RsaPublicKey *pub2 = NULL; 97 struct GNUNET_CRYPTO_RsaPublicKey *pub2 = NULL;
98 struct GNUNET_CRYPTO_RsaSignature *sig; 98 struct GNUNET_CRYPTO_RsaSignature *sig;
99 struct GNUNET_CRYPTO_RsaSignature *sig2 = NULL; 99 struct GNUNET_CRYPTO_RsaSignature *sig2 = NULL;
100 struct GNUNET_TIME_Absolute abs_time = GNUNET_TIME_absolute_get (); 100 struct GNUNET_TIME_Absolute abs_time = GNUNET_TIME_absolute_get();
101 struct GNUNET_TIME_Absolute abs_time2; 101 struct GNUNET_TIME_Absolute abs_time2;
102 struct GNUNET_TIME_Absolute forever = GNUNET_TIME_UNIT_FOREVER_ABS; 102 struct GNUNET_TIME_Absolute forever = GNUNET_TIME_UNIT_FOREVER_ABS;
103 struct GNUNET_TIME_Absolute forever2; 103 struct GNUNET_TIME_Absolute forever2;
@@ -117,102 +117,102 @@ run_queries (PGconn *conn)
117 uint64_t u64; 117 uint64_t u64;
118 uint64_t u642; 118 uint64_t u642;
119 119
120 priv = GNUNET_CRYPTO_rsa_private_key_create (1024); 120 priv = GNUNET_CRYPTO_rsa_private_key_create(1024);
121 pub = GNUNET_CRYPTO_rsa_private_key_get_public (priv); 121 pub = GNUNET_CRYPTO_rsa_private_key_get_public(priv);
122 memset (&hmsg, 42, sizeof (hmsg)); 122 memset(&hmsg, 42, sizeof(hmsg));
123 sig = GNUNET_CRYPTO_rsa_sign_fdh (priv, 123 sig = GNUNET_CRYPTO_rsa_sign_fdh(priv,
124 &hmsg); 124 &hmsg);
125 u16 = 16; 125 u16 = 16;
126 u32 = 32; 126 u32 = 32;
127 u64 = 64; 127 u64 = 64;
128 /* FIXME: test GNUNET_PQ_result_spec_variable_size */ 128 /* FIXME: test GNUNET_PQ_result_spec_variable_size */
129 { 129 {
130 struct GNUNET_PQ_QueryParam params_insert[] = { 130 struct GNUNET_PQ_QueryParam params_insert[] = {
131 GNUNET_PQ_query_param_rsa_public_key (pub), 131 GNUNET_PQ_query_param_rsa_public_key(pub),
132 GNUNET_PQ_query_param_rsa_signature (sig), 132 GNUNET_PQ_query_param_rsa_signature(sig),
133 GNUNET_PQ_query_param_absolute_time (&abs_time), 133 GNUNET_PQ_query_param_absolute_time(&abs_time),
134 GNUNET_PQ_query_param_absolute_time (&forever), 134 GNUNET_PQ_query_param_absolute_time(&forever),
135 GNUNET_PQ_query_param_auto_from_type (&hc), 135 GNUNET_PQ_query_param_auto_from_type(&hc),
136 GNUNET_PQ_query_param_fixed_size (msg, strlen (msg)), 136 GNUNET_PQ_query_param_fixed_size(msg, strlen(msg)),
137 GNUNET_PQ_query_param_uint16 (&u16), 137 GNUNET_PQ_query_param_uint16(&u16),
138 GNUNET_PQ_query_param_uint32 (&u32), 138 GNUNET_PQ_query_param_uint32(&u32),
139 GNUNET_PQ_query_param_uint64 (&u64), 139 GNUNET_PQ_query_param_uint64(&u64),
140 GNUNET_PQ_query_param_end 140 GNUNET_PQ_query_param_end
141 }; 141 };
142 struct GNUNET_PQ_QueryParam params_select[] = { 142 struct GNUNET_PQ_QueryParam params_select[] = {
143 GNUNET_PQ_query_param_end 143 GNUNET_PQ_query_param_end
144 }; 144 };
145 struct GNUNET_PQ_ResultSpec results_select[] = { 145 struct GNUNET_PQ_ResultSpec results_select[] = {
146 GNUNET_PQ_result_spec_rsa_public_key ("pub", &pub2), 146 GNUNET_PQ_result_spec_rsa_public_key("pub", &pub2),
147 GNUNET_PQ_result_spec_rsa_signature ("sig", &sig2), 147 GNUNET_PQ_result_spec_rsa_signature("sig", &sig2),
148 GNUNET_PQ_result_spec_absolute_time ("abs_time", &abs_time2), 148 GNUNET_PQ_result_spec_absolute_time("abs_time", &abs_time2),
149 GNUNET_PQ_result_spec_absolute_time ("forever", &forever2), 149 GNUNET_PQ_result_spec_absolute_time("forever", &forever2),
150 GNUNET_PQ_result_spec_auto_from_type ("hash", &hc2), 150 GNUNET_PQ_result_spec_auto_from_type("hash", &hc2),
151 GNUNET_PQ_result_spec_variable_size ("vsize", &msg2, &msg2_len), 151 GNUNET_PQ_result_spec_variable_size("vsize", &msg2, &msg2_len),
152 GNUNET_PQ_result_spec_uint16 ("u16", &u162), 152 GNUNET_PQ_result_spec_uint16("u16", &u162),
153 GNUNET_PQ_result_spec_uint32 ("u32", &u322), 153 GNUNET_PQ_result_spec_uint32("u32", &u322),
154 GNUNET_PQ_result_spec_uint64 ("u64", &u642), 154 GNUNET_PQ_result_spec_uint64("u64", &u642),
155 GNUNET_PQ_result_spec_end 155 GNUNET_PQ_result_spec_end
156 }; 156 };
157 157
158 result = GNUNET_PQ_exec_prepared (conn, 158 result = GNUNET_PQ_exec_prepared(conn,
159 "test_insert", 159 "test_insert",
160 params_insert); 160 params_insert);
161 if (PGRES_COMMAND_OK != PQresultStatus (result)) 161 if (PGRES_COMMAND_OK != PQresultStatus(result))
162 { 162 {
163 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 163 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
164 "Database failure: %s\n", 164 "Database failure: %s\n",
165 PQresultErrorMessage (result)); 165 PQresultErrorMessage(result));
166 PQclear (result); 166 PQclear(result);
167 GNUNET_CRYPTO_rsa_signature_free (sig); 167 GNUNET_CRYPTO_rsa_signature_free(sig);
168 GNUNET_CRYPTO_rsa_private_key_free (priv); 168 GNUNET_CRYPTO_rsa_private_key_free(priv);
169 GNUNET_CRYPTO_rsa_public_key_free (pub); 169 GNUNET_CRYPTO_rsa_public_key_free(pub);
170 return 1; 170 return 1;
171 } 171 }
172 172
173 PQclear (result); 173 PQclear(result);
174 result = GNUNET_PQ_exec_prepared (conn, 174 result = GNUNET_PQ_exec_prepared(conn,
175 "test_select", 175 "test_select",
176 params_select); 176 params_select);
177 if (1 != 177 if (1 !=
178 PQntuples (result)) 178 PQntuples(result))
179 { 179 {
180 GNUNET_break (0); 180 GNUNET_break(0);
181 PQclear (result); 181 PQclear(result);
182 GNUNET_CRYPTO_rsa_signature_free (sig); 182 GNUNET_CRYPTO_rsa_signature_free(sig);
183 GNUNET_CRYPTO_rsa_private_key_free (priv); 183 GNUNET_CRYPTO_rsa_private_key_free(priv);
184 GNUNET_CRYPTO_rsa_public_key_free (pub); 184 GNUNET_CRYPTO_rsa_public_key_free(pub);
185 return 1; 185 return 1;
186 } 186 }
187 ret = GNUNET_PQ_extract_result (result, 187 ret = GNUNET_PQ_extract_result(result,
188 results_select, 188 results_select,
189 0); 189 0);
190 GNUNET_break (GNUNET_YES == ret); 190 GNUNET_break(GNUNET_YES == ret);
191 GNUNET_break (abs_time.abs_value_us == abs_time2.abs_value_us); 191 GNUNET_break(abs_time.abs_value_us == abs_time2.abs_value_us);
192 GNUNET_break (forever.abs_value_us == forever2.abs_value_us); 192 GNUNET_break(forever.abs_value_us == forever2.abs_value_us);
193 GNUNET_break (0 == 193 GNUNET_break(0 ==
194 GNUNET_memcmp (&hc, 194 GNUNET_memcmp(&hc,
195 &hc2)); 195 &hc2));
196 GNUNET_break (0 == 196 GNUNET_break(0 ==
197 GNUNET_CRYPTO_rsa_signature_cmp (sig, 197 GNUNET_CRYPTO_rsa_signature_cmp(sig,
198 sig2)); 198 sig2));
199 GNUNET_break (0 == 199 GNUNET_break(0 ==
200 GNUNET_CRYPTO_rsa_public_key_cmp (pub, 200 GNUNET_CRYPTO_rsa_public_key_cmp(pub,
201 pub2)); 201 pub2));
202 GNUNET_break (strlen (msg) == msg2_len); 202 GNUNET_break(strlen(msg) == msg2_len);
203 GNUNET_break (0 == 203 GNUNET_break(0 ==
204 strncmp (msg, 204 strncmp(msg,
205 msg2, 205 msg2,
206 msg2_len)); 206 msg2_len));
207 GNUNET_break (16 == u162); 207 GNUNET_break(16 == u162);
208 GNUNET_break (32 == u322); 208 GNUNET_break(32 == u322);
209 GNUNET_break (64 == u642); 209 GNUNET_break(64 == u642);
210 GNUNET_PQ_cleanup_result (results_select); 210 GNUNET_PQ_cleanup_result(results_select);
211 PQclear (result); 211 PQclear(result);
212 } 212 }
213 GNUNET_CRYPTO_rsa_signature_free (sig); 213 GNUNET_CRYPTO_rsa_signature_free(sig);
214 GNUNET_CRYPTO_rsa_private_key_free (priv); 214 GNUNET_CRYPTO_rsa_private_key_free(priv);
215 GNUNET_CRYPTO_rsa_public_key_free (pub); 215 GNUNET_CRYPTO_rsa_public_key_free(pub);
216 if (GNUNET_OK != ret) 216 if (GNUNET_OK != ret)
217 return 1; 217 return 1;
218 218
@@ -221,70 +221,70 @@ run_queries (PGconn *conn)
221 221
222 222
223int 223int
224main (int argc, 224main(int argc,
225 const char *const argv[]) 225 const char *const argv[])
226{ 226{
227 PGconn *conn; 227 PGconn *conn;
228 PGresult *result; 228 PGresult *result;
229 int ret; 229 int ret;
230 230
231 GNUNET_log_setup ("test-pq", 231 GNUNET_log_setup("test-pq",
232 "WARNING", 232 "WARNING",
233 NULL); 233 NULL);
234 conn = PQconnectdb ("postgres:///gnunetcheck"); 234 conn = PQconnectdb("postgres:///gnunetcheck");
235 if (CONNECTION_OK != PQstatus (conn)) 235 if (CONNECTION_OK != PQstatus(conn))
236 { 236 {
237 fprintf (stderr, 237 fprintf(stderr,
238 "Cannot run test, database connection failed: %s\n", 238 "Cannot run test, database connection failed: %s\n",
239 PQerrorMessage (conn)); 239 PQerrorMessage(conn));
240 GNUNET_break (0); 240 GNUNET_break(0);
241 PQfinish (conn); 241 PQfinish(conn);
242 return 77; /* signal test was skipped */ 242 return 77; /* signal test was skipped */
243 } 243 }
244 244
245 result = PQexec (conn, 245 result = PQexec(conn,
246 "CREATE TEMPORARY TABLE IF NOT EXISTS test_pq (" 246 "CREATE TEMPORARY TABLE IF NOT EXISTS test_pq ("
247 " pub BYTEA NOT NULL" 247 " pub BYTEA NOT NULL"
248 ",sig BYTEA NOT NULL" 248 ",sig BYTEA NOT NULL"
249 ",abs_time INT8 NOT NULL" 249 ",abs_time INT8 NOT NULL"
250 ",forever INT8 NOT NULL" 250 ",forever INT8 NOT NULL"
251 ",hash BYTEA NOT NULL CHECK(LENGTH(hash)=64)" 251 ",hash BYTEA NOT NULL CHECK(LENGTH(hash)=64)"
252 ",vsize VARCHAR NOT NULL" 252 ",vsize VARCHAR NOT NULL"
253 ",u16 INT2 NOT NULL" 253 ",u16 INT2 NOT NULL"
254 ",u32 INT4 NOT NULL" 254 ",u32 INT4 NOT NULL"
255 ",u64 INT8 NOT NULL" 255 ",u64 INT8 NOT NULL"
256 ")"); 256 ")");
257 if (PGRES_COMMAND_OK != PQresultStatus (result)) 257 if (PGRES_COMMAND_OK != PQresultStatus(result))
258 { 258 {
259 fprintf (stderr, 259 fprintf(stderr,
260 "Failed to create table: %s\n", 260 "Failed to create table: %s\n",
261 PQerrorMessage (conn)); 261 PQerrorMessage(conn));
262 PQclear (result); 262 PQclear(result);
263 PQfinish (conn); 263 PQfinish(conn);
264 return 1; 264 return 1;
265 } 265 }
266 PQclear (result); 266 PQclear(result);
267 if (GNUNET_OK != 267 if (GNUNET_OK !=
268 postgres_prepare (conn)) 268 postgres_prepare(conn))
269 { 269 {
270 GNUNET_break (0); 270 GNUNET_break(0);
271 PQfinish (conn); 271 PQfinish(conn);
272 return 1; 272 return 1;
273 } 273 }
274 ret = run_queries (conn); 274 ret = run_queries(conn);
275 result = PQexec (conn, 275 result = PQexec(conn,
276 "DROP TABLE test_pq"); 276 "DROP TABLE test_pq");
277 if (PGRES_COMMAND_OK != PQresultStatus (result)) 277 if (PGRES_COMMAND_OK != PQresultStatus(result))
278 { 278 {
279 fprintf (stderr, 279 fprintf(stderr,
280 "Failed to create table: %s\n", 280 "Failed to create table: %s\n",
281 PQerrorMessage (conn)); 281 PQerrorMessage(conn));
282 PQclear (result); 282 PQclear(result);
283 PQfinish (conn); 283 PQfinish(conn);
284 return 1; 284 return 1;
285 } 285 }
286 PQclear (result); 286 PQclear(result);
287 PQfinish (conn); 287 PQfinish(conn);
288 return ret; 288 return ret;
289} 289}
290 290