lsquic_packet_gquic.c revision 5392f7a3
1/* Copyright (c) 2017 - 2019 LiteSpeed Technologies Inc.  See LICENSE. */
2#include <stdint.h>
3#include <stdlib.h>
4
5#include "lsquic_packet_common.h"
6#include "lsquic_packet_gquic.h"
7
8
9
10lsquic_packno_t
11restore_packno (lsquic_packno_t cur_packno,
12                unsigned len,
13                lsquic_packno_t max_packno)
14{
15    lsquic_packno_t candidates[3], epoch_delta;
16    int64_t diffs[3];
17    unsigned min;
18
19    epoch_delta = 1ULL << (len << 3);
20    candidates[1] = (max_packno & ~(epoch_delta - 1)) + cur_packno;
21    candidates[0] = candidates[1] - epoch_delta;
22    candidates[2] = candidates[1] + epoch_delta;
23
24    diffs[0] = llabs((int64_t) candidates[0] - (int64_t) max_packno);
25    diffs[1] = llabs((int64_t) candidates[1] - (int64_t) max_packno);
26    diffs[2] = llabs((int64_t) candidates[2] - (int64_t) max_packno);
27
28    min = diffs[1] < diffs[0];
29    if (diffs[2] < diffs[min])
30        min = 2;
31
32    return candidates[min];
33}
34