blob: d50c6c3749bc35577ef580ca2af1772f04734a22 [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>
Radek Krejcicad716e2018-11-26 15:18:27 +010019#include <limits.h>
Michal Vasko1324b6c2018-09-07 11:16:23 +020020#include <stdlib.h>
Michal Vasko841d1a92018-09-07 15:40:31 +020021#include <stdio.h>
22#include <ctype.h>
23#include <string.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020024#include <sys/mman.h>
25#include <sys/stat.h>
26#include <sys/types.h>
27#include <unistd.h>
Michal Vasko841d1a92018-09-07 15:40:31 +020028
29#include "tree_schema.h"
Michal Vasko1324b6c2018-09-07 11:16:23 +020030
Radek Krejcic59bc972018-09-17 16:13:06 +020031const 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
106const 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
141const 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 Krejcicad716e2018-11-26 15:18:27 +0100148#ifndef __USE_GNU
149char *
150get_current_dir_name(void)
151{
Radek Krejci7802bae2018-11-26 15:34:10 +0100152#ifdef __APPLE__
153 char tmp[MAXPATHLEN];
154#else
Radek Krejcicad716e2018-11-26 15:18:27 +0100155 char tmp[PATH_MAX];
Radek Krejci7802bae2018-11-26 15:34:10 +0100156#endif
Radek Krejcicad716e2018-11-26 15:18:27 +0100157 char *retval;
158
159 if (getcwd(tmp, sizeof(tmp))) {
160 retval = strdup(tmp);
161 LY_CHECK_ERR_RETURN(!retval, LOGMEM(NULL), NULL);
162 return retval;
163 }
164 return NULL;
165}
166#endif
167
Michal Vasko1324b6c2018-09-07 11:16:23 +0200168void *
169ly_realloc(void *ptr, size_t size)
170{
171 void *new_mem;
172
173 new_mem = realloc(ptr, size);
174 if (!new_mem) {
175 free(ptr);
176 }
177
178 return new_mem;
179}
Michal Vasko841d1a92018-09-07 15:40:31 +0200180
Radek Krejcib416be62018-10-01 14:51:45 +0200181LY_ERR
182ly_getutf8(const char **input, unsigned int *utf8_char, size_t *bytes_read)
183{
184 unsigned int c, len;
185 int aux;
186 int i;
187
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 Krejcid972c252018-09-25 13:23:39 +0200257size_t
258LY_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 Krejcif345c012018-09-19 11:12:59 +0200270LY_ERR
Radek Krejci86d106e2018-10-18 09:53:19 +0200271ly_mmap(struct ly_ctx *ctx, int fd, size_t *length, void **addr)
Michal Vasko841d1a92018-09-07 15:40:31 +0200272{
Radek Krejci86d106e2018-10-18 09:53:19 +0200273 struct stat sb;
274 long pagesize;
275 size_t m;
Michal Vasko841d1a92018-09-07 15:40:31 +0200276
Radek Krejci86d106e2018-10-18 09:53:19 +0200277 assert(length);
278 assert(addr);
279 assert(fd >= 0);
Michal Vasko841d1a92018-09-07 15:40:31 +0200280
Radek Krejci86d106e2018-10-18 09:53:19 +0200281 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 Vasko841d1a92018-09-07 15:40:31 +0200284 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200285 if (!S_ISREG(sb.st_mode)) {
286 LOGERR(ctx, LY_EINVAL, "File to mmap() is not a regular file.");
287 return LY_ESYS;
Michal Vasko841d1a92018-09-07 15:40:31 +0200288 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200289 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 Vasko841d1a92018-09-07 15:40:31 +0200314 }
315
Radek Krejcif345c012018-09-19 11:12:59 +0200316 return LY_SUCCESS;
Radek Krejci86d106e2018-10-18 09:53:19 +0200317}
Michal Vasko841d1a92018-09-07 15:40:31 +0200318
Radek Krejci86d106e2018-10-18 09:53:19 +0200319LY_ERR
320ly_munmap(void *addr, size_t length)
321{
322 if (munmap(addr, length)) {
323 return LY_ESYS;
324 }
325 return LY_SUCCESS;
Michal Vasko841d1a92018-09-07 15:40:31 +0200326}
Radek Krejci4f28eda2018-11-12 11:46:16 +0100327
328LY_ERR
329ly_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
356LY_ERR
357ly_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}