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