blob: f5aeed1834b9a03efdb38e3777c64f2a86c12903 [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{
152 char tmp[PATH_MAX];
153 char *retval;
154
155 if (getcwd(tmp, sizeof(tmp))) {
156 retval = strdup(tmp);
157 LY_CHECK_ERR_RETURN(!retval, LOGMEM(NULL), NULL);
158 return retval;
159 }
160 return NULL;
161}
162#endif
163
Michal Vasko1324b6c2018-09-07 11:16:23 +0200164void *
165ly_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 Vasko841d1a92018-09-07 15:40:31 +0200176
Radek Krejcib416be62018-10-01 14:51:45 +0200177LY_ERR
178ly_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
184 c = (*input)[0];
185 LY_CHECK_RET(!c, LY_EINVAL);
186
187 if (!(c & 0x80)) {
188 /* one byte character */
189 len = 1;
190
191 if (c < 0x20 && c != 0x9 && c != 0xa && c != 0xd) {
192 return LY_EINVAL;
193 }
194 } else if ((c & 0xe0) == 0xc0) {
195 /* two bytes character */
196 len = 2;
197
198 aux = (*input)[1];
199 if ((aux & 0xc0) != 0x80) {
200 return LY_EINVAL;
201 }
202 c = ((c & 0x1f) << 6) | (aux & 0x3f);
203
204 if (c < 0x80) {
205 return LY_EINVAL;
206 }
207 } else if ((c & 0xf0) == 0xe0) {
208 /* three bytes character */
209 len = 3;
210
211 c &= 0x0f;
212 for (i = 1; i <= 2; 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 < 0x800 || (c > 0xd7ff && c < 0xe000) || c > 0xfffd) {
222 return LY_EINVAL;
223 }
224 } else if ((c & 0xf8) == 0xf0) {
225 /* four bytes character */
226 len = 4;
227
228 c &= 0x07;
229 for (i = 1; i <= 3; i++) {
230 aux = (*input)[i];
231 if ((aux & 0xc0) != 0x80) {
232 return LY_EINVAL;
233 }
234
235 c = (c << 6) | (aux & 0x3f);
236 }
237
238 if (c < 0x1000 || c > 0x10ffff) {
239 return LY_EINVAL;
240 }
241 } else {
242 return LY_EINVAL;
243 }
244
245 (*utf8_char) = c;
246 (*input) += len;
247 if (bytes_read) {
248 (*bytes_read) = len;
249 }
250 return LY_SUCCESS;
251}
252
Radek Krejcid972c252018-09-25 13:23:39 +0200253size_t
254LY_VCODE_INSTREXP_len(const char *str)
255{
256 size_t len = 0;
257 if (!str) {
258 return len;
259 } else if (!str[0]) {
260 return 1;
261 }
262 for (len = 1; len < LY_VCODE_INSTREXP_MAXLEN && str[len]; ++len);
263 return len;
264}
265
Radek Krejcif345c012018-09-19 11:12:59 +0200266LY_ERR
Radek Krejci86d106e2018-10-18 09:53:19 +0200267ly_mmap(struct ly_ctx *ctx, int fd, size_t *length, void **addr)
Michal Vasko841d1a92018-09-07 15:40:31 +0200268{
Radek Krejci86d106e2018-10-18 09:53:19 +0200269 struct stat sb;
270 long pagesize;
271 size_t m;
Michal Vasko841d1a92018-09-07 15:40:31 +0200272
Radek Krejci86d106e2018-10-18 09:53:19 +0200273 assert(length);
274 assert(addr);
275 assert(fd >= 0);
Michal Vasko841d1a92018-09-07 15:40:31 +0200276
Radek Krejci86d106e2018-10-18 09:53:19 +0200277 if (fstat(fd, &sb) == -1) {
278 LOGERR(ctx, LY_ESYS, "Failed to stat the file descriptor (%s) for the mmap().", strerror(errno));
279 return LY_ESYS;
Michal Vasko841d1a92018-09-07 15:40:31 +0200280 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200281 if (!S_ISREG(sb.st_mode)) {
282 LOGERR(ctx, LY_EINVAL, "File to mmap() is not a regular file.");
283 return LY_ESYS;
Michal Vasko841d1a92018-09-07 15:40:31 +0200284 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200285 if (!sb.st_size) {
286 *addr = NULL;
287 return LY_SUCCESS;
288 }
289 pagesize = sysconf(_SC_PAGESIZE);
290
291 m = sb.st_size % pagesize;
292 if (m && pagesize - m >= 1) {
293 /* there will be enough space (at least 1 byte) after the file content mapping to provide zeroed NULL-termination byte */
294 *length = sb.st_size + 1;
295 *addr = mmap(NULL, *length, PROT_READ, MAP_PRIVATE, fd, 0);
296 } else {
297 /* there will not be enough bytes after the file content mapping for the additional bytes and some of them
298 * would overflow into another page that would not be zerroed and any access into it would generate SIGBUS.
299 * Therefore we have to do the following hack with double mapping. First, the required number of bytes
300 * (including the additinal bytes) is required as anonymous and thus they will be really provided (actually more
301 * because of using whole pages) and also initialized by zeros. Then, the file is mapped to the same address
302 * where the anonymous mapping starts. */
303 *length = sb.st_size + pagesize;
304 *addr = mmap(NULL, *length, PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
305 *addr = mmap(*addr, sb.st_size, PROT_READ, MAP_PRIVATE | MAP_FIXED, fd, 0);
306 }
307 if (*addr == MAP_FAILED) {
308 LOGERR(ctx, LY_ESYS, "mmap() failed (%s).", strerror(errno));
309 return LY_ESYS;
Michal Vasko841d1a92018-09-07 15:40:31 +0200310 }
311
Radek Krejcif345c012018-09-19 11:12:59 +0200312 return LY_SUCCESS;
Radek Krejci86d106e2018-10-18 09:53:19 +0200313}
Michal Vasko841d1a92018-09-07 15:40:31 +0200314
Radek Krejci86d106e2018-10-18 09:53:19 +0200315LY_ERR
316ly_munmap(void *addr, size_t length)
317{
318 if (munmap(addr, length)) {
319 return LY_ESYS;
320 }
321 return LY_SUCCESS;
Michal Vasko841d1a92018-09-07 15:40:31 +0200322}
Radek Krejci4f28eda2018-11-12 11:46:16 +0100323
324LY_ERR
325ly_parse_int(const char *val_str, int64_t min, int64_t max, int base, int64_t *ret)
326{
327 char *strptr;
328
329 LY_CHECK_ARG_RET(NULL, val_str, val_str[0], LY_EINVAL);
330
331 /* convert to 64-bit integer, all the redundant characters are handled */
332 errno = 0;
333 strptr = NULL;
334
335 /* parse the value */
336 *ret = strtoll(val_str, &strptr, base);
337 if (errno) {
338 return LY_EVALID;
339 } else if ((*ret < min) || (*ret > max)) {
340 return LY_EDENIED;
341 } else if (strptr && *strptr) {
342 while (isspace(*strptr)) {
343 ++strptr;
344 }
345 if (*strptr) {
346 return LY_EVALID;
347 }
348 }
349 return LY_SUCCESS;
350}
351
352LY_ERR
353ly_parse_uint(const char *val_str, uint64_t max, int base, uint64_t *ret)
354{
355 char *strptr;
356 uint64_t u;
357
358 LY_CHECK_ARG_RET(NULL, val_str, val_str[0], LY_EINVAL);
359
360 errno = 0;
361 strptr = NULL;
362 u = strtoull(val_str, &strptr, base);
363 if (errno) {
364 return LY_EVALID;
365 } else if ((u > max) || (u && val_str[0] == '-')) {
366 return LY_EDENIED;
367 } else if (strptr && *strptr) {
368 while (isspace(*strptr)) {
369 ++strptr;
370 }
371 if (*strptr) {
372 return LY_EVALID;
373 }
374 }
375
376 *ret = u;
377 return LY_SUCCESS;
378}