blob: f8c4bfd8254aab48e9226f9424c254a8f5dca07f [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
aPiecek570d7ed2021-09-10 07:15:35 +020041static LY_ERR lyb_print_siblings(struct ly_out *out, const struct lyd_node *node, struct lyd_lyb_ctx *lybctx);
aPiecek270f72b2021-09-09 11:39:31 +020042
Michal Vasko60ea6352020-06-29 13:39:39 +020043/**
44 * @brief Hash table equal callback for checking hash equality only.
Radek Krejci857189e2020-09-01 13:26:36 +020045 *
Michal Vasko62524a92021-02-26 10:08:50 +010046 * Implementation of ::lyht_value_equal_cb.
Michal Vasko60ea6352020-06-29 13:39:39 +020047 */
Radek Krejci857189e2020-09-01 13:26:36 +020048static ly_bool
49lyb_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 +020050{
51 /* for this purpose, if hash matches, the value does also, we do not want 2 values to have the same hash */
52 return 1;
53}
54
55/**
56 * @brief Hash table equal callback for checking value pointer equality only.
Radek Krejci857189e2020-09-01 13:26:36 +020057 *
Michal Vasko62524a92021-02-26 10:08:50 +010058 * Implementation of ::lyht_value_equal_cb.
Michal Vasko60ea6352020-06-29 13:39:39 +020059 */
Radek Krejci857189e2020-09-01 13:26:36 +020060static ly_bool
61lyb_ptr_equal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
Michal Vasko60ea6352020-06-29 13:39:39 +020062{
63 struct lysc_node *val1 = *(struct lysc_node **)val1_p;
64 struct lysc_node *val2 = *(struct lysc_node **)val2_p;
65
66 if (val1 == val2) {
67 return 1;
68 }
69 return 0;
70}
71
72/**
73 * @brief Check that sibling collision hash is safe to insert into hash table.
74 *
75 * @param[in] ht Hash table.
76 * @param[in] sibling Hashed sibling.
77 * @param[in] ht_col_id Sibling hash collision ID.
78 * @param[in] compare_col_id Last collision ID to compare with.
79 * @return LY_SUCCESS when the whole hash sequence does not collide,
80 * @return LY_EEXIST when the whole hash sequence sollides.
81 */
82static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +020083lyb_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 +020084{
Michal Vasko60ea6352020-06-29 13:39:39 +020085 struct lysc_node **col_node;
86
87 /* get the first node inserted with last hash col ID ht_col_id */
Michal Vasko11f76c82021-04-15 14:36:14 +020088 if (lyht_find(ht, &sibling, lyb_get_hash(sibling, ht_col_id), (void **)&col_node)) {
Michal Vasko60ea6352020-06-29 13:39:39 +020089 /* there is none. valid situation */
90 return LY_SUCCESS;
91 }
92
93 lyht_set_cb(ht, lyb_ptr_equal_cb);
94 do {
Radek Krejci1deb5be2020-08-26 16:43:36 +020095 int64_t j;
96 for (j = (int64_t)compare_col_id; j > -1; --j) {
Michal Vasko11f76c82021-04-15 14:36:14 +020097 if (lyb_get_hash(sibling, j) != lyb_get_hash(*col_node, j)) {
Michal Vasko60ea6352020-06-29 13:39:39 +020098 /* one non-colliding hash */
99 break;
100 }
101 }
102 if (j == -1) {
103 /* all whole hash sequences of nodes inserted with last hash col ID compare_col_id collide */
104 lyht_set_cb(ht, lyb_hash_equal_cb);
105 return LY_EEXIST;
106 }
107
108 /* get next node inserted with last hash col ID ht_col_id */
Michal Vasko11f76c82021-04-15 14:36:14 +0200109 } 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 +0200110
111 lyht_set_cb(ht, lyb_hash_equal_cb);
112 return LY_SUCCESS;
113}
114
115/**
116 * @brief Hash all the siblings and add them also into a separate hash table.
117 *
118 * @param[in] sibling Any sibling in all the siblings on one level.
119 * @param[out] ht_p Created hash table.
120 * @return LY_ERR value.
121 */
122static LY_ERR
123lyb_hash_siblings(struct lysc_node *sibling, struct hash_table **ht_p)
124{
125 struct hash_table *ht;
126 const struct lysc_node *parent;
127 const struct lys_module *mod;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200128 LYB_HASH i;
Michal Vasko1e0a80a2020-12-09 18:12:51 +0100129 uint32_t getnext_opts;
Michal Vasko60ea6352020-06-29 13:39:39 +0200130
131 ht = lyht_new(1, sizeof(struct lysc_node *), lyb_hash_equal_cb, NULL, 1);
132 LY_CHECK_ERR_RET(!ht, LOGMEM(sibling->module->ctx), LY_EMEM);
133
Michal Vasko1e0a80a2020-12-09 18:12:51 +0100134 getnext_opts = 0;
Michal Vaskod1e53b92021-01-28 13:11:06 +0100135 if (sibling->flags & LYS_IS_OUTPUT) {
Michal Vasko1e0a80a2020-12-09 18:12:51 +0100136 getnext_opts = LYS_GETNEXT_OUTPUT;
137 }
138
Michal Vasko60ea6352020-06-29 13:39:39 +0200139 parent = lysc_data_parent(sibling);
140 mod = sibling->module;
141
142 sibling = NULL;
Michal Vasko1e0a80a2020-12-09 18:12:51 +0100143 while ((sibling = (struct lysc_node *)lys_getnext(sibling, parent, mod->compiled, getnext_opts))) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200144 /* find the first non-colliding hash (or specifically non-colliding hash sequence) */
145 for (i = 0; i < LYB_HASH_BITS; ++i) {
146 /* check that we are not colliding with nodes inserted with a lower collision ID than ours */
Radek Krejci1deb5be2020-08-26 16:43:36 +0200147 int64_t j;
148 for (j = (int64_t)i - 1; j > -1; --j) {
149 if (lyb_hash_sequence_check(ht, sibling, (LYB_HASH)j, i)) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200150 break;
151 }
152 }
153 if (j > -1) {
154 /* some check failed, we must use a higher collision ID */
155 continue;
156 }
157
158 /* try to insert node with the current collision ID */
Michal Vasko11f76c82021-04-15 14:36:14 +0200159 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 +0200160 /* success, no collision */
161 break;
162 }
163
164 /* make sure we really cannot insert it with this hash col ID (meaning the whole hash sequence is colliding) */
165 if (i && !lyb_hash_sequence_check(ht, sibling, i, i)) {
166 /* it can be inserted after all, even though there is already a node with the same last collision ID */
167 lyht_set_cb(ht, lyb_ptr_equal_cb);
Michal Vasko11f76c82021-04-15 14:36:14 +0200168 if (lyht_insert(ht, &sibling, lyb_get_hash(sibling, i), NULL)) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200169 LOGINT(sibling->module->ctx);
170 lyht_set_cb(ht, lyb_hash_equal_cb);
171 lyht_free(ht);
172 return LY_EINT;
173 }
174 lyht_set_cb(ht, lyb_hash_equal_cb);
175 break;
176 }
177 /* there is still another colliding schema node with the same hash sequence, try higher collision ID */
178 }
179
180 if (i == LYB_HASH_BITS) {
181 /* wow */
182 LOGINT(sibling->module->ctx);
183 lyht_free(ht);
184 return LY_EINT;
185 }
186 }
187
188 /* change val equal callback so that the HT is usable for finding value hashes */
189 lyht_set_cb(ht, lyb_ptr_equal_cb);
190
191 *ht_p = ht;
192 return LY_SUCCESS;
193}
194
195/**
196 * @brief Find node hash in a hash table.
197 *
198 * @param[in] ht Hash table to search in.
199 * @param[in] node Node to find.
200 * @param[out] hash_p First non-colliding hash found.
201 * @return LY_ERR value.
202 */
203static LY_ERR
204lyb_hash_find(struct hash_table *ht, struct lysc_node *node, LYB_HASH *hash_p)
205{
206 LYB_HASH hash;
207 uint32_t i;
208
209 for (i = 0; i < LYB_HASH_BITS; ++i) {
Michal Vasko11f76c82021-04-15 14:36:14 +0200210 hash = lyb_get_hash(node, i);
Michal Vasko60ea6352020-06-29 13:39:39 +0200211 if (!hash) {
212 LOGINT_RET(node->module->ctx);
213 }
214
215 if (!lyht_find(ht, &node, hash, NULL)) {
216 /* success, no collision */
217 break;
218 }
219 }
220 /* cannot happen, we already calculated the hash */
221 if (i == LYB_HASH_BITS) {
222 LOGINT_RET(node->module->ctx);
223 }
224
225 *hash_p = hash;
226 return LY_SUCCESS;
227}
228
229/**
aPiecek6828a312021-09-17 15:53:18 +0200230 * @brief Write metadata about siblings.
231 *
232 * @param[in] out Out structure.
233 * @param[in] sib Contains metadata that is written.
234 */
235static LY_ERR
236lyb_write_sibling_meta(struct ly_out *out, struct lyd_lyb_sibling *sib)
237{
238 uint8_t meta_buf[LYB_META_BYTES];
239 uint64_t num = 0;
240
241 /* write the meta chunk information */
242 num = htole64(sib->written & LYB_SIZE_MAX);
243 memcpy(meta_buf, &num, LYB_SIZE_BYTES);
244 num = htole64(sib->inner_chunks & LYB_INCHUNK_MAX);
245 memcpy(meta_buf + LYB_SIZE_BYTES, &num, LYB_INCHUNK_BYTES);
246
247 LY_CHECK_RET(ly_write_skipped(out, sib->position, (char *)&meta_buf, LYB_META_BYTES));
248
249 return LY_SUCCESS;
250}
251
252/**
Michal Vasko60ea6352020-06-29 13:39:39 +0200253 * @brief Write LYB data fully handling the metadata.
254 *
255 * @param[in] out Out structure.
256 * @param[in] buf Source buffer.
257 * @param[in] count Number of bytes to write.
258 * @param[in] lybctx LYB context.
259 * @return LY_ERR value.
260 */
261static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200262lyb_write(struct ly_out *out, const uint8_t *buf, size_t count, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200263{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200264 LY_ARRAY_COUNT_TYPE u;
aPiecek570d7ed2021-09-10 07:15:35 +0200265 struct lyd_lyb_sibling *full, *iter;
Michal Vasko5233e962020-08-14 14:26:20 +0200266 size_t to_write;
Michal Vasko60ea6352020-06-29 13:39:39 +0200267
268 while (1) {
269 /* check for full data chunks */
270 to_write = count;
271 full = NULL;
aPiecek570d7ed2021-09-10 07:15:35 +0200272 LY_ARRAY_FOR(lybctx->siblings, u) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200273 /* we want the innermost chunks resolved first, so replace previous full chunks */
aPiecek570d7ed2021-09-10 07:15:35 +0200274 if (lybctx->siblings[u].written + to_write >= LYB_SIZE_MAX) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200275 /* full chunk, do not write more than allowed */
aPiecek570d7ed2021-09-10 07:15:35 +0200276 to_write = LYB_SIZE_MAX - lybctx->siblings[u].written;
277 full = &lybctx->siblings[u];
Michal Vasko60ea6352020-06-29 13:39:39 +0200278 }
279 }
280
281 if (!full && !count) {
282 break;
283 }
284
285 /* we are actually writing some data, not just finishing another chunk */
286 if (to_write) {
Michal Vasko5233e962020-08-14 14:26:20 +0200287 LY_CHECK_RET(ly_write_(out, (char *)buf, to_write));
Michal Vasko60ea6352020-06-29 13:39:39 +0200288
aPiecek570d7ed2021-09-10 07:15:35 +0200289 LY_ARRAY_FOR(lybctx->siblings, u) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200290 /* increase all written counters */
aPiecek570d7ed2021-09-10 07:15:35 +0200291 lybctx->siblings[u].written += to_write;
292 assert(lybctx->siblings[u].written <= LYB_SIZE_MAX);
Michal Vasko60ea6352020-06-29 13:39:39 +0200293 }
294 /* decrease count/buf */
Michal Vasko5233e962020-08-14 14:26:20 +0200295 count -= to_write;
296 buf += to_write;
Michal Vasko60ea6352020-06-29 13:39:39 +0200297 }
298
299 if (full) {
300 /* write the meta information (inner chunk count and chunk size) */
aPiecek6828a312021-09-17 15:53:18 +0200301 LY_CHECK_RET(lyb_write_sibling_meta(out, full));
Michal Vasko60ea6352020-06-29 13:39:39 +0200302
303 /* zero written and inner chunks */
304 full->written = 0;
305 full->inner_chunks = 0;
306
307 /* skip space for another chunk size */
Michal Vasko5233e962020-08-14 14:26:20 +0200308 LY_CHECK_RET(ly_write_skip(out, LYB_META_BYTES, &full->position));
Michal Vasko60ea6352020-06-29 13:39:39 +0200309
310 /* increase inner chunk count */
aPiecek570d7ed2021-09-10 07:15:35 +0200311 for (iter = &lybctx->siblings[0]; iter != full; ++iter) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200312 if (iter->inner_chunks == LYB_INCHUNK_MAX) {
313 LOGINT(lybctx->ctx);
314 return LY_EINT;
315 }
316 ++iter->inner_chunks;
317 }
318 }
319 }
320
321 return LY_SUCCESS;
322}
323
324/**
aPiecek570d7ed2021-09-10 07:15:35 +0200325 * @brief Stop the current "siblings" - write its final metadata.
Michal Vasko60ea6352020-06-29 13:39:39 +0200326 *
327 * @param[in] out Out structure.
328 * @param[in] lybctx LYB context.
329 * @return LY_ERR value.
330 */
331static LY_ERR
aPiecek570d7ed2021-09-10 07:15:35 +0200332lyb_write_stop_siblings(struct ly_out *out, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200333{
Michal Vasko60ea6352020-06-29 13:39:39 +0200334 /* write the meta chunk information */
aPiecek6828a312021-09-17 15:53:18 +0200335 lyb_write_sibling_meta(out, &LYB_LAST_SIBLING(lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200336
aPiecek570d7ed2021-09-10 07:15:35 +0200337 LY_ARRAY_DECREMENT(lybctx->siblings);
Michal Vasko60ea6352020-06-29 13:39:39 +0200338 return LY_SUCCESS;
339}
340
341/**
aPiecek570d7ed2021-09-10 07:15:35 +0200342 * @brief Start a new "siblings" - skip bytes for its metadata.
Michal Vasko60ea6352020-06-29 13:39:39 +0200343 *
344 * @param[in] out Out structure.
345 * @param[in] lybctx LYB context.
346 * @return LY_ERR value.
347 */
348static LY_ERR
aPiecek570d7ed2021-09-10 07:15:35 +0200349lyb_write_start_siblings(struct ly_out *out, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200350{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200351 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +0200352
aPiecek570d7ed2021-09-10 07:15:35 +0200353 u = LY_ARRAY_COUNT(lybctx->siblings);
354 if (u == lybctx->sibling_size) {
355 LY_ARRAY_CREATE_RET(lybctx->ctx, lybctx->siblings, u + LYB_SIBLING_STEP, LY_EMEM);
356 lybctx->sibling_size = u + LYB_SIBLING_STEP;
Michal Vasko60ea6352020-06-29 13:39:39 +0200357 }
358
aPiecek570d7ed2021-09-10 07:15:35 +0200359 LY_ARRAY_INCREMENT(lybctx->siblings);
360 LYB_LAST_SIBLING(lybctx).written = 0;
361 LYB_LAST_SIBLING(lybctx).inner_chunks = 0;
Michal Vasko60ea6352020-06-29 13:39:39 +0200362
363 /* another inner chunk */
aPiecek570d7ed2021-09-10 07:15:35 +0200364 for (u = 0; u < LY_ARRAY_COUNT(lybctx->siblings) - 1; ++u) {
365 if (lybctx->siblings[u].inner_chunks == LYB_INCHUNK_MAX) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200366 LOGINT(lybctx->ctx);
Michal Vasko5233e962020-08-14 14:26:20 +0200367 return LY_EINT;
Michal Vasko60ea6352020-06-29 13:39:39 +0200368 }
aPiecek570d7ed2021-09-10 07:15:35 +0200369 ++lybctx->siblings[u].inner_chunks;
Michal Vasko60ea6352020-06-29 13:39:39 +0200370 }
371
aPiecek570d7ed2021-09-10 07:15:35 +0200372 LY_CHECK_RET(ly_write_skip(out, LYB_META_BYTES, &LYB_LAST_SIBLING(lybctx).position));
Michal Vasko60ea6352020-06-29 13:39:39 +0200373
374 return LY_SUCCESS;
375}
376
377/**
378 * @brief Write a number.
379 *
380 * @param[in] num Number to write.
381 * @param[in] bytes Actual accessible bytes of @p num.
382 * @param[in] out Out structure.
383 * @param[in] lybctx LYB context.
384 * @return LY_ERR value.
385 */
386static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200387lyb_write_number(uint64_t num, size_t bytes, struct ly_out *out, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200388{
389 /* correct byte order */
390 num = htole64(num);
391
392 return lyb_write(out, (uint8_t *)&num, bytes, lybctx);
393}
394
395/**
396 * @brief Write a string.
397 *
398 * @param[in] str String to write.
399 * @param[in] str_len Length of @p str.
aPieceke99345d2021-09-30 12:49:59 +0200400 * @param[in] len_size Size of @ str_len in bytes.
Michal Vasko60ea6352020-06-29 13:39:39 +0200401 * @param[in] out Out structure.
402 * @param[in] lybctx LYB context.
403 * @return LY_ERR value.
404 */
405static LY_ERR
aPieceke99345d2021-09-30 12:49:59 +0200406lyb_write_string(const char *str, size_t str_len, uint8_t len_size, struct ly_out *out, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200407{
aPieceke99345d2021-09-30 12:49:59 +0200408 ly_bool error;
409
Michal Vasko60ea6352020-06-29 13:39:39 +0200410 if (!str) {
411 str = "";
Michal Vasko30667352020-08-13 09:06:14 +0200412 LY_CHECK_ERR_RET(str_len, LOGINT(lybctx->ctx), LY_EINT);
Michal Vasko60ea6352020-06-29 13:39:39 +0200413 }
aPieceke99345d2021-09-30 12:49:59 +0200414
Michal Vasko60ea6352020-06-29 13:39:39 +0200415 if (!str_len) {
416 str_len = strlen(str);
417 }
418
aPieceke99345d2021-09-30 12:49:59 +0200419 switch (len_size) {
420 case sizeof(uint8_t):
421 error = str_len > UINT8_MAX;
422 break;
423 case sizeof(uint16_t):
424 error = str_len > UINT16_MAX;
425 break;
426 case sizeof(uint32_t):
427 error = str_len > UINT32_MAX;
428 break;
429 case sizeof(uint64_t):
430 error = str_len > UINT64_MAX;
431 break;
432 default:
433 error = 1;
Michal Vasko60ea6352020-06-29 13:39:39 +0200434 }
aPieceke99345d2021-09-30 12:49:59 +0200435 if (error) {
436 LOGINT(lybctx->ctx);
437 return LY_EINT;
438 }
439
440 LY_CHECK_RET(lyb_write_number(str_len, len_size, out, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200441
Michal Vasko63f3d842020-07-08 10:10:14 +0200442 LY_CHECK_RET(lyb_write(out, (const uint8_t *)str, str_len, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200443
444 return LY_SUCCESS;
445}
446
447/**
448 * @brief Print YANG module info.
449 *
450 * @param[in] out Out structure.
451 * @param[in] mod Module to print.
452 * @param[in] lybctx LYB context.
453 * @return LY_ERR value.
454 */
455static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200456lyb_print_model(struct ly_out *out, const struct lys_module *mod, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200457{
Michal Vasko60ea6352020-06-29 13:39:39 +0200458 uint16_t revision;
459
460 /* model name length and model name */
461 if (mod) {
aPieceke99345d2021-09-30 12:49:59 +0200462 LY_CHECK_RET(lyb_write_string(mod->name, 0, sizeof(uint16_t), out, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200463 } else {
aPiecek570d7ed2021-09-10 07:15:35 +0200464 lyb_write_number(0, 2, out, lybctx);
465 return LY_SUCCESS;
Michal Vasko60ea6352020-06-29 13:39:39 +0200466 }
467
468 /* model revision as XXXX XXXX XXXX XXXX (2B) (year is offset from 2000)
469 * YYYY YYYM MMMD DDDD */
470 revision = 0;
471 if (mod && mod->revision) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200472 int r = atoi(mod->revision);
Radek Krejcif13b87b2020-12-01 22:02:17 +0100473 r -= LYB_REV_YEAR_OFFSET;
474 r <<= LYB_REV_YEAR_SHIFT;
Michal Vasko60ea6352020-06-29 13:39:39 +0200475
476 revision |= r;
477
Radek Krejcif13b87b2020-12-01 22:02:17 +0100478 r = atoi(mod->revision + ly_strlen_const("YYYY-"));
479 r <<= LYB_REV_MONTH_SHIFT;
Michal Vasko60ea6352020-06-29 13:39:39 +0200480
481 revision |= r;
482
Radek Krejcif13b87b2020-12-01 22:02:17 +0100483 r = atoi(mod->revision + ly_strlen_const("YYYY-MM-"));
Michal Vasko60ea6352020-06-29 13:39:39 +0200484
485 revision |= r;
486 }
487 LY_CHECK_RET(lyb_write_number(revision, sizeof revision, out, lybctx));
488
Michal Vasko11f76c82021-04-15 14:36:14 +0200489 if (mod) {
490 /* fill cached hashes, if not already */
491 lyb_cache_module_hash(mod);
492 }
493
Michal Vasko60ea6352020-06-29 13:39:39 +0200494 return LY_SUCCESS;
495}
496
497/**
498 * @brief Print all used YANG modules.
499 *
500 * @param[in] out Out structure.
501 * @param[in] root Data root.
502 * @param[in] lybctx LYB context.
503 * @return LY_ERR value.
504 */
505static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200506lyb_print_data_models(struct ly_out *out, const struct lyd_node *root, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200507{
508 struct ly_set *set;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200509 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +0200510 LY_ERR ret = LY_SUCCESS;
511 struct lys_module *mod;
Michal Vaskof4b4d002021-08-23 12:16:02 +0200512 const struct lyd_node *elem, *node;
Michal Vasko60ea6352020-06-29 13:39:39 +0200513 uint32_t i;
514
Radek Krejciba03a5a2020-08-27 14:40:41 +0200515 LY_CHECK_RET(ly_set_new(&set));
Michal Vasko60ea6352020-06-29 13:39:39 +0200516
517 /* collect all data node modules */
Michal Vaskof4b4d002021-08-23 12:16:02 +0200518 LY_LIST_FOR(root, elem) {
519 LYD_TREE_DFS_BEGIN(elem, node) {
520 if (node->schema) {
521 mod = node->schema->module;
522 ret = ly_set_add(set, mod, 0, NULL);
523 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200524
Michal Vaskof4b4d002021-08-23 12:16:02 +0200525 /* add also their modules deviating or augmenting them */
526 LY_ARRAY_FOR(mod->deviated_by, u) {
527 ret = ly_set_add(set, mod->deviated_by[u], 0, NULL);
528 LY_CHECK_GOTO(ret, cleanup);
529 }
530 LY_ARRAY_FOR(mod->augmented_by, u) {
531 ret = ly_set_add(set, mod->augmented_by[u], 0, NULL);
532 LY_CHECK_GOTO(ret, cleanup);
533 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200534
Michal Vaskof4b4d002021-08-23 12:16:02 +0200535 /* only top-level nodes are processed */
536 LYD_TREE_DFS_continue = 1;
537 }
538
539 LYD_TREE_DFS_END(elem, node);
Michal Vasko60ea6352020-06-29 13:39:39 +0200540 }
541 }
542
543 /* now write module count on 2 bytes */
544 LY_CHECK_GOTO(ret = lyb_write_number(set->count, 2, out, lybctx), cleanup);
545
546 /* and all the used models */
547 for (i = 0; i < set->count; ++i) {
548 LY_CHECK_GOTO(ret = lyb_print_model(out, set->objs[i], lybctx), cleanup);
549 }
550
551cleanup:
552 ly_set_free(set, NULL);
553 return ret;
554}
555
556/**
557 * @brief Print LYB magic number.
558 *
559 * @param[in] out Out structure.
Michal Vasko60ea6352020-06-29 13:39:39 +0200560 * @return LY_ERR value.
561 */
562static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200563lyb_print_magic_number(struct ly_out *out)
Michal Vasko60ea6352020-06-29 13:39:39 +0200564{
Michal Vasko60ea6352020-06-29 13:39:39 +0200565 /* 'l', 'y', 'b' - 0x6c7962 */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100566 char magic_number[] = {'l', 'y', 'b'};
Michal Vasko60ea6352020-06-29 13:39:39 +0200567
Radek Krejcidefd4d72020-12-01 12:20:30 +0100568 LY_CHECK_RET(ly_write_(out, magic_number, 3));
Michal Vasko60ea6352020-06-29 13:39:39 +0200569
570 return LY_SUCCESS;
571}
572
573/**
574 * @brief Print LYB header.
575 *
576 * @param[in] out Out structure.
Michal Vasko60ea6352020-06-29 13:39:39 +0200577 * @return LY_ERR value.
578 */
579static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200580lyb_print_header(struct ly_out *out)
Michal Vasko60ea6352020-06-29 13:39:39 +0200581{
Michal Vasko60ea6352020-06-29 13:39:39 +0200582 uint8_t byte = 0;
583
584 /* version, future flags */
585 byte |= LYB_VERSION_NUM;
586
Michal Vasko5233e962020-08-14 14:26:20 +0200587 LY_CHECK_RET(ly_write_(out, (char *)&byte, 1));
Michal Vasko60ea6352020-06-29 13:39:39 +0200588
589 return LY_SUCCESS;
590}
591
592/**
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100593 * @brief Print prefix data.
Michal Vasko60ea6352020-06-29 13:39:39 +0200594 *
595 * @param[in] out Out structure.
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100596 * @param[in] format Value prefix format.
Radek Krejci8df109d2021-04-23 12:19:08 +0200597 * @param[in] prefix_data Format-specific data for resolving any prefixes (see ::ly_resolve_prefix).
Michal Vasko60ea6352020-06-29 13:39:39 +0200598 * @param[in] lybctx LYB context.
599 * @return LY_ERR value.
600 */
601static LY_ERR
Radek Krejci8df109d2021-04-23 12:19:08 +0200602lyb_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 +0200603{
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100604 const struct ly_set *set;
605 const struct lyxml_ns *ns;
606 uint32_t i;
Michal Vasko60ea6352020-06-29 13:39:39 +0200607
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100608 switch (format) {
Radek Krejci8df109d2021-04-23 12:19:08 +0200609 case LY_VALUE_XML:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100610 set = prefix_data;
611 if (!set) {
612 /* no prefix data */
613 i = 0;
614 LY_CHECK_RET(lyb_write(out, (uint8_t *)&i, 1, lybctx));
615 break;
616 }
617 if (set->count > UINT8_MAX) {
618 LOGERR(lybctx->ctx, LY_EINT, "Maximum supported number of prefixes is %u.", UINT8_MAX);
619 return LY_EINT;
620 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200621
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100622 /* write number of prefixes on 1 byte */
Michal Vasko403beac2021-08-24 08:27:52 +0200623 LY_CHECK_RET(lyb_write_number(set->count, 1, out, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200624
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100625 /* write all the prefixes */
626 for (i = 0; i < set->count; ++i) {
627 ns = set->objs[i];
Michal Vasko60ea6352020-06-29 13:39:39 +0200628
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100629 /* prefix */
aPieceke99345d2021-09-30 12:49:59 +0200630 LY_CHECK_RET(lyb_write_string(ns->prefix, 0, sizeof(uint16_t), out, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200631
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100632 /* namespace */
aPieceke99345d2021-09-30 12:49:59 +0200633 LY_CHECK_RET(lyb_write_string(ns->uri, 0, sizeof(uint16_t), out, lybctx));
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100634 }
635 break;
Radek Krejci8df109d2021-04-23 12:19:08 +0200636 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +0200637 case LY_VALUE_LYB:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100638 /* nothing to print */
639 break;
640 default:
641 LOGINT_RET(lybctx->ctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200642 }
643
644 return LY_SUCCESS;
645}
646
647/**
Michal Vasko60ea6352020-06-29 13:39:39 +0200648 * @brief Print term node.
649 *
650 * @param[in] term Node to print.
651 * @param[in] out Out structure.
652 * @param[in] lybctx LYB context.
653 * @return LY_ERR value.
654 */
655static LY_ERR
aPieceka19ca152021-09-09 12:27:35 +0200656lyb_print_term_value(struct lyd_node_term *term, struct ly_out *out, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200657{
aPiecekaa5b70a2021-08-30 08:33:25 +0200658 LY_ERR ret = LY_SUCCESS;
aPiecekea304e32021-08-18 09:13:47 +0200659 ly_bool dynamic = 0;
660 void *value;
661 size_t value_len = 0;
aPiecekaa5b70a2021-08-30 08:33:25 +0200662 int32_t lyb_data_len;
663 lyplg_type_print_clb print;
aPiecekea304e32021-08-18 09:13:47 +0200664
665 assert(term->value.realtype && term->value.realtype->plugin && term->value.realtype->plugin->print &&
666 term->schema);
667
aPiecekaa5b70a2021-08-30 08:33:25 +0200668 /* Get length of LYB data to print. */
669 lyb_data_len = term->value.realtype->plugin->lyb_data_len;
aPiecekea304e32021-08-18 09:13:47 +0200670
aPiecekaa5b70a2021-08-30 08:33:25 +0200671 /* Get value and also print its length only if size is not fixed. */
672 print = term->value.realtype->plugin->print;
673 if (lyb_data_len < 0) {
674 /* Variable-length data. */
675
676 /* Get value and its length from plugin. */
677 value = (void *)print(term->schema->module->ctx, &term->value,
678 LY_VALUE_LYB, NULL, &dynamic, &value_len);
679 LY_CHECK_GOTO(ret, cleanup);
680
681 if (value_len > UINT32_MAX) {
682 LOGERR(lybctx->ctx, LY_EINT, "The maximum length of the LYB data "
683 "from a term node must not exceed %lu.", UINT32_MAX);
684 ret = LY_EINT;
685 goto cleanup;
686 }
687
aPieceke99345d2021-09-30 12:49:59 +0200688 /* Print the length of the data as 64-bit unsigned integer. */
689 ret = lyb_write_number(value_len, sizeof(uint64_t), out, lybctx);
aPiecekaa5b70a2021-08-30 08:33:25 +0200690 LY_CHECK_GOTO(ret, cleanup);
691 } else {
692 /* Fixed-length data. */
693
694 /* Get value from plugin. */
695 value = (void *)print(term->schema->module->ctx, &term->value,
696 LY_VALUE_LYB, NULL, &dynamic, NULL);
697 LY_CHECK_GOTO(ret, cleanup);
698
699 /* Copy the length from the compiled node. */
700 value_len = lyb_data_len;
aPiecekea304e32021-08-18 09:13:47 +0200701 }
702
aPiecekaa5b70a2021-08-30 08:33:25 +0200703 /* Print value. */
aPiecekea304e32021-08-18 09:13:47 +0200704 if (value_len > 0) {
aPiecekaa5b70a2021-08-30 08:33:25 +0200705 /* Print the value simply as it is. */
aPiecekea304e32021-08-18 09:13:47 +0200706 ret = lyb_write(out, value, value_len, lybctx);
707 LY_CHECK_GOTO(ret, cleanup);
708 }
709
710cleanup:
711 if (dynamic) {
712 free(value);
713 }
714
715 return ret;
Michal Vasko60ea6352020-06-29 13:39:39 +0200716}
717
718/**
719 * @brief Print YANG node metadata.
720 *
721 * @param[in] out Out structure.
722 * @param[in] node Data node whose metadata to print.
723 * @param[in] lybctx LYB context.
724 * @return LY_ERR value.
725 */
726static LY_ERR
727lyb_print_metadata(struct ly_out *out, const struct lyd_node *node, struct lyd_lyb_ctx *lybctx)
728{
Michal Vasko60ea6352020-06-29 13:39:39 +0200729 uint8_t count = 0;
730 const struct lys_module *wd_mod = NULL;
731 struct lyd_meta *iter;
Michal Vasko60ea6352020-06-29 13:39:39 +0200732
733 /* with-defaults */
734 if (node->schema->nodetype & LYD_NODE_TERM) {
Radek Krejci7931b192020-06-25 17:05:03 +0200735 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 +0200736 ((lybctx->print_options & LYD_PRINT_WD_ALL_TAG) && lyd_is_default(node))) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200737 /* we have implicit OR explicit default node, print attribute only if context include with-defaults schema */
738 wd_mod = ly_ctx_get_module_latest(node->schema->module->ctx, "ietf-netconf-with-defaults");
739 }
740 }
741
742 /* count metadata */
743 if (wd_mod) {
744 ++count;
745 }
746 for (iter = node->meta; iter; iter = iter->next) {
747 if (count == UINT8_MAX) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200748 LOGERR(lybctx->lybctx->ctx, LY_EINT, "Maximum supported number of data node metadata is %u.", UINT8_MAX);
Michal Vasko60ea6352020-06-29 13:39:39 +0200749 return LY_EINT;
750 }
751 ++count;
752 }
753
754 /* write number of metadata on 1 byte */
Radek Krejci1798aae2020-07-14 13:26:06 +0200755 LY_CHECK_RET(lyb_write(out, &count, 1, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200756
757 if (wd_mod) {
758 /* write the "default" metadata */
Radek Krejci1798aae2020-07-14 13:26:06 +0200759 LY_CHECK_RET(lyb_print_model(out, wd_mod, lybctx->lybctx));
aPieceke99345d2021-09-30 12:49:59 +0200760 LY_CHECK_RET(lyb_write_string("default", 0, sizeof(uint16_t), out, lybctx->lybctx));
761 LY_CHECK_RET(lyb_write_string("true", 0, sizeof(uint16_t), out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200762 }
763
764 /* write all the node metadata */
765 LY_LIST_FOR(node->meta, iter) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200766 /* model */
Radek Krejci1798aae2020-07-14 13:26:06 +0200767 LY_CHECK_RET(lyb_print_model(out, iter->annotation->module, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200768
769 /* annotation name with length */
aPieceke99345d2021-09-30 12:49:59 +0200770 LY_CHECK_RET(lyb_write_string(iter->name, 0, sizeof(uint16_t), out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200771
Michal Vasko60ea6352020-06-29 13:39:39 +0200772 /* metadata value */
aPieceke99345d2021-09-30 12:49:59 +0200773 LY_CHECK_RET(lyb_write_string(lyd_get_meta_value(iter), 0, sizeof(uint64_t), out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200774 }
775
776 return LY_SUCCESS;
777}
778
779/**
780 * @brief Print opaque node attributes.
781 *
782 * @param[in] out Out structure.
783 * @param[in] node Opaque node whose attributes to print.
784 * @param[in] lybctx LYB context.
785 * @return LY_ERR value.
786 */
787static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200788lyb_print_attributes(struct ly_out *out, const struct lyd_node_opaq *node, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200789{
790 uint8_t count = 0;
Radek Krejci5536d282020-08-04 23:27:44 +0200791 struct lyd_attr *iter;
Michal Vasko60ea6352020-06-29 13:39:39 +0200792
793 for (iter = node->attr; iter; iter = iter->next) {
794 if (count == UINT8_MAX) {
795 LOGERR(lybctx->ctx, LY_EINT, "Maximum supported number of data node attributes is %u.", UINT8_MAX);
796 return LY_EINT;
797 }
798 ++count;
799 }
800
801 /* write number of attributes on 1 byte */
802 LY_CHECK_RET(lyb_write(out, &count, 1, lybctx));
803
804 /* write all the attributes */
805 LY_LIST_FOR(node->attr, iter) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200806 /* prefix */
aPieceke99345d2021-09-30 12:49:59 +0200807 LY_CHECK_RET(lyb_write_string(iter->name.prefix, 0, sizeof(uint16_t), out, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200808
809 /* namespace */
aPieceke99345d2021-09-30 12:49:59 +0200810 LY_CHECK_RET(lyb_write_string(iter->name.module_name, 0, sizeof(uint16_t), out, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200811
812 /* name */
aPieceke99345d2021-09-30 12:49:59 +0200813 LY_CHECK_RET(lyb_write_string(iter->name.name, 0, sizeof(uint16_t), out, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200814
Michal Vasko60ea6352020-06-29 13:39:39 +0200815 /* format */
816 LY_CHECK_RET(lyb_write_number(iter->format, 1, out, lybctx));
817
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100818 /* value prefixes */
819 LY_CHECK_RET(lyb_print_prefix_data(out, iter->format, iter->val_prefix_data, lybctx));
820
Michal Vasko60ea6352020-06-29 13:39:39 +0200821 /* value */
aPieceke99345d2021-09-30 12:49:59 +0200822 LY_CHECK_RET(lyb_write_string(iter->value, 0, sizeof(uint64_t), out, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200823 }
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 Vasko11f76c82021-04-15 14:36:14 +0200893 hash = lyb_get_hash(schema, i - 1);
Michal Vasko60ea6352020-06-29 13:39:39 +0200894 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/**
aPiecek6fdac2f2021-09-10 11:21:10 +0200906 * @brief Print header for non-opaq node.
907 *
908 * @param[in] out Out structure.
909 * @param[in] node Current data node to print.
910 * @param[in] lybctx LYB context.
911 * @return LY_ERR value.
912 */
913static LY_ERR
914lyb_print_node_header(struct ly_out *out, const struct lyd_node *node, struct lyd_lyb_ctx *lybctx)
915{
916 /* write any metadata */
917 LY_CHECK_RET(lyb_print_metadata(out, node, lybctx));
918
919 /* write node flags */
920 LY_CHECK_RET(lyb_write_number(node->flags, sizeof node->flags, out, lybctx->lybctx));
921
922 return LY_SUCCESS;
923}
924
925/**
aPiecek5777f122021-09-10 10:26:23 +0200926 * @brief Print inner node.
Michal Vasko60ea6352020-06-29 13:39:39 +0200927 *
928 * @param[in] out Out structure.
aPiecek5777f122021-09-10 10:26:23 +0200929 * @param[in] node Current data node to print.
Michal Vasko60ea6352020-06-29 13:39:39 +0200930 * @param[in] lybctx LYB context.
931 * @return LY_ERR value.
932 */
933static LY_ERR
aPiecek5777f122021-09-10 10:26:23 +0200934lyb_print_node_inner(struct ly_out *out, const struct lyd_node *node, struct lyd_lyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200935{
aPiecek586b1d22021-09-10 09:00:57 +0200936 /* write necessary basic data */
937 LY_CHECK_RET(lyb_print_node_header(out, node, lybctx));
aPiecek570d7ed2021-09-10 07:15:35 +0200938
aPiecek5777f122021-09-10 10:26:23 +0200939 /* recursively write all the descendants */
940 LY_CHECK_RET(lyb_print_siblings(out, lyd_child(node), lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200941
aPiecek570d7ed2021-09-10 07:15:35 +0200942 return LY_SUCCESS;
943}
944
945/**
aPiecek6fdac2f2021-09-10 11:21:10 +0200946 * @brief Print opaque node and its descendants.
947 *
948 * @param[in] out Out structure.
949 * @param[in] opaq Node to print.
950 * @param[in] lyd_lybctx LYB context.
951 * @return LY_ERR value.
952 */
953static LY_ERR
954lyb_print_node_opaq(struct ly_out *out, const struct lyd_node_opaq *opaq, struct lyd_lyb_ctx *lyd_lybctx)
955{
956 struct lylyb_ctx *lybctx = lyd_lybctx->lybctx;
957
958 /* write attributes */
959 LY_CHECK_RET(lyb_print_attributes(out, opaq, lybctx));
960
961 /* write node flags */
962 LY_CHECK_RET(lyb_write_number(opaq->flags, sizeof opaq->flags, out, lybctx));
963
964 /* prefix */
aPieceke99345d2021-09-30 12:49:59 +0200965 LY_CHECK_RET(lyb_write_string(opaq->name.prefix, 0, sizeof(uint16_t), out, lybctx));
aPiecek6fdac2f2021-09-10 11:21:10 +0200966
967 /* module reference */
aPieceke99345d2021-09-30 12:49:59 +0200968 LY_CHECK_RET(lyb_write_string(opaq->name.module_name, 0, sizeof(uint16_t), out, lybctx));
aPiecek6fdac2f2021-09-10 11:21:10 +0200969
970 /* name */
aPieceke99345d2021-09-30 12:49:59 +0200971 LY_CHECK_RET(lyb_write_string(opaq->name.name, 0, sizeof(uint16_t), out, lybctx));
aPiecek6fdac2f2021-09-10 11:21:10 +0200972
973 /* value */
aPieceke99345d2021-09-30 12:49:59 +0200974 LY_CHECK_RET(lyb_write_string(opaq->value, 0, sizeof(uint64_t), out, lybctx));
aPiecek6fdac2f2021-09-10 11:21:10 +0200975
976 /* format */
977 LY_CHECK_RET(lyb_write_number(opaq->format, 1, out, lybctx));
978
979 /* value prefixes */
980 LY_CHECK_RET(lyb_print_prefix_data(out, opaq->format, opaq->val_prefix_data, lybctx));
981
982 /* recursively write all the descendants */
983 LY_CHECK_RET(lyb_print_siblings(out, opaq->child, lyd_lybctx));
984
985 return LY_SUCCESS;
986}
987
988/**
989 * @brief Print anydata or anyxml node.
990 *
991 * @param[in] anydata Node to print.
992 * @param[in] out Out structure.
993 * @param[in] lyd_lybctx LYB context.
994 * @return LY_ERR value.
995 */
996static LY_ERR
997lyb_print_node_any(struct ly_out *out, struct lyd_node_any *anydata, struct lyd_lyb_ctx *lyd_lybctx)
998{
999 LY_ERR ret = LY_SUCCESS;
1000 LYD_ANYDATA_VALUETYPE value_type;
1001 int len;
1002 char *buf = NULL;
1003 const char *str;
1004 struct ly_out *out2 = NULL;
1005 struct lylyb_ctx *lybctx = lyd_lybctx->lybctx;
1006
1007 if (anydata->value_type == LYD_ANYDATA_DATATREE) {
1008 /* will be printed as a nested LYB data tree */
1009 value_type = LYD_ANYDATA_LYB;
1010 } else {
1011 value_type = anydata->value_type;
1012 }
1013
1014 /* write necessary basic data */
1015 LY_CHECK_RET(lyb_print_node_header(out, (struct lyd_node *)anydata, lyd_lybctx));
1016
1017 /* first byte is type */
1018 LY_CHECK_GOTO(ret = lyb_write_number(value_type, sizeof value_type, out, lybctx), cleanup);
1019
1020 if (anydata->value_type == LYD_ANYDATA_DATATREE) {
1021 /* print LYB data tree to memory */
1022 LY_CHECK_GOTO(ret = ly_out_new_memory(&buf, 0, &out2), cleanup);
1023 LY_CHECK_GOTO(ret = lyb_print_data(out2, anydata->value.tree, LYD_PRINT_WITHSIBLINGS), cleanup);
1024
1025 len = lyd_lyb_data_length(buf);
1026 assert(len != -1);
1027 str = buf;
1028 } else if (anydata->value_type == LYD_ANYDATA_LYB) {
1029 len = lyd_lyb_data_length(anydata->value.mem);
1030 assert(len != -1);
1031 str = anydata->value.mem;
1032 } else {
1033 len = strlen(anydata->value.str);
1034 str = anydata->value.str;
1035 }
1036
1037 /* followed by the content */
aPieceke99345d2021-09-30 12:49:59 +02001038 LY_CHECK_GOTO(ret = lyb_write_string(str, (size_t)len, sizeof(uint64_t), out, lybctx), cleanup);
aPiecek6fdac2f2021-09-10 11:21:10 +02001039
1040cleanup:
1041 ly_out_free(out2, NULL, 1);
1042 return ret;
1043}
1044
1045/**
aPiecek8a555d72021-09-10 10:15:26 +02001046 * @brief Print leaf node.
1047 *
1048 * @param[in] out Out structure.
1049 * @param[in] node Current data node to print.
1050 * @param[in] lybctx LYB context.
1051 * @return LY_ERR value.
1052 */
1053static LY_ERR
1054lyb_print_node_leaf(struct ly_out *out, const struct lyd_node *node, struct lyd_lyb_ctx *lybctx)
1055{
1056 /* write necessary basic data */
1057 LY_CHECK_RET(lyb_print_node_header(out, node, lybctx));
1058
1059 /* write term value */
1060 LY_CHECK_RET(lyb_print_term_value((struct lyd_node_term *)node, out, lybctx->lybctx));
1061
1062 return LY_SUCCESS;
1063}
1064
1065/**
aPiecek5777f122021-09-10 10:26:23 +02001066 * @brief Print all leaflist nodes which belong to same schema.
1067 *
1068 * @param[in] out Out structure.
1069 * @param[in] node Current data node to print.
1070 * @param[in] lybctx LYB context.
1071 * @param[out] printed_node Last node that was printed by this function.
1072 * @return LY_ERR value.
1073 */
1074static LY_ERR
1075lyb_print_node_leaflist(struct ly_out *out, const struct lyd_node *node, struct lyd_lyb_ctx *lybctx,
1076 const struct lyd_node **printed_node)
1077{
1078 const struct lysc_node *schema;
1079
1080 /* register a new sibling */
1081 LY_CHECK_RET(lyb_write_start_siblings(out, lybctx->lybctx));
1082
1083 schema = node->schema;
1084
1085 /* write all the siblings */
1086 LY_LIST_FOR(node, node) {
1087 if (schema != node->schema) {
1088 /* all leaflist nodes was printed */
1089 break;
1090 }
1091
1092 /* write leaf data */
1093 LY_CHECK_RET(lyb_print_node_leaf(out, node, lybctx));
1094 *printed_node = node;
1095 }
1096
1097 /* finish this sibling */
1098 LY_CHECK_RET(lyb_write_stop_siblings(out, lybctx->lybctx));
1099
1100 return LY_SUCCESS;
1101}
1102
1103/**
aPiecek77d3e962021-09-10 10:33:59 +02001104 * @brief Print all list nodes which belong to same schema.
1105 *
1106 * @param[in] out Out structure.
1107 * @param[in] node Current data node to print.
1108 * @param[in] lybctx LYB context.
1109 * @param[out] printed_node Last node that was printed by this function.
1110 * @return LY_ERR value.
1111 */
1112static LY_ERR
1113lyb_print_node_list(struct ly_out *out, const struct lyd_node *node, struct lyd_lyb_ctx *lybctx,
1114 const struct lyd_node **printed_node)
1115{
1116 const struct lysc_node *schema;
1117
1118 /* register a new sibling */
1119 LY_CHECK_RET(lyb_write_start_siblings(out, lybctx->lybctx));
1120
1121 schema = node->schema;
1122
1123 LY_LIST_FOR(node, node) {
1124 if (schema != node->schema) {
1125 /* all list nodes was printed */
1126 break;
1127 }
1128
1129 /* write necessary basic data */
1130 LY_CHECK_RET(lyb_print_node_header(out, node, lybctx));
1131
1132 /* recursively write all the descendants */
1133 LY_CHECK_RET(lyb_print_siblings(out, lyd_child(node), lybctx));
1134
1135 *printed_node = node;
1136 }
1137
1138 /* finish this sibling */
1139 LY_CHECK_RET(lyb_write_stop_siblings(out, lybctx->lybctx));
1140
1141 return LY_SUCCESS;
1142}
1143
1144/**
aPiecek17737b52021-09-21 12:29:52 +02001145 * @brief Print node.
1146 *
1147 * @param[in] out Out structure.
1148 * @param[in,out] printed_node Current data node to print. Sets to the last printed node.
1149 * @param[in,out] sibling_ht Cached hash table for these siblings, created if NULL.
1150 * @param[in] lybctx LYB context.
1151 * @return LY_ERR value.
1152 */
1153static LY_ERR
1154lyb_print_node(struct ly_out *out, const struct lyd_node **printed_node, struct hash_table **sibling_ht,
1155 struct lyd_lyb_ctx *lybctx)
1156{
1157 const struct lyd_node *node = *printed_node;
1158
1159 /* write model info first, for all opaque and top-level nodes */
1160 if (!node->schema && (!node->parent || !node->parent->schema)) {
1161 LY_CHECK_RET(lyb_print_model(out, NULL, lybctx->lybctx));
1162 } else if (node->schema && !lysc_data_parent(node->schema)) {
1163 LY_CHECK_RET(lyb_print_model(out, node->schema->module, lybctx->lybctx));
1164 }
1165
1166 /* write schema hash */
1167 LY_CHECK_RET(lyb_print_schema_hash(out, (struct lysc_node *)node->schema, sibling_ht, lybctx->lybctx));
1168
1169 if (!node->schema) {
1170 LY_CHECK_RET(lyb_print_node_opaq(out, (struct lyd_node_opaq *)node, lybctx));
1171 } else if (node->schema->nodetype & LYS_LEAFLIST) {
1172 LY_CHECK_RET(lyb_print_node_leaflist(out, node, lybctx, &node));
1173 } else if (node->schema->nodetype == LYS_LIST) {
1174 LY_CHECK_RET(lyb_print_node_list(out, node, lybctx, &node));
1175 } else if (node->schema->nodetype & LYD_NODE_ANY) {
1176 LY_CHECK_RET(lyb_print_node_any(out, (struct lyd_node_any *)node, lybctx));
1177 } else if (node->schema->nodetype & LYD_NODE_INNER) {
1178 LY_CHECK_RET(lyb_print_node_inner(out, node, lybctx));
1179 } else {
1180 LY_CHECK_RET(lyb_print_node_leaf(out, node, lybctx));
1181 }
1182
1183 *printed_node = node;
1184
1185 return LY_SUCCESS;
1186}
1187
1188/**
aPiecek570d7ed2021-09-10 07:15:35 +02001189 * @brief Print siblings.
1190 *
aPiecek6c194d72021-09-10 10:40:36 +02001191 * @verbatim
1192
1193 sb = siblings_start
1194 se = siblings_end
aPiecek6828a312021-09-17 15:53:18 +02001195 siblings = zero-LYB_SIZE_BYTES | (sb instance+ se)
aPiecek6c194d72021-09-10 10:40:36 +02001196 instance = model hash node
1197 model = 16bit_zero | (model_name_length model_name revision)
1198 node = opaq | leaflist | list | any | inner | leaf
1199 opaq = opaq_data siblings
1200 leaflist = sb leaf+ se
1201 list = sb (node_header siblings)+ se
1202 any = node_header anydata_data
1203 inner = node_header siblings
1204 leaf = node_header term_value
1205 node_header = metadata node_flags
1206
1207 @endverbatim
1208 *
aPiecek570d7ed2021-09-10 07:15:35 +02001209 * @param[in] out Out structure.
1210 * @param[in] node Current data node to print.
1211 * @param[in] lybctx LYB context.
1212 * @return LY_ERR value.
1213 */
1214static LY_ERR
1215lyb_print_siblings(struct ly_out *out, const struct lyd_node *node, struct lyd_lyb_ctx *lybctx)
1216{
1217 struct hash_table *sibling_ht = NULL;
1218 const struct lys_module *prev_mod = NULL;
1219 ly_bool top_level;
aPiecek6828a312021-09-17 15:53:18 +02001220 uint8_t zero[LYB_SIZE_BYTES] = {0};
aPiecek570d7ed2021-09-10 07:15:35 +02001221
1222 if (!node) {
aPiecek6828a312021-09-17 15:53:18 +02001223 lyb_write(out, zero, LYB_SIZE_BYTES, lybctx->lybctx);
aPiecek570d7ed2021-09-10 07:15:35 +02001224 return LY_SUCCESS;
Michal Vasko60ea6352020-06-29 13:39:39 +02001225 }
1226
aPiecek570d7ed2021-09-10 07:15:35 +02001227 top_level = !LY_ARRAY_COUNT(lybctx->lybctx->siblings);
1228
1229 LY_CHECK_RET(lyb_write_start_siblings(out, lybctx->lybctx));
1230
aPiecek17737b52021-09-21 12:29:52 +02001231 if (top_level) {
1232 /* write all the siblings */
1233 LY_LIST_FOR(node, node) {
1234 /* do not reuse sibling hash tables from different modules */
1235 if (!node->schema || (node->schema->module != prev_mod)) {
1236 sibling_ht = NULL;
1237 prev_mod = node->schema ? node->schema->module : NULL;
1238 }
aPiecek570d7ed2021-09-10 07:15:35 +02001239
aPiecek17737b52021-09-21 12:29:52 +02001240 LY_CHECK_RET(lyb_print_node(out, &node, &sibling_ht, lybctx));
1241
1242 if (!(lybctx->print_options & LYD_PRINT_WITHSIBLINGS)) {
1243 break;
1244 }
aPiecek570d7ed2021-09-10 07:15:35 +02001245 }
aPiecek17737b52021-09-21 12:29:52 +02001246 } else {
1247 LY_LIST_FOR(node, node) {
1248 LY_CHECK_RET(lyb_print_node(out, &node, &sibling_ht, lybctx));
aPiecek570d7ed2021-09-10 07:15:35 +02001249 }
1250 }
1251
1252 LY_CHECK_RET(lyb_write_stop_siblings(out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +02001253
1254 return LY_SUCCESS;
1255}
1256
1257LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001258lyb_print_data(struct ly_out *out, const struct lyd_node *root, uint32_t options)
Michal Vasko60ea6352020-06-29 13:39:39 +02001259{
1260 LY_ERR ret = LY_SUCCESS;
1261 uint8_t zero = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +02001262 struct lyd_lyb_ctx *lybctx;
Michal Vaskob7be7a82020-08-20 09:09:04 +02001263 const struct ly_ctx *ctx = root ? LYD_CTX(root) : NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +02001264
Radek Krejci1798aae2020-07-14 13:26:06 +02001265 lybctx = calloc(1, sizeof *lybctx);
1266 LY_CHECK_ERR_RET(!lybctx, LOGMEM(ctx), LY_EMEM);
1267 lybctx->lybctx = calloc(1, sizeof *lybctx->lybctx);
1268 LY_CHECK_ERR_RET(!lybctx, LOGMEM(ctx), LY_EMEM);
1269
1270 lybctx->print_options = options;
Michal Vasko60ea6352020-06-29 13:39:39 +02001271 if (root) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001272 lybctx->lybctx->ctx = ctx;
Michal Vasko60ea6352020-06-29 13:39:39 +02001273
1274 if (root->schema && lysc_data_parent(root->schema)) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001275 LOGERR(lybctx->lybctx->ctx, LY_EINVAL, "LYB printer supports only printing top-level nodes.");
Michal Vasko9acaf492020-08-13 09:05:58 +02001276 ret = LY_EINVAL;
1277 goto cleanup;
Michal Vasko60ea6352020-06-29 13:39:39 +02001278 }
1279 }
1280
1281 /* LYB magic number */
Michal Vasko63f3d842020-07-08 10:10:14 +02001282 LY_CHECK_GOTO(ret = lyb_print_magic_number(out), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001283
1284 /* LYB header */
Michal Vasko63f3d842020-07-08 10:10:14 +02001285 LY_CHECK_GOTO(ret = lyb_print_header(out), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001286
1287 /* all used models */
Radek Krejci1798aae2020-07-14 13:26:06 +02001288 LY_CHECK_GOTO(ret = lyb_print_data_models(out, root, lybctx->lybctx), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001289
aPiecek570d7ed2021-09-10 07:15:35 +02001290 ret = lyb_print_siblings(out, root, lybctx);
1291 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001292
1293 /* ending zero byte */
Radek Krejci1798aae2020-07-14 13:26:06 +02001294 LY_CHECK_GOTO(ret = lyb_write(out, &zero, sizeof zero, lybctx->lybctx), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001295
1296cleanup:
Radek Krejci1798aae2020-07-14 13:26:06 +02001297 lyd_lyb_ctx_free((struct lyd_ctx *)lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001298 return ret;
1299}