/src/openssl30/crypto/asn1/a_utf8.c
| Line | Count | Source (jump to first uncovered line) | 
| 1 |  | /* | 
| 2 |  |  * Copyright 1995-2021 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 |  |  | 
| 10 |  | #include <stdio.h> | 
| 11 |  | #include "internal/cryptlib.h" | 
| 12 |  | #include "internal/unicode.h" | 
| 13 |  | #include <openssl/asn1.h> | 
| 14 |  |  | 
| 15 |  | /* UTF8 utilities */ | 
| 16 |  |  | 
| 17 |  | /*- | 
| 18 |  |  * This parses a UTF8 string one character at a time. It is passed a pointer | 
| 19 |  |  * to the string and the length of the string. It sets 'value' to the value of | 
| 20 |  |  * the current character. It returns the number of characters read or a | 
| 21 |  |  * negative error code: | 
| 22 |  |  * -1 = string too short | 
| 23 |  |  * -2 = illegal character | 
| 24 |  |  * -3 = subsequent characters not of the form 10xxxxxx | 
| 25 |  |  * -4 = character encoded incorrectly (not minimal length). | 
| 26 |  |  */ | 
| 27 |  |  | 
| 28 |  | int UTF8_getc(const unsigned char *str, int len, unsigned long *val) | 
| 29 | 3.68M | { | 
| 30 | 3.68M |     const unsigned char *p; | 
| 31 | 3.68M |     unsigned long value; | 
| 32 | 3.68M |     int ret; | 
| 33 | 3.68M |     if (len <= 0) | 
| 34 | 0 |         return 0; | 
| 35 | 3.68M |     p = str; | 
| 36 |  |  | 
| 37 |  |     /* Check syntax and work out the encoded value (if correct) */ | 
| 38 | 3.68M |     if ((*p & 0x80) == 0) { | 
| 39 | 2.44M |         value = *p++ & 0x7f; | 
| 40 | 2.44M |         ret = 1; | 
| 41 | 2.44M |     } else if ((*p & 0xe0) == 0xc0) { | 
| 42 | 488k |         if (len < 2) | 
| 43 | 3.86k |             return -1; | 
| 44 | 484k |         if ((p[1] & 0xc0) != 0x80) | 
| 45 | 4.78k |             return -3; | 
| 46 | 480k |         value = (*p++ & 0x1f) << 6; | 
| 47 | 480k |         value |= *p++ & 0x3f; | 
| 48 | 480k |         if (value < 0x80) | 
| 49 | 975 |             return -4; | 
| 50 | 479k |         ret = 2; | 
| 51 | 746k |     } else if ((*p & 0xf0) == 0xe0) { | 
| 52 | 719k |         if (len < 3) | 
| 53 | 1.87k |             return -1; | 
| 54 | 717k |         if (((p[1] & 0xc0) != 0x80) | 
| 55 | 717k |             || ((p[2] & 0xc0) != 0x80)) | 
| 56 | 4.36k |             return -3; | 
| 57 | 713k |         value = (*p++ & 0xf) << 12; | 
| 58 | 713k |         value |= (*p++ & 0x3f) << 6; | 
| 59 | 713k |         value |= *p++ & 0x3f; | 
| 60 | 713k |         if (value < 0x800) | 
| 61 | 1.59k |             return -4; | 
| 62 | 711k |         if (is_unicode_surrogate(value)) | 
| 63 | 817 |             return -2; | 
| 64 | 710k |         ret = 3; | 
| 65 | 710k |     } else if ((*p & 0xf8) == 0xf0) { | 
| 66 | 21.6k |         if (len < 4) | 
| 67 | 1.33k |             return -1; | 
| 68 | 20.2k |         if (((p[1] & 0xc0) != 0x80) | 
| 69 | 20.2k |             || ((p[2] & 0xc0) != 0x80) | 
| 70 | 20.2k |             || ((p[3] & 0xc0) != 0x80)) | 
| 71 | 11.4k |             return -3; | 
| 72 | 8.78k |         value = ((unsigned long)(*p++ & 0x7)) << 18; | 
| 73 | 8.78k |         value |= (*p++ & 0x3f) << 12; | 
| 74 | 8.78k |         value |= (*p++ & 0x3f) << 6; | 
| 75 | 8.78k |         value |= *p++ & 0x3f; | 
| 76 | 8.78k |         if (value < 0x10000) | 
| 77 | 1.23k |             return -4; | 
| 78 | 7.55k |         ret = 4; | 
| 79 | 7.55k |     } else | 
| 80 | 5.67k |         return -2; | 
| 81 | 3.64M |     *val = value; | 
| 82 | 3.64M |     return ret; | 
| 83 | 3.68M | } | 
| 84 |  |  | 
| 85 |  | /* | 
| 86 |  |  * This takes a character 'value' and writes the UTF8 encoded value in 'str' | 
| 87 |  |  * where 'str' is a buffer containing 'len' characters. Returns the number of | 
| 88 |  |  * characters written, -1 if 'len' is too small or -2 if 'value' is out of | 
| 89 |  |  * range. 'str' can be set to NULL in which case it just returns the number of | 
| 90 |  |  * characters. It will need at most 4 characters. | 
| 91 |  |  */ | 
| 92 |  |  | 
| 93 |  | int UTF8_putc(unsigned char *str, int len, unsigned long value) | 
| 94 | 226M | { | 
| 95 | 226M |     if (!str) | 
| 96 | 113M |         len = 4;                /* Maximum we will need */ | 
| 97 | 113M |     else if (len <= 0) | 
| 98 | 0 |         return -1; | 
| 99 | 226M |     if (value < 0x80) { | 
| 100 | 17.2M |         if (str) | 
| 101 | 8.65M |             *str = (unsigned char)value; | 
| 102 | 17.2M |         return 1; | 
| 103 | 17.2M |     } | 
| 104 | 209M |     if (value < 0x800) { | 
| 105 | 207M |         if (len < 2) | 
| 106 | 0 |             return -1; | 
| 107 | 207M |         if (str) { | 
| 108 | 103M |             *str++ = (unsigned char)(((value >> 6) & 0x1f) | 0xc0); | 
| 109 | 103M |             *str = (unsigned char)((value & 0x3f) | 0x80); | 
| 110 | 103M |         } | 
| 111 | 207M |         return 2; | 
| 112 | 207M |     } | 
| 113 | 2.37M |     if (value < 0x10000) { | 
| 114 | 2.36M |         if (is_unicode_surrogate(value)) | 
| 115 | 240 |             return -2; | 
| 116 | 2.36M |         if (len < 3) | 
| 117 | 0 |             return -1; | 
| 118 | 2.36M |         if (str) { | 
| 119 | 1.23M |             *str++ = (unsigned char)(((value >> 12) & 0xf) | 0xe0); | 
| 120 | 1.23M |             *str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80); | 
| 121 | 1.23M |             *str = (unsigned char)((value & 0x3f) | 0x80); | 
| 122 | 1.23M |         } | 
| 123 | 2.36M |         return 3; | 
| 124 | 2.36M |     } | 
| 125 | 6.21k |     if (value < UNICODE_LIMIT) { | 
| 126 | 4.85k |         if (len < 4) | 
| 127 | 0 |             return -1; | 
| 128 | 4.85k |         if (str) { | 
| 129 | 2.48k |             *str++ = (unsigned char)(((value >> 18) & 0x7) | 0xf0); | 
| 130 | 2.48k |             *str++ = (unsigned char)(((value >> 12) & 0x3f) | 0x80); | 
| 131 | 2.48k |             *str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80); | 
| 132 | 2.48k |             *str = (unsigned char)((value & 0x3f) | 0x80); | 
| 133 | 2.48k |         } | 
| 134 | 4.85k |         return 4; | 
| 135 | 4.85k |     } | 
| 136 | 1.36k |     return -2; | 
| 137 | 6.21k | } |