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 | |
| 15 | #include "common.h" |
Michal Vasko | 1324b6c | 2018-09-07 11:16:23 +0200 | [diff] [blame] | 16 | |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 17 | #include <assert.h> |
| 18 | #include <errno.h> |
Michal Vasko | 1324b6c | 2018-09-07 11:16:23 +0200 | [diff] [blame] | 19 | #include <stdlib.h> |
Michal Vasko | 841d1a9 | 2018-09-07 15:40:31 +0200 | [diff] [blame] | 20 | #include <ctype.h> |
| 21 | #include <string.h> |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 22 | #include <sys/mman.h> |
| 23 | #include <sys/stat.h> |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 24 | #include <unistd.h> |
Michal Vasko | 841d1a9 | 2018-09-07 15:40:31 +0200 | [diff] [blame] | 25 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 26 | #include "extensions.h" |
Michal Vasko | 841d1a9 | 2018-09-07 15:40:31 +0200 | [diff] [blame] | 27 | #include "tree_schema.h" |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 28 | #include "tree_schema_internal.h" |
Michal Vasko | 1324b6c | 2018-09-07 11:16:23 +0200 | [diff] [blame] | 29 | |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 30 | const char *const ly_stmt_list[] = { |
| 31 | [YANG_ACTION] = "action", |
| 32 | [YANG_ANYDATA] = "anydata", |
| 33 | [YANG_ANYXML] = "anyxml", |
| 34 | [YANG_ARGUMENT] = "argument", |
| 35 | [YANG_AUGMENT] = "augment", |
| 36 | [YANG_BASE] = "base", |
| 37 | [YANG_BELONGS_TO] = "belongs-to", |
| 38 | [YANG_BIT] = "bit", |
| 39 | [YANG_CASE] = "case", |
| 40 | [YANG_CHOICE] = "choice", |
| 41 | [YANG_CONFIG] = "config", |
| 42 | [YANG_CONTACT] = "contact", |
| 43 | [YANG_CONTAINER] = "container", |
| 44 | [YANG_CUSTOM] = "<extension-instance>", |
| 45 | [YANG_DEFAULT] = "default", |
| 46 | [YANG_DESCRIPTION] = "description", |
| 47 | [YANG_DEVIATE] = "deviate", |
| 48 | [YANG_DEVIATION] = "deviation", |
| 49 | [YANG_ENUM] = "enum", |
| 50 | [YANG_ERROR_APP_TAG] = "error-app-tag", |
| 51 | [YANG_ERROR_MESSAGE] = "error-message", |
| 52 | [YANG_EXTENSION] = "extension", |
| 53 | [YANG_FEATURE] = "feature", |
| 54 | [YANG_FRACTION_DIGITS] = "fraction-digits", |
| 55 | [YANG_GROUPING] = "grouping", |
| 56 | [YANG_IDENTITY] = "identitiy", |
| 57 | [YANG_IF_FEATURE] = "if-feature", |
| 58 | [YANG_IMPORT] = "import", |
| 59 | [YANG_INCLUDE] = "include", |
| 60 | [YANG_INPUT] = "input", |
| 61 | [YANG_KEY] = "key", |
| 62 | [YANG_LEAF] = "leaf", |
| 63 | [YANG_LEAF_LIST] = "leaf-list", |
| 64 | [YANG_LENGTH] = "length", |
| 65 | [YANG_LIST] = "list", |
| 66 | [YANG_MANDATORY] = "mandatory", |
| 67 | [YANG_MAX_ELEMENTS] = "max-elements", |
| 68 | [YANG_MIN_ELEMENTS] = "min-elements", |
| 69 | [YANG_MODIFIER] = "modifier", |
| 70 | [YANG_MODULE] = "module", |
| 71 | [YANG_MUST] = "must", |
| 72 | [YANG_NAMESPACE] = "namespace", |
| 73 | [YANG_NOTIFICATION] = "notification", |
| 74 | [YANG_ORDERED_BY] = "ordered-by", |
| 75 | [YANG_ORGANIZATION] = "organization", |
| 76 | [YANG_OUTPUT] = "output", |
| 77 | [YANG_PATH] = "path", |
| 78 | [YANG_PATTERN] = "pattern", |
| 79 | [YANG_POSITION] = "position", |
| 80 | [YANG_PREFIX] = "prefix", |
| 81 | [YANG_PRESENCE] = "presence", |
| 82 | [YANG_RANGE] = "range", |
| 83 | [YANG_REFERENCE] = "reference", |
| 84 | [YANG_REFINE] = "refine", |
| 85 | [YANG_REQUIRE_INSTANCE] = "require-instance", |
| 86 | [YANG_REVISION] = "revision", |
| 87 | [YANG_REVISION_DATE] = "revision-date", |
| 88 | [YANG_RPC] = "rpc", |
| 89 | [YANG_STATUS] = "status", |
| 90 | [YANG_SUBMODULE] = "submodule", |
| 91 | [YANG_TYPE] = "type", |
| 92 | [YANG_TYPEDEF] = "typedef", |
| 93 | [YANG_UNIQUE] = "unique", |
| 94 | [YANG_UNITS] = "units", |
| 95 | [YANG_USES] = "uses", |
| 96 | [YANG_VALUE] = "value", |
| 97 | [YANG_WHEN] = "when", |
| 98 | [YANG_YANG_VERSION] = "yang-version", |
| 99 | [YANG_YIN_ELEMENT] = "yin-element", |
| 100 | [YANG_SEMICOLON] = ";", |
| 101 | [YANG_LEFT_BRACE] = "{", |
| 102 | [YANG_RIGHT_BRACE] = "}", |
| 103 | }; |
| 104 | |
| 105 | const char *const lyext_substmt_list[] = { |
| 106 | [LYEXT_SUBSTMT_ARGUMENT] = "argument", |
| 107 | [LYEXT_SUBSTMT_BASE] = "base", |
| 108 | [LYEXT_SUBSTMT_BELONGSTO] = "belongs-to", |
| 109 | [LYEXT_SUBSTMT_CONTACT] = "contact", |
| 110 | [LYEXT_SUBSTMT_DEFAULT] = "default", |
| 111 | [LYEXT_SUBSTMT_DESCRIPTION] = "description", |
| 112 | [LYEXT_SUBSTMT_ERRTAG] = "error-app-tag", |
| 113 | [LYEXT_SUBSTMT_ERRMSG] = "error-message", |
| 114 | [LYEXT_SUBSTMT_KEY] = "key", |
| 115 | [LYEXT_SUBSTMT_NAMESPACE] = "namespace", |
| 116 | [LYEXT_SUBSTMT_ORGANIZATION] = "organization", |
| 117 | [LYEXT_SUBSTMT_PATH] = "path", |
| 118 | [LYEXT_SUBSTMT_PREFIX] = "prefix", |
| 119 | [LYEXT_SUBSTMT_PRESENCE] = "presence", |
| 120 | [LYEXT_SUBSTMT_REFERENCE] = "reference", |
| 121 | [LYEXT_SUBSTMT_REVISIONDATE] = "revision-date", |
| 122 | [LYEXT_SUBSTMT_UNITS] = "units", |
| 123 | [LYEXT_SUBSTMT_VALUE] = "value", |
| 124 | [LYEXT_SUBSTMT_VERSION] = "yang-version", |
| 125 | [LYEXT_SUBSTMT_MODIFIER] = "modifier", |
| 126 | [LYEXT_SUBSTMT_REQINSTANCE] = "require-instance", |
| 127 | [LYEXT_SUBSTMT_YINELEM] = "yin-element", |
| 128 | [LYEXT_SUBSTMT_CONFIG] = "config", |
| 129 | [LYEXT_SUBSTMT_MANDATORY] = "mandatory", |
| 130 | [LYEXT_SUBSTMT_ORDEREDBY] = "ordered-by", |
| 131 | [LYEXT_SUBSTMT_STATUS] = "status", |
| 132 | [LYEXT_SUBSTMT_FRACDIGITS] = "fraction-digits", |
| 133 | [LYEXT_SUBSTMT_MAX] = "max-elements", |
| 134 | [LYEXT_SUBSTMT_MIN] = "min-elements", |
| 135 | [LYEXT_SUBSTMT_POSITION] = "position", |
| 136 | [LYEXT_SUBSTMT_UNIQUE] = "unique", |
| 137 | [LYEXT_SUBSTMT_IFFEATURE] = "if-feature", |
| 138 | }; |
| 139 | |
| 140 | const char *const ly_devmod_list[] = { |
| 141 | [LYS_DEV_NOT_SUPPORTED] = "not-supported", |
| 142 | [LYS_DEV_ADD] = "add", |
| 143 | [LYS_DEV_DELETE] = "delete", |
| 144 | [LYS_DEV_REPLACE] = "replace", |
| 145 | }; |
| 146 | |
Michal Vasko | 1324b6c | 2018-09-07 11:16:23 +0200 | [diff] [blame] | 147 | void * |
| 148 | ly_realloc(void *ptr, size_t size) |
| 149 | { |
| 150 | void *new_mem; |
| 151 | |
| 152 | new_mem = realloc(ptr, size); |
| 153 | if (!new_mem) { |
| 154 | free(ptr); |
| 155 | } |
| 156 | |
| 157 | return new_mem; |
| 158 | } |
Michal Vasko | 841d1a9 | 2018-09-07 15:40:31 +0200 | [diff] [blame] | 159 | |
Radek Krejci | b416be6 | 2018-10-01 14:51:45 +0200 | [diff] [blame] | 160 | LY_ERR |
| 161 | ly_getutf8(const char **input, unsigned int *utf8_char, size_t *bytes_read) |
| 162 | { |
| 163 | unsigned int c, len; |
| 164 | int aux; |
| 165 | int i; |
| 166 | |
Radek Krejci | cc6a45c | 2019-05-13 10:16:14 +0200 | [diff] [blame] | 167 | if (bytes_read) { |
| 168 | (*bytes_read) = 0; |
| 169 | } |
| 170 | |
Radek Krejci | b416be6 | 2018-10-01 14:51:45 +0200 | [diff] [blame] | 171 | c = (*input)[0]; |
| 172 | LY_CHECK_RET(!c, LY_EINVAL); |
| 173 | |
| 174 | if (!(c & 0x80)) { |
| 175 | /* one byte character */ |
| 176 | len = 1; |
| 177 | |
| 178 | if (c < 0x20 && c != 0x9 && c != 0xa && c != 0xd) { |
| 179 | return LY_EINVAL; |
| 180 | } |
| 181 | } else if ((c & 0xe0) == 0xc0) { |
| 182 | /* two bytes character */ |
| 183 | len = 2; |
| 184 | |
| 185 | aux = (*input)[1]; |
| 186 | if ((aux & 0xc0) != 0x80) { |
| 187 | return LY_EINVAL; |
| 188 | } |
| 189 | c = ((c & 0x1f) << 6) | (aux & 0x3f); |
| 190 | |
| 191 | if (c < 0x80) { |
| 192 | return LY_EINVAL; |
| 193 | } |
| 194 | } else if ((c & 0xf0) == 0xe0) { |
| 195 | /* three bytes character */ |
| 196 | len = 3; |
| 197 | |
| 198 | c &= 0x0f; |
| 199 | for (i = 1; i <= 2; i++) { |
| 200 | aux = (*input)[i]; |
| 201 | if ((aux & 0xc0) != 0x80) { |
| 202 | return LY_EINVAL; |
| 203 | } |
| 204 | |
| 205 | c = (c << 6) | (aux & 0x3f); |
| 206 | } |
| 207 | |
| 208 | if (c < 0x800 || (c > 0xd7ff && c < 0xe000) || c > 0xfffd) { |
| 209 | return LY_EINVAL; |
| 210 | } |
| 211 | } else if ((c & 0xf8) == 0xf0) { |
| 212 | /* four bytes character */ |
| 213 | len = 4; |
| 214 | |
| 215 | c &= 0x07; |
| 216 | for (i = 1; i <= 3; i++) { |
| 217 | aux = (*input)[i]; |
| 218 | if ((aux & 0xc0) != 0x80) { |
| 219 | return LY_EINVAL; |
| 220 | } |
| 221 | |
| 222 | c = (c << 6) | (aux & 0x3f); |
| 223 | } |
| 224 | |
| 225 | if (c < 0x1000 || c > 0x10ffff) { |
| 226 | return LY_EINVAL; |
| 227 | } |
| 228 | } else { |
| 229 | return LY_EINVAL; |
| 230 | } |
| 231 | |
| 232 | (*utf8_char) = c; |
| 233 | (*input) += len; |
| 234 | if (bytes_read) { |
| 235 | (*bytes_read) = len; |
| 236 | } |
| 237 | return LY_SUCCESS; |
| 238 | } |
| 239 | |
Radek Krejci | d972c25 | 2018-09-25 13:23:39 +0200 | [diff] [blame] | 240 | size_t |
| 241 | LY_VCODE_INSTREXP_len(const char *str) |
| 242 | { |
| 243 | size_t len = 0; |
| 244 | if (!str) { |
| 245 | return len; |
| 246 | } else if (!str[0]) { |
| 247 | return 1; |
| 248 | } |
| 249 | for (len = 1; len < LY_VCODE_INSTREXP_MAXLEN && str[len]; ++len); |
| 250 | return len; |
| 251 | } |
| 252 | |
Radek Krejci | f345c01 | 2018-09-19 11:12:59 +0200 | [diff] [blame] | 253 | LY_ERR |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 254 | 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] | 255 | { |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 256 | struct stat sb; |
| 257 | long pagesize; |
| 258 | size_t m; |
Michal Vasko | 841d1a9 | 2018-09-07 15:40:31 +0200 | [diff] [blame] | 259 | |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 260 | assert(length); |
| 261 | assert(addr); |
| 262 | assert(fd >= 0); |
Michal Vasko | 841d1a9 | 2018-09-07 15:40:31 +0200 | [diff] [blame] | 263 | |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 264 | if (fstat(fd, &sb) == -1) { |
| 265 | LOGERR(ctx, LY_ESYS, "Failed to stat the file descriptor (%s) for the mmap().", strerror(errno)); |
| 266 | return LY_ESYS; |
Michal Vasko | 841d1a9 | 2018-09-07 15:40:31 +0200 | [diff] [blame] | 267 | } |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 268 | if (!S_ISREG(sb.st_mode)) { |
| 269 | LOGERR(ctx, LY_EINVAL, "File to mmap() is not a regular file."); |
| 270 | return LY_ESYS; |
Michal Vasko | 841d1a9 | 2018-09-07 15:40:31 +0200 | [diff] [blame] | 271 | } |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 272 | if (!sb.st_size) { |
| 273 | *addr = NULL; |
| 274 | return LY_SUCCESS; |
| 275 | } |
| 276 | pagesize = sysconf(_SC_PAGESIZE); |
| 277 | |
| 278 | m = sb.st_size % pagesize; |
| 279 | if (m && pagesize - m >= 1) { |
| 280 | /* there will be enough space (at least 1 byte) after the file content mapping to provide zeroed NULL-termination byte */ |
| 281 | *length = sb.st_size + 1; |
| 282 | *addr = mmap(NULL, *length, PROT_READ, MAP_PRIVATE, fd, 0); |
| 283 | } else { |
| 284 | /* there will not be enough bytes after the file content mapping for the additional bytes and some of them |
| 285 | * would overflow into another page that would not be zerroed and any access into it would generate SIGBUS. |
| 286 | * Therefore we have to do the following hack with double mapping. First, the required number of bytes |
| 287 | * (including the additinal bytes) is required as anonymous and thus they will be really provided (actually more |
| 288 | * because of using whole pages) and also initialized by zeros. Then, the file is mapped to the same address |
| 289 | * where the anonymous mapping starts. */ |
| 290 | *length = sb.st_size + pagesize; |
| 291 | *addr = mmap(NULL, *length, PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
| 292 | *addr = mmap(*addr, sb.st_size, PROT_READ, MAP_PRIVATE | MAP_FIXED, fd, 0); |
| 293 | } |
| 294 | if (*addr == MAP_FAILED) { |
| 295 | LOGERR(ctx, LY_ESYS, "mmap() failed (%s).", strerror(errno)); |
| 296 | return LY_ESYS; |
Michal Vasko | 841d1a9 | 2018-09-07 15:40:31 +0200 | [diff] [blame] | 297 | } |
| 298 | |
Radek Krejci | f345c01 | 2018-09-19 11:12:59 +0200 | [diff] [blame] | 299 | return LY_SUCCESS; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 300 | } |
Michal Vasko | 841d1a9 | 2018-09-07 15:40:31 +0200 | [diff] [blame] | 301 | |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 302 | LY_ERR |
| 303 | ly_munmap(void *addr, size_t length) |
| 304 | { |
| 305 | if (munmap(addr, length)) { |
| 306 | return LY_ESYS; |
| 307 | } |
| 308 | return LY_SUCCESS; |
Michal Vasko | 841d1a9 | 2018-09-07 15:40:31 +0200 | [diff] [blame] | 309 | } |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 310 | |
| 311 | LY_ERR |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 312 | 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] | 313 | { |
| 314 | char *strptr; |
Radek Krejci | 9ea8ca1 | 2019-06-10 13:11:55 +0200 | [diff] [blame] | 315 | int64_t i; |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 316 | |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 317 | 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] | 318 | |
| 319 | /* convert to 64-bit integer, all the redundant characters are handled */ |
| 320 | errno = 0; |
| 321 | strptr = NULL; |
| 322 | |
| 323 | /* parse the value */ |
Radek Krejci | 9ea8ca1 | 2019-06-10 13:11:55 +0200 | [diff] [blame] | 324 | i = strtoll(val_str, &strptr, base); |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 325 | if (errno || strptr == val_str) { |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 326 | return LY_EVALID; |
Radek Krejci | 9ea8ca1 | 2019-06-10 13:11:55 +0200 | [diff] [blame] | 327 | } else if ((i < min) || (i > max)) { |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 328 | return LY_EDENIED; |
| 329 | } else if (strptr && *strptr) { |
| 330 | while (isspace(*strptr)) { |
| 331 | ++strptr; |
| 332 | } |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 333 | if (*strptr && strptr < val_str + val_len) { |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 334 | return LY_EVALID; |
| 335 | } |
| 336 | } |
Radek Krejci | 9ea8ca1 | 2019-06-10 13:11:55 +0200 | [diff] [blame] | 337 | |
| 338 | *ret = i; |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 339 | return LY_SUCCESS; |
| 340 | } |
| 341 | |
| 342 | LY_ERR |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 343 | 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] | 344 | { |
| 345 | char *strptr; |
| 346 | uint64_t u; |
| 347 | |
| 348 | LY_CHECK_ARG_RET(NULL, val_str, val_str[0], LY_EINVAL); |
| 349 | |
| 350 | errno = 0; |
| 351 | strptr = NULL; |
| 352 | u = strtoull(val_str, &strptr, base); |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 353 | if (errno || strptr == val_str) { |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 354 | return LY_EVALID; |
| 355 | } else if ((u > max) || (u && val_str[0] == '-')) { |
| 356 | return LY_EDENIED; |
| 357 | } else if (strptr && *strptr) { |
| 358 | while (isspace(*strptr)) { |
| 359 | ++strptr; |
| 360 | } |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 361 | if (*strptr && strptr < val_str + val_len) { |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 362 | return LY_EVALID; |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | *ret = u; |
| 367 | return LY_SUCCESS; |
| 368 | } |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 369 | |
| 370 | /** |
| 371 | * @brief Parse an identifier. |
| 372 | * |
| 373 | * ;; An identifier MUST NOT start with (('X'|'x') ('M'|'m') ('L'|'l')) |
| 374 | * identifier = (ALPHA / "_") |
| 375 | * *(ALPHA / DIGIT / "_" / "-" / ".") |
| 376 | * |
| 377 | * @param[in,out] id Identifier to parse. When returned, it points to the first character which is not part of the identifier. |
| 378 | * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid starting character. |
| 379 | */ |
| 380 | static LY_ERR |
| 381 | lys_parse_id(const char **id) |
| 382 | { |
| 383 | assert(id && *id); |
| 384 | |
| 385 | if (!is_yangidentstartchar(**id)) { |
| 386 | return LY_EINVAL; |
| 387 | } |
| 388 | ++(*id); |
| 389 | |
| 390 | while (is_yangidentchar(**id)) { |
| 391 | ++(*id); |
| 392 | } |
| 393 | return LY_SUCCESS; |
| 394 | } |
| 395 | |
| 396 | LY_ERR |
| 397 | ly_parse_nodeid(const char **id, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len) |
| 398 | { |
| 399 | assert(id && *id); |
| 400 | assert(prefix && prefix_len); |
| 401 | assert(name && name_len); |
| 402 | |
| 403 | *prefix = *id; |
| 404 | *prefix_len = 0; |
| 405 | *name = NULL; |
| 406 | *name_len = 0; |
| 407 | |
| 408 | LY_CHECK_RET(lys_parse_id(id)); |
| 409 | if (**id == ':') { |
| 410 | /* there is prefix */ |
| 411 | *prefix_len = *id - *prefix; |
| 412 | ++(*id); |
| 413 | *name = *id; |
| 414 | |
| 415 | LY_CHECK_RET(lys_parse_id(id)); |
| 416 | *name_len = *id - *name; |
| 417 | } else { |
| 418 | /* there is no prefix, so what we have as prefix now is actually the name */ |
| 419 | *name = *prefix; |
| 420 | *name_len = *id - *name; |
| 421 | *prefix = NULL; |
| 422 | } |
| 423 | |
| 424 | return LY_SUCCESS; |
| 425 | } |
| 426 | |
| 427 | LY_ERR |
| 428 | ly_parse_instance_predicate(const char **pred, size_t limit, |
| 429 | const char **prefix, size_t *prefix_len, const char **id, size_t *id_len, const char **value, size_t *value_len, |
| 430 | const char **errmsg) |
| 431 | { |
| 432 | LY_ERR ret = LY_EVALID; |
| 433 | const char *in = *pred; |
| 434 | size_t offset = 1; |
| 435 | int expr = 0; |
| 436 | char quot; |
| 437 | |
| 438 | assert(in[0] == '\['); |
| 439 | |
| 440 | *prefix = *id = *value = NULL; |
| 441 | *prefix_len = *id_len = *value_len = 0; |
| 442 | |
| 443 | /* leading *WSP */ |
| 444 | for (; isspace(in[offset]); offset++); |
| 445 | |
| 446 | if (isdigit(in[offset])) { |
| 447 | /* pos: "[" *WSP positive-integer-value *WSP "]" */ |
| 448 | if (in[offset] == '0') { |
| 449 | /* zero */ |
| 450 | *errmsg = "The position predicate cannot be zero."; |
| 451 | goto error; |
| 452 | } |
| 453 | |
| 454 | /* positive-integer-value */ |
Radek Krejci | 10bfdf8 | 2019-06-10 14:08:13 +0200 | [diff] [blame^] | 455 | *value = &in[offset++]; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 456 | for (; isdigit(in[offset]); offset++); |
Radek Krejci | 10bfdf8 | 2019-06-10 14:08:13 +0200 | [diff] [blame^] | 457 | *value_len = &in[offset] - *value; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 458 | |
| 459 | } else if (in[offset] == '.') { |
| 460 | /* leaf-list-predicate: "[" *WSP "." *WSP "=" *WSP quoted-string *WSP "]" */ |
| 461 | *id = &in[offset]; |
| 462 | *id_len = 1; |
| 463 | offset++; |
| 464 | expr = 1; |
Radek Krejci | 10bfdf8 | 2019-06-10 14:08:13 +0200 | [diff] [blame^] | 465 | } else if (in[offset] == '-') { |
| 466 | /* typically negative value */ |
| 467 | *errmsg = "Invalid instance predicate format (negative position or invalid node-identifier)."; |
| 468 | goto error; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 469 | } else { |
| 470 | /* key-predicate: "[" *WSP node-identifier *WSP "=" *WSP quoted-string *WSP "]" */ |
| 471 | in = &in[offset]; |
| 472 | if (ly_parse_nodeid(&in, prefix, prefix_len, id, id_len)) { |
| 473 | *errmsg = "Invalid node-identifier."; |
| 474 | goto error; |
| 475 | } |
| 476 | offset = in - *pred; |
| 477 | in = *pred; |
Radek Krejci | 10bfdf8 | 2019-06-10 14:08:13 +0200 | [diff] [blame^] | 478 | expr = 2; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | if (expr) { |
| 482 | /* *WSP "=" *WSP quoted-string *WSP "]" */ |
| 483 | for (; isspace(in[offset]); offset++); |
| 484 | |
| 485 | if (in[offset] != '=') { |
Radek Krejci | 10bfdf8 | 2019-06-10 14:08:13 +0200 | [diff] [blame^] | 486 | if (expr == 1) { |
| 487 | *errmsg = "Unexpected character instead of \'=\' in leaf-list-predicate."; |
| 488 | } else { /* 2 */ |
| 489 | *errmsg = "Unexpected character instead of \'=\' in key-predicate."; |
| 490 | } |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 491 | goto error; |
| 492 | } |
| 493 | offset++; |
| 494 | for (; isspace(in[offset]); offset++); |
| 495 | |
| 496 | /* quoted-string */ |
| 497 | quot = in[offset++]; |
| 498 | if (quot != '\'' && quot != '\"') { |
| 499 | *errmsg = "String value is not quoted."; |
| 500 | goto error; |
| 501 | } |
| 502 | *value = &in[offset]; |
| 503 | for (;offset < limit && in[offset] != quot; offset++); |
Radek Krejci | 10bfdf8 | 2019-06-10 14:08:13 +0200 | [diff] [blame^] | 504 | if (in[offset] == quot) { |
| 505 | *value_len = &in[offset] - *value; |
| 506 | offset++; |
| 507 | } else { |
| 508 | *errmsg = "Value is not terminated quoted-string."; |
| 509 | goto error; |
| 510 | } |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | /* *WSP "]" */ |
| 514 | for(; isspace(in[offset]); offset++); |
| 515 | if (in[offset] != ']') { |
Radek Krejci | 10bfdf8 | 2019-06-10 14:08:13 +0200 | [diff] [blame^] | 516 | if (expr == 0) { |
| 517 | *errmsg = "Predicate (pos) is not terminated by \']\' character."; |
| 518 | } else if (expr == 1) { |
| 519 | *errmsg = "Predicate (leaf-list-predicate) is not terminated by \']\' character."; |
| 520 | } else { /* 2 */ |
| 521 | *errmsg = "Predicate (key-predicate) is not terminated by \']\' character."; |
| 522 | } |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 523 | goto error; |
| 524 | } |
Radek Krejci | 10bfdf8 | 2019-06-10 14:08:13 +0200 | [diff] [blame^] | 525 | offset++; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 526 | |
Radek Krejci | 10bfdf8 | 2019-06-10 14:08:13 +0200 | [diff] [blame^] | 527 | if (offset <= limit) { |
| 528 | *pred = &in[offset]; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 529 | return LY_SUCCESS; |
| 530 | } |
| 531 | |
| 532 | /* we read after the limit */ |
| 533 | *errmsg = "Predicate is incomplete."; |
| 534 | *prefix = *id = *value = NULL; |
| 535 | *prefix_len = *id_len = *value_len = 0; |
| 536 | offset = limit; |
| 537 | ret = LY_EINVAL; |
| 538 | |
| 539 | error: |
| 540 | *pred = &in[offset]; |
| 541 | return ret; |
| 542 | } |