aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2008-02-20 17:48:39 +0000
committerNick Mathewson <nickm@torproject.org>2008-02-20 17:48:39 +0000
commit7521ef9a00e71b62bcdce199639e923dd284976b (patch)
tree6df3b788f2aeabd297513cefce9950ba983b84ec /src
parent5dd9d16b94e23ed6223f7583b8c7143cfed62d59 (diff)
downloadtor-7521ef9a00e71b62bcdce199639e923dd284976b.tar
tor-7521ef9a00e71b62bcdce199639e923dd284976b.tar.gz
r18264@catbus: nickm | 2008-02-20 12:48:21 -0500
fix bufs in buf_pos_t implementation. svn:r13623
Diffstat (limited to 'src')
-rw-r--r--src/or/buffers.c26
-rw-r--r--src/or/test.c2
2 files changed, 14 insertions, 14 deletions
diff --git a/src/or/buffers.c b/src/or/buffers.c
index 13fda0fb5..f5c1b3ba3 100644
--- a/src/or/buffers.c
+++ b/src/or/buffers.c
@@ -998,9 +998,8 @@ move_buf_to_buf(buf_t *buf_out, buf_t *buf_in, size_t *buf_flushlen)
/** Internal structure: represents a position in a buffer. */
typedef struct buf_pos_t {
const chunk_t *chunk; /**< Which chunk are we pointing to? */
- int pos; /**< Which character inside the chunk's data are we pointing to? */
- int pos_absolute; /**< Which character inside the buffer are we pointing to?
- */
+ off_t pos;/**< Which character inside the chunk's data are we pointing to? */
+ size_t chunk_pos; /**< Total length of all previous chunks. */
} buf_pos_t;
/** Initialize <b>out</b> to point to the first character of <b>buf</b>.*/
@@ -1009,7 +1008,7 @@ buf_pos_init(const buf_t *buf, buf_pos_t *out)
{
out->chunk = buf->head;
out->pos = 0;
- out->pos_absolute = 0;
+ out->chunk_pos = 0;
}
/** Advance <b>out</b> to the first appearance of <b>ch</b> at the current
@@ -1019,19 +1018,19 @@ static int
buf_find_pos_of_char(char ch, buf_pos_t *out)
{
const chunk_t *chunk;
- int offset = 0; /*XXXX020 should this be pos_absolute? Otherwise, bug. */
int pos;
- tor_assert(out && out->chunk && out->pos < (int)out->chunk->datalen);
+ tor_assert(out);
+ if (out->chunk)
+ tor_assert(out->pos < out->chunk->datalen);
pos = out->pos;
for (chunk = out->chunk; chunk; chunk = chunk->next) {
char *cp = memchr(chunk->data+pos, ch, chunk->datalen-pos);
if (cp) {
out->chunk = chunk;
out->pos = cp - chunk->data;
- out->pos_absolute = offset + (cp - chunk->data);
- return out->pos_absolute;
+ return out->chunk_pos + out->pos;
} else {
- offset += chunk->datalen;
+ out->chunk_pos += chunk->datalen;
pos = 0;
}
}
@@ -1043,15 +1042,14 @@ buf_find_pos_of_char(char ch, buf_pos_t *out)
static INLINE int
buf_pos_inc(buf_pos_t *pos)
{
- if ((size_t)pos->pos == pos->chunk->datalen) {
+ ++pos->pos;
+ if (pos->pos == pos->chunk->datalen) {
if (!pos->chunk->next)
return -1;
+ pos->chunk_pos += pos->chunk->datalen;
pos->chunk = pos->chunk->next;
pos->pos = 0;
- } else {
- ++pos->pos;
}
- ++pos->pos_absolute;
return 0;
}
@@ -1084,7 +1082,7 @@ buf_find_string_offset(const buf_t *buf, const char *s, size_t n)
buf_pos_init(buf, &pos);
while (buf_find_pos_of_char(*s, &pos) >= 0) {
if (buf_matches_at_pos(&pos, s, n)) {
- return pos.pos_absolute;
+ return pos.chunk_pos + pos.pos;
} else {
if (buf_pos_inc(&pos)<0)
return -1;
diff --git a/src/or/test.c b/src/or/test.c
index ffaf29097..3dff0a42e 100644
--- a/src/or/test.c
+++ b/src/or/test.c
@@ -255,6 +255,8 @@ test_buffers(void)
buf_free(buf);
buf_free(buf2);
+ /*XXXX020 Test code to find chars and strings on buffers. */
+
#if 0
{
int s;