quic_vlint.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Copyright 2022-2023 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #ifndef OSSL_INTERNAL_QUIC_VLINT_H
  10. # define OSSL_INTERNAL_QUIC_VLINT_H
  11. # pragma once
  12. # include "internal/e_os.h"
  13. # ifndef OPENSSL_NO_QUIC
  14. /* The smallest value requiring a 1, 2, 4, or 8-byte representation. */
  15. #define OSSL_QUIC_VLINT_1B_MIN 0
  16. #define OSSL_QUIC_VLINT_2B_MIN 64
  17. #define OSSL_QUIC_VLINT_4B_MIN 16384
  18. #define OSSL_QUIC_VLINT_8B_MIN 1073741824
  19. /* The largest value representable in a given number of bytes. */
  20. #define OSSL_QUIC_VLINT_1B_MAX (OSSL_QUIC_VLINT_2B_MIN - 1)
  21. #define OSSL_QUIC_VLINT_2B_MAX (OSSL_QUIC_VLINT_4B_MIN - 1)
  22. #define OSSL_QUIC_VLINT_4B_MAX (OSSL_QUIC_VLINT_8B_MIN - 1)
  23. #define OSSL_QUIC_VLINT_8B_MAX (((uint64_t)1 << 62) - 1)
  24. /* The largest value representable as a variable-length integer. */
  25. #define OSSL_QUIC_VLINT_MAX OSSL_QUIC_VLINT_8B_MAX
  26. /*
  27. * Returns the number of bytes needed to encode v in the QUIC variable-length
  28. * integer encoding.
  29. *
  30. * Returns 0 if v exceeds OSSL_QUIC_VLINT_MAX.
  31. */
  32. static ossl_unused ossl_inline size_t ossl_quic_vlint_encode_len(uint64_t v)
  33. {
  34. if (v < OSSL_QUIC_VLINT_2B_MIN)
  35. return 1;
  36. if (v < OSSL_QUIC_VLINT_4B_MIN)
  37. return 2;
  38. if (v < OSSL_QUIC_VLINT_8B_MIN)
  39. return 4;
  40. if (v <= OSSL_QUIC_VLINT_MAX)
  41. return 8;
  42. return 0;
  43. }
  44. /*
  45. * This function writes a QUIC varable-length encoded integer to buf.
  46. * The smallest usable representation is used.
  47. *
  48. * It is the caller's responsibility to ensure that the buffer is big enough by
  49. * calling ossl_quic_vlint_encode_len(v) before calling this function.
  50. *
  51. * Precondition: buf is at least ossl_quic_vlint_enc_len(v) bytes in size
  52. * (unchecked)
  53. * Precondition: v does not exceed OSSL_QUIC_VLINT_MAX
  54. * (unchecked)
  55. */
  56. void ossl_quic_vlint_encode(unsigned char *buf, uint64_t v);
  57. /*
  58. * This function writes a QUIC variable-length encoded integer to buf. The
  59. * specified number of bytes n are used for the encoding, which means that the
  60. * encoded value may take up more space than necessary.
  61. *
  62. * It is the caller's responsibility to ensure that the buffer is of at least n
  63. * bytes, and that v is representable by a n-byte QUIC variable-length integer.
  64. * The representable ranges are:
  65. *
  66. * 1-byte encoding: [0, 2** 6-1]
  67. * 2-byte encoding: [0, 2**14-1]
  68. * 4-byte encoding: [0, 2**30-1]
  69. * 8-byte encoding: [0, 2**62-1]
  70. *
  71. * Precondition: buf is at least n bytes in size (unchecked)
  72. * Precondition: v does not exceed the representable range
  73. * (ossl_quic_vlint_encode_len(v) <= n) (unchecked)
  74. * Precondition: v does not exceed OSSL_QUIC_VLINT_MAX
  75. * (unchecked)
  76. */
  77. void ossl_quic_vlint_encode_n(unsigned char *buf, uint64_t v, int n);
  78. /*
  79. * Given the first byte of an encoded QUIC variable-length integer, returns
  80. * the number of bytes comprising the encoded integer, including the first
  81. * byte.
  82. */
  83. static ossl_unused ossl_inline size_t ossl_quic_vlint_decode_len(uint8_t first_byte)
  84. {
  85. return 1U << ((first_byte & 0xC0) >> 6);
  86. }
  87. /*
  88. * Given a buffer containing an encoded QUIC variable-length integer, returns
  89. * the decoded value. The buffer must be of at least
  90. * ossl_quic_vlint_decode_len(buf[0]) bytes in size, and the caller is responsible
  91. * for checking this.
  92. *
  93. * Precondition: buf is at least ossl_quic_vlint_decode_len(buf[0]) bytes in size
  94. * (unchecked)
  95. */
  96. uint64_t ossl_quic_vlint_decode_unchecked(const unsigned char *buf);
  97. /*
  98. * Given a buffer buf of buf_len bytes in length, attempts to decode an encoded
  99. * QUIC variable-length integer at the start of the buffer and writes the result
  100. * to *v. If buf_len is inadequate, suggesting a truncated encoded integer, the
  101. * function fails and 0 is returned. Otherwise, returns the number of bytes
  102. * consumed.
  103. *
  104. * Precondition: buf is at least buf_len bytes in size
  105. * Precondition: v (unchecked)
  106. */
  107. int ossl_quic_vlint_decode(const unsigned char *buf, size_t buf_len, uint64_t *v);
  108. # endif
  109. #endif