trafficserver-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From masa...@apache.org
Subject [trafficserver] 06/08: draft-08: Support Variable-Length Integer Encoding
Date Wed, 13 Dec 2017 23:44:38 GMT
This is an automated email from the ASF dual-hosted git repository.

masaori pushed a commit to branch quic-latest
in repository https://gitbox.apache.org/repos/asf/trafficserver.git

commit 7e7e3fcf0d2990170772cb7dd86b3ee79a60cfc2
Author: Masaori Koshiba <masaori@apache.org>
AuthorDate: Wed Dec 13 10:22:00 2017 +0900

    draft-08: Support Variable-Length Integer Encoding
---
 iocore/net/quic/QUICTypes.cc              |  72 ++++++++++++++++++++
 iocore/net/quic/QUICTypes.h               |  11 ++-
 iocore/net/quic/test/test_QUICTypeUtil.cc | 107 ++++++++++++++++++++++++++++++
 3 files changed, 189 insertions(+), 1 deletion(-)

diff --git a/iocore/net/quic/QUICTypes.cc b/iocore/net/quic/QUICTypes.cc
index 44e89bd..2c21896 100644
--- a/iocore/net/quic/QUICTypes.cc
+++ b/iocore/net/quic/QUICTypes.cc
@@ -167,3 +167,75 @@ QUICError::code()
 {
   return static_cast<uint16_t>(this->trans_error_code);
 }
+
+size_t
+QUICVariableInt::size(const uint8_t *src)
+{
+  return 1 << (src[0] >> 6);
+}
+
+size_t
+QUICVariableInt::size(uint64_t src)
+{
+  uint8_t flag = 0;
+  if (src > 4611686018427387903) {
+    // max usable bits is 62
+    return 0;
+  } else if (src > 1073741823) {
+    flag = 0x03;
+  } else if (src > 16383) {
+    flag = 0x02;
+  } else if (src > 63) {
+    flag = 0x01;
+  } else {
+    flag = 0x00;
+  }
+
+  return 1 << flag;
+}
+
+int
+QUICVariableInt::encode(uint8_t *dst, size_t dst_len, size_t &len, uint64_t src)
+{
+  uint8_t flag = 0;
+  if (src > 4611686018427387903) {
+    // max usable bits is 62
+    return 1;
+  } else if (src > 1073741823) {
+    flag = 0x03;
+  } else if (src > 16383) {
+    flag = 0x02;
+  } else if (src > 63) {
+    flag = 0x01;
+  } else {
+    flag = 0x00;
+  }
+
+  len = 1 << flag;
+  if (len > dst_len) {
+    return 1;
+  }
+
+  size_t dummy = 0;
+  QUICTypeUtil::write_uint_as_nbytes(src, len, dst, &dummy);
+  dst[0] |= (flag << 6);
+
+  return 0;
+}
+
+int
+QUICVariableInt::decode(uint64_t &dst, size_t &len, const uint8_t *src, size_t src_len)
+{
+  len = 1 << (src[0] >> 6);
+  if (src_len < len) {
+    return 1;
+  }
+
+  uint8_t buf[8] = {0};
+  memcpy(buf, src, len);
+  buf[0] &= 0x3f;
+
+  dst = QUICTypeUtil::read_nbytes_as_uint(buf, len);
+
+  return 0;
+}
diff --git a/iocore/net/quic/QUICTypes.h b/iocore/net/quic/QUICTypes.h
index 7429009..7d60131 100644
--- a/iocore/net/quic/QUICTypes.h
+++ b/iocore/net/quic/QUICTypes.h
@@ -51,7 +51,7 @@ ats_unique_buf ats_unique_malloc(size_t size);
 
 using QUICPacketNumber = uint64_t;
 using QUICVersion      = uint32_t;
-using QUICStreamId     = uint32_t;
+using QUICStreamId     = uint64_t;
 using QUICOffset       = uint64_t;
 
 // TODO: Update version number
@@ -274,3 +274,12 @@ public:
 
 private:
 };
+
+class QUICVariableInt
+{
+public:
+  static size_t size(const uint8_t *src);
+  static size_t size(uint64_t src);
+  static int encode(uint8_t *dst, size_t dst_len, size_t &len, uint64_t src);
+  static int decode(uint64_t &dst, size_t &len, const uint8_t *src, size_t src_len);
+};
diff --git a/iocore/net/quic/test/test_QUICTypeUtil.cc b/iocore/net/quic/test/test_QUICTypeUtil.cc
index 1bea4ba..99ae818 100644
--- a/iocore/net/quic/test/test_QUICTypeUtil.cc
+++ b/iocore/net/quic/test/test_QUICTypeUtil.cc
@@ -67,3 +67,110 @@ TEST_CASE("QUICTypeUtil", "[quic]")
   INFO("2 byte to 8 byte");
   CHECK(memcmp(buf, "\x00\x00\x00\x00\x00\x00\x11\xff", 8) == 0);
 }
+
+TEST_CASE("Variable Length - encoding 1", "[quic]")
+{
+  uint8_t dst[8]   = {0};
+  uint64_t src     = 151288809941952652;
+  size_t len       = 0;
+  uint8_t expect[] = {0xc2, 0x19, 0x7c, 0x5e, 0xff, 0x14, 0xe8, 0x8c};
+
+  QUICVariableInt::encode(dst, sizeof(dst), len, src);
+
+  CHECK(len == 8);
+  CHECK(memcmp(dst, expect, 8) == 0);
+}
+
+TEST_CASE("Variable Length - encoding 2", "[quic]")
+{
+  uint8_t dst[8]   = {0};
+  uint64_t src     = 494878333;
+  size_t len       = 0;
+  uint8_t expect[] = {0x9d, 0x7f, 0x3e, 0x7d};
+
+  QUICVariableInt::encode(dst, sizeof(dst), len, src);
+
+  CHECK(len == 4);
+  CHECK(memcmp(dst, expect, 4) == 0);
+}
+
+TEST_CASE("Variable Length - encoding 3", "[quic]")
+{
+  uint8_t dst[8]   = {0};
+  uint64_t src     = 15293;
+  size_t len       = 0;
+  uint8_t expect[] = {0x7b, 0xbd};
+
+  QUICVariableInt::encode(dst, sizeof(dst), len, src);
+
+  CHECK(len == 2);
+  CHECK(memcmp(dst, expect, 2) == 0);
+}
+
+TEST_CASE("Variable Length - encoding 4", "[quic]")
+{
+  uint8_t dst[8]   = {0};
+  uint64_t src     = 37;
+  size_t len       = 0;
+  uint8_t expect[] = {0x25};
+
+  QUICVariableInt::encode(dst, sizeof(dst), len, src);
+
+  CHECK(len == 1);
+  CHECK(memcmp(dst, expect, 1) == 0);
+}
+
+TEST_CASE("Variable Length - decoding 1", "[quic]")
+{
+  uint8_t src[] = {0xc2, 0x19, 0x7c, 0x5e, 0xff, 0x14, 0xe8, 0x8c};
+  uint64_t dst  = 0;
+  size_t len    = 0;
+  QUICVariableInt::decode(dst, len, src, sizeof(src));
+
+  CHECK(dst == 151288809941952652);
+  CHECK(len == 8);
+}
+
+TEST_CASE("Variable Length - decoding 2", "[quic]")
+{
+  uint8_t src[] = {0x9d, 0x7f, 0x3e, 0x7d, 0x00, 0x00, 0x00, 0x00};
+  uint64_t dst  = 0;
+  size_t len    = 0;
+  QUICVariableInt::decode(dst, len, src, sizeof(src));
+
+  CHECK(dst == 494878333);
+  CHECK(len == 4);
+}
+
+TEST_CASE("Variable Length - decoding 3", "[quic]")
+{
+  uint8_t src[] = {0x7b, 0xbd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
+  uint64_t dst  = 0;
+  size_t len    = 0;
+  QUICVariableInt::decode(dst, len, src, sizeof(src));
+
+  CHECK(dst == 15293);
+  CHECK(len == 2);
+}
+
+TEST_CASE("Variable Length - decoding 4", "[quic]")
+{
+  uint8_t src[] = {0x25, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
+  uint64_t dst  = 0;
+  size_t len    = 0;
+  QUICVariableInt::decode(dst, len, src, sizeof(src));
+
+  CHECK(dst == 37);
+  CHECK(len == 1);
+}
+
+TEST_CASE("Variable Length - decoding 5", "[quic]")
+{
+  uint8_t src[] = {0x40, 0x25, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
+  uint64_t dst  = 0;
+  size_t len    = 0;
+  QUICVariableInt::decode(dst, len, src, sizeof(src));
+
+  CHECK(dst == 37);
+  CHECK(len == 2);
+}

-- 
To stop receiving notification emails like this one, please contact
"commits@trafficserver.apache.org" <commits@trafficserver.apache.org>.

Mime
View raw message