blob: f75e57cd628d7e26fa429d908b1bbdfaa66d3308 [file] [log] [blame]
Michal Vasko60ea6352020-06-29 13:39:39 +02001/**
2 * @file parser_lyb.c
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @brief LYB data parser for libyang
5 *
6 * Copyright (c) 2020 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 */
14
15#include "lyb.h"
16
17#include <assert.h>
Radek Krejciad97c5f2020-06-30 09:19:28 +020018#include <stdint.h>
Michal Vasko60ea6352020-06-29 13:39:39 +020019#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22
23#include "common.h"
24#include "compat.h"
Radek Krejciad97c5f2020-06-30 09:19:28 +020025#include "context.h"
26#include "dict.h"
Radek Krejci47fab892020-11-05 17:02:41 +010027#include "hash_table.h"
28#include "in.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020029#include "in_internal.h"
Radek Krejciad97c5f2020-06-30 09:19:28 +020030#include "log.h"
Radek Krejci7931b192020-06-25 17:05:03 +020031#include "parser_data.h"
32#include "parser_internal.h"
Radek Krejciad97c5f2020-06-30 09:19:28 +020033#include "tree.h"
Radek Krejci47fab892020-11-05 17:02:41 +010034#include "tree_data.h"
Michal Vasko60ea6352020-06-29 13:39:39 +020035#include "tree_data_internal.h"
36#include "tree_schema.h"
37#include "validation.h"
Michal Vasko6b5cb2a2020-11-11 19:11:21 +010038#include "xml.h"
Michal Vasko60ea6352020-06-29 13:39:39 +020039
Radek Krejci1798aae2020-07-14 13:26:06 +020040void
41lylyb_ctx_free(struct lylyb_ctx *ctx)
42{
43 LY_ARRAY_COUNT_TYPE u;
44
45 LY_ARRAY_FREE(ctx->subtrees);
46 LY_ARRAY_FREE(ctx->models);
47
48 LY_ARRAY_FOR(ctx->sib_hts, u) {
49 lyht_free(ctx->sib_hts[u].ht);
50 }
51 LY_ARRAY_FREE(ctx->sib_hts);
52
53 free(ctx);
54}
55
56void
57lyd_lyb_ctx_free(struct lyd_ctx *lydctx)
58{
59 struct lyd_lyb_ctx *ctx = (struct lyd_lyb_ctx *)lydctx;
60
61 lyd_ctx_free(lydctx);
62 lylyb_ctx_free(ctx->lybctx);
63 free(ctx);
64}
65
Michal Vasko60ea6352020-06-29 13:39:39 +020066/**
67 * @brief Read YANG data from LYB input. Metadata are handled transparently and not returned.
68 *
69 * @param[in] buf Destination buffer.
70 * @param[in] count Number of bytes to read.
71 * @param[in] lybctx LYB context.
72 */
73static void
Radek Krejci1798aae2020-07-14 13:26:06 +020074lyb_read(uint8_t *buf, size_t count, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +020075{
Michal Vaskofd69e1d2020-07-03 11:57:17 +020076 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +020077 struct lyd_lyb_subtree *empty;
78 size_t to_read;
79 uint8_t meta_buf[LYB_META_BYTES];
80
81 assert(lybctx);
82
83 while (1) {
84 /* check for fully-read (empty) data chunks */
85 to_read = count;
86 empty = NULL;
87 LY_ARRAY_FOR(lybctx->subtrees, u) {
88 /* we want the innermost chunks resolved first, so replace previous empty chunks,
89 * also ignore chunks that are completely finished, there is nothing for us to do */
90 if ((lybctx->subtrees[u].written <= to_read) && lybctx->subtrees[u].position) {
91 /* empty chunk, do not read more */
92 to_read = lybctx->subtrees[u].written;
93 empty = &lybctx->subtrees[u];
94 }
95 }
96
97 if (!empty && !count) {
98 break;
99 }
100
101 /* we are actually reading some data, not just finishing another chunk */
102 if (to_read) {
103 if (buf) {
Michal Vasko63f3d842020-07-08 10:10:14 +0200104 ly_in_read(lybctx->in, buf, to_read);
105 } else {
106 ly_in_skip(lybctx->in, to_read);
Michal Vasko60ea6352020-06-29 13:39:39 +0200107 }
108
109 LY_ARRAY_FOR(lybctx->subtrees, u) {
110 /* decrease all written counters */
111 lybctx->subtrees[u].written -= to_read;
112 assert(lybctx->subtrees[u].written <= LYB_SIZE_MAX);
113 }
114 /* decrease count/buf */
115 count -= to_read;
116 if (buf) {
117 buf += to_read;
118 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200119 }
120
121 if (empty) {
122 /* read the next chunk meta information */
Michal Vasko63f3d842020-07-08 10:10:14 +0200123 ly_in_read(lybctx->in, meta_buf, LYB_META_BYTES);
Michal Vasko60ea6352020-06-29 13:39:39 +0200124 empty->written = meta_buf[0];
125 empty->inner_chunks = meta_buf[1];
126
127 /* remember whether there is a following chunk or not */
128 empty->position = (empty->written == LYB_SIZE_MAX ? 1 : 0);
Michal Vasko60ea6352020-06-29 13:39:39 +0200129 }
130 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200131}
132
133/**
134 * @brief Read a number.
135 *
136 * @param[in] num Destination buffer.
137 * @param[in] num_size Size of @p num.
138 * @param[in] bytes Number of bytes to read.
139 * @param[in] lybctx LYB context.
140 */
141static void
Radek Krejci1798aae2020-07-14 13:26:06 +0200142lyb_read_number(void *num, size_t num_size, size_t bytes, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200143{
144 uint64_t buf = 0;
145
146 lyb_read((uint8_t *)&buf, bytes, lybctx);
147
148 /* correct byte order */
149 buf = le64toh(buf);
150
151 switch (num_size) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100152 case sizeof(uint8_t):
Michal Vasko60ea6352020-06-29 13:39:39 +0200153 *((uint8_t *)num) = buf;
154 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100155 case sizeof(uint16_t):
Michal Vasko60ea6352020-06-29 13:39:39 +0200156 *((uint16_t *)num) = buf;
157 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100158 case sizeof(uint32_t):
Michal Vasko60ea6352020-06-29 13:39:39 +0200159 *((uint32_t *)num) = buf;
160 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100161 case sizeof(uint64_t):
Michal Vasko60ea6352020-06-29 13:39:39 +0200162 *((uint64_t *)num) = buf;
163 break;
164 default:
165 LOGINT(lybctx->ctx);
166 }
167}
168
169/**
170 * @brief Read a string.
171 *
172 * @param[in] str Destination buffer, is allocated.
173 * @param[in] with_length Whether the string is preceded with its length or it ends at the end of this subtree.
174 * @param[in] lybctx LYB context.
175 * @return LY_ERR value.
176 */
177static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200178lyb_read_string(char **str, ly_bool with_length, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200179{
Radek Krejci857189e2020-09-01 13:26:36 +0200180 ly_bool next_chunk = 0;
Michal Vasko60ea6352020-06-29 13:39:39 +0200181 size_t len = 0, cur_len;
182
183 *str = NULL;
184
185 if (with_length) {
186 lyb_read_number(&len, sizeof len, 2, lybctx);
187 } else {
188 /* read until the end of this subtree */
189 len = LYB_LAST_SUBTREE(lybctx).written;
190 if (LYB_LAST_SUBTREE(lybctx).position) {
191 next_chunk = 1;
192 }
193 }
194
195 *str = malloc((len + 1) * sizeof **str);
196 LY_CHECK_ERR_RET(!*str, LOGMEM(lybctx->ctx), LY_EMEM);
197
198 lyb_read((uint8_t *)*str, len, lybctx);
199
200 while (next_chunk) {
201 cur_len = LYB_LAST_SUBTREE(lybctx).written;
202 if (LYB_LAST_SUBTREE(lybctx).position) {
203 next_chunk = 1;
204 } else {
205 next_chunk = 0;
206 }
207
208 *str = ly_realloc(*str, (len + cur_len + 1) * sizeof **str);
209 LY_CHECK_ERR_RET(!*str, LOGMEM(lybctx->ctx), LY_EMEM);
210
211 lyb_read(((uint8_t *)*str) + len, cur_len, lybctx);
212
213 len += cur_len;
214 }
215
216 ((char *)*str)[len] = '\0';
217 return LY_SUCCESS;
218}
219
220/**
221 * @brief Stop the current subtree - change LYB context state.
222 *
223 * @param[in] lybctx LYB context.
224 * @return LY_ERR value.
225 */
226static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200227lyb_read_stop_subtree(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200228{
229 if (LYB_LAST_SUBTREE(lybctx).written) {
230 LOGINT_RET(lybctx->ctx);
231 }
232
233 LY_ARRAY_DECREMENT(lybctx->subtrees);
234 return LY_SUCCESS;
235}
236
237/**
238 * @brief Start a new subtree - change LYB context state but also read the expected metadata.
239 *
240 * @param[in] lybctx LYB context.
241 * @return LY_ERR value.
242 */
243static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200244lyb_read_start_subtree(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200245{
246 uint8_t meta_buf[LYB_META_BYTES];
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200247 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +0200248
Radek Krejcic7d13e32020-12-09 12:32:24 +0100249 u = LY_ARRAY_COUNT(lybctx->subtrees);
Michal Vasko60ea6352020-06-29 13:39:39 +0200250 if (u == lybctx->subtree_size) {
251 LY_ARRAY_CREATE_RET(lybctx->ctx, lybctx->subtrees, u + LYB_SUBTREE_STEP, LY_EMEM);
252 lybctx->subtree_size = u + LYB_SUBTREE_STEP;
253 }
254
Michal Vasko63f3d842020-07-08 10:10:14 +0200255 LY_CHECK_RET(ly_in_read(lybctx->in, meta_buf, LYB_META_BYTES));
Michal Vasko60ea6352020-06-29 13:39:39 +0200256
257 LY_ARRAY_INCREMENT(lybctx->subtrees);
258 LYB_LAST_SUBTREE(lybctx).written = meta_buf[0];
259 LYB_LAST_SUBTREE(lybctx).inner_chunks = meta_buf[LYB_SIZE_BYTES];
260 LYB_LAST_SUBTREE(lybctx).position = (LYB_LAST_SUBTREE(lybctx).written == LYB_SIZE_MAX ? 1 : 0);
261
Michal Vasko60ea6352020-06-29 13:39:39 +0200262 return LY_SUCCESS;
263}
264
265/**
266 * @brief Parse YANG model info.
267 *
268 * @param[in] lybctx LYB context.
269 * @param[out] mod Parsed module.
270 * @return LY_ERR value.
271 */
272static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200273lyb_parse_model(struct lylyb_ctx *lybctx, uint32_t parse_options, const struct lys_module **mod)
Michal Vasko60ea6352020-06-29 13:39:39 +0200274{
275 LY_ERR ret = LY_SUCCESS;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100276 char *mod_name = NULL, mod_rev[LY_REV_SIZE];
Michal Vasko60ea6352020-06-29 13:39:39 +0200277 uint16_t rev;
278
279 /* model name */
280 ret = lyb_read_string(&mod_name, 1, lybctx);
281 LY_CHECK_GOTO(ret, cleanup);
282
283 /* revision */
284 lyb_read_number(&rev, sizeof rev, 2, lybctx);
285
286 if (!mod_name[0]) {
287 /* opaq node, no module */
288 *mod = NULL;
289 goto cleanup;
290 }
291
292 if (rev) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100293 sprintf(mod_rev, "%04u-%02u-%02u", ((rev & LYB_REV_YEAR_MASK) >> LYB_REV_YEAR_SHIFT) + LYB_REV_YEAR_OFFSET,
294 (rev & LYB_REV_MONTH_MASK) >> LYB_REV_MONTH_SHIFT, rev & LYB_REV_DAY_MASK);
Michal Vasko60ea6352020-06-29 13:39:39 +0200295 *mod = ly_ctx_get_module(lybctx->ctx, mod_name, mod_rev);
Radek Krejci1798aae2020-07-14 13:26:06 +0200296 if ((parse_options & LYD_PARSE_LYB_MOD_UPDATE) && !(*mod)) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200297 /* try to use an updated module */
298 *mod = ly_ctx_get_module_implemented(lybctx->ctx, mod_name);
299 if (*mod && (!(*mod)->revision || (strcmp((*mod)->revision, mod_rev) < 0))) {
300 /* not an implemented module in a newer revision */
301 *mod = NULL;
302 }
303 }
304 } else {
305 *mod = ly_ctx_get_module_latest(lybctx->ctx, mod_name);
306 }
307 /* TODO data_clb supported?
308 if (lybctx->ctx->data_clb) {
309 if (!*mod) {
310 *mod = lybctx->ctx->data_clb(lybctx->ctx, mod_name, NULL, 0, lybctx->ctx->data_clb_data);
311 } else if (!(*mod)->implemented) {
312 *mod = lybctx->ctx->data_clb(lybctx->ctx, mod_name, (*mod)->ns, LY_MODCLB_NOT_IMPLEMENTED, lybctx->ctx->data_clb_data);
313 }
314 }*/
315
316 if (!*mod || !(*mod)->implemented) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200317 if (parse_options & LYD_PARSE_STRICT) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200318 if (!*mod) {
319 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid context for LYB data parsing, missing module \"%s%s%s\".",
Michal Vasko69730152020-10-09 16:30:07 +0200320 mod_name, rev ? "@" : "", rev ? mod_rev : "");
Michal Vasko60ea6352020-06-29 13:39:39 +0200321 } else if (!(*mod)->implemented) {
322 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid context for LYB data parsing, module \"%s%s%s\" not implemented.",
Michal Vasko69730152020-10-09 16:30:07 +0200323 mod_name, rev ? "@" : "", rev ? mod_rev : "");
Michal Vasko60ea6352020-06-29 13:39:39 +0200324 }
325 ret = LY_EINVAL;
326 goto cleanup;
327 }
328
329 }
330
331cleanup:
332 free(mod_name);
333 return ret;
334}
335
336/**
337 * @brief Parse YANG node metadata.
338 *
339 * @param[in] lybctx LYB context.
Michal Vasko60ea6352020-06-29 13:39:39 +0200340 * @param[out] meta Parsed metadata.
341 * @return LY_ERR value.
342 */
343static LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200344lyb_parse_metadata(struct lyd_lyb_ctx *lybctx, struct lyd_meta **meta)
Michal Vasko60ea6352020-06-29 13:39:39 +0200345{
346 LY_ERR ret = LY_SUCCESS;
Radek Krejci857189e2020-09-01 13:26:36 +0200347 ly_bool dynamic;
Michal Vasko60ea6352020-06-29 13:39:39 +0200348 uint8_t i, count = 0;
Michal Vasko1e5d5612020-07-03 13:29:26 +0200349 char *meta_name = NULL, *meta_value;
Michal Vasko60ea6352020-06-29 13:39:39 +0200350 const struct lys_module *mod;
351
352 /* read number of attributes stored */
Radek Krejci1798aae2020-07-14 13:26:06 +0200353 lyb_read(&count, 1, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200354
355 /* read attributes */
356 for (i = 0; i < count; ++i) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200357 ret = lyb_read_start_subtree(lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200358 LY_CHECK_GOTO(ret, cleanup);
359
360 /* find model */
Radek Krejci1798aae2020-07-14 13:26:06 +0200361 ret = lyb_parse_model(lybctx->lybctx, lybctx->parse_options, &mod);
Michal Vasko60ea6352020-06-29 13:39:39 +0200362 LY_CHECK_GOTO(ret, cleanup);
363
364 if (!mod) {
365 /* skip it */
366 do {
Radek Krejci1798aae2020-07-14 13:26:06 +0200367 lyb_read(NULL, LYB_LAST_SUBTREE(lybctx->lybctx).written, lybctx->lybctx);
368 } while (LYB_LAST_SUBTREE(lybctx->lybctx).written);
Michal Vasko60ea6352020-06-29 13:39:39 +0200369 goto stop_subtree;
370 }
371
372 /* meta name */
Radek Krejci1798aae2020-07-14 13:26:06 +0200373 ret = lyb_read_string(&meta_name, 1, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200374 LY_CHECK_GOTO(ret, cleanup);
375
376 /* meta value */
Radek Krejci1798aae2020-07-14 13:26:06 +0200377 ret = lyb_read_string(&meta_value, 0, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200378 LY_CHECK_GOTO(ret, cleanup);
379 dynamic = 1;
380
381 /* create metadata */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200382 ret = lyd_parser_create_meta((struct lyd_ctx *)lybctx, NULL, meta, mod, meta_name, strlen(meta_name), meta_value,
Michal Vasko69730152020-10-09 16:30:07 +0200383 ly_strlen(meta_value), &dynamic, LY_PREF_JSON, NULL, LYD_HINT_DATA);
Michal Vasko60ea6352020-06-29 13:39:39 +0200384
385 /* free strings */
386 free(meta_name);
387 meta_name = NULL;
388 if (dynamic) {
389 free(meta_value);
390 dynamic = 0;
391 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200392
Radek Krejci1798aae2020-07-14 13:26:06 +0200393 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200394
395stop_subtree:
Radek Krejci1798aae2020-07-14 13:26:06 +0200396 ret = lyb_read_stop_subtree(lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200397 LY_CHECK_GOTO(ret, cleanup);
398 }
399
400cleanup:
401 free(meta_name);
Michal Vasko60ea6352020-06-29 13:39:39 +0200402 if (ret) {
Michal Vasko3a41dff2020-07-15 14:30:28 +0200403 lyd_free_meta_siblings(*meta);
Michal Vasko60ea6352020-06-29 13:39:39 +0200404 *meta = NULL;
405 }
406 return ret;
407}
408
409/**
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100410 * @brief Parse format-specific prefix data.
Michal Vasko60ea6352020-06-29 13:39:39 +0200411 *
412 * @param[in] lybctx LYB context.
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100413 * @param[in] format Prefix data format.
414 * @param[out] prefix_data Parsed prefix data.
Michal Vasko60ea6352020-06-29 13:39:39 +0200415 * @return LY_ERR value.
416 */
417static LY_ERR
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100418lyb_parse_prefix_data(struct lylyb_ctx *lybctx, LY_PREFIX_FORMAT format, void **prefix_data)
Michal Vasko60ea6352020-06-29 13:39:39 +0200419{
420 LY_ERR ret = LY_SUCCESS;
421 uint8_t count, i;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100422 struct ly_set *set = NULL;
423 struct lyxml_ns *ns = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200424
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100425 switch (format) {
426 case LY_PREF_XML:
427 /* read count */
428 lyb_read(&count, 1, lybctx);
429 if (!count) {
430 return LY_SUCCESS;
431 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200432
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100433 /* read all NS elements */
434 LY_CHECK_GOTO(ret = ly_set_new(&set), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200435
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100436 for (i = 0; i < count; ++i) {
437 ns = calloc(1, sizeof *ns);
Michal Vasko60ea6352020-06-29 13:39:39 +0200438
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100439 /* prefix */
440 LY_CHECK_GOTO(ret = lyb_read_string(&ns->prefix, 1, lybctx), cleanup);
441
442 /* namespace */
443 LY_CHECK_GOTO(ret = lyb_read_string(&ns->uri, 1, lybctx), cleanup);
444
445 LY_CHECK_GOTO(ret = ly_set_add(set, ns, 1, NULL), cleanup);
446 ns = NULL;
447 }
448
449 *prefix_data = set;
450 break;
451 case LY_PREF_JSON:
452 /* nothing stored */
453 break;
454 default:
455 LOGINT(lybctx->ctx);
456 ret = LY_EINT;
457 break;
Michal Vasko60ea6352020-06-29 13:39:39 +0200458 }
459
460cleanup:
461 if (ret) {
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100462 ly_free_prefix_data(format, set);
463 if (ns) {
464 free(ns->prefix);
465 free(ns->uri);
466 free(ns);
467 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200468 }
469 return ret;
470}
471
472/**
473 * @brief Parse opaque attributes.
474 *
475 * @param[in] lybctx LYB context.
476 * @param[out] attr Parsed attributes.
477 * @return LY_ERR value.
478 */
479static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200480lyb_parse_attributes(struct lylyb_ctx *lybctx, struct lyd_attr **attr)
Michal Vasko60ea6352020-06-29 13:39:39 +0200481{
482 LY_ERR ret = LY_SUCCESS;
483 uint8_t count, i;
Radek Krejci1798aae2020-07-14 13:26:06 +0200484 struct lyd_attr *attr2;
485 char *prefix = NULL, *module_name = NULL, *name = NULL, *value = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +0200486 ly_bool dynamic = 0;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100487 LY_PREFIX_FORMAT format = 0;
488 void *val_prefix_data = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200489
490 /* read count */
491 lyb_read(&count, 1, lybctx);
492
493 /* read attributes */
494 for (i = 0; i < count; ++i) {
495 ret = lyb_read_start_subtree(lybctx);
496 LY_CHECK_GOTO(ret, cleanup);
497
Michal Vasko0fdcd242020-11-11 19:12:30 +0100498 /* prefix, may be empty */
Michal Vasko60ea6352020-06-29 13:39:39 +0200499 ret = lyb_read_string(&prefix, 1, lybctx);
500 LY_CHECK_GOTO(ret, cleanup);
501 if (!prefix[0]) {
502 free(prefix);
503 prefix = NULL;
504 }
505
506 /* namespace, may be empty */
Radek Krejci1798aae2020-07-14 13:26:06 +0200507 ret = lyb_read_string(&module_name, 1, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200508 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200509 if (!module_name[0]) {
510 free(module_name);
511 module_name = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200512 }
513
514 /* name */
515 ret = lyb_read_string(&name, 1, lybctx);
516 LY_CHECK_GOTO(ret, cleanup);
517
Michal Vasko60ea6352020-06-29 13:39:39 +0200518 /* format */
519 lyb_read((uint8_t *)&format, 1, lybctx);
520
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100521 /* value prefixes */
522 ret = lyb_parse_prefix_data(lybctx, format, &val_prefix_data);
523 LY_CHECK_GOTO(ret, cleanup);
524
Michal Vasko60ea6352020-06-29 13:39:39 +0200525 /* value */
526 ret = lyb_read_string(&value, 0, lybctx);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100527 LY_CHECK_ERR_GOTO(ret, ly_free_prefix_data(format, val_prefix_data), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200528 dynamic = 1;
529
530 /* attr2 is always changed to the created attribute */
Michal Vasko501af032020-11-11 20:27:44 +0100531 ret = lyd_create_attr(NULL, &attr2, lybctx->ctx, name, strlen(name), prefix, ly_strlen(prefix), module_name,
532 ly_strlen(module_name), value, ly_strlen(value), &dynamic, format, val_prefix_data, 0);
Michal Vasko60ea6352020-06-29 13:39:39 +0200533 LY_CHECK_GOTO(ret, cleanup);
534
535 free(prefix);
536 prefix = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +0200537 free(module_name);
538 module_name = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200539 free(name);
540 name = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200541 assert(!dynamic);
542 value = NULL;
543
544 if (!*attr) {
545 *attr = attr2;
546 }
547
548 ret = lyb_read_stop_subtree(lybctx);
549 LY_CHECK_GOTO(ret, cleanup);
550 }
551
552cleanup:
553 free(prefix);
Radek Krejci1798aae2020-07-14 13:26:06 +0200554 free(module_name);
Michal Vasko60ea6352020-06-29 13:39:39 +0200555 free(name);
556 if (dynamic) {
557 free(value);
558 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200559 if (ret) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200560 lyd_free_attr_siblings(lybctx->ctx, *attr);
Michal Vasko60ea6352020-06-29 13:39:39 +0200561 *attr = NULL;
562 }
563 return ret;
564}
565
566/**
567 * @brief Check whether a schema node matches a hash(es).
568 *
569 * @param[in] sibling Schema node to check.
570 * @param[in] hash Hash array to check.
571 * @param[in] hash_count Number of hashes in @p hash.
572 * @return non-zero if matches,
573 * @return 0 if not.
574 */
575static int
576lyb_is_schema_hash_match(struct lysc_node *sibling, LYB_HASH *hash, uint8_t hash_count)
577{
578 LYB_HASH sibling_hash;
579 uint8_t i;
580
581 /* compare all the hashes starting from collision ID 0 */
582 for (i = 0; i < hash_count; ++i) {
583 sibling_hash = lyb_hash(sibling, i);
584 if (sibling_hash != hash[i]) {
585 return 0;
586 }
587 }
588
589 return 1;
590}
591
592/**
Michal Vasko60ea6352020-06-29 13:39:39 +0200593 * @brief Parse schema node hash.
594 *
595 * @param[in] lybctx LYB context.
596 * @param[in] sparent Schema parent, must be set if @p mod is not.
597 * @param[in] mod Module of the top-level node, must be set if @p sparent is not.
598 * @param[out] snode Parsed found schema node, may be NULL if opaque.
599 * @return LY_ERR value.
600 */
601static LY_ERR
602lyb_parse_schema_hash(struct lyd_lyb_ctx *lybctx, const struct lysc_node *sparent, const struct lys_module *mod,
Radek Krejci0f969882020-08-21 16:56:47 +0200603 const struct lysc_node **snode)
Michal Vasko60ea6352020-06-29 13:39:39 +0200604{
605 LY_ERR ret;
606 uint8_t i, j;
607 const struct lysc_node *sibling;
608 LYB_HASH hash[LYB_HASH_BITS - 1];
Radek Krejci1deb5be2020-08-26 16:43:36 +0200609 uint32_t getnext_opts;
Michal Vasko60ea6352020-06-29 13:39:39 +0200610
611 *snode = NULL;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100612 getnext_opts = lybctx->int_opts & LYD_INTOPT_REPLY ? LYS_GETNEXT_OUTPUT : 0;
Michal Vasko60ea6352020-06-29 13:39:39 +0200613
614 /* read the first hash */
Radek Krejci1798aae2020-07-14 13:26:06 +0200615 lyb_read(&hash[0], sizeof *hash, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200616
617 if (!hash[0]) {
618 /* opaque node */
619 return LY_SUCCESS;
620 }
621
622 /* based on the first hash read all the other ones, if any */
623 for (i = 0; !(hash[0] & (LYB_HASH_COLLISION_ID >> i)); ++i) {
624 if (i > LYB_HASH_BITS) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200625 LOGINT_RET(lybctx->lybctx->ctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200626 }
627 }
628
629 /* move the first hash on its accurate position */
630 hash[i] = hash[0];
631
632 /* read the rest of hashes */
633 for (j = i; j; --j) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200634 lyb_read(&hash[j - 1], sizeof *hash, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200635
636 /* correct collision ID */
637 assert(hash[j - 1] & (LYB_HASH_COLLISION_ID >> (j - 1)));
638 /* preceded with zeros */
639 assert(!(hash[j - 1] & (LYB_HASH_MASK << (LYB_HASH_BITS - (j - 1)))));
640 }
641
642 /* find our node with matching hashes */
643 sibling = NULL;
644 while ((sibling = lys_getnext(sibling, sparent, mod ? mod->compiled : NULL, getnext_opts))) {
645 /* skip schema nodes from models not present during printing */
Michal Vasko69730152020-10-09 16:30:07 +0200646 if (lyb_has_schema_model(sibling, lybctx->lybctx->models) &&
647 lyb_is_schema_hash_match((struct lysc_node *)sibling, hash, i + 1)) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200648 /* match found */
649 break;
650 }
651 }
652
Radek Krejci7931b192020-06-25 17:05:03 +0200653 if (!sibling && (lybctx->parse_options & LYD_PARSE_STRICT)) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200654 if (mod) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100655 LOGVAL(lybctx->lybctx->ctx, LYVE_REFERENCE, "Failed to find matching hash for a top-level node"
Michal Vasko69730152020-10-09 16:30:07 +0200656 " from \"%s\".", mod->name);
Michal Vasko60ea6352020-06-29 13:39:39 +0200657 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100658 LOGVAL(lybctx->lybctx->ctx, LYVE_REFERENCE, "Failed to find matching hash for a child node"
Michal Vasko69730152020-10-09 16:30:07 +0200659 " of \"%s\".", sparent->name);
Michal Vasko60ea6352020-06-29 13:39:39 +0200660 }
661 return LY_EVALID;
Radek Krejci1798aae2020-07-14 13:26:06 +0200662 } else if (sibling && (ret = lyd_parser_check_schema((struct lyd_ctx *)lybctx, sibling))) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200663 return ret;
664 }
665
666 *snode = sibling;
667 return LY_SUCCESS;
668}
669
670/**
671 * @brief Read until the end of the current subtree.
672 *
673 * @param[in] lybctx LYB context.
674 */
675static void
Radek Krejci1798aae2020-07-14 13:26:06 +0200676lyb_skip_subtree(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200677{
Michal Vasko60ea6352020-06-29 13:39:39 +0200678 do {
679 /* first skip any meta information inside */
Michal Vasko63f3d842020-07-08 10:10:14 +0200680 ly_in_skip(lybctx->in, LYB_LAST_SUBTREE(lybctx).inner_chunks * LYB_META_BYTES);
Michal Vasko60ea6352020-06-29 13:39:39 +0200681
682 /* then read data */
683 lyb_read(NULL, LYB_LAST_SUBTREE(lybctx).written, lybctx);
684 } while (LYB_LAST_SUBTREE(lybctx).written);
685}
686
687/**
688 * @brief Parse LYB subtree.
689 *
690 * @param[in] lybctx LYB context.
691 * @param[in] parent Data parent of the subtree, must be set if @p first is not.
692 * @param[in,out] first First top-level sibling, must be set if @p parent is not.
693 * @return LY_ERR value.
694 */
695static LY_ERR
696lyb_parse_subtree_r(struct lyd_lyb_ctx *lybctx, struct lyd_node_inner *parent, struct lyd_node **first)
697{
698 LY_ERR ret = LY_SUCCESS;
699 struct lyd_node *node = NULL, *tree;
700 const struct lys_module *mod;
701 const struct lysc_node *snode = NULL;
702 struct lyd_meta *meta = NULL, *m;
Radek Krejci1798aae2020-07-14 13:26:06 +0200703 struct lyd_attr *attr = NULL, *a;
Michal Vasko60ea6352020-06-29 13:39:39 +0200704 LYD_ANYDATA_VALUETYPE value_type;
Radek Krejci1798aae2020-07-14 13:26:06 +0200705 char *value = NULL, *name = NULL, *prefix = NULL, *module_key = NULL;
Michal Vasko9afe3df2021-02-05 16:33:50 +0100706 const char *val_dict;
Radek Krejci857189e2020-09-01 13:26:36 +0200707 ly_bool dynamic = 0;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100708 LY_PREFIX_FORMAT format = 0;
709 void *val_prefix_data = NULL;
Michal Vaskoc5e866a2020-11-04 17:09:26 +0100710 uint32_t prev_lo, flags;
Radek Krejci1798aae2020-07-14 13:26:06 +0200711 const struct ly_ctx *ctx = lybctx->lybctx->ctx;
Michal Vasko60ea6352020-06-29 13:39:39 +0200712
713 /* register a new subtree */
Radek Krejci1798aae2020-07-14 13:26:06 +0200714 LY_CHECK_GOTO(ret = lyb_read_start_subtree(lybctx->lybctx), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200715
716 if (!parent) {
717 /* top-level, read module name */
Radek Krejci1798aae2020-07-14 13:26:06 +0200718 ret = lyb_parse_model(lybctx->lybctx, lybctx->parse_options, &mod);
Michal Vasko60ea6352020-06-29 13:39:39 +0200719 LY_CHECK_GOTO(ret, cleanup);
720
721 /* read hash, find the schema node starting from mod */
722 ret = lyb_parse_schema_hash(lybctx, NULL, mod, &snode);
723 LY_CHECK_GOTO(ret, cleanup);
724 } else {
725 /* read hash, find the schema node starting from parent schema */
726 ret = lyb_parse_schema_hash(lybctx, parent->schema, NULL, &snode);
727 LY_CHECK_GOTO(ret, cleanup);
728 }
729
Radek Krejci0f969882020-08-21 16:56:47 +0200730 if (!snode && !(lybctx->parse_options & LYD_PARSE_OPAQ)) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200731 /* unknown data, skip them */
Radek Krejci1798aae2020-07-14 13:26:06 +0200732 lyb_skip_subtree(lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200733 goto stop_subtree;
Radek Krejci0f969882020-08-21 16:56:47 +0200734 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200735
736 /* create metadata/attributes */
737 if (snode) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200738 ret = lyb_parse_metadata(lybctx, &meta);
Michal Vasko60ea6352020-06-29 13:39:39 +0200739 LY_CHECK_GOTO(ret, cleanup);
740 } else {
Radek Krejci1798aae2020-07-14 13:26:06 +0200741 ret = lyb_parse_attributes(lybctx->lybctx, &attr);
Michal Vasko60ea6352020-06-29 13:39:39 +0200742 LY_CHECK_GOTO(ret, cleanup);
743 }
744
Michal Vaskoc5e866a2020-11-04 17:09:26 +0100745 /* read flags */
746 lyb_read_number(&flags, sizeof flags, sizeof flags, lybctx->lybctx);
747
Michal Vasko60ea6352020-06-29 13:39:39 +0200748 if (!snode) {
749 /* parse prefix */
Radek Krejci1798aae2020-07-14 13:26:06 +0200750 ret = lyb_read_string(&prefix, 1, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200751 LY_CHECK_GOTO(ret, cleanup);
752
Radek Krejci1798aae2020-07-14 13:26:06 +0200753 /* parse module key */
754 ret = lyb_read_string(&module_key, 1, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200755 LY_CHECK_GOTO(ret, cleanup);
756
757 /* parse name */
Radek Krejci1798aae2020-07-14 13:26:06 +0200758 ret = lyb_read_string(&name, 1, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200759 LY_CHECK_GOTO(ret, cleanup);
760
Michal Vasko60ea6352020-06-29 13:39:39 +0200761 /* parse format */
Radek Krejci1798aae2020-07-14 13:26:06 +0200762 lyb_read((uint8_t *)&format, 1, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200763
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100764 /* parse value prefixes */
765 ret = lyb_parse_prefix_data(lybctx->lybctx, format, &val_prefix_data);
766 LY_CHECK_GOTO(ret, cleanup);
767
Michal Vasko60ea6352020-06-29 13:39:39 +0200768 /* parse value */
Radek Krejci1798aae2020-07-14 13:26:06 +0200769 ret = lyb_read_string(&value, 0, lybctx->lybctx);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100770 LY_CHECK_ERR_GOTO(ret, ly_free_prefix_data(format, val_prefix_data), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200771 dynamic = 1;
772
773 /* create node */
Michal Vasko501af032020-11-11 20:27:44 +0100774 ret = lyd_create_opaq(ctx, name, strlen(name), prefix, ly_strlen(prefix), module_key, ly_strlen(module_key),
775 value, strlen(value), &dynamic, format, val_prefix_data, 0, &node);
Michal Vasko60ea6352020-06-29 13:39:39 +0200776 LY_CHECK_GOTO(ret, cleanup);
777
778 /* process children */
Radek Krejci1798aae2020-07-14 13:26:06 +0200779 while (LYB_LAST_SUBTREE(lybctx->lybctx).written) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200780 ret = lyb_parse_subtree_r(lybctx, (struct lyd_node_inner *)node, NULL);
781 LY_CHECK_GOTO(ret, cleanup);
782 }
783 } else if (snode->nodetype & LYD_NODE_TERM) {
784 /* parse value */
Radek Krejci1798aae2020-07-14 13:26:06 +0200785 ret = lyb_read_string(&value, 0, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200786 LY_CHECK_GOTO(ret, cleanup);
787 dynamic = 1;
788
789 /* create node */
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200790 ret = lyd_parser_create_term((struct lyd_ctx *)lybctx, snode, value, ly_strlen(value), &dynamic, LY_PREF_JSON,
Michal Vasko69730152020-10-09 16:30:07 +0200791 NULL, LYD_HINT_DATA, &node);
Michal Vasko60ea6352020-06-29 13:39:39 +0200792 if (dynamic) {
793 free(value);
794 dynamic = 0;
795 }
796 value = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +0200797 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200798 } else if (snode->nodetype & LYD_NODE_INNER) {
799 /* create node */
800 ret = lyd_create_inner(snode, &node);
801 LY_CHECK_GOTO(ret, cleanup);
802
803 /* process children */
Radek Krejci1798aae2020-07-14 13:26:06 +0200804 while (LYB_LAST_SUBTREE(lybctx->lybctx).written) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200805 ret = lyb_parse_subtree_r(lybctx, (struct lyd_node_inner *)node, NULL);
806 LY_CHECK_GOTO(ret, cleanup);
807 }
808
Radek Krejci7931b192020-06-25 17:05:03 +0200809 if (!(lybctx->parse_options & LYD_PARSE_ONLY)) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200810 /* new node validation, autodelete CANNOT occur, all nodes are new */
Michal Vasko8104fd42020-07-13 11:09:51 +0200811 ret = lyd_validate_new(lyd_node_children_p(node), snode, NULL, NULL);
Michal Vasko60ea6352020-06-29 13:39:39 +0200812 LY_CHECK_GOTO(ret, cleanup);
813
814 /* add any missing default children */
Michal Vaskoa6669ba2020-08-06 16:14:26 +0200815 ret = lyd_new_implicit_r(node, lyd_node_children_p(node), NULL, NULL, &lybctx->unres_node_type,
Michal Vasko69730152020-10-09 16:30:07 +0200816 &lybctx->when_check, (lybctx->validate_options & LYD_VALIDATE_NO_STATE) ?
817 LYD_IMPLICIT_NO_STATE : 0, NULL);
Michal Vasko60ea6352020-06-29 13:39:39 +0200818 LY_CHECK_GOTO(ret, cleanup);
819 }
820
Michal Vasko751cb4d2020-07-14 12:25:28 +0200821 if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200822 /* rememeber the RPC/action/notification */
Radek Krejci1798aae2020-07-14 13:26:06 +0200823 lybctx->op_node = node;
Michal Vasko60ea6352020-06-29 13:39:39 +0200824 }
825 } else if (snode->nodetype & LYD_NODE_ANY) {
826 /* parse value type */
Radek Krejci1798aae2020-07-14 13:26:06 +0200827 lyb_read((uint8_t *)&value_type, sizeof value_type, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200828 if (value_type == LYD_ANYDATA_DATATREE) {
829 /* invalid situation */
Radek Krejci1798aae2020-07-14 13:26:06 +0200830 LOGINT(ctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200831 goto cleanup;
832 }
833
834 /* read anydata content */
Radek Krejci1798aae2020-07-14 13:26:06 +0200835 ret = lyb_read_string(&value, 0, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200836 LY_CHECK_GOTO(ret, cleanup);
837 dynamic = 1;
838
839 if (value_type == LYD_ANYDATA_LYB) {
840 /* turn logging off */
841 prev_lo = ly_log_options(0);
842
843 /* try to parse LYB into a data tree */
Radek Krejci1798aae2020-07-14 13:26:06 +0200844 if (lyd_parse_data_mem(ctx, value, LYD_LYB, LYD_PARSE_ONLY | LYD_PARSE_OPAQ | LYD_PARSE_STRICT, 0, &tree) == LY_SUCCESS) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200845 /* successfully parsed */
846 free(value);
847 value = (char *)tree;
848 value_type = LYD_ANYDATA_DATATREE;
849 }
Radek Krejci7931b192020-06-25 17:05:03 +0200850
851 /* turn logging on again */
852 ly_log_options(prev_lo);
Michal Vasko60ea6352020-06-29 13:39:39 +0200853 }
854
Michal Vasko9afe3df2021-02-05 16:33:50 +0100855 /* create the node */
856 switch (value_type) {
857 case LYD_ANYDATA_LYB:
858 case LYD_ANYDATA_DATATREE:
859 /* use the value directly */
860 ret = lyd_create_any(snode, value, value_type, 1, &node);
861 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200862
Michal Vasko9afe3df2021-02-05 16:33:50 +0100863 dynamic = 0;
864 value = NULL;
865 break;
866 case LYD_ANYDATA_STRING:
867 case LYD_ANYDATA_XML:
868 case LYD_ANYDATA_JSON:
869 /* value is expected to be in the dictionary */
870 ret = lydict_insert_zc(ctx, value, &val_dict);
871 LY_CHECK_GOTO(ret, cleanup);
872 dynamic = 0;
873 value = NULL;
874
875 /* use the value in the dictionary */
876 ret = lyd_create_any(snode, val_dict, value_type, 1, &node);
877 if (ret) {
878 lydict_remove(ctx, val_dict);
879 goto cleanup;
880 }
881 break;
882 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200883 }
884 assert(node);
885
Michal Vaskoc5e866a2020-11-04 17:09:26 +0100886 /* set flags */
887 node->flags = flags;
Michal Vasko60ea6352020-06-29 13:39:39 +0200888
889 /* add metadata/attributes */
890 if (snode) {
891 LY_LIST_FOR(meta, m) {
892 m->parent = node;
893 }
894 node->meta = meta;
895 meta = NULL;
896 } else {
897 assert(!node->schema);
898 LY_LIST_FOR(attr, a) {
899 a->parent = (struct lyd_node_opaq *)node;
900 }
901 ((struct lyd_node_opaq *)node)->attr = attr;
902 attr = NULL;
903 }
904
Michal Vaskob104f112020-07-17 09:54:54 +0200905 /* insert, keep first pointer correct */
Michal Vasko9e685082021-01-29 14:49:09 +0100906 lyd_insert_node(&parent->node, first, node);
Michal Vaskob104f112020-07-17 09:54:54 +0200907 while (!parent && (*first)->prev->next) {
908 *first = (*first)->prev;
909 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200910 node = NULL;
911
912stop_subtree:
913 /* end the subtree */
Radek Krejci1798aae2020-07-14 13:26:06 +0200914 ret = lyb_read_stop_subtree(lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200915 LY_CHECK_GOTO(ret, cleanup);
916
917cleanup:
918 free(prefix);
Radek Krejci1798aae2020-07-14 13:26:06 +0200919 free(module_key);
Michal Vasko60ea6352020-06-29 13:39:39 +0200920 free(name);
921 if (dynamic) {
922 free(value);
923 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200924
Michal Vasko3a41dff2020-07-15 14:30:28 +0200925 lyd_free_meta_siblings(meta);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200926 lyd_free_attr_siblings(ctx, attr);
Michal Vasko60ea6352020-06-29 13:39:39 +0200927 lyd_free_tree(node);
928 return ret;
929}
930
931/**
932 * @brief Parse used YANG data models.
933 *
934 * @param[in] lybctx LYB context.
935 * @return LY_ERR value.
936 */
937static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200938lyb_parse_data_models(struct lylyb_ctx *lybctx, uint32_t parse_options)
Michal Vasko60ea6352020-06-29 13:39:39 +0200939{
940 LY_ERR ret;
941 uint32_t count;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200942 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +0200943
944 /* read model count */
945 lyb_read_number(&count, sizeof count, 2, lybctx);
946
947 if (count) {
948 LY_ARRAY_CREATE_RET(lybctx->ctx, lybctx->models, count, LY_EMEM);
949
950 /* read modules */
951 for (u = 0; u < count; ++u) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200952 ret = lyb_parse_model(lybctx, parse_options, &lybctx->models[u]);
Michal Vasko60ea6352020-06-29 13:39:39 +0200953 LY_CHECK_RET(ret);
954 LY_ARRAY_INCREMENT(lybctx->models);
955 }
956 }
957
958 return LY_SUCCESS;
959}
960
961/**
962 * @brief Parse LYB magic number.
963 *
964 * @param[in] lybctx LYB context.
965 * @return LY_ERR value.
966 */
967static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200968lyb_parse_magic_number(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200969{
970 char magic_byte = 0;
971
972 lyb_read((uint8_t *)&magic_byte, 1, lybctx);
973 if (magic_byte != 'l') {
974 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid first magic number byte \"0x%02x\".", magic_byte);
975 return LY_EINVAL;
976 }
977
978 lyb_read((uint8_t *)&magic_byte, 1, lybctx);
979 if (magic_byte != 'y') {
980 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid second magic number byte \"0x%02x\".", magic_byte);
981 return LY_EINVAL;
982 }
983
984 lyb_read((uint8_t *)&magic_byte, 1, lybctx);
985 if (magic_byte != 'b') {
986 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid third magic number byte \"0x%02x\".", magic_byte);
987 return LY_EINVAL;
988 }
989
990 return LY_SUCCESS;
991}
992
993/**
994 * @brief Parse LYB header.
995 *
996 * @param[in] lybctx LYB context.
997 * @return LY_ERR value.
998 */
999static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +02001000lyb_parse_header(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +02001001{
1002 uint8_t byte = 0;
1003
1004 /* version, future flags */
1005 lyb_read((uint8_t *)&byte, sizeof byte, lybctx);
1006
1007 if ((byte & LYB_VERSION_MASK) != LYB_VERSION_NUM) {
1008 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid LYB format version \"0x%02x\", expected \"0x%02x\".",
Michal Vasko69730152020-10-09 16:30:07 +02001009 byte & LYB_VERSION_MASK, LYB_VERSION_NUM);
Michal Vasko60ea6352020-06-29 13:39:39 +02001010 return LY_EINVAL;
1011 }
1012
1013 return LY_SUCCESS;
1014}
1015
Michal Vaskoee38a5d2020-11-09 21:02:18 +01001016/**
Radek Krejci1798aae2020-07-14 13:26:06 +02001017 * @param[in] ctx libyang context for logging
Michal Vaskoee38a5d2020-11-09 21:02:18 +01001018 * @param[in] parent Parent node where to connect the parsed data, required for reply where the reply data are connected
1019 * with the request operation
Radek Krejci1798aae2020-07-14 13:26:06 +02001020 * @param[in] in Input structure.
1021 * @param[in] parse_options Options for parser, see @ref dataparseroptions.
1022 * @param[in] validate_options Options for the validation phase, see @ref datavalidationoptions.
1023 * @param[in] data_type Internal data parser flag to distnguish type of the data to parse (RPC/Reply/Notification/regular data].
1024 * @param[out] tree_p Parsed data tree. Note that NULL can be a valid result.
1025 * @param[out] op_p Optional pointer to the actual operation. Useful for action and inner notifications.
1026 * @param[out] lydctx_p Data parser context to finish validation.
1027 */
1028static LY_ERR
Michal Vaskoee38a5d2020-11-09 21:02:18 +01001029lyd_parse_lyb_(const struct ly_ctx *ctx, struct lyd_node_inner **parent, struct ly_in *in, uint32_t parse_options,
1030 uint32_t validate_options, uint32_t data_type, struct lyd_node **tree_p, struct lyd_node **op_p,
1031 struct lyd_ctx **lydctx_p)
Michal Vasko60ea6352020-06-29 13:39:39 +02001032{
1033 LY_ERR ret = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001034 struct lyd_lyb_ctx *lybctx;
1035 struct lyd_node *tree = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +02001036
Radek Krejci7931b192020-06-25 17:05:03 +02001037 assert(!(parse_options & ~LYD_PARSE_OPTS_MASK));
1038 assert(!(validate_options & ~LYD_VALIDATE_OPTS_MASK));
1039
Radek Krejci1798aae2020-07-14 13:26:06 +02001040 lybctx = calloc(1, sizeof *lybctx);
1041 LY_CHECK_ERR_RET(!lybctx, LOGMEM(ctx), LY_EMEM);
1042 lybctx->lybctx = calloc(1, sizeof *lybctx->lybctx);
Michal Vasko9acaf492020-08-13 09:05:58 +02001043 LY_CHECK_ERR_GOTO(!lybctx->lybctx, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001044
Radek Krejci1798aae2020-07-14 13:26:06 +02001045 lybctx->lybctx->in = in;
1046 lybctx->lybctx->ctx = ctx;
1047 lybctx->parse_options = parse_options;
1048 lybctx->validate_options = validate_options;
1049 lybctx->int_opts = data_type;
1050 lybctx->free = lyd_lyb_ctx_free;
Michal Vasko60ea6352020-06-29 13:39:39 +02001051
1052 /* read magic number */
Radek Krejci1798aae2020-07-14 13:26:06 +02001053 ret = lyb_parse_magic_number(lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001054 LY_CHECK_GOTO(ret, cleanup);
1055
1056 /* read header */
Radek Krejci1798aae2020-07-14 13:26:06 +02001057 ret = lyb_parse_header(lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001058 LY_CHECK_GOTO(ret, cleanup);
1059
1060 /* read used models */
Radek Krejci1798aae2020-07-14 13:26:06 +02001061 ret = lyb_parse_data_models(lybctx->lybctx, lybctx->parse_options);
Michal Vasko60ea6352020-06-29 13:39:39 +02001062 LY_CHECK_GOTO(ret, cleanup);
1063
1064 /* read subtree(s) */
Radek Krejci1798aae2020-07-14 13:26:06 +02001065 while (lybctx->lybctx->in->current[0]) {
1066 ret = lyb_parse_subtree_r(lybctx, parent ? *parent : NULL, &tree);
Michal Vasko60ea6352020-06-29 13:39:39 +02001067 LY_CHECK_GOTO(ret, cleanup);
1068 }
1069
1070 /* read the last zero, parsing finished */
Radek Krejci1798aae2020-07-14 13:26:06 +02001071 ly_in_skip(lybctx->lybctx->in, 1);
Michal Vasko60ea6352020-06-29 13:39:39 +02001072
Michal Vasko2552ea32020-12-08 15:32:34 +01001073 if (data_type == (LYD_INTOPT_RPC | LYD_INTOPT_REPLY)) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001074 /* make sure we have parsed some operation */
1075 if (!lybctx->op_node) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001076 LOGVAL(ctx, LYVE_DATA, "Missing the \"rpc\"/\"action\" node.");
Radek Krejci1798aae2020-07-14 13:26:06 +02001077 ret = LY_EVALID;
1078 goto cleanup;
1079 }
1080
1081 if (op_p) {
1082 *op_p = lybctx->op_node;
1083 }
1084 assert(tree);
Radek Krejci1798aae2020-07-14 13:26:06 +02001085
1086 } else if (data_type == LYD_INTOPT_NOTIF) {
1087 /* make sure we have parsed some notification */
1088 if (!lybctx->op_node) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001089 LOGVAL(ctx, LYVE_DATA, "Missing the \"notification\" node.");
Radek Krejci1798aae2020-07-14 13:26:06 +02001090 ret = LY_EVALID;
1091 goto cleanup;
1092 }
1093
1094 if (op_p) {
1095 *op_p = lybctx->op_node;
1096 }
1097 assert(tree);
1098 }
Michal Vasko60ea6352020-06-29 13:39:39 +02001099
1100cleanup:
Michal Vaskofe3a5542020-12-08 10:05:23 +01001101 if (ret) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001102 lyd_lyb_ctx_free((struct lyd_ctx *)lybctx);
Michal Vaskofe3a5542020-12-08 10:05:23 +01001103 lyd_free_all(tree);
Radek Krejci1798aae2020-07-14 13:26:06 +02001104 } else {
Michal Vaskofe3a5542020-12-08 10:05:23 +01001105 if (lydctx_p) {
1106 *lydctx_p = (struct lyd_ctx *)lybctx;
1107 } else {
1108 lyd_lyb_ctx_free((struct lyd_ctx *)lybctx);
1109 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001110 if (tree_p) {
1111 *tree_p = tree;
1112 }
Michal Vasko60ea6352020-06-29 13:39:39 +02001113 }
1114 return ret;
1115}
1116
1117LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001118lyd_parse_lyb_data(const struct ly_ctx *ctx, struct ly_in *in, uint32_t parse_options, uint32_t validate_options,
Radek Krejci0f969882020-08-21 16:56:47 +02001119 struct lyd_node **tree_p, struct lyd_ctx **lydctx_p)
Michal Vasko60ea6352020-06-29 13:39:39 +02001120{
Radek Krejci1798aae2020-07-14 13:26:06 +02001121 return lyd_parse_lyb_(ctx, NULL, in, parse_options, validate_options, 0, tree_p, NULL, lydctx_p);
Michal Vasko60ea6352020-06-29 13:39:39 +02001122}
1123
1124LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +02001125lyd_parse_lyb_rpc(const struct ly_ctx *ctx, struct ly_in *in, struct lyd_node **tree_p, struct lyd_node **op_p)
Michal Vasko60ea6352020-06-29 13:39:39 +02001126{
Radek Krejci1798aae2020-07-14 13:26:06 +02001127 return lyd_parse_lyb_(ctx, NULL, in, LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, LYD_INTOPT_RPC, tree_p, op_p, NULL);
Michal Vasko60ea6352020-06-29 13:39:39 +02001128}
1129
1130LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +02001131lyd_parse_lyb_notif(const struct ly_ctx *ctx, struct ly_in *in, struct lyd_node **tree_p, struct lyd_node **ntf_p)
Michal Vasko60ea6352020-06-29 13:39:39 +02001132{
Radek Krejci1798aae2020-07-14 13:26:06 +02001133 return lyd_parse_lyb_(ctx, NULL, in, LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, LYD_INTOPT_NOTIF, tree_p, ntf_p, NULL);
1134}
Michal Vasko60ea6352020-06-29 13:39:39 +02001135
Radek Krejci1798aae2020-07-14 13:26:06 +02001136LY_ERR
Michal Vasko2552ea32020-12-08 15:32:34 +01001137lyd_parse_lyb_reply(const struct ly_ctx *ctx, struct ly_in *in, struct lyd_node **tree_p, struct lyd_node **op_p)
Radek Krejci1798aae2020-07-14 13:26:06 +02001138{
Michal Vasko2552ea32020-12-08 15:32:34 +01001139 return lyd_parse_lyb_(ctx, NULL, in, LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, LYD_INTOPT_REPLY, tree_p, op_p, NULL);
Michal Vasko60ea6352020-06-29 13:39:39 +02001140}
1141
1142API int
1143lyd_lyb_data_length(const char *data)
1144{
1145 LY_ERR ret = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001146 struct lylyb_ctx *lybctx;
Michal Vasko60ea6352020-06-29 13:39:39 +02001147 int count, i;
1148 size_t len;
1149 uint8_t buf[LYB_SIZE_MAX];
1150
1151 if (!data) {
1152 return -1;
1153 }
1154
Radek Krejci1798aae2020-07-14 13:26:06 +02001155 lybctx = calloc(1, sizeof *lybctx);
1156 LY_CHECK_ERR_RET(!lybctx, LOGMEM(NULL), LY_EMEM);
1157 ret = ly_in_new_memory(data, &lybctx->in);
Michal Vasko63f3d842020-07-08 10:10:14 +02001158 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001159
1160 /* read magic number */
Radek Krejci1798aae2020-07-14 13:26:06 +02001161 ret = lyb_parse_magic_number(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001162 LY_CHECK_GOTO(ret, cleanup);
1163
1164 /* read header */
Radek Krejci1798aae2020-07-14 13:26:06 +02001165 ret = lyb_parse_header(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001166 LY_CHECK_GOTO(ret, cleanup);
1167
1168 /* read model count */
Radek Krejci1798aae2020-07-14 13:26:06 +02001169 lyb_read_number(&count, sizeof count, 2, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001170
1171 /* read all models */
1172 for (i = 0; i < count; ++i) {
1173 /* module name length */
1174 len = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +02001175 lyb_read_number(&len, sizeof len, 2, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001176
1177 /* model name */
Radek Krejci1798aae2020-07-14 13:26:06 +02001178 lyb_read(buf, len, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001179
1180 /* revision */
Radek Krejci1798aae2020-07-14 13:26:06 +02001181 lyb_read(buf, 2, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001182 }
1183
Radek Krejci1798aae2020-07-14 13:26:06 +02001184 while (lybctx->in->current[0]) {
Michal Vasko60ea6352020-06-29 13:39:39 +02001185 /* register a new subtree */
Radek Krejci1798aae2020-07-14 13:26:06 +02001186 ret = lyb_read_start_subtree(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001187 LY_CHECK_GOTO(ret, cleanup);
1188
1189 /* skip it */
Radek Krejci1798aae2020-07-14 13:26:06 +02001190 lyb_skip_subtree(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001191
1192 /* subtree finished */
Radek Krejci1798aae2020-07-14 13:26:06 +02001193 ret = lyb_read_stop_subtree(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001194 LY_CHECK_GOTO(ret, cleanup);
1195 }
1196
1197 /* read the last zero, parsing finished */
Radek Krejci1798aae2020-07-14 13:26:06 +02001198 ly_in_skip(lybctx->in, 1);
Michal Vasko60ea6352020-06-29 13:39:39 +02001199
1200cleanup:
Radek Krejci1798aae2020-07-14 13:26:06 +02001201 count = lybctx->in->current - lybctx->in->start;
Michal Vasko63f3d842020-07-08 10:10:14 +02001202
Radek Krejci1798aae2020-07-14 13:26:06 +02001203 ly_in_free(lybctx->in, 0);
1204 lylyb_ctx_free(lybctx);
1205
Michal Vasko63f3d842020-07-08 10:10:14 +02001206 return ret ? -1 : count;
Michal Vasko60ea6352020-06-29 13:39:39 +02001207}