blob: 1b89e392ef7d90e05ca57ce9293e60a1f68c1b03 [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>
18#include <stdio.h>
19#include <stdint.h>
20#include <stdlib.h>
21#include <string.h>
Radek Krejciad97c5f2020-06-30 09:19:28 +020022#include <sys/types.h>
Michal Vasko60ea6352020-06-29 13:39:39 +020023
24#include "common.h"
25#include "compat.h"
Radek Krejciad97c5f2020-06-30 09:19:28 +020026#include "config.h"
27#include "context.h"
28#include "hash_table.h"
Michal Vasko60ea6352020-06-29 13:39:39 +020029#include "log.h"
Radek Krejci7931b192020-06-25 17:05:03 +020030#include "parser_data.h"
Michal Vasko60ea6352020-06-29 13:39:39 +020031#include "printer.h"
Radek Krejciad97c5f2020-06-30 09:19:28 +020032#include "printer_data.h"
Michal Vasko60ea6352020-06-29 13:39:39 +020033#include "printer_internal.h"
Radek Krejciad97c5f2020-06-30 09:19:28 +020034#include "set.h"
35#include "tree.h"
Michal Vasko60ea6352020-06-29 13:39:39 +020036#include "tree_data_internal.h"
37#include "tree_schema.h"
38#include "tree_schema_internal.h"
39
40/**
41 * @brief Hash table equal callback for checking hash equality only.
42 */
43static int
44lyb_hash_equal_cb(void *UNUSED(val1_p), void *UNUSED(val2_p), int UNUSED(mod), void *UNUSED(cb_data))
45{
46 /* for this purpose, if hash matches, the value does also, we do not want 2 values to have the same hash */
47 return 1;
48}
49
50/**
51 * @brief Hash table equal callback for checking value pointer equality only.
52 */
53static int
54lyb_ptr_equal_cb(void *val1_p, void *val2_p, int UNUSED(mod), void *UNUSED(cb_data))
55{
56 struct lysc_node *val1 = *(struct lysc_node **)val1_p;
57 struct lysc_node *val2 = *(struct lysc_node **)val2_p;
58
59 if (val1 == val2) {
60 return 1;
61 }
62 return 0;
63}
64
65/**
66 * @brief Check that sibling collision hash is safe to insert into hash table.
67 *
68 * @param[in] ht Hash table.
69 * @param[in] sibling Hashed sibling.
70 * @param[in] ht_col_id Sibling hash collision ID.
71 * @param[in] compare_col_id Last collision ID to compare with.
72 * @return LY_SUCCESS when the whole hash sequence does not collide,
73 * @return LY_EEXIST when the whole hash sequence sollides.
74 */
75static LY_ERR
76lyb_hash_sequence_check(struct hash_table *ht, struct lysc_node *sibling, int ht_col_id, int compare_col_id)
77{
78 int j;
79 struct lysc_node **col_node;
80
81 /* get the first node inserted with last hash col ID ht_col_id */
82 if (lyht_find(ht, &sibling, lyb_hash(sibling, ht_col_id), (void **)&col_node)) {
83 /* there is none. valid situation */
84 return LY_SUCCESS;
85 }
86
87 lyht_set_cb(ht, lyb_ptr_equal_cb);
88 do {
89 for (j = compare_col_id; j > -1; --j) {
90 if (lyb_hash(sibling, j) != lyb_hash(*col_node, j)) {
91 /* one non-colliding hash */
92 break;
93 }
94 }
95 if (j == -1) {
96 /* all whole hash sequences of nodes inserted with last hash col ID compare_col_id collide */
97 lyht_set_cb(ht, lyb_hash_equal_cb);
98 return LY_EEXIST;
99 }
100
101 /* get next node inserted with last hash col ID ht_col_id */
102 } while (!lyht_find_next(ht, col_node, lyb_hash(*col_node, ht_col_id), (void **)&col_node));
103
104 lyht_set_cb(ht, lyb_hash_equal_cb);
105 return LY_SUCCESS;
106}
107
108/**
109 * @brief Hash all the siblings and add them also into a separate hash table.
110 *
111 * @param[in] sibling Any sibling in all the siblings on one level.
112 * @param[out] ht_p Created hash table.
113 * @return LY_ERR value.
114 */
115static LY_ERR
116lyb_hash_siblings(struct lysc_node *sibling, struct hash_table **ht_p)
117{
118 struct hash_table *ht;
119 const struct lysc_node *parent;
120 const struct lys_module *mod;
121 int i, j;
122
123 ht = lyht_new(1, sizeof(struct lysc_node *), lyb_hash_equal_cb, NULL, 1);
124 LY_CHECK_ERR_RET(!ht, LOGMEM(sibling->module->ctx), LY_EMEM);
125
126 parent = lysc_data_parent(sibling);
127 mod = sibling->module;
128
129 sibling = NULL;
130 /* ignore features so that their state does not affect hashes */
131 while ((sibling = (struct lysc_node *)lys_getnext(sibling, parent, mod->compiled, LYS_GETNEXT_NOSTATECHECK))) {
132 /* find the first non-colliding hash (or specifically non-colliding hash sequence) */
133 for (i = 0; i < LYB_HASH_BITS; ++i) {
134 /* check that we are not colliding with nodes inserted with a lower collision ID than ours */
135 for (j = i - 1; j > -1; --j) {
136 if (lyb_hash_sequence_check(ht, sibling, j, i)) {
137 break;
138 }
139 }
140 if (j > -1) {
141 /* some check failed, we must use a higher collision ID */
142 continue;
143 }
144
145 /* try to insert node with the current collision ID */
146 if (!lyht_insert_with_resize_cb(ht, &sibling, lyb_hash(sibling, i), lyb_ptr_equal_cb, NULL)) {
147 /* success, no collision */
148 break;
149 }
150
151 /* make sure we really cannot insert it with this hash col ID (meaning the whole hash sequence is colliding) */
152 if (i && !lyb_hash_sequence_check(ht, sibling, i, i)) {
153 /* it can be inserted after all, even though there is already a node with the same last collision ID */
154 lyht_set_cb(ht, lyb_ptr_equal_cb);
155 if (lyht_insert(ht, &sibling, lyb_hash(sibling, i), NULL)) {
156 LOGINT(sibling->module->ctx);
157 lyht_set_cb(ht, lyb_hash_equal_cb);
158 lyht_free(ht);
159 return LY_EINT;
160 }
161 lyht_set_cb(ht, lyb_hash_equal_cb);
162 break;
163 }
164 /* there is still another colliding schema node with the same hash sequence, try higher collision ID */
165 }
166
167 if (i == LYB_HASH_BITS) {
168 /* wow */
169 LOGINT(sibling->module->ctx);
170 lyht_free(ht);
171 return LY_EINT;
172 }
173 }
174
175 /* change val equal callback so that the HT is usable for finding value hashes */
176 lyht_set_cb(ht, lyb_ptr_equal_cb);
177
178 *ht_p = ht;
179 return LY_SUCCESS;
180}
181
182/**
183 * @brief Find node hash in a hash table.
184 *
185 * @param[in] ht Hash table to search in.
186 * @param[in] node Node to find.
187 * @param[out] hash_p First non-colliding hash found.
188 * @return LY_ERR value.
189 */
190static LY_ERR
191lyb_hash_find(struct hash_table *ht, struct lysc_node *node, LYB_HASH *hash_p)
192{
193 LYB_HASH hash;
194 uint32_t i;
195
196 for (i = 0; i < LYB_HASH_BITS; ++i) {
197 hash = lyb_hash(node, i);
198 if (!hash) {
199 LOGINT_RET(node->module->ctx);
200 }
201
202 if (!lyht_find(ht, &node, hash, NULL)) {
203 /* success, no collision */
204 break;
205 }
206 }
207 /* cannot happen, we already calculated the hash */
208 if (i == LYB_HASH_BITS) {
209 LOGINT_RET(node->module->ctx);
210 }
211
212 *hash_p = hash;
213 return LY_SUCCESS;
214}
215
216/**
217 * @brief Write LYB data fully handling the metadata.
218 *
219 * @param[in] out Out structure.
220 * @param[in] buf Source buffer.
221 * @param[in] count Number of bytes to write.
222 * @param[in] lybctx LYB context.
223 * @return LY_ERR value.
224 */
225static LY_ERR
226lyb_write(struct ly_out *out, const uint8_t *buf, size_t count, struct lyd_lyb_ctx *lybctx)
227{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200228 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +0200229 struct lyd_lyb_subtree *full, *iter;
230 ssize_t r, to_write;
231 uint8_t meta_buf[LYB_META_BYTES];
232
233 while (1) {
234 /* check for full data chunks */
235 to_write = count;
236 full = NULL;
237 LY_ARRAY_FOR(lybctx->subtrees, u) {
238 /* we want the innermost chunks resolved first, so replace previous full chunks */
239 if (lybctx->subtrees[u].written + to_write >= LYB_SIZE_MAX) {
240 /* full chunk, do not write more than allowed */
241 to_write = LYB_SIZE_MAX - lybctx->subtrees[u].written;
242 full = &lybctx->subtrees[u];
243 }
244 }
245
246 if (!full && !count) {
247 break;
248 }
249
250 /* we are actually writing some data, not just finishing another chunk */
251 if (to_write) {
252 r = ly_write(out, (char *)buf, to_write);
253 if (r < to_write) {
254 return LY_ESYS;
255 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200256
257 LY_ARRAY_FOR(lybctx->subtrees, u) {
258 /* increase all written counters */
259 lybctx->subtrees[u].written += r;
260 assert(lybctx->subtrees[u].written <= LYB_SIZE_MAX);
261 }
262 /* decrease count/buf */
263 count -= r;
264 buf += r;
265 }
266
267 if (full) {
268 /* write the meta information (inner chunk count and chunk size) */
269 meta_buf[0] = full->written & 0xFF;
270 meta_buf[1] = full->inner_chunks & 0xFF;
271
272 r = ly_write_skipped(out, full->position, (char *)meta_buf, LYB_META_BYTES);
273 if (r < 0) {
274 return LY_ESYS;
275 }
276 /* these bytes were already counted */
277
278 /* zero written and inner chunks */
279 full->written = 0;
280 full->inner_chunks = 0;
281
282 /* skip space for another chunk size */
283 r = ly_write_skip(out, LYB_META_BYTES, &full->position);
284 if (r < LYB_META_BYTES) {
285 return LY_ESYS;
286 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200287
288 /* increase inner chunk count */
289 for (iter = &lybctx->subtrees[0]; iter != full; ++iter) {
290 if (iter->inner_chunks == LYB_INCHUNK_MAX) {
291 LOGINT(lybctx->ctx);
292 return LY_EINT;
293 }
294 ++iter->inner_chunks;
295 }
296 }
297 }
298
299 return LY_SUCCESS;
300}
301
302/**
303 * @brief Stop the current subtree - write its final metadata.
304 *
305 * @param[in] out Out structure.
306 * @param[in] lybctx LYB context.
307 * @return LY_ERR value.
308 */
309static LY_ERR
310lyb_write_stop_subtree(struct ly_out *out, struct lyd_lyb_ctx *lybctx)
311{
312 ssize_t r;
313 uint8_t meta_buf[LYB_META_BYTES];
314
315 /* write the meta chunk information */
316 meta_buf[0] = LYB_LAST_SUBTREE(lybctx).written & 0xFF;
317 meta_buf[1] = LYB_LAST_SUBTREE(lybctx).inner_chunks & 0xFF;
318
319 r = ly_write_skipped(out, LYB_LAST_SUBTREE(lybctx).position, (char *)&meta_buf, LYB_META_BYTES);
320 if (r < 0) {
321 return LY_ESYS;
322 }
323 /* do not count these bytes */
324
325 LY_ARRAY_DECREMENT(lybctx->subtrees);
326 return LY_SUCCESS;
327}
328
329/**
330 * @brief Start a new subtree - skip bytes for its metadata.
331 *
332 * @param[in] out Out structure.
333 * @param[in] lybctx LYB context.
334 * @return LY_ERR value.
335 */
336static LY_ERR
337lyb_write_start_subtree(struct ly_out *out, struct lyd_lyb_ctx *lybctx)
338{
339 ssize_t r;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200340 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +0200341
342 if (!lybctx->subtrees) {
Radek Krejcif6d14cb2020-07-02 16:11:45 +0200343 assert(lybctx->subtree_size == 0);
Michal Vasko60ea6352020-06-29 13:39:39 +0200344 u = 0;
345 } else {
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200346 u = LY_ARRAY_COUNT(lybctx->subtrees);
Michal Vasko60ea6352020-06-29 13:39:39 +0200347 }
348 if (u == lybctx->subtree_size) {
349 LY_ARRAY_CREATE_RET(lybctx->ctx, lybctx->subtrees, u + LYB_SUBTREE_STEP, LY_EMEM);
350 lybctx->subtree_size = u + LYB_SUBTREE_STEP;
351 }
352
353 LY_ARRAY_INCREMENT(lybctx->subtrees);
354 LYB_LAST_SUBTREE(lybctx).written = 0;
355 LYB_LAST_SUBTREE(lybctx).inner_chunks = 0;
356
357 /* another inner chunk */
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200358 for (u = 0; u < LY_ARRAY_COUNT(lybctx->subtrees) - 1; ++u) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200359 if (lybctx->subtrees[u].inner_chunks == LYB_INCHUNK_MAX) {
360 LOGINT(lybctx->ctx);
361 return -1;
362 }
363 ++lybctx->subtrees[u].inner_chunks;
364 }
365
366 r = ly_write_skip(out, LYB_META_BYTES, &LYB_LAST_SUBTREE(lybctx).position);
367 if (r < LYB_META_BYTES) {
368 return LY_ESYS;
369 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200370
371 return LY_SUCCESS;
372}
373
374/**
375 * @brief Write a number.
376 *
377 * @param[in] num Number to write.
378 * @param[in] bytes Actual accessible bytes of @p num.
379 * @param[in] out Out structure.
380 * @param[in] lybctx LYB context.
381 * @return LY_ERR value.
382 */
383static LY_ERR
384lyb_write_number(uint64_t num, size_t bytes, struct ly_out *out, struct lyd_lyb_ctx *lybctx)
385{
386 /* correct byte order */
387 num = htole64(num);
388
389 return lyb_write(out, (uint8_t *)&num, bytes, lybctx);
390}
391
392/**
393 * @brief Write a string.
394 *
395 * @param[in] str String to write.
396 * @param[in] str_len Length of @p str.
397 * @param[in] with_length Whether to precede the string with its length.
398 * @param[in] out Out structure.
399 * @param[in] lybctx LYB context.
400 * @return LY_ERR value.
401 */
402static LY_ERR
403lyb_write_string(const char *str, size_t str_len, int with_length, struct ly_out *out, struct lyd_lyb_ctx *lybctx)
404{
Michal Vasko60ea6352020-06-29 13:39:39 +0200405 if (!str) {
406 str = "";
407 }
408 if (!str_len) {
409 str_len = strlen(str);
410 }
411
412 if (with_length) {
413 /* print length on 2 bytes */
414 if (str_len > UINT16_MAX) {
415 LOGINT(lybctx->ctx);
416 return LY_EINT;
417 }
418 LY_CHECK_RET(lyb_write_number(str_len, 2, out, lybctx));
419 }
420
Michal Vasko63f3d842020-07-08 10:10:14 +0200421 LY_CHECK_RET(lyb_write(out, (const uint8_t *)str, str_len, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200422
423 return LY_SUCCESS;
424}
425
426/**
427 * @brief Print YANG module info.
428 *
429 * @param[in] out Out structure.
430 * @param[in] mod Module to print.
431 * @param[in] lybctx LYB context.
432 * @return LY_ERR value.
433 */
434static LY_ERR
435lyb_print_model(struct ly_out *out, const struct lys_module *mod, struct lyd_lyb_ctx *lybctx)
436{
437 int r;
438 uint16_t revision;
439
440 /* model name length and model name */
441 if (mod) {
442 LY_CHECK_RET(lyb_write_string(mod->name, 0, 1, out, lybctx));
443 } else {
444 LY_CHECK_RET(lyb_write_string("", 0, 1, out, lybctx));
445 }
446
447 /* model revision as XXXX XXXX XXXX XXXX (2B) (year is offset from 2000)
448 * YYYY YYYM MMMD DDDD */
449 revision = 0;
450 if (mod && mod->revision) {
451 r = atoi(mod->revision);
452 r -= 2000;
453 r <<= 9;
454
455 revision |= r;
456
457 r = atoi(mod->revision + 5);
458 r <<= 5;
459
460 revision |= r;
461
462 r = atoi(mod->revision + 8);
463
464 revision |= r;
465 }
466 LY_CHECK_RET(lyb_write_number(revision, sizeof revision, out, lybctx));
467
468 return LY_SUCCESS;
469}
470
471/**
472 * @brief Print all used YANG modules.
473 *
474 * @param[in] out Out structure.
475 * @param[in] root Data root.
476 * @param[in] lybctx LYB context.
477 * @return LY_ERR value.
478 */
479static LY_ERR
480lyb_print_data_models(struct ly_out *out, const struct lyd_node *root, struct lyd_lyb_ctx *lybctx)
481{
482 struct ly_set *set;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200483 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +0200484 LY_ERR ret = LY_SUCCESS;
485 struct lys_module *mod;
486 const struct lyd_node *node;
487 uint32_t i;
488
489 set = ly_set_new();
490 LY_CHECK_RET(!set, LY_EMEM);
491
492 /* collect all data node modules */
493 LY_LIST_FOR(root, node) {
494 if (!node->schema) {
495 continue;
496 }
497
498 mod = node->schema->module;
499 ly_set_add(set, mod, 0);
500
501 /* add also their modules deviating or augmenting them */
502 LY_ARRAY_FOR(mod->compiled->deviated_by, u) {
503 ly_set_add(set, mod->compiled->deviated_by[u], 0);
504 }
505 LY_ARRAY_FOR(mod->compiled->augmented_by, u) {
506 ly_set_add(set, mod->compiled->augmented_by[u], 0);
507 }
508 }
509
510 /* now write module count on 2 bytes */
511 LY_CHECK_GOTO(ret = lyb_write_number(set->count, 2, out, lybctx), cleanup);
512
513 /* and all the used models */
514 for (i = 0; i < set->count; ++i) {
515 LY_CHECK_GOTO(ret = lyb_print_model(out, set->objs[i], lybctx), cleanup);
516 }
517
518cleanup:
519 ly_set_free(set, NULL);
520 return ret;
521}
522
523/**
524 * @brief Print LYB magic number.
525 *
526 * @param[in] out Out structure.
Michal Vasko60ea6352020-06-29 13:39:39 +0200527 * @return LY_ERR value.
528 */
529static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200530lyb_print_magic_number(struct ly_out *out)
Michal Vasko60ea6352020-06-29 13:39:39 +0200531{
532 int r;
533 uint32_t magic_number;
534
535 /* 'l', 'y', 'b' - 0x6c7962 */
536 ((char *)&magic_number)[0] = 'l';
537 ((char *)&magic_number)[1] = 'y';
538 ((char *)&magic_number)[2] = 'b';
539
540 r = ly_write(out, (char *)&magic_number, 3);
541 if (r < 3) {
542 return LY_ESYS;
543 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200544
545 return LY_SUCCESS;
546}
547
548/**
549 * @brief Print LYB header.
550 *
551 * @param[in] out Out structure.
Michal Vasko60ea6352020-06-29 13:39:39 +0200552 * @return LY_ERR value.
553 */
554static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200555lyb_print_header(struct ly_out *out)
Michal Vasko60ea6352020-06-29 13:39:39 +0200556{
557 int r;
558 uint8_t byte = 0;
559
560 /* version, future flags */
561 byte |= LYB_VERSION_NUM;
562
563 r = ly_write(out, (char *)&byte, 1);
564 if (r < 1) {
565 return LY_ESYS;
566 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200567
568 return LY_SUCCESS;
569}
570
571/**
572 * @brief Print opaque prefixes.
573 *
574 * @param[in] out Out structure.
575 * @param[in] prefs Prefixes to print.
576 * @param[in] lybctx LYB context.
577 * @return LY_ERR value.
578 */
579static LY_ERR
580lyb_print_opaq_prefixes(struct ly_out *out, const struct ly_prefix *prefs, struct lyd_lyb_ctx *lybctx)
581{
582 uint8_t count;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200583 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +0200584
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200585 if (prefs && (LY_ARRAY_COUNT(prefs) > UINT8_MAX)) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200586 LOGERR(lybctx->ctx, LY_EINT, "Maximum supported number of prefixes is %u.", UINT8_MAX);
587 return LY_EINT;
588 }
589
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200590 count = prefs ? LY_ARRAY_COUNT(prefs) : 0;
Michal Vasko60ea6352020-06-29 13:39:39 +0200591
592 /* write number of prefixes on 1 byte */
593 LY_CHECK_RET(lyb_write(out, &count, 1, lybctx));
594
595 /* write all the prefixes */
596 LY_ARRAY_FOR(prefs, u) {
597 /* prefix */
598 LY_CHECK_RET(lyb_write_string(prefs[u].pref, 0, 1, out, lybctx));
599
600 /* namespace */
601 LY_CHECK_RET(lyb_write_string(prefs[u].ns, 0, 1, out, lybctx));
602 }
603
604 return LY_SUCCESS;
605}
606
607/**
608 * @brief Print opaque node.
609 *
610 * @param[in] opaq Node to print.
611 * @param[in] out Out structure.
612 * @param[in] lybctx LYB context.
613 * @return LY_ERR value.
614 */
615static LY_ERR
616lyb_print_opaq(struct lyd_node_opaq *opaq, struct ly_out *out, struct lyd_lyb_ctx *lybctx)
617{
618 /* prefix */
619 LY_CHECK_RET(lyb_write_string(opaq->prefix.pref, 0, 1, out, lybctx));
620
621 /* namespace */
622 LY_CHECK_RET(lyb_write_string(opaq->prefix.ns, 0, 1, out, lybctx));
623
624 /* name */
625 LY_CHECK_RET(lyb_write_string(opaq->name, 0, 1, out, lybctx));
626
627 /* value prefixes */
628 LY_CHECK_RET(lyb_print_opaq_prefixes(out, opaq->val_prefs, lybctx));
629
630 /* format */
631 LY_CHECK_RET(lyb_write_number(opaq->format, 1, out, lybctx));
632
633 /* value */
634 LY_CHECK_RET(lyb_write_string(opaq->value, 0, 0, out, lybctx));
635
636 return LY_SUCCESS;
637}
638
639/**
640 * @brief Print anydata node.
641 *
642 * @param[in] anydata Node to print.
643 * @param[in] out Out structure.
644 * @param[in] lybctx LYB context.
645 * @return LY_ERR value.
646 */
647static LY_ERR
648lyb_print_anydata(struct lyd_node_any *anydata, struct ly_out *out, struct lyd_lyb_ctx *lybctx)
649{
650 LY_ERR ret = LY_SUCCESS;
651 LYD_ANYDATA_VALUETYPE value_type;
652 int len;
653 char *buf = NULL;
654 const char *str;
655 struct ly_out *out2 = NULL;
656
657 if (anydata->value_type == LYD_ANYDATA_DATATREE) {
658 /* will be printed as a nested LYB data tree */
659 value_type = LYD_ANYDATA_LYB;
660 } else {
661 value_type = anydata->value_type;
662 }
663
664 /* first byte is type */
665 LY_CHECK_GOTO(ret = lyb_write(out, (uint8_t *)&value_type, sizeof value_type, lybctx), cleanup);
666
667 if (anydata->value_type == LYD_ANYDATA_DATATREE) {
668 /* print LYB data tree to memory */
669 LY_CHECK_GOTO(ret = ly_out_new_memory(&buf, 0, &out2), cleanup);
Radek Krejci7931b192020-06-25 17:05:03 +0200670 LY_CHECK_GOTO(ret = lyb_print_data(out2, anydata->value.tree, LYD_PRINT_WITHSIBLINGS), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200671
672 len = lyd_lyb_data_length(buf);
673 assert(len != -1);
674 str = buf;
675 } else if (anydata->value_type == LYD_ANYDATA_LYB) {
676 len = lyd_lyb_data_length(anydata->value.mem);
677 assert(len != -1);
678 str = anydata->value.mem;
679 } else {
680 len = strlen(anydata->value.str);
681 str = anydata->value.str;
682 }
683
684 /* followed by the content */
685 LY_CHECK_GOTO(ret = lyb_write_string(str, (size_t)len, 0, out, lybctx), cleanup);
686
687cleanup:
688 ly_out_free(out2, NULL, 1);
689 return ret;
690}
691
692/**
693 * @brief Print term node.
694 *
695 * @param[in] term Node to print.
696 * @param[in] out Out structure.
697 * @param[in] lybctx LYB context.
698 * @return LY_ERR value.
699 */
700static LY_ERR
701lyb_print_term(struct lyd_node_term *term, struct ly_out *out, struct lyd_lyb_ctx *lybctx)
702{
703 LY_ERR ret;
704 int dynamic;
705 const char *str;
706
707 /* get value */
708 str = lyd_value2str(term, &dynamic);
709
710 /* print it */
711 ret = lyb_write_string(str, 0, 0, out, lybctx);
712
713 if (dynamic) {
714 free((char *)str);
715 }
716 return ret;
717}
718
719/**
720 * @brief Print YANG node metadata.
721 *
722 * @param[in] out Out structure.
723 * @param[in] node Data node whose metadata to print.
724 * @param[in] lybctx LYB context.
725 * @return LY_ERR value.
726 */
727static LY_ERR
728lyb_print_metadata(struct ly_out *out, const struct lyd_node *node, struct lyd_lyb_ctx *lybctx)
729{
730 LY_ERR ret;
731 int dynamic;
732 uint8_t count = 0;
733 const struct lys_module *wd_mod = NULL;
734 struct lyd_meta *iter;
735 const char *str;
736
737 /* with-defaults */
738 if (node->schema->nodetype & LYD_NODE_TERM) {
Radek Krejci7931b192020-06-25 17:05:03 +0200739 if (((node->flags & LYD_DEFAULT) && (lybctx->print_options & (LYD_PRINT_WD_ALL_TAG | LYD_PRINT_WD_IMPL_TAG))) ||
740 ((lybctx->print_options & LYD_PRINT_WD_ALL_TAG) && ly_is_default(node))) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200741 /* we have implicit OR explicit default node, print attribute only if context include with-defaults schema */
742 wd_mod = ly_ctx_get_module_latest(node->schema->module->ctx, "ietf-netconf-with-defaults");
743 }
744 }
745
746 /* count metadata */
747 if (wd_mod) {
748 ++count;
749 }
750 for (iter = node->meta; iter; iter = iter->next) {
751 if (count == UINT8_MAX) {
752 LOGERR(lybctx->ctx, LY_EINT, "Maximum supported number of data node metadata is %u.", UINT8_MAX);
753 return LY_EINT;
754 }
755 ++count;
756 }
757
758 /* write number of metadata on 1 byte */
759 LY_CHECK_RET(lyb_write(out, &count, 1, lybctx));
760
761 if (wd_mod) {
762 /* write the "default" metadata */
763 LY_CHECK_RET(lyb_write_start_subtree(out, lybctx));
764 LY_CHECK_RET(lyb_print_model(out, wd_mod, lybctx));
765 LY_CHECK_RET(lyb_write_string("default", 0, 1, out, lybctx));
766 LY_CHECK_RET(lyb_write_string("true", 0, 0, out, lybctx));
767 LY_CHECK_RET(lyb_write_stop_subtree(out, lybctx));
768 }
769
770 /* write all the node metadata */
771 LY_LIST_FOR(node->meta, iter) {
772 /* each metadata is a subtree */
773 LY_CHECK_RET(lyb_write_start_subtree(out, lybctx));
774
775 /* model */
776 LY_CHECK_RET(lyb_print_model(out, iter->annotation->module, lybctx));
777
778 /* annotation name with length */
779 LY_CHECK_RET(lyb_write_string(iter->name, 0, 1, out, lybctx));
780
781 /* get the value */
782 str = lyd_meta2str(iter, &dynamic);
783
784 /* metadata value */
785 ret = lyb_write_string(str, 0, 0, out, lybctx);
786 if (dynamic) {
787 free((char *)str);
788 }
789 LY_CHECK_RET(ret);
790
791 /* finish metadata subtree */
792 LY_CHECK_RET(lyb_write_stop_subtree(out, lybctx));
793 }
794
795 return LY_SUCCESS;
796}
797
798/**
799 * @brief Print opaque node attributes.
800 *
801 * @param[in] out Out structure.
802 * @param[in] node Opaque node whose attributes to print.
803 * @param[in] lybctx LYB context.
804 * @return LY_ERR value.
805 */
806static LY_ERR
807lyb_print_attributes(struct ly_out *out, const struct lyd_node_opaq *node, struct lyd_lyb_ctx *lybctx)
808{
809 uint8_t count = 0;
810 struct ly_attr *iter;
811
812 for (iter = node->attr; iter; iter = iter->next) {
813 if (count == UINT8_MAX) {
814 LOGERR(lybctx->ctx, LY_EINT, "Maximum supported number of data node attributes is %u.", UINT8_MAX);
815 return LY_EINT;
816 }
817 ++count;
818 }
819
820 /* write number of attributes on 1 byte */
821 LY_CHECK_RET(lyb_write(out, &count, 1, lybctx));
822
823 /* write all the attributes */
824 LY_LIST_FOR(node->attr, iter) {
825 /* each attribute is a subtree */
826 LY_CHECK_RET(lyb_write_start_subtree(out, lybctx));
827
828 /* prefix */
829 LY_CHECK_RET(lyb_write_string(iter->prefix.pref, 0, 1, out, lybctx));
830
831 /* namespace */
832 LY_CHECK_RET(lyb_write_string(iter->prefix.ns, 0, 1, out, lybctx));
833
834 /* name */
835 LY_CHECK_RET(lyb_write_string(iter->name, 0, 1, out, lybctx));
836
837 /* value prefixes */
838 LY_CHECK_RET(lyb_print_opaq_prefixes(out, iter->val_prefs, lybctx));
839
840 /* format */
841 LY_CHECK_RET(lyb_write_number(iter->format, 1, out, lybctx));
842
843 /* value */
844 LY_CHECK_RET(lyb_write_string(iter->value, 0, 0, out, lybctx));
845
846 /* finish attribute subtree */
847 LY_CHECK_RET(lyb_write_stop_subtree(out, lybctx));
848 }
849
850 return LY_SUCCESS;
851}
852
853/**
854 * @brief Print schema node hash.
855 *
856 * @param[in] out Out structure.
857 * @param[in] schema Schema node whose hash to print.
858 * @param[in,out] sibling_ht Cached hash table for these siblings, created if NULL.
859 * @param[in] lybctx LYB context.
860 * @return LY_ERR value.
861 */
862static LY_ERR
863lyb_print_schema_hash(struct ly_out *out, struct lysc_node *schema, struct hash_table **sibling_ht, struct lyd_lyb_ctx *lybctx)
864{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200865 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +0200866 uint32_t i;
867 LYB_HASH hash;
868 struct lyd_lyb_sib_ht *sib_ht;
869 struct lysc_node *first_sibling;
870
871 if (!schema) {
872 /* opaque node, write empty hash */
873 hash = 0;
874 LY_CHECK_RET(lyb_write(out, &hash, sizeof hash, lybctx));
875 return LY_SUCCESS;
876 }
877
878 /* create whole sibling HT if not already created and saved */
879 if (!*sibling_ht) {
880 /* get first schema data sibling (or input/output) */
881 first_sibling = (struct lysc_node *)lys_getnext(NULL, lysc_data_parent(schema), schema->module->compiled, 0);
882 LY_ARRAY_FOR(lybctx->sib_hts, u) {
883 if (lybctx->sib_hts[u].first_sibling == first_sibling) {
884 /* we have already created a hash table for these siblings */
885 *sibling_ht = lybctx->sib_hts[u].ht;
886 break;
887 }
888 }
889
890 if (!*sibling_ht) {
891 /* we must create sibling hash table */
892 LY_CHECK_RET(lyb_hash_siblings(first_sibling, sibling_ht));
893
894 /* and save it */
895 LY_ARRAY_NEW_RET(lybctx->ctx, lybctx->sib_hts, sib_ht, LY_EMEM);
896
897 sib_ht->first_sibling = first_sibling;
898 sib_ht->ht = *sibling_ht;
899 }
900 }
901
902 /* get our hash */
903 LY_CHECK_RET(lyb_hash_find(*sibling_ht, schema, &hash));
904
905 /* write the hash */
906 LY_CHECK_RET(lyb_write(out, &hash, sizeof hash, lybctx));
907
908 if (hash & LYB_HASH_COLLISION_ID) {
909 /* no collision for this hash, we are done */
910 return LY_SUCCESS;
911 }
912
913 /* written hash was a collision, write also all the preceding hashes */
914 for (i = 0; !(hash & (LYB_HASH_COLLISION_ID >> i)); ++i);
915
916 for (; i; --i) {
917 hash = lyb_hash(schema, i - 1);
918 if (!hash) {
919 return LY_EINT;
920 }
921 assert(hash & (LYB_HASH_COLLISION_ID >> (i - 1)));
922
923 LY_CHECK_RET(lyb_write(out, &hash, sizeof hash, lybctx));
924 }
925
926 return LY_SUCCESS;
927}
928
929/**
930 * @brief Print data subtree.
931 *
932 * @param[in] out Out structure.
933 * @param[in] node Root node of the subtree to print.
934 * @param[in,out] sibling_ht Cached hash table for these data siblings, created if NULL.
935 * @param[in] lybctx LYB context.
936 * @return LY_ERR value.
937 */
938static LY_ERR
939lyb_print_subtree(struct ly_out *out, const struct lyd_node *node, struct hash_table **sibling_ht, struct lyd_lyb_ctx *lybctx)
940{
941 struct hash_table *child_ht = NULL;
942
943 /* register a new subtree */
944 LY_CHECK_RET(lyb_write_start_subtree(out, lybctx));
945
946 /* write model info first */
947 if (!node->schema && !((struct lyd_node_opaq *)node)->parent) {
948 LY_CHECK_RET(lyb_print_model(out, NULL, lybctx));
949 } else if (node->schema && !lysc_data_parent(node->schema)) {
950 LY_CHECK_RET(lyb_print_model(out, node->schema->module, lybctx));
951 }
952
953 /* write schema hash */
954 LY_CHECK_RET(lyb_print_schema_hash(out, (struct lysc_node *)node->schema, sibling_ht, lybctx));
955
956 /* write any metadata/attributes */
957 if (node->schema) {
958 LY_CHECK_RET(lyb_print_metadata(out, node, lybctx));
959 } else {
960 LY_CHECK_RET(lyb_print_attributes(out, (struct lyd_node_opaq *)node, lybctx));
961 }
962
963 /* write node content */
964 if (!node->schema) {
965 LY_CHECK_RET(lyb_print_opaq((struct lyd_node_opaq *)node, out, lybctx));
966 } else if (node->schema->nodetype & LYD_NODE_INNER) {
967 /* nothing to write */
968 } else if (node->schema->nodetype & LYD_NODE_TERM) {
969 LY_CHECK_RET(lyb_print_term((struct lyd_node_term *)node, out, lybctx));
970 } else if (node->schema->nodetype & LYD_NODE_ANY) {
971 LY_CHECK_RET(lyb_print_anydata((struct lyd_node_any *)node, out, lybctx));
972 } else {
973 LOGINT_RET(lybctx->ctx);
974 }
975
976 /* recursively write all the descendants */
977 LY_LIST_FOR(lyd_node_children(node, 0), node) {
978 LY_CHECK_RET(lyb_print_subtree(out, node, &child_ht, lybctx));
979 }
980
981 /* finish this subtree */
982 LY_CHECK_RET(lyb_write_stop_subtree(out, lybctx));
983
984 return LY_SUCCESS;
985}
986
987LY_ERR
988lyb_print_data(struct ly_out *out, const struct lyd_node *root, int options)
989{
990 LY_ERR ret = LY_SUCCESS;
991 uint8_t zero = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200992 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +0200993 struct hash_table *top_sibling_ht = NULL;
994 const struct lys_module *prev_mod = NULL;
995 struct lyd_lyb_ctx lybctx = {0};
996
Radek Krejci7931b192020-06-25 17:05:03 +0200997 lybctx.print_options = options;
Michal Vasko60ea6352020-06-29 13:39:39 +0200998 if (root) {
999 lybctx.ctx = LYD_NODE_CTX(root);
1000
1001 if (root->schema && lysc_data_parent(root->schema)) {
1002 LOGERR(lybctx.ctx, LY_EINVAL, "LYB printer supports only printing top-level nodes.");
1003 return LY_EINVAL;
1004 }
1005 }
1006
1007 /* LYB magic number */
Michal Vasko63f3d842020-07-08 10:10:14 +02001008 LY_CHECK_GOTO(ret = lyb_print_magic_number(out), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001009
1010 /* LYB header */
Michal Vasko63f3d842020-07-08 10:10:14 +02001011 LY_CHECK_GOTO(ret = lyb_print_header(out), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001012
1013 /* all used models */
1014 LY_CHECK_GOTO(ret = lyb_print_data_models(out, root, &lybctx), cleanup);
1015
1016 LY_LIST_FOR(root, root) {
1017 /* do not reuse sibling hash tables from different modules */
1018 if (!root->schema || (root->schema->module != prev_mod)) {
1019 top_sibling_ht = NULL;
1020 prev_mod = root->schema ? root->schema->module : NULL;
1021 }
1022
1023 LY_CHECK_GOTO(ret = lyb_print_subtree(out, root, &top_sibling_ht, &lybctx), cleanup);
1024
Radek Krejci7931b192020-06-25 17:05:03 +02001025 if (!(options & LYD_PRINT_WITHSIBLINGS)) {
Michal Vasko60ea6352020-06-29 13:39:39 +02001026 break;
1027 }
1028 }
1029
1030 /* ending zero byte */
1031 LY_CHECK_GOTO(ret = lyb_write(out, &zero, sizeof zero, &lybctx), cleanup);
1032
1033cleanup:
1034 LY_ARRAY_FREE(lybctx.subtrees);
1035 LY_ARRAY_FOR(lybctx.sib_hts, u) {
1036 lyht_free(lybctx.sib_hts[u].ht);
1037 }
1038 LY_ARRAY_FREE(lybctx.sib_hts);
1039
1040 return ret;
1041}