blob: 4f606bdc869bcfcfe754fe4bb107d6e2e919c7c3 [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"
Radek Krejci859a15a2021-03-05 20:56:59 +010036#include "tree_edit.h"
Michal Vasko60ea6352020-06-29 13:39:39 +020037#include "tree_schema.h"
38#include "tree_schema_internal.h"
Michal Vasko6b5cb2a2020-11-11 19:11:21 +010039#include "xml.h"
Michal Vasko60ea6352020-06-29 13:39:39 +020040
41/**
42 * @brief Hash table equal callback for checking hash equality only.
Radek Krejci857189e2020-09-01 13:26:36 +020043 *
Michal Vasko62524a92021-02-26 10:08:50 +010044 * Implementation of ::lyht_value_equal_cb.
Michal Vasko60ea6352020-06-29 13:39:39 +020045 */
Radek Krejci857189e2020-09-01 13:26:36 +020046static ly_bool
47lyb_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 +020048{
49 /* for this purpose, if hash matches, the value does also, we do not want 2 values to have the same hash */
50 return 1;
51}
52
53/**
54 * @brief Hash table equal callback for checking value pointer equality only.
Radek Krejci857189e2020-09-01 13:26:36 +020055 *
Michal Vasko62524a92021-02-26 10:08:50 +010056 * Implementation of ::lyht_value_equal_cb.
Michal Vasko60ea6352020-06-29 13:39:39 +020057 */
Radek Krejci857189e2020-09-01 13:26:36 +020058static ly_bool
59lyb_ptr_equal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
Michal Vasko60ea6352020-06-29 13:39:39 +020060{
61 struct lysc_node *val1 = *(struct lysc_node **)val1_p;
62 struct lysc_node *val2 = *(struct lysc_node **)val2_p;
63
64 if (val1 == val2) {
65 return 1;
66 }
67 return 0;
68}
69
70/**
71 * @brief Check that sibling collision hash is safe to insert into hash table.
72 *
73 * @param[in] ht Hash table.
74 * @param[in] sibling Hashed sibling.
75 * @param[in] ht_col_id Sibling hash collision ID.
76 * @param[in] compare_col_id Last collision ID to compare with.
77 * @return LY_SUCCESS when the whole hash sequence does not collide,
78 * @return LY_EEXIST when the whole hash sequence sollides.
79 */
80static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +020081lyb_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 +020082{
Michal Vasko60ea6352020-06-29 13:39:39 +020083 struct lysc_node **col_node;
84
85 /* get the first node inserted with last hash col ID ht_col_id */
86 if (lyht_find(ht, &sibling, lyb_hash(sibling, ht_col_id), (void **)&col_node)) {
87 /* there is none. valid situation */
88 return LY_SUCCESS;
89 }
90
91 lyht_set_cb(ht, lyb_ptr_equal_cb);
92 do {
Radek Krejci1deb5be2020-08-26 16:43:36 +020093 int64_t j;
94 for (j = (int64_t)compare_col_id; j > -1; --j) {
Michal Vasko60ea6352020-06-29 13:39:39 +020095 if (lyb_hash(sibling, j) != lyb_hash(*col_node, j)) {
96 /* one non-colliding hash */
97 break;
98 }
99 }
100 if (j == -1) {
101 /* all whole hash sequences of nodes inserted with last hash col ID compare_col_id collide */
102 lyht_set_cb(ht, lyb_hash_equal_cb);
103 return LY_EEXIST;
104 }
105
106 /* get next node inserted with last hash col ID ht_col_id */
107 } while (!lyht_find_next(ht, col_node, lyb_hash(*col_node, ht_col_id), (void **)&col_node));
108
109 lyht_set_cb(ht, lyb_hash_equal_cb);
110 return LY_SUCCESS;
111}
112
113/**
114 * @brief Hash all the siblings and add them also into a separate hash table.
115 *
116 * @param[in] sibling Any sibling in all the siblings on one level.
117 * @param[out] ht_p Created hash table.
118 * @return LY_ERR value.
119 */
120static LY_ERR
121lyb_hash_siblings(struct lysc_node *sibling, struct hash_table **ht_p)
122{
123 struct hash_table *ht;
124 const struct lysc_node *parent;
125 const struct lys_module *mod;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200126 LYB_HASH i;
Michal Vasko1e0a80a2020-12-09 18:12:51 +0100127 uint32_t getnext_opts;
Michal Vasko60ea6352020-06-29 13:39:39 +0200128
129 ht = lyht_new(1, sizeof(struct lysc_node *), lyb_hash_equal_cb, NULL, 1);
130 LY_CHECK_ERR_RET(!ht, LOGMEM(sibling->module->ctx), LY_EMEM);
131
Michal Vasko1e0a80a2020-12-09 18:12:51 +0100132 getnext_opts = 0;
Michal Vaskod1e53b92021-01-28 13:11:06 +0100133 if (sibling->flags & LYS_IS_OUTPUT) {
Michal Vasko1e0a80a2020-12-09 18:12:51 +0100134 getnext_opts = LYS_GETNEXT_OUTPUT;
135 }
136
Michal Vasko60ea6352020-06-29 13:39:39 +0200137 parent = lysc_data_parent(sibling);
138 mod = sibling->module;
139
140 sibling = NULL;
Michal Vasko1e0a80a2020-12-09 18:12:51 +0100141 while ((sibling = (struct lysc_node *)lys_getnext(sibling, parent, mod->compiled, getnext_opts))) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200142 /* find the first non-colliding hash (or specifically non-colliding hash sequence) */
143 for (i = 0; i < LYB_HASH_BITS; ++i) {
144 /* check that we are not colliding with nodes inserted with a lower collision ID than ours */
Radek Krejci1deb5be2020-08-26 16:43:36 +0200145 int64_t j;
146 for (j = (int64_t)i - 1; j > -1; --j) {
147 if (lyb_hash_sequence_check(ht, sibling, (LYB_HASH)j, i)) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200148 break;
149 }
150 }
151 if (j > -1) {
152 /* some check failed, we must use a higher collision ID */
153 continue;
154 }
155
156 /* try to insert node with the current collision ID */
157 if (!lyht_insert_with_resize_cb(ht, &sibling, lyb_hash(sibling, i), lyb_ptr_equal_cb, NULL)) {
158 /* success, no collision */
159 break;
160 }
161
162 /* make sure we really cannot insert it with this hash col ID (meaning the whole hash sequence is colliding) */
163 if (i && !lyb_hash_sequence_check(ht, sibling, i, i)) {
164 /* it can be inserted after all, even though there is already a node with the same last collision ID */
165 lyht_set_cb(ht, lyb_ptr_equal_cb);
166 if (lyht_insert(ht, &sibling, lyb_hash(sibling, i), NULL)) {
167 LOGINT(sibling->module->ctx);
168 lyht_set_cb(ht, lyb_hash_equal_cb);
169 lyht_free(ht);
170 return LY_EINT;
171 }
172 lyht_set_cb(ht, lyb_hash_equal_cb);
173 break;
174 }
175 /* there is still another colliding schema node with the same hash sequence, try higher collision ID */
176 }
177
178 if (i == LYB_HASH_BITS) {
179 /* wow */
180 LOGINT(sibling->module->ctx);
181 lyht_free(ht);
182 return LY_EINT;
183 }
184 }
185
186 /* change val equal callback so that the HT is usable for finding value hashes */
187 lyht_set_cb(ht, lyb_ptr_equal_cb);
188
189 *ht_p = ht;
190 return LY_SUCCESS;
191}
192
193/**
194 * @brief Find node hash in a hash table.
195 *
196 * @param[in] ht Hash table to search in.
197 * @param[in] node Node to find.
198 * @param[out] hash_p First non-colliding hash found.
199 * @return LY_ERR value.
200 */
201static LY_ERR
202lyb_hash_find(struct hash_table *ht, struct lysc_node *node, LYB_HASH *hash_p)
203{
204 LYB_HASH hash;
205 uint32_t i;
206
207 for (i = 0; i < LYB_HASH_BITS; ++i) {
208 hash = lyb_hash(node, i);
209 if (!hash) {
210 LOGINT_RET(node->module->ctx);
211 }
212
213 if (!lyht_find(ht, &node, hash, NULL)) {
214 /* success, no collision */
215 break;
216 }
217 }
218 /* cannot happen, we already calculated the hash */
219 if (i == LYB_HASH_BITS) {
220 LOGINT_RET(node->module->ctx);
221 }
222
223 *hash_p = hash;
224 return LY_SUCCESS;
225}
226
227/**
228 * @brief Write LYB data fully handling the metadata.
229 *
230 * @param[in] out Out structure.
231 * @param[in] buf Source buffer.
232 * @param[in] count Number of bytes to write.
233 * @param[in] lybctx LYB context.
234 * @return LY_ERR value.
235 */
236static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200237lyb_write(struct ly_out *out, const uint8_t *buf, size_t count, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200238{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200239 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +0200240 struct lyd_lyb_subtree *full, *iter;
Michal Vasko5233e962020-08-14 14:26:20 +0200241 size_t to_write;
Michal Vasko60ea6352020-06-29 13:39:39 +0200242 uint8_t meta_buf[LYB_META_BYTES];
243
244 while (1) {
245 /* check for full data chunks */
246 to_write = count;
247 full = NULL;
248 LY_ARRAY_FOR(lybctx->subtrees, u) {
249 /* we want the innermost chunks resolved first, so replace previous full chunks */
250 if (lybctx->subtrees[u].written + to_write >= LYB_SIZE_MAX) {
251 /* full chunk, do not write more than allowed */
252 to_write = LYB_SIZE_MAX - lybctx->subtrees[u].written;
253 full = &lybctx->subtrees[u];
254 }
255 }
256
257 if (!full && !count) {
258 break;
259 }
260
261 /* we are actually writing some data, not just finishing another chunk */
262 if (to_write) {
Michal Vasko5233e962020-08-14 14:26:20 +0200263 LY_CHECK_RET(ly_write_(out, (char *)buf, to_write));
Michal Vasko60ea6352020-06-29 13:39:39 +0200264
265 LY_ARRAY_FOR(lybctx->subtrees, u) {
266 /* increase all written counters */
Michal Vasko5233e962020-08-14 14:26:20 +0200267 lybctx->subtrees[u].written += to_write;
Michal Vasko60ea6352020-06-29 13:39:39 +0200268 assert(lybctx->subtrees[u].written <= LYB_SIZE_MAX);
269 }
270 /* decrease count/buf */
Michal Vasko5233e962020-08-14 14:26:20 +0200271 count -= to_write;
272 buf += to_write;
Michal Vasko60ea6352020-06-29 13:39:39 +0200273 }
274
275 if (full) {
276 /* write the meta information (inner chunk count and chunk size) */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100277 meta_buf[0] = full->written & LYB_BYTE_MASK;
278 meta_buf[1] = full->inner_chunks & LYB_BYTE_MASK;
Michal Vasko5233e962020-08-14 14:26:20 +0200279 LY_CHECK_RET(ly_write_skipped(out, full->position, (char *)meta_buf, LYB_META_BYTES));
Michal Vasko60ea6352020-06-29 13:39:39 +0200280
281 /* zero written and inner chunks */
282 full->written = 0;
283 full->inner_chunks = 0;
284
285 /* skip space for another chunk size */
Michal Vasko5233e962020-08-14 14:26:20 +0200286 LY_CHECK_RET(ly_write_skip(out, LYB_META_BYTES, &full->position));
Michal Vasko60ea6352020-06-29 13:39:39 +0200287
288 /* increase inner chunk count */
289 for (iter = &lybctx->subtrees[0]; iter != full; ++iter) {
290 if (iter->inner_chunks == LYB_INCHUNK_MAX) {
291 LOGINT(lybctx->ctx);
292 return LY_EINT;
293 }
294 ++iter->inner_chunks;
295 }
296 }
297 }
298
299 return LY_SUCCESS;
300}
301
302/**
303 * @brief Stop the current subtree - write its final metadata.
304 *
305 * @param[in] out Out structure.
306 * @param[in] lybctx LYB context.
307 * @return LY_ERR value.
308 */
309static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200310lyb_write_stop_subtree(struct ly_out *out, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200311{
Michal Vasko60ea6352020-06-29 13:39:39 +0200312 uint8_t meta_buf[LYB_META_BYTES];
313
314 /* write the meta chunk information */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100315 meta_buf[0] = LYB_LAST_SUBTREE(lybctx).written & LYB_BYTE_MASK;
316 meta_buf[1] = LYB_LAST_SUBTREE(lybctx).inner_chunks & LYB_BYTE_MASK;
Michal Vasko5233e962020-08-14 14:26:20 +0200317 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 +0200318
319 LY_ARRAY_DECREMENT(lybctx->subtrees);
320 return LY_SUCCESS;
321}
322
323/**
324 * @brief Start a new subtree - skip bytes for its metadata.
325 *
326 * @param[in] out Out structure.
327 * @param[in] lybctx LYB context.
328 * @return LY_ERR value.
329 */
330static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200331lyb_write_start_subtree(struct ly_out *out, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200332{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200333 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +0200334
Radek Krejcic7d13e32020-12-09 12:32:24 +0100335 u = LY_ARRAY_COUNT(lybctx->subtrees);
Michal Vasko60ea6352020-06-29 13:39:39 +0200336 if (u == lybctx->subtree_size) {
337 LY_ARRAY_CREATE_RET(lybctx->ctx, lybctx->subtrees, u + LYB_SUBTREE_STEP, LY_EMEM);
338 lybctx->subtree_size = u + LYB_SUBTREE_STEP;
339 }
340
341 LY_ARRAY_INCREMENT(lybctx->subtrees);
342 LYB_LAST_SUBTREE(lybctx).written = 0;
343 LYB_LAST_SUBTREE(lybctx).inner_chunks = 0;
344
345 /* another inner chunk */
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200346 for (u = 0; u < LY_ARRAY_COUNT(lybctx->subtrees) - 1; ++u) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200347 if (lybctx->subtrees[u].inner_chunks == LYB_INCHUNK_MAX) {
348 LOGINT(lybctx->ctx);
Michal Vasko5233e962020-08-14 14:26:20 +0200349 return LY_EINT;
Michal Vasko60ea6352020-06-29 13:39:39 +0200350 }
351 ++lybctx->subtrees[u].inner_chunks;
352 }
353
Michal Vasko5233e962020-08-14 14:26:20 +0200354 LY_CHECK_RET(ly_write_skip(out, LYB_META_BYTES, &LYB_LAST_SUBTREE(lybctx).position));
Michal Vasko60ea6352020-06-29 13:39:39 +0200355
356 return LY_SUCCESS;
357}
358
359/**
360 * @brief Write a number.
361 *
362 * @param[in] num Number to write.
363 * @param[in] bytes Actual accessible bytes of @p num.
364 * @param[in] out Out structure.
365 * @param[in] lybctx LYB context.
366 * @return LY_ERR value.
367 */
368static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200369lyb_write_number(uint64_t num, size_t bytes, struct ly_out *out, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200370{
371 /* correct byte order */
372 num = htole64(num);
373
374 return lyb_write(out, (uint8_t *)&num, bytes, lybctx);
375}
376
377/**
378 * @brief Write a string.
379 *
380 * @param[in] str String to write.
381 * @param[in] str_len Length of @p str.
382 * @param[in] with_length Whether to precede the string with its length.
383 * @param[in] out Out structure.
384 * @param[in] lybctx LYB context.
385 * @return LY_ERR value.
386 */
387static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200388lyb_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 +0200389{
Michal Vasko60ea6352020-06-29 13:39:39 +0200390 if (!str) {
391 str = "";
Michal Vasko30667352020-08-13 09:06:14 +0200392 LY_CHECK_ERR_RET(str_len, LOGINT(lybctx->ctx), LY_EINT);
Michal Vasko60ea6352020-06-29 13:39:39 +0200393 }
394 if (!str_len) {
395 str_len = strlen(str);
396 }
397
398 if (with_length) {
399 /* print length on 2 bytes */
400 if (str_len > UINT16_MAX) {
401 LOGINT(lybctx->ctx);
402 return LY_EINT;
403 }
404 LY_CHECK_RET(lyb_write_number(str_len, 2, out, lybctx));
405 }
406
Michal Vasko63f3d842020-07-08 10:10:14 +0200407 LY_CHECK_RET(lyb_write(out, (const uint8_t *)str, str_len, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200408
409 return LY_SUCCESS;
410}
411
412/**
413 * @brief Print YANG module info.
414 *
415 * @param[in] out Out structure.
416 * @param[in] mod Module to print.
417 * @param[in] lybctx LYB context.
418 * @return LY_ERR value.
419 */
420static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200421lyb_print_model(struct ly_out *out, const struct lys_module *mod, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200422{
Michal Vasko60ea6352020-06-29 13:39:39 +0200423 uint16_t revision;
424
425 /* model name length and model name */
426 if (mod) {
427 LY_CHECK_RET(lyb_write_string(mod->name, 0, 1, out, lybctx));
428 } else {
429 LY_CHECK_RET(lyb_write_string("", 0, 1, out, lybctx));
430 }
431
432 /* model revision as XXXX XXXX XXXX XXXX (2B) (year is offset from 2000)
433 * YYYY YYYM MMMD DDDD */
434 revision = 0;
435 if (mod && mod->revision) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200436 int r = atoi(mod->revision);
Radek Krejcif13b87b2020-12-01 22:02:17 +0100437 r -= LYB_REV_YEAR_OFFSET;
438 r <<= LYB_REV_YEAR_SHIFT;
Michal Vasko60ea6352020-06-29 13:39:39 +0200439
440 revision |= r;
441
Radek Krejcif13b87b2020-12-01 22:02:17 +0100442 r = atoi(mod->revision + ly_strlen_const("YYYY-"));
443 r <<= LYB_REV_MONTH_SHIFT;
Michal Vasko60ea6352020-06-29 13:39:39 +0200444
445 revision |= r;
446
Radek Krejcif13b87b2020-12-01 22:02:17 +0100447 r = atoi(mod->revision + ly_strlen_const("YYYY-MM-"));
Michal Vasko60ea6352020-06-29 13:39:39 +0200448
449 revision |= r;
450 }
451 LY_CHECK_RET(lyb_write_number(revision, sizeof revision, out, lybctx));
452
453 return LY_SUCCESS;
454}
455
456/**
457 * @brief Print all used YANG modules.
458 *
459 * @param[in] out Out structure.
460 * @param[in] root Data root.
461 * @param[in] lybctx LYB context.
462 * @return LY_ERR value.
463 */
464static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200465lyb_print_data_models(struct ly_out *out, const struct lyd_node *root, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200466{
467 struct ly_set *set;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200468 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +0200469 LY_ERR ret = LY_SUCCESS;
470 struct lys_module *mod;
471 const struct lyd_node *node;
472 uint32_t i;
473
Radek Krejciba03a5a2020-08-27 14:40:41 +0200474 LY_CHECK_RET(ly_set_new(&set));
Michal Vasko60ea6352020-06-29 13:39:39 +0200475
476 /* collect all data node modules */
477 LY_LIST_FOR(root, node) {
478 if (!node->schema) {
479 continue;
480 }
481
482 mod = node->schema->module;
Radek Krejciba03a5a2020-08-27 14:40:41 +0200483 ret = ly_set_add(set, mod, 0, NULL);
484 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200485
486 /* add also their modules deviating or augmenting them */
Michal Vasko7f45cf22020-10-01 12:49:44 +0200487 LY_ARRAY_FOR(mod->deviated_by, u) {
488 ret = ly_set_add(set, mod->deviated_by[u], 0, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200489 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200490 }
Michal Vasko7f45cf22020-10-01 12:49:44 +0200491 LY_ARRAY_FOR(mod->augmented_by, u) {
492 ret = ly_set_add(set, mod->augmented_by[u], 0, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200493 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200494 }
495 }
496
497 /* now write module count on 2 bytes */
498 LY_CHECK_GOTO(ret = lyb_write_number(set->count, 2, out, lybctx), cleanup);
499
500 /* and all the used models */
501 for (i = 0; i < set->count; ++i) {
502 LY_CHECK_GOTO(ret = lyb_print_model(out, set->objs[i], lybctx), cleanup);
503 }
504
505cleanup:
506 ly_set_free(set, NULL);
507 return ret;
508}
509
510/**
511 * @brief Print LYB magic number.
512 *
513 * @param[in] out Out structure.
Michal Vasko60ea6352020-06-29 13:39:39 +0200514 * @return LY_ERR value.
515 */
516static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200517lyb_print_magic_number(struct ly_out *out)
Michal Vasko60ea6352020-06-29 13:39:39 +0200518{
Michal Vasko60ea6352020-06-29 13:39:39 +0200519 /* 'l', 'y', 'b' - 0x6c7962 */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100520 char magic_number[] = {'l', 'y', 'b'};
Michal Vasko60ea6352020-06-29 13:39:39 +0200521
Radek Krejcidefd4d72020-12-01 12:20:30 +0100522 LY_CHECK_RET(ly_write_(out, magic_number, 3));
Michal Vasko60ea6352020-06-29 13:39:39 +0200523
524 return LY_SUCCESS;
525}
526
527/**
528 * @brief Print LYB header.
529 *
530 * @param[in] out Out structure.
Michal Vasko60ea6352020-06-29 13:39:39 +0200531 * @return LY_ERR value.
532 */
533static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200534lyb_print_header(struct ly_out *out)
Michal Vasko60ea6352020-06-29 13:39:39 +0200535{
Michal Vasko60ea6352020-06-29 13:39:39 +0200536 uint8_t byte = 0;
537
538 /* version, future flags */
539 byte |= LYB_VERSION_NUM;
540
Michal Vasko5233e962020-08-14 14:26:20 +0200541 LY_CHECK_RET(ly_write_(out, (char *)&byte, 1));
Michal Vasko60ea6352020-06-29 13:39:39 +0200542
543 return LY_SUCCESS;
544}
545
546/**
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100547 * @brief Print prefix data.
Michal Vasko60ea6352020-06-29 13:39:39 +0200548 *
549 * @param[in] out Out structure.
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100550 * @param[in] format Value prefix format.
Radek Krejciec9ad602021-01-04 10:46:30 +0100551 * @param[in] prefix_data Format-specific data to print:
552 * LY_PREF_SCHEMA - const struct lysp_module * (module used for resolving prefixes from imports)
553 * LY_PREF_SCHEMA_RESOLVED - struct lyd_value_prefix * (sized array of pairs: prefix - module)
554 * LY_PREF_XML - const struct ly_set * (set with defined namespaces stored as ::lyxml_ns)
555 * LY_PREF_JSON - NULL
Michal Vasko60ea6352020-06-29 13:39:39 +0200556 * @param[in] lybctx LYB context.
557 * @return LY_ERR value.
558 */
559static LY_ERR
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100560lyb_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 +0200561{
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100562 const struct ly_set *set;
563 const struct lyxml_ns *ns;
564 uint32_t i;
Michal Vasko60ea6352020-06-29 13:39:39 +0200565
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100566 switch (format) {
567 case LY_PREF_XML:
568 set = prefix_data;
569 if (!set) {
570 /* no prefix data */
571 i = 0;
572 LY_CHECK_RET(lyb_write(out, (uint8_t *)&i, 1, lybctx));
573 break;
574 }
575 if (set->count > UINT8_MAX) {
576 LOGERR(lybctx->ctx, LY_EINT, "Maximum supported number of prefixes is %u.", UINT8_MAX);
577 return LY_EINT;
578 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200579
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100580 /* write number of prefixes on 1 byte */
581 LY_CHECK_RET(lyb_write(out, (uint8_t *)&set->count, 1, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200582
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100583 /* write all the prefixes */
584 for (i = 0; i < set->count; ++i) {
585 ns = set->objs[i];
Michal Vasko60ea6352020-06-29 13:39:39 +0200586
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100587 /* prefix */
588 LY_CHECK_RET(lyb_write_string(ns->prefix, 0, 1, out, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200589
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100590 /* namespace */
591 LY_CHECK_RET(lyb_write_string(ns->uri, 0, 1, out, lybctx));
592 }
593 break;
594 case LY_PREF_JSON:
595 /* nothing to print */
596 break;
597 default:
598 LOGINT_RET(lybctx->ctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200599 }
600
601 return LY_SUCCESS;
602}
603
604/**
605 * @brief Print opaque node.
606 *
607 * @param[in] opaq Node to print.
608 * @param[in] out Out structure.
609 * @param[in] lybctx LYB context.
610 * @return LY_ERR value.
611 */
612static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200613lyb_print_opaq(struct lyd_node_opaq *opaq, struct ly_out *out, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200614{
615 /* prefix */
Michal Vaskoad92b672020-11-12 13:11:31 +0100616 LY_CHECK_RET(lyb_write_string(opaq->name.prefix, 0, 1, out, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200617
Radek Krejci1798aae2020-07-14 13:26:06 +0200618 /* module reference */
Michal Vaskoad92b672020-11-12 13:11:31 +0100619 LY_CHECK_RET(lyb_write_string(opaq->name.module_name, 0, 1, out, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200620
621 /* name */
Michal Vaskoad92b672020-11-12 13:11:31 +0100622 LY_CHECK_RET(lyb_write_string(opaq->name.name, 0, 1, out, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200623
Michal Vasko60ea6352020-06-29 13:39:39 +0200624 /* format */
625 LY_CHECK_RET(lyb_write_number(opaq->format, 1, out, lybctx));
626
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100627 /* value prefixes */
628 LY_CHECK_RET(lyb_print_prefix_data(out, opaq->format, opaq->val_prefix_data, lybctx));
629
Michal Vasko60ea6352020-06-29 13:39:39 +0200630 /* value */
631 LY_CHECK_RET(lyb_write_string(opaq->value, 0, 0, out, lybctx));
632
633 return LY_SUCCESS;
634}
635
636/**
637 * @brief Print anydata node.
638 *
639 * @param[in] anydata Node to print.
640 * @param[in] out Out structure.
641 * @param[in] lybctx LYB context.
642 * @return LY_ERR value.
643 */
644static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200645lyb_print_anydata(struct lyd_node_any *anydata, struct ly_out *out, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200646{
647 LY_ERR ret = LY_SUCCESS;
648 LYD_ANYDATA_VALUETYPE value_type;
649 int len;
650 char *buf = NULL;
651 const char *str;
652 struct ly_out *out2 = NULL;
653
654 if (anydata->value_type == LYD_ANYDATA_DATATREE) {
655 /* will be printed as a nested LYB data tree */
656 value_type = LYD_ANYDATA_LYB;
657 } else {
658 value_type = anydata->value_type;
659 }
660
661 /* first byte is type */
662 LY_CHECK_GOTO(ret = lyb_write(out, (uint8_t *)&value_type, sizeof value_type, lybctx), cleanup);
663
664 if (anydata->value_type == LYD_ANYDATA_DATATREE) {
665 /* print LYB data tree to memory */
666 LY_CHECK_GOTO(ret = ly_out_new_memory(&buf, 0, &out2), cleanup);
Radek Krejci7931b192020-06-25 17:05:03 +0200667 LY_CHECK_GOTO(ret = lyb_print_data(out2, anydata->value.tree, LYD_PRINT_WITHSIBLINGS), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200668
669 len = lyd_lyb_data_length(buf);
670 assert(len != -1);
671 str = buf;
672 } else if (anydata->value_type == LYD_ANYDATA_LYB) {
673 len = lyd_lyb_data_length(anydata->value.mem);
674 assert(len != -1);
675 str = anydata->value.mem;
676 } else {
677 len = strlen(anydata->value.str);
678 str = anydata->value.str;
679 }
680
681 /* followed by the content */
682 LY_CHECK_GOTO(ret = lyb_write_string(str, (size_t)len, 0, out, lybctx), cleanup);
683
684cleanup:
685 ly_out_free(out2, NULL, 1);
686 return ret;
687}
688
689/**
690 * @brief Print term node.
691 *
692 * @param[in] term Node to print.
693 * @param[in] out Out structure.
694 * @param[in] lybctx LYB context.
695 * @return LY_ERR value.
696 */
697static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200698lyb_print_term(struct lyd_node_term *term, struct ly_out *out, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200699{
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200700 /* print the value */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200701 return lyb_write_string(LYD_CANON_VALUE(term), 0, 0, out, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200702}
703
704/**
705 * @brief Print YANG node metadata.
706 *
707 * @param[in] out Out structure.
708 * @param[in] node Data node whose metadata to print.
709 * @param[in] lybctx LYB context.
710 * @return LY_ERR value.
711 */
712static LY_ERR
713lyb_print_metadata(struct ly_out *out, const struct lyd_node *node, struct lyd_lyb_ctx *lybctx)
714{
Michal Vasko60ea6352020-06-29 13:39:39 +0200715 uint8_t count = 0;
716 const struct lys_module *wd_mod = NULL;
717 struct lyd_meta *iter;
Michal Vasko60ea6352020-06-29 13:39:39 +0200718
719 /* with-defaults */
720 if (node->schema->nodetype & LYD_NODE_TERM) {
Radek Krejci7931b192020-06-25 17:05:03 +0200721 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 +0200722 ((lybctx->print_options & LYD_PRINT_WD_ALL_TAG) && lyd_is_default(node))) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200723 /* we have implicit OR explicit default node, print attribute only if context include with-defaults schema */
724 wd_mod = ly_ctx_get_module_latest(node->schema->module->ctx, "ietf-netconf-with-defaults");
725 }
726 }
727
728 /* count metadata */
729 if (wd_mod) {
730 ++count;
731 }
732 for (iter = node->meta; iter; iter = iter->next) {
733 if (count == UINT8_MAX) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200734 LOGERR(lybctx->lybctx->ctx, LY_EINT, "Maximum supported number of data node metadata is %u.", UINT8_MAX);
Michal Vasko60ea6352020-06-29 13:39:39 +0200735 return LY_EINT;
736 }
737 ++count;
738 }
739
740 /* write number of metadata on 1 byte */
Radek Krejci1798aae2020-07-14 13:26:06 +0200741 LY_CHECK_RET(lyb_write(out, &count, 1, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200742
743 if (wd_mod) {
744 /* write the "default" metadata */
Radek Krejci1798aae2020-07-14 13:26:06 +0200745 LY_CHECK_RET(lyb_write_start_subtree(out, lybctx->lybctx));
746 LY_CHECK_RET(lyb_print_model(out, wd_mod, lybctx->lybctx));
747 LY_CHECK_RET(lyb_write_string("default", 0, 1, out, lybctx->lybctx));
748 LY_CHECK_RET(lyb_write_string("true", 0, 0, out, lybctx->lybctx));
749 LY_CHECK_RET(lyb_write_stop_subtree(out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200750 }
751
752 /* write all the node metadata */
753 LY_LIST_FOR(node->meta, iter) {
754 /* each metadata is a subtree */
Radek Krejci1798aae2020-07-14 13:26:06 +0200755 LY_CHECK_RET(lyb_write_start_subtree(out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200756
757 /* model */
Radek Krejci1798aae2020-07-14 13:26:06 +0200758 LY_CHECK_RET(lyb_print_model(out, iter->annotation->module, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200759
760 /* annotation name with length */
Radek Krejci1798aae2020-07-14 13:26:06 +0200761 LY_CHECK_RET(lyb_write_string(iter->name, 0, 1, out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200762
Michal Vasko60ea6352020-06-29 13:39:39 +0200763 /* metadata value */
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200764 LY_CHECK_RET(lyb_write_string(iter->value.canonical, 0, 0, out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200765
766 /* finish metadata subtree */
Radek Krejci1798aae2020-07-14 13:26:06 +0200767 LY_CHECK_RET(lyb_write_stop_subtree(out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200768 }
769
770 return LY_SUCCESS;
771}
772
773/**
774 * @brief Print opaque node attributes.
775 *
776 * @param[in] out Out structure.
777 * @param[in] node Opaque node whose attributes to print.
778 * @param[in] lybctx LYB context.
779 * @return LY_ERR value.
780 */
781static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200782lyb_print_attributes(struct ly_out *out, const struct lyd_node_opaq *node, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200783{
784 uint8_t count = 0;
Radek Krejci5536d282020-08-04 23:27:44 +0200785 struct lyd_attr *iter;
Michal Vasko60ea6352020-06-29 13:39:39 +0200786
787 for (iter = node->attr; iter; iter = iter->next) {
788 if (count == UINT8_MAX) {
789 LOGERR(lybctx->ctx, LY_EINT, "Maximum supported number of data node attributes is %u.", UINT8_MAX);
790 return LY_EINT;
791 }
792 ++count;
793 }
794
795 /* write number of attributes on 1 byte */
796 LY_CHECK_RET(lyb_write(out, &count, 1, lybctx));
797
798 /* write all the attributes */
799 LY_LIST_FOR(node->attr, iter) {
800 /* each attribute is a subtree */
801 LY_CHECK_RET(lyb_write_start_subtree(out, lybctx));
802
803 /* prefix */
Michal Vaskoad92b672020-11-12 13:11:31 +0100804 LY_CHECK_RET(lyb_write_string(iter->name.prefix, 0, 1, out, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200805
806 /* namespace */
Michal Vaskoad92b672020-11-12 13:11:31 +0100807 LY_CHECK_RET(lyb_write_string(iter->name.module_name, 0, 1, out, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200808
809 /* name */
Michal Vaskoad92b672020-11-12 13:11:31 +0100810 LY_CHECK_RET(lyb_write_string(iter->name.name, 0, 1, out, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200811
Michal Vasko60ea6352020-06-29 13:39:39 +0200812 /* format */
813 LY_CHECK_RET(lyb_write_number(iter->format, 1, out, lybctx));
814
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100815 /* value prefixes */
816 LY_CHECK_RET(lyb_print_prefix_data(out, iter->format, iter->val_prefix_data, lybctx));
817
Michal Vasko60ea6352020-06-29 13:39:39 +0200818 /* value */
819 LY_CHECK_RET(lyb_write_string(iter->value, 0, 0, out, lybctx));
820
821 /* finish attribute subtree */
822 LY_CHECK_RET(lyb_write_stop_subtree(out, lybctx));
823 }
824
825 return LY_SUCCESS;
826}
827
828/**
829 * @brief Print schema node hash.
830 *
831 * @param[in] out Out structure.
832 * @param[in] schema Schema node whose hash to print.
833 * @param[in,out] sibling_ht Cached hash table for these siblings, created if NULL.
834 * @param[in] lybctx LYB context.
835 * @return LY_ERR value.
836 */
837static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200838lyb_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 +0200839{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200840 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +0200841 uint32_t i;
842 LYB_HASH hash;
843 struct lyd_lyb_sib_ht *sib_ht;
844 struct lysc_node *first_sibling;
845
846 if (!schema) {
847 /* opaque node, write empty hash */
848 hash = 0;
849 LY_CHECK_RET(lyb_write(out, &hash, sizeof hash, lybctx));
850 return LY_SUCCESS;
851 }
852
853 /* create whole sibling HT if not already created and saved */
854 if (!*sibling_ht) {
Michal Vasko1e0a80a2020-12-09 18:12:51 +0100855 /* get first schema data sibling */
856 first_sibling = (struct lysc_node *)lys_getnext(NULL, lysc_data_parent(schema), schema->module->compiled,
Michal Vaskod1e53b92021-01-28 13:11:06 +0100857 (schema->flags & LYS_IS_OUTPUT) ? LYS_GETNEXT_OUTPUT : 0);
Michal Vasko60ea6352020-06-29 13:39:39 +0200858 LY_ARRAY_FOR(lybctx->sib_hts, u) {
859 if (lybctx->sib_hts[u].first_sibling == first_sibling) {
860 /* we have already created a hash table for these siblings */
861 *sibling_ht = lybctx->sib_hts[u].ht;
862 break;
863 }
864 }
865
866 if (!*sibling_ht) {
867 /* we must create sibling hash table */
868 LY_CHECK_RET(lyb_hash_siblings(first_sibling, sibling_ht));
869
870 /* and save it */
871 LY_ARRAY_NEW_RET(lybctx->ctx, lybctx->sib_hts, sib_ht, LY_EMEM);
872
873 sib_ht->first_sibling = first_sibling;
874 sib_ht->ht = *sibling_ht;
875 }
876 }
877
878 /* get our hash */
879 LY_CHECK_RET(lyb_hash_find(*sibling_ht, schema, &hash));
880
881 /* write the hash */
882 LY_CHECK_RET(lyb_write(out, &hash, sizeof hash, lybctx));
883
884 if (hash & LYB_HASH_COLLISION_ID) {
885 /* no collision for this hash, we are done */
886 return LY_SUCCESS;
887 }
888
889 /* written hash was a collision, write also all the preceding hashes */
Radek Krejci1e008d22020-08-17 11:37:37 +0200890 for (i = 0; !(hash & (LYB_HASH_COLLISION_ID >> i)); ++i) {}
Michal Vasko60ea6352020-06-29 13:39:39 +0200891
Michal Vaskod989ba02020-08-24 10:59:24 +0200892 for ( ; i; --i) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200893 hash = lyb_hash(schema, i - 1);
894 if (!hash) {
895 return LY_EINT;
896 }
897 assert(hash & (LYB_HASH_COLLISION_ID >> (i - 1)));
898
899 LY_CHECK_RET(lyb_write(out, &hash, sizeof hash, lybctx));
900 }
901
902 return LY_SUCCESS;
903}
904
905/**
906 * @brief Print data subtree.
907 *
908 * @param[in] out Out structure.
909 * @param[in] node Root node of the subtree to print.
910 * @param[in,out] sibling_ht Cached hash table for these data siblings, created if NULL.
911 * @param[in] lybctx LYB context.
912 * @return LY_ERR value.
913 */
914static LY_ERR
915lyb_print_subtree(struct ly_out *out, const struct lyd_node *node, struct hash_table **sibling_ht, struct lyd_lyb_ctx *lybctx)
916{
917 struct hash_table *child_ht = NULL;
918
919 /* register a new subtree */
Radek Krejci1798aae2020-07-14 13:26:06 +0200920 LY_CHECK_RET(lyb_write_start_subtree(out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200921
922 /* write model info first */
Michal Vasko9e685082021-01-29 14:49:09 +0100923 if (!node->schema && !lyd_parent(node)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200924 LY_CHECK_RET(lyb_print_model(out, NULL, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200925 } else if (node->schema && !lysc_data_parent(node->schema)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200926 LY_CHECK_RET(lyb_print_model(out, node->schema->module, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200927 }
928
929 /* write schema hash */
Radek Krejci1798aae2020-07-14 13:26:06 +0200930 LY_CHECK_RET(lyb_print_schema_hash(out, (struct lysc_node *)node->schema, sibling_ht, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200931
932 /* write any metadata/attributes */
933 if (node->schema) {
934 LY_CHECK_RET(lyb_print_metadata(out, node, lybctx));
935 } else {
Radek Krejci1798aae2020-07-14 13:26:06 +0200936 LY_CHECK_RET(lyb_print_attributes(out, (struct lyd_node_opaq *)node, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200937 }
938
Michal Vaskoc5e866a2020-11-04 17:09:26 +0100939 /* write node flags */
940 LY_CHECK_RET(lyb_write_number(node->flags, sizeof node->flags, out, lybctx->lybctx));
941
Michal Vasko60ea6352020-06-29 13:39:39 +0200942 /* write node content */
943 if (!node->schema) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200944 LY_CHECK_RET(lyb_print_opaq((struct lyd_node_opaq *)node, out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200945 } else if (node->schema->nodetype & LYD_NODE_INNER) {
946 /* nothing to write */
947 } else if (node->schema->nodetype & LYD_NODE_TERM) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200948 LY_CHECK_RET(lyb_print_term((struct lyd_node_term *)node, out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200949 } else if (node->schema->nodetype & LYD_NODE_ANY) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200950 LY_CHECK_RET(lyb_print_anydata((struct lyd_node_any *)node, out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200951 } else {
Radek Krejci1798aae2020-07-14 13:26:06 +0200952 LOGINT_RET(lybctx->lybctx->ctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200953 }
954
955 /* recursively write all the descendants */
Radek Krejcia1c1e542020-09-29 16:06:52 +0200956 LY_LIST_FOR(lyd_child(node), node) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200957 LY_CHECK_RET(lyb_print_subtree(out, node, &child_ht, lybctx));
958 }
959
960 /* finish this subtree */
Radek Krejci1798aae2020-07-14 13:26:06 +0200961 LY_CHECK_RET(lyb_write_stop_subtree(out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200962
963 return LY_SUCCESS;
964}
965
966LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200967lyb_print_data(struct ly_out *out, const struct lyd_node *root, uint32_t options)
Michal Vasko60ea6352020-06-29 13:39:39 +0200968{
969 LY_ERR ret = LY_SUCCESS;
970 uint8_t zero = 0;
Michal Vasko60ea6352020-06-29 13:39:39 +0200971 struct hash_table *top_sibling_ht = NULL;
972 const struct lys_module *prev_mod = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +0200973 struct lyd_lyb_ctx *lybctx;
Michal Vaskob7be7a82020-08-20 09:09:04 +0200974 const struct ly_ctx *ctx = root ? LYD_CTX(root) : NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200975
Radek Krejci1798aae2020-07-14 13:26:06 +0200976 lybctx = calloc(1, sizeof *lybctx);
977 LY_CHECK_ERR_RET(!lybctx, LOGMEM(ctx), LY_EMEM);
978 lybctx->lybctx = calloc(1, sizeof *lybctx->lybctx);
979 LY_CHECK_ERR_RET(!lybctx, LOGMEM(ctx), LY_EMEM);
980
981 lybctx->print_options = options;
Michal Vasko60ea6352020-06-29 13:39:39 +0200982 if (root) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200983 lybctx->lybctx->ctx = ctx;
Michal Vasko60ea6352020-06-29 13:39:39 +0200984
985 if (root->schema && lysc_data_parent(root->schema)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200986 LOGERR(lybctx->lybctx->ctx, LY_EINVAL, "LYB printer supports only printing top-level nodes.");
Michal Vasko9acaf492020-08-13 09:05:58 +0200987 ret = LY_EINVAL;
988 goto cleanup;
Michal Vasko60ea6352020-06-29 13:39:39 +0200989 }
990 }
991
992 /* LYB magic number */
Michal Vasko63f3d842020-07-08 10:10:14 +0200993 LY_CHECK_GOTO(ret = lyb_print_magic_number(out), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200994
995 /* LYB header */
Michal Vasko63f3d842020-07-08 10:10:14 +0200996 LY_CHECK_GOTO(ret = lyb_print_header(out), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200997
998 /* all used models */
Radek Krejci1798aae2020-07-14 13:26:06 +0200999 LY_CHECK_GOTO(ret = lyb_print_data_models(out, root, lybctx->lybctx), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001000
1001 LY_LIST_FOR(root, root) {
1002 /* do not reuse sibling hash tables from different modules */
1003 if (!root->schema || (root->schema->module != prev_mod)) {
1004 top_sibling_ht = NULL;
1005 prev_mod = root->schema ? root->schema->module : NULL;
1006 }
1007
Radek Krejci1798aae2020-07-14 13:26:06 +02001008 LY_CHECK_GOTO(ret = lyb_print_subtree(out, root, &top_sibling_ht, lybctx), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001009
Radek Krejci7931b192020-06-25 17:05:03 +02001010 if (!(options & LYD_PRINT_WITHSIBLINGS)) {
Michal Vasko60ea6352020-06-29 13:39:39 +02001011 break;
1012 }
1013 }
1014
1015 /* ending zero byte */
Radek Krejci1798aae2020-07-14 13:26:06 +02001016 LY_CHECK_GOTO(ret = lyb_write(out, &zero, sizeof zero, lybctx->lybctx), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001017
1018cleanup:
Radek Krejci1798aae2020-07-14 13:26:06 +02001019 lyd_lyb_ctx_free((struct lyd_ctx *)lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001020 return ret;
1021}