From 24e8e1fb36c3fa6319b00dff8b1db7feeb214905 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Fri, 15 Feb 2008 23:39:14 +0000 Subject: [PATCH] r14185@tombo: nickm | 2008-02-15 18:05:54 -0500 Replace the hefty tor_strpartition with a simple function to replace its only (trivial) use. svn:r13532 --- ChangeLog | 5 +++++ src/common/crypto.c | 21 ++++++++++++++++++-- src/common/crypto.h | 1 + src/common/util.c | 48 --------------------------------------------- src/common/util.h | 2 -- src/or/test.c | 15 ++++++++++---- 6 files changed, 36 insertions(+), 56 deletions(-) diff --git a/ChangeLog b/ChangeLog index c478676e04..209d100ae9 100644 --- a/ChangeLog +++ b/ChangeLog @@ -18,6 +18,11 @@ Changes in version 0.2.0.20-?? - 2008-02-?? - Have the new hidden service code respect the SafeLogging setting. Bugfix on 0.2.0.x. Patch from Karsten. + o Code simplifications and refactoring: + - Remove the tor_strpartition function: its logic was confused, + and it was only used for one thing that could be implemented far + more easily. + Changes in version 0.2.0.19-alpha - 2008-02-09 Tor 0.2.0.19-alpha makes more progress towards normalizing Tor's TLS diff --git a/src/common/crypto.c b/src/common/crypto.c index cd10e649c4..a00d07465d 100644 --- a/src/common/crypto.c +++ b/src/common/crypto.c @@ -999,6 +999,24 @@ crypto_pk_get_digest(crypto_pk_env_t *pk, char *digest_out) return 0; } +/** Copy in to the outlen-byte buffer out, adding spaces + * every four spaces. */ +/* static */ void +add_spaces_to_fp(char *out, size_t outlen, const char *in) +{ + int n = 0; + char *end = out+outlen; + while (*in && outpk, put a fingerprint of the * public key into fp_out (must have at least FINGERPRINT_LEN+1 bytes of * space). Return 0 on success, -1 on failure. @@ -1019,8 +1037,7 @@ crypto_pk_get_fingerprint(crypto_pk_env_t *pk, char *fp_out, int add_space) } base16_encode(hexdigest,sizeof(hexdigest),digest,DIGEST_LEN); if (add_space) { - if (tor_strpartition(fp_out, FINGERPRINT_LEN+1, hexdigest, " ", 4)<0) - return -1; + add_spaces_to_fp(fp_out, FINGERPRINT_LEN+1, hexdigest); } else { strncpy(fp_out, hexdigest, HEX_DIGEST_LEN+1); } diff --git a/src/common/crypto.h b/src/common/crypto.h index aa5cf920c4..bc47813889 100644 --- a/src/common/crypto.h +++ b/src/common/crypto.h @@ -207,6 +207,7 @@ struct dh_st *_crypto_dh_env_get_dh(crypto_dh_env_t *dh); /* Prototypes for private functions only used by crypto.c and test.c*/ int crypto_pk_read_private_key_from_string(crypto_pk_env_t *env, const char *s); +void add_spaces_to_fp(char *out, size_t outlen, const char *in); #endif #endif diff --git a/src/common/util.c b/src/common/util.c index 8d1d08a8be..64464d953f 100644 --- a/src/common/util.c +++ b/src/common/util.c @@ -347,54 +347,6 @@ tor_strstrip(char *s, const char *strip) return read-s; } -/** Set the dest_len-byte buffer buf to contain the - * string s, with the string insert inserted after every - * n characters. Return 0 on success, -1 on failure. - * - * Never end the string with insert, even if its length is a - * multiple of n. - */ -int -tor_strpartition(char *dest, size_t dest_len, - const char *s, const char *insert, size_t n) -{ - char *destp; - size_t len_in, len_out, len_ins; - int is_even, remaining; - tor_assert(s); - tor_assert(insert); - tor_assert(n > 0); - tor_assert(n < SIZE_T_CEILING); - tor_assert(dest_len < SIZE_T_CEILING); - len_in = strlen(s); - len_ins = strlen(insert); - tor_assert(len_in < SIZE_T_CEILING); - tor_assert(len_in/n < SIZE_T_CEILING/len_ins); /* avoid overflow */ - len_out = len_in + (len_in/n)*len_ins; - is_even = (len_in%n) == 0; - if (is_even && len_in) - len_out -= len_ins; - if (dest_len < len_out+1) - return -1; - destp = dest; - remaining = len_in; - while (remaining) { - strncpy(destp, s, n); - remaining -= n; - if (remaining < 0) { - break; - } else if (remaining == 0) { - *(destp+n) = '\0'; - break; - } - strncpy(destp+n, insert, len_ins+1); - s += n; - destp += n+len_ins; - } - tor_assert(len_out == strlen(dest)); - return 0; -} - /** Return a pointer to a NUL-terminated hexadecimal string encoding * the first fromlen bytes of from. (fromlen must be \<= 32.) The * result does not need to be deallocated, but repeated calls to diff --git a/src/common/util.h b/src/common/util.h index 1ca6dd9b61..14638b29a4 100644 --- a/src/common/util.h +++ b/src/common/util.h @@ -167,8 +167,6 @@ int strcmpend(const char *s1, const char *s2) ATTR_PURE ATTR_NONNULL((1,2)); int strcasecmpend(const char *s1, const char *s2) ATTR_PURE ATTR_NONNULL((1,2)); int tor_strstrip(char *s, const char *strip) ATTR_NONNULL((1,2)); -int tor_strpartition(char *dest, size_t dest_len, - const char *s, const char *insert, size_t n); long tor_parse_long(const char *s, int base, long min, long max, int *ok, char **next); unsigned long tor_parse_ulong(const char *s, int base, unsigned long min, diff --git a/src/or/test.c b/src/or/test.c index 18d46d21ab..ffaf29097f 100644 --- a/src/or/test.c +++ b/src/or/test.c @@ -624,6 +624,17 @@ test_crypto(void) tor_free(data1); tor_free(data2); tor_free(data3); + + /* Add spaces to fingerprint */ + { + data1 = tor_strdup("ABCD1234ABCD56780000ABCD1234ABCD56780000"); + test_eq(strlen(data1), 40); + data2 = tor_malloc(FINGERPRINT_LEN+1); + add_spaces_to_fp(data2, FINGERPRINT_LEN+1, data1); + test_streq(data2, "ABCD 1234 ABCD 5678 0000 ABCD 1234 ABCD 5678 0000"); + tor_free(data1); + tor_free(data2); + } } static void @@ -758,10 +769,6 @@ test_util(void) test_eq(5, tor_strstrip(buf, "!? ")); test_streq(buf, "Testing123"); - /* Test tor_strpartition() */ - test_assert(! tor_strpartition(buf, sizeof(buf), "abcdefghi", "##", 3)); - test_streq(buf, "abc##def##ghi"); - /* Test parse_addr_port */ cp = NULL; u32 = 3; u16 = 3; test_assert(!parse_addr_port(LOG_WARN, "1.2.3.4", &cp, &u32, &u16));