/src/h2o/deps/quicly/lib/recvstate.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 2017 Fastly, Kazuho Oku |
3 | | * |
4 | | * Permission is hereby granted, free of charge, to any person obtaining a copy |
5 | | * of this software and associated documentation files (the "Software"), to |
6 | | * deal in the Software without restriction, including without limitation the |
7 | | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
8 | | * sell copies of the Software, and to permit persons to whom the Software is |
9 | | * furnished to do so, subject to the following conditions: |
10 | | * |
11 | | * The above copyright notice and this permission notice shall be included in |
12 | | * all copies or substantial portions of the Software. |
13 | | * |
14 | | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
15 | | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
16 | | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
17 | | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
18 | | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
19 | | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
20 | | * IN THE SOFTWARE. |
21 | | */ |
22 | | #include "quicly/constants.h" |
23 | | #include "quicly/recvstate.h" |
24 | | |
25 | | void quicly_recvstate_init(quicly_recvstate_t *state) |
26 | 0 | { |
27 | 0 | quicly_ranges_init_with_range(&state->received, 0, 0); |
28 | 0 | state->data_off = 0; |
29 | 0 | state->eos = UINT64_MAX; |
30 | 0 | } |
31 | | |
32 | | void quicly_recvstate_init_closed(quicly_recvstate_t *state) |
33 | 0 | { |
34 | 0 | quicly_ranges_init(&state->received); |
35 | 0 | state->data_off = 0; |
36 | 0 | state->eos = 0; |
37 | 0 | } |
38 | | |
39 | | void quicly_recvstate_dispose(quicly_recvstate_t *state) |
40 | 0 | { |
41 | 0 | quicly_ranges_clear(&state->received); |
42 | 0 | } |
43 | | |
44 | | quicly_error_t quicly_recvstate_update(quicly_recvstate_t *state, uint64_t off, size_t *len, int is_fin, size_t max_ranges) |
45 | 0 | { |
46 | 0 | assert(!quicly_recvstate_transfer_complete(state)); |
47 | | |
48 | | /* eos handling */ |
49 | 0 | if (state->eos == UINT64_MAX) { |
50 | 0 | if (is_fin) { |
51 | 0 | state->eos = off + *len; |
52 | 0 | if (state->eos < state->received.ranges[state->received.num_ranges - 1].end) |
53 | 0 | return QUICLY_TRANSPORT_ERROR_FINAL_SIZE; |
54 | 0 | } |
55 | 0 | } else { |
56 | 0 | if (off + *len > state->eos) |
57 | 0 | return QUICLY_TRANSPORT_ERROR_FINAL_SIZE; |
58 | 0 | } |
59 | | |
60 | | /* no state change; entire data has already been received */ |
61 | 0 | if (off + *len <= state->data_off) { |
62 | 0 | *len = 0; |
63 | 0 | if (state->received.ranges[0].end == state->eos) |
64 | 0 | goto Complete; |
65 | 0 | return 0; |
66 | 0 | } |
67 | | |
68 | | /* adjust if partially received */ |
69 | 0 | if (off < state->data_off) { |
70 | 0 | size_t delta = state->data_off - off; |
71 | 0 | off += delta; |
72 | 0 | *len -= delta; |
73 | 0 | } |
74 | | |
75 | | /* update received range */ |
76 | 0 | if (*len != 0) { |
77 | 0 | int ret; |
78 | 0 | if ((ret = quicly_ranges_add(&state->received, off, off + *len)) != 0) |
79 | 0 | return ret; |
80 | 0 | if (state->received.num_ranges > max_ranges) |
81 | 0 | return QUICLY_ERROR_STATE_EXHAUSTION; |
82 | 0 | } |
83 | 0 | if (state->received.num_ranges == 1 && state->received.ranges[0].start == 0 && state->received.ranges[0].end == state->eos) |
84 | 0 | goto Complete; |
85 | | |
86 | 0 | return 0; |
87 | | |
88 | 0 | Complete: |
89 | 0 | quicly_ranges_clear(&state->received); |
90 | 0 | return 0; |
91 | 0 | } |
92 | | |
93 | | quicly_error_t quicly_recvstate_reset(quicly_recvstate_t *state, uint64_t eos_at, uint64_t *bytes_missing) |
94 | 0 | { |
95 | 0 | assert(!quicly_recvstate_transfer_complete(state)); |
96 | | |
97 | | /* validate */ |
98 | 0 | if (state->eos != UINT64_MAX && state->eos != eos_at) |
99 | 0 | return QUICLY_TRANSPORT_ERROR_FINAL_SIZE; |
100 | 0 | if (eos_at < state->received.ranges[state->received.num_ranges - 1].end) |
101 | 0 | return QUICLY_TRANSPORT_ERROR_FINAL_SIZE; |
102 | | |
103 | | /* calculate bytes missing */ |
104 | 0 | *bytes_missing = eos_at - state->received.ranges[state->received.num_ranges - 1].end; |
105 | | |
106 | | /* clear the received range */ |
107 | 0 | quicly_ranges_clear(&state->received); |
108 | |
|
109 | 0 | return 0; |
110 | 0 | } |