blob: ab3b71f3cd55f6d62df65b82d250b6639bec33aa [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 */
Michal Vasko841d1a92018-09-07 15:40:31 +020014#define _XOPEN_SOURCE
Radek Krejci86d106e2018-10-18 09:53:19 +020015#define _DEFAULT_SOURCE
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 <stdio.h>
21#include <ctype.h>
22#include <string.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020023#include <sys/mman.h>
24#include <sys/stat.h>
25#include <sys/types.h>
26#include <unistd.h>
Michal Vasko841d1a92018-09-07 15:40:31 +020027
Michal Vasko58257712018-09-12 11:11:38 +020028#include "common.h"
Michal Vasko841d1a92018-09-07 15:40:31 +020029#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
Michal Vasko1324b6c2018-09-07 11:16:23 +0200148void *
149ly_realloc(void *ptr, size_t size)
150{
151 void *new_mem;
152
153 new_mem = realloc(ptr, size);
154 if (!new_mem) {
155 free(ptr);
156 }
157
158 return new_mem;
159}
Michal Vasko841d1a92018-09-07 15:40:31 +0200160
Radek Krejcib416be62018-10-01 14:51:45 +0200161LY_ERR
162ly_getutf8(const char **input, unsigned int *utf8_char, size_t *bytes_read)
163{
164 unsigned int c, len;
165 int aux;
166 int i;
167
168 c = (*input)[0];
169 LY_CHECK_RET(!c, LY_EINVAL);
170
171 if (!(c & 0x80)) {
172 /* one byte character */
173 len = 1;
174
175 if (c < 0x20 && c != 0x9 && c != 0xa && c != 0xd) {
176 return LY_EINVAL;
177 }
178 } else if ((c & 0xe0) == 0xc0) {
179 /* two bytes character */
180 len = 2;
181
182 aux = (*input)[1];
183 if ((aux & 0xc0) != 0x80) {
184 return LY_EINVAL;
185 }
186 c = ((c & 0x1f) << 6) | (aux & 0x3f);
187
188 if (c < 0x80) {
189 return LY_EINVAL;
190 }
191 } else if ((c & 0xf0) == 0xe0) {
192 /* three bytes character */
193 len = 3;
194
195 c &= 0x0f;
196 for (i = 1; i <= 2; i++) {
197 aux = (*input)[i];
198 if ((aux & 0xc0) != 0x80) {
199 return LY_EINVAL;
200 }
201
202 c = (c << 6) | (aux & 0x3f);
203 }
204
205 if (c < 0x800 || (c > 0xd7ff && c < 0xe000) || c > 0xfffd) {
206 return LY_EINVAL;
207 }
208 } else if ((c & 0xf8) == 0xf0) {
209 /* four bytes character */
210 len = 4;
211
212 c &= 0x07;
213 for (i = 1; i <= 3; i++) {
214 aux = (*input)[i];
215 if ((aux & 0xc0) != 0x80) {
216 return LY_EINVAL;
217 }
218
219 c = (c << 6) | (aux & 0x3f);
220 }
221
222 if (c < 0x1000 || c > 0x10ffff) {
223 return LY_EINVAL;
224 }
225 } else {
226 return LY_EINVAL;
227 }
228
229 (*utf8_char) = c;
230 (*input) += len;
231 if (bytes_read) {
232 (*bytes_read) = len;
233 }
234 return LY_SUCCESS;
235}
236
Radek Krejcid972c252018-09-25 13:23:39 +0200237size_t
238LY_VCODE_INSTREXP_len(const char *str)
239{
240 size_t len = 0;
241 if (!str) {
242 return len;
243 } else if (!str[0]) {
244 return 1;
245 }
246 for (len = 1; len < LY_VCODE_INSTREXP_MAXLEN && str[len]; ++len);
247 return len;
248}
249
Radek Krejcif345c012018-09-19 11:12:59 +0200250LY_ERR
Radek Krejci86d106e2018-10-18 09:53:19 +0200251ly_mmap(struct ly_ctx *ctx, int fd, size_t *length, void **addr)
Michal Vasko841d1a92018-09-07 15:40:31 +0200252{
Radek Krejci86d106e2018-10-18 09:53:19 +0200253 struct stat sb;
254 long pagesize;
255 size_t m;
Michal Vasko841d1a92018-09-07 15:40:31 +0200256
Radek Krejci86d106e2018-10-18 09:53:19 +0200257 assert(length);
258 assert(addr);
259 assert(fd >= 0);
Michal Vasko841d1a92018-09-07 15:40:31 +0200260
Radek Krejci86d106e2018-10-18 09:53:19 +0200261 if (fstat(fd, &sb) == -1) {
262 LOGERR(ctx, LY_ESYS, "Failed to stat the file descriptor (%s) for the mmap().", strerror(errno));
263 return LY_ESYS;
Michal Vasko841d1a92018-09-07 15:40:31 +0200264 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200265 if (!S_ISREG(sb.st_mode)) {
266 LOGERR(ctx, LY_EINVAL, "File to mmap() is not a regular file.");
267 return LY_ESYS;
Michal Vasko841d1a92018-09-07 15:40:31 +0200268 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200269 if (!sb.st_size) {
270 *addr = NULL;
271 return LY_SUCCESS;
272 }
273 pagesize = sysconf(_SC_PAGESIZE);
274
275 m = sb.st_size % pagesize;
276 if (m && pagesize - m >= 1) {
277 /* there will be enough space (at least 1 byte) after the file content mapping to provide zeroed NULL-termination byte */
278 *length = sb.st_size + 1;
279 *addr = mmap(NULL, *length, PROT_READ, MAP_PRIVATE, fd, 0);
280 } else {
281 /* there will not be enough bytes after the file content mapping for the additional bytes and some of them
282 * would overflow into another page that would not be zerroed and any access into it would generate SIGBUS.
283 * Therefore we have to do the following hack with double mapping. First, the required number of bytes
284 * (including the additinal bytes) is required as anonymous and thus they will be really provided (actually more
285 * because of using whole pages) and also initialized by zeros. Then, the file is mapped to the same address
286 * where the anonymous mapping starts. */
287 *length = sb.st_size + pagesize;
288 *addr = mmap(NULL, *length, PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
289 *addr = mmap(*addr, sb.st_size, PROT_READ, MAP_PRIVATE | MAP_FIXED, fd, 0);
290 }
291 if (*addr == MAP_FAILED) {
292 LOGERR(ctx, LY_ESYS, "mmap() failed (%s).", strerror(errno));
293 return LY_ESYS;
Michal Vasko841d1a92018-09-07 15:40:31 +0200294 }
295
Radek Krejcif345c012018-09-19 11:12:59 +0200296 return LY_SUCCESS;
Radek Krejci86d106e2018-10-18 09:53:19 +0200297}
Michal Vasko841d1a92018-09-07 15:40:31 +0200298
Radek Krejci86d106e2018-10-18 09:53:19 +0200299LY_ERR
300ly_munmap(void *addr, size_t length)
301{
302 if (munmap(addr, length)) {
303 return LY_ESYS;
304 }
305 return LY_SUCCESS;
Michal Vasko841d1a92018-09-07 15:40:31 +0200306}