lsquic_version.c revision c95974e9
1/* Copyright (c) 2017 - 2018 LiteSpeed Technologies Inc.  See LICENSE. */
2#include <string.h>
3
4#include "lsquic.h"
5#include "lsquic_int_types.h"
6#include "lsquic_version.h"
7
8
9static const unsigned char version_tags[N_LSQVER][4] =
10{
11    [LSQVER_035] = { 'Q', '0', '3', '5', },
12    [LSQVER_037] = { 'Q', '0', '3', '7', },
13    [LSQVER_038] = { 'Q', '0', '3', '8', },
14    [LSQVER_039] = { 'Q', '0', '3', '9', },
15    [LSQVER_041] = { 'Q', '0', '4', '1', },
16    [LSQVER_042] = { 'Q', '0', '4', '2', },
17    [LSQVER_043] = { 'Q', '0', '4', '3', },
18};
19
20
21uint32_t
22lsquic_ver2tag (unsigned version)
23{
24    lsquic_ver_tag_t tag;
25    if (version < N_LSQVER)
26    {
27        memcpy(&tag, version_tags[version], 4);
28        return tag;
29    }
30    else
31        return 0;
32}
33
34
35enum lsquic_version
36lsquic_tag2ver (uint32_t ver_tag)
37{
38    unsigned n;
39    for (n = 0; n < sizeof(version_tags) / sizeof(version_tags[0]); ++n)
40        if (0 == memcmp(version_tags[n], &ver_tag, sizeof(ver_tag)))
41            return n;
42    return -1;
43}
44
45
46enum lsquic_version
47lsquic_str2ver (const char *str, size_t len)
48{
49    uint32_t tag;
50
51    if (len == sizeof(tag) && 'Q' == str[0])
52    {
53        memcpy(&tag, str, sizeof(tag));
54        return lsquic_tag2ver(tag);
55    }
56    else
57        return -1;
58}
59
60
61const char *const lsquic_ver2str[N_LSQVER] = {
62    [LSQVER_035] = "Q035",
63    [LSQVER_037] = "Q037",
64    [LSQVER_038] = "Q038",
65    [LSQVER_039] = "Q039",
66    [LSQVER_041] = "Q041",
67    [LSQVER_042] = "Q042",
68    [LSQVER_043] = "Q043",
69};
70
71
72int
73gen_ver_tags (unsigned char *buf, size_t bufsz, unsigned version_bitmask)
74{
75    unsigned n;
76    lsquic_ver_tag_t tag;
77    unsigned char *p = buf;
78    unsigned char *const pend = p + bufsz;
79    for (n = 0; version_bitmask; ++n)
80    {
81        if (version_bitmask & (1 << n))
82        {
83            if (p + 4 > pend)
84                return -1;
85            version_bitmask &= ~(1 << n);
86            tag = lsquic_ver2tag(n);
87            if (0 == tag)
88                return -1;
89            memcpy(p, &tag, 4);
90            p += 4;
91        }
92    }
93    return p - buf;
94}
95