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