From 87534f72154e9bc859919afaebbc7321dc4fda87 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Sun, 4 Mar 2012 14:09:57 +0000 Subject: [PATCH] -renaming --- src/include/gnunet_crypto_lib.h | 32 -------- src/include/gnunet_strings_lib.h | 36 ++++++++ src/util/crypto_hash.c | 120 +-------------------------- src/util/crypto_rsa.c | 4 +- src/util/strings.c | 137 +++++++++++++++++++++++++++++++ 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, void GNUNET_CRYPTO_random_disable_entropy_gathering (void); -/** - * Convert binary data to ASCII encoding. The ASCII encoding is rather - * GNUnet specific. It was chosen such that it only uses characters - * in [0-9A-V], can be produced without complex arithmetics and uses a - * small number of characters. The GNUnet encoding uses 103 characters. - * Does not append 0-terminator, but returns a pointer to the place where - * it should be placed, if needed. - * - * @param data data to encode - * @param size size of data (in bytes) - * @param out buffer to fill - * @param out_size size of the buffer. Must be large enough to hold - * ((size*8) + (((size*8) % 5) > 0 ? 5 - ((size*8) % 5) : 0)) / 5 - * @return pointer to the next byte in 'out' or NULL on error. - */ -char * -GNUNET_CRYPTO_data_to_string (unsigned char *data, size_t size, - char *out, size_t out_size); - -/** - * Convert ASCII encoding back to data - * out_size must match exactly the size of the data before it was encoded. - * - * @param enc the encoding - * @param enclen number of characters in 'enc' (without 0-terminator, which can be missing) - * @param out location where to store the decoded data - * @param out_size sizeof the output buffer - * @return GNUNET_OK on success, GNUNET_SYSERR if result has the wrong encoding - */ -int -GNUNET_CRYPTO_string_to_data (const char *enc, size_t enclen, - unsigned char *out, size_t out_size); #if 0 /* keep Emacsens' auto-indent happy */ { 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); const char * GNUNET_STRINGS_get_short_name (const char *filename); + +/** + * Convert binary data to ASCII encoding. The ASCII encoding is rather + * GNUnet specific. It was chosen such that it only uses characters + * in [0-9A-V], can be produced without complex arithmetics and uses a + * small number of characters. The GNUnet encoding uses 103 characters. + * Does not append 0-terminator, but returns a pointer to the place where + * it should be placed, if needed. + * + * @param data data to encode + * @param size size of data (in bytes) + * @param out buffer to fill + * @param out_size size of the buffer. Must be large enough to hold + * ((size*8) + (((size*8) % 5) > 0 ? 5 - ((size*8) % 5) : 0)) / 5 + * @return pointer to the next byte in 'out' or NULL on error. + */ +char * +GNUNET_STRINGS_data_to_string (unsigned char *data, size_t size, + char *out, size_t out_size); + + +/** + * Convert ASCII encoding back to data + * out_size must match exactly the size of the data before it was encoded. + * + * @param enc the encoding + * @param enclen number of characters in 'enc' (without 0-terminator, which can be missing) + * @param out location where to store the decoded data + * @param out_size sizeof the output buffer + * @return GNUNET_OK on success, GNUNET_SYSERR if result has the wrong encoding + */ +int +GNUNET_STRINGS_string_to_data (const char *enc, size_t enclen, + unsigned char *out, size_t out_size); + + #if 0 /* keep Emacsens' auto-indent happy */ { #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) /* ***************** binary-ASCII encoding *************** */ +/* FIXME: should use GNUNET_STRINGS_data_to_string and strings_to_data below!!! */ + /** * Get the numeric value corresponding to a character. * @@ -260,124 +262,6 @@ getValue__ (unsigned char a) } -/** - * Convert binary data to ASCII encoding. The ASCII encoding is rather - * GNUnet specific. It was chosen such that it only uses characters - * in [0-9A-V], can be produced without complex arithmetics and uses a - * small number of characters. - * Does not append 0-terminator, but returns a pointer to the place where - * it should be placed, if needed. - * - * @param data data to encode - * @param size size of data (in bytes) - * @param out buffer to fill - * @param out_size size of the buffer. Must be large enough to hold - * ((size*8) + (((size*8) % 5) > 0 ? 5 - ((size*8) % 5) : 0)) / 5 bytes - * @return pointer to the next byte in 'out' or NULL on error. - */ -char * -GNUNET_CRYPTO_data_to_string (unsigned char *data, size_t size, char *out, size_t out_size) -{ - /** - * 32 characters for encoding (GNUNET_CRYPTO_hash => 32 characters) - */ - static char *encTable__ = "0123456789ABCDEFGHIJKLMNOPQRSTUV"; - unsigned int wpos; - unsigned int rpos; - unsigned int bits; - unsigned int vbit; - - GNUNET_assert (data != NULL); - GNUNET_assert (out != NULL); - GNUNET_assert (out_size >= (((size*8) + ((size*8) % 5)) % 5)); - vbit = 0; - wpos = 0; - rpos = 0; - bits = 0; - while ((rpos < size) || (vbit > 0)) - { - if ((rpos < size) && (vbit < 5)) - { - bits = (bits << 8) | data[rpos++]; /* eat 8 more bits */ - vbit += 8; - } - if (vbit < 5) - { - bits <<= (5 - vbit); /* zero-padding */ - GNUNET_assert (vbit == ((size * 8) % 5)); - vbit = 5; - } - if (wpos >= out_size) - return NULL; - out[wpos++] = encTable__[(bits >> (vbit - 5)) & 31]; - vbit -= 5; - } - if (wpos != out_size) - return NULL; - GNUNET_assert (vbit == 0); - return &out[wpos]; -} - - -/** - * Convert ASCII encoding back to data - * out_size must match exactly the size of the data before it was encoded. - * - * @param enc the encoding - * @param enclen number of characters in 'enc' (without 0-terminator, which can be missing) - * @param out location where to store the decoded data - * @param out_size sizeof the output buffer - * @return GNUNET_OK on success, GNUNET_SYSERR if result has the wrong encoding - */ -int -GNUNET_CRYPTO_string_to_data (const char *enc, size_t enclen, - unsigned char *out, size_t out_size) -{ - unsigned int rpos; - unsigned int wpos; - unsigned int bits; - unsigned int vbit; - int ret; - int shift; - int encoded_len = out_size * 8; - if (encoded_len % 5 > 0) - { - vbit = encoded_len % 5; /* padding! */ - shift = 5 - vbit; - } - else - { - vbit = 0; - shift = 0; - } - if ((encoded_len + shift) / 5 != enclen) - return GNUNET_SYSERR; - - wpos = out_size; - rpos = enclen; - bits = (ret = getValue__ (enc[--rpos])) >> (5 - encoded_len % 5); - if (-1 == ret) - return GNUNET_SYSERR; - while (wpos > 0) - { - GNUNET_assert (rpos > 0); - bits = ((ret = getValue__ (enc[--rpos])) << vbit) | bits; - if (-1 == ret) - return GNUNET_SYSERR; - vbit += 5; - if (vbit >= 8) - { - out[--wpos] = (unsigned char) bits; - bits >>= 8; - vbit -= 8; - } - } - GNUNET_assert (rpos == 0); - GNUNET_assert (vbit == 0); - return GNUNET_OK; -} - - /** * Convert GNUNET_CRYPTO_hash to ASCII encoding. The ASCII encoding is rather * GNUnet specific. It was chosen such that it only uses characters 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 keylen += 5 - keylen % 5; keylen /= 5; pubkeybuf = GNUNET_malloc (keylen + 1); - end = GNUNET_CRYPTO_data_to_string ((unsigned char *) &pub, + end = GNUNET_STRINGS_data_to_string ((unsigned char *) &pub, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), pubkeybuf, keylen); @@ -266,7 +266,7 @@ GNUNET_CRYPTO_rsa_public_key_from_string (const char *enc, if (enclen != keylen) return GNUNET_SYSERR; - if (GNUNET_OK != GNUNET_CRYPTO_string_to_data (enc, enclen, + if (GNUNET_OK != GNUNET_STRINGS_string_to_data (enc, enclen, (unsigned char*) pub, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded))) 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) return short_fn; } + +/** + * Get the numeric value corresponding to a character. + * + * @param a a character + * @return corresponding numeric value + */ +static unsigned int +getValue__ (unsigned char a) +{ + if ((a >= '0') && (a <= '9')) + return a - '0'; + if ((a >= 'A') && (a <= 'V')) + return (a - 'A' + 10); + return -1; +} + + +/** + * Convert binary data to ASCII encoding. The ASCII encoding is rather + * GNUnet specific. It was chosen such that it only uses characters + * in [0-9A-V], can be produced without complex arithmetics and uses a + * small number of characters. + * Does not append 0-terminator, but returns a pointer to the place where + * it should be placed, if needed. + * + * @param data data to encode + * @param size size of data (in bytes) + * @param out buffer to fill + * @param out_size size of the buffer. Must be large enough to hold + * ((size*8) + (((size*8) % 5) > 0 ? 5 - ((size*8) % 5) : 0)) / 5 bytes + * @return pointer to the next byte in 'out' or NULL on error. + */ +char * +GNUNET_STRINGS_data_to_string (unsigned char *data, size_t size, char *out, size_t out_size) +{ + /** + * 32 characters for encoding (GNUNET_CRYPTO_hash => 32 characters) + */ + static char *encTable__ = "0123456789ABCDEFGHIJKLMNOPQRSTUV"; + unsigned int wpos; + unsigned int rpos; + unsigned int bits; + unsigned int vbit; + + GNUNET_assert (data != NULL); + GNUNET_assert (out != NULL); + GNUNET_assert (out_size >= (((size*8) + ((size*8) % 5)) % 5)); + vbit = 0; + wpos = 0; + rpos = 0; + bits = 0; + while ((rpos < size) || (vbit > 0)) + { + if ((rpos < size) && (vbit < 5)) + { + bits = (bits << 8) | data[rpos++]; /* eat 8 more bits */ + vbit += 8; + } + if (vbit < 5) + { + bits <<= (5 - vbit); /* zero-padding */ + GNUNET_assert (vbit == ((size * 8) % 5)); + vbit = 5; + } + if (wpos >= out_size) + return NULL; + out[wpos++] = encTable__[(bits >> (vbit - 5)) & 31]; + vbit -= 5; + } + if (wpos != out_size) + return NULL; + GNUNET_assert (vbit == 0); + return &out[wpos]; +} + + +/** + * Convert ASCII encoding back to data + * out_size must match exactly the size of the data before it was encoded. + * + * @param enc the encoding + * @param enclen number of characters in 'enc' (without 0-terminator, which can be missing) + * @param out location where to store the decoded data + * @param out_size sizeof the output buffer + * @return GNUNET_OK on success, GNUNET_SYSERR if result has the wrong encoding + */ +int +GNUNET_STRINGS_string_to_data (const char *enc, size_t enclen, + unsigned char *out, size_t out_size) +{ + unsigned int rpos; + unsigned int wpos; + unsigned int bits; + unsigned int vbit; + int ret; + int shift; + int encoded_len = out_size * 8; + if (encoded_len % 5 > 0) + { + vbit = encoded_len % 5; /* padding! */ + shift = 5 - vbit; + } + else + { + vbit = 0; + shift = 0; + } + if ((encoded_len + shift) / 5 != enclen) + return GNUNET_SYSERR; + + wpos = out_size; + rpos = enclen; + bits = (ret = getValue__ (enc[--rpos])) >> (5 - encoded_len % 5); + if (-1 == ret) + return GNUNET_SYSERR; + while (wpos > 0) + { + GNUNET_assert (rpos > 0); + bits = ((ret = getValue__ (enc[--rpos])) << vbit) | bits; + if (-1 == ret) + return GNUNET_SYSERR; + vbit += 5; + if (vbit >= 8) + { + out[--wpos] = (unsigned char) bits; + bits >>= 8; + vbit -= 8; + } + } + GNUNET_assert (rpos == 0); + GNUNET_assert (vbit == 0); + return GNUNET_OK; +} + + + /* end of strings.c */ -- 2.25.1