aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/include/gnunet_crypto_lib.h1
-rw-r--r--src/include/gnunet_strings_lib.h4
-rw-r--r--src/util/strings.c191
3 files changed, 88 insertions, 108 deletions
diff --git a/src/include/gnunet_crypto_lib.h b/src/include/gnunet_crypto_lib.h
index 04b460446..6224546b9 100644
--- a/src/include/gnunet_crypto_lib.h
+++ b/src/include/gnunet_crypto_lib.h
@@ -346,6 +346,7 @@ GNUNET_CRYPTO_random_permute (enum GNUNET_CRYPTO_Quality mode, unsigned int n);
346void 346void
347GNUNET_CRYPTO_aes_create_session_key (struct GNUNET_CRYPTO_AesSessionKey *key); 347GNUNET_CRYPTO_aes_create_session_key (struct GNUNET_CRYPTO_AesSessionKey *key);
348 348
349
349/** 350/**
350 * Check that a new session key is well-formed. 351 * Check that a new session key is well-formed.
351 * 352 *
diff --git a/src/include/gnunet_strings_lib.h b/src/include/gnunet_strings_lib.h
index 1398a0826..997ca7dd9 100644
--- a/src/include/gnunet_strings_lib.h
+++ b/src/include/gnunet_strings_lib.h
@@ -66,12 +66,12 @@ GNUNET_STRINGS_fancy_size_to_bytes (const char *fancy_size,
66 * Convert a given fancy human-readable time to our internal 66 * Convert a given fancy human-readable time to our internal
67 * representation. 67 * representation.
68 * 68 *
69 * @param fancy_size human readable string (i.e. 1 minute) 69 * @param fancy_time human readable string (i.e. 1 minute)
70 * @param rtime set to the relative time 70 * @param rtime set to the relative time
71 * @return GNUNET_OK on success, GNUNET_SYSERR on error 71 * @return GNUNET_OK on success, GNUNET_SYSERR on error
72 */ 72 */
73int 73int
74GNUNET_STRINGS_fancy_time_to_relative (const char *fancy_size, 74GNUNET_STRINGS_fancy_time_to_relative (const char *fancy_time,
75 struct GNUNET_TIME_Relative *rtime); 75 struct GNUNET_TIME_Relative *rtime);
76 76
77 77
diff --git a/src/util/strings.c b/src/util/strings.c
index 8c7c68bb7..2ed1a0da3 100644
--- a/src/util/strings.c
+++ b/src/util/strings.c
@@ -170,51 +170,38 @@ GNUNET_STRINGS_byte_size_fancy (unsigned long long size)
170 170
171 171
172/** 172/**
173 * Convert a given fancy human-readable size to bytes. 173 * Unit conversion table entry for 'convert_with_table'.
174 */
175struct ConversionTable
176{
177 /**
178 * Name of the unit (or NULL for end of table).
179 */
180 const char *name;
181
182 /**
183 * Factor to apply for this unit.
184 */
185 unsigned long long value;
186};
187
188
189/**
190 * Convert a string of the form "4 X 5 Y" into a numeric value
191 * by interpreting "X" and "Y" as units and then multiplying
192 * the numbers with the values associated with the respective
193 * unit from the conversion table.
174 * 194 *
175 * @param fancy_size human readable string (i.e. 1 MB) 195 * @param input input string to parse
176 * @param size set to the size in bytes 196 * @param table table with the conversion of unit names to numbers
197 * @param output where to store the result
177 * @return GNUNET_OK on success, GNUNET_SYSERR on error 198 * @return GNUNET_OK on success, GNUNET_SYSERR on error
178 */ 199 */
179int 200static int
180GNUNET_STRINGS_fancy_size_to_bytes (const char *fancy_size, 201convert_with_table (const char *input,
181 unsigned long long *size) 202 const struct ConversionTable *table,
203 unsigned long long *output)
182{ 204{
183 struct
184 {
185 const char *name;
186 unsigned long long value;
187 } table[] =
188 {
189 {
190 "B", 1},
191 {
192 "KiB", 1024},
193 {
194 "kB", 1000},
195 {
196 "MiB", 1024 * 1024},
197 {
198 "MB", 1000 * 1000},
199 {
200 "GiB", 1024 * 1024 * 1024},
201 {
202 "GB", 1000 * 1000 * 1000},
203 {
204 "TiB", 1024LL * 1024LL * 1024LL * 1024LL},
205 {
206 "TB", 1000LL * 1000LL * 1000LL * 1024LL},
207 {
208 "PiB", 1024LL * 1024LL * 1024LL * 1024LL * 1024LL},
209 {
210 "PB", 1000LL * 1000LL * 1000LL * 1024LL * 1000LL},
211 {
212 "EiB", 1024LL * 1024LL * 1024LL * 1024LL * 1024LL * 1024LL},
213 {
214 "EB", 1000LL * 1000LL * 1000LL * 1024LL * 1000LL * 1000LL},
215 {
216 NULL, 0}
217 };
218 unsigned long long ret; 205 unsigned long long ret;
219 char *in; 206 char *in;
220 const char *tok; 207 const char *tok;
@@ -223,7 +210,7 @@ GNUNET_STRINGS_fancy_size_to_bytes (const char *fancy_size,
223 210
224 ret = 0; 211 ret = 0;
225 last = 0; 212 last = 0;
226 in = GNUNET_strdup (fancy_size); 213 in = GNUNET_strdup (input);
227 for (tok = strtok (in, " "); tok != NULL; tok = strtok (NULL, " ")) 214 for (tok = strtok (in, " "); tok != NULL; tok = strtok (NULL, " "))
228 { 215 {
229 i = 0; 216 i = 0;
@@ -243,88 +230,80 @@ GNUNET_STRINGS_fancy_size_to_bytes (const char *fancy_size,
243 } 230 }
244 } 231 }
245 ret += last; 232 ret += last;
246 *size = ret; 233 *output = ret;
247 GNUNET_free (in); 234 GNUNET_free (in);
248 return GNUNET_OK; 235 return GNUNET_OK;
249} 236}
250 237
251 238
252/** 239/**
240 * Convert a given fancy human-readable size to bytes.
241 *
242 * @param fancy_size human readable string (i.e. 1 MB)
243 * @param size set to the size in bytes
244 * @return GNUNET_OK on success, GNUNET_SYSERR on error
245 */
246int
247GNUNET_STRINGS_fancy_size_to_bytes (const char *fancy_size,
248 unsigned long long *size)
249{
250 static const struct ConversionTable table[] =
251 {
252 { "B", 1},
253 { "KiB", 1024},
254 { "kB", 1000},
255 { "MiB", 1024 * 1024},
256 { "MB", 1000 * 1000},
257 { "GiB", 1024 * 1024 * 1024},
258 { "GB", 1000 * 1000 * 1000},
259 { "TiB", 1024LL * 1024LL * 1024LL * 1024LL},
260 { "TB", 1000LL * 1000LL * 1000LL * 1024LL},
261 { "PiB", 1024LL * 1024LL * 1024LL * 1024LL * 1024LL},
262 { "PB", 1000LL * 1000LL * 1000LL * 1024LL * 1000LL},
263 { "EiB", 1024LL * 1024LL * 1024LL * 1024LL * 1024LL * 1024LL},
264 { "EB", 1000LL * 1000LL * 1000LL * 1024LL * 1000LL * 1000LL},
265 { NULL, 0}
266 };
267
268 return convert_with_table (fancy_size,
269 table,
270 size);
271}
272
273
274/**
253 * Convert a given fancy human-readable time to our internal 275 * Convert a given fancy human-readable time to our internal
254 * representation. 276 * representation.
255 * 277 *
256 * @param fancy_size human readable string (i.e. 1 minute) 278 * @param fancy_time human readable string (i.e. 1 minute)
257 * @param rtime set to the relative time 279 * @param rtime set to the relative time
258 * @return GNUNET_OK on success, GNUNET_SYSERR on error 280 * @return GNUNET_OK on success, GNUNET_SYSERR on error
259 */ 281 */
260int 282int
261GNUNET_STRINGS_fancy_time_to_relative (const char *fancy_size, 283GNUNET_STRINGS_fancy_time_to_relative (const char *fancy_time,
262 struct GNUNET_TIME_Relative *rtime) 284 struct GNUNET_TIME_Relative *rtime)
263{ 285{
264 struct 286 static const struct ConversionTable table[] =
265 { 287 {
266 const char *name; 288 { "ms", 1},
267 unsigned long long value; 289 { "s", 1000},
268 } table[] = 290 { "\"", 1000},
269 { 291 { "min", 60 * 1000},
270 { 292 { "minutes", 60 * 1000},
271 "ms", 1}, 293 { "'", 60 * 1000},
272 { 294 { "h", 60 * 60 * 1000},
273 "s", 1000}, 295 { "d", 24 * 60 * 60 * 1000},
274 { 296 { "a", 31557600 /* year */ },
275 "\"", 1000}, 297 { NULL, 0}
276 {
277 "min", 60 * 1000},
278 {
279 "minutes", 60 * 1000},
280 {
281 "'", 60 * 1000},
282 {
283 "h", 60 * 60 * 1000},
284 {
285 "d", 24 * 60 * 60 * 1000},
286 {
287 "a", 31557600 /* year */ },
288 {
289 NULL, 0}
290 }; 298 };
291 unsigned long long ret; 299 int ret;
292 char *in; 300 unsigned long long val;
293 const char *tok;
294 unsigned long long last;
295 unsigned int i;
296 301
297 if ((0 == strcasecmp (fancy_size, "infinity")) || 302 ret = convert_with_table (fancy_time,
298 (0 == strcasecmp (fancy_size, "forever"))) 303 table,
299 { 304 &val);
300 *rtime = GNUNET_TIME_UNIT_FOREVER_REL; 305 rtime->rel_value = (uint64_t) val;
301 return GNUNET_OK; 306 return ret;
302 }
303 ret = 0;
304 last = 0;
305 in = GNUNET_strdup (fancy_size);
306 for (tok = strtok (in, " "); tok != NULL; tok = strtok (NULL, " "))
307 {
308 i = 0;
309 while ((table[i].name != NULL) && (0 != strcasecmp (table[i].name, tok)))
310 i++;
311 if (table[i].name != NULL)
312 last *= table[i].value;
313 else
314 {
315 ret += last;
316 last = 0;
317 if (1 != SSCANF (tok, "%llu", &last))
318 {
319 GNUNET_free (in);
320 return GNUNET_SYSERR; /* expected number */
321 }
322 }
323 }
324 ret += last;
325 rtime->rel_value = (uint64_t) ret;
326 GNUNET_free (in);
327 return GNUNET_OK;
328} 307}
329 308
330/** 309/**