Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/asn1/a_utf8.c

raw
1 // Copyright (C) 1995-1998 Eric Young ([email protected]) All rights reserved.
2 // SPDX-License-Identifier: Apache-2.0
3
4 #include <openssl/asn1.h>
5
6 #include <openssl/err.h>
7 #include <openssl/mem.h>
8
9 #include "internal.h"
10
11 // UTF8 utilities
12
13 // This parses a UTF8 string one character at a time. It is passed a pointer
14 // to the string and the length of the string. It sets 'value' to the value
15 // of the current character. It returns the number of characters read or a
16 // negative error code: -1 = string too short -2 = illegal character -3 =
17 // subsequent characters not of the form 10xxxxxx -4 = character encoded
18 // incorrectly (not minimal length).
19
20 int UTF8_getc(const unsigned char *str, int len, uint32_t *val) {
21 const unsigned char *p;
22 uint32_t value;
23 int ret;
24 if (len <= 0) {
25 return 0;
26 }
27 p = str;
28
29 // Check syntax and work out the encoded value (if correct)
30 if ((*p & 0x80) == 0) {
31 value = *p++ & 0x7f;
32 ret = 1;
33 } else if ((*p & 0xe0) == 0xc0) {
34 if (len < 2) {
35 return -1;
36 }
37 if ((p[1] & 0xc0) != 0x80) {
38 return -3;
39 }
40 value = (*p++ & 0x1f) << 6;
41 value |= *p++ & 0x3f;
42 if (value < 0x80) {
43 return -4;
44 }
45 ret = 2;
46 } else if ((*p & 0xf0) == 0xe0) {
47 if (len < 3) {
48 return -1;
49 }
50 if (((p[1] & 0xc0) != 0x80) || ((p[2] & 0xc0) != 0x80)) {
51 return -3;
52 }
53 value = (*p++ & 0xf) << 12;
54 value |= (*p++ & 0x3f) << 6;
55 value |= *p++ & 0x3f;
56 if (value < 0x800) {
57 return -4;
58 }
59 ret = 3;
60 } else if ((*p & 0xf8) == 0xf0) {
61 if (len < 4) {
62 return -1;
63 }
64 if (((p[1] & 0xc0) != 0x80) || ((p[2] & 0xc0) != 0x80) ||
65 ((p[3] & 0xc0) != 0x80)) {
66 return -3;
67 }
68 value = ((uint32_t)(*p++ & 0x7)) << 18;
69 value |= (*p++ & 0x3f) << 12;
70 value |= (*p++ & 0x3f) << 6;
71 value |= *p++ & 0x3f;
72 if (value < 0x10000) {
73 return -4;
74 }
75 ret = 4;
76 } else if ((*p & 0xfc) == 0xf8) {
77 if (len < 5) {
78 return -1;
79 }
80 if (((p[1] & 0xc0) != 0x80) || ((p[2] & 0xc0) != 0x80) ||
81 ((p[3] & 0xc0) != 0x80) || ((p[4] & 0xc0) != 0x80)) {
82 return -3;
83 }
84 value = ((uint32_t)(*p++ & 0x3)) << 24;
85 value |= ((uint32_t)(*p++ & 0x3f)) << 18;
86 value |= ((uint32_t)(*p++ & 0x3f)) << 12;
87 value |= (*p++ & 0x3f) << 6;
88 value |= *p++ & 0x3f;
89 if (value < 0x200000) {
90 return -4;
91 }
92 ret = 5;
93 } else if ((*p & 0xfe) == 0xfc) {
94 if (len < 6) {
95 return -1;
96 }
97 if (((p[1] & 0xc0) != 0x80) || ((p[2] & 0xc0) != 0x80) ||
98 ((p[3] & 0xc0) != 0x80) || ((p[4] & 0xc0) != 0x80) ||
99 ((p[5] & 0xc0) != 0x80)) {
100 return -3;
101 }
102 value = ((uint32_t)(*p++ & 0x1)) << 30;
103 value |= ((uint32_t)(*p++ & 0x3f)) << 24;
104 value |= ((uint32_t)(*p++ & 0x3f)) << 18;
105 value |= ((uint32_t)(*p++ & 0x3f)) << 12;
106 value |= (*p++ & 0x3f) << 6;
107 value |= *p++ & 0x3f;
108 if (value < 0x4000000) {
109 return -4;
110 }
111 ret = 6;
112 } else {
113 return -2;
114 }
115 *val = value;
116 return ret;
117 }
118
119 // This takes a character 'value' and writes the UTF8 encoded value in 'str'
120 // where 'str' is a buffer containing 'len' characters. Returns the number of
121 // characters written or -1 if 'len' is too small. 'str' can be set to NULL
122 // in which case it just returns the number of characters. It will need at
123 // most 6 characters.
124
125 int UTF8_putc(unsigned char *str, int len, uint32_t value) {
126 if (!str) {
127 len = 6; // Maximum we will need
128 } else if (len <= 0) {
129 return -1;
130 }
131 if (value < 0x80) {
132 if (str) {
133 *str = (unsigned char)value;
134 }
135 return 1;
136 }
137 if (value < 0x800) {
138 if (len < 2) {
139 return -1;
140 }
141 if (str) {
142 *str++ = (unsigned char)(((value >> 6) & 0x1f) | 0xc0);
143 *str = (unsigned char)((value & 0x3f) | 0x80);
144 }
145 return 2;
146 }
147 if (value < 0x10000) {
148 if (len < 3) {
149 return -1;
150 }
151 if (str) {
152 *str++ = (unsigned char)(((value >> 12) & 0xf) | 0xe0);
153 *str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80);
154 *str = (unsigned char)((value & 0x3f) | 0x80);
155 }
156 return 3;
157 }
158 if (value < 0x200000) {
159 if (len < 4) {
160 return -1;
161 }
162 if (str) {
163 *str++ = (unsigned char)(((value >> 18) & 0x7) | 0xf0);
164 *str++ = (unsigned char)(((value >> 12) & 0x3f) | 0x80);
165 *str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80);
166 *str = (unsigned char)((value & 0x3f) | 0x80);
167 }
168 return 4;
169 }
170 if (value < 0x4000000) {
171 if (len < 5) {
172 return -1;
173 }
174 if (str) {
175 *str++ = (unsigned char)(((value >> 24) & 0x3) | 0xf8);
176 *str++ = (unsigned char)(((value >> 18) & 0x3f) | 0x80);
177 *str++ = (unsigned char)(((value >> 12) & 0x3f) | 0x80);
178 *str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80);
179 *str = (unsigned char)((value & 0x3f) | 0x80);
180 }
181 return 5;
182 }
183 if (len < 6) {
184 return -1;
185 }
186 if (str) {
187 *str++ = (unsigned char)(((value >> 30) & 0x1) | 0xfc);
188 *str++ = (unsigned char)(((value >> 24) & 0x3f) | 0x80);
189 *str++ = (unsigned char)(((value >> 18) & 0x3f) | 0x80);
190 *str++ = (unsigned char)(((value >> 12) & 0x3f) | 0x80);
191 *str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80);
192 *str = (unsigned char)((value & 0x3f) | 0x80);
193 }
194 return 6;
195 }