lsquic_version.c revision be4cfad0
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}; 18 19 20uint32_t 21lsquic_ver2tag (unsigned version) 22{ 23 lsquic_ver_tag_t tag; 24 if (version < N_LSQVER) 25 { 26 memcpy(&tag, version_tags[version], 4); 27 return tag; 28 } 29 else 30 return 0; 31} 32 33 34enum lsquic_version 35lsquic_tag2ver (uint32_t ver_tag) 36{ 37 unsigned n; 38 for (n = 0; n < sizeof(version_tags) / sizeof(version_tags[0]); ++n) 39 if (0 == memcmp(version_tags[n], &ver_tag, sizeof(ver_tag))) 40 return n; 41 return -1; 42} 43 44 45enum lsquic_version 46lsquic_str2ver (const char *str, size_t len) 47{ 48 uint32_t tag; 49 50 if (len == sizeof(tag) && 'Q' == str[0]) 51 { 52 memcpy(&tag, str, sizeof(tag)); 53 return lsquic_tag2ver(tag); 54 } 55 else 56 return -1; 57} 58 59 60const char *const lsquic_ver2str[N_LSQVER] = { 61 [LSQVER_035] = "Q035", 62 [LSQVER_037] = "Q037", 63 [LSQVER_038] = "Q038", 64 [LSQVER_039] = "Q039", 65 [LSQVER_041] = "Q041", 66 [LSQVER_042] = "Q042", 67}; 68 69 70int 71gen_ver_tags (unsigned char *buf, size_t bufsz, unsigned version_bitmask) 72{ 73 unsigned n; 74 lsquic_ver_tag_t tag; 75 unsigned char *p = buf; 76 unsigned char *const pend = p + bufsz; 77 for (n = 0; version_bitmask; ++n) 78 { 79 if (version_bitmask & (1 << n)) 80 { 81 if (p + 4 > pend) 82 return -1; 83 version_bitmask &= ~(1 << n); 84 tag = lsquic_ver2tag(n); 85 if (0 == tag) 86 return -1; 87 memcpy(p, &tag, 4); 88 p += 4; 89 } 90 } 91 return p - buf; 92} 93