1 /* ssl/packet_locl.h */
3 * Written by Matt Caswell for the OpenSSL project.
5 /* ====================================================================
6 * Copyright (c) 2015 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * openssl-core@openssl.org.
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
59 #ifndef HEADER_PACKET_LOCL_H
60 # define HEADER_PACKET_LOCL_H
63 # include <openssl/bn.h>
64 # include <openssl/buffer.h>
72 /* Pointer to the start of the buffer data */
75 /* Pointer to the first byte after the end of the buffer data */
78 /* Pointer to where we are currently reading from */
83 * Returns 1 if there are exactly |len| bytes left to be read from |pkt|
86 __owur static inline size_t PACKET_remaining(PACKET *pkt)
88 return (size_t)(pkt->end - pkt->curr);
92 * Initialise a PACKET with |len| bytes held in |buf|. This does not make a
93 * copy of the data so |buf| must be present for the whole time that the PACKET
96 static inline int PACKET_buf_init(PACKET *pkt, unsigned char *buf, size_t len)
98 pkt->start = pkt->curr = buf;
99 pkt->end = pkt->start + len;
102 if (pkt->start > pkt->end
103 || pkt->curr < pkt->start
104 || pkt->curr > pkt->end
105 || len != (size_t)(pkt->end - pkt->start)) {
113 * Peek ahead and initialize |subpkt| with the next |len| bytes read from |pkt|.
114 * Data is not copied: the |subpkt| packet will share its underlying buffer with
115 * the original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
117 __owur static inline int PACKET_peek_sub_packet(PACKET *pkt, PACKET *subpkt,
120 if (PACKET_remaining(pkt) < len)
123 PACKET_buf_init(subpkt, pkt->curr, len);
129 * Initialize |subpkt| with the next |len| bytes read from |pkt|. Data is not
130 * copied: the |subpkt| packet will share its underlying buffer with the
131 * original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
133 __owur static inline int PACKET_get_sub_packet(PACKET *pkt, PACKET *subpkt,
136 if (!PACKET_peek_sub_packet(pkt, subpkt, len))
144 /* Peek ahead at 2 bytes in network order from |pkt| and store the value in
147 __owur static inline int PACKET_peek_net_2(PACKET *pkt, unsigned int *data)
149 if (PACKET_remaining(pkt) < 2)
152 *data = ((unsigned int)(*pkt->curr)) << 8;
153 *data |= *(pkt->curr + 1);
158 /* Equivalent of n2s */
159 /* Get 2 bytes in network order from |pkt| and store the value in |*data| */
160 __owur static inline int PACKET_get_net_2(PACKET *pkt, unsigned int *data)
162 if (!PACKET_peek_net_2(pkt, data))
170 /* Peek ahead at 3 bytes in network order from |pkt| and store the value in
173 __owur static inline int PACKET_peek_net_3(PACKET *pkt, unsigned long *data)
175 if (PACKET_remaining(pkt) < 3)
178 *data = ((unsigned long)(*pkt->curr)) << 16;
179 *data |= ((unsigned long)(*pkt->curr + 1)) << 8;
180 *data |= *pkt->curr + 2;
185 /* Equivalent of n2l3 */
186 /* Get 3 bytes in network order from |pkt| and store the value in |*data| */
187 __owur static inline int PACKET_get_net_3(PACKET *pkt, unsigned long *data)
189 if (!PACKET_peek_net_3(pkt, data))
197 /* Peek ahead at 4 bytes in network order from |pkt| and store the value in
200 __owur static inline int PACKET_peek_net_4(PACKET *pkt, unsigned long *data)
202 if (PACKET_remaining(pkt) < 4)
205 *data = ((unsigned long)(*pkt->curr)) << 24;
206 *data |= ((unsigned long)(*pkt->curr + 1)) << 16;
207 *data |= ((unsigned long)(*pkt->curr + 2)) << 8;
208 *data |= *pkt->curr+3;
213 /* Equivalent of n2l */
214 /* Get 4 bytes in network order from |pkt| and store the value in |*data| */
215 __owur static inline int PACKET_get_net_4(PACKET *pkt, unsigned long *data)
217 if (!PACKET_peek_net_4(pkt, data))
225 /* Peek ahead at 1 byte from |pkt| and store the value in |*data| */
226 __owur static inline int PACKET_peek_1(PACKET *pkt, unsigned int *data)
228 if (!PACKET_remaining(pkt))
236 /* Get 1 byte from |pkt| and store the value in |*data| */
237 __owur static inline int PACKET_get_1(PACKET *pkt, unsigned int *data)
239 if (!PACKET_peek_1(pkt, data))
248 * Peek ahead at 4 bytes in reverse network order from |pkt| and store the value
251 __owur static inline int PACKET_peek_4(PACKET *pkt, unsigned long *data)
253 if (PACKET_remaining(pkt) < 4)
257 *data |= ((unsigned long)(*pkt->curr + 1)) << 8;
258 *data |= ((unsigned long)(*pkt->curr + 2)) << 16;
259 *data |= ((unsigned long)(*pkt->curr + 3)) << 24;
264 /* Equivalent of c2l */
266 * Get 4 bytes in reverse network order from |pkt| and store the value in
269 __owur static inline int PACKET_get_4(PACKET *pkt, unsigned long *data)
271 if (!PACKET_peek_4(pkt, data))
280 * Peek ahead at |len| bytes from the |pkt| and store a pointer to them in
281 * |*data|. This just points at the underlying buffer that |pkt| is using. The
282 * caller should not free this data directly (it will be freed when the
283 * underlying buffer gets freed
285 __owur static inline int PACKET_peek_bytes(PACKET *pkt, unsigned char **data,
288 if (PACKET_remaining(pkt) < len)
297 * Read |len| bytes from the |pkt| and store a pointer to them in |*data|. This
298 * just points at the underlying buffer that |pkt| is using. The caller should
299 * not free this data directly (it will be freed when the underlying buffer gets
302 __owur static inline int PACKET_get_bytes(PACKET *pkt, unsigned char **data,
305 if (!PACKET_peek_bytes(pkt, data, len))
313 /* Peek ahead at |len| bytes from |pkt| and copy them to |data| */
314 __owur static inline int PACKET_peek_copy_bytes(PACKET *pkt,
315 unsigned char *data, size_t len)
317 if (PACKET_remaining(pkt) < len)
320 memcpy(data, pkt->curr, len);
325 /* Read |len| bytes from |pkt| and copy them to |data| */
326 __owur static inline int PACKET_copy_bytes(PACKET *pkt, unsigned char *data,
329 if (!PACKET_peek_copy_bytes(pkt, data, len))
337 /* Move the current reading position back |len| bytes */
338 __owur static inline int PACKET_back(PACKET *pkt, size_t len)
340 if (len > (size_t)(pkt->curr - pkt->start))
348 /* Move the current reading position forward |len| bytes */
349 __owur static inline int PACKET_forward(PACKET *pkt, size_t len)
351 if (PACKET_remaining(pkt) < len)
359 /* Store a bookmark for the current reading position in |*bm| */
360 __owur static inline int PACKET_get_bookmark(PACKET *pkt, size_t *bm)
362 *bm = pkt->curr - pkt->start;
367 /* Set the current reading position to the bookmark |bm| */
368 __owur static inline int PACKET_goto_bookmark(PACKET *pkt, size_t bm)
370 if (bm > (size_t)(pkt->end - pkt->start))
373 pkt->curr = pkt->start + bm;
379 * Stores the total length of the packet we have in the underlying buffer in
382 __owur static inline int PACKET_length(PACKET *pkt, size_t *len)
384 *len = pkt->end - pkt->start;
393 #endif /* HEADER_PACKET_LOCL_H */