/src/openssl111/crypto/asn1/a_utf8.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the OpenSSL license (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 | | |
10 | | #include <stdio.h> |
11 | | #include "internal/cryptlib.h" |
12 | | #include <openssl/asn1.h> |
13 | | |
14 | | /* UTF8 utilities */ |
15 | | |
16 | | /*- |
17 | | * This parses a UTF8 string one character at a time. It is passed a pointer |
18 | | * to the string and the length of the string. It sets 'value' to the value of |
19 | | * the current character. It returns the number of characters read or a |
20 | | * negative error code: |
21 | | * -1 = string too short |
22 | | * -2 = illegal character |
23 | | * -3 = subsequent characters not of the form 10xxxxxx |
24 | | * -4 = character encoded incorrectly (not minimal length). |
25 | | */ |
26 | | |
27 | | int UTF8_getc(const unsigned char *str, int len, unsigned long *val) |
28 | 41.2k | { |
29 | 41.2k | const unsigned char *p; |
30 | 41.2k | unsigned long value; |
31 | 41.2k | int ret; |
32 | 41.2k | if (len <= 0) |
33 | 0 | return 0; |
34 | 41.2k | p = str; |
35 | | |
36 | | /* Check syntax and work out the encoded value (if correct) */ |
37 | 41.2k | if ((*p & 0x80) == 0) { |
38 | 28.4k | value = *p++ & 0x7f; |
39 | 28.4k | ret = 1; |
40 | 28.4k | } else if ((*p & 0xe0) == 0xc0) { |
41 | 683 | if (len < 2) |
42 | 69 | return -1; |
43 | 614 | if ((p[1] & 0xc0) != 0x80) |
44 | 115 | return -3; |
45 | 499 | value = (*p++ & 0x1f) << 6; |
46 | 499 | value |= *p++ & 0x3f; |
47 | 499 | if (value < 0x80) |
48 | 68 | return -4; |
49 | 431 | ret = 2; |
50 | 12.1k | } else if ((*p & 0xf0) == 0xe0) { |
51 | 538 | if (len < 3) |
52 | 12 | return -1; |
53 | 526 | if (((p[1] & 0xc0) != 0x80) |
54 | 526 | || ((p[2] & 0xc0) != 0x80)) |
55 | 106 | return -3; |
56 | 420 | value = (*p++ & 0xf) << 12; |
57 | 420 | value |= (*p++ & 0x3f) << 6; |
58 | 420 | value |= *p++ & 0x3f; |
59 | 420 | if (value < 0x800) |
60 | 201 | return -4; |
61 | 219 | ret = 3; |
62 | 11.5k | } else if ((*p & 0xf8) == 0xf0) { |
63 | 5.68k | if (len < 4) |
64 | 25 | return -1; |
65 | 5.65k | if (((p[1] & 0xc0) != 0x80) |
66 | 5.65k | || ((p[2] & 0xc0) != 0x80) |
67 | 5.65k | || ((p[3] & 0xc0) != 0x80)) |
68 | 476 | return -3; |
69 | 5.18k | value = ((unsigned long)(*p++ & 0x7)) << 18; |
70 | 5.18k | value |= (*p++ & 0x3f) << 12; |
71 | 5.18k | value |= (*p++ & 0x3f) << 6; |
72 | 5.18k | value |= *p++ & 0x3f; |
73 | 5.18k | if (value < 0x10000) |
74 | 244 | return -4; |
75 | 4.93k | ret = 4; |
76 | 5.88k | } else if ((*p & 0xfc) == 0xf8) { |
77 | 1.40k | if (len < 5) |
78 | 121 | return -1; |
79 | 1.28k | if (((p[1] & 0xc0) != 0x80) |
80 | 1.28k | || ((p[2] & 0xc0) != 0x80) |
81 | 1.28k | || ((p[3] & 0xc0) != 0x80) |
82 | 1.28k | || ((p[4] & 0xc0) != 0x80)) |
83 | 350 | return -3; |
84 | 932 | value = ((unsigned long)(*p++ & 0x3)) << 24; |
85 | 932 | value |= ((unsigned long)(*p++ & 0x3f)) << 18; |
86 | 932 | value |= ((unsigned long)(*p++ & 0x3f)) << 12; |
87 | 932 | value |= (*p++ & 0x3f) << 6; |
88 | 932 | value |= *p++ & 0x3f; |
89 | 932 | if (value < 0x200000) |
90 | 385 | return -4; |
91 | 547 | ret = 5; |
92 | 4.48k | } else if ((*p & 0xfe) == 0xfc) { |
93 | 3.96k | if (len < 6) |
94 | 207 | return -1; |
95 | 3.75k | if (((p[1] & 0xc0) != 0x80) |
96 | 3.75k | || ((p[2] & 0xc0) != 0x80) |
97 | 3.75k | || ((p[3] & 0xc0) != 0x80) |
98 | 3.75k | || ((p[4] & 0xc0) != 0x80) |
99 | 3.75k | || ((p[5] & 0xc0) != 0x80)) |
100 | 3.37k | return -3; |
101 | 374 | value = ((unsigned long)(*p++ & 0x1)) << 30; |
102 | 374 | value |= ((unsigned long)(*p++ & 0x3f)) << 24; |
103 | 374 | value |= ((unsigned long)(*p++ & 0x3f)) << 18; |
104 | 374 | value |= ((unsigned long)(*p++ & 0x3f)) << 12; |
105 | 374 | value |= (*p++ & 0x3f) << 6; |
106 | 374 | value |= *p++ & 0x3f; |
107 | 374 | if (value < 0x4000000) |
108 | 14 | return -4; |
109 | 360 | ret = 6; |
110 | 360 | } else |
111 | 522 | return -2; |
112 | 34.9k | *val = value; |
113 | 34.9k | return ret; |
114 | 41.2k | } |
115 | | |
116 | | /* |
117 | | * This takes a character 'value' and writes the UTF8 encoded value in 'str' |
118 | | * where 'str' is a buffer containing 'len' characters. Returns the number of |
119 | | * characters written or -1 if 'len' is too small. 'str' can be set to NULL |
120 | | * in which case it just returns the number of characters. It will need at |
121 | | * most 6 characters. |
122 | | */ |
123 | | |
124 | | int UTF8_putc(unsigned char *str, int len, unsigned long value) |
125 | 7.56M | { |
126 | 7.56M | if (!str) |
127 | 3.77M | len = 6; /* Maximum we will need */ |
128 | 3.79M | else if (len <= 0) |
129 | 0 | return -1; |
130 | 7.56M | if (value < 0x80) { |
131 | 1.78M | if (str) |
132 | 897k | *str = (unsigned char)value; |
133 | 1.78M | return 1; |
134 | 1.78M | } |
135 | 5.78M | if (value < 0x800) { |
136 | 634k | if (len < 2) |
137 | 0 | return -1; |
138 | 634k | if (str) { |
139 | 319k | *str++ = (unsigned char)(((value >> 6) & 0x1f) | 0xc0); |
140 | 319k | *str = (unsigned char)((value & 0x3f) | 0x80); |
141 | 319k | } |
142 | 634k | return 2; |
143 | 634k | } |
144 | 5.14M | if (value < 0x10000) { |
145 | 6.48k | if (len < 3) |
146 | 0 | return -1; |
147 | 6.48k | if (str) { |
148 | 3.31k | *str++ = (unsigned char)(((value >> 12) & 0xf) | 0xe0); |
149 | 3.31k | *str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80); |
150 | 3.31k | *str = (unsigned char)((value & 0x3f) | 0x80); |
151 | 3.31k | } |
152 | 6.48k | return 3; |
153 | 6.48k | } |
154 | 5.14M | if (value < 0x200000) { |
155 | 4.47k | if (len < 4) |
156 | 0 | return -1; |
157 | 4.47k | if (str) { |
158 | 2.25k | *str++ = (unsigned char)(((value >> 18) & 0x7) | 0xf0); |
159 | 2.25k | *str++ = (unsigned char)(((value >> 12) & 0x3f) | 0x80); |
160 | 2.25k | *str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80); |
161 | 2.25k | *str = (unsigned char)((value & 0x3f) | 0x80); |
162 | 2.25k | } |
163 | 4.47k | return 4; |
164 | 4.47k | } |
165 | 5.13M | if (value < 0x4000000) { |
166 | 93.9k | if (len < 5) |
167 | 0 | return -1; |
168 | 93.9k | if (str) { |
169 | 47.0k | *str++ = (unsigned char)(((value >> 24) & 0x3) | 0xf8); |
170 | 47.0k | *str++ = (unsigned char)(((value >> 18) & 0x3f) | 0x80); |
171 | 47.0k | *str++ = (unsigned char)(((value >> 12) & 0x3f) | 0x80); |
172 | 47.0k | *str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80); |
173 | 47.0k | *str = (unsigned char)((value & 0x3f) | 0x80); |
174 | 47.0k | } |
175 | 93.9k | return 5; |
176 | 93.9k | } |
177 | 5.04M | if (len < 6) |
178 | 0 | return -1; |
179 | 5.04M | if (str) { |
180 | 2.52M | *str++ = (unsigned char)(((value >> 30) & 0x1) | 0xfc); |
181 | 2.52M | *str++ = (unsigned char)(((value >> 24) & 0x3f) | 0x80); |
182 | 2.52M | *str++ = (unsigned char)(((value >> 18) & 0x3f) | 0x80); |
183 | 2.52M | *str++ = (unsigned char)(((value >> 12) & 0x3f) | 0x80); |
184 | 2.52M | *str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80); |
185 | 2.52M | *str = (unsigned char)((value & 0x3f) | 0x80); |
186 | 2.52M | } |
187 | 5.04M | return 6; |
188 | 5.04M | } |