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 <stdio.h> |
| 21 | #include <ctype.h> |
| 22 | #include <string.h> |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 23 | #include <sys/mman.h> |
| 24 | #include <sys/stat.h> |
| 25 | #include <sys/types.h> |
| 26 | #include <unistd.h> |
Michal Vasko | 841d1a9 | 2018-09-07 15:40:31 +0200 | [diff] [blame] | 27 | |
| 28 | #include "tree_schema.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 | |
| 167 | c = (*input)[0]; |
| 168 | LY_CHECK_RET(!c, LY_EINVAL); |
| 169 | |
| 170 | if (!(c & 0x80)) { |
| 171 | /* one byte character */ |
| 172 | len = 1; |
| 173 | |
| 174 | if (c < 0x20 && c != 0x9 && c != 0xa && c != 0xd) { |
| 175 | return LY_EINVAL; |
| 176 | } |
| 177 | } else if ((c & 0xe0) == 0xc0) { |
| 178 | /* two bytes character */ |
| 179 | len = 2; |
| 180 | |
| 181 | aux = (*input)[1]; |
| 182 | if ((aux & 0xc0) != 0x80) { |
| 183 | return LY_EINVAL; |
| 184 | } |
| 185 | c = ((c & 0x1f) << 6) | (aux & 0x3f); |
| 186 | |
| 187 | if (c < 0x80) { |
| 188 | return LY_EINVAL; |
| 189 | } |
| 190 | } else if ((c & 0xf0) == 0xe0) { |
| 191 | /* three bytes character */ |
| 192 | len = 3; |
| 193 | |
| 194 | c &= 0x0f; |
| 195 | for (i = 1; i <= 2; i++) { |
| 196 | aux = (*input)[i]; |
| 197 | if ((aux & 0xc0) != 0x80) { |
| 198 | return LY_EINVAL; |
| 199 | } |
| 200 | |
| 201 | c = (c << 6) | (aux & 0x3f); |
| 202 | } |
| 203 | |
| 204 | if (c < 0x800 || (c > 0xd7ff && c < 0xe000) || c > 0xfffd) { |
| 205 | return LY_EINVAL; |
| 206 | } |
| 207 | } else if ((c & 0xf8) == 0xf0) { |
| 208 | /* four bytes character */ |
| 209 | len = 4; |
| 210 | |
| 211 | c &= 0x07; |
| 212 | for (i = 1; i <= 3; i++) { |
| 213 | aux = (*input)[i]; |
| 214 | if ((aux & 0xc0) != 0x80) { |
| 215 | return LY_EINVAL; |
| 216 | } |
| 217 | |
| 218 | c = (c << 6) | (aux & 0x3f); |
| 219 | } |
| 220 | |
| 221 | if (c < 0x1000 || c > 0x10ffff) { |
| 222 | return LY_EINVAL; |
| 223 | } |
| 224 | } else { |
| 225 | return LY_EINVAL; |
| 226 | } |
| 227 | |
| 228 | (*utf8_char) = c; |
| 229 | (*input) += len; |
| 230 | if (bytes_read) { |
| 231 | (*bytes_read) = len; |
| 232 | } |
| 233 | return LY_SUCCESS; |
| 234 | } |
| 235 | |
Radek Krejci | d972c25 | 2018-09-25 13:23:39 +0200 | [diff] [blame] | 236 | size_t |
| 237 | LY_VCODE_INSTREXP_len(const char *str) |
| 238 | { |
| 239 | size_t len = 0; |
| 240 | if (!str) { |
| 241 | return len; |
| 242 | } else if (!str[0]) { |
| 243 | return 1; |
| 244 | } |
| 245 | for (len = 1; len < LY_VCODE_INSTREXP_MAXLEN && str[len]; ++len); |
| 246 | return len; |
| 247 | } |
| 248 | |
Radek Krejci | f345c01 | 2018-09-19 11:12:59 +0200 | [diff] [blame] | 249 | LY_ERR |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 250 | 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] | 251 | { |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 252 | struct stat sb; |
| 253 | long pagesize; |
| 254 | size_t m; |
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 | assert(length); |
| 257 | assert(addr); |
| 258 | assert(fd >= 0); |
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 | if (fstat(fd, &sb) == -1) { |
| 261 | LOGERR(ctx, LY_ESYS, "Failed to stat the file descriptor (%s) for the mmap().", strerror(errno)); |
| 262 | return LY_ESYS; |
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 (!S_ISREG(sb.st_mode)) { |
| 265 | LOGERR(ctx, LY_EINVAL, "File to mmap() is not a regular file."); |
| 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 (!sb.st_size) { |
| 269 | *addr = NULL; |
| 270 | return LY_SUCCESS; |
| 271 | } |
| 272 | pagesize = sysconf(_SC_PAGESIZE); |
| 273 | |
| 274 | m = sb.st_size % pagesize; |
| 275 | if (m && pagesize - m >= 1) { |
| 276 | /* there will be enough space (at least 1 byte) after the file content mapping to provide zeroed NULL-termination byte */ |
| 277 | *length = sb.st_size + 1; |
| 278 | *addr = mmap(NULL, *length, PROT_READ, MAP_PRIVATE, fd, 0); |
| 279 | } else { |
| 280 | /* there will not be enough bytes after the file content mapping for the additional bytes and some of them |
| 281 | * would overflow into another page that would not be zerroed and any access into it would generate SIGBUS. |
| 282 | * Therefore we have to do the following hack with double mapping. First, the required number of bytes |
| 283 | * (including the additinal bytes) is required as anonymous and thus they will be really provided (actually more |
| 284 | * because of using whole pages) and also initialized by zeros. Then, the file is mapped to the same address |
| 285 | * where the anonymous mapping starts. */ |
| 286 | *length = sb.st_size + pagesize; |
| 287 | *addr = mmap(NULL, *length, PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
| 288 | *addr = mmap(*addr, sb.st_size, PROT_READ, MAP_PRIVATE | MAP_FIXED, fd, 0); |
| 289 | } |
| 290 | if (*addr == MAP_FAILED) { |
| 291 | LOGERR(ctx, LY_ESYS, "mmap() failed (%s).", strerror(errno)); |
| 292 | return LY_ESYS; |
Michal Vasko | 841d1a9 | 2018-09-07 15:40:31 +0200 | [diff] [blame] | 293 | } |
| 294 | |
Radek Krejci | f345c01 | 2018-09-19 11:12:59 +0200 | [diff] [blame] | 295 | return LY_SUCCESS; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 296 | } |
Michal Vasko | 841d1a9 | 2018-09-07 15:40:31 +0200 | [diff] [blame] | 297 | |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 298 | LY_ERR |
| 299 | ly_munmap(void *addr, size_t length) |
| 300 | { |
| 301 | if (munmap(addr, length)) { |
| 302 | return LY_ESYS; |
| 303 | } |
| 304 | return LY_SUCCESS; |
Michal Vasko | 841d1a9 | 2018-09-07 15:40:31 +0200 | [diff] [blame] | 305 | } |