blob: f300958c4224196f60dc761a6c2268224a041e16 [file] [log] [blame]
Michal Vasko1324b6c2018-09-07 11:16:23 +02001/**
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 Krejcib7db73a2018-10-24 14:18:40 +020014
15#include "common.h"
Michal Vasko1324b6c2018-09-07 11:16:23 +020016
Radek Krejci86d106e2018-10-18 09:53:19 +020017#include <assert.h>
18#include <errno.h>
Michal Vasko1324b6c2018-09-07 11:16:23 +020019#include <stdlib.h>
Michal Vasko841d1a92018-09-07 15:40:31 +020020#include <ctype.h>
21#include <string.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020022#include <sys/mman.h>
23#include <sys/stat.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020024#include <unistd.h>
Michal Vasko841d1a92018-09-07 15:40:31 +020025
Radek Krejcie7b95092019-05-15 11:03:07 +020026#include "extensions.h"
Michal Vasko841d1a92018-09-07 15:40:31 +020027#include "tree_schema.h"
Radek Krejcib4a4a272019-06-10 12:44:52 +020028#include "tree_schema_internal.h"
Michal Vasko1324b6c2018-09-07 11:16:23 +020029
Radek Krejcic59bc972018-09-17 16:13:06 +020030const 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
105const 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
140const 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 Vasko1324b6c2018-09-07 11:16:23 +0200147void *
148ly_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 Vasko841d1a92018-09-07 15:40:31 +0200159
Radek Krejcib416be62018-10-01 14:51:45 +0200160LY_ERR
161ly_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 Krejcicc6a45c2019-05-13 10:16:14 +0200167 if (bytes_read) {
168 (*bytes_read) = 0;
169 }
170
Radek Krejcib416be62018-10-01 14:51:45 +0200171 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 Krejcid972c252018-09-25 13:23:39 +0200240size_t
241LY_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 Krejcif345c012018-09-19 11:12:59 +0200253LY_ERR
Radek Krejci86d106e2018-10-18 09:53:19 +0200254ly_mmap(struct ly_ctx *ctx, int fd, size_t *length, void **addr)
Michal Vasko841d1a92018-09-07 15:40:31 +0200255{
Radek Krejci86d106e2018-10-18 09:53:19 +0200256 struct stat sb;
257 long pagesize;
258 size_t m;
Michal Vasko841d1a92018-09-07 15:40:31 +0200259
Radek Krejci86d106e2018-10-18 09:53:19 +0200260 assert(length);
261 assert(addr);
262 assert(fd >= 0);
Michal Vasko841d1a92018-09-07 15:40:31 +0200263
Radek Krejci86d106e2018-10-18 09:53:19 +0200264 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 Vasko841d1a92018-09-07 15:40:31 +0200267 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200268 if (!S_ISREG(sb.st_mode)) {
269 LOGERR(ctx, LY_EINVAL, "File to mmap() is not a regular file.");
270 return LY_ESYS;
Michal Vasko841d1a92018-09-07 15:40:31 +0200271 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200272 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 Vasko841d1a92018-09-07 15:40:31 +0200297 }
298
Radek Krejcif345c012018-09-19 11:12:59 +0200299 return LY_SUCCESS;
Radek Krejci86d106e2018-10-18 09:53:19 +0200300}
Michal Vasko841d1a92018-09-07 15:40:31 +0200301
Radek Krejci86d106e2018-10-18 09:53:19 +0200302LY_ERR
303ly_munmap(void *addr, size_t length)
304{
305 if (munmap(addr, length)) {
306 return LY_ESYS;
307 }
308 return LY_SUCCESS;
Michal Vasko841d1a92018-09-07 15:40:31 +0200309}
Radek Krejci4f28eda2018-11-12 11:46:16 +0100310
311LY_ERR
Radek Krejci249973a2019-06-10 10:50:54 +0200312ly_parse_int(const char *val_str, size_t val_len, int64_t min, int64_t max, int base, int64_t *ret)
Radek Krejci4f28eda2018-11-12 11:46:16 +0100313{
314 char *strptr;
Radek Krejci9ea8ca12019-06-10 13:11:55 +0200315 int64_t i;
Radek Krejci4f28eda2018-11-12 11:46:16 +0100316
Radek Krejci249973a2019-06-10 10:50:54 +0200317 LY_CHECK_ARG_RET(NULL, val_str, val_str[0], val_len, LY_EINVAL);
Radek Krejci4f28eda2018-11-12 11:46:16 +0100318
319 /* convert to 64-bit integer, all the redundant characters are handled */
320 errno = 0;
321 strptr = NULL;
322
323 /* parse the value */
Radek Krejci9ea8ca12019-06-10 13:11:55 +0200324 i = strtoll(val_str, &strptr, base);
Radek Krejci249973a2019-06-10 10:50:54 +0200325 if (errno || strptr == val_str) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100326 return LY_EVALID;
Radek Krejci9ea8ca12019-06-10 13:11:55 +0200327 } else if ((i < min) || (i > max)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100328 return LY_EDENIED;
329 } else if (strptr && *strptr) {
330 while (isspace(*strptr)) {
331 ++strptr;
332 }
Radek Krejci249973a2019-06-10 10:50:54 +0200333 if (*strptr && strptr < val_str + val_len) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100334 return LY_EVALID;
335 }
336 }
Radek Krejci9ea8ca12019-06-10 13:11:55 +0200337
338 *ret = i;
Radek Krejci4f28eda2018-11-12 11:46:16 +0100339 return LY_SUCCESS;
340}
341
342LY_ERR
Radek Krejci249973a2019-06-10 10:50:54 +0200343ly_parse_uint(const char *val_str, size_t val_len, uint64_t max, int base, uint64_t *ret)
Radek Krejci4f28eda2018-11-12 11:46:16 +0100344{
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 Krejci249973a2019-06-10 10:50:54 +0200353 if (errno || strptr == val_str) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100354 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 Krejci249973a2019-06-10 10:50:54 +0200361 if (*strptr && strptr < val_str + val_len) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100362 return LY_EVALID;
363 }
364 }
365
366 *ret = u;
367 return LY_SUCCESS;
368}
Radek Krejcib4a4a272019-06-10 12:44:52 +0200369
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 */
380static LY_ERR
381lys_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
396LY_ERR
397ly_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
427LY_ERR
428ly_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 */
455 *id = &in[offset++];
456 for (; isdigit(in[offset]); offset++);
457 *id_len = &in[offset] - *id;
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;
465
466 } else {
467 /* key-predicate: "[" *WSP node-identifier *WSP "=" *WSP quoted-string *WSP "]" */
468 in = &in[offset];
469 if (ly_parse_nodeid(&in, prefix, prefix_len, id, id_len)) {
470 *errmsg = "Invalid node-identifier.";
471 goto error;
472 }
473 offset = in - *pred;
474 in = *pred;
475 expr = 1;
476 }
477
478 if (expr) {
479 /* *WSP "=" *WSP quoted-string *WSP "]" */
480 for (; isspace(in[offset]); offset++);
481
482 if (in[offset] != '=') {
483 *errmsg = "Unexpected character instead of \'=\'.";
484 goto error;
485 }
486 offset++;
487 for (; isspace(in[offset]); offset++);
488
489 /* quoted-string */
490 quot = in[offset++];
491 if (quot != '\'' && quot != '\"') {
492 *errmsg = "String value is not quoted.";
493 goto error;
494 }
495 *value = &in[offset];
496 for (;offset < limit && in[offset] != quot; offset++);
497 *value_len = &in[offset] - *value;
498 }
499
500 /* *WSP "]" */
501 for(; isspace(in[offset]); offset++);
502 if (in[offset] != ']') {
503 *errmsg = "Predicate is not terminated by \']\' character.";
504 goto error;
505 }
506
507 if (offset < limit) {
508 return LY_SUCCESS;
509 }
510
511 /* we read after the limit */
512 *errmsg = "Predicate is incomplete.";
513 *prefix = *id = *value = NULL;
514 *prefix_len = *id_len = *value_len = 0;
515 offset = limit;
516 ret = LY_EINVAL;
517
518error:
519 *pred = &in[offset];
520 return ret;
521}