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