blob: fc48cd9328567702237000499bf37f0608d04e0c [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) {
152 case 1:
153 *((uint8_t *)num) = buf;
154 break;
155 case 2:
156 *((uint16_t *)num) = buf;
157 break;
158 case 4:
159 *((uint32_t *)num) = buf;
160 break;
161 case 8:
162 *((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
249 if (!lybctx->subtrees) {
Radek Krejcif6d14cb2020-07-02 16:11:45 +0200250 assert(lybctx->subtree_size == 0);
Michal Vasko60ea6352020-06-29 13:39:39 +0200251 u = 0;
252 } else {
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200253 u = LY_ARRAY_COUNT(lybctx->subtrees);
Michal Vasko60ea6352020-06-29 13:39:39 +0200254 }
255 if (u == lybctx->subtree_size) {
256 LY_ARRAY_CREATE_RET(lybctx->ctx, lybctx->subtrees, u + LYB_SUBTREE_STEP, LY_EMEM);
257 lybctx->subtree_size = u + LYB_SUBTREE_STEP;
258 }
259
Michal Vasko63f3d842020-07-08 10:10:14 +0200260 LY_CHECK_RET(ly_in_read(lybctx->in, meta_buf, LYB_META_BYTES));
Michal Vasko60ea6352020-06-29 13:39:39 +0200261
262 LY_ARRAY_INCREMENT(lybctx->subtrees);
263 LYB_LAST_SUBTREE(lybctx).written = meta_buf[0];
264 LYB_LAST_SUBTREE(lybctx).inner_chunks = meta_buf[LYB_SIZE_BYTES];
265 LYB_LAST_SUBTREE(lybctx).position = (LYB_LAST_SUBTREE(lybctx).written == LYB_SIZE_MAX ? 1 : 0);
266
Michal Vasko60ea6352020-06-29 13:39:39 +0200267 return LY_SUCCESS;
268}
269
270/**
271 * @brief Parse YANG model info.
272 *
273 * @param[in] lybctx LYB context.
274 * @param[out] mod Parsed module.
275 * @return LY_ERR value.
276 */
277static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200278lyb_parse_model(struct lylyb_ctx *lybctx, uint32_t parse_options, const struct lys_module **mod)
Michal Vasko60ea6352020-06-29 13:39:39 +0200279{
280 LY_ERR ret = LY_SUCCESS;
281 char *mod_name = NULL, mod_rev[11];
282 uint16_t rev;
283
284 /* model name */
285 ret = lyb_read_string(&mod_name, 1, lybctx);
286 LY_CHECK_GOTO(ret, cleanup);
287
288 /* revision */
289 lyb_read_number(&rev, sizeof rev, 2, lybctx);
290
291 if (!mod_name[0]) {
292 /* opaq node, no module */
293 *mod = NULL;
294 goto cleanup;
295 }
296
297 if (rev) {
298 sprintf(mod_rev, "%04u-%02u-%02u", ((rev & 0xFE00) >> 9) + 2000, (rev & 0x01E0) >> 5, rev & 0x001Fu);
299 *mod = ly_ctx_get_module(lybctx->ctx, mod_name, mod_rev);
Radek Krejci1798aae2020-07-14 13:26:06 +0200300 if ((parse_options & LYD_PARSE_LYB_MOD_UPDATE) && !(*mod)) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200301 /* try to use an updated module */
302 *mod = ly_ctx_get_module_implemented(lybctx->ctx, mod_name);
303 if (*mod && (!(*mod)->revision || (strcmp((*mod)->revision, mod_rev) < 0))) {
304 /* not an implemented module in a newer revision */
305 *mod = NULL;
306 }
307 }
308 } else {
309 *mod = ly_ctx_get_module_latest(lybctx->ctx, mod_name);
310 }
311 /* TODO data_clb supported?
312 if (lybctx->ctx->data_clb) {
313 if (!*mod) {
314 *mod = lybctx->ctx->data_clb(lybctx->ctx, mod_name, NULL, 0, lybctx->ctx->data_clb_data);
315 } else if (!(*mod)->implemented) {
316 *mod = lybctx->ctx->data_clb(lybctx->ctx, mod_name, (*mod)->ns, LY_MODCLB_NOT_IMPLEMENTED, lybctx->ctx->data_clb_data);
317 }
318 }*/
319
320 if (!*mod || !(*mod)->implemented) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200321 if (parse_options & LYD_PARSE_STRICT) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200322 if (!*mod) {
323 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid context for LYB data parsing, missing module \"%s%s%s\".",
Michal Vasko69730152020-10-09 16:30:07 +0200324 mod_name, rev ? "@" : "", rev ? mod_rev : "");
Michal Vasko60ea6352020-06-29 13:39:39 +0200325 } else if (!(*mod)->implemented) {
326 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid context for LYB data parsing, module \"%s%s%s\" not implemented.",
Michal Vasko69730152020-10-09 16:30:07 +0200327 mod_name, rev ? "@" : "", rev ? mod_rev : "");
Michal Vasko60ea6352020-06-29 13:39:39 +0200328 }
329 ret = LY_EINVAL;
330 goto cleanup;
331 }
332
333 }
334
335cleanup:
336 free(mod_name);
337 return ret;
338}
339
340/**
341 * @brief Parse YANG node metadata.
342 *
343 * @param[in] lybctx LYB context.
Michal Vasko60ea6352020-06-29 13:39:39 +0200344 * @param[out] meta Parsed metadata.
345 * @return LY_ERR value.
346 */
347static LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200348lyb_parse_metadata(struct lyd_lyb_ctx *lybctx, struct lyd_meta **meta)
Michal Vasko60ea6352020-06-29 13:39:39 +0200349{
350 LY_ERR ret = LY_SUCCESS;
Radek Krejci857189e2020-09-01 13:26:36 +0200351 ly_bool dynamic;
Michal Vasko60ea6352020-06-29 13:39:39 +0200352 uint8_t i, count = 0;
Michal Vasko1e5d5612020-07-03 13:29:26 +0200353 char *meta_name = NULL, *meta_value;
Michal Vasko60ea6352020-06-29 13:39:39 +0200354 const struct lys_module *mod;
355
356 /* read number of attributes stored */
Radek Krejci1798aae2020-07-14 13:26:06 +0200357 lyb_read(&count, 1, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200358
359 /* read attributes */
360 for (i = 0; i < count; ++i) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200361 ret = lyb_read_start_subtree(lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200362 LY_CHECK_GOTO(ret, cleanup);
363
364 /* find model */
Radek Krejci1798aae2020-07-14 13:26:06 +0200365 ret = lyb_parse_model(lybctx->lybctx, lybctx->parse_options, &mod);
Michal Vasko60ea6352020-06-29 13:39:39 +0200366 LY_CHECK_GOTO(ret, cleanup);
367
368 if (!mod) {
369 /* skip it */
370 do {
Radek Krejci1798aae2020-07-14 13:26:06 +0200371 lyb_read(NULL, LYB_LAST_SUBTREE(lybctx->lybctx).written, lybctx->lybctx);
372 } while (LYB_LAST_SUBTREE(lybctx->lybctx).written);
Michal Vasko60ea6352020-06-29 13:39:39 +0200373 goto stop_subtree;
374 }
375
376 /* meta name */
Radek Krejci1798aae2020-07-14 13:26:06 +0200377 ret = lyb_read_string(&meta_name, 1, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200378 LY_CHECK_GOTO(ret, cleanup);
379
380 /* meta value */
Radek Krejci1798aae2020-07-14 13:26:06 +0200381 ret = lyb_read_string(&meta_value, 0, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200382 LY_CHECK_GOTO(ret, cleanup);
383 dynamic = 1;
384
385 /* create metadata */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200386 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 +0200387 ly_strlen(meta_value), &dynamic, LY_PREF_JSON, NULL, LYD_HINT_DATA);
Michal Vasko60ea6352020-06-29 13:39:39 +0200388
389 /* free strings */
390 free(meta_name);
391 meta_name = NULL;
392 if (dynamic) {
393 free(meta_value);
394 dynamic = 0;
395 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200396
Radek Krejci1798aae2020-07-14 13:26:06 +0200397 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200398
399stop_subtree:
Radek Krejci1798aae2020-07-14 13:26:06 +0200400 ret = lyb_read_stop_subtree(lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200401 LY_CHECK_GOTO(ret, cleanup);
402 }
403
404cleanup:
405 free(meta_name);
Michal Vasko60ea6352020-06-29 13:39:39 +0200406 if (ret) {
Michal Vasko3a41dff2020-07-15 14:30:28 +0200407 lyd_free_meta_siblings(*meta);
Michal Vasko60ea6352020-06-29 13:39:39 +0200408 *meta = NULL;
409 }
410 return ret;
411}
412
413/**
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100414 * @brief Parse format-specific prefix data.
Michal Vasko60ea6352020-06-29 13:39:39 +0200415 *
416 * @param[in] lybctx LYB context.
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100417 * @param[in] format Prefix data format.
418 * @param[out] prefix_data Parsed prefix data.
Michal Vasko60ea6352020-06-29 13:39:39 +0200419 * @return LY_ERR value.
420 */
421static LY_ERR
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100422lyb_parse_prefix_data(struct lylyb_ctx *lybctx, LY_PREFIX_FORMAT format, void **prefix_data)
Michal Vasko60ea6352020-06-29 13:39:39 +0200423{
424 LY_ERR ret = LY_SUCCESS;
425 uint8_t count, i;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100426 struct ly_set *set = NULL;
427 struct lyxml_ns *ns = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200428
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100429 switch (format) {
430 case LY_PREF_XML:
431 /* read count */
432 lyb_read(&count, 1, lybctx);
433 if (!count) {
434 return LY_SUCCESS;
435 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200436
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100437 /* read all NS elements */
438 LY_CHECK_GOTO(ret = ly_set_new(&set), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200439
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100440 for (i = 0; i < count; ++i) {
441 ns = calloc(1, sizeof *ns);
Michal Vasko60ea6352020-06-29 13:39:39 +0200442
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100443 /* prefix */
444 LY_CHECK_GOTO(ret = lyb_read_string(&ns->prefix, 1, lybctx), cleanup);
445
446 /* namespace */
447 LY_CHECK_GOTO(ret = lyb_read_string(&ns->uri, 1, lybctx), cleanup);
448
449 LY_CHECK_GOTO(ret = ly_set_add(set, ns, 1, NULL), cleanup);
450 ns = NULL;
451 }
452
453 *prefix_data = set;
454 break;
455 case LY_PREF_JSON:
456 /* nothing stored */
457 break;
458 default:
459 LOGINT(lybctx->ctx);
460 ret = LY_EINT;
461 break;
Michal Vasko60ea6352020-06-29 13:39:39 +0200462 }
463
464cleanup:
465 if (ret) {
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100466 ly_free_prefix_data(format, set);
467 if (ns) {
468 free(ns->prefix);
469 free(ns->uri);
470 free(ns);
471 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200472 }
473 return ret;
474}
475
476/**
477 * @brief Parse opaque attributes.
478 *
479 * @param[in] lybctx LYB context.
480 * @param[out] attr Parsed attributes.
481 * @return LY_ERR value.
482 */
483static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200484lyb_parse_attributes(struct lylyb_ctx *lybctx, struct lyd_attr **attr)
Michal Vasko60ea6352020-06-29 13:39:39 +0200485{
486 LY_ERR ret = LY_SUCCESS;
487 uint8_t count, i;
Radek Krejci1798aae2020-07-14 13:26:06 +0200488 struct lyd_attr *attr2;
489 char *prefix = NULL, *module_name = NULL, *name = NULL, *value = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +0200490 ly_bool dynamic = 0;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100491 LY_PREFIX_FORMAT format = 0;
492 void *val_prefix_data = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200493
494 /* read count */
495 lyb_read(&count, 1, lybctx);
496
497 /* read attributes */
498 for (i = 0; i < count; ++i) {
499 ret = lyb_read_start_subtree(lybctx);
500 LY_CHECK_GOTO(ret, cleanup);
501
Michal Vasko0fdcd242020-11-11 19:12:30 +0100502 /* prefix, may be empty */
Michal Vasko60ea6352020-06-29 13:39:39 +0200503 ret = lyb_read_string(&prefix, 1, lybctx);
504 LY_CHECK_GOTO(ret, cleanup);
505 if (!prefix[0]) {
506 free(prefix);
507 prefix = NULL;
508 }
509
510 /* namespace, may be empty */
Radek Krejci1798aae2020-07-14 13:26:06 +0200511 ret = lyb_read_string(&module_name, 1, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200512 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200513 if (!module_name[0]) {
514 free(module_name);
515 module_name = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200516 }
517
518 /* name */
519 ret = lyb_read_string(&name, 1, lybctx);
520 LY_CHECK_GOTO(ret, cleanup);
521
Michal Vasko60ea6352020-06-29 13:39:39 +0200522 /* format */
523 lyb_read((uint8_t *)&format, 1, lybctx);
524
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100525 /* value prefixes */
526 ret = lyb_parse_prefix_data(lybctx, format, &val_prefix_data);
527 LY_CHECK_GOTO(ret, cleanup);
528
Michal Vasko60ea6352020-06-29 13:39:39 +0200529 /* value */
530 ret = lyb_read_string(&value, 0, lybctx);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100531 LY_CHECK_ERR_GOTO(ret, ly_free_prefix_data(format, val_prefix_data), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200532 dynamic = 1;
533
534 /* attr2 is always changed to the created attribute */
Michal Vasko501af032020-11-11 20:27:44 +0100535 ret = lyd_create_attr(NULL, &attr2, lybctx->ctx, name, strlen(name), prefix, ly_strlen(prefix), module_name,
536 ly_strlen(module_name), value, ly_strlen(value), &dynamic, format, val_prefix_data, 0);
Michal Vasko60ea6352020-06-29 13:39:39 +0200537 LY_CHECK_GOTO(ret, cleanup);
538
539 free(prefix);
540 prefix = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +0200541 free(module_name);
542 module_name = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200543 free(name);
544 name = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200545 assert(!dynamic);
546 value = NULL;
547
548 if (!*attr) {
549 *attr = attr2;
550 }
551
552 ret = lyb_read_stop_subtree(lybctx);
553 LY_CHECK_GOTO(ret, cleanup);
554 }
555
556cleanup:
557 free(prefix);
Radek Krejci1798aae2020-07-14 13:26:06 +0200558 free(module_name);
Michal Vasko60ea6352020-06-29 13:39:39 +0200559 free(name);
560 if (dynamic) {
561 free(value);
562 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200563 if (ret) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200564 lyd_free_attr_siblings(lybctx->ctx, *attr);
Michal Vasko60ea6352020-06-29 13:39:39 +0200565 *attr = NULL;
566 }
567 return ret;
568}
569
570/**
571 * @brief Check whether a schema node matches a hash(es).
572 *
573 * @param[in] sibling Schema node to check.
574 * @param[in] hash Hash array to check.
575 * @param[in] hash_count Number of hashes in @p hash.
576 * @return non-zero if matches,
577 * @return 0 if not.
578 */
579static int
580lyb_is_schema_hash_match(struct lysc_node *sibling, LYB_HASH *hash, uint8_t hash_count)
581{
582 LYB_HASH sibling_hash;
583 uint8_t i;
584
585 /* compare all the hashes starting from collision ID 0 */
586 for (i = 0; i < hash_count; ++i) {
587 sibling_hash = lyb_hash(sibling, i);
588 if (sibling_hash != hash[i]) {
589 return 0;
590 }
591 }
592
593 return 1;
594}
595
596/**
Michal Vasko60ea6352020-06-29 13:39:39 +0200597 * @brief Parse schema node hash.
598 *
599 * @param[in] lybctx LYB context.
600 * @param[in] sparent Schema parent, must be set if @p mod is not.
601 * @param[in] mod Module of the top-level node, must be set if @p sparent is not.
602 * @param[out] snode Parsed found schema node, may be NULL if opaque.
603 * @return LY_ERR value.
604 */
605static LY_ERR
606lyb_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 +0200607 const struct lysc_node **snode)
Michal Vasko60ea6352020-06-29 13:39:39 +0200608{
609 LY_ERR ret;
610 uint8_t i, j;
611 const struct lysc_node *sibling;
612 LYB_HASH hash[LYB_HASH_BITS - 1];
Radek Krejci1deb5be2020-08-26 16:43:36 +0200613 uint32_t getnext_opts;
Michal Vasko60ea6352020-06-29 13:39:39 +0200614
615 *snode = NULL;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100616 getnext_opts = lybctx->int_opts & LYD_INTOPT_REPLY ? LYS_GETNEXT_OUTPUT : 0;
Michal Vasko60ea6352020-06-29 13:39:39 +0200617
618 /* read the first hash */
Radek Krejci1798aae2020-07-14 13:26:06 +0200619 lyb_read(&hash[0], sizeof *hash, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200620
621 if (!hash[0]) {
622 /* opaque node */
623 return LY_SUCCESS;
624 }
625
626 /* based on the first hash read all the other ones, if any */
627 for (i = 0; !(hash[0] & (LYB_HASH_COLLISION_ID >> i)); ++i) {
628 if (i > LYB_HASH_BITS) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200629 LOGINT_RET(lybctx->lybctx->ctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200630 }
631 }
632
633 /* move the first hash on its accurate position */
634 hash[i] = hash[0];
635
636 /* read the rest of hashes */
637 for (j = i; j; --j) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200638 lyb_read(&hash[j - 1], sizeof *hash, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200639
640 /* correct collision ID */
641 assert(hash[j - 1] & (LYB_HASH_COLLISION_ID >> (j - 1)));
642 /* preceded with zeros */
643 assert(!(hash[j - 1] & (LYB_HASH_MASK << (LYB_HASH_BITS - (j - 1)))));
644 }
645
646 /* find our node with matching hashes */
647 sibling = NULL;
648 while ((sibling = lys_getnext(sibling, sparent, mod ? mod->compiled : NULL, getnext_opts))) {
649 /* skip schema nodes from models not present during printing */
Michal Vasko69730152020-10-09 16:30:07 +0200650 if (lyb_has_schema_model(sibling, lybctx->lybctx->models) &&
651 lyb_is_schema_hash_match((struct lysc_node *)sibling, hash, i + 1)) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200652 /* match found */
653 break;
654 }
655 }
656
Radek Krejci7931b192020-06-25 17:05:03 +0200657 if (!sibling && (lybctx->parse_options & LYD_PARSE_STRICT)) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200658 if (mod) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200659 LOGVAL(lybctx->lybctx->ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "Failed to find matching hash for a top-level node"
Michal Vasko69730152020-10-09 16:30:07 +0200660 " from \"%s\".", mod->name);
Michal Vasko60ea6352020-06-29 13:39:39 +0200661 } else {
Radek Krejci1798aae2020-07-14 13:26:06 +0200662 LOGVAL(lybctx->lybctx->ctx, LY_VLOG_LYSC, sparent, LYVE_REFERENCE, "Failed to find matching hash for a child node"
Michal Vasko69730152020-10-09 16:30:07 +0200663 " of \"%s\".", sparent->name);
Michal Vasko60ea6352020-06-29 13:39:39 +0200664 }
665 return LY_EVALID;
Radek Krejci1798aae2020-07-14 13:26:06 +0200666 } else if (sibling && (ret = lyd_parser_check_schema((struct lyd_ctx *)lybctx, sibling))) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200667 return ret;
668 }
669
670 *snode = sibling;
671 return LY_SUCCESS;
672}
673
674/**
675 * @brief Read until the end of the current subtree.
676 *
677 * @param[in] lybctx LYB context.
678 */
679static void
Radek Krejci1798aae2020-07-14 13:26:06 +0200680lyb_skip_subtree(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200681{
Michal Vasko60ea6352020-06-29 13:39:39 +0200682 do {
683 /* first skip any meta information inside */
Michal Vasko63f3d842020-07-08 10:10:14 +0200684 ly_in_skip(lybctx->in, LYB_LAST_SUBTREE(lybctx).inner_chunks * LYB_META_BYTES);
Michal Vasko60ea6352020-06-29 13:39:39 +0200685
686 /* then read data */
687 lyb_read(NULL, LYB_LAST_SUBTREE(lybctx).written, lybctx);
688 } while (LYB_LAST_SUBTREE(lybctx).written);
689}
690
691/**
692 * @brief Parse LYB subtree.
693 *
694 * @param[in] lybctx LYB context.
695 * @param[in] parent Data parent of the subtree, must be set if @p first is not.
696 * @param[in,out] first First top-level sibling, must be set if @p parent is not.
697 * @return LY_ERR value.
698 */
699static LY_ERR
700lyb_parse_subtree_r(struct lyd_lyb_ctx *lybctx, struct lyd_node_inner *parent, struct lyd_node **first)
701{
702 LY_ERR ret = LY_SUCCESS;
703 struct lyd_node *node = NULL, *tree;
704 const struct lys_module *mod;
705 const struct lysc_node *snode = NULL;
706 struct lyd_meta *meta = NULL, *m;
Radek Krejci1798aae2020-07-14 13:26:06 +0200707 struct lyd_attr *attr = NULL, *a;
Michal Vasko60ea6352020-06-29 13:39:39 +0200708 LYD_ANYDATA_VALUETYPE value_type;
Radek Krejci1798aae2020-07-14 13:26:06 +0200709 char *value = NULL, *name = NULL, *prefix = NULL, *module_key = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +0200710 ly_bool dynamic = 0;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100711 LY_PREFIX_FORMAT format = 0;
712 void *val_prefix_data = NULL;
Michal Vaskoc5e866a2020-11-04 17:09:26 +0100713 uint32_t prev_lo, flags;
Radek Krejci1798aae2020-07-14 13:26:06 +0200714 const struct ly_ctx *ctx = lybctx->lybctx->ctx;
Michal Vasko60ea6352020-06-29 13:39:39 +0200715
716 /* register a new subtree */
Radek Krejci1798aae2020-07-14 13:26:06 +0200717 LY_CHECK_GOTO(ret = lyb_read_start_subtree(lybctx->lybctx), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200718
719 if (!parent) {
720 /* top-level, read module name */
Radek Krejci1798aae2020-07-14 13:26:06 +0200721 ret = lyb_parse_model(lybctx->lybctx, lybctx->parse_options, &mod);
Michal Vasko60ea6352020-06-29 13:39:39 +0200722 LY_CHECK_GOTO(ret, cleanup);
723
724 /* read hash, find the schema node starting from mod */
725 ret = lyb_parse_schema_hash(lybctx, NULL, mod, &snode);
726 LY_CHECK_GOTO(ret, cleanup);
727 } else {
728 /* read hash, find the schema node starting from parent schema */
729 ret = lyb_parse_schema_hash(lybctx, parent->schema, NULL, &snode);
730 LY_CHECK_GOTO(ret, cleanup);
731 }
732
Radek Krejci0f969882020-08-21 16:56:47 +0200733 if (!snode && !(lybctx->parse_options & LYD_PARSE_OPAQ)) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200734 /* unknown data, skip them */
Radek Krejci1798aae2020-07-14 13:26:06 +0200735 lyb_skip_subtree(lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200736 goto stop_subtree;
Radek Krejci0f969882020-08-21 16:56:47 +0200737 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200738
739 /* create metadata/attributes */
740 if (snode) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200741 ret = lyb_parse_metadata(lybctx, &meta);
Michal Vasko60ea6352020-06-29 13:39:39 +0200742 LY_CHECK_GOTO(ret, cleanup);
743 } else {
Radek Krejci1798aae2020-07-14 13:26:06 +0200744 ret = lyb_parse_attributes(lybctx->lybctx, &attr);
Michal Vasko60ea6352020-06-29 13:39:39 +0200745 LY_CHECK_GOTO(ret, cleanup);
746 }
747
Michal Vaskoc5e866a2020-11-04 17:09:26 +0100748 /* read flags */
749 lyb_read_number(&flags, sizeof flags, sizeof flags, lybctx->lybctx);
750
Michal Vasko60ea6352020-06-29 13:39:39 +0200751 if (!snode) {
752 /* parse prefix */
Radek Krejci1798aae2020-07-14 13:26:06 +0200753 ret = lyb_read_string(&prefix, 1, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200754 LY_CHECK_GOTO(ret, cleanup);
755
Radek Krejci1798aae2020-07-14 13:26:06 +0200756 /* parse module key */
757 ret = lyb_read_string(&module_key, 1, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200758 LY_CHECK_GOTO(ret, cleanup);
759
760 /* parse name */
Radek Krejci1798aae2020-07-14 13:26:06 +0200761 ret = lyb_read_string(&name, 1, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200762 LY_CHECK_GOTO(ret, cleanup);
763
Michal Vasko60ea6352020-06-29 13:39:39 +0200764 /* parse format */
Radek Krejci1798aae2020-07-14 13:26:06 +0200765 lyb_read((uint8_t *)&format, 1, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200766
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100767 /* parse value prefixes */
768 ret = lyb_parse_prefix_data(lybctx->lybctx, format, &val_prefix_data);
769 LY_CHECK_GOTO(ret, cleanup);
770
Michal Vasko60ea6352020-06-29 13:39:39 +0200771 /* parse value */
Radek Krejci1798aae2020-07-14 13:26:06 +0200772 ret = lyb_read_string(&value, 0, lybctx->lybctx);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100773 LY_CHECK_ERR_GOTO(ret, ly_free_prefix_data(format, val_prefix_data), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200774 dynamic = 1;
775
776 /* create node */
Michal Vasko501af032020-11-11 20:27:44 +0100777 ret = lyd_create_opaq(ctx, name, strlen(name), prefix, ly_strlen(prefix), module_key, ly_strlen(module_key),
778 value, strlen(value), &dynamic, format, val_prefix_data, 0, &node);
Michal Vasko60ea6352020-06-29 13:39:39 +0200779 LY_CHECK_GOTO(ret, cleanup);
780
781 /* process children */
Radek Krejci1798aae2020-07-14 13:26:06 +0200782 while (LYB_LAST_SUBTREE(lybctx->lybctx).written) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200783 ret = lyb_parse_subtree_r(lybctx, (struct lyd_node_inner *)node, NULL);
784 LY_CHECK_GOTO(ret, cleanup);
785 }
786 } else if (snode->nodetype & LYD_NODE_TERM) {
787 /* parse value */
Radek Krejci1798aae2020-07-14 13:26:06 +0200788 ret = lyb_read_string(&value, 0, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200789 LY_CHECK_GOTO(ret, cleanup);
790 dynamic = 1;
791
792 /* create node */
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200793 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 +0200794 NULL, LYD_HINT_DATA, &node);
Michal Vasko60ea6352020-06-29 13:39:39 +0200795 if (dynamic) {
796 free(value);
797 dynamic = 0;
798 }
799 value = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +0200800 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200801 } else if (snode->nodetype & LYD_NODE_INNER) {
802 /* create node */
803 ret = lyd_create_inner(snode, &node);
804 LY_CHECK_GOTO(ret, cleanup);
805
806 /* process children */
Radek Krejci1798aae2020-07-14 13:26:06 +0200807 while (LYB_LAST_SUBTREE(lybctx->lybctx).written) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200808 ret = lyb_parse_subtree_r(lybctx, (struct lyd_node_inner *)node, NULL);
809 LY_CHECK_GOTO(ret, cleanup);
810 }
811
Radek Krejci7931b192020-06-25 17:05:03 +0200812 if (!(lybctx->parse_options & LYD_PARSE_ONLY)) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200813 /* new node validation, autodelete CANNOT occur, all nodes are new */
Michal Vasko8104fd42020-07-13 11:09:51 +0200814 ret = lyd_validate_new(lyd_node_children_p(node), snode, NULL, NULL);
Michal Vasko60ea6352020-06-29 13:39:39 +0200815 LY_CHECK_GOTO(ret, cleanup);
816
817 /* add any missing default children */
Michal Vaskoa6669ba2020-08-06 16:14:26 +0200818 ret = lyd_new_implicit_r(node, lyd_node_children_p(node), NULL, NULL, &lybctx->unres_node_type,
Michal Vasko69730152020-10-09 16:30:07 +0200819 &lybctx->when_check, (lybctx->validate_options & LYD_VALIDATE_NO_STATE) ?
820 LYD_IMPLICIT_NO_STATE : 0, NULL);
Michal Vasko60ea6352020-06-29 13:39:39 +0200821 LY_CHECK_GOTO(ret, cleanup);
822 }
823
Michal Vasko751cb4d2020-07-14 12:25:28 +0200824 if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200825 /* rememeber the RPC/action/notification */
Radek Krejci1798aae2020-07-14 13:26:06 +0200826 lybctx->op_node = node;
Michal Vasko60ea6352020-06-29 13:39:39 +0200827 }
828 } else if (snode->nodetype & LYD_NODE_ANY) {
829 /* parse value type */
Radek Krejci1798aae2020-07-14 13:26:06 +0200830 lyb_read((uint8_t *)&value_type, sizeof value_type, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200831 if (value_type == LYD_ANYDATA_DATATREE) {
832 /* invalid situation */
Radek Krejci1798aae2020-07-14 13:26:06 +0200833 LOGINT(ctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200834 goto cleanup;
835 }
836
837 /* read anydata content */
Radek Krejci1798aae2020-07-14 13:26:06 +0200838 ret = lyb_read_string(&value, 0, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200839 LY_CHECK_GOTO(ret, cleanup);
840 dynamic = 1;
841
842 if (value_type == LYD_ANYDATA_LYB) {
843 /* turn logging off */
844 prev_lo = ly_log_options(0);
845
846 /* try to parse LYB into a data tree */
Radek Krejci1798aae2020-07-14 13:26:06 +0200847 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 +0200848 /* successfully parsed */
849 free(value);
850 value = (char *)tree;
851 value_type = LYD_ANYDATA_DATATREE;
852 }
Radek Krejci7931b192020-06-25 17:05:03 +0200853
854 /* turn logging on again */
855 ly_log_options(prev_lo);
Michal Vasko60ea6352020-06-29 13:39:39 +0200856 }
857
858 /* create node */
859 ret = lyd_create_any(snode, value, value_type, &node);
860 LY_CHECK_GOTO(ret, cleanup);
861
862 dynamic = 0;
863 value = NULL;
864 }
865 assert(node);
866
Michal Vaskoc5e866a2020-11-04 17:09:26 +0100867 /* set flags */
868 node->flags = flags;
Michal Vasko60ea6352020-06-29 13:39:39 +0200869
870 /* add metadata/attributes */
871 if (snode) {
872 LY_LIST_FOR(meta, m) {
873 m->parent = node;
874 }
875 node->meta = meta;
876 meta = NULL;
877 } else {
878 assert(!node->schema);
879 LY_LIST_FOR(attr, a) {
880 a->parent = (struct lyd_node_opaq *)node;
881 }
882 ((struct lyd_node_opaq *)node)->attr = attr;
883 attr = NULL;
884 }
885
Michal Vaskob104f112020-07-17 09:54:54 +0200886 /* insert, keep first pointer correct */
Michal Vasko60ea6352020-06-29 13:39:39 +0200887 lyd_insert_node((struct lyd_node *)parent, first, node);
Michal Vaskob104f112020-07-17 09:54:54 +0200888 while (!parent && (*first)->prev->next) {
889 *first = (*first)->prev;
890 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200891 node = NULL;
892
893stop_subtree:
894 /* end the subtree */
Radek Krejci1798aae2020-07-14 13:26:06 +0200895 ret = lyb_read_stop_subtree(lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200896 LY_CHECK_GOTO(ret, cleanup);
897
898cleanup:
899 free(prefix);
Radek Krejci1798aae2020-07-14 13:26:06 +0200900 free(module_key);
Michal Vasko60ea6352020-06-29 13:39:39 +0200901 free(name);
902 if (dynamic) {
903 free(value);
904 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200905
Michal Vasko3a41dff2020-07-15 14:30:28 +0200906 lyd_free_meta_siblings(meta);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200907 lyd_free_attr_siblings(ctx, attr);
Michal Vasko60ea6352020-06-29 13:39:39 +0200908 lyd_free_tree(node);
909 return ret;
910}
911
912/**
913 * @brief Parse used YANG data models.
914 *
915 * @param[in] lybctx LYB context.
916 * @return LY_ERR value.
917 */
918static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200919lyb_parse_data_models(struct lylyb_ctx *lybctx, uint32_t parse_options)
Michal Vasko60ea6352020-06-29 13:39:39 +0200920{
921 LY_ERR ret;
922 uint32_t count;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200923 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +0200924
925 /* read model count */
926 lyb_read_number(&count, sizeof count, 2, lybctx);
927
928 if (count) {
929 LY_ARRAY_CREATE_RET(lybctx->ctx, lybctx->models, count, LY_EMEM);
930
931 /* read modules */
932 for (u = 0; u < count; ++u) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200933 ret = lyb_parse_model(lybctx, parse_options, &lybctx->models[u]);
Michal Vasko60ea6352020-06-29 13:39:39 +0200934 LY_CHECK_RET(ret);
935 LY_ARRAY_INCREMENT(lybctx->models);
936 }
937 }
938
939 return LY_SUCCESS;
940}
941
942/**
943 * @brief Parse LYB magic number.
944 *
945 * @param[in] lybctx LYB context.
946 * @return LY_ERR value.
947 */
948static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200949lyb_parse_magic_number(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200950{
951 char magic_byte = 0;
952
953 lyb_read((uint8_t *)&magic_byte, 1, lybctx);
954 if (magic_byte != 'l') {
955 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid first magic number byte \"0x%02x\".", magic_byte);
956 return LY_EINVAL;
957 }
958
959 lyb_read((uint8_t *)&magic_byte, 1, lybctx);
960 if (magic_byte != 'y') {
961 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid second magic number byte \"0x%02x\".", magic_byte);
962 return LY_EINVAL;
963 }
964
965 lyb_read((uint8_t *)&magic_byte, 1, lybctx);
966 if (magic_byte != 'b') {
967 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid third magic number byte \"0x%02x\".", magic_byte);
968 return LY_EINVAL;
969 }
970
971 return LY_SUCCESS;
972}
973
974/**
975 * @brief Parse LYB header.
976 *
977 * @param[in] lybctx LYB context.
978 * @return LY_ERR value.
979 */
980static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200981lyb_parse_header(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200982{
983 uint8_t byte = 0;
984
985 /* version, future flags */
986 lyb_read((uint8_t *)&byte, sizeof byte, lybctx);
987
988 if ((byte & LYB_VERSION_MASK) != LYB_VERSION_NUM) {
989 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid LYB format version \"0x%02x\", expected \"0x%02x\".",
Michal Vasko69730152020-10-09 16:30:07 +0200990 byte & LYB_VERSION_MASK, LYB_VERSION_NUM);
Michal Vasko60ea6352020-06-29 13:39:39 +0200991 return LY_EINVAL;
992 }
993
994 return LY_SUCCESS;
995}
996
Michal Vaskoee38a5d2020-11-09 21:02:18 +0100997/**
Radek Krejci1798aae2020-07-14 13:26:06 +0200998 * @param[in] ctx libyang context for logging
Michal Vaskoee38a5d2020-11-09 21:02:18 +0100999 * @param[in] parent Parent node where to connect the parsed data, required for reply where the reply data are connected
1000 * with the request operation
Radek Krejci1798aae2020-07-14 13:26:06 +02001001 * @param[in] in Input structure.
1002 * @param[in] parse_options Options for parser, see @ref dataparseroptions.
1003 * @param[in] validate_options Options for the validation phase, see @ref datavalidationoptions.
1004 * @param[in] data_type Internal data parser flag to distnguish type of the data to parse (RPC/Reply/Notification/regular data].
1005 * @param[out] tree_p Parsed data tree. Note that NULL can be a valid result.
1006 * @param[out] op_p Optional pointer to the actual operation. Useful for action and inner notifications.
1007 * @param[out] lydctx_p Data parser context to finish validation.
1008 */
1009static LY_ERR
Michal Vaskoee38a5d2020-11-09 21:02:18 +01001010lyd_parse_lyb_(const struct ly_ctx *ctx, struct lyd_node_inner **parent, struct ly_in *in, uint32_t parse_options,
1011 uint32_t validate_options, uint32_t data_type, struct lyd_node **tree_p, struct lyd_node **op_p,
1012 struct lyd_ctx **lydctx_p)
Michal Vasko60ea6352020-06-29 13:39:39 +02001013{
1014 LY_ERR ret = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001015 struct lyd_lyb_ctx *lybctx;
1016 struct lyd_node *tree = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +02001017
Radek Krejci7931b192020-06-25 17:05:03 +02001018 assert(!(parse_options & ~LYD_PARSE_OPTS_MASK));
1019 assert(!(validate_options & ~LYD_VALIDATE_OPTS_MASK));
1020
Radek Krejci1798aae2020-07-14 13:26:06 +02001021 lybctx = calloc(1, sizeof *lybctx);
1022 LY_CHECK_ERR_RET(!lybctx, LOGMEM(ctx), LY_EMEM);
1023 lybctx->lybctx = calloc(1, sizeof *lybctx->lybctx);
Michal Vasko9acaf492020-08-13 09:05:58 +02001024 LY_CHECK_ERR_GOTO(!lybctx->lybctx, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001025
Radek Krejci1798aae2020-07-14 13:26:06 +02001026 lybctx->lybctx->in = in;
1027 lybctx->lybctx->ctx = ctx;
1028 lybctx->parse_options = parse_options;
1029 lybctx->validate_options = validate_options;
1030 lybctx->int_opts = data_type;
1031 lybctx->free = lyd_lyb_ctx_free;
Michal Vasko60ea6352020-06-29 13:39:39 +02001032
1033 /* read magic number */
Radek Krejci1798aae2020-07-14 13:26:06 +02001034 ret = lyb_parse_magic_number(lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001035 LY_CHECK_GOTO(ret, cleanup);
1036
1037 /* read header */
Radek Krejci1798aae2020-07-14 13:26:06 +02001038 ret = lyb_parse_header(lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001039 LY_CHECK_GOTO(ret, cleanup);
1040
1041 /* read used models */
Radek Krejci1798aae2020-07-14 13:26:06 +02001042 ret = lyb_parse_data_models(lybctx->lybctx, lybctx->parse_options);
Michal Vasko60ea6352020-06-29 13:39:39 +02001043 LY_CHECK_GOTO(ret, cleanup);
1044
1045 /* read subtree(s) */
Radek Krejci1798aae2020-07-14 13:26:06 +02001046 while (lybctx->lybctx->in->current[0]) {
1047 ret = lyb_parse_subtree_r(lybctx, parent ? *parent : NULL, &tree);
Michal Vasko60ea6352020-06-29 13:39:39 +02001048 LY_CHECK_GOTO(ret, cleanup);
1049 }
1050
1051 /* read the last zero, parsing finished */
Radek Krejci1798aae2020-07-14 13:26:06 +02001052 ly_in_skip(lybctx->lybctx->in, 1);
Michal Vasko60ea6352020-06-29 13:39:39 +02001053
Radek Krejci1798aae2020-07-14 13:26:06 +02001054 if (data_type == LYD_INTOPT_RPC) {
1055 /* make sure we have parsed some operation */
1056 if (!lybctx->op_node) {
1057 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_DATA, "Missing the \"rpc\"/\"action\" node.");
1058 ret = LY_EVALID;
1059 goto cleanup;
1060 }
1061
1062 if (op_p) {
1063 *op_p = lybctx->op_node;
1064 }
1065 assert(tree);
1066 } else if (data_type == LYD_INTOPT_REPLY) {
1067 struct lyd_node_inner *iter;
1068
1069 assert(parent);
1070
1071 if (op_p) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001072 *op_p = (struct lyd_node *)(*parent);
Radek Krejci1798aae2020-07-14 13:26:06 +02001073 }
Radek Krejci1e008d22020-08-17 11:37:37 +02001074 for (iter = *parent; iter->parent; iter = iter->parent) {}
Radek Krejci1798aae2020-07-14 13:26:06 +02001075 tree = (struct lyd_node *)iter;
1076 *parent = NULL;
1077
1078 } else if (data_type == LYD_INTOPT_NOTIF) {
1079 /* make sure we have parsed some notification */
1080 if (!lybctx->op_node) {
1081 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_DATA, "Missing the \"notification\" node.");
1082 ret = LY_EVALID;
1083 goto cleanup;
1084 }
1085
1086 if (op_p) {
1087 *op_p = lybctx->op_node;
1088 }
1089 assert(tree);
1090 }
Michal Vasko60ea6352020-06-29 13:39:39 +02001091
1092cleanup:
Radek Krejci1798aae2020-07-14 13:26:06 +02001093 if (ret || !lydctx_p) {
1094 lyd_lyb_ctx_free((struct lyd_ctx *)lybctx);
1095 if (ret) {
1096 lyd_free_all(tree);
1097 }
1098 } else {
1099 *lydctx_p = (struct lyd_ctx *)lybctx;
1100 if (tree_p) {
1101 *tree_p = tree;
1102 }
Michal Vasko60ea6352020-06-29 13:39:39 +02001103 }
1104 return ret;
1105}
1106
1107LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001108lyd_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 +02001109 struct lyd_node **tree_p, struct lyd_ctx **lydctx_p)
Michal Vasko60ea6352020-06-29 13:39:39 +02001110{
Radek Krejci1798aae2020-07-14 13:26:06 +02001111 return lyd_parse_lyb_(ctx, NULL, in, parse_options, validate_options, 0, tree_p, NULL, lydctx_p);
Michal Vasko60ea6352020-06-29 13:39:39 +02001112}
1113
1114LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +02001115lyd_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 +02001116{
Radek Krejci1798aae2020-07-14 13:26:06 +02001117 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 +02001118}
1119
1120LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +02001121lyd_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 +02001122{
Radek Krejci1798aae2020-07-14 13:26:06 +02001123 return lyd_parse_lyb_(ctx, NULL, in, LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, LYD_INTOPT_NOTIF, tree_p, ntf_p, NULL);
1124}
Michal Vasko60ea6352020-06-29 13:39:39 +02001125
Radek Krejci1798aae2020-07-14 13:26:06 +02001126LY_ERR
1127lyd_parse_lyb_reply(const struct lyd_node *request, struct ly_in *in, struct lyd_node **tree_p, struct lyd_node **op_p)
1128{
1129 LY_ERR ret;
1130 struct lyd_node *req_op, *rep_op = NULL;
Michal Vaskob7be7a82020-08-20 09:09:04 +02001131 const struct ly_ctx *ctx = LYD_CTX(request);
Michal Vasko60ea6352020-06-29 13:39:39 +02001132
1133 /* find request OP */
Michal Vasko56daf732020-08-10 10:57:18 +02001134 LYD_TREE_DFS_BEGIN((struct lyd_node *)request, req_op) {
Michal Vasko60ea6352020-06-29 13:39:39 +02001135 if (req_op->schema->nodetype & (LYS_RPC | LYS_ACTION)) {
1136 break;
1137 }
Michal Vasko56daf732020-08-10 10:57:18 +02001138 LYD_TREE_DFS_END(request, req_op);
Michal Vasko60ea6352020-06-29 13:39:39 +02001139 }
1140 if (!(req_op->schema->nodetype & (LYS_RPC | LYS_ACTION))) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001141 LOGERR(LYD_CTX(request), LY_EINVAL, "No RPC/action in the request found.");
Radek Krejci1798aae2020-07-14 13:26:06 +02001142 return LY_EINVAL;
Michal Vasko60ea6352020-06-29 13:39:39 +02001143 }
1144
1145 /* duplicate request OP with parents */
Radek Krejci1798aae2020-07-14 13:26:06 +02001146 LY_CHECK_RET(lyd_dup_single(req_op, NULL, LYD_DUP_WITH_PARENTS, &rep_op));
Michal Vasko60ea6352020-06-29 13:39:39 +02001147
Michal Vaskoee38a5d2020-11-09 21:02:18 +01001148 ret = lyd_parse_lyb_(ctx, (struct lyd_node_inner **)&rep_op, in, LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0,
1149 LYD_INTOPT_REPLY, tree_p, op_p, NULL);
Michal Vasko60ea6352020-06-29 13:39:39 +02001150
Michal Vasko60ea6352020-06-29 13:39:39 +02001151 lyd_free_all(rep_op);
Michal Vasko60ea6352020-06-29 13:39:39 +02001152
Michal Vasko60ea6352020-06-29 13:39:39 +02001153 return ret;
1154}
1155
1156API int
1157lyd_lyb_data_length(const char *data)
1158{
1159 LY_ERR ret = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001160 struct lylyb_ctx *lybctx;
Michal Vasko60ea6352020-06-29 13:39:39 +02001161 int count, i;
1162 size_t len;
1163 uint8_t buf[LYB_SIZE_MAX];
1164
1165 if (!data) {
1166 return -1;
1167 }
1168
Radek Krejci1798aae2020-07-14 13:26:06 +02001169 lybctx = calloc(1, sizeof *lybctx);
1170 LY_CHECK_ERR_RET(!lybctx, LOGMEM(NULL), LY_EMEM);
1171 ret = ly_in_new_memory(data, &lybctx->in);
Michal Vasko63f3d842020-07-08 10:10:14 +02001172 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001173
1174 /* read magic number */
Radek Krejci1798aae2020-07-14 13:26:06 +02001175 ret = lyb_parse_magic_number(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001176 LY_CHECK_GOTO(ret, cleanup);
1177
1178 /* read header */
Radek Krejci1798aae2020-07-14 13:26:06 +02001179 ret = lyb_parse_header(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001180 LY_CHECK_GOTO(ret, cleanup);
1181
1182 /* read model count */
Radek Krejci1798aae2020-07-14 13:26:06 +02001183 lyb_read_number(&count, sizeof count, 2, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001184
1185 /* read all models */
1186 for (i = 0; i < count; ++i) {
1187 /* module name length */
1188 len = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +02001189 lyb_read_number(&len, sizeof len, 2, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001190
1191 /* model name */
Radek Krejci1798aae2020-07-14 13:26:06 +02001192 lyb_read(buf, len, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001193
1194 /* revision */
Radek Krejci1798aae2020-07-14 13:26:06 +02001195 lyb_read(buf, 2, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001196 }
1197
Radek Krejci1798aae2020-07-14 13:26:06 +02001198 while (lybctx->in->current[0]) {
Michal Vasko60ea6352020-06-29 13:39:39 +02001199 /* register a new subtree */
Radek Krejci1798aae2020-07-14 13:26:06 +02001200 ret = lyb_read_start_subtree(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001201 LY_CHECK_GOTO(ret, cleanup);
1202
1203 /* skip it */
Radek Krejci1798aae2020-07-14 13:26:06 +02001204 lyb_skip_subtree(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001205
1206 /* subtree finished */
Radek Krejci1798aae2020-07-14 13:26:06 +02001207 ret = lyb_read_stop_subtree(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001208 LY_CHECK_GOTO(ret, cleanup);
1209 }
1210
1211 /* read the last zero, parsing finished */
Radek Krejci1798aae2020-07-14 13:26:06 +02001212 ly_in_skip(lybctx->in, 1);
Michal Vasko60ea6352020-06-29 13:39:39 +02001213
1214cleanup:
Radek Krejci1798aae2020-07-14 13:26:06 +02001215 count = lybctx->in->current - lybctx->in->start;
Michal Vasko63f3d842020-07-08 10:10:14 +02001216
Radek Krejci1798aae2020-07-14 13:26:06 +02001217 ly_in_free(lybctx->in, 0);
1218 lylyb_ctx_free(lybctx);
1219
Michal Vasko63f3d842020-07-08 10:10:14 +02001220 return ret ? -1 : count;
Michal Vasko60ea6352020-06-29 13:39:39 +02001221}