blob: 10fea3fae1a2cd999295242f1289f77e5e7202ad [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);
aPiecek5777f122021-09-10 10:26:23 +020043static LY_ERR lyb_print_node_inner(struct ly_out *out, const struct lyd_node *node, struct lyd_lyb_ctx *lybctx);
aPiecek570d7ed2021-09-10 07:15:35 +020044static LY_ERR lyb_print_siblings(struct ly_out *out, const struct lyd_node *node, struct lyd_lyb_ctx *lybctx);
aPiecek843157f2021-09-09 12:38:59 +020045static LY_ERR lyb_print_node_header(struct ly_out *out, const struct lyd_node *node, struct lyd_lyb_ctx *lybctx);
aPiecek270f72b2021-09-09 11:39:31 +020046
Michal Vasko60ea6352020-06-29 13:39:39 +020047/**
48 * @brief Hash table equal callback for checking hash equality only.
Radek Krejci857189e2020-09-01 13:26:36 +020049 *
Michal Vasko62524a92021-02-26 10:08:50 +010050 * Implementation of ::lyht_value_equal_cb.
Michal Vasko60ea6352020-06-29 13:39:39 +020051 */
Radek Krejci857189e2020-09-01 13:26:36 +020052static ly_bool
53lyb_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 +020054{
55 /* for this purpose, if hash matches, the value does also, we do not want 2 values to have the same hash */
56 return 1;
57}
58
59/**
60 * @brief Hash table equal callback for checking value pointer equality only.
Radek Krejci857189e2020-09-01 13:26:36 +020061 *
Michal Vasko62524a92021-02-26 10:08:50 +010062 * Implementation of ::lyht_value_equal_cb.
Michal Vasko60ea6352020-06-29 13:39:39 +020063 */
Radek Krejci857189e2020-09-01 13:26:36 +020064static ly_bool
65lyb_ptr_equal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
Michal Vasko60ea6352020-06-29 13:39:39 +020066{
67 struct lysc_node *val1 = *(struct lysc_node **)val1_p;
68 struct lysc_node *val2 = *(struct lysc_node **)val2_p;
69
70 if (val1 == val2) {
71 return 1;
72 }
73 return 0;
74}
75
76/**
77 * @brief Check that sibling collision hash is safe to insert into hash table.
78 *
79 * @param[in] ht Hash table.
80 * @param[in] sibling Hashed sibling.
81 * @param[in] ht_col_id Sibling hash collision ID.
82 * @param[in] compare_col_id Last collision ID to compare with.
83 * @return LY_SUCCESS when the whole hash sequence does not collide,
84 * @return LY_EEXIST when the whole hash sequence sollides.
85 */
86static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +020087lyb_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 +020088{
Michal Vasko60ea6352020-06-29 13:39:39 +020089 struct lysc_node **col_node;
90
91 /* get the first node inserted with last hash col ID ht_col_id */
Michal Vasko11f76c82021-04-15 14:36:14 +020092 if (lyht_find(ht, &sibling, lyb_get_hash(sibling, ht_col_id), (void **)&col_node)) {
Michal Vasko60ea6352020-06-29 13:39:39 +020093 /* there is none. valid situation */
94 return LY_SUCCESS;
95 }
96
97 lyht_set_cb(ht, lyb_ptr_equal_cb);
98 do {
Radek Krejci1deb5be2020-08-26 16:43:36 +020099 int64_t j;
100 for (j = (int64_t)compare_col_id; j > -1; --j) {
Michal Vasko11f76c82021-04-15 14:36:14 +0200101 if (lyb_get_hash(sibling, j) != lyb_get_hash(*col_node, j)) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200102 /* one non-colliding hash */
103 break;
104 }
105 }
106 if (j == -1) {
107 /* all whole hash sequences of nodes inserted with last hash col ID compare_col_id collide */
108 lyht_set_cb(ht, lyb_hash_equal_cb);
109 return LY_EEXIST;
110 }
111
112 /* get next node inserted with last hash col ID ht_col_id */
Michal Vasko11f76c82021-04-15 14:36:14 +0200113 } 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 +0200114
115 lyht_set_cb(ht, lyb_hash_equal_cb);
116 return LY_SUCCESS;
117}
118
119/**
120 * @brief Hash all the siblings and add them also into a separate hash table.
121 *
122 * @param[in] sibling Any sibling in all the siblings on one level.
123 * @param[out] ht_p Created hash table.
124 * @return LY_ERR value.
125 */
126static LY_ERR
127lyb_hash_siblings(struct lysc_node *sibling, struct hash_table **ht_p)
128{
129 struct hash_table *ht;
130 const struct lysc_node *parent;
131 const struct lys_module *mod;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200132 LYB_HASH i;
Michal Vasko1e0a80a2020-12-09 18:12:51 +0100133 uint32_t getnext_opts;
Michal Vasko60ea6352020-06-29 13:39:39 +0200134
135 ht = lyht_new(1, sizeof(struct lysc_node *), lyb_hash_equal_cb, NULL, 1);
136 LY_CHECK_ERR_RET(!ht, LOGMEM(sibling->module->ctx), LY_EMEM);
137
Michal Vasko1e0a80a2020-12-09 18:12:51 +0100138 getnext_opts = 0;
Michal Vaskod1e53b92021-01-28 13:11:06 +0100139 if (sibling->flags & LYS_IS_OUTPUT) {
Michal Vasko1e0a80a2020-12-09 18:12:51 +0100140 getnext_opts = LYS_GETNEXT_OUTPUT;
141 }
142
Michal Vasko60ea6352020-06-29 13:39:39 +0200143 parent = lysc_data_parent(sibling);
144 mod = sibling->module;
145
146 sibling = NULL;
Michal Vasko1e0a80a2020-12-09 18:12:51 +0100147 while ((sibling = (struct lysc_node *)lys_getnext(sibling, parent, mod->compiled, getnext_opts))) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200148 /* find the first non-colliding hash (or specifically non-colliding hash sequence) */
149 for (i = 0; i < LYB_HASH_BITS; ++i) {
150 /* check that we are not colliding with nodes inserted with a lower collision ID than ours */
Radek Krejci1deb5be2020-08-26 16:43:36 +0200151 int64_t j;
152 for (j = (int64_t)i - 1; j > -1; --j) {
153 if (lyb_hash_sequence_check(ht, sibling, (LYB_HASH)j, i)) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200154 break;
155 }
156 }
157 if (j > -1) {
158 /* some check failed, we must use a higher collision ID */
159 continue;
160 }
161
162 /* try to insert node with the current collision ID */
Michal Vasko11f76c82021-04-15 14:36:14 +0200163 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 +0200164 /* success, no collision */
165 break;
166 }
167
168 /* make sure we really cannot insert it with this hash col ID (meaning the whole hash sequence is colliding) */
169 if (i && !lyb_hash_sequence_check(ht, sibling, i, i)) {
170 /* it can be inserted after all, even though there is already a node with the same last collision ID */
171 lyht_set_cb(ht, lyb_ptr_equal_cb);
Michal Vasko11f76c82021-04-15 14:36:14 +0200172 if (lyht_insert(ht, &sibling, lyb_get_hash(sibling, i), NULL)) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200173 LOGINT(sibling->module->ctx);
174 lyht_set_cb(ht, lyb_hash_equal_cb);
175 lyht_free(ht);
176 return LY_EINT;
177 }
178 lyht_set_cb(ht, lyb_hash_equal_cb);
179 break;
180 }
181 /* there is still another colliding schema node with the same hash sequence, try higher collision ID */
182 }
183
184 if (i == LYB_HASH_BITS) {
185 /* wow */
186 LOGINT(sibling->module->ctx);
187 lyht_free(ht);
188 return LY_EINT;
189 }
190 }
191
192 /* change val equal callback so that the HT is usable for finding value hashes */
193 lyht_set_cb(ht, lyb_ptr_equal_cb);
194
195 *ht_p = ht;
196 return LY_SUCCESS;
197}
198
199/**
200 * @brief Find node hash in a hash table.
201 *
202 * @param[in] ht Hash table to search in.
203 * @param[in] node Node to find.
204 * @param[out] hash_p First non-colliding hash found.
205 * @return LY_ERR value.
206 */
207static LY_ERR
208lyb_hash_find(struct hash_table *ht, struct lysc_node *node, LYB_HASH *hash_p)
209{
210 LYB_HASH hash;
211 uint32_t i;
212
213 for (i = 0; i < LYB_HASH_BITS; ++i) {
Michal Vasko11f76c82021-04-15 14:36:14 +0200214 hash = lyb_get_hash(node, i);
Michal Vasko60ea6352020-06-29 13:39:39 +0200215 if (!hash) {
216 LOGINT_RET(node->module->ctx);
217 }
218
219 if (!lyht_find(ht, &node, hash, NULL)) {
220 /* success, no collision */
221 break;
222 }
223 }
224 /* cannot happen, we already calculated the hash */
225 if (i == LYB_HASH_BITS) {
226 LOGINT_RET(node->module->ctx);
227 }
228
229 *hash_p = hash;
230 return LY_SUCCESS;
231}
232
233/**
234 * @brief Write LYB data fully handling the metadata.
235 *
236 * @param[in] out Out structure.
237 * @param[in] buf Source buffer.
238 * @param[in] count Number of bytes to write.
239 * @param[in] lybctx LYB context.
240 * @return LY_ERR value.
241 */
242static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200243lyb_write(struct ly_out *out, const uint8_t *buf, size_t count, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200244{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200245 LY_ARRAY_COUNT_TYPE u;
aPiecek570d7ed2021-09-10 07:15:35 +0200246 struct lyd_lyb_sibling *full, *iter;
Michal Vasko5233e962020-08-14 14:26:20 +0200247 size_t to_write;
Michal Vasko60ea6352020-06-29 13:39:39 +0200248 uint8_t meta_buf[LYB_META_BYTES];
249
250 while (1) {
251 /* check for full data chunks */
252 to_write = count;
253 full = NULL;
aPiecek570d7ed2021-09-10 07:15:35 +0200254 LY_ARRAY_FOR(lybctx->siblings, u) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200255 /* we want the innermost chunks resolved first, so replace previous full chunks */
aPiecek570d7ed2021-09-10 07:15:35 +0200256 if (lybctx->siblings[u].written + to_write >= LYB_SIZE_MAX) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200257 /* full chunk, do not write more than allowed */
aPiecek570d7ed2021-09-10 07:15:35 +0200258 to_write = LYB_SIZE_MAX - lybctx->siblings[u].written;
259 full = &lybctx->siblings[u];
Michal Vasko60ea6352020-06-29 13:39:39 +0200260 }
261 }
262
263 if (!full && !count) {
264 break;
265 }
266
267 /* we are actually writing some data, not just finishing another chunk */
268 if (to_write) {
Michal Vasko5233e962020-08-14 14:26:20 +0200269 LY_CHECK_RET(ly_write_(out, (char *)buf, to_write));
Michal Vasko60ea6352020-06-29 13:39:39 +0200270
aPiecek570d7ed2021-09-10 07:15:35 +0200271 LY_ARRAY_FOR(lybctx->siblings, u) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200272 /* increase all written counters */
aPiecek570d7ed2021-09-10 07:15:35 +0200273 lybctx->siblings[u].written += to_write;
274 assert(lybctx->siblings[u].written <= LYB_SIZE_MAX);
Michal Vasko60ea6352020-06-29 13:39:39 +0200275 }
276 /* decrease count/buf */
Michal Vasko5233e962020-08-14 14:26:20 +0200277 count -= to_write;
278 buf += to_write;
Michal Vasko60ea6352020-06-29 13:39:39 +0200279 }
280
281 if (full) {
282 /* write the meta information (inner chunk count and chunk size) */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100283 meta_buf[0] = full->written & LYB_BYTE_MASK;
284 meta_buf[1] = full->inner_chunks & LYB_BYTE_MASK;
Michal Vasko5233e962020-08-14 14:26:20 +0200285 LY_CHECK_RET(ly_write_skipped(out, full->position, (char *)meta_buf, LYB_META_BYTES));
Michal Vasko60ea6352020-06-29 13:39:39 +0200286
287 /* zero written and inner chunks */
288 full->written = 0;
289 full->inner_chunks = 0;
290
291 /* skip space for another chunk size */
Michal Vasko5233e962020-08-14 14:26:20 +0200292 LY_CHECK_RET(ly_write_skip(out, LYB_META_BYTES, &full->position));
Michal Vasko60ea6352020-06-29 13:39:39 +0200293
294 /* increase inner chunk count */
aPiecek570d7ed2021-09-10 07:15:35 +0200295 for (iter = &lybctx->siblings[0]; iter != full; ++iter) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200296 if (iter->inner_chunks == LYB_INCHUNK_MAX) {
297 LOGINT(lybctx->ctx);
298 return LY_EINT;
299 }
300 ++iter->inner_chunks;
301 }
302 }
303 }
304
305 return LY_SUCCESS;
306}
307
308/**
aPiecek570d7ed2021-09-10 07:15:35 +0200309 * @brief Stop the current "siblings" - write its final metadata.
Michal Vasko60ea6352020-06-29 13:39:39 +0200310 *
311 * @param[in] out Out structure.
312 * @param[in] lybctx LYB context.
313 * @return LY_ERR value.
314 */
315static LY_ERR
aPiecek570d7ed2021-09-10 07:15:35 +0200316lyb_write_stop_siblings(struct ly_out *out, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200317{
Michal Vasko60ea6352020-06-29 13:39:39 +0200318 uint8_t meta_buf[LYB_META_BYTES];
319
320 /* write the meta chunk information */
aPiecek570d7ed2021-09-10 07:15:35 +0200321 meta_buf[0] = LYB_LAST_SIBLING(lybctx).written & LYB_BYTE_MASK;
322 meta_buf[1] = LYB_LAST_SIBLING(lybctx).inner_chunks & LYB_BYTE_MASK;
323 LY_CHECK_RET(ly_write_skipped(out, LYB_LAST_SIBLING(lybctx).position, (char *)&meta_buf, LYB_META_BYTES));
Michal Vasko60ea6352020-06-29 13:39:39 +0200324
aPiecek570d7ed2021-09-10 07:15:35 +0200325 LY_ARRAY_DECREMENT(lybctx->siblings);
Michal Vasko60ea6352020-06-29 13:39:39 +0200326 return LY_SUCCESS;
327}
328
329/**
aPiecek570d7ed2021-09-10 07:15:35 +0200330 * @brief Start a new "siblings" - skip bytes for its metadata.
Michal Vasko60ea6352020-06-29 13:39:39 +0200331 *
332 * @param[in] out Out structure.
333 * @param[in] lybctx LYB context.
334 * @return LY_ERR value.
335 */
336static LY_ERR
aPiecek570d7ed2021-09-10 07:15:35 +0200337lyb_write_start_siblings(struct ly_out *out, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200338{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200339 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +0200340
aPiecek570d7ed2021-09-10 07:15:35 +0200341 u = LY_ARRAY_COUNT(lybctx->siblings);
342 if (u == lybctx->sibling_size) {
343 LY_ARRAY_CREATE_RET(lybctx->ctx, lybctx->siblings, u + LYB_SIBLING_STEP, LY_EMEM);
344 lybctx->sibling_size = u + LYB_SIBLING_STEP;
Michal Vasko60ea6352020-06-29 13:39:39 +0200345 }
346
aPiecek570d7ed2021-09-10 07:15:35 +0200347 LY_ARRAY_INCREMENT(lybctx->siblings);
348 LYB_LAST_SIBLING(lybctx).written = 0;
349 LYB_LAST_SIBLING(lybctx).inner_chunks = 0;
Michal Vasko60ea6352020-06-29 13:39:39 +0200350
351 /* another inner chunk */
aPiecek570d7ed2021-09-10 07:15:35 +0200352 for (u = 0; u < LY_ARRAY_COUNT(lybctx->siblings) - 1; ++u) {
353 if (lybctx->siblings[u].inner_chunks == LYB_INCHUNK_MAX) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200354 LOGINT(lybctx->ctx);
Michal Vasko5233e962020-08-14 14:26:20 +0200355 return LY_EINT;
Michal Vasko60ea6352020-06-29 13:39:39 +0200356 }
aPiecek570d7ed2021-09-10 07:15:35 +0200357 ++lybctx->siblings[u].inner_chunks;
Michal Vasko60ea6352020-06-29 13:39:39 +0200358 }
359
aPiecek570d7ed2021-09-10 07:15:35 +0200360 LY_CHECK_RET(ly_write_skip(out, LYB_META_BYTES, &LYB_LAST_SIBLING(lybctx).position));
Michal Vasko60ea6352020-06-29 13:39:39 +0200361
362 return LY_SUCCESS;
363}
364
365/**
366 * @brief Write a number.
367 *
368 * @param[in] num Number to write.
369 * @param[in] bytes Actual accessible bytes of @p num.
370 * @param[in] out Out structure.
371 * @param[in] lybctx LYB context.
372 * @return LY_ERR value.
373 */
374static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200375lyb_write_number(uint64_t num, size_t bytes, struct ly_out *out, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200376{
377 /* correct byte order */
378 num = htole64(num);
379
380 return lyb_write(out, (uint8_t *)&num, bytes, lybctx);
381}
382
383/**
384 * @brief Write a string.
385 *
386 * @param[in] str String to write.
387 * @param[in] str_len Length of @p str.
388 * @param[in] with_length Whether to precede the string with its length.
389 * @param[in] out Out structure.
390 * @param[in] lybctx LYB context.
391 * @return LY_ERR value.
392 */
393static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200394lyb_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 +0200395{
Michal Vasko60ea6352020-06-29 13:39:39 +0200396 if (!str) {
397 str = "";
Michal Vasko30667352020-08-13 09:06:14 +0200398 LY_CHECK_ERR_RET(str_len, LOGINT(lybctx->ctx), LY_EINT);
Michal Vasko60ea6352020-06-29 13:39:39 +0200399 }
400 if (!str_len) {
401 str_len = strlen(str);
402 }
403
404 if (with_length) {
405 /* print length on 2 bytes */
406 if (str_len > UINT16_MAX) {
407 LOGINT(lybctx->ctx);
408 return LY_EINT;
409 }
410 LY_CHECK_RET(lyb_write_number(str_len, 2, out, lybctx));
411 }
412
Michal Vasko63f3d842020-07-08 10:10:14 +0200413 LY_CHECK_RET(lyb_write(out, (const uint8_t *)str, str_len, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200414
415 return LY_SUCCESS;
416}
417
418/**
419 * @brief Print YANG module info.
420 *
421 * @param[in] out Out structure.
422 * @param[in] mod Module to print.
423 * @param[in] lybctx LYB context.
424 * @return LY_ERR value.
425 */
426static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200427lyb_print_model(struct ly_out *out, const struct lys_module *mod, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200428{
Michal Vasko60ea6352020-06-29 13:39:39 +0200429 uint16_t revision;
430
431 /* model name length and model name */
432 if (mod) {
433 LY_CHECK_RET(lyb_write_string(mod->name, 0, 1, out, lybctx));
434 } else {
aPiecek570d7ed2021-09-10 07:15:35 +0200435 lyb_write_number(0, 2, out, lybctx);
436 return LY_SUCCESS;
Michal Vasko60ea6352020-06-29 13:39:39 +0200437 }
438
439 /* model revision as XXXX XXXX XXXX XXXX (2B) (year is offset from 2000)
440 * YYYY YYYM MMMD DDDD */
441 revision = 0;
442 if (mod && mod->revision) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200443 int r = atoi(mod->revision);
Radek Krejcif13b87b2020-12-01 22:02:17 +0100444 r -= LYB_REV_YEAR_OFFSET;
445 r <<= LYB_REV_YEAR_SHIFT;
Michal Vasko60ea6352020-06-29 13:39:39 +0200446
447 revision |= r;
448
Radek Krejcif13b87b2020-12-01 22:02:17 +0100449 r = atoi(mod->revision + ly_strlen_const("YYYY-"));
450 r <<= LYB_REV_MONTH_SHIFT;
Michal Vasko60ea6352020-06-29 13:39:39 +0200451
452 revision |= r;
453
Radek Krejcif13b87b2020-12-01 22:02:17 +0100454 r = atoi(mod->revision + ly_strlen_const("YYYY-MM-"));
Michal Vasko60ea6352020-06-29 13:39:39 +0200455
456 revision |= r;
457 }
458 LY_CHECK_RET(lyb_write_number(revision, sizeof revision, out, lybctx));
459
Michal Vasko11f76c82021-04-15 14:36:14 +0200460 if (mod) {
461 /* fill cached hashes, if not already */
462 lyb_cache_module_hash(mod);
463 }
464
Michal Vasko60ea6352020-06-29 13:39:39 +0200465 return LY_SUCCESS;
466}
467
468/**
469 * @brief Print all used YANG modules.
470 *
471 * @param[in] out Out structure.
472 * @param[in] root Data root.
473 * @param[in] lybctx LYB context.
474 * @return LY_ERR value.
475 */
476static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200477lyb_print_data_models(struct ly_out *out, const struct lyd_node *root, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200478{
479 struct ly_set *set;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200480 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +0200481 LY_ERR ret = LY_SUCCESS;
482 struct lys_module *mod;
Michal Vaskof4b4d002021-08-23 12:16:02 +0200483 const struct lyd_node *elem, *node;
Michal Vasko60ea6352020-06-29 13:39:39 +0200484 uint32_t i;
485
Radek Krejciba03a5a2020-08-27 14:40:41 +0200486 LY_CHECK_RET(ly_set_new(&set));
Michal Vasko60ea6352020-06-29 13:39:39 +0200487
488 /* collect all data node modules */
Michal Vaskof4b4d002021-08-23 12:16:02 +0200489 LY_LIST_FOR(root, elem) {
490 LYD_TREE_DFS_BEGIN(elem, node) {
491 if (node->schema) {
492 mod = node->schema->module;
493 ret = ly_set_add(set, mod, 0, NULL);
494 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200495
Michal Vaskof4b4d002021-08-23 12:16:02 +0200496 /* add also their modules deviating or augmenting them */
497 LY_ARRAY_FOR(mod->deviated_by, u) {
498 ret = ly_set_add(set, mod->deviated_by[u], 0, NULL);
499 LY_CHECK_GOTO(ret, cleanup);
500 }
501 LY_ARRAY_FOR(mod->augmented_by, u) {
502 ret = ly_set_add(set, mod->augmented_by[u], 0, NULL);
503 LY_CHECK_GOTO(ret, cleanup);
504 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200505
Michal Vaskof4b4d002021-08-23 12:16:02 +0200506 /* only top-level nodes are processed */
507 LYD_TREE_DFS_continue = 1;
508 }
509
510 LYD_TREE_DFS_END(elem, node);
Michal Vasko60ea6352020-06-29 13:39:39 +0200511 }
512 }
513
514 /* now write module count on 2 bytes */
515 LY_CHECK_GOTO(ret = lyb_write_number(set->count, 2, out, lybctx), cleanup);
516
517 /* and all the used models */
518 for (i = 0; i < set->count; ++i) {
519 LY_CHECK_GOTO(ret = lyb_print_model(out, set->objs[i], lybctx), cleanup);
520 }
521
522cleanup:
523 ly_set_free(set, NULL);
524 return ret;
525}
526
527/**
528 * @brief Print LYB magic number.
529 *
530 * @param[in] out Out structure.
Michal Vasko60ea6352020-06-29 13:39:39 +0200531 * @return LY_ERR value.
532 */
533static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200534lyb_print_magic_number(struct ly_out *out)
Michal Vasko60ea6352020-06-29 13:39:39 +0200535{
Michal Vasko60ea6352020-06-29 13:39:39 +0200536 /* 'l', 'y', 'b' - 0x6c7962 */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100537 char magic_number[] = {'l', 'y', 'b'};
Michal Vasko60ea6352020-06-29 13:39:39 +0200538
Radek Krejcidefd4d72020-12-01 12:20:30 +0100539 LY_CHECK_RET(ly_write_(out, magic_number, 3));
Michal Vasko60ea6352020-06-29 13:39:39 +0200540
541 return LY_SUCCESS;
542}
543
544/**
545 * @brief Print LYB header.
546 *
547 * @param[in] out Out structure.
Michal Vasko60ea6352020-06-29 13:39:39 +0200548 * @return LY_ERR value.
549 */
550static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200551lyb_print_header(struct ly_out *out)
Michal Vasko60ea6352020-06-29 13:39:39 +0200552{
Michal Vasko60ea6352020-06-29 13:39:39 +0200553 uint8_t byte = 0;
554
555 /* version, future flags */
556 byte |= LYB_VERSION_NUM;
557
Michal Vasko5233e962020-08-14 14:26:20 +0200558 LY_CHECK_RET(ly_write_(out, (char *)&byte, 1));
Michal Vasko60ea6352020-06-29 13:39:39 +0200559
560 return LY_SUCCESS;
561}
562
563/**
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100564 * @brief Print prefix data.
Michal Vasko60ea6352020-06-29 13:39:39 +0200565 *
566 * @param[in] out Out structure.
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100567 * @param[in] format Value prefix format.
Radek Krejci8df109d2021-04-23 12:19:08 +0200568 * @param[in] prefix_data Format-specific data for resolving any prefixes (see ::ly_resolve_prefix).
Michal Vasko60ea6352020-06-29 13:39:39 +0200569 * @param[in] lybctx LYB context.
570 * @return LY_ERR value.
571 */
572static LY_ERR
Radek Krejci8df109d2021-04-23 12:19:08 +0200573lyb_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 +0200574{
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100575 const struct ly_set *set;
576 const struct lyxml_ns *ns;
577 uint32_t i;
Michal Vasko60ea6352020-06-29 13:39:39 +0200578
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100579 switch (format) {
Radek Krejci8df109d2021-04-23 12:19:08 +0200580 case LY_VALUE_XML:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100581 set = prefix_data;
582 if (!set) {
583 /* no prefix data */
584 i = 0;
585 LY_CHECK_RET(lyb_write(out, (uint8_t *)&i, 1, lybctx));
586 break;
587 }
588 if (set->count > UINT8_MAX) {
589 LOGERR(lybctx->ctx, LY_EINT, "Maximum supported number of prefixes is %u.", UINT8_MAX);
590 return LY_EINT;
591 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200592
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100593 /* write number of prefixes on 1 byte */
Michal Vasko403beac2021-08-24 08:27:52 +0200594 LY_CHECK_RET(lyb_write_number(set->count, 1, out, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200595
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100596 /* write all the prefixes */
597 for (i = 0; i < set->count; ++i) {
598 ns = set->objs[i];
Michal Vasko60ea6352020-06-29 13:39:39 +0200599
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100600 /* prefix */
601 LY_CHECK_RET(lyb_write_string(ns->prefix, 0, 1, out, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200602
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100603 /* namespace */
604 LY_CHECK_RET(lyb_write_string(ns->uri, 0, 1, out, lybctx));
605 }
606 break;
Radek Krejci8df109d2021-04-23 12:19:08 +0200607 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +0200608 case LY_VALUE_LYB:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100609 /* nothing to print */
610 break;
611 default:
612 LOGINT_RET(lybctx->ctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200613 }
614
615 return LY_SUCCESS;
616}
617
618/**
aPiecek60d90af2021-09-09 12:32:38 +0200619 * @brief Print opaque node and its descendants.
Michal Vasko60ea6352020-06-29 13:39:39 +0200620 *
Michal Vasko60ea6352020-06-29 13:39:39 +0200621 * @param[in] out Out structure.
aPiecek60d90af2021-09-09 12:32:38 +0200622 * @param[in] opaq Node to print.
623 * @param[in] lyd_lybctx LYB context.
Michal Vasko60ea6352020-06-29 13:39:39 +0200624 * @return LY_ERR value.
625 */
626static LY_ERR
aPiecek60d90af2021-09-09 12:32:38 +0200627lyb_print_node_opaq(struct ly_out *out, const struct lyd_node_opaq *opaq, struct lyd_lyb_ctx *lyd_lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200628{
aPiecek60d90af2021-09-09 12:32:38 +0200629 struct lylyb_ctx *lybctx = lyd_lybctx->lybctx;
630
aPiecek270f72b2021-09-09 11:39:31 +0200631 /* write attributes */
632 LY_CHECK_RET(lyb_print_attributes(out, opaq, lybctx));
633
634 /* write node flags */
635 LY_CHECK_RET(lyb_write_number(opaq->flags, sizeof opaq->flags, out, lybctx));
636
Michal Vasko60ea6352020-06-29 13:39:39 +0200637 /* prefix */
Michal Vaskoad92b672020-11-12 13:11:31 +0100638 LY_CHECK_RET(lyb_write_string(opaq->name.prefix, 0, 1, out, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200639
Radek Krejci1798aae2020-07-14 13:26:06 +0200640 /* module reference */
Michal Vaskoad92b672020-11-12 13:11:31 +0100641 LY_CHECK_RET(lyb_write_string(opaq->name.module_name, 0, 1, out, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200642
643 /* name */
Michal Vaskoad92b672020-11-12 13:11:31 +0100644 LY_CHECK_RET(lyb_write_string(opaq->name.name, 0, 1, out, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200645
Michal Vaskoffc92bf2021-06-21 08:15:14 +0200646 /* value */
647 LY_CHECK_RET(lyb_write_string(opaq->value, 0, 1, out, lybctx));
648
Michal Vasko60ea6352020-06-29 13:39:39 +0200649 /* format */
650 LY_CHECK_RET(lyb_write_number(opaq->format, 1, out, lybctx));
651
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100652 /* value prefixes */
653 LY_CHECK_RET(lyb_print_prefix_data(out, opaq->format, opaq->val_prefix_data, lybctx));
654
aPiecek60d90af2021-09-09 12:32:38 +0200655 /* recursively write all the descendants */
aPiecek570d7ed2021-09-10 07:15:35 +0200656 LY_CHECK_RET(lyb_print_siblings(out, opaq->child, lyd_lybctx));
aPiecek60d90af2021-09-09 12:32:38 +0200657
Michal Vasko60ea6352020-06-29 13:39:39 +0200658 return LY_SUCCESS;
659}
660
661/**
aPiecek843157f2021-09-09 12:38:59 +0200662 * @brief Print anydata or anyxml node.
Michal Vasko60ea6352020-06-29 13:39:39 +0200663 *
664 * @param[in] anydata Node to print.
665 * @param[in] out Out structure.
aPiecek843157f2021-09-09 12:38:59 +0200666 * @param[in] lyd_lybctx LYB context.
Michal Vasko60ea6352020-06-29 13:39:39 +0200667 * @return LY_ERR value.
668 */
669static LY_ERR
aPiecek843157f2021-09-09 12:38:59 +0200670lyb_print_node_any(struct ly_out *out, struct lyd_node_any *anydata, struct lyd_lyb_ctx *lyd_lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200671{
672 LY_ERR ret = LY_SUCCESS;
673 LYD_ANYDATA_VALUETYPE value_type;
674 int len;
675 char *buf = NULL;
676 const char *str;
677 struct ly_out *out2 = NULL;
aPiecek843157f2021-09-09 12:38:59 +0200678 struct lylyb_ctx *lybctx = lyd_lybctx->lybctx;
Michal Vasko60ea6352020-06-29 13:39:39 +0200679
680 if (anydata->value_type == LYD_ANYDATA_DATATREE) {
681 /* will be printed as a nested LYB data tree */
682 value_type = LYD_ANYDATA_LYB;
683 } else {
684 value_type = anydata->value_type;
685 }
686
aPiecek843157f2021-09-09 12:38:59 +0200687 /* write necessary basic data */
688 LY_CHECK_RET(lyb_print_node_header(out, (struct lyd_node *)anydata, lyd_lybctx));
689
Michal Vasko60ea6352020-06-29 13:39:39 +0200690 /* first byte is type */
Michal Vasko403beac2021-08-24 08:27:52 +0200691 LY_CHECK_GOTO(ret = lyb_write_number(value_type, sizeof value_type, out, lybctx), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200692
693 if (anydata->value_type == LYD_ANYDATA_DATATREE) {
694 /* print LYB data tree to memory */
695 LY_CHECK_GOTO(ret = ly_out_new_memory(&buf, 0, &out2), cleanup);
Radek Krejci7931b192020-06-25 17:05:03 +0200696 LY_CHECK_GOTO(ret = lyb_print_data(out2, anydata->value.tree, LYD_PRINT_WITHSIBLINGS), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200697
698 len = lyd_lyb_data_length(buf);
699 assert(len != -1);
700 str = buf;
701 } else if (anydata->value_type == LYD_ANYDATA_LYB) {
702 len = lyd_lyb_data_length(anydata->value.mem);
703 assert(len != -1);
704 str = anydata->value.mem;
705 } else {
706 len = strlen(anydata->value.str);
707 str = anydata->value.str;
708 }
709
710 /* followed by the content */
aPiecek570d7ed2021-09-10 07:15:35 +0200711 LY_CHECK_GOTO(ret = lyb_write_string(str, (size_t)len, 1, out, lybctx), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200712
713cleanup:
714 ly_out_free(out2, NULL, 1);
715 return ret;
716}
717
718/**
719 * @brief Print term node.
720 *
721 * @param[in] term Node to print.
722 * @param[in] out Out structure.
723 * @param[in] lybctx LYB context.
724 * @return LY_ERR value.
725 */
726static LY_ERR
aPieceka19ca152021-09-09 12:27:35 +0200727lyb_print_term_value(struct lyd_node_term *term, struct ly_out *out, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200728{
aPiecekaa5b70a2021-08-30 08:33:25 +0200729 LY_ERR ret = LY_SUCCESS;
aPiecekea304e32021-08-18 09:13:47 +0200730 ly_bool dynamic = 0;
731 void *value;
732 size_t value_len = 0;
aPiecekaa5b70a2021-08-30 08:33:25 +0200733 int32_t lyb_data_len;
734 lyplg_type_print_clb print;
aPiecekea304e32021-08-18 09:13:47 +0200735
736 assert(term->value.realtype && term->value.realtype->plugin && term->value.realtype->plugin->print &&
737 term->schema);
738
aPiecekaa5b70a2021-08-30 08:33:25 +0200739 /* Get length of LYB data to print. */
740 lyb_data_len = term->value.realtype->plugin->lyb_data_len;
aPiecekea304e32021-08-18 09:13:47 +0200741
aPiecekaa5b70a2021-08-30 08:33:25 +0200742 /* Get value and also print its length only if size is not fixed. */
743 print = term->value.realtype->plugin->print;
744 if (lyb_data_len < 0) {
745 /* Variable-length data. */
746
747 /* Get value and its length from plugin. */
748 value = (void *)print(term->schema->module->ctx, &term->value,
749 LY_VALUE_LYB, NULL, &dynamic, &value_len);
750 LY_CHECK_GOTO(ret, cleanup);
751
752 if (value_len > UINT32_MAX) {
753 LOGERR(lybctx->ctx, LY_EINT, "The maximum length of the LYB data "
754 "from a term node must not exceed %lu.", UINT32_MAX);
755 ret = LY_EINT;
756 goto cleanup;
757 }
758
759 /* Print the length of the data as 32-bit unsigned integer. */
760 ret = lyb_write_number(value_len, sizeof(uint32_t), out, lybctx);
761 LY_CHECK_GOTO(ret, cleanup);
762 } else {
763 /* Fixed-length data. */
764
765 /* Get value from plugin. */
766 value = (void *)print(term->schema->module->ctx, &term->value,
767 LY_VALUE_LYB, NULL, &dynamic, NULL);
768 LY_CHECK_GOTO(ret, cleanup);
769
770 /* Copy the length from the compiled node. */
771 value_len = lyb_data_len;
aPiecekea304e32021-08-18 09:13:47 +0200772 }
773
aPiecekaa5b70a2021-08-30 08:33:25 +0200774 /* Print value. */
aPiecekea304e32021-08-18 09:13:47 +0200775 if (value_len > 0) {
aPiecekaa5b70a2021-08-30 08:33:25 +0200776 /* Print the value simply as it is. */
aPiecekea304e32021-08-18 09:13:47 +0200777 ret = lyb_write(out, value, value_len, lybctx);
778 LY_CHECK_GOTO(ret, cleanup);
779 }
780
781cleanup:
782 if (dynamic) {
783 free(value);
784 }
785
786 return ret;
Michal Vasko60ea6352020-06-29 13:39:39 +0200787}
788
789/**
790 * @brief Print YANG node metadata.
791 *
792 * @param[in] out Out structure.
793 * @param[in] node Data node whose metadata to print.
794 * @param[in] lybctx LYB context.
795 * @return LY_ERR value.
796 */
797static LY_ERR
798lyb_print_metadata(struct ly_out *out, const struct lyd_node *node, struct lyd_lyb_ctx *lybctx)
799{
Michal Vasko60ea6352020-06-29 13:39:39 +0200800 uint8_t count = 0;
801 const struct lys_module *wd_mod = NULL;
802 struct lyd_meta *iter;
Michal Vasko60ea6352020-06-29 13:39:39 +0200803
804 /* with-defaults */
805 if (node->schema->nodetype & LYD_NODE_TERM) {
Radek Krejci7931b192020-06-25 17:05:03 +0200806 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 +0200807 ((lybctx->print_options & LYD_PRINT_WD_ALL_TAG) && lyd_is_default(node))) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200808 /* we have implicit OR explicit default node, print attribute only if context include with-defaults schema */
809 wd_mod = ly_ctx_get_module_latest(node->schema->module->ctx, "ietf-netconf-with-defaults");
810 }
811 }
812
813 /* count metadata */
814 if (wd_mod) {
815 ++count;
816 }
817 for (iter = node->meta; iter; iter = iter->next) {
818 if (count == UINT8_MAX) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200819 LOGERR(lybctx->lybctx->ctx, LY_EINT, "Maximum supported number of data node metadata is %u.", UINT8_MAX);
Michal Vasko60ea6352020-06-29 13:39:39 +0200820 return LY_EINT;
821 }
822 ++count;
823 }
824
825 /* write number of metadata on 1 byte */
Radek Krejci1798aae2020-07-14 13:26:06 +0200826 LY_CHECK_RET(lyb_write(out, &count, 1, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200827
828 if (wd_mod) {
829 /* write the "default" metadata */
aPiecek570d7ed2021-09-10 07:15:35 +0200830 LY_CHECK_RET(lyb_write_start_siblings(out, lybctx->lybctx));
Radek Krejci1798aae2020-07-14 13:26:06 +0200831 LY_CHECK_RET(lyb_print_model(out, wd_mod, lybctx->lybctx));
832 LY_CHECK_RET(lyb_write_string("default", 0, 1, out, lybctx->lybctx));
833 LY_CHECK_RET(lyb_write_string("true", 0, 0, out, lybctx->lybctx));
aPiecek570d7ed2021-09-10 07:15:35 +0200834 LY_CHECK_RET(lyb_write_stop_siblings(out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200835 }
836
837 /* write all the node metadata */
838 LY_LIST_FOR(node->meta, iter) {
aPiecek570d7ed2021-09-10 07:15:35 +0200839 /* each metadata is a sibling */
840 LY_CHECK_RET(lyb_write_start_siblings(out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200841
842 /* model */
Radek Krejci1798aae2020-07-14 13:26:06 +0200843 LY_CHECK_RET(lyb_print_model(out, iter->annotation->module, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200844
845 /* annotation name with length */
Radek Krejci1798aae2020-07-14 13:26:06 +0200846 LY_CHECK_RET(lyb_write_string(iter->name, 0, 1, out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200847
Michal Vasko60ea6352020-06-29 13:39:39 +0200848 /* metadata value */
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200849 LY_CHECK_RET(lyb_write_string(lyd_get_meta_value(iter), 0, 0, out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200850
aPiecek570d7ed2021-09-10 07:15:35 +0200851 /* finish metadata sibling */
852 LY_CHECK_RET(lyb_write_stop_siblings(out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200853 }
854
855 return LY_SUCCESS;
856}
857
858/**
859 * @brief Print opaque node attributes.
860 *
861 * @param[in] out Out structure.
862 * @param[in] node Opaque node whose attributes to print.
863 * @param[in] lybctx LYB context.
864 * @return LY_ERR value.
865 */
866static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200867lyb_print_attributes(struct ly_out *out, const struct lyd_node_opaq *node, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200868{
869 uint8_t count = 0;
Radek Krejci5536d282020-08-04 23:27:44 +0200870 struct lyd_attr *iter;
Michal Vasko60ea6352020-06-29 13:39:39 +0200871
872 for (iter = node->attr; iter; iter = iter->next) {
873 if (count == UINT8_MAX) {
874 LOGERR(lybctx->ctx, LY_EINT, "Maximum supported number of data node attributes is %u.", UINT8_MAX);
875 return LY_EINT;
876 }
877 ++count;
878 }
879
880 /* write number of attributes on 1 byte */
881 LY_CHECK_RET(lyb_write(out, &count, 1, lybctx));
882
883 /* write all the attributes */
884 LY_LIST_FOR(node->attr, iter) {
aPiecek570d7ed2021-09-10 07:15:35 +0200885 /* each attribute is a sibling */
886 LY_CHECK_RET(lyb_write_start_siblings(out, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200887
888 /* prefix */
Michal Vaskoad92b672020-11-12 13:11:31 +0100889 LY_CHECK_RET(lyb_write_string(iter->name.prefix, 0, 1, out, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200890
891 /* namespace */
Michal Vaskoad92b672020-11-12 13:11:31 +0100892 LY_CHECK_RET(lyb_write_string(iter->name.module_name, 0, 1, out, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200893
894 /* name */
Michal Vaskoad92b672020-11-12 13:11:31 +0100895 LY_CHECK_RET(lyb_write_string(iter->name.name, 0, 1, out, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200896
Michal Vasko60ea6352020-06-29 13:39:39 +0200897 /* format */
898 LY_CHECK_RET(lyb_write_number(iter->format, 1, out, lybctx));
899
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100900 /* value prefixes */
901 LY_CHECK_RET(lyb_print_prefix_data(out, iter->format, iter->val_prefix_data, lybctx));
902
Michal Vasko60ea6352020-06-29 13:39:39 +0200903 /* value */
904 LY_CHECK_RET(lyb_write_string(iter->value, 0, 0, out, lybctx));
905
aPiecek570d7ed2021-09-10 07:15:35 +0200906 /* finish attribute sibling */
907 LY_CHECK_RET(lyb_write_stop_siblings(out, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200908 }
909
910 return LY_SUCCESS;
911}
912
913/**
aPiecek270f72b2021-09-09 11:39:31 +0200914 * @brief Print header for non-opaq node.
915 *
916 * @param[in] out Out structure.
917 * @param[in] node Current data node to print.
918 * @param[in] lybctx LYB context.
919 * @return LY_ERR value.
920 */
921static LY_ERR
922lyb_print_node_header(struct ly_out *out, const struct lyd_node *node, struct lyd_lyb_ctx *lybctx)
923{
924 /* write any metadata */
925 LY_CHECK_RET(lyb_print_metadata(out, node, lybctx));
926
927 /* write node flags */
928 LY_CHECK_RET(lyb_write_number(node->flags, sizeof node->flags, out, lybctx->lybctx));
929
930 return LY_SUCCESS;
931}
932
933/**
Michal Vasko60ea6352020-06-29 13:39:39 +0200934 * @brief Print schema node hash.
935 *
936 * @param[in] out Out structure.
937 * @param[in] schema Schema node whose hash to print.
938 * @param[in,out] sibling_ht Cached hash table for these siblings, created if NULL.
939 * @param[in] lybctx LYB context.
940 * @return LY_ERR value.
941 */
942static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200943lyb_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 +0200944{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200945 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +0200946 uint32_t i;
947 LYB_HASH hash;
948 struct lyd_lyb_sib_ht *sib_ht;
949 struct lysc_node *first_sibling;
950
951 if (!schema) {
952 /* opaque node, write empty hash */
953 hash = 0;
954 LY_CHECK_RET(lyb_write(out, &hash, sizeof hash, lybctx));
955 return LY_SUCCESS;
956 }
957
958 /* create whole sibling HT if not already created and saved */
959 if (!*sibling_ht) {
Michal Vasko1e0a80a2020-12-09 18:12:51 +0100960 /* get first schema data sibling */
961 first_sibling = (struct lysc_node *)lys_getnext(NULL, lysc_data_parent(schema), schema->module->compiled,
Michal Vaskod1e53b92021-01-28 13:11:06 +0100962 (schema->flags & LYS_IS_OUTPUT) ? LYS_GETNEXT_OUTPUT : 0);
Michal Vasko60ea6352020-06-29 13:39:39 +0200963 LY_ARRAY_FOR(lybctx->sib_hts, u) {
964 if (lybctx->sib_hts[u].first_sibling == first_sibling) {
965 /* we have already created a hash table for these siblings */
966 *sibling_ht = lybctx->sib_hts[u].ht;
967 break;
968 }
969 }
970
971 if (!*sibling_ht) {
972 /* we must create sibling hash table */
973 LY_CHECK_RET(lyb_hash_siblings(first_sibling, sibling_ht));
974
975 /* and save it */
976 LY_ARRAY_NEW_RET(lybctx->ctx, lybctx->sib_hts, sib_ht, LY_EMEM);
977
978 sib_ht->first_sibling = first_sibling;
979 sib_ht->ht = *sibling_ht;
980 }
981 }
982
983 /* get our hash */
984 LY_CHECK_RET(lyb_hash_find(*sibling_ht, schema, &hash));
985
986 /* write the hash */
987 LY_CHECK_RET(lyb_write(out, &hash, sizeof hash, lybctx));
988
989 if (hash & LYB_HASH_COLLISION_ID) {
990 /* no collision for this hash, we are done */
991 return LY_SUCCESS;
992 }
993
994 /* written hash was a collision, write also all the preceding hashes */
Radek Krejci1e008d22020-08-17 11:37:37 +0200995 for (i = 0; !(hash & (LYB_HASH_COLLISION_ID >> i)); ++i) {}
Michal Vasko60ea6352020-06-29 13:39:39 +0200996
Michal Vaskod989ba02020-08-24 10:59:24 +0200997 for ( ; i; --i) {
Michal Vasko11f76c82021-04-15 14:36:14 +0200998 hash = lyb_get_hash(schema, i - 1);
Michal Vasko60ea6352020-06-29 13:39:39 +0200999 if (!hash) {
1000 return LY_EINT;
1001 }
1002 assert(hash & (LYB_HASH_COLLISION_ID >> (i - 1)));
1003
1004 LY_CHECK_RET(lyb_write(out, &hash, sizeof hash, lybctx));
1005 }
1006
1007 return LY_SUCCESS;
1008}
1009
1010/**
aPiecek5777f122021-09-10 10:26:23 +02001011 * @brief Print inner node.
Michal Vasko60ea6352020-06-29 13:39:39 +02001012 *
1013 * @param[in] out Out structure.
aPiecek5777f122021-09-10 10:26:23 +02001014 * @param[in] node Current data node to print.
Michal Vasko60ea6352020-06-29 13:39:39 +02001015 * @param[in] lybctx LYB context.
1016 * @return LY_ERR value.
1017 */
1018static LY_ERR
aPiecek5777f122021-09-10 10:26:23 +02001019lyb_print_node_inner(struct ly_out *out, const struct lyd_node *node, struct lyd_lyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +02001020{
aPiecek586b1d22021-09-10 09:00:57 +02001021 /* write necessary basic data */
1022 LY_CHECK_RET(lyb_print_node_header(out, node, lybctx));
aPiecek570d7ed2021-09-10 07:15:35 +02001023
aPiecek5777f122021-09-10 10:26:23 +02001024 /* recursively write all the descendants */
1025 LY_CHECK_RET(lyb_print_siblings(out, lyd_child(node), lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +02001026
aPiecek570d7ed2021-09-10 07:15:35 +02001027 return LY_SUCCESS;
1028}
1029
1030/**
aPiecek8a555d72021-09-10 10:15:26 +02001031 * @brief Print leaf node.
1032 *
1033 * @param[in] out Out structure.
1034 * @param[in] node Current data node to print.
1035 * @param[in] lybctx LYB context.
1036 * @return LY_ERR value.
1037 */
1038static LY_ERR
1039lyb_print_node_leaf(struct ly_out *out, const struct lyd_node *node, struct lyd_lyb_ctx *lybctx)
1040{
1041 /* write necessary basic data */
1042 LY_CHECK_RET(lyb_print_node_header(out, node, lybctx));
1043
1044 /* write term value */
1045 LY_CHECK_RET(lyb_print_term_value((struct lyd_node_term *)node, out, lybctx->lybctx));
1046
1047 return LY_SUCCESS;
1048}
1049
1050/**
aPiecek5777f122021-09-10 10:26:23 +02001051 * @brief Print all leaflist nodes which belong to same schema.
1052 *
1053 * @param[in] out Out structure.
1054 * @param[in] node Current data node to print.
1055 * @param[in] lybctx LYB context.
1056 * @param[out] printed_node Last node that was printed by this function.
1057 * @return LY_ERR value.
1058 */
1059static LY_ERR
1060lyb_print_node_leaflist(struct ly_out *out, const struct lyd_node *node, struct lyd_lyb_ctx *lybctx,
1061 const struct lyd_node **printed_node)
1062{
1063 const struct lysc_node *schema;
1064
1065 /* register a new sibling */
1066 LY_CHECK_RET(lyb_write_start_siblings(out, lybctx->lybctx));
1067
1068 schema = node->schema;
1069
1070 /* write all the siblings */
1071 LY_LIST_FOR(node, node) {
1072 if (schema != node->schema) {
1073 /* all leaflist nodes was printed */
1074 break;
1075 }
1076
1077 /* write leaf data */
1078 LY_CHECK_RET(lyb_print_node_leaf(out, node, lybctx));
1079 *printed_node = node;
1080 }
1081
1082 /* finish this sibling */
1083 LY_CHECK_RET(lyb_write_stop_siblings(out, lybctx->lybctx));
1084
1085 return LY_SUCCESS;
1086}
1087
1088/**
aPiecek570d7ed2021-09-10 07:15:35 +02001089 * @brief Print siblings.
1090 *
1091 * @param[in] out Out structure.
1092 * @param[in] node Current data node to print.
1093 * @param[in] lybctx LYB context.
1094 * @return LY_ERR value.
1095 */
1096static LY_ERR
1097lyb_print_siblings(struct ly_out *out, const struct lyd_node *node, struct lyd_lyb_ctx *lybctx)
1098{
1099 struct hash_table *sibling_ht = NULL;
1100 const struct lys_module *prev_mod = NULL;
1101 ly_bool top_level;
1102 uint8_t zero = 0;
1103
1104 if (!node) {
1105 lyb_write(out, &zero, 1, lybctx->lybctx);
1106 return LY_SUCCESS;
Michal Vasko60ea6352020-06-29 13:39:39 +02001107 }
1108
aPiecek570d7ed2021-09-10 07:15:35 +02001109 top_level = !LY_ARRAY_COUNT(lybctx->lybctx->siblings);
1110
1111 LY_CHECK_RET(lyb_write_start_siblings(out, lybctx->lybctx));
1112
1113 /* write all the siblings */
1114 LY_LIST_FOR(node, node) {
1115
1116 /* do not reuse sibling hash tables from different modules */
1117 if (top_level && (!node->schema || (node->schema->module != prev_mod))) {
1118 sibling_ht = NULL;
1119 prev_mod = node->schema ? node->schema->module : NULL;
1120 }
1121
1122 /* write model info first, for all opaque and top-level nodes */
1123 if (!node->schema && (!node->parent || !node->parent->schema)) {
1124 LY_CHECK_RET(lyb_print_model(out, NULL, lybctx->lybctx));
1125 } else if (node->schema && !lysc_data_parent(node->schema)) {
1126 LY_CHECK_RET(lyb_print_model(out, node->schema->module, lybctx->lybctx));
1127 }
1128
1129 /* write schema hash */
1130 LY_CHECK_RET(lyb_print_schema_hash(out, (struct lysc_node *)node->schema, &sibling_ht, lybctx->lybctx));
1131
aPiecek586b1d22021-09-10 09:00:57 +02001132 if (!node->schema) {
1133 LY_CHECK_RET(lyb_print_node_opaq(out, (struct lyd_node_opaq *)node, lybctx));
aPiecek5777f122021-09-10 10:26:23 +02001134 } else if (node->schema->nodetype & LYS_LEAFLIST) {
1135 LY_CHECK_RET(lyb_print_node_leaflist(out, node, lybctx, &node));
aPiecek586b1d22021-09-10 09:00:57 +02001136 } else if (node->schema->nodetype & LYD_NODE_ANY) {
1137 LY_CHECK_RET(lyb_print_node_any(out, (struct lyd_node_any *)node, lybctx));
aPiecek5777f122021-09-10 10:26:23 +02001138 } else if (node->schema->nodetype & LYD_NODE_INNER) {
1139 LY_CHECK_RET(lyb_print_node_inner(out, node, lybctx));
aPiecek586b1d22021-09-10 09:00:57 +02001140 } else {
aPiecek5777f122021-09-10 10:26:23 +02001141 LY_CHECK_RET(lyb_print_node_leaf(out, node, lybctx));
aPiecek586b1d22021-09-10 09:00:57 +02001142 }
aPiecek570d7ed2021-09-10 07:15:35 +02001143
1144 if (top_level && !(lybctx->print_options & LYD_PRINT_WITHSIBLINGS)) {
1145 break;
1146 }
1147 }
1148
1149 LY_CHECK_RET(lyb_write_stop_siblings(out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +02001150
1151 return LY_SUCCESS;
1152}
1153
1154LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001155lyb_print_data(struct ly_out *out, const struct lyd_node *root, uint32_t options)
Michal Vasko60ea6352020-06-29 13:39:39 +02001156{
1157 LY_ERR ret = LY_SUCCESS;
1158 uint8_t zero = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +02001159 struct lyd_lyb_ctx *lybctx;
Michal Vaskob7be7a82020-08-20 09:09:04 +02001160 const struct ly_ctx *ctx = root ? LYD_CTX(root) : NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +02001161
Radek Krejci1798aae2020-07-14 13:26:06 +02001162 lybctx = calloc(1, sizeof *lybctx);
1163 LY_CHECK_ERR_RET(!lybctx, LOGMEM(ctx), LY_EMEM);
1164 lybctx->lybctx = calloc(1, sizeof *lybctx->lybctx);
1165 LY_CHECK_ERR_RET(!lybctx, LOGMEM(ctx), LY_EMEM);
1166
1167 lybctx->print_options = options;
Michal Vasko60ea6352020-06-29 13:39:39 +02001168 if (root) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001169 lybctx->lybctx->ctx = ctx;
Michal Vasko60ea6352020-06-29 13:39:39 +02001170
1171 if (root->schema && lysc_data_parent(root->schema)) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001172 LOGERR(lybctx->lybctx->ctx, LY_EINVAL, "LYB printer supports only printing top-level nodes.");
Michal Vasko9acaf492020-08-13 09:05:58 +02001173 ret = LY_EINVAL;
1174 goto cleanup;
Michal Vasko60ea6352020-06-29 13:39:39 +02001175 }
1176 }
1177
1178 /* LYB magic number */
Michal Vasko63f3d842020-07-08 10:10:14 +02001179 LY_CHECK_GOTO(ret = lyb_print_magic_number(out), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001180
1181 /* LYB header */
Michal Vasko63f3d842020-07-08 10:10:14 +02001182 LY_CHECK_GOTO(ret = lyb_print_header(out), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001183
1184 /* all used models */
Radek Krejci1798aae2020-07-14 13:26:06 +02001185 LY_CHECK_GOTO(ret = lyb_print_data_models(out, root, lybctx->lybctx), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001186
aPiecek570d7ed2021-09-10 07:15:35 +02001187 ret = lyb_print_siblings(out, root, lybctx);
1188 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001189
1190 /* ending zero byte */
Radek Krejci1798aae2020-07-14 13:26:06 +02001191 LY_CHECK_GOTO(ret = lyb_write(out, &zero, sizeof zero, lybctx->lybctx), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001192
1193cleanup:
Radek Krejci1798aae2020-07-14 13:26:06 +02001194 lyd_lyb_ctx_free((struct lyd_ctx *)lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001195 return ret;
1196}