[or-cvs] r13532: Replace the hefty tor_strpartition with a simple function to (in tor/trunk: . src/common src/or)
nickm at seul.org
nickm at seul.org
Fri Feb 15 23:39:15 UTC 2008
Author: nickm
Date: 2008-02-15 18:39:14 -0500 (Fri, 15 Feb 2008)
New Revision: 13532
Modified:
tor/trunk/
tor/trunk/ChangeLog
tor/trunk/src/common/crypto.c
tor/trunk/src/common/crypto.h
tor/trunk/src/common/util.c
tor/trunk/src/common/util.h
tor/trunk/src/or/test.c
Log:
r14185 at tombo: nickm | 2008-02-15 18:05:54 -0500
Replace the hefty tor_strpartition with a simple function to replace its only (trivial) use.
Property changes on: tor/trunk
___________________________________________________________________
svk:merge ticket from /tor/trunk [r14185] on 49666b30-7950-49c5-bedf-9dc8f3168102
Modified: tor/trunk/ChangeLog
===================================================================
--- tor/trunk/ChangeLog 2008-02-15 23:39:10 UTC (rev 13531)
+++ tor/trunk/ChangeLog 2008-02-15 23:39:14 UTC (rev 13532)
@@ -18,7 +18,12 @@
- 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
handshake, makes path selection for relays more secure and IP address
Modified: tor/trunk/src/common/crypto.c
===================================================================
--- tor/trunk/src/common/crypto.c 2008-02-15 23:39:10 UTC (rev 13531)
+++ tor/trunk/src/common/crypto.c 2008-02-15 23:39:14 UTC (rev 13532)
@@ -999,6 +999,24 @@
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 @@
}
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);
}
Modified: tor/trunk/src/common/crypto.h
===================================================================
--- tor/trunk/src/common/crypto.h 2008-02-15 23:39:10 UTC (rev 13531)
+++ tor/trunk/src/common/crypto.h 2008-02-15 23:39:14 UTC (rev 13532)
@@ -207,6 +207,7 @@
/* 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
Modified: tor/trunk/src/common/util.c
===================================================================
--- tor/trunk/src/common/util.c 2008-02-15 23:39:10 UTC (rev 13531)
+++ tor/trunk/src/common/util.c 2008-02-15 23:39:14 UTC (rev 13532)
@@ -347,54 +347,6 @@
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
Modified: tor/trunk/src/common/util.h
===================================================================
--- tor/trunk/src/common/util.h 2008-02-15 23:39:10 UTC (rev 13531)
+++ tor/trunk/src/common/util.h 2008-02-15 23:39:14 UTC (rev 13532)
@@ -167,8 +167,6 @@
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,
Modified: tor/trunk/src/or/test.c
===================================================================
--- tor/trunk/src/or/test.c 2008-02-15 23:39:10 UTC (rev 13531)
+++ tor/trunk/src/or/test.c 2008-02-15 23:39:14 UTC (rev 13532)
@@ -624,6 +624,17 @@
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_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));
More information about the tor-commits
mailing list