#define DEFAULT_BUF_SIZE 256
int WPACKET_allocate_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)
+{
+ if (!WPACKET_reserve_bytes(pkt, len, allocbytes))
+ return 0;
+
+ pkt->written += len;
+ pkt->curr += len;
+ return 1;
+}
+
+int WPACKET_sub_allocate_bytes__(WPACKET *pkt, size_t len,
+ unsigned char **allocbytes, size_t lenbytes)
+{
+ if (!WPACKET_start_sub_packet_len__(pkt, lenbytes)
+ || !WPACKET_allocate_bytes(pkt, len, allocbytes)
+ || !WPACKET_close(pkt))
+ return 0;
+
+ return 1;
+}
+
+int WPACKET_reserve_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)
{
/* Internal API, so should not fail */
assert(pkt->subs != NULL && len != 0);
if (pkt->buf->length - pkt->written < len) {
size_t newlen;
+ size_t reflen;
- if (pkt->buf->length > SIZE_MAX / 2) {
+ reflen = (len > pkt->buf->length) ? len : pkt->buf->length;
+
+ if (reflen > SIZE_MAX / 2) {
newlen = SIZE_MAX;
} else {
- newlen = (pkt->buf->length == 0) ? DEFAULT_BUF_SIZE
- : pkt->buf->length * 2;
+ newlen = reflen * 2;
+ if (newlen < DEFAULT_BUF_SIZE)
+ newlen = DEFAULT_BUF_SIZE;
}
if (BUF_MEM_grow(pkt->buf, newlen) == 0)
return 0;
}
- pkt->written += len;
*allocbytes = (unsigned char *)pkt->buf->data + pkt->curr;
- pkt->curr += len;
+
+ return 1;
+}
+
+int WPACKET_sub_reserve_bytes__(WPACKET *pkt, size_t len,
+ unsigned char **allocbytes, size_t lenbytes)
+{
+ if (!WPACKET_reserve_bytes(pkt, lenbytes + len, allocbytes))
+ return 0;
+
+ *allocbytes += lenbytes;
return 1;
}
{
if (lenbytes >= sizeof(size_t) || lenbytes == 0)
return SIZE_MAX;
- else
- return ((size_t)1 << (lenbytes * 8)) - 1 + lenbytes;
+
+ return ((size_t)1 << (lenbytes * 8)) - 1 + lenbytes;
}
int WPACKET_init_len(WPACKET *pkt, BUF_MEM *buf, size_t lenbytes)
return 1;
}
+/* Store the |value| of length |len| at location |data| */
+static int put_value(unsigned char *data, size_t value, size_t len)
+{
+ for (data += len - 1; len > 0; len--) {
+ *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
size_t packlen = pkt->written - sub->pwritten;
if (packlen == 0
- && sub->flags & WPACKET_FLAGS_NON_ZERO_LENGTH)
+ && (sub->flags & WPACKET_FLAGS_NON_ZERO_LENGTH) != 0)
return 0;
if (packlen == 0
}
/* 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);
return ret;
}
-int WPACKET_start_sub_packet_len(WPACKET *pkt, size_t lenbytes)
+int WPACKET_start_sub_packet_len__(WPACKET *pkt, size_t lenbytes)
{
WPACKET_SUB *sub;
unsigned char *lenchars;
if (!WPACKET_allocate_bytes(pkt, lenbytes, &lenchars))
return 0;
+ /* Convert to an offset in case the underlying BUF_MEM gets realloc'd */
sub->packet_len = lenchars - (unsigned char *)pkt->buf->data;
return 1;
int WPACKET_start_sub_packet(WPACKET *pkt)
{
- return WPACKET_start_sub_packet_len(pkt, 0);
+ return WPACKET_start_sub_packet_len__(pkt, 0);
}
-int WPACKET_put_bytes(WPACKET *pkt, unsigned int val, size_t size)
+int WPACKET_put_bytes__(WPACKET *pkt, unsigned int val, size_t size)
{
unsigned char *data;
/* 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)
+ if (size > sizeof(unsigned int)
+ || !WPACKET_allocate_bytes(pkt, size, &data)
+ || !put_value(data, val, size))
return 0;
return 1;
return 1;
}
-int WPACKET_sub_memcpy(WPACKET *pkt, const void *src, size_t len, size_t lenbytes)
+int WPACKET_sub_memcpy__(WPACKET *pkt, const void *src, size_t len,
+ size_t lenbytes)
{
- if (!WPACKET_start_sub_packet_len(pkt, lenbytes)
+ if (!WPACKET_start_sub_packet_len__(pkt, lenbytes)
|| !WPACKET_memcpy(pkt, src, len)
|| !WPACKET_close(pkt))
return 0;