/src/samba/third_party/ngtcp2/lib/ngtcp2_range.c
Line | Count | Source |
1 | | /* |
2 | | * ngtcp2 |
3 | | * |
4 | | * Copyright (c) 2017 ngtcp2 contributors |
5 | | * |
6 | | * Permission is hereby granted, free of charge, to any person obtaining |
7 | | * a copy of this software and associated documentation files (the |
8 | | * "Software"), to deal in the Software without restriction, including |
9 | | * without limitation the rights to use, copy, modify, merge, publish, |
10 | | * distribute, sublicense, and/or sell copies of the Software, and to |
11 | | * permit persons to whom the Software is furnished to do so, subject to |
12 | | * the following conditions: |
13 | | * |
14 | | * The above copyright notice and this permission notice shall be |
15 | | * included in all copies or substantial portions of the Software. |
16 | | * |
17 | | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
18 | | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
19 | | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
20 | | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
21 | | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
22 | | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
23 | | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
24 | | */ |
25 | | #include "ngtcp2_range.h" |
26 | | #include "ngtcp2_macro.h" |
27 | | |
28 | 0 | void ngtcp2_range_init(ngtcp2_range *r, uint64_t begin, uint64_t end) { |
29 | 0 | *r = (ngtcp2_range){ |
30 | 0 | .begin = begin, |
31 | 0 | .end = end, |
32 | 0 | }; |
33 | 0 | } |
34 | | |
35 | | ngtcp2_range ngtcp2_range_intersect(const ngtcp2_range *a, |
36 | 0 | const ngtcp2_range *b) { |
37 | 0 | ngtcp2_range r; |
38 | 0 | uint64_t begin = ngtcp2_max_uint64(a->begin, b->begin); |
39 | 0 | uint64_t end = ngtcp2_min_uint64(a->end, b->end); |
40 | |
|
41 | 0 | if (begin < end) { |
42 | 0 | ngtcp2_range_init(&r, begin, end); |
43 | 0 | } else { |
44 | 0 | r = (ngtcp2_range){0}; |
45 | 0 | } |
46 | |
|
47 | 0 | return r; |
48 | 0 | } |
49 | | |
50 | 0 | uint64_t ngtcp2_range_len(const ngtcp2_range *r) { return r->end - r->begin; } |
51 | | |
52 | 0 | int ngtcp2_range_eq(const ngtcp2_range *a, const ngtcp2_range *b) { |
53 | 0 | return a->begin == b->begin && a->end == b->end; |
54 | 0 | } |
55 | | |
56 | | void ngtcp2_range_cut(ngtcp2_range *left, ngtcp2_range *right, |
57 | 0 | const ngtcp2_range *a, const ngtcp2_range *b) { |
58 | | /* Assume that b is included in a */ |
59 | 0 | *left = (ngtcp2_range){ |
60 | 0 | .begin = a->begin, |
61 | 0 | .end = b->begin, |
62 | 0 | }; |
63 | 0 | *right = (ngtcp2_range){ |
64 | 0 | .begin = b->end, |
65 | 0 | .end = a->end, |
66 | 0 | }; |
67 | 0 | } |
68 | | |
69 | 0 | int ngtcp2_range_not_after(const ngtcp2_range *a, const ngtcp2_range *b) { |
70 | 0 | return a->end <= b->end; |
71 | 0 | } |