blob: 860d5a05f3c433e92a73aa0cc848934d37d47972 [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 *
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>
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>
Radek Krejciad97c5f2020-06-30 09:19:28 +020022#include <sys/types.h>
Michal Vasko60ea6352020-06-29 13:39:39 +020023
24#include "common.h"
25#include "compat.h"
Radek Krejciad97c5f2020-06-30 09:19:28 +020026#include "context.h"
27#include "hash_table.h"
Michal Vasko60ea6352020-06-29 13:39:39 +020028#include "log.h"
Radek Krejci7931b192020-06-25 17:05:03 +020029#include "parser_data.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020030#include "out_internal.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"
Michal Vasko60ea6352020-06-29 13:39:39 +020035#include "tree_data_internal.h"
36#include "tree_schema.h"
37#include "tree_schema_internal.h"
38
39/**
40 * @brief Hash table equal callback for checking hash equality only.
Radek Krejci857189e2020-09-01 13:26:36 +020041 *
42 * Implementation of ::values_equal_cb.
Michal Vasko60ea6352020-06-29 13:39:39 +020043 */
Radek Krejci857189e2020-09-01 13:26:36 +020044static ly_bool
45lyb_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 +020046{
47 /* for this purpose, if hash matches, the value does also, we do not want 2 values to have the same hash */
48 return 1;
49}
50
51/**
52 * @brief Hash table equal callback for checking value pointer equality only.
Radek Krejci857189e2020-09-01 13:26:36 +020053 *
54 * Implementation of ::values_equal_cb.
Michal Vasko60ea6352020-06-29 13:39:39 +020055 */
Radek Krejci857189e2020-09-01 13:26:36 +020056static ly_bool
57lyb_ptr_equal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
Michal Vasko60ea6352020-06-29 13:39:39 +020058{
59 struct lysc_node *val1 = *(struct lysc_node **)val1_p;
60 struct lysc_node *val2 = *(struct lysc_node **)val2_p;
61
62 if (val1 == val2) {
63 return 1;
64 }
65 return 0;
66}
67
68/**
69 * @brief Check that sibling collision hash is safe to insert into hash table.
70 *
71 * @param[in] ht Hash table.
72 * @param[in] sibling Hashed sibling.
73 * @param[in] ht_col_id Sibling hash collision ID.
74 * @param[in] compare_col_id Last collision ID to compare with.
75 * @return LY_SUCCESS when the whole hash sequence does not collide,
76 * @return LY_EEXIST when the whole hash sequence sollides.
77 */
78static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +020079lyb_hash_sequence_check(struct hash_table *ht, struct lysc_node *sibling, LYB_HASH ht_col_id, LYB_HASH compare_col_id)
Michal Vasko60ea6352020-06-29 13:39:39 +020080{
Michal Vasko60ea6352020-06-29 13:39:39 +020081 struct lysc_node **col_node;
82
83 /* get the first node inserted with last hash col ID ht_col_id */
84 if (lyht_find(ht, &sibling, lyb_hash(sibling, ht_col_id), (void **)&col_node)) {
85 /* there is none. valid situation */
86 return LY_SUCCESS;
87 }
88
89 lyht_set_cb(ht, lyb_ptr_equal_cb);
90 do {
Radek Krejci1deb5be2020-08-26 16:43:36 +020091 int64_t j;
92 for (j = (int64_t)compare_col_id; j > -1; --j) {
Michal Vasko60ea6352020-06-29 13:39:39 +020093 if (lyb_hash(sibling, j) != lyb_hash(*col_node, j)) {
94 /* one non-colliding hash */
95 break;
96 }
97 }
98 if (j == -1) {
99 /* all whole hash sequences of nodes inserted with last hash col ID compare_col_id collide */
100 lyht_set_cb(ht, lyb_hash_equal_cb);
101 return LY_EEXIST;
102 }
103
104 /* get next node inserted with last hash col ID ht_col_id */
105 } while (!lyht_find_next(ht, col_node, lyb_hash(*col_node, ht_col_id), (void **)&col_node));
106
107 lyht_set_cb(ht, lyb_hash_equal_cb);
108 return LY_SUCCESS;
109}
110
111/**
112 * @brief Hash all the siblings and add them also into a separate hash table.
113 *
114 * @param[in] sibling Any sibling in all the siblings on one level.
115 * @param[out] ht_p Created hash table.
116 * @return LY_ERR value.
117 */
118static LY_ERR
119lyb_hash_siblings(struct lysc_node *sibling, struct hash_table **ht_p)
120{
121 struct hash_table *ht;
122 const struct lysc_node *parent;
123 const struct lys_module *mod;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200124 LYB_HASH i;
Michal Vasko60ea6352020-06-29 13:39:39 +0200125
126 ht = lyht_new(1, sizeof(struct lysc_node *), lyb_hash_equal_cb, NULL, 1);
127 LY_CHECK_ERR_RET(!ht, LOGMEM(sibling->module->ctx), LY_EMEM);
128
129 parent = lysc_data_parent(sibling);
130 mod = sibling->module;
131
132 sibling = NULL;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100133 while ((sibling = (struct lysc_node *)lys_getnext(sibling, parent, mod->compiled, 0))) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200134 /* find the first non-colliding hash (or specifically non-colliding hash sequence) */
135 for (i = 0; i < LYB_HASH_BITS; ++i) {
136 /* check that we are not colliding with nodes inserted with a lower collision ID than ours */
Radek Krejci1deb5be2020-08-26 16:43:36 +0200137 int64_t j;
138 for (j = (int64_t)i - 1; j > -1; --j) {
139 if (lyb_hash_sequence_check(ht, sibling, (LYB_HASH)j, i)) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200140 break;
141 }
142 }
143 if (j > -1) {
144 /* some check failed, we must use a higher collision ID */
145 continue;
146 }
147
148 /* try to insert node with the current collision ID */
149 if (!lyht_insert_with_resize_cb(ht, &sibling, lyb_hash(sibling, i), lyb_ptr_equal_cb, NULL)) {
150 /* success, no collision */
151 break;
152 }
153
154 /* make sure we really cannot insert it with this hash col ID (meaning the whole hash sequence is colliding) */
155 if (i && !lyb_hash_sequence_check(ht, sibling, i, i)) {
156 /* it can be inserted after all, even though there is already a node with the same last collision ID */
157 lyht_set_cb(ht, lyb_ptr_equal_cb);
158 if (lyht_insert(ht, &sibling, lyb_hash(sibling, i), NULL)) {
159 LOGINT(sibling->module->ctx);
160 lyht_set_cb(ht, lyb_hash_equal_cb);
161 lyht_free(ht);
162 return LY_EINT;
163 }
164 lyht_set_cb(ht, lyb_hash_equal_cb);
165 break;
166 }
167 /* there is still another colliding schema node with the same hash sequence, try higher collision ID */
168 }
169
170 if (i == LYB_HASH_BITS) {
171 /* wow */
172 LOGINT(sibling->module->ctx);
173 lyht_free(ht);
174 return LY_EINT;
175 }
176 }
177
178 /* change val equal callback so that the HT is usable for finding value hashes */
179 lyht_set_cb(ht, lyb_ptr_equal_cb);
180
181 *ht_p = ht;
182 return LY_SUCCESS;
183}
184
185/**
186 * @brief Find node hash in a hash table.
187 *
188 * @param[in] ht Hash table to search in.
189 * @param[in] node Node to find.
190 * @param[out] hash_p First non-colliding hash found.
191 * @return LY_ERR value.
192 */
193static LY_ERR
194lyb_hash_find(struct hash_table *ht, struct lysc_node *node, LYB_HASH *hash_p)
195{
196 LYB_HASH hash;
197 uint32_t i;
198
199 for (i = 0; i < LYB_HASH_BITS; ++i) {
200 hash = lyb_hash(node, i);
201 if (!hash) {
202 LOGINT_RET(node->module->ctx);
203 }
204
205 if (!lyht_find(ht, &node, hash, NULL)) {
206 /* success, no collision */
207 break;
208 }
209 }
210 /* cannot happen, we already calculated the hash */
211 if (i == LYB_HASH_BITS) {
212 LOGINT_RET(node->module->ctx);
213 }
214
215 *hash_p = hash;
216 return LY_SUCCESS;
217}
218
219/**
220 * @brief Write LYB data fully handling the metadata.
221 *
222 * @param[in] out Out structure.
223 * @param[in] buf Source buffer.
224 * @param[in] count Number of bytes to write.
225 * @param[in] lybctx LYB context.
226 * @return LY_ERR value.
227 */
228static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200229lyb_write(struct ly_out *out, const uint8_t *buf, size_t count, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200230{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200231 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +0200232 struct lyd_lyb_subtree *full, *iter;
Michal Vasko5233e962020-08-14 14:26:20 +0200233 size_t to_write;
Michal Vasko60ea6352020-06-29 13:39:39 +0200234 uint8_t meta_buf[LYB_META_BYTES];
235
236 while (1) {
237 /* check for full data chunks */
238 to_write = count;
239 full = NULL;
240 LY_ARRAY_FOR(lybctx->subtrees, u) {
241 /* we want the innermost chunks resolved first, so replace previous full chunks */
242 if (lybctx->subtrees[u].written + to_write >= LYB_SIZE_MAX) {
243 /* full chunk, do not write more than allowed */
244 to_write = LYB_SIZE_MAX - lybctx->subtrees[u].written;
245 full = &lybctx->subtrees[u];
246 }
247 }
248
249 if (!full && !count) {
250 break;
251 }
252
253 /* we are actually writing some data, not just finishing another chunk */
254 if (to_write) {
Michal Vasko5233e962020-08-14 14:26:20 +0200255 LY_CHECK_RET(ly_write_(out, (char *)buf, to_write));
Michal Vasko60ea6352020-06-29 13:39:39 +0200256
257 LY_ARRAY_FOR(lybctx->subtrees, u) {
258 /* increase all written counters */
Michal Vasko5233e962020-08-14 14:26:20 +0200259 lybctx->subtrees[u].written += to_write;
Michal Vasko60ea6352020-06-29 13:39:39 +0200260 assert(lybctx->subtrees[u].written <= LYB_SIZE_MAX);
261 }
262 /* decrease count/buf */
Michal Vasko5233e962020-08-14 14:26:20 +0200263 count -= to_write;
264 buf += to_write;
Michal Vasko60ea6352020-06-29 13:39:39 +0200265 }
266
267 if (full) {
268 /* write the meta information (inner chunk count and chunk size) */
269 meta_buf[0] = full->written & 0xFF;
270 meta_buf[1] = full->inner_chunks & 0xFF;
Michal Vasko5233e962020-08-14 14:26:20 +0200271 LY_CHECK_RET(ly_write_skipped(out, full->position, (char *)meta_buf, LYB_META_BYTES));
Michal Vasko60ea6352020-06-29 13:39:39 +0200272
273 /* zero written and inner chunks */
274 full->written = 0;
275 full->inner_chunks = 0;
276
277 /* skip space for another chunk size */
Michal Vasko5233e962020-08-14 14:26:20 +0200278 LY_CHECK_RET(ly_write_skip(out, LYB_META_BYTES, &full->position));
Michal Vasko60ea6352020-06-29 13:39:39 +0200279
280 /* increase inner chunk count */
281 for (iter = &lybctx->subtrees[0]; iter != full; ++iter) {
282 if (iter->inner_chunks == LYB_INCHUNK_MAX) {
283 LOGINT(lybctx->ctx);
284 return LY_EINT;
285 }
286 ++iter->inner_chunks;
287 }
288 }
289 }
290
291 return LY_SUCCESS;
292}
293
294/**
295 * @brief Stop the current subtree - write its final metadata.
296 *
297 * @param[in] out Out structure.
298 * @param[in] lybctx LYB context.
299 * @return LY_ERR value.
300 */
301static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200302lyb_write_stop_subtree(struct ly_out *out, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200303{
Michal Vasko60ea6352020-06-29 13:39:39 +0200304 uint8_t meta_buf[LYB_META_BYTES];
305
306 /* write the meta chunk information */
307 meta_buf[0] = LYB_LAST_SUBTREE(lybctx).written & 0xFF;
308 meta_buf[1] = LYB_LAST_SUBTREE(lybctx).inner_chunks & 0xFF;
Michal Vasko5233e962020-08-14 14:26:20 +0200309 LY_CHECK_RET(ly_write_skipped(out, LYB_LAST_SUBTREE(lybctx).position, (char *)&meta_buf, LYB_META_BYTES));
Michal Vasko60ea6352020-06-29 13:39:39 +0200310
311 LY_ARRAY_DECREMENT(lybctx->subtrees);
312 return LY_SUCCESS;
313}
314
315/**
316 * @brief Start a new subtree - skip bytes for its metadata.
317 *
318 * @param[in] out Out structure.
319 * @param[in] lybctx LYB context.
320 * @return LY_ERR value.
321 */
322static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200323lyb_write_start_subtree(struct ly_out *out, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200324{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200325 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +0200326
327 if (!lybctx->subtrees) {
Radek Krejcif6d14cb2020-07-02 16:11:45 +0200328 assert(lybctx->subtree_size == 0);
Michal Vasko60ea6352020-06-29 13:39:39 +0200329 u = 0;
330 } else {
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200331 u = LY_ARRAY_COUNT(lybctx->subtrees);
Michal Vasko60ea6352020-06-29 13:39:39 +0200332 }
333 if (u == lybctx->subtree_size) {
334 LY_ARRAY_CREATE_RET(lybctx->ctx, lybctx->subtrees, u + LYB_SUBTREE_STEP, LY_EMEM);
335 lybctx->subtree_size = u + LYB_SUBTREE_STEP;
336 }
337
338 LY_ARRAY_INCREMENT(lybctx->subtrees);
339 LYB_LAST_SUBTREE(lybctx).written = 0;
340 LYB_LAST_SUBTREE(lybctx).inner_chunks = 0;
341
342 /* another inner chunk */
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200343 for (u = 0; u < LY_ARRAY_COUNT(lybctx->subtrees) - 1; ++u) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200344 if (lybctx->subtrees[u].inner_chunks == LYB_INCHUNK_MAX) {
345 LOGINT(lybctx->ctx);
Michal Vasko5233e962020-08-14 14:26:20 +0200346 return LY_EINT;
Michal Vasko60ea6352020-06-29 13:39:39 +0200347 }
348 ++lybctx->subtrees[u].inner_chunks;
349 }
350
Michal Vasko5233e962020-08-14 14:26:20 +0200351 LY_CHECK_RET(ly_write_skip(out, LYB_META_BYTES, &LYB_LAST_SUBTREE(lybctx).position));
Michal Vasko60ea6352020-06-29 13:39:39 +0200352
353 return LY_SUCCESS;
354}
355
356/**
357 * @brief Write a number.
358 *
359 * @param[in] num Number to write.
360 * @param[in] bytes Actual accessible bytes of @p num.
361 * @param[in] out Out structure.
362 * @param[in] lybctx LYB context.
363 * @return LY_ERR value.
364 */
365static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200366lyb_write_number(uint64_t num, size_t bytes, struct ly_out *out, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200367{
368 /* correct byte order */
369 num = htole64(num);
370
371 return lyb_write(out, (uint8_t *)&num, bytes, lybctx);
372}
373
374/**
375 * @brief Write a string.
376 *
377 * @param[in] str String to write.
378 * @param[in] str_len Length of @p str.
379 * @param[in] with_length Whether to precede the string with its length.
380 * @param[in] out Out structure.
381 * @param[in] lybctx LYB context.
382 * @return LY_ERR value.
383 */
384static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200385lyb_write_string(const char *str, size_t str_len, ly_bool with_length, struct ly_out *out, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200386{
Michal Vasko60ea6352020-06-29 13:39:39 +0200387 if (!str) {
388 str = "";
Michal Vasko30667352020-08-13 09:06:14 +0200389 LY_CHECK_ERR_RET(str_len, LOGINT(lybctx->ctx), LY_EINT);
Michal Vasko60ea6352020-06-29 13:39:39 +0200390 }
391 if (!str_len) {
392 str_len = strlen(str);
393 }
394
395 if (with_length) {
396 /* print length on 2 bytes */
397 if (str_len > UINT16_MAX) {
398 LOGINT(lybctx->ctx);
399 return LY_EINT;
400 }
401 LY_CHECK_RET(lyb_write_number(str_len, 2, out, lybctx));
402 }
403
Michal Vasko63f3d842020-07-08 10:10:14 +0200404 LY_CHECK_RET(lyb_write(out, (const uint8_t *)str, str_len, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200405
406 return LY_SUCCESS;
407}
408
409/**
410 * @brief Print YANG module info.
411 *
412 * @param[in] out Out structure.
413 * @param[in] mod Module to print.
414 * @param[in] lybctx LYB context.
415 * @return LY_ERR value.
416 */
417static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200418lyb_print_model(struct ly_out *out, const struct lys_module *mod, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200419{
Michal Vasko60ea6352020-06-29 13:39:39 +0200420 uint16_t revision;
421
422 /* model name length and model name */
423 if (mod) {
424 LY_CHECK_RET(lyb_write_string(mod->name, 0, 1, out, lybctx));
425 } else {
426 LY_CHECK_RET(lyb_write_string("", 0, 1, out, lybctx));
427 }
428
429 /* model revision as XXXX XXXX XXXX XXXX (2B) (year is offset from 2000)
430 * YYYY YYYM MMMD DDDD */
431 revision = 0;
432 if (mod && mod->revision) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200433 int r = atoi(mod->revision);
Michal Vasko60ea6352020-06-29 13:39:39 +0200434 r -= 2000;
435 r <<= 9;
436
437 revision |= r;
438
439 r = atoi(mod->revision + 5);
440 r <<= 5;
441
442 revision |= r;
443
444 r = atoi(mod->revision + 8);
445
446 revision |= r;
447 }
448 LY_CHECK_RET(lyb_write_number(revision, sizeof revision, out, lybctx));
449
450 return LY_SUCCESS;
451}
452
453/**
454 * @brief Print all used YANG modules.
455 *
456 * @param[in] out Out structure.
457 * @param[in] root Data root.
458 * @param[in] lybctx LYB context.
459 * @return LY_ERR value.
460 */
461static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200462lyb_print_data_models(struct ly_out *out, const struct lyd_node *root, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200463{
464 struct ly_set *set;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200465 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +0200466 LY_ERR ret = LY_SUCCESS;
467 struct lys_module *mod;
468 const struct lyd_node *node;
469 uint32_t i;
470
Radek Krejciba03a5a2020-08-27 14:40:41 +0200471 LY_CHECK_RET(ly_set_new(&set));
Michal Vasko60ea6352020-06-29 13:39:39 +0200472
473 /* collect all data node modules */
474 LY_LIST_FOR(root, node) {
475 if (!node->schema) {
476 continue;
477 }
478
479 mod = node->schema->module;
Radek Krejciba03a5a2020-08-27 14:40:41 +0200480 ret = ly_set_add(set, mod, 0, NULL);
481 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200482
483 /* add also their modules deviating or augmenting them */
Michal Vasko7f45cf22020-10-01 12:49:44 +0200484 LY_ARRAY_FOR(mod->deviated_by, u) {
485 ret = ly_set_add(set, mod->deviated_by[u], 0, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200486 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200487 }
Michal Vasko7f45cf22020-10-01 12:49:44 +0200488 LY_ARRAY_FOR(mod->augmented_by, u) {
489 ret = ly_set_add(set, mod->augmented_by[u], 0, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200490 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200491 }
492 }
493
494 /* now write module count on 2 bytes */
495 LY_CHECK_GOTO(ret = lyb_write_number(set->count, 2, out, lybctx), cleanup);
496
497 /* and all the used models */
498 for (i = 0; i < set->count; ++i) {
499 LY_CHECK_GOTO(ret = lyb_print_model(out, set->objs[i], lybctx), cleanup);
500 }
501
502cleanup:
503 ly_set_free(set, NULL);
504 return ret;
505}
506
507/**
508 * @brief Print LYB magic number.
509 *
510 * @param[in] out Out structure.
Michal Vasko60ea6352020-06-29 13:39:39 +0200511 * @return LY_ERR value.
512 */
513static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200514lyb_print_magic_number(struct ly_out *out)
Michal Vasko60ea6352020-06-29 13:39:39 +0200515{
Michal Vasko60ea6352020-06-29 13:39:39 +0200516 uint32_t magic_number;
517
518 /* 'l', 'y', 'b' - 0x6c7962 */
519 ((char *)&magic_number)[0] = 'l';
520 ((char *)&magic_number)[1] = 'y';
521 ((char *)&magic_number)[2] = 'b';
522
Michal Vasko5233e962020-08-14 14:26:20 +0200523 LY_CHECK_RET(ly_write_(out, (char *)&magic_number, 3));
Michal Vasko60ea6352020-06-29 13:39:39 +0200524
525 return LY_SUCCESS;
526}
527
528/**
529 * @brief Print LYB header.
530 *
531 * @param[in] out Out structure.
Michal Vasko60ea6352020-06-29 13:39:39 +0200532 * @return LY_ERR value.
533 */
534static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200535lyb_print_header(struct ly_out *out)
Michal Vasko60ea6352020-06-29 13:39:39 +0200536{
Michal Vasko60ea6352020-06-29 13:39:39 +0200537 uint8_t byte = 0;
538
539 /* version, future flags */
540 byte |= LYB_VERSION_NUM;
541
Michal Vasko5233e962020-08-14 14:26:20 +0200542 LY_CHECK_RET(ly_write_(out, (char *)&byte, 1));
Michal Vasko60ea6352020-06-29 13:39:39 +0200543
544 return LY_SUCCESS;
545}
546
547/**
548 * @brief Print opaque prefixes.
549 *
550 * @param[in] out Out structure.
551 * @param[in] prefs Prefixes to print.
552 * @param[in] lybctx LYB context.
553 * @return LY_ERR value.
554 */
555static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200556lyb_print_opaq_prefixes(struct ly_out *out, const struct ly_prefix *prefs, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200557{
558 uint8_t count;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200559 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +0200560
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200561 if (prefs && (LY_ARRAY_COUNT(prefs) > UINT8_MAX)) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200562 LOGERR(lybctx->ctx, LY_EINT, "Maximum supported number of prefixes is %u.", UINT8_MAX);
563 return LY_EINT;
564 }
565
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200566 count = prefs ? LY_ARRAY_COUNT(prefs) : 0;
Michal Vasko60ea6352020-06-29 13:39:39 +0200567
568 /* write number of prefixes on 1 byte */
569 LY_CHECK_RET(lyb_write(out, &count, 1, lybctx));
570
571 /* write all the prefixes */
572 LY_ARRAY_FOR(prefs, u) {
573 /* prefix */
Radek Krejci1798aae2020-07-14 13:26:06 +0200574 LY_CHECK_RET(lyb_write_string(prefs[u].id, 0, 1, out, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200575
576 /* namespace */
Radek Krejci1798aae2020-07-14 13:26:06 +0200577 LY_CHECK_RET(lyb_write_string(prefs[u].module_name, 0, 1, out, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200578 }
579
580 return LY_SUCCESS;
581}
582
583/**
584 * @brief Print opaque node.
585 *
586 * @param[in] opaq Node to print.
587 * @param[in] out Out structure.
588 * @param[in] lybctx LYB context.
589 * @return LY_ERR value.
590 */
591static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200592lyb_print_opaq(struct lyd_node_opaq *opaq, struct ly_out *out, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200593{
594 /* prefix */
Radek Krejci1798aae2020-07-14 13:26:06 +0200595 LY_CHECK_RET(lyb_write_string(opaq->prefix.id, 0, 1, out, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200596
Radek Krejci1798aae2020-07-14 13:26:06 +0200597 /* module reference */
598 LY_CHECK_RET(lyb_write_string(opaq->prefix.module_name, 0, 1, out, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200599
600 /* name */
601 LY_CHECK_RET(lyb_write_string(opaq->name, 0, 1, out, lybctx));
602
603 /* value prefixes */
604 LY_CHECK_RET(lyb_print_opaq_prefixes(out, opaq->val_prefs, lybctx));
605
606 /* format */
607 LY_CHECK_RET(lyb_write_number(opaq->format, 1, out, lybctx));
608
609 /* value */
610 LY_CHECK_RET(lyb_write_string(opaq->value, 0, 0, out, lybctx));
611
612 return LY_SUCCESS;
613}
614
615/**
616 * @brief Print anydata node.
617 *
618 * @param[in] anydata Node to print.
619 * @param[in] out Out structure.
620 * @param[in] lybctx LYB context.
621 * @return LY_ERR value.
622 */
623static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200624lyb_print_anydata(struct lyd_node_any *anydata, struct ly_out *out, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200625{
626 LY_ERR ret = LY_SUCCESS;
627 LYD_ANYDATA_VALUETYPE value_type;
628 int len;
629 char *buf = NULL;
630 const char *str;
631 struct ly_out *out2 = NULL;
632
633 if (anydata->value_type == LYD_ANYDATA_DATATREE) {
634 /* will be printed as a nested LYB data tree */
635 value_type = LYD_ANYDATA_LYB;
636 } else {
637 value_type = anydata->value_type;
638 }
639
640 /* first byte is type */
641 LY_CHECK_GOTO(ret = lyb_write(out, (uint8_t *)&value_type, sizeof value_type, lybctx), cleanup);
642
643 if (anydata->value_type == LYD_ANYDATA_DATATREE) {
644 /* print LYB data tree to memory */
645 LY_CHECK_GOTO(ret = ly_out_new_memory(&buf, 0, &out2), cleanup);
Radek Krejci7931b192020-06-25 17:05:03 +0200646 LY_CHECK_GOTO(ret = lyb_print_data(out2, anydata->value.tree, LYD_PRINT_WITHSIBLINGS), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200647
648 len = lyd_lyb_data_length(buf);
649 assert(len != -1);
650 str = buf;
651 } else if (anydata->value_type == LYD_ANYDATA_LYB) {
652 len = lyd_lyb_data_length(anydata->value.mem);
653 assert(len != -1);
654 str = anydata->value.mem;
655 } else {
656 len = strlen(anydata->value.str);
657 str = anydata->value.str;
658 }
659
660 /* followed by the content */
661 LY_CHECK_GOTO(ret = lyb_write_string(str, (size_t)len, 0, out, lybctx), cleanup);
662
663cleanup:
664 ly_out_free(out2, NULL, 1);
665 return ret;
666}
667
668/**
669 * @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
Radek Krejci1798aae2020-07-14 13:26:06 +0200677lyb_print_term(struct lyd_node_term *term, struct ly_out *out, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200678{
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200679 /* print the value */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200680 return lyb_write_string(LYD_CANON_VALUE(term), 0, 0, out, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200681}
682
683/**
684 * @brief Print YANG node metadata.
685 *
686 * @param[in] out Out structure.
687 * @param[in] node Data node whose metadata to print.
688 * @param[in] lybctx LYB context.
689 * @return LY_ERR value.
690 */
691static LY_ERR
692lyb_print_metadata(struct ly_out *out, const struct lyd_node *node, struct lyd_lyb_ctx *lybctx)
693{
Michal Vasko60ea6352020-06-29 13:39:39 +0200694 uint8_t count = 0;
695 const struct lys_module *wd_mod = NULL;
696 struct lyd_meta *iter;
Michal Vasko60ea6352020-06-29 13:39:39 +0200697
698 /* with-defaults */
699 if (node->schema->nodetype & LYD_NODE_TERM) {
Radek Krejci7931b192020-06-25 17:05:03 +0200700 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 +0200701 ((lybctx->print_options & LYD_PRINT_WD_ALL_TAG) && lyd_is_default(node))) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200702 /* we have implicit OR explicit default node, print attribute only if context include with-defaults schema */
703 wd_mod = ly_ctx_get_module_latest(node->schema->module->ctx, "ietf-netconf-with-defaults");
704 }
705 }
706
707 /* count metadata */
708 if (wd_mod) {
709 ++count;
710 }
711 for (iter = node->meta; iter; iter = iter->next) {
712 if (count == UINT8_MAX) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200713 LOGERR(lybctx->lybctx->ctx, LY_EINT, "Maximum supported number of data node metadata is %u.", UINT8_MAX);
Michal Vasko60ea6352020-06-29 13:39:39 +0200714 return LY_EINT;
715 }
716 ++count;
717 }
718
719 /* write number of metadata on 1 byte */
Radek Krejci1798aae2020-07-14 13:26:06 +0200720 LY_CHECK_RET(lyb_write(out, &count, 1, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200721
722 if (wd_mod) {
723 /* write the "default" metadata */
Radek Krejci1798aae2020-07-14 13:26:06 +0200724 LY_CHECK_RET(lyb_write_start_subtree(out, lybctx->lybctx));
725 LY_CHECK_RET(lyb_print_model(out, wd_mod, lybctx->lybctx));
726 LY_CHECK_RET(lyb_write_string("default", 0, 1, out, lybctx->lybctx));
727 LY_CHECK_RET(lyb_write_string("true", 0, 0, out, lybctx->lybctx));
728 LY_CHECK_RET(lyb_write_stop_subtree(out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200729 }
730
731 /* write all the node metadata */
732 LY_LIST_FOR(node->meta, iter) {
733 /* each metadata is a subtree */
Radek Krejci1798aae2020-07-14 13:26:06 +0200734 LY_CHECK_RET(lyb_write_start_subtree(out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200735
736 /* model */
Radek Krejci1798aae2020-07-14 13:26:06 +0200737 LY_CHECK_RET(lyb_print_model(out, iter->annotation->module, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200738
739 /* annotation name with length */
Radek Krejci1798aae2020-07-14 13:26:06 +0200740 LY_CHECK_RET(lyb_write_string(iter->name, 0, 1, out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200741
Michal Vasko60ea6352020-06-29 13:39:39 +0200742 /* metadata value */
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200743 LY_CHECK_RET(lyb_write_string(iter->value.canonical, 0, 0, out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200744
745 /* finish metadata subtree */
Radek Krejci1798aae2020-07-14 13:26:06 +0200746 LY_CHECK_RET(lyb_write_stop_subtree(out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200747 }
748
749 return LY_SUCCESS;
750}
751
752/**
753 * @brief Print opaque node attributes.
754 *
755 * @param[in] out Out structure.
756 * @param[in] node Opaque node whose attributes to print.
757 * @param[in] lybctx LYB context.
758 * @return LY_ERR value.
759 */
760static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200761lyb_print_attributes(struct ly_out *out, const struct lyd_node_opaq *node, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200762{
763 uint8_t count = 0;
Radek Krejci5536d282020-08-04 23:27:44 +0200764 struct lyd_attr *iter;
Michal Vasko60ea6352020-06-29 13:39:39 +0200765
766 for (iter = node->attr; iter; iter = iter->next) {
767 if (count == UINT8_MAX) {
768 LOGERR(lybctx->ctx, LY_EINT, "Maximum supported number of data node attributes is %u.", UINT8_MAX);
769 return LY_EINT;
770 }
771 ++count;
772 }
773
774 /* write number of attributes on 1 byte */
775 LY_CHECK_RET(lyb_write(out, &count, 1, lybctx));
776
777 /* write all the attributes */
778 LY_LIST_FOR(node->attr, iter) {
779 /* each attribute is a subtree */
780 LY_CHECK_RET(lyb_write_start_subtree(out, lybctx));
781
782 /* prefix */
Radek Krejci1798aae2020-07-14 13:26:06 +0200783 LY_CHECK_RET(lyb_write_string(iter->prefix.id, 0, 1, out, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200784
785 /* namespace */
Radek Krejci1798aae2020-07-14 13:26:06 +0200786 LY_CHECK_RET(lyb_write_string(iter->prefix.module_name, 0, 1, out, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200787
788 /* name */
789 LY_CHECK_RET(lyb_write_string(iter->name, 0, 1, out, lybctx));
790
791 /* value prefixes */
792 LY_CHECK_RET(lyb_print_opaq_prefixes(out, iter->val_prefs, lybctx));
793
794 /* format */
795 LY_CHECK_RET(lyb_write_number(iter->format, 1, out, lybctx));
796
797 /* value */
798 LY_CHECK_RET(lyb_write_string(iter->value, 0, 0, out, lybctx));
799
800 /* finish attribute subtree */
801 LY_CHECK_RET(lyb_write_stop_subtree(out, lybctx));
802 }
803
804 return LY_SUCCESS;
805}
806
807/**
808 * @brief Print schema node hash.
809 *
810 * @param[in] out Out structure.
811 * @param[in] schema Schema node whose hash to print.
812 * @param[in,out] sibling_ht Cached hash table for these siblings, created if NULL.
813 * @param[in] lybctx LYB context.
814 * @return LY_ERR value.
815 */
816static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200817lyb_print_schema_hash(struct ly_out *out, struct lysc_node *schema, struct hash_table **sibling_ht, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200818{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200819 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +0200820 uint32_t i;
821 LYB_HASH hash;
822 struct lyd_lyb_sib_ht *sib_ht;
823 struct lysc_node *first_sibling;
824
825 if (!schema) {
826 /* opaque node, write empty hash */
827 hash = 0;
828 LY_CHECK_RET(lyb_write(out, &hash, sizeof hash, lybctx));
829 return LY_SUCCESS;
830 }
831
832 /* create whole sibling HT if not already created and saved */
833 if (!*sibling_ht) {
834 /* get first schema data sibling (or input/output) */
835 first_sibling = (struct lysc_node *)lys_getnext(NULL, lysc_data_parent(schema), schema->module->compiled, 0);
836 LY_ARRAY_FOR(lybctx->sib_hts, u) {
837 if (lybctx->sib_hts[u].first_sibling == first_sibling) {
838 /* we have already created a hash table for these siblings */
839 *sibling_ht = lybctx->sib_hts[u].ht;
840 break;
841 }
842 }
843
844 if (!*sibling_ht) {
845 /* we must create sibling hash table */
846 LY_CHECK_RET(lyb_hash_siblings(first_sibling, sibling_ht));
847
848 /* and save it */
849 LY_ARRAY_NEW_RET(lybctx->ctx, lybctx->sib_hts, sib_ht, LY_EMEM);
850
851 sib_ht->first_sibling = first_sibling;
852 sib_ht->ht = *sibling_ht;
853 }
854 }
855
856 /* get our hash */
857 LY_CHECK_RET(lyb_hash_find(*sibling_ht, schema, &hash));
858
859 /* write the hash */
860 LY_CHECK_RET(lyb_write(out, &hash, sizeof hash, lybctx));
861
862 if (hash & LYB_HASH_COLLISION_ID) {
863 /* no collision for this hash, we are done */
864 return LY_SUCCESS;
865 }
866
867 /* written hash was a collision, write also all the preceding hashes */
Radek Krejci1e008d22020-08-17 11:37:37 +0200868 for (i = 0; !(hash & (LYB_HASH_COLLISION_ID >> i)); ++i) {}
Michal Vasko60ea6352020-06-29 13:39:39 +0200869
Michal Vaskod989ba02020-08-24 10:59:24 +0200870 for ( ; i; --i) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200871 hash = lyb_hash(schema, i - 1);
872 if (!hash) {
873 return LY_EINT;
874 }
875 assert(hash & (LYB_HASH_COLLISION_ID >> (i - 1)));
876
877 LY_CHECK_RET(lyb_write(out, &hash, sizeof hash, lybctx));
878 }
879
880 return LY_SUCCESS;
881}
882
883/**
884 * @brief Print data subtree.
885 *
886 * @param[in] out Out structure.
887 * @param[in] node Root node of the subtree to print.
888 * @param[in,out] sibling_ht Cached hash table for these data siblings, created if NULL.
889 * @param[in] lybctx LYB context.
890 * @return LY_ERR value.
891 */
892static LY_ERR
893lyb_print_subtree(struct ly_out *out, const struct lyd_node *node, struct hash_table **sibling_ht, struct lyd_lyb_ctx *lybctx)
894{
895 struct hash_table *child_ht = NULL;
896
897 /* register a new subtree */
Radek Krejci1798aae2020-07-14 13:26:06 +0200898 LY_CHECK_RET(lyb_write_start_subtree(out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200899
900 /* write model info first */
901 if (!node->schema && !((struct lyd_node_opaq *)node)->parent) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200902 LY_CHECK_RET(lyb_print_model(out, NULL, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200903 } else if (node->schema && !lysc_data_parent(node->schema)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200904 LY_CHECK_RET(lyb_print_model(out, node->schema->module, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200905 }
906
907 /* write schema hash */
Radek Krejci1798aae2020-07-14 13:26:06 +0200908 LY_CHECK_RET(lyb_print_schema_hash(out, (struct lysc_node *)node->schema, sibling_ht, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200909
910 /* write any metadata/attributes */
911 if (node->schema) {
912 LY_CHECK_RET(lyb_print_metadata(out, node, lybctx));
913 } else {
Radek Krejci1798aae2020-07-14 13:26:06 +0200914 LY_CHECK_RET(lyb_print_attributes(out, (struct lyd_node_opaq *)node, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200915 }
916
917 /* write node content */
918 if (!node->schema) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200919 LY_CHECK_RET(lyb_print_opaq((struct lyd_node_opaq *)node, out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200920 } else if (node->schema->nodetype & LYD_NODE_INNER) {
921 /* nothing to write */
922 } else if (node->schema->nodetype & LYD_NODE_TERM) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200923 LY_CHECK_RET(lyb_print_term((struct lyd_node_term *)node, out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200924 } else if (node->schema->nodetype & LYD_NODE_ANY) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200925 LY_CHECK_RET(lyb_print_anydata((struct lyd_node_any *)node, out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200926 } else {
Radek Krejci1798aae2020-07-14 13:26:06 +0200927 LOGINT_RET(lybctx->lybctx->ctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200928 }
929
930 /* recursively write all the descendants */
Radek Krejcia1c1e542020-09-29 16:06:52 +0200931 LY_LIST_FOR(lyd_child(node), node) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200932 LY_CHECK_RET(lyb_print_subtree(out, node, &child_ht, lybctx));
933 }
934
935 /* finish this subtree */
Radek Krejci1798aae2020-07-14 13:26:06 +0200936 LY_CHECK_RET(lyb_write_stop_subtree(out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200937
938 return LY_SUCCESS;
939}
940
941LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200942lyb_print_data(struct ly_out *out, const struct lyd_node *root, uint32_t options)
Michal Vasko60ea6352020-06-29 13:39:39 +0200943{
944 LY_ERR ret = LY_SUCCESS;
945 uint8_t zero = 0;
Michal Vasko60ea6352020-06-29 13:39:39 +0200946 struct hash_table *top_sibling_ht = NULL;
947 const struct lys_module *prev_mod = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +0200948 struct lyd_lyb_ctx *lybctx;
Michal Vaskob7be7a82020-08-20 09:09:04 +0200949 const struct ly_ctx *ctx = root ? LYD_CTX(root) : NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200950
Radek Krejci1798aae2020-07-14 13:26:06 +0200951 lybctx = calloc(1, sizeof *lybctx);
952 LY_CHECK_ERR_RET(!lybctx, LOGMEM(ctx), LY_EMEM);
953 lybctx->lybctx = calloc(1, sizeof *lybctx->lybctx);
954 LY_CHECK_ERR_RET(!lybctx, LOGMEM(ctx), LY_EMEM);
955
956 lybctx->print_options = options;
Michal Vasko60ea6352020-06-29 13:39:39 +0200957 if (root) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200958 lybctx->lybctx->ctx = ctx;
Michal Vasko60ea6352020-06-29 13:39:39 +0200959
960 if (root->schema && lysc_data_parent(root->schema)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200961 LOGERR(lybctx->lybctx->ctx, LY_EINVAL, "LYB printer supports only printing top-level nodes.");
Michal Vasko9acaf492020-08-13 09:05:58 +0200962 ret = LY_EINVAL;
963 goto cleanup;
Michal Vasko60ea6352020-06-29 13:39:39 +0200964 }
965 }
966
967 /* LYB magic number */
Michal Vasko63f3d842020-07-08 10:10:14 +0200968 LY_CHECK_GOTO(ret = lyb_print_magic_number(out), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200969
970 /* LYB header */
Michal Vasko63f3d842020-07-08 10:10:14 +0200971 LY_CHECK_GOTO(ret = lyb_print_header(out), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200972
973 /* all used models */
Radek Krejci1798aae2020-07-14 13:26:06 +0200974 LY_CHECK_GOTO(ret = lyb_print_data_models(out, root, lybctx->lybctx), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200975
976 LY_LIST_FOR(root, root) {
977 /* do not reuse sibling hash tables from different modules */
978 if (!root->schema || (root->schema->module != prev_mod)) {
979 top_sibling_ht = NULL;
980 prev_mod = root->schema ? root->schema->module : NULL;
981 }
982
Radek Krejci1798aae2020-07-14 13:26:06 +0200983 LY_CHECK_GOTO(ret = lyb_print_subtree(out, root, &top_sibling_ht, lybctx), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200984
Radek Krejci7931b192020-06-25 17:05:03 +0200985 if (!(options & LYD_PRINT_WITHSIBLINGS)) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200986 break;
987 }
988 }
989
990 /* ending zero byte */
Radek Krejci1798aae2020-07-14 13:26:06 +0200991 LY_CHECK_GOTO(ret = lyb_write(out, &zero, sizeof zero, lybctx->lybctx), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200992
993cleanup:
Radek Krejci1798aae2020-07-14 13:26:06 +0200994 lyd_lyb_ctx_free((struct lyd_ctx *)lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200995 return ret;
996}