blob: 10afe811d3e78d4cd5ada37a47cec4e8bfff87a7 [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
aPiecek270f72b2021-09-09 11:39:31 +020041static LY_ERR lyb_print_schema_hash(struct ly_out *out, struct lysc_node *schema, struct hash_table **sibling_ht, struct lylyb_ctx *lybctx);
42static LY_ERR lyb_print_attributes(struct ly_out *out, const struct lyd_node_opaq *node, struct lylyb_ctx *lybctx);
43
Michal Vasko60ea6352020-06-29 13:39:39 +020044/**
45 * @brief Hash table equal callback for checking hash equality only.
Radek Krejci857189e2020-09-01 13:26:36 +020046 *
Michal Vasko62524a92021-02-26 10:08:50 +010047 * Implementation of ::lyht_value_equal_cb.
Michal Vasko60ea6352020-06-29 13:39:39 +020048 */
Radek Krejci857189e2020-09-01 13:26:36 +020049static ly_bool
50lyb_hash_equal_cb(void *UNUSED(val1_p), void *UNUSED(val2_p), ly_bool UNUSED(mod), void *UNUSED(cb_data))
Michal Vasko60ea6352020-06-29 13:39:39 +020051{
52 /* for this purpose, if hash matches, the value does also, we do not want 2 values to have the same hash */
53 return 1;
54}
55
56/**
57 * @brief Hash table equal callback for checking value pointer equality only.
Radek Krejci857189e2020-09-01 13:26:36 +020058 *
Michal Vasko62524a92021-02-26 10:08:50 +010059 * Implementation of ::lyht_value_equal_cb.
Michal Vasko60ea6352020-06-29 13:39:39 +020060 */
Radek Krejci857189e2020-09-01 13:26:36 +020061static ly_bool
62lyb_ptr_equal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
Michal Vasko60ea6352020-06-29 13:39:39 +020063{
64 struct lysc_node *val1 = *(struct lysc_node **)val1_p;
65 struct lysc_node *val2 = *(struct lysc_node **)val2_p;
66
67 if (val1 == val2) {
68 return 1;
69 }
70 return 0;
71}
72
73/**
74 * @brief Check that sibling collision hash is safe to insert into hash table.
75 *
76 * @param[in] ht Hash table.
77 * @param[in] sibling Hashed sibling.
78 * @param[in] ht_col_id Sibling hash collision ID.
79 * @param[in] compare_col_id Last collision ID to compare with.
80 * @return LY_SUCCESS when the whole hash sequence does not collide,
81 * @return LY_EEXIST when the whole hash sequence sollides.
82 */
83static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +020084lyb_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 +020085{
Michal Vasko60ea6352020-06-29 13:39:39 +020086 struct lysc_node **col_node;
87
88 /* get the first node inserted with last hash col ID ht_col_id */
Michal Vasko11f76c82021-04-15 14:36:14 +020089 if (lyht_find(ht, &sibling, lyb_get_hash(sibling, ht_col_id), (void **)&col_node)) {
Michal Vasko60ea6352020-06-29 13:39:39 +020090 /* there is none. valid situation */
91 return LY_SUCCESS;
92 }
93
94 lyht_set_cb(ht, lyb_ptr_equal_cb);
95 do {
Radek Krejci1deb5be2020-08-26 16:43:36 +020096 int64_t j;
97 for (j = (int64_t)compare_col_id; j > -1; --j) {
Michal Vasko11f76c82021-04-15 14:36:14 +020098 if (lyb_get_hash(sibling, j) != lyb_get_hash(*col_node, j)) {
Michal Vasko60ea6352020-06-29 13:39:39 +020099 /* one non-colliding hash */
100 break;
101 }
102 }
103 if (j == -1) {
104 /* all whole hash sequences of nodes inserted with last hash col ID compare_col_id collide */
105 lyht_set_cb(ht, lyb_hash_equal_cb);
106 return LY_EEXIST;
107 }
108
109 /* get next node inserted with last hash col ID ht_col_id */
Michal Vasko11f76c82021-04-15 14:36:14 +0200110 } while (!lyht_find_next(ht, col_node, lyb_get_hash(*col_node, ht_col_id), (void **)&col_node));
Michal Vasko60ea6352020-06-29 13:39:39 +0200111
112 lyht_set_cb(ht, lyb_hash_equal_cb);
113 return LY_SUCCESS;
114}
115
116/**
117 * @brief Hash all the siblings and add them also into a separate hash table.
118 *
119 * @param[in] sibling Any sibling in all the siblings on one level.
120 * @param[out] ht_p Created hash table.
121 * @return LY_ERR value.
122 */
123static LY_ERR
124lyb_hash_siblings(struct lysc_node *sibling, struct hash_table **ht_p)
125{
126 struct hash_table *ht;
127 const struct lysc_node *parent;
128 const struct lys_module *mod;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200129 LYB_HASH i;
Michal Vasko1e0a80a2020-12-09 18:12:51 +0100130 uint32_t getnext_opts;
Michal Vasko60ea6352020-06-29 13:39:39 +0200131
132 ht = lyht_new(1, sizeof(struct lysc_node *), lyb_hash_equal_cb, NULL, 1);
133 LY_CHECK_ERR_RET(!ht, LOGMEM(sibling->module->ctx), LY_EMEM);
134
Michal Vasko1e0a80a2020-12-09 18:12:51 +0100135 getnext_opts = 0;
Michal Vaskod1e53b92021-01-28 13:11:06 +0100136 if (sibling->flags & LYS_IS_OUTPUT) {
Michal Vasko1e0a80a2020-12-09 18:12:51 +0100137 getnext_opts = LYS_GETNEXT_OUTPUT;
138 }
139
Michal Vasko60ea6352020-06-29 13:39:39 +0200140 parent = lysc_data_parent(sibling);
141 mod = sibling->module;
142
143 sibling = NULL;
Michal Vasko1e0a80a2020-12-09 18:12:51 +0100144 while ((sibling = (struct lysc_node *)lys_getnext(sibling, parent, mod->compiled, getnext_opts))) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200145 /* find the first non-colliding hash (or specifically non-colliding hash sequence) */
146 for (i = 0; i < LYB_HASH_BITS; ++i) {
147 /* check that we are not colliding with nodes inserted with a lower collision ID than ours */
Radek Krejci1deb5be2020-08-26 16:43:36 +0200148 int64_t j;
149 for (j = (int64_t)i - 1; j > -1; --j) {
150 if (lyb_hash_sequence_check(ht, sibling, (LYB_HASH)j, i)) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200151 break;
152 }
153 }
154 if (j > -1) {
155 /* some check failed, we must use a higher collision ID */
156 continue;
157 }
158
159 /* try to insert node with the current collision ID */
Michal Vasko11f76c82021-04-15 14:36:14 +0200160 if (!lyht_insert_with_resize_cb(ht, &sibling, lyb_get_hash(sibling, i), lyb_ptr_equal_cb, NULL)) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200161 /* success, no collision */
162 break;
163 }
164
165 /* make sure we really cannot insert it with this hash col ID (meaning the whole hash sequence is colliding) */
166 if (i && !lyb_hash_sequence_check(ht, sibling, i, i)) {
167 /* it can be inserted after all, even though there is already a node with the same last collision ID */
168 lyht_set_cb(ht, lyb_ptr_equal_cb);
Michal Vasko11f76c82021-04-15 14:36:14 +0200169 if (lyht_insert(ht, &sibling, lyb_get_hash(sibling, i), NULL)) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200170 LOGINT(sibling->module->ctx);
171 lyht_set_cb(ht, lyb_hash_equal_cb);
172 lyht_free(ht);
173 return LY_EINT;
174 }
175 lyht_set_cb(ht, lyb_hash_equal_cb);
176 break;
177 }
178 /* there is still another colliding schema node with the same hash sequence, try higher collision ID */
179 }
180
181 if (i == LYB_HASH_BITS) {
182 /* wow */
183 LOGINT(sibling->module->ctx);
184 lyht_free(ht);
185 return LY_EINT;
186 }
187 }
188
189 /* change val equal callback so that the HT is usable for finding value hashes */
190 lyht_set_cb(ht, lyb_ptr_equal_cb);
191
192 *ht_p = ht;
193 return LY_SUCCESS;
194}
195
196/**
197 * @brief Find node hash in a hash table.
198 *
199 * @param[in] ht Hash table to search in.
200 * @param[in] node Node to find.
201 * @param[out] hash_p First non-colliding hash found.
202 * @return LY_ERR value.
203 */
204static LY_ERR
205lyb_hash_find(struct hash_table *ht, struct lysc_node *node, LYB_HASH *hash_p)
206{
207 LYB_HASH hash;
208 uint32_t i;
209
210 for (i = 0; i < LYB_HASH_BITS; ++i) {
Michal Vasko11f76c82021-04-15 14:36:14 +0200211 hash = lyb_get_hash(node, i);
Michal Vasko60ea6352020-06-29 13:39:39 +0200212 if (!hash) {
213 LOGINT_RET(node->module->ctx);
214 }
215
216 if (!lyht_find(ht, &node, hash, NULL)) {
217 /* success, no collision */
218 break;
219 }
220 }
221 /* cannot happen, we already calculated the hash */
222 if (i == LYB_HASH_BITS) {
223 LOGINT_RET(node->module->ctx);
224 }
225
226 *hash_p = hash;
227 return LY_SUCCESS;
228}
229
230/**
231 * @brief Write LYB data fully handling the metadata.
232 *
233 * @param[in] out Out structure.
234 * @param[in] buf Source buffer.
235 * @param[in] count Number of bytes to write.
236 * @param[in] lybctx LYB context.
237 * @return LY_ERR value.
238 */
239static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200240lyb_write(struct ly_out *out, const uint8_t *buf, size_t count, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200241{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200242 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +0200243 struct lyd_lyb_subtree *full, *iter;
Michal Vasko5233e962020-08-14 14:26:20 +0200244 size_t to_write;
Michal Vasko60ea6352020-06-29 13:39:39 +0200245 uint8_t meta_buf[LYB_META_BYTES];
246
247 while (1) {
248 /* check for full data chunks */
249 to_write = count;
250 full = NULL;
251 LY_ARRAY_FOR(lybctx->subtrees, u) {
252 /* we want the innermost chunks resolved first, so replace previous full chunks */
253 if (lybctx->subtrees[u].written + to_write >= LYB_SIZE_MAX) {
254 /* full chunk, do not write more than allowed */
255 to_write = LYB_SIZE_MAX - lybctx->subtrees[u].written;
256 full = &lybctx->subtrees[u];
257 }
258 }
259
260 if (!full && !count) {
261 break;
262 }
263
264 /* we are actually writing some data, not just finishing another chunk */
265 if (to_write) {
Michal Vasko5233e962020-08-14 14:26:20 +0200266 LY_CHECK_RET(ly_write_(out, (char *)buf, to_write));
Michal Vasko60ea6352020-06-29 13:39:39 +0200267
268 LY_ARRAY_FOR(lybctx->subtrees, u) {
269 /* increase all written counters */
Michal Vasko5233e962020-08-14 14:26:20 +0200270 lybctx->subtrees[u].written += to_write;
Michal Vasko60ea6352020-06-29 13:39:39 +0200271 assert(lybctx->subtrees[u].written <= LYB_SIZE_MAX);
272 }
273 /* decrease count/buf */
Michal Vasko5233e962020-08-14 14:26:20 +0200274 count -= to_write;
275 buf += to_write;
Michal Vasko60ea6352020-06-29 13:39:39 +0200276 }
277
278 if (full) {
279 /* write the meta information (inner chunk count and chunk size) */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100280 meta_buf[0] = full->written & LYB_BYTE_MASK;
281 meta_buf[1] = full->inner_chunks & LYB_BYTE_MASK;
Michal Vasko5233e962020-08-14 14:26:20 +0200282 LY_CHECK_RET(ly_write_skipped(out, full->position, (char *)meta_buf, LYB_META_BYTES));
Michal Vasko60ea6352020-06-29 13:39:39 +0200283
284 /* zero written and inner chunks */
285 full->written = 0;
286 full->inner_chunks = 0;
287
288 /* skip space for another chunk size */
Michal Vasko5233e962020-08-14 14:26:20 +0200289 LY_CHECK_RET(ly_write_skip(out, LYB_META_BYTES, &full->position));
Michal Vasko60ea6352020-06-29 13:39:39 +0200290
291 /* increase inner chunk count */
292 for (iter = &lybctx->subtrees[0]; iter != full; ++iter) {
293 if (iter->inner_chunks == LYB_INCHUNK_MAX) {
294 LOGINT(lybctx->ctx);
295 return LY_EINT;
296 }
297 ++iter->inner_chunks;
298 }
299 }
300 }
301
302 return LY_SUCCESS;
303}
304
305/**
306 * @brief Stop the current subtree - write its final metadata.
307 *
308 * @param[in] out Out structure.
309 * @param[in] lybctx LYB context.
310 * @return LY_ERR value.
311 */
312static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200313lyb_write_stop_subtree(struct ly_out *out, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200314{
Michal Vasko60ea6352020-06-29 13:39:39 +0200315 uint8_t meta_buf[LYB_META_BYTES];
316
317 /* write the meta chunk information */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100318 meta_buf[0] = LYB_LAST_SUBTREE(lybctx).written & LYB_BYTE_MASK;
319 meta_buf[1] = LYB_LAST_SUBTREE(lybctx).inner_chunks & LYB_BYTE_MASK;
Michal Vasko5233e962020-08-14 14:26:20 +0200320 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 +0200321
322 LY_ARRAY_DECREMENT(lybctx->subtrees);
323 return LY_SUCCESS;
324}
325
326/**
327 * @brief Start a new subtree - skip bytes for its metadata.
328 *
329 * @param[in] out Out structure.
330 * @param[in] lybctx LYB context.
331 * @return LY_ERR value.
332 */
333static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200334lyb_write_start_subtree(struct ly_out *out, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200335{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200336 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +0200337
Radek Krejcic7d13e32020-12-09 12:32:24 +0100338 u = LY_ARRAY_COUNT(lybctx->subtrees);
Michal Vasko60ea6352020-06-29 13:39:39 +0200339 if (u == lybctx->subtree_size) {
340 LY_ARRAY_CREATE_RET(lybctx->ctx, lybctx->subtrees, u + LYB_SUBTREE_STEP, LY_EMEM);
341 lybctx->subtree_size = u + LYB_SUBTREE_STEP;
342 }
343
344 LY_ARRAY_INCREMENT(lybctx->subtrees);
345 LYB_LAST_SUBTREE(lybctx).written = 0;
346 LYB_LAST_SUBTREE(lybctx).inner_chunks = 0;
347
348 /* another inner chunk */
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200349 for (u = 0; u < LY_ARRAY_COUNT(lybctx->subtrees) - 1; ++u) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200350 if (lybctx->subtrees[u].inner_chunks == LYB_INCHUNK_MAX) {
351 LOGINT(lybctx->ctx);
Michal Vasko5233e962020-08-14 14:26:20 +0200352 return LY_EINT;
Michal Vasko60ea6352020-06-29 13:39:39 +0200353 }
354 ++lybctx->subtrees[u].inner_chunks;
355 }
356
Michal Vasko5233e962020-08-14 14:26:20 +0200357 LY_CHECK_RET(ly_write_skip(out, LYB_META_BYTES, &LYB_LAST_SUBTREE(lybctx).position));
Michal Vasko60ea6352020-06-29 13:39:39 +0200358
359 return LY_SUCCESS;
360}
361
362/**
363 * @brief Write a number.
364 *
365 * @param[in] num Number to write.
366 * @param[in] bytes Actual accessible bytes of @p num.
367 * @param[in] out Out structure.
368 * @param[in] lybctx LYB context.
369 * @return LY_ERR value.
370 */
371static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200372lyb_write_number(uint64_t num, size_t bytes, struct ly_out *out, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200373{
374 /* correct byte order */
375 num = htole64(num);
376
377 return lyb_write(out, (uint8_t *)&num, bytes, lybctx);
378}
379
380/**
381 * @brief Write a string.
382 *
383 * @param[in] str String to write.
384 * @param[in] str_len Length of @p str.
385 * @param[in] with_length Whether to precede the string with its length.
386 * @param[in] out Out structure.
387 * @param[in] lybctx LYB context.
388 * @return LY_ERR value.
389 */
390static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200391lyb_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 +0200392{
Michal Vasko60ea6352020-06-29 13:39:39 +0200393 if (!str) {
394 str = "";
Michal Vasko30667352020-08-13 09:06:14 +0200395 LY_CHECK_ERR_RET(str_len, LOGINT(lybctx->ctx), LY_EINT);
Michal Vasko60ea6352020-06-29 13:39:39 +0200396 }
397 if (!str_len) {
398 str_len = strlen(str);
399 }
400
401 if (with_length) {
402 /* print length on 2 bytes */
403 if (str_len > UINT16_MAX) {
404 LOGINT(lybctx->ctx);
405 return LY_EINT;
406 }
407 LY_CHECK_RET(lyb_write_number(str_len, 2, out, lybctx));
408 }
409
Michal Vasko63f3d842020-07-08 10:10:14 +0200410 LY_CHECK_RET(lyb_write(out, (const uint8_t *)str, str_len, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200411
412 return LY_SUCCESS;
413}
414
415/**
416 * @brief Print YANG module info.
417 *
418 * @param[in] out Out structure.
419 * @param[in] mod Module to print.
420 * @param[in] lybctx LYB context.
421 * @return LY_ERR value.
422 */
423static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200424lyb_print_model(struct ly_out *out, const struct lys_module *mod, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200425{
Michal Vasko60ea6352020-06-29 13:39:39 +0200426 uint16_t revision;
427
428 /* model name length and model name */
429 if (mod) {
430 LY_CHECK_RET(lyb_write_string(mod->name, 0, 1, out, lybctx));
431 } else {
432 LY_CHECK_RET(lyb_write_string("", 0, 1, out, lybctx));
433 }
434
435 /* model revision as XXXX XXXX XXXX XXXX (2B) (year is offset from 2000)
436 * YYYY YYYM MMMD DDDD */
437 revision = 0;
438 if (mod && mod->revision) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200439 int r = atoi(mod->revision);
Radek Krejcif13b87b2020-12-01 22:02:17 +0100440 r -= LYB_REV_YEAR_OFFSET;
441 r <<= LYB_REV_YEAR_SHIFT;
Michal Vasko60ea6352020-06-29 13:39:39 +0200442
443 revision |= r;
444
Radek Krejcif13b87b2020-12-01 22:02:17 +0100445 r = atoi(mod->revision + ly_strlen_const("YYYY-"));
446 r <<= LYB_REV_MONTH_SHIFT;
Michal Vasko60ea6352020-06-29 13:39:39 +0200447
448 revision |= r;
449
Radek Krejcif13b87b2020-12-01 22:02:17 +0100450 r = atoi(mod->revision + ly_strlen_const("YYYY-MM-"));
Michal Vasko60ea6352020-06-29 13:39:39 +0200451
452 revision |= r;
453 }
454 LY_CHECK_RET(lyb_write_number(revision, sizeof revision, out, lybctx));
455
Michal Vasko11f76c82021-04-15 14:36:14 +0200456 if (mod) {
457 /* fill cached hashes, if not already */
458 lyb_cache_module_hash(mod);
459 }
460
Michal Vasko60ea6352020-06-29 13:39:39 +0200461 return LY_SUCCESS;
462}
463
464/**
465 * @brief Print all used YANG modules.
466 *
467 * @param[in] out Out structure.
468 * @param[in] root Data root.
469 * @param[in] lybctx LYB context.
470 * @return LY_ERR value.
471 */
472static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200473lyb_print_data_models(struct ly_out *out, const struct lyd_node *root, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200474{
475 struct ly_set *set;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200476 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +0200477 LY_ERR ret = LY_SUCCESS;
478 struct lys_module *mod;
Michal Vaskof4b4d002021-08-23 12:16:02 +0200479 const struct lyd_node *elem, *node;
Michal Vasko60ea6352020-06-29 13:39:39 +0200480 uint32_t i;
481
Radek Krejciba03a5a2020-08-27 14:40:41 +0200482 LY_CHECK_RET(ly_set_new(&set));
Michal Vasko60ea6352020-06-29 13:39:39 +0200483
484 /* collect all data node modules */
Michal Vaskof4b4d002021-08-23 12:16:02 +0200485 LY_LIST_FOR(root, elem) {
486 LYD_TREE_DFS_BEGIN(elem, node) {
487 if (node->schema) {
488 mod = node->schema->module;
489 ret = ly_set_add(set, mod, 0, NULL);
490 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200491
Michal Vaskof4b4d002021-08-23 12:16:02 +0200492 /* add also their modules deviating or augmenting them */
493 LY_ARRAY_FOR(mod->deviated_by, u) {
494 ret = ly_set_add(set, mod->deviated_by[u], 0, NULL);
495 LY_CHECK_GOTO(ret, cleanup);
496 }
497 LY_ARRAY_FOR(mod->augmented_by, u) {
498 ret = ly_set_add(set, mod->augmented_by[u], 0, NULL);
499 LY_CHECK_GOTO(ret, cleanup);
500 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200501
Michal Vaskof4b4d002021-08-23 12:16:02 +0200502 /* only top-level nodes are processed */
503 LYD_TREE_DFS_continue = 1;
504 }
505
506 LYD_TREE_DFS_END(elem, node);
Michal Vasko60ea6352020-06-29 13:39:39 +0200507 }
508 }
509
510 /* now write module count on 2 bytes */
511 LY_CHECK_GOTO(ret = lyb_write_number(set->count, 2, out, lybctx), cleanup);
512
513 /* and all the used models */
514 for (i = 0; i < set->count; ++i) {
515 LY_CHECK_GOTO(ret = lyb_print_model(out, set->objs[i], lybctx), cleanup);
516 }
517
518cleanup:
519 ly_set_free(set, NULL);
520 return ret;
521}
522
523/**
524 * @brief Print LYB magic number.
525 *
526 * @param[in] out Out structure.
Michal Vasko60ea6352020-06-29 13:39:39 +0200527 * @return LY_ERR value.
528 */
529static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200530lyb_print_magic_number(struct ly_out *out)
Michal Vasko60ea6352020-06-29 13:39:39 +0200531{
Michal Vasko60ea6352020-06-29 13:39:39 +0200532 /* 'l', 'y', 'b' - 0x6c7962 */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100533 char magic_number[] = {'l', 'y', 'b'};
Michal Vasko60ea6352020-06-29 13:39:39 +0200534
Radek Krejcidefd4d72020-12-01 12:20:30 +0100535 LY_CHECK_RET(ly_write_(out, magic_number, 3));
Michal Vasko60ea6352020-06-29 13:39:39 +0200536
537 return LY_SUCCESS;
538}
539
540/**
541 * @brief Print LYB header.
542 *
543 * @param[in] out Out structure.
Michal Vasko60ea6352020-06-29 13:39:39 +0200544 * @return LY_ERR value.
545 */
546static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200547lyb_print_header(struct ly_out *out)
Michal Vasko60ea6352020-06-29 13:39:39 +0200548{
Michal Vasko60ea6352020-06-29 13:39:39 +0200549 uint8_t byte = 0;
550
551 /* version, future flags */
552 byte |= LYB_VERSION_NUM;
553
Michal Vasko5233e962020-08-14 14:26:20 +0200554 LY_CHECK_RET(ly_write_(out, (char *)&byte, 1));
Michal Vasko60ea6352020-06-29 13:39:39 +0200555
556 return LY_SUCCESS;
557}
558
559/**
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100560 * @brief Print prefix data.
Michal Vasko60ea6352020-06-29 13:39:39 +0200561 *
562 * @param[in] out Out structure.
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100563 * @param[in] format Value prefix format.
Radek Krejci8df109d2021-04-23 12:19:08 +0200564 * @param[in] prefix_data Format-specific data for resolving any prefixes (see ::ly_resolve_prefix).
Michal Vasko60ea6352020-06-29 13:39:39 +0200565 * @param[in] lybctx LYB context.
566 * @return LY_ERR value.
567 */
568static LY_ERR
Radek Krejci8df109d2021-04-23 12:19:08 +0200569lyb_print_prefix_data(struct ly_out *out, LY_VALUE_FORMAT format, const void *prefix_data, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200570{
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100571 const struct ly_set *set;
572 const struct lyxml_ns *ns;
573 uint32_t i;
Michal Vasko60ea6352020-06-29 13:39:39 +0200574
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100575 switch (format) {
Radek Krejci8df109d2021-04-23 12:19:08 +0200576 case LY_VALUE_XML:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100577 set = prefix_data;
578 if (!set) {
579 /* no prefix data */
580 i = 0;
581 LY_CHECK_RET(lyb_write(out, (uint8_t *)&i, 1, lybctx));
582 break;
583 }
584 if (set->count > UINT8_MAX) {
585 LOGERR(lybctx->ctx, LY_EINT, "Maximum supported number of prefixes is %u.", UINT8_MAX);
586 return LY_EINT;
587 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200588
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100589 /* write number of prefixes on 1 byte */
Michal Vasko403beac2021-08-24 08:27:52 +0200590 LY_CHECK_RET(lyb_write_number(set->count, 1, out, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200591
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100592 /* write all the prefixes */
593 for (i = 0; i < set->count; ++i) {
594 ns = set->objs[i];
Michal Vasko60ea6352020-06-29 13:39:39 +0200595
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100596 /* prefix */
597 LY_CHECK_RET(lyb_write_string(ns->prefix, 0, 1, out, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200598
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100599 /* namespace */
600 LY_CHECK_RET(lyb_write_string(ns->uri, 0, 1, out, lybctx));
601 }
602 break;
Radek Krejci8df109d2021-04-23 12:19:08 +0200603 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +0200604 case LY_VALUE_LYB:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100605 /* nothing to print */
606 break;
607 default:
608 LOGINT_RET(lybctx->ctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200609 }
610
611 return LY_SUCCESS;
612}
613
614/**
615 * @brief Print opaque node.
616 *
617 * @param[in] opaq Node to print.
618 * @param[in] out Out structure.
619 * @param[in] lybctx LYB context.
620 * @return LY_ERR value.
621 */
622static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200623lyb_print_opaq(struct lyd_node_opaq *opaq, struct ly_out *out, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200624{
aPiecek270f72b2021-09-09 11:39:31 +0200625 /* write attributes */
626 LY_CHECK_RET(lyb_print_attributes(out, opaq, lybctx));
627
628 /* write node flags */
629 LY_CHECK_RET(lyb_write_number(opaq->flags, sizeof opaq->flags, out, lybctx));
630
Michal Vasko60ea6352020-06-29 13:39:39 +0200631 /* prefix */
Michal Vaskoad92b672020-11-12 13:11:31 +0100632 LY_CHECK_RET(lyb_write_string(opaq->name.prefix, 0, 1, out, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200633
Radek Krejci1798aae2020-07-14 13:26:06 +0200634 /* module reference */
Michal Vaskoad92b672020-11-12 13:11:31 +0100635 LY_CHECK_RET(lyb_write_string(opaq->name.module_name, 0, 1, out, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200636
637 /* name */
Michal Vaskoad92b672020-11-12 13:11:31 +0100638 LY_CHECK_RET(lyb_write_string(opaq->name.name, 0, 1, out, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200639
Michal Vaskoffc92bf2021-06-21 08:15:14 +0200640 /* value */
641 LY_CHECK_RET(lyb_write_string(opaq->value, 0, 1, out, lybctx));
642
Michal Vasko60ea6352020-06-29 13:39:39 +0200643 /* format */
644 LY_CHECK_RET(lyb_write_number(opaq->format, 1, out, lybctx));
645
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100646 /* value prefixes */
647 LY_CHECK_RET(lyb_print_prefix_data(out, opaq->format, opaq->val_prefix_data, lybctx));
648
Michal Vasko60ea6352020-06-29 13:39:39 +0200649 return LY_SUCCESS;
650}
651
652/**
653 * @brief Print anydata node.
654 *
655 * @param[in] anydata Node to print.
656 * @param[in] out Out structure.
657 * @param[in] lybctx LYB context.
658 * @return LY_ERR value.
659 */
660static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200661lyb_print_anydata(struct lyd_node_any *anydata, struct ly_out *out, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200662{
663 LY_ERR ret = LY_SUCCESS;
664 LYD_ANYDATA_VALUETYPE value_type;
665 int len;
666 char *buf = NULL;
667 const char *str;
668 struct ly_out *out2 = NULL;
669
670 if (anydata->value_type == LYD_ANYDATA_DATATREE) {
671 /* will be printed as a nested LYB data tree */
672 value_type = LYD_ANYDATA_LYB;
673 } else {
674 value_type = anydata->value_type;
675 }
676
677 /* first byte is type */
Michal Vasko403beac2021-08-24 08:27:52 +0200678 LY_CHECK_GOTO(ret = lyb_write_number(value_type, sizeof value_type, out, lybctx), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200679
680 if (anydata->value_type == LYD_ANYDATA_DATATREE) {
681 /* print LYB data tree to memory */
682 LY_CHECK_GOTO(ret = ly_out_new_memory(&buf, 0, &out2), cleanup);
Radek Krejci7931b192020-06-25 17:05:03 +0200683 LY_CHECK_GOTO(ret = lyb_print_data(out2, anydata->value.tree, LYD_PRINT_WITHSIBLINGS), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200684
685 len = lyd_lyb_data_length(buf);
686 assert(len != -1);
687 str = buf;
688 } else if (anydata->value_type == LYD_ANYDATA_LYB) {
689 len = lyd_lyb_data_length(anydata->value.mem);
690 assert(len != -1);
691 str = anydata->value.mem;
692 } else {
693 len = strlen(anydata->value.str);
694 str = anydata->value.str;
695 }
696
697 /* followed by the content */
698 LY_CHECK_GOTO(ret = lyb_write_string(str, (size_t)len, 0, out, lybctx), cleanup);
699
700cleanup:
701 ly_out_free(out2, NULL, 1);
702 return ret;
703}
704
705/**
706 * @brief Print term node.
707 *
708 * @param[in] term Node to print.
709 * @param[in] out Out structure.
710 * @param[in] lybctx LYB context.
711 * @return LY_ERR value.
712 */
713static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200714lyb_print_term(struct lyd_node_term *term, struct ly_out *out, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200715{
aPiecekaa5b70a2021-08-30 08:33:25 +0200716 LY_ERR ret = LY_SUCCESS;
aPiecekea304e32021-08-18 09:13:47 +0200717 ly_bool dynamic = 0;
718 void *value;
719 size_t value_len = 0;
aPiecekaa5b70a2021-08-30 08:33:25 +0200720 int32_t lyb_data_len;
721 lyplg_type_print_clb print;
aPiecekea304e32021-08-18 09:13:47 +0200722
723 assert(term->value.realtype && term->value.realtype->plugin && term->value.realtype->plugin->print &&
724 term->schema);
725
aPiecekaa5b70a2021-08-30 08:33:25 +0200726 /* Get length of LYB data to print. */
727 lyb_data_len = term->value.realtype->plugin->lyb_data_len;
aPiecekea304e32021-08-18 09:13:47 +0200728
aPiecekaa5b70a2021-08-30 08:33:25 +0200729 /* Get value and also print its length only if size is not fixed. */
730 print = term->value.realtype->plugin->print;
731 if (lyb_data_len < 0) {
732 /* Variable-length data. */
733
734 /* Get value and its length from plugin. */
735 value = (void *)print(term->schema->module->ctx, &term->value,
736 LY_VALUE_LYB, NULL, &dynamic, &value_len);
737 LY_CHECK_GOTO(ret, cleanup);
738
739 if (value_len > UINT32_MAX) {
740 LOGERR(lybctx->ctx, LY_EINT, "The maximum length of the LYB data "
741 "from a term node must not exceed %lu.", UINT32_MAX);
742 ret = LY_EINT;
743 goto cleanup;
744 }
745
746 /* Print the length of the data as 32-bit unsigned integer. */
747 ret = lyb_write_number(value_len, sizeof(uint32_t), out, lybctx);
748 LY_CHECK_GOTO(ret, cleanup);
749 } else {
750 /* Fixed-length data. */
751
752 /* Get value from plugin. */
753 value = (void *)print(term->schema->module->ctx, &term->value,
754 LY_VALUE_LYB, NULL, &dynamic, NULL);
755 LY_CHECK_GOTO(ret, cleanup);
756
757 /* Copy the length from the compiled node. */
758 value_len = lyb_data_len;
aPiecekea304e32021-08-18 09:13:47 +0200759 }
760
aPiecekaa5b70a2021-08-30 08:33:25 +0200761 /* Print value. */
aPiecekea304e32021-08-18 09:13:47 +0200762 if (value_len > 0) {
aPiecekaa5b70a2021-08-30 08:33:25 +0200763 /* Print the value simply as it is. */
aPiecekea304e32021-08-18 09:13:47 +0200764 ret = lyb_write(out, value, value_len, lybctx);
765 LY_CHECK_GOTO(ret, cleanup);
766 }
767
768cleanup:
769 if (dynamic) {
770 free(value);
771 }
772
773 return ret;
Michal Vasko60ea6352020-06-29 13:39:39 +0200774}
775
776/**
777 * @brief Print YANG node metadata.
778 *
779 * @param[in] out Out structure.
780 * @param[in] node Data node whose metadata to print.
781 * @param[in] lybctx LYB context.
782 * @return LY_ERR value.
783 */
784static LY_ERR
785lyb_print_metadata(struct ly_out *out, const struct lyd_node *node, struct lyd_lyb_ctx *lybctx)
786{
Michal Vasko60ea6352020-06-29 13:39:39 +0200787 uint8_t count = 0;
788 const struct lys_module *wd_mod = NULL;
789 struct lyd_meta *iter;
Michal Vasko60ea6352020-06-29 13:39:39 +0200790
791 /* with-defaults */
792 if (node->schema->nodetype & LYD_NODE_TERM) {
Radek Krejci7931b192020-06-25 17:05:03 +0200793 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 +0200794 ((lybctx->print_options & LYD_PRINT_WD_ALL_TAG) && lyd_is_default(node))) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200795 /* we have implicit OR explicit default node, print attribute only if context include with-defaults schema */
796 wd_mod = ly_ctx_get_module_latest(node->schema->module->ctx, "ietf-netconf-with-defaults");
797 }
798 }
799
800 /* count metadata */
801 if (wd_mod) {
802 ++count;
803 }
804 for (iter = node->meta; iter; iter = iter->next) {
805 if (count == UINT8_MAX) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200806 LOGERR(lybctx->lybctx->ctx, LY_EINT, "Maximum supported number of data node metadata is %u.", UINT8_MAX);
Michal Vasko60ea6352020-06-29 13:39:39 +0200807 return LY_EINT;
808 }
809 ++count;
810 }
811
812 /* write number of metadata on 1 byte */
Radek Krejci1798aae2020-07-14 13:26:06 +0200813 LY_CHECK_RET(lyb_write(out, &count, 1, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200814
815 if (wd_mod) {
816 /* write the "default" metadata */
Radek Krejci1798aae2020-07-14 13:26:06 +0200817 LY_CHECK_RET(lyb_write_start_subtree(out, lybctx->lybctx));
818 LY_CHECK_RET(lyb_print_model(out, wd_mod, lybctx->lybctx));
819 LY_CHECK_RET(lyb_write_string("default", 0, 1, out, lybctx->lybctx));
820 LY_CHECK_RET(lyb_write_string("true", 0, 0, out, lybctx->lybctx));
821 LY_CHECK_RET(lyb_write_stop_subtree(out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200822 }
823
824 /* write all the node metadata */
825 LY_LIST_FOR(node->meta, iter) {
826 /* each metadata is a subtree */
Radek Krejci1798aae2020-07-14 13:26:06 +0200827 LY_CHECK_RET(lyb_write_start_subtree(out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200828
829 /* model */
Radek Krejci1798aae2020-07-14 13:26:06 +0200830 LY_CHECK_RET(lyb_print_model(out, iter->annotation->module, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200831
832 /* annotation name with length */
Radek Krejci1798aae2020-07-14 13:26:06 +0200833 LY_CHECK_RET(lyb_write_string(iter->name, 0, 1, out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200834
Michal Vasko60ea6352020-06-29 13:39:39 +0200835 /* metadata value */
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200836 LY_CHECK_RET(lyb_write_string(lyd_get_meta_value(iter), 0, 0, out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200837
838 /* finish metadata subtree */
Radek Krejci1798aae2020-07-14 13:26:06 +0200839 LY_CHECK_RET(lyb_write_stop_subtree(out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200840 }
841
842 return LY_SUCCESS;
843}
844
845/**
846 * @brief Print opaque node attributes.
847 *
848 * @param[in] out Out structure.
849 * @param[in] node Opaque node whose attributes to print.
850 * @param[in] lybctx LYB context.
851 * @return LY_ERR value.
852 */
853static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200854lyb_print_attributes(struct ly_out *out, const struct lyd_node_opaq *node, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200855{
856 uint8_t count = 0;
Radek Krejci5536d282020-08-04 23:27:44 +0200857 struct lyd_attr *iter;
Michal Vasko60ea6352020-06-29 13:39:39 +0200858
859 for (iter = node->attr; iter; iter = iter->next) {
860 if (count == UINT8_MAX) {
861 LOGERR(lybctx->ctx, LY_EINT, "Maximum supported number of data node attributes is %u.", UINT8_MAX);
862 return LY_EINT;
863 }
864 ++count;
865 }
866
867 /* write number of attributes on 1 byte */
868 LY_CHECK_RET(lyb_write(out, &count, 1, lybctx));
869
870 /* write all the attributes */
871 LY_LIST_FOR(node->attr, iter) {
872 /* each attribute is a subtree */
873 LY_CHECK_RET(lyb_write_start_subtree(out, lybctx));
874
875 /* prefix */
Michal Vaskoad92b672020-11-12 13:11:31 +0100876 LY_CHECK_RET(lyb_write_string(iter->name.prefix, 0, 1, out, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200877
878 /* namespace */
Michal Vaskoad92b672020-11-12 13:11:31 +0100879 LY_CHECK_RET(lyb_write_string(iter->name.module_name, 0, 1, out, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200880
881 /* name */
Michal Vaskoad92b672020-11-12 13:11:31 +0100882 LY_CHECK_RET(lyb_write_string(iter->name.name, 0, 1, out, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200883
Michal Vasko60ea6352020-06-29 13:39:39 +0200884 /* format */
885 LY_CHECK_RET(lyb_write_number(iter->format, 1, out, lybctx));
886
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100887 /* value prefixes */
888 LY_CHECK_RET(lyb_print_prefix_data(out, iter->format, iter->val_prefix_data, lybctx));
889
Michal Vasko60ea6352020-06-29 13:39:39 +0200890 /* value */
891 LY_CHECK_RET(lyb_write_string(iter->value, 0, 0, out, lybctx));
892
893 /* finish attribute subtree */
894 LY_CHECK_RET(lyb_write_stop_subtree(out, lybctx));
895 }
896
897 return LY_SUCCESS;
898}
899
900/**
aPiecek270f72b2021-09-09 11:39:31 +0200901 * @brief Print header for non-opaq node.
902 *
903 * @param[in] out Out structure.
904 * @param[in] node Current data node to print.
905 * @param[in] lybctx LYB context.
906 * @return LY_ERR value.
907 */
908static LY_ERR
909lyb_print_node_header(struct ly_out *out, const struct lyd_node *node, struct lyd_lyb_ctx *lybctx)
910{
911 /* write any metadata */
912 LY_CHECK_RET(lyb_print_metadata(out, node, lybctx));
913
914 /* write node flags */
915 LY_CHECK_RET(lyb_write_number(node->flags, sizeof node->flags, out, lybctx->lybctx));
916
917 return LY_SUCCESS;
918}
919
920/**
921 * @brief Print model and hash.
922 *
923 * @param[in] out Out structure.
924 * @param[in] node Current data node to print.
925 * @param[in,out] sibling_ht Cached hash table for these siblings, created if NULL.
926 * @param[in] lybctx LYB context.
927 * @return LY_ERR value.
928 */
929static LY_ERR
930lyb_print_model_and_hash(struct ly_out *out, const struct lyd_node *node, struct hash_table **sibling_ht,
931 struct lylyb_ctx *lybctx)
932{
933 /* write model info first, for all opaque and top-level nodes */
934 if (!node->schema && (!node->parent || !node->parent->schema)) {
935 LY_CHECK_RET(lyb_print_model(out, NULL, lybctx));
936 } else if (node->schema && !lysc_data_parent(node->schema)) {
937 LY_CHECK_RET(lyb_print_model(out, node->schema->module, lybctx));
938 }
939
940 /* write schema hash */
941 LY_CHECK_RET(lyb_print_schema_hash(out, (struct lysc_node *)node->schema, sibling_ht, lybctx));
942
943 return LY_SUCCESS;
944}
945
946/**
Michal Vasko60ea6352020-06-29 13:39:39 +0200947 * @brief Print schema node hash.
948 *
949 * @param[in] out Out structure.
950 * @param[in] schema Schema node whose hash to print.
951 * @param[in,out] sibling_ht Cached hash table for these siblings, created if NULL.
952 * @param[in] lybctx LYB context.
953 * @return LY_ERR value.
954 */
955static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200956lyb_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 +0200957{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200958 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +0200959 uint32_t i;
960 LYB_HASH hash;
961 struct lyd_lyb_sib_ht *sib_ht;
962 struct lysc_node *first_sibling;
963
964 if (!schema) {
965 /* opaque node, write empty hash */
966 hash = 0;
967 LY_CHECK_RET(lyb_write(out, &hash, sizeof hash, lybctx));
968 return LY_SUCCESS;
969 }
970
971 /* create whole sibling HT if not already created and saved */
972 if (!*sibling_ht) {
Michal Vasko1e0a80a2020-12-09 18:12:51 +0100973 /* get first schema data sibling */
974 first_sibling = (struct lysc_node *)lys_getnext(NULL, lysc_data_parent(schema), schema->module->compiled,
Michal Vaskod1e53b92021-01-28 13:11:06 +0100975 (schema->flags & LYS_IS_OUTPUT) ? LYS_GETNEXT_OUTPUT : 0);
Michal Vasko60ea6352020-06-29 13:39:39 +0200976 LY_ARRAY_FOR(lybctx->sib_hts, u) {
977 if (lybctx->sib_hts[u].first_sibling == first_sibling) {
978 /* we have already created a hash table for these siblings */
979 *sibling_ht = lybctx->sib_hts[u].ht;
980 break;
981 }
982 }
983
984 if (!*sibling_ht) {
985 /* we must create sibling hash table */
986 LY_CHECK_RET(lyb_hash_siblings(first_sibling, sibling_ht));
987
988 /* and save it */
989 LY_ARRAY_NEW_RET(lybctx->ctx, lybctx->sib_hts, sib_ht, LY_EMEM);
990
991 sib_ht->first_sibling = first_sibling;
992 sib_ht->ht = *sibling_ht;
993 }
994 }
995
996 /* get our hash */
997 LY_CHECK_RET(lyb_hash_find(*sibling_ht, schema, &hash));
998
999 /* write the hash */
1000 LY_CHECK_RET(lyb_write(out, &hash, sizeof hash, lybctx));
1001
1002 if (hash & LYB_HASH_COLLISION_ID) {
1003 /* no collision for this hash, we are done */
1004 return LY_SUCCESS;
1005 }
1006
1007 /* written hash was a collision, write also all the preceding hashes */
Radek Krejci1e008d22020-08-17 11:37:37 +02001008 for (i = 0; !(hash & (LYB_HASH_COLLISION_ID >> i)); ++i) {}
Michal Vasko60ea6352020-06-29 13:39:39 +02001009
Michal Vaskod989ba02020-08-24 10:59:24 +02001010 for ( ; i; --i) {
Michal Vasko11f76c82021-04-15 14:36:14 +02001011 hash = lyb_get_hash(schema, i - 1);
Michal Vasko60ea6352020-06-29 13:39:39 +02001012 if (!hash) {
1013 return LY_EINT;
1014 }
1015 assert(hash & (LYB_HASH_COLLISION_ID >> (i - 1)));
1016
1017 LY_CHECK_RET(lyb_write(out, &hash, sizeof hash, lybctx));
1018 }
1019
1020 return LY_SUCCESS;
1021}
1022
1023/**
1024 * @brief Print data subtree.
1025 *
1026 * @param[in] out Out structure.
1027 * @param[in] node Root node of the subtree to print.
1028 * @param[in,out] sibling_ht Cached hash table for these data siblings, created if NULL.
1029 * @param[in] lybctx LYB context.
1030 * @return LY_ERR value.
1031 */
1032static LY_ERR
1033lyb_print_subtree(struct ly_out *out, const struct lyd_node *node, struct hash_table **sibling_ht, struct lyd_lyb_ctx *lybctx)
1034{
1035 struct hash_table *child_ht = NULL;
1036
1037 /* register a new subtree */
Radek Krejci1798aae2020-07-14 13:26:06 +02001038 LY_CHECK_RET(lyb_write_start_subtree(out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +02001039
aPiecek270f72b2021-09-09 11:39:31 +02001040 LY_CHECK_RET(lyb_print_model_and_hash(out, node, sibling_ht, lybctx->lybctx));
Michal Vaskoc5e866a2020-11-04 17:09:26 +01001041
Michal Vasko60ea6352020-06-29 13:39:39 +02001042 /* write node content */
1043 if (!node->schema) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001044 LY_CHECK_RET(lyb_print_opaq((struct lyd_node_opaq *)node, out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +02001045 } else if (node->schema->nodetype & LYD_NODE_INNER) {
aPiecek270f72b2021-09-09 11:39:31 +02001046 LY_CHECK_RET(lyb_print_node_header(out, node, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +02001047 } else if (node->schema->nodetype & LYD_NODE_TERM) {
aPiecek270f72b2021-09-09 11:39:31 +02001048 LY_CHECK_RET(lyb_print_node_header(out, node, lybctx));
Radek Krejci1798aae2020-07-14 13:26:06 +02001049 LY_CHECK_RET(lyb_print_term((struct lyd_node_term *)node, out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +02001050 } else if (node->schema->nodetype & LYD_NODE_ANY) {
aPiecek270f72b2021-09-09 11:39:31 +02001051 LY_CHECK_RET(lyb_print_node_header(out, node, lybctx));
Radek Krejci1798aae2020-07-14 13:26:06 +02001052 LY_CHECK_RET(lyb_print_anydata((struct lyd_node_any *)node, out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +02001053 } else {
Radek Krejci1798aae2020-07-14 13:26:06 +02001054 LOGINT_RET(lybctx->lybctx->ctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001055 }
1056
1057 /* recursively write all the descendants */
Radek Krejcia1c1e542020-09-29 16:06:52 +02001058 LY_LIST_FOR(lyd_child(node), node) {
Michal Vasko60ea6352020-06-29 13:39:39 +02001059 LY_CHECK_RET(lyb_print_subtree(out, node, &child_ht, lybctx));
1060 }
1061
1062 /* finish this subtree */
Radek Krejci1798aae2020-07-14 13:26:06 +02001063 LY_CHECK_RET(lyb_write_stop_subtree(out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +02001064
1065 return LY_SUCCESS;
1066}
1067
1068LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001069lyb_print_data(struct ly_out *out, const struct lyd_node *root, uint32_t options)
Michal Vasko60ea6352020-06-29 13:39:39 +02001070{
1071 LY_ERR ret = LY_SUCCESS;
1072 uint8_t zero = 0;
Michal Vasko60ea6352020-06-29 13:39:39 +02001073 struct hash_table *top_sibling_ht = NULL;
1074 const struct lys_module *prev_mod = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +02001075 struct lyd_lyb_ctx *lybctx;
Michal Vaskob7be7a82020-08-20 09:09:04 +02001076 const struct ly_ctx *ctx = root ? LYD_CTX(root) : NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +02001077
Radek Krejci1798aae2020-07-14 13:26:06 +02001078 lybctx = calloc(1, sizeof *lybctx);
1079 LY_CHECK_ERR_RET(!lybctx, LOGMEM(ctx), LY_EMEM);
1080 lybctx->lybctx = calloc(1, sizeof *lybctx->lybctx);
1081 LY_CHECK_ERR_RET(!lybctx, LOGMEM(ctx), LY_EMEM);
1082
1083 lybctx->print_options = options;
Michal Vasko60ea6352020-06-29 13:39:39 +02001084 if (root) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001085 lybctx->lybctx->ctx = ctx;
Michal Vasko60ea6352020-06-29 13:39:39 +02001086
1087 if (root->schema && lysc_data_parent(root->schema)) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001088 LOGERR(lybctx->lybctx->ctx, LY_EINVAL, "LYB printer supports only printing top-level nodes.");
Michal Vasko9acaf492020-08-13 09:05:58 +02001089 ret = LY_EINVAL;
1090 goto cleanup;
Michal Vasko60ea6352020-06-29 13:39:39 +02001091 }
1092 }
1093
1094 /* LYB magic number */
Michal Vasko63f3d842020-07-08 10:10:14 +02001095 LY_CHECK_GOTO(ret = lyb_print_magic_number(out), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001096
1097 /* LYB header */
Michal Vasko63f3d842020-07-08 10:10:14 +02001098 LY_CHECK_GOTO(ret = lyb_print_header(out), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001099
1100 /* all used models */
Radek Krejci1798aae2020-07-14 13:26:06 +02001101 LY_CHECK_GOTO(ret = lyb_print_data_models(out, root, lybctx->lybctx), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001102
1103 LY_LIST_FOR(root, root) {
1104 /* do not reuse sibling hash tables from different modules */
1105 if (!root->schema || (root->schema->module != prev_mod)) {
1106 top_sibling_ht = NULL;
1107 prev_mod = root->schema ? root->schema->module : NULL;
1108 }
1109
Radek Krejci1798aae2020-07-14 13:26:06 +02001110 LY_CHECK_GOTO(ret = lyb_print_subtree(out, root, &top_sibling_ht, lybctx), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001111
Radek Krejci7931b192020-06-25 17:05:03 +02001112 if (!(options & LYD_PRINT_WITHSIBLINGS)) {
Michal Vasko60ea6352020-06-29 13:39:39 +02001113 break;
1114 }
1115 }
1116
1117 /* ending zero byte */
Radek Krejci1798aae2020-07-14 13:26:06 +02001118 LY_CHECK_GOTO(ret = lyb_write(out, &zero, sizeof zero, lybctx->lybctx), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001119
1120cleanup:
Radek Krejci1798aae2020-07-14 13:26:06 +02001121 lyd_lyb_ctx_free((struct lyd_ctx *)lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001122 return ret;
1123}