blob: 0b6a48a1a00aaa750f4f8ae74020b864acce775a [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);
aPiecek570d7ed2021-09-10 07:15:35 +020043static 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 +020044static 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 +020045
Michal Vasko60ea6352020-06-29 13:39:39 +020046/**
47 * @brief Hash table equal callback for checking hash equality only.
Radek Krejci857189e2020-09-01 13:26:36 +020048 *
Michal Vasko62524a92021-02-26 10:08:50 +010049 * Implementation of ::lyht_value_equal_cb.
Michal Vasko60ea6352020-06-29 13:39:39 +020050 */
Radek Krejci857189e2020-09-01 13:26:36 +020051static ly_bool
52lyb_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 +020053{
54 /* for this purpose, if hash matches, the value does also, we do not want 2 values to have the same hash */
55 return 1;
56}
57
58/**
59 * @brief Hash table equal callback for checking value pointer equality only.
Radek Krejci857189e2020-09-01 13:26:36 +020060 *
Michal Vasko62524a92021-02-26 10:08:50 +010061 * Implementation of ::lyht_value_equal_cb.
Michal Vasko60ea6352020-06-29 13:39:39 +020062 */
Radek Krejci857189e2020-09-01 13:26:36 +020063static ly_bool
64lyb_ptr_equal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
Michal Vasko60ea6352020-06-29 13:39:39 +020065{
66 struct lysc_node *val1 = *(struct lysc_node **)val1_p;
67 struct lysc_node *val2 = *(struct lysc_node **)val2_p;
68
69 if (val1 == val2) {
70 return 1;
71 }
72 return 0;
73}
74
75/**
76 * @brief Check that sibling collision hash is safe to insert into hash table.
77 *
78 * @param[in] ht Hash table.
79 * @param[in] sibling Hashed sibling.
80 * @param[in] ht_col_id Sibling hash collision ID.
81 * @param[in] compare_col_id Last collision ID to compare with.
82 * @return LY_SUCCESS when the whole hash sequence does not collide,
83 * @return LY_EEXIST when the whole hash sequence sollides.
84 */
85static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +020086lyb_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 +020087{
Michal Vasko60ea6352020-06-29 13:39:39 +020088 struct lysc_node **col_node;
89
90 /* get the first node inserted with last hash col ID ht_col_id */
Michal Vasko11f76c82021-04-15 14:36:14 +020091 if (lyht_find(ht, &sibling, lyb_get_hash(sibling, ht_col_id), (void **)&col_node)) {
Michal Vasko60ea6352020-06-29 13:39:39 +020092 /* there is none. valid situation */
93 return LY_SUCCESS;
94 }
95
96 lyht_set_cb(ht, lyb_ptr_equal_cb);
97 do {
Radek Krejci1deb5be2020-08-26 16:43:36 +020098 int64_t j;
99 for (j = (int64_t)compare_col_id; j > -1; --j) {
Michal Vasko11f76c82021-04-15 14:36:14 +0200100 if (lyb_get_hash(sibling, j) != lyb_get_hash(*col_node, j)) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200101 /* one non-colliding hash */
102 break;
103 }
104 }
105 if (j == -1) {
106 /* all whole hash sequences of nodes inserted with last hash col ID compare_col_id collide */
107 lyht_set_cb(ht, lyb_hash_equal_cb);
108 return LY_EEXIST;
109 }
110
111 /* get next node inserted with last hash col ID ht_col_id */
Michal Vasko11f76c82021-04-15 14:36:14 +0200112 } 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 +0200113
114 lyht_set_cb(ht, lyb_hash_equal_cb);
115 return LY_SUCCESS;
116}
117
118/**
119 * @brief Hash all the siblings and add them also into a separate hash table.
120 *
121 * @param[in] sibling Any sibling in all the siblings on one level.
122 * @param[out] ht_p Created hash table.
123 * @return LY_ERR value.
124 */
125static LY_ERR
126lyb_hash_siblings(struct lysc_node *sibling, struct hash_table **ht_p)
127{
128 struct hash_table *ht;
129 const struct lysc_node *parent;
130 const struct lys_module *mod;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200131 LYB_HASH i;
Michal Vasko1e0a80a2020-12-09 18:12:51 +0100132 uint32_t getnext_opts;
Michal Vasko60ea6352020-06-29 13:39:39 +0200133
134 ht = lyht_new(1, sizeof(struct lysc_node *), lyb_hash_equal_cb, NULL, 1);
135 LY_CHECK_ERR_RET(!ht, LOGMEM(sibling->module->ctx), LY_EMEM);
136
Michal Vasko1e0a80a2020-12-09 18:12:51 +0100137 getnext_opts = 0;
Michal Vaskod1e53b92021-01-28 13:11:06 +0100138 if (sibling->flags & LYS_IS_OUTPUT) {
Michal Vasko1e0a80a2020-12-09 18:12:51 +0100139 getnext_opts = LYS_GETNEXT_OUTPUT;
140 }
141
Michal Vasko60ea6352020-06-29 13:39:39 +0200142 parent = lysc_data_parent(sibling);
143 mod = sibling->module;
144
145 sibling = NULL;
Michal Vasko1e0a80a2020-12-09 18:12:51 +0100146 while ((sibling = (struct lysc_node *)lys_getnext(sibling, parent, mod->compiled, getnext_opts))) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200147 /* find the first non-colliding hash (or specifically non-colliding hash sequence) */
148 for (i = 0; i < LYB_HASH_BITS; ++i) {
149 /* check that we are not colliding with nodes inserted with a lower collision ID than ours */
Radek Krejci1deb5be2020-08-26 16:43:36 +0200150 int64_t j;
151 for (j = (int64_t)i - 1; j > -1; --j) {
152 if (lyb_hash_sequence_check(ht, sibling, (LYB_HASH)j, i)) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200153 break;
154 }
155 }
156 if (j > -1) {
157 /* some check failed, we must use a higher collision ID */
158 continue;
159 }
160
161 /* try to insert node with the current collision ID */
Michal Vasko11f76c82021-04-15 14:36:14 +0200162 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 +0200163 /* success, no collision */
164 break;
165 }
166
167 /* make sure we really cannot insert it with this hash col ID (meaning the whole hash sequence is colliding) */
168 if (i && !lyb_hash_sequence_check(ht, sibling, i, i)) {
169 /* it can be inserted after all, even though there is already a node with the same last collision ID */
170 lyht_set_cb(ht, lyb_ptr_equal_cb);
Michal Vasko11f76c82021-04-15 14:36:14 +0200171 if (lyht_insert(ht, &sibling, lyb_get_hash(sibling, i), NULL)) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200172 LOGINT(sibling->module->ctx);
173 lyht_set_cb(ht, lyb_hash_equal_cb);
174 lyht_free(ht);
175 return LY_EINT;
176 }
177 lyht_set_cb(ht, lyb_hash_equal_cb);
178 break;
179 }
180 /* there is still another colliding schema node with the same hash sequence, try higher collision ID */
181 }
182
183 if (i == LYB_HASH_BITS) {
184 /* wow */
185 LOGINT(sibling->module->ctx);
186 lyht_free(ht);
187 return LY_EINT;
188 }
189 }
190
191 /* change val equal callback so that the HT is usable for finding value hashes */
192 lyht_set_cb(ht, lyb_ptr_equal_cb);
193
194 *ht_p = ht;
195 return LY_SUCCESS;
196}
197
198/**
199 * @brief Find node hash in a hash table.
200 *
201 * @param[in] ht Hash table to search in.
202 * @param[in] node Node to find.
203 * @param[out] hash_p First non-colliding hash found.
204 * @return LY_ERR value.
205 */
206static LY_ERR
207lyb_hash_find(struct hash_table *ht, struct lysc_node *node, LYB_HASH *hash_p)
208{
209 LYB_HASH hash;
210 uint32_t i;
211
212 for (i = 0; i < LYB_HASH_BITS; ++i) {
Michal Vasko11f76c82021-04-15 14:36:14 +0200213 hash = lyb_get_hash(node, i);
Michal Vasko60ea6352020-06-29 13:39:39 +0200214 if (!hash) {
215 LOGINT_RET(node->module->ctx);
216 }
217
218 if (!lyht_find(ht, &node, hash, NULL)) {
219 /* success, no collision */
220 break;
221 }
222 }
223 /* cannot happen, we already calculated the hash */
224 if (i == LYB_HASH_BITS) {
225 LOGINT_RET(node->module->ctx);
226 }
227
228 *hash_p = hash;
229 return LY_SUCCESS;
230}
231
232/**
233 * @brief Write LYB data fully handling the metadata.
234 *
235 * @param[in] out Out structure.
236 * @param[in] buf Source buffer.
237 * @param[in] count Number of bytes to write.
238 * @param[in] lybctx LYB context.
239 * @return LY_ERR value.
240 */
241static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200242lyb_write(struct ly_out *out, const uint8_t *buf, size_t count, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200243{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200244 LY_ARRAY_COUNT_TYPE u;
aPiecek570d7ed2021-09-10 07:15:35 +0200245 struct lyd_lyb_sibling *full, *iter;
Michal Vasko5233e962020-08-14 14:26:20 +0200246 size_t to_write;
Michal Vasko60ea6352020-06-29 13:39:39 +0200247 uint8_t meta_buf[LYB_META_BYTES];
248
249 while (1) {
250 /* check for full data chunks */
251 to_write = count;
252 full = NULL;
aPiecek570d7ed2021-09-10 07:15:35 +0200253 LY_ARRAY_FOR(lybctx->siblings, u) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200254 /* we want the innermost chunks resolved first, so replace previous full chunks */
aPiecek570d7ed2021-09-10 07:15:35 +0200255 if (lybctx->siblings[u].written + to_write >= LYB_SIZE_MAX) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200256 /* full chunk, do not write more than allowed */
aPiecek570d7ed2021-09-10 07:15:35 +0200257 to_write = LYB_SIZE_MAX - lybctx->siblings[u].written;
258 full = &lybctx->siblings[u];
Michal Vasko60ea6352020-06-29 13:39:39 +0200259 }
260 }
261
262 if (!full && !count) {
263 break;
264 }
265
266 /* we are actually writing some data, not just finishing another chunk */
267 if (to_write) {
Michal Vasko5233e962020-08-14 14:26:20 +0200268 LY_CHECK_RET(ly_write_(out, (char *)buf, to_write));
Michal Vasko60ea6352020-06-29 13:39:39 +0200269
aPiecek570d7ed2021-09-10 07:15:35 +0200270 LY_ARRAY_FOR(lybctx->siblings, u) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200271 /* increase all written counters */
aPiecek570d7ed2021-09-10 07:15:35 +0200272 lybctx->siblings[u].written += to_write;
273 assert(lybctx->siblings[u].written <= LYB_SIZE_MAX);
Michal Vasko60ea6352020-06-29 13:39:39 +0200274 }
275 /* decrease count/buf */
Michal Vasko5233e962020-08-14 14:26:20 +0200276 count -= to_write;
277 buf += to_write;
Michal Vasko60ea6352020-06-29 13:39:39 +0200278 }
279
280 if (full) {
281 /* write the meta information (inner chunk count and chunk size) */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100282 meta_buf[0] = full->written & LYB_BYTE_MASK;
283 meta_buf[1] = full->inner_chunks & LYB_BYTE_MASK;
Michal Vasko5233e962020-08-14 14:26:20 +0200284 LY_CHECK_RET(ly_write_skipped(out, full->position, (char *)meta_buf, LYB_META_BYTES));
Michal Vasko60ea6352020-06-29 13:39:39 +0200285
286 /* zero written and inner chunks */
287 full->written = 0;
288 full->inner_chunks = 0;
289
290 /* skip space for another chunk size */
Michal Vasko5233e962020-08-14 14:26:20 +0200291 LY_CHECK_RET(ly_write_skip(out, LYB_META_BYTES, &full->position));
Michal Vasko60ea6352020-06-29 13:39:39 +0200292
293 /* increase inner chunk count */
aPiecek570d7ed2021-09-10 07:15:35 +0200294 for (iter = &lybctx->siblings[0]; iter != full; ++iter) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200295 if (iter->inner_chunks == LYB_INCHUNK_MAX) {
296 LOGINT(lybctx->ctx);
297 return LY_EINT;
298 }
299 ++iter->inner_chunks;
300 }
301 }
302 }
303
304 return LY_SUCCESS;
305}
306
307/**
aPiecek570d7ed2021-09-10 07:15:35 +0200308 * @brief Stop the current "siblings" - write its final metadata.
Michal Vasko60ea6352020-06-29 13:39:39 +0200309 *
310 * @param[in] out Out structure.
311 * @param[in] lybctx LYB context.
312 * @return LY_ERR value.
313 */
314static LY_ERR
aPiecek570d7ed2021-09-10 07:15:35 +0200315lyb_write_stop_siblings(struct ly_out *out, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200316{
Michal Vasko60ea6352020-06-29 13:39:39 +0200317 uint8_t meta_buf[LYB_META_BYTES];
318
319 /* write the meta chunk information */
aPiecek570d7ed2021-09-10 07:15:35 +0200320 meta_buf[0] = LYB_LAST_SIBLING(lybctx).written & LYB_BYTE_MASK;
321 meta_buf[1] = LYB_LAST_SIBLING(lybctx).inner_chunks & LYB_BYTE_MASK;
322 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 +0200323
aPiecek570d7ed2021-09-10 07:15:35 +0200324 LY_ARRAY_DECREMENT(lybctx->siblings);
Michal Vasko60ea6352020-06-29 13:39:39 +0200325 return LY_SUCCESS;
326}
327
328/**
aPiecek570d7ed2021-09-10 07:15:35 +0200329 * @brief Start a new "siblings" - skip bytes for its metadata.
Michal Vasko60ea6352020-06-29 13:39:39 +0200330 *
331 * @param[in] out Out structure.
332 * @param[in] lybctx LYB context.
333 * @return LY_ERR value.
334 */
335static LY_ERR
aPiecek570d7ed2021-09-10 07:15:35 +0200336lyb_write_start_siblings(struct ly_out *out, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200337{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200338 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +0200339
aPiecek570d7ed2021-09-10 07:15:35 +0200340 u = LY_ARRAY_COUNT(lybctx->siblings);
341 if (u == lybctx->sibling_size) {
342 LY_ARRAY_CREATE_RET(lybctx->ctx, lybctx->siblings, u + LYB_SIBLING_STEP, LY_EMEM);
343 lybctx->sibling_size = u + LYB_SIBLING_STEP;
Michal Vasko60ea6352020-06-29 13:39:39 +0200344 }
345
aPiecek570d7ed2021-09-10 07:15:35 +0200346 LY_ARRAY_INCREMENT(lybctx->siblings);
347 LYB_LAST_SIBLING(lybctx).written = 0;
348 LYB_LAST_SIBLING(lybctx).inner_chunks = 0;
Michal Vasko60ea6352020-06-29 13:39:39 +0200349
350 /* another inner chunk */
aPiecek570d7ed2021-09-10 07:15:35 +0200351 for (u = 0; u < LY_ARRAY_COUNT(lybctx->siblings) - 1; ++u) {
352 if (lybctx->siblings[u].inner_chunks == LYB_INCHUNK_MAX) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200353 LOGINT(lybctx->ctx);
Michal Vasko5233e962020-08-14 14:26:20 +0200354 return LY_EINT;
Michal Vasko60ea6352020-06-29 13:39:39 +0200355 }
aPiecek570d7ed2021-09-10 07:15:35 +0200356 ++lybctx->siblings[u].inner_chunks;
Michal Vasko60ea6352020-06-29 13:39:39 +0200357 }
358
aPiecek570d7ed2021-09-10 07:15:35 +0200359 LY_CHECK_RET(ly_write_skip(out, LYB_META_BYTES, &LYB_LAST_SIBLING(lybctx).position));
Michal Vasko60ea6352020-06-29 13:39:39 +0200360
361 return LY_SUCCESS;
362}
363
364/**
365 * @brief Write a number.
366 *
367 * @param[in] num Number to write.
368 * @param[in] bytes Actual accessible bytes of @p num.
369 * @param[in] out Out structure.
370 * @param[in] lybctx LYB context.
371 * @return LY_ERR value.
372 */
373static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200374lyb_write_number(uint64_t num, size_t bytes, struct ly_out *out, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200375{
376 /* correct byte order */
377 num = htole64(num);
378
379 return lyb_write(out, (uint8_t *)&num, bytes, lybctx);
380}
381
382/**
383 * @brief Write a string.
384 *
385 * @param[in] str String to write.
386 * @param[in] str_len Length of @p str.
387 * @param[in] with_length Whether to precede the string with its length.
388 * @param[in] out Out structure.
389 * @param[in] lybctx LYB context.
390 * @return LY_ERR value.
391 */
392static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200393lyb_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 +0200394{
Michal Vasko60ea6352020-06-29 13:39:39 +0200395 if (!str) {
396 str = "";
Michal Vasko30667352020-08-13 09:06:14 +0200397 LY_CHECK_ERR_RET(str_len, LOGINT(lybctx->ctx), LY_EINT);
Michal Vasko60ea6352020-06-29 13:39:39 +0200398 }
399 if (!str_len) {
400 str_len = strlen(str);
401 }
402
403 if (with_length) {
404 /* print length on 2 bytes */
405 if (str_len > UINT16_MAX) {
406 LOGINT(lybctx->ctx);
407 return LY_EINT;
408 }
409 LY_CHECK_RET(lyb_write_number(str_len, 2, out, lybctx));
410 }
411
Michal Vasko63f3d842020-07-08 10:10:14 +0200412 LY_CHECK_RET(lyb_write(out, (const uint8_t *)str, str_len, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200413
414 return LY_SUCCESS;
415}
416
417/**
418 * @brief Print YANG module info.
419 *
420 * @param[in] out Out structure.
421 * @param[in] mod Module to print.
422 * @param[in] lybctx LYB context.
423 * @return LY_ERR value.
424 */
425static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200426lyb_print_model(struct ly_out *out, const struct lys_module *mod, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200427{
Michal Vasko60ea6352020-06-29 13:39:39 +0200428 uint16_t revision;
429
430 /* model name length and model name */
431 if (mod) {
432 LY_CHECK_RET(lyb_write_string(mod->name, 0, 1, out, lybctx));
433 } else {
aPiecek570d7ed2021-09-10 07:15:35 +0200434 lyb_write_number(0, 2, out, lybctx);
435 return LY_SUCCESS;
Michal Vasko60ea6352020-06-29 13:39:39 +0200436 }
437
438 /* model revision as XXXX XXXX XXXX XXXX (2B) (year is offset from 2000)
439 * YYYY YYYM MMMD DDDD */
440 revision = 0;
441 if (mod && mod->revision) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200442 int r = atoi(mod->revision);
Radek Krejcif13b87b2020-12-01 22:02:17 +0100443 r -= LYB_REV_YEAR_OFFSET;
444 r <<= LYB_REV_YEAR_SHIFT;
Michal Vasko60ea6352020-06-29 13:39:39 +0200445
446 revision |= r;
447
Radek Krejcif13b87b2020-12-01 22:02:17 +0100448 r = atoi(mod->revision + ly_strlen_const("YYYY-"));
449 r <<= LYB_REV_MONTH_SHIFT;
Michal Vasko60ea6352020-06-29 13:39:39 +0200450
451 revision |= r;
452
Radek Krejcif13b87b2020-12-01 22:02:17 +0100453 r = atoi(mod->revision + ly_strlen_const("YYYY-MM-"));
Michal Vasko60ea6352020-06-29 13:39:39 +0200454
455 revision |= r;
456 }
457 LY_CHECK_RET(lyb_write_number(revision, sizeof revision, out, lybctx));
458
Michal Vasko11f76c82021-04-15 14:36:14 +0200459 if (mod) {
460 /* fill cached hashes, if not already */
461 lyb_cache_module_hash(mod);
462 }
463
Michal Vasko60ea6352020-06-29 13:39:39 +0200464 return LY_SUCCESS;
465}
466
467/**
468 * @brief Print all used YANG modules.
469 *
470 * @param[in] out Out structure.
471 * @param[in] root Data root.
472 * @param[in] lybctx LYB context.
473 * @return LY_ERR value.
474 */
475static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200476lyb_print_data_models(struct ly_out *out, const struct lyd_node *root, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200477{
478 struct ly_set *set;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200479 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +0200480 LY_ERR ret = LY_SUCCESS;
481 struct lys_module *mod;
Michal Vaskof4b4d002021-08-23 12:16:02 +0200482 const struct lyd_node *elem, *node;
Michal Vasko60ea6352020-06-29 13:39:39 +0200483 uint32_t i;
484
Radek Krejciba03a5a2020-08-27 14:40:41 +0200485 LY_CHECK_RET(ly_set_new(&set));
Michal Vasko60ea6352020-06-29 13:39:39 +0200486
487 /* collect all data node modules */
Michal Vaskof4b4d002021-08-23 12:16:02 +0200488 LY_LIST_FOR(root, elem) {
489 LYD_TREE_DFS_BEGIN(elem, node) {
490 if (node->schema) {
491 mod = node->schema->module;
492 ret = ly_set_add(set, mod, 0, NULL);
493 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200494
Michal Vaskof4b4d002021-08-23 12:16:02 +0200495 /* add also their modules deviating or augmenting them */
496 LY_ARRAY_FOR(mod->deviated_by, u) {
497 ret = ly_set_add(set, mod->deviated_by[u], 0, NULL);
498 LY_CHECK_GOTO(ret, cleanup);
499 }
500 LY_ARRAY_FOR(mod->augmented_by, u) {
501 ret = ly_set_add(set, mod->augmented_by[u], 0, NULL);
502 LY_CHECK_GOTO(ret, cleanup);
503 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200504
Michal Vaskof4b4d002021-08-23 12:16:02 +0200505 /* only top-level nodes are processed */
506 LYD_TREE_DFS_continue = 1;
507 }
508
509 LYD_TREE_DFS_END(elem, node);
Michal Vasko60ea6352020-06-29 13:39:39 +0200510 }
511 }
512
513 /* now write module count on 2 bytes */
514 LY_CHECK_GOTO(ret = lyb_write_number(set->count, 2, out, lybctx), cleanup);
515
516 /* and all the used models */
517 for (i = 0; i < set->count; ++i) {
518 LY_CHECK_GOTO(ret = lyb_print_model(out, set->objs[i], lybctx), cleanup);
519 }
520
521cleanup:
522 ly_set_free(set, NULL);
523 return ret;
524}
525
526/**
527 * @brief Print LYB magic number.
528 *
529 * @param[in] out Out structure.
Michal Vasko60ea6352020-06-29 13:39:39 +0200530 * @return LY_ERR value.
531 */
532static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200533lyb_print_magic_number(struct ly_out *out)
Michal Vasko60ea6352020-06-29 13:39:39 +0200534{
Michal Vasko60ea6352020-06-29 13:39:39 +0200535 /* 'l', 'y', 'b' - 0x6c7962 */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100536 char magic_number[] = {'l', 'y', 'b'};
Michal Vasko60ea6352020-06-29 13:39:39 +0200537
Radek Krejcidefd4d72020-12-01 12:20:30 +0100538 LY_CHECK_RET(ly_write_(out, magic_number, 3));
Michal Vasko60ea6352020-06-29 13:39:39 +0200539
540 return LY_SUCCESS;
541}
542
543/**
544 * @brief Print LYB header.
545 *
546 * @param[in] out Out structure.
Michal Vasko60ea6352020-06-29 13:39:39 +0200547 * @return LY_ERR value.
548 */
549static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200550lyb_print_header(struct ly_out *out)
Michal Vasko60ea6352020-06-29 13:39:39 +0200551{
Michal Vasko60ea6352020-06-29 13:39:39 +0200552 uint8_t byte = 0;
553
554 /* version, future flags */
555 byte |= LYB_VERSION_NUM;
556
Michal Vasko5233e962020-08-14 14:26:20 +0200557 LY_CHECK_RET(ly_write_(out, (char *)&byte, 1));
Michal Vasko60ea6352020-06-29 13:39:39 +0200558
559 return LY_SUCCESS;
560}
561
562/**
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100563 * @brief Print prefix data.
Michal Vasko60ea6352020-06-29 13:39:39 +0200564 *
565 * @param[in] out Out structure.
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100566 * @param[in] format Value prefix format.
Radek Krejci8df109d2021-04-23 12:19:08 +0200567 * @param[in] prefix_data Format-specific data for resolving any prefixes (see ::ly_resolve_prefix).
Michal Vasko60ea6352020-06-29 13:39:39 +0200568 * @param[in] lybctx LYB context.
569 * @return LY_ERR value.
570 */
571static LY_ERR
Radek Krejci8df109d2021-04-23 12:19:08 +0200572lyb_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 +0200573{
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100574 const struct ly_set *set;
575 const struct lyxml_ns *ns;
576 uint32_t i;
Michal Vasko60ea6352020-06-29 13:39:39 +0200577
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100578 switch (format) {
Radek Krejci8df109d2021-04-23 12:19:08 +0200579 case LY_VALUE_XML:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100580 set = prefix_data;
581 if (!set) {
582 /* no prefix data */
583 i = 0;
584 LY_CHECK_RET(lyb_write(out, (uint8_t *)&i, 1, lybctx));
585 break;
586 }
587 if (set->count > UINT8_MAX) {
588 LOGERR(lybctx->ctx, LY_EINT, "Maximum supported number of prefixes is %u.", UINT8_MAX);
589 return LY_EINT;
590 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200591
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100592 /* write number of prefixes on 1 byte */
Michal Vasko403beac2021-08-24 08:27:52 +0200593 LY_CHECK_RET(lyb_write_number(set->count, 1, out, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200594
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100595 /* write all the prefixes */
596 for (i = 0; i < set->count; ++i) {
597 ns = set->objs[i];
Michal Vasko60ea6352020-06-29 13:39:39 +0200598
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100599 /* prefix */
600 LY_CHECK_RET(lyb_write_string(ns->prefix, 0, 1, out, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200601
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100602 /* namespace */
603 LY_CHECK_RET(lyb_write_string(ns->uri, 0, 1, out, lybctx));
604 }
605 break;
Radek Krejci8df109d2021-04-23 12:19:08 +0200606 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +0200607 case LY_VALUE_LYB:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100608 /* nothing to print */
609 break;
610 default:
611 LOGINT_RET(lybctx->ctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200612 }
613
614 return LY_SUCCESS;
615}
616
617/**
aPiecek60d90af2021-09-09 12:32:38 +0200618 * @brief Print opaque node and its descendants.
Michal Vasko60ea6352020-06-29 13:39:39 +0200619 *
Michal Vasko60ea6352020-06-29 13:39:39 +0200620 * @param[in] out Out structure.
aPiecek60d90af2021-09-09 12:32:38 +0200621 * @param[in] opaq Node to print.
622 * @param[in] lyd_lybctx LYB context.
Michal Vasko60ea6352020-06-29 13:39:39 +0200623 * @return LY_ERR value.
624 */
625static LY_ERR
aPiecek60d90af2021-09-09 12:32:38 +0200626lyb_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 +0200627{
aPiecek60d90af2021-09-09 12:32:38 +0200628 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 */
aPiecek570d7ed2021-09-10 07:15:35 +0200655 LY_CHECK_RET(lyb_print_siblings(out, opaq->child, lyd_lybctx));
aPiecek60d90af2021-09-09 12:32:38 +0200656
Michal Vasko60ea6352020-06-29 13:39:39 +0200657 return LY_SUCCESS;
658}
659
660/**
aPiecek843157f2021-09-09 12:38:59 +0200661 * @brief Print anydata or anyxml node.
Michal Vasko60ea6352020-06-29 13:39:39 +0200662 *
663 * @param[in] anydata Node to print.
664 * @param[in] out Out structure.
aPiecek843157f2021-09-09 12:38:59 +0200665 * @param[in] lyd_lybctx LYB context.
Michal Vasko60ea6352020-06-29 13:39:39 +0200666 * @return LY_ERR value.
667 */
668static LY_ERR
aPiecek843157f2021-09-09 12:38:59 +0200669lyb_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 +0200670{
671 LY_ERR ret = LY_SUCCESS;
672 LYD_ANYDATA_VALUETYPE value_type;
673 int len;
674 char *buf = NULL;
675 const char *str;
676 struct ly_out *out2 = NULL;
aPiecek843157f2021-09-09 12:38:59 +0200677 struct lylyb_ctx *lybctx = lyd_lybctx->lybctx;
Michal Vasko60ea6352020-06-29 13:39:39 +0200678
679 if (anydata->value_type == LYD_ANYDATA_DATATREE) {
680 /* will be printed as a nested LYB data tree */
681 value_type = LYD_ANYDATA_LYB;
682 } else {
683 value_type = anydata->value_type;
684 }
685
aPiecek843157f2021-09-09 12:38:59 +0200686 /* write necessary basic data */
687 LY_CHECK_RET(lyb_print_node_header(out, (struct lyd_node *)anydata, lyd_lybctx));
688
Michal Vasko60ea6352020-06-29 13:39:39 +0200689 /* first byte is type */
Michal Vasko403beac2021-08-24 08:27:52 +0200690 LY_CHECK_GOTO(ret = lyb_write_number(value_type, sizeof value_type, out, lybctx), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200691
692 if (anydata->value_type == LYD_ANYDATA_DATATREE) {
693 /* print LYB data tree to memory */
694 LY_CHECK_GOTO(ret = ly_out_new_memory(&buf, 0, &out2), cleanup);
Radek Krejci7931b192020-06-25 17:05:03 +0200695 LY_CHECK_GOTO(ret = lyb_print_data(out2, anydata->value.tree, LYD_PRINT_WITHSIBLINGS), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200696
697 len = lyd_lyb_data_length(buf);
698 assert(len != -1);
699 str = buf;
700 } else if (anydata->value_type == LYD_ANYDATA_LYB) {
701 len = lyd_lyb_data_length(anydata->value.mem);
702 assert(len != -1);
703 str = anydata->value.mem;
704 } else {
705 len = strlen(anydata->value.str);
706 str = anydata->value.str;
707 }
708
709 /* followed by the content */
aPiecek570d7ed2021-09-10 07:15:35 +0200710 LY_CHECK_GOTO(ret = lyb_write_string(str, (size_t)len, 1, out, lybctx), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200711
712cleanup:
713 ly_out_free(out2, NULL, 1);
714 return ret;
715}
716
717/**
718 * @brief Print term node.
719 *
720 * @param[in] term Node to print.
721 * @param[in] out Out structure.
722 * @param[in] lybctx LYB context.
723 * @return LY_ERR value.
724 */
725static LY_ERR
aPieceka19ca152021-09-09 12:27:35 +0200726lyb_print_term_value(struct lyd_node_term *term, struct ly_out *out, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200727{
aPiecekaa5b70a2021-08-30 08:33:25 +0200728 LY_ERR ret = LY_SUCCESS;
aPiecekea304e32021-08-18 09:13:47 +0200729 ly_bool dynamic = 0;
730 void *value;
731 size_t value_len = 0;
aPiecekaa5b70a2021-08-30 08:33:25 +0200732 int32_t lyb_data_len;
733 lyplg_type_print_clb print;
aPiecekea304e32021-08-18 09:13:47 +0200734
735 assert(term->value.realtype && term->value.realtype->plugin && term->value.realtype->plugin->print &&
736 term->schema);
737
aPiecekaa5b70a2021-08-30 08:33:25 +0200738 /* Get length of LYB data to print. */
739 lyb_data_len = term->value.realtype->plugin->lyb_data_len;
aPiecekea304e32021-08-18 09:13:47 +0200740
aPiecekaa5b70a2021-08-30 08:33:25 +0200741 /* Get value and also print its length only if size is not fixed. */
742 print = term->value.realtype->plugin->print;
743 if (lyb_data_len < 0) {
744 /* Variable-length data. */
745
746 /* Get value and its length from plugin. */
747 value = (void *)print(term->schema->module->ctx, &term->value,
748 LY_VALUE_LYB, NULL, &dynamic, &value_len);
749 LY_CHECK_GOTO(ret, cleanup);
750
751 if (value_len > UINT32_MAX) {
752 LOGERR(lybctx->ctx, LY_EINT, "The maximum length of the LYB data "
753 "from a term node must not exceed %lu.", UINT32_MAX);
754 ret = LY_EINT;
755 goto cleanup;
756 }
757
758 /* Print the length of the data as 32-bit unsigned integer. */
759 ret = lyb_write_number(value_len, sizeof(uint32_t), out, lybctx);
760 LY_CHECK_GOTO(ret, cleanup);
761 } else {
762 /* Fixed-length data. */
763
764 /* Get value from plugin. */
765 value = (void *)print(term->schema->module->ctx, &term->value,
766 LY_VALUE_LYB, NULL, &dynamic, NULL);
767 LY_CHECK_GOTO(ret, cleanup);
768
769 /* Copy the length from the compiled node. */
770 value_len = lyb_data_len;
aPiecekea304e32021-08-18 09:13:47 +0200771 }
772
aPiecekaa5b70a2021-08-30 08:33:25 +0200773 /* Print value. */
aPiecekea304e32021-08-18 09:13:47 +0200774 if (value_len > 0) {
aPiecekaa5b70a2021-08-30 08:33:25 +0200775 /* Print the value simply as it is. */
aPiecekea304e32021-08-18 09:13:47 +0200776 ret = lyb_write(out, value, value_len, lybctx);
777 LY_CHECK_GOTO(ret, cleanup);
778 }
779
780cleanup:
781 if (dynamic) {
782 free(value);
783 }
784
785 return ret;
Michal Vasko60ea6352020-06-29 13:39:39 +0200786}
787
788/**
789 * @brief Print YANG node metadata.
790 *
791 * @param[in] out Out structure.
792 * @param[in] node Data node whose metadata to print.
793 * @param[in] lybctx LYB context.
794 * @return LY_ERR value.
795 */
796static LY_ERR
797lyb_print_metadata(struct ly_out *out, const struct lyd_node *node, struct lyd_lyb_ctx *lybctx)
798{
Michal Vasko60ea6352020-06-29 13:39:39 +0200799 uint8_t count = 0;
800 const struct lys_module *wd_mod = NULL;
801 struct lyd_meta *iter;
Michal Vasko60ea6352020-06-29 13:39:39 +0200802
803 /* with-defaults */
804 if (node->schema->nodetype & LYD_NODE_TERM) {
Radek Krejci7931b192020-06-25 17:05:03 +0200805 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 +0200806 ((lybctx->print_options & LYD_PRINT_WD_ALL_TAG) && lyd_is_default(node))) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200807 /* we have implicit OR explicit default node, print attribute only if context include with-defaults schema */
808 wd_mod = ly_ctx_get_module_latest(node->schema->module->ctx, "ietf-netconf-with-defaults");
809 }
810 }
811
812 /* count metadata */
813 if (wd_mod) {
814 ++count;
815 }
816 for (iter = node->meta; iter; iter = iter->next) {
817 if (count == UINT8_MAX) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200818 LOGERR(lybctx->lybctx->ctx, LY_EINT, "Maximum supported number of data node metadata is %u.", UINT8_MAX);
Michal Vasko60ea6352020-06-29 13:39:39 +0200819 return LY_EINT;
820 }
821 ++count;
822 }
823
824 /* write number of metadata on 1 byte */
Radek Krejci1798aae2020-07-14 13:26:06 +0200825 LY_CHECK_RET(lyb_write(out, &count, 1, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200826
827 if (wd_mod) {
828 /* write the "default" metadata */
aPiecek570d7ed2021-09-10 07:15:35 +0200829 LY_CHECK_RET(lyb_write_start_siblings(out, lybctx->lybctx));
Radek Krejci1798aae2020-07-14 13:26:06 +0200830 LY_CHECK_RET(lyb_print_model(out, wd_mod, lybctx->lybctx));
831 LY_CHECK_RET(lyb_write_string("default", 0, 1, out, lybctx->lybctx));
832 LY_CHECK_RET(lyb_write_string("true", 0, 0, out, lybctx->lybctx));
aPiecek570d7ed2021-09-10 07:15:35 +0200833 LY_CHECK_RET(lyb_write_stop_siblings(out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200834 }
835
836 /* write all the node metadata */
837 LY_LIST_FOR(node->meta, iter) {
aPiecek570d7ed2021-09-10 07:15:35 +0200838 /* each metadata is a sibling */
839 LY_CHECK_RET(lyb_write_start_siblings(out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200840
841 /* model */
Radek Krejci1798aae2020-07-14 13:26:06 +0200842 LY_CHECK_RET(lyb_print_model(out, iter->annotation->module, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200843
844 /* annotation name with length */
Radek Krejci1798aae2020-07-14 13:26:06 +0200845 LY_CHECK_RET(lyb_write_string(iter->name, 0, 1, out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200846
Michal Vasko60ea6352020-06-29 13:39:39 +0200847 /* metadata value */
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200848 LY_CHECK_RET(lyb_write_string(lyd_get_meta_value(iter), 0, 0, out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200849
aPiecek570d7ed2021-09-10 07:15:35 +0200850 /* finish metadata sibling */
851 LY_CHECK_RET(lyb_write_stop_siblings(out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200852 }
853
854 return LY_SUCCESS;
855}
856
857/**
858 * @brief Print opaque node attributes.
859 *
860 * @param[in] out Out structure.
861 * @param[in] node Opaque node whose attributes to print.
862 * @param[in] lybctx LYB context.
863 * @return LY_ERR value.
864 */
865static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200866lyb_print_attributes(struct ly_out *out, const struct lyd_node_opaq *node, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200867{
868 uint8_t count = 0;
Radek Krejci5536d282020-08-04 23:27:44 +0200869 struct lyd_attr *iter;
Michal Vasko60ea6352020-06-29 13:39:39 +0200870
871 for (iter = node->attr; iter; iter = iter->next) {
872 if (count == UINT8_MAX) {
873 LOGERR(lybctx->ctx, LY_EINT, "Maximum supported number of data node attributes is %u.", UINT8_MAX);
874 return LY_EINT;
875 }
876 ++count;
877 }
878
879 /* write number of attributes on 1 byte */
880 LY_CHECK_RET(lyb_write(out, &count, 1, lybctx));
881
882 /* write all the attributes */
883 LY_LIST_FOR(node->attr, iter) {
aPiecek570d7ed2021-09-10 07:15:35 +0200884 /* each attribute is a sibling */
885 LY_CHECK_RET(lyb_write_start_siblings(out, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200886
887 /* prefix */
Michal Vaskoad92b672020-11-12 13:11:31 +0100888 LY_CHECK_RET(lyb_write_string(iter->name.prefix, 0, 1, out, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200889
890 /* namespace */
Michal Vaskoad92b672020-11-12 13:11:31 +0100891 LY_CHECK_RET(lyb_write_string(iter->name.module_name, 0, 1, out, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200892
893 /* name */
Michal Vaskoad92b672020-11-12 13:11:31 +0100894 LY_CHECK_RET(lyb_write_string(iter->name.name, 0, 1, out, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200895
Michal Vasko60ea6352020-06-29 13:39:39 +0200896 /* format */
897 LY_CHECK_RET(lyb_write_number(iter->format, 1, out, lybctx));
898
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100899 /* value prefixes */
900 LY_CHECK_RET(lyb_print_prefix_data(out, iter->format, iter->val_prefix_data, lybctx));
901
Michal Vasko60ea6352020-06-29 13:39:39 +0200902 /* value */
903 LY_CHECK_RET(lyb_write_string(iter->value, 0, 0, out, lybctx));
904
aPiecek570d7ed2021-09-10 07:15:35 +0200905 /* finish attribute sibling */
906 LY_CHECK_RET(lyb_write_stop_siblings(out, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200907 }
908
909 return LY_SUCCESS;
910}
911
912/**
aPiecek270f72b2021-09-09 11:39:31 +0200913 * @brief Print header for non-opaq node.
914 *
915 * @param[in] out Out structure.
916 * @param[in] node Current data node to print.
917 * @param[in] lybctx LYB context.
918 * @return LY_ERR value.
919 */
920static LY_ERR
921lyb_print_node_header(struct ly_out *out, const struct lyd_node *node, struct lyd_lyb_ctx *lybctx)
922{
923 /* write any metadata */
924 LY_CHECK_RET(lyb_print_metadata(out, node, lybctx));
925
926 /* write node flags */
927 LY_CHECK_RET(lyb_write_number(node->flags, sizeof node->flags, out, lybctx->lybctx));
928
929 return LY_SUCCESS;
930}
931
932/**
Michal Vasko60ea6352020-06-29 13:39:39 +0200933 * @brief Print schema node hash.
934 *
935 * @param[in] out Out structure.
936 * @param[in] schema Schema node whose hash to print.
937 * @param[in,out] sibling_ht Cached hash table for these siblings, created if NULL.
938 * @param[in] lybctx LYB context.
939 * @return LY_ERR value.
940 */
941static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200942lyb_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 +0200943{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200944 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +0200945 uint32_t i;
946 LYB_HASH hash;
947 struct lyd_lyb_sib_ht *sib_ht;
948 struct lysc_node *first_sibling;
949
950 if (!schema) {
951 /* opaque node, write empty hash */
952 hash = 0;
953 LY_CHECK_RET(lyb_write(out, &hash, sizeof hash, lybctx));
954 return LY_SUCCESS;
955 }
956
957 /* create whole sibling HT if not already created and saved */
958 if (!*sibling_ht) {
Michal Vasko1e0a80a2020-12-09 18:12:51 +0100959 /* get first schema data sibling */
960 first_sibling = (struct lysc_node *)lys_getnext(NULL, lysc_data_parent(schema), schema->module->compiled,
Michal Vaskod1e53b92021-01-28 13:11:06 +0100961 (schema->flags & LYS_IS_OUTPUT) ? LYS_GETNEXT_OUTPUT : 0);
Michal Vasko60ea6352020-06-29 13:39:39 +0200962 LY_ARRAY_FOR(lybctx->sib_hts, u) {
963 if (lybctx->sib_hts[u].first_sibling == first_sibling) {
964 /* we have already created a hash table for these siblings */
965 *sibling_ht = lybctx->sib_hts[u].ht;
966 break;
967 }
968 }
969
970 if (!*sibling_ht) {
971 /* we must create sibling hash table */
972 LY_CHECK_RET(lyb_hash_siblings(first_sibling, sibling_ht));
973
974 /* and save it */
975 LY_ARRAY_NEW_RET(lybctx->ctx, lybctx->sib_hts, sib_ht, LY_EMEM);
976
977 sib_ht->first_sibling = first_sibling;
978 sib_ht->ht = *sibling_ht;
979 }
980 }
981
982 /* get our hash */
983 LY_CHECK_RET(lyb_hash_find(*sibling_ht, schema, &hash));
984
985 /* write the hash */
986 LY_CHECK_RET(lyb_write(out, &hash, sizeof hash, lybctx));
987
988 if (hash & LYB_HASH_COLLISION_ID) {
989 /* no collision for this hash, we are done */
990 return LY_SUCCESS;
991 }
992
993 /* written hash was a collision, write also all the preceding hashes */
Radek Krejci1e008d22020-08-17 11:37:37 +0200994 for (i = 0; !(hash & (LYB_HASH_COLLISION_ID >> i)); ++i) {}
Michal Vasko60ea6352020-06-29 13:39:39 +0200995
Michal Vaskod989ba02020-08-24 10:59:24 +0200996 for ( ; i; --i) {
Michal Vasko11f76c82021-04-15 14:36:14 +0200997 hash = lyb_get_hash(schema, i - 1);
Michal Vasko60ea6352020-06-29 13:39:39 +0200998 if (!hash) {
999 return LY_EINT;
1000 }
1001 assert(hash & (LYB_HASH_COLLISION_ID >> (i - 1)));
1002
1003 LY_CHECK_RET(lyb_write(out, &hash, sizeof hash, lybctx));
1004 }
1005
1006 return LY_SUCCESS;
1007}
1008
1009/**
aPiecek570d7ed2021-09-10 07:15:35 +02001010 * @brief Print node with childs.
Michal Vasko60ea6352020-06-29 13:39:39 +02001011 *
1012 * @param[in] out Out structure.
aPiecek570d7ed2021-09-10 07:15:35 +02001013 * @param[in] node Root node of the sibling to print.
Michal Vasko60ea6352020-06-29 13:39:39 +02001014 * @param[in] lybctx LYB context.
1015 * @return LY_ERR value.
1016 */
1017static LY_ERR
aPiecek570d7ed2021-09-10 07:15:35 +02001018lyb_print_node(struct ly_out *out, const struct lyd_node *node, struct lyd_lyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +02001019{
Michal Vasko60ea6352020-06-29 13:39:39 +02001020 /* write node content */
1021 if (!node->schema) {
aPiecek60d90af2021-09-09 12:32:38 +02001022 LY_CHECK_RET(lyb_print_node_opaq(out, (struct lyd_node_opaq *)node, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +02001023 } else if (node->schema->nodetype & LYD_NODE_INNER) {
aPiecek570d7ed2021-09-10 07:15:35 +02001024 /* write necessary basic data */
aPiecek270f72b2021-09-09 11:39:31 +02001025 LY_CHECK_RET(lyb_print_node_header(out, node, lybctx));
aPiecek570d7ed2021-09-10 07:15:35 +02001026
1027 /* recursively write all the descendants */
1028 LY_CHECK_RET(lyb_print_siblings(out, lyd_child(node), lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +02001029 } else if (node->schema->nodetype & LYD_NODE_TERM) {
aPiecek270f72b2021-09-09 11:39:31 +02001030 LY_CHECK_RET(lyb_print_node_header(out, node, lybctx));
aPieceka19ca152021-09-09 12:27:35 +02001031 LY_CHECK_RET(lyb_print_term_value((struct lyd_node_term *)node, out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +02001032 } else if (node->schema->nodetype & LYD_NODE_ANY) {
aPiecek843157f2021-09-09 12:38:59 +02001033 LY_CHECK_RET(lyb_print_node_any(out, (struct lyd_node_any *)node, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +02001034 } else {
Radek Krejci1798aae2020-07-14 13:26:06 +02001035 LOGINT_RET(lybctx->lybctx->ctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001036 }
1037
aPiecek570d7ed2021-09-10 07:15:35 +02001038 return LY_SUCCESS;
1039}
1040
1041/**
1042 * @brief Print siblings.
1043 *
1044 * @param[in] out Out structure.
1045 * @param[in] node Current data node to print.
1046 * @param[in] lybctx LYB context.
1047 * @return LY_ERR value.
1048 */
1049static LY_ERR
1050lyb_print_siblings(struct ly_out *out, const struct lyd_node *node, struct lyd_lyb_ctx *lybctx)
1051{
1052 struct hash_table *sibling_ht = NULL;
1053 const struct lys_module *prev_mod = NULL;
1054 ly_bool top_level;
1055 uint8_t zero = 0;
1056
1057 if (!node) {
1058 lyb_write(out, &zero, 1, lybctx->lybctx);
1059 return LY_SUCCESS;
Michal Vasko60ea6352020-06-29 13:39:39 +02001060 }
1061
aPiecek570d7ed2021-09-10 07:15:35 +02001062 top_level = !LY_ARRAY_COUNT(lybctx->lybctx->siblings);
1063
1064 LY_CHECK_RET(lyb_write_start_siblings(out, lybctx->lybctx));
1065
1066 /* write all the siblings */
1067 LY_LIST_FOR(node, node) {
1068
1069 /* do not reuse sibling hash tables from different modules */
1070 if (top_level && (!node->schema || (node->schema->module != prev_mod))) {
1071 sibling_ht = NULL;
1072 prev_mod = node->schema ? node->schema->module : NULL;
1073 }
1074
1075 /* write model info first, for all opaque and top-level nodes */
1076 if (!node->schema && (!node->parent || !node->parent->schema)) {
1077 LY_CHECK_RET(lyb_print_model(out, NULL, lybctx->lybctx));
1078 } else if (node->schema && !lysc_data_parent(node->schema)) {
1079 LY_CHECK_RET(lyb_print_model(out, node->schema->module, lybctx->lybctx));
1080 }
1081
1082 /* write schema hash */
1083 LY_CHECK_RET(lyb_print_schema_hash(out, (struct lysc_node *)node->schema, &sibling_ht, lybctx->lybctx));
1084
1085 LY_CHECK_RET(lyb_print_node(out, node, lybctx));
1086
1087 if (top_level && !(lybctx->print_options & LYD_PRINT_WITHSIBLINGS)) {
1088 break;
1089 }
1090 }
1091
1092 LY_CHECK_RET(lyb_write_stop_siblings(out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +02001093
1094 return LY_SUCCESS;
1095}
1096
1097LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001098lyb_print_data(struct ly_out *out, const struct lyd_node *root, uint32_t options)
Michal Vasko60ea6352020-06-29 13:39:39 +02001099{
1100 LY_ERR ret = LY_SUCCESS;
1101 uint8_t zero = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +02001102 struct lyd_lyb_ctx *lybctx;
Michal Vaskob7be7a82020-08-20 09:09:04 +02001103 const struct ly_ctx *ctx = root ? LYD_CTX(root) : NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +02001104
Radek Krejci1798aae2020-07-14 13:26:06 +02001105 lybctx = calloc(1, sizeof *lybctx);
1106 LY_CHECK_ERR_RET(!lybctx, LOGMEM(ctx), LY_EMEM);
1107 lybctx->lybctx = calloc(1, sizeof *lybctx->lybctx);
1108 LY_CHECK_ERR_RET(!lybctx, LOGMEM(ctx), LY_EMEM);
1109
1110 lybctx->print_options = options;
Michal Vasko60ea6352020-06-29 13:39:39 +02001111 if (root) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001112 lybctx->lybctx->ctx = ctx;
Michal Vasko60ea6352020-06-29 13:39:39 +02001113
1114 if (root->schema && lysc_data_parent(root->schema)) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001115 LOGERR(lybctx->lybctx->ctx, LY_EINVAL, "LYB printer supports only printing top-level nodes.");
Michal Vasko9acaf492020-08-13 09:05:58 +02001116 ret = LY_EINVAL;
1117 goto cleanup;
Michal Vasko60ea6352020-06-29 13:39:39 +02001118 }
1119 }
1120
1121 /* LYB magic number */
Michal Vasko63f3d842020-07-08 10:10:14 +02001122 LY_CHECK_GOTO(ret = lyb_print_magic_number(out), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001123
1124 /* LYB header */
Michal Vasko63f3d842020-07-08 10:10:14 +02001125 LY_CHECK_GOTO(ret = lyb_print_header(out), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001126
1127 /* all used models */
Radek Krejci1798aae2020-07-14 13:26:06 +02001128 LY_CHECK_GOTO(ret = lyb_print_data_models(out, root, lybctx->lybctx), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001129
aPiecek570d7ed2021-09-10 07:15:35 +02001130 ret = lyb_print_siblings(out, root, lybctx);
1131 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001132
1133 /* ending zero byte */
Radek Krejci1798aae2020-07-14 13:26:06 +02001134 LY_CHECK_GOTO(ret = lyb_write(out, &zero, sizeof zero, lybctx->lybctx), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001135
1136cleanup:
Radek Krejci1798aae2020-07-14 13:26:06 +02001137 lyd_lyb_ctx_free((struct lyd_ctx *)lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001138 return ret;
1139}