blob: cb7caff2e3b28378885943baa185c88c33003442 [file] [log] [blame]
Michal Vasko60ea6352020-06-29 13:39:39 +02001/**
2 * @file parser_lyb.c
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @brief LYB data parser for libyang
5 *
6 * Copyright (c) 2020 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
15#include "lyb.h"
16
17#include <assert.h>
Radek Krejciad97c5f2020-06-30 09:19:28 +020018#include <stdint.h>
Michal Vasko60ea6352020-06-29 13:39:39 +020019#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22
23#include "common.h"
24#include "compat.h"
Radek Krejciad97c5f2020-06-30 09:19:28 +020025#include "context.h"
26#include "dict.h"
Radek Krejci47fab892020-11-05 17:02:41 +010027#include "hash_table.h"
28#include "in.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020029#include "in_internal.h"
Radek Krejciad97c5f2020-06-30 09:19:28 +020030#include "log.h"
Radek Krejci7931b192020-06-25 17:05:03 +020031#include "parser_data.h"
32#include "parser_internal.h"
Radek Krejciad97c5f2020-06-30 09:19:28 +020033#include "tree.h"
Radek Krejci47fab892020-11-05 17:02:41 +010034#include "tree_data.h"
Michal Vasko60ea6352020-06-29 13:39:39 +020035#include "tree_data_internal.h"
36#include "tree_schema.h"
37#include "validation.h"
Michal Vasko6b5cb2a2020-11-11 19:11:21 +010038#include "xml.h"
Michal Vasko60ea6352020-06-29 13:39:39 +020039
Radek Krejci1798aae2020-07-14 13:26:06 +020040void
41lylyb_ctx_free(struct lylyb_ctx *ctx)
42{
43 LY_ARRAY_COUNT_TYPE u;
44
45 LY_ARRAY_FREE(ctx->subtrees);
46 LY_ARRAY_FREE(ctx->models);
47
48 LY_ARRAY_FOR(ctx->sib_hts, u) {
49 lyht_free(ctx->sib_hts[u].ht);
50 }
51 LY_ARRAY_FREE(ctx->sib_hts);
52
53 free(ctx);
54}
55
56void
57lyd_lyb_ctx_free(struct lyd_ctx *lydctx)
58{
59 struct lyd_lyb_ctx *ctx = (struct lyd_lyb_ctx *)lydctx;
60
61 lyd_ctx_free(lydctx);
62 lylyb_ctx_free(ctx->lybctx);
63 free(ctx);
64}
65
Michal Vasko60ea6352020-06-29 13:39:39 +020066/**
67 * @brief Read YANG data from LYB input. Metadata are handled transparently and not returned.
68 *
69 * @param[in] buf Destination buffer.
70 * @param[in] count Number of bytes to read.
71 * @param[in] lybctx LYB context.
72 */
73static void
Radek Krejci1798aae2020-07-14 13:26:06 +020074lyb_read(uint8_t *buf, size_t count, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +020075{
Michal Vaskofd69e1d2020-07-03 11:57:17 +020076 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +020077 struct lyd_lyb_subtree *empty;
78 size_t to_read;
79 uint8_t meta_buf[LYB_META_BYTES];
80
81 assert(lybctx);
82
83 while (1) {
84 /* check for fully-read (empty) data chunks */
85 to_read = count;
86 empty = NULL;
87 LY_ARRAY_FOR(lybctx->subtrees, u) {
88 /* we want the innermost chunks resolved first, so replace previous empty chunks,
89 * also ignore chunks that are completely finished, there is nothing for us to do */
90 if ((lybctx->subtrees[u].written <= to_read) && lybctx->subtrees[u].position) {
91 /* empty chunk, do not read more */
92 to_read = lybctx->subtrees[u].written;
93 empty = &lybctx->subtrees[u];
94 }
95 }
96
97 if (!empty && !count) {
98 break;
99 }
100
101 /* we are actually reading some data, not just finishing another chunk */
102 if (to_read) {
103 if (buf) {
Michal Vasko63f3d842020-07-08 10:10:14 +0200104 ly_in_read(lybctx->in, buf, to_read);
105 } else {
106 ly_in_skip(lybctx->in, to_read);
Michal Vasko60ea6352020-06-29 13:39:39 +0200107 }
108
109 LY_ARRAY_FOR(lybctx->subtrees, u) {
110 /* decrease all written counters */
111 lybctx->subtrees[u].written -= to_read;
112 assert(lybctx->subtrees[u].written <= LYB_SIZE_MAX);
113 }
114 /* decrease count/buf */
115 count -= to_read;
116 if (buf) {
117 buf += to_read;
118 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200119 }
120
121 if (empty) {
122 /* read the next chunk meta information */
Michal Vasko63f3d842020-07-08 10:10:14 +0200123 ly_in_read(lybctx->in, meta_buf, LYB_META_BYTES);
Michal Vasko60ea6352020-06-29 13:39:39 +0200124 empty->written = meta_buf[0];
125 empty->inner_chunks = meta_buf[1];
126
127 /* remember whether there is a following chunk or not */
128 empty->position = (empty->written == LYB_SIZE_MAX ? 1 : 0);
Michal Vasko60ea6352020-06-29 13:39:39 +0200129 }
130 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200131}
132
133/**
134 * @brief Read a number.
135 *
136 * @param[in] num Destination buffer.
137 * @param[in] num_size Size of @p num.
138 * @param[in] bytes Number of bytes to read.
139 * @param[in] lybctx LYB context.
140 */
141static void
Radek Krejci1798aae2020-07-14 13:26:06 +0200142lyb_read_number(void *num, size_t num_size, size_t bytes, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200143{
144 uint64_t buf = 0;
145
146 lyb_read((uint8_t *)&buf, bytes, lybctx);
147
148 /* correct byte order */
149 buf = le64toh(buf);
150
151 switch (num_size) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100152 case sizeof(uint8_t):
Michal Vasko60ea6352020-06-29 13:39:39 +0200153 *((uint8_t *)num) = buf;
154 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100155 case sizeof(uint16_t):
Michal Vasko60ea6352020-06-29 13:39:39 +0200156 *((uint16_t *)num) = buf;
157 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100158 case sizeof(uint32_t):
Michal Vasko60ea6352020-06-29 13:39:39 +0200159 *((uint32_t *)num) = buf;
160 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100161 case sizeof(uint64_t):
Michal Vasko60ea6352020-06-29 13:39:39 +0200162 *((uint64_t *)num) = buf;
163 break;
164 default:
165 LOGINT(lybctx->ctx);
166 }
167}
168
169/**
170 * @brief Read a string.
171 *
172 * @param[in] str Destination buffer, is allocated.
173 * @param[in] with_length Whether the string is preceded with its length or it ends at the end of this subtree.
174 * @param[in] lybctx LYB context.
175 * @return LY_ERR value.
176 */
177static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200178lyb_read_string(char **str, ly_bool with_length, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200179{
Radek Krejci857189e2020-09-01 13:26:36 +0200180 ly_bool next_chunk = 0;
Michal Vasko60ea6352020-06-29 13:39:39 +0200181 size_t len = 0, cur_len;
182
183 *str = NULL;
184
185 if (with_length) {
186 lyb_read_number(&len, sizeof len, 2, lybctx);
187 } else {
188 /* read until the end of this subtree */
189 len = LYB_LAST_SUBTREE(lybctx).written;
190 if (LYB_LAST_SUBTREE(lybctx).position) {
191 next_chunk = 1;
192 }
193 }
194
195 *str = malloc((len + 1) * sizeof **str);
196 LY_CHECK_ERR_RET(!*str, LOGMEM(lybctx->ctx), LY_EMEM);
197
198 lyb_read((uint8_t *)*str, len, lybctx);
199
200 while (next_chunk) {
201 cur_len = LYB_LAST_SUBTREE(lybctx).written;
202 if (LYB_LAST_SUBTREE(lybctx).position) {
203 next_chunk = 1;
204 } else {
205 next_chunk = 0;
206 }
207
208 *str = ly_realloc(*str, (len + cur_len + 1) * sizeof **str);
209 LY_CHECK_ERR_RET(!*str, LOGMEM(lybctx->ctx), LY_EMEM);
210
211 lyb_read(((uint8_t *)*str) + len, cur_len, lybctx);
212
213 len += cur_len;
214 }
215
216 ((char *)*str)[len] = '\0';
217 return LY_SUCCESS;
218}
219
220/**
221 * @brief Stop the current subtree - change LYB context state.
222 *
223 * @param[in] lybctx LYB context.
224 * @return LY_ERR value.
225 */
226static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200227lyb_read_stop_subtree(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200228{
229 if (LYB_LAST_SUBTREE(lybctx).written) {
230 LOGINT_RET(lybctx->ctx);
231 }
232
233 LY_ARRAY_DECREMENT(lybctx->subtrees);
234 return LY_SUCCESS;
235}
236
237/**
238 * @brief Start a new subtree - change LYB context state but also read the expected metadata.
239 *
240 * @param[in] lybctx LYB context.
241 * @return LY_ERR value.
242 */
243static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200244lyb_read_start_subtree(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200245{
246 uint8_t meta_buf[LYB_META_BYTES];
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200247 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +0200248
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;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100281 char *mod_name = NULL, mod_rev[LY_REV_SIZE];
Michal Vasko60ea6352020-06-29 13:39:39 +0200282 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) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100298 sprintf(mod_rev, "%04u-%02u-%02u", ((rev & LYB_REV_YEAR_MASK) >> LYB_REV_YEAR_SHIFT) + LYB_REV_YEAR_OFFSET,
299 (rev & LYB_REV_MONTH_MASK) >> LYB_REV_MONTH_SHIFT, rev & LYB_REV_DAY_MASK);
Michal Vasko60ea6352020-06-29 13:39:39 +0200300 *mod = ly_ctx_get_module(lybctx->ctx, mod_name, mod_rev);
Radek Krejci1798aae2020-07-14 13:26:06 +0200301 if ((parse_options & LYD_PARSE_LYB_MOD_UPDATE) && !(*mod)) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200302 /* try to use an updated module */
303 *mod = ly_ctx_get_module_implemented(lybctx->ctx, mod_name);
304 if (*mod && (!(*mod)->revision || (strcmp((*mod)->revision, mod_rev) < 0))) {
305 /* not an implemented module in a newer revision */
306 *mod = NULL;
307 }
308 }
309 } else {
310 *mod = ly_ctx_get_module_latest(lybctx->ctx, mod_name);
311 }
312 /* TODO data_clb supported?
313 if (lybctx->ctx->data_clb) {
314 if (!*mod) {
315 *mod = lybctx->ctx->data_clb(lybctx->ctx, mod_name, NULL, 0, lybctx->ctx->data_clb_data);
316 } else if (!(*mod)->implemented) {
317 *mod = lybctx->ctx->data_clb(lybctx->ctx, mod_name, (*mod)->ns, LY_MODCLB_NOT_IMPLEMENTED, lybctx->ctx->data_clb_data);
318 }
319 }*/
320
321 if (!*mod || !(*mod)->implemented) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200322 if (parse_options & LYD_PARSE_STRICT) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200323 if (!*mod) {
324 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid context for LYB data parsing, missing module \"%s%s%s\".",
Michal Vasko69730152020-10-09 16:30:07 +0200325 mod_name, rev ? "@" : "", rev ? mod_rev : "");
Michal Vasko60ea6352020-06-29 13:39:39 +0200326 } else if (!(*mod)->implemented) {
327 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid context for LYB data parsing, module \"%s%s%s\" not implemented.",
Michal Vasko69730152020-10-09 16:30:07 +0200328 mod_name, rev ? "@" : "", rev ? mod_rev : "");
Michal Vasko60ea6352020-06-29 13:39:39 +0200329 }
330 ret = LY_EINVAL;
331 goto cleanup;
332 }
333
334 }
335
336cleanup:
337 free(mod_name);
338 return ret;
339}
340
341/**
342 * @brief Parse YANG node metadata.
343 *
344 * @param[in] lybctx LYB context.
Michal Vasko60ea6352020-06-29 13:39:39 +0200345 * @param[out] meta Parsed metadata.
346 * @return LY_ERR value.
347 */
348static LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200349lyb_parse_metadata(struct lyd_lyb_ctx *lybctx, struct lyd_meta **meta)
Michal Vasko60ea6352020-06-29 13:39:39 +0200350{
351 LY_ERR ret = LY_SUCCESS;
Radek Krejci857189e2020-09-01 13:26:36 +0200352 ly_bool dynamic;
Michal Vasko60ea6352020-06-29 13:39:39 +0200353 uint8_t i, count = 0;
Michal Vasko1e5d5612020-07-03 13:29:26 +0200354 char *meta_name = NULL, *meta_value;
Michal Vasko60ea6352020-06-29 13:39:39 +0200355 const struct lys_module *mod;
356
357 /* read number of attributes stored */
Radek Krejci1798aae2020-07-14 13:26:06 +0200358 lyb_read(&count, 1, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200359
360 /* read attributes */
361 for (i = 0; i < count; ++i) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200362 ret = lyb_read_start_subtree(lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200363 LY_CHECK_GOTO(ret, cleanup);
364
365 /* find model */
Radek Krejci1798aae2020-07-14 13:26:06 +0200366 ret = lyb_parse_model(lybctx->lybctx, lybctx->parse_options, &mod);
Michal Vasko60ea6352020-06-29 13:39:39 +0200367 LY_CHECK_GOTO(ret, cleanup);
368
369 if (!mod) {
370 /* skip it */
371 do {
Radek Krejci1798aae2020-07-14 13:26:06 +0200372 lyb_read(NULL, LYB_LAST_SUBTREE(lybctx->lybctx).written, lybctx->lybctx);
373 } while (LYB_LAST_SUBTREE(lybctx->lybctx).written);
Michal Vasko60ea6352020-06-29 13:39:39 +0200374 goto stop_subtree;
375 }
376
377 /* meta name */
Radek Krejci1798aae2020-07-14 13:26:06 +0200378 ret = lyb_read_string(&meta_name, 1, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200379 LY_CHECK_GOTO(ret, cleanup);
380
381 /* meta value */
Radek Krejci1798aae2020-07-14 13:26:06 +0200382 ret = lyb_read_string(&meta_value, 0, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200383 LY_CHECK_GOTO(ret, cleanup);
384 dynamic = 1;
385
386 /* create metadata */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200387 ret = lyd_parser_create_meta((struct lyd_ctx *)lybctx, NULL, meta, mod, meta_name, strlen(meta_name), meta_value,
Michal Vasko69730152020-10-09 16:30:07 +0200388 ly_strlen(meta_value), &dynamic, LY_PREF_JSON, NULL, LYD_HINT_DATA);
Michal Vasko60ea6352020-06-29 13:39:39 +0200389
390 /* free strings */
391 free(meta_name);
392 meta_name = NULL;
393 if (dynamic) {
394 free(meta_value);
395 dynamic = 0;
396 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200397
Radek Krejci1798aae2020-07-14 13:26:06 +0200398 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200399
400stop_subtree:
Radek Krejci1798aae2020-07-14 13:26:06 +0200401 ret = lyb_read_stop_subtree(lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200402 LY_CHECK_GOTO(ret, cleanup);
403 }
404
405cleanup:
406 free(meta_name);
Michal Vasko60ea6352020-06-29 13:39:39 +0200407 if (ret) {
Michal Vasko3a41dff2020-07-15 14:30:28 +0200408 lyd_free_meta_siblings(*meta);
Michal Vasko60ea6352020-06-29 13:39:39 +0200409 *meta = NULL;
410 }
411 return ret;
412}
413
414/**
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100415 * @brief Parse format-specific prefix data.
Michal Vasko60ea6352020-06-29 13:39:39 +0200416 *
417 * @param[in] lybctx LYB context.
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100418 * @param[in] format Prefix data format.
419 * @param[out] prefix_data Parsed prefix data.
Michal Vasko60ea6352020-06-29 13:39:39 +0200420 * @return LY_ERR value.
421 */
422static LY_ERR
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100423lyb_parse_prefix_data(struct lylyb_ctx *lybctx, LY_PREFIX_FORMAT format, void **prefix_data)
Michal Vasko60ea6352020-06-29 13:39:39 +0200424{
425 LY_ERR ret = LY_SUCCESS;
426 uint8_t count, i;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100427 struct ly_set *set = NULL;
428 struct lyxml_ns *ns = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200429
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100430 switch (format) {
431 case LY_PREF_XML:
432 /* read count */
433 lyb_read(&count, 1, lybctx);
434 if (!count) {
435 return LY_SUCCESS;
436 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200437
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100438 /* read all NS elements */
439 LY_CHECK_GOTO(ret = ly_set_new(&set), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200440
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100441 for (i = 0; i < count; ++i) {
442 ns = calloc(1, sizeof *ns);
Michal Vasko60ea6352020-06-29 13:39:39 +0200443
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100444 /* prefix */
445 LY_CHECK_GOTO(ret = lyb_read_string(&ns->prefix, 1, lybctx), cleanup);
446
447 /* namespace */
448 LY_CHECK_GOTO(ret = lyb_read_string(&ns->uri, 1, lybctx), cleanup);
449
450 LY_CHECK_GOTO(ret = ly_set_add(set, ns, 1, NULL), cleanup);
451 ns = NULL;
452 }
453
454 *prefix_data = set;
455 break;
456 case LY_PREF_JSON:
457 /* nothing stored */
458 break;
459 default:
460 LOGINT(lybctx->ctx);
461 ret = LY_EINT;
462 break;
Michal Vasko60ea6352020-06-29 13:39:39 +0200463 }
464
465cleanup:
466 if (ret) {
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100467 ly_free_prefix_data(format, set);
468 if (ns) {
469 free(ns->prefix);
470 free(ns->uri);
471 free(ns);
472 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200473 }
474 return ret;
475}
476
477/**
478 * @brief Parse opaque attributes.
479 *
480 * @param[in] lybctx LYB context.
481 * @param[out] attr Parsed attributes.
482 * @return LY_ERR value.
483 */
484static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200485lyb_parse_attributes(struct lylyb_ctx *lybctx, struct lyd_attr **attr)
Michal Vasko60ea6352020-06-29 13:39:39 +0200486{
487 LY_ERR ret = LY_SUCCESS;
488 uint8_t count, i;
Radek Krejci1798aae2020-07-14 13:26:06 +0200489 struct lyd_attr *attr2;
490 char *prefix = NULL, *module_name = NULL, *name = NULL, *value = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +0200491 ly_bool dynamic = 0;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100492 LY_PREFIX_FORMAT format = 0;
493 void *val_prefix_data = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200494
495 /* read count */
496 lyb_read(&count, 1, lybctx);
497
498 /* read attributes */
499 for (i = 0; i < count; ++i) {
500 ret = lyb_read_start_subtree(lybctx);
501 LY_CHECK_GOTO(ret, cleanup);
502
Michal Vasko0fdcd242020-11-11 19:12:30 +0100503 /* prefix, may be empty */
Michal Vasko60ea6352020-06-29 13:39:39 +0200504 ret = lyb_read_string(&prefix, 1, lybctx);
505 LY_CHECK_GOTO(ret, cleanup);
506 if (!prefix[0]) {
507 free(prefix);
508 prefix = NULL;
509 }
510
511 /* namespace, may be empty */
Radek Krejci1798aae2020-07-14 13:26:06 +0200512 ret = lyb_read_string(&module_name, 1, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200513 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200514 if (!module_name[0]) {
515 free(module_name);
516 module_name = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200517 }
518
519 /* name */
520 ret = lyb_read_string(&name, 1, lybctx);
521 LY_CHECK_GOTO(ret, cleanup);
522
Michal Vasko60ea6352020-06-29 13:39:39 +0200523 /* format */
524 lyb_read((uint8_t *)&format, 1, lybctx);
525
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100526 /* value prefixes */
527 ret = lyb_parse_prefix_data(lybctx, format, &val_prefix_data);
528 LY_CHECK_GOTO(ret, cleanup);
529
Michal Vasko60ea6352020-06-29 13:39:39 +0200530 /* value */
531 ret = lyb_read_string(&value, 0, lybctx);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100532 LY_CHECK_ERR_GOTO(ret, ly_free_prefix_data(format, val_prefix_data), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200533 dynamic = 1;
534
535 /* attr2 is always changed to the created attribute */
Michal Vasko501af032020-11-11 20:27:44 +0100536 ret = lyd_create_attr(NULL, &attr2, lybctx->ctx, name, strlen(name), prefix, ly_strlen(prefix), module_name,
537 ly_strlen(module_name), value, ly_strlen(value), &dynamic, format, val_prefix_data, 0);
Michal Vasko60ea6352020-06-29 13:39:39 +0200538 LY_CHECK_GOTO(ret, cleanup);
539
540 free(prefix);
541 prefix = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +0200542 free(module_name);
543 module_name = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200544 free(name);
545 name = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200546 assert(!dynamic);
547 value = NULL;
548
549 if (!*attr) {
550 *attr = attr2;
551 }
552
553 ret = lyb_read_stop_subtree(lybctx);
554 LY_CHECK_GOTO(ret, cleanup);
555 }
556
557cleanup:
558 free(prefix);
Radek Krejci1798aae2020-07-14 13:26:06 +0200559 free(module_name);
Michal Vasko60ea6352020-06-29 13:39:39 +0200560 free(name);
561 if (dynamic) {
562 free(value);
563 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200564 if (ret) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200565 lyd_free_attr_siblings(lybctx->ctx, *attr);
Michal Vasko60ea6352020-06-29 13:39:39 +0200566 *attr = NULL;
567 }
568 return ret;
569}
570
571/**
572 * @brief Check whether a schema node matches a hash(es).
573 *
574 * @param[in] sibling Schema node to check.
575 * @param[in] hash Hash array to check.
576 * @param[in] hash_count Number of hashes in @p hash.
577 * @return non-zero if matches,
578 * @return 0 if not.
579 */
580static int
581lyb_is_schema_hash_match(struct lysc_node *sibling, LYB_HASH *hash, uint8_t hash_count)
582{
583 LYB_HASH sibling_hash;
584 uint8_t i;
585
586 /* compare all the hashes starting from collision ID 0 */
587 for (i = 0; i < hash_count; ++i) {
588 sibling_hash = lyb_hash(sibling, i);
589 if (sibling_hash != hash[i]) {
590 return 0;
591 }
592 }
593
594 return 1;
595}
596
597/**
Michal Vasko60ea6352020-06-29 13:39:39 +0200598 * @brief Parse schema node hash.
599 *
600 * @param[in] lybctx LYB context.
601 * @param[in] sparent Schema parent, must be set if @p mod is not.
602 * @param[in] mod Module of the top-level node, must be set if @p sparent is not.
603 * @param[out] snode Parsed found schema node, may be NULL if opaque.
604 * @return LY_ERR value.
605 */
606static LY_ERR
607lyb_parse_schema_hash(struct lyd_lyb_ctx *lybctx, const struct lysc_node *sparent, const struct lys_module *mod,
Radek Krejci0f969882020-08-21 16:56:47 +0200608 const struct lysc_node **snode)
Michal Vasko60ea6352020-06-29 13:39:39 +0200609{
610 LY_ERR ret;
611 uint8_t i, j;
612 const struct lysc_node *sibling;
613 LYB_HASH hash[LYB_HASH_BITS - 1];
Radek Krejci1deb5be2020-08-26 16:43:36 +0200614 uint32_t getnext_opts;
Michal Vasko60ea6352020-06-29 13:39:39 +0200615
616 *snode = NULL;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100617 getnext_opts = lybctx->int_opts & LYD_INTOPT_REPLY ? LYS_GETNEXT_OUTPUT : 0;
Michal Vasko60ea6352020-06-29 13:39:39 +0200618
619 /* read the first hash */
Radek Krejci1798aae2020-07-14 13:26:06 +0200620 lyb_read(&hash[0], sizeof *hash, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200621
622 if (!hash[0]) {
623 /* opaque node */
624 return LY_SUCCESS;
625 }
626
627 /* based on the first hash read all the other ones, if any */
628 for (i = 0; !(hash[0] & (LYB_HASH_COLLISION_ID >> i)); ++i) {
629 if (i > LYB_HASH_BITS) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200630 LOGINT_RET(lybctx->lybctx->ctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200631 }
632 }
633
634 /* move the first hash on its accurate position */
635 hash[i] = hash[0];
636
637 /* read the rest of hashes */
638 for (j = i; j; --j) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200639 lyb_read(&hash[j - 1], sizeof *hash, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200640
641 /* correct collision ID */
642 assert(hash[j - 1] & (LYB_HASH_COLLISION_ID >> (j - 1)));
643 /* preceded with zeros */
644 assert(!(hash[j - 1] & (LYB_HASH_MASK << (LYB_HASH_BITS - (j - 1)))));
645 }
646
647 /* find our node with matching hashes */
648 sibling = NULL;
649 while ((sibling = lys_getnext(sibling, sparent, mod ? mod->compiled : NULL, getnext_opts))) {
650 /* skip schema nodes from models not present during printing */
Michal Vasko69730152020-10-09 16:30:07 +0200651 if (lyb_has_schema_model(sibling, lybctx->lybctx->models) &&
652 lyb_is_schema_hash_match((struct lysc_node *)sibling, hash, i + 1)) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200653 /* match found */
654 break;
655 }
656 }
657
Radek Krejci7931b192020-06-25 17:05:03 +0200658 if (!sibling && (lybctx->parse_options & LYD_PARSE_STRICT)) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200659 if (mod) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200660 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 +0200661 " from \"%s\".", mod->name);
Michal Vasko60ea6352020-06-29 13:39:39 +0200662 } else {
Radek Krejci1798aae2020-07-14 13:26:06 +0200663 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 +0200664 " of \"%s\".", sparent->name);
Michal Vasko60ea6352020-06-29 13:39:39 +0200665 }
666 return LY_EVALID;
Radek Krejci1798aae2020-07-14 13:26:06 +0200667 } else if (sibling && (ret = lyd_parser_check_schema((struct lyd_ctx *)lybctx, sibling))) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200668 return ret;
669 }
670
671 *snode = sibling;
672 return LY_SUCCESS;
673}
674
675/**
676 * @brief Read until the end of the current subtree.
677 *
678 * @param[in] lybctx LYB context.
679 */
680static void
Radek Krejci1798aae2020-07-14 13:26:06 +0200681lyb_skip_subtree(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200682{
Michal Vasko60ea6352020-06-29 13:39:39 +0200683 do {
684 /* first skip any meta information inside */
Michal Vasko63f3d842020-07-08 10:10:14 +0200685 ly_in_skip(lybctx->in, LYB_LAST_SUBTREE(lybctx).inner_chunks * LYB_META_BYTES);
Michal Vasko60ea6352020-06-29 13:39:39 +0200686
687 /* then read data */
688 lyb_read(NULL, LYB_LAST_SUBTREE(lybctx).written, lybctx);
689 } while (LYB_LAST_SUBTREE(lybctx).written);
690}
691
692/**
693 * @brief Parse LYB subtree.
694 *
695 * @param[in] lybctx LYB context.
696 * @param[in] parent Data parent of the subtree, must be set if @p first is not.
697 * @param[in,out] first First top-level sibling, must be set if @p parent is not.
698 * @return LY_ERR value.
699 */
700static LY_ERR
701lyb_parse_subtree_r(struct lyd_lyb_ctx *lybctx, struct lyd_node_inner *parent, struct lyd_node **first)
702{
703 LY_ERR ret = LY_SUCCESS;
704 struct lyd_node *node = NULL, *tree;
705 const struct lys_module *mod;
706 const struct lysc_node *snode = NULL;
707 struct lyd_meta *meta = NULL, *m;
Radek Krejci1798aae2020-07-14 13:26:06 +0200708 struct lyd_attr *attr = NULL, *a;
Michal Vasko60ea6352020-06-29 13:39:39 +0200709 LYD_ANYDATA_VALUETYPE value_type;
Radek Krejci1798aae2020-07-14 13:26:06 +0200710 char *value = NULL, *name = NULL, *prefix = NULL, *module_key = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +0200711 ly_bool dynamic = 0;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100712 LY_PREFIX_FORMAT format = 0;
713 void *val_prefix_data = NULL;
Michal Vaskoc5e866a2020-11-04 17:09:26 +0100714 uint32_t prev_lo, flags;
Radek Krejci1798aae2020-07-14 13:26:06 +0200715 const struct ly_ctx *ctx = lybctx->lybctx->ctx;
Michal Vasko60ea6352020-06-29 13:39:39 +0200716
717 /* register a new subtree */
Radek Krejci1798aae2020-07-14 13:26:06 +0200718 LY_CHECK_GOTO(ret = lyb_read_start_subtree(lybctx->lybctx), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200719
720 if (!parent) {
721 /* top-level, read module name */
Radek Krejci1798aae2020-07-14 13:26:06 +0200722 ret = lyb_parse_model(lybctx->lybctx, lybctx->parse_options, &mod);
Michal Vasko60ea6352020-06-29 13:39:39 +0200723 LY_CHECK_GOTO(ret, cleanup);
724
725 /* read hash, find the schema node starting from mod */
726 ret = lyb_parse_schema_hash(lybctx, NULL, mod, &snode);
727 LY_CHECK_GOTO(ret, cleanup);
728 } else {
729 /* read hash, find the schema node starting from parent schema */
730 ret = lyb_parse_schema_hash(lybctx, parent->schema, NULL, &snode);
731 LY_CHECK_GOTO(ret, cleanup);
732 }
733
Radek Krejci0f969882020-08-21 16:56:47 +0200734 if (!snode && !(lybctx->parse_options & LYD_PARSE_OPAQ)) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200735 /* unknown data, skip them */
Radek Krejci1798aae2020-07-14 13:26:06 +0200736 lyb_skip_subtree(lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200737 goto stop_subtree;
Radek Krejci0f969882020-08-21 16:56:47 +0200738 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200739
740 /* create metadata/attributes */
741 if (snode) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200742 ret = lyb_parse_metadata(lybctx, &meta);
Michal Vasko60ea6352020-06-29 13:39:39 +0200743 LY_CHECK_GOTO(ret, cleanup);
744 } else {
Radek Krejci1798aae2020-07-14 13:26:06 +0200745 ret = lyb_parse_attributes(lybctx->lybctx, &attr);
Michal Vasko60ea6352020-06-29 13:39:39 +0200746 LY_CHECK_GOTO(ret, cleanup);
747 }
748
Michal Vaskoc5e866a2020-11-04 17:09:26 +0100749 /* read flags */
750 lyb_read_number(&flags, sizeof flags, sizeof flags, lybctx->lybctx);
751
Michal Vasko60ea6352020-06-29 13:39:39 +0200752 if (!snode) {
753 /* parse prefix */
Radek Krejci1798aae2020-07-14 13:26:06 +0200754 ret = lyb_read_string(&prefix, 1, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200755 LY_CHECK_GOTO(ret, cleanup);
756
Radek Krejci1798aae2020-07-14 13:26:06 +0200757 /* parse module key */
758 ret = lyb_read_string(&module_key, 1, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200759 LY_CHECK_GOTO(ret, cleanup);
760
761 /* parse name */
Radek Krejci1798aae2020-07-14 13:26:06 +0200762 ret = lyb_read_string(&name, 1, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200763 LY_CHECK_GOTO(ret, cleanup);
764
Michal Vasko60ea6352020-06-29 13:39:39 +0200765 /* parse format */
Radek Krejci1798aae2020-07-14 13:26:06 +0200766 lyb_read((uint8_t *)&format, 1, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200767
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100768 /* parse value prefixes */
769 ret = lyb_parse_prefix_data(lybctx->lybctx, format, &val_prefix_data);
770 LY_CHECK_GOTO(ret, cleanup);
771
Michal Vasko60ea6352020-06-29 13:39:39 +0200772 /* parse value */
Radek Krejci1798aae2020-07-14 13:26:06 +0200773 ret = lyb_read_string(&value, 0, lybctx->lybctx);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100774 LY_CHECK_ERR_GOTO(ret, ly_free_prefix_data(format, val_prefix_data), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200775 dynamic = 1;
776
777 /* create node */
Michal Vasko501af032020-11-11 20:27:44 +0100778 ret = lyd_create_opaq(ctx, name, strlen(name), prefix, ly_strlen(prefix), module_key, ly_strlen(module_key),
779 value, strlen(value), &dynamic, format, val_prefix_data, 0, &node);
Michal Vasko60ea6352020-06-29 13:39:39 +0200780 LY_CHECK_GOTO(ret, cleanup);
781
782 /* process children */
Radek Krejci1798aae2020-07-14 13:26:06 +0200783 while (LYB_LAST_SUBTREE(lybctx->lybctx).written) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200784 ret = lyb_parse_subtree_r(lybctx, (struct lyd_node_inner *)node, NULL);
785 LY_CHECK_GOTO(ret, cleanup);
786 }
787 } else if (snode->nodetype & LYD_NODE_TERM) {
788 /* parse value */
Radek Krejci1798aae2020-07-14 13:26:06 +0200789 ret = lyb_read_string(&value, 0, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200790 LY_CHECK_GOTO(ret, cleanup);
791 dynamic = 1;
792
793 /* create node */
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200794 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 +0200795 NULL, LYD_HINT_DATA, &node);
Michal Vasko60ea6352020-06-29 13:39:39 +0200796 if (dynamic) {
797 free(value);
798 dynamic = 0;
799 }
800 value = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +0200801 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200802 } else if (snode->nodetype & LYD_NODE_INNER) {
803 /* create node */
804 ret = lyd_create_inner(snode, &node);
805 LY_CHECK_GOTO(ret, cleanup);
806
807 /* process children */
Radek Krejci1798aae2020-07-14 13:26:06 +0200808 while (LYB_LAST_SUBTREE(lybctx->lybctx).written) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200809 ret = lyb_parse_subtree_r(lybctx, (struct lyd_node_inner *)node, NULL);
810 LY_CHECK_GOTO(ret, cleanup);
811 }
812
Radek Krejci7931b192020-06-25 17:05:03 +0200813 if (!(lybctx->parse_options & LYD_PARSE_ONLY)) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200814 /* new node validation, autodelete CANNOT occur, all nodes are new */
Michal Vasko8104fd42020-07-13 11:09:51 +0200815 ret = lyd_validate_new(lyd_node_children_p(node), snode, NULL, NULL);
Michal Vasko60ea6352020-06-29 13:39:39 +0200816 LY_CHECK_GOTO(ret, cleanup);
817
818 /* add any missing default children */
Michal Vaskoa6669ba2020-08-06 16:14:26 +0200819 ret = lyd_new_implicit_r(node, lyd_node_children_p(node), NULL, NULL, &lybctx->unres_node_type,
Michal Vasko69730152020-10-09 16:30:07 +0200820 &lybctx->when_check, (lybctx->validate_options & LYD_VALIDATE_NO_STATE) ?
821 LYD_IMPLICIT_NO_STATE : 0, NULL);
Michal Vasko60ea6352020-06-29 13:39:39 +0200822 LY_CHECK_GOTO(ret, cleanup);
823 }
824
Michal Vasko751cb4d2020-07-14 12:25:28 +0200825 if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200826 /* rememeber the RPC/action/notification */
Radek Krejci1798aae2020-07-14 13:26:06 +0200827 lybctx->op_node = node;
Michal Vasko60ea6352020-06-29 13:39:39 +0200828 }
829 } else if (snode->nodetype & LYD_NODE_ANY) {
830 /* parse value type */
Radek Krejci1798aae2020-07-14 13:26:06 +0200831 lyb_read((uint8_t *)&value_type, sizeof value_type, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200832 if (value_type == LYD_ANYDATA_DATATREE) {
833 /* invalid situation */
Radek Krejci1798aae2020-07-14 13:26:06 +0200834 LOGINT(ctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200835 goto cleanup;
836 }
837
838 /* read anydata content */
Radek Krejci1798aae2020-07-14 13:26:06 +0200839 ret = lyb_read_string(&value, 0, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200840 LY_CHECK_GOTO(ret, cleanup);
841 dynamic = 1;
842
843 if (value_type == LYD_ANYDATA_LYB) {
844 /* turn logging off */
845 prev_lo = ly_log_options(0);
846
847 /* try to parse LYB into a data tree */
Radek Krejci1798aae2020-07-14 13:26:06 +0200848 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 +0200849 /* successfully parsed */
850 free(value);
851 value = (char *)tree;
852 value_type = LYD_ANYDATA_DATATREE;
853 }
Radek Krejci7931b192020-06-25 17:05:03 +0200854
855 /* turn logging on again */
856 ly_log_options(prev_lo);
Michal Vasko60ea6352020-06-29 13:39:39 +0200857 }
858
859 /* create node */
860 ret = lyd_create_any(snode, value, value_type, &node);
861 LY_CHECK_GOTO(ret, cleanup);
862
863 dynamic = 0;
864 value = NULL;
865 }
866 assert(node);
867
Michal Vaskoc5e866a2020-11-04 17:09:26 +0100868 /* set flags */
869 node->flags = flags;
Michal Vasko60ea6352020-06-29 13:39:39 +0200870
871 /* add metadata/attributes */
872 if (snode) {
873 LY_LIST_FOR(meta, m) {
874 m->parent = node;
875 }
876 node->meta = meta;
877 meta = NULL;
878 } else {
879 assert(!node->schema);
880 LY_LIST_FOR(attr, a) {
881 a->parent = (struct lyd_node_opaq *)node;
882 }
883 ((struct lyd_node_opaq *)node)->attr = attr;
884 attr = NULL;
885 }
886
Michal Vaskob104f112020-07-17 09:54:54 +0200887 /* insert, keep first pointer correct */
Michal Vasko60ea6352020-06-29 13:39:39 +0200888 lyd_insert_node((struct lyd_node *)parent, first, node);
Michal Vaskob104f112020-07-17 09:54:54 +0200889 while (!parent && (*first)->prev->next) {
890 *first = (*first)->prev;
891 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200892 node = NULL;
893
894stop_subtree:
895 /* end the subtree */
Radek Krejci1798aae2020-07-14 13:26:06 +0200896 ret = lyb_read_stop_subtree(lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200897 LY_CHECK_GOTO(ret, cleanup);
898
899cleanup:
900 free(prefix);
Radek Krejci1798aae2020-07-14 13:26:06 +0200901 free(module_key);
Michal Vasko60ea6352020-06-29 13:39:39 +0200902 free(name);
903 if (dynamic) {
904 free(value);
905 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200906
Michal Vasko3a41dff2020-07-15 14:30:28 +0200907 lyd_free_meta_siblings(meta);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200908 lyd_free_attr_siblings(ctx, attr);
Michal Vasko60ea6352020-06-29 13:39:39 +0200909 lyd_free_tree(node);
910 return ret;
911}
912
913/**
914 * @brief Parse used YANG data models.
915 *
916 * @param[in] lybctx LYB context.
917 * @return LY_ERR value.
918 */
919static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200920lyb_parse_data_models(struct lylyb_ctx *lybctx, uint32_t parse_options)
Michal Vasko60ea6352020-06-29 13:39:39 +0200921{
922 LY_ERR ret;
923 uint32_t count;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200924 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +0200925
926 /* read model count */
927 lyb_read_number(&count, sizeof count, 2, lybctx);
928
929 if (count) {
930 LY_ARRAY_CREATE_RET(lybctx->ctx, lybctx->models, count, LY_EMEM);
931
932 /* read modules */
933 for (u = 0; u < count; ++u) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200934 ret = lyb_parse_model(lybctx, parse_options, &lybctx->models[u]);
Michal Vasko60ea6352020-06-29 13:39:39 +0200935 LY_CHECK_RET(ret);
936 LY_ARRAY_INCREMENT(lybctx->models);
937 }
938 }
939
940 return LY_SUCCESS;
941}
942
943/**
944 * @brief Parse LYB magic number.
945 *
946 * @param[in] lybctx LYB context.
947 * @return LY_ERR value.
948 */
949static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200950lyb_parse_magic_number(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200951{
952 char magic_byte = 0;
953
954 lyb_read((uint8_t *)&magic_byte, 1, lybctx);
955 if (magic_byte != 'l') {
956 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid first magic number byte \"0x%02x\".", magic_byte);
957 return LY_EINVAL;
958 }
959
960 lyb_read((uint8_t *)&magic_byte, 1, lybctx);
961 if (magic_byte != 'y') {
962 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid second magic number byte \"0x%02x\".", magic_byte);
963 return LY_EINVAL;
964 }
965
966 lyb_read((uint8_t *)&magic_byte, 1, lybctx);
967 if (magic_byte != 'b') {
968 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid third magic number byte \"0x%02x\".", magic_byte);
969 return LY_EINVAL;
970 }
971
972 return LY_SUCCESS;
973}
974
975/**
976 * @brief Parse LYB header.
977 *
978 * @param[in] lybctx LYB context.
979 * @return LY_ERR value.
980 */
981static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200982lyb_parse_header(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200983{
984 uint8_t byte = 0;
985
986 /* version, future flags */
987 lyb_read((uint8_t *)&byte, sizeof byte, lybctx);
988
989 if ((byte & LYB_VERSION_MASK) != LYB_VERSION_NUM) {
990 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid LYB format version \"0x%02x\", expected \"0x%02x\".",
Michal Vasko69730152020-10-09 16:30:07 +0200991 byte & LYB_VERSION_MASK, LYB_VERSION_NUM);
Michal Vasko60ea6352020-06-29 13:39:39 +0200992 return LY_EINVAL;
993 }
994
995 return LY_SUCCESS;
996}
997
Michal Vaskoee38a5d2020-11-09 21:02:18 +0100998/**
Radek Krejci1798aae2020-07-14 13:26:06 +0200999 * @param[in] ctx libyang context for logging
Michal Vaskoee38a5d2020-11-09 21:02:18 +01001000 * @param[in] parent Parent node where to connect the parsed data, required for reply where the reply data are connected
1001 * with the request operation
Radek Krejci1798aae2020-07-14 13:26:06 +02001002 * @param[in] in Input structure.
1003 * @param[in] parse_options Options for parser, see @ref dataparseroptions.
1004 * @param[in] validate_options Options for the validation phase, see @ref datavalidationoptions.
1005 * @param[in] data_type Internal data parser flag to distnguish type of the data to parse (RPC/Reply/Notification/regular data].
1006 * @param[out] tree_p Parsed data tree. Note that NULL can be a valid result.
1007 * @param[out] op_p Optional pointer to the actual operation. Useful for action and inner notifications.
1008 * @param[out] lydctx_p Data parser context to finish validation.
1009 */
1010static LY_ERR
Michal Vaskoee38a5d2020-11-09 21:02:18 +01001011lyd_parse_lyb_(const struct ly_ctx *ctx, struct lyd_node_inner **parent, struct ly_in *in, uint32_t parse_options,
1012 uint32_t validate_options, uint32_t data_type, struct lyd_node **tree_p, struct lyd_node **op_p,
1013 struct lyd_ctx **lydctx_p)
Michal Vasko60ea6352020-06-29 13:39:39 +02001014{
1015 LY_ERR ret = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001016 struct lyd_lyb_ctx *lybctx;
1017 struct lyd_node *tree = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +02001018
Radek Krejci7931b192020-06-25 17:05:03 +02001019 assert(!(parse_options & ~LYD_PARSE_OPTS_MASK));
1020 assert(!(validate_options & ~LYD_VALIDATE_OPTS_MASK));
1021
Radek Krejci1798aae2020-07-14 13:26:06 +02001022 lybctx = calloc(1, sizeof *lybctx);
1023 LY_CHECK_ERR_RET(!lybctx, LOGMEM(ctx), LY_EMEM);
1024 lybctx->lybctx = calloc(1, sizeof *lybctx->lybctx);
Michal Vasko9acaf492020-08-13 09:05:58 +02001025 LY_CHECK_ERR_GOTO(!lybctx->lybctx, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001026
Radek Krejci1798aae2020-07-14 13:26:06 +02001027 lybctx->lybctx->in = in;
1028 lybctx->lybctx->ctx = ctx;
1029 lybctx->parse_options = parse_options;
1030 lybctx->validate_options = validate_options;
1031 lybctx->int_opts = data_type;
1032 lybctx->free = lyd_lyb_ctx_free;
Michal Vasko60ea6352020-06-29 13:39:39 +02001033
1034 /* read magic number */
Radek Krejci1798aae2020-07-14 13:26:06 +02001035 ret = lyb_parse_magic_number(lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001036 LY_CHECK_GOTO(ret, cleanup);
1037
1038 /* read header */
Radek Krejci1798aae2020-07-14 13:26:06 +02001039 ret = lyb_parse_header(lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001040 LY_CHECK_GOTO(ret, cleanup);
1041
1042 /* read used models */
Radek Krejci1798aae2020-07-14 13:26:06 +02001043 ret = lyb_parse_data_models(lybctx->lybctx, lybctx->parse_options);
Michal Vasko60ea6352020-06-29 13:39:39 +02001044 LY_CHECK_GOTO(ret, cleanup);
1045
1046 /* read subtree(s) */
Radek Krejci1798aae2020-07-14 13:26:06 +02001047 while (lybctx->lybctx->in->current[0]) {
1048 ret = lyb_parse_subtree_r(lybctx, parent ? *parent : NULL, &tree);
Michal Vasko60ea6352020-06-29 13:39:39 +02001049 LY_CHECK_GOTO(ret, cleanup);
1050 }
1051
1052 /* read the last zero, parsing finished */
Radek Krejci1798aae2020-07-14 13:26:06 +02001053 ly_in_skip(lybctx->lybctx->in, 1);
Michal Vasko60ea6352020-06-29 13:39:39 +02001054
Radek Krejci1798aae2020-07-14 13:26:06 +02001055 if (data_type == LYD_INTOPT_RPC) {
1056 /* make sure we have parsed some operation */
1057 if (!lybctx->op_node) {
1058 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_DATA, "Missing the \"rpc\"/\"action\" node.");
1059 ret = LY_EVALID;
1060 goto cleanup;
1061 }
1062
1063 if (op_p) {
1064 *op_p = lybctx->op_node;
1065 }
1066 assert(tree);
1067 } else if (data_type == LYD_INTOPT_REPLY) {
1068 struct lyd_node_inner *iter;
1069
1070 assert(parent);
1071
1072 if (op_p) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001073 *op_p = (struct lyd_node *)(*parent);
Radek Krejci1798aae2020-07-14 13:26:06 +02001074 }
Radek Krejci1e008d22020-08-17 11:37:37 +02001075 for (iter = *parent; iter->parent; iter = iter->parent) {}
Radek Krejci1798aae2020-07-14 13:26:06 +02001076 tree = (struct lyd_node *)iter;
1077 *parent = NULL;
1078
1079 } else if (data_type == LYD_INTOPT_NOTIF) {
1080 /* make sure we have parsed some notification */
1081 if (!lybctx->op_node) {
1082 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_DATA, "Missing the \"notification\" node.");
1083 ret = LY_EVALID;
1084 goto cleanup;
1085 }
1086
1087 if (op_p) {
1088 *op_p = lybctx->op_node;
1089 }
1090 assert(tree);
1091 }
Michal Vasko60ea6352020-06-29 13:39:39 +02001092
1093cleanup:
Radek Krejci1798aae2020-07-14 13:26:06 +02001094 if (ret || !lydctx_p) {
1095 lyd_lyb_ctx_free((struct lyd_ctx *)lybctx);
1096 if (ret) {
1097 lyd_free_all(tree);
1098 }
1099 } else {
1100 *lydctx_p = (struct lyd_ctx *)lybctx;
1101 if (tree_p) {
1102 *tree_p = tree;
1103 }
Michal Vasko60ea6352020-06-29 13:39:39 +02001104 }
1105 return ret;
1106}
1107
1108LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001109lyd_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 +02001110 struct lyd_node **tree_p, struct lyd_ctx **lydctx_p)
Michal Vasko60ea6352020-06-29 13:39:39 +02001111{
Radek Krejci1798aae2020-07-14 13:26:06 +02001112 return lyd_parse_lyb_(ctx, NULL, in, parse_options, validate_options, 0, tree_p, NULL, lydctx_p);
Michal Vasko60ea6352020-06-29 13:39:39 +02001113}
1114
1115LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +02001116lyd_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 +02001117{
Radek Krejci1798aae2020-07-14 13:26:06 +02001118 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 +02001119}
1120
1121LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +02001122lyd_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 +02001123{
Radek Krejci1798aae2020-07-14 13:26:06 +02001124 return lyd_parse_lyb_(ctx, NULL, in, LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, LYD_INTOPT_NOTIF, tree_p, ntf_p, NULL);
1125}
Michal Vasko60ea6352020-06-29 13:39:39 +02001126
Radek Krejci1798aae2020-07-14 13:26:06 +02001127LY_ERR
1128lyd_parse_lyb_reply(const struct lyd_node *request, struct ly_in *in, struct lyd_node **tree_p, struct lyd_node **op_p)
1129{
1130 LY_ERR ret;
1131 struct lyd_node *req_op, *rep_op = NULL;
Michal Vaskob7be7a82020-08-20 09:09:04 +02001132 const struct ly_ctx *ctx = LYD_CTX(request);
Michal Vasko60ea6352020-06-29 13:39:39 +02001133
1134 /* find request OP */
Michal Vasko56daf732020-08-10 10:57:18 +02001135 LYD_TREE_DFS_BEGIN((struct lyd_node *)request, req_op) {
Michal Vasko60ea6352020-06-29 13:39:39 +02001136 if (req_op->schema->nodetype & (LYS_RPC | LYS_ACTION)) {
1137 break;
1138 }
Michal Vasko56daf732020-08-10 10:57:18 +02001139 LYD_TREE_DFS_END(request, req_op);
Michal Vasko60ea6352020-06-29 13:39:39 +02001140 }
1141 if (!(req_op->schema->nodetype & (LYS_RPC | LYS_ACTION))) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001142 LOGERR(LYD_CTX(request), LY_EINVAL, "No RPC/action in the request found.");
Radek Krejci1798aae2020-07-14 13:26:06 +02001143 return LY_EINVAL;
Michal Vasko60ea6352020-06-29 13:39:39 +02001144 }
1145
1146 /* duplicate request OP with parents */
Radek Krejci1798aae2020-07-14 13:26:06 +02001147 LY_CHECK_RET(lyd_dup_single(req_op, NULL, LYD_DUP_WITH_PARENTS, &rep_op));
Michal Vasko60ea6352020-06-29 13:39:39 +02001148
Michal Vaskoee38a5d2020-11-09 21:02:18 +01001149 ret = lyd_parse_lyb_(ctx, (struct lyd_node_inner **)&rep_op, in, LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0,
1150 LYD_INTOPT_REPLY, tree_p, op_p, NULL);
Michal Vasko60ea6352020-06-29 13:39:39 +02001151
Michal Vasko60ea6352020-06-29 13:39:39 +02001152 lyd_free_all(rep_op);
Michal Vasko60ea6352020-06-29 13:39:39 +02001153
Michal Vasko60ea6352020-06-29 13:39:39 +02001154 return ret;
1155}
1156
1157API int
1158lyd_lyb_data_length(const char *data)
1159{
1160 LY_ERR ret = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001161 struct lylyb_ctx *lybctx;
Michal Vasko60ea6352020-06-29 13:39:39 +02001162 int count, i;
1163 size_t len;
1164 uint8_t buf[LYB_SIZE_MAX];
1165
1166 if (!data) {
1167 return -1;
1168 }
1169
Radek Krejci1798aae2020-07-14 13:26:06 +02001170 lybctx = calloc(1, sizeof *lybctx);
1171 LY_CHECK_ERR_RET(!lybctx, LOGMEM(NULL), LY_EMEM);
1172 ret = ly_in_new_memory(data, &lybctx->in);
Michal Vasko63f3d842020-07-08 10:10:14 +02001173 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001174
1175 /* read magic number */
Radek Krejci1798aae2020-07-14 13:26:06 +02001176 ret = lyb_parse_magic_number(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001177 LY_CHECK_GOTO(ret, cleanup);
1178
1179 /* read header */
Radek Krejci1798aae2020-07-14 13:26:06 +02001180 ret = lyb_parse_header(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001181 LY_CHECK_GOTO(ret, cleanup);
1182
1183 /* read model count */
Radek Krejci1798aae2020-07-14 13:26:06 +02001184 lyb_read_number(&count, sizeof count, 2, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001185
1186 /* read all models */
1187 for (i = 0; i < count; ++i) {
1188 /* module name length */
1189 len = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +02001190 lyb_read_number(&len, sizeof len, 2, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001191
1192 /* model name */
Radek Krejci1798aae2020-07-14 13:26:06 +02001193 lyb_read(buf, len, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001194
1195 /* revision */
Radek Krejci1798aae2020-07-14 13:26:06 +02001196 lyb_read(buf, 2, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001197 }
1198
Radek Krejci1798aae2020-07-14 13:26:06 +02001199 while (lybctx->in->current[0]) {
Michal Vasko60ea6352020-06-29 13:39:39 +02001200 /* register a new subtree */
Radek Krejci1798aae2020-07-14 13:26:06 +02001201 ret = lyb_read_start_subtree(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001202 LY_CHECK_GOTO(ret, cleanup);
1203
1204 /* skip it */
Radek Krejci1798aae2020-07-14 13:26:06 +02001205 lyb_skip_subtree(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001206
1207 /* subtree finished */
Radek Krejci1798aae2020-07-14 13:26:06 +02001208 ret = lyb_read_stop_subtree(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001209 LY_CHECK_GOTO(ret, cleanup);
1210 }
1211
1212 /* read the last zero, parsing finished */
Radek Krejci1798aae2020-07-14 13:26:06 +02001213 ly_in_skip(lybctx->in, 1);
Michal Vasko60ea6352020-06-29 13:39:39 +02001214
1215cleanup:
Radek Krejci1798aae2020-07-14 13:26:06 +02001216 count = lybctx->in->current - lybctx->in->start;
Michal Vasko63f3d842020-07-08 10:10:14 +02001217
Radek Krejci1798aae2020-07-14 13:26:06 +02001218 ly_in_free(lybctx->in, 0);
1219 lylyb_ctx_free(lybctx);
1220
Michal Vasko63f3d842020-07-08 10:10:14 +02001221 return ret ? -1 : count;
Michal Vasko60ea6352020-06-29 13:39:39 +02001222}