tRemove ugly off++ trick for 4 bytes big endian integers - libeech - bittorrent library
(HTM) git clone git://z3bra.org/libeech.git
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
(DIR) LICENSE
---
(DIR) commit 586e58d4469b1212bcd72ca83d546f3b3437380f
(DIR) parent 1871407a84e54060480834c2b7577b62dfd12198
(HTM) Author: z3bra <contactatz3bradotorg>
Date: Tue, 3 Jul 2018 23:39:07 +0200
Remove ugly off++ trick for 4 bytes big endian integers
Diffstat:
M libeech.c | 50 ++++++++++----------------------
1 file changed, 15 insertions(+), 35 deletions(-)
---
(DIR) diff --git a/libeech.c b/libeech.c
t@@ -488,23 +488,17 @@ static ssize_t
pwptxhv(struct peer *p, int n)
{
char m[MSGSIZ];
- ssize_t off = 0;
memset(m, 0, 9);
+
/* Message length */
- m[off++] = (9 >> 24) & 0xff;
- m[off++] = (9 >> 16) & 0xff;
- m[off++] = (9 >> 8) & 0xff;
- m[off++] = (9 >> 0) & 0xff;
+ U32(m) = htonl(9);
/* Message type */
- m[off++] = HAVE;
+ m[4] = HAVE;
/* Piece number */
- m[off++] = (n >> 24) & 0xff;
- m[off++] = (n >> 16) & 0xff;
- m[off++] = (n >> 8) & 0xff;
- m[off++] = (n >> 0) & 0xff;
+ U32(m+5) = htonl(n);
return pwpsend(p, m, 9);
}
t@@ -513,19 +507,17 @@ static ssize_t
pwptxbf(struct torrent *t, struct peer *p)
{
char m[MSGSIZ];
- ssize_t l, off = 0;
+ ssize_t l;
l = t->npiece / 8 + 1;
memset(m, 0, l + 5);
+
/* Message length */
- m[off++] = ((l + 1) >> 24) & 0xff;
- m[off++] = ((l + 1) >> 16) & 0xff;
- m[off++] = ((l + 1) >> 8) & 0xff;
- m[off++] = ((l + 1) >> 0) & 0xff;
+ U32(m) = htonl(l + 1);
/* Message type */
- m[off++] = BITFIELD;
+ m[4] = BITFIELD;
return pwpsend(p, m, l + 5);
}
t@@ -535,7 +527,7 @@ pwptxrq(struct torrent *t, struct peer *p)
{
int i;
char m[MSGSIZ];
- ssize_t bl, off = 0;
+ ssize_t bl;
/* Find a missing piece we can get from the peer */
for (i = 0; i >= t->npiece || (!bit(t->bf, i) && bit(p->bf, i)); i++);
t@@ -550,33 +542,21 @@ pwptxrq(struct torrent *t, struct peer *p)
bl = p->piece.sz - i;
memset(m, 0, MSGSIZ);
- off = 0;
+
/* Message length */
- m[off++] = (13 >> 24) & 0xff;
- m[off++] = (13 >> 16) & 0xff;
- m[off++] = (13 >> 8) & 0xff;
- m[off++] = (13 >> 0) & 0xff;
+ U32(m) = htonl(13);
/* Message type */
- m[off++] = REQUEST;
+ m[4] = REQUEST;
/* Piece index */
- m[off++] = (p->piece.n >> 24) & 0xff;
- m[off++] = (p->piece.n >> 16) & 0xff;
- m[off++] = (p->piece.n >> 8) & 0xff;
- m[off++] = (p->piece.n >> 0) & 0xff;
+ U32(m+5) = htonl(p->piece.n);
/* Block offset */
- m[off++] = (i >> 24) & 0xff;
- m[off++] = (i >> 16) & 0xff;
- m[off++] = (i >> 8) & 0xff;
- m[off++] = (i >> 0) & 0xff;
+ U32(m+9) = htonl(i);
/* Block length */
- m[off++] = (bl >> 24) & 0xff;
- m[off++] = (bl >> 16) & 0xff;
- m[off++] = (bl >> 8) & 0xff;
- m[off++] = (bl >> 0) & 0xff;
+ U32(m+13) = htonl(bl);
pwpsend(p, m, 17);