blob: 80c4159362599aa4bfcba07ce4e7d1a91cb3cf9c [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"
27#include "log.h"
Radek Krejci7931b192020-06-25 17:05:03 +020028#include "parser_data.h"
29#include "parser_internal.h"
Radek Krejciad97c5f2020-06-30 09:19:28 +020030#include "set.h"
31#include "tree.h"
Michal Vasko60ea6352020-06-29 13:39:39 +020032#include "tree_data_internal.h"
33#include "tree_schema.h"
34#include "validation.h"
35
Radek Krejci1798aae2020-07-14 13:26:06 +020036void
37lylyb_ctx_free(struct lylyb_ctx *ctx)
38{
39 LY_ARRAY_COUNT_TYPE u;
40
41 LY_ARRAY_FREE(ctx->subtrees);
42 LY_ARRAY_FREE(ctx->models);
43
44 LY_ARRAY_FOR(ctx->sib_hts, u) {
45 lyht_free(ctx->sib_hts[u].ht);
46 }
47 LY_ARRAY_FREE(ctx->sib_hts);
48
49 free(ctx);
50}
51
52void
53lyd_lyb_ctx_free(struct lyd_ctx *lydctx)
54{
55 struct lyd_lyb_ctx *ctx = (struct lyd_lyb_ctx *)lydctx;
56
57 lyd_ctx_free(lydctx);
58 lylyb_ctx_free(ctx->lybctx);
59 free(ctx);
60}
61
Michal Vasko60ea6352020-06-29 13:39:39 +020062/**
63 * @brief Read YANG data from LYB input. Metadata are handled transparently and not returned.
64 *
65 * @param[in] buf Destination buffer.
66 * @param[in] count Number of bytes to read.
67 * @param[in] lybctx LYB context.
68 */
69static void
Radek Krejci1798aae2020-07-14 13:26:06 +020070lyb_read(uint8_t *buf, size_t count, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +020071{
Michal Vaskofd69e1d2020-07-03 11:57:17 +020072 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +020073 struct lyd_lyb_subtree *empty;
74 size_t to_read;
75 uint8_t meta_buf[LYB_META_BYTES];
76
77 assert(lybctx);
78
79 while (1) {
80 /* check for fully-read (empty) data chunks */
81 to_read = count;
82 empty = NULL;
83 LY_ARRAY_FOR(lybctx->subtrees, u) {
84 /* we want the innermost chunks resolved first, so replace previous empty chunks,
85 * also ignore chunks that are completely finished, there is nothing for us to do */
86 if ((lybctx->subtrees[u].written <= to_read) && lybctx->subtrees[u].position) {
87 /* empty chunk, do not read more */
88 to_read = lybctx->subtrees[u].written;
89 empty = &lybctx->subtrees[u];
90 }
91 }
92
93 if (!empty && !count) {
94 break;
95 }
96
97 /* we are actually reading some data, not just finishing another chunk */
98 if (to_read) {
99 if (buf) {
Michal Vasko63f3d842020-07-08 10:10:14 +0200100 ly_in_read(lybctx->in, buf, to_read);
101 } else {
102 ly_in_skip(lybctx->in, to_read);
Michal Vasko60ea6352020-06-29 13:39:39 +0200103 }
104
105 LY_ARRAY_FOR(lybctx->subtrees, u) {
106 /* decrease all written counters */
107 lybctx->subtrees[u].written -= to_read;
108 assert(lybctx->subtrees[u].written <= LYB_SIZE_MAX);
109 }
110 /* decrease count/buf */
111 count -= to_read;
112 if (buf) {
113 buf += to_read;
114 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200115 }
116
117 if (empty) {
118 /* read the next chunk meta information */
Michal Vasko63f3d842020-07-08 10:10:14 +0200119 ly_in_read(lybctx->in, meta_buf, LYB_META_BYTES);
Michal Vasko60ea6352020-06-29 13:39:39 +0200120 empty->written = meta_buf[0];
121 empty->inner_chunks = meta_buf[1];
122
123 /* remember whether there is a following chunk or not */
124 empty->position = (empty->written == LYB_SIZE_MAX ? 1 : 0);
Michal Vasko60ea6352020-06-29 13:39:39 +0200125 }
126 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200127}
128
129/**
130 * @brief Read a number.
131 *
132 * @param[in] num Destination buffer.
133 * @param[in] num_size Size of @p num.
134 * @param[in] bytes Number of bytes to read.
135 * @param[in] lybctx LYB context.
136 */
137static void
Radek Krejci1798aae2020-07-14 13:26:06 +0200138lyb_read_number(void *num, size_t num_size, size_t bytes, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200139{
140 uint64_t buf = 0;
141
142 lyb_read((uint8_t *)&buf, bytes, lybctx);
143
144 /* correct byte order */
145 buf = le64toh(buf);
146
147 switch (num_size) {
148 case 1:
149 *((uint8_t *)num) = buf;
150 break;
151 case 2:
152 *((uint16_t *)num) = buf;
153 break;
154 case 4:
155 *((uint32_t *)num) = buf;
156 break;
157 case 8:
158 *((uint64_t *)num) = buf;
159 break;
160 default:
161 LOGINT(lybctx->ctx);
162 }
163}
164
165/**
166 * @brief Read a string.
167 *
168 * @param[in] str Destination buffer, is allocated.
169 * @param[in] with_length Whether the string is preceded with its length or it ends at the end of this subtree.
170 * @param[in] lybctx LYB context.
171 * @return LY_ERR value.
172 */
173static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200174lyb_read_string(char **str, int with_length, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200175{
176 int next_chunk = 0;
177 size_t len = 0, cur_len;
178
179 *str = NULL;
180
181 if (with_length) {
182 lyb_read_number(&len, sizeof len, 2, lybctx);
183 } else {
184 /* read until the end of this subtree */
185 len = LYB_LAST_SUBTREE(lybctx).written;
186 if (LYB_LAST_SUBTREE(lybctx).position) {
187 next_chunk = 1;
188 }
189 }
190
191 *str = malloc((len + 1) * sizeof **str);
192 LY_CHECK_ERR_RET(!*str, LOGMEM(lybctx->ctx), LY_EMEM);
193
194 lyb_read((uint8_t *)*str, len, lybctx);
195
196 while (next_chunk) {
197 cur_len = LYB_LAST_SUBTREE(lybctx).written;
198 if (LYB_LAST_SUBTREE(lybctx).position) {
199 next_chunk = 1;
200 } else {
201 next_chunk = 0;
202 }
203
204 *str = ly_realloc(*str, (len + cur_len + 1) * sizeof **str);
205 LY_CHECK_ERR_RET(!*str, LOGMEM(lybctx->ctx), LY_EMEM);
206
207 lyb_read(((uint8_t *)*str) + len, cur_len, lybctx);
208
209 len += cur_len;
210 }
211
212 ((char *)*str)[len] = '\0';
213 return LY_SUCCESS;
214}
215
216/**
217 * @brief Stop the current subtree - change LYB context state.
218 *
219 * @param[in] lybctx LYB context.
220 * @return LY_ERR value.
221 */
222static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200223lyb_read_stop_subtree(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200224{
225 if (LYB_LAST_SUBTREE(lybctx).written) {
226 LOGINT_RET(lybctx->ctx);
227 }
228
229 LY_ARRAY_DECREMENT(lybctx->subtrees);
230 return LY_SUCCESS;
231}
232
233/**
234 * @brief Start a new subtree - change LYB context state but also read the expected metadata.
235 *
236 * @param[in] lybctx LYB context.
237 * @return LY_ERR value.
238 */
239static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200240lyb_read_start_subtree(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200241{
242 uint8_t meta_buf[LYB_META_BYTES];
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200243 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +0200244
245 if (!lybctx->subtrees) {
Radek Krejcif6d14cb2020-07-02 16:11:45 +0200246 assert(lybctx->subtree_size == 0);
Michal Vasko60ea6352020-06-29 13:39:39 +0200247 u = 0;
248 } else {
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200249 u = LY_ARRAY_COUNT(lybctx->subtrees);
Michal Vasko60ea6352020-06-29 13:39:39 +0200250 }
251 if (u == lybctx->subtree_size) {
252 LY_ARRAY_CREATE_RET(lybctx->ctx, lybctx->subtrees, u + LYB_SUBTREE_STEP, LY_EMEM);
253 lybctx->subtree_size = u + LYB_SUBTREE_STEP;
254 }
255
Michal Vasko63f3d842020-07-08 10:10:14 +0200256 LY_CHECK_RET(ly_in_read(lybctx->in, meta_buf, LYB_META_BYTES));
Michal Vasko60ea6352020-06-29 13:39:39 +0200257
258 LY_ARRAY_INCREMENT(lybctx->subtrees);
259 LYB_LAST_SUBTREE(lybctx).written = meta_buf[0];
260 LYB_LAST_SUBTREE(lybctx).inner_chunks = meta_buf[LYB_SIZE_BYTES];
261 LYB_LAST_SUBTREE(lybctx).position = (LYB_LAST_SUBTREE(lybctx).written == LYB_SIZE_MAX ? 1 : 0);
262
Michal Vasko60ea6352020-06-29 13:39:39 +0200263 return LY_SUCCESS;
264}
265
266/**
267 * @brief Parse YANG model info.
268 *
269 * @param[in] lybctx LYB context.
270 * @param[out] mod Parsed module.
271 * @return LY_ERR value.
272 */
273static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200274lyb_parse_model(struct lylyb_ctx *lybctx, uint32_t parse_options, const struct lys_module **mod)
Michal Vasko60ea6352020-06-29 13:39:39 +0200275{
276 LY_ERR ret = LY_SUCCESS;
277 char *mod_name = NULL, mod_rev[11];
278 uint16_t rev;
279
280 /* model name */
281 ret = lyb_read_string(&mod_name, 1, lybctx);
282 LY_CHECK_GOTO(ret, cleanup);
283
284 /* revision */
285 lyb_read_number(&rev, sizeof rev, 2, lybctx);
286
287 if (!mod_name[0]) {
288 /* opaq node, no module */
289 *mod = NULL;
290 goto cleanup;
291 }
292
293 if (rev) {
294 sprintf(mod_rev, "%04u-%02u-%02u", ((rev & 0xFE00) >> 9) + 2000, (rev & 0x01E0) >> 5, rev & 0x001Fu);
295 *mod = ly_ctx_get_module(lybctx->ctx, mod_name, mod_rev);
Radek Krejci1798aae2020-07-14 13:26:06 +0200296 if ((parse_options & LYD_PARSE_LYB_MOD_UPDATE) && !(*mod)) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200297 /* try to use an updated module */
298 *mod = ly_ctx_get_module_implemented(lybctx->ctx, mod_name);
299 if (*mod && (!(*mod)->revision || (strcmp((*mod)->revision, mod_rev) < 0))) {
300 /* not an implemented module in a newer revision */
301 *mod = NULL;
302 }
303 }
304 } else {
305 *mod = ly_ctx_get_module_latest(lybctx->ctx, mod_name);
306 }
307 /* TODO data_clb supported?
308 if (lybctx->ctx->data_clb) {
309 if (!*mod) {
310 *mod = lybctx->ctx->data_clb(lybctx->ctx, mod_name, NULL, 0, lybctx->ctx->data_clb_data);
311 } else if (!(*mod)->implemented) {
312 *mod = lybctx->ctx->data_clb(lybctx->ctx, mod_name, (*mod)->ns, LY_MODCLB_NOT_IMPLEMENTED, lybctx->ctx->data_clb_data);
313 }
314 }*/
315
316 if (!*mod || !(*mod)->implemented) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200317 if (parse_options & LYD_PARSE_STRICT) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200318 if (!*mod) {
319 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid context for LYB data parsing, missing module \"%s%s%s\".",
320 mod_name, rev ? "@" : "", rev ? mod_rev : "");
321 } else if (!(*mod)->implemented) {
322 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid context for LYB data parsing, module \"%s%s%s\" not implemented.",
323 mod_name, rev ? "@" : "", rev ? mod_rev : "");
324 }
325 ret = LY_EINVAL;
326 goto cleanup;
327 }
328
329 }
330
331cleanup:
332 free(mod_name);
333 return ret;
334}
335
336/**
337 * @brief Parse YANG node metadata.
338 *
339 * @param[in] lybctx LYB context.
340 * @param[in] sparent Schema parent node.
341 * @param[out] meta Parsed metadata.
342 * @return LY_ERR value.
343 */
344static LY_ERR
345lyb_parse_metadata(struct lyd_lyb_ctx *lybctx, const struct lysc_node *sparent, struct lyd_meta **meta)
346{
347 LY_ERR ret = LY_SUCCESS;
Michal Vasko1e5d5612020-07-03 13:29:26 +0200348 int dynamic;
Michal Vasko60ea6352020-06-29 13:39:39 +0200349 uint8_t i, count = 0;
Michal Vasko1e5d5612020-07-03 13:29:26 +0200350 char *meta_name = NULL, *meta_value;
Michal Vasko60ea6352020-06-29 13:39:39 +0200351 const struct lys_module *mod;
352
353 /* read number of attributes stored */
Radek Krejci1798aae2020-07-14 13:26:06 +0200354 lyb_read(&count, 1, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200355
356 /* read attributes */
357 for (i = 0; i < count; ++i) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200358 ret = lyb_read_start_subtree(lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200359 LY_CHECK_GOTO(ret, cleanup);
360
361 /* find model */
Radek Krejci1798aae2020-07-14 13:26:06 +0200362 ret = lyb_parse_model(lybctx->lybctx, lybctx->parse_options, &mod);
Michal Vasko60ea6352020-06-29 13:39:39 +0200363 LY_CHECK_GOTO(ret, cleanup);
364
365 if (!mod) {
366 /* skip it */
367 do {
Radek Krejci1798aae2020-07-14 13:26:06 +0200368 lyb_read(NULL, LYB_LAST_SUBTREE(lybctx->lybctx).written, lybctx->lybctx);
369 } while (LYB_LAST_SUBTREE(lybctx->lybctx).written);
Michal Vasko60ea6352020-06-29 13:39:39 +0200370 goto stop_subtree;
371 }
372
373 /* meta name */
Radek Krejci1798aae2020-07-14 13:26:06 +0200374 ret = lyb_read_string(&meta_name, 1, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200375 LY_CHECK_GOTO(ret, cleanup);
376
377 /* meta value */
Radek Krejci1798aae2020-07-14 13:26:06 +0200378 ret = lyb_read_string(&meta_value, 0, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200379 LY_CHECK_GOTO(ret, cleanup);
380 dynamic = 1;
381
382 /* create metadata */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200383 ret = lyd_parser_create_meta((struct lyd_ctx *)lybctx, NULL, meta, mod, meta_name, strlen(meta_name), meta_value,
384 ly_strlen(meta_value), &dynamic, 0, LY_PREF_JSON, NULL, sparent);
Michal Vasko60ea6352020-06-29 13:39:39 +0200385
386 /* free strings */
387 free(meta_name);
388 meta_name = NULL;
389 if (dynamic) {
390 free(meta_value);
391 dynamic = 0;
392 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200393
Radek Krejci1798aae2020-07-14 13:26:06 +0200394 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200395
396stop_subtree:
Radek Krejci1798aae2020-07-14 13:26:06 +0200397 ret = lyb_read_stop_subtree(lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200398 LY_CHECK_GOTO(ret, cleanup);
399 }
400
401cleanup:
402 free(meta_name);
Michal Vasko60ea6352020-06-29 13:39:39 +0200403 if (ret) {
Michal Vasko3a41dff2020-07-15 14:30:28 +0200404 lyd_free_meta_siblings(*meta);
Michal Vasko60ea6352020-06-29 13:39:39 +0200405 *meta = NULL;
406 }
407 return ret;
408}
409
410/**
411 * @brief Parse opaque prefixes structure.
412 *
413 * @param[in] lybctx LYB context.
414 * @param[out] prefs Parsed prefixes.
415 * @return LY_ERR value.
416 */
417static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200418lyb_parse_opaq_prefixes(struct lylyb_ctx *lybctx, struct ly_prefix **prefs)
Michal Vasko60ea6352020-06-29 13:39:39 +0200419{
420 LY_ERR ret = LY_SUCCESS;
421 uint8_t count, i;
422 char *str;
423
424 /* read count */
425 lyb_read(&count, 1, lybctx);
426 if (!count) {
427 return LY_SUCCESS;
428 }
429
430 LY_ARRAY_CREATE_RET(lybctx->ctx, *prefs, count, LY_EMEM);
431 for (i = 0; i < count; ++i) {
432 LY_ARRAY_INCREMENT(*prefs);
433
434 /* prefix */
435 ret = lyb_read_string(&str, 1, lybctx);
436 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200437 (*prefs)[i].id = lydict_insert_zc(lybctx->ctx, str);
Michal Vasko60ea6352020-06-29 13:39:39 +0200438
Radek Krejci1798aae2020-07-14 13:26:06 +0200439 /* module reference is the same as JSON name */
Michal Vasko60ea6352020-06-29 13:39:39 +0200440 ret = lyb_read_string(&str, 1, lybctx);
441 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200442 (*prefs)[i].module_name = lydict_insert_zc(lybctx->ctx, str);
Michal Vasko60ea6352020-06-29 13:39:39 +0200443 }
444
445cleanup:
446 if (ret) {
447 ly_free_val_prefs(lybctx->ctx, *prefs);
448 *prefs = NULL;
449 }
450 return ret;
451}
452
453/**
454 * @brief Parse opaque attributes.
455 *
456 * @param[in] lybctx LYB context.
457 * @param[out] attr Parsed attributes.
458 * @return LY_ERR value.
459 */
460static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200461lyb_parse_attributes(struct lylyb_ctx *lybctx, struct lyd_attr **attr)
Michal Vasko60ea6352020-06-29 13:39:39 +0200462{
463 LY_ERR ret = LY_SUCCESS;
464 uint8_t count, i;
Radek Krejci1798aae2020-07-14 13:26:06 +0200465 struct lyd_attr *attr2;
466 char *prefix = NULL, *module_name = NULL, *name = NULL, *value = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200467 int dynamic = 0;
468 LYD_FORMAT format = 0;
469 struct ly_prefix *val_prefs = NULL;
470
471 /* read count */
472 lyb_read(&count, 1, lybctx);
473
474 /* read attributes */
475 for (i = 0; i < count; ++i) {
476 ret = lyb_read_start_subtree(lybctx);
477 LY_CHECK_GOTO(ret, cleanup);
478
479 /* prefix, may be emtpy */
480 ret = lyb_read_string(&prefix, 1, lybctx);
481 LY_CHECK_GOTO(ret, cleanup);
482 if (!prefix[0]) {
483 free(prefix);
484 prefix = NULL;
485 }
486
487 /* namespace, may be empty */
Radek Krejci1798aae2020-07-14 13:26:06 +0200488 ret = lyb_read_string(&module_name, 1, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200489 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200490 if (!module_name[0]) {
491 free(module_name);
492 module_name = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200493 }
494
495 /* name */
496 ret = lyb_read_string(&name, 1, lybctx);
497 LY_CHECK_GOTO(ret, cleanup);
498
499 /* value prefixes */
500 ret = lyb_parse_opaq_prefixes(lybctx, &val_prefs);
501 LY_CHECK_GOTO(ret, cleanup);
502
503 /* format */
504 lyb_read((uint8_t *)&format, 1, lybctx);
505
506 /* value */
507 ret = lyb_read_string(&value, 0, lybctx);
508 LY_CHECK_GOTO(ret, cleanup);
509 dynamic = 1;
510
511 /* attr2 is always changed to the created attribute */
Radek Krejci1798aae2020-07-14 13:26:06 +0200512 ret = lyd_create_attr(NULL, &attr2, lybctx->ctx, name, strlen(name), value, ly_strlen(value), &dynamic, 0, format,
513 val_prefs, prefix, ly_strlen(prefix), module_name, ly_strlen(module_name));
Michal Vasko60ea6352020-06-29 13:39:39 +0200514 LY_CHECK_GOTO(ret, cleanup);
515
516 free(prefix);
517 prefix = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +0200518 free(module_name);
519 module_name = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200520 free(name);
521 name = NULL;
522 val_prefs = NULL;
523 assert(!dynamic);
524 value = NULL;
525
526 if (!*attr) {
527 *attr = attr2;
528 }
529
530 ret = lyb_read_stop_subtree(lybctx);
531 LY_CHECK_GOTO(ret, cleanup);
532 }
533
534cleanup:
535 free(prefix);
Radek Krejci1798aae2020-07-14 13:26:06 +0200536 free(module_name);
Michal Vasko60ea6352020-06-29 13:39:39 +0200537 free(name);
538 if (dynamic) {
539 free(value);
540 }
541 ly_free_val_prefs(lybctx->ctx, val_prefs);
542 if (ret) {
Michal Vasko3a41dff2020-07-15 14:30:28 +0200543 ly_free_attr_siblings(lybctx->ctx, *attr);
Michal Vasko60ea6352020-06-29 13:39:39 +0200544 *attr = NULL;
545 }
546 return ret;
547}
548
549/**
550 * @brief Check whether a schema node matches a hash(es).
551 *
552 * @param[in] sibling Schema node to check.
553 * @param[in] hash Hash array to check.
554 * @param[in] hash_count Number of hashes in @p hash.
555 * @return non-zero if matches,
556 * @return 0 if not.
557 */
558static int
559lyb_is_schema_hash_match(struct lysc_node *sibling, LYB_HASH *hash, uint8_t hash_count)
560{
561 LYB_HASH sibling_hash;
562 uint8_t i;
563
564 /* compare all the hashes starting from collision ID 0 */
565 for (i = 0; i < hash_count; ++i) {
566 sibling_hash = lyb_hash(sibling, i);
567 if (sibling_hash != hash[i]) {
568 return 0;
569 }
570 }
571
572 return 1;
573}
574
575/**
Michal Vasko60ea6352020-06-29 13:39:39 +0200576 * @brief Parse schema node hash.
577 *
578 * @param[in] lybctx LYB context.
579 * @param[in] sparent Schema parent, must be set if @p mod is not.
580 * @param[in] mod Module of the top-level node, must be set if @p sparent is not.
581 * @param[out] snode Parsed found schema node, may be NULL if opaque.
582 * @return LY_ERR value.
583 */
584static LY_ERR
585lyb_parse_schema_hash(struct lyd_lyb_ctx *lybctx, const struct lysc_node *sparent, const struct lys_module *mod,
586 const struct lysc_node **snode)
587{
588 LY_ERR ret;
589 uint8_t i, j;
590 const struct lysc_node *sibling;
591 LYB_HASH hash[LYB_HASH_BITS - 1];
592 int getnext_opts;
593
594 *snode = NULL;
595 /* leave if-feature check for validation */
596 getnext_opts = LYS_GETNEXT_NOSTATECHECK | (lybctx->int_opts & LYD_INTOPT_REPLY ? LYS_GETNEXT_OUTPUT : 0);
597
598 /* read the first hash */
Radek Krejci1798aae2020-07-14 13:26:06 +0200599 lyb_read(&hash[0], sizeof *hash, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200600
601 if (!hash[0]) {
602 /* opaque node */
603 return LY_SUCCESS;
604 }
605
606 /* based on the first hash read all the other ones, if any */
607 for (i = 0; !(hash[0] & (LYB_HASH_COLLISION_ID >> i)); ++i) {
608 if (i > LYB_HASH_BITS) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200609 LOGINT_RET(lybctx->lybctx->ctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200610 }
611 }
612
613 /* move the first hash on its accurate position */
614 hash[i] = hash[0];
615
616 /* read the rest of hashes */
617 for (j = i; j; --j) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200618 lyb_read(&hash[j - 1], sizeof *hash, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200619
620 /* correct collision ID */
621 assert(hash[j - 1] & (LYB_HASH_COLLISION_ID >> (j - 1)));
622 /* preceded with zeros */
623 assert(!(hash[j - 1] & (LYB_HASH_MASK << (LYB_HASH_BITS - (j - 1)))));
624 }
625
626 /* find our node with matching hashes */
627 sibling = NULL;
628 while ((sibling = lys_getnext(sibling, sparent, mod ? mod->compiled : NULL, getnext_opts))) {
629 /* skip schema nodes from models not present during printing */
Radek Krejci1798aae2020-07-14 13:26:06 +0200630 if (lyb_has_schema_model(sibling, lybctx->lybctx->models)
Michal Vasko60ea6352020-06-29 13:39:39 +0200631 && lyb_is_schema_hash_match((struct lysc_node *)sibling, hash, i + 1)) {
632 /* match found */
633 break;
634 }
635 }
636
Radek Krejci7931b192020-06-25 17:05:03 +0200637 if (!sibling && (lybctx->parse_options & LYD_PARSE_STRICT)) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200638 if (mod) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200639 LOGVAL(lybctx->lybctx->ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "Failed to find matching hash for a top-level node"
Michal Vasko60ea6352020-06-29 13:39:39 +0200640 " from \"%s\".", mod->name);
641 } else {
Radek Krejci1798aae2020-07-14 13:26:06 +0200642 LOGVAL(lybctx->lybctx->ctx, LY_VLOG_LYSC, sparent, LYVE_REFERENCE, "Failed to find matching hash for a child node"
Michal Vasko60ea6352020-06-29 13:39:39 +0200643 " of \"%s\".", sparent->name);
644 }
645 return LY_EVALID;
Radek Krejci1798aae2020-07-14 13:26:06 +0200646 } else if (sibling && (ret = lyd_parser_check_schema((struct lyd_ctx *)lybctx, sibling))) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200647 return ret;
648 }
649
650 *snode = sibling;
651 return LY_SUCCESS;
652}
653
654/**
655 * @brief Read until the end of the current subtree.
656 *
657 * @param[in] lybctx LYB context.
658 */
659static void
Radek Krejci1798aae2020-07-14 13:26:06 +0200660lyb_skip_subtree(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200661{
Michal Vasko60ea6352020-06-29 13:39:39 +0200662 do {
663 /* first skip any meta information inside */
Michal Vasko63f3d842020-07-08 10:10:14 +0200664 ly_in_skip(lybctx->in, LYB_LAST_SUBTREE(lybctx).inner_chunks * LYB_META_BYTES);
Michal Vasko60ea6352020-06-29 13:39:39 +0200665
666 /* then read data */
667 lyb_read(NULL, LYB_LAST_SUBTREE(lybctx).written, lybctx);
668 } while (LYB_LAST_SUBTREE(lybctx).written);
669}
670
671/**
672 * @brief Parse LYB subtree.
673 *
674 * @param[in] lybctx LYB context.
675 * @param[in] parent Data parent of the subtree, must be set if @p first is not.
676 * @param[in,out] first First top-level sibling, must be set if @p parent is not.
677 * @return LY_ERR value.
678 */
679static LY_ERR
680lyb_parse_subtree_r(struct lyd_lyb_ctx *lybctx, struct lyd_node_inner *parent, struct lyd_node **first)
681{
682 LY_ERR ret = LY_SUCCESS;
683 struct lyd_node *node = NULL, *tree;
684 const struct lys_module *mod;
685 const struct lysc_node *snode = NULL;
686 struct lyd_meta *meta = NULL, *m;
Radek Krejci1798aae2020-07-14 13:26:06 +0200687 struct lyd_attr *attr = NULL, *a;
Michal Vasko60ea6352020-06-29 13:39:39 +0200688 struct ly_prefix *val_prefs = NULL;
689 LYD_ANYDATA_VALUETYPE value_type;
Radek Krejci1798aae2020-07-14 13:26:06 +0200690 char *value = NULL, *name = NULL, *prefix = NULL, *module_key = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200691 int dynamic = 0;
692 LYD_FORMAT format = 0;
693 int prev_lo;
Radek Krejci1798aae2020-07-14 13:26:06 +0200694 const struct ly_ctx *ctx = lybctx->lybctx->ctx;
Michal Vasko60ea6352020-06-29 13:39:39 +0200695
696 /* register a new subtree */
Radek Krejci1798aae2020-07-14 13:26:06 +0200697 LY_CHECK_GOTO(ret = lyb_read_start_subtree(lybctx->lybctx), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200698
699 if (!parent) {
700 /* top-level, read module name */
Radek Krejci1798aae2020-07-14 13:26:06 +0200701 ret = lyb_parse_model(lybctx->lybctx, lybctx->parse_options, &mod);
Michal Vasko60ea6352020-06-29 13:39:39 +0200702 LY_CHECK_GOTO(ret, cleanup);
703
704 /* read hash, find the schema node starting from mod */
705 ret = lyb_parse_schema_hash(lybctx, NULL, mod, &snode);
706 LY_CHECK_GOTO(ret, cleanup);
707 } else {
708 /* read hash, find the schema node starting from parent schema */
709 ret = lyb_parse_schema_hash(lybctx, parent->schema, NULL, &snode);
710 LY_CHECK_GOTO(ret, cleanup);
711 }
712
Radek Krejci7931b192020-06-25 17:05:03 +0200713 if (!snode && !(lybctx->parse_options & LYD_PARSE_OPAQ)) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200714 /* unknown data, skip them */
Radek Krejci1798aae2020-07-14 13:26:06 +0200715 lyb_skip_subtree(lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200716 goto stop_subtree;
717 }
718
719 /* create metadata/attributes */
720 if (snode) {
721 ret = lyb_parse_metadata(lybctx, snode, &meta);
722 LY_CHECK_GOTO(ret, cleanup);
723 } else {
Radek Krejci1798aae2020-07-14 13:26:06 +0200724 ret = lyb_parse_attributes(lybctx->lybctx, &attr);
Michal Vasko60ea6352020-06-29 13:39:39 +0200725 LY_CHECK_GOTO(ret, cleanup);
726 }
727
728 if (!snode) {
729 /* parse prefix */
Radek Krejci1798aae2020-07-14 13:26:06 +0200730 ret = lyb_read_string(&prefix, 1, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200731 LY_CHECK_GOTO(ret, cleanup);
732
Radek Krejci1798aae2020-07-14 13:26:06 +0200733 /* parse module key */
734 ret = lyb_read_string(&module_key, 1, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200735 LY_CHECK_GOTO(ret, cleanup);
736
737 /* parse name */
Radek Krejci1798aae2020-07-14 13:26:06 +0200738 ret = lyb_read_string(&name, 1, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200739 LY_CHECK_GOTO(ret, cleanup);
740
741 /* parse value prefixes */
Radek Krejci1798aae2020-07-14 13:26:06 +0200742 ret = lyb_parse_opaq_prefixes(lybctx->lybctx, &val_prefs);
Michal Vasko60ea6352020-06-29 13:39:39 +0200743 LY_CHECK_GOTO(ret, cleanup);
744
745 /* parse format */
Radek Krejci1798aae2020-07-14 13:26:06 +0200746 lyb_read((uint8_t *)&format, 1, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200747
748 /* parse value */
Radek Krejci1798aae2020-07-14 13:26:06 +0200749 ret = lyb_read_string(&value, 0, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200750 LY_CHECK_GOTO(ret, cleanup);
751 dynamic = 1;
752
753 /* create node */
Radek Krejci1798aae2020-07-14 13:26:06 +0200754 ret = lyd_create_opaq(ctx, name, strlen(name), value, strlen(value), &dynamic, 0, format, val_prefs, prefix,
755 ly_strlen(prefix), module_key, ly_strlen(module_key), &node);
Michal Vasko60ea6352020-06-29 13:39:39 +0200756 LY_CHECK_GOTO(ret, cleanup);
757
758 /* process children */
Radek Krejci1798aae2020-07-14 13:26:06 +0200759 while (LYB_LAST_SUBTREE(lybctx->lybctx).written) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200760 ret = lyb_parse_subtree_r(lybctx, (struct lyd_node_inner *)node, NULL);
761 LY_CHECK_GOTO(ret, cleanup);
762 }
763 } else if (snode->nodetype & LYD_NODE_TERM) {
764 /* parse value */
Radek Krejci1798aae2020-07-14 13:26:06 +0200765 ret = lyb_read_string(&value, 0, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200766 LY_CHECK_GOTO(ret, cleanup);
767 dynamic = 1;
768
769 /* create node */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200770 ret = lyd_parser_create_term((struct lyd_ctx *)lybctx, snode, value, ly_strlen(value), &dynamic, 0,
771 LY_PREF_JSON, NULL, &node);
Michal Vasko60ea6352020-06-29 13:39:39 +0200772 if (dynamic) {
773 free(value);
774 dynamic = 0;
775 }
776 value = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +0200777 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200778 } else if (snode->nodetype & LYD_NODE_INNER) {
779 /* create node */
780 ret = lyd_create_inner(snode, &node);
781 LY_CHECK_GOTO(ret, cleanup);
782
783 /* process children */
Radek Krejci1798aae2020-07-14 13:26:06 +0200784 while (LYB_LAST_SUBTREE(lybctx->lybctx).written) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200785 ret = lyb_parse_subtree_r(lybctx, (struct lyd_node_inner *)node, NULL);
786 LY_CHECK_GOTO(ret, cleanup);
787 }
788
Radek Krejci7931b192020-06-25 17:05:03 +0200789 if (!(lybctx->parse_options & LYD_PARSE_ONLY)) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200790 /* new node validation, autodelete CANNOT occur, all nodes are new */
Michal Vasko8104fd42020-07-13 11:09:51 +0200791 ret = lyd_validate_new(lyd_node_children_p(node), snode, NULL, NULL);
Michal Vasko60ea6352020-06-29 13:39:39 +0200792 LY_CHECK_GOTO(ret, cleanup);
793
794 /* add any missing default children */
Michal Vaskoa6669ba2020-08-06 16:14:26 +0200795 ret = lyd_new_implicit_r(node, lyd_node_children_p(node), NULL, NULL, &lybctx->unres_node_type,
796 &lybctx->when_check, (lybctx->validate_options & LYD_VALIDATE_NO_STATE)
797 ? LYD_IMPLICIT_NO_STATE : 0, NULL);
Michal Vasko60ea6352020-06-29 13:39:39 +0200798 LY_CHECK_GOTO(ret, cleanup);
799 }
800
Michal Vasko751cb4d2020-07-14 12:25:28 +0200801 if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200802 /* rememeber the RPC/action/notification */
Radek Krejci1798aae2020-07-14 13:26:06 +0200803 lybctx->op_node = node;
Michal Vasko60ea6352020-06-29 13:39:39 +0200804 }
805 } else if (snode->nodetype & LYD_NODE_ANY) {
806 /* parse value type */
Radek Krejci1798aae2020-07-14 13:26:06 +0200807 lyb_read((uint8_t *)&value_type, sizeof value_type, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200808 if (value_type == LYD_ANYDATA_DATATREE) {
809 /* invalid situation */
Radek Krejci1798aae2020-07-14 13:26:06 +0200810 LOGINT(ctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200811 goto cleanup;
812 }
813
814 /* read anydata content */
Radek Krejci1798aae2020-07-14 13:26:06 +0200815 ret = lyb_read_string(&value, 0, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200816 LY_CHECK_GOTO(ret, cleanup);
817 dynamic = 1;
818
819 if (value_type == LYD_ANYDATA_LYB) {
820 /* turn logging off */
821 prev_lo = ly_log_options(0);
822
823 /* try to parse LYB into a data tree */
Radek Krejci1798aae2020-07-14 13:26:06 +0200824 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 +0200825 /* successfully parsed */
826 free(value);
827 value = (char *)tree;
828 value_type = LYD_ANYDATA_DATATREE;
829 }
Radek Krejci7931b192020-06-25 17:05:03 +0200830
831 /* turn logging on again */
832 ly_log_options(prev_lo);
Michal Vasko60ea6352020-06-29 13:39:39 +0200833 }
834
835 /* create node */
836 ret = lyd_create_any(snode, value, value_type, &node);
837 LY_CHECK_GOTO(ret, cleanup);
838
839 dynamic = 0;
840 value = NULL;
841 }
842 assert(node);
843
844 /* add/correct flags */
845 if (snode) {
Radek Krejci7931b192020-06-25 17:05:03 +0200846 lyd_parse_set_data_flags(node, &lybctx->when_check, &meta, lybctx->parse_options);
Michal Vasko60ea6352020-06-29 13:39:39 +0200847 }
848
849 /* add metadata/attributes */
850 if (snode) {
851 LY_LIST_FOR(meta, m) {
852 m->parent = node;
853 }
854 node->meta = meta;
855 meta = NULL;
856 } else {
857 assert(!node->schema);
858 LY_LIST_FOR(attr, a) {
859 a->parent = (struct lyd_node_opaq *)node;
860 }
861 ((struct lyd_node_opaq *)node)->attr = attr;
862 attr = NULL;
863 }
864
Michal Vaskob104f112020-07-17 09:54:54 +0200865 /* insert, keep first pointer correct */
Michal Vasko60ea6352020-06-29 13:39:39 +0200866 lyd_insert_node((struct lyd_node *)parent, first, node);
Michal Vaskob104f112020-07-17 09:54:54 +0200867 while (!parent && (*first)->prev->next) {
868 *first = (*first)->prev;
869 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200870 node = NULL;
871
872stop_subtree:
873 /* end the subtree */
Radek Krejci1798aae2020-07-14 13:26:06 +0200874 ret = lyb_read_stop_subtree(lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200875 LY_CHECK_GOTO(ret, cleanup);
876
877cleanup:
878 free(prefix);
Radek Krejci1798aae2020-07-14 13:26:06 +0200879 free(module_key);
Michal Vasko60ea6352020-06-29 13:39:39 +0200880 free(name);
881 if (dynamic) {
882 free(value);
883 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200884 ly_free_val_prefs(ctx, val_prefs);
Michal Vasko60ea6352020-06-29 13:39:39 +0200885
Michal Vasko3a41dff2020-07-15 14:30:28 +0200886 lyd_free_meta_siblings(meta);
Radek Krejci1798aae2020-07-14 13:26:06 +0200887 ly_free_attr_siblings(ctx, attr);
Michal Vasko60ea6352020-06-29 13:39:39 +0200888 lyd_free_tree(node);
889 return ret;
890}
891
892/**
893 * @brief Parse used YANG data models.
894 *
895 * @param[in] lybctx LYB context.
896 * @return LY_ERR value.
897 */
898static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200899lyb_parse_data_models(struct lylyb_ctx *lybctx, uint32_t parse_options)
Michal Vasko60ea6352020-06-29 13:39:39 +0200900{
901 LY_ERR ret;
902 uint32_t count;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200903 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +0200904
905 /* read model count */
906 lyb_read_number(&count, sizeof count, 2, lybctx);
907
908 if (count) {
909 LY_ARRAY_CREATE_RET(lybctx->ctx, lybctx->models, count, LY_EMEM);
910
911 /* read modules */
912 for (u = 0; u < count; ++u) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200913 ret = lyb_parse_model(lybctx, parse_options, &lybctx->models[u]);
Michal Vasko60ea6352020-06-29 13:39:39 +0200914 LY_CHECK_RET(ret);
915 LY_ARRAY_INCREMENT(lybctx->models);
916 }
917 }
918
919 return LY_SUCCESS;
920}
921
922/**
923 * @brief Parse LYB magic number.
924 *
925 * @param[in] lybctx LYB context.
926 * @return LY_ERR value.
927 */
928static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200929lyb_parse_magic_number(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200930{
931 char magic_byte = 0;
932
933 lyb_read((uint8_t *)&magic_byte, 1, lybctx);
934 if (magic_byte != 'l') {
935 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid first magic number byte \"0x%02x\".", magic_byte);
936 return LY_EINVAL;
937 }
938
939 lyb_read((uint8_t *)&magic_byte, 1, lybctx);
940 if (magic_byte != 'y') {
941 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid second magic number byte \"0x%02x\".", magic_byte);
942 return LY_EINVAL;
943 }
944
945 lyb_read((uint8_t *)&magic_byte, 1, lybctx);
946 if (magic_byte != 'b') {
947 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid third magic number byte \"0x%02x\".", magic_byte);
948 return LY_EINVAL;
949 }
950
951 return LY_SUCCESS;
952}
953
954/**
955 * @brief Parse LYB header.
956 *
957 * @param[in] lybctx LYB context.
958 * @return LY_ERR value.
959 */
960static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200961lyb_parse_header(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200962{
963 uint8_t byte = 0;
964
965 /* version, future flags */
966 lyb_read((uint8_t *)&byte, sizeof byte, lybctx);
967
968 if ((byte & LYB_VERSION_MASK) != LYB_VERSION_NUM) {
969 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid LYB format version \"0x%02x\", expected \"0x%02x\".",
970 byte & LYB_VERSION_MASK, LYB_VERSION_NUM);
971 return LY_EINVAL;
972 }
973
974 return LY_SUCCESS;
975}
976
Radek Krejci1798aae2020-07-14 13:26:06 +0200977/*
978 * @param[in] ctx libyang context for logging
979 * @param[in] parent Parent node where to connect the parsed data, required for reply where the reply data are connected with the request operation
980 * @param[in] in Input structure.
981 * @param[in] parse_options Options for parser, see @ref dataparseroptions.
982 * @param[in] validate_options Options for the validation phase, see @ref datavalidationoptions.
983 * @param[in] data_type Internal data parser flag to distnguish type of the data to parse (RPC/Reply/Notification/regular data].
984 * @param[out] tree_p Parsed data tree. Note that NULL can be a valid result.
985 * @param[out] op_p Optional pointer to the actual operation. Useful for action and inner notifications.
986 * @param[out] lydctx_p Data parser context to finish validation.
987 */
988static LY_ERR
989lyd_parse_lyb_(const struct ly_ctx *ctx, struct lyd_node_inner **parent, struct ly_in *in, int parse_options, int validate_options,
990 int data_type, struct lyd_node **tree_p, struct lyd_node **op_p, struct lyd_ctx **lydctx_p)
Michal Vasko60ea6352020-06-29 13:39:39 +0200991{
992 LY_ERR ret = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +0200993 struct lyd_lyb_ctx *lybctx;
994 struct lyd_node *tree = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200995
Radek Krejci7931b192020-06-25 17:05:03 +0200996 assert(!(parse_options & ~LYD_PARSE_OPTS_MASK));
997 assert(!(validate_options & ~LYD_VALIDATE_OPTS_MASK));
998
Radek Krejci1798aae2020-07-14 13:26:06 +0200999 lybctx = calloc(1, sizeof *lybctx);
1000 LY_CHECK_ERR_RET(!lybctx, LOGMEM(ctx), LY_EMEM);
1001 lybctx->lybctx = calloc(1, sizeof *lybctx->lybctx);
Michal Vasko9acaf492020-08-13 09:05:58 +02001002 LY_CHECK_ERR_GOTO(!lybctx->lybctx, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001003
Radek Krejci1798aae2020-07-14 13:26:06 +02001004 lybctx->lybctx->in = in;
1005 lybctx->lybctx->ctx = ctx;
1006 lybctx->parse_options = parse_options;
1007 lybctx->validate_options = validate_options;
1008 lybctx->int_opts = data_type;
1009 lybctx->free = lyd_lyb_ctx_free;
Michal Vasko60ea6352020-06-29 13:39:39 +02001010
1011 /* read magic number */
Radek Krejci1798aae2020-07-14 13:26:06 +02001012 ret = lyb_parse_magic_number(lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001013 LY_CHECK_GOTO(ret, cleanup);
1014
1015 /* read header */
Radek Krejci1798aae2020-07-14 13:26:06 +02001016 ret = lyb_parse_header(lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001017 LY_CHECK_GOTO(ret, cleanup);
1018
1019 /* read used models */
Radek Krejci1798aae2020-07-14 13:26:06 +02001020 ret = lyb_parse_data_models(lybctx->lybctx, lybctx->parse_options);
Michal Vasko60ea6352020-06-29 13:39:39 +02001021 LY_CHECK_GOTO(ret, cleanup);
1022
1023 /* read subtree(s) */
Radek Krejci1798aae2020-07-14 13:26:06 +02001024 while (lybctx->lybctx->in->current[0]) {
1025 ret = lyb_parse_subtree_r(lybctx, parent ? *parent : NULL, &tree);
Michal Vasko60ea6352020-06-29 13:39:39 +02001026 LY_CHECK_GOTO(ret, cleanup);
1027 }
1028
1029 /* read the last zero, parsing finished */
Radek Krejci1798aae2020-07-14 13:26:06 +02001030 ly_in_skip(lybctx->lybctx->in, 1);
Michal Vasko60ea6352020-06-29 13:39:39 +02001031
Radek Krejci1798aae2020-07-14 13:26:06 +02001032 if (data_type == LYD_INTOPT_RPC) {
1033 /* make sure we have parsed some operation */
1034 if (!lybctx->op_node) {
1035 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_DATA, "Missing the \"rpc\"/\"action\" node.");
1036 ret = LY_EVALID;
1037 goto cleanup;
1038 }
1039
1040 if (op_p) {
1041 *op_p = lybctx->op_node;
1042 }
1043 assert(tree);
1044 } else if (data_type == LYD_INTOPT_REPLY) {
1045 struct lyd_node_inner *iter;
1046
1047 assert(parent);
1048
1049 if (op_p) {
1050 *op_p = (struct lyd_node*)(*parent);
1051 }
1052 for (iter = *parent; iter->parent; iter = iter->parent);
1053 tree = (struct lyd_node *)iter;
1054 *parent = NULL;
1055
1056 } else if (data_type == LYD_INTOPT_NOTIF) {
1057 /* make sure we have parsed some notification */
1058 if (!lybctx->op_node) {
1059 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_DATA, "Missing the \"notification\" node.");
1060 ret = LY_EVALID;
1061 goto cleanup;
1062 }
1063
1064 if (op_p) {
1065 *op_p = lybctx->op_node;
1066 }
1067 assert(tree);
1068 }
Michal Vasko60ea6352020-06-29 13:39:39 +02001069
1070cleanup:
Radek Krejci1798aae2020-07-14 13:26:06 +02001071 if (ret || !lydctx_p) {
1072 lyd_lyb_ctx_free((struct lyd_ctx *)lybctx);
1073 if (ret) {
1074 lyd_free_all(tree);
1075 }
1076 } else {
1077 *lydctx_p = (struct lyd_ctx *)lybctx;
1078 if (tree_p) {
1079 *tree_p = tree;
1080 }
Michal Vasko60ea6352020-06-29 13:39:39 +02001081 }
1082 return ret;
1083}
1084
1085LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +02001086lyd_parse_lyb_data(const struct ly_ctx *ctx, struct ly_in *in, int parse_options, int validate_options,
1087 struct lyd_node **tree_p, struct lyd_ctx **lydctx_p)
Michal Vasko60ea6352020-06-29 13:39:39 +02001088{
Radek Krejci1798aae2020-07-14 13:26:06 +02001089 return lyd_parse_lyb_(ctx, NULL, in, parse_options, validate_options, 0, tree_p, NULL, lydctx_p);
Michal Vasko60ea6352020-06-29 13:39:39 +02001090}
1091
1092LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +02001093lyd_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 +02001094{
Radek Krejci1798aae2020-07-14 13:26:06 +02001095 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 +02001096}
1097
1098LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +02001099lyd_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 +02001100{
Radek Krejci1798aae2020-07-14 13:26:06 +02001101 return lyd_parse_lyb_(ctx, NULL, in, LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, LYD_INTOPT_NOTIF, tree_p, ntf_p, NULL);
1102}
Michal Vasko60ea6352020-06-29 13:39:39 +02001103
Radek Krejci1798aae2020-07-14 13:26:06 +02001104LY_ERR
1105lyd_parse_lyb_reply(const struct lyd_node *request, struct ly_in *in, struct lyd_node **tree_p, struct lyd_node **op_p)
1106{
1107 LY_ERR ret;
1108 struct lyd_node *req_op, *rep_op = NULL;
1109 const struct ly_ctx *ctx = LYD_NODE_CTX(request);
Michal Vasko60ea6352020-06-29 13:39:39 +02001110
1111 /* find request OP */
Michal Vasko56daf732020-08-10 10:57:18 +02001112 LYD_TREE_DFS_BEGIN((struct lyd_node *)request, req_op) {
Michal Vasko60ea6352020-06-29 13:39:39 +02001113 if (req_op->schema->nodetype & (LYS_RPC | LYS_ACTION)) {
1114 break;
1115 }
Michal Vasko56daf732020-08-10 10:57:18 +02001116 LYD_TREE_DFS_END(request, req_op);
Michal Vasko60ea6352020-06-29 13:39:39 +02001117 }
1118 if (!(req_op->schema->nodetype & (LYS_RPC | LYS_ACTION))) {
1119 LOGERR(LYD_NODE_CTX(request), LY_EINVAL, "No RPC/action in the request found.");
Radek Krejci1798aae2020-07-14 13:26:06 +02001120 return LY_EINVAL;
Michal Vasko60ea6352020-06-29 13:39:39 +02001121 }
1122
1123 /* duplicate request OP with parents */
Radek Krejci1798aae2020-07-14 13:26:06 +02001124 LY_CHECK_RET(lyd_dup_single(req_op, NULL, LYD_DUP_WITH_PARENTS, &rep_op));
Michal Vasko60ea6352020-06-29 13:39:39 +02001125
Radek Krejci1798aae2020-07-14 13:26:06 +02001126 ret = lyd_parse_lyb_(ctx, (struct lyd_node_inner **)&rep_op, in, LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, LYD_INTOPT_REPLY, tree_p, op_p, NULL);
Michal Vasko60ea6352020-06-29 13:39:39 +02001127
Michal Vasko60ea6352020-06-29 13:39:39 +02001128 lyd_free_all(rep_op);
Michal Vasko60ea6352020-06-29 13:39:39 +02001129
Michal Vasko60ea6352020-06-29 13:39:39 +02001130 return ret;
1131}
1132
1133API int
1134lyd_lyb_data_length(const char *data)
1135{
1136 LY_ERR ret = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001137 struct lylyb_ctx *lybctx;
Michal Vasko60ea6352020-06-29 13:39:39 +02001138 int count, i;
1139 size_t len;
1140 uint8_t buf[LYB_SIZE_MAX];
1141
1142 if (!data) {
1143 return -1;
1144 }
1145
Radek Krejci1798aae2020-07-14 13:26:06 +02001146 lybctx = calloc(1, sizeof *lybctx);
1147 LY_CHECK_ERR_RET(!lybctx, LOGMEM(NULL), LY_EMEM);
1148 ret = ly_in_new_memory(data, &lybctx->in);
Michal Vasko63f3d842020-07-08 10:10:14 +02001149 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001150
1151 /* read magic number */
Radek Krejci1798aae2020-07-14 13:26:06 +02001152 ret = lyb_parse_magic_number(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001153 LY_CHECK_GOTO(ret, cleanup);
1154
1155 /* read header */
Radek Krejci1798aae2020-07-14 13:26:06 +02001156 ret = lyb_parse_header(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001157 LY_CHECK_GOTO(ret, cleanup);
1158
1159 /* read model count */
Radek Krejci1798aae2020-07-14 13:26:06 +02001160 lyb_read_number(&count, sizeof count, 2, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001161
1162 /* read all models */
1163 for (i = 0; i < count; ++i) {
1164 /* module name length */
1165 len = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +02001166 lyb_read_number(&len, sizeof len, 2, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001167
1168 /* model name */
Radek Krejci1798aae2020-07-14 13:26:06 +02001169 lyb_read(buf, len, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001170
1171 /* revision */
Radek Krejci1798aae2020-07-14 13:26:06 +02001172 lyb_read(buf, 2, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001173 }
1174
Radek Krejci1798aae2020-07-14 13:26:06 +02001175 while (lybctx->in->current[0]) {
Michal Vasko60ea6352020-06-29 13:39:39 +02001176 /* register a new subtree */
Radek Krejci1798aae2020-07-14 13:26:06 +02001177 ret = lyb_read_start_subtree(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001178 LY_CHECK_GOTO(ret, cleanup);
1179
1180 /* skip it */
Radek Krejci1798aae2020-07-14 13:26:06 +02001181 lyb_skip_subtree(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001182
1183 /* subtree finished */
Radek Krejci1798aae2020-07-14 13:26:06 +02001184 ret = lyb_read_stop_subtree(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001185 LY_CHECK_GOTO(ret, cleanup);
1186 }
1187
1188 /* read the last zero, parsing finished */
Radek Krejci1798aae2020-07-14 13:26:06 +02001189 ly_in_skip(lybctx->in, 1);
Michal Vasko60ea6352020-06-29 13:39:39 +02001190
1191cleanup:
Radek Krejci1798aae2020-07-14 13:26:06 +02001192 count = lybctx->in->current - lybctx->in->start;
Michal Vasko63f3d842020-07-08 10:10:14 +02001193
Radek Krejci1798aae2020-07-14 13:26:06 +02001194 ly_in_free(lybctx->in, 0);
1195 lylyb_ctx_free(lybctx);
1196
Michal Vasko63f3d842020-07-08 10:10:14 +02001197 return ret ? -1 : count;
Michal Vasko60ea6352020-06-29 13:39:39 +02001198}