Use a uniform random number mod an RSA composites for both
[oweals/gnunet.git] / src / my / my.c
1 /*
2      This file is part of GNUnet
3      Copyright (C) 2016 Inria & GNUnet e.V.
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20 /**
21  * @file my/my.c
22  * @brief library to help with access to a MySQL database
23  * @author Christophe Genevey
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include <mysql/mysql.h>
28 #include "gnunet_my_lib.h"
29
30
31
32 /**
33  * Run a prepared SELECT statement.
34  *
35  * @param mc mysql context
36  * @param sh handle to SELECT statment
37  * @param params parameters to the statement
38  * @return 
39       #GNUNET_YES if we can prepare all statement
40       #GNUNET_SYSERR if we can't prepare all statement
41  */
42
43 int
44 GNUNET_MY_exec_prepared (struct GNUNET_MYSQL_Context *mc,
45                          struct GNUNET_MYSQL_StatementHandle *sh,
46                          const struct GNUNET_MY_QueryParam *params)
47 {
48   const struct GNUNET_MY_QueryParam *p;
49   unsigned int num;
50   unsigned int i;
51   MYSQL_STMT *stmt;
52
53   num = 0;
54   for (i=0;NULL != params[i].conv;i++)
55     num += params[i].num_params;
56   {
57     MYSQL_BIND qbind[num];
58     unsigned int off;
59
60     memset(qbind, 0, sizeof(qbind));
61     off = 0;
62     for (i=0;NULL != (p = &params[i])->conv;i++)
63     {
64       if (GNUNET_OK !=
65           p->conv (p->conv_cls,
66                    p,
67                    &qbind[off]))
68       {
69         return GNUNET_SYSERR;
70       }
71       off += p->num_params;
72     }
73     stmt = GNUNET_MYSQL_statement_get_stmt (mc, sh);
74     if (mysql_stmt_bind_param (stmt,
75                                qbind))
76     {
77       GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "mysql",
78                        _("`%s' failed at %s:%d with error: %s\n"),
79                        "mysql_stmt_bind_param", __FILE__, __LINE__,
80                        mysql_stmt_error (stmt));
81       GNUNET_MYSQL_statements_invalidate (mc);
82       return GNUNET_SYSERR;
83     }
84   }
85   if (mysql_stmt_execute (stmt))
86   {
87     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "mysql",
88                      _("`%s' failed at %s:%d with error: %s\n"),
89                      "mysql_stmt_execute", __FILE__, __LINE__,
90                      mysql_stmt_error (stmt));
91     GNUNET_MYSQL_statements_invalidate (mc);
92     return GNUNET_SYSERR;
93   }
94   return GNUNET_OK;
95 }
96
97
98 /**
99  * Extract results from a query result according
100  * to the given specification. If colums are NULL,
101  * the destination is not modified, and #GNUNET_NO is returned4
102  *
103  *
104  * @param result
105  * @param row, the row from the result to extract
106  * @param result specificatio to extract for
107  * @return
108     #GNUNET_YES if all results could be extracted
109     #GNUNET_NO if at least one result was NULL
110     #GNUNET_SYSERR if a result was invalid
111 */
112 int
113 GNUNET_MY_extract_result (struct GNUNET_MYSQL_StatementHandle *sh,
114                           struct GNUNET_MY_QueryParam *qp,
115                           struct GNUNET_MY_ResultSpec *rs,
116                           int row)
117 {
118   MYSQL_BIND *result;
119   unsigned int i;
120   int had_null = GNUNET_NO;
121   int ret;
122   
123   MYSQL_STMT *stmt;
124
125   stmt = GNUNET_MYSQL_statement_get_stmt (NULL /* FIXME */, sh);
126   // result = mysql_get_result (stmt);
127   result = NULL;
128
129   if (mysql_stmt_bind_result(stmt, result))
130   {
131
132       GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "mysql",
133                        _("`%s' failed at %s:%d with error: %s\n"),
134                        "mysql_stmt_bind_result", __FILE__, __LINE__,
135                        mysql_stmt_error (stmt));
136       return GNUNET_SYSERR;
137   }
138
139   for (i = 0 ; NULL != rs[i].conv ; i++)
140   {
141     struct GNUNET_MY_ResultSpec *spec;
142
143     spec = &rs[i];
144     ret = spec->conv (spec->conv_cls,
145                       spec,
146                       result);
147
148     if (GNUNET_SYSERR == ret)
149     {
150       return GNUNET_SYSERR;
151     }
152
153     if (NULL != spec->result_size)
154       *spec->result_size = spec->dst_size;
155   }
156
157   if (GNUNET_YES == had_null)
158     return GNUNET_NO;
159
160   return GNUNET_OK;
161 }
162
163 /* end of my.c */