/src/openssl32/crypto/quic_vlint.c
Line | Count | Source (jump to first uncovered line) |
1 | | #include "internal/quic_vlint.h" |
2 | | #include "internal/e_os.h" |
3 | | |
4 | | #ifndef OPENSSL_NO_QUIC |
5 | | |
6 | | void ossl_quic_vlint_encode_n(uint8_t *buf, uint64_t v, int n) |
7 | 19.4M | { |
8 | 19.4M | if (n == 1) { |
9 | 13.3M | buf[0] = (uint8_t)v; |
10 | 13.3M | } else if (n == 2) { |
11 | 1.19M | buf[0] = (uint8_t)(0x40 | ((v >> 8) & 0x3F)); |
12 | 1.19M | buf[1] = (uint8_t)v; |
13 | 4.89M | } else if (n == 4) { |
14 | 4.64M | buf[0] = (uint8_t)(0x80 | ((v >> 24) & 0x3F)); |
15 | 4.64M | buf[1] = (uint8_t)(v >> 16); |
16 | 4.64M | buf[2] = (uint8_t)(v >> 8); |
17 | 4.64M | buf[3] = (uint8_t)v; |
18 | 4.64M | } else { |
19 | 243k | buf[0] = (uint8_t)(0xC0 | ((v >> 56) & 0x3F)); |
20 | 243k | buf[1] = (uint8_t)(v >> 48); |
21 | 243k | buf[2] = (uint8_t)(v >> 40); |
22 | 243k | buf[3] = (uint8_t)(v >> 32); |
23 | 243k | buf[4] = (uint8_t)(v >> 24); |
24 | 243k | buf[5] = (uint8_t)(v >> 16); |
25 | 243k | buf[6] = (uint8_t)(v >> 8); |
26 | 243k | buf[7] = (uint8_t)v; |
27 | 243k | } |
28 | 19.4M | } |
29 | | |
30 | | void ossl_quic_vlint_encode(uint8_t *buf, uint64_t v) |
31 | 19.4M | { |
32 | 19.4M | ossl_quic_vlint_encode_n(buf, v, ossl_quic_vlint_encode_len(v)); |
33 | 19.4M | } |
34 | | |
35 | | uint64_t ossl_quic_vlint_decode_unchecked(const unsigned char *buf) |
36 | 7.16M | { |
37 | 7.16M | uint8_t first_byte = buf[0]; |
38 | 7.16M | size_t sz = ossl_quic_vlint_decode_len(first_byte); |
39 | | |
40 | 7.16M | if (sz == 1) |
41 | 6.13M | return first_byte & 0x3F; |
42 | | |
43 | 1.03M | if (sz == 2) |
44 | 596k | return ((uint64_t)(first_byte & 0x3F) << 8) |
45 | 596k | | buf[1]; |
46 | | |
47 | 438k | if (sz == 4) |
48 | 416k | return ((uint64_t)(first_byte & 0x3F) << 24) |
49 | 416k | | ((uint64_t)buf[1] << 16) |
50 | 416k | | ((uint64_t)buf[2] << 8) |
51 | 416k | | buf[3]; |
52 | | |
53 | 22.7k | return ((uint64_t)(first_byte & 0x3F) << 56) |
54 | 22.7k | | ((uint64_t)buf[1] << 48) |
55 | 22.7k | | ((uint64_t)buf[2] << 40) |
56 | 22.7k | | ((uint64_t)buf[3] << 32) |
57 | 22.7k | | ((uint64_t)buf[4] << 24) |
58 | 22.7k | | ((uint64_t)buf[5] << 16) |
59 | 22.7k | | ((uint64_t)buf[6] << 8) |
60 | 22.7k | | buf[7]; |
61 | 438k | } |
62 | | |
63 | | int ossl_quic_vlint_decode(const unsigned char *buf, size_t buf_len, uint64_t *v) |
64 | 0 | { |
65 | 0 | size_t dec_len; |
66 | 0 | uint64_t x; |
67 | |
|
68 | 0 | if (buf_len < 1) |
69 | 0 | return 0; |
70 | | |
71 | 0 | dec_len = ossl_quic_vlint_decode_len(buf[0]); |
72 | 0 | if (buf_len < dec_len) |
73 | 0 | return 0; |
74 | | |
75 | 0 | x = ossl_quic_vlint_decode_unchecked(buf); |
76 | |
|
77 | 0 | *v = x; |
78 | 0 | return dec_len; |
79 | 0 | } |
80 | | |
81 | | #endif |