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