Coverage Report

Created: 2025-11-09 06:45

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/h2o/deps/quicly/lib/recvstate.c
Line
Count
Source
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
6.65k
{
27
6.65k
    quicly_ranges_init_with_range(&state->received, 0, 0);
28
6.65k
    state->data_off = 0;
29
6.65k
    state->eos = UINT64_MAX;
30
6.65k
}
31
32
void quicly_recvstate_init_closed(quicly_recvstate_t *state)
33
19.9k
{
34
19.9k
    quicly_ranges_init(&state->received);
35
19.9k
    state->data_off = 0;
36
19.9k
    state->eos = 0;
37
19.9k
}
38
39
void quicly_recvstate_dispose(quicly_recvstate_t *state)
40
26.6k
{
41
26.6k
    quicly_ranges_clear(&state->received);
42
26.6k
}
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
6.65k
{
46
6.65k
    assert(!quicly_recvstate_transfer_complete(state));
47
48
    /* eos handling */
49
6.65k
    if (state->eos == UINT64_MAX) {
50
6.65k
        if (is_fin) {
51
6.65k
            state->eos = off + *len;
52
6.65k
            if (state->eos < state->received.ranges[state->received.num_ranges - 1].end)
53
0
                return QUICLY_TRANSPORT_ERROR_FINAL_SIZE;
54
6.65k
        }
55
6.65k
    } 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
6.65k
    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
6.65k
    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
6.65k
    if (*len != 0) {
77
6.65k
        int ret;
78
6.65k
        if ((ret = quicly_ranges_add(&state->received, off, off + *len)) != 0)
79
0
            return ret;
80
6.65k
        if (state->received.num_ranges > max_ranges)
81
0
            return QUICLY_ERROR_STATE_EXHAUSTION;
82
6.65k
    }
83
6.65k
    if (state->received.num_ranges == 1 && state->received.ranges[0].start == 0 && state->received.ranges[0].end == state->eos)
84
6.65k
        goto Complete;
85
86
0
    return 0;
87
88
6.65k
Complete:
89
6.65k
    quicly_ranges_clear(&state->received);
90
6.65k
    return 0;
91
6.65k
}
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
}