summaryrefslogtreecommitdiff
path: root/src/or/buffers.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/or/buffers.c')
-rw-r--r--src/or/buffers.c26
1 files changed, 24 insertions, 2 deletions
diff --git a/src/or/buffers.c b/src/or/buffers.c
index 30b476e200..5b4053c90d 100644
--- a/src/or/buffers.c
+++ b/src/or/buffers.c
@@ -129,9 +129,9 @@ int write_to_buf(char *string, int string_len,
}
int fetch_from_buf(char *string, int string_len,
- char **buf, int *buflen, int *buf_datalen) {
+ char **buf, int *buflen, int *buf_datalen) {
- /* if there is string_len bytes in buf, write them onto string,
+ /* if there are string_len bytes in buf, write them onto string,
* then memmove buf back (that is, remove them from buf) */
assert(string && buf && *buf && buflen && buf_datalen);
@@ -147,3 +147,25 @@ int fetch_from_buf(char *string, int string_len,
return *buf_datalen;
}
+int find_on_inbuf(char *string, int string_len,
+ char *buf, int buf_datalen) {
+ /* find first instance of needle 'string' on haystack 'buf'. return how
+ * many bytes from the beginning of buf to the end of string.
+ * If it's not there, return -1.
+ */
+
+ char *location;
+ char *last_possible = buf + buf_datalen - string_len;
+
+ assert(string && string_len > 0 && buf);
+
+ if(buf_datalen < string_len)
+ return -1;
+
+ for(location = buf; location <= last_possible; location++)
+ if((*location == *string) && !memcmp(location+1, string+1, string_len-1))
+ return location-buf+string_len;
+
+ return -1;
+}
+