aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2008-02-15 23:39:14 +0000
committerNick Mathewson <nickm@torproject.org>2008-02-15 23:39:14 +0000
commit24e8e1fb36c3fa6319b00dff8b1db7feeb214905 (patch)
treeb9d908a734052bfd47c3107ae24e5b180b87220c /src
parentf5ed1f8469d28879a5efbf2a0ccad5766019bcbb (diff)
downloadtor-24e8e1fb36c3fa6319b00dff8b1db7feeb214905.tar.gz
tor-24e8e1fb36c3fa6319b00dff8b1db7feeb214905.zip
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
Diffstat (limited to 'src')
-rw-r--r--src/common/crypto.c21
-rw-r--r--src/common/crypto.h1
-rw-r--r--src/common/util.c48
-rw-r--r--src/common/util.h2
-rw-r--r--src/or/test.c15
5 files changed, 31 insertions, 56 deletions
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 <b>in</b> to the <b>outlen</b>-byte buffer <b>out</b>, 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 && out<end) {
+ *out++ = *in++;
+ if (++n == 4 && *in && out<end) {
+ n = 0;
+ *out++ = ' ';
+ }
+ }
+ tor_assert(out<end);
+ *out = '\0';
+}
+
/** Given a private or public key <b>pk</b>, put a fingerprint of the
* public key into <b>fp_out</b> (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 <b>dest_len</b>-byte buffer <b>buf</b> to contain the
- * string <b>s</b>, with the string <b>insert</b> inserted after every
- * <b>n</b> characters. Return 0 on success, -1 on failure.
- *
- * Never end the string with <b>insert</b>, even if its length <i>is</i> a
- * multiple of <b>n</b>.
- */
-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 <b>fromlen</b> bytes of <b>from</b>. (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));