aboutsummaryrefslogtreecommitdiff
path: root/src/libventi
diff options
context:
space:
mode:
authorrsc <devnull@localhost>2006-05-05 13:17:39 +0000
committerrsc <devnull@localhost>2006-05-05 13:17:39 +0000
commit02846514a3170314bde9ac4678a2e9cdf153c695 (patch)
treedade261ccd0e070862240c0a85a1fbd4ae4e04a4 /src/libventi
parent6cd6f689109a3021bd81cbdd62a738e26b693f23 (diff)
downloadplan9port-02846514a3170314bde9ac4678a2e9cdf153c695.tar.gz
plan9port-02846514a3170314bde9ac4678a2e9cdf153c695.tar.bz2
plan9port-02846514a3170314bde9ac4678a2e9cdf153c695.zip
add debugpacket
Diffstat (limited to 'src/libventi')
-rw-r--r--src/libventi/debugpacket.c265
1 files changed, 265 insertions, 0 deletions
diff --git a/src/libventi/debugpacket.c b/src/libventi/debugpacket.c
new file mode 100644
index 00000000..4c13bc87
--- /dev/null
+++ b/src/libventi/debugpacket.c
@@ -0,0 +1,265 @@
+#include <u.h>
+#include <libc.h>
+#include <venti.h>
+#include <libsec.h>
+
+#define MAGIC 0x54798314
+#define NOTFREE(p) assert((p)->magic == MAGIC)
+
+struct Packet
+{
+ char *data;
+ int len;
+ void (*free)(void*);
+ void *arg;
+ int magic;
+};
+
+Packet*
+packetalloc(void)
+{
+ Packet *p;
+
+ p = vtmallocz(sizeof *p);
+ p->free = vtfree;
+ p->arg = nil;
+ p->magic = MAGIC;
+ return p;
+}
+
+void
+packetappend(Packet *p, uchar *buf, int n)
+{
+ NOTFREE(p);
+ if(n < 0)
+ abort();
+ if(p->free != vtfree)
+ sysfatal("packetappend");
+ p->data = vtrealloc(p->data, p->len+n);
+ p->arg = p->data;
+ memmove(p->data+p->len, buf, n);
+ p->len += n;
+}
+
+uint
+packetasize(Packet *p)
+{
+ NOTFREE(p);
+ return p->len;
+}
+
+int
+packetcmp(Packet *p, Packet *q)
+{
+ int i, len;
+
+ NOTFREE(p);
+ NOTFREE(q);
+ len = p->len;
+ if(len > q->len)
+ len = q->len;
+ if(len && (i=memcmp(p->data, q->data, len)) != 0)
+ return i;
+ if(p->len > len)
+ return 1;
+ if(q->len > len)
+ return -1;
+ return 0;
+}
+
+void
+packetconcat(Packet *p, Packet *q)
+{
+ NOTFREE(p);
+ NOTFREE(q);
+ packetappend(p, q->data, q->len);
+ if(q->free == vtfree)
+ memset(q->data, 0xFE, q->len);
+ q->free(q->arg);
+ q->data = nil;
+ q->len = 0;
+}
+
+int
+packetconsume(Packet *p, uchar *buf, int n)
+{
+ NOTFREE(p);
+ if(n < 0)
+ abort();
+ if(p->len < n)
+ abort();
+ memmove(buf, p->data, n);
+ p->len -= n;
+ memmove(p->data, p->data+n, p->len);
+ return 0;
+}
+
+int
+packetcopy(Packet *p, uchar *buf, int offset, int n)
+{
+ NOTFREE(p);
+ if(offset < 0 || n < 0)
+ abort();
+ if(offset > p->len)
+ abort();
+ if(offset+n > p->len)
+ n = p->len - offset;
+ memmove(buf, p->data+offset, n);
+ return 0;
+}
+
+Packet*
+packetdup(Packet *p, int offset, int n)
+{
+ Packet *q;
+
+ NOTFREE(p);
+ if(offset < 0 || n < 0)
+ abort();
+ if(offset > p->len)
+ abort();
+ if(offset+n > p->len)
+ n = p->len - offset;
+ q = packetalloc();
+ packetappend(q, p->data+offset, n);
+ return q;
+}
+
+Packet*
+packetforeign(uchar *buf, int n, void (*free)(void*), void *a)
+{
+ Packet *p;
+
+ if(n < 0)
+ abort();
+ p = packetalloc();
+ p->data = (char*)buf;
+ p->len = n;
+ p->free = free;
+ p->arg = a;
+ return p;
+}
+
+int
+packetfragments(Packet *p, IOchunk *io, int nio, int offset)
+{
+ NOTFREE(p);
+ if(offset < 0)
+ abort();
+ if(nio == 0)
+ return 0;
+ memset(io, 0, sizeof(io[0])*nio);
+ if(offset >= p->len)
+ return 0;
+ io[0].addr = p->data + offset;
+ io[0].len = p->len - offset;
+ return p->len;
+}
+
+void
+packetfree(Packet *p)
+{
+ NOTFREE(p);
+ if(p->free == free)
+ memset(p->data, 0xFE, p->len);
+ p->free(p->arg);
+ p->data = nil;
+ p->len = 0;
+ memset(p, 0xFB, sizeof *p);
+ free(p);
+}
+
+uchar*
+packetheader(Packet *p, int n)
+{
+ NOTFREE(p);
+ if(n < 0)
+ abort();
+ if(n > p->len)
+ abort();
+ return p->data;
+}
+
+uchar*
+packetpeek(Packet *p, uchar *buf, int offset, int n)
+{
+ NOTFREE(p);
+ if(offset < 0 || n < 0)
+ abort();
+ if(offset+n > p->len)
+ abort();
+ return p->data+offset;
+}
+
+void
+packetprefix(Packet *p, uchar *buf, int n)
+{
+ NOTFREE(p);
+ if(n < 0)
+ abort();
+ if(p->free != free)
+ sysfatal("packetappend");
+ p->data = vtrealloc(p->data, p->len+n);
+ p->arg = p->data;
+ memmove(p->data+n, p->data, p->len);
+ memmove(p->data, buf, n);
+ p->len += n;
+}
+
+void
+packetsha1(Packet *p, uchar d[20])
+{
+ NOTFREE(p);
+ sha1((uchar*)p->data, p->len, d, nil);
+}
+
+uint
+packetsize(Packet *p)
+{
+ NOTFREE(p);
+ return p->len;
+}
+
+Packet*
+packetsplit(Packet *p, int n)
+{
+ Packet *q;
+
+ NOTFREE(p);
+ q = packetalloc();
+ q->data = vtmalloc(n);
+ q->arg = q->data;
+ q->free = vtfree;
+ packetconsume(p, q->data, n);
+ return q;
+}
+
+void
+packetstats(void)
+{
+}
+
+uchar*
+packettrailer(Packet *p, int n)
+{
+ NOTFREE(p);
+ if(n < 0)
+ abort();
+ if(n > p->len)
+ abort();
+ return p->data + p->len - n;
+}
+
+int
+packettrim(Packet *p, int offset, int n)
+{
+ NOTFREE(p);
+ if(offset < 0 || n < 0)
+ abort();
+ if(offset+n > p->len)
+ abort();
+ memmove(p->data+offset, p->data+offset+n, p->len-offset-n);
+ p->len -= n;
+ return 0;
+}
+