blob: 38f51eaa359c89850fd1fbc911770c2dc17e718d [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
Radek Krejci535ea9f2020-05-29 16:01:05 +020015#define _GNU_SOURCE
16
Radek Krejcib7db73a2018-10-24 14:18:40 +020017#include "common.h"
Michal Vasko1324b6c2018-09-07 11:16:23 +020018
Radek Krejci86d106e2018-10-18 09:53:19 +020019#include <assert.h>
Michal Vasko841d1a92018-09-07 15:40:31 +020020#include <ctype.h>
Radek Krejci4546aa62019-07-15 16:53:32 +020021#include <errno.h>
Jan Kundrátd31adc12022-07-07 21:36:15 +020022#include <fcntl.h>
Michal Vasko15dc9fa2021-05-03 14:33:05 +020023#include <inttypes.h>
Radek Krejci4546aa62019-07-15 16:53:32 +020024#include <stdarg.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020025#include <stdio.h>
Radek Krejci4546aa62019-07-15 16:53:32 +020026#include <stdlib.h>
Michal Vasko841d1a92018-09-07 15:40:31 +020027#include <string.h>
Jan Kundrátf1960dc2021-12-12 03:12:23 +010028#ifndef _WIN32
Radek Krejci86d106e2018-10-18 09:53:19 +020029#include <sys/mman.h>
Jan Kundrátd31adc12022-07-07 21:36:15 +020030#else
31#include <io.h>
Jan Kundrátf1960dc2021-12-12 03:12:23 +010032#endif
Radek Krejci86d106e2018-10-18 09:53:19 +020033#include <sys/stat.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020034#include <unistd.h>
Michal Vasko841d1a92018-09-07 15:40:31 +020035
Radek Krejciaa45bda2020-07-20 07:43:38 +020036#include "compat.h"
Radek Krejcib4a4a272019-06-10 12:44:52 +020037#include "tree_schema_internal.h"
aPiecek704f8e92021-08-25 13:35:05 +020038#include "xml.h"
Michal Vasko1324b6c2018-09-07 11:16:23 +020039
40void *
41ly_realloc(void *ptr, size_t size)
42{
43 void *new_mem;
44
45 new_mem = realloc(ptr, size);
46 if (!new_mem) {
47 free(ptr);
48 }
49
50 return new_mem;
51}
Michal Vasko841d1a92018-09-07 15:40:31 +020052
Michal Vasko03ff5a72019-09-11 13:49:33 +020053char *
Radek Krejci1deb5be2020-08-26 16:43:36 +020054ly_strnchr(const char *s, int c, size_t len)
Michal Vasko03ff5a72019-09-11 13:49:33 +020055{
Michal Vaskob4d40d62021-05-04 11:42:44 +020056 for ( ; len && (*s != (char)c); ++s, --len) {}
57 return len ? (char *)s : NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +020058}
59
Radek Krejci7f9b6512019-09-18 13:11:09 +020060int
61ly_strncmp(const char *refstr, const char *str, size_t str_len)
62{
63 int rc = strncmp(refstr, str, str_len);
Michal Vasko69730152020-10-09 16:30:07 +020064
65 if (!rc && (refstr[str_len] == '\0')) {
Radek Krejci7f9b6512019-09-18 13:11:09 +020066 return 0;
67 } else {
68 return rc ? rc : 1;
69 }
70}
71
Michal Vasko15dc9fa2021-05-03 14:33:05 +020072LY_ERR
73ly_strntou8(const char *nptr, size_t len, uint8_t *ret)
74{
75 uint8_t num = 0, dig, dec_pow;
76
77 if (len > 3) {
78 /* overflow for sure */
79 return LY_EDENIED;
80 }
81
82 dec_pow = 1;
83 for ( ; len && isdigit(nptr[len - 1]); --len) {
84 dig = nptr[len - 1] - 48;
85
86 if (LY_OVERFLOW_MUL(UINT8_MAX, dig, dec_pow)) {
87 return LY_EDENIED;
88 }
89 dig *= dec_pow;
90
91 if (LY_OVERFLOW_ADD(UINT8_MAX, num, dig)) {
92 return LY_EDENIED;
93 }
94 num += dig;
95
96 dec_pow *= 10;
97 }
98
99 if (len) {
100 return LY_EVALID;
101 }
102 *ret = num;
103 return LY_SUCCESS;
104}
105
aPieceke3f828d2021-05-10 15:34:41 +0200106LY_ERR
107ly_value_prefix_next(const char *str_begin, const char *str_end, uint32_t *len, ly_bool *is_prefix, const char **str_next)
aPiecekf102d4d2021-03-30 12:18:38 +0200108{
109 const char *stop, *prefix;
aPieceke3f828d2021-05-10 15:34:41 +0200110 size_t bytes_read;
aPiecekf102d4d2021-03-30 12:18:38 +0200111 uint32_t c;
112 ly_bool prefix_found;
aPieceke3f828d2021-05-10 15:34:41 +0200113 LY_ERR ret = LY_SUCCESS;
aPiecekf102d4d2021-03-30 12:18:38 +0200114
aPieceke3f828d2021-05-10 15:34:41 +0200115 assert(len && is_prefix && str_next);
aPiecekf102d4d2021-03-30 12:18:38 +0200116
117#define IS_AT_END(PTR, STR_END) (STR_END ? PTR == STR_END : !(*PTR))
118
119 *str_next = NULL;
120 *is_prefix = 0;
aPieceke3f828d2021-05-10 15:34:41 +0200121 *len = 0;
aPiecekf102d4d2021-03-30 12:18:38 +0200122
123 if (!str_begin || !(*str_begin) || (str_begin == str_end)) {
124 return ret;
125 }
126
127 stop = str_begin;
128 prefix = NULL;
129 prefix_found = 0;
130
131 do {
132 /* look for the beginning of the YANG value */
aPieceke3f828d2021-05-10 15:34:41 +0200133 do {
134 LY_CHECK_RET(ly_getutf8(&stop, &c, &bytes_read));
135 } while (!is_xmlqnamestartchar(c) && !IS_AT_END(stop, str_end));
aPiecekf102d4d2021-03-30 12:18:38 +0200136
137 if (IS_AT_END(stop, str_end)) {
138 break;
139 }
140
141 /* maybe the prefix was found */
aPieceke3f828d2021-05-10 15:34:41 +0200142 prefix = stop - bytes_read;
aPiecekf102d4d2021-03-30 12:18:38 +0200143
144 /* look for the the end of the prefix */
aPieceke3f828d2021-05-10 15:34:41 +0200145 do {
146 LY_CHECK_RET(ly_getutf8(&stop, &c, &bytes_read));
147 } while (is_xmlqnamechar(c) && !IS_AT_END(stop, str_end));
aPiecekf102d4d2021-03-30 12:18:38 +0200148
149 prefix_found = c == ':' ? 1 : 0;
150
151 /* if it wasn't the prefix, keep looking */
152 } while (!IS_AT_END(stop, str_end) && !prefix_found);
153
154 if ((str_begin == prefix) && prefix_found) {
155 /* prefix found at the beginning of the input string */
156 *is_prefix = 1;
157 *str_next = IS_AT_END(stop, str_end) ? NULL : stop;
aPieceke3f828d2021-05-10 15:34:41 +0200158 *len = (stop - bytes_read) - str_begin;
aPiecekf102d4d2021-03-30 12:18:38 +0200159 } else if ((str_begin != prefix) && (prefix_found)) {
160 /* there is a some string before prefix */
161 *str_next = prefix;
aPieceke3f828d2021-05-10 15:34:41 +0200162 *len = prefix - str_begin;
aPiecekf102d4d2021-03-30 12:18:38 +0200163 } else {
164 /* no prefix found */
aPieceke3f828d2021-05-10 15:34:41 +0200165 *len = stop - str_begin;
aPiecekf102d4d2021-03-30 12:18:38 +0200166 }
167
168#undef IS_AT_END
169
170 return ret;
171}
172
Radek Krejcib416be62018-10-01 14:51:45 +0200173LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100174ly_getutf8(const char **input, uint32_t *utf8_char, size_t *bytes_read)
Radek Krejcib416be62018-10-01 14:51:45 +0200175{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200176 uint32_t c, aux;
177 size_t len;
Radek Krejcib416be62018-10-01 14:51:45 +0200178
Radek Krejcicc6a45c2019-05-13 10:16:14 +0200179 if (bytes_read) {
180 (*bytes_read) = 0;
181 }
182
Radek Krejcib416be62018-10-01 14:51:45 +0200183 c = (*input)[0];
184 LY_CHECK_RET(!c, LY_EINVAL);
185
186 if (!(c & 0x80)) {
187 /* one byte character */
188 len = 1;
189
Michal Vasko69730152020-10-09 16:30:07 +0200190 if ((c < 0x20) && (c != 0x9) && (c != 0xa) && (c != 0xd)) {
Radek Krejcib416be62018-10-01 14:51:45 +0200191 return LY_EINVAL;
192 }
193 } else if ((c & 0xe0) == 0xc0) {
194 /* two bytes character */
195 len = 2;
196
197 aux = (*input)[1];
198 if ((aux & 0xc0) != 0x80) {
199 return LY_EINVAL;
200 }
201 c = ((c & 0x1f) << 6) | (aux & 0x3f);
202
203 if (c < 0x80) {
204 return LY_EINVAL;
205 }
206 } else if ((c & 0xf0) == 0xe0) {
207 /* three bytes character */
208 len = 3;
209
210 c &= 0x0f;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200211 for (uint64_t i = 1; i <= 2; i++) {
Radek Krejcib416be62018-10-01 14:51:45 +0200212 aux = (*input)[i];
213 if ((aux & 0xc0) != 0x80) {
214 return LY_EINVAL;
215 }
216
217 c = (c << 6) | (aux & 0x3f);
218 }
219
Michal Vasko69730152020-10-09 16:30:07 +0200220 if ((c < 0x800) || ((c > 0xd7ff) && (c < 0xe000)) || (c > 0xfffd)) {
Radek Krejcib416be62018-10-01 14:51:45 +0200221 return LY_EINVAL;
222 }
223 } else if ((c & 0xf8) == 0xf0) {
224 /* four bytes character */
225 len = 4;
226
227 c &= 0x07;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200228 for (uint64_t i = 1; i <= 3; i++) {
Radek Krejcib416be62018-10-01 14:51:45 +0200229 aux = (*input)[i];
230 if ((aux & 0xc0) != 0x80) {
231 return LY_EINVAL;
232 }
233
234 c = (c << 6) | (aux & 0x3f);
235 }
236
Michal Vasko69730152020-10-09 16:30:07 +0200237 if ((c < 0x1000) || (c > 0x10ffff)) {
Radek Krejcib416be62018-10-01 14:51:45 +0200238 return LY_EINVAL;
239 }
240 } else {
241 return LY_EINVAL;
242 }
243
244 (*utf8_char) = c;
245 (*input) += len;
246 if (bytes_read) {
247 (*bytes_read) = len;
248 }
249 return LY_SUCCESS;
250}
251
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200252LY_ERR
253ly_pututf8(char *dst, uint32_t value, size_t *bytes_written)
254{
255 if (value < 0x80) {
256 /* one byte character */
Michal Vasko69730152020-10-09 16:30:07 +0200257 if ((value < 0x20) &&
258 (value != 0x09) &&
259 (value != 0x0a) &&
260 (value != 0x0d)) {
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200261 return LY_EINVAL;
262 }
263
264 dst[0] = value;
265 (*bytes_written) = 1;
266 } else if (value < 0x800) {
267 /* two bytes character */
268 dst[0] = 0xc0 | (value >> 6);
269 dst[1] = 0x80 | (value & 0x3f);
270 (*bytes_written) = 2;
271 } else if (value < 0xfffe) {
272 /* three bytes character */
273 if (((value & 0xf800) == 0xd800) ||
Michal Vasko69730152020-10-09 16:30:07 +0200274 ((value >= 0xfdd0) && (value <= 0xfdef))) {
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200275 /* exclude surrogate blocks %xD800-DFFF */
276 /* exclude noncharacters %xFDD0-FDEF */
277 return LY_EINVAL;
278 }
279
280 dst[0] = 0xe0 | (value >> 12);
281 dst[1] = 0x80 | ((value >> 6) & 0x3f);
282 dst[2] = 0x80 | (value & 0x3f);
283
284 (*bytes_written) = 3;
285 } else if (value < 0x10fffe) {
286 if ((value & 0xffe) == 0xffe) {
287 /* exclude noncharacters %xFFFE-FFFF, %x1FFFE-1FFFF, %x2FFFE-2FFFF, %x3FFFE-3FFFF, %x4FFFE-4FFFF,
288 * %x5FFFE-5FFFF, %x6FFFE-6FFFF, %x7FFFE-7FFFF, %x8FFFE-8FFFF, %x9FFFE-9FFFF, %xAFFFE-AFFFF,
289 * %xBFFFE-BFFFF, %xCFFFE-CFFFF, %xDFFFE-DFFFF, %xEFFFE-EFFFF, %xFFFFE-FFFFF, %x10FFFE-10FFFF */
290 return LY_EINVAL;
291 }
292 /* four bytes character */
293 dst[0] = 0xf0 | (value >> 18);
294 dst[1] = 0x80 | ((value >> 12) & 0x3f);
295 dst[2] = 0x80 | ((value >> 6) & 0x3f);
296 dst[3] = 0x80 | (value & 0x3f);
297
298 (*bytes_written) = 4;
299 } else {
300 return LY_EINVAL;
301 }
302 return LY_SUCCESS;
303}
304
Radek Krejci76c98012019-08-14 11:23:24 +0200305/**
306 * @brief Static table of the UTF8 characters lengths according to their first byte.
307 */
Radek Krejcif6a11002020-08-21 13:29:07 +0200308static const unsigned char utf8_char_length_table[] = {
Radek Krejci76c98012019-08-14 11:23:24 +0200309 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
310 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
311 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
312 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
313 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
314 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
315 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
316 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
317 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
318 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
319 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
320 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
321 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
322 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
323 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
324 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 1, 1
325};
326
327size_t
328ly_utf8len(const char *str, size_t bytes)
329{
Radek Krejci1e008d22020-08-17 11:37:37 +0200330 size_t len = 0;
331 const char *ptr = str;
Radek Krejci76c98012019-08-14 11:23:24 +0200332
Michal Vaskob4d40d62021-05-04 11:42:44 +0200333 while (((size_t)(ptr - str) < bytes) && *ptr) {
Radek Krejci1e008d22020-08-17 11:37:37 +0200334 ++len;
335 ptr += utf8_char_length_table[((unsigned char)(*ptr))];
336 }
Radek Krejci76c98012019-08-14 11:23:24 +0200337 return len;
338}
339
Radek Krejcid972c252018-09-25 13:23:39 +0200340size_t
341LY_VCODE_INSTREXP_len(const char *str)
342{
343 size_t len = 0;
Michal Vasko69730152020-10-09 16:30:07 +0200344
Radek Krejcid972c252018-09-25 13:23:39 +0200345 if (!str) {
346 return len;
347 } else if (!str[0]) {
348 return 1;
349 }
Radek Krejci1e008d22020-08-17 11:37:37 +0200350 for (len = 1; len < LY_VCODE_INSTREXP_MAXLEN && str[len]; ++len) {}
Radek Krejcid972c252018-09-25 13:23:39 +0200351 return len;
352}
353
Jan Kundrátf1960dc2021-12-12 03:12:23 +0100354#ifdef HAVE_MMAP
Radek Krejcif345c012018-09-19 11:12:59 +0200355LY_ERR
Radek Krejci86d106e2018-10-18 09:53:19 +0200356ly_mmap(struct ly_ctx *ctx, int fd, size_t *length, void **addr)
Michal Vasko841d1a92018-09-07 15:40:31 +0200357{
Radek Krejci86d106e2018-10-18 09:53:19 +0200358 struct stat sb;
359 long pagesize;
360 size_t m;
Michal Vasko841d1a92018-09-07 15:40:31 +0200361
Radek Krejci86d106e2018-10-18 09:53:19 +0200362 assert(length);
363 assert(addr);
364 assert(fd >= 0);
Michal Vasko841d1a92018-09-07 15:40:31 +0200365
Radek Krejci86d106e2018-10-18 09:53:19 +0200366 if (fstat(fd, &sb) == -1) {
367 LOGERR(ctx, LY_ESYS, "Failed to stat the file descriptor (%s) for the mmap().", strerror(errno));
368 return LY_ESYS;
Michal Vasko841d1a92018-09-07 15:40:31 +0200369 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200370 if (!S_ISREG(sb.st_mode)) {
371 LOGERR(ctx, LY_EINVAL, "File to mmap() is not a regular file.");
372 return LY_ESYS;
Michal Vasko841d1a92018-09-07 15:40:31 +0200373 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200374 if (!sb.st_size) {
375 *addr = NULL;
376 return LY_SUCCESS;
377 }
378 pagesize = sysconf(_SC_PAGESIZE);
379
380 m = sb.st_size % pagesize;
Michal Vasko69730152020-10-09 16:30:07 +0200381 if (m && (pagesize - m >= 1)) {
Radek Krejci86d106e2018-10-18 09:53:19 +0200382 /* there will be enough space (at least 1 byte) after the file content mapping to provide zeroed NULL-termination byte */
383 *length = sb.st_size + 1;
384 *addr = mmap(NULL, *length, PROT_READ, MAP_PRIVATE, fd, 0);
385 } else {
386 /* there will not be enough bytes after the file content mapping for the additional bytes and some of them
387 * would overflow into another page that would not be zerroed and any access into it would generate SIGBUS.
388 * Therefore we have to do the following hack with double mapping. First, the required number of bytes
389 * (including the additinal bytes) is required as anonymous and thus they will be really provided (actually more
390 * because of using whole pages) and also initialized by zeros. Then, the file is mapped to the same address
391 * where the anonymous mapping starts. */
392 *length = sb.st_size + pagesize;
393 *addr = mmap(NULL, *length, PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
394 *addr = mmap(*addr, sb.st_size, PROT_READ, MAP_PRIVATE | MAP_FIXED, fd, 0);
395 }
396 if (*addr == MAP_FAILED) {
397 LOGERR(ctx, LY_ESYS, "mmap() failed (%s).", strerror(errno));
398 return LY_ESYS;
Michal Vasko841d1a92018-09-07 15:40:31 +0200399 }
400
Radek Krejcif345c012018-09-19 11:12:59 +0200401 return LY_SUCCESS;
Radek Krejci86d106e2018-10-18 09:53:19 +0200402}
Michal Vasko841d1a92018-09-07 15:40:31 +0200403
Radek Krejci86d106e2018-10-18 09:53:19 +0200404LY_ERR
405ly_munmap(void *addr, size_t length)
406{
407 if (munmap(addr, length)) {
408 return LY_ESYS;
409 }
410 return LY_SUCCESS;
Michal Vasko841d1a92018-09-07 15:40:31 +0200411}
Radek Krejci4f28eda2018-11-12 11:46:16 +0100412
Jan Kundrátf1960dc2021-12-12 03:12:23 +0100413#else
414
415LY_ERR
416ly_mmap(struct ly_ctx *ctx, int fd, size_t *length, void **addr)
417{
418 struct stat sb;
419 size_t m;
420
421 assert(length);
422 assert(addr);
423 assert(fd >= 0);
424
Jan Kundrátd31adc12022-07-07 21:36:15 +0200425#if _WIN32
426 if (_setmode(fd, _O_BINARY) == -1) {
427 LOGERR(ctx, LY_ESYS, "Failed to switch the file descriptor to binary mode.", strerror(errno));
428 return LY_ESYS;
429 }
430#endif
431
Jan Kundrátf1960dc2021-12-12 03:12:23 +0100432 if (fstat(fd, &sb) == -1) {
433 LOGERR(ctx, LY_ESYS, "Failed to stat the file descriptor (%s) for the mmap().", strerror(errno));
434 return LY_ESYS;
435 }
436 if (!S_ISREG(sb.st_mode)) {
437 LOGERR(ctx, LY_EINVAL, "File to mmap() is not a regular file.");
438 return LY_ESYS;
439 }
440 if (!sb.st_size) {
441 *addr = NULL;
442 return LY_SUCCESS;
443 }
444 /* On Windows, the mman-win32 mmap() emulation uses CreateFileMapping and MapViewOfFile, and these functions
445 * do not allow mapping more than "length of file" bytes for PROT_READ. Remapping existing mappings is not allowed, either.
446 * At that point the path of least resistance is just reading the file in as-is. */
447 m = sb.st_size + 1;
448 char *buf = calloc(m, 1);
449
450 if (!buf) {
451 LOGERR(ctx, LY_ESYS, "ly_mmap: malloc() failed (%s).", strerror(errno));
452 }
453 *addr = buf;
454 *length = m;
455
456 lseek(fd, 0, SEEK_SET);
457 ssize_t to_read = m - 1;
458
459 while (to_read > 0) {
460 ssize_t n = read(fd, buf, to_read);
Michal Vasko2bf4af42023-01-04 12:08:38 +0100461
Jan Kundrátf1960dc2021-12-12 03:12:23 +0100462 if (n == 0) {
463 return LY_SUCCESS;
464 } else if (n < 0) {
465 if (errno == EINTR) {
466 continue; // can I get this on Windows?
467 }
468 LOGERR(ctx, LY_ESYS, "ly_mmap: read() failed (%s).", strerror(errno));
469 }
470 to_read -= n;
471 buf += n;
472 }
473 return LY_SUCCESS;
474}
475
476LY_ERR
477ly_munmap(void *addr, size_t length)
478{
479 (void)length;
480 free(addr);
481 return LY_SUCCESS;
482}
483
484#endif
485
Radek Krejci4f28eda2018-11-12 11:46:16 +0100486LY_ERR
Radek Krejci4546aa62019-07-15 16:53:32 +0200487ly_strcat(char **dest, const char *format, ...)
488{
489 va_list fp;
490 char *addition = NULL;
491 size_t len;
492
493 va_start(fp, format);
494 len = vasprintf(&addition, format, fp);
495 len += (*dest ? strlen(*dest) : 0) + 1;
496
497 if (*dest) {
498 *dest = ly_realloc(*dest, len);
499 if (!*dest) {
Radek Krejci1cd812f2020-12-01 12:17:53 +0100500 va_end(fp);
Radek Krejci4546aa62019-07-15 16:53:32 +0200501 return LY_EMEM;
502 }
503 *dest = strcat(*dest, addition);
504 free(addition);
505 } else {
506 *dest = addition;
507 }
508
509 va_end(fp);
510 return LY_SUCCESS;
511}
512
513LY_ERR
Radek Krejci249973a2019-06-10 10:50:54 +0200514ly_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 +0100515{
Michal Vaskob4d40d62021-05-04 11:42:44 +0200516 LY_ERR rc = LY_SUCCESS;
517 char *ptr, *str;
Radek Krejci9ea8ca12019-06-10 13:11:55 +0200518 int64_t i;
Radek Krejci4f28eda2018-11-12 11:46:16 +0100519
Radek Krejci249973a2019-06-10 10:50:54 +0200520 LY_CHECK_ARG_RET(NULL, val_str, val_str[0], val_len, LY_EINVAL);
Radek Krejci4f28eda2018-11-12 11:46:16 +0100521
Michal Vaskob4d40d62021-05-04 11:42:44 +0200522 /* duplicate the value */
523 str = strndup(val_str, val_len);
524 LY_CHECK_RET(!str, LY_EMEM);
Radek Krejci4f28eda2018-11-12 11:46:16 +0100525
Michal Vaskob4d40d62021-05-04 11:42:44 +0200526 /* parse the value to avoid accessing following bytes */
527 errno = 0;
528 i = strtoll(str, &ptr, base);
529 if (errno || (ptr == str)) {
530 /* invalid string */
531 rc = LY_EVALID;
Radek Krejci9ea8ca12019-06-10 13:11:55 +0200532 } else if ((i < min) || (i > max)) {
Michal Vaskob4d40d62021-05-04 11:42:44 +0200533 /* invalid number */
534 rc = LY_EDENIED;
535 } else if (*ptr) {
536 while (isspace(*ptr)) {
537 ++ptr;
Radek Krejci4f28eda2018-11-12 11:46:16 +0100538 }
Michal Vaskob4d40d62021-05-04 11:42:44 +0200539 if (*ptr) {
540 /* invalid characters after some number */
541 rc = LY_EVALID;
Radek Krejci4f28eda2018-11-12 11:46:16 +0100542 }
543 }
Radek Krejci9ea8ca12019-06-10 13:11:55 +0200544
Michal Vaskob4d40d62021-05-04 11:42:44 +0200545 /* cleanup */
546 free(str);
547 if (!rc) {
548 *ret = i;
549 }
550 return rc;
Radek Krejci4f28eda2018-11-12 11:46:16 +0100551}
552
553LY_ERR
Radek Krejci249973a2019-06-10 10:50:54 +0200554ly_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 +0100555{
Michal Vaskob4d40d62021-05-04 11:42:44 +0200556 LY_ERR rc = LY_SUCCESS;
557 char *ptr, *str;
Radek Krejci4f28eda2018-11-12 11:46:16 +0100558 uint64_t u;
559
Michal Vaskob4d40d62021-05-04 11:42:44 +0200560 LY_CHECK_ARG_RET(NULL, val_str, val_str[0], val_len, LY_EINVAL);
Radek Krejci4f28eda2018-11-12 11:46:16 +0100561
Michal Vaskob4d40d62021-05-04 11:42:44 +0200562 /* duplicate the value to avoid accessing following bytes */
563 str = strndup(val_str, val_len);
564 LY_CHECK_RET(!str, LY_EMEM);
565
566 /* parse the value */
Radek Krejci4f28eda2018-11-12 11:46:16 +0100567 errno = 0;
Michal Vaskob4d40d62021-05-04 11:42:44 +0200568 u = strtoull(str, &ptr, base);
569 if (errno || (ptr == str)) {
570 /* invalid string */
571 rc = LY_EVALID;
572 } else if ((u > max) || (u && (str[0] == '-'))) {
573 /* invalid number */
574 rc = LY_EDENIED;
575 } else if (*ptr) {
576 while (isspace(*ptr)) {
577 ++ptr;
Radek Krejci4f28eda2018-11-12 11:46:16 +0100578 }
Michal Vaskob4d40d62021-05-04 11:42:44 +0200579 if (*ptr) {
580 /* invalid characters after some number */
581 rc = LY_EVALID;
Radek Krejci4f28eda2018-11-12 11:46:16 +0100582 }
583 }
584
Michal Vaskob4d40d62021-05-04 11:42:44 +0200585 /* cleanup */
586 free(str);
587 if (!rc) {
588 *ret = u;
589 }
590 return rc;
Radek Krejci4f28eda2018-11-12 11:46:16 +0100591}
Radek Krejcib4a4a272019-06-10 12:44:52 +0200592
593/**
594 * @brief Parse an identifier.
595 *
596 * ;; An identifier MUST NOT start with (('X'|'x') ('M'|'m') ('L'|'l'))
597 * identifier = (ALPHA / "_")
598 * *(ALPHA / DIGIT / "_" / "-" / ".")
599 *
600 * @param[in,out] id Identifier to parse. When returned, it points to the first character which is not part of the identifier.
601 * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid starting character.
602 */
603static LY_ERR
604lys_parse_id(const char **id)
605{
606 assert(id && *id);
607
608 if (!is_yangidentstartchar(**id)) {
609 return LY_EINVAL;
610 }
611 ++(*id);
612
613 while (is_yangidentchar(**id)) {
614 ++(*id);
615 }
616 return LY_SUCCESS;
617}
618
619LY_ERR
620ly_parse_nodeid(const char **id, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len)
621{
622 assert(id && *id);
623 assert(prefix && prefix_len);
624 assert(name && name_len);
625
626 *prefix = *id;
627 *prefix_len = 0;
628 *name = NULL;
629 *name_len = 0;
630
631 LY_CHECK_RET(lys_parse_id(id));
632 if (**id == ':') {
633 /* there is prefix */
634 *prefix_len = *id - *prefix;
635 ++(*id);
636 *name = *id;
637
638 LY_CHECK_RET(lys_parse_id(id));
639 *name_len = *id - *name;
640 } else {
641 /* there is no prefix, so what we have as prefix now is actually the name */
642 *name = *prefix;
643 *name_len = *id - *name;
644 *prefix = NULL;
645 }
646
647 return LY_SUCCESS;
648}
649
650LY_ERR
Radek Krejci084289f2019-07-09 17:35:30 +0200651ly_parse_instance_predicate(const char **pred, size_t limit, LYD_FORMAT format,
Radek Krejci0f969882020-08-21 16:56:47 +0200652 const char **prefix, size_t *prefix_len, const char **id, size_t *id_len, const char **value, size_t *value_len,
653 const char **errmsg)
Radek Krejcib4a4a272019-06-10 12:44:52 +0200654{
655 LY_ERR ret = LY_EVALID;
656 const char *in = *pred;
657 size_t offset = 1;
Radek Krejci857189e2020-09-01 13:26:36 +0200658 uint8_t expr = 0; /* 0 - position predicate; 1 - leaf-list-predicate; 2 - key-predicate */
Radek Krejcib4a4a272019-06-10 12:44:52 +0200659 char quot;
660
Radek Krejci4607f542020-12-01 12:18:49 +0100661 assert(in[0] == '[');
Radek Krejcib4a4a272019-06-10 12:44:52 +0200662
663 *prefix = *id = *value = NULL;
664 *prefix_len = *id_len = *value_len = 0;
665
666 /* leading *WSP */
Michal Vaskod989ba02020-08-24 10:59:24 +0200667 for ( ; isspace(in[offset]); offset++) {}
Radek Krejcib4a4a272019-06-10 12:44:52 +0200668
669 if (isdigit(in[offset])) {
670 /* pos: "[" *WSP positive-integer-value *WSP "]" */
671 if (in[offset] == '0') {
672 /* zero */
673 *errmsg = "The position predicate cannot be zero.";
674 goto error;
675 }
676
677 /* positive-integer-value */
Radek Krejci10bfdf82019-06-10 14:08:13 +0200678 *value = &in[offset++];
Michal Vaskod989ba02020-08-24 10:59:24 +0200679 for ( ; isdigit(in[offset]); offset++) {}
Radek Krejci10bfdf82019-06-10 14:08:13 +0200680 *value_len = &in[offset] - *value;
Radek Krejcib4a4a272019-06-10 12:44:52 +0200681
682 } else if (in[offset] == '.') {
683 /* leaf-list-predicate: "[" *WSP "." *WSP "=" *WSP quoted-string *WSP "]" */
684 *id = &in[offset];
685 *id_len = 1;
686 offset++;
687 expr = 1;
Radek Krejci10bfdf82019-06-10 14:08:13 +0200688 } else if (in[offset] == '-') {
689 /* typically negative value */
690 *errmsg = "Invalid instance predicate format (negative position or invalid node-identifier).";
691 goto error;
Radek Krejcib4a4a272019-06-10 12:44:52 +0200692 } else {
693 /* key-predicate: "[" *WSP node-identifier *WSP "=" *WSP quoted-string *WSP "]" */
694 in = &in[offset];
695 if (ly_parse_nodeid(&in, prefix, prefix_len, id, id_len)) {
696 *errmsg = "Invalid node-identifier.";
697 goto error;
698 }
Michal Vasko69730152020-10-09 16:30:07 +0200699 if ((format == LYD_XML) && !(*prefix)) {
Radek Krejci084289f2019-07-09 17:35:30 +0200700 /* all node names MUST be qualified with explicit namespace prefix */
701 *errmsg = "Missing prefix of a node name.";
702 goto error;
703 }
Radek Krejcib4a4a272019-06-10 12:44:52 +0200704 offset = in - *pred;
705 in = *pred;
Radek Krejci10bfdf82019-06-10 14:08:13 +0200706 expr = 2;
Radek Krejcib4a4a272019-06-10 12:44:52 +0200707 }
708
709 if (expr) {
710 /* *WSP "=" *WSP quoted-string *WSP "]" */
Michal Vaskod989ba02020-08-24 10:59:24 +0200711 for ( ; isspace(in[offset]); offset++) {}
Radek Krejcib4a4a272019-06-10 12:44:52 +0200712
713 if (in[offset] != '=') {
Radek Krejci10bfdf82019-06-10 14:08:13 +0200714 if (expr == 1) {
715 *errmsg = "Unexpected character instead of \'=\' in leaf-list-predicate.";
716 } else { /* 2 */
717 *errmsg = "Unexpected character instead of \'=\' in key-predicate.";
718 }
Radek Krejcib4a4a272019-06-10 12:44:52 +0200719 goto error;
720 }
721 offset++;
Michal Vaskod989ba02020-08-24 10:59:24 +0200722 for ( ; isspace(in[offset]); offset++) {}
Radek Krejcib4a4a272019-06-10 12:44:52 +0200723
724 /* quoted-string */
725 quot = in[offset++];
Michal Vasko69730152020-10-09 16:30:07 +0200726 if ((quot != '\'') && (quot != '\"')) {
Radek Krejcib4a4a272019-06-10 12:44:52 +0200727 *errmsg = "String value is not quoted.";
728 goto error;
729 }
730 *value = &in[offset];
Michal Vaskod989ba02020-08-24 10:59:24 +0200731 for ( ; offset < limit && (in[offset] != quot || (offset && in[offset - 1] == '\\')); offset++) {}
Radek Krejci10bfdf82019-06-10 14:08:13 +0200732 if (in[offset] == quot) {
733 *value_len = &in[offset] - *value;
734 offset++;
735 } else {
736 *errmsg = "Value is not terminated quoted-string.";
737 goto error;
738 }
Radek Krejcib4a4a272019-06-10 12:44:52 +0200739 }
740
741 /* *WSP "]" */
Michal Vaskod989ba02020-08-24 10:59:24 +0200742 for ( ; isspace(in[offset]); offset++) {}
Radek Krejcib4a4a272019-06-10 12:44:52 +0200743 if (in[offset] != ']') {
Radek Krejci10bfdf82019-06-10 14:08:13 +0200744 if (expr == 0) {
745 *errmsg = "Predicate (pos) is not terminated by \']\' character.";
746 } else if (expr == 1) {
747 *errmsg = "Predicate (leaf-list-predicate) is not terminated by \']\' character.";
748 } else { /* 2 */
749 *errmsg = "Predicate (key-predicate) is not terminated by \']\' character.";
750 }
Radek Krejcib4a4a272019-06-10 12:44:52 +0200751 goto error;
752 }
Radek Krejci10bfdf82019-06-10 14:08:13 +0200753 offset++;
Radek Krejcib4a4a272019-06-10 12:44:52 +0200754
Radek Krejci10bfdf82019-06-10 14:08:13 +0200755 if (offset <= limit) {
756 *pred = &in[offset];
Radek Krejcib4a4a272019-06-10 12:44:52 +0200757 return LY_SUCCESS;
758 }
759
760 /* we read after the limit */
761 *errmsg = "Predicate is incomplete.";
762 *prefix = *id = *value = NULL;
763 *prefix_len = *id_len = *value_len = 0;
764 offset = limit;
765 ret = LY_EINVAL;
766
767error:
768 *pred = &in[offset];
769 return ret;
770}