blob: b011d8a9336f74f342413f819749c8864fb00661 [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 Krejci77114102021-03-10 15:21:57 +010033#include "set.h"
Radek Krejciad97c5f2020-06-30 09:19:28 +020034#include "tree.h"
Radek Krejci47fab892020-11-05 17:02:41 +010035#include "tree_data.h"
Michal Vasko60ea6352020-06-29 13:39:39 +020036#include "tree_data_internal.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010037#include "tree_edit.h"
Michal Vasko60ea6352020-06-29 13:39:39 +020038#include "tree_schema.h"
39#include "validation.h"
Michal Vasko6b5cb2a2020-11-11 19:11:21 +010040#include "xml.h"
Michal Vasko60ea6352020-06-29 13:39:39 +020041
Radek Krejci1798aae2020-07-14 13:26:06 +020042void
43lylyb_ctx_free(struct lylyb_ctx *ctx)
44{
45 LY_ARRAY_COUNT_TYPE u;
46
47 LY_ARRAY_FREE(ctx->subtrees);
48 LY_ARRAY_FREE(ctx->models);
49
50 LY_ARRAY_FOR(ctx->sib_hts, u) {
51 lyht_free(ctx->sib_hts[u].ht);
52 }
53 LY_ARRAY_FREE(ctx->sib_hts);
54
55 free(ctx);
56}
57
58void
59lyd_lyb_ctx_free(struct lyd_ctx *lydctx)
60{
61 struct lyd_lyb_ctx *ctx = (struct lyd_lyb_ctx *)lydctx;
62
63 lyd_ctx_free(lydctx);
64 lylyb_ctx_free(ctx->lybctx);
65 free(ctx);
66}
67
Michal Vasko60ea6352020-06-29 13:39:39 +020068/**
69 * @brief Read YANG data from LYB input. Metadata are handled transparently and not returned.
70 *
71 * @param[in] buf Destination buffer.
72 * @param[in] count Number of bytes to read.
73 * @param[in] lybctx LYB context.
74 */
75static void
Radek Krejci1798aae2020-07-14 13:26:06 +020076lyb_read(uint8_t *buf, size_t count, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +020077{
Michal Vaskofd69e1d2020-07-03 11:57:17 +020078 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +020079 struct lyd_lyb_subtree *empty;
80 size_t to_read;
81 uint8_t meta_buf[LYB_META_BYTES];
82
83 assert(lybctx);
84
85 while (1) {
86 /* check for fully-read (empty) data chunks */
87 to_read = count;
88 empty = NULL;
89 LY_ARRAY_FOR(lybctx->subtrees, u) {
90 /* we want the innermost chunks resolved first, so replace previous empty chunks,
91 * also ignore chunks that are completely finished, there is nothing for us to do */
92 if ((lybctx->subtrees[u].written <= to_read) && lybctx->subtrees[u].position) {
93 /* empty chunk, do not read more */
94 to_read = lybctx->subtrees[u].written;
95 empty = &lybctx->subtrees[u];
96 }
97 }
98
99 if (!empty && !count) {
100 break;
101 }
102
103 /* we are actually reading some data, not just finishing another chunk */
104 if (to_read) {
105 if (buf) {
Michal Vasko63f3d842020-07-08 10:10:14 +0200106 ly_in_read(lybctx->in, buf, to_read);
107 } else {
108 ly_in_skip(lybctx->in, to_read);
Michal Vasko60ea6352020-06-29 13:39:39 +0200109 }
110
111 LY_ARRAY_FOR(lybctx->subtrees, u) {
112 /* decrease all written counters */
113 lybctx->subtrees[u].written -= to_read;
114 assert(lybctx->subtrees[u].written <= LYB_SIZE_MAX);
115 }
116 /* decrease count/buf */
117 count -= to_read;
118 if (buf) {
119 buf += to_read;
120 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200121 }
122
123 if (empty) {
124 /* read the next chunk meta information */
Michal Vasko63f3d842020-07-08 10:10:14 +0200125 ly_in_read(lybctx->in, meta_buf, LYB_META_BYTES);
Michal Vasko60ea6352020-06-29 13:39:39 +0200126 empty->written = meta_buf[0];
127 empty->inner_chunks = meta_buf[1];
128
129 /* remember whether there is a following chunk or not */
130 empty->position = (empty->written == LYB_SIZE_MAX ? 1 : 0);
Michal Vasko60ea6352020-06-29 13:39:39 +0200131 }
132 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200133}
134
135/**
136 * @brief Read a number.
137 *
138 * @param[in] num Destination buffer.
139 * @param[in] num_size Size of @p num.
140 * @param[in] bytes Number of bytes to read.
141 * @param[in] lybctx LYB context.
142 */
143static void
Radek Krejci1798aae2020-07-14 13:26:06 +0200144lyb_read_number(void *num, size_t num_size, size_t bytes, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200145{
146 uint64_t buf = 0;
147
148 lyb_read((uint8_t *)&buf, bytes, lybctx);
149
150 /* correct byte order */
151 buf = le64toh(buf);
152
153 switch (num_size) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100154 case sizeof(uint8_t):
Michal Vasko60ea6352020-06-29 13:39:39 +0200155 *((uint8_t *)num) = buf;
156 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100157 case sizeof(uint16_t):
Michal Vasko60ea6352020-06-29 13:39:39 +0200158 *((uint16_t *)num) = buf;
159 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100160 case sizeof(uint32_t):
Michal Vasko60ea6352020-06-29 13:39:39 +0200161 *((uint32_t *)num) = buf;
162 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100163 case sizeof(uint64_t):
Michal Vasko60ea6352020-06-29 13:39:39 +0200164 *((uint64_t *)num) = buf;
165 break;
166 default:
167 LOGINT(lybctx->ctx);
168 }
169}
170
171/**
172 * @brief Read a string.
173 *
174 * @param[in] str Destination buffer, is allocated.
175 * @param[in] with_length Whether the string is preceded with its length or it ends at the end of this subtree.
176 * @param[in] lybctx LYB context.
177 * @return LY_ERR value.
178 */
179static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200180lyb_read_string(char **str, ly_bool with_length, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200181{
Radek Krejci857189e2020-09-01 13:26:36 +0200182 ly_bool next_chunk = 0;
Michal Vasko60ea6352020-06-29 13:39:39 +0200183 size_t len = 0, cur_len;
184
185 *str = NULL;
186
187 if (with_length) {
188 lyb_read_number(&len, sizeof len, 2, lybctx);
189 } else {
190 /* read until the end of this subtree */
191 len = LYB_LAST_SUBTREE(lybctx).written;
192 if (LYB_LAST_SUBTREE(lybctx).position) {
193 next_chunk = 1;
194 }
195 }
196
197 *str = malloc((len + 1) * sizeof **str);
198 LY_CHECK_ERR_RET(!*str, LOGMEM(lybctx->ctx), LY_EMEM);
199
200 lyb_read((uint8_t *)*str, len, lybctx);
201
202 while (next_chunk) {
203 cur_len = LYB_LAST_SUBTREE(lybctx).written;
204 if (LYB_LAST_SUBTREE(lybctx).position) {
205 next_chunk = 1;
206 } else {
207 next_chunk = 0;
208 }
209
210 *str = ly_realloc(*str, (len + cur_len + 1) * sizeof **str);
211 LY_CHECK_ERR_RET(!*str, LOGMEM(lybctx->ctx), LY_EMEM);
212
213 lyb_read(((uint8_t *)*str) + len, cur_len, lybctx);
214
215 len += cur_len;
216 }
217
218 ((char *)*str)[len] = '\0';
219 return LY_SUCCESS;
220}
221
222/**
223 * @brief Stop the current subtree - change LYB context state.
224 *
225 * @param[in] lybctx LYB context.
226 * @return LY_ERR value.
227 */
228static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200229lyb_read_stop_subtree(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200230{
231 if (LYB_LAST_SUBTREE(lybctx).written) {
232 LOGINT_RET(lybctx->ctx);
233 }
234
235 LY_ARRAY_DECREMENT(lybctx->subtrees);
236 return LY_SUCCESS;
237}
238
239/**
240 * @brief Start a new subtree - change LYB context state but also read the expected metadata.
241 *
242 * @param[in] lybctx LYB context.
243 * @return LY_ERR value.
244 */
245static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200246lyb_read_start_subtree(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200247{
248 uint8_t meta_buf[LYB_META_BYTES];
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200249 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +0200250
Radek Krejcic7d13e32020-12-09 12:32:24 +0100251 u = LY_ARRAY_COUNT(lybctx->subtrees);
Michal Vasko60ea6352020-06-29 13:39:39 +0200252 if (u == lybctx->subtree_size) {
253 LY_ARRAY_CREATE_RET(lybctx->ctx, lybctx->subtrees, u + LYB_SUBTREE_STEP, LY_EMEM);
254 lybctx->subtree_size = u + LYB_SUBTREE_STEP;
255 }
256
Michal Vasko63f3d842020-07-08 10:10:14 +0200257 LY_CHECK_RET(ly_in_read(lybctx->in, meta_buf, LYB_META_BYTES));
Michal Vasko60ea6352020-06-29 13:39:39 +0200258
259 LY_ARRAY_INCREMENT(lybctx->subtrees);
260 LYB_LAST_SUBTREE(lybctx).written = meta_buf[0];
261 LYB_LAST_SUBTREE(lybctx).inner_chunks = meta_buf[LYB_SIZE_BYTES];
262 LYB_LAST_SUBTREE(lybctx).position = (LYB_LAST_SUBTREE(lybctx).written == LYB_SIZE_MAX ? 1 : 0);
263
Michal Vasko60ea6352020-06-29 13:39:39 +0200264 return LY_SUCCESS;
265}
266
267/**
268 * @brief Parse YANG model info.
269 *
270 * @param[in] lybctx LYB context.
271 * @param[out] mod Parsed module.
272 * @return LY_ERR value.
273 */
274static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200275lyb_parse_model(struct lylyb_ctx *lybctx, uint32_t parse_options, const struct lys_module **mod)
Michal Vasko60ea6352020-06-29 13:39:39 +0200276{
277 LY_ERR ret = LY_SUCCESS;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100278 char *mod_name = NULL, mod_rev[LY_REV_SIZE];
Michal Vasko60ea6352020-06-29 13:39:39 +0200279 uint16_t rev;
280
281 /* model name */
282 ret = lyb_read_string(&mod_name, 1, lybctx);
283 LY_CHECK_GOTO(ret, cleanup);
284
285 /* revision */
286 lyb_read_number(&rev, sizeof rev, 2, lybctx);
287
288 if (!mod_name[0]) {
289 /* opaq node, no module */
290 *mod = NULL;
291 goto cleanup;
292 }
293
294 if (rev) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100295 sprintf(mod_rev, "%04u-%02u-%02u", ((rev & LYB_REV_YEAR_MASK) >> LYB_REV_YEAR_SHIFT) + LYB_REV_YEAR_OFFSET,
296 (rev & LYB_REV_MONTH_MASK) >> LYB_REV_MONTH_SHIFT, rev & LYB_REV_DAY_MASK);
Michal Vasko60ea6352020-06-29 13:39:39 +0200297 *mod = ly_ctx_get_module(lybctx->ctx, mod_name, mod_rev);
Radek Krejci1798aae2020-07-14 13:26:06 +0200298 if ((parse_options & LYD_PARSE_LYB_MOD_UPDATE) && !(*mod)) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200299 /* try to use an updated module */
300 *mod = ly_ctx_get_module_implemented(lybctx->ctx, mod_name);
301 if (*mod && (!(*mod)->revision || (strcmp((*mod)->revision, mod_rev) < 0))) {
302 /* not an implemented module in a newer revision */
303 *mod = NULL;
304 }
305 }
306 } else {
307 *mod = ly_ctx_get_module_latest(lybctx->ctx, mod_name);
308 }
309 /* TODO data_clb supported?
310 if (lybctx->ctx->data_clb) {
311 if (!*mod) {
312 *mod = lybctx->ctx->data_clb(lybctx->ctx, mod_name, NULL, 0, lybctx->ctx->data_clb_data);
313 } else if (!(*mod)->implemented) {
314 *mod = lybctx->ctx->data_clb(lybctx->ctx, mod_name, (*mod)->ns, LY_MODCLB_NOT_IMPLEMENTED, lybctx->ctx->data_clb_data);
315 }
316 }*/
317
318 if (!*mod || !(*mod)->implemented) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200319 if (parse_options & LYD_PARSE_STRICT) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200320 if (!*mod) {
321 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid context for LYB data parsing, missing module \"%s%s%s\".",
Michal Vasko69730152020-10-09 16:30:07 +0200322 mod_name, rev ? "@" : "", rev ? mod_rev : "");
Michal Vasko60ea6352020-06-29 13:39:39 +0200323 } else if (!(*mod)->implemented) {
324 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid context for LYB data parsing, module \"%s%s%s\" not implemented.",
Michal Vasko69730152020-10-09 16:30:07 +0200325 mod_name, rev ? "@" : "", rev ? mod_rev : "");
Michal Vasko60ea6352020-06-29 13:39:39 +0200326 }
327 ret = LY_EINVAL;
328 goto cleanup;
329 }
330
331 }
332
Michal Vasko11f76c82021-04-15 14:36:14 +0200333 /* fill cached hashes, if not already */
334 lyb_cache_module_hash(*mod);
335
Michal Vasko60ea6352020-06-29 13:39:39 +0200336cleanup:
337 free(mod_name);
338 return ret;
339}
340
341/**
342 * @brief Parse YANG node metadata.
343 *
344 * @param[in] lybctx LYB context.
Michal Vasko60ea6352020-06-29 13:39:39 +0200345 * @param[out] meta Parsed metadata.
346 * @return LY_ERR value.
347 */
348static LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200349lyb_parse_metadata(struct lyd_lyb_ctx *lybctx, struct lyd_meta **meta)
Michal Vasko60ea6352020-06-29 13:39:39 +0200350{
351 LY_ERR ret = LY_SUCCESS;
Radek Krejci857189e2020-09-01 13:26:36 +0200352 ly_bool dynamic;
Michal Vasko60ea6352020-06-29 13:39:39 +0200353 uint8_t i, count = 0;
Michal Vasko1e5d5612020-07-03 13:29:26 +0200354 char *meta_name = NULL, *meta_value;
Michal Vasko60ea6352020-06-29 13:39:39 +0200355 const struct lys_module *mod;
356
357 /* read number of attributes stored */
Radek Krejci1798aae2020-07-14 13:26:06 +0200358 lyb_read(&count, 1, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200359
360 /* read attributes */
361 for (i = 0; i < count; ++i) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200362 ret = lyb_read_start_subtree(lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200363 LY_CHECK_GOTO(ret, cleanup);
364
365 /* find model */
Michal Vaskoe0665742021-02-11 11:08:44 +0100366 ret = lyb_parse_model(lybctx->lybctx, lybctx->parse_opts, &mod);
Michal Vasko60ea6352020-06-29 13:39:39 +0200367 LY_CHECK_GOTO(ret, cleanup);
368
369 if (!mod) {
370 /* skip it */
371 do {
Radek Krejci1798aae2020-07-14 13:26:06 +0200372 lyb_read(NULL, LYB_LAST_SUBTREE(lybctx->lybctx).written, lybctx->lybctx);
373 } while (LYB_LAST_SUBTREE(lybctx->lybctx).written);
Michal Vasko60ea6352020-06-29 13:39:39 +0200374 goto stop_subtree;
375 }
376
377 /* meta name */
Radek Krejci1798aae2020-07-14 13:26:06 +0200378 ret = lyb_read_string(&meta_name, 1, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200379 LY_CHECK_GOTO(ret, cleanup);
380
381 /* meta value */
Radek Krejci1798aae2020-07-14 13:26:06 +0200382 ret = lyb_read_string(&meta_value, 0, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200383 LY_CHECK_GOTO(ret, cleanup);
384 dynamic = 1;
385
386 /* create metadata */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200387 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 +0200388 ly_strlen(meta_value), &dynamic, LY_PREF_JSON, NULL, LYD_HINT_DATA);
Michal Vasko60ea6352020-06-29 13:39:39 +0200389
390 /* free strings */
391 free(meta_name);
392 meta_name = NULL;
393 if (dynamic) {
394 free(meta_value);
395 dynamic = 0;
396 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200397
Radek Krejci1798aae2020-07-14 13:26:06 +0200398 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200399
400stop_subtree:
Radek Krejci1798aae2020-07-14 13:26:06 +0200401 ret = lyb_read_stop_subtree(lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200402 LY_CHECK_GOTO(ret, cleanup);
403 }
404
405cleanup:
406 free(meta_name);
Michal Vasko60ea6352020-06-29 13:39:39 +0200407 if (ret) {
Michal Vasko3a41dff2020-07-15 14:30:28 +0200408 lyd_free_meta_siblings(*meta);
Michal Vasko60ea6352020-06-29 13:39:39 +0200409 *meta = NULL;
410 }
411 return ret;
412}
413
414/**
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100415 * @brief Parse format-specific prefix data.
Michal Vasko60ea6352020-06-29 13:39:39 +0200416 *
417 * @param[in] lybctx LYB context.
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100418 * @param[in] format Prefix data format.
419 * @param[out] prefix_data Parsed prefix data.
Michal Vasko60ea6352020-06-29 13:39:39 +0200420 * @return LY_ERR value.
421 */
422static LY_ERR
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100423lyb_parse_prefix_data(struct lylyb_ctx *lybctx, LY_PREFIX_FORMAT format, void **prefix_data)
Michal Vasko60ea6352020-06-29 13:39:39 +0200424{
425 LY_ERR ret = LY_SUCCESS;
426 uint8_t count, i;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100427 struct ly_set *set = NULL;
428 struct lyxml_ns *ns = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200429
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100430 switch (format) {
431 case LY_PREF_XML:
432 /* read count */
433 lyb_read(&count, 1, lybctx);
434 if (!count) {
435 return LY_SUCCESS;
436 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200437
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100438 /* read all NS elements */
439 LY_CHECK_GOTO(ret = ly_set_new(&set), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200440
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100441 for (i = 0; i < count; ++i) {
442 ns = calloc(1, sizeof *ns);
Michal Vasko60ea6352020-06-29 13:39:39 +0200443
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100444 /* prefix */
445 LY_CHECK_GOTO(ret = lyb_read_string(&ns->prefix, 1, lybctx), cleanup);
446
447 /* namespace */
448 LY_CHECK_GOTO(ret = lyb_read_string(&ns->uri, 1, lybctx), cleanup);
449
450 LY_CHECK_GOTO(ret = ly_set_add(set, ns, 1, NULL), cleanup);
451 ns = NULL;
452 }
453
454 *prefix_data = set;
455 break;
456 case LY_PREF_JSON:
457 /* nothing stored */
458 break;
459 default:
460 LOGINT(lybctx->ctx);
461 ret = LY_EINT;
462 break;
Michal Vasko60ea6352020-06-29 13:39:39 +0200463 }
464
465cleanup:
466 if (ret) {
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100467 ly_free_prefix_data(format, set);
468 if (ns) {
469 free(ns->prefix);
470 free(ns->uri);
471 free(ns);
472 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200473 }
474 return ret;
475}
476
477/**
478 * @brief Parse opaque attributes.
479 *
480 * @param[in] lybctx LYB context.
481 * @param[out] attr Parsed attributes.
482 * @return LY_ERR value.
483 */
484static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200485lyb_parse_attributes(struct lylyb_ctx *lybctx, struct lyd_attr **attr)
Michal Vasko60ea6352020-06-29 13:39:39 +0200486{
487 LY_ERR ret = LY_SUCCESS;
488 uint8_t count, i;
Radek Krejci1798aae2020-07-14 13:26:06 +0200489 struct lyd_attr *attr2;
490 char *prefix = NULL, *module_name = NULL, *name = NULL, *value = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +0200491 ly_bool dynamic = 0;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100492 LY_PREFIX_FORMAT format = 0;
493 void *val_prefix_data = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200494
495 /* read count */
496 lyb_read(&count, 1, lybctx);
497
498 /* read attributes */
499 for (i = 0; i < count; ++i) {
500 ret = lyb_read_start_subtree(lybctx);
501 LY_CHECK_GOTO(ret, cleanup);
502
Michal Vasko0fdcd242020-11-11 19:12:30 +0100503 /* prefix, may be empty */
Michal Vasko60ea6352020-06-29 13:39:39 +0200504 ret = lyb_read_string(&prefix, 1, lybctx);
505 LY_CHECK_GOTO(ret, cleanup);
506 if (!prefix[0]) {
507 free(prefix);
508 prefix = NULL;
509 }
510
511 /* namespace, may be empty */
Radek Krejci1798aae2020-07-14 13:26:06 +0200512 ret = lyb_read_string(&module_name, 1, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200513 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200514 if (!module_name[0]) {
515 free(module_name);
516 module_name = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200517 }
518
519 /* name */
520 ret = lyb_read_string(&name, 1, lybctx);
521 LY_CHECK_GOTO(ret, cleanup);
522
Michal Vasko60ea6352020-06-29 13:39:39 +0200523 /* format */
524 lyb_read((uint8_t *)&format, 1, lybctx);
525
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100526 /* value prefixes */
527 ret = lyb_parse_prefix_data(lybctx, format, &val_prefix_data);
528 LY_CHECK_GOTO(ret, cleanup);
529
Michal Vasko60ea6352020-06-29 13:39:39 +0200530 /* value */
531 ret = lyb_read_string(&value, 0, lybctx);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100532 LY_CHECK_ERR_GOTO(ret, ly_free_prefix_data(format, val_prefix_data), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200533 dynamic = 1;
534
535 /* attr2 is always changed to the created attribute */
Michal Vasko501af032020-11-11 20:27:44 +0100536 ret = lyd_create_attr(NULL, &attr2, lybctx->ctx, name, strlen(name), prefix, ly_strlen(prefix), module_name,
537 ly_strlen(module_name), value, ly_strlen(value), &dynamic, format, val_prefix_data, 0);
Michal Vasko60ea6352020-06-29 13:39:39 +0200538 LY_CHECK_GOTO(ret, cleanup);
539
540 free(prefix);
541 prefix = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +0200542 free(module_name);
543 module_name = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200544 free(name);
545 name = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200546 assert(!dynamic);
547 value = NULL;
548
549 if (!*attr) {
550 *attr = attr2;
551 }
552
553 ret = lyb_read_stop_subtree(lybctx);
554 LY_CHECK_GOTO(ret, cleanup);
555 }
556
557cleanup:
558 free(prefix);
Radek Krejci1798aae2020-07-14 13:26:06 +0200559 free(module_name);
Michal Vasko60ea6352020-06-29 13:39:39 +0200560 free(name);
561 if (dynamic) {
562 free(value);
563 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200564 if (ret) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200565 lyd_free_attr_siblings(lybctx->ctx, *attr);
Michal Vasko60ea6352020-06-29 13:39:39 +0200566 *attr = NULL;
567 }
568 return ret;
569}
570
571/**
572 * @brief Check whether a schema node matches a hash(es).
573 *
574 * @param[in] sibling Schema node to check.
575 * @param[in] hash Hash array to check.
576 * @param[in] hash_count Number of hashes in @p hash.
577 * @return non-zero if matches,
578 * @return 0 if not.
579 */
580static int
581lyb_is_schema_hash_match(struct lysc_node *sibling, LYB_HASH *hash, uint8_t hash_count)
582{
583 LYB_HASH sibling_hash;
584 uint8_t i;
585
586 /* compare all the hashes starting from collision ID 0 */
587 for (i = 0; i < hash_count; ++i) {
Michal Vasko11f76c82021-04-15 14:36:14 +0200588 sibling_hash = lyb_get_hash(sibling, i);
Michal Vasko60ea6352020-06-29 13:39:39 +0200589 if (sibling_hash != hash[i]) {
590 return 0;
591 }
592 }
593
594 return 1;
595}
596
597/**
Michal Vasko60ea6352020-06-29 13:39:39 +0200598 * @brief Parse schema node hash.
599 *
600 * @param[in] lybctx LYB context.
601 * @param[in] sparent Schema parent, must be set if @p mod is not.
602 * @param[in] mod Module of the top-level node, must be set if @p sparent is not.
603 * @param[out] snode Parsed found schema node, may be NULL if opaque.
604 * @return LY_ERR value.
605 */
606static LY_ERR
607lyb_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 +0200608 const struct lysc_node **snode)
Michal Vasko60ea6352020-06-29 13:39:39 +0200609{
610 LY_ERR ret;
611 uint8_t i, j;
612 const struct lysc_node *sibling;
613 LYB_HASH hash[LYB_HASH_BITS - 1];
Radek Krejci1deb5be2020-08-26 16:43:36 +0200614 uint32_t getnext_opts;
Michal Vasko60ea6352020-06-29 13:39:39 +0200615
616 *snode = NULL;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100617 getnext_opts = lybctx->int_opts & LYD_INTOPT_REPLY ? LYS_GETNEXT_OUTPUT : 0;
Michal Vasko60ea6352020-06-29 13:39:39 +0200618
619 /* read the first hash */
Radek Krejci1798aae2020-07-14 13:26:06 +0200620 lyb_read(&hash[0], sizeof *hash, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200621
622 if (!hash[0]) {
623 /* opaque node */
624 return LY_SUCCESS;
625 }
626
627 /* based on the first hash read all the other ones, if any */
628 for (i = 0; !(hash[0] & (LYB_HASH_COLLISION_ID >> i)); ++i) {
629 if (i > LYB_HASH_BITS) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200630 LOGINT_RET(lybctx->lybctx->ctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200631 }
632 }
633
634 /* move the first hash on its accurate position */
635 hash[i] = hash[0];
636
637 /* read the rest of hashes */
638 for (j = i; j; --j) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200639 lyb_read(&hash[j - 1], sizeof *hash, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200640
641 /* correct collision ID */
642 assert(hash[j - 1] & (LYB_HASH_COLLISION_ID >> (j - 1)));
643 /* preceded with zeros */
644 assert(!(hash[j - 1] & (LYB_HASH_MASK << (LYB_HASH_BITS - (j - 1)))));
645 }
646
647 /* find our node with matching hashes */
648 sibling = NULL;
Radek Krejcif16e2542021-02-17 15:39:23 +0100649 while (1) {
650 if (!sparent && lybctx->ext) {
651 sibling = lys_getnext_ext(sibling, sparent, lybctx->ext, getnext_opts);
652 } else {
653 sibling = lys_getnext(sibling, sparent, mod ? mod->compiled : NULL, getnext_opts);
654 }
655 if (!sibling) {
656 break;
657 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200658 /* skip schema nodes from models not present during printing */
Michal Vasko69730152020-10-09 16:30:07 +0200659 if (lyb_has_schema_model(sibling, lybctx->lybctx->models) &&
660 lyb_is_schema_hash_match((struct lysc_node *)sibling, hash, i + 1)) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200661 /* match found */
662 break;
663 }
664 }
665
Michal Vaskoe0665742021-02-11 11:08:44 +0100666 if (!sibling && (lybctx->parse_opts & LYD_PARSE_STRICT)) {
Radek Krejcif16e2542021-02-17 15:39:23 +0100667 if (lybctx->ext) {
668 LOGVAL(lybctx->lybctx->ctx, LYVE_REFERENCE, "Failed to find matching hash for a node from \"%s\" extension instance node.",
669 lybctx->ext->def->name);
670 } else if (mod) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100671 LOGVAL(lybctx->lybctx->ctx, LYVE_REFERENCE, "Failed to find matching hash for a top-level node"
Michal Vasko69730152020-10-09 16:30:07 +0200672 " from \"%s\".", mod->name);
Michal Vasko60ea6352020-06-29 13:39:39 +0200673 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100674 LOGVAL(lybctx->lybctx->ctx, LYVE_REFERENCE, "Failed to find matching hash for a child node"
Michal Vasko69730152020-10-09 16:30:07 +0200675 " of \"%s\".", sparent->name);
Michal Vasko60ea6352020-06-29 13:39:39 +0200676 }
677 return LY_EVALID;
Radek Krejci1798aae2020-07-14 13:26:06 +0200678 } else if (sibling && (ret = lyd_parser_check_schema((struct lyd_ctx *)lybctx, sibling))) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200679 return ret;
680 }
681
682 *snode = sibling;
683 return LY_SUCCESS;
684}
685
686/**
687 * @brief Read until the end of the current subtree.
688 *
689 * @param[in] lybctx LYB context.
690 */
691static void
Radek Krejci1798aae2020-07-14 13:26:06 +0200692lyb_skip_subtree(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200693{
Michal Vasko60ea6352020-06-29 13:39:39 +0200694 do {
695 /* first skip any meta information inside */
Michal Vasko63f3d842020-07-08 10:10:14 +0200696 ly_in_skip(lybctx->in, LYB_LAST_SUBTREE(lybctx).inner_chunks * LYB_META_BYTES);
Michal Vasko60ea6352020-06-29 13:39:39 +0200697
698 /* then read data */
699 lyb_read(NULL, LYB_LAST_SUBTREE(lybctx).written, lybctx);
700 } while (LYB_LAST_SUBTREE(lybctx).written);
701}
702
703/**
704 * @brief Parse LYB subtree.
705 *
706 * @param[in] lybctx LYB context.
707 * @param[in] parent Data parent of the subtree, must be set if @p first is not.
708 * @param[in,out] first First top-level sibling, must be set if @p parent is not.
709 * @return LY_ERR value.
710 */
711static LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +0100712lyb_parse_subtree_r(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, struct lyd_node **first_p, struct ly_set *parsed)
Michal Vasko60ea6352020-06-29 13:39:39 +0200713{
714 LY_ERR ret = LY_SUCCESS;
715 struct lyd_node *node = NULL, *tree;
716 const struct lys_module *mod;
717 const struct lysc_node *snode = NULL;
718 struct lyd_meta *meta = NULL, *m;
Radek Krejci1798aae2020-07-14 13:26:06 +0200719 struct lyd_attr *attr = NULL, *a;
Michal Vasko60ea6352020-06-29 13:39:39 +0200720 LYD_ANYDATA_VALUETYPE value_type;
Radek Krejci1798aae2020-07-14 13:26:06 +0200721 char *value = NULL, *name = NULL, *prefix = NULL, *module_key = NULL;
Michal Vasko9afe3df2021-02-05 16:33:50 +0100722 const char *val_dict;
Radek Krejci857189e2020-09-01 13:26:36 +0200723 ly_bool dynamic = 0;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100724 LY_PREFIX_FORMAT format = 0;
725 void *val_prefix_data = NULL;
Michal Vaskoc5e866a2020-11-04 17:09:26 +0100726 uint32_t prev_lo, flags;
Radek Krejci1798aae2020-07-14 13:26:06 +0200727 const struct ly_ctx *ctx = lybctx->lybctx->ctx;
Michal Vasko60ea6352020-06-29 13:39:39 +0200728
729 /* register a new subtree */
Radek Krejci1798aae2020-07-14 13:26:06 +0200730 LY_CHECK_GOTO(ret = lyb_read_start_subtree(lybctx->lybctx), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200731
732 if (!parent) {
733 /* top-level, read module name */
Michal Vaskoe0665742021-02-11 11:08:44 +0100734 ret = lyb_parse_model(lybctx->lybctx, lybctx->parse_opts, &mod);
Michal Vasko60ea6352020-06-29 13:39:39 +0200735 LY_CHECK_GOTO(ret, cleanup);
736
737 /* read hash, find the schema node starting from mod */
738 ret = lyb_parse_schema_hash(lybctx, NULL, mod, &snode);
739 LY_CHECK_GOTO(ret, cleanup);
740 } else {
741 /* read hash, find the schema node starting from parent schema */
742 ret = lyb_parse_schema_hash(lybctx, parent->schema, NULL, &snode);
743 LY_CHECK_GOTO(ret, cleanup);
744 }
745
Michal Vaskoe0665742021-02-11 11:08:44 +0100746 if (!snode && !(lybctx->parse_opts & LYD_PARSE_OPAQ)) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200747 /* unknown data, skip them */
Radek Krejci1798aae2020-07-14 13:26:06 +0200748 lyb_skip_subtree(lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200749 goto stop_subtree;
Radek Krejci0f969882020-08-21 16:56:47 +0200750 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200751
752 /* create metadata/attributes */
753 if (snode) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200754 ret = lyb_parse_metadata(lybctx, &meta);
Michal Vasko60ea6352020-06-29 13:39:39 +0200755 LY_CHECK_GOTO(ret, cleanup);
756 } else {
Radek Krejci1798aae2020-07-14 13:26:06 +0200757 ret = lyb_parse_attributes(lybctx->lybctx, &attr);
Michal Vasko60ea6352020-06-29 13:39:39 +0200758 LY_CHECK_GOTO(ret, cleanup);
759 }
760
Michal Vaskoc5e866a2020-11-04 17:09:26 +0100761 /* read flags */
762 lyb_read_number(&flags, sizeof flags, sizeof flags, lybctx->lybctx);
763
Michal Vasko60ea6352020-06-29 13:39:39 +0200764 if (!snode) {
765 /* parse prefix */
Radek Krejci1798aae2020-07-14 13:26:06 +0200766 ret = lyb_read_string(&prefix, 1, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200767 LY_CHECK_GOTO(ret, cleanup);
768
Radek Krejci1798aae2020-07-14 13:26:06 +0200769 /* parse module key */
770 ret = lyb_read_string(&module_key, 1, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200771 LY_CHECK_GOTO(ret, cleanup);
772
773 /* parse name */
Radek Krejci1798aae2020-07-14 13:26:06 +0200774 ret = lyb_read_string(&name, 1, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200775 LY_CHECK_GOTO(ret, cleanup);
776
Michal Vasko60ea6352020-06-29 13:39:39 +0200777 /* parse format */
Radek Krejci1798aae2020-07-14 13:26:06 +0200778 lyb_read((uint8_t *)&format, 1, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200779
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100780 /* parse value prefixes */
781 ret = lyb_parse_prefix_data(lybctx->lybctx, format, &val_prefix_data);
782 LY_CHECK_GOTO(ret, cleanup);
783
Michal Vasko60ea6352020-06-29 13:39:39 +0200784 /* parse value */
Radek Krejci1798aae2020-07-14 13:26:06 +0200785 ret = lyb_read_string(&value, 0, lybctx->lybctx);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100786 LY_CHECK_ERR_GOTO(ret, ly_free_prefix_data(format, val_prefix_data), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200787 dynamic = 1;
788
789 /* create node */
Michal Vasko501af032020-11-11 20:27:44 +0100790 ret = lyd_create_opaq(ctx, name, strlen(name), prefix, ly_strlen(prefix), module_key, ly_strlen(module_key),
791 value, strlen(value), &dynamic, format, val_prefix_data, 0, &node);
Michal Vasko60ea6352020-06-29 13:39:39 +0200792 LY_CHECK_GOTO(ret, cleanup);
793
794 /* process children */
Radek Krejci1798aae2020-07-14 13:26:06 +0200795 while (LYB_LAST_SUBTREE(lybctx->lybctx).written) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100796 ret = lyb_parse_subtree_r(lybctx, node, NULL, NULL);
Michal Vasko60ea6352020-06-29 13:39:39 +0200797 LY_CHECK_GOTO(ret, cleanup);
798 }
799 } else if (snode->nodetype & LYD_NODE_TERM) {
800 /* parse value */
Radek Krejci1798aae2020-07-14 13:26:06 +0200801 ret = lyb_read_string(&value, 0, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200802 LY_CHECK_GOTO(ret, cleanup);
803 dynamic = 1;
804
805 /* create node */
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200806 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 +0200807 NULL, LYD_HINT_DATA, &node);
Michal Vasko60ea6352020-06-29 13:39:39 +0200808 if (dynamic) {
809 free(value);
810 dynamic = 0;
811 }
812 value = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +0200813 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200814 } else if (snode->nodetype & LYD_NODE_INNER) {
815 /* create node */
816 ret = lyd_create_inner(snode, &node);
817 LY_CHECK_GOTO(ret, cleanup);
818
819 /* process children */
Radek Krejci1798aae2020-07-14 13:26:06 +0200820 while (LYB_LAST_SUBTREE(lybctx->lybctx).written) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100821 ret = lyb_parse_subtree_r(lybctx, node, NULL, NULL);
Michal Vasko60ea6352020-06-29 13:39:39 +0200822 LY_CHECK_GOTO(ret, cleanup);
823 }
824
Michal Vaskoe0665742021-02-11 11:08:44 +0100825 if (!(lybctx->parse_opts & LYD_PARSE_ONLY)) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200826 /* new node validation, autodelete CANNOT occur, all nodes are new */
Michal Vaskoe0665742021-02-11 11:08:44 +0100827 ret = lyd_validate_new(lyd_node_child_p(node), snode, NULL, NULL);
Michal Vasko60ea6352020-06-29 13:39:39 +0200828 LY_CHECK_GOTO(ret, cleanup);
829
830 /* add any missing default children */
Radek Krejci4f2e3e52021-03-30 14:20:28 +0200831 ret = lyd_new_implicit_r(node, lyd_node_child_p(node), NULL, NULL, &lybctx->node_when, &lybctx->node_exts,
Michal Vaskoc43c8ab2021-03-05 13:32:44 +0100832 &lybctx->node_types, (lybctx->val_opts & LYD_VALIDATE_NO_STATE) ? LYD_IMPLICIT_NO_STATE : 0, NULL);
Michal Vasko60ea6352020-06-29 13:39:39 +0200833 LY_CHECK_GOTO(ret, cleanup);
834 }
835
Michal Vasko751cb4d2020-07-14 12:25:28 +0200836 if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200837 /* rememeber the RPC/action/notification */
Radek Krejci1798aae2020-07-14 13:26:06 +0200838 lybctx->op_node = node;
Michal Vasko60ea6352020-06-29 13:39:39 +0200839 }
840 } else if (snode->nodetype & LYD_NODE_ANY) {
841 /* parse value type */
Radek Krejci1798aae2020-07-14 13:26:06 +0200842 lyb_read((uint8_t *)&value_type, sizeof value_type, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200843 if (value_type == LYD_ANYDATA_DATATREE) {
844 /* invalid situation */
Radek Krejci1798aae2020-07-14 13:26:06 +0200845 LOGINT(ctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200846 goto cleanup;
847 }
848
849 /* read anydata content */
Radek Krejci1798aae2020-07-14 13:26:06 +0200850 ret = lyb_read_string(&value, 0, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200851 LY_CHECK_GOTO(ret, cleanup);
852 dynamic = 1;
853
854 if (value_type == LYD_ANYDATA_LYB) {
855 /* turn logging off */
856 prev_lo = ly_log_options(0);
857
858 /* try to parse LYB into a data tree */
Radek Krejci1798aae2020-07-14 13:26:06 +0200859 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 +0200860 /* successfully parsed */
861 free(value);
862 value = (char *)tree;
863 value_type = LYD_ANYDATA_DATATREE;
864 }
Radek Krejci7931b192020-06-25 17:05:03 +0200865
866 /* turn logging on again */
867 ly_log_options(prev_lo);
Michal Vasko60ea6352020-06-29 13:39:39 +0200868 }
869
Michal Vasko9afe3df2021-02-05 16:33:50 +0100870 /* create the node */
871 switch (value_type) {
872 case LYD_ANYDATA_LYB:
873 case LYD_ANYDATA_DATATREE:
874 /* use the value directly */
875 ret = lyd_create_any(snode, value, value_type, 1, &node);
876 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200877
Michal Vasko9afe3df2021-02-05 16:33:50 +0100878 dynamic = 0;
879 value = NULL;
880 break;
881 case LYD_ANYDATA_STRING:
882 case LYD_ANYDATA_XML:
883 case LYD_ANYDATA_JSON:
884 /* value is expected to be in the dictionary */
885 ret = lydict_insert_zc(ctx, value, &val_dict);
886 LY_CHECK_GOTO(ret, cleanup);
887 dynamic = 0;
888 value = NULL;
889
890 /* use the value in the dictionary */
891 ret = lyd_create_any(snode, val_dict, value_type, 1, &node);
892 if (ret) {
893 lydict_remove(ctx, val_dict);
894 goto cleanup;
895 }
896 break;
897 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200898 }
899 assert(node);
900
Michal Vaskoc5e866a2020-11-04 17:09:26 +0100901 /* set flags */
902 node->flags = flags;
Michal Vasko60ea6352020-06-29 13:39:39 +0200903
904 /* add metadata/attributes */
905 if (snode) {
906 LY_LIST_FOR(meta, m) {
907 m->parent = node;
908 }
909 node->meta = meta;
910 meta = NULL;
911 } else {
912 assert(!node->schema);
913 LY_LIST_FOR(attr, a) {
914 a->parent = (struct lyd_node_opaq *)node;
915 }
916 ((struct lyd_node_opaq *)node)->attr = attr;
917 attr = NULL;
918 }
919
Michal Vaskob104f112020-07-17 09:54:54 +0200920 /* insert, keep first pointer correct */
Michal Vaskoe0665742021-02-11 11:08:44 +0100921 lyd_insert_node(parent, first_p, node);
922 while (!parent && (*first_p)->prev->next) {
923 *first_p = (*first_p)->prev;
924 }
925
926 /* rememeber a successfully parsed node */
927 if (parsed) {
928 ly_set_add(parsed, node, 1, NULL);
Michal Vaskob104f112020-07-17 09:54:54 +0200929 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200930 node = NULL;
931
932stop_subtree:
933 /* end the subtree */
Radek Krejci1798aae2020-07-14 13:26:06 +0200934 ret = lyb_read_stop_subtree(lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200935 LY_CHECK_GOTO(ret, cleanup);
936
937cleanup:
938 free(prefix);
Radek Krejci1798aae2020-07-14 13:26:06 +0200939 free(module_key);
Michal Vasko60ea6352020-06-29 13:39:39 +0200940 free(name);
941 if (dynamic) {
942 free(value);
943 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200944
Michal Vasko3a41dff2020-07-15 14:30:28 +0200945 lyd_free_meta_siblings(meta);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200946 lyd_free_attr_siblings(ctx, attr);
Michal Vasko60ea6352020-06-29 13:39:39 +0200947 lyd_free_tree(node);
948 return ret;
949}
950
951/**
952 * @brief Parse used YANG data models.
953 *
954 * @param[in] lybctx LYB context.
955 * @return LY_ERR value.
956 */
957static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200958lyb_parse_data_models(struct lylyb_ctx *lybctx, uint32_t parse_options)
Michal Vasko60ea6352020-06-29 13:39:39 +0200959{
960 LY_ERR ret;
961 uint32_t count;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200962 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +0200963
964 /* read model count */
965 lyb_read_number(&count, sizeof count, 2, lybctx);
966
967 if (count) {
968 LY_ARRAY_CREATE_RET(lybctx->ctx, lybctx->models, count, LY_EMEM);
969
970 /* read modules */
971 for (u = 0; u < count; ++u) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200972 ret = lyb_parse_model(lybctx, parse_options, &lybctx->models[u]);
Michal Vasko60ea6352020-06-29 13:39:39 +0200973 LY_CHECK_RET(ret);
974 LY_ARRAY_INCREMENT(lybctx->models);
975 }
976 }
977
978 return LY_SUCCESS;
979}
980
981/**
982 * @brief Parse LYB magic number.
983 *
984 * @param[in] lybctx LYB context.
985 * @return LY_ERR value.
986 */
987static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200988lyb_parse_magic_number(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200989{
990 char magic_byte = 0;
991
992 lyb_read((uint8_t *)&magic_byte, 1, lybctx);
993 if (magic_byte != 'l') {
994 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid first magic number byte \"0x%02x\".", magic_byte);
995 return LY_EINVAL;
996 }
997
998 lyb_read((uint8_t *)&magic_byte, 1, lybctx);
999 if (magic_byte != 'y') {
1000 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid second magic number byte \"0x%02x\".", magic_byte);
1001 return LY_EINVAL;
1002 }
1003
1004 lyb_read((uint8_t *)&magic_byte, 1, lybctx);
1005 if (magic_byte != 'b') {
1006 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid third magic number byte \"0x%02x\".", magic_byte);
1007 return LY_EINVAL;
1008 }
1009
1010 return LY_SUCCESS;
1011}
1012
1013/**
1014 * @brief Parse LYB header.
1015 *
1016 * @param[in] lybctx LYB context.
1017 * @return LY_ERR value.
1018 */
1019static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +02001020lyb_parse_header(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +02001021{
1022 uint8_t byte = 0;
1023
1024 /* version, future flags */
1025 lyb_read((uint8_t *)&byte, sizeof byte, lybctx);
1026
1027 if ((byte & LYB_VERSION_MASK) != LYB_VERSION_NUM) {
1028 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid LYB format version \"0x%02x\", expected \"0x%02x\".",
Michal Vasko69730152020-10-09 16:30:07 +02001029 byte & LYB_VERSION_MASK, LYB_VERSION_NUM);
Michal Vasko60ea6352020-06-29 13:39:39 +02001030 return LY_EINVAL;
1031 }
1032
1033 return LY_SUCCESS;
1034}
1035
Michal Vaskoe0665742021-02-11 11:08:44 +01001036LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +01001037lyd_parse_lyb(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
1038 struct lyd_node **first_p, struct ly_in *in, uint32_t parse_opts, uint32_t val_opts, enum lyd_type data_type,
1039 struct ly_set *parsed, struct lyd_ctx **lydctx_p)
Michal Vasko60ea6352020-06-29 13:39:39 +02001040{
Michal Vaskoe0665742021-02-11 11:08:44 +01001041 LY_ERR rc = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001042 struct lyd_lyb_ctx *lybctx;
Michal Vaskoe0665742021-02-11 11:08:44 +01001043 uint32_t int_opts;
Michal Vasko60ea6352020-06-29 13:39:39 +02001044
Michal Vaskoe0665742021-02-11 11:08:44 +01001045 assert(!(parse_opts & ~LYD_PARSE_OPTS_MASK));
1046 assert(!(val_opts & ~LYD_VALIDATE_OPTS_MASK));
Radek Krejci7931b192020-06-25 17:05:03 +02001047
Radek Krejci1798aae2020-07-14 13:26:06 +02001048 lybctx = calloc(1, sizeof *lybctx);
1049 LY_CHECK_ERR_RET(!lybctx, LOGMEM(ctx), LY_EMEM);
1050 lybctx->lybctx = calloc(1, sizeof *lybctx->lybctx);
Michal Vaskoe0665742021-02-11 11:08:44 +01001051 LY_CHECK_ERR_GOTO(!lybctx->lybctx, LOGMEM(ctx); rc = LY_EMEM, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001052
Radek Krejci1798aae2020-07-14 13:26:06 +02001053 lybctx->lybctx->in = in;
1054 lybctx->lybctx->ctx = ctx;
Michal Vaskoe0665742021-02-11 11:08:44 +01001055 lybctx->parse_opts = parse_opts;
1056 lybctx->val_opts = val_opts;
Radek Krejci1798aae2020-07-14 13:26:06 +02001057 lybctx->free = lyd_lyb_ctx_free;
Michal Vasko60ea6352020-06-29 13:39:39 +02001058
Michal Vaskoe0665742021-02-11 11:08:44 +01001059 switch (data_type) {
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001060 case LYD_TYPE_DATA_YANG:
Michal Vaskoe0665742021-02-11 11:08:44 +01001061 int_opts = LYD_INTOPT_WITH_SIBLINGS;
1062 break;
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001063 case LYD_TYPE_RPC_YANG:
Michal Vaskoe0665742021-02-11 11:08:44 +01001064 int_opts = LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NO_SIBLINGS;
1065 break;
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001066 case LYD_TYPE_NOTIF_YANG:
Michal Vaskoe0665742021-02-11 11:08:44 +01001067 int_opts = LYD_INTOPT_NOTIF | LYD_INTOPT_NO_SIBLINGS;
1068 break;
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001069 case LYD_TYPE_REPLY_YANG:
Michal Vaskoe0665742021-02-11 11:08:44 +01001070 int_opts = LYD_INTOPT_REPLY | LYD_INTOPT_NO_SIBLINGS;
1071 break;
1072 default:
1073 LOGINT(ctx);
1074 rc = LY_EINT;
1075 goto cleanup;
1076 }
1077 lybctx->int_opts = int_opts;
Radek Krejcif16e2542021-02-17 15:39:23 +01001078 lybctx->ext = ext;
Michal Vaskoe0665742021-02-11 11:08:44 +01001079
1080 /* find the operation node if it exists already */
1081 LY_CHECK_GOTO(rc = lyd_parser_find_operation(parent, int_opts, &lybctx->op_node), cleanup);
1082
Michal Vasko60ea6352020-06-29 13:39:39 +02001083 /* read magic number */
Michal Vaskoe0665742021-02-11 11:08:44 +01001084 rc = lyb_parse_magic_number(lybctx->lybctx);
1085 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001086
1087 /* read header */
Michal Vaskoe0665742021-02-11 11:08:44 +01001088 rc = lyb_parse_header(lybctx->lybctx);
1089 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001090
1091 /* read used models */
Michal Vaskoe0665742021-02-11 11:08:44 +01001092 rc = lyb_parse_data_models(lybctx->lybctx, lybctx->parse_opts);
1093 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001094
1095 /* read subtree(s) */
Radek Krejci1798aae2020-07-14 13:26:06 +02001096 while (lybctx->lybctx->in->current[0]) {
Michal Vaskoe0665742021-02-11 11:08:44 +01001097 rc = lyb_parse_subtree_r(lybctx, parent, first_p, parsed);
1098 LY_CHECK_GOTO(rc, cleanup);
1099
1100 if (!(int_opts & LYD_INTOPT_WITH_SIBLINGS)) {
1101 break;
1102 }
1103 }
1104
1105 if ((int_opts & LYD_INTOPT_NO_SIBLINGS) && lybctx->lybctx->in->current[0]) {
1106 LOGVAL(ctx, LYVE_SYNTAX, "Unexpected sibling node.");
1107 rc = LY_EVALID;
1108 goto cleanup;
1109 }
1110 if ((int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NOTIF | LYD_INTOPT_REPLY)) && !lybctx->op_node) {
1111 LOGVAL(ctx, LYVE_DATA, "Missing the operation node.");
1112 rc = LY_EVALID;
1113 goto cleanup;
Michal Vasko60ea6352020-06-29 13:39:39 +02001114 }
1115
1116 /* read the last zero, parsing finished */
Radek Krejci1798aae2020-07-14 13:26:06 +02001117 ly_in_skip(lybctx->lybctx->in, 1);
Michal Vasko60ea6352020-06-29 13:39:39 +02001118
Michal Vasko60ea6352020-06-29 13:39:39 +02001119cleanup:
Michal Vaskoe0665742021-02-11 11:08:44 +01001120 /* there should be no unres stored if validation should be skipped */
1121 assert(!(parse_opts & LYD_PARSE_ONLY) || (!lybctx->node_types.count && !lybctx->meta_types.count &&
1122 !lybctx->node_when.count));
1123
1124 if (rc) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001125 lyd_lyb_ctx_free((struct lyd_ctx *)lybctx);
Radek Krejci1798aae2020-07-14 13:26:06 +02001126 } else {
Michal Vaskoe0665742021-02-11 11:08:44 +01001127 *lydctx_p = (struct lyd_ctx *)lybctx;
Michal Vasko60ea6352020-06-29 13:39:39 +02001128 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001129 return rc;
Michal Vasko60ea6352020-06-29 13:39:39 +02001130}
1131
1132API int
1133lyd_lyb_data_length(const char *data)
1134{
1135 LY_ERR ret = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001136 struct lylyb_ctx *lybctx;
Michal Vasko60ea6352020-06-29 13:39:39 +02001137 int count, i;
1138 size_t len;
1139 uint8_t buf[LYB_SIZE_MAX];
1140
1141 if (!data) {
1142 return -1;
1143 }
1144
Radek Krejci1798aae2020-07-14 13:26:06 +02001145 lybctx = calloc(1, sizeof *lybctx);
1146 LY_CHECK_ERR_RET(!lybctx, LOGMEM(NULL), LY_EMEM);
1147 ret = ly_in_new_memory(data, &lybctx->in);
Michal Vasko63f3d842020-07-08 10:10:14 +02001148 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001149
1150 /* read magic number */
Radek Krejci1798aae2020-07-14 13:26:06 +02001151 ret = lyb_parse_magic_number(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001152 LY_CHECK_GOTO(ret, cleanup);
1153
1154 /* read header */
Radek Krejci1798aae2020-07-14 13:26:06 +02001155 ret = lyb_parse_header(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001156 LY_CHECK_GOTO(ret, cleanup);
1157
1158 /* read model count */
Radek Krejci1798aae2020-07-14 13:26:06 +02001159 lyb_read_number(&count, sizeof count, 2, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001160
1161 /* read all models */
1162 for (i = 0; i < count; ++i) {
1163 /* module name length */
1164 len = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +02001165 lyb_read_number(&len, sizeof len, 2, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001166
1167 /* model name */
Radek Krejci1798aae2020-07-14 13:26:06 +02001168 lyb_read(buf, len, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001169
1170 /* revision */
Radek Krejci1798aae2020-07-14 13:26:06 +02001171 lyb_read(buf, 2, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001172 }
1173
Radek Krejci1798aae2020-07-14 13:26:06 +02001174 while (lybctx->in->current[0]) {
Michal Vasko60ea6352020-06-29 13:39:39 +02001175 /* register a new subtree */
Radek Krejci1798aae2020-07-14 13:26:06 +02001176 ret = lyb_read_start_subtree(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001177 LY_CHECK_GOTO(ret, cleanup);
1178
1179 /* skip it */
Radek Krejci1798aae2020-07-14 13:26:06 +02001180 lyb_skip_subtree(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001181
1182 /* subtree finished */
Radek Krejci1798aae2020-07-14 13:26:06 +02001183 ret = lyb_read_stop_subtree(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001184 LY_CHECK_GOTO(ret, cleanup);
1185 }
1186
1187 /* read the last zero, parsing finished */
Radek Krejci1798aae2020-07-14 13:26:06 +02001188 ly_in_skip(lybctx->in, 1);
Michal Vasko60ea6352020-06-29 13:39:39 +02001189
1190cleanup:
Radek Krejci1798aae2020-07-14 13:26:06 +02001191 count = lybctx->in->current - lybctx->in->start;
Michal Vasko63f3d842020-07-08 10:10:14 +02001192
Radek Krejci1798aae2020-07-14 13:26:06 +02001193 ly_in_free(lybctx->in, 0);
1194 lylyb_ctx_free(lybctx);
1195
Michal Vasko63f3d842020-07-08 10:10:14 +02001196 return ret ? -1 : count;
Michal Vasko60ea6352020-06-29 13:39:39 +02001197}