blob: dc03f61614f8c410df3409de7f44dc04e6faa1bc [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{
228 LY_ARRAY_SIZE_TYPE u;
229 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 }
256 lybctx->byte_count += r;
257
258 LY_ARRAY_FOR(lybctx->subtrees, u) {
259 /* increase all written counters */
260 lybctx->subtrees[u].written += r;
261 assert(lybctx->subtrees[u].written <= LYB_SIZE_MAX);
262 }
263 /* decrease count/buf */
264 count -= r;
265 buf += r;
266 }
267
268 if (full) {
269 /* write the meta information (inner chunk count and chunk size) */
270 meta_buf[0] = full->written & 0xFF;
271 meta_buf[1] = full->inner_chunks & 0xFF;
272
273 r = ly_write_skipped(out, full->position, (char *)meta_buf, LYB_META_BYTES);
274 if (r < 0) {
275 return LY_ESYS;
276 }
277 /* these bytes were already counted */
278
279 /* zero written and inner chunks */
280 full->written = 0;
281 full->inner_chunks = 0;
282
283 /* skip space for another chunk size */
284 r = ly_write_skip(out, LYB_META_BYTES, &full->position);
285 if (r < LYB_META_BYTES) {
286 return LY_ESYS;
287 }
288 lybctx->byte_count += r;
289
290 /* increase inner chunk count */
291 for (iter = &lybctx->subtrees[0]; iter != full; ++iter) {
292 if (iter->inner_chunks == LYB_INCHUNK_MAX) {
293 LOGINT(lybctx->ctx);
294 return LY_EINT;
295 }
296 ++iter->inner_chunks;
297 }
298 }
299 }
300
301 return LY_SUCCESS;
302}
303
304/**
305 * @brief Stop the current subtree - write its final metadata.
306 *
307 * @param[in] out Out structure.
308 * @param[in] lybctx LYB context.
309 * @return LY_ERR value.
310 */
311static LY_ERR
312lyb_write_stop_subtree(struct ly_out *out, struct lyd_lyb_ctx *lybctx)
313{
314 ssize_t r;
315 uint8_t meta_buf[LYB_META_BYTES];
316
317 /* write the meta chunk information */
318 meta_buf[0] = LYB_LAST_SUBTREE(lybctx).written & 0xFF;
319 meta_buf[1] = LYB_LAST_SUBTREE(lybctx).inner_chunks & 0xFF;
320
321 r = ly_write_skipped(out, LYB_LAST_SUBTREE(lybctx).position, (char *)&meta_buf, LYB_META_BYTES);
322 if (r < 0) {
323 return LY_ESYS;
324 }
325 /* do not count these bytes */
326
327 LY_ARRAY_DECREMENT(lybctx->subtrees);
328 return LY_SUCCESS;
329}
330
331/**
332 * @brief Start a new subtree - skip bytes for its metadata.
333 *
334 * @param[in] out Out structure.
335 * @param[in] lybctx LYB context.
336 * @return LY_ERR value.
337 */
338static LY_ERR
339lyb_write_start_subtree(struct ly_out *out, struct lyd_lyb_ctx *lybctx)
340{
341 ssize_t r;
342 LY_ARRAY_SIZE_TYPE u;
343
344 if (!lybctx->subtrees) {
345 u = 0;
346 } else {
347 u = LY_ARRAY_SIZE(lybctx->subtrees);
348 }
349 if (u == lybctx->subtree_size) {
350 LY_ARRAY_CREATE_RET(lybctx->ctx, lybctx->subtrees, u + LYB_SUBTREE_STEP, LY_EMEM);
351 lybctx->subtree_size = u + LYB_SUBTREE_STEP;
352 }
353
354 LY_ARRAY_INCREMENT(lybctx->subtrees);
355 LYB_LAST_SUBTREE(lybctx).written = 0;
356 LYB_LAST_SUBTREE(lybctx).inner_chunks = 0;
357
358 /* another inner chunk */
359 for (u = 0; u < LY_ARRAY_SIZE(lybctx->subtrees) - 1; ++u) {
360 if (lybctx->subtrees[u].inner_chunks == LYB_INCHUNK_MAX) {
361 LOGINT(lybctx->ctx);
362 return -1;
363 }
364 ++lybctx->subtrees[u].inner_chunks;
365 }
366
367 r = ly_write_skip(out, LYB_META_BYTES, &LYB_LAST_SUBTREE(lybctx).position);
368 if (r < LYB_META_BYTES) {
369 return LY_ESYS;
370 }
371 lybctx->byte_count += r;
372
373 return LY_SUCCESS;
374}
375
376/**
377 * @brief Write a number.
378 *
379 * @param[in] num Number to write.
380 * @param[in] bytes Actual accessible bytes of @p num.
381 * @param[in] out Out structure.
382 * @param[in] lybctx LYB context.
383 * @return LY_ERR value.
384 */
385static LY_ERR
386lyb_write_number(uint64_t num, size_t bytes, struct ly_out *out, struct lyd_lyb_ctx *lybctx)
387{
388 /* correct byte order */
389 num = htole64(num);
390
391 return lyb_write(out, (uint8_t *)&num, bytes, lybctx);
392}
393
394/**
395 * @brief Write a string.
396 *
397 * @param[in] str String to write.
398 * @param[in] str_len Length of @p str.
399 * @param[in] with_length Whether to precede the string with its length.
400 * @param[in] out Out structure.
401 * @param[in] lybctx LYB context.
402 * @return LY_ERR value.
403 */
404static LY_ERR
405lyb_write_string(const char *str, size_t str_len, int with_length, struct ly_out *out, struct lyd_lyb_ctx *lybctx)
406{
407 int r;
408
409 if (!str) {
410 str = "";
411 }
412 if (!str_len) {
413 str_len = strlen(str);
414 }
415
416 if (with_length) {
417 /* print length on 2 bytes */
418 if (str_len > UINT16_MAX) {
419 LOGINT(lybctx->ctx);
420 return LY_EINT;
421 }
422 LY_CHECK_RET(lyb_write_number(str_len, 2, out, lybctx));
423 }
424
425 r = lyb_write(out, (const uint8_t *)str, str_len, lybctx);
426 if (r < 0) {
427 return LY_ESYS;
428 }
429 lybctx->byte_count += r;
430
431 return LY_SUCCESS;
432}
433
434/**
435 * @brief Print YANG module info.
436 *
437 * @param[in] out Out structure.
438 * @param[in] mod Module to print.
439 * @param[in] lybctx LYB context.
440 * @return LY_ERR value.
441 */
442static LY_ERR
443lyb_print_model(struct ly_out *out, const struct lys_module *mod, struct lyd_lyb_ctx *lybctx)
444{
445 int r;
446 uint16_t revision;
447
448 /* model name length and model name */
449 if (mod) {
450 LY_CHECK_RET(lyb_write_string(mod->name, 0, 1, out, lybctx));
451 } else {
452 LY_CHECK_RET(lyb_write_string("", 0, 1, out, lybctx));
453 }
454
455 /* model revision as XXXX XXXX XXXX XXXX (2B) (year is offset from 2000)
456 * YYYY YYYM MMMD DDDD */
457 revision = 0;
458 if (mod && mod->revision) {
459 r = atoi(mod->revision);
460 r -= 2000;
461 r <<= 9;
462
463 revision |= r;
464
465 r = atoi(mod->revision + 5);
466 r <<= 5;
467
468 revision |= r;
469
470 r = atoi(mod->revision + 8);
471
472 revision |= r;
473 }
474 LY_CHECK_RET(lyb_write_number(revision, sizeof revision, out, lybctx));
475
476 return LY_SUCCESS;
477}
478
479/**
480 * @brief Print all used YANG modules.
481 *
482 * @param[in] out Out structure.
483 * @param[in] root Data root.
484 * @param[in] lybctx LYB context.
485 * @return LY_ERR value.
486 */
487static LY_ERR
488lyb_print_data_models(struct ly_out *out, const struct lyd_node *root, struct lyd_lyb_ctx *lybctx)
489{
490 struct ly_set *set;
491 LY_ARRAY_SIZE_TYPE u;
492 LY_ERR ret = LY_SUCCESS;
493 struct lys_module *mod;
494 const struct lyd_node *node;
495 uint32_t i;
496
497 set = ly_set_new();
498 LY_CHECK_RET(!set, LY_EMEM);
499
500 /* collect all data node modules */
501 LY_LIST_FOR(root, node) {
502 if (!node->schema) {
503 continue;
504 }
505
506 mod = node->schema->module;
507 ly_set_add(set, mod, 0);
508
509 /* add also their modules deviating or augmenting them */
510 LY_ARRAY_FOR(mod->compiled->deviated_by, u) {
511 ly_set_add(set, mod->compiled->deviated_by[u], 0);
512 }
513 LY_ARRAY_FOR(mod->compiled->augmented_by, u) {
514 ly_set_add(set, mod->compiled->augmented_by[u], 0);
515 }
516 }
517
518 /* now write module count on 2 bytes */
519 LY_CHECK_GOTO(ret = lyb_write_number(set->count, 2, out, lybctx), cleanup);
520
521 /* and all the used models */
522 for (i = 0; i < set->count; ++i) {
523 LY_CHECK_GOTO(ret = lyb_print_model(out, set->objs[i], lybctx), cleanup);
524 }
525
526cleanup:
527 ly_set_free(set, NULL);
528 return ret;
529}
530
531/**
532 * @brief Print LYB magic number.
533 *
534 * @param[in] out Out structure.
535 * @param[in] lybctx LYB context.
536 * @return LY_ERR value.
537 */
538static LY_ERR
539lyb_print_magic_number(struct ly_out *out, struct lyd_lyb_ctx *lybctx)
540{
541 int r;
542 uint32_t magic_number;
543
544 /* 'l', 'y', 'b' - 0x6c7962 */
545 ((char *)&magic_number)[0] = 'l';
546 ((char *)&magic_number)[1] = 'y';
547 ((char *)&magic_number)[2] = 'b';
548
549 r = ly_write(out, (char *)&magic_number, 3);
550 if (r < 3) {
551 return LY_ESYS;
552 }
553 lybctx->byte_count += 3;
554
555 return LY_SUCCESS;
556}
557
558/**
559 * @brief Print LYB header.
560 *
561 * @param[in] out Out structure.
562 * @param[in] lybctx LYB context.
563 * @return LY_ERR value.
564 */
565static LY_ERR
566lyb_print_header(struct ly_out *out, struct lyd_lyb_ctx *lybctx)
567{
568 int r;
569 uint8_t byte = 0;
570
571 /* version, future flags */
572 byte |= LYB_VERSION_NUM;
573
574 r = ly_write(out, (char *)&byte, 1);
575 if (r < 1) {
576 return LY_ESYS;
577 }
578 lybctx->byte_count += 1;
579
580 return LY_SUCCESS;
581}
582
583/**
584 * @brief Print opaque prefixes.
585 *
586 * @param[in] out Out structure.
587 * @param[in] prefs Prefixes to print.
588 * @param[in] lybctx LYB context.
589 * @return LY_ERR value.
590 */
591static LY_ERR
592lyb_print_opaq_prefixes(struct ly_out *out, const struct ly_prefix *prefs, struct lyd_lyb_ctx *lybctx)
593{
594 uint8_t count;
595 LY_ARRAY_SIZE_TYPE u;
596
597 if (prefs && (LY_ARRAY_SIZE(prefs) > UINT8_MAX)) {
598 LOGERR(lybctx->ctx, LY_EINT, "Maximum supported number of prefixes is %u.", UINT8_MAX);
599 return LY_EINT;
600 }
601
602 count = prefs ? LY_ARRAY_SIZE(prefs) : 0;
603
604 /* write number of prefixes on 1 byte */
605 LY_CHECK_RET(lyb_write(out, &count, 1, lybctx));
606
607 /* write all the prefixes */
608 LY_ARRAY_FOR(prefs, u) {
609 /* prefix */
610 LY_CHECK_RET(lyb_write_string(prefs[u].pref, 0, 1, out, lybctx));
611
612 /* namespace */
613 LY_CHECK_RET(lyb_write_string(prefs[u].ns, 0, 1, out, lybctx));
614 }
615
616 return LY_SUCCESS;
617}
618
619/**
620 * @brief Print opaque node.
621 *
622 * @param[in] opaq Node to print.
623 * @param[in] out Out structure.
624 * @param[in] lybctx LYB context.
625 * @return LY_ERR value.
626 */
627static LY_ERR
628lyb_print_opaq(struct lyd_node_opaq *opaq, struct ly_out *out, struct lyd_lyb_ctx *lybctx)
629{
630 /* prefix */
631 LY_CHECK_RET(lyb_write_string(opaq->prefix.pref, 0, 1, out, lybctx));
632
633 /* namespace */
634 LY_CHECK_RET(lyb_write_string(opaq->prefix.ns, 0, 1, out, lybctx));
635
636 /* name */
637 LY_CHECK_RET(lyb_write_string(opaq->name, 0, 1, out, lybctx));
638
639 /* value prefixes */
640 LY_CHECK_RET(lyb_print_opaq_prefixes(out, opaq->val_prefs, lybctx));
641
642 /* format */
643 LY_CHECK_RET(lyb_write_number(opaq->format, 1, out, lybctx));
644
645 /* value */
646 LY_CHECK_RET(lyb_write_string(opaq->value, 0, 0, out, lybctx));
647
648 return LY_SUCCESS;
649}
650
651/**
652 * @brief Print anydata node.
653 *
654 * @param[in] anydata Node to print.
655 * @param[in] out Out structure.
656 * @param[in] lybctx LYB context.
657 * @return LY_ERR value.
658 */
659static LY_ERR
660lyb_print_anydata(struct lyd_node_any *anydata, struct ly_out *out, struct lyd_lyb_ctx *lybctx)
661{
662 LY_ERR ret = LY_SUCCESS;
663 LYD_ANYDATA_VALUETYPE value_type;
664 int len;
665 char *buf = NULL;
666 const char *str;
667 struct ly_out *out2 = NULL;
668
669 if (anydata->value_type == LYD_ANYDATA_DATATREE) {
670 /* will be printed as a nested LYB data tree */
671 value_type = LYD_ANYDATA_LYB;
672 } else {
673 value_type = anydata->value_type;
674 }
675
676 /* first byte is type */
677 LY_CHECK_GOTO(ret = lyb_write(out, (uint8_t *)&value_type, sizeof value_type, lybctx), cleanup);
678
679 if (anydata->value_type == LYD_ANYDATA_DATATREE) {
680 /* print LYB data tree to memory */
681 LY_CHECK_GOTO(ret = ly_out_new_memory(&buf, 0, &out2), cleanup);
Radek Krejci7931b192020-06-25 17:05:03 +0200682 LY_CHECK_GOTO(ret = lyb_print_data(out2, anydata->value.tree, LYD_PRINT_WITHSIBLINGS), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200683
684 len = lyd_lyb_data_length(buf);
685 assert(len != -1);
686 str = buf;
687 } else if (anydata->value_type == LYD_ANYDATA_LYB) {
688 len = lyd_lyb_data_length(anydata->value.mem);
689 assert(len != -1);
690 str = anydata->value.mem;
691 } else {
692 len = strlen(anydata->value.str);
693 str = anydata->value.str;
694 }
695
696 /* followed by the content */
697 LY_CHECK_GOTO(ret = lyb_write_string(str, (size_t)len, 0, out, lybctx), cleanup);
698
699cleanup:
700 ly_out_free(out2, NULL, 1);
701 return ret;
702}
703
704/**
705 * @brief Print term node.
706 *
707 * @param[in] term Node to print.
708 * @param[in] out Out structure.
709 * @param[in] lybctx LYB context.
710 * @return LY_ERR value.
711 */
712static LY_ERR
713lyb_print_term(struct lyd_node_term *term, struct ly_out *out, struct lyd_lyb_ctx *lybctx)
714{
715 LY_ERR ret;
716 int dynamic;
717 const char *str;
718
719 /* get value */
720 str = lyd_value2str(term, &dynamic);
721
722 /* print it */
723 ret = lyb_write_string(str, 0, 0, out, lybctx);
724
725 if (dynamic) {
726 free((char *)str);
727 }
728 return ret;
729}
730
731/**
732 * @brief Print YANG node metadata.
733 *
734 * @param[in] out Out structure.
735 * @param[in] node Data node whose metadata to print.
736 * @param[in] lybctx LYB context.
737 * @return LY_ERR value.
738 */
739static LY_ERR
740lyb_print_metadata(struct ly_out *out, const struct lyd_node *node, struct lyd_lyb_ctx *lybctx)
741{
742 LY_ERR ret;
743 int dynamic;
744 uint8_t count = 0;
745 const struct lys_module *wd_mod = NULL;
746 struct lyd_meta *iter;
747 const char *str;
748
749 /* with-defaults */
750 if (node->schema->nodetype & LYD_NODE_TERM) {
Radek Krejci7931b192020-06-25 17:05:03 +0200751 if (((node->flags & LYD_DEFAULT) && (lybctx->print_options & (LYD_PRINT_WD_ALL_TAG | LYD_PRINT_WD_IMPL_TAG))) ||
752 ((lybctx->print_options & LYD_PRINT_WD_ALL_TAG) && ly_is_default(node))) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200753 /* we have implicit OR explicit default node, print attribute only if context include with-defaults schema */
754 wd_mod = ly_ctx_get_module_latest(node->schema->module->ctx, "ietf-netconf-with-defaults");
755 }
756 }
757
758 /* count metadata */
759 if (wd_mod) {
760 ++count;
761 }
762 for (iter = node->meta; iter; iter = iter->next) {
763 if (count == UINT8_MAX) {
764 LOGERR(lybctx->ctx, LY_EINT, "Maximum supported number of data node metadata is %u.", UINT8_MAX);
765 return LY_EINT;
766 }
767 ++count;
768 }
769
770 /* write number of metadata on 1 byte */
771 LY_CHECK_RET(lyb_write(out, &count, 1, lybctx));
772
773 if (wd_mod) {
774 /* write the "default" metadata */
775 LY_CHECK_RET(lyb_write_start_subtree(out, lybctx));
776 LY_CHECK_RET(lyb_print_model(out, wd_mod, lybctx));
777 LY_CHECK_RET(lyb_write_string("default", 0, 1, out, lybctx));
778 LY_CHECK_RET(lyb_write_string("true", 0, 0, out, lybctx));
779 LY_CHECK_RET(lyb_write_stop_subtree(out, lybctx));
780 }
781
782 /* write all the node metadata */
783 LY_LIST_FOR(node->meta, iter) {
784 /* each metadata is a subtree */
785 LY_CHECK_RET(lyb_write_start_subtree(out, lybctx));
786
787 /* model */
788 LY_CHECK_RET(lyb_print_model(out, iter->annotation->module, lybctx));
789
790 /* annotation name with length */
791 LY_CHECK_RET(lyb_write_string(iter->name, 0, 1, out, lybctx));
792
793 /* get the value */
794 str = lyd_meta2str(iter, &dynamic);
795
796 /* metadata value */
797 ret = lyb_write_string(str, 0, 0, out, lybctx);
798 if (dynamic) {
799 free((char *)str);
800 }
801 LY_CHECK_RET(ret);
802
803 /* finish metadata subtree */
804 LY_CHECK_RET(lyb_write_stop_subtree(out, lybctx));
805 }
806
807 return LY_SUCCESS;
808}
809
810/**
811 * @brief Print opaque node attributes.
812 *
813 * @param[in] out Out structure.
814 * @param[in] node Opaque node whose attributes to print.
815 * @param[in] lybctx LYB context.
816 * @return LY_ERR value.
817 */
818static LY_ERR
819lyb_print_attributes(struct ly_out *out, const struct lyd_node_opaq *node, struct lyd_lyb_ctx *lybctx)
820{
821 uint8_t count = 0;
822 struct ly_attr *iter;
823
824 for (iter = node->attr; iter; iter = iter->next) {
825 if (count == UINT8_MAX) {
826 LOGERR(lybctx->ctx, LY_EINT, "Maximum supported number of data node attributes is %u.", UINT8_MAX);
827 return LY_EINT;
828 }
829 ++count;
830 }
831
832 /* write number of attributes on 1 byte */
833 LY_CHECK_RET(lyb_write(out, &count, 1, lybctx));
834
835 /* write all the attributes */
836 LY_LIST_FOR(node->attr, iter) {
837 /* each attribute is a subtree */
838 LY_CHECK_RET(lyb_write_start_subtree(out, lybctx));
839
840 /* prefix */
841 LY_CHECK_RET(lyb_write_string(iter->prefix.pref, 0, 1, out, lybctx));
842
843 /* namespace */
844 LY_CHECK_RET(lyb_write_string(iter->prefix.ns, 0, 1, out, lybctx));
845
846 /* name */
847 LY_CHECK_RET(lyb_write_string(iter->name, 0, 1, out, lybctx));
848
849 /* value prefixes */
850 LY_CHECK_RET(lyb_print_opaq_prefixes(out, iter->val_prefs, lybctx));
851
852 /* format */
853 LY_CHECK_RET(lyb_write_number(iter->format, 1, out, lybctx));
854
855 /* value */
856 LY_CHECK_RET(lyb_write_string(iter->value, 0, 0, out, lybctx));
857
858 /* finish attribute subtree */
859 LY_CHECK_RET(lyb_write_stop_subtree(out, lybctx));
860 }
861
862 return LY_SUCCESS;
863}
864
865/**
866 * @brief Print schema node hash.
867 *
868 * @param[in] out Out structure.
869 * @param[in] schema Schema node whose hash to print.
870 * @param[in,out] sibling_ht Cached hash table for these siblings, created if NULL.
871 * @param[in] lybctx LYB context.
872 * @return LY_ERR value.
873 */
874static LY_ERR
875lyb_print_schema_hash(struct ly_out *out, struct lysc_node *schema, struct hash_table **sibling_ht, struct lyd_lyb_ctx *lybctx)
876{
877 LY_ARRAY_SIZE_TYPE u;
878 uint32_t i;
879 LYB_HASH hash;
880 struct lyd_lyb_sib_ht *sib_ht;
881 struct lysc_node *first_sibling;
882
883 if (!schema) {
884 /* opaque node, write empty hash */
885 hash = 0;
886 LY_CHECK_RET(lyb_write(out, &hash, sizeof hash, lybctx));
887 return LY_SUCCESS;
888 }
889
890 /* create whole sibling HT if not already created and saved */
891 if (!*sibling_ht) {
892 /* get first schema data sibling (or input/output) */
893 first_sibling = (struct lysc_node *)lys_getnext(NULL, lysc_data_parent(schema), schema->module->compiled, 0);
894 LY_ARRAY_FOR(lybctx->sib_hts, u) {
895 if (lybctx->sib_hts[u].first_sibling == first_sibling) {
896 /* we have already created a hash table for these siblings */
897 *sibling_ht = lybctx->sib_hts[u].ht;
898 break;
899 }
900 }
901
902 if (!*sibling_ht) {
903 /* we must create sibling hash table */
904 LY_CHECK_RET(lyb_hash_siblings(first_sibling, sibling_ht));
905
906 /* and save it */
907 LY_ARRAY_NEW_RET(lybctx->ctx, lybctx->sib_hts, sib_ht, LY_EMEM);
908
909 sib_ht->first_sibling = first_sibling;
910 sib_ht->ht = *sibling_ht;
911 }
912 }
913
914 /* get our hash */
915 LY_CHECK_RET(lyb_hash_find(*sibling_ht, schema, &hash));
916
917 /* write the hash */
918 LY_CHECK_RET(lyb_write(out, &hash, sizeof hash, lybctx));
919
920 if (hash & LYB_HASH_COLLISION_ID) {
921 /* no collision for this hash, we are done */
922 return LY_SUCCESS;
923 }
924
925 /* written hash was a collision, write also all the preceding hashes */
926 for (i = 0; !(hash & (LYB_HASH_COLLISION_ID >> i)); ++i);
927
928 for (; i; --i) {
929 hash = lyb_hash(schema, i - 1);
930 if (!hash) {
931 return LY_EINT;
932 }
933 assert(hash & (LYB_HASH_COLLISION_ID >> (i - 1)));
934
935 LY_CHECK_RET(lyb_write(out, &hash, sizeof hash, lybctx));
936 }
937
938 return LY_SUCCESS;
939}
940
941/**
942 * @brief Print data subtree.
943 *
944 * @param[in] out Out structure.
945 * @param[in] node Root node of the subtree to print.
946 * @param[in,out] sibling_ht Cached hash table for these data siblings, created if NULL.
947 * @param[in] lybctx LYB context.
948 * @return LY_ERR value.
949 */
950static LY_ERR
951lyb_print_subtree(struct ly_out *out, const struct lyd_node *node, struct hash_table **sibling_ht, struct lyd_lyb_ctx *lybctx)
952{
953 struct hash_table *child_ht = NULL;
954
955 /* register a new subtree */
956 LY_CHECK_RET(lyb_write_start_subtree(out, lybctx));
957
958 /* write model info first */
959 if (!node->schema && !((struct lyd_node_opaq *)node)->parent) {
960 LY_CHECK_RET(lyb_print_model(out, NULL, lybctx));
961 } else if (node->schema && !lysc_data_parent(node->schema)) {
962 LY_CHECK_RET(lyb_print_model(out, node->schema->module, lybctx));
963 }
964
965 /* write schema hash */
966 LY_CHECK_RET(lyb_print_schema_hash(out, (struct lysc_node *)node->schema, sibling_ht, lybctx));
967
968 /* write any metadata/attributes */
969 if (node->schema) {
970 LY_CHECK_RET(lyb_print_metadata(out, node, lybctx));
971 } else {
972 LY_CHECK_RET(lyb_print_attributes(out, (struct lyd_node_opaq *)node, lybctx));
973 }
974
975 /* write node content */
976 if (!node->schema) {
977 LY_CHECK_RET(lyb_print_opaq((struct lyd_node_opaq *)node, out, lybctx));
978 } else if (node->schema->nodetype & LYD_NODE_INNER) {
979 /* nothing to write */
980 } else if (node->schema->nodetype & LYD_NODE_TERM) {
981 LY_CHECK_RET(lyb_print_term((struct lyd_node_term *)node, out, lybctx));
982 } else if (node->schema->nodetype & LYD_NODE_ANY) {
983 LY_CHECK_RET(lyb_print_anydata((struct lyd_node_any *)node, out, lybctx));
984 } else {
985 LOGINT_RET(lybctx->ctx);
986 }
987
988 /* recursively write all the descendants */
989 LY_LIST_FOR(lyd_node_children(node, 0), node) {
990 LY_CHECK_RET(lyb_print_subtree(out, node, &child_ht, lybctx));
991 }
992
993 /* finish this subtree */
994 LY_CHECK_RET(lyb_write_stop_subtree(out, lybctx));
995
996 return LY_SUCCESS;
997}
998
999LY_ERR
1000lyb_print_data(struct ly_out *out, const struct lyd_node *root, int options)
1001{
1002 LY_ERR ret = LY_SUCCESS;
1003 uint8_t zero = 0;
1004 LY_ARRAY_SIZE_TYPE u;
1005 struct hash_table *top_sibling_ht = NULL;
1006 const struct lys_module *prev_mod = NULL;
1007 struct lyd_lyb_ctx lybctx = {0};
1008
Radek Krejci7931b192020-06-25 17:05:03 +02001009 lybctx.print_options = options;
Michal Vasko60ea6352020-06-29 13:39:39 +02001010 if (root) {
1011 lybctx.ctx = LYD_NODE_CTX(root);
1012
1013 if (root->schema && lysc_data_parent(root->schema)) {
1014 LOGERR(lybctx.ctx, LY_EINVAL, "LYB printer supports only printing top-level nodes.");
1015 return LY_EINVAL;
1016 }
1017 }
1018
1019 /* LYB magic number */
1020 LY_CHECK_GOTO(ret = lyb_print_magic_number(out, &lybctx), cleanup);
1021
1022 /* LYB header */
1023 LY_CHECK_GOTO(ret = lyb_print_header(out, &lybctx), cleanup);
1024
1025 /* all used models */
1026 LY_CHECK_GOTO(ret = lyb_print_data_models(out, root, &lybctx), cleanup);
1027
1028 LY_LIST_FOR(root, root) {
1029 /* do not reuse sibling hash tables from different modules */
1030 if (!root->schema || (root->schema->module != prev_mod)) {
1031 top_sibling_ht = NULL;
1032 prev_mod = root->schema ? root->schema->module : NULL;
1033 }
1034
1035 LY_CHECK_GOTO(ret = lyb_print_subtree(out, root, &top_sibling_ht, &lybctx), cleanup);
1036
Radek Krejci7931b192020-06-25 17:05:03 +02001037 if (!(options & LYD_PRINT_WITHSIBLINGS)) {
Michal Vasko60ea6352020-06-29 13:39:39 +02001038 break;
1039 }
1040 }
1041
1042 /* ending zero byte */
1043 LY_CHECK_GOTO(ret = lyb_write(out, &zero, sizeof zero, &lybctx), cleanup);
1044
1045cleanup:
1046 LY_ARRAY_FREE(lybctx.subtrees);
1047 LY_ARRAY_FOR(lybctx.sib_hts, u) {
1048 lyht_free(lybctx.sib_hts[u].ht);
1049 }
1050 LY_ARRAY_FREE(lybctx.sib_hts);
1051
1052 return ret;
1053}