Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1 | /** |
Michal Vasko | afac782 | 2020-10-20 14:22:26 +0200 | [diff] [blame] | 2 | * @file in.c |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
Michal Vasko | afac782 | 2020-10-20 14:22:26 +0200 | [diff] [blame] | 4 | * @brief libyang input functions. |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 5 | * |
Michal Vasko | afac782 | 2020-10-20 14:22:26 +0200 | [diff] [blame] | 6 | * Copyright (c) 2015 - 2020 CESNET, z.s.p.o. |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 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 | */ |
| 14 | |
| 15 | #define _GNU_SOURCE |
Radek Krejci | f8dc59a | 2020-11-25 13:47:44 +0100 | [diff] [blame] | 16 | #define _POSIX_C_SOURCE 200809L /* strdup, strndup */ |
| 17 | |
| 18 | #ifdef __APPLE__ |
| 19 | #define _DARWIN_C_SOURCE /* F_GETPATH */ |
| 20 | #endif |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 21 | |
Radek Krejci | d43298b | 2021-03-25 16:17:15 +0100 | [diff] [blame] | 22 | #if defined (__NetBSD__) || defined (__OpenBSD__) |
Christian Hopps | 6f32621 | 2021-03-23 12:37:29 -0400 | [diff] [blame] | 23 | /* realpath */ |
Christian Hopps | f9b8741 | 2021-04-12 16:56:15 -0400 | [diff] [blame] | 24 | #define _XOPEN_SOURCE 1 |
| 25 | #define _XOPEN_SOURCE_EXTENDED 1 |
Christian Hopps | 6f32621 | 2021-03-23 12:37:29 -0400 | [diff] [blame] | 26 | #endif |
| 27 | |
Michal Vasko | afac782 | 2020-10-20 14:22:26 +0200 | [diff] [blame] | 28 | #include "in.h" |
| 29 | #include "in_internal.h" |
Radek Krejci | ca376bd | 2020-06-11 16:04:06 +0200 | [diff] [blame] | 30 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 31 | #include <errno.h> |
| 32 | #include <fcntl.h> |
| 33 | #include <limits.h> |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 34 | #include <stdint.h> |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 35 | #include <stdio.h> |
Radek Krejci | ca376bd | 2020-06-11 16:04:06 +0200 | [diff] [blame] | 36 | #include <stdlib.h> |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 37 | #include <string.h> |
Radek Krejci | ca376bd | 2020-06-11 16:04:06 +0200 | [diff] [blame] | 38 | #include <unistd.h> |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 39 | |
Radek Krejci | ca376bd | 2020-06-11 16:04:06 +0200 | [diff] [blame] | 40 | #include "common.h" |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 41 | #include "compat.h" |
Radek Krejci | ca376bd | 2020-06-11 16:04:06 +0200 | [diff] [blame] | 42 | #include "dict.h" |
| 43 | #include "log.h" |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 44 | #include "parser_data.h" |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 45 | #include "parser_internal.h" |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 46 | #include "set.h" |
Radek Krejci | 7711410 | 2021-03-10 15:21:57 +0100 | [diff] [blame] | 47 | #include "tree.h" |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 48 | #include "tree_data.h" |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 49 | #include "tree_data_internal.h" |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 50 | #include "tree_schema.h" |
Radek Krejci | ca376bd | 2020-06-11 16:04:06 +0200 | [diff] [blame] | 51 | #include "tree_schema_internal.h" |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 52 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 53 | LIBYANG_API_DEF LY_IN_TYPE |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 54 | ly_in_type(const struct ly_in *in) |
| 55 | { |
| 56 | LY_CHECK_ARG_RET(NULL, in, LY_IN_ERROR); |
| 57 | return in->type; |
| 58 | } |
| 59 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 60 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame^] | 61 | ly_in_reset(struct ly_in *in) |
| 62 | { |
| 63 | LY_CHECK_ARG_RET(NULL, in, LY_EINVAL); |
| 64 | |
| 65 | in->current = in->func_start = in->start; |
| 66 | in->line = 1; |
| 67 | return LY_SUCCESS; |
| 68 | } |
| 69 | |
| 70 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 71 | ly_in_new_fd(int fd, struct ly_in **in) |
| 72 | { |
| 73 | size_t length; |
| 74 | char *addr; |
| 75 | |
| 76 | LY_CHECK_ARG_RET(NULL, fd >= 0, in, LY_EINVAL); |
| 77 | |
| 78 | LY_CHECK_RET(ly_mmap(NULL, fd, &length, (void **)&addr)); |
| 79 | if (!addr) { |
| 80 | LOGERR(NULL, LY_EINVAL, "Empty input file."); |
| 81 | return LY_EINVAL; |
| 82 | } |
| 83 | |
| 84 | *in = calloc(1, sizeof **in); |
| 85 | LY_CHECK_ERR_RET(!*in, LOGMEM(NULL); ly_munmap(addr, length), LY_EMEM); |
| 86 | |
| 87 | (*in)->type = LY_IN_FD; |
| 88 | (*in)->method.fd = fd; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 89 | (*in)->current = (*in)->start = (*in)->func_start = addr; |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 90 | (*in)->line = 1; |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 91 | (*in)->length = length; |
| 92 | |
| 93 | return LY_SUCCESS; |
| 94 | } |
| 95 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 96 | LIBYANG_API_DEF int |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 97 | ly_in_fd(struct ly_in *in, int fd) |
| 98 | { |
| 99 | int prev_fd; |
| 100 | size_t length; |
| 101 | const char *addr; |
| 102 | |
| 103 | LY_CHECK_ARG_RET(NULL, in, in->type == LY_IN_FD, -1); |
| 104 | |
| 105 | prev_fd = in->method.fd; |
| 106 | |
| 107 | if (fd != -1) { |
| 108 | LY_CHECK_RET(ly_mmap(NULL, fd, &length, (void **)&addr), -1); |
| 109 | if (!addr) { |
| 110 | LOGERR(NULL, LY_EINVAL, "Empty input file."); |
| 111 | return -1; |
| 112 | } |
| 113 | |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 114 | ly_munmap((char *)in->start, in->length); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 115 | |
| 116 | in->method.fd = fd; |
| 117 | in->current = in->start = addr; |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 118 | in->line = 1; |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 119 | in->length = length; |
| 120 | } |
| 121 | |
| 122 | return prev_fd; |
| 123 | } |
| 124 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 125 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 126 | ly_in_new_file(FILE *f, struct ly_in **in) |
| 127 | { |
| 128 | LY_CHECK_ARG_RET(NULL, f, in, LY_EINVAL); |
| 129 | |
| 130 | LY_CHECK_RET(ly_in_new_fd(fileno(f), in)); |
| 131 | |
| 132 | /* convert the LY_IN_FD input handler into the LY_IN_FILE */ |
| 133 | (*in)->type = LY_IN_FILE; |
| 134 | (*in)->method.f = f; |
| 135 | |
| 136 | return LY_SUCCESS; |
| 137 | } |
| 138 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 139 | LIBYANG_API_DEF FILE * |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 140 | ly_in_file(struct ly_in *in, FILE *f) |
| 141 | { |
| 142 | FILE *prev_f; |
| 143 | |
| 144 | LY_CHECK_ARG_RET(NULL, in, in->type == LY_IN_FILE, NULL); |
| 145 | |
| 146 | prev_f = in->method.f; |
| 147 | |
| 148 | if (f) { |
| 149 | /* convert LY_IN_FILE handler into LY_IN_FD to be able to update it via ly_in_fd() */ |
| 150 | in->type = LY_IN_FD; |
| 151 | in->method.fd = fileno(prev_f); |
| 152 | if (ly_in_fd(in, fileno(f)) == -1) { |
| 153 | in->type = LY_IN_FILE; |
| 154 | in->method.f = prev_f; |
| 155 | return NULL; |
| 156 | } |
| 157 | |
| 158 | /* if success, convert the result back */ |
| 159 | in->type = LY_IN_FILE; |
| 160 | in->method.f = f; |
| 161 | } |
| 162 | |
| 163 | return prev_f; |
| 164 | } |
| 165 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 166 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 167 | ly_in_new_memory(const char *str, struct ly_in **in) |
| 168 | { |
| 169 | LY_CHECK_ARG_RET(NULL, str, in, LY_EINVAL); |
| 170 | |
| 171 | *in = calloc(1, sizeof **in); |
| 172 | LY_CHECK_ERR_RET(!*in, LOGMEM(NULL), LY_EMEM); |
| 173 | |
| 174 | (*in)->type = LY_IN_MEMORY; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 175 | (*in)->start = (*in)->current = (*in)->func_start = str; |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 176 | (*in)->line = 1; |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 177 | |
| 178 | return LY_SUCCESS; |
| 179 | } |
| 180 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 181 | LIBYANG_API_DEF const char * |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 182 | ly_in_memory(struct ly_in *in, const char *str) |
| 183 | { |
| 184 | const char *data; |
| 185 | |
| 186 | LY_CHECK_ARG_RET(NULL, in, in->type == LY_IN_MEMORY, NULL); |
| 187 | |
| 188 | data = in->current; |
| 189 | |
| 190 | if (str) { |
| 191 | in->start = in->current = str; |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 192 | in->line = 1; |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | return data; |
| 196 | } |
| 197 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 198 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 199 | ly_in_new_filepath(const char *filepath, size_t len, struct ly_in **in) |
| 200 | { |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 201 | LY_ERR ret; |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 202 | char *fp; |
| 203 | int fd; |
| 204 | |
| 205 | LY_CHECK_ARG_RET(NULL, filepath, in, LY_EINVAL); |
| 206 | |
| 207 | if (len) { |
| 208 | fp = strndup(filepath, len); |
| 209 | } else { |
| 210 | fp = strdup(filepath); |
| 211 | } |
| 212 | |
| 213 | fd = open(fp, O_RDONLY); |
Michal Vasko | f2eb8af | 2020-07-14 12:22:40 +0200 | [diff] [blame] | 214 | LY_CHECK_ERR_RET(fd == -1, LOGERR(NULL, LY_ESYS, "Failed to open file \"%s\" (%s).", fp, strerror(errno)); free(fp), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 215 | LY_ESYS); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 216 | |
| 217 | LY_CHECK_ERR_RET(ret = ly_in_new_fd(fd, in), free(fp), ret); |
| 218 | |
| 219 | /* convert the LY_IN_FD input handler into the LY_IN_FILE */ |
| 220 | (*in)->type = LY_IN_FILEPATH; |
| 221 | (*in)->method.fpath.fd = fd; |
| 222 | (*in)->method.fpath.filepath = fp; |
| 223 | |
| 224 | return LY_SUCCESS; |
| 225 | } |
| 226 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 227 | LIBYANG_API_DEF const char * |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 228 | ly_in_filepath(struct ly_in *in, const char *filepath, size_t len) |
| 229 | { |
| 230 | int fd, prev_fd; |
| 231 | char *fp = NULL; |
| 232 | |
| 233 | LY_CHECK_ARG_RET(NULL, in, in->type == LY_IN_FILEPATH, filepath ? NULL : ((void *)-1)); |
| 234 | |
| 235 | if (!filepath) { |
| 236 | return in->method.fpath.filepath; |
| 237 | } |
| 238 | |
| 239 | if (len) { |
| 240 | fp = strndup(filepath, len); |
| 241 | } else { |
| 242 | fp = strdup(filepath); |
| 243 | } |
| 244 | |
| 245 | /* replace filepath */ |
| 246 | fd = open(fp, O_RDONLY); |
| 247 | LY_CHECK_ERR_RET(!fd, LOGERR(NULL, LY_ESYS, "Failed to open file \"%s\" (%s).", fp, strerror(errno)); free(fp), NULL); |
| 248 | |
| 249 | /* convert LY_IN_FILEPATH handler into LY_IN_FD to be able to update it via ly_in_fd() */ |
| 250 | in->type = LY_IN_FD; |
| 251 | prev_fd = ly_in_fd(in, fd); |
| 252 | LY_CHECK_ERR_RET(prev_fd == -1, in->type = LY_IN_FILEPATH; free(fp), NULL); |
| 253 | |
| 254 | /* and convert the result back */ |
| 255 | in->type = LY_IN_FILEPATH; |
| 256 | close(prev_fd); |
| 257 | free(in->method.fpath.filepath); |
| 258 | in->method.fpath.fd = fd; |
| 259 | in->method.fpath.filepath = fp; |
| 260 | |
| 261 | return NULL; |
| 262 | } |
| 263 | |
| 264 | void |
| 265 | lys_parser_fill_filepath(struct ly_ctx *ctx, struct ly_in *in, const char **filepath) |
| 266 | { |
| 267 | char path[PATH_MAX]; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 268 | |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 269 | #ifndef __APPLE__ |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 270 | char proc_path[32]; |
| 271 | int len; |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 272 | #endif |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 273 | |
| 274 | LY_CHECK_ARG_RET(NULL, ctx, in, filepath, ); |
| 275 | if (*filepath) { |
| 276 | /* filepath already set */ |
| 277 | return; |
| 278 | } |
| 279 | |
| 280 | switch (in->type) { |
| 281 | case LY_IN_FILEPATH: |
| 282 | if (realpath(in->method.fpath.filepath, path) != NULL) { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 283 | lydict_insert(ctx, path, 0, filepath); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 284 | } else { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 285 | lydict_insert(ctx, in->method.fpath.filepath, 0, filepath); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | break; |
| 289 | case LY_IN_FD: |
| 290 | #ifdef __APPLE__ |
| 291 | if (fcntl(in->method.fd, F_GETPATH, path) != -1) { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 292 | lydict_insert(ctx, path, 0, filepath); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 293 | } |
Jan Kundrát | ca643df | 2021-12-10 16:47:27 +0100 | [diff] [blame] | 294 | #elif defined _WIN32 |
| 295 | HANDLE h = _get_osfhandle(in->method.fd); |
| 296 | FILE_NAME_INFO info; |
| 297 | if (GetFileInformationByHandleEx(h, FileNameInfo, &info, sizeof info)) { |
| 298 | char *buf = calloc(info.FileNameLength + 1 /* trailing NULL */, MB_CUR_MAX); |
| 299 | len = wcstombs(buf, info.FileName, info.FileNameLength * MB_CUR_MAX); |
| 300 | lydict_insert(ctx, buf, len, filepath); |
| 301 | } |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 302 | #else |
| 303 | /* get URI if there is /proc */ |
| 304 | sprintf(proc_path, "/proc/self/fd/%d", in->method.fd); |
| 305 | if ((len = readlink(proc_path, path, PATH_MAX - 1)) > 0) { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 306 | lydict_insert(ctx, path, len, filepath); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 307 | } |
| 308 | #endif |
| 309 | break; |
| 310 | case LY_IN_MEMORY: |
| 311 | case LY_IN_FILE: |
| 312 | /* nothing to do */ |
| 313 | break; |
| 314 | default: |
| 315 | LOGINT(ctx); |
| 316 | break; |
| 317 | } |
| 318 | |
| 319 | } |
| 320 | |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame^] | 321 | LIBYANG_API_DEF size_t |
| 322 | ly_in_parsed(const struct ly_in *in) |
| 323 | { |
| 324 | return in->current - in->func_start; |
| 325 | } |
| 326 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 327 | LIBYANG_API_DEF void |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 328 | ly_in_free(struct ly_in *in, ly_bool destroy) |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 329 | { |
| 330 | if (!in) { |
| 331 | return; |
| 332 | } else if (in->type == LY_IN_ERROR) { |
| 333 | LOGINT(NULL); |
| 334 | return; |
| 335 | } |
| 336 | |
| 337 | if (destroy) { |
| 338 | if (in->type == LY_IN_MEMORY) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 339 | free((char *)in->start); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 340 | } else { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 341 | ly_munmap((char *)in->start, in->length); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 342 | |
| 343 | if (in->type == LY_IN_FILE) { |
| 344 | fclose(in->method.f); |
| 345 | } else { |
| 346 | close(in->method.fd); |
| 347 | |
| 348 | if (in->type == LY_IN_FILEPATH) { |
| 349 | free(in->method.fpath.filepath); |
| 350 | } |
| 351 | } |
| 352 | } |
| 353 | } else if (in->type != LY_IN_MEMORY) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 354 | ly_munmap((char *)in->start, in->length); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 355 | |
| 356 | if (in->type == LY_IN_FILEPATH) { |
| 357 | close(in->method.fpath.fd); |
| 358 | free(in->method.fpath.filepath); |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | free(in); |
| 363 | } |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 364 | |
| 365 | LY_ERR |
| 366 | ly_in_read(struct ly_in *in, void *buf, size_t count) |
| 367 | { |
| 368 | if (in->length && (in->length - (in->current - in->start) < count)) { |
| 369 | /* EOF */ |
| 370 | return LY_EDENIED; |
| 371 | } |
| 372 | |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 373 | if (count) { |
| 374 | memcpy(buf, in->current, count); |
| 375 | } |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 376 | in->current += count; |
| 377 | return LY_SUCCESS; |
| 378 | } |
| 379 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 380 | LY_ERR |
| 381 | ly_in_skip(struct ly_in *in, size_t count) |
| 382 | { |
| 383 | if (in->length && (in->length - (in->current - in->start) < count)) { |
| 384 | /* EOF */ |
| 385 | return LY_EDENIED; |
| 386 | } |
| 387 | |
| 388 | in->current += count; |
| 389 | return LY_SUCCESS; |
| 390 | } |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 391 | |
| 392 | void |
| 393 | lyd_ctx_free(struct lyd_ctx *lydctx) |
| 394 | { |
Michal Vasko | 3271138 | 2020-12-03 14:14:31 +0100 | [diff] [blame] | 395 | ly_set_erase(&lydctx->node_types, NULL); |
| 396 | ly_set_erase(&lydctx->meta_types, NULL); |
| 397 | ly_set_erase(&lydctx->node_when, NULL); |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame^] | 398 | ly_set_erase(&lydctx->ext_val, free); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | LY_ERR |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 402 | lyd_parser_find_operation(const struct lyd_node *parent, uint32_t int_opts, struct lyd_node **op) |
| 403 | { |
| 404 | const struct lyd_node *iter; |
| 405 | |
| 406 | *op = NULL; |
| 407 | |
| 408 | if (!parent) { |
| 409 | /* no parent, nothing to look for */ |
| 410 | return LY_SUCCESS; |
| 411 | } |
| 412 | |
| 413 | /* we need to find the operation node if it already exists */ |
| 414 | for (iter = parent; iter; iter = lyd_parent(iter)) { |
| 415 | if (iter->schema && (iter->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF))) { |
| 416 | break; |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | if (!iter) { |
| 421 | /* no operation found */ |
| 422 | return LY_SUCCESS; |
| 423 | } |
| 424 | |
| 425 | if (!(int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NOTIF | LYD_INTOPT_REPLY))) { |
| 426 | LOGERR(LYD_CTX(parent), LY_EINVAL, "Invalid parent %s \"%s\" node when not parsing any operation.", |
| 427 | lys_nodetype2str(iter->schema->nodetype), iter->schema->name); |
| 428 | return LY_EINVAL; |
| 429 | } else if ((iter->schema->nodetype == LYS_RPC) && !(int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_REPLY))) { |
| 430 | LOGERR(LYD_CTX(parent), LY_EINVAL, "Invalid parent RPC \"%s\" node when not parsing RPC nor rpc-reply.", |
| 431 | iter->schema->name); |
| 432 | return LY_EINVAL; |
| 433 | } else if ((iter->schema->nodetype == LYS_ACTION) && !(int_opts & (LYD_INTOPT_ACTION | LYD_INTOPT_REPLY))) { |
| 434 | LOGERR(LYD_CTX(parent), LY_EINVAL, "Invalid parent action \"%s\" node when not parsing action nor rpc-reply.", |
| 435 | iter->schema->name); |
| 436 | return LY_EINVAL; |
| 437 | } else if ((iter->schema->nodetype == LYS_NOTIF) && !(int_opts & LYD_INTOPT_NOTIF)) { |
| 438 | LOGERR(LYD_CTX(parent), LY_EINVAL, "Invalid parent notification \"%s\" node when not parsing a notification.", |
| 439 | iter->schema->name); |
| 440 | return LY_EINVAL; |
| 441 | } |
| 442 | |
| 443 | *op = (struct lyd_node *)iter; |
| 444 | return LY_SUCCESS; |
| 445 | } |
| 446 | |
| 447 | LY_ERR |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 448 | lyd_parser_check_schema(struct lyd_ctx *lydctx, const struct lysc_node *snode) |
| 449 | { |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 450 | LY_ERR rc = LY_SUCCESS; |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 451 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 452 | LOG_LOCSET(snode, NULL, NULL, NULL); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 453 | |
Michal Vasko | 02ed9d8 | 2021-07-15 14:58:04 +0200 | [diff] [blame] | 454 | if (lydctx->int_opts & LYD_INTOPT_ANY) { |
| 455 | /* nothing to check, everything is allowed */ |
| 456 | goto cleanup; |
| 457 | } |
| 458 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 459 | if ((lydctx->parse_opts & LYD_PARSE_NO_STATE) && (snode->flags & LYS_CONFIG_R)) { |
Michal Vasko | 224e777 | 2021-02-18 14:22:33 +0100 | [diff] [blame] | 460 | LOGVAL(lydctx->data_ctx->ctx, LY_VCODE_UNEXPNODE, "state", snode->name); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 461 | rc = LY_EVALID; |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 462 | goto cleanup; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 463 | } |
| 464 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 465 | if (snode->nodetype == LYS_RPC) { |
Michal Vasko | 2552ea3 | 2020-12-08 15:32:34 +0100 | [diff] [blame] | 466 | if (lydctx->int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_REPLY)) { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 467 | if (lydctx->op_node) { |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 468 | goto error_node_dup; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 469 | } |
| 470 | } else { |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 471 | goto error_node_inval; |
| 472 | } |
| 473 | } else if (snode->nodetype == LYS_ACTION) { |
| 474 | if (lydctx->int_opts & (LYD_INTOPT_ACTION | LYD_INTOPT_REPLY)) { |
| 475 | if (lydctx->op_node) { |
| 476 | goto error_node_dup; |
| 477 | } |
| 478 | } else { |
| 479 | goto error_node_inval; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 480 | } |
| 481 | } else if (snode->nodetype == LYS_NOTIF) { |
| 482 | if (lydctx->int_opts & LYD_INTOPT_NOTIF) { |
| 483 | if (lydctx->op_node) { |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 484 | goto error_node_dup; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 485 | } |
| 486 | } else { |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 487 | goto error_node_inval; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 488 | } |
| 489 | } |
| 490 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 491 | /* success */ |
| 492 | goto cleanup; |
| 493 | |
| 494 | error_node_dup: |
| 495 | LOGVAL(lydctx->data_ctx->ctx, LYVE_DATA, "Unexpected %s element \"%s\", %s \"%s\" already parsed.", |
| 496 | lys_nodetype2str(snode->nodetype), snode->name, lys_nodetype2str(lydctx->op_node->schema->nodetype), |
| 497 | lydctx->op_node->schema->name); |
| 498 | rc = LY_EVALID; |
| 499 | goto cleanup; |
| 500 | |
| 501 | error_node_inval: |
| 502 | LOGVAL(lydctx->data_ctx->ctx, LYVE_DATA, "Unexpected %s element \"%s\".", lys_nodetype2str(snode->nodetype), |
| 503 | snode->name); |
| 504 | rc = LY_EVALID; |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 505 | |
| 506 | cleanup: |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 507 | LOG_LOCBACK(1, 0, 0, 0); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 508 | return rc; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | LY_ERR |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 512 | lyd_parser_create_term(struct lyd_ctx *lydctx, const struct lysc_node *schema, const void *value, size_t value_len, |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 513 | ly_bool *dynamic, LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints, struct lyd_node **node) |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 514 | { |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 515 | ly_bool incomplete; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 516 | |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 517 | LY_CHECK_RET(lyd_create_term(schema, value, value_len, dynamic, format, prefix_data, hints, &incomplete, node)); |
| 518 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 519 | if (incomplete && !(lydctx->parse_opts & LYD_PARSE_ONLY)) { |
Michal Vasko | 3271138 | 2020-12-03 14:14:31 +0100 | [diff] [blame] | 520 | LY_CHECK_RET(ly_set_add(&lydctx->node_types, *node, 1, NULL)); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 521 | } |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 522 | return LY_SUCCESS; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | LY_ERR |
| 526 | lyd_parser_create_meta(struct lyd_ctx *lydctx, struct lyd_node *parent, struct lyd_meta **meta, const struct lys_module *mod, |
Radek Krejci | f994364 | 2021-04-26 10:18:21 +0200 | [diff] [blame] | 527 | const char *name, size_t name_len, const void *value, size_t value_len, ly_bool *dynamic, LY_VALUE_FORMAT format, |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame^] | 528 | void *prefix_data, uint32_t hints, const struct lysc_node *ctx_node) |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 529 | { |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 530 | ly_bool incomplete; |
Michal Vasko | b68571a | 2020-11-06 17:18:41 +0100 | [diff] [blame] | 531 | struct lyd_meta *first = NULL; |
| 532 | |
| 533 | if (meta && *meta) { |
| 534 | /* remember the first metadata */ |
| 535 | first = *meta; |
| 536 | } |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 537 | |
| 538 | LY_CHECK_RET(lyd_create_meta(parent, meta, mod, name, name_len, value, value_len, dynamic, format, prefix_data, |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame^] | 539 | hints, ctx_node, 0, &incomplete)); |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 540 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 541 | if (incomplete && !(lydctx->parse_opts & LYD_PARSE_ONLY)) { |
Michal Vasko | 3271138 | 2020-12-03 14:14:31 +0100 | [diff] [blame] | 542 | LY_CHECK_RET(ly_set_add(&lydctx->meta_types, *meta, 1, NULL)); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 543 | } |
Michal Vasko | b68571a | 2020-11-06 17:18:41 +0100 | [diff] [blame] | 544 | |
| 545 | if (first) { |
| 546 | /* always return the first metadata */ |
| 547 | *meta = first; |
| 548 | } |
| 549 | |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 550 | return LY_SUCCESS; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 551 | } |