Michal Vasko | 1324b6c | 2018-09-07 11:16:23 +0200 | [diff] [blame] | 1 | /** |
Michal Vasko | 8f702ee | 2024-02-20 15:44:24 +0100 | [diff] [blame^] | 2 | * @file ly_common.c |
Michal Vasko | 1324b6c | 2018-09-07 11:16:23 +0200 | [diff] [blame] | 3 | * @author Michal Vasko <mvasko@cesnet.cz> |
| 4 | * @brief common internal definitions for libyang |
| 5 | * |
Michal Vasko | 8f702ee | 2024-02-20 15:44:24 +0100 | [diff] [blame^] | 6 | * Copyright (c) 2018 - 2024 CESNET, z.s.p.o. |
Michal Vasko | 1324b6c | 2018-09-07 11:16:23 +0200 | [diff] [blame] | 7 | * |
| 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
| 13 | */ |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 14 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 15 | #define _GNU_SOURCE |
| 16 | |
Michal Vasko | 8f702ee | 2024-02-20 15:44:24 +0100 | [diff] [blame^] | 17 | #include "ly_common.h" |
Michal Vasko | 1324b6c | 2018-09-07 11:16:23 +0200 | [diff] [blame] | 18 | |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 19 | #include <assert.h> |
Michal Vasko | 841d1a9 | 2018-09-07 15:40:31 +0200 | [diff] [blame] | 20 | #include <ctype.h> |
Radek Krejci | 4546aa6 | 2019-07-15 16:53:32 +0200 | [diff] [blame] | 21 | #include <errno.h> |
Jan Kundrát | d31adc1 | 2022-07-07 21:36:15 +0200 | [diff] [blame] | 22 | #include <fcntl.h> |
Michal Vasko | 15dc9fa | 2021-05-03 14:33:05 +0200 | [diff] [blame] | 23 | #include <inttypes.h> |
Radek Krejci | 4546aa6 | 2019-07-15 16:53:32 +0200 | [diff] [blame] | 24 | #include <stdarg.h> |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 25 | #include <stdio.h> |
Radek Krejci | 4546aa6 | 2019-07-15 16:53:32 +0200 | [diff] [blame] | 26 | #include <stdlib.h> |
Michal Vasko | 841d1a9 | 2018-09-07 15:40:31 +0200 | [diff] [blame] | 27 | #include <string.h> |
Jan Kundrát | f1960dc | 2021-12-12 03:12:23 +0100 | [diff] [blame] | 28 | #ifndef _WIN32 |
Michal Vasko | 8f702ee | 2024-02-20 15:44:24 +0100 | [diff] [blame^] | 29 | # ifdef HAVE_MMAP |
| 30 | # include <sys/mman.h> |
| 31 | # endif |
Jan Kundrát | d31adc1 | 2022-07-07 21:36:15 +0200 | [diff] [blame] | 32 | #else |
Michal Vasko | 8f702ee | 2024-02-20 15:44:24 +0100 | [diff] [blame^] | 33 | # include <io.h> |
Jan Kundrát | f1960dc | 2021-12-12 03:12:23 +0100 | [diff] [blame] | 34 | #endif |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 35 | #include <sys/stat.h> |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 36 | #include <unistd.h> |
Michal Vasko | 841d1a9 | 2018-09-07 15:40:31 +0200 | [diff] [blame] | 37 | |
Radek Krejci | aa45bda | 2020-07-20 07:43:38 +0200 | [diff] [blame] | 38 | #include "compat.h" |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 39 | #include "tree_schema_internal.h" |
aPiecek | 704f8e9 | 2021-08-25 13:35:05 +0200 | [diff] [blame] | 40 | #include "xml.h" |
Michal Vasko | 1324b6c | 2018-09-07 11:16:23 +0200 | [diff] [blame] | 41 | |
| 42 | void * |
| 43 | ly_realloc(void *ptr, size_t size) |
| 44 | { |
| 45 | void *new_mem; |
| 46 | |
| 47 | new_mem = realloc(ptr, size); |
| 48 | if (!new_mem) { |
| 49 | free(ptr); |
| 50 | } |
| 51 | |
| 52 | return new_mem; |
| 53 | } |
Michal Vasko | 841d1a9 | 2018-09-07 15:40:31 +0200 | [diff] [blame] | 54 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 55 | char * |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 56 | ly_strnchr(const char *s, int c, size_t len) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 57 | { |
Michal Vasko | b4d40d6 | 2021-05-04 11:42:44 +0200 | [diff] [blame] | 58 | for ( ; len && (*s != (char)c); ++s, --len) {} |
| 59 | return len ? (char *)s : NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 60 | } |
| 61 | |
Radek Krejci | 7f9b651 | 2019-09-18 13:11:09 +0200 | [diff] [blame] | 62 | int |
| 63 | ly_strncmp(const char *refstr, const char *str, size_t str_len) |
| 64 | { |
| 65 | int rc = strncmp(refstr, str, str_len); |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 66 | |
| 67 | if (!rc && (refstr[str_len] == '\0')) { |
Radek Krejci | 7f9b651 | 2019-09-18 13:11:09 +0200 | [diff] [blame] | 68 | return 0; |
| 69 | } else { |
| 70 | return rc ? rc : 1; |
| 71 | } |
| 72 | } |
| 73 | |
Michal Vasko | 15dc9fa | 2021-05-03 14:33:05 +0200 | [diff] [blame] | 74 | LY_ERR |
| 75 | ly_strntou8(const char *nptr, size_t len, uint8_t *ret) |
| 76 | { |
| 77 | uint8_t num = 0, dig, dec_pow; |
| 78 | |
| 79 | if (len > 3) { |
| 80 | /* overflow for sure */ |
| 81 | return LY_EDENIED; |
| 82 | } |
| 83 | |
| 84 | dec_pow = 1; |
| 85 | for ( ; len && isdigit(nptr[len - 1]); --len) { |
| 86 | dig = nptr[len - 1] - 48; |
| 87 | |
| 88 | if (LY_OVERFLOW_MUL(UINT8_MAX, dig, dec_pow)) { |
| 89 | return LY_EDENIED; |
| 90 | } |
| 91 | dig *= dec_pow; |
| 92 | |
| 93 | if (LY_OVERFLOW_ADD(UINT8_MAX, num, dig)) { |
| 94 | return LY_EDENIED; |
| 95 | } |
| 96 | num += dig; |
| 97 | |
| 98 | dec_pow *= 10; |
| 99 | } |
| 100 | |
| 101 | if (len) { |
| 102 | return LY_EVALID; |
| 103 | } |
| 104 | *ret = num; |
| 105 | return LY_SUCCESS; |
| 106 | } |
| 107 | |
aPiecek | e3f828d | 2021-05-10 15:34:41 +0200 | [diff] [blame] | 108 | LY_ERR |
| 109 | ly_value_prefix_next(const char *str_begin, const char *str_end, uint32_t *len, ly_bool *is_prefix, const char **str_next) |
aPiecek | f102d4d | 2021-03-30 12:18:38 +0200 | [diff] [blame] | 110 | { |
| 111 | const char *stop, *prefix; |
aPiecek | e3f828d | 2021-05-10 15:34:41 +0200 | [diff] [blame] | 112 | size_t bytes_read; |
aPiecek | f102d4d | 2021-03-30 12:18:38 +0200 | [diff] [blame] | 113 | uint32_t c; |
| 114 | ly_bool prefix_found; |
aPiecek | e3f828d | 2021-05-10 15:34:41 +0200 | [diff] [blame] | 115 | LY_ERR ret = LY_SUCCESS; |
aPiecek | f102d4d | 2021-03-30 12:18:38 +0200 | [diff] [blame] | 116 | |
aPiecek | e3f828d | 2021-05-10 15:34:41 +0200 | [diff] [blame] | 117 | assert(len && is_prefix && str_next); |
aPiecek | f102d4d | 2021-03-30 12:18:38 +0200 | [diff] [blame] | 118 | |
| 119 | #define IS_AT_END(PTR, STR_END) (STR_END ? PTR == STR_END : !(*PTR)) |
| 120 | |
| 121 | *str_next = NULL; |
| 122 | *is_prefix = 0; |
aPiecek | e3f828d | 2021-05-10 15:34:41 +0200 | [diff] [blame] | 123 | *len = 0; |
aPiecek | f102d4d | 2021-03-30 12:18:38 +0200 | [diff] [blame] | 124 | |
| 125 | if (!str_begin || !(*str_begin) || (str_begin == str_end)) { |
| 126 | return ret; |
| 127 | } |
| 128 | |
| 129 | stop = str_begin; |
| 130 | prefix = NULL; |
| 131 | prefix_found = 0; |
| 132 | |
| 133 | do { |
| 134 | /* look for the beginning of the YANG value */ |
aPiecek | e3f828d | 2021-05-10 15:34:41 +0200 | [diff] [blame] | 135 | do { |
| 136 | LY_CHECK_RET(ly_getutf8(&stop, &c, &bytes_read)); |
| 137 | } while (!is_xmlqnamestartchar(c) && !IS_AT_END(stop, str_end)); |
aPiecek | f102d4d | 2021-03-30 12:18:38 +0200 | [diff] [blame] | 138 | |
| 139 | if (IS_AT_END(stop, str_end)) { |
| 140 | break; |
| 141 | } |
| 142 | |
| 143 | /* maybe the prefix was found */ |
aPiecek | e3f828d | 2021-05-10 15:34:41 +0200 | [diff] [blame] | 144 | prefix = stop - bytes_read; |
aPiecek | f102d4d | 2021-03-30 12:18:38 +0200 | [diff] [blame] | 145 | |
| 146 | /* look for the the end of the prefix */ |
aPiecek | e3f828d | 2021-05-10 15:34:41 +0200 | [diff] [blame] | 147 | do { |
| 148 | LY_CHECK_RET(ly_getutf8(&stop, &c, &bytes_read)); |
| 149 | } while (is_xmlqnamechar(c) && !IS_AT_END(stop, str_end)); |
aPiecek | f102d4d | 2021-03-30 12:18:38 +0200 | [diff] [blame] | 150 | |
| 151 | prefix_found = c == ':' ? 1 : 0; |
| 152 | |
| 153 | /* if it wasn't the prefix, keep looking */ |
| 154 | } while (!IS_AT_END(stop, str_end) && !prefix_found); |
| 155 | |
| 156 | if ((str_begin == prefix) && prefix_found) { |
| 157 | /* prefix found at the beginning of the input string */ |
| 158 | *is_prefix = 1; |
| 159 | *str_next = IS_AT_END(stop, str_end) ? NULL : stop; |
aPiecek | e3f828d | 2021-05-10 15:34:41 +0200 | [diff] [blame] | 160 | *len = (stop - bytes_read) - str_begin; |
aPiecek | f102d4d | 2021-03-30 12:18:38 +0200 | [diff] [blame] | 161 | } else if ((str_begin != prefix) && (prefix_found)) { |
| 162 | /* there is a some string before prefix */ |
| 163 | *str_next = prefix; |
aPiecek | e3f828d | 2021-05-10 15:34:41 +0200 | [diff] [blame] | 164 | *len = prefix - str_begin; |
aPiecek | f102d4d | 2021-03-30 12:18:38 +0200 | [diff] [blame] | 165 | } else { |
| 166 | /* no prefix found */ |
aPiecek | e3f828d | 2021-05-10 15:34:41 +0200 | [diff] [blame] | 167 | *len = stop - str_begin; |
aPiecek | f102d4d | 2021-03-30 12:18:38 +0200 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | #undef IS_AT_END |
| 171 | |
| 172 | return ret; |
| 173 | } |
| 174 | |
Radek Krejci | b416be6 | 2018-10-01 14:51:45 +0200 | [diff] [blame] | 175 | LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 176 | ly_getutf8(const char **input, uint32_t *utf8_char, size_t *bytes_read) |
Radek Krejci | b416be6 | 2018-10-01 14:51:45 +0200 | [diff] [blame] | 177 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 178 | uint32_t c, aux; |
| 179 | size_t len; |
Radek Krejci | b416be6 | 2018-10-01 14:51:45 +0200 | [diff] [blame] | 180 | |
| 181 | c = (*input)[0]; |
Radek Krejci | b416be6 | 2018-10-01 14:51:45 +0200 | [diff] [blame] | 182 | |
| 183 | if (!(c & 0x80)) { |
| 184 | /* one byte character */ |
| 185 | len = 1; |
| 186 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 187 | if ((c < 0x20) && (c != 0x9) && (c != 0xa) && (c != 0xd)) { |
Michal Vasko | da09abf | 2023-10-06 15:53:18 +0200 | [diff] [blame] | 188 | goto error; |
Radek Krejci | b416be6 | 2018-10-01 14:51:45 +0200 | [diff] [blame] | 189 | } |
| 190 | } else if ((c & 0xe0) == 0xc0) { |
| 191 | /* two bytes character */ |
| 192 | len = 2; |
| 193 | |
| 194 | aux = (*input)[1]; |
| 195 | if ((aux & 0xc0) != 0x80) { |
Michal Vasko | da09abf | 2023-10-06 15:53:18 +0200 | [diff] [blame] | 196 | goto error; |
Radek Krejci | b416be6 | 2018-10-01 14:51:45 +0200 | [diff] [blame] | 197 | } |
| 198 | c = ((c & 0x1f) << 6) | (aux & 0x3f); |
| 199 | |
| 200 | if (c < 0x80) { |
Michal Vasko | da09abf | 2023-10-06 15:53:18 +0200 | [diff] [blame] | 201 | goto error; |
Radek Krejci | b416be6 | 2018-10-01 14:51:45 +0200 | [diff] [blame] | 202 | } |
| 203 | } else if ((c & 0xf0) == 0xe0) { |
| 204 | /* three bytes character */ |
| 205 | len = 3; |
| 206 | |
| 207 | c &= 0x0f; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 208 | for (uint64_t i = 1; i <= 2; i++) { |
Radek Krejci | b416be6 | 2018-10-01 14:51:45 +0200 | [diff] [blame] | 209 | aux = (*input)[i]; |
| 210 | if ((aux & 0xc0) != 0x80) { |
Michal Vasko | da09abf | 2023-10-06 15:53:18 +0200 | [diff] [blame] | 211 | goto error; |
Radek Krejci | b416be6 | 2018-10-01 14:51:45 +0200 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | c = (c << 6) | (aux & 0x3f); |
| 215 | } |
| 216 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 217 | if ((c < 0x800) || ((c > 0xd7ff) && (c < 0xe000)) || (c > 0xfffd)) { |
Michal Vasko | da09abf | 2023-10-06 15:53:18 +0200 | [diff] [blame] | 218 | goto error; |
Radek Krejci | b416be6 | 2018-10-01 14:51:45 +0200 | [diff] [blame] | 219 | } |
| 220 | } else if ((c & 0xf8) == 0xf0) { |
| 221 | /* four bytes character */ |
| 222 | len = 4; |
| 223 | |
| 224 | c &= 0x07; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 225 | for (uint64_t i = 1; i <= 3; i++) { |
Radek Krejci | b416be6 | 2018-10-01 14:51:45 +0200 | [diff] [blame] | 226 | aux = (*input)[i]; |
| 227 | if ((aux & 0xc0) != 0x80) { |
Michal Vasko | da09abf | 2023-10-06 15:53:18 +0200 | [diff] [blame] | 228 | goto error; |
Radek Krejci | b416be6 | 2018-10-01 14:51:45 +0200 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | c = (c << 6) | (aux & 0x3f); |
| 232 | } |
| 233 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 234 | if ((c < 0x1000) || (c > 0x10ffff)) { |
Michal Vasko | da09abf | 2023-10-06 15:53:18 +0200 | [diff] [blame] | 235 | goto error; |
Radek Krejci | b416be6 | 2018-10-01 14:51:45 +0200 | [diff] [blame] | 236 | } |
| 237 | } else { |
Michal Vasko | da09abf | 2023-10-06 15:53:18 +0200 | [diff] [blame] | 238 | goto error; |
Radek Krejci | b416be6 | 2018-10-01 14:51:45 +0200 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | (*utf8_char) = c; |
| 242 | (*input) += len; |
| 243 | if (bytes_read) { |
| 244 | (*bytes_read) = len; |
| 245 | } |
| 246 | return LY_SUCCESS; |
Michal Vasko | da09abf | 2023-10-06 15:53:18 +0200 | [diff] [blame] | 247 | |
| 248 | error: |
| 249 | if (bytes_read) { |
| 250 | (*bytes_read) = 0; |
| 251 | } |
| 252 | return LY_EINVAL; |
Radek Krejci | b416be6 | 2018-10-01 14:51:45 +0200 | [diff] [blame] | 253 | } |
| 254 | |
Michal Vasko | 989cdb4 | 2023-10-06 15:32:37 +0200 | [diff] [blame] | 255 | /** |
| 256 | * @brief Check whether an UTF-8 string is equal to a hex string after a bitwise and. |
| 257 | * |
| 258 | * (input & 0x[arg1][arg3][arg5]...) == 0x[arg2][arg4][arg6]... |
| 259 | * |
| 260 | * @param[in] input UTF-8 string. |
| 261 | * @param[in] bytes Number of bytes to compare. |
| 262 | * @param[in] ... 2x @p bytes number of bytes to perform bitwise and and equality operations. |
| 263 | * @return Result of the operation. |
| 264 | */ |
| 265 | static int |
| 266 | ly_utf8_and_equal(const char *input, uint8_t bytes, ...) |
| 267 | { |
| 268 | va_list ap; |
| 269 | int i, and, byte; |
| 270 | |
| 271 | va_start(ap, bytes); |
| 272 | for (i = 0; i < bytes; ++i) { |
| 273 | and = va_arg(ap, int); |
| 274 | byte = va_arg(ap, int); |
| 275 | |
| 276 | /* compare each byte */ |
| 277 | if (((uint8_t)input[i] & and) != (uint8_t)byte) { |
| 278 | return 0; |
| 279 | } |
| 280 | } |
| 281 | va_end(ap); |
| 282 | |
| 283 | return 1; |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * @brief Check whether an UTF-8 string is smaller than a hex string. |
| 288 | * |
| 289 | * input < 0x[arg1][arg2]... |
| 290 | * |
| 291 | * @param[in] input UTF-8 string. |
| 292 | * @param[in] bytes Number of bytes to compare. |
| 293 | * @param[in] ... @p bytes number of bytes to compare with. |
| 294 | * @return Result of the operation. |
| 295 | */ |
| 296 | static int |
| 297 | ly_utf8_less(const char *input, uint8_t bytes, ...) |
| 298 | { |
| 299 | va_list ap; |
| 300 | int i, byte; |
| 301 | |
| 302 | va_start(ap, bytes); |
| 303 | for (i = 0; i < bytes; ++i) { |
| 304 | byte = va_arg(ap, int); |
| 305 | |
| 306 | /* compare until bytes differ */ |
| 307 | if ((uint8_t)input[i] > (uint8_t)byte) { |
| 308 | return 0; |
| 309 | } else if ((uint8_t)input[i] < (uint8_t)byte) { |
| 310 | return 1; |
| 311 | } |
| 312 | } |
| 313 | va_end(ap); |
| 314 | |
| 315 | /* equals */ |
| 316 | return 0; |
| 317 | } |
| 318 | |
| 319 | /** |
| 320 | * @brief Check whether an UTF-8 string is greater than a hex string. |
| 321 | * |
| 322 | * input > 0x[arg1][arg2]... |
| 323 | * |
| 324 | * @param[in] input UTF-8 string. |
| 325 | * @param[in] bytes Number of bytes to compare. |
| 326 | * @param[in] ... @p bytes number of bytes to compare with. |
| 327 | * @return Result of the operation. |
| 328 | */ |
| 329 | static int |
| 330 | ly_utf8_greater(const char *input, uint8_t bytes, ...) |
| 331 | { |
| 332 | va_list ap; |
| 333 | int i, byte; |
| 334 | |
| 335 | va_start(ap, bytes); |
| 336 | for (i = 0; i < bytes; ++i) { |
| 337 | byte = va_arg(ap, int); |
| 338 | |
| 339 | /* compare until bytes differ */ |
| 340 | if ((uint8_t)input[i] > (uint8_t)byte) { |
| 341 | return 1; |
| 342 | } else if ((uint8_t)input[i] < (uint8_t)byte) { |
| 343 | return 0; |
| 344 | } |
| 345 | } |
| 346 | va_end(ap); |
| 347 | |
| 348 | /* equals */ |
| 349 | return 0; |
| 350 | } |
| 351 | |
| 352 | LY_ERR |
| 353 | ly_checkutf8(const char *input, size_t in_len, size_t *utf8_len) |
| 354 | { |
| 355 | size_t len; |
| 356 | |
| 357 | if (!(input[0] & 0x80)) { |
| 358 | /* one byte character */ |
| 359 | len = 1; |
| 360 | |
| 361 | if (ly_utf8_less(input, 1, 0x20) && (input[0] != 0x9) && (input[0] != 0xa) && (input[0] != 0xd)) { |
| 362 | /* invalid control characters */ |
| 363 | return LY_EINVAL; |
| 364 | } |
| 365 | } else if (((input[0] & 0xe0) == 0xc0) && (in_len > 1)) { |
| 366 | /* two bytes character */ |
| 367 | len = 2; |
| 368 | |
| 369 | /* (input < 0xC280) || (input > 0xDFBF) || ((input & 0xE0C0) != 0xC080) */ |
| 370 | if (ly_utf8_less(input, 2, 0xC2, 0x80) || ly_utf8_greater(input, 2, 0xDF, 0xBF) || |
| 371 | !ly_utf8_and_equal(input, 2, 0xE0, 0xC0, 0xC0, 0x80)) { |
| 372 | return LY_EINVAL; |
| 373 | } |
| 374 | } else if (((input[0] & 0xf0) == 0xe0) && (in_len > 2)) { |
| 375 | /* three bytes character */ |
| 376 | len = 3; |
| 377 | |
| 378 | /* (input >= 0xEDA080) && (input <= 0xEDBFBF) */ |
| 379 | if (!ly_utf8_less(input, 3, 0xED, 0xA0, 0x80) && !ly_utf8_greater(input, 3, 0xED, 0xBF, 0xBF)) { |
| 380 | /* reject UTF-16 surrogates */ |
| 381 | return LY_EINVAL; |
| 382 | } |
| 383 | |
| 384 | /* (input < 0xE0A080) || (input > 0xEFBFBF) || ((input & 0xF0C0C0) != 0xE08080) */ |
| 385 | if (ly_utf8_less(input, 3, 0xE0, 0xA0, 0x80) || ly_utf8_greater(input, 3, 0xEF, 0xBF, 0xBF) || |
| 386 | !ly_utf8_and_equal(input, 3, 0xF0, 0xE0, 0xC0, 0x80, 0xC0, 0x80)) { |
| 387 | return LY_EINVAL; |
| 388 | } |
| 389 | } else if (((input[0] & 0xf8) == 0xf0) && (in_len > 3)) { |
| 390 | /* four bytes character */ |
| 391 | len = 4; |
| 392 | |
| 393 | /* (input < 0xF0908080) || (input > 0xF48FBFBF) || ((input & 0xF8C0C0C0) != 0xF0808080) */ |
| 394 | if (ly_utf8_less(input, 4, 0xF0, 0x90, 0x80, 0x80) || ly_utf8_greater(input, 4, 0xF4, 0x8F, 0xBF, 0xBF) || |
| 395 | !ly_utf8_and_equal(input, 4, 0xF8, 0xF0, 0xC0, 0x80, 0xC0, 0x80, 0xC0, 0x80)) { |
| 396 | return LY_EINVAL; |
| 397 | } |
| 398 | } else { |
| 399 | return LY_EINVAL; |
| 400 | } |
| 401 | |
| 402 | *utf8_len = len; |
| 403 | return LY_SUCCESS; |
| 404 | } |
| 405 | |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 406 | LY_ERR |
| 407 | ly_pututf8(char *dst, uint32_t value, size_t *bytes_written) |
| 408 | { |
| 409 | if (value < 0x80) { |
| 410 | /* one byte character */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 411 | if ((value < 0x20) && |
| 412 | (value != 0x09) && |
| 413 | (value != 0x0a) && |
| 414 | (value != 0x0d)) { |
Michal Vasko | 519097f | 2023-05-25 10:00:44 +0200 | [diff] [blame] | 415 | /* valid UTF8 but not YANG string character */ |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 416 | return LY_EINVAL; |
| 417 | } |
| 418 | |
| 419 | dst[0] = value; |
| 420 | (*bytes_written) = 1; |
| 421 | } else if (value < 0x800) { |
| 422 | /* two bytes character */ |
| 423 | dst[0] = 0xc0 | (value >> 6); |
| 424 | dst[1] = 0x80 | (value & 0x3f); |
| 425 | (*bytes_written) = 2; |
| 426 | } else if (value < 0xfffe) { |
| 427 | /* three bytes character */ |
| 428 | if (((value & 0xf800) == 0xd800) || |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 429 | ((value >= 0xfdd0) && (value <= 0xfdef))) { |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 430 | /* exclude surrogate blocks %xD800-DFFF */ |
| 431 | /* exclude noncharacters %xFDD0-FDEF */ |
| 432 | return LY_EINVAL; |
| 433 | } |
| 434 | |
| 435 | dst[0] = 0xe0 | (value >> 12); |
| 436 | dst[1] = 0x80 | ((value >> 6) & 0x3f); |
| 437 | dst[2] = 0x80 | (value & 0x3f); |
| 438 | |
| 439 | (*bytes_written) = 3; |
| 440 | } else if (value < 0x10fffe) { |
| 441 | if ((value & 0xffe) == 0xffe) { |
| 442 | /* exclude noncharacters %xFFFE-FFFF, %x1FFFE-1FFFF, %x2FFFE-2FFFF, %x3FFFE-3FFFF, %x4FFFE-4FFFF, |
| 443 | * %x5FFFE-5FFFF, %x6FFFE-6FFFF, %x7FFFE-7FFFF, %x8FFFE-8FFFF, %x9FFFE-9FFFF, %xAFFFE-AFFFF, |
| 444 | * %xBFFFE-BFFFF, %xCFFFE-CFFFF, %xDFFFE-DFFFF, %xEFFFE-EFFFF, %xFFFFE-FFFFF, %x10FFFE-10FFFF */ |
| 445 | return LY_EINVAL; |
| 446 | } |
| 447 | /* four bytes character */ |
| 448 | dst[0] = 0xf0 | (value >> 18); |
| 449 | dst[1] = 0x80 | ((value >> 12) & 0x3f); |
| 450 | dst[2] = 0x80 | ((value >> 6) & 0x3f); |
| 451 | dst[3] = 0x80 | (value & 0x3f); |
| 452 | |
| 453 | (*bytes_written) = 4; |
| 454 | } else { |
| 455 | return LY_EINVAL; |
| 456 | } |
| 457 | return LY_SUCCESS; |
| 458 | } |
| 459 | |
Radek Krejci | 76c9801 | 2019-08-14 11:23:24 +0200 | [diff] [blame] | 460 | /** |
| 461 | * @brief Static table of the UTF8 characters lengths according to their first byte. |
| 462 | */ |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 463 | static const unsigned char utf8_char_length_table[] = { |
Radek Krejci | 76c9801 | 2019-08-14 11:23:24 +0200 | [diff] [blame] | 464 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 465 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 466 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 467 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 468 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 469 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 470 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 471 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 472 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 473 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 474 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 475 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 476 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 477 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 478 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 479 | 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 1, 1 |
| 480 | }; |
| 481 | |
| 482 | size_t |
| 483 | ly_utf8len(const char *str, size_t bytes) |
| 484 | { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 485 | size_t len = 0; |
| 486 | const char *ptr = str; |
Radek Krejci | 76c9801 | 2019-08-14 11:23:24 +0200 | [diff] [blame] | 487 | |
Michal Vasko | b4d40d6 | 2021-05-04 11:42:44 +0200 | [diff] [blame] | 488 | while (((size_t)(ptr - str) < bytes) && *ptr) { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 489 | ++len; |
| 490 | ptr += utf8_char_length_table[((unsigned char)(*ptr))]; |
| 491 | } |
Radek Krejci | 76c9801 | 2019-08-14 11:23:24 +0200 | [diff] [blame] | 492 | return len; |
| 493 | } |
| 494 | |
Michal Vasko | 7b3a00e | 2023-08-09 11:58:03 +0200 | [diff] [blame] | 495 | int |
Radek Krejci | d972c25 | 2018-09-25 13:23:39 +0200 | [diff] [blame] | 496 | LY_VCODE_INSTREXP_len(const char *str) |
| 497 | { |
Michal Vasko | 7b3a00e | 2023-08-09 11:58:03 +0200 | [diff] [blame] | 498 | int len = 0; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 499 | |
Radek Krejci | d972c25 | 2018-09-25 13:23:39 +0200 | [diff] [blame] | 500 | if (!str) { |
| 501 | return len; |
| 502 | } else if (!str[0]) { |
| 503 | return 1; |
| 504 | } |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 505 | for (len = 1; len < LY_VCODE_INSTREXP_MAXLEN && str[len]; ++len) {} |
Radek Krejci | d972c25 | 2018-09-25 13:23:39 +0200 | [diff] [blame] | 506 | return len; |
| 507 | } |
| 508 | |
Jan Kundrát | f1960dc | 2021-12-12 03:12:23 +0100 | [diff] [blame] | 509 | #ifdef HAVE_MMAP |
Radek Krejci | f345c01 | 2018-09-19 11:12:59 +0200 | [diff] [blame] | 510 | LY_ERR |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 511 | ly_mmap(struct ly_ctx *ctx, int fd, size_t *length, void **addr) |
Michal Vasko | 841d1a9 | 2018-09-07 15:40:31 +0200 | [diff] [blame] | 512 | { |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 513 | struct stat sb; |
| 514 | long pagesize; |
| 515 | size_t m; |
Michal Vasko | 841d1a9 | 2018-09-07 15:40:31 +0200 | [diff] [blame] | 516 | |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 517 | assert(length); |
| 518 | assert(addr); |
| 519 | assert(fd >= 0); |
Michal Vasko | 841d1a9 | 2018-09-07 15:40:31 +0200 | [diff] [blame] | 520 | |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 521 | if (fstat(fd, &sb) == -1) { |
| 522 | LOGERR(ctx, LY_ESYS, "Failed to stat the file descriptor (%s) for the mmap().", strerror(errno)); |
| 523 | return LY_ESYS; |
Michal Vasko | 841d1a9 | 2018-09-07 15:40:31 +0200 | [diff] [blame] | 524 | } |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 525 | if (!S_ISREG(sb.st_mode)) { |
| 526 | LOGERR(ctx, LY_EINVAL, "File to mmap() is not a regular file."); |
| 527 | return LY_ESYS; |
Michal Vasko | 841d1a9 | 2018-09-07 15:40:31 +0200 | [diff] [blame] | 528 | } |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 529 | if (!sb.st_size) { |
| 530 | *addr = NULL; |
| 531 | return LY_SUCCESS; |
| 532 | } |
| 533 | pagesize = sysconf(_SC_PAGESIZE); |
| 534 | |
| 535 | m = sb.st_size % pagesize; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 536 | if (m && (pagesize - m >= 1)) { |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 537 | /* there will be enough space (at least 1 byte) after the file content mapping to provide zeroed NULL-termination byte */ |
| 538 | *length = sb.st_size + 1; |
| 539 | *addr = mmap(NULL, *length, PROT_READ, MAP_PRIVATE, fd, 0); |
| 540 | } else { |
| 541 | /* there will not be enough bytes after the file content mapping for the additional bytes and some of them |
| 542 | * would overflow into another page that would not be zerroed and any access into it would generate SIGBUS. |
| 543 | * Therefore we have to do the following hack with double mapping. First, the required number of bytes |
| 544 | * (including the additinal bytes) is required as anonymous and thus they will be really provided (actually more |
| 545 | * because of using whole pages) and also initialized by zeros. Then, the file is mapped to the same address |
| 546 | * where the anonymous mapping starts. */ |
| 547 | *length = sb.st_size + pagesize; |
| 548 | *addr = mmap(NULL, *length, PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
| 549 | *addr = mmap(*addr, sb.st_size, PROT_READ, MAP_PRIVATE | MAP_FIXED, fd, 0); |
| 550 | } |
| 551 | if (*addr == MAP_FAILED) { |
| 552 | LOGERR(ctx, LY_ESYS, "mmap() failed (%s).", strerror(errno)); |
| 553 | return LY_ESYS; |
Michal Vasko | 841d1a9 | 2018-09-07 15:40:31 +0200 | [diff] [blame] | 554 | } |
| 555 | |
Radek Krejci | f345c01 | 2018-09-19 11:12:59 +0200 | [diff] [blame] | 556 | return LY_SUCCESS; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 557 | } |
Michal Vasko | 841d1a9 | 2018-09-07 15:40:31 +0200 | [diff] [blame] | 558 | |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 559 | LY_ERR |
| 560 | ly_munmap(void *addr, size_t length) |
| 561 | { |
| 562 | if (munmap(addr, length)) { |
| 563 | return LY_ESYS; |
| 564 | } |
| 565 | return LY_SUCCESS; |
Michal Vasko | 841d1a9 | 2018-09-07 15:40:31 +0200 | [diff] [blame] | 566 | } |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 567 | |
Jan Kundrát | f1960dc | 2021-12-12 03:12:23 +0100 | [diff] [blame] | 568 | #else |
| 569 | |
| 570 | LY_ERR |
| 571 | ly_mmap(struct ly_ctx *ctx, int fd, size_t *length, void **addr) |
| 572 | { |
| 573 | struct stat sb; |
| 574 | size_t m; |
| 575 | |
| 576 | assert(length); |
| 577 | assert(addr); |
| 578 | assert(fd >= 0); |
| 579 | |
Jan Kundrát | d31adc1 | 2022-07-07 21:36:15 +0200 | [diff] [blame] | 580 | #if _WIN32 |
| 581 | if (_setmode(fd, _O_BINARY) == -1) { |
| 582 | LOGERR(ctx, LY_ESYS, "Failed to switch the file descriptor to binary mode.", strerror(errno)); |
| 583 | return LY_ESYS; |
| 584 | } |
| 585 | #endif |
| 586 | |
Jan Kundrát | f1960dc | 2021-12-12 03:12:23 +0100 | [diff] [blame] | 587 | if (fstat(fd, &sb) == -1) { |
| 588 | LOGERR(ctx, LY_ESYS, "Failed to stat the file descriptor (%s) for the mmap().", strerror(errno)); |
| 589 | return LY_ESYS; |
| 590 | } |
| 591 | if (!S_ISREG(sb.st_mode)) { |
| 592 | LOGERR(ctx, LY_EINVAL, "File to mmap() is not a regular file."); |
| 593 | return LY_ESYS; |
| 594 | } |
| 595 | if (!sb.st_size) { |
| 596 | *addr = NULL; |
| 597 | return LY_SUCCESS; |
| 598 | } |
| 599 | /* On Windows, the mman-win32 mmap() emulation uses CreateFileMapping and MapViewOfFile, and these functions |
| 600 | * do not allow mapping more than "length of file" bytes for PROT_READ. Remapping existing mappings is not allowed, either. |
| 601 | * At that point the path of least resistance is just reading the file in as-is. */ |
| 602 | m = sb.st_size + 1; |
| 603 | char *buf = calloc(m, 1); |
| 604 | |
| 605 | if (!buf) { |
| 606 | LOGERR(ctx, LY_ESYS, "ly_mmap: malloc() failed (%s).", strerror(errno)); |
| 607 | } |
| 608 | *addr = buf; |
| 609 | *length = m; |
| 610 | |
| 611 | lseek(fd, 0, SEEK_SET); |
| 612 | ssize_t to_read = m - 1; |
| 613 | |
| 614 | while (to_read > 0) { |
| 615 | ssize_t n = read(fd, buf, to_read); |
Michal Vasko | 2bf4af4 | 2023-01-04 12:08:38 +0100 | [diff] [blame] | 616 | |
Jan Kundrát | f1960dc | 2021-12-12 03:12:23 +0100 | [diff] [blame] | 617 | if (n == 0) { |
| 618 | return LY_SUCCESS; |
| 619 | } else if (n < 0) { |
| 620 | if (errno == EINTR) { |
| 621 | continue; // can I get this on Windows? |
| 622 | } |
| 623 | LOGERR(ctx, LY_ESYS, "ly_mmap: read() failed (%s).", strerror(errno)); |
| 624 | } |
| 625 | to_read -= n; |
| 626 | buf += n; |
| 627 | } |
| 628 | return LY_SUCCESS; |
| 629 | } |
| 630 | |
| 631 | LY_ERR |
| 632 | ly_munmap(void *addr, size_t length) |
| 633 | { |
| 634 | (void)length; |
| 635 | free(addr); |
| 636 | return LY_SUCCESS; |
| 637 | } |
| 638 | |
| 639 | #endif |
| 640 | |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 641 | LY_ERR |
Radek Krejci | 4546aa6 | 2019-07-15 16:53:32 +0200 | [diff] [blame] | 642 | ly_strcat(char **dest, const char *format, ...) |
| 643 | { |
| 644 | va_list fp; |
| 645 | char *addition = NULL; |
| 646 | size_t len; |
| 647 | |
| 648 | va_start(fp, format); |
| 649 | len = vasprintf(&addition, format, fp); |
| 650 | len += (*dest ? strlen(*dest) : 0) + 1; |
| 651 | |
| 652 | if (*dest) { |
| 653 | *dest = ly_realloc(*dest, len); |
| 654 | if (!*dest) { |
Radek Krejci | 1cd812f | 2020-12-01 12:17:53 +0100 | [diff] [blame] | 655 | va_end(fp); |
Radek Krejci | 4546aa6 | 2019-07-15 16:53:32 +0200 | [diff] [blame] | 656 | return LY_EMEM; |
| 657 | } |
| 658 | *dest = strcat(*dest, addition); |
| 659 | free(addition); |
| 660 | } else { |
| 661 | *dest = addition; |
| 662 | } |
| 663 | |
| 664 | va_end(fp); |
| 665 | return LY_SUCCESS; |
| 666 | } |
| 667 | |
| 668 | LY_ERR |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 669 | ly_parse_int(const char *val_str, size_t val_len, int64_t min, int64_t max, int base, int64_t *ret) |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 670 | { |
Michal Vasko | b4d40d6 | 2021-05-04 11:42:44 +0200 | [diff] [blame] | 671 | LY_ERR rc = LY_SUCCESS; |
| 672 | char *ptr, *str; |
Radek Krejci | 9ea8ca1 | 2019-06-10 13:11:55 +0200 | [diff] [blame] | 673 | int64_t i; |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 674 | |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 675 | LY_CHECK_ARG_RET(NULL, val_str, val_str[0], val_len, LY_EINVAL); |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 676 | |
Michal Vasko | b4d40d6 | 2021-05-04 11:42:44 +0200 | [diff] [blame] | 677 | /* duplicate the value */ |
| 678 | str = strndup(val_str, val_len); |
| 679 | LY_CHECK_RET(!str, LY_EMEM); |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 680 | |
Michal Vasko | b4d40d6 | 2021-05-04 11:42:44 +0200 | [diff] [blame] | 681 | /* parse the value to avoid accessing following bytes */ |
| 682 | errno = 0; |
| 683 | i = strtoll(str, &ptr, base); |
| 684 | if (errno || (ptr == str)) { |
| 685 | /* invalid string */ |
| 686 | rc = LY_EVALID; |
Radek Krejci | 9ea8ca1 | 2019-06-10 13:11:55 +0200 | [diff] [blame] | 687 | } else if ((i < min) || (i > max)) { |
Michal Vasko | b4d40d6 | 2021-05-04 11:42:44 +0200 | [diff] [blame] | 688 | /* invalid number */ |
| 689 | rc = LY_EDENIED; |
| 690 | } else if (*ptr) { |
| 691 | while (isspace(*ptr)) { |
| 692 | ++ptr; |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 693 | } |
Michal Vasko | b4d40d6 | 2021-05-04 11:42:44 +0200 | [diff] [blame] | 694 | if (*ptr) { |
| 695 | /* invalid characters after some number */ |
| 696 | rc = LY_EVALID; |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 697 | } |
| 698 | } |
Radek Krejci | 9ea8ca1 | 2019-06-10 13:11:55 +0200 | [diff] [blame] | 699 | |
Michal Vasko | b4d40d6 | 2021-05-04 11:42:44 +0200 | [diff] [blame] | 700 | /* cleanup */ |
| 701 | free(str); |
| 702 | if (!rc) { |
| 703 | *ret = i; |
| 704 | } |
| 705 | return rc; |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | LY_ERR |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 709 | ly_parse_uint(const char *val_str, size_t val_len, uint64_t max, int base, uint64_t *ret) |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 710 | { |
Michal Vasko | b4d40d6 | 2021-05-04 11:42:44 +0200 | [diff] [blame] | 711 | LY_ERR rc = LY_SUCCESS; |
| 712 | char *ptr, *str; |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 713 | uint64_t u; |
| 714 | |
Michal Vasko | b4d40d6 | 2021-05-04 11:42:44 +0200 | [diff] [blame] | 715 | LY_CHECK_ARG_RET(NULL, val_str, val_str[0], val_len, LY_EINVAL); |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 716 | |
Michal Vasko | b4d40d6 | 2021-05-04 11:42:44 +0200 | [diff] [blame] | 717 | /* duplicate the value to avoid accessing following bytes */ |
| 718 | str = strndup(val_str, val_len); |
| 719 | LY_CHECK_RET(!str, LY_EMEM); |
| 720 | |
| 721 | /* parse the value */ |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 722 | errno = 0; |
Michal Vasko | b4d40d6 | 2021-05-04 11:42:44 +0200 | [diff] [blame] | 723 | u = strtoull(str, &ptr, base); |
| 724 | if (errno || (ptr == str)) { |
| 725 | /* invalid string */ |
| 726 | rc = LY_EVALID; |
| 727 | } else if ((u > max) || (u && (str[0] == '-'))) { |
| 728 | /* invalid number */ |
| 729 | rc = LY_EDENIED; |
| 730 | } else if (*ptr) { |
| 731 | while (isspace(*ptr)) { |
| 732 | ++ptr; |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 733 | } |
Michal Vasko | b4d40d6 | 2021-05-04 11:42:44 +0200 | [diff] [blame] | 734 | if (*ptr) { |
| 735 | /* invalid characters after some number */ |
| 736 | rc = LY_EVALID; |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 737 | } |
| 738 | } |
| 739 | |
Michal Vasko | b4d40d6 | 2021-05-04 11:42:44 +0200 | [diff] [blame] | 740 | /* cleanup */ |
| 741 | free(str); |
| 742 | if (!rc) { |
| 743 | *ret = u; |
| 744 | } |
| 745 | return rc; |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 746 | } |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 747 | |
| 748 | /** |
| 749 | * @brief Parse an identifier. |
| 750 | * |
| 751 | * ;; An identifier MUST NOT start with (('X'|'x') ('M'|'m') ('L'|'l')) |
| 752 | * identifier = (ALPHA / "_") |
| 753 | * *(ALPHA / DIGIT / "_" / "-" / ".") |
| 754 | * |
| 755 | * @param[in,out] id Identifier to parse. When returned, it points to the first character which is not part of the identifier. |
| 756 | * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid starting character. |
| 757 | */ |
| 758 | static LY_ERR |
| 759 | lys_parse_id(const char **id) |
| 760 | { |
| 761 | assert(id && *id); |
| 762 | |
| 763 | if (!is_yangidentstartchar(**id)) { |
| 764 | return LY_EINVAL; |
| 765 | } |
| 766 | ++(*id); |
| 767 | |
| 768 | while (is_yangidentchar(**id)) { |
| 769 | ++(*id); |
| 770 | } |
| 771 | return LY_SUCCESS; |
| 772 | } |
| 773 | |
| 774 | LY_ERR |
| 775 | ly_parse_nodeid(const char **id, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len) |
| 776 | { |
| 777 | assert(id && *id); |
| 778 | assert(prefix && prefix_len); |
| 779 | assert(name && name_len); |
| 780 | |
| 781 | *prefix = *id; |
| 782 | *prefix_len = 0; |
| 783 | *name = NULL; |
| 784 | *name_len = 0; |
| 785 | |
| 786 | LY_CHECK_RET(lys_parse_id(id)); |
| 787 | if (**id == ':') { |
| 788 | /* there is prefix */ |
| 789 | *prefix_len = *id - *prefix; |
| 790 | ++(*id); |
| 791 | *name = *id; |
| 792 | |
| 793 | LY_CHECK_RET(lys_parse_id(id)); |
| 794 | *name_len = *id - *name; |
| 795 | } else { |
| 796 | /* there is no prefix, so what we have as prefix now is actually the name */ |
| 797 | *name = *prefix; |
| 798 | *name_len = *id - *name; |
| 799 | *prefix = NULL; |
| 800 | } |
| 801 | |
| 802 | return LY_SUCCESS; |
| 803 | } |
| 804 | |
| 805 | LY_ERR |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 806 | ly_parse_instance_predicate(const char **pred, size_t limit, LYD_FORMAT format, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 807 | const char **prefix, size_t *prefix_len, const char **id, size_t *id_len, const char **value, size_t *value_len, |
| 808 | const char **errmsg) |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 809 | { |
| 810 | LY_ERR ret = LY_EVALID; |
| 811 | const char *in = *pred; |
| 812 | size_t offset = 1; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 813 | uint8_t expr = 0; /* 0 - position predicate; 1 - leaf-list-predicate; 2 - key-predicate */ |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 814 | char quot; |
| 815 | |
Radek Krejci | 4607f54 | 2020-12-01 12:18:49 +0100 | [diff] [blame] | 816 | assert(in[0] == '['); |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 817 | |
| 818 | *prefix = *id = *value = NULL; |
| 819 | *prefix_len = *id_len = *value_len = 0; |
| 820 | |
| 821 | /* leading *WSP */ |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 822 | for ( ; isspace(in[offset]); offset++) {} |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 823 | |
| 824 | if (isdigit(in[offset])) { |
| 825 | /* pos: "[" *WSP positive-integer-value *WSP "]" */ |
| 826 | if (in[offset] == '0') { |
| 827 | /* zero */ |
| 828 | *errmsg = "The position predicate cannot be zero."; |
| 829 | goto error; |
| 830 | } |
| 831 | |
| 832 | /* positive-integer-value */ |
Radek Krejci | 10bfdf8 | 2019-06-10 14:08:13 +0200 | [diff] [blame] | 833 | *value = &in[offset++]; |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 834 | for ( ; isdigit(in[offset]); offset++) {} |
Radek Krejci | 10bfdf8 | 2019-06-10 14:08:13 +0200 | [diff] [blame] | 835 | *value_len = &in[offset] - *value; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 836 | |
| 837 | } else if (in[offset] == '.') { |
| 838 | /* leaf-list-predicate: "[" *WSP "." *WSP "=" *WSP quoted-string *WSP "]" */ |
| 839 | *id = &in[offset]; |
| 840 | *id_len = 1; |
| 841 | offset++; |
| 842 | expr = 1; |
Radek Krejci | 10bfdf8 | 2019-06-10 14:08:13 +0200 | [diff] [blame] | 843 | } else if (in[offset] == '-') { |
| 844 | /* typically negative value */ |
| 845 | *errmsg = "Invalid instance predicate format (negative position or invalid node-identifier)."; |
| 846 | goto error; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 847 | } else { |
| 848 | /* key-predicate: "[" *WSP node-identifier *WSP "=" *WSP quoted-string *WSP "]" */ |
| 849 | in = &in[offset]; |
| 850 | if (ly_parse_nodeid(&in, prefix, prefix_len, id, id_len)) { |
| 851 | *errmsg = "Invalid node-identifier."; |
| 852 | goto error; |
| 853 | } |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 854 | if ((format == LYD_XML) && !(*prefix)) { |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 855 | /* all node names MUST be qualified with explicit namespace prefix */ |
| 856 | *errmsg = "Missing prefix of a node name."; |
| 857 | goto error; |
| 858 | } |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 859 | offset = in - *pred; |
| 860 | in = *pred; |
Radek Krejci | 10bfdf8 | 2019-06-10 14:08:13 +0200 | [diff] [blame] | 861 | expr = 2; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 862 | } |
| 863 | |
| 864 | if (expr) { |
| 865 | /* *WSP "=" *WSP quoted-string *WSP "]" */ |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 866 | for ( ; isspace(in[offset]); offset++) {} |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 867 | |
| 868 | if (in[offset] != '=') { |
Radek Krejci | 10bfdf8 | 2019-06-10 14:08:13 +0200 | [diff] [blame] | 869 | if (expr == 1) { |
| 870 | *errmsg = "Unexpected character instead of \'=\' in leaf-list-predicate."; |
| 871 | } else { /* 2 */ |
| 872 | *errmsg = "Unexpected character instead of \'=\' in key-predicate."; |
| 873 | } |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 874 | goto error; |
| 875 | } |
| 876 | offset++; |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 877 | for ( ; isspace(in[offset]); offset++) {} |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 878 | |
| 879 | /* quoted-string */ |
| 880 | quot = in[offset++]; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 881 | if ((quot != '\'') && (quot != '\"')) { |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 882 | *errmsg = "String value is not quoted."; |
| 883 | goto error; |
| 884 | } |
| 885 | *value = &in[offset]; |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 886 | for ( ; offset < limit && (in[offset] != quot || (offset && in[offset - 1] == '\\')); offset++) {} |
Radek Krejci | 10bfdf8 | 2019-06-10 14:08:13 +0200 | [diff] [blame] | 887 | if (in[offset] == quot) { |
| 888 | *value_len = &in[offset] - *value; |
| 889 | offset++; |
| 890 | } else { |
| 891 | *errmsg = "Value is not terminated quoted-string."; |
| 892 | goto error; |
| 893 | } |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 894 | } |
| 895 | |
| 896 | /* *WSP "]" */ |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 897 | for ( ; isspace(in[offset]); offset++) {} |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 898 | if (in[offset] != ']') { |
Radek Krejci | 10bfdf8 | 2019-06-10 14:08:13 +0200 | [diff] [blame] | 899 | if (expr == 0) { |
| 900 | *errmsg = "Predicate (pos) is not terminated by \']\' character."; |
| 901 | } else if (expr == 1) { |
| 902 | *errmsg = "Predicate (leaf-list-predicate) is not terminated by \']\' character."; |
| 903 | } else { /* 2 */ |
| 904 | *errmsg = "Predicate (key-predicate) is not terminated by \']\' character."; |
| 905 | } |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 906 | goto error; |
| 907 | } |
Radek Krejci | 10bfdf8 | 2019-06-10 14:08:13 +0200 | [diff] [blame] | 908 | offset++; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 909 | |
Radek Krejci | 10bfdf8 | 2019-06-10 14:08:13 +0200 | [diff] [blame] | 910 | if (offset <= limit) { |
| 911 | *pred = &in[offset]; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 912 | return LY_SUCCESS; |
| 913 | } |
| 914 | |
| 915 | /* we read after the limit */ |
| 916 | *errmsg = "Predicate is incomplete."; |
| 917 | *prefix = *id = *value = NULL; |
| 918 | *prefix_len = *id_len = *value_len = 0; |
| 919 | offset = limit; |
| 920 | ret = LY_EINVAL; |
| 921 | |
| 922 | error: |
| 923 | *pred = &in[offset]; |
| 924 | return ret; |
| 925 | } |