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 | |
Radek Krejci | cc6a45c | 2019-05-13 10:16:14 +0200 | [diff] [blame] | 179 | if (bytes_read) { |
| 180 | (*bytes_read) = 0; |
| 181 | } |
| 182 | |
Radek Krejci | b416be6 | 2018-10-01 14:51:45 +0200 | [diff] [blame] | 183 | c = (*input)[0]; |
| 184 | LY_CHECK_RET(!c, LY_EINVAL); |
| 185 | |
| 186 | if (!(c & 0x80)) { |
| 187 | /* one byte character */ |
| 188 | len = 1; |
| 189 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 190 | if ((c < 0x20) && (c != 0x9) && (c != 0xa) && (c != 0xd)) { |
Radek Krejci | b416be6 | 2018-10-01 14:51:45 +0200 | [diff] [blame] | 191 | return LY_EINVAL; |
| 192 | } |
| 193 | } else if ((c & 0xe0) == 0xc0) { |
| 194 | /* two bytes character */ |
| 195 | len = 2; |
| 196 | |
| 197 | aux = (*input)[1]; |
| 198 | if ((aux & 0xc0) != 0x80) { |
| 199 | return LY_EINVAL; |
| 200 | } |
| 201 | c = ((c & 0x1f) << 6) | (aux & 0x3f); |
| 202 | |
| 203 | if (c < 0x80) { |
| 204 | return LY_EINVAL; |
| 205 | } |
| 206 | } else if ((c & 0xf0) == 0xe0) { |
| 207 | /* three bytes character */ |
| 208 | len = 3; |
| 209 | |
| 210 | c &= 0x0f; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 211 | for (uint64_t i = 1; i <= 2; i++) { |
Radek Krejci | b416be6 | 2018-10-01 14:51:45 +0200 | [diff] [blame] | 212 | aux = (*input)[i]; |
| 213 | if ((aux & 0xc0) != 0x80) { |
| 214 | return LY_EINVAL; |
| 215 | } |
| 216 | |
| 217 | c = (c << 6) | (aux & 0x3f); |
| 218 | } |
| 219 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 220 | if ((c < 0x800) || ((c > 0xd7ff) && (c < 0xe000)) || (c > 0xfffd)) { |
Radek Krejci | b416be6 | 2018-10-01 14:51:45 +0200 | [diff] [blame] | 221 | return LY_EINVAL; |
| 222 | } |
| 223 | } else if ((c & 0xf8) == 0xf0) { |
| 224 | /* four bytes character */ |
| 225 | len = 4; |
| 226 | |
| 227 | c &= 0x07; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 228 | for (uint64_t i = 1; i <= 3; i++) { |
Radek Krejci | b416be6 | 2018-10-01 14:51:45 +0200 | [diff] [blame] | 229 | aux = (*input)[i]; |
| 230 | if ((aux & 0xc0) != 0x80) { |
| 231 | return LY_EINVAL; |
| 232 | } |
| 233 | |
| 234 | c = (c << 6) | (aux & 0x3f); |
| 235 | } |
| 236 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 237 | if ((c < 0x1000) || (c > 0x10ffff)) { |
Radek Krejci | b416be6 | 2018-10-01 14:51:45 +0200 | [diff] [blame] | 238 | return LY_EINVAL; |
| 239 | } |
| 240 | } else { |
| 241 | return LY_EINVAL; |
| 242 | } |
| 243 | |
| 244 | (*utf8_char) = c; |
| 245 | (*input) += len; |
| 246 | if (bytes_read) { |
| 247 | (*bytes_read) = len; |
| 248 | } |
| 249 | return LY_SUCCESS; |
| 250 | } |
| 251 | |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 252 | LY_ERR |
| 253 | ly_pututf8(char *dst, uint32_t value, size_t *bytes_written) |
| 254 | { |
| 255 | if (value < 0x80) { |
| 256 | /* one byte character */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 257 | if ((value < 0x20) && |
| 258 | (value != 0x09) && |
| 259 | (value != 0x0a) && |
| 260 | (value != 0x0d)) { |
Michal Vasko | 519097f | 2023-05-25 10:00:44 +0200 | [diff] [blame] | 261 | /* valid UTF8 but not YANG string character */ |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 262 | return LY_EINVAL; |
| 263 | } |
| 264 | |
| 265 | dst[0] = value; |
| 266 | (*bytes_written) = 1; |
| 267 | } else if (value < 0x800) { |
| 268 | /* two bytes character */ |
| 269 | dst[0] = 0xc0 | (value >> 6); |
| 270 | dst[1] = 0x80 | (value & 0x3f); |
| 271 | (*bytes_written) = 2; |
| 272 | } else if (value < 0xfffe) { |
| 273 | /* three bytes character */ |
| 274 | if (((value & 0xf800) == 0xd800) || |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 275 | ((value >= 0xfdd0) && (value <= 0xfdef))) { |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 276 | /* exclude surrogate blocks %xD800-DFFF */ |
| 277 | /* exclude noncharacters %xFDD0-FDEF */ |
| 278 | return LY_EINVAL; |
| 279 | } |
| 280 | |
| 281 | dst[0] = 0xe0 | (value >> 12); |
| 282 | dst[1] = 0x80 | ((value >> 6) & 0x3f); |
| 283 | dst[2] = 0x80 | (value & 0x3f); |
| 284 | |
| 285 | (*bytes_written) = 3; |
| 286 | } else if (value < 0x10fffe) { |
| 287 | if ((value & 0xffe) == 0xffe) { |
| 288 | /* exclude noncharacters %xFFFE-FFFF, %x1FFFE-1FFFF, %x2FFFE-2FFFF, %x3FFFE-3FFFF, %x4FFFE-4FFFF, |
| 289 | * %x5FFFE-5FFFF, %x6FFFE-6FFFF, %x7FFFE-7FFFF, %x8FFFE-8FFFF, %x9FFFE-9FFFF, %xAFFFE-AFFFF, |
| 290 | * %xBFFFE-BFFFF, %xCFFFE-CFFFF, %xDFFFE-DFFFF, %xEFFFE-EFFFF, %xFFFFE-FFFFF, %x10FFFE-10FFFF */ |
| 291 | return LY_EINVAL; |
| 292 | } |
| 293 | /* four bytes character */ |
| 294 | dst[0] = 0xf0 | (value >> 18); |
| 295 | dst[1] = 0x80 | ((value >> 12) & 0x3f); |
| 296 | dst[2] = 0x80 | ((value >> 6) & 0x3f); |
| 297 | dst[3] = 0x80 | (value & 0x3f); |
| 298 | |
| 299 | (*bytes_written) = 4; |
| 300 | } else { |
| 301 | return LY_EINVAL; |
| 302 | } |
| 303 | return LY_SUCCESS; |
| 304 | } |
| 305 | |
Radek Krejci | 76c9801 | 2019-08-14 11:23:24 +0200 | [diff] [blame] | 306 | /** |
| 307 | * @brief Static table of the UTF8 characters lengths according to their first byte. |
| 308 | */ |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 309 | static const unsigned char utf8_char_length_table[] = { |
Radek Krejci | 76c9801 | 2019-08-14 11:23:24 +0200 | [diff] [blame] | 310 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 311 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 312 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 313 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 314 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 315 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 316 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 317 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 318 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 319 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 320 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 321 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 322 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 323 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 324 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 325 | 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 1, 1 |
| 326 | }; |
| 327 | |
| 328 | size_t |
| 329 | ly_utf8len(const char *str, size_t bytes) |
| 330 | { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 331 | size_t len = 0; |
| 332 | const char *ptr = str; |
Radek Krejci | 76c9801 | 2019-08-14 11:23:24 +0200 | [diff] [blame] | 333 | |
Michal Vasko | b4d40d6 | 2021-05-04 11:42:44 +0200 | [diff] [blame] | 334 | while (((size_t)(ptr - str) < bytes) && *ptr) { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 335 | ++len; |
| 336 | ptr += utf8_char_length_table[((unsigned char)(*ptr))]; |
| 337 | } |
Radek Krejci | 76c9801 | 2019-08-14 11:23:24 +0200 | [diff] [blame] | 338 | return len; |
| 339 | } |
| 340 | |
Michal Vasko | 7b3a00e | 2023-08-09 11:58:03 +0200 | [diff] [blame] | 341 | int |
Radek Krejci | d972c25 | 2018-09-25 13:23:39 +0200 | [diff] [blame] | 342 | LY_VCODE_INSTREXP_len(const char *str) |
| 343 | { |
Michal Vasko | 7b3a00e | 2023-08-09 11:58:03 +0200 | [diff] [blame] | 344 | int len = 0; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 345 | |
Radek Krejci | d972c25 | 2018-09-25 13:23:39 +0200 | [diff] [blame] | 346 | if (!str) { |
| 347 | return len; |
| 348 | } else if (!str[0]) { |
| 349 | return 1; |
| 350 | } |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 351 | for (len = 1; len < LY_VCODE_INSTREXP_MAXLEN && str[len]; ++len) {} |
Radek Krejci | d972c25 | 2018-09-25 13:23:39 +0200 | [diff] [blame] | 352 | return len; |
| 353 | } |
| 354 | |
Jan Kundrát | f1960dc | 2021-12-12 03:12:23 +0100 | [diff] [blame] | 355 | #ifdef HAVE_MMAP |
Radek Krejci | f345c01 | 2018-09-19 11:12:59 +0200 | [diff] [blame] | 356 | LY_ERR |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 357 | 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] | 358 | { |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 359 | struct stat sb; |
| 360 | long pagesize; |
| 361 | size_t m; |
Michal Vasko | 841d1a9 | 2018-09-07 15:40:31 +0200 | [diff] [blame] | 362 | |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 363 | assert(length); |
| 364 | assert(addr); |
| 365 | assert(fd >= 0); |
Michal Vasko | 841d1a9 | 2018-09-07 15:40:31 +0200 | [diff] [blame] | 366 | |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 367 | if (fstat(fd, &sb) == -1) { |
| 368 | LOGERR(ctx, LY_ESYS, "Failed to stat the file descriptor (%s) for the mmap().", strerror(errno)); |
| 369 | return LY_ESYS; |
Michal Vasko | 841d1a9 | 2018-09-07 15:40:31 +0200 | [diff] [blame] | 370 | } |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 371 | if (!S_ISREG(sb.st_mode)) { |
| 372 | LOGERR(ctx, LY_EINVAL, "File to mmap() is not a regular file."); |
| 373 | return LY_ESYS; |
Michal Vasko | 841d1a9 | 2018-09-07 15:40:31 +0200 | [diff] [blame] | 374 | } |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 375 | if (!sb.st_size) { |
| 376 | *addr = NULL; |
| 377 | return LY_SUCCESS; |
| 378 | } |
| 379 | pagesize = sysconf(_SC_PAGESIZE); |
| 380 | |
| 381 | m = sb.st_size % pagesize; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 382 | if (m && (pagesize - m >= 1)) { |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 383 | /* there will be enough space (at least 1 byte) after the file content mapping to provide zeroed NULL-termination byte */ |
| 384 | *length = sb.st_size + 1; |
| 385 | *addr = mmap(NULL, *length, PROT_READ, MAP_PRIVATE, fd, 0); |
| 386 | } else { |
| 387 | /* there will not be enough bytes after the file content mapping for the additional bytes and some of them |
| 388 | * would overflow into another page that would not be zerroed and any access into it would generate SIGBUS. |
| 389 | * Therefore we have to do the following hack with double mapping. First, the required number of bytes |
| 390 | * (including the additinal bytes) is required as anonymous and thus they will be really provided (actually more |
| 391 | * because of using whole pages) and also initialized by zeros. Then, the file is mapped to the same address |
| 392 | * where the anonymous mapping starts. */ |
| 393 | *length = sb.st_size + pagesize; |
| 394 | *addr = mmap(NULL, *length, PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
| 395 | *addr = mmap(*addr, sb.st_size, PROT_READ, MAP_PRIVATE | MAP_FIXED, fd, 0); |
| 396 | } |
| 397 | if (*addr == MAP_FAILED) { |
| 398 | LOGERR(ctx, LY_ESYS, "mmap() failed (%s).", strerror(errno)); |
| 399 | return LY_ESYS; |
Michal Vasko | 841d1a9 | 2018-09-07 15:40:31 +0200 | [diff] [blame] | 400 | } |
| 401 | |
Radek Krejci | f345c01 | 2018-09-19 11:12:59 +0200 | [diff] [blame] | 402 | return LY_SUCCESS; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 403 | } |
Michal Vasko | 841d1a9 | 2018-09-07 15:40:31 +0200 | [diff] [blame] | 404 | |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 405 | LY_ERR |
| 406 | ly_munmap(void *addr, size_t length) |
| 407 | { |
| 408 | if (munmap(addr, length)) { |
| 409 | return LY_ESYS; |
| 410 | } |
| 411 | return LY_SUCCESS; |
Michal Vasko | 841d1a9 | 2018-09-07 15:40:31 +0200 | [diff] [blame] | 412 | } |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 413 | |
Jan Kundrát | f1960dc | 2021-12-12 03:12:23 +0100 | [diff] [blame] | 414 | #else |
| 415 | |
| 416 | LY_ERR |
| 417 | ly_mmap(struct ly_ctx *ctx, int fd, size_t *length, void **addr) |
| 418 | { |
| 419 | struct stat sb; |
| 420 | size_t m; |
| 421 | |
| 422 | assert(length); |
| 423 | assert(addr); |
| 424 | assert(fd >= 0); |
| 425 | |
Jan Kundrát | d31adc1 | 2022-07-07 21:36:15 +0200 | [diff] [blame] | 426 | #if _WIN32 |
| 427 | if (_setmode(fd, _O_BINARY) == -1) { |
| 428 | LOGERR(ctx, LY_ESYS, "Failed to switch the file descriptor to binary mode.", strerror(errno)); |
| 429 | return LY_ESYS; |
| 430 | } |
| 431 | #endif |
| 432 | |
Jan Kundrát | f1960dc | 2021-12-12 03:12:23 +0100 | [diff] [blame] | 433 | if (fstat(fd, &sb) == -1) { |
| 434 | LOGERR(ctx, LY_ESYS, "Failed to stat the file descriptor (%s) for the mmap().", strerror(errno)); |
| 435 | return LY_ESYS; |
| 436 | } |
| 437 | if (!S_ISREG(sb.st_mode)) { |
| 438 | LOGERR(ctx, LY_EINVAL, "File to mmap() is not a regular file."); |
| 439 | return LY_ESYS; |
| 440 | } |
| 441 | if (!sb.st_size) { |
| 442 | *addr = NULL; |
| 443 | return LY_SUCCESS; |
| 444 | } |
| 445 | /* On Windows, the mman-win32 mmap() emulation uses CreateFileMapping and MapViewOfFile, and these functions |
| 446 | * do not allow mapping more than "length of file" bytes for PROT_READ. Remapping existing mappings is not allowed, either. |
| 447 | * At that point the path of least resistance is just reading the file in as-is. */ |
| 448 | m = sb.st_size + 1; |
| 449 | char *buf = calloc(m, 1); |
| 450 | |
| 451 | if (!buf) { |
| 452 | LOGERR(ctx, LY_ESYS, "ly_mmap: malloc() failed (%s).", strerror(errno)); |
| 453 | } |
| 454 | *addr = buf; |
| 455 | *length = m; |
| 456 | |
| 457 | lseek(fd, 0, SEEK_SET); |
| 458 | ssize_t to_read = m - 1; |
| 459 | |
| 460 | while (to_read > 0) { |
| 461 | ssize_t n = read(fd, buf, to_read); |
Michal Vasko | 2bf4af4 | 2023-01-04 12:08:38 +0100 | [diff] [blame] | 462 | |
Jan Kundrát | f1960dc | 2021-12-12 03:12:23 +0100 | [diff] [blame] | 463 | if (n == 0) { |
| 464 | return LY_SUCCESS; |
| 465 | } else if (n < 0) { |
| 466 | if (errno == EINTR) { |
| 467 | continue; // can I get this on Windows? |
| 468 | } |
| 469 | LOGERR(ctx, LY_ESYS, "ly_mmap: read() failed (%s).", strerror(errno)); |
| 470 | } |
| 471 | to_read -= n; |
| 472 | buf += n; |
| 473 | } |
| 474 | return LY_SUCCESS; |
| 475 | } |
| 476 | |
| 477 | LY_ERR |
| 478 | ly_munmap(void *addr, size_t length) |
| 479 | { |
| 480 | (void)length; |
| 481 | free(addr); |
| 482 | return LY_SUCCESS; |
| 483 | } |
| 484 | |
| 485 | #endif |
| 486 | |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 487 | LY_ERR |
Radek Krejci | 4546aa6 | 2019-07-15 16:53:32 +0200 | [diff] [blame] | 488 | ly_strcat(char **dest, const char *format, ...) |
| 489 | { |
| 490 | va_list fp; |
| 491 | char *addition = NULL; |
| 492 | size_t len; |
| 493 | |
| 494 | va_start(fp, format); |
| 495 | len = vasprintf(&addition, format, fp); |
| 496 | len += (*dest ? strlen(*dest) : 0) + 1; |
| 497 | |
| 498 | if (*dest) { |
| 499 | *dest = ly_realloc(*dest, len); |
| 500 | if (!*dest) { |
Radek Krejci | 1cd812f | 2020-12-01 12:17:53 +0100 | [diff] [blame] | 501 | va_end(fp); |
Radek Krejci | 4546aa6 | 2019-07-15 16:53:32 +0200 | [diff] [blame] | 502 | return LY_EMEM; |
| 503 | } |
| 504 | *dest = strcat(*dest, addition); |
| 505 | free(addition); |
| 506 | } else { |
| 507 | *dest = addition; |
| 508 | } |
| 509 | |
| 510 | va_end(fp); |
| 511 | return LY_SUCCESS; |
| 512 | } |
| 513 | |
| 514 | LY_ERR |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 515 | 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] | 516 | { |
Michal Vasko | b4d40d6 | 2021-05-04 11:42:44 +0200 | [diff] [blame] | 517 | LY_ERR rc = LY_SUCCESS; |
| 518 | char *ptr, *str; |
Radek Krejci | 9ea8ca1 | 2019-06-10 13:11:55 +0200 | [diff] [blame] | 519 | int64_t i; |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 520 | |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 521 | 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] | 522 | |
Michal Vasko | b4d40d6 | 2021-05-04 11:42:44 +0200 | [diff] [blame] | 523 | /* duplicate the value */ |
| 524 | str = strndup(val_str, val_len); |
| 525 | LY_CHECK_RET(!str, LY_EMEM); |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 526 | |
Michal Vasko | b4d40d6 | 2021-05-04 11:42:44 +0200 | [diff] [blame] | 527 | /* parse the value to avoid accessing following bytes */ |
| 528 | errno = 0; |
| 529 | i = strtoll(str, &ptr, base); |
| 530 | if (errno || (ptr == str)) { |
| 531 | /* invalid string */ |
| 532 | rc = LY_EVALID; |
Radek Krejci | 9ea8ca1 | 2019-06-10 13:11:55 +0200 | [diff] [blame] | 533 | } else if ((i < min) || (i > max)) { |
Michal Vasko | b4d40d6 | 2021-05-04 11:42:44 +0200 | [diff] [blame] | 534 | /* invalid number */ |
| 535 | rc = LY_EDENIED; |
| 536 | } else if (*ptr) { |
| 537 | while (isspace(*ptr)) { |
| 538 | ++ptr; |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 539 | } |
Michal Vasko | b4d40d6 | 2021-05-04 11:42:44 +0200 | [diff] [blame] | 540 | if (*ptr) { |
| 541 | /* invalid characters after some number */ |
| 542 | rc = LY_EVALID; |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 543 | } |
| 544 | } |
Radek Krejci | 9ea8ca1 | 2019-06-10 13:11:55 +0200 | [diff] [blame] | 545 | |
Michal Vasko | b4d40d6 | 2021-05-04 11:42:44 +0200 | [diff] [blame] | 546 | /* cleanup */ |
| 547 | free(str); |
| 548 | if (!rc) { |
| 549 | *ret = i; |
| 550 | } |
| 551 | return rc; |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | LY_ERR |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 555 | 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] | 556 | { |
Michal Vasko | b4d40d6 | 2021-05-04 11:42:44 +0200 | [diff] [blame] | 557 | LY_ERR rc = LY_SUCCESS; |
| 558 | char *ptr, *str; |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 559 | uint64_t u; |
| 560 | |
Michal Vasko | b4d40d6 | 2021-05-04 11:42:44 +0200 | [diff] [blame] | 561 | 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] | 562 | |
Michal Vasko | b4d40d6 | 2021-05-04 11:42:44 +0200 | [diff] [blame] | 563 | /* duplicate the value to avoid accessing following bytes */ |
| 564 | str = strndup(val_str, val_len); |
| 565 | LY_CHECK_RET(!str, LY_EMEM); |
| 566 | |
| 567 | /* parse the value */ |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 568 | errno = 0; |
Michal Vasko | b4d40d6 | 2021-05-04 11:42:44 +0200 | [diff] [blame] | 569 | u = strtoull(str, &ptr, base); |
| 570 | if (errno || (ptr == str)) { |
| 571 | /* invalid string */ |
| 572 | rc = LY_EVALID; |
| 573 | } else if ((u > max) || (u && (str[0] == '-'))) { |
| 574 | /* invalid number */ |
| 575 | rc = LY_EDENIED; |
| 576 | } else if (*ptr) { |
| 577 | while (isspace(*ptr)) { |
| 578 | ++ptr; |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 579 | } |
Michal Vasko | b4d40d6 | 2021-05-04 11:42:44 +0200 | [diff] [blame] | 580 | if (*ptr) { |
| 581 | /* invalid characters after some number */ |
| 582 | rc = LY_EVALID; |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 583 | } |
| 584 | } |
| 585 | |
Michal Vasko | b4d40d6 | 2021-05-04 11:42:44 +0200 | [diff] [blame] | 586 | /* cleanup */ |
| 587 | free(str); |
| 588 | if (!rc) { |
| 589 | *ret = u; |
| 590 | } |
| 591 | return rc; |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 592 | } |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 593 | |
| 594 | /** |
| 595 | * @brief Parse an identifier. |
| 596 | * |
| 597 | * ;; An identifier MUST NOT start with (('X'|'x') ('M'|'m') ('L'|'l')) |
| 598 | * identifier = (ALPHA / "_") |
| 599 | * *(ALPHA / DIGIT / "_" / "-" / ".") |
| 600 | * |
| 601 | * @param[in,out] id Identifier to parse. When returned, it points to the first character which is not part of the identifier. |
| 602 | * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid starting character. |
| 603 | */ |
| 604 | static LY_ERR |
| 605 | lys_parse_id(const char **id) |
| 606 | { |
| 607 | assert(id && *id); |
| 608 | |
| 609 | if (!is_yangidentstartchar(**id)) { |
| 610 | return LY_EINVAL; |
| 611 | } |
| 612 | ++(*id); |
| 613 | |
| 614 | while (is_yangidentchar(**id)) { |
| 615 | ++(*id); |
| 616 | } |
| 617 | return LY_SUCCESS; |
| 618 | } |
| 619 | |
| 620 | LY_ERR |
| 621 | ly_parse_nodeid(const char **id, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len) |
| 622 | { |
| 623 | assert(id && *id); |
| 624 | assert(prefix && prefix_len); |
| 625 | assert(name && name_len); |
| 626 | |
| 627 | *prefix = *id; |
| 628 | *prefix_len = 0; |
| 629 | *name = NULL; |
| 630 | *name_len = 0; |
| 631 | |
| 632 | LY_CHECK_RET(lys_parse_id(id)); |
| 633 | if (**id == ':') { |
| 634 | /* there is prefix */ |
| 635 | *prefix_len = *id - *prefix; |
| 636 | ++(*id); |
| 637 | *name = *id; |
| 638 | |
| 639 | LY_CHECK_RET(lys_parse_id(id)); |
| 640 | *name_len = *id - *name; |
| 641 | } else { |
| 642 | /* there is no prefix, so what we have as prefix now is actually the name */ |
| 643 | *name = *prefix; |
| 644 | *name_len = *id - *name; |
| 645 | *prefix = NULL; |
| 646 | } |
| 647 | |
| 648 | return LY_SUCCESS; |
| 649 | } |
| 650 | |
| 651 | LY_ERR |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 652 | 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] | 653 | const char **prefix, size_t *prefix_len, const char **id, size_t *id_len, const char **value, size_t *value_len, |
| 654 | const char **errmsg) |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 655 | { |
| 656 | LY_ERR ret = LY_EVALID; |
| 657 | const char *in = *pred; |
| 658 | size_t offset = 1; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 659 | 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] | 660 | char quot; |
| 661 | |
Radek Krejci | 4607f54 | 2020-12-01 12:18:49 +0100 | [diff] [blame] | 662 | assert(in[0] == '['); |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 663 | |
| 664 | *prefix = *id = *value = NULL; |
| 665 | *prefix_len = *id_len = *value_len = 0; |
| 666 | |
| 667 | /* leading *WSP */ |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 668 | for ( ; isspace(in[offset]); offset++) {} |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 669 | |
| 670 | if (isdigit(in[offset])) { |
| 671 | /* pos: "[" *WSP positive-integer-value *WSP "]" */ |
| 672 | if (in[offset] == '0') { |
| 673 | /* zero */ |
| 674 | *errmsg = "The position predicate cannot be zero."; |
| 675 | goto error; |
| 676 | } |
| 677 | |
| 678 | /* positive-integer-value */ |
Radek Krejci | 10bfdf8 | 2019-06-10 14:08:13 +0200 | [diff] [blame] | 679 | *value = &in[offset++]; |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 680 | for ( ; isdigit(in[offset]); offset++) {} |
Radek Krejci | 10bfdf8 | 2019-06-10 14:08:13 +0200 | [diff] [blame] | 681 | *value_len = &in[offset] - *value; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 682 | |
| 683 | } else if (in[offset] == '.') { |
| 684 | /* leaf-list-predicate: "[" *WSP "." *WSP "=" *WSP quoted-string *WSP "]" */ |
| 685 | *id = &in[offset]; |
| 686 | *id_len = 1; |
| 687 | offset++; |
| 688 | expr = 1; |
Radek Krejci | 10bfdf8 | 2019-06-10 14:08:13 +0200 | [diff] [blame] | 689 | } else if (in[offset] == '-') { |
| 690 | /* typically negative value */ |
| 691 | *errmsg = "Invalid instance predicate format (negative position or invalid node-identifier)."; |
| 692 | goto error; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 693 | } else { |
| 694 | /* key-predicate: "[" *WSP node-identifier *WSP "=" *WSP quoted-string *WSP "]" */ |
| 695 | in = &in[offset]; |
| 696 | if (ly_parse_nodeid(&in, prefix, prefix_len, id, id_len)) { |
| 697 | *errmsg = "Invalid node-identifier."; |
| 698 | goto error; |
| 699 | } |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 700 | if ((format == LYD_XML) && !(*prefix)) { |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 701 | /* all node names MUST be qualified with explicit namespace prefix */ |
| 702 | *errmsg = "Missing prefix of a node name."; |
| 703 | goto error; |
| 704 | } |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 705 | offset = in - *pred; |
| 706 | in = *pred; |
Radek Krejci | 10bfdf8 | 2019-06-10 14:08:13 +0200 | [diff] [blame] | 707 | expr = 2; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | if (expr) { |
| 711 | /* *WSP "=" *WSP quoted-string *WSP "]" */ |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 712 | for ( ; isspace(in[offset]); offset++) {} |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 713 | |
| 714 | if (in[offset] != '=') { |
Radek Krejci | 10bfdf8 | 2019-06-10 14:08:13 +0200 | [diff] [blame] | 715 | if (expr == 1) { |
| 716 | *errmsg = "Unexpected character instead of \'=\' in leaf-list-predicate."; |
| 717 | } else { /* 2 */ |
| 718 | *errmsg = "Unexpected character instead of \'=\' in key-predicate."; |
| 719 | } |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 720 | goto error; |
| 721 | } |
| 722 | offset++; |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 723 | for ( ; isspace(in[offset]); offset++) {} |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 724 | |
| 725 | /* quoted-string */ |
| 726 | quot = in[offset++]; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 727 | if ((quot != '\'') && (quot != '\"')) { |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 728 | *errmsg = "String value is not quoted."; |
| 729 | goto error; |
| 730 | } |
| 731 | *value = &in[offset]; |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 732 | for ( ; offset < limit && (in[offset] != quot || (offset && in[offset - 1] == '\\')); offset++) {} |
Radek Krejci | 10bfdf8 | 2019-06-10 14:08:13 +0200 | [diff] [blame] | 733 | if (in[offset] == quot) { |
| 734 | *value_len = &in[offset] - *value; |
| 735 | offset++; |
| 736 | } else { |
| 737 | *errmsg = "Value is not terminated quoted-string."; |
| 738 | goto error; |
| 739 | } |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 740 | } |
| 741 | |
| 742 | /* *WSP "]" */ |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 743 | for ( ; isspace(in[offset]); offset++) {} |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 744 | if (in[offset] != ']') { |
Radek Krejci | 10bfdf8 | 2019-06-10 14:08:13 +0200 | [diff] [blame] | 745 | if (expr == 0) { |
| 746 | *errmsg = "Predicate (pos) is not terminated by \']\' character."; |
| 747 | } else if (expr == 1) { |
| 748 | *errmsg = "Predicate (leaf-list-predicate) is not terminated by \']\' character."; |
| 749 | } else { /* 2 */ |
| 750 | *errmsg = "Predicate (key-predicate) is not terminated by \']\' character."; |
| 751 | } |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 752 | goto error; |
| 753 | } |
Radek Krejci | 10bfdf8 | 2019-06-10 14:08:13 +0200 | [diff] [blame] | 754 | offset++; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 755 | |
Radek Krejci | 10bfdf8 | 2019-06-10 14:08:13 +0200 | [diff] [blame] | 756 | if (offset <= limit) { |
| 757 | *pred = &in[offset]; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 758 | return LY_SUCCESS; |
| 759 | } |
| 760 | |
| 761 | /* we read after the limit */ |
| 762 | *errmsg = "Predicate is incomplete."; |
| 763 | *prefix = *id = *value = NULL; |
| 764 | *prefix_len = *id_len = *value_len = 0; |
| 765 | offset = limit; |
| 766 | ret = LY_EINVAL; |
| 767 | |
| 768 | error: |
| 769 | *pred = &in[offset]; |
| 770 | return ret; |
| 771 | } |