Lines Matching +full:- +full:r

2  * Copyright 2022-2023 The OpenSSL Project Authors. All Rights Reserved.
19 * Byte-wise ring buffer which supports pushing and popping blocks of multiple
45 #define MAX_OFFSET (((uint64_t)1) << 62) /* QUIC-imposed limit */ in OSSL_SAFE_MATH_UNSIGNED()
47 static ossl_inline int ring_buf_init(struct ring_buf *r) in OSSL_SAFE_MATH_UNSIGNED()
49 r->start = NULL; in OSSL_SAFE_MATH_UNSIGNED()
50 r->alloc = 0; in OSSL_SAFE_MATH_UNSIGNED()
51 r->head_offset = r->ctail_offset = 0; in OSSL_SAFE_MATH_UNSIGNED()
55 static ossl_inline void ring_buf_destroy(struct ring_buf *r, int cleanse) in ring_buf_destroy() argument
58 OPENSSL_clear_free(r->start, r->alloc); in ring_buf_destroy()
60 OPENSSL_free(r->start); in ring_buf_destroy()
61 r->start = NULL; in ring_buf_destroy()
62 r->alloc = 0; in ring_buf_destroy()
65 static ossl_inline size_t ring_buf_used(struct ring_buf *r) in ring_buf_used() argument
67 return (size_t)(r->head_offset - r->ctail_offset); in ring_buf_used()
70 static ossl_inline size_t ring_buf_avail(struct ring_buf *r) in ring_buf_avail() argument
72 return r->alloc - ring_buf_used(r); in ring_buf_avail()
75 static ossl_inline int ring_buf_write_at(struct ring_buf *r, in ring_buf_write_at() argument
81 unsigned char *start = r->start; in ring_buf_write_at()
84 avail = ring_buf_avail(r); in ring_buf_write_at()
85 if (logical_offset < r->ctail_offset in ring_buf_write_at()
87 > safe_add_u64(r->head_offset, avail, &err) in ring_buf_write_at()
88 || safe_add_u64(r->head_offset, buf_len, &err) in ring_buf_write_at()
94 idx = logical_offset % r->alloc; in ring_buf_write_at()
95 l = r->alloc - idx; in ring_buf_write_at()
100 if (r->head_offset < logical_offset + l) in ring_buf_write_at()
101 r->head_offset = logical_offset + l; in ring_buf_write_at()
105 buf_len -= l; in ring_buf_write_at()
113 static ossl_inline size_t ring_buf_push(struct ring_buf *r, in ring_buf_push() argument
118 unsigned char *start = r->start; in ring_buf_push()
121 avail = ring_buf_avail(r); in ring_buf_push()
125 if (buf_len > MAX_OFFSET - r->head_offset) in ring_buf_push()
126 buf_len = (size_t)(MAX_OFFSET - r->head_offset); in ring_buf_push()
131 idx = r->head_offset % r->alloc; in ring_buf_push()
132 l = r->alloc - idx; in ring_buf_push()
137 r->head_offset += l; in ring_buf_push()
139 buf_len -= l; in ring_buf_push()
146 static ossl_inline const unsigned char *ring_buf_get_ptr(const struct ring_buf *r, in ring_buf_get_ptr() argument
150 unsigned char *start = r->start; in ring_buf_get_ptr()
153 if (logical_offset >= r->head_offset || logical_offset < r->ctail_offset) in ring_buf_get_ptr()
155 idx = logical_offset % r->alloc; in ring_buf_get_ptr()
156 *max_len = r->alloc - idx; in ring_buf_get_ptr()
172 static ossl_inline int ring_buf_get_buf_at(const struct ring_buf *r, in ring_buf_get_buf_at() argument
177 const unsigned char *start = r->start; in ring_buf_get_buf_at()
180 if (logical_offset > r->head_offset || logical_offset < r->ctail_offset) in ring_buf_get_buf_at()
183 if (r->alloc == 0) { in ring_buf_get_buf_at()
189 idx = logical_offset % r->alloc; in ring_buf_get_buf_at()
190 l = (size_t)(r->head_offset - logical_offset); in ring_buf_get_buf_at()
191 if (l > r->alloc - idx) in ring_buf_get_buf_at()
192 l = r->alloc - idx; in ring_buf_get_buf_at()
199 static ossl_inline void ring_buf_cpop_range(struct ring_buf *r, in ring_buf_cpop_range() argument
205 if (start > r->ctail_offset || end >= MAX_OFFSET) in ring_buf_cpop_range()
208 if (cleanse && r->alloc > 0 && end > r->ctail_offset) { in ring_buf_cpop_range()
209 size_t idx = r->ctail_offset % r->alloc; in ring_buf_cpop_range()
213 if (cleanse_end > r->head_offset) in ring_buf_cpop_range()
214 cleanse_end = r->head_offset; in ring_buf_cpop_range()
215 l = (size_t)(cleanse_end - r->ctail_offset); in ring_buf_cpop_range()
216 if (l > r->alloc - idx) { in ring_buf_cpop_range()
217 OPENSSL_cleanse((unsigned char *)r->start + idx, r->alloc - idx); in ring_buf_cpop_range()
218 l -= r->alloc - idx; in ring_buf_cpop_range()
222 OPENSSL_cleanse((unsigned char *)r->start + idx, l); in ring_buf_cpop_range()
225 r->ctail_offset = end + 1; in ring_buf_cpop_range()
227 if (r->head_offset < r->ctail_offset) in ring_buf_cpop_range()
228 r->head_offset = r->ctail_offset; in ring_buf_cpop_range()
231 static ossl_inline int ring_buf_resize(struct ring_buf *r, size_t num_bytes, in ring_buf_resize() argument
238 if (num_bytes == r->alloc) in ring_buf_resize()
241 if (num_bytes < ring_buf_used(r)) in ring_buf_resize()
249 rnew.head_offset = r->head_offset - ring_buf_used(r); in ring_buf_resize()
253 if (!ring_buf_get_buf_at(r, r->ctail_offset + copied, &src, &src_len)) { in ring_buf_resize()
269 assert(rnew.head_offset == r->head_offset); in ring_buf_resize()
270 rnew.ctail_offset = r->ctail_offset; in ring_buf_resize()
272 ring_buf_destroy(r, cleanse); in ring_buf_resize()
273 memcpy(r, &rnew, sizeof(*r)); in ring_buf_resize()