mirror of
https://gitlab.torproject.org/tpo/core/tor.git
synced 2024-11-27 22:03:31 +01:00
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
This commit is contained in:
parent
f5ed1f8469
commit
24e8e1fb36
@ -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
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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,
|
||||
|
@ -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));
|
||||
|
Loading…
Reference in New Issue
Block a user