aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2012-03-04 14:09:57 +0000
committerChristian Grothoff <christian@grothoff.org>2012-03-04 14:09:57 +0000
commit87534f72154e9bc859919afaebbc7321dc4fda87 (patch)
tree4dbc81d20e9ec45992e724133e1dd772b281fb9e
parentfc646798891d075673e2ad8f2011c1e15160b6c1 (diff)
downloadgnunet-87534f72154e9bc859919afaebbc7321dc4fda87.tar.gz
gnunet-87534f72154e9bc859919afaebbc7321dc4fda87.zip
-renaming
-rw-r--r--src/include/gnunet_crypto_lib.h32
-rw-r--r--src/include/gnunet_strings_lib.h36
-rw-r--r--src/util/crypto_hash.c120
-rw-r--r--src/util/crypto_rsa.c4
-rw-r--r--src/util/strings.c137
5 files changed, 177 insertions, 152 deletions
diff --git a/src/include/gnunet_crypto_lib.h b/src/include/gnunet_crypto_lib.h
index 7224e84f8..04b460446 100644
--- a/src/include/gnunet_crypto_lib.h
+++ b/src/include/gnunet_crypto_lib.h
@@ -941,38 +941,6 @@ GNUNET_CRYPTO_rsa_verify (uint32_t purpose,
941void 941void
942GNUNET_CRYPTO_random_disable_entropy_gathering (void); 942GNUNET_CRYPTO_random_disable_entropy_gathering (void);
943 943
944/**
945 * Convert binary data to ASCII encoding. The ASCII encoding is rather
946 * GNUnet specific. It was chosen such that it only uses characters
947 * in [0-9A-V], can be produced without complex arithmetics and uses a
948 * small number of characters. The GNUnet encoding uses 103 characters.
949 * Does not append 0-terminator, but returns a pointer to the place where
950 * it should be placed, if needed.
951 *
952 * @param data data to encode
953 * @param size size of data (in bytes)
954 * @param out buffer to fill
955 * @param out_size size of the buffer. Must be large enough to hold
956 * ((size*8) + (((size*8) % 5) > 0 ? 5 - ((size*8) % 5) : 0)) / 5
957 * @return pointer to the next byte in 'out' or NULL on error.
958 */
959char *
960GNUNET_CRYPTO_data_to_string (unsigned char *data, size_t size,
961 char *out, size_t out_size);
962
963/**
964 * Convert ASCII encoding back to data
965 * out_size must match exactly the size of the data before it was encoded.
966 *
967 * @param enc the encoding
968 * @param enclen number of characters in 'enc' (without 0-terminator, which can be missing)
969 * @param out location where to store the decoded data
970 * @param out_size sizeof the output buffer
971 * @return GNUNET_OK on success, GNUNET_SYSERR if result has the wrong encoding
972 */
973int
974GNUNET_CRYPTO_string_to_data (const char *enc, size_t enclen,
975 unsigned char *out, size_t out_size);
976 944
977#if 0 /* keep Emacsens' auto-indent happy */ 945#if 0 /* keep Emacsens' auto-indent happy */
978{ 946{
diff --git a/src/include/gnunet_strings_lib.h b/src/include/gnunet_strings_lib.h
index 8101a8113..4fb007707 100644
--- a/src/include/gnunet_strings_lib.h
+++ b/src/include/gnunet_strings_lib.h
@@ -213,6 +213,42 @@ GNUNET_STRINGS_relative_time_to_string (struct GNUNET_TIME_Relative delta);
213const char * 213const char *
214GNUNET_STRINGS_get_short_name (const char *filename); 214GNUNET_STRINGS_get_short_name (const char *filename);
215 215
216
217/**
218 * Convert binary data to ASCII encoding. The ASCII encoding is rather
219 * GNUnet specific. It was chosen such that it only uses characters
220 * in [0-9A-V], can be produced without complex arithmetics and uses a
221 * small number of characters. The GNUnet encoding uses 103 characters.
222 * Does not append 0-terminator, but returns a pointer to the place where
223 * it should be placed, if needed.
224 *
225 * @param data data to encode
226 * @param size size of data (in bytes)
227 * @param out buffer to fill
228 * @param out_size size of the buffer. Must be large enough to hold
229 * ((size*8) + (((size*8) % 5) > 0 ? 5 - ((size*8) % 5) : 0)) / 5
230 * @return pointer to the next byte in 'out' or NULL on error.
231 */
232char *
233GNUNET_STRINGS_data_to_string (unsigned char *data, size_t size,
234 char *out, size_t out_size);
235
236
237/**
238 * Convert ASCII encoding back to data
239 * out_size must match exactly the size of the data before it was encoded.
240 *
241 * @param enc the encoding
242 * @param enclen number of characters in 'enc' (without 0-terminator, which can be missing)
243 * @param out location where to store the decoded data
244 * @param out_size sizeof the output buffer
245 * @return GNUNET_OK on success, GNUNET_SYSERR if result has the wrong encoding
246 */
247int
248GNUNET_STRINGS_string_to_data (const char *enc, size_t enclen,
249 unsigned char *out, size_t out_size);
250
251
216#if 0 /* keep Emacsens' auto-indent happy */ 252#if 0 /* keep Emacsens' auto-indent happy */
217{ 253{
218#endif 254#endif
diff --git a/src/util/crypto_hash.c b/src/util/crypto_hash.c
index 572586b34..f89639045 100644
--- a/src/util/crypto_hash.c
+++ b/src/util/crypto_hash.c
@@ -243,6 +243,8 @@ GNUNET_CRYPTO_hash_file_cancel (struct GNUNET_CRYPTO_FileHashContext *fhc)
243 243
244/* ***************** binary-ASCII encoding *************** */ 244/* ***************** binary-ASCII encoding *************** */
245 245
246/* FIXME: should use GNUNET_STRINGS_data_to_string and strings_to_data below!!! */
247
246/** 248/**
247 * Get the numeric value corresponding to a character. 249 * Get the numeric value corresponding to a character.
248 * 250 *
@@ -261,124 +263,6 @@ getValue__ (unsigned char a)
261 263
262 264
263/** 265/**
264 * Convert binary data to ASCII encoding. The ASCII encoding is rather
265 * GNUnet specific. It was chosen such that it only uses characters
266 * in [0-9A-V], can be produced without complex arithmetics and uses a
267 * small number of characters.
268 * Does not append 0-terminator, but returns a pointer to the place where
269 * it should be placed, if needed.
270 *
271 * @param data data to encode
272 * @param size size of data (in bytes)
273 * @param out buffer to fill
274 * @param out_size size of the buffer. Must be large enough to hold
275 * ((size*8) + (((size*8) % 5) > 0 ? 5 - ((size*8) % 5) : 0)) / 5 bytes
276 * @return pointer to the next byte in 'out' or NULL on error.
277 */
278char *
279GNUNET_CRYPTO_data_to_string (unsigned char *data, size_t size, char *out, size_t out_size)
280{
281 /**
282 * 32 characters for encoding (GNUNET_CRYPTO_hash => 32 characters)
283 */
284 static char *encTable__ = "0123456789ABCDEFGHIJKLMNOPQRSTUV";
285 unsigned int wpos;
286 unsigned int rpos;
287 unsigned int bits;
288 unsigned int vbit;
289
290 GNUNET_assert (data != NULL);
291 GNUNET_assert (out != NULL);
292 GNUNET_assert (out_size >= (((size*8) + ((size*8) % 5)) % 5));
293 vbit = 0;
294 wpos = 0;
295 rpos = 0;
296 bits = 0;
297 while ((rpos < size) || (vbit > 0))
298 {
299 if ((rpos < size) && (vbit < 5))
300 {
301 bits = (bits << 8) | data[rpos++]; /* eat 8 more bits */
302 vbit += 8;
303 }
304 if (vbit < 5)
305 {
306 bits <<= (5 - vbit); /* zero-padding */
307 GNUNET_assert (vbit == ((size * 8) % 5));
308 vbit = 5;
309 }
310 if (wpos >= out_size)
311 return NULL;
312 out[wpos++] = encTable__[(bits >> (vbit - 5)) & 31];
313 vbit -= 5;
314 }
315 if (wpos != out_size)
316 return NULL;
317 GNUNET_assert (vbit == 0);
318 return &out[wpos];
319}
320
321
322/**
323 * Convert ASCII encoding back to data
324 * out_size must match exactly the size of the data before it was encoded.
325 *
326 * @param enc the encoding
327 * @param enclen number of characters in 'enc' (without 0-terminator, which can be missing)
328 * @param out location where to store the decoded data
329 * @param out_size sizeof the output buffer
330 * @return GNUNET_OK on success, GNUNET_SYSERR if result has the wrong encoding
331 */
332int
333GNUNET_CRYPTO_string_to_data (const char *enc, size_t enclen,
334 unsigned char *out, size_t out_size)
335{
336 unsigned int rpos;
337 unsigned int wpos;
338 unsigned int bits;
339 unsigned int vbit;
340 int ret;
341 int shift;
342 int encoded_len = out_size * 8;
343 if (encoded_len % 5 > 0)
344 {
345 vbit = encoded_len % 5; /* padding! */
346 shift = 5 - vbit;
347 }
348 else
349 {
350 vbit = 0;
351 shift = 0;
352 }
353 if ((encoded_len + shift) / 5 != enclen)
354 return GNUNET_SYSERR;
355
356 wpos = out_size;
357 rpos = enclen;
358 bits = (ret = getValue__ (enc[--rpos])) >> (5 - encoded_len % 5);
359 if (-1 == ret)
360 return GNUNET_SYSERR;
361 while (wpos > 0)
362 {
363 GNUNET_assert (rpos > 0);
364 bits = ((ret = getValue__ (enc[--rpos])) << vbit) | bits;
365 if (-1 == ret)
366 return GNUNET_SYSERR;
367 vbit += 5;
368 if (vbit >= 8)
369 {
370 out[--wpos] = (unsigned char) bits;
371 bits >>= 8;
372 vbit -= 8;
373 }
374 }
375 GNUNET_assert (rpos == 0);
376 GNUNET_assert (vbit == 0);
377 return GNUNET_OK;
378}
379
380
381/**
382 * Convert GNUNET_CRYPTO_hash to ASCII encoding. The ASCII encoding is rather 266 * Convert GNUNET_CRYPTO_hash to ASCII encoding. The ASCII encoding is rather
383 * GNUnet specific. It was chosen such that it only uses characters 267 * GNUnet specific. It was chosen such that it only uses characters
384 * in [0-9A-V], can be produced without complex arithmetics and uses a 268 * in [0-9A-V], can be produced without complex arithmetics and uses a
diff --git a/src/util/crypto_rsa.c b/src/util/crypto_rsa.c
index 89351f280..9044c3019 100644
--- a/src/util/crypto_rsa.c
+++ b/src/util/crypto_rsa.c
@@ -231,7 +231,7 @@ GNUNET_CRYPTO_rsa_public_key_to_string (struct GNUNET_CRYPTO_RsaPublicKeyBinaryE
231 keylen += 5 - keylen % 5; 231 keylen += 5 - keylen % 5;
232 keylen /= 5; 232 keylen /= 5;
233 pubkeybuf = GNUNET_malloc (keylen + 1); 233 pubkeybuf = GNUNET_malloc (keylen + 1);
234 end = GNUNET_CRYPTO_data_to_string ((unsigned char *) &pub, 234 end = GNUNET_STRINGS_data_to_string ((unsigned char *) &pub,
235 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), 235 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
236 pubkeybuf, 236 pubkeybuf,
237 keylen); 237 keylen);
@@ -266,7 +266,7 @@ GNUNET_CRYPTO_rsa_public_key_from_string (const char *enc,
266 if (enclen != keylen) 266 if (enclen != keylen)
267 return GNUNET_SYSERR; 267 return GNUNET_SYSERR;
268 268
269 if (GNUNET_OK != GNUNET_CRYPTO_string_to_data (enc, enclen, 269 if (GNUNET_OK != GNUNET_STRINGS_string_to_data (enc, enclen,
270 (unsigned char*) pub, 270 (unsigned char*) pub,
271 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded))) 271 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded)))
272 return GNUNET_SYSERR; 272 return GNUNET_SYSERR;
diff --git a/src/util/strings.c b/src/util/strings.c
index 8000a93fb..2b8b955c8 100644
--- a/src/util/strings.c
+++ b/src/util/strings.c
@@ -630,4 +630,141 @@ GNUNET_STRINGS_get_short_name (const char *filename)
630 return short_fn; 630 return short_fn;
631} 631}
632 632
633
634/**
635 * Get the numeric value corresponding to a character.
636 *
637 * @param a a character
638 * @return corresponding numeric value
639 */
640static unsigned int
641getValue__ (unsigned char a)
642{
643 if ((a >= '0') && (a <= '9'))
644 return a - '0';
645 if ((a >= 'A') && (a <= 'V'))
646 return (a - 'A' + 10);
647 return -1;
648}
649
650
651/**
652 * Convert binary data to ASCII encoding. The ASCII encoding is rather
653 * GNUnet specific. It was chosen such that it only uses characters
654 * in [0-9A-V], can be produced without complex arithmetics and uses a
655 * small number of characters.
656 * Does not append 0-terminator, but returns a pointer to the place where
657 * it should be placed, if needed.
658 *
659 * @param data data to encode
660 * @param size size of data (in bytes)
661 * @param out buffer to fill
662 * @param out_size size of the buffer. Must be large enough to hold
663 * ((size*8) + (((size*8) % 5) > 0 ? 5 - ((size*8) % 5) : 0)) / 5 bytes
664 * @return pointer to the next byte in 'out' or NULL on error.
665 */
666char *
667GNUNET_STRINGS_data_to_string (unsigned char *data, size_t size, char *out, size_t out_size)
668{
669 /**
670 * 32 characters for encoding (GNUNET_CRYPTO_hash => 32 characters)
671 */
672 static char *encTable__ = "0123456789ABCDEFGHIJKLMNOPQRSTUV";
673 unsigned int wpos;
674 unsigned int rpos;
675 unsigned int bits;
676 unsigned int vbit;
677
678 GNUNET_assert (data != NULL);
679 GNUNET_assert (out != NULL);
680 GNUNET_assert (out_size >= (((size*8) + ((size*8) % 5)) % 5));
681 vbit = 0;
682 wpos = 0;
683 rpos = 0;
684 bits = 0;
685 while ((rpos < size) || (vbit > 0))
686 {
687 if ((rpos < size) && (vbit < 5))
688 {
689 bits = (bits << 8) | data[rpos++]; /* eat 8 more bits */
690 vbit += 8;
691 }
692 if (vbit < 5)
693 {
694 bits <<= (5 - vbit); /* zero-padding */
695 GNUNET_assert (vbit == ((size * 8) % 5));
696 vbit = 5;
697 }
698 if (wpos >= out_size)
699 return NULL;
700 out[wpos++] = encTable__[(bits >> (vbit - 5)) & 31];
701 vbit -= 5;
702 }
703 if (wpos != out_size)
704 return NULL;
705 GNUNET_assert (vbit == 0);
706 return &out[wpos];
707}
708
709
710/**
711 * Convert ASCII encoding back to data
712 * out_size must match exactly the size of the data before it was encoded.
713 *
714 * @param enc the encoding
715 * @param enclen number of characters in 'enc' (without 0-terminator, which can be missing)
716 * @param out location where to store the decoded data
717 * @param out_size sizeof the output buffer
718 * @return GNUNET_OK on success, GNUNET_SYSERR if result has the wrong encoding
719 */
720int
721GNUNET_STRINGS_string_to_data (const char *enc, size_t enclen,
722 unsigned char *out, size_t out_size)
723{
724 unsigned int rpos;
725 unsigned int wpos;
726 unsigned int bits;
727 unsigned int vbit;
728 int ret;
729 int shift;
730 int encoded_len = out_size * 8;
731 if (encoded_len % 5 > 0)
732 {
733 vbit = encoded_len % 5; /* padding! */
734 shift = 5 - vbit;
735 }
736 else
737 {
738 vbit = 0;
739 shift = 0;
740 }
741 if ((encoded_len + shift) / 5 != enclen)
742 return GNUNET_SYSERR;
743
744 wpos = out_size;
745 rpos = enclen;
746 bits = (ret = getValue__ (enc[--rpos])) >> (5 - encoded_len % 5);
747 if (-1 == ret)
748 return GNUNET_SYSERR;
749 while (wpos > 0)
750 {
751 GNUNET_assert (rpos > 0);
752 bits = ((ret = getValue__ (enc[--rpos])) << vbit) | bits;
753 if (-1 == ret)
754 return GNUNET_SYSERR;
755 vbit += 5;
756 if (vbit >= 8)
757 {
758 out[--wpos] = (unsigned char) bits;
759 bits >>= 8;
760 vbit -= 8;
761 }
762 }
763 GNUNET_assert (rpos == 0);
764 GNUNET_assert (vbit == 0);
765 return GNUNET_OK;
766}
767
768
769
633/* end of strings.c */ 770/* end of strings.c */