blob: de6ae97dc6d64f6b6d41ef90d0aa5debc6597c54 [file] [log] [blame]
Michal Vasko60ea6352020-06-29 13:39:39 +02001/**
2 * @file printer_lyb.c
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @brief LYB printer for libyang data structure
5 *
Michal Vasko78041d12022-11-29 14:11:07 +01006 * Copyright (c) 2020 - 2022 CESNET, z.s.p.o.
Michal Vasko60ea6352020-06-29 13:39:39 +02007 *
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>
Michal Vasko60ea6352020-06-29 13:39:39 +020018#include <stdint.h>
Michal Vasko69730152020-10-09 16:30:07 +020019#include <stdio.h>
Michal Vasko60ea6352020-06-29 13:39:39 +020020#include <stdlib.h>
21#include <string.h>
22
Michal Vasko60ea6352020-06-29 13:39:39 +020023#include "compat.h"
Radek Krejciad97c5f2020-06-30 09:19:28 +020024#include "context.h"
25#include "hash_table.h"
Michal Vasko60ea6352020-06-29 13:39:39 +020026#include "log.h"
Michal Vasko8f702ee2024-02-20 15:44:24 +010027#include "ly_common.h"
Radek Krejci47fab892020-11-05 17:02:41 +010028#include "out.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020029#include "out_internal.h"
Michal Vaskob4750962022-10-06 15:33:35 +020030#include "plugins_exts/metadata.h"
Radek Krejciad97c5f2020-06-30 09:19:28 +020031#include "printer_data.h"
Michal Vasko60ea6352020-06-29 13:39:39 +020032#include "printer_internal.h"
Radek Krejciad97c5f2020-06-30 09:19:28 +020033#include "set.h"
34#include "tree.h"
Radek Krejci47fab892020-11-05 17:02:41 +010035#include "tree_data.h"
Michal Vasko60ea6352020-06-29 13:39:39 +020036#include "tree_data_internal.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010037#include "tree_edit.h"
Michal Vasko60ea6352020-06-29 13:39:39 +020038#include "tree_schema.h"
39#include "tree_schema_internal.h"
Michal Vasko6b5cb2a2020-11-11 19:11:21 +010040#include "xml.h"
Michal Vasko60ea6352020-06-29 13:39:39 +020041
aPiecek570d7ed2021-09-10 07:15:35 +020042static LY_ERR lyb_print_siblings(struct ly_out *out, const struct lyd_node *node, struct lyd_lyb_ctx *lybctx);
aPiecek270f72b2021-09-09 11:39:31 +020043
Michal Vasko60ea6352020-06-29 13:39:39 +020044/**
45 * @brief Hash table equal callback for checking hash equality only.
Radek Krejci857189e2020-09-01 13:26:36 +020046 *
Michal Vasko62524a92021-02-26 10:08:50 +010047 * Implementation of ::lyht_value_equal_cb.
Michal Vasko60ea6352020-06-29 13:39:39 +020048 */
Radek Krejci857189e2020-09-01 13:26:36 +020049static ly_bool
50lyb_hash_equal_cb(void *UNUSED(val1_p), void *UNUSED(val2_p), ly_bool UNUSED(mod), void *UNUSED(cb_data))
Michal Vasko60ea6352020-06-29 13:39:39 +020051{
52 /* for this purpose, if hash matches, the value does also, we do not want 2 values to have the same hash */
53 return 1;
54}
55
56/**
57 * @brief Hash table equal callback for checking value pointer equality only.
Radek Krejci857189e2020-09-01 13:26:36 +020058 *
Michal Vasko62524a92021-02-26 10:08:50 +010059 * Implementation of ::lyht_value_equal_cb.
Michal Vasko60ea6352020-06-29 13:39:39 +020060 */
Radek Krejci857189e2020-09-01 13:26:36 +020061static ly_bool
62lyb_ptr_equal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
Michal Vasko60ea6352020-06-29 13:39:39 +020063{
64 struct lysc_node *val1 = *(struct lysc_node **)val1_p;
65 struct lysc_node *val2 = *(struct lysc_node **)val2_p;
66
67 if (val1 == val2) {
68 return 1;
69 }
70 return 0;
71}
72
73/**
74 * @brief Check that sibling collision hash is safe to insert into hash table.
75 *
76 * @param[in] ht Hash table.
77 * @param[in] sibling Hashed sibling.
78 * @param[in] ht_col_id Sibling hash collision ID.
79 * @param[in] compare_col_id Last collision ID to compare with.
80 * @return LY_SUCCESS when the whole hash sequence does not collide,
81 * @return LY_EEXIST when the whole hash sequence sollides.
82 */
83static LY_ERR
Michal Vasko8efac242023-03-30 08:24:56 +020084lyb_hash_sequence_check(struct ly_ht *ht, struct lysc_node *sibling, LYB_HASH ht_col_id, LYB_HASH compare_col_id)
Michal Vasko60ea6352020-06-29 13:39:39 +020085{
Michal Vasko60ea6352020-06-29 13:39:39 +020086 struct lysc_node **col_node;
87
88 /* get the first node inserted with last hash col ID ht_col_id */
Michal Vasko11f76c82021-04-15 14:36:14 +020089 if (lyht_find(ht, &sibling, lyb_get_hash(sibling, ht_col_id), (void **)&col_node)) {
Michal Vasko60ea6352020-06-29 13:39:39 +020090 /* there is none. valid situation */
91 return LY_SUCCESS;
92 }
93
94 lyht_set_cb(ht, lyb_ptr_equal_cb);
95 do {
Radek Krejci1deb5be2020-08-26 16:43:36 +020096 int64_t j;
Michal Vasko26bbb272022-08-02 14:54:33 +020097
Radek Krejci1deb5be2020-08-26 16:43:36 +020098 for (j = (int64_t)compare_col_id; j > -1; --j) {
Michal Vasko11f76c82021-04-15 14:36:14 +020099 if (lyb_get_hash(sibling, j) != lyb_get_hash(*col_node, j)) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200100 /* one non-colliding hash */
101 break;
102 }
103 }
104 if (j == -1) {
105 /* all whole hash sequences of nodes inserted with last hash col ID compare_col_id collide */
106 lyht_set_cb(ht, lyb_hash_equal_cb);
107 return LY_EEXIST;
108 }
109
110 /* get next node inserted with last hash col ID ht_col_id */
Michal Vasko6374de22022-09-05 15:48:48 +0200111 } while (!lyht_find_next_with_collision_cb(ht, col_node, lyb_get_hash(*col_node, ht_col_id), lyb_hash_equal_cb,
112 (void **)&col_node));
Michal Vasko60ea6352020-06-29 13:39:39 +0200113
114 lyht_set_cb(ht, lyb_hash_equal_cb);
115 return LY_SUCCESS;
116}
117
118/**
119 * @brief Hash all the siblings and add them also into a separate hash table.
120 *
121 * @param[in] sibling Any sibling in all the siblings on one level.
122 * @param[out] ht_p Created hash table.
123 * @return LY_ERR value.
124 */
125static LY_ERR
Michal Vasko8efac242023-03-30 08:24:56 +0200126lyb_hash_siblings(struct lysc_node *sibling, struct ly_ht **ht_p)
Michal Vasko60ea6352020-06-29 13:39:39 +0200127{
Michal Vasko8efac242023-03-30 08:24:56 +0200128 struct ly_ht *ht;
Michal Vasko60ea6352020-06-29 13:39:39 +0200129 const struct lysc_node *parent;
130 const struct lys_module *mod;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200131 LYB_HASH i;
Michal Vasko1e0a80a2020-12-09 18:12:51 +0100132 uint32_t getnext_opts;
Michal Vasko60ea6352020-06-29 13:39:39 +0200133
134 ht = lyht_new(1, sizeof(struct lysc_node *), lyb_hash_equal_cb, NULL, 1);
135 LY_CHECK_ERR_RET(!ht, LOGMEM(sibling->module->ctx), LY_EMEM);
136
Michal Vasko1e0a80a2020-12-09 18:12:51 +0100137 getnext_opts = 0;
Michal Vaskod1e53b92021-01-28 13:11:06 +0100138 if (sibling->flags & LYS_IS_OUTPUT) {
Michal Vasko1e0a80a2020-12-09 18:12:51 +0100139 getnext_opts = LYS_GETNEXT_OUTPUT;
140 }
141
Michal Vasko60ea6352020-06-29 13:39:39 +0200142 parent = lysc_data_parent(sibling);
143 mod = sibling->module;
144
145 sibling = NULL;
Michal Vasko1e0a80a2020-12-09 18:12:51 +0100146 while ((sibling = (struct lysc_node *)lys_getnext(sibling, parent, mod->compiled, getnext_opts))) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200147 /* find the first non-colliding hash (or specifically non-colliding hash sequence) */
148 for (i = 0; i < LYB_HASH_BITS; ++i) {
149 /* check that we are not colliding with nodes inserted with a lower collision ID than ours */
Radek Krejci1deb5be2020-08-26 16:43:36 +0200150 int64_t j;
Michal Vasko26bbb272022-08-02 14:54:33 +0200151
Radek Krejci1deb5be2020-08-26 16:43:36 +0200152 for (j = (int64_t)i - 1; j > -1; --j) {
153 if (lyb_hash_sequence_check(ht, sibling, (LYB_HASH)j, i)) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200154 break;
155 }
156 }
157 if (j > -1) {
158 /* some check failed, we must use a higher collision ID */
159 continue;
160 }
161
162 /* try to insert node with the current collision ID */
Michal Vasko11f76c82021-04-15 14:36:14 +0200163 if (!lyht_insert_with_resize_cb(ht, &sibling, lyb_get_hash(sibling, i), lyb_ptr_equal_cb, NULL)) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200164 /* success, no collision */
165 break;
166 }
167
168 /* make sure we really cannot insert it with this hash col ID (meaning the whole hash sequence is colliding) */
169 if (i && !lyb_hash_sequence_check(ht, sibling, i, i)) {
170 /* it can be inserted after all, even though there is already a node with the same last collision ID */
171 lyht_set_cb(ht, lyb_ptr_equal_cb);
Michal Vasko11f76c82021-04-15 14:36:14 +0200172 if (lyht_insert(ht, &sibling, lyb_get_hash(sibling, i), NULL)) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200173 LOGINT(sibling->module->ctx);
174 lyht_set_cb(ht, lyb_hash_equal_cb);
Michal Vasko77b7f90a2023-01-31 15:42:41 +0100175 lyht_free(ht, NULL);
Michal Vasko60ea6352020-06-29 13:39:39 +0200176 return LY_EINT;
177 }
178 lyht_set_cb(ht, lyb_hash_equal_cb);
179 break;
180 }
181 /* there is still another colliding schema node with the same hash sequence, try higher collision ID */
182 }
183
184 if (i == LYB_HASH_BITS) {
185 /* wow */
186 LOGINT(sibling->module->ctx);
Michal Vasko77b7f90a2023-01-31 15:42:41 +0100187 lyht_free(ht, NULL);
Michal Vasko60ea6352020-06-29 13:39:39 +0200188 return LY_EINT;
189 }
190 }
191
192 /* change val equal callback so that the HT is usable for finding value hashes */
193 lyht_set_cb(ht, lyb_ptr_equal_cb);
194
195 *ht_p = ht;
196 return LY_SUCCESS;
197}
198
199/**
200 * @brief Find node hash in a hash table.
201 *
202 * @param[in] ht Hash table to search in.
203 * @param[in] node Node to find.
204 * @param[out] hash_p First non-colliding hash found.
205 * @return LY_ERR value.
206 */
207static LY_ERR
Michal Vasko8efac242023-03-30 08:24:56 +0200208lyb_hash_find(struct ly_ht *ht, struct lysc_node *node, LYB_HASH *hash_p)
Michal Vasko60ea6352020-06-29 13:39:39 +0200209{
210 LYB_HASH hash;
211 uint32_t i;
212
213 for (i = 0; i < LYB_HASH_BITS; ++i) {
Michal Vasko11f76c82021-04-15 14:36:14 +0200214 hash = lyb_get_hash(node, i);
Michal Vasko60ea6352020-06-29 13:39:39 +0200215 if (!hash) {
216 LOGINT_RET(node->module->ctx);
217 }
218
219 if (!lyht_find(ht, &node, hash, NULL)) {
220 /* success, no collision */
221 break;
222 }
223 }
224 /* cannot happen, we already calculated the hash */
225 if (i == LYB_HASH_BITS) {
226 LOGINT_RET(node->module->ctx);
227 }
228
229 *hash_p = hash;
230 return LY_SUCCESS;
231}
232
233/**
aPiecek6828a312021-09-17 15:53:18 +0200234 * @brief Write metadata about siblings.
235 *
236 * @param[in] out Out structure.
237 * @param[in] sib Contains metadata that is written.
238 */
239static LY_ERR
240lyb_write_sibling_meta(struct ly_out *out, struct lyd_lyb_sibling *sib)
241{
242 uint8_t meta_buf[LYB_META_BYTES];
243 uint64_t num = 0;
244
245 /* write the meta chunk information */
aPiecek58fef2e2021-09-30 13:21:51 +0200246 num = htole64((uint64_t)sib->written & LYB_SIZE_MAX);
aPiecek6828a312021-09-17 15:53:18 +0200247 memcpy(meta_buf, &num, LYB_SIZE_BYTES);
aPiecek58fef2e2021-09-30 13:21:51 +0200248 num = htole64((uint64_t)sib->inner_chunks & LYB_INCHUNK_MAX);
aPiecek6828a312021-09-17 15:53:18 +0200249 memcpy(meta_buf + LYB_SIZE_BYTES, &num, LYB_INCHUNK_BYTES);
250
251 LY_CHECK_RET(ly_write_skipped(out, sib->position, (char *)&meta_buf, LYB_META_BYTES));
252
253 return LY_SUCCESS;
254}
255
256/**
Michal Vasko60ea6352020-06-29 13:39:39 +0200257 * @brief Write LYB data fully handling the metadata.
258 *
259 * @param[in] out Out structure.
260 * @param[in] buf Source buffer.
261 * @param[in] count Number of bytes to write.
262 * @param[in] lybctx LYB context.
263 * @return LY_ERR value.
264 */
265static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200266lyb_write(struct ly_out *out, const uint8_t *buf, size_t count, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200267{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200268 LY_ARRAY_COUNT_TYPE u;
aPiecek570d7ed2021-09-10 07:15:35 +0200269 struct lyd_lyb_sibling *full, *iter;
Michal Vasko5233e962020-08-14 14:26:20 +0200270 size_t to_write;
Michal Vasko60ea6352020-06-29 13:39:39 +0200271
272 while (1) {
273 /* check for full data chunks */
274 to_write = count;
275 full = NULL;
aPiecek570d7ed2021-09-10 07:15:35 +0200276 LY_ARRAY_FOR(lybctx->siblings, u) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200277 /* we want the innermost chunks resolved first, so replace previous full chunks */
aPiecek570d7ed2021-09-10 07:15:35 +0200278 if (lybctx->siblings[u].written + to_write >= LYB_SIZE_MAX) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200279 /* full chunk, do not write more than allowed */
aPiecek570d7ed2021-09-10 07:15:35 +0200280 to_write = LYB_SIZE_MAX - lybctx->siblings[u].written;
281 full = &lybctx->siblings[u];
Michal Vasko60ea6352020-06-29 13:39:39 +0200282 }
283 }
284
285 if (!full && !count) {
286 break;
287 }
288
289 /* we are actually writing some data, not just finishing another chunk */
290 if (to_write) {
Michal Vasko5233e962020-08-14 14:26:20 +0200291 LY_CHECK_RET(ly_write_(out, (char *)buf, to_write));
Michal Vasko60ea6352020-06-29 13:39:39 +0200292
aPiecek570d7ed2021-09-10 07:15:35 +0200293 LY_ARRAY_FOR(lybctx->siblings, u) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200294 /* increase all written counters */
aPiecek570d7ed2021-09-10 07:15:35 +0200295 lybctx->siblings[u].written += to_write;
296 assert(lybctx->siblings[u].written <= LYB_SIZE_MAX);
Michal Vasko60ea6352020-06-29 13:39:39 +0200297 }
298 /* decrease count/buf */
Michal Vasko5233e962020-08-14 14:26:20 +0200299 count -= to_write;
300 buf += to_write;
Michal Vasko60ea6352020-06-29 13:39:39 +0200301 }
302
303 if (full) {
304 /* write the meta information (inner chunk count and chunk size) */
aPiecek6828a312021-09-17 15:53:18 +0200305 LY_CHECK_RET(lyb_write_sibling_meta(out, full));
Michal Vasko60ea6352020-06-29 13:39:39 +0200306
307 /* zero written and inner chunks */
308 full->written = 0;
309 full->inner_chunks = 0;
310
311 /* skip space for another chunk size */
Michal Vasko5233e962020-08-14 14:26:20 +0200312 LY_CHECK_RET(ly_write_skip(out, LYB_META_BYTES, &full->position));
Michal Vasko60ea6352020-06-29 13:39:39 +0200313
314 /* increase inner chunk count */
aPiecek570d7ed2021-09-10 07:15:35 +0200315 for (iter = &lybctx->siblings[0]; iter != full; ++iter) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200316 if (iter->inner_chunks == LYB_INCHUNK_MAX) {
317 LOGINT(lybctx->ctx);
318 return LY_EINT;
319 }
320 ++iter->inner_chunks;
321 }
322 }
323 }
324
325 return LY_SUCCESS;
326}
327
328/**
aPiecek570d7ed2021-09-10 07:15:35 +0200329 * @brief Stop the current "siblings" - write its final metadata.
Michal Vasko60ea6352020-06-29 13:39:39 +0200330 *
331 * @param[in] out Out structure.
332 * @param[in] lybctx LYB context.
333 * @return LY_ERR value.
334 */
335static LY_ERR
aPiecek570d7ed2021-09-10 07:15:35 +0200336lyb_write_stop_siblings(struct ly_out *out, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200337{
Michal Vasko60ea6352020-06-29 13:39:39 +0200338 /* write the meta chunk information */
aPiecek6828a312021-09-17 15:53:18 +0200339 lyb_write_sibling_meta(out, &LYB_LAST_SIBLING(lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200340
aPiecek570d7ed2021-09-10 07:15:35 +0200341 LY_ARRAY_DECREMENT(lybctx->siblings);
Michal Vasko60ea6352020-06-29 13:39:39 +0200342 return LY_SUCCESS;
343}
344
345/**
aPiecek570d7ed2021-09-10 07:15:35 +0200346 * @brief Start a new "siblings" - skip bytes for its metadata.
Michal Vasko60ea6352020-06-29 13:39:39 +0200347 *
348 * @param[in] out Out structure.
349 * @param[in] lybctx LYB context.
350 * @return LY_ERR value.
351 */
352static LY_ERR
aPiecek570d7ed2021-09-10 07:15:35 +0200353lyb_write_start_siblings(struct ly_out *out, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200354{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200355 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +0200356
aPiecek570d7ed2021-09-10 07:15:35 +0200357 u = LY_ARRAY_COUNT(lybctx->siblings);
358 if (u == lybctx->sibling_size) {
359 LY_ARRAY_CREATE_RET(lybctx->ctx, lybctx->siblings, u + LYB_SIBLING_STEP, LY_EMEM);
360 lybctx->sibling_size = u + LYB_SIBLING_STEP;
Michal Vasko60ea6352020-06-29 13:39:39 +0200361 }
362
aPiecek570d7ed2021-09-10 07:15:35 +0200363 LY_ARRAY_INCREMENT(lybctx->siblings);
364 LYB_LAST_SIBLING(lybctx).written = 0;
365 LYB_LAST_SIBLING(lybctx).inner_chunks = 0;
Michal Vasko60ea6352020-06-29 13:39:39 +0200366
367 /* another inner chunk */
aPiecek570d7ed2021-09-10 07:15:35 +0200368 for (u = 0; u < LY_ARRAY_COUNT(lybctx->siblings) - 1; ++u) {
369 if (lybctx->siblings[u].inner_chunks == LYB_INCHUNK_MAX) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200370 LOGINT(lybctx->ctx);
Michal Vasko5233e962020-08-14 14:26:20 +0200371 return LY_EINT;
Michal Vasko60ea6352020-06-29 13:39:39 +0200372 }
aPiecek570d7ed2021-09-10 07:15:35 +0200373 ++lybctx->siblings[u].inner_chunks;
Michal Vasko60ea6352020-06-29 13:39:39 +0200374 }
375
aPiecek570d7ed2021-09-10 07:15:35 +0200376 LY_CHECK_RET(ly_write_skip(out, LYB_META_BYTES, &LYB_LAST_SIBLING(lybctx).position));
Michal Vasko60ea6352020-06-29 13:39:39 +0200377
378 return LY_SUCCESS;
379}
380
381/**
382 * @brief Write a number.
383 *
384 * @param[in] num Number to write.
385 * @param[in] bytes Actual accessible bytes of @p num.
386 * @param[in] out Out structure.
387 * @param[in] lybctx LYB context.
388 * @return LY_ERR value.
389 */
390static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200391lyb_write_number(uint64_t num, size_t bytes, struct ly_out *out, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200392{
393 /* correct byte order */
394 num = htole64(num);
395
396 return lyb_write(out, (uint8_t *)&num, bytes, lybctx);
397}
398
399/**
400 * @brief Write a string.
401 *
402 * @param[in] str String to write.
403 * @param[in] str_len Length of @p str.
Michal Vasko9883a3e2022-03-31 12:16:00 +0200404 * @param[in] len_size Size of @p str_len in bytes.
Michal Vasko60ea6352020-06-29 13:39:39 +0200405 * @param[in] out Out structure.
406 * @param[in] lybctx LYB context.
407 * @return LY_ERR value.
408 */
409static LY_ERR
aPieceke99345d2021-09-30 12:49:59 +0200410lyb_write_string(const char *str, size_t str_len, uint8_t len_size, struct ly_out *out, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200411{
aPieceke99345d2021-09-30 12:49:59 +0200412 ly_bool error;
413
Michal Vasko60ea6352020-06-29 13:39:39 +0200414 if (!str) {
415 str = "";
Michal Vasko30667352020-08-13 09:06:14 +0200416 LY_CHECK_ERR_RET(str_len, LOGINT(lybctx->ctx), LY_EINT);
Michal Vasko60ea6352020-06-29 13:39:39 +0200417 }
aPieceke99345d2021-09-30 12:49:59 +0200418
Michal Vasko60ea6352020-06-29 13:39:39 +0200419 if (!str_len) {
420 str_len = strlen(str);
421 }
422
aPieceke99345d2021-09-30 12:49:59 +0200423 switch (len_size) {
424 case sizeof(uint8_t):
425 error = str_len > UINT8_MAX;
426 break;
427 case sizeof(uint16_t):
428 error = str_len > UINT16_MAX;
429 break;
430 case sizeof(uint32_t):
431 error = str_len > UINT32_MAX;
432 break;
433 case sizeof(uint64_t):
Michal Vasko21eaa392024-02-20 15:48:42 +0100434 error = 0;
aPieceke99345d2021-09-30 12:49:59 +0200435 break;
436 default:
437 error = 1;
Michal Vasko60ea6352020-06-29 13:39:39 +0200438 }
aPieceke99345d2021-09-30 12:49:59 +0200439 if (error) {
440 LOGINT(lybctx->ctx);
441 return LY_EINT;
442 }
443
444 LY_CHECK_RET(lyb_write_number(str_len, len_size, out, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200445
Michal Vasko63f3d842020-07-08 10:10:14 +0200446 LY_CHECK_RET(lyb_write(out, (const uint8_t *)str, str_len, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200447
448 return LY_SUCCESS;
449}
450
451/**
452 * @brief Print YANG module info.
453 *
454 * @param[in] out Out structure.
455 * @param[in] mod Module to print.
Michal Vasko78041d12022-11-29 14:11:07 +0100456 * @param[in] with_features Whether to also print enabled features or not.
Michal Vasko60ea6352020-06-29 13:39:39 +0200457 * @param[in] lybctx LYB context.
458 * @return LY_ERR value.
459 */
460static LY_ERR
Michal Vasko78041d12022-11-29 14:11:07 +0100461lyb_print_model(struct ly_out *out, const struct lys_module *mod, ly_bool with_features, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200462{
Michal Vasko78041d12022-11-29 14:11:07 +0100463 LY_ERR rc = LY_SUCCESS;
Michal Vasko60ea6352020-06-29 13:39:39 +0200464 uint16_t revision;
Michal Vasko78041d12022-11-29 14:11:07 +0100465 struct ly_set feat_set = {0};
466 struct lysp_feature *f = NULL;
467 uint32_t i = 0;
Michal Vasko9883a3e2022-03-31 12:16:00 +0200468 int r;
Michal Vasko60ea6352020-06-29 13:39:39 +0200469
470 /* model name length and model name */
Michal Vasko78041d12022-11-29 14:11:07 +0100471 LY_CHECK_GOTO(rc = lyb_write_string(mod->name, 0, sizeof(uint16_t), out, lybctx), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200472
473 /* model revision as XXXX XXXX XXXX XXXX (2B) (year is offset from 2000)
474 * YYYY YYYM MMMD DDDD */
475 revision = 0;
Michal Vasko9883a3e2022-03-31 12:16:00 +0200476 if (mod->revision) {
477 r = atoi(mod->revision);
Radek Krejcif13b87b2020-12-01 22:02:17 +0100478 r -= LYB_REV_YEAR_OFFSET;
479 r <<= LYB_REV_YEAR_SHIFT;
Michal Vasko60ea6352020-06-29 13:39:39 +0200480
481 revision |= r;
482
Radek Krejcif13b87b2020-12-01 22:02:17 +0100483 r = atoi(mod->revision + ly_strlen_const("YYYY-"));
484 r <<= LYB_REV_MONTH_SHIFT;
Michal Vasko60ea6352020-06-29 13:39:39 +0200485
486 revision |= r;
487
Radek Krejcif13b87b2020-12-01 22:02:17 +0100488 r = atoi(mod->revision + ly_strlen_const("YYYY-MM-"));
Michal Vasko60ea6352020-06-29 13:39:39 +0200489
490 revision |= r;
491 }
Michal Vasko78041d12022-11-29 14:11:07 +0100492 LY_CHECK_GOTO(rc = lyb_write_number(revision, sizeof revision, out, lybctx), cleanup);
493
494 if (with_features) {
495 /* collect enabled module features */
496 while ((f = lysp_feature_next(f, mod->parsed, &i))) {
497 if (f->flags & LYS_FENABLED) {
498 LY_CHECK_GOTO(rc = ly_set_add(&feat_set, f, 1, NULL), cleanup);
499 }
500 }
501
502 /* print enabled feature count and their names */
503 LY_CHECK_GOTO(rc = lyb_write_number(feat_set.count, sizeof(uint16_t), out, lybctx), cleanup);
504 for (i = 0; i < feat_set.count; ++i) {
505 f = feat_set.objs[i];
506 LY_CHECK_GOTO(rc = lyb_write_string(f->name, 0, sizeof(uint16_t), out, lybctx), cleanup);
507 }
508 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200509
Michal Vasko9883a3e2022-03-31 12:16:00 +0200510 /* fill cached hashes, if not already */
511 lyb_cache_module_hash(mod);
Michal Vasko11f76c82021-04-15 14:36:14 +0200512
Michal Vasko78041d12022-11-29 14:11:07 +0100513cleanup:
514 ly_set_erase(&feat_set, NULL);
515 return rc;
Michal Vasko60ea6352020-06-29 13:39:39 +0200516}
517
518/**
519 * @brief Print all used YANG modules.
520 *
521 * @param[in] out Out structure.
522 * @param[in] root Data root.
523 * @param[in] lybctx LYB context.
524 * @return LY_ERR value.
525 */
526static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200527lyb_print_data_models(struct ly_out *out, const struct lyd_node *root, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200528{
529 struct ly_set *set;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200530 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +0200531 LY_ERR ret = LY_SUCCESS;
532 struct lys_module *mod;
Michal Vaskof4b4d002021-08-23 12:16:02 +0200533 const struct lyd_node *elem, *node;
Michal Vasko60ea6352020-06-29 13:39:39 +0200534 uint32_t i;
535
Radek Krejciba03a5a2020-08-27 14:40:41 +0200536 LY_CHECK_RET(ly_set_new(&set));
Michal Vasko60ea6352020-06-29 13:39:39 +0200537
538 /* collect all data node modules */
Michal Vaskof4b4d002021-08-23 12:16:02 +0200539 LY_LIST_FOR(root, elem) {
540 LYD_TREE_DFS_BEGIN(elem, node) {
541 if (node->schema) {
542 mod = node->schema->module;
543 ret = ly_set_add(set, mod, 0, NULL);
544 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200545
Michal Vaskof4b4d002021-08-23 12:16:02 +0200546 /* add also their modules deviating or augmenting them */
547 LY_ARRAY_FOR(mod->deviated_by, u) {
548 ret = ly_set_add(set, mod->deviated_by[u], 0, NULL);
549 LY_CHECK_GOTO(ret, cleanup);
550 }
551 LY_ARRAY_FOR(mod->augmented_by, u) {
552 ret = ly_set_add(set, mod->augmented_by[u], 0, NULL);
553 LY_CHECK_GOTO(ret, cleanup);
554 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200555
Michal Vaskof4b4d002021-08-23 12:16:02 +0200556 /* only top-level nodes are processed */
557 LYD_TREE_DFS_continue = 1;
558 }
559
560 LYD_TREE_DFS_END(elem, node);
Michal Vasko60ea6352020-06-29 13:39:39 +0200561 }
562 }
563
564 /* now write module count on 2 bytes */
565 LY_CHECK_GOTO(ret = lyb_write_number(set->count, 2, out, lybctx), cleanup);
566
567 /* and all the used models */
568 for (i = 0; i < set->count; ++i) {
Michal Vasko78041d12022-11-29 14:11:07 +0100569 LY_CHECK_GOTO(ret = lyb_print_model(out, set->objs[i], 1, lybctx), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200570 }
571
572cleanup:
573 ly_set_free(set, NULL);
574 return ret;
575}
576
577/**
578 * @brief Print LYB magic number.
579 *
580 * @param[in] out Out structure.
Michal Vasko60ea6352020-06-29 13:39:39 +0200581 * @return LY_ERR value.
582 */
583static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200584lyb_print_magic_number(struct ly_out *out)
Michal Vasko60ea6352020-06-29 13:39:39 +0200585{
Michal Vasko60ea6352020-06-29 13:39:39 +0200586 /* 'l', 'y', 'b' - 0x6c7962 */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100587 char magic_number[] = {'l', 'y', 'b'};
Michal Vasko60ea6352020-06-29 13:39:39 +0200588
Radek Krejcidefd4d72020-12-01 12:20:30 +0100589 LY_CHECK_RET(ly_write_(out, magic_number, 3));
Michal Vasko60ea6352020-06-29 13:39:39 +0200590
591 return LY_SUCCESS;
592}
593
594/**
595 * @brief Print LYB header.
596 *
597 * @param[in] out Out structure.
Michal Vasko60ea6352020-06-29 13:39:39 +0200598 * @return LY_ERR value.
599 */
600static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200601lyb_print_header(struct ly_out *out)
Michal Vasko60ea6352020-06-29 13:39:39 +0200602{
Michal Vasko60ea6352020-06-29 13:39:39 +0200603 uint8_t byte = 0;
604
605 /* version, future flags */
606 byte |= LYB_VERSION_NUM;
607
Michal Vasko5233e962020-08-14 14:26:20 +0200608 LY_CHECK_RET(ly_write_(out, (char *)&byte, 1));
Michal Vasko60ea6352020-06-29 13:39:39 +0200609
610 return LY_SUCCESS;
611}
612
613/**
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100614 * @brief Print prefix data.
Michal Vasko60ea6352020-06-29 13:39:39 +0200615 *
616 * @param[in] out Out structure.
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100617 * @param[in] format Value prefix format.
Radek Krejci8df109d2021-04-23 12:19:08 +0200618 * @param[in] prefix_data Format-specific data for resolving any prefixes (see ::ly_resolve_prefix).
Michal Vasko60ea6352020-06-29 13:39:39 +0200619 * @param[in] lybctx LYB context.
620 * @return LY_ERR value.
621 */
622static LY_ERR
Radek Krejci8df109d2021-04-23 12:19:08 +0200623lyb_print_prefix_data(struct ly_out *out, LY_VALUE_FORMAT format, const void *prefix_data, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200624{
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100625 const struct ly_set *set;
626 const struct lyxml_ns *ns;
627 uint32_t i;
Michal Vasko60ea6352020-06-29 13:39:39 +0200628
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100629 switch (format) {
Radek Krejci8df109d2021-04-23 12:19:08 +0200630 case LY_VALUE_XML:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100631 set = prefix_data;
632 if (!set) {
633 /* no prefix data */
634 i = 0;
635 LY_CHECK_RET(lyb_write(out, (uint8_t *)&i, 1, lybctx));
636 break;
637 }
638 if (set->count > UINT8_MAX) {
639 LOGERR(lybctx->ctx, LY_EINT, "Maximum supported number of prefixes is %u.", UINT8_MAX);
640 return LY_EINT;
641 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200642
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100643 /* write number of prefixes on 1 byte */
Michal Vasko403beac2021-08-24 08:27:52 +0200644 LY_CHECK_RET(lyb_write_number(set->count, 1, out, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200645
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100646 /* write all the prefixes */
647 for (i = 0; i < set->count; ++i) {
648 ns = set->objs[i];
Michal Vasko60ea6352020-06-29 13:39:39 +0200649
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100650 /* prefix */
aPieceke99345d2021-09-30 12:49:59 +0200651 LY_CHECK_RET(lyb_write_string(ns->prefix, 0, sizeof(uint16_t), out, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200652
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100653 /* namespace */
aPieceke99345d2021-09-30 12:49:59 +0200654 LY_CHECK_RET(lyb_write_string(ns->uri, 0, sizeof(uint16_t), out, lybctx));
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100655 }
656 break;
Radek Krejci8df109d2021-04-23 12:19:08 +0200657 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +0200658 case LY_VALUE_LYB:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100659 /* nothing to print */
660 break;
661 default:
662 LOGINT_RET(lybctx->ctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200663 }
664
665 return LY_SUCCESS;
666}
667
668/**
Michal Vasko60ea6352020-06-29 13:39:39 +0200669 * @brief Print term node.
670 *
671 * @param[in] term Node to print.
672 * @param[in] out Out structure.
673 * @param[in] lybctx LYB context.
674 * @return LY_ERR value.
675 */
676static LY_ERR
aPieceka19ca152021-09-09 12:27:35 +0200677lyb_print_term_value(struct lyd_node_term *term, struct ly_out *out, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200678{
aPiecekaa5b70a2021-08-30 08:33:25 +0200679 LY_ERR ret = LY_SUCCESS;
aPiecekea304e32021-08-18 09:13:47 +0200680 ly_bool dynamic = 0;
681 void *value;
682 size_t value_len = 0;
aPiecekaa5b70a2021-08-30 08:33:25 +0200683 int32_t lyb_data_len;
684 lyplg_type_print_clb print;
aPiecekea304e32021-08-18 09:13:47 +0200685
686 assert(term->value.realtype && term->value.realtype->plugin && term->value.realtype->plugin->print &&
687 term->schema);
688
aPiecekaa5b70a2021-08-30 08:33:25 +0200689 /* Get length of LYB data to print. */
690 lyb_data_len = term->value.realtype->plugin->lyb_data_len;
aPiecekea304e32021-08-18 09:13:47 +0200691
aPiecekaa5b70a2021-08-30 08:33:25 +0200692 /* Get value and also print its length only if size is not fixed. */
693 print = term->value.realtype->plugin->print;
694 if (lyb_data_len < 0) {
695 /* Variable-length data. */
696
697 /* Get value and its length from plugin. */
698 value = (void *)print(term->schema->module->ctx, &term->value,
699 LY_VALUE_LYB, NULL, &dynamic, &value_len);
700 LY_CHECK_GOTO(ret, cleanup);
701
702 if (value_len > UINT32_MAX) {
703 LOGERR(lybctx->ctx, LY_EINT, "The maximum length of the LYB data "
Michal Vasko7b3a00e2023-08-09 11:58:03 +0200704 "from a term node must not exceed %" PRIu32 ".", UINT32_MAX);
aPiecekaa5b70a2021-08-30 08:33:25 +0200705 ret = LY_EINT;
706 goto cleanup;
707 }
708
aPieceke99345d2021-09-30 12:49:59 +0200709 /* Print the length of the data as 64-bit unsigned integer. */
710 ret = lyb_write_number(value_len, sizeof(uint64_t), out, lybctx);
aPiecekaa5b70a2021-08-30 08:33:25 +0200711 LY_CHECK_GOTO(ret, cleanup);
712 } else {
713 /* Fixed-length data. */
714
715 /* Get value from plugin. */
716 value = (void *)print(term->schema->module->ctx, &term->value,
717 LY_VALUE_LYB, NULL, &dynamic, NULL);
718 LY_CHECK_GOTO(ret, cleanup);
719
720 /* Copy the length from the compiled node. */
721 value_len = lyb_data_len;
aPiecekea304e32021-08-18 09:13:47 +0200722 }
723
aPiecekaa5b70a2021-08-30 08:33:25 +0200724 /* Print value. */
aPiecekea304e32021-08-18 09:13:47 +0200725 if (value_len > 0) {
aPiecekaa5b70a2021-08-30 08:33:25 +0200726 /* Print the value simply as it is. */
aPiecekea304e32021-08-18 09:13:47 +0200727 ret = lyb_write(out, value, value_len, lybctx);
728 LY_CHECK_GOTO(ret, cleanup);
729 }
730
731cleanup:
732 if (dynamic) {
733 free(value);
734 }
735
736 return ret;
Michal Vasko60ea6352020-06-29 13:39:39 +0200737}
738
739/**
740 * @brief Print YANG node metadata.
741 *
742 * @param[in] out Out structure.
743 * @param[in] node Data node whose metadata to print.
744 * @param[in] lybctx LYB context.
745 * @return LY_ERR value.
746 */
747static LY_ERR
748lyb_print_metadata(struct ly_out *out, const struct lyd_node *node, struct lyd_lyb_ctx *lybctx)
749{
Michal Vasko60ea6352020-06-29 13:39:39 +0200750 uint8_t count = 0;
751 const struct lys_module *wd_mod = NULL;
752 struct lyd_meta *iter;
Michal Vasko60ea6352020-06-29 13:39:39 +0200753
754 /* with-defaults */
755 if (node->schema->nodetype & LYD_NODE_TERM) {
Radek Krejci7931b192020-06-25 17:05:03 +0200756 if (((node->flags & LYD_DEFAULT) && (lybctx->print_options & (LYD_PRINT_WD_ALL_TAG | LYD_PRINT_WD_IMPL_TAG))) ||
Radek Krejci19611252020-10-04 13:54:53 +0200757 ((lybctx->print_options & LYD_PRINT_WD_ALL_TAG) && lyd_is_default(node))) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200758 /* we have implicit OR explicit default node, print attribute only if context include with-defaults schema */
759 wd_mod = ly_ctx_get_module_latest(node->schema->module->ctx, "ietf-netconf-with-defaults");
760 }
761 }
762
763 /* count metadata */
764 if (wd_mod) {
765 ++count;
766 }
767 for (iter = node->meta; iter; iter = iter->next) {
aPiecek6cf1d162023-11-08 16:07:00 +0100768 if (!lyd_metadata_should_print(iter)) {
769 continue;
770 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200771 if (count == UINT8_MAX) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200772 LOGERR(lybctx->lybctx->ctx, LY_EINT, "Maximum supported number of data node metadata is %u.", UINT8_MAX);
Michal Vasko60ea6352020-06-29 13:39:39 +0200773 return LY_EINT;
774 }
775 ++count;
776 }
777
778 /* write number of metadata on 1 byte */
Radek Krejci1798aae2020-07-14 13:26:06 +0200779 LY_CHECK_RET(lyb_write(out, &count, 1, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200780
781 if (wd_mod) {
782 /* write the "default" metadata */
Michal Vasko78041d12022-11-29 14:11:07 +0100783 LY_CHECK_RET(lyb_print_model(out, wd_mod, 0, lybctx->lybctx));
aPieceke99345d2021-09-30 12:49:59 +0200784 LY_CHECK_RET(lyb_write_string("default", 0, sizeof(uint16_t), out, lybctx->lybctx));
785 LY_CHECK_RET(lyb_write_string("true", 0, sizeof(uint16_t), out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200786 }
787
788 /* write all the node metadata */
789 LY_LIST_FOR(node->meta, iter) {
aPiecek6cf1d162023-11-08 16:07:00 +0100790 if (!lyd_metadata_should_print(iter)) {
791 continue;
792 }
793
Michal Vasko60ea6352020-06-29 13:39:39 +0200794 /* model */
Michal Vasko78041d12022-11-29 14:11:07 +0100795 LY_CHECK_RET(lyb_print_model(out, iter->annotation->module, 0, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200796
797 /* annotation name with length */
aPieceke99345d2021-09-30 12:49:59 +0200798 LY_CHECK_RET(lyb_write_string(iter->name, 0, sizeof(uint16_t), out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200799
Michal Vasko60ea6352020-06-29 13:39:39 +0200800 /* metadata value */
aPieceke99345d2021-09-30 12:49:59 +0200801 LY_CHECK_RET(lyb_write_string(lyd_get_meta_value(iter), 0, sizeof(uint64_t), out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200802 }
803
804 return LY_SUCCESS;
805}
806
807/**
808 * @brief Print opaque node attributes.
809 *
810 * @param[in] out Out structure.
811 * @param[in] node Opaque node whose attributes to print.
812 * @param[in] lybctx LYB context.
813 * @return LY_ERR value.
814 */
815static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200816lyb_print_attributes(struct ly_out *out, const struct lyd_node_opaq *node, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200817{
818 uint8_t count = 0;
Radek Krejci5536d282020-08-04 23:27:44 +0200819 struct lyd_attr *iter;
Michal Vasko60ea6352020-06-29 13:39:39 +0200820
821 for (iter = node->attr; iter; iter = iter->next) {
822 if (count == UINT8_MAX) {
823 LOGERR(lybctx->ctx, LY_EINT, "Maximum supported number of data node attributes is %u.", UINT8_MAX);
824 return LY_EINT;
825 }
826 ++count;
827 }
828
829 /* write number of attributes on 1 byte */
830 LY_CHECK_RET(lyb_write(out, &count, 1, lybctx));
831
832 /* write all the attributes */
833 LY_LIST_FOR(node->attr, iter) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200834 /* prefix */
aPieceke99345d2021-09-30 12:49:59 +0200835 LY_CHECK_RET(lyb_write_string(iter->name.prefix, 0, sizeof(uint16_t), out, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200836
837 /* namespace */
aPieceke99345d2021-09-30 12:49:59 +0200838 LY_CHECK_RET(lyb_write_string(iter->name.module_name, 0, sizeof(uint16_t), out, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200839
840 /* name */
aPieceke99345d2021-09-30 12:49:59 +0200841 LY_CHECK_RET(lyb_write_string(iter->name.name, 0, sizeof(uint16_t), out, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200842
Michal Vasko60ea6352020-06-29 13:39:39 +0200843 /* format */
844 LY_CHECK_RET(lyb_write_number(iter->format, 1, out, lybctx));
845
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100846 /* value prefixes */
847 LY_CHECK_RET(lyb_print_prefix_data(out, iter->format, iter->val_prefix_data, lybctx));
848
Michal Vasko60ea6352020-06-29 13:39:39 +0200849 /* value */
aPieceke99345d2021-09-30 12:49:59 +0200850 LY_CHECK_RET(lyb_write_string(iter->value, 0, sizeof(uint64_t), out, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200851 }
852
853 return LY_SUCCESS;
854}
855
856/**
857 * @brief Print schema node hash.
858 *
859 * @param[in] out Out structure.
860 * @param[in] schema Schema node whose hash to print.
861 * @param[in,out] sibling_ht Cached hash table for these siblings, created if NULL.
862 * @param[in] lybctx LYB context.
863 * @return LY_ERR value.
864 */
865static LY_ERR
Michal Vasko8efac242023-03-30 08:24:56 +0200866lyb_print_schema_hash(struct ly_out *out, struct lysc_node *schema, struct ly_ht **sibling_ht, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200867{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200868 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +0200869 uint32_t i;
870 LYB_HASH hash;
871 struct lyd_lyb_sib_ht *sib_ht;
872 struct lysc_node *first_sibling;
873
874 if (!schema) {
875 /* opaque node, write empty hash */
876 hash = 0;
877 LY_CHECK_RET(lyb_write(out, &hash, sizeof hash, lybctx));
878 return LY_SUCCESS;
879 }
880
881 /* create whole sibling HT if not already created and saved */
882 if (!*sibling_ht) {
Michal Vasko1e0a80a2020-12-09 18:12:51 +0100883 /* get first schema data sibling */
884 first_sibling = (struct lysc_node *)lys_getnext(NULL, lysc_data_parent(schema), schema->module->compiled,
Michal Vaskod1e53b92021-01-28 13:11:06 +0100885 (schema->flags & LYS_IS_OUTPUT) ? LYS_GETNEXT_OUTPUT : 0);
Michal Vasko60ea6352020-06-29 13:39:39 +0200886 LY_ARRAY_FOR(lybctx->sib_hts, u) {
887 if (lybctx->sib_hts[u].first_sibling == first_sibling) {
888 /* we have already created a hash table for these siblings */
889 *sibling_ht = lybctx->sib_hts[u].ht;
890 break;
891 }
892 }
893
894 if (!*sibling_ht) {
895 /* we must create sibling hash table */
896 LY_CHECK_RET(lyb_hash_siblings(first_sibling, sibling_ht));
897
898 /* and save it */
899 LY_ARRAY_NEW_RET(lybctx->ctx, lybctx->sib_hts, sib_ht, LY_EMEM);
900
901 sib_ht->first_sibling = first_sibling;
902 sib_ht->ht = *sibling_ht;
903 }
904 }
905
906 /* get our hash */
907 LY_CHECK_RET(lyb_hash_find(*sibling_ht, schema, &hash));
908
909 /* write the hash */
910 LY_CHECK_RET(lyb_write(out, &hash, sizeof hash, lybctx));
911
912 if (hash & LYB_HASH_COLLISION_ID) {
913 /* no collision for this hash, we are done */
914 return LY_SUCCESS;
915 }
916
917 /* written hash was a collision, write also all the preceding hashes */
Radek Krejci1e008d22020-08-17 11:37:37 +0200918 for (i = 0; !(hash & (LYB_HASH_COLLISION_ID >> i)); ++i) {}
Michal Vasko60ea6352020-06-29 13:39:39 +0200919
Michal Vaskod989ba02020-08-24 10:59:24 +0200920 for ( ; i; --i) {
Michal Vasko11f76c82021-04-15 14:36:14 +0200921 hash = lyb_get_hash(schema, i - 1);
Michal Vasko60ea6352020-06-29 13:39:39 +0200922 if (!hash) {
923 return LY_EINT;
924 }
925 assert(hash & (LYB_HASH_COLLISION_ID >> (i - 1)));
926
927 LY_CHECK_RET(lyb_write(out, &hash, sizeof hash, lybctx));
928 }
929
930 return LY_SUCCESS;
931}
932
933/**
aPiecek6fdac2f2021-09-10 11:21:10 +0200934 * @brief Print header for non-opaq node.
935 *
936 * @param[in] out Out structure.
937 * @param[in] node Current data node to print.
938 * @param[in] lybctx LYB context.
939 * @return LY_ERR value.
940 */
941static LY_ERR
942lyb_print_node_header(struct ly_out *out, const struct lyd_node *node, struct lyd_lyb_ctx *lybctx)
943{
944 /* write any metadata */
945 LY_CHECK_RET(lyb_print_metadata(out, node, lybctx));
946
947 /* write node flags */
948 LY_CHECK_RET(lyb_write_number(node->flags, sizeof node->flags, out, lybctx->lybctx));
949
950 return LY_SUCCESS;
951}
952
953/**
Michal Vasko9883a3e2022-03-31 12:16:00 +0200954 * @brief Print LYB node type.
955 *
956 * @param[in] out Out structure.
957 * @param[in] node Current data node to print.
958 * @param[in] lybctx LYB context.
959 * @return LY_ERR value.
960 */
961static LY_ERR
962lyb_print_lyb_type(struct ly_out *out, const struct lyd_node *node, struct lyd_lyb_ctx *lybctx)
963{
964 enum lylyb_node_type lyb_type;
965
966 if (node->flags & LYD_EXT) {
967 assert(node->schema);
968 lyb_type = LYB_NODE_EXT;
969 } else if (!node->schema) {
970 lyb_type = LYB_NODE_OPAQ;
971 } else if (!lysc_data_parent(node->schema)) {
972 lyb_type = LYB_NODE_TOP;
973 } else {
974 lyb_type = LYB_NODE_CHILD;
975 }
976
977 LY_CHECK_RET(lyb_write_number(lyb_type, 1, out, lybctx->lybctx));
978
979 return LY_SUCCESS;
980}
981
982/**
aPiecek5777f122021-09-10 10:26:23 +0200983 * @brief Print inner node.
Michal Vasko60ea6352020-06-29 13:39:39 +0200984 *
985 * @param[in] out Out structure.
aPiecek5777f122021-09-10 10:26:23 +0200986 * @param[in] node Current data node to print.
Michal Vasko60ea6352020-06-29 13:39:39 +0200987 * @param[in] lybctx LYB context.
988 * @return LY_ERR value.
989 */
990static LY_ERR
aPiecek5777f122021-09-10 10:26:23 +0200991lyb_print_node_inner(struct ly_out *out, const struct lyd_node *node, struct lyd_lyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200992{
aPiecek586b1d22021-09-10 09:00:57 +0200993 /* write necessary basic data */
994 LY_CHECK_RET(lyb_print_node_header(out, node, lybctx));
aPiecek570d7ed2021-09-10 07:15:35 +0200995
aPiecek5777f122021-09-10 10:26:23 +0200996 /* recursively write all the descendants */
997 LY_CHECK_RET(lyb_print_siblings(out, lyd_child(node), lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200998
aPiecek570d7ed2021-09-10 07:15:35 +0200999 return LY_SUCCESS;
1000}
1001
1002/**
aPiecek6fdac2f2021-09-10 11:21:10 +02001003 * @brief Print opaque node and its descendants.
1004 *
1005 * @param[in] out Out structure.
1006 * @param[in] opaq Node to print.
1007 * @param[in] lyd_lybctx LYB context.
1008 * @return LY_ERR value.
1009 */
1010static LY_ERR
1011lyb_print_node_opaq(struct ly_out *out, const struct lyd_node_opaq *opaq, struct lyd_lyb_ctx *lyd_lybctx)
1012{
1013 struct lylyb_ctx *lybctx = lyd_lybctx->lybctx;
1014
1015 /* write attributes */
1016 LY_CHECK_RET(lyb_print_attributes(out, opaq, lybctx));
1017
1018 /* write node flags */
1019 LY_CHECK_RET(lyb_write_number(opaq->flags, sizeof opaq->flags, out, lybctx));
1020
1021 /* prefix */
aPieceke99345d2021-09-30 12:49:59 +02001022 LY_CHECK_RET(lyb_write_string(opaq->name.prefix, 0, sizeof(uint16_t), out, lybctx));
aPiecek6fdac2f2021-09-10 11:21:10 +02001023
1024 /* module reference */
aPieceke99345d2021-09-30 12:49:59 +02001025 LY_CHECK_RET(lyb_write_string(opaq->name.module_name, 0, sizeof(uint16_t), out, lybctx));
aPiecek6fdac2f2021-09-10 11:21:10 +02001026
1027 /* name */
aPieceke99345d2021-09-30 12:49:59 +02001028 LY_CHECK_RET(lyb_write_string(opaq->name.name, 0, sizeof(uint16_t), out, lybctx));
aPiecek6fdac2f2021-09-10 11:21:10 +02001029
1030 /* value */
aPieceke99345d2021-09-30 12:49:59 +02001031 LY_CHECK_RET(lyb_write_string(opaq->value, 0, sizeof(uint64_t), out, lybctx));
aPiecek6fdac2f2021-09-10 11:21:10 +02001032
1033 /* format */
1034 LY_CHECK_RET(lyb_write_number(opaq->format, 1, out, lybctx));
1035
1036 /* value prefixes */
1037 LY_CHECK_RET(lyb_print_prefix_data(out, opaq->format, opaq->val_prefix_data, lybctx));
1038
1039 /* recursively write all the descendants */
1040 LY_CHECK_RET(lyb_print_siblings(out, opaq->child, lyd_lybctx));
1041
1042 return LY_SUCCESS;
1043}
1044
1045/**
1046 * @brief Print anydata or anyxml node.
1047 *
1048 * @param[in] anydata Node to print.
1049 * @param[in] out Out structure.
1050 * @param[in] lyd_lybctx LYB context.
1051 * @return LY_ERR value.
1052 */
1053static LY_ERR
1054lyb_print_node_any(struct ly_out *out, struct lyd_node_any *anydata, struct lyd_lyb_ctx *lyd_lybctx)
1055{
1056 LY_ERR ret = LY_SUCCESS;
1057 LYD_ANYDATA_VALUETYPE value_type;
1058 int len;
1059 char *buf = NULL;
1060 const char *str;
1061 struct ly_out *out2 = NULL;
1062 struct lylyb_ctx *lybctx = lyd_lybctx->lybctx;
1063
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001064 if ((anydata->schema->nodetype == LYS_ANYDATA) && (anydata->value_type != LYD_ANYDATA_DATATREE)) {
1065 LOGINT_RET(lybctx->ctx);
1066 }
1067
aPiecek6fdac2f2021-09-10 11:21:10 +02001068 if (anydata->value_type == LYD_ANYDATA_DATATREE) {
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001069 /* will be printed as a nested LYB data tree because the used modules need to be written */
aPiecek6fdac2f2021-09-10 11:21:10 +02001070 value_type = LYD_ANYDATA_LYB;
1071 } else {
1072 value_type = anydata->value_type;
1073 }
1074
1075 /* write necessary basic data */
1076 LY_CHECK_RET(lyb_print_node_header(out, (struct lyd_node *)anydata, lyd_lybctx));
1077
1078 /* first byte is type */
1079 LY_CHECK_GOTO(ret = lyb_write_number(value_type, sizeof value_type, out, lybctx), cleanup);
1080
1081 if (anydata->value_type == LYD_ANYDATA_DATATREE) {
1082 /* print LYB data tree to memory */
1083 LY_CHECK_GOTO(ret = ly_out_new_memory(&buf, 0, &out2), cleanup);
1084 LY_CHECK_GOTO(ret = lyb_print_data(out2, anydata->value.tree, LYD_PRINT_WITHSIBLINGS), cleanup);
1085
1086 len = lyd_lyb_data_length(buf);
1087 assert(len != -1);
1088 str = buf;
1089 } else if (anydata->value_type == LYD_ANYDATA_LYB) {
1090 len = lyd_lyb_data_length(anydata->value.mem);
1091 assert(len != -1);
1092 str = anydata->value.mem;
1093 } else {
1094 len = strlen(anydata->value.str);
1095 str = anydata->value.str;
1096 }
1097
1098 /* followed by the content */
aPieceke99345d2021-09-30 12:49:59 +02001099 LY_CHECK_GOTO(ret = lyb_write_string(str, (size_t)len, sizeof(uint64_t), out, lybctx), cleanup);
aPiecek6fdac2f2021-09-10 11:21:10 +02001100
1101cleanup:
1102 ly_out_free(out2, NULL, 1);
1103 return ret;
1104}
1105
1106/**
aPiecek8a555d72021-09-10 10:15:26 +02001107 * @brief Print leaf node.
1108 *
1109 * @param[in] out Out structure.
1110 * @param[in] node Current data node to print.
1111 * @param[in] lybctx LYB context.
1112 * @return LY_ERR value.
1113 */
1114static LY_ERR
1115lyb_print_node_leaf(struct ly_out *out, const struct lyd_node *node, struct lyd_lyb_ctx *lybctx)
1116{
1117 /* write necessary basic data */
1118 LY_CHECK_RET(lyb_print_node_header(out, node, lybctx));
1119
1120 /* write term value */
1121 LY_CHECK_RET(lyb_print_term_value((struct lyd_node_term *)node, out, lybctx->lybctx));
1122
1123 return LY_SUCCESS;
1124}
1125
1126/**
aPiecek5777f122021-09-10 10:26:23 +02001127 * @brief Print all leaflist nodes which belong to same schema.
1128 *
1129 * @param[in] out Out structure.
1130 * @param[in] node Current data node to print.
1131 * @param[in] lybctx LYB context.
1132 * @param[out] printed_node Last node that was printed by this function.
1133 * @return LY_ERR value.
1134 */
1135static LY_ERR
1136lyb_print_node_leaflist(struct ly_out *out, const struct lyd_node *node, struct lyd_lyb_ctx *lybctx,
1137 const struct lyd_node **printed_node)
1138{
1139 const struct lysc_node *schema;
1140
1141 /* register a new sibling */
1142 LY_CHECK_RET(lyb_write_start_siblings(out, lybctx->lybctx));
1143
1144 schema = node->schema;
1145
1146 /* write all the siblings */
1147 LY_LIST_FOR(node, node) {
1148 if (schema != node->schema) {
1149 /* all leaflist nodes was printed */
1150 break;
1151 }
1152
1153 /* write leaf data */
1154 LY_CHECK_RET(lyb_print_node_leaf(out, node, lybctx));
1155 *printed_node = node;
1156 }
1157
1158 /* finish this sibling */
1159 LY_CHECK_RET(lyb_write_stop_siblings(out, lybctx->lybctx));
1160
1161 return LY_SUCCESS;
1162}
1163
1164/**
aPiecek77d3e962021-09-10 10:33:59 +02001165 * @brief Print all list nodes which belong to same schema.
1166 *
1167 * @param[in] out Out structure.
1168 * @param[in] node Current data node to print.
1169 * @param[in] lybctx LYB context.
1170 * @param[out] printed_node Last node that was printed by this function.
1171 * @return LY_ERR value.
1172 */
1173static LY_ERR
1174lyb_print_node_list(struct ly_out *out, const struct lyd_node *node, struct lyd_lyb_ctx *lybctx,
1175 const struct lyd_node **printed_node)
1176{
1177 const struct lysc_node *schema;
1178
1179 /* register a new sibling */
1180 LY_CHECK_RET(lyb_write_start_siblings(out, lybctx->lybctx));
1181
1182 schema = node->schema;
1183
1184 LY_LIST_FOR(node, node) {
1185 if (schema != node->schema) {
1186 /* all list nodes was printed */
1187 break;
1188 }
1189
1190 /* write necessary basic data */
1191 LY_CHECK_RET(lyb_print_node_header(out, node, lybctx));
1192
1193 /* recursively write all the descendants */
1194 LY_CHECK_RET(lyb_print_siblings(out, lyd_child(node), lybctx));
1195
1196 *printed_node = node;
1197 }
1198
1199 /* finish this sibling */
1200 LY_CHECK_RET(lyb_write_stop_siblings(out, lybctx->lybctx));
1201
1202 return LY_SUCCESS;
1203}
1204
1205/**
aPiecek17737b52021-09-21 12:29:52 +02001206 * @brief Print node.
1207 *
1208 * @param[in] out Out structure.
1209 * @param[in,out] printed_node Current data node to print. Sets to the last printed node.
1210 * @param[in,out] sibling_ht Cached hash table for these siblings, created if NULL.
1211 * @param[in] lybctx LYB context.
1212 * @return LY_ERR value.
1213 */
1214static LY_ERR
Michal Vasko8efac242023-03-30 08:24:56 +02001215lyb_print_node(struct ly_out *out, const struct lyd_node **printed_node, struct ly_ht **sibling_ht,
aPiecek17737b52021-09-21 12:29:52 +02001216 struct lyd_lyb_ctx *lybctx)
1217{
1218 const struct lyd_node *node = *printed_node;
1219
Michal Vasko9883a3e2022-03-31 12:16:00 +02001220 /* write node type */
1221 LY_CHECK_RET(lyb_print_lyb_type(out, node, lybctx));
1222
1223 /* write model info first */
1224 if (node->schema && ((node->flags & LYD_EXT) || !lysc_data_parent(node->schema))) {
Michal Vasko78041d12022-11-29 14:11:07 +01001225 LY_CHECK_RET(lyb_print_model(out, node->schema->module, 0, lybctx->lybctx));
aPiecek17737b52021-09-21 12:29:52 +02001226 }
1227
Michal Vasko9883a3e2022-03-31 12:16:00 +02001228 if (node->flags & LYD_EXT) {
1229 /* write schema node name */
1230 LY_CHECK_RET(lyb_write_string(node->schema->name, 0, sizeof(uint16_t), out, lybctx->lybctx));
1231 } else {
1232 /* write schema hash */
1233 LY_CHECK_RET(lyb_print_schema_hash(out, (struct lysc_node *)node->schema, sibling_ht, lybctx->lybctx));
1234 }
aPiecek17737b52021-09-21 12:29:52 +02001235
1236 if (!node->schema) {
1237 LY_CHECK_RET(lyb_print_node_opaq(out, (struct lyd_node_opaq *)node, lybctx));
1238 } else if (node->schema->nodetype & LYS_LEAFLIST) {
1239 LY_CHECK_RET(lyb_print_node_leaflist(out, node, lybctx, &node));
1240 } else if (node->schema->nodetype == LYS_LIST) {
1241 LY_CHECK_RET(lyb_print_node_list(out, node, lybctx, &node));
1242 } else if (node->schema->nodetype & LYD_NODE_ANY) {
1243 LY_CHECK_RET(lyb_print_node_any(out, (struct lyd_node_any *)node, lybctx));
1244 } else if (node->schema->nodetype & LYD_NODE_INNER) {
1245 LY_CHECK_RET(lyb_print_node_inner(out, node, lybctx));
1246 } else {
1247 LY_CHECK_RET(lyb_print_node_leaf(out, node, lybctx));
1248 }
1249
1250 *printed_node = node;
1251
1252 return LY_SUCCESS;
1253}
1254
1255/**
aPiecek570d7ed2021-09-10 07:15:35 +02001256 * @brief Print siblings.
1257 *
1258 * @param[in] out Out structure.
1259 * @param[in] node Current data node to print.
1260 * @param[in] lybctx LYB context.
1261 * @return LY_ERR value.
1262 */
1263static LY_ERR
1264lyb_print_siblings(struct ly_out *out, const struct lyd_node *node, struct lyd_lyb_ctx *lybctx)
1265{
Michal Vasko8efac242023-03-30 08:24:56 +02001266 struct ly_ht *sibling_ht = NULL;
aPiecek570d7ed2021-09-10 07:15:35 +02001267 const struct lys_module *prev_mod = NULL;
aPiecek570d7ed2021-09-10 07:15:35 +02001268
1269 LY_CHECK_RET(lyb_write_start_siblings(out, lybctx->lybctx));
1270
Michal Vaskod92c8512023-02-01 12:31:56 +01001271 /* write all the siblings */
1272 LY_LIST_FOR(node, node) {
1273 /* do not reuse top-level sibling hash tables from different modules */
1274 if (!node->schema || (!lysc_data_parent(node->schema) && (node->schema->module != prev_mod))) {
1275 sibling_ht = NULL;
1276 prev_mod = node->schema ? node->schema->module : NULL;
aPiecek570d7ed2021-09-10 07:15:35 +02001277 }
Michal Vaskod92c8512023-02-01 12:31:56 +01001278
1279 LY_CHECK_RET(lyb_print_node(out, &node, &sibling_ht, lybctx));
1280
1281 if (!lyd_parent(node) && !(lybctx->print_options & LYD_PRINT_WITHSIBLINGS)) {
1282 break;
aPiecek570d7ed2021-09-10 07:15:35 +02001283 }
1284 }
1285
1286 LY_CHECK_RET(lyb_write_stop_siblings(out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +02001287
1288 return LY_SUCCESS;
1289}
1290
1291LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001292lyb_print_data(struct ly_out *out, const struct lyd_node *root, uint32_t options)
Michal Vasko60ea6352020-06-29 13:39:39 +02001293{
1294 LY_ERR ret = LY_SUCCESS;
1295 uint8_t zero = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +02001296 struct lyd_lyb_ctx *lybctx;
Michal Vaskob7be7a82020-08-20 09:09:04 +02001297 const struct ly_ctx *ctx = root ? LYD_CTX(root) : NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +02001298
Radek Krejci1798aae2020-07-14 13:26:06 +02001299 lybctx = calloc(1, sizeof *lybctx);
Michal Vasko82df24a2023-02-21 15:02:11 +01001300 LY_CHECK_ERR_GOTO(!lybctx, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001301 lybctx->lybctx = calloc(1, sizeof *lybctx->lybctx);
Michal Vasko82df24a2023-02-21 15:02:11 +01001302 LY_CHECK_ERR_GOTO(!lybctx->lybctx, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001303
1304 lybctx->print_options = options;
Michal Vasko60ea6352020-06-29 13:39:39 +02001305 if (root) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001306 lybctx->lybctx->ctx = ctx;
Michal Vasko60ea6352020-06-29 13:39:39 +02001307
1308 if (root->schema && lysc_data_parent(root->schema)) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001309 LOGERR(lybctx->lybctx->ctx, LY_EINVAL, "LYB printer supports only printing top-level nodes.");
Michal Vasko9acaf492020-08-13 09:05:58 +02001310 ret = LY_EINVAL;
1311 goto cleanup;
Michal Vasko60ea6352020-06-29 13:39:39 +02001312 }
1313 }
1314
1315 /* LYB magic number */
Michal Vasko63f3d842020-07-08 10:10:14 +02001316 LY_CHECK_GOTO(ret = lyb_print_magic_number(out), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001317
1318 /* LYB header */
Michal Vasko63f3d842020-07-08 10:10:14 +02001319 LY_CHECK_GOTO(ret = lyb_print_header(out), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001320
1321 /* all used models */
Radek Krejci1798aae2020-07-14 13:26:06 +02001322 LY_CHECK_GOTO(ret = lyb_print_data_models(out, root, lybctx->lybctx), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001323
aPiecek570d7ed2021-09-10 07:15:35 +02001324 ret = lyb_print_siblings(out, root, lybctx);
1325 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001326
1327 /* ending zero byte */
Radek Krejci1798aae2020-07-14 13:26:06 +02001328 LY_CHECK_GOTO(ret = lyb_write(out, &zero, sizeof zero, lybctx->lybctx), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001329
1330cleanup:
Radek Krejci1798aae2020-07-14 13:26:06 +02001331 lyd_lyb_ctx_free((struct lyd_ctx *)lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001332 return ret;
1333}