blob: 3b62d27c03bab9cefb3f03e92b731a195b563a41 [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>
22
23#include "common.h"
24#include "compat.h"
Radek Krejciad97c5f2020-06-30 09:19:28 +020025#include "context.h"
26#include "hash_table.h"
Michal Vasko60ea6352020-06-29 13:39:39 +020027#include "log.h"
Radek Krejci47fab892020-11-05 17:02:41 +010028#include "out.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020029#include "out_internal.h"
Radek Krejciad97c5f2020-06-30 09:19:28 +020030#include "printer_data.h"
Michal Vasko60ea6352020-06-29 13:39:39 +020031#include "printer_internal.h"
Radek Krejciad97c5f2020-06-30 09:19:28 +020032#include "set.h"
33#include "tree.h"
Radek Krejci47fab892020-11-05 17:02:41 +010034#include "tree_data.h"
Michal Vasko60ea6352020-06-29 13:39:39 +020035#include "tree_data_internal.h"
36#include "tree_schema.h"
37#include "tree_schema_internal.h"
Michal Vasko6b5cb2a2020-11-11 19:11:21 +010038#include "xml.h"
Michal Vasko60ea6352020-06-29 13:39:39 +020039
40/**
41 * @brief Hash table equal callback for checking hash equality only.
Radek Krejci857189e2020-09-01 13:26:36 +020042 *
43 * Implementation of ::values_equal_cb.
Michal Vasko60ea6352020-06-29 13:39:39 +020044 */
Radek Krejci857189e2020-09-01 13:26:36 +020045static ly_bool
46lyb_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 +020047{
48 /* for this purpose, if hash matches, the value does also, we do not want 2 values to have the same hash */
49 return 1;
50}
51
52/**
53 * @brief Hash table equal callback for checking value pointer equality only.
Radek Krejci857189e2020-09-01 13:26:36 +020054 *
55 * Implementation of ::values_equal_cb.
Michal Vasko60ea6352020-06-29 13:39:39 +020056 */
Radek Krejci857189e2020-09-01 13:26:36 +020057static ly_bool
58lyb_ptr_equal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
Michal Vasko60ea6352020-06-29 13:39:39 +020059{
60 struct lysc_node *val1 = *(struct lysc_node **)val1_p;
61 struct lysc_node *val2 = *(struct lysc_node **)val2_p;
62
63 if (val1 == val2) {
64 return 1;
65 }
66 return 0;
67}
68
69/**
70 * @brief Check that sibling collision hash is safe to insert into hash table.
71 *
72 * @param[in] ht Hash table.
73 * @param[in] sibling Hashed sibling.
74 * @param[in] ht_col_id Sibling hash collision ID.
75 * @param[in] compare_col_id Last collision ID to compare with.
76 * @return LY_SUCCESS when the whole hash sequence does not collide,
77 * @return LY_EEXIST when the whole hash sequence sollides.
78 */
79static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +020080lyb_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 +020081{
Michal Vasko60ea6352020-06-29 13:39:39 +020082 struct lysc_node **col_node;
83
84 /* get the first node inserted with last hash col ID ht_col_id */
85 if (lyht_find(ht, &sibling, lyb_hash(sibling, ht_col_id), (void **)&col_node)) {
86 /* there is none. valid situation */
87 return LY_SUCCESS;
88 }
89
90 lyht_set_cb(ht, lyb_ptr_equal_cb);
91 do {
Radek Krejci1deb5be2020-08-26 16:43:36 +020092 int64_t j;
93 for (j = (int64_t)compare_col_id; j > -1; --j) {
Michal Vasko60ea6352020-06-29 13:39:39 +020094 if (lyb_hash(sibling, j) != lyb_hash(*col_node, j)) {
95 /* one non-colliding hash */
96 break;
97 }
98 }
99 if (j == -1) {
100 /* all whole hash sequences of nodes inserted with last hash col ID compare_col_id collide */
101 lyht_set_cb(ht, lyb_hash_equal_cb);
102 return LY_EEXIST;
103 }
104
105 /* get next node inserted with last hash col ID ht_col_id */
106 } while (!lyht_find_next(ht, col_node, lyb_hash(*col_node, ht_col_id), (void **)&col_node));
107
108 lyht_set_cb(ht, lyb_hash_equal_cb);
109 return LY_SUCCESS;
110}
111
112/**
113 * @brief Hash all the siblings and add them also into a separate hash table.
114 *
115 * @param[in] sibling Any sibling in all the siblings on one level.
116 * @param[out] ht_p Created hash table.
117 * @return LY_ERR value.
118 */
119static LY_ERR
120lyb_hash_siblings(struct lysc_node *sibling, struct hash_table **ht_p)
121{
122 struct hash_table *ht;
123 const struct lysc_node *parent;
124 const struct lys_module *mod;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200125 LYB_HASH i;
Michal Vasko60ea6352020-06-29 13:39:39 +0200126
127 ht = lyht_new(1, sizeof(struct lysc_node *), lyb_hash_equal_cb, NULL, 1);
128 LY_CHECK_ERR_RET(!ht, LOGMEM(sibling->module->ctx), LY_EMEM);
129
130 parent = lysc_data_parent(sibling);
131 mod = sibling->module;
132
133 sibling = NULL;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100134 while ((sibling = (struct lysc_node *)lys_getnext(sibling, parent, mod->compiled, 0))) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200135 /* find the first non-colliding hash (or specifically non-colliding hash sequence) */
136 for (i = 0; i < LYB_HASH_BITS; ++i) {
137 /* check that we are not colliding with nodes inserted with a lower collision ID than ours */
Radek Krejci1deb5be2020-08-26 16:43:36 +0200138 int64_t j;
139 for (j = (int64_t)i - 1; j > -1; --j) {
140 if (lyb_hash_sequence_check(ht, sibling, (LYB_HASH)j, i)) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200141 break;
142 }
143 }
144 if (j > -1) {
145 /* some check failed, we must use a higher collision ID */
146 continue;
147 }
148
149 /* try to insert node with the current collision ID */
150 if (!lyht_insert_with_resize_cb(ht, &sibling, lyb_hash(sibling, i), lyb_ptr_equal_cb, NULL)) {
151 /* success, no collision */
152 break;
153 }
154
155 /* make sure we really cannot insert it with this hash col ID (meaning the whole hash sequence is colliding) */
156 if (i && !lyb_hash_sequence_check(ht, sibling, i, i)) {
157 /* it can be inserted after all, even though there is already a node with the same last collision ID */
158 lyht_set_cb(ht, lyb_ptr_equal_cb);
159 if (lyht_insert(ht, &sibling, lyb_hash(sibling, i), NULL)) {
160 LOGINT(sibling->module->ctx);
161 lyht_set_cb(ht, lyb_hash_equal_cb);
162 lyht_free(ht);
163 return LY_EINT;
164 }
165 lyht_set_cb(ht, lyb_hash_equal_cb);
166 break;
167 }
168 /* there is still another colliding schema node with the same hash sequence, try higher collision ID */
169 }
170
171 if (i == LYB_HASH_BITS) {
172 /* wow */
173 LOGINT(sibling->module->ctx);
174 lyht_free(ht);
175 return LY_EINT;
176 }
177 }
178
179 /* change val equal callback so that the HT is usable for finding value hashes */
180 lyht_set_cb(ht, lyb_ptr_equal_cb);
181
182 *ht_p = ht;
183 return LY_SUCCESS;
184}
185
186/**
187 * @brief Find node hash in a hash table.
188 *
189 * @param[in] ht Hash table to search in.
190 * @param[in] node Node to find.
191 * @param[out] hash_p First non-colliding hash found.
192 * @return LY_ERR value.
193 */
194static LY_ERR
195lyb_hash_find(struct hash_table *ht, struct lysc_node *node, LYB_HASH *hash_p)
196{
197 LYB_HASH hash;
198 uint32_t i;
199
200 for (i = 0; i < LYB_HASH_BITS; ++i) {
201 hash = lyb_hash(node, i);
202 if (!hash) {
203 LOGINT_RET(node->module->ctx);
204 }
205
206 if (!lyht_find(ht, &node, hash, NULL)) {
207 /* success, no collision */
208 break;
209 }
210 }
211 /* cannot happen, we already calculated the hash */
212 if (i == LYB_HASH_BITS) {
213 LOGINT_RET(node->module->ctx);
214 }
215
216 *hash_p = hash;
217 return LY_SUCCESS;
218}
219
220/**
221 * @brief Write LYB data fully handling the metadata.
222 *
223 * @param[in] out Out structure.
224 * @param[in] buf Source buffer.
225 * @param[in] count Number of bytes to write.
226 * @param[in] lybctx LYB context.
227 * @return LY_ERR value.
228 */
229static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200230lyb_write(struct ly_out *out, const uint8_t *buf, size_t count, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200231{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200232 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +0200233 struct lyd_lyb_subtree *full, *iter;
Michal Vasko5233e962020-08-14 14:26:20 +0200234 size_t to_write;
Michal Vasko60ea6352020-06-29 13:39:39 +0200235 uint8_t meta_buf[LYB_META_BYTES];
236
237 while (1) {
238 /* check for full data chunks */
239 to_write = count;
240 full = NULL;
241 LY_ARRAY_FOR(lybctx->subtrees, u) {
242 /* we want the innermost chunks resolved first, so replace previous full chunks */
243 if (lybctx->subtrees[u].written + to_write >= LYB_SIZE_MAX) {
244 /* full chunk, do not write more than allowed */
245 to_write = LYB_SIZE_MAX - lybctx->subtrees[u].written;
246 full = &lybctx->subtrees[u];
247 }
248 }
249
250 if (!full && !count) {
251 break;
252 }
253
254 /* we are actually writing some data, not just finishing another chunk */
255 if (to_write) {
Michal Vasko5233e962020-08-14 14:26:20 +0200256 LY_CHECK_RET(ly_write_(out, (char *)buf, to_write));
Michal Vasko60ea6352020-06-29 13:39:39 +0200257
258 LY_ARRAY_FOR(lybctx->subtrees, u) {
259 /* increase all written counters */
Michal Vasko5233e962020-08-14 14:26:20 +0200260 lybctx->subtrees[u].written += to_write;
Michal Vasko60ea6352020-06-29 13:39:39 +0200261 assert(lybctx->subtrees[u].written <= LYB_SIZE_MAX);
262 }
263 /* decrease count/buf */
Michal Vasko5233e962020-08-14 14:26:20 +0200264 count -= to_write;
265 buf += to_write;
Michal Vasko60ea6352020-06-29 13:39:39 +0200266 }
267
268 if (full) {
269 /* write the meta information (inner chunk count and chunk size) */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100270 meta_buf[0] = full->written & LYB_BYTE_MASK;
271 meta_buf[1] = full->inner_chunks & LYB_BYTE_MASK;
Michal Vasko5233e962020-08-14 14:26:20 +0200272 LY_CHECK_RET(ly_write_skipped(out, full->position, (char *)meta_buf, LYB_META_BYTES));
Michal Vasko60ea6352020-06-29 13:39:39 +0200273
274 /* zero written and inner chunks */
275 full->written = 0;
276 full->inner_chunks = 0;
277
278 /* skip space for another chunk size */
Michal Vasko5233e962020-08-14 14:26:20 +0200279 LY_CHECK_RET(ly_write_skip(out, LYB_META_BYTES, &full->position));
Michal Vasko60ea6352020-06-29 13:39:39 +0200280
281 /* increase inner chunk count */
282 for (iter = &lybctx->subtrees[0]; iter != full; ++iter) {
283 if (iter->inner_chunks == LYB_INCHUNK_MAX) {
284 LOGINT(lybctx->ctx);
285 return LY_EINT;
286 }
287 ++iter->inner_chunks;
288 }
289 }
290 }
291
292 return LY_SUCCESS;
293}
294
295/**
296 * @brief Stop the current subtree - write its final metadata.
297 *
298 * @param[in] out Out structure.
299 * @param[in] lybctx LYB context.
300 * @return LY_ERR value.
301 */
302static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200303lyb_write_stop_subtree(struct ly_out *out, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200304{
Michal Vasko60ea6352020-06-29 13:39:39 +0200305 uint8_t meta_buf[LYB_META_BYTES];
306
307 /* write the meta chunk information */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100308 meta_buf[0] = LYB_LAST_SUBTREE(lybctx).written & LYB_BYTE_MASK;
309 meta_buf[1] = LYB_LAST_SUBTREE(lybctx).inner_chunks & LYB_BYTE_MASK;
Michal Vasko5233e962020-08-14 14:26:20 +0200310 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 +0200311
312 LY_ARRAY_DECREMENT(lybctx->subtrees);
313 return LY_SUCCESS;
314}
315
316/**
317 * @brief Start a new subtree - skip bytes for its metadata.
318 *
319 * @param[in] out Out structure.
320 * @param[in] lybctx LYB context.
321 * @return LY_ERR value.
322 */
323static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200324lyb_write_start_subtree(struct ly_out *out, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200325{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200326 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +0200327
Radek Krejcic7d13e32020-12-09 12:32:24 +0100328 u = LY_ARRAY_COUNT(lybctx->subtrees);
Michal Vasko60ea6352020-06-29 13:39:39 +0200329 if (u == lybctx->subtree_size) {
330 LY_ARRAY_CREATE_RET(lybctx->ctx, lybctx->subtrees, u + LYB_SUBTREE_STEP, LY_EMEM);
331 lybctx->subtree_size = u + LYB_SUBTREE_STEP;
332 }
333
334 LY_ARRAY_INCREMENT(lybctx->subtrees);
335 LYB_LAST_SUBTREE(lybctx).written = 0;
336 LYB_LAST_SUBTREE(lybctx).inner_chunks = 0;
337
338 /* another inner chunk */
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200339 for (u = 0; u < LY_ARRAY_COUNT(lybctx->subtrees) - 1; ++u) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200340 if (lybctx->subtrees[u].inner_chunks == LYB_INCHUNK_MAX) {
341 LOGINT(lybctx->ctx);
Michal Vasko5233e962020-08-14 14:26:20 +0200342 return LY_EINT;
Michal Vasko60ea6352020-06-29 13:39:39 +0200343 }
344 ++lybctx->subtrees[u].inner_chunks;
345 }
346
Michal Vasko5233e962020-08-14 14:26:20 +0200347 LY_CHECK_RET(ly_write_skip(out, LYB_META_BYTES, &LYB_LAST_SUBTREE(lybctx).position));
Michal Vasko60ea6352020-06-29 13:39:39 +0200348
349 return LY_SUCCESS;
350}
351
352/**
353 * @brief Write a number.
354 *
355 * @param[in] num Number to write.
356 * @param[in] bytes Actual accessible bytes of @p num.
357 * @param[in] out Out structure.
358 * @param[in] lybctx LYB context.
359 * @return LY_ERR value.
360 */
361static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200362lyb_write_number(uint64_t num, size_t bytes, struct ly_out *out, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200363{
364 /* correct byte order */
365 num = htole64(num);
366
367 return lyb_write(out, (uint8_t *)&num, bytes, lybctx);
368}
369
370/**
371 * @brief Write a string.
372 *
373 * @param[in] str String to write.
374 * @param[in] str_len Length of @p str.
375 * @param[in] with_length Whether to precede the string with its length.
376 * @param[in] out Out structure.
377 * @param[in] lybctx LYB context.
378 * @return LY_ERR value.
379 */
380static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200381lyb_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 +0200382{
Michal Vasko60ea6352020-06-29 13:39:39 +0200383 if (!str) {
384 str = "";
Michal Vasko30667352020-08-13 09:06:14 +0200385 LY_CHECK_ERR_RET(str_len, LOGINT(lybctx->ctx), LY_EINT);
Michal Vasko60ea6352020-06-29 13:39:39 +0200386 }
387 if (!str_len) {
388 str_len = strlen(str);
389 }
390
391 if (with_length) {
392 /* print length on 2 bytes */
393 if (str_len > UINT16_MAX) {
394 LOGINT(lybctx->ctx);
395 return LY_EINT;
396 }
397 LY_CHECK_RET(lyb_write_number(str_len, 2, out, lybctx));
398 }
399
Michal Vasko63f3d842020-07-08 10:10:14 +0200400 LY_CHECK_RET(lyb_write(out, (const uint8_t *)str, str_len, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200401
402 return LY_SUCCESS;
403}
404
405/**
406 * @brief Print YANG module info.
407 *
408 * @param[in] out Out structure.
409 * @param[in] mod Module to print.
410 * @param[in] lybctx LYB context.
411 * @return LY_ERR value.
412 */
413static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200414lyb_print_model(struct ly_out *out, const struct lys_module *mod, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200415{
Michal Vasko60ea6352020-06-29 13:39:39 +0200416 uint16_t revision;
417
418 /* model name length and model name */
419 if (mod) {
420 LY_CHECK_RET(lyb_write_string(mod->name, 0, 1, out, lybctx));
421 } else {
422 LY_CHECK_RET(lyb_write_string("", 0, 1, out, lybctx));
423 }
424
425 /* model revision as XXXX XXXX XXXX XXXX (2B) (year is offset from 2000)
426 * YYYY YYYM MMMD DDDD */
427 revision = 0;
428 if (mod && mod->revision) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200429 int r = atoi(mod->revision);
Radek Krejcif13b87b2020-12-01 22:02:17 +0100430 r -= LYB_REV_YEAR_OFFSET;
431 r <<= LYB_REV_YEAR_SHIFT;
Michal Vasko60ea6352020-06-29 13:39:39 +0200432
433 revision |= r;
434
Radek Krejcif13b87b2020-12-01 22:02:17 +0100435 r = atoi(mod->revision + ly_strlen_const("YYYY-"));
436 r <<= LYB_REV_MONTH_SHIFT;
Michal Vasko60ea6352020-06-29 13:39:39 +0200437
438 revision |= r;
439
Radek Krejcif13b87b2020-12-01 22:02:17 +0100440 r = atoi(mod->revision + ly_strlen_const("YYYY-MM-"));
Michal Vasko60ea6352020-06-29 13:39:39 +0200441
442 revision |= r;
443 }
444 LY_CHECK_RET(lyb_write_number(revision, sizeof revision, out, lybctx));
445
446 return LY_SUCCESS;
447}
448
449/**
450 * @brief Print all used YANG modules.
451 *
452 * @param[in] out Out structure.
453 * @param[in] root Data root.
454 * @param[in] lybctx LYB context.
455 * @return LY_ERR value.
456 */
457static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200458lyb_print_data_models(struct ly_out *out, const struct lyd_node *root, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200459{
460 struct ly_set *set;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200461 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +0200462 LY_ERR ret = LY_SUCCESS;
463 struct lys_module *mod;
464 const struct lyd_node *node;
465 uint32_t i;
466
Radek Krejciba03a5a2020-08-27 14:40:41 +0200467 LY_CHECK_RET(ly_set_new(&set));
Michal Vasko60ea6352020-06-29 13:39:39 +0200468
469 /* collect all data node modules */
470 LY_LIST_FOR(root, node) {
471 if (!node->schema) {
472 continue;
473 }
474
475 mod = node->schema->module;
Radek Krejciba03a5a2020-08-27 14:40:41 +0200476 ret = ly_set_add(set, mod, 0, NULL);
477 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200478
479 /* add also their modules deviating or augmenting them */
Michal Vasko7f45cf22020-10-01 12:49:44 +0200480 LY_ARRAY_FOR(mod->deviated_by, u) {
481 ret = ly_set_add(set, mod->deviated_by[u], 0, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200482 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200483 }
Michal Vasko7f45cf22020-10-01 12:49:44 +0200484 LY_ARRAY_FOR(mod->augmented_by, u) {
485 ret = ly_set_add(set, mod->augmented_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 }
488 }
489
490 /* now write module count on 2 bytes */
491 LY_CHECK_GOTO(ret = lyb_write_number(set->count, 2, out, lybctx), cleanup);
492
493 /* and all the used models */
494 for (i = 0; i < set->count; ++i) {
495 LY_CHECK_GOTO(ret = lyb_print_model(out, set->objs[i], lybctx), cleanup);
496 }
497
498cleanup:
499 ly_set_free(set, NULL);
500 return ret;
501}
502
503/**
504 * @brief Print LYB magic number.
505 *
506 * @param[in] out Out structure.
Michal Vasko60ea6352020-06-29 13:39:39 +0200507 * @return LY_ERR value.
508 */
509static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200510lyb_print_magic_number(struct ly_out *out)
Michal Vasko60ea6352020-06-29 13:39:39 +0200511{
Michal Vasko60ea6352020-06-29 13:39:39 +0200512 /* 'l', 'y', 'b' - 0x6c7962 */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100513 char magic_number[] = {'l', 'y', 'b'};
Michal Vasko60ea6352020-06-29 13:39:39 +0200514
Radek Krejcidefd4d72020-12-01 12:20:30 +0100515 LY_CHECK_RET(ly_write_(out, magic_number, 3));
Michal Vasko60ea6352020-06-29 13:39:39 +0200516
517 return LY_SUCCESS;
518}
519
520/**
521 * @brief Print LYB header.
522 *
523 * @param[in] out Out structure.
Michal Vasko60ea6352020-06-29 13:39:39 +0200524 * @return LY_ERR value.
525 */
526static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200527lyb_print_header(struct ly_out *out)
Michal Vasko60ea6352020-06-29 13:39:39 +0200528{
Michal Vasko60ea6352020-06-29 13:39:39 +0200529 uint8_t byte = 0;
530
531 /* version, future flags */
532 byte |= LYB_VERSION_NUM;
533
Michal Vasko5233e962020-08-14 14:26:20 +0200534 LY_CHECK_RET(ly_write_(out, (char *)&byte, 1));
Michal Vasko60ea6352020-06-29 13:39:39 +0200535
536 return LY_SUCCESS;
537}
538
539/**
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100540 * @brief Print prefix data.
Michal Vasko60ea6352020-06-29 13:39:39 +0200541 *
542 * @param[in] out Out structure.
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100543 * @param[in] format Value prefix format.
544 * @param[in] prefix_data Prefix data to print.
Michal Vasko60ea6352020-06-29 13:39:39 +0200545 * @param[in] lybctx LYB context.
546 * @return LY_ERR value.
547 */
548static LY_ERR
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100549lyb_print_prefix_data(struct ly_out *out, LY_PREFIX_FORMAT format, const void *prefix_data, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200550{
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100551 const struct ly_set *set;
552 const struct lyxml_ns *ns;
553 uint32_t i;
Michal Vasko60ea6352020-06-29 13:39:39 +0200554
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100555 switch (format) {
556 case LY_PREF_XML:
557 set = prefix_data;
558 if (!set) {
559 /* no prefix data */
560 i = 0;
561 LY_CHECK_RET(lyb_write(out, (uint8_t *)&i, 1, lybctx));
562 break;
563 }
564 if (set->count > UINT8_MAX) {
565 LOGERR(lybctx->ctx, LY_EINT, "Maximum supported number of prefixes is %u.", UINT8_MAX);
566 return LY_EINT;
567 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200568
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100569 /* write number of prefixes on 1 byte */
570 LY_CHECK_RET(lyb_write(out, (uint8_t *)&set->count, 1, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200571
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100572 /* write all the prefixes */
573 for (i = 0; i < set->count; ++i) {
574 ns = set->objs[i];
Michal Vasko60ea6352020-06-29 13:39:39 +0200575
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100576 /* prefix */
577 LY_CHECK_RET(lyb_write_string(ns->prefix, 0, 1, out, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200578
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100579 /* namespace */
580 LY_CHECK_RET(lyb_write_string(ns->uri, 0, 1, out, lybctx));
581 }
582 break;
583 case LY_PREF_JSON:
584 /* nothing to print */
585 break;
586 default:
587 LOGINT_RET(lybctx->ctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200588 }
589
590 return LY_SUCCESS;
591}
592
593/**
594 * @brief Print opaque node.
595 *
596 * @param[in] opaq Node to print.
597 * @param[in] out Out structure.
598 * @param[in] lybctx LYB context.
599 * @return LY_ERR value.
600 */
601static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200602lyb_print_opaq(struct lyd_node_opaq *opaq, struct ly_out *out, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200603{
604 /* prefix */
Michal Vaskoad92b672020-11-12 13:11:31 +0100605 LY_CHECK_RET(lyb_write_string(opaq->name.prefix, 0, 1, out, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200606
Radek Krejci1798aae2020-07-14 13:26:06 +0200607 /* module reference */
Michal Vaskoad92b672020-11-12 13:11:31 +0100608 LY_CHECK_RET(lyb_write_string(opaq->name.module_name, 0, 1, out, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200609
610 /* name */
Michal Vaskoad92b672020-11-12 13:11:31 +0100611 LY_CHECK_RET(lyb_write_string(opaq->name.name, 0, 1, out, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200612
Michal Vasko60ea6352020-06-29 13:39:39 +0200613 /* format */
614 LY_CHECK_RET(lyb_write_number(opaq->format, 1, out, lybctx));
615
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100616 /* value prefixes */
617 LY_CHECK_RET(lyb_print_prefix_data(out, opaq->format, opaq->val_prefix_data, lybctx));
618
Michal Vasko60ea6352020-06-29 13:39:39 +0200619 /* value */
620 LY_CHECK_RET(lyb_write_string(opaq->value, 0, 0, out, lybctx));
621
622 return LY_SUCCESS;
623}
624
625/**
626 * @brief Print anydata node.
627 *
628 * @param[in] anydata Node to print.
629 * @param[in] out Out structure.
630 * @param[in] lybctx LYB context.
631 * @return LY_ERR value.
632 */
633static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200634lyb_print_anydata(struct lyd_node_any *anydata, struct ly_out *out, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200635{
636 LY_ERR ret = LY_SUCCESS;
637 LYD_ANYDATA_VALUETYPE value_type;
638 int len;
639 char *buf = NULL;
640 const char *str;
641 struct ly_out *out2 = NULL;
642
643 if (anydata->value_type == LYD_ANYDATA_DATATREE) {
644 /* will be printed as a nested LYB data tree */
645 value_type = LYD_ANYDATA_LYB;
646 } else {
647 value_type = anydata->value_type;
648 }
649
650 /* first byte is type */
651 LY_CHECK_GOTO(ret = lyb_write(out, (uint8_t *)&value_type, sizeof value_type, lybctx), cleanup);
652
653 if (anydata->value_type == LYD_ANYDATA_DATATREE) {
654 /* print LYB data tree to memory */
655 LY_CHECK_GOTO(ret = ly_out_new_memory(&buf, 0, &out2), cleanup);
Radek Krejci7931b192020-06-25 17:05:03 +0200656 LY_CHECK_GOTO(ret = lyb_print_data(out2, anydata->value.tree, LYD_PRINT_WITHSIBLINGS), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200657
658 len = lyd_lyb_data_length(buf);
659 assert(len != -1);
660 str = buf;
661 } else if (anydata->value_type == LYD_ANYDATA_LYB) {
662 len = lyd_lyb_data_length(anydata->value.mem);
663 assert(len != -1);
664 str = anydata->value.mem;
665 } else {
666 len = strlen(anydata->value.str);
667 str = anydata->value.str;
668 }
669
670 /* followed by the content */
671 LY_CHECK_GOTO(ret = lyb_write_string(str, (size_t)len, 0, out, lybctx), cleanup);
672
673cleanup:
674 ly_out_free(out2, NULL, 1);
675 return ret;
676}
677
678/**
679 * @brief Print term node.
680 *
681 * @param[in] term Node to print.
682 * @param[in] out Out structure.
683 * @param[in] lybctx LYB context.
684 * @return LY_ERR value.
685 */
686static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200687lyb_print_term(struct lyd_node_term *term, struct ly_out *out, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200688{
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200689 /* print the value */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200690 return lyb_write_string(LYD_CANON_VALUE(term), 0, 0, out, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200691}
692
693/**
694 * @brief Print YANG node metadata.
695 *
696 * @param[in] out Out structure.
697 * @param[in] node Data node whose metadata to print.
698 * @param[in] lybctx LYB context.
699 * @return LY_ERR value.
700 */
701static LY_ERR
702lyb_print_metadata(struct ly_out *out, const struct lyd_node *node, struct lyd_lyb_ctx *lybctx)
703{
Michal Vasko60ea6352020-06-29 13:39:39 +0200704 uint8_t count = 0;
705 const struct lys_module *wd_mod = NULL;
706 struct lyd_meta *iter;
Michal Vasko60ea6352020-06-29 13:39:39 +0200707
708 /* with-defaults */
709 if (node->schema->nodetype & LYD_NODE_TERM) {
Radek Krejci7931b192020-06-25 17:05:03 +0200710 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 +0200711 ((lybctx->print_options & LYD_PRINT_WD_ALL_TAG) && lyd_is_default(node))) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200712 /* we have implicit OR explicit default node, print attribute only if context include with-defaults schema */
713 wd_mod = ly_ctx_get_module_latest(node->schema->module->ctx, "ietf-netconf-with-defaults");
714 }
715 }
716
717 /* count metadata */
718 if (wd_mod) {
719 ++count;
720 }
721 for (iter = node->meta; iter; iter = iter->next) {
722 if (count == UINT8_MAX) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200723 LOGERR(lybctx->lybctx->ctx, LY_EINT, "Maximum supported number of data node metadata is %u.", UINT8_MAX);
Michal Vasko60ea6352020-06-29 13:39:39 +0200724 return LY_EINT;
725 }
726 ++count;
727 }
728
729 /* write number of metadata on 1 byte */
Radek Krejci1798aae2020-07-14 13:26:06 +0200730 LY_CHECK_RET(lyb_write(out, &count, 1, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200731
732 if (wd_mod) {
733 /* write the "default" metadata */
Radek Krejci1798aae2020-07-14 13:26:06 +0200734 LY_CHECK_RET(lyb_write_start_subtree(out, lybctx->lybctx));
735 LY_CHECK_RET(lyb_print_model(out, wd_mod, lybctx->lybctx));
736 LY_CHECK_RET(lyb_write_string("default", 0, 1, out, lybctx->lybctx));
737 LY_CHECK_RET(lyb_write_string("true", 0, 0, out, lybctx->lybctx));
738 LY_CHECK_RET(lyb_write_stop_subtree(out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200739 }
740
741 /* write all the node metadata */
742 LY_LIST_FOR(node->meta, iter) {
743 /* each metadata is a subtree */
Radek Krejci1798aae2020-07-14 13:26:06 +0200744 LY_CHECK_RET(lyb_write_start_subtree(out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200745
746 /* model */
Radek Krejci1798aae2020-07-14 13:26:06 +0200747 LY_CHECK_RET(lyb_print_model(out, iter->annotation->module, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200748
749 /* annotation name with length */
Radek Krejci1798aae2020-07-14 13:26:06 +0200750 LY_CHECK_RET(lyb_write_string(iter->name, 0, 1, out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200751
Michal Vasko60ea6352020-06-29 13:39:39 +0200752 /* metadata value */
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200753 LY_CHECK_RET(lyb_write_string(iter->value.canonical, 0, 0, out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200754
755 /* finish metadata subtree */
Radek Krejci1798aae2020-07-14 13:26:06 +0200756 LY_CHECK_RET(lyb_write_stop_subtree(out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200757 }
758
759 return LY_SUCCESS;
760}
761
762/**
763 * @brief Print opaque node attributes.
764 *
765 * @param[in] out Out structure.
766 * @param[in] node Opaque node whose attributes to print.
767 * @param[in] lybctx LYB context.
768 * @return LY_ERR value.
769 */
770static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200771lyb_print_attributes(struct ly_out *out, const struct lyd_node_opaq *node, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200772{
773 uint8_t count = 0;
Radek Krejci5536d282020-08-04 23:27:44 +0200774 struct lyd_attr *iter;
Michal Vasko60ea6352020-06-29 13:39:39 +0200775
776 for (iter = node->attr; iter; iter = iter->next) {
777 if (count == UINT8_MAX) {
778 LOGERR(lybctx->ctx, LY_EINT, "Maximum supported number of data node attributes is %u.", UINT8_MAX);
779 return LY_EINT;
780 }
781 ++count;
782 }
783
784 /* write number of attributes on 1 byte */
785 LY_CHECK_RET(lyb_write(out, &count, 1, lybctx));
786
787 /* write all the attributes */
788 LY_LIST_FOR(node->attr, iter) {
789 /* each attribute is a subtree */
790 LY_CHECK_RET(lyb_write_start_subtree(out, lybctx));
791
792 /* prefix */
Michal Vaskoad92b672020-11-12 13:11:31 +0100793 LY_CHECK_RET(lyb_write_string(iter->name.prefix, 0, 1, out, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200794
795 /* namespace */
Michal Vaskoad92b672020-11-12 13:11:31 +0100796 LY_CHECK_RET(lyb_write_string(iter->name.module_name, 0, 1, out, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200797
798 /* name */
Michal Vaskoad92b672020-11-12 13:11:31 +0100799 LY_CHECK_RET(lyb_write_string(iter->name.name, 0, 1, out, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200800
Michal Vasko60ea6352020-06-29 13:39:39 +0200801 /* format */
802 LY_CHECK_RET(lyb_write_number(iter->format, 1, out, lybctx));
803
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100804 /* value prefixes */
805 LY_CHECK_RET(lyb_print_prefix_data(out, iter->format, iter->val_prefix_data, lybctx));
806
Michal Vasko60ea6352020-06-29 13:39:39 +0200807 /* value */
808 LY_CHECK_RET(lyb_write_string(iter->value, 0, 0, out, lybctx));
809
810 /* finish attribute subtree */
811 LY_CHECK_RET(lyb_write_stop_subtree(out, lybctx));
812 }
813
814 return LY_SUCCESS;
815}
816
817/**
818 * @brief Print schema node hash.
819 *
820 * @param[in] out Out structure.
821 * @param[in] schema Schema node whose hash to print.
822 * @param[in,out] sibling_ht Cached hash table for these siblings, created if NULL.
823 * @param[in] lybctx LYB context.
824 * @return LY_ERR value.
825 */
826static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200827lyb_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 +0200828{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200829 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +0200830 uint32_t i;
831 LYB_HASH hash;
832 struct lyd_lyb_sib_ht *sib_ht;
833 struct lysc_node *first_sibling;
834
835 if (!schema) {
836 /* opaque node, write empty hash */
837 hash = 0;
838 LY_CHECK_RET(lyb_write(out, &hash, sizeof hash, lybctx));
839 return LY_SUCCESS;
840 }
841
842 /* create whole sibling HT if not already created and saved */
843 if (!*sibling_ht) {
844 /* get first schema data sibling (or input/output) */
845 first_sibling = (struct lysc_node *)lys_getnext(NULL, lysc_data_parent(schema), schema->module->compiled, 0);
846 LY_ARRAY_FOR(lybctx->sib_hts, u) {
847 if (lybctx->sib_hts[u].first_sibling == first_sibling) {
848 /* we have already created a hash table for these siblings */
849 *sibling_ht = lybctx->sib_hts[u].ht;
850 break;
851 }
852 }
853
854 if (!*sibling_ht) {
855 /* we must create sibling hash table */
856 LY_CHECK_RET(lyb_hash_siblings(first_sibling, sibling_ht));
857
858 /* and save it */
859 LY_ARRAY_NEW_RET(lybctx->ctx, lybctx->sib_hts, sib_ht, LY_EMEM);
860
861 sib_ht->first_sibling = first_sibling;
862 sib_ht->ht = *sibling_ht;
863 }
864 }
865
866 /* get our hash */
867 LY_CHECK_RET(lyb_hash_find(*sibling_ht, schema, &hash));
868
869 /* write the hash */
870 LY_CHECK_RET(lyb_write(out, &hash, sizeof hash, lybctx));
871
872 if (hash & LYB_HASH_COLLISION_ID) {
873 /* no collision for this hash, we are done */
874 return LY_SUCCESS;
875 }
876
877 /* written hash was a collision, write also all the preceding hashes */
Radek Krejci1e008d22020-08-17 11:37:37 +0200878 for (i = 0; !(hash & (LYB_HASH_COLLISION_ID >> i)); ++i) {}
Michal Vasko60ea6352020-06-29 13:39:39 +0200879
Michal Vaskod989ba02020-08-24 10:59:24 +0200880 for ( ; i; --i) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200881 hash = lyb_hash(schema, i - 1);
882 if (!hash) {
883 return LY_EINT;
884 }
885 assert(hash & (LYB_HASH_COLLISION_ID >> (i - 1)));
886
887 LY_CHECK_RET(lyb_write(out, &hash, sizeof hash, lybctx));
888 }
889
890 return LY_SUCCESS;
891}
892
893/**
894 * @brief Print data subtree.
895 *
896 * @param[in] out Out structure.
897 * @param[in] node Root node of the subtree to print.
898 * @param[in,out] sibling_ht Cached hash table for these data siblings, created if NULL.
899 * @param[in] lybctx LYB context.
900 * @return LY_ERR value.
901 */
902static LY_ERR
903lyb_print_subtree(struct ly_out *out, const struct lyd_node *node, struct hash_table **sibling_ht, struct lyd_lyb_ctx *lybctx)
904{
905 struct hash_table *child_ht = NULL;
906
907 /* register a new subtree */
Radek Krejci1798aae2020-07-14 13:26:06 +0200908 LY_CHECK_RET(lyb_write_start_subtree(out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200909
910 /* write model info first */
911 if (!node->schema && !((struct lyd_node_opaq *)node)->parent) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200912 LY_CHECK_RET(lyb_print_model(out, NULL, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200913 } else if (node->schema && !lysc_data_parent(node->schema)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200914 LY_CHECK_RET(lyb_print_model(out, node->schema->module, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200915 }
916
917 /* write schema hash */
Radek Krejci1798aae2020-07-14 13:26:06 +0200918 LY_CHECK_RET(lyb_print_schema_hash(out, (struct lysc_node *)node->schema, sibling_ht, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200919
920 /* write any metadata/attributes */
921 if (node->schema) {
922 LY_CHECK_RET(lyb_print_metadata(out, node, lybctx));
923 } else {
Radek Krejci1798aae2020-07-14 13:26:06 +0200924 LY_CHECK_RET(lyb_print_attributes(out, (struct lyd_node_opaq *)node, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200925 }
926
Michal Vaskoc5e866a2020-11-04 17:09:26 +0100927 /* write node flags */
928 LY_CHECK_RET(lyb_write_number(node->flags, sizeof node->flags, out, lybctx->lybctx));
929
Michal Vasko60ea6352020-06-29 13:39:39 +0200930 /* write node content */
931 if (!node->schema) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200932 LY_CHECK_RET(lyb_print_opaq((struct lyd_node_opaq *)node, out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200933 } else if (node->schema->nodetype & LYD_NODE_INNER) {
934 /* nothing to write */
935 } else if (node->schema->nodetype & LYD_NODE_TERM) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200936 LY_CHECK_RET(lyb_print_term((struct lyd_node_term *)node, out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200937 } else if (node->schema->nodetype & LYD_NODE_ANY) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200938 LY_CHECK_RET(lyb_print_anydata((struct lyd_node_any *)node, out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200939 } else {
Radek Krejci1798aae2020-07-14 13:26:06 +0200940 LOGINT_RET(lybctx->lybctx->ctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200941 }
942
943 /* recursively write all the descendants */
Radek Krejcia1c1e542020-09-29 16:06:52 +0200944 LY_LIST_FOR(lyd_child(node), node) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200945 LY_CHECK_RET(lyb_print_subtree(out, node, &child_ht, lybctx));
946 }
947
948 /* finish this subtree */
Radek Krejci1798aae2020-07-14 13:26:06 +0200949 LY_CHECK_RET(lyb_write_stop_subtree(out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200950
951 return LY_SUCCESS;
952}
953
954LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200955lyb_print_data(struct ly_out *out, const struct lyd_node *root, uint32_t options)
Michal Vasko60ea6352020-06-29 13:39:39 +0200956{
957 LY_ERR ret = LY_SUCCESS;
958 uint8_t zero = 0;
Michal Vasko60ea6352020-06-29 13:39:39 +0200959 struct hash_table *top_sibling_ht = NULL;
960 const struct lys_module *prev_mod = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +0200961 struct lyd_lyb_ctx *lybctx;
Michal Vaskob7be7a82020-08-20 09:09:04 +0200962 const struct ly_ctx *ctx = root ? LYD_CTX(root) : NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200963
Radek Krejci1798aae2020-07-14 13:26:06 +0200964 lybctx = calloc(1, sizeof *lybctx);
965 LY_CHECK_ERR_RET(!lybctx, LOGMEM(ctx), LY_EMEM);
966 lybctx->lybctx = calloc(1, sizeof *lybctx->lybctx);
967 LY_CHECK_ERR_RET(!lybctx, LOGMEM(ctx), LY_EMEM);
968
969 lybctx->print_options = options;
Michal Vasko60ea6352020-06-29 13:39:39 +0200970 if (root) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200971 lybctx->lybctx->ctx = ctx;
Michal Vasko60ea6352020-06-29 13:39:39 +0200972
973 if (root->schema && lysc_data_parent(root->schema)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200974 LOGERR(lybctx->lybctx->ctx, LY_EINVAL, "LYB printer supports only printing top-level nodes.");
Michal Vasko9acaf492020-08-13 09:05:58 +0200975 ret = LY_EINVAL;
976 goto cleanup;
Michal Vasko60ea6352020-06-29 13:39:39 +0200977 }
978 }
979
980 /* LYB magic number */
Michal Vasko63f3d842020-07-08 10:10:14 +0200981 LY_CHECK_GOTO(ret = lyb_print_magic_number(out), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200982
983 /* LYB header */
Michal Vasko63f3d842020-07-08 10:10:14 +0200984 LY_CHECK_GOTO(ret = lyb_print_header(out), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200985
986 /* all used models */
Radek Krejci1798aae2020-07-14 13:26:06 +0200987 LY_CHECK_GOTO(ret = lyb_print_data_models(out, root, lybctx->lybctx), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200988
989 LY_LIST_FOR(root, root) {
990 /* do not reuse sibling hash tables from different modules */
991 if (!root->schema || (root->schema->module != prev_mod)) {
992 top_sibling_ht = NULL;
993 prev_mod = root->schema ? root->schema->module : NULL;
994 }
995
Radek Krejci1798aae2020-07-14 13:26:06 +0200996 LY_CHECK_GOTO(ret = lyb_print_subtree(out, root, &top_sibling_ht, lybctx), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200997
Radek Krejci7931b192020-06-25 17:05:03 +0200998 if (!(options & LYD_PRINT_WITHSIBLINGS)) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200999 break;
1000 }
1001 }
1002
1003 /* ending zero byte */
Radek Krejci1798aae2020-07-14 13:26:06 +02001004 LY_CHECK_GOTO(ret = lyb_write(out, &zero, sizeof zero, lybctx->lybctx), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001005
1006cleanup:
Radek Krejci1798aae2020-07-14 13:26:06 +02001007 lyd_lyb_ctx_free((struct lyd_ctx *)lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001008 return ret;
1009}