blob: fd7c3da45c31e53c59218ce32080b984c593b8f9 [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);
aPiecek60d90af2021-09-09 12:32:38 +020043static LY_ERR lyb_print_subtree(struct ly_out *out, const struct lyd_node *node, struct hash_table **sibling_ht, struct lyd_lyb_ctx *lybctx);
aPiecek270f72b2021-09-09 11:39:31 +020044
Michal Vasko60ea6352020-06-29 13:39:39 +020045/**
46 * @brief Hash table equal callback for checking hash equality only.
Radek Krejci857189e2020-09-01 13:26:36 +020047 *
Michal Vasko62524a92021-02-26 10:08:50 +010048 * Implementation of ::lyht_value_equal_cb.
Michal Vasko60ea6352020-06-29 13:39:39 +020049 */
Radek Krejci857189e2020-09-01 13:26:36 +020050static ly_bool
51lyb_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 +020052{
53 /* for this purpose, if hash matches, the value does also, we do not want 2 values to have the same hash */
54 return 1;
55}
56
57/**
58 * @brief Hash table equal callback for checking value pointer equality only.
Radek Krejci857189e2020-09-01 13:26:36 +020059 *
Michal Vasko62524a92021-02-26 10:08:50 +010060 * Implementation of ::lyht_value_equal_cb.
Michal Vasko60ea6352020-06-29 13:39:39 +020061 */
Radek Krejci857189e2020-09-01 13:26:36 +020062static ly_bool
63lyb_ptr_equal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
Michal Vasko60ea6352020-06-29 13:39:39 +020064{
65 struct lysc_node *val1 = *(struct lysc_node **)val1_p;
66 struct lysc_node *val2 = *(struct lysc_node **)val2_p;
67
68 if (val1 == val2) {
69 return 1;
70 }
71 return 0;
72}
73
74/**
75 * @brief Check that sibling collision hash is safe to insert into hash table.
76 *
77 * @param[in] ht Hash table.
78 * @param[in] sibling Hashed sibling.
79 * @param[in] ht_col_id Sibling hash collision ID.
80 * @param[in] compare_col_id Last collision ID to compare with.
81 * @return LY_SUCCESS when the whole hash sequence does not collide,
82 * @return LY_EEXIST when the whole hash sequence sollides.
83 */
84static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +020085lyb_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 +020086{
Michal Vasko60ea6352020-06-29 13:39:39 +020087 struct lysc_node **col_node;
88
89 /* get the first node inserted with last hash col ID ht_col_id */
Michal Vasko11f76c82021-04-15 14:36:14 +020090 if (lyht_find(ht, &sibling, lyb_get_hash(sibling, ht_col_id), (void **)&col_node)) {
Michal Vasko60ea6352020-06-29 13:39:39 +020091 /* there is none. valid situation */
92 return LY_SUCCESS;
93 }
94
95 lyht_set_cb(ht, lyb_ptr_equal_cb);
96 do {
Radek Krejci1deb5be2020-08-26 16:43:36 +020097 int64_t j;
98 for (j = (int64_t)compare_col_id; j > -1; --j) {
Michal Vasko11f76c82021-04-15 14:36:14 +020099 if (lyb_get_hash(sibling, j) != lyb_get_hash(*col_node, j)) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200100 /* one non-colliding hash */
101 break;
102 }
103 }
104 if (j == -1) {
105 /* all whole hash sequences of nodes inserted with last hash col ID compare_col_id collide */
106 lyht_set_cb(ht, lyb_hash_equal_cb);
107 return LY_EEXIST;
108 }
109
110 /* get next node inserted with last hash col ID ht_col_id */
Michal Vasko11f76c82021-04-15 14:36:14 +0200111 } 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 +0200112
113 lyht_set_cb(ht, lyb_hash_equal_cb);
114 return LY_SUCCESS;
115}
116
117/**
118 * @brief Hash all the siblings and add them also into a separate hash table.
119 *
120 * @param[in] sibling Any sibling in all the siblings on one level.
121 * @param[out] ht_p Created hash table.
122 * @return LY_ERR value.
123 */
124static LY_ERR
125lyb_hash_siblings(struct lysc_node *sibling, struct hash_table **ht_p)
126{
127 struct hash_table *ht;
128 const struct lysc_node *parent;
129 const struct lys_module *mod;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200130 LYB_HASH i;
Michal Vasko1e0a80a2020-12-09 18:12:51 +0100131 uint32_t getnext_opts;
Michal Vasko60ea6352020-06-29 13:39:39 +0200132
133 ht = lyht_new(1, sizeof(struct lysc_node *), lyb_hash_equal_cb, NULL, 1);
134 LY_CHECK_ERR_RET(!ht, LOGMEM(sibling->module->ctx), LY_EMEM);
135
Michal Vasko1e0a80a2020-12-09 18:12:51 +0100136 getnext_opts = 0;
Michal Vaskod1e53b92021-01-28 13:11:06 +0100137 if (sibling->flags & LYS_IS_OUTPUT) {
Michal Vasko1e0a80a2020-12-09 18:12:51 +0100138 getnext_opts = LYS_GETNEXT_OUTPUT;
139 }
140
Michal Vasko60ea6352020-06-29 13:39:39 +0200141 parent = lysc_data_parent(sibling);
142 mod = sibling->module;
143
144 sibling = NULL;
Michal Vasko1e0a80a2020-12-09 18:12:51 +0100145 while ((sibling = (struct lysc_node *)lys_getnext(sibling, parent, mod->compiled, getnext_opts))) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200146 /* find the first non-colliding hash (or specifically non-colliding hash sequence) */
147 for (i = 0; i < LYB_HASH_BITS; ++i) {
148 /* check that we are not colliding with nodes inserted with a lower collision ID than ours */
Radek Krejci1deb5be2020-08-26 16:43:36 +0200149 int64_t j;
150 for (j = (int64_t)i - 1; j > -1; --j) {
151 if (lyb_hash_sequence_check(ht, sibling, (LYB_HASH)j, i)) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200152 break;
153 }
154 }
155 if (j > -1) {
156 /* some check failed, we must use a higher collision ID */
157 continue;
158 }
159
160 /* try to insert node with the current collision ID */
Michal Vasko11f76c82021-04-15 14:36:14 +0200161 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 +0200162 /* success, no collision */
163 break;
164 }
165
166 /* make sure we really cannot insert it with this hash col ID (meaning the whole hash sequence is colliding) */
167 if (i && !lyb_hash_sequence_check(ht, sibling, i, i)) {
168 /* it can be inserted after all, even though there is already a node with the same last collision ID */
169 lyht_set_cb(ht, lyb_ptr_equal_cb);
Michal Vasko11f76c82021-04-15 14:36:14 +0200170 if (lyht_insert(ht, &sibling, lyb_get_hash(sibling, i), NULL)) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200171 LOGINT(sibling->module->ctx);
172 lyht_set_cb(ht, lyb_hash_equal_cb);
173 lyht_free(ht);
174 return LY_EINT;
175 }
176 lyht_set_cb(ht, lyb_hash_equal_cb);
177 break;
178 }
179 /* there is still another colliding schema node with the same hash sequence, try higher collision ID */
180 }
181
182 if (i == LYB_HASH_BITS) {
183 /* wow */
184 LOGINT(sibling->module->ctx);
185 lyht_free(ht);
186 return LY_EINT;
187 }
188 }
189
190 /* change val equal callback so that the HT is usable for finding value hashes */
191 lyht_set_cb(ht, lyb_ptr_equal_cb);
192
193 *ht_p = ht;
194 return LY_SUCCESS;
195}
196
197/**
198 * @brief Find node hash in a hash table.
199 *
200 * @param[in] ht Hash table to search in.
201 * @param[in] node Node to find.
202 * @param[out] hash_p First non-colliding hash found.
203 * @return LY_ERR value.
204 */
205static LY_ERR
206lyb_hash_find(struct hash_table *ht, struct lysc_node *node, LYB_HASH *hash_p)
207{
208 LYB_HASH hash;
209 uint32_t i;
210
211 for (i = 0; i < LYB_HASH_BITS; ++i) {
Michal Vasko11f76c82021-04-15 14:36:14 +0200212 hash = lyb_get_hash(node, i);
Michal Vasko60ea6352020-06-29 13:39:39 +0200213 if (!hash) {
214 LOGINT_RET(node->module->ctx);
215 }
216
217 if (!lyht_find(ht, &node, hash, NULL)) {
218 /* success, no collision */
219 break;
220 }
221 }
222 /* cannot happen, we already calculated the hash */
223 if (i == LYB_HASH_BITS) {
224 LOGINT_RET(node->module->ctx);
225 }
226
227 *hash_p = hash;
228 return LY_SUCCESS;
229}
230
231/**
232 * @brief Write LYB data fully handling the metadata.
233 *
234 * @param[in] out Out structure.
235 * @param[in] buf Source buffer.
236 * @param[in] count Number of bytes to write.
237 * @param[in] lybctx LYB context.
238 * @return LY_ERR value.
239 */
240static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200241lyb_write(struct ly_out *out, const uint8_t *buf, size_t count, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200242{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200243 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +0200244 struct lyd_lyb_subtree *full, *iter;
Michal Vasko5233e962020-08-14 14:26:20 +0200245 size_t to_write;
Michal Vasko60ea6352020-06-29 13:39:39 +0200246 uint8_t meta_buf[LYB_META_BYTES];
247
248 while (1) {
249 /* check for full data chunks */
250 to_write = count;
251 full = NULL;
252 LY_ARRAY_FOR(lybctx->subtrees, u) {
253 /* we want the innermost chunks resolved first, so replace previous full chunks */
254 if (lybctx->subtrees[u].written + to_write >= LYB_SIZE_MAX) {
255 /* full chunk, do not write more than allowed */
256 to_write = LYB_SIZE_MAX - lybctx->subtrees[u].written;
257 full = &lybctx->subtrees[u];
258 }
259 }
260
261 if (!full && !count) {
262 break;
263 }
264
265 /* we are actually writing some data, not just finishing another chunk */
266 if (to_write) {
Michal Vasko5233e962020-08-14 14:26:20 +0200267 LY_CHECK_RET(ly_write_(out, (char *)buf, to_write));
Michal Vasko60ea6352020-06-29 13:39:39 +0200268
269 LY_ARRAY_FOR(lybctx->subtrees, u) {
270 /* increase all written counters */
Michal Vasko5233e962020-08-14 14:26:20 +0200271 lybctx->subtrees[u].written += to_write;
Michal Vasko60ea6352020-06-29 13:39:39 +0200272 assert(lybctx->subtrees[u].written <= LYB_SIZE_MAX);
273 }
274 /* decrease count/buf */
Michal Vasko5233e962020-08-14 14:26:20 +0200275 count -= to_write;
276 buf += to_write;
Michal Vasko60ea6352020-06-29 13:39:39 +0200277 }
278
279 if (full) {
280 /* write the meta information (inner chunk count and chunk size) */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100281 meta_buf[0] = full->written & LYB_BYTE_MASK;
282 meta_buf[1] = full->inner_chunks & LYB_BYTE_MASK;
Michal Vasko5233e962020-08-14 14:26:20 +0200283 LY_CHECK_RET(ly_write_skipped(out, full->position, (char *)meta_buf, LYB_META_BYTES));
Michal Vasko60ea6352020-06-29 13:39:39 +0200284
285 /* zero written and inner chunks */
286 full->written = 0;
287 full->inner_chunks = 0;
288
289 /* skip space for another chunk size */
Michal Vasko5233e962020-08-14 14:26:20 +0200290 LY_CHECK_RET(ly_write_skip(out, LYB_META_BYTES, &full->position));
Michal Vasko60ea6352020-06-29 13:39:39 +0200291
292 /* increase inner chunk count */
293 for (iter = &lybctx->subtrees[0]; iter != full; ++iter) {
294 if (iter->inner_chunks == LYB_INCHUNK_MAX) {
295 LOGINT(lybctx->ctx);
296 return LY_EINT;
297 }
298 ++iter->inner_chunks;
299 }
300 }
301 }
302
303 return LY_SUCCESS;
304}
305
306/**
307 * @brief Stop the current subtree - write its final metadata.
308 *
309 * @param[in] out Out structure.
310 * @param[in] lybctx LYB context.
311 * @return LY_ERR value.
312 */
313static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200314lyb_write_stop_subtree(struct ly_out *out, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200315{
Michal Vasko60ea6352020-06-29 13:39:39 +0200316 uint8_t meta_buf[LYB_META_BYTES];
317
318 /* write the meta chunk information */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100319 meta_buf[0] = LYB_LAST_SUBTREE(lybctx).written & LYB_BYTE_MASK;
320 meta_buf[1] = LYB_LAST_SUBTREE(lybctx).inner_chunks & LYB_BYTE_MASK;
Michal Vasko5233e962020-08-14 14:26:20 +0200321 LY_CHECK_RET(ly_write_skipped(out, LYB_LAST_SUBTREE(lybctx).position, (char *)&meta_buf, LYB_META_BYTES));
Michal Vasko60ea6352020-06-29 13:39:39 +0200322
323 LY_ARRAY_DECREMENT(lybctx->subtrees);
324 return LY_SUCCESS;
325}
326
327/**
328 * @brief Start a new subtree - skip bytes for its metadata.
329 *
330 * @param[in] out Out structure.
331 * @param[in] lybctx LYB context.
332 * @return LY_ERR value.
333 */
334static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200335lyb_write_start_subtree(struct ly_out *out, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200336{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200337 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +0200338
Radek Krejcic7d13e32020-12-09 12:32:24 +0100339 u = LY_ARRAY_COUNT(lybctx->subtrees);
Michal Vasko60ea6352020-06-29 13:39:39 +0200340 if (u == lybctx->subtree_size) {
341 LY_ARRAY_CREATE_RET(lybctx->ctx, lybctx->subtrees, u + LYB_SUBTREE_STEP, LY_EMEM);
342 lybctx->subtree_size = u + LYB_SUBTREE_STEP;
343 }
344
345 LY_ARRAY_INCREMENT(lybctx->subtrees);
346 LYB_LAST_SUBTREE(lybctx).written = 0;
347 LYB_LAST_SUBTREE(lybctx).inner_chunks = 0;
348
349 /* another inner chunk */
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200350 for (u = 0; u < LY_ARRAY_COUNT(lybctx->subtrees) - 1; ++u) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200351 if (lybctx->subtrees[u].inner_chunks == LYB_INCHUNK_MAX) {
352 LOGINT(lybctx->ctx);
Michal Vasko5233e962020-08-14 14:26:20 +0200353 return LY_EINT;
Michal Vasko60ea6352020-06-29 13:39:39 +0200354 }
355 ++lybctx->subtrees[u].inner_chunks;
356 }
357
Michal Vasko5233e962020-08-14 14:26:20 +0200358 LY_CHECK_RET(ly_write_skip(out, LYB_META_BYTES, &LYB_LAST_SUBTREE(lybctx).position));
Michal Vasko60ea6352020-06-29 13:39:39 +0200359
360 return LY_SUCCESS;
361}
362
363/**
364 * @brief Write a number.
365 *
366 * @param[in] num Number to write.
367 * @param[in] bytes Actual accessible bytes of @p num.
368 * @param[in] out Out structure.
369 * @param[in] lybctx LYB context.
370 * @return LY_ERR value.
371 */
372static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200373lyb_write_number(uint64_t num, size_t bytes, struct ly_out *out, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200374{
375 /* correct byte order */
376 num = htole64(num);
377
378 return lyb_write(out, (uint8_t *)&num, bytes, lybctx);
379}
380
381/**
382 * @brief Write a string.
383 *
384 * @param[in] str String to write.
385 * @param[in] str_len Length of @p str.
386 * @param[in] with_length Whether to precede the string with its length.
387 * @param[in] out Out structure.
388 * @param[in] lybctx LYB context.
389 * @return LY_ERR value.
390 */
391static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200392lyb_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 +0200393{
Michal Vasko60ea6352020-06-29 13:39:39 +0200394 if (!str) {
395 str = "";
Michal Vasko30667352020-08-13 09:06:14 +0200396 LY_CHECK_ERR_RET(str_len, LOGINT(lybctx->ctx), LY_EINT);
Michal Vasko60ea6352020-06-29 13:39:39 +0200397 }
398 if (!str_len) {
399 str_len = strlen(str);
400 }
401
402 if (with_length) {
403 /* print length on 2 bytes */
404 if (str_len > UINT16_MAX) {
405 LOGINT(lybctx->ctx);
406 return LY_EINT;
407 }
408 LY_CHECK_RET(lyb_write_number(str_len, 2, out, lybctx));
409 }
410
Michal Vasko63f3d842020-07-08 10:10:14 +0200411 LY_CHECK_RET(lyb_write(out, (const uint8_t *)str, str_len, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200412
413 return LY_SUCCESS;
414}
415
416/**
417 * @brief Print YANG module info.
418 *
419 * @param[in] out Out structure.
420 * @param[in] mod Module to print.
421 * @param[in] lybctx LYB context.
422 * @return LY_ERR value.
423 */
424static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200425lyb_print_model(struct ly_out *out, const struct lys_module *mod, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200426{
Michal Vasko60ea6352020-06-29 13:39:39 +0200427 uint16_t revision;
428
429 /* model name length and model name */
430 if (mod) {
431 LY_CHECK_RET(lyb_write_string(mod->name, 0, 1, out, lybctx));
432 } else {
433 LY_CHECK_RET(lyb_write_string("", 0, 1, out, lybctx));
434 }
435
436 /* model revision as XXXX XXXX XXXX XXXX (2B) (year is offset from 2000)
437 * YYYY YYYM MMMD DDDD */
438 revision = 0;
439 if (mod && mod->revision) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200440 int r = atoi(mod->revision);
Radek Krejcif13b87b2020-12-01 22:02:17 +0100441 r -= LYB_REV_YEAR_OFFSET;
442 r <<= LYB_REV_YEAR_SHIFT;
Michal Vasko60ea6352020-06-29 13:39:39 +0200443
444 revision |= r;
445
Radek Krejcif13b87b2020-12-01 22:02:17 +0100446 r = atoi(mod->revision + ly_strlen_const("YYYY-"));
447 r <<= LYB_REV_MONTH_SHIFT;
Michal Vasko60ea6352020-06-29 13:39:39 +0200448
449 revision |= r;
450
Radek Krejcif13b87b2020-12-01 22:02:17 +0100451 r = atoi(mod->revision + ly_strlen_const("YYYY-MM-"));
Michal Vasko60ea6352020-06-29 13:39:39 +0200452
453 revision |= r;
454 }
455 LY_CHECK_RET(lyb_write_number(revision, sizeof revision, out, lybctx));
456
Michal Vasko11f76c82021-04-15 14:36:14 +0200457 if (mod) {
458 /* fill cached hashes, if not already */
459 lyb_cache_module_hash(mod);
460 }
461
Michal Vasko60ea6352020-06-29 13:39:39 +0200462 return LY_SUCCESS;
463}
464
465/**
466 * @brief Print all used YANG modules.
467 *
468 * @param[in] out Out structure.
469 * @param[in] root Data root.
470 * @param[in] lybctx LYB context.
471 * @return LY_ERR value.
472 */
473static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200474lyb_print_data_models(struct ly_out *out, const struct lyd_node *root, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200475{
476 struct ly_set *set;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200477 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +0200478 LY_ERR ret = LY_SUCCESS;
479 struct lys_module *mod;
Michal Vaskof4b4d002021-08-23 12:16:02 +0200480 const struct lyd_node *elem, *node;
Michal Vasko60ea6352020-06-29 13:39:39 +0200481 uint32_t i;
482
Radek Krejciba03a5a2020-08-27 14:40:41 +0200483 LY_CHECK_RET(ly_set_new(&set));
Michal Vasko60ea6352020-06-29 13:39:39 +0200484
485 /* collect all data node modules */
Michal Vaskof4b4d002021-08-23 12:16:02 +0200486 LY_LIST_FOR(root, elem) {
487 LYD_TREE_DFS_BEGIN(elem, node) {
488 if (node->schema) {
489 mod = node->schema->module;
490 ret = ly_set_add(set, mod, 0, NULL);
491 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200492
Michal Vaskof4b4d002021-08-23 12:16:02 +0200493 /* add also their modules deviating or augmenting them */
494 LY_ARRAY_FOR(mod->deviated_by, u) {
495 ret = ly_set_add(set, mod->deviated_by[u], 0, NULL);
496 LY_CHECK_GOTO(ret, cleanup);
497 }
498 LY_ARRAY_FOR(mod->augmented_by, u) {
499 ret = ly_set_add(set, mod->augmented_by[u], 0, NULL);
500 LY_CHECK_GOTO(ret, cleanup);
501 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200502
Michal Vaskof4b4d002021-08-23 12:16:02 +0200503 /* only top-level nodes are processed */
504 LYD_TREE_DFS_continue = 1;
505 }
506
507 LYD_TREE_DFS_END(elem, node);
Michal Vasko60ea6352020-06-29 13:39:39 +0200508 }
509 }
510
511 /* now write module count on 2 bytes */
512 LY_CHECK_GOTO(ret = lyb_write_number(set->count, 2, out, lybctx), cleanup);
513
514 /* and all the used models */
515 for (i = 0; i < set->count; ++i) {
516 LY_CHECK_GOTO(ret = lyb_print_model(out, set->objs[i], lybctx), cleanup);
517 }
518
519cleanup:
520 ly_set_free(set, NULL);
521 return ret;
522}
523
524/**
525 * @brief Print LYB magic number.
526 *
527 * @param[in] out Out structure.
Michal Vasko60ea6352020-06-29 13:39:39 +0200528 * @return LY_ERR value.
529 */
530static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200531lyb_print_magic_number(struct ly_out *out)
Michal Vasko60ea6352020-06-29 13:39:39 +0200532{
Michal Vasko60ea6352020-06-29 13:39:39 +0200533 /* 'l', 'y', 'b' - 0x6c7962 */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100534 char magic_number[] = {'l', 'y', 'b'};
Michal Vasko60ea6352020-06-29 13:39:39 +0200535
Radek Krejcidefd4d72020-12-01 12:20:30 +0100536 LY_CHECK_RET(ly_write_(out, magic_number, 3));
Michal Vasko60ea6352020-06-29 13:39:39 +0200537
538 return LY_SUCCESS;
539}
540
541/**
542 * @brief Print LYB header.
543 *
544 * @param[in] out Out structure.
Michal Vasko60ea6352020-06-29 13:39:39 +0200545 * @return LY_ERR value.
546 */
547static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200548lyb_print_header(struct ly_out *out)
Michal Vasko60ea6352020-06-29 13:39:39 +0200549{
Michal Vasko60ea6352020-06-29 13:39:39 +0200550 uint8_t byte = 0;
551
552 /* version, future flags */
553 byte |= LYB_VERSION_NUM;
554
Michal Vasko5233e962020-08-14 14:26:20 +0200555 LY_CHECK_RET(ly_write_(out, (char *)&byte, 1));
Michal Vasko60ea6352020-06-29 13:39:39 +0200556
557 return LY_SUCCESS;
558}
559
560/**
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100561 * @brief Print prefix data.
Michal Vasko60ea6352020-06-29 13:39:39 +0200562 *
563 * @param[in] out Out structure.
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100564 * @param[in] format Value prefix format.
Radek Krejci8df109d2021-04-23 12:19:08 +0200565 * @param[in] prefix_data Format-specific data for resolving any prefixes (see ::ly_resolve_prefix).
Michal Vasko60ea6352020-06-29 13:39:39 +0200566 * @param[in] lybctx LYB context.
567 * @return LY_ERR value.
568 */
569static LY_ERR
Radek Krejci8df109d2021-04-23 12:19:08 +0200570lyb_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 +0200571{
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100572 const struct ly_set *set;
573 const struct lyxml_ns *ns;
574 uint32_t i;
Michal Vasko60ea6352020-06-29 13:39:39 +0200575
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100576 switch (format) {
Radek Krejci8df109d2021-04-23 12:19:08 +0200577 case LY_VALUE_XML:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100578 set = prefix_data;
579 if (!set) {
580 /* no prefix data */
581 i = 0;
582 LY_CHECK_RET(lyb_write(out, (uint8_t *)&i, 1, lybctx));
583 break;
584 }
585 if (set->count > UINT8_MAX) {
586 LOGERR(lybctx->ctx, LY_EINT, "Maximum supported number of prefixes is %u.", UINT8_MAX);
587 return LY_EINT;
588 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200589
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100590 /* write number of prefixes on 1 byte */
Michal Vasko403beac2021-08-24 08:27:52 +0200591 LY_CHECK_RET(lyb_write_number(set->count, 1, out, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200592
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100593 /* write all the prefixes */
594 for (i = 0; i < set->count; ++i) {
595 ns = set->objs[i];
Michal Vasko60ea6352020-06-29 13:39:39 +0200596
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100597 /* prefix */
598 LY_CHECK_RET(lyb_write_string(ns->prefix, 0, 1, out, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200599
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100600 /* namespace */
601 LY_CHECK_RET(lyb_write_string(ns->uri, 0, 1, out, lybctx));
602 }
603 break;
Radek Krejci8df109d2021-04-23 12:19:08 +0200604 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +0200605 case LY_VALUE_LYB:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100606 /* nothing to print */
607 break;
608 default:
609 LOGINT_RET(lybctx->ctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200610 }
611
612 return LY_SUCCESS;
613}
614
615/**
aPiecek60d90af2021-09-09 12:32:38 +0200616 * @brief Print opaque node and its descendants.
Michal Vasko60ea6352020-06-29 13:39:39 +0200617 *
Michal Vasko60ea6352020-06-29 13:39:39 +0200618 * @param[in] out Out structure.
aPiecek60d90af2021-09-09 12:32:38 +0200619 * @param[in] opaq Node to print.
620 * @param[in] lyd_lybctx LYB context.
Michal Vasko60ea6352020-06-29 13:39:39 +0200621 * @return LY_ERR value.
622 */
623static LY_ERR
aPiecek60d90af2021-09-09 12:32:38 +0200624lyb_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 +0200625{
aPiecek60d90af2021-09-09 12:32:38 +0200626 const struct lyd_node *node;
627 struct hash_table *child_ht = NULL;
628 struct lylyb_ctx *lybctx = lyd_lybctx->lybctx;
629
aPiecek270f72b2021-09-09 11:39:31 +0200630 /* write attributes */
631 LY_CHECK_RET(lyb_print_attributes(out, opaq, lybctx));
632
633 /* write node flags */
634 LY_CHECK_RET(lyb_write_number(opaq->flags, sizeof opaq->flags, out, lybctx));
635
Michal Vasko60ea6352020-06-29 13:39:39 +0200636 /* prefix */
Michal Vaskoad92b672020-11-12 13:11:31 +0100637 LY_CHECK_RET(lyb_write_string(opaq->name.prefix, 0, 1, out, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200638
Radek Krejci1798aae2020-07-14 13:26:06 +0200639 /* module reference */
Michal Vaskoad92b672020-11-12 13:11:31 +0100640 LY_CHECK_RET(lyb_write_string(opaq->name.module_name, 0, 1, out, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200641
642 /* name */
Michal Vaskoad92b672020-11-12 13:11:31 +0100643 LY_CHECK_RET(lyb_write_string(opaq->name.name, 0, 1, out, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200644
Michal Vaskoffc92bf2021-06-21 08:15:14 +0200645 /* value */
646 LY_CHECK_RET(lyb_write_string(opaq->value, 0, 1, out, lybctx));
647
Michal Vasko60ea6352020-06-29 13:39:39 +0200648 /* format */
649 LY_CHECK_RET(lyb_write_number(opaq->format, 1, out, lybctx));
650
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100651 /* value prefixes */
652 LY_CHECK_RET(lyb_print_prefix_data(out, opaq->format, opaq->val_prefix_data, lybctx));
653
aPiecek60d90af2021-09-09 12:32:38 +0200654 /* recursively write all the descendants */
655 LY_LIST_FOR(lyd_child((struct lyd_node *)opaq), node) {
656 LY_CHECK_RET(lyb_print_subtree(out, node, &child_ht, lyd_lybctx));
657 }
658
Michal Vasko60ea6352020-06-29 13:39:39 +0200659 return LY_SUCCESS;
660}
661
662/**
663 * @brief Print anydata node.
664 *
665 * @param[in] anydata Node to print.
666 * @param[in] out Out structure.
667 * @param[in] lybctx LYB context.
668 * @return LY_ERR value.
669 */
670static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200671lyb_print_anydata(struct lyd_node_any *anydata, struct ly_out *out, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200672{
673 LY_ERR ret = LY_SUCCESS;
674 LYD_ANYDATA_VALUETYPE value_type;
675 int len;
676 char *buf = NULL;
677 const char *str;
678 struct ly_out *out2 = NULL;
679
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
687 /* first byte is type */
Michal Vasko403beac2021-08-24 08:27:52 +0200688 LY_CHECK_GOTO(ret = lyb_write_number(value_type, sizeof value_type, out, lybctx), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200689
690 if (anydata->value_type == LYD_ANYDATA_DATATREE) {
691 /* print LYB data tree to memory */
692 LY_CHECK_GOTO(ret = ly_out_new_memory(&buf, 0, &out2), cleanup);
Radek Krejci7931b192020-06-25 17:05:03 +0200693 LY_CHECK_GOTO(ret = lyb_print_data(out2, anydata->value.tree, LYD_PRINT_WITHSIBLINGS), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200694
695 len = lyd_lyb_data_length(buf);
696 assert(len != -1);
697 str = buf;
698 } else if (anydata->value_type == LYD_ANYDATA_LYB) {
699 len = lyd_lyb_data_length(anydata->value.mem);
700 assert(len != -1);
701 str = anydata->value.mem;
702 } else {
703 len = strlen(anydata->value.str);
704 str = anydata->value.str;
705 }
706
707 /* followed by the content */
708 LY_CHECK_GOTO(ret = lyb_write_string(str, (size_t)len, 0, out, lybctx), cleanup);
709
710cleanup:
711 ly_out_free(out2, NULL, 1);
712 return ret;
713}
714
715/**
716 * @brief Print term node.
717 *
718 * @param[in] term Node to print.
719 * @param[in] out Out structure.
720 * @param[in] lybctx LYB context.
721 * @return LY_ERR value.
722 */
723static LY_ERR
aPieceka19ca152021-09-09 12:27:35 +0200724lyb_print_term_value(struct lyd_node_term *term, struct ly_out *out, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200725{
aPiecekaa5b70a2021-08-30 08:33:25 +0200726 LY_ERR ret = LY_SUCCESS;
aPiecekea304e32021-08-18 09:13:47 +0200727 ly_bool dynamic = 0;
728 void *value;
729 size_t value_len = 0;
aPiecekaa5b70a2021-08-30 08:33:25 +0200730 int32_t lyb_data_len;
731 lyplg_type_print_clb print;
aPiecekea304e32021-08-18 09:13:47 +0200732
733 assert(term->value.realtype && term->value.realtype->plugin && term->value.realtype->plugin->print &&
734 term->schema);
735
aPiecekaa5b70a2021-08-30 08:33:25 +0200736 /* Get length of LYB data to print. */
737 lyb_data_len = term->value.realtype->plugin->lyb_data_len;
aPiecekea304e32021-08-18 09:13:47 +0200738
aPiecekaa5b70a2021-08-30 08:33:25 +0200739 /* Get value and also print its length only if size is not fixed. */
740 print = term->value.realtype->plugin->print;
741 if (lyb_data_len < 0) {
742 /* Variable-length data. */
743
744 /* Get value and its length from plugin. */
745 value = (void *)print(term->schema->module->ctx, &term->value,
746 LY_VALUE_LYB, NULL, &dynamic, &value_len);
747 LY_CHECK_GOTO(ret, cleanup);
748
749 if (value_len > UINT32_MAX) {
750 LOGERR(lybctx->ctx, LY_EINT, "The maximum length of the LYB data "
751 "from a term node must not exceed %lu.", UINT32_MAX);
752 ret = LY_EINT;
753 goto cleanup;
754 }
755
756 /* Print the length of the data as 32-bit unsigned integer. */
757 ret = lyb_write_number(value_len, sizeof(uint32_t), out, lybctx);
758 LY_CHECK_GOTO(ret, cleanup);
759 } else {
760 /* Fixed-length data. */
761
762 /* Get value from plugin. */
763 value = (void *)print(term->schema->module->ctx, &term->value,
764 LY_VALUE_LYB, NULL, &dynamic, NULL);
765 LY_CHECK_GOTO(ret, cleanup);
766
767 /* Copy the length from the compiled node. */
768 value_len = lyb_data_len;
aPiecekea304e32021-08-18 09:13:47 +0200769 }
770
aPiecekaa5b70a2021-08-30 08:33:25 +0200771 /* Print value. */
aPiecekea304e32021-08-18 09:13:47 +0200772 if (value_len > 0) {
aPiecekaa5b70a2021-08-30 08:33:25 +0200773 /* Print the value simply as it is. */
aPiecekea304e32021-08-18 09:13:47 +0200774 ret = lyb_write(out, value, value_len, lybctx);
775 LY_CHECK_GOTO(ret, cleanup);
776 }
777
778cleanup:
779 if (dynamic) {
780 free(value);
781 }
782
783 return ret;
Michal Vasko60ea6352020-06-29 13:39:39 +0200784}
785
786/**
787 * @brief Print YANG node metadata.
788 *
789 * @param[in] out Out structure.
790 * @param[in] node Data node whose metadata to print.
791 * @param[in] lybctx LYB context.
792 * @return LY_ERR value.
793 */
794static LY_ERR
795lyb_print_metadata(struct ly_out *out, const struct lyd_node *node, struct lyd_lyb_ctx *lybctx)
796{
Michal Vasko60ea6352020-06-29 13:39:39 +0200797 uint8_t count = 0;
798 const struct lys_module *wd_mod = NULL;
799 struct lyd_meta *iter;
Michal Vasko60ea6352020-06-29 13:39:39 +0200800
801 /* with-defaults */
802 if (node->schema->nodetype & LYD_NODE_TERM) {
Radek Krejci7931b192020-06-25 17:05:03 +0200803 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 +0200804 ((lybctx->print_options & LYD_PRINT_WD_ALL_TAG) && lyd_is_default(node))) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200805 /* we have implicit OR explicit default node, print attribute only if context include with-defaults schema */
806 wd_mod = ly_ctx_get_module_latest(node->schema->module->ctx, "ietf-netconf-with-defaults");
807 }
808 }
809
810 /* count metadata */
811 if (wd_mod) {
812 ++count;
813 }
814 for (iter = node->meta; iter; iter = iter->next) {
815 if (count == UINT8_MAX) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200816 LOGERR(lybctx->lybctx->ctx, LY_EINT, "Maximum supported number of data node metadata is %u.", UINT8_MAX);
Michal Vasko60ea6352020-06-29 13:39:39 +0200817 return LY_EINT;
818 }
819 ++count;
820 }
821
822 /* write number of metadata on 1 byte */
Radek Krejci1798aae2020-07-14 13:26:06 +0200823 LY_CHECK_RET(lyb_write(out, &count, 1, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200824
825 if (wd_mod) {
826 /* write the "default" metadata */
Radek Krejci1798aae2020-07-14 13:26:06 +0200827 LY_CHECK_RET(lyb_write_start_subtree(out, lybctx->lybctx));
828 LY_CHECK_RET(lyb_print_model(out, wd_mod, lybctx->lybctx));
829 LY_CHECK_RET(lyb_write_string("default", 0, 1, out, lybctx->lybctx));
830 LY_CHECK_RET(lyb_write_string("true", 0, 0, out, lybctx->lybctx));
831 LY_CHECK_RET(lyb_write_stop_subtree(out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200832 }
833
834 /* write all the node metadata */
835 LY_LIST_FOR(node->meta, iter) {
836 /* each metadata is a subtree */
Radek Krejci1798aae2020-07-14 13:26:06 +0200837 LY_CHECK_RET(lyb_write_start_subtree(out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200838
839 /* model */
Radek Krejci1798aae2020-07-14 13:26:06 +0200840 LY_CHECK_RET(lyb_print_model(out, iter->annotation->module, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200841
842 /* annotation name with length */
Radek Krejci1798aae2020-07-14 13:26:06 +0200843 LY_CHECK_RET(lyb_write_string(iter->name, 0, 1, out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200844
Michal Vasko60ea6352020-06-29 13:39:39 +0200845 /* metadata value */
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200846 LY_CHECK_RET(lyb_write_string(lyd_get_meta_value(iter), 0, 0, out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200847
848 /* finish metadata subtree */
Radek Krejci1798aae2020-07-14 13:26:06 +0200849 LY_CHECK_RET(lyb_write_stop_subtree(out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200850 }
851
852 return LY_SUCCESS;
853}
854
855/**
856 * @brief Print opaque node attributes.
857 *
858 * @param[in] out Out structure.
859 * @param[in] node Opaque node whose attributes to print.
860 * @param[in] lybctx LYB context.
861 * @return LY_ERR value.
862 */
863static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200864lyb_print_attributes(struct ly_out *out, const struct lyd_node_opaq *node, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200865{
866 uint8_t count = 0;
Radek Krejci5536d282020-08-04 23:27:44 +0200867 struct lyd_attr *iter;
Michal Vasko60ea6352020-06-29 13:39:39 +0200868
869 for (iter = node->attr; iter; iter = iter->next) {
870 if (count == UINT8_MAX) {
871 LOGERR(lybctx->ctx, LY_EINT, "Maximum supported number of data node attributes is %u.", UINT8_MAX);
872 return LY_EINT;
873 }
874 ++count;
875 }
876
877 /* write number of attributes on 1 byte */
878 LY_CHECK_RET(lyb_write(out, &count, 1, lybctx));
879
880 /* write all the attributes */
881 LY_LIST_FOR(node->attr, iter) {
882 /* each attribute is a subtree */
883 LY_CHECK_RET(lyb_write_start_subtree(out, lybctx));
884
885 /* prefix */
Michal Vaskoad92b672020-11-12 13:11:31 +0100886 LY_CHECK_RET(lyb_write_string(iter->name.prefix, 0, 1, out, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200887
888 /* namespace */
Michal Vaskoad92b672020-11-12 13:11:31 +0100889 LY_CHECK_RET(lyb_write_string(iter->name.module_name, 0, 1, out, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200890
891 /* name */
Michal Vaskoad92b672020-11-12 13:11:31 +0100892 LY_CHECK_RET(lyb_write_string(iter->name.name, 0, 1, out, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200893
Michal Vasko60ea6352020-06-29 13:39:39 +0200894 /* format */
895 LY_CHECK_RET(lyb_write_number(iter->format, 1, out, lybctx));
896
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100897 /* value prefixes */
898 LY_CHECK_RET(lyb_print_prefix_data(out, iter->format, iter->val_prefix_data, lybctx));
899
Michal Vasko60ea6352020-06-29 13:39:39 +0200900 /* value */
901 LY_CHECK_RET(lyb_write_string(iter->value, 0, 0, out, lybctx));
902
903 /* finish attribute subtree */
904 LY_CHECK_RET(lyb_write_stop_subtree(out, lybctx));
905 }
906
907 return LY_SUCCESS;
908}
909
910/**
aPiecek270f72b2021-09-09 11:39:31 +0200911 * @brief Print header for non-opaq node.
912 *
913 * @param[in] out Out structure.
914 * @param[in] node Current data node to print.
915 * @param[in] lybctx LYB context.
916 * @return LY_ERR value.
917 */
918static LY_ERR
919lyb_print_node_header(struct ly_out *out, const struct lyd_node *node, struct lyd_lyb_ctx *lybctx)
920{
921 /* write any metadata */
922 LY_CHECK_RET(lyb_print_metadata(out, node, lybctx));
923
924 /* write node flags */
925 LY_CHECK_RET(lyb_write_number(node->flags, sizeof node->flags, out, lybctx->lybctx));
926
927 return LY_SUCCESS;
928}
929
930/**
931 * @brief Print model and hash.
932 *
933 * @param[in] out Out structure.
934 * @param[in] node Current data node to print.
935 * @param[in,out] sibling_ht Cached hash table for these siblings, created if NULL.
936 * @param[in] lybctx LYB context.
937 * @return LY_ERR value.
938 */
939static LY_ERR
940lyb_print_model_and_hash(struct ly_out *out, const struct lyd_node *node, struct hash_table **sibling_ht,
941 struct lylyb_ctx *lybctx)
942{
943 /* write model info first, for all opaque and top-level nodes */
944 if (!node->schema && (!node->parent || !node->parent->schema)) {
945 LY_CHECK_RET(lyb_print_model(out, NULL, lybctx));
946 } else if (node->schema && !lysc_data_parent(node->schema)) {
947 LY_CHECK_RET(lyb_print_model(out, node->schema->module, lybctx));
948 }
949
950 /* write schema hash */
951 LY_CHECK_RET(lyb_print_schema_hash(out, (struct lysc_node *)node->schema, sibling_ht, lybctx));
952
953 return LY_SUCCESS;
954}
955
956/**
Michal Vasko60ea6352020-06-29 13:39:39 +0200957 * @brief Print schema node hash.
958 *
959 * @param[in] out Out structure.
960 * @param[in] schema Schema node whose hash to print.
961 * @param[in,out] sibling_ht Cached hash table for these siblings, created if NULL.
962 * @param[in] lybctx LYB context.
963 * @return LY_ERR value.
964 */
965static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200966lyb_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 +0200967{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200968 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +0200969 uint32_t i;
970 LYB_HASH hash;
971 struct lyd_lyb_sib_ht *sib_ht;
972 struct lysc_node *first_sibling;
973
974 if (!schema) {
975 /* opaque node, write empty hash */
976 hash = 0;
977 LY_CHECK_RET(lyb_write(out, &hash, sizeof hash, lybctx));
978 return LY_SUCCESS;
979 }
980
981 /* create whole sibling HT if not already created and saved */
982 if (!*sibling_ht) {
Michal Vasko1e0a80a2020-12-09 18:12:51 +0100983 /* get first schema data sibling */
984 first_sibling = (struct lysc_node *)lys_getnext(NULL, lysc_data_parent(schema), schema->module->compiled,
Michal Vaskod1e53b92021-01-28 13:11:06 +0100985 (schema->flags & LYS_IS_OUTPUT) ? LYS_GETNEXT_OUTPUT : 0);
Michal Vasko60ea6352020-06-29 13:39:39 +0200986 LY_ARRAY_FOR(lybctx->sib_hts, u) {
987 if (lybctx->sib_hts[u].first_sibling == first_sibling) {
988 /* we have already created a hash table for these siblings */
989 *sibling_ht = lybctx->sib_hts[u].ht;
990 break;
991 }
992 }
993
994 if (!*sibling_ht) {
995 /* we must create sibling hash table */
996 LY_CHECK_RET(lyb_hash_siblings(first_sibling, sibling_ht));
997
998 /* and save it */
999 LY_ARRAY_NEW_RET(lybctx->ctx, lybctx->sib_hts, sib_ht, LY_EMEM);
1000
1001 sib_ht->first_sibling = first_sibling;
1002 sib_ht->ht = *sibling_ht;
1003 }
1004 }
1005
1006 /* get our hash */
1007 LY_CHECK_RET(lyb_hash_find(*sibling_ht, schema, &hash));
1008
1009 /* write the hash */
1010 LY_CHECK_RET(lyb_write(out, &hash, sizeof hash, lybctx));
1011
1012 if (hash & LYB_HASH_COLLISION_ID) {
1013 /* no collision for this hash, we are done */
1014 return LY_SUCCESS;
1015 }
1016
1017 /* written hash was a collision, write also all the preceding hashes */
Radek Krejci1e008d22020-08-17 11:37:37 +02001018 for (i = 0; !(hash & (LYB_HASH_COLLISION_ID >> i)); ++i) {}
Michal Vasko60ea6352020-06-29 13:39:39 +02001019
Michal Vaskod989ba02020-08-24 10:59:24 +02001020 for ( ; i; --i) {
Michal Vasko11f76c82021-04-15 14:36:14 +02001021 hash = lyb_get_hash(schema, i - 1);
Michal Vasko60ea6352020-06-29 13:39:39 +02001022 if (!hash) {
1023 return LY_EINT;
1024 }
1025 assert(hash & (LYB_HASH_COLLISION_ID >> (i - 1)));
1026
1027 LY_CHECK_RET(lyb_write(out, &hash, sizeof hash, lybctx));
1028 }
1029
1030 return LY_SUCCESS;
1031}
1032
1033/**
1034 * @brief Print data subtree.
1035 *
1036 * @param[in] out Out structure.
1037 * @param[in] node Root node of the subtree to print.
1038 * @param[in,out] sibling_ht Cached hash table for these data siblings, created if NULL.
1039 * @param[in] lybctx LYB context.
1040 * @return LY_ERR value.
1041 */
1042static LY_ERR
1043lyb_print_subtree(struct ly_out *out, const struct lyd_node *node, struct hash_table **sibling_ht, struct lyd_lyb_ctx *lybctx)
1044{
1045 struct hash_table *child_ht = NULL;
1046
1047 /* register a new subtree */
Radek Krejci1798aae2020-07-14 13:26:06 +02001048 LY_CHECK_RET(lyb_write_start_subtree(out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +02001049
aPiecek270f72b2021-09-09 11:39:31 +02001050 LY_CHECK_RET(lyb_print_model_and_hash(out, node, sibling_ht, lybctx->lybctx));
Michal Vaskoc5e866a2020-11-04 17:09:26 +01001051
Michal Vasko60ea6352020-06-29 13:39:39 +02001052 /* write node content */
1053 if (!node->schema) {
aPiecek60d90af2021-09-09 12:32:38 +02001054 LY_CHECK_RET(lyb_print_node_opaq(out, (struct lyd_node_opaq *)node, lybctx));
1055 goto stop_subtree;
Michal Vasko60ea6352020-06-29 13:39:39 +02001056 } else if (node->schema->nodetype & LYD_NODE_INNER) {
aPiecek270f72b2021-09-09 11:39:31 +02001057 LY_CHECK_RET(lyb_print_node_header(out, node, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +02001058 } else if (node->schema->nodetype & LYD_NODE_TERM) {
aPiecek270f72b2021-09-09 11:39:31 +02001059 LY_CHECK_RET(lyb_print_node_header(out, node, lybctx));
aPieceka19ca152021-09-09 12:27:35 +02001060 LY_CHECK_RET(lyb_print_term_value((struct lyd_node_term *)node, out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +02001061 } else if (node->schema->nodetype & LYD_NODE_ANY) {
aPiecek270f72b2021-09-09 11:39:31 +02001062 LY_CHECK_RET(lyb_print_node_header(out, node, lybctx));
Radek Krejci1798aae2020-07-14 13:26:06 +02001063 LY_CHECK_RET(lyb_print_anydata((struct lyd_node_any *)node, out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +02001064 } else {
Radek Krejci1798aae2020-07-14 13:26:06 +02001065 LOGINT_RET(lybctx->lybctx->ctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001066 }
1067
1068 /* recursively write all the descendants */
Radek Krejcia1c1e542020-09-29 16:06:52 +02001069 LY_LIST_FOR(lyd_child(node), node) {
Michal Vasko60ea6352020-06-29 13:39:39 +02001070 LY_CHECK_RET(lyb_print_subtree(out, node, &child_ht, lybctx));
1071 }
1072
aPiecek60d90af2021-09-09 12:32:38 +02001073stop_subtree:
Michal Vasko60ea6352020-06-29 13:39:39 +02001074 /* finish this subtree */
Radek Krejci1798aae2020-07-14 13:26:06 +02001075 LY_CHECK_RET(lyb_write_stop_subtree(out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +02001076
1077 return LY_SUCCESS;
1078}
1079
1080LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001081lyb_print_data(struct ly_out *out, const struct lyd_node *root, uint32_t options)
Michal Vasko60ea6352020-06-29 13:39:39 +02001082{
1083 LY_ERR ret = LY_SUCCESS;
1084 uint8_t zero = 0;
Michal Vasko60ea6352020-06-29 13:39:39 +02001085 struct hash_table *top_sibling_ht = NULL;
1086 const struct lys_module *prev_mod = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +02001087 struct lyd_lyb_ctx *lybctx;
Michal Vaskob7be7a82020-08-20 09:09:04 +02001088 const struct ly_ctx *ctx = root ? LYD_CTX(root) : NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +02001089
Radek Krejci1798aae2020-07-14 13:26:06 +02001090 lybctx = calloc(1, sizeof *lybctx);
1091 LY_CHECK_ERR_RET(!lybctx, LOGMEM(ctx), LY_EMEM);
1092 lybctx->lybctx = calloc(1, sizeof *lybctx->lybctx);
1093 LY_CHECK_ERR_RET(!lybctx, LOGMEM(ctx), LY_EMEM);
1094
1095 lybctx->print_options = options;
Michal Vasko60ea6352020-06-29 13:39:39 +02001096 if (root) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001097 lybctx->lybctx->ctx = ctx;
Michal Vasko60ea6352020-06-29 13:39:39 +02001098
1099 if (root->schema && lysc_data_parent(root->schema)) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001100 LOGERR(lybctx->lybctx->ctx, LY_EINVAL, "LYB printer supports only printing top-level nodes.");
Michal Vasko9acaf492020-08-13 09:05:58 +02001101 ret = LY_EINVAL;
1102 goto cleanup;
Michal Vasko60ea6352020-06-29 13:39:39 +02001103 }
1104 }
1105
1106 /* LYB magic number */
Michal Vasko63f3d842020-07-08 10:10:14 +02001107 LY_CHECK_GOTO(ret = lyb_print_magic_number(out), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001108
1109 /* LYB header */
Michal Vasko63f3d842020-07-08 10:10:14 +02001110 LY_CHECK_GOTO(ret = lyb_print_header(out), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001111
1112 /* all used models */
Radek Krejci1798aae2020-07-14 13:26:06 +02001113 LY_CHECK_GOTO(ret = lyb_print_data_models(out, root, lybctx->lybctx), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001114
1115 LY_LIST_FOR(root, root) {
1116 /* do not reuse sibling hash tables from different modules */
1117 if (!root->schema || (root->schema->module != prev_mod)) {
1118 top_sibling_ht = NULL;
1119 prev_mod = root->schema ? root->schema->module : NULL;
1120 }
1121
Radek Krejci1798aae2020-07-14 13:26:06 +02001122 LY_CHECK_GOTO(ret = lyb_print_subtree(out, root, &top_sibling_ht, lybctx), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001123
Radek Krejci7931b192020-06-25 17:05:03 +02001124 if (!(options & LYD_PRINT_WITHSIBLINGS)) {
Michal Vasko60ea6352020-06-29 13:39:39 +02001125 break;
1126 }
1127 }
1128
1129 /* ending zero byte */
Radek Krejci1798aae2020-07-14 13:26:06 +02001130 LY_CHECK_GOTO(ret = lyb_write(out, &zero, sizeof zero, lybctx->lybctx), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001131
1132cleanup:
Radek Krejci1798aae2020-07-14 13:26:06 +02001133 lyd_lyb_ctx_free((struct lyd_ctx *)lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001134 return ret;
1135}