diff options
author | jospaeth <spaethj@in.tum.de> | 2020-08-29 22:39:10 +0200 |
---|---|---|
committer | jospaeth <spaethj@in.tum.de> | 2020-08-29 22:39:10 +0200 |
commit | e59a1f216e5ac50d734d3936ca739fcd057aba32 (patch) | |
tree | 273697f3c29993cf265b829dafcf7a03913d844a | |
parent | abd526c01164422a086374a1a96ca9ee5e781b01 (diff) | |
download | gnunet-e59a1f216e5ac50d734d3936ca739fcd057aba32.tar.gz gnunet-e59a1f216e5ac50d734d3936ca739fcd057aba32.zip |
refactor anchor_string_to_data and anchor_data_to_string in the plugins
-rw-r--r-- | src/escrow/escrow_plugin_helper.c | 74 | ||||
-rw-r--r-- | src/escrow/escrow_plugin_helper.h | 25 | ||||
-rw-r--r-- | src/escrow/plugin_escrow_anastasis.c | 47 | ||||
-rw-r--r-- | src/escrow/plugin_escrow_gns.c | 47 | ||||
-rw-r--r-- | src/escrow/plugin_escrow_plaintext.c | 47 |
5 files changed, 111 insertions, 129 deletions
diff --git a/src/escrow/escrow_plugin_helper.c b/src/escrow/escrow_plugin_helper.c index 36acae049..154195e7e 100644 --- a/src/escrow/escrow_plugin_helper.c +++ b/src/escrow/escrow_plugin_helper.c | |||
@@ -358,4 +358,78 @@ ESCROW_get_escrow_status (struct GNUNET_ESCROW_Handle *h, | |||
358 | } | 358 | } |
359 | 359 | ||
360 | 360 | ||
361 | /** | ||
362 | * Deserialize an escrow anchor string into a GNUNET_ESCROW_Anchor struct | ||
363 | * | ||
364 | * @param anchorString the encoded escrow anchor string | ||
365 | * @param method the escrow plugin calling this function | ||
366 | * | ||
367 | * @return the deserialized data packed into a GNUNET_ESCROW_Anchor struct, | ||
368 | * NULL if we failed to parse the string | ||
369 | */ | ||
370 | struct GNUNET_ESCROW_Anchor * | ||
371 | ESCROW_anchor_string_to_data (char *anchorString, | ||
372 | enum GNUNET_ESCROW_Key_Escrow_Method method) | ||
373 | { | ||
374 | struct GNUNET_ESCROW_Anchor *anchor; | ||
375 | uint32_t data_size; | ||
376 | char *anchorStringCopy, *ptr, *egoNameCopy; | ||
377 | char delimiter[] = ":"; | ||
378 | |||
379 | anchorStringCopy = GNUNET_strdup (anchorString); | ||
380 | |||
381 | // split the string at the first occurrence of the delimiter | ||
382 | ptr = strtok (anchorStringCopy, delimiter); | ||
383 | egoNameCopy = GNUNET_strdup (ptr); | ||
384 | ptr = strtok (NULL, delimiter); | ||
385 | |||
386 | if (NULL == ptr) | ||
387 | { | ||
388 | // delimiter was not found | ||
389 | GNUNET_free (egoNameCopy); | ||
390 | GNUNET_free (anchorStringCopy); | ||
391 | return NULL; | ||
392 | } | ||
393 | |||
394 | data_size = strlen (ptr) + 1; | ||
395 | anchor = GNUNET_malloc (sizeof (struct GNUNET_ESCROW_Anchor) + data_size); | ||
396 | anchor->size = data_size; | ||
397 | anchor->egoName = egoNameCopy; | ||
398 | anchor->method = method; | ||
399 | |||
400 | // TODO: deserialize? | ||
401 | GNUNET_memcpy (&anchor[1], ptr, data_size); | ||
402 | |||
403 | GNUNET_free (anchorStringCopy); | ||
404 | |||
405 | return anchor; | ||
406 | } | ||
407 | |||
408 | |||
409 | /** | ||
410 | * Serialize an escrow anchor struct into a string | ||
411 | * | ||
412 | * @param escrowAnchor the escrow anchor struct | ||
413 | * @param method the escrow plugin calling this function | ||
414 | * | ||
415 | * @return the encoded escrow anchor string | ||
416 | */ | ||
417 | char * | ||
418 | ESCROW_anchor_data_to_string (struct GNUNET_ESCROW_Anchor *escrowAnchor, | ||
419 | enum GNUNET_ESCROW_Key_Escrow_Method method) | ||
420 | { | ||
421 | char *anchorString; | ||
422 | size_t egoNameSize; | ||
423 | |||
424 | egoNameSize = strlen (escrowAnchor->egoName); | ||
425 | |||
426 | anchorString = GNUNET_malloc (egoNameSize + 1 + escrowAnchor->size); | ||
427 | GNUNET_memcpy (anchorString, escrowAnchor->egoName, egoNameSize); | ||
428 | anchorString[egoNameSize] = ':'; | ||
429 | GNUNET_memcpy (anchorString + egoNameSize + 1, &escrowAnchor[1], escrowAnchor->size); | ||
430 | |||
431 | return anchorString; | ||
432 | } | ||
433 | |||
434 | |||
361 | /* end of escrow_plugin.c */ | 435 | /* end of escrow_plugin.c */ |
diff --git a/src/escrow/escrow_plugin_helper.h b/src/escrow/escrow_plugin_helper.h index f2a54eb6b..dd03522fe 100644 --- a/src/escrow/escrow_plugin_helper.h +++ b/src/escrow/escrow_plugin_helper.h | |||
@@ -106,3 +106,28 @@ ESCROW_update_escrow_status (struct GNUNET_ESCROW_Handle *h, | |||
106 | struct GNUNET_ESCROW_Status * | 106 | struct GNUNET_ESCROW_Status * |
107 | ESCROW_get_escrow_status (struct GNUNET_ESCROW_Handle *h, | 107 | ESCROW_get_escrow_status (struct GNUNET_ESCROW_Handle *h, |
108 | struct GNUNET_IDENTITY_Ego *ego); | 108 | struct GNUNET_IDENTITY_Ego *ego); |
109 | |||
110 | /** | ||
111 | * Deserialize an escrow anchor string into a GNUNET_ESCROW_Anchor struct | ||
112 | * | ||
113 | * @param anchorString the encoded escrow anchor string | ||
114 | * @param method the escrow plugin calling this function | ||
115 | * | ||
116 | * @return the deserialized data packed into a GNUNET_ESCROW_Anchor struct, | ||
117 | * NULL if we failed to parse the string | ||
118 | */ | ||
119 | struct GNUNET_ESCROW_Anchor * | ||
120 | ESCROW_anchor_string_to_data (char *anchorString, | ||
121 | enum GNUNET_ESCROW_Key_Escrow_Method method); | ||
122 | |||
123 | /** | ||
124 | * Serialize an escrow anchor struct into a string | ||
125 | * | ||
126 | * @param escrowAnchor the escrow anchor struct | ||
127 | * @param method the escrow plugin calling this function | ||
128 | * | ||
129 | * @return the encoded escrow anchor string | ||
130 | */ | ||
131 | char * | ||
132 | ESCROW_anchor_data_to_string (struct GNUNET_ESCROW_Anchor *escrowAnchor, | ||
133 | enum GNUNET_ESCROW_Key_Escrow_Method method); | ||
diff --git a/src/escrow/plugin_escrow_anastasis.c b/src/escrow/plugin_escrow_anastasis.c index b3029f885..f4ab4dcbe 100644 --- a/src/escrow/plugin_escrow_anastasis.c +++ b/src/escrow/plugin_escrow_anastasis.c | |||
@@ -164,38 +164,8 @@ struct GNUNET_ESCROW_Anchor * | |||
164 | anastasis_anchor_string_to_data (struct GNUNET_ESCROW_Handle *h, | 164 | anastasis_anchor_string_to_data (struct GNUNET_ESCROW_Handle *h, |
165 | char *anchorString) | 165 | char *anchorString) |
166 | { | 166 | { |
167 | struct GNUNET_ESCROW_Anchor *anchor; | 167 | return ESCROW_anchor_string_to_data (anchorString, |
168 | uint32_t data_size; | 168 | GNUNET_ESCROW_KEY_ANASTASIS); |
169 | char *anchorStringCopy, *ptr, *egoNameCopy; | ||
170 | char delimiter[] = ":"; | ||
171 | |||
172 | anchorStringCopy = GNUNET_strdup (anchorString); | ||
173 | |||
174 | // split the string at the first occurrence of the delimiter | ||
175 | ptr = strtok (anchorStringCopy, delimiter); | ||
176 | egoNameCopy = GNUNET_strdup (ptr); | ||
177 | ptr = strtok (NULL, delimiter); | ||
178 | |||
179 | if (NULL == ptr) | ||
180 | { | ||
181 | // delimiter was not found | ||
182 | GNUNET_free (egoNameCopy); | ||
183 | GNUNET_free (anchorStringCopy); | ||
184 | return NULL; | ||
185 | } | ||
186 | |||
187 | data_size = strlen (ptr) + 1; | ||
188 | anchor = GNUNET_malloc (sizeof (struct GNUNET_ESCROW_Anchor) + data_size); | ||
189 | anchor->size = data_size; | ||
190 | anchor->egoName = egoNameCopy; | ||
191 | anchor->method = GNUNET_ESCROW_KEY_ANASTASIS; | ||
192 | |||
193 | // TODO: deserialize? | ||
194 | GNUNET_memcpy (&anchor[1], ptr, data_size); | ||
195 | |||
196 | GNUNET_free (anchorStringCopy); | ||
197 | |||
198 | return anchor; | ||
199 | } | 169 | } |
200 | 170 | ||
201 | 171 | ||
@@ -211,17 +181,8 @@ char * | |||
211 | anastasis_anchor_data_to_string (struct GNUNET_ESCROW_Handle *h, | 181 | anastasis_anchor_data_to_string (struct GNUNET_ESCROW_Handle *h, |
212 | struct GNUNET_ESCROW_Anchor *escrowAnchor) | 182 | struct GNUNET_ESCROW_Anchor *escrowAnchor) |
213 | { | 183 | { |
214 | char *anchorString; | 184 | return ESCROW_anchor_data_to_string (escrowAnchor, |
215 | size_t egoNameSize; | 185 | GNUNET_ESCROW_KEY_ANASTASIS); |
216 | |||
217 | egoNameSize = strlen (escrowAnchor->egoName); | ||
218 | |||
219 | anchorString = GNUNET_malloc (egoNameSize + 1 + escrowAnchor->size); | ||
220 | GNUNET_memcpy (anchorString, escrowAnchor->egoName, egoNameSize); | ||
221 | anchorString[egoNameSize] = ':'; | ||
222 | GNUNET_memcpy (anchorString + egoNameSize + 1, &escrowAnchor[1], escrowAnchor->size); | ||
223 | |||
224 | return anchorString; | ||
225 | } | 186 | } |
226 | 187 | ||
227 | 188 | ||
diff --git a/src/escrow/plugin_escrow_gns.c b/src/escrow/plugin_escrow_gns.c index 94ce175c5..121fdc4af 100644 --- a/src/escrow/plugin_escrow_gns.c +++ b/src/escrow/plugin_escrow_gns.c | |||
@@ -1668,38 +1668,8 @@ struct GNUNET_ESCROW_Anchor * | |||
1668 | gns_anchor_string_to_data (struct GNUNET_ESCROW_Handle *h, | 1668 | gns_anchor_string_to_data (struct GNUNET_ESCROW_Handle *h, |
1669 | char *anchorString) | 1669 | char *anchorString) |
1670 | { | 1670 | { |
1671 | struct GNUNET_ESCROW_Anchor *anchor; | 1671 | return ESCROW_anchor_string_to_data (anchorString, |
1672 | uint32_t data_size; | 1672 | GNUNET_ESCROW_KEY_GNS); |
1673 | char *anchorStringCopy, *ptr, *egoNameCopy; | ||
1674 | char delimiter[] = ":"; | ||
1675 | |||
1676 | anchorStringCopy = GNUNET_strdup (anchorString); | ||
1677 | |||
1678 | // split the string at the first occurrence of the delimiter | ||
1679 | ptr = strtok (anchorStringCopy, delimiter); | ||
1680 | egoNameCopy = GNUNET_strdup (ptr); | ||
1681 | ptr = strtok (NULL, delimiter); | ||
1682 | |||
1683 | if (NULL == ptr) | ||
1684 | { | ||
1685 | // delimiter was not found | ||
1686 | GNUNET_free (egoNameCopy); | ||
1687 | GNUNET_free (anchorStringCopy); | ||
1688 | return NULL; | ||
1689 | } | ||
1690 | |||
1691 | data_size = strlen (ptr) + 1; | ||
1692 | anchor = GNUNET_malloc (sizeof (struct GNUNET_ESCROW_Anchor) + data_size); | ||
1693 | anchor->size = data_size; | ||
1694 | anchor->egoName = egoNameCopy; | ||
1695 | anchor->method = GNUNET_ESCROW_KEY_GNS; | ||
1696 | |||
1697 | // TODO: deserialize? | ||
1698 | GNUNET_memcpy (&anchor[1], ptr, data_size); | ||
1699 | |||
1700 | GNUNET_free (anchorStringCopy); | ||
1701 | |||
1702 | return anchor; | ||
1703 | } | 1673 | } |
1704 | 1674 | ||
1705 | 1675 | ||
@@ -1715,17 +1685,8 @@ char * | |||
1715 | gns_anchor_data_to_string (struct GNUNET_ESCROW_Handle *h, | 1685 | gns_anchor_data_to_string (struct GNUNET_ESCROW_Handle *h, |
1716 | struct GNUNET_ESCROW_Anchor *escrowAnchor) | 1686 | struct GNUNET_ESCROW_Anchor *escrowAnchor) |
1717 | { | 1687 | { |
1718 | char *anchorString; | 1688 | return ESCROW_anchor_data_to_string (escrowAnchor, |
1719 | size_t egoNameSize; | 1689 | GNUNET_ESCROW_KEY_GNS); |
1720 | |||
1721 | egoNameSize = strlen (escrowAnchor->egoName); | ||
1722 | |||
1723 | anchorString = GNUNET_malloc (egoNameSize + 1 + escrowAnchor->size); | ||
1724 | GNUNET_memcpy (anchorString, escrowAnchor->egoName, egoNameSize); | ||
1725 | anchorString[egoNameSize] = ':'; | ||
1726 | GNUNET_memcpy (anchorString + egoNameSize + 1, &escrowAnchor[1], escrowAnchor->size); | ||
1727 | |||
1728 | return anchorString; | ||
1729 | } | 1690 | } |
1730 | 1691 | ||
1731 | 1692 | ||
diff --git a/src/escrow/plugin_escrow_plaintext.c b/src/escrow/plugin_escrow_plaintext.c index eace829ea..215c8127e 100644 --- a/src/escrow/plugin_escrow_plaintext.c +++ b/src/escrow/plugin_escrow_plaintext.c | |||
@@ -457,38 +457,8 @@ struct GNUNET_ESCROW_Anchor * | |||
457 | plaintext_anchor_string_to_data (struct GNUNET_ESCROW_Handle *h, | 457 | plaintext_anchor_string_to_data (struct GNUNET_ESCROW_Handle *h, |
458 | char *anchorString) | 458 | char *anchorString) |
459 | { | 459 | { |
460 | struct GNUNET_ESCROW_Anchor *anchor; | 460 | return ESCROW_anchor_string_to_data (anchorString, |
461 | uint32_t data_size; | 461 | GNUNET_ESCROW_KEY_PLAINTEXT); |
462 | char *anchorStringCopy, *ptr, *egoNameCopy; | ||
463 | char delimiter[] = ":"; | ||
464 | |||
465 | anchorStringCopy = GNUNET_strdup (anchorString); | ||
466 | |||
467 | // split the string at the first occurrence of the delimiter | ||
468 | ptr = strtok (anchorStringCopy, delimiter); | ||
469 | egoNameCopy = GNUNET_strdup (ptr); | ||
470 | ptr = strtok (NULL, delimiter); | ||
471 | |||
472 | if (NULL == ptr) | ||
473 | { | ||
474 | // delimiter was not found | ||
475 | GNUNET_free (egoNameCopy); | ||
476 | GNUNET_free (anchorStringCopy); | ||
477 | return NULL; | ||
478 | } | ||
479 | |||
480 | data_size = strlen (ptr) + 1; | ||
481 | anchor = GNUNET_malloc (sizeof (struct GNUNET_ESCROW_Anchor) + data_size); | ||
482 | anchor->size = data_size; | ||
483 | anchor->egoName = egoNameCopy; | ||
484 | anchor->method = GNUNET_ESCROW_KEY_PLAINTEXT; | ||
485 | |||
486 | // TODO: deserialize? | ||
487 | GNUNET_memcpy (&anchor[1], ptr, data_size); | ||
488 | |||
489 | GNUNET_free (anchorStringCopy); | ||
490 | |||
491 | return anchor; | ||
492 | } | 462 | } |
493 | 463 | ||
494 | 464 | ||
@@ -504,17 +474,8 @@ char * | |||
504 | plaintext_anchor_data_to_string (struct GNUNET_ESCROW_Handle *h, | 474 | plaintext_anchor_data_to_string (struct GNUNET_ESCROW_Handle *h, |
505 | struct GNUNET_ESCROW_Anchor *escrowAnchor) | 475 | struct GNUNET_ESCROW_Anchor *escrowAnchor) |
506 | { | 476 | { |
507 | char *anchorString; | 477 | return ESCROW_anchor_data_to_string (escrowAnchor, |
508 | size_t egoNameSize; | 478 | GNUNET_ESCROW_KEY_PLAINTEXT); |
509 | |||
510 | egoNameSize = strlen (escrowAnchor->egoName); | ||
511 | |||
512 | anchorString = GNUNET_malloc (egoNameSize + 1 + escrowAnchor->size); | ||
513 | GNUNET_memcpy (anchorString, escrowAnchor->egoName, egoNameSize); | ||
514 | anchorString[egoNameSize] = ':'; | ||
515 | GNUNET_memcpy (anchorString + egoNameSize + 1, &escrowAnchor[1], escrowAnchor->size); | ||
516 | |||
517 | return anchorString; | ||
518 | } | 479 | } |
519 | 480 | ||
520 | 481 | ||