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