aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2004-10-30 19:26:31 +0000
committerNick Mathewson <nickm@torproject.org>2004-10-30 19:26:31 +0000
commit5bc0dba9338c6722f6833351b1401571533eb63d (patch)
treef20dabe9b36bffce31d8b7b6d0edf7773f04277d /src
parent9510d9a79286d4baf3f3bd8ee69ae5ff422f9735 (diff)
downloadtor-5bc0dba9338c6722f6833351b1401571533eb63d.tar
tor-5bc0dba9338c6722f6833351b1401571533eb63d.tar.gz
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
Diffstat (limited to 'src')
-rw-r--r--src/common/crypto.c63
-rw-r--r--src/common/util.c62
2 files changed, 64 insertions, 61 deletions
diff --git a/src/common/crypto.c b/src/common/crypto.c
index 4fe2ae0e8..e63a92c99 100644
--- a/src/common/crypto.c
+++ b/src/common/crypto.c
@@ -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
* 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
@@ -1540,59 +1550,6 @@ base32_encode(char *dest, size_t destlen, const char *src, size_t srclen)
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:
diff --git a/src/common/util.c b/src/common/util.c
index 3aa6d0bdd..2d7ebbe68 100644
--- a/src/common/util.c
+++ b/src/common/util.c
@@ -476,14 +476,6 @@ void smartlist_subtract(smartlist_t *sl1, const smartlist_t *sl2) {
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.
*/
void *smartlist_get(const smartlist_t *sl, int idx)
@@ -2402,6 +2394,60 @@ void tor_mutex_release(tor_mutex_t *m)
#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:
mode:c