summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2006-07-23 05:32:35 +0000
committerNick Mathewson <nickm@torproject.org>2006-07-23 05:32:35 +0000
commit898f7fa1ecfd4f6e6f432da5b7ddd87fe58020b5 (patch)
tree94a94e3017e239f251b36336251c84a514af9996
parent2ff54beca687f97c8a23dd38eeba3a914ba3d1ae (diff)
downloadtor-898f7fa1ecfd4f6e6f432da5b7ddd87fe58020b5.tar.gz
tor-898f7fa1ecfd4f6e6f432da5b7ddd87fe58020b5.zip
Add a mem_is_zero function (I think we will need this) and a STRUCT_OFFSET macro (we already need this).
svn:r6810
-rw-r--r--trunk/src/common/util.c23
-rw-r--r--trunk/src/common/util.h5
2 files changed, 25 insertions, 3 deletions
diff --git a/trunk/src/common/util.c b/trunk/src/common/util.c
index e80a15c649..39efee9cd2 100644
--- a/trunk/src/common/util.c
+++ b/trunk/src/common/util.c
@@ -428,13 +428,30 @@ find_whitespace(const char *s)
return s;
}
+/** Return true iff the 'len' bytes at 'mem' are all zero. */
+int tor_mem_is_zero(const char *mem, size_t len)
+{
+ static const char ZERO[] = {
+ 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
+ };
+ while (len >= sizeof(ZERO)) {
+ if (memcmp(mem, ZERO, sizeof(ZERO)))
+ return 0;
+ len -= sizeof(ZERO);
+ mem += sizeof(ZERO);
+ }
+ /* Deal with leftover bytes. */
+ if (len)
+ return ! memcmp(mem, ZERO, len);
+
+ return 1;
+}
+
/** Return true iff the DIGEST_LEN bytes in digest are all zero. */
int
tor_digest_is_zero(const char *digest)
{
- static char ZERO_DIGEST[] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
-
- return !memcmp(digest, ZERO_DIGEST, DIGEST_LEN);
+ return tor_mem_is_zero(digest, DIGEST_LEN);
}
#define CHECK_STRTOX_RESULT() \
diff --git a/trunk/src/common/util.h b/trunk/src/common/util.h
index b61b399dec..bc21d2051f 100644
--- a/trunk/src/common/util.h
+++ b/trunk/src/common/util.h
@@ -88,6 +88,10 @@ extern int dmalloc_free(const char *file, const int line, void *pnt,
#define tor_strndup(s, n) _tor_strndup(s, n DMALLOC_ARGS)
#define tor_memdup(s, n) _tor_memdup(s, n DMALLOC_ARGS)
+/** Return the offset of <b>member</b> within the type <b>tp</b>, in bytes */
+#define STRUCT_OFFSET(tp, member) \
+ ((off_t) (((char*)&((tp*)0)->member)-(char*)0))
+
/* String manipulation */
#define HEX_CHARACTERS "0123456789ABCDEFabcdef"
void tor_strlower(char *s);
@@ -114,6 +118,7 @@ const char *hex_str(const char *from, size_t fromlen);
const char *eat_whitespace(const char *s);
const char *eat_whitespace_no_nl(const char *s);
const char *find_whitespace(const char *s);
+int tor_mem_is_zero(const char *mem, size_t len);
int tor_digest_is_zero(const char *digest);
char *esc_for_log(const char *string);
const char *escaped(const char *string);