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