Move all util functions that need openssl into crypto.c; make non-openssl functions that util needs into util. Now openssl can be separated.

svn:r2628
This commit is contained in:
Nick Mathewson 2004-10-30 19:26:31 +00:00
parent 9510d9a792
commit 5bc0dba933
2 changed files with 64 additions and 61 deletions

View File

@ -1470,6 +1470,16 @@ int crypto_pseudo_rand_int(unsigned int max) {
} }
} }
/** Return a randomly chosen element of sl; or NULL if sl is empty.
*/
void *smartlist_choose(const smartlist_t *sl) {
size_t len;
len = smartlist_len(sl);
if(len)
return smartlist_get(sl,crypto_pseudo_rand_int(len));
return NULL; /* no elements to choose from */
}
/** Base-64 encode <b>srclen</b> bytes of data from <b>src</b>. Write /** Base-64 encode <b>srclen</b> bytes of data from <b>src</b>. Write
* the result into <b>dest</b>, if it will fit within <b>destlen</b> * the result into <b>dest</b>, if it will fit within <b>destlen</b>
* bytes. Return the number of bytes written on success; -1 if * bytes. Return the number of bytes written on success; -1 if
@ -1540,59 +1550,6 @@ base32_encode(char *dest, size_t destlen, const char *src, size_t srclen)
dest[i] = '\0'; dest[i] = '\0';
} }
void base16_encode(char *dest, size_t destlen, const char *src, size_t srclen)
{
const char *end;
char *cp;
tor_assert(destlen >= srclen*2+1);
cp = dest;
end = src+srclen;
while (src<end) {
sprintf(cp,"%02X",*(const uint8_t*)src);
++src;
cp += 2;
}
*cp = '\0';
}
static const char HEX_DIGITS[] = "0123456789ABCDEFabcdef";
static INLINE int hex_decode_digit(char c)
{
const char *cp;
int n;
cp = strchr(HEX_DIGITS, c);
if (!cp)
return -1;
n = cp-HEX_DIGITS;
if (n<=15)
return n; /* digit or uppercase */
else
return n-6; /* lowercase */
}
int base16_decode(char *dest, size_t destlen, const char *src, size_t srclen)
{
const char *end;
int v1,v2;
if ((srclen % 2) != 0)
return -1;
if (destlen < srclen/2)
return -1;
end = src+srclen;
while (src<end) {
v1 = hex_decode_digit(*src);
v2 = hex_decode_digit(*(src+1));
if(v1<0||v2<0)
return -1;
*(uint8_t*)dest = (v1<<4)|v2;
++dest;
src+=2;
}
return 0;
}
/* /*
Local Variables: Local Variables:

View File

@ -476,14 +476,6 @@ void smartlist_subtract(smartlist_t *sl1, const smartlist_t *sl2) {
smartlist_remove(sl1, sl2->list[i]); smartlist_remove(sl1, sl2->list[i]);
} }
/** Return a randomly chosen element of sl; or NULL if sl is empty.
*/
void *smartlist_choose(const smartlist_t *sl) {
if(sl->num_used)
return sl->list[crypto_pseudo_rand_int(sl->num_used)];
return NULL; /* no elements to choose from */
}
/** Return the <b>idx</b>th element of sl. /** Return the <b>idx</b>th element of sl.
*/ */
void *smartlist_get(const smartlist_t *sl, int idx) void *smartlist_get(const smartlist_t *sl, int idx)
@ -2402,6 +2394,60 @@ void tor_mutex_release(tor_mutex_t *m)
#endif #endif
void base16_encode(char *dest, size_t destlen, const char *src, size_t srclen)
{
const char *end;
char *cp;
tor_assert(destlen >= srclen*2+1);
cp = dest;
end = src+srclen;
while (src<end) {
sprintf(cp,"%02X",*(const uint8_t*)src);
++src;
cp += 2;
}
*cp = '\0';
}
static const char HEX_DIGITS[] = "0123456789ABCDEFabcdef";
static INLINE int hex_decode_digit(char c)
{
const char *cp;
int n;
cp = strchr(HEX_DIGITS, c);
if (!cp)
return -1;
n = cp-HEX_DIGITS;
if (n<=15)
return n; /* digit or uppercase */
else
return n-6; /* lowercase */
}
int base16_decode(char *dest, size_t destlen, const char *src, size_t srclen)
{
const char *end;
int v1,v2;
if ((srclen % 2) != 0)
return -1;
if (destlen < srclen/2)
return -1;
end = src+srclen;
while (src<end) {
v1 = hex_decode_digit(*src);
v2 = hex_decode_digit(*(src+1));
if(v1<0||v2<0)
return -1;
*(uint8_t*)dest = (v1<<4)|v2;
++dest;
src+=2;
}
return 0;
}
/* /*
Local Variables: Local Variables:
mode:c mode:c