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> |
Radek Krejci | cad716e | 2018-11-26 15:18:27 +0100 | [diff] [blame] | 19 | #include <limits.h> |
Michal Vasko | 1324b6c | 2018-09-07 11:16:23 +0200 | [diff] [blame] | 20 | #include <stdlib.h> |
Michal Vasko | 841d1a9 | 2018-09-07 15:40:31 +0200 | [diff] [blame] | 21 | #include <stdio.h> |
| 22 | #include <ctype.h> |
| 23 | #include <string.h> |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 24 | #include <sys/mman.h> |
| 25 | #include <sys/stat.h> |
| 26 | #include <sys/types.h> |
| 27 | #include <unistd.h> |
Michal Vasko | 841d1a9 | 2018-09-07 15:40:31 +0200 | [diff] [blame] | 28 | |
| 29 | #include "tree_schema.h" |
Michal Vasko | 1324b6c | 2018-09-07 11:16:23 +0200 | [diff] [blame] | 30 | |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 31 | const char *const ly_stmt_list[] = { |
| 32 | [YANG_ACTION] = "action", |
| 33 | [YANG_ANYDATA] = "anydata", |
| 34 | [YANG_ANYXML] = "anyxml", |
| 35 | [YANG_ARGUMENT] = "argument", |
| 36 | [YANG_AUGMENT] = "augment", |
| 37 | [YANG_BASE] = "base", |
| 38 | [YANG_BELONGS_TO] = "belongs-to", |
| 39 | [YANG_BIT] = "bit", |
| 40 | [YANG_CASE] = "case", |
| 41 | [YANG_CHOICE] = "choice", |
| 42 | [YANG_CONFIG] = "config", |
| 43 | [YANG_CONTACT] = "contact", |
| 44 | [YANG_CONTAINER] = "container", |
| 45 | [YANG_CUSTOM] = "<extension-instance>", |
| 46 | [YANG_DEFAULT] = "default", |
| 47 | [YANG_DESCRIPTION] = "description", |
| 48 | [YANG_DEVIATE] = "deviate", |
| 49 | [YANG_DEVIATION] = "deviation", |
| 50 | [YANG_ENUM] = "enum", |
| 51 | [YANG_ERROR_APP_TAG] = "error-app-tag", |
| 52 | [YANG_ERROR_MESSAGE] = "error-message", |
| 53 | [YANG_EXTENSION] = "extension", |
| 54 | [YANG_FEATURE] = "feature", |
| 55 | [YANG_FRACTION_DIGITS] = "fraction-digits", |
| 56 | [YANG_GROUPING] = "grouping", |
| 57 | [YANG_IDENTITY] = "identitiy", |
| 58 | [YANG_IF_FEATURE] = "if-feature", |
| 59 | [YANG_IMPORT] = "import", |
| 60 | [YANG_INCLUDE] = "include", |
| 61 | [YANG_INPUT] = "input", |
| 62 | [YANG_KEY] = "key", |
| 63 | [YANG_LEAF] = "leaf", |
| 64 | [YANG_LEAF_LIST] = "leaf-list", |
| 65 | [YANG_LENGTH] = "length", |
| 66 | [YANG_LIST] = "list", |
| 67 | [YANG_MANDATORY] = "mandatory", |
| 68 | [YANG_MAX_ELEMENTS] = "max-elements", |
| 69 | [YANG_MIN_ELEMENTS] = "min-elements", |
| 70 | [YANG_MODIFIER] = "modifier", |
| 71 | [YANG_MODULE] = "module", |
| 72 | [YANG_MUST] = "must", |
| 73 | [YANG_NAMESPACE] = "namespace", |
| 74 | [YANG_NOTIFICATION] = "notification", |
| 75 | [YANG_ORDERED_BY] = "ordered-by", |
| 76 | [YANG_ORGANIZATION] = "organization", |
| 77 | [YANG_OUTPUT] = "output", |
| 78 | [YANG_PATH] = "path", |
| 79 | [YANG_PATTERN] = "pattern", |
| 80 | [YANG_POSITION] = "position", |
| 81 | [YANG_PREFIX] = "prefix", |
| 82 | [YANG_PRESENCE] = "presence", |
| 83 | [YANG_RANGE] = "range", |
| 84 | [YANG_REFERENCE] = "reference", |
| 85 | [YANG_REFINE] = "refine", |
| 86 | [YANG_REQUIRE_INSTANCE] = "require-instance", |
| 87 | [YANG_REVISION] = "revision", |
| 88 | [YANG_REVISION_DATE] = "revision-date", |
| 89 | [YANG_RPC] = "rpc", |
| 90 | [YANG_STATUS] = "status", |
| 91 | [YANG_SUBMODULE] = "submodule", |
| 92 | [YANG_TYPE] = "type", |
| 93 | [YANG_TYPEDEF] = "typedef", |
| 94 | [YANG_UNIQUE] = "unique", |
| 95 | [YANG_UNITS] = "units", |
| 96 | [YANG_USES] = "uses", |
| 97 | [YANG_VALUE] = "value", |
| 98 | [YANG_WHEN] = "when", |
| 99 | [YANG_YANG_VERSION] = "yang-version", |
| 100 | [YANG_YIN_ELEMENT] = "yin-element", |
| 101 | [YANG_SEMICOLON] = ";", |
| 102 | [YANG_LEFT_BRACE] = "{", |
| 103 | [YANG_RIGHT_BRACE] = "}", |
| 104 | }; |
| 105 | |
| 106 | const char *const lyext_substmt_list[] = { |
| 107 | [LYEXT_SUBSTMT_ARGUMENT] = "argument", |
| 108 | [LYEXT_SUBSTMT_BASE] = "base", |
| 109 | [LYEXT_SUBSTMT_BELONGSTO] = "belongs-to", |
| 110 | [LYEXT_SUBSTMT_CONTACT] = "contact", |
| 111 | [LYEXT_SUBSTMT_DEFAULT] = "default", |
| 112 | [LYEXT_SUBSTMT_DESCRIPTION] = "description", |
| 113 | [LYEXT_SUBSTMT_ERRTAG] = "error-app-tag", |
| 114 | [LYEXT_SUBSTMT_ERRMSG] = "error-message", |
| 115 | [LYEXT_SUBSTMT_KEY] = "key", |
| 116 | [LYEXT_SUBSTMT_NAMESPACE] = "namespace", |
| 117 | [LYEXT_SUBSTMT_ORGANIZATION] = "organization", |
| 118 | [LYEXT_SUBSTMT_PATH] = "path", |
| 119 | [LYEXT_SUBSTMT_PREFIX] = "prefix", |
| 120 | [LYEXT_SUBSTMT_PRESENCE] = "presence", |
| 121 | [LYEXT_SUBSTMT_REFERENCE] = "reference", |
| 122 | [LYEXT_SUBSTMT_REVISIONDATE] = "revision-date", |
| 123 | [LYEXT_SUBSTMT_UNITS] = "units", |
| 124 | [LYEXT_SUBSTMT_VALUE] = "value", |
| 125 | [LYEXT_SUBSTMT_VERSION] = "yang-version", |
| 126 | [LYEXT_SUBSTMT_MODIFIER] = "modifier", |
| 127 | [LYEXT_SUBSTMT_REQINSTANCE] = "require-instance", |
| 128 | [LYEXT_SUBSTMT_YINELEM] = "yin-element", |
| 129 | [LYEXT_SUBSTMT_CONFIG] = "config", |
| 130 | [LYEXT_SUBSTMT_MANDATORY] = "mandatory", |
| 131 | [LYEXT_SUBSTMT_ORDEREDBY] = "ordered-by", |
| 132 | [LYEXT_SUBSTMT_STATUS] = "status", |
| 133 | [LYEXT_SUBSTMT_FRACDIGITS] = "fraction-digits", |
| 134 | [LYEXT_SUBSTMT_MAX] = "max-elements", |
| 135 | [LYEXT_SUBSTMT_MIN] = "min-elements", |
| 136 | [LYEXT_SUBSTMT_POSITION] = "position", |
| 137 | [LYEXT_SUBSTMT_UNIQUE] = "unique", |
| 138 | [LYEXT_SUBSTMT_IFFEATURE] = "if-feature", |
| 139 | }; |
| 140 | |
| 141 | const char *const ly_devmod_list[] = { |
| 142 | [LYS_DEV_NOT_SUPPORTED] = "not-supported", |
| 143 | [LYS_DEV_ADD] = "add", |
| 144 | [LYS_DEV_DELETE] = "delete", |
| 145 | [LYS_DEV_REPLACE] = "replace", |
| 146 | }; |
| 147 | |
Radek Krejci | cad716e | 2018-11-26 15:18:27 +0100 | [diff] [blame] | 148 | #ifndef __USE_GNU |
| 149 | char * |
| 150 | get_current_dir_name(void) |
| 151 | { |
| 152 | char tmp[PATH_MAX]; |
| 153 | char *retval; |
| 154 | |
| 155 | if (getcwd(tmp, sizeof(tmp))) { |
| 156 | retval = strdup(tmp); |
Radek Krejci | 60794f5 | 2018-11-27 09:06:44 +0100 | [diff] [blame] | 157 | LY_CHECK_ERR_RET(!retval, LOGMEM(NULL), NULL); |
Radek Krejci | cad716e | 2018-11-26 15:18:27 +0100 | [diff] [blame] | 158 | return retval; |
| 159 | } |
| 160 | return NULL; |
| 161 | } |
| 162 | #endif |
| 163 | |
Michal Vasko | 1324b6c | 2018-09-07 11:16:23 +0200 | [diff] [blame] | 164 | void * |
| 165 | ly_realloc(void *ptr, size_t size) |
| 166 | { |
| 167 | void *new_mem; |
| 168 | |
| 169 | new_mem = realloc(ptr, size); |
| 170 | if (!new_mem) { |
| 171 | free(ptr); |
| 172 | } |
| 173 | |
| 174 | return new_mem; |
| 175 | } |
Michal Vasko | 841d1a9 | 2018-09-07 15:40:31 +0200 | [diff] [blame] | 176 | |
Radek Krejci | b416be6 | 2018-10-01 14:51:45 +0200 | [diff] [blame] | 177 | LY_ERR |
| 178 | ly_getutf8(const char **input, unsigned int *utf8_char, size_t *bytes_read) |
| 179 | { |
| 180 | unsigned int c, len; |
| 181 | int aux; |
| 182 | int i; |
| 183 | |
Radek Krejci | cc6a45c | 2019-05-13 10:16:14 +0200 | [diff] [blame^] | 184 | if (bytes_read) { |
| 185 | (*bytes_read) = 0; |
| 186 | } |
| 187 | |
Radek Krejci | b416be6 | 2018-10-01 14:51:45 +0200 | [diff] [blame] | 188 | c = (*input)[0]; |
| 189 | LY_CHECK_RET(!c, LY_EINVAL); |
| 190 | |
| 191 | if (!(c & 0x80)) { |
| 192 | /* one byte character */ |
| 193 | len = 1; |
| 194 | |
| 195 | if (c < 0x20 && c != 0x9 && c != 0xa && c != 0xd) { |
| 196 | return LY_EINVAL; |
| 197 | } |
| 198 | } else if ((c & 0xe0) == 0xc0) { |
| 199 | /* two bytes character */ |
| 200 | len = 2; |
| 201 | |
| 202 | aux = (*input)[1]; |
| 203 | if ((aux & 0xc0) != 0x80) { |
| 204 | return LY_EINVAL; |
| 205 | } |
| 206 | c = ((c & 0x1f) << 6) | (aux & 0x3f); |
| 207 | |
| 208 | if (c < 0x80) { |
| 209 | return LY_EINVAL; |
| 210 | } |
| 211 | } else if ((c & 0xf0) == 0xe0) { |
| 212 | /* three bytes character */ |
| 213 | len = 3; |
| 214 | |
| 215 | c &= 0x0f; |
| 216 | for (i = 1; i <= 2; 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 < 0x800 || (c > 0xd7ff && c < 0xe000) || c > 0xfffd) { |
| 226 | return LY_EINVAL; |
| 227 | } |
| 228 | } else if ((c & 0xf8) == 0xf0) { |
| 229 | /* four bytes character */ |
| 230 | len = 4; |
| 231 | |
| 232 | c &= 0x07; |
| 233 | for (i = 1; i <= 3; i++) { |
| 234 | aux = (*input)[i]; |
| 235 | if ((aux & 0xc0) != 0x80) { |
| 236 | return LY_EINVAL; |
| 237 | } |
| 238 | |
| 239 | c = (c << 6) | (aux & 0x3f); |
| 240 | } |
| 241 | |
| 242 | if (c < 0x1000 || c > 0x10ffff) { |
| 243 | return LY_EINVAL; |
| 244 | } |
| 245 | } else { |
| 246 | return LY_EINVAL; |
| 247 | } |
| 248 | |
| 249 | (*utf8_char) = c; |
| 250 | (*input) += len; |
| 251 | if (bytes_read) { |
| 252 | (*bytes_read) = len; |
| 253 | } |
| 254 | return LY_SUCCESS; |
| 255 | } |
| 256 | |
Radek Krejci | d972c25 | 2018-09-25 13:23:39 +0200 | [diff] [blame] | 257 | size_t |
| 258 | LY_VCODE_INSTREXP_len(const char *str) |
| 259 | { |
| 260 | size_t len = 0; |
| 261 | if (!str) { |
| 262 | return len; |
| 263 | } else if (!str[0]) { |
| 264 | return 1; |
| 265 | } |
| 266 | for (len = 1; len < LY_VCODE_INSTREXP_MAXLEN && str[len]; ++len); |
| 267 | return len; |
| 268 | } |
| 269 | |
Radek Krejci | f345c01 | 2018-09-19 11:12:59 +0200 | [diff] [blame] | 270 | LY_ERR |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 271 | 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] | 272 | { |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 273 | struct stat sb; |
| 274 | long pagesize; |
| 275 | size_t m; |
Michal Vasko | 841d1a9 | 2018-09-07 15:40:31 +0200 | [diff] [blame] | 276 | |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 277 | assert(length); |
| 278 | assert(addr); |
| 279 | assert(fd >= 0); |
Michal Vasko | 841d1a9 | 2018-09-07 15:40:31 +0200 | [diff] [blame] | 280 | |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 281 | if (fstat(fd, &sb) == -1) { |
| 282 | LOGERR(ctx, LY_ESYS, "Failed to stat the file descriptor (%s) for the mmap().", strerror(errno)); |
| 283 | return LY_ESYS; |
Michal Vasko | 841d1a9 | 2018-09-07 15:40:31 +0200 | [diff] [blame] | 284 | } |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 285 | if (!S_ISREG(sb.st_mode)) { |
| 286 | LOGERR(ctx, LY_EINVAL, "File to mmap() is not a regular file."); |
| 287 | return LY_ESYS; |
Michal Vasko | 841d1a9 | 2018-09-07 15:40:31 +0200 | [diff] [blame] | 288 | } |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 289 | if (!sb.st_size) { |
| 290 | *addr = NULL; |
| 291 | return LY_SUCCESS; |
| 292 | } |
| 293 | pagesize = sysconf(_SC_PAGESIZE); |
| 294 | |
| 295 | m = sb.st_size % pagesize; |
| 296 | if (m && pagesize - m >= 1) { |
| 297 | /* there will be enough space (at least 1 byte) after the file content mapping to provide zeroed NULL-termination byte */ |
| 298 | *length = sb.st_size + 1; |
| 299 | *addr = mmap(NULL, *length, PROT_READ, MAP_PRIVATE, fd, 0); |
| 300 | } else { |
| 301 | /* there will not be enough bytes after the file content mapping for the additional bytes and some of them |
| 302 | * would overflow into another page that would not be zerroed and any access into it would generate SIGBUS. |
| 303 | * Therefore we have to do the following hack with double mapping. First, the required number of bytes |
| 304 | * (including the additinal bytes) is required as anonymous and thus they will be really provided (actually more |
| 305 | * because of using whole pages) and also initialized by zeros. Then, the file is mapped to the same address |
| 306 | * where the anonymous mapping starts. */ |
| 307 | *length = sb.st_size + pagesize; |
| 308 | *addr = mmap(NULL, *length, PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
| 309 | *addr = mmap(*addr, sb.st_size, PROT_READ, MAP_PRIVATE | MAP_FIXED, fd, 0); |
| 310 | } |
| 311 | if (*addr == MAP_FAILED) { |
| 312 | LOGERR(ctx, LY_ESYS, "mmap() failed (%s).", strerror(errno)); |
| 313 | return LY_ESYS; |
Michal Vasko | 841d1a9 | 2018-09-07 15:40:31 +0200 | [diff] [blame] | 314 | } |
| 315 | |
Radek Krejci | f345c01 | 2018-09-19 11:12:59 +0200 | [diff] [blame] | 316 | return LY_SUCCESS; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 317 | } |
Michal Vasko | 841d1a9 | 2018-09-07 15:40:31 +0200 | [diff] [blame] | 318 | |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 319 | LY_ERR |
| 320 | ly_munmap(void *addr, size_t length) |
| 321 | { |
| 322 | if (munmap(addr, length)) { |
| 323 | return LY_ESYS; |
| 324 | } |
| 325 | return LY_SUCCESS; |
Michal Vasko | 841d1a9 | 2018-09-07 15:40:31 +0200 | [diff] [blame] | 326 | } |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 327 | |
| 328 | LY_ERR |
| 329 | ly_parse_int(const char *val_str, int64_t min, int64_t max, int base, int64_t *ret) |
| 330 | { |
| 331 | char *strptr; |
| 332 | |
| 333 | LY_CHECK_ARG_RET(NULL, val_str, val_str[0], LY_EINVAL); |
| 334 | |
| 335 | /* convert to 64-bit integer, all the redundant characters are handled */ |
| 336 | errno = 0; |
| 337 | strptr = NULL; |
| 338 | |
| 339 | /* parse the value */ |
| 340 | *ret = strtoll(val_str, &strptr, base); |
| 341 | if (errno) { |
| 342 | return LY_EVALID; |
| 343 | } else if ((*ret < min) || (*ret > max)) { |
| 344 | return LY_EDENIED; |
| 345 | } else if (strptr && *strptr) { |
| 346 | while (isspace(*strptr)) { |
| 347 | ++strptr; |
| 348 | } |
| 349 | if (*strptr) { |
| 350 | return LY_EVALID; |
| 351 | } |
| 352 | } |
| 353 | return LY_SUCCESS; |
| 354 | } |
| 355 | |
| 356 | LY_ERR |
| 357 | ly_parse_uint(const char *val_str, uint64_t max, int base, uint64_t *ret) |
| 358 | { |
| 359 | char *strptr; |
| 360 | uint64_t u; |
| 361 | |
| 362 | LY_CHECK_ARG_RET(NULL, val_str, val_str[0], LY_EINVAL); |
| 363 | |
| 364 | errno = 0; |
| 365 | strptr = NULL; |
| 366 | u = strtoull(val_str, &strptr, base); |
| 367 | if (errno) { |
| 368 | return LY_EVALID; |
| 369 | } else if ((u > max) || (u && val_str[0] == '-')) { |
| 370 | return LY_EDENIED; |
| 371 | } else if (strptr && *strptr) { |
| 372 | while (isspace(*strptr)) { |
| 373 | ++strptr; |
| 374 | } |
| 375 | if (*strptr) { |
| 376 | return LY_EVALID; |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | *ret = u; |
| 381 | return LY_SUCCESS; |
| 382 | } |