Lines Matching +full:pkt +full:- +full:size

2  * Copyright 2015-2024 The OpenSSL Project Authors. All Rights Reserved.
30 static ossl_inline void packet_forward(PACKET *pkt, size_t len) in packet_forward() argument
32 pkt->curr += len; in packet_forward()
33 pkt->remaining -= len; in packet_forward()
39 static ossl_inline size_t PACKET_remaining(const PACKET *pkt) in PACKET_remaining() argument
41 return pkt->remaining; in PACKET_remaining()
46 * Useful for integrating with non-PACKET parsing code.
50 static ossl_inline const unsigned char *PACKET_end(const PACKET *pkt) in PACKET_end() argument
52 return pkt->curr + pkt->remaining; in PACKET_end()
57 * For use in non-PACKETized APIs.
59 static ossl_inline const unsigned char *PACKET_data(const PACKET *pkt) in PACKET_data() argument
61 return pkt->curr; in PACKET_data()
69 __owur static ossl_inline int PACKET_buf_init(PACKET *pkt, in PACKET_buf_init() argument
77 pkt->curr = buf; in PACKET_buf_init()
78 pkt->remaining = len; in PACKET_buf_init()
83 static ossl_inline void PACKET_null_init(PACKET *pkt) in PACKET_null_init() argument
85 pkt->curr = NULL; in PACKET_null_init()
86 pkt->remaining = 0; in PACKET_null_init()
94 __owur static ossl_inline int PACKET_equal(const PACKET *pkt, const void *ptr, in PACKET_equal() argument
97 if (PACKET_remaining(pkt) != num) in PACKET_equal()
99 return CRYPTO_memcmp(pkt->curr, ptr, num) == 0; in PACKET_equal()
103 * Peek ahead and initialize |subpkt| with the next |len| bytes read from |pkt|.
105 * the original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
107 __owur static ossl_inline int PACKET_peek_sub_packet(const PACKET *pkt, in PACKET_peek_sub_packet() argument
110 if (PACKET_remaining(pkt) < len) in PACKET_peek_sub_packet()
113 return PACKET_buf_init(subpkt, pkt->curr, len); in PACKET_peek_sub_packet()
117 * Initialize |subpkt| with the next |len| bytes read from |pkt|. Data is not
119 * original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
121 __owur static ossl_inline int PACKET_get_sub_packet(PACKET *pkt, in PACKET_get_sub_packet() argument
124 if (!PACKET_peek_sub_packet(pkt, subpkt, len)) in PACKET_get_sub_packet()
127 packet_forward(pkt, len); in PACKET_get_sub_packet()
133 * Peek ahead at 2 bytes in network order from |pkt| and store the value in
136 __owur static ossl_inline int PACKET_peek_net_2(const PACKET *pkt, in PACKET_peek_net_2() argument
139 if (PACKET_remaining(pkt) < 2) in PACKET_peek_net_2()
142 *data = ((unsigned int)(*pkt->curr)) << 8; in PACKET_peek_net_2()
143 *data |= *(pkt->curr + 1); in PACKET_peek_net_2()
149 /* Get 2 bytes in network order from |pkt| and store the value in |*data| */
150 __owur static ossl_inline int PACKET_get_net_2(PACKET *pkt, unsigned int *data) in PACKET_get_net_2() argument
152 if (!PACKET_peek_net_2(pkt, data)) in PACKET_get_net_2()
155 packet_forward(pkt, 2); in PACKET_get_net_2()
161 __owur static ossl_inline int PACKET_get_net_2_len(PACKET *pkt, size_t *data) in PACKET_get_net_2_len() argument
164 int ret = PACKET_get_net_2(pkt, &i); in PACKET_get_net_2_len()
173 * Peek ahead at 3 bytes in network order from |pkt| and store the value in
176 __owur static ossl_inline int PACKET_peek_net_3(const PACKET *pkt, in PACKET_peek_net_3() argument
179 if (PACKET_remaining(pkt) < 3) in PACKET_peek_net_3()
182 *data = ((unsigned long)(*pkt->curr)) << 16; in PACKET_peek_net_3()
183 *data |= ((unsigned long)(*(pkt->curr + 1))) << 8; in PACKET_peek_net_3()
184 *data |= *(pkt->curr + 2); in PACKET_peek_net_3()
190 /* Get 3 bytes in network order from |pkt| and store the value in |*data| */
191 __owur static ossl_inline int PACKET_get_net_3(PACKET *pkt, unsigned long *data) in PACKET_get_net_3() argument
193 if (!PACKET_peek_net_3(pkt, data)) in PACKET_get_net_3()
196 packet_forward(pkt, 3); in PACKET_get_net_3()
202 __owur static ossl_inline int PACKET_get_net_3_len(PACKET *pkt, size_t *data) in PACKET_get_net_3_len() argument
205 int ret = PACKET_get_net_3(pkt, &i); in PACKET_get_net_3_len()
214 * Peek ahead at 4 bytes in network order from |pkt| and store the value in
217 __owur static ossl_inline int PACKET_peek_net_4(const PACKET *pkt, in PACKET_peek_net_4() argument
220 if (PACKET_remaining(pkt) < 4) in PACKET_peek_net_4()
223 *data = ((unsigned long)(*pkt->curr)) << 24; in PACKET_peek_net_4()
224 *data |= ((unsigned long)(*(pkt->curr + 1))) << 16; in PACKET_peek_net_4()
225 *data |= ((unsigned long)(*(pkt->curr + 2))) << 8; in PACKET_peek_net_4()
226 *data |= *(pkt->curr + 3); in PACKET_peek_net_4()
232 * Peek ahead at 8 bytes in network order from |pkt| and store the value in
235 __owur static ossl_inline int PACKET_peek_net_8(const PACKET *pkt, in PACKET_peek_net_8() argument
238 if (PACKET_remaining(pkt) < 8) in PACKET_peek_net_8()
241 *data = ((uint64_t)(*pkt->curr)) << 56; in PACKET_peek_net_8()
242 *data |= ((uint64_t)(*(pkt->curr + 1))) << 48; in PACKET_peek_net_8()
243 *data |= ((uint64_t)(*(pkt->curr + 2))) << 40; in PACKET_peek_net_8()
244 *data |= ((uint64_t)(*(pkt->curr + 3))) << 32; in PACKET_peek_net_8()
245 *data |= ((uint64_t)(*(pkt->curr + 4))) << 24; in PACKET_peek_net_8()
246 *data |= ((uint64_t)(*(pkt->curr + 5))) << 16; in PACKET_peek_net_8()
247 *data |= ((uint64_t)(*(pkt->curr + 6))) << 8; in PACKET_peek_net_8()
248 *data |= *(pkt->curr + 7); in PACKET_peek_net_8()
254 /* Get 4 bytes in network order from |pkt| and store the value in |*data| */
255 __owur static ossl_inline int PACKET_get_net_4(PACKET *pkt, unsigned long *data) in PACKET_get_net_4() argument
257 if (!PACKET_peek_net_4(pkt, data)) in PACKET_get_net_4()
260 packet_forward(pkt, 4); in PACKET_get_net_4()
266 __owur static ossl_inline int PACKET_get_net_4_len(PACKET *pkt, size_t *data) in PACKET_get_net_4_len() argument
269 int ret = PACKET_get_net_4(pkt, &i); in PACKET_get_net_4_len()
277 /* Get 8 bytes in network order from |pkt| and store the value in |*data| */
278 __owur static ossl_inline int PACKET_get_net_8(PACKET *pkt, uint64_t *data) in PACKET_get_net_8() argument
280 if (!PACKET_peek_net_8(pkt, data)) in PACKET_get_net_8()
283 packet_forward(pkt, 8); in PACKET_get_net_8()
288 /* Peek ahead at 1 byte from |pkt| and store the value in |*data| */
289 __owur static ossl_inline int PACKET_peek_1(const PACKET *pkt, in PACKET_peek_1() argument
292 if (!PACKET_remaining(pkt)) in PACKET_peek_1()
295 *data = *pkt->curr; in PACKET_peek_1()
300 /* Get 1 byte from |pkt| and store the value in |*data| */
301 __owur static ossl_inline int PACKET_get_1(PACKET *pkt, unsigned int *data) in PACKET_get_1() argument
303 if (!PACKET_peek_1(pkt, data)) in PACKET_get_1()
306 packet_forward(pkt, 1); in PACKET_get_1()
312 __owur static ossl_inline int PACKET_get_1_len(PACKET *pkt, size_t *data) in PACKET_get_1_len() argument
315 int ret = PACKET_get_1(pkt, &i); in PACKET_get_1_len()
324 * Peek ahead at 4 bytes in reverse network order from |pkt| and store the value
327 __owur static ossl_inline int PACKET_peek_4(const PACKET *pkt, in PACKET_peek_4() argument
330 if (PACKET_remaining(pkt) < 4) in PACKET_peek_4()
333 *data = *pkt->curr; in PACKET_peek_4()
334 *data |= ((unsigned long)(*(pkt->curr + 1))) << 8; in PACKET_peek_4()
335 *data |= ((unsigned long)(*(pkt->curr + 2))) << 16; in PACKET_peek_4()
336 *data |= ((unsigned long)(*(pkt->curr + 3))) << 24; in PACKET_peek_4()
343 * Get 4 bytes in reverse network order from |pkt| and store the value in
346 __owur static ossl_inline int PACKET_get_4(PACKET *pkt, unsigned long *data) in PACKET_get_4() argument
348 if (!PACKET_peek_4(pkt, data)) in PACKET_get_4()
351 packet_forward(pkt, 4); in PACKET_get_4()
357 * Peek ahead at |len| bytes from the |pkt| and store a pointer to them in
358 * |*data|. This just points at the underlying buffer that |pkt| is using. The
362 __owur static ossl_inline int PACKET_peek_bytes(const PACKET *pkt, in PACKET_peek_bytes() argument
366 if (PACKET_remaining(pkt) < len) in PACKET_peek_bytes()
369 *data = pkt->curr; in PACKET_peek_bytes()
375 * Read |len| bytes from the |pkt| and store a pointer to them in |*data|. This
376 * just points at the underlying buffer that |pkt| is using. The caller should
380 __owur static ossl_inline int PACKET_get_bytes(PACKET *pkt, in PACKET_get_bytes() argument
384 if (!PACKET_peek_bytes(pkt, data, len)) in PACKET_get_bytes()
387 packet_forward(pkt, len); in PACKET_get_bytes()
392 /* Peek ahead at |len| bytes from |pkt| and copy them to |data| */
393 __owur static ossl_inline int PACKET_peek_copy_bytes(const PACKET *pkt, in PACKET_peek_copy_bytes() argument
397 if (PACKET_remaining(pkt) < len) in PACKET_peek_copy_bytes()
400 memcpy(data, pkt->curr, len); in PACKET_peek_copy_bytes()
406 * Read |len| bytes from |pkt| and copy them to |data|.
409 __owur static ossl_inline int PACKET_copy_bytes(PACKET *pkt, in PACKET_copy_bytes() argument
412 if (!PACKET_peek_copy_bytes(pkt, data, len)) in PACKET_copy_bytes()
415 packet_forward(pkt, len); in PACKET_copy_bytes()
427 __owur static ossl_inline int PACKET_copy_all(const PACKET *pkt, in PACKET_copy_all() argument
431 if (PACKET_remaining(pkt) > dest_len) { in PACKET_copy_all()
435 *len = pkt->remaining; in PACKET_copy_all()
436 memcpy(dest, pkt->curr, pkt->remaining); in PACKET_copy_all()
441 * Copy |pkt| bytes to a newly allocated buffer and store a pointer to the
449 __owur static ossl_inline int PACKET_memdup(const PACKET *pkt, in PACKET_memdup() argument
458 length = PACKET_remaining(pkt); in PACKET_memdup()
463 *data = OPENSSL_memdup(pkt->curr, length); in PACKET_memdup()
472 * Read a C string from |pkt| and copy to a newly allocated, NUL-terminated
475 * If the data in |pkt| does not contain a NUL-byte, the entire data is
476 * copied and NUL-terminated.
481 __owur static ossl_inline int PACKET_strndup(const PACKET *pkt, char **data) in PACKET_strndup() argument
485 /* This will succeed on an empty packet, unless pkt->curr == NULL. */ in PACKET_strndup()
486 *data = OPENSSL_strndup((const char *)pkt->curr, PACKET_remaining(pkt)); in PACKET_strndup()
490 /* Returns 1 if |pkt| contains at least one 0-byte, 0 otherwise. */
491 static ossl_inline int PACKET_contains_zero_byte(const PACKET *pkt) in PACKET_contains_zero_byte() argument
493 return memchr(pkt->curr, 0, pkt->remaining) != NULL; in PACKET_contains_zero_byte()
497 __owur static ossl_inline int PACKET_forward(PACKET *pkt, size_t len) in PACKET_forward() argument
499 if (PACKET_remaining(pkt) < len) in PACKET_forward()
502 packet_forward(pkt, len); in PACKET_forward()
508 * Reads a variable-length vector prefixed with a one-byte length, and stores
509 * the contents in |subpkt|. |pkt| can equal |subpkt|.
511 * the original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
512 * Upon failure, the original |pkt| and |subpkt| are not modified.
514 __owur static ossl_inline int PACKET_get_length_prefixed_1(PACKET *pkt, in PACKET_get_length_prefixed_1() argument
519 PACKET tmp = *pkt; in PACKET_get_length_prefixed_1()
525 *pkt = tmp; in PACKET_get_length_prefixed_1()
526 subpkt->curr = data; in PACKET_get_length_prefixed_1()
527 subpkt->remaining = length; in PACKET_get_length_prefixed_1()
534 * leftover bytes in |pkt|.
536 __owur static ossl_inline int PACKET_as_length_prefixed_1(PACKET *pkt, in PACKET_as_length_prefixed_1() argument
541 PACKET tmp = *pkt; in PACKET_as_length_prefixed_1()
548 *pkt = tmp; in PACKET_as_length_prefixed_1()
549 subpkt->curr = data; in PACKET_as_length_prefixed_1()
550 subpkt->remaining = length; in PACKET_as_length_prefixed_1()
556 * Reads a variable-length vector prefixed with a two-byte length, and stores
557 * the contents in |subpkt|. |pkt| can equal |subpkt|.
559 * the original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
560 * Upon failure, the original |pkt| and |subpkt| are not modified.
562 __owur static ossl_inline int PACKET_get_length_prefixed_2(PACKET *pkt, in PACKET_get_length_prefixed_2() argument
567 PACKET tmp = *pkt; in PACKET_get_length_prefixed_2()
574 *pkt = tmp; in PACKET_get_length_prefixed_2()
575 subpkt->curr = data; in PACKET_get_length_prefixed_2()
576 subpkt->remaining = length; in PACKET_get_length_prefixed_2()
583 * leftover bytes in |pkt|.
585 __owur static ossl_inline int PACKET_as_length_prefixed_2(PACKET *pkt, in PACKET_as_length_prefixed_2() argument
590 PACKET tmp = *pkt; in PACKET_as_length_prefixed_2()
598 *pkt = tmp; in PACKET_as_length_prefixed_2()
599 subpkt->curr = data; in PACKET_as_length_prefixed_2()
600 subpkt->remaining = length; in PACKET_as_length_prefixed_2()
606 * Reads a variable-length vector prefixed with a three-byte length, and stores
607 * the contents in |subpkt|. |pkt| can equal |subpkt|.
609 * the original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
610 * Upon failure, the original |pkt| and |subpkt| are not modified.
612 __owur static ossl_inline int PACKET_get_length_prefixed_3(PACKET *pkt, in PACKET_get_length_prefixed_3() argument
617 PACKET tmp = *pkt; in PACKET_get_length_prefixed_3()
623 *pkt = tmp; in PACKET_get_length_prefixed_3()
624 subpkt->curr = data; in PACKET_get_length_prefixed_3()
625 subpkt->remaining = length; in PACKET_get_length_prefixed_3()
649 /* Flags for this sub-packet */
673 /* Our sub-packets (always at least one if not finished) */
694 /* QUIC variable-length integer length prefix */
703 int WPACKET_init_len(WPACKET *pkt, BUF_MEM *buf, size_t lenbytes);
709 int WPACKET_init(WPACKET *pkt, BUF_MEM *buf);
716 int WPACKET_init_null(WPACKET *pkt, size_t lenbytes);
720 * encoding for sub-packets.
722 int WPACKET_init_null_der(WPACKET *pkt);
726 * A fixed buffer of memory |buf| of size |len| is used instead. A failure will
729 int WPACKET_init_static_len(WPACKET *pkt, unsigned char *buf, size_t len,
735 * DER length encoding for sub-packets.
737 int WPACKET_init_der(WPACKET *pkt, unsigned char *buf, size_t len);
740 * Set the flags to be applied to the current sub-packet
742 int WPACKET_set_flags(WPACKET *pkt, unsigned int flags);
745 * Closes the most recent sub-packet. It also writes out the length of the
750 int WPACKET_close(WPACKET *pkt);
756 int WPACKET_finish(WPACKET *pkt);
759 * Iterate through all the sub-packets and write out their lengths as if they
761 * when the sub-packets are eventually closed (which may be different if more
762 * data is added to the WPACKET). This function fails if a sub-packet is of 0
765 int WPACKET_fill_lengths(WPACKET *pkt);
768 * Initialise a new sub-packet. Additionally |lenbytes| of data is preallocated
769 * at the start of the sub-packet to store its length once we know it. Don't
772 int WPACKET_start_sub_packet_len__(WPACKET *pkt, size_t lenbytes);
778 #define WPACKET_start_sub_packet_u8(pkt) \ argument
779 WPACKET_start_sub_packet_len__((pkt), 1)
780 #define WPACKET_start_sub_packet_u16(pkt) \ argument
781 WPACKET_start_sub_packet_len__((pkt), 2)
782 #define WPACKET_start_sub_packet_u24(pkt) \ argument
783 WPACKET_start_sub_packet_len__((pkt), 3)
784 #define WPACKET_start_sub_packet_u32(pkt) \ argument
785 WPACKET_start_sub_packet_len__((pkt), 4)
788 * Same as WPACKET_start_sub_packet_len__() except no bytes are pre-allocated
789 * for the sub-packet length.
791 int WPACKET_start_sub_packet(WPACKET *pkt);
801 int WPACKET_allocate_bytes(WPACKET *pkt, size_t len,
805 * The same as WPACKET_allocate_bytes() except additionally a new sub-packet is
807 * number of length bytes for the sub-packet is in |lenbytes|. Don't call this
810 int WPACKET_sub_allocate_bytes__(WPACKET *pkt, size_t len,
817 #define WPACKET_sub_allocate_bytes_u8(pkt, len, bytes) \ argument
818 WPACKET_sub_allocate_bytes__((pkt), (len), (bytes), 1)
819 #define WPACKET_sub_allocate_bytes_u16(pkt, len, bytes) \ argument
820 WPACKET_sub_allocate_bytes__((pkt), (len), (bytes), 2)
821 #define WPACKET_sub_allocate_bytes_u24(pkt, len, bytes) \ argument
822 WPACKET_sub_allocate_bytes__((pkt), (len), (bytes), 3)
823 #define WPACKET_sub_allocate_bytes_u32(pkt, len, bytes) \ argument
824 WPACKET_sub_allocate_bytes__((pkt), (len), (bytes), 4)
830 * maximum size will be. If this function is used, then it should be immediately
834 * For example: If we are generating a signature, then the size of that
838 * if (!WPACKET_sub_reserve_bytes_u16(&pkt, EVP_PKEY_get_size(pkey), &sigbytes1)
840 * || !WPACKET_sub_allocate_bytes_u16(&pkt, siglen, &sigbytes2)
844 int WPACKET_reserve_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes);
849 int WPACKET_sub_reserve_bytes__(WPACKET *pkt, size_t len,
855 #define WPACKET_sub_reserve_bytes_u8(pkt, len, bytes) \ argument
856 WPACKET_reserve_bytes__((pkt), (len), (bytes), 1)
857 #define WPACKET_sub_reserve_bytes_u16(pkt, len, bytes) \ argument
858 WPACKET_sub_reserve_bytes__((pkt), (len), (bytes), 2)
859 #define WPACKET_sub_reserve_bytes_u24(pkt, len, bytes) \ argument
860 WPACKET_sub_reserve_bytes__((pkt), (len), (bytes), 3)
861 #define WPACKET_sub_reserve_bytes_u32(pkt, len, bytes) \ argument
862 WPACKET_sub_reserve_bytes__((pkt), (len), (bytes), 4)
871 int WPACKET_put_bytes__(WPACKET *pkt, uint64_t val, size_t bytes);
877 #define WPACKET_put_bytes_u8(pkt, val) \ argument
878 WPACKET_put_bytes__((pkt), (val), 1)
879 #define WPACKET_put_bytes_u16(pkt, val) \ argument
880 WPACKET_put_bytes__((pkt), (val), 2)
881 #define WPACKET_put_bytes_u24(pkt, val) \ argument
882 WPACKET_put_bytes__((pkt), (val), 3)
883 #define WPACKET_put_bytes_u32(pkt, val) \ argument
884 WPACKET_put_bytes__((pkt), (val), 4)
885 #define WPACKET_put_bytes_u64(pkt, val) \ argument
886 WPACKET_put_bytes__((pkt), (val), 8)
888 /* Set a maximum size that we will not allow the WPACKET to grow beyond */
889 int WPACKET_set_max_size(WPACKET *pkt, size_t maxsize);
892 int WPACKET_memcpy(WPACKET *pkt, const void *src, size_t len);
895 int WPACKET_memset(WPACKET *pkt, int ch, size_t len);
902 int WPACKET_sub_memcpy__(WPACKET *pkt, const void *src, size_t len,
906 #define WPACKET_sub_memcpy_u8(pkt, src, len) \ argument
907 WPACKET_sub_memcpy__((pkt), (src), (len), 1)
908 #define WPACKET_sub_memcpy_u16(pkt, src, len) \ argument
909 WPACKET_sub_memcpy__((pkt), (src), (len), 2)
910 #define WPACKET_sub_memcpy_u24(pkt, src, len) \ argument
911 WPACKET_sub_memcpy__((pkt), (src), (len), 3)
912 #define WPACKET_sub_memcpy_u32(pkt, src, len) \ argument
913 WPACKET_sub_memcpy__((pkt), (src), (len), 4)
919 int WPACKET_get_total_written(WPACKET *pkt, size_t *written);
922 * Returns the length of the current sub-packet. This excludes any bytes
925 int WPACKET_get_length(WPACKET *pkt, size_t *len);
931 unsigned char *WPACKET_get_curr(WPACKET *pkt);
934 int WPACKET_is_null_buf(WPACKET *pkt);
937 void WPACKET_cleanup(WPACKET *pkt);