diff options
Diffstat (limited to 'src/include/taler_util.h')
-rw-r--r-- | src/include/taler_util.h | 255 |
1 files changed, 255 insertions, 0 deletions
diff --git a/src/include/taler_util.h b/src/include/taler_util.h new file mode 100644 index 000000000..a8a7c2013 --- /dev/null +++ b/src/include/taler_util.h | |||
@@ -0,0 +1,255 @@ | |||
1 | /* | ||
2 | This file is part of TALER | ||
3 | (C) 2014 Christian Grothoff (and other contributing authors) | ||
4 | |||
5 | TALER is free software; you can redistribute it and/or modify it under the | ||
6 | terms of the GNU General Public License as published by the Free Software | ||
7 | Foundation; either version 3, or (at your option) any later version. | ||
8 | |||
9 | TALER is distributed in the hope that it will be useful, but WITHOUT ANY | ||
10 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR | ||
11 | A PARTICULAR PURPOSE. See the GNU General Public License for more details. | ||
12 | |||
13 | You should have received a copy of the GNU General Public License along with | ||
14 | TALER; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/> | ||
15 | */ | ||
16 | |||
17 | /** | ||
18 | * @file include/taler_util.h | ||
19 | * @brief Interface for common utility functions | ||
20 | * @author Sree Harsha Totakura <sreeharsha@totakura.in> | ||
21 | */ | ||
22 | |||
23 | #ifndef UTIL_H_ | ||
24 | #define UTIL_H_ | ||
25 | |||
26 | #include <gnunet/gnunet_util_lib.h> | ||
27 | #include <gcrypt.h> | ||
28 | |||
29 | /* Define logging functions */ | ||
30 | #define LOG_DEBUG(...) \ | ||
31 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__) | ||
32 | |||
33 | #define LOG_WARNING(...) \ | ||
34 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, __VA_ARGS__) | ||
35 | |||
36 | #define LOG_ERROR(...) \ | ||
37 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, __VA_ARGS__) | ||
38 | |||
39 | |||
40 | /** | ||
41 | * Tests a given as assertion and if failed prints it as a warning with the | ||
42 | * given reason | ||
43 | * | ||
44 | * @param EXP the expression to test as assertion | ||
45 | * @param reason string to print as warning | ||
46 | */ | ||
47 | #define TALER_assert_as(EXP, reason) \ | ||
48 | do { \ | ||
49 | if (EXP) break; \ | ||
50 | LOG_ERROR("%s at %s:%d\n", reason, __FILE__, __LINE__); \ | ||
51 | abort(); \ | ||
52 | } while(0) | ||
53 | |||
54 | |||
55 | |||
56 | /** | ||
57 | * Log an error message at log-level 'level' that indicates | ||
58 | * a failure of the command 'cmd' with the message given | ||
59 | * by gcry_strerror(rc). | ||
60 | */ | ||
61 | #define LOG_GCRY_ERROR(cmd, rc) do { LOG_ERROR("`%s' failed at %s:%d with error: %s\n", cmd, __FILE__, __LINE__, gcry_strerror(rc)); } while(0) | ||
62 | |||
63 | |||
64 | #define TALER_gcry_ok(cmd) \ | ||
65 | do {int rc; rc = cmd; if (!rc) break; LOG_ERROR("A Gcrypt call failed at %s:%d with error: %s\n", __FILE__, __LINE__, gcry_strerror(rc)); abort(); } while (0) | ||
66 | |||
67 | |||
68 | #define TALER_CURRENCY_LEN 4 | ||
69 | |||
70 | |||
71 | GNUNET_NETWORK_STRUCT_BEGIN | ||
72 | |||
73 | struct TALER_AmountNBO | ||
74 | { | ||
75 | uint32_t value; | ||
76 | uint32_t fraction; | ||
77 | char currency[TALER_CURRENCY_LEN]; | ||
78 | }; | ||
79 | |||
80 | GNUNET_NETWORK_STRUCT_END | ||
81 | |||
82 | struct TALER_HashContext | ||
83 | { | ||
84 | gcry_md_hd_t hd; | ||
85 | }; | ||
86 | |||
87 | |||
88 | |||
89 | /** | ||
90 | * Representation of monetary value in a given currency. | ||
91 | */ | ||
92 | struct TALER_Amount | ||
93 | { | ||
94 | /** | ||
95 | * Value (numerator of fraction) | ||
96 | */ | ||
97 | uint32_t value; | ||
98 | /** | ||
99 | * Fraction (denominator of fraction) | ||
100 | */ | ||
101 | uint32_t fraction; | ||
102 | /** | ||
103 | * Currency string, left adjusted and padded with zeros. | ||
104 | */ | ||
105 | char currency[4]; | ||
106 | }; | ||
107 | |||
108 | |||
109 | /** | ||
110 | * Initialize Gcrypt library. | ||
111 | */ | ||
112 | void | ||
113 | TALER_gcrypt_init(); | ||
114 | |||
115 | |||
116 | /** | ||
117 | * Generate a ECC private key. | ||
118 | * | ||
119 | * @return the s-expression representing the generated ECC private key; NULL | ||
120 | * upon error | ||
121 | */ | ||
122 | gcry_sexp_t | ||
123 | TALER_genkey (); | ||
124 | |||
125 | |||
126 | /** | ||
127 | * Parse denomination description, in the format "T : V : F". | ||
128 | * | ||
129 | * @param str denomination description | ||
130 | * @param denom denomination to write the result to | ||
131 | * @return GNUNET_OK if the string is a valid denomination specification, | ||
132 | * GNUNET_SYSERR if it is invalid. | ||
133 | */ | ||
134 | int | ||
135 | TALER_string_to_amount (const char *str, struct TALER_Amount *denom); | ||
136 | |||
137 | |||
138 | /** | ||
139 | * FIXME | ||
140 | */ | ||
141 | struct TALER_AmountNBO | ||
142 | TALER_amount_hton (struct TALER_Amount d); | ||
143 | |||
144 | |||
145 | /** | ||
146 | * FIXME | ||
147 | */ | ||
148 | struct TALER_Amount | ||
149 | TALER_amount_ntoh (struct TALER_AmountNBO dn); | ||
150 | |||
151 | /** | ||
152 | * Compare the value/fraction of two amounts. Does not compare the currency, | ||
153 | * i.e. comparing amounts with the same value and fraction but different | ||
154 | * currency would return 0. | ||
155 | * | ||
156 | * @param a1 first amount | ||
157 | * @param a2 second amount | ||
158 | * @return result of the comparison | ||
159 | */ | ||
160 | int | ||
161 | TALER_amount_cmp (struct TALER_Amount a1, struct TALER_Amount a2); | ||
162 | |||
163 | |||
164 | /** | ||
165 | * Perform saturating subtraction of amounts. | ||
166 | * | ||
167 | * @param a1 amount to subtract from | ||
168 | * @param a2 amount to subtract | ||
169 | * @return (a1-a2) or 0 if a2>=a1 | ||
170 | */ | ||
171 | struct TALER_Amount | ||
172 | TALER_amount_subtract (struct TALER_Amount a1, struct TALER_Amount a2); | ||
173 | |||
174 | |||
175 | /** | ||
176 | * Perform saturating addition of amounts | ||
177 | * | ||
178 | * @param a1 first amount to add | ||
179 | * @param a2 second amount to add | ||
180 | * @return sum of a1 and a2 | ||
181 | */ | ||
182 | struct TALER_Amount | ||
183 | TALER_amount_add (struct TALER_Amount a1, struct TALER_Amount a2); | ||
184 | |||
185 | |||
186 | /** | ||
187 | * Normalize the given amount. | ||
188 | * | ||
189 | * @param amout amount to normalize | ||
190 | * @return normalized amount | ||
191 | */ | ||
192 | struct TALER_Amount | ||
193 | TALER_amount_normalize (struct TALER_Amount amount); | ||
194 | |||
195 | |||
196 | /** | ||
197 | * Convert amount to string. | ||
198 | * | ||
199 | * @param amount amount to convert to string | ||
200 | * @return freshly allocated string representation | ||
201 | */ | ||
202 | char * | ||
203 | TALER_amount_to_string (struct TALER_Amount amount); | ||
204 | |||
205 | |||
206 | /** | ||
207 | * Return the base32crockford encoding of the given buffer. | ||
208 | * | ||
209 | * The returned string will be freshly allocated, and must be free'd | ||
210 | * with GNUNET_free. | ||
211 | * | ||
212 | * @param buffer with data | ||
213 | * @param size size of the buffer | ||
214 | * @return freshly allocated, null-terminated string | ||
215 | */ | ||
216 | char * | ||
217 | TALER_data_to_string_alloc (const void *buf, size_t size); | ||
218 | |||
219 | |||
220 | /** | ||
221 | * Get encoded binary data from a configuration. | ||
222 | * | ||
223 | * @return GNUNET_OK on success | ||
224 | * GNUNET_NO is the value does not exist | ||
225 | * GNUNET_SYSERR on encoding error | ||
226 | */ | ||
227 | int | ||
228 | TALER_configuration_get_data (const struct GNUNET_CONFIGURATION_Handle *cfg, | ||
229 | const char *section, const char *option, | ||
230 | void *buf, size_t buf_size); | ||
231 | |||
232 | |||
233 | |||
234 | |||
235 | int | ||
236 | TALER_refresh_decrypt (const void *input, size_t input_size, const struct GNUNET_HashCode *secret, void *result); | ||
237 | |||
238 | int | ||
239 | TALER_refresh_encrypt (const void *input, size_t input_size, const struct GNUNET_HashCode *secret, void *result); | ||
240 | |||
241 | |||
242 | |||
243 | void | ||
244 | TALER_hash_context_start (struct TALER_HashContext *hc); | ||
245 | |||
246 | |||
247 | void | ||
248 | TALER_hash_context_read (struct TALER_HashContext *hc, void *buf, size_t size); | ||
249 | |||
250 | |||
251 | void | ||
252 | TALER_hash_context_finish (struct TALER_HashContext *hc, | ||
253 | struct GNUNET_HashCode *r_hash); | ||
254 | |||
255 | #endif | ||