Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/bytestring/unicode.c

raw
1 // Copyright (c) 2018, Google Inc.
2 // SPDX-License-Identifier: ISC
3
4 #include <openssl/bytestring.h>
5
6 #include "internal.h"
7
8
9 static int is_valid_code_point(uint32_t v) {
10 // References in the following are to Unicode 15.0.0.
11 if (// The Unicode space runs from zero to 0x10ffff (3.4 D9).
12 v > 0x10ffff ||
13 // Values 0x...fffe, 0x...ffff, and 0xfdd0-0xfdef are permanently reserved
14 // as noncharacters (3.4 D14). See also 23.7. As our APIs are intended for
15 // "open interchange", such as ASN.1, we reject them.
16 (v & 0xfffe) == 0xfffe ||
17 (v >= 0xfdd0 && v <= 0xfdef) ||
18 // Surrogate code points are invalid (3.2 C1).
19 (v >= 0xd800 && v <= 0xdfff)) {
20 return 0;
21 }
22 return 1;
23 }
24
25 // BOTTOM_BITS returns a byte with the bottom |n| bits set.
26 #define BOTTOM_BITS(n) (uint8_t)((1u << (n)) - 1)
27
28 // TOP_BITS returns a byte with the top |n| bits set.
29 #define TOP_BITS(n) ((uint8_t)~BOTTOM_BITS(8 - (n)))
30
31 int cbs_get_utf8(CBS *cbs, uint32_t *out) {
32 uint8_t c;
33 if (!CBS_get_u8(cbs, &c)) {
34 return 0;
35 }
36 if (c <= 0x7f) {
37 *out = c;
38 return 1;
39 }
40 uint32_t v, lower_bound;
41 size_t len;
42 if ((c & TOP_BITS(3)) == TOP_BITS(2)) {
43 v = c & BOTTOM_BITS(5);
44 len = 1;
45 lower_bound = 0x80;
46 } else if ((c & TOP_BITS(4)) == TOP_BITS(3)) {
47 v = c & BOTTOM_BITS(4);
48 len = 2;
49 lower_bound = 0x800;
50 } else if ((c & TOP_BITS(5)) == TOP_BITS(4)) {
51 v = c & BOTTOM_BITS(3);
52 len = 3;
53 lower_bound = 0x10000;
54 } else {
55 return 0;
56 }
57 for (size_t i = 0; i < len; i++) {
58 if (!CBS_get_u8(cbs, &c) ||
59 (c & TOP_BITS(2)) != TOP_BITS(1)) {
60 return 0;
61 }
62 v <<= 6;
63 v |= c & BOTTOM_BITS(6);
64 }
65 if (!is_valid_code_point(v) ||
66 v < lower_bound) {
67 return 0;
68 }
69 *out = v;
70 return 1;
71 }
72
73 int cbs_get_latin1(CBS *cbs, uint32_t *out) {
74 uint8_t c;
75 if (!CBS_get_u8(cbs, &c)) {
76 return 0;
77 }
78 *out = c;
79 return 1;
80 }
81
82 int cbs_get_ucs2_be(CBS *cbs, uint32_t *out) {
83 // Note UCS-2 (used by BMPString) does not support surrogates.
84 uint16_t c;
85 if (!CBS_get_u16(cbs, &c) ||
86 !is_valid_code_point(c)) {
87 return 0;
88 }
89 *out = c;
90 return 1;
91 }
92
93 int cbs_get_utf32_be(CBS *cbs, uint32_t *out) {
94 return CBS_get_u32(cbs, out) && is_valid_code_point(*out);
95 }
96
97 size_t cbb_get_utf8_len(uint32_t u) {
98 if (u <= 0x7f) {
99 return 1;
100 }
101 if (u <= 0x7ff) {
102 return 2;
103 }
104 if (u <= 0xffff) {
105 return 3;
106 }
107 return 4;
108 }
109
110 int cbb_add_utf8(CBB *cbb, uint32_t u) {
111 if (!is_valid_code_point(u)) {
112 return 0;
113 }
114 if (u <= 0x7f) {
115 return CBB_add_u8(cbb, (uint8_t)u);
116 }
117 if (u <= 0x7ff) {
118 return CBB_add_u8(cbb, TOP_BITS(2) | (u >> 6)) &&
119 CBB_add_u8(cbb, TOP_BITS(1) | (u & BOTTOM_BITS(6)));
120 }
121 if (u <= 0xffff) {
122 return CBB_add_u8(cbb, TOP_BITS(3) | (u >> 12)) &&
123 CBB_add_u8(cbb, TOP_BITS(1) | ((u >> 6) & BOTTOM_BITS(6))) &&
124 CBB_add_u8(cbb, TOP_BITS(1) | (u & BOTTOM_BITS(6)));
125 }
126 if (u <= 0x10ffff) {
127 return CBB_add_u8(cbb, TOP_BITS(4) | (u >> 18)) &&
128 CBB_add_u8(cbb, TOP_BITS(1) | ((u >> 12) & BOTTOM_BITS(6))) &&
129 CBB_add_u8(cbb, TOP_BITS(1) | ((u >> 6) & BOTTOM_BITS(6))) &&
130 CBB_add_u8(cbb, TOP_BITS(1) | (u & BOTTOM_BITS(6)));
131 }
132 return 0;
133 }
134
135 int cbb_add_latin1(CBB *cbb, uint32_t u) {
136 return u <= 0xff && CBB_add_u8(cbb, (uint8_t)u);
137 }
138
139 int cbb_add_ucs2_be(CBB *cbb, uint32_t u) {
140 return u <= 0xffff && is_valid_code_point(u) && CBB_add_u16(cbb, (uint16_t)u);
141 }
142
143 int cbb_add_utf32_be(CBB *cbb, uint32_t u) {
144 return is_valid_code_point(u) && CBB_add_u32(cbb, u);
145 }