Rename PACKETW to WPACKET
[openssl.git] / ssl / packet_locl.h
index 0ff4cccf3b071dd214fe5f4734eab18934f3013c..88bc7ddebc621a8294975742bbd0dc89dc7c6341 100644 (file)
@@ -54,6 +54,7 @@ static ossl_inline const unsigned char *PACKET_end(const PACKET *pkt)
 {
     return pkt->curr + pkt->remaining;
 }
+
 /*
  * Returns a pointer to the PACKET's current position.
  * For use in non-PACKETized APIs.
@@ -107,8 +108,7 @@ __owur static ossl_inline int PACKET_equal(const PACKET *pkt, const void *ptr,
  * the original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
  */
 __owur static ossl_inline int PACKET_peek_sub_packet(const PACKET *pkt,
-                                                     PACKET *subpkt,
-                                                     size_t len)
+                                                     PACKET *subpkt, size_t len)
 {
     if (PACKET_remaining(pkt) < len)
         return 0;
@@ -122,8 +122,7 @@ __owur static ossl_inline int PACKET_peek_sub_packet(const PACKET *pkt,
  * original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
  */
 __owur static ossl_inline int PACKET_get_sub_packet(PACKET *pkt,
-                                                    PACKET *subpkt,
-                                                    size_t len)
+                                                    PACKET *subpkt, size_t len)
 {
     if (!PACKET_peek_sub_packet(pkt, subpkt, len))
         return 0;
@@ -151,8 +150,7 @@ __owur static ossl_inline int PACKET_peek_net_2(const PACKET *pkt,
 
 /* Equivalent of n2s */
 /* Get 2 bytes in network order from |pkt| and store the value in |*data| */
-__owur static ossl_inline int PACKET_get_net_2(PACKET *pkt,
-                                               unsigned int *data)
+__owur static ossl_inline int PACKET_get_net_2(PACKET *pkt, unsigned int *data)
 {
     if (!PACKET_peek_net_2(pkt, data))
         return 0;
@@ -181,8 +179,7 @@ __owur static ossl_inline int PACKET_peek_net_3(const PACKET *pkt,
 
 /* Equivalent of n2l3 */
 /* Get 3 bytes in network order from |pkt| and store the value in |*data| */
-__owur static ossl_inline int PACKET_get_net_3(PACKET *pkt,
-                                               unsigned long *data)
+__owur static ossl_inline int PACKET_get_net_3(PACKET *pkt, unsigned long *data)
 {
     if (!PACKET_peek_net_3(pkt, data))
         return 0;
@@ -212,8 +209,7 @@ __owur static ossl_inline int PACKET_peek_net_4(const PACKET *pkt,
 
 /* Equivalent of n2l */
 /* Get 4 bytes in network order from |pkt| and store the value in |*data| */
-__owur static ossl_inline int PACKET_get_net_4(PACKET *pkt,
-                                               unsigned long *data)
+__owur static ossl_inline int PACKET_get_net_4(PACKET *pkt, unsigned long *data)
 {
     if (!PACKET_peek_net_4(pkt, data))
         return 0;
@@ -333,8 +329,7 @@ __owur static ossl_inline int PACKET_peek_copy_bytes(const PACKET *pkt,
  * The caller is responsible for ensuring that |data| can hold |len| bytes.
  */
 __owur static ossl_inline int PACKET_copy_bytes(PACKET *pkt,
-                                                unsigned char *data,
-                                                size_t len)
+                                                unsigned char *data, size_t len)
 {
     if (!PACKET_peek_copy_bytes(pkt, data, len))
         return 0;
@@ -417,7 +412,7 @@ __owur static ossl_inline int PACKET_strndup(const PACKET *pkt, char **data)
 /* Returns 1 if |pkt| contains at least one 0-byte, 0 otherwise. */
 static ossl_inline int PACKET_contains_zero_byte(const PACKET *pkt)
 {
-  return memchr(pkt->curr, 0, pkt->remaining) != NULL;
+    return memchr(pkt->curr, 0, pkt->remaining) != NULL;
 }
 
 /* Move the current reading position forward |len| bytes */
@@ -460,22 +455,23 @@ __owur static ossl_inline int PACKET_get_length_prefixed_1(PACKET *pkt,
  * Like PACKET_get_length_prefixed_1, but additionally, fails when there are
  * leftover bytes in |pkt|.
  */
-__owur static ossl_inline int PACKET_as_length_prefixed_1(PACKET *pkt, PACKET *subpkt)
+__owur static ossl_inline int PACKET_as_length_prefixed_1(PACKET *pkt,
+                                                          PACKET *subpkt)
 {
-  unsigned int length;
-  const unsigned char *data;
-  PACKET tmp = *pkt;
-  if (!PACKET_get_1(&tmp, &length) ||
-      !PACKET_get_bytes(&tmp, &data, (size_t)length) ||
-      PACKET_remaining(&tmp) != 0) {
-      return 0;
-  }
+    unsigned int length;
+    const unsigned char *data;
+    PACKET tmp = *pkt;
+    if (!PACKET_get_1(&tmp, &length) ||
+        !PACKET_get_bytes(&tmp, &data, (size_t)length) ||
+        PACKET_remaining(&tmp) != 0) {
+        return 0;
+    }
 
-  *pkt = tmp;
-  subpkt->curr = data;
-  subpkt->remaining = length;
+    *pkt = tmp;
+    subpkt->curr = data;
+    subpkt->remaining = length;
 
-  return 1;
+    return 1;
 }
 
 /*
@@ -511,21 +507,21 @@ __owur static ossl_inline int PACKET_get_length_prefixed_2(PACKET *pkt,
 __owur static ossl_inline int PACKET_as_length_prefixed_2(PACKET *pkt,
                                                           PACKET *subpkt)
 {
-  unsigned int length;
-  const unsigned char *data;
-  PACKET tmp = *pkt;
+    unsigned int length;
+    const unsigned char *data;
+    PACKET tmp = *pkt;
 
-  if (!PACKET_get_net_2(&tmp, &length) ||
-      !PACKET_get_bytes(&tmp, &data, (size_t)length) ||
-      PACKET_remaining(&tmp) != 0) {
-      return 0;
-  }
+    if (!PACKET_get_net_2(&tmp, &length) ||
+        !PACKET_get_bytes(&tmp, &data, (size_t)length) ||
+        PACKET_remaining(&tmp) != 0) {
+        return 0;
+    }
 
-  *pkt = tmp;
-  subpkt->curr = data;
-  subpkt->remaining = length;
+    *pkt = tmp;
+    subpkt->curr = data;
+    subpkt->remaining = length;
 
-  return 1;
+    return 1;
 }
 
 /*
@@ -552,6 +548,82 @@ __owur static ossl_inline int PACKET_get_length_prefixed_3(PACKET *pkt,
 
     return 1;
 }
+
+/* Writeable packets */
+
+typedef struct packetw_buf {
+    /* The buffer where we store the output data */
+    BUF_MEM *buf;
+
+    /* Pointer to where we are currently writing */
+    unsigned char *curr;
+
+    /* Number of bytes written so far */
+    size_t written;
+
+    /*
+     * Maximum number of bytes we will allow to be written to this WPACKET. Zero
+     * if no maximum
+     */
+    size_t maxsize;
+} WPACKET_BUF;
+
+typedef struct packetw_st WPACKET;
+struct packetw_st {
+    /* The parent WPACKET if we have one or NULL otherwise */
+    WPACKET *parent;
+
+    /* The actual buffer - shared with sub-packets */
+    WPACKET_BUF *wbuf;
+
+    /* Flags for this WPACKET */
+    unsigned int flags;
+
+    /*
+     * Pointer to where the length of this WPACKET goes (or NULL if we don't
+     * write the length)
+     */
+    unsigned char *packet_len;
+
+    /* Number of bytes in the packet_len */
+    size_t lenbytes;
+
+    /* Number of bytes written to the buf prior to this packet starting */
+    size_t pwritten;
+
+    /* True if we have an active sub-packet or false otherwise */
+    int haschild;
+
+    /* True if WPACKET_close() has been called on this WPACKET */
+    int isclosed;
+};
+
+/* Flags */
+#define OPENSSL_WPACKET_FLAGS_NONE                      0
+/* Error on WPACKET_close() if no data written to the WPACKET */
+#define OPENSSL_WPACKET_FLAGS_NON_ZERO_LENGTH           1
+/*
+ * Abandon all changes on WPACKET_close() if no data written to the WPACKET,
+ * i.e. this does not write out a zero packet length
+ */
+#define OPENSSL_WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH    2
+
+int WPACKET_init_len(WPACKET *pkt, BUF_MEM *buf, size_t lenbytes);
+int WPACKET_init(WPACKET *pkt, BUF_MEM *buf);
+int WPACKET_set_flags(WPACKET *pkt, unsigned int flags);
+int WPACKET_set_packet_len(WPACKET *pkt, unsigned char *packet_len,
+                           size_t lenbytes);
+int WPACKET_close(WPACKET *pkt);
+int WPACKET_get_sub_packet_len(WPACKET *pkt, WPACKET *subpkt, size_t lenbytes);
+int WPACKET_get_sub_packet(WPACKET *pkt, WPACKET *subpkt);
+int WPACKET_allocate_bytes(WPACKET *pkt, size_t bytes,
+                           unsigned char **allocbytes);
+int WPACKET_put_bytes(WPACKET *pkt, unsigned int val, size_t bytes);
+int WPACKET_set_max_size(WPACKET *pkt, size_t maxsize);
+int WPACKET_memcpy(WPACKET *pkt, const void *src, size_t len);
+int WPACKET_get_total_written(WPACKET *pkt, size_t *written);
+int WPACKET_get_length(WPACKET *pkt, size_t *len);
+
 # ifdef __cplusplus
 }
 # endif