summaryrefslogtreecommitdiffstats
path: root/ssl/packet.c
diff options
context:
space:
mode:
authorMatt Caswell <matt@openssl.org>2016-09-12 09:39:10 +0100
committerMatt Caswell <matt@openssl.org>2016-09-13 09:41:21 +0100
commit826573559df6c74a6773475a1b7a2a2ba13cec28 (patch)
tree67714dd06e7ffbfd5c6f28d3e07b89f44df2291f /ssl/packet.c
parentc39609aa6a575c9645d87711e3db439eb832ca70 (diff)
Pull out some common packet code into a function
Two locations had the same loop for writing out a value. Pull it out into a function. Reviewed-by: Rich Salz <rsalz@openssl.org>
Diffstat (limited to 'ssl/packet.c')
-rw-r--r--ssl/packet.c46
1 files changed, 21 insertions, 25 deletions
diff --git a/ssl/packet.c b/ssl/packet.c
index aab2b546c6..7d37e4db79 100644
--- a/ssl/packet.c
+++ b/ssl/packet.c
@@ -100,6 +100,22 @@ int WPACKET_set_flags(WPACKET *pkt, unsigned int flags)
return 1;
}
+/* Store the |value| of length |size| at location |data| */
+static int put_value(unsigned char *data, size_t value, size_t size)
+{
+ for (data += size - 1; size > 0; size--) {
+ *data = (unsigned char)(value & 0xff);
+ data--;
+ value >>= 8;
+ }
+
+ /* Check whether we could fit the value in the assigned number of bytes */
+ if (value > 0)
+ return 0;
+
+ return 1;
+}
+
/*
* Internal helper function used by WPACKET_close() and WPACKET_finish() to
@@ -128,21 +144,10 @@ static int wpacket_intern_close(WPACKET *pkt)
}
/* Write out the WPACKET length if needed */
- if (sub->lenbytes > 0) {
- size_t lenbytes;
-
- for (lenbytes = sub->lenbytes; lenbytes > 0; lenbytes--) {
- pkt->buf->data[sub->packet_len + lenbytes - 1]
- = (unsigned char)(packlen & 0xff);
- packlen >>= 8;
- }
- if (packlen > 0) {
- /*
- * We've extended beyond the max allowed for the number of len bytes
- */
+ if (sub->lenbytes > 0
+ && !put_value((unsigned char *)&pkt->buf->data[sub->packet_len],
+ packlen, sub->lenbytes))
return 0;
- }
- }
pkt->subs = sub->parent;
OPENSSL_free(sub);
@@ -225,17 +230,8 @@ int WPACKET_put_bytes(WPACKET *pkt, unsigned int val, size_t size)
/* Internal API, so should not fail */
assert(size <= sizeof(unsigned int));
if (size > sizeof(unsigned int)
- || !WPACKET_allocate_bytes(pkt, size, &data))
- return 0;
-
- for (data += size - 1; size > 0; size--) {
- *data = (unsigned char)(val & 0xff);
- data--;
- val >>= 8;
- }
-
- /* Check whether we could fit the value in the assigned number of bytes */
- if (val > 0)
+ || !WPACKET_allocate_bytes(pkt, size, &data)
+ || !put_value(data, val, size))
return 0;
return 1;