blob: 1d93bbb4b2a4247f0169e71cd4f4a5682dc54ed3 [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
Radek Krejci1798aae2020-07-14 13:26:06 +0200226lyb_write(struct ly_out *out, const uint8_t *buf, size_t count, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200227{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200228 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +0200229 struct lyd_lyb_subtree *full, *iter;
230 ssize_t r, to_write;
231 uint8_t meta_buf[LYB_META_BYTES];
232
233 while (1) {
234 /* check for full data chunks */
235 to_write = count;
236 full = NULL;
237 LY_ARRAY_FOR(lybctx->subtrees, u) {
238 /* we want the innermost chunks resolved first, so replace previous full chunks */
239 if (lybctx->subtrees[u].written + to_write >= LYB_SIZE_MAX) {
240 /* full chunk, do not write more than allowed */
241 to_write = LYB_SIZE_MAX - lybctx->subtrees[u].written;
242 full = &lybctx->subtrees[u];
243 }
244 }
245
246 if (!full && !count) {
247 break;
248 }
249
250 /* we are actually writing some data, not just finishing another chunk */
251 if (to_write) {
252 r = ly_write(out, (char *)buf, to_write);
253 if (r < to_write) {
254 return LY_ESYS;
255 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200256
257 LY_ARRAY_FOR(lybctx->subtrees, u) {
258 /* increase all written counters */
259 lybctx->subtrees[u].written += r;
260 assert(lybctx->subtrees[u].written <= LYB_SIZE_MAX);
261 }
262 /* decrease count/buf */
263 count -= r;
264 buf += r;
265 }
266
267 if (full) {
268 /* write the meta information (inner chunk count and chunk size) */
269 meta_buf[0] = full->written & 0xFF;
270 meta_buf[1] = full->inner_chunks & 0xFF;
271
272 r = ly_write_skipped(out, full->position, (char *)meta_buf, LYB_META_BYTES);
273 if (r < 0) {
274 return LY_ESYS;
275 }
276 /* these bytes were already counted */
277
278 /* zero written and inner chunks */
279 full->written = 0;
280 full->inner_chunks = 0;
281
282 /* skip space for another chunk size */
283 r = ly_write_skip(out, LYB_META_BYTES, &full->position);
284 if (r < LYB_META_BYTES) {
285 return LY_ESYS;
286 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200287
288 /* increase inner chunk count */
289 for (iter = &lybctx->subtrees[0]; iter != full; ++iter) {
290 if (iter->inner_chunks == LYB_INCHUNK_MAX) {
291 LOGINT(lybctx->ctx);
292 return LY_EINT;
293 }
294 ++iter->inner_chunks;
295 }
296 }
297 }
298
299 return LY_SUCCESS;
300}
301
302/**
303 * @brief Stop the current subtree - write its final metadata.
304 *
305 * @param[in] out Out structure.
306 * @param[in] lybctx LYB context.
307 * @return LY_ERR value.
308 */
309static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200310lyb_write_stop_subtree(struct ly_out *out, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200311{
312 ssize_t r;
313 uint8_t meta_buf[LYB_META_BYTES];
314
315 /* write the meta chunk information */
316 meta_buf[0] = LYB_LAST_SUBTREE(lybctx).written & 0xFF;
317 meta_buf[1] = LYB_LAST_SUBTREE(lybctx).inner_chunks & 0xFF;
318
319 r = ly_write_skipped(out, LYB_LAST_SUBTREE(lybctx).position, (char *)&meta_buf, LYB_META_BYTES);
320 if (r < 0) {
321 return LY_ESYS;
322 }
323 /* do not count these bytes */
324
325 LY_ARRAY_DECREMENT(lybctx->subtrees);
326 return LY_SUCCESS;
327}
328
329/**
330 * @brief Start a new subtree - skip bytes for its metadata.
331 *
332 * @param[in] out Out structure.
333 * @param[in] lybctx LYB context.
334 * @return LY_ERR value.
335 */
336static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200337lyb_write_start_subtree(struct ly_out *out, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200338{
339 ssize_t r;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200340 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +0200341
342 if (!lybctx->subtrees) {
Radek Krejcif6d14cb2020-07-02 16:11:45 +0200343 assert(lybctx->subtree_size == 0);
Michal Vasko60ea6352020-06-29 13:39:39 +0200344 u = 0;
345 } else {
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200346 u = LY_ARRAY_COUNT(lybctx->subtrees);
Michal Vasko60ea6352020-06-29 13:39:39 +0200347 }
348 if (u == lybctx->subtree_size) {
349 LY_ARRAY_CREATE_RET(lybctx->ctx, lybctx->subtrees, u + LYB_SUBTREE_STEP, LY_EMEM);
350 lybctx->subtree_size = u + LYB_SUBTREE_STEP;
351 }
352
353 LY_ARRAY_INCREMENT(lybctx->subtrees);
354 LYB_LAST_SUBTREE(lybctx).written = 0;
355 LYB_LAST_SUBTREE(lybctx).inner_chunks = 0;
356
357 /* another inner chunk */
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200358 for (u = 0; u < LY_ARRAY_COUNT(lybctx->subtrees) - 1; ++u) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200359 if (lybctx->subtrees[u].inner_chunks == LYB_INCHUNK_MAX) {
360 LOGINT(lybctx->ctx);
361 return -1;
362 }
363 ++lybctx->subtrees[u].inner_chunks;
364 }
365
366 r = ly_write_skip(out, LYB_META_BYTES, &LYB_LAST_SUBTREE(lybctx).position);
367 if (r < LYB_META_BYTES) {
368 return LY_ESYS;
369 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200370
371 return LY_SUCCESS;
372}
373
374/**
375 * @brief Write a number.
376 *
377 * @param[in] num Number to write.
378 * @param[in] bytes Actual accessible bytes of @p num.
379 * @param[in] out Out structure.
380 * @param[in] lybctx LYB context.
381 * @return LY_ERR value.
382 */
383static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200384lyb_write_number(uint64_t num, size_t bytes, struct ly_out *out, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200385{
386 /* correct byte order */
387 num = htole64(num);
388
389 return lyb_write(out, (uint8_t *)&num, bytes, lybctx);
390}
391
392/**
393 * @brief Write a string.
394 *
395 * @param[in] str String to write.
396 * @param[in] str_len Length of @p str.
397 * @param[in] with_length Whether to precede the string with its length.
398 * @param[in] out Out structure.
399 * @param[in] lybctx LYB context.
400 * @return LY_ERR value.
401 */
402static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200403lyb_write_string(const char *str, size_t str_len, int with_length, struct ly_out *out, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200404{
Michal Vasko60ea6352020-06-29 13:39:39 +0200405 if (!str) {
406 str = "";
Michal Vasko30667352020-08-13 09:06:14 +0200407 LY_CHECK_ERR_RET(str_len, LOGINT(lybctx->ctx), LY_EINT);
Michal Vasko60ea6352020-06-29 13:39:39 +0200408 }
409 if (!str_len) {
410 str_len = strlen(str);
411 }
412
413 if (with_length) {
414 /* print length on 2 bytes */
415 if (str_len > UINT16_MAX) {
416 LOGINT(lybctx->ctx);
417 return LY_EINT;
418 }
419 LY_CHECK_RET(lyb_write_number(str_len, 2, out, lybctx));
420 }
421
Michal Vasko63f3d842020-07-08 10:10:14 +0200422 LY_CHECK_RET(lyb_write(out, (const uint8_t *)str, str_len, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200423
424 return LY_SUCCESS;
425}
426
427/**
428 * @brief Print YANG module info.
429 *
430 * @param[in] out Out structure.
431 * @param[in] mod Module to print.
432 * @param[in] lybctx LYB context.
433 * @return LY_ERR value.
434 */
435static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200436lyb_print_model(struct ly_out *out, const struct lys_module *mod, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200437{
438 int r;
439 uint16_t revision;
440
441 /* model name length and model name */
442 if (mod) {
443 LY_CHECK_RET(lyb_write_string(mod->name, 0, 1, out, lybctx));
444 } else {
445 LY_CHECK_RET(lyb_write_string("", 0, 1, out, lybctx));
446 }
447
448 /* model revision as XXXX XXXX XXXX XXXX (2B) (year is offset from 2000)
449 * YYYY YYYM MMMD DDDD */
450 revision = 0;
451 if (mod && mod->revision) {
452 r = atoi(mod->revision);
453 r -= 2000;
454 r <<= 9;
455
456 revision |= r;
457
458 r = atoi(mod->revision + 5);
459 r <<= 5;
460
461 revision |= r;
462
463 r = atoi(mod->revision + 8);
464
465 revision |= r;
466 }
467 LY_CHECK_RET(lyb_write_number(revision, sizeof revision, out, lybctx));
468
469 return LY_SUCCESS;
470}
471
472/**
473 * @brief Print all used YANG modules.
474 *
475 * @param[in] out Out structure.
476 * @param[in] root Data root.
477 * @param[in] lybctx LYB context.
478 * @return LY_ERR value.
479 */
480static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200481lyb_print_data_models(struct ly_out *out, const struct lyd_node *root, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200482{
483 struct ly_set *set;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200484 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +0200485 LY_ERR ret = LY_SUCCESS;
486 struct lys_module *mod;
487 const struct lyd_node *node;
488 uint32_t i;
489
490 set = ly_set_new();
491 LY_CHECK_RET(!set, LY_EMEM);
492
493 /* collect all data node modules */
494 LY_LIST_FOR(root, node) {
495 if (!node->schema) {
496 continue;
497 }
498
499 mod = node->schema->module;
500 ly_set_add(set, mod, 0);
501
502 /* add also their modules deviating or augmenting them */
503 LY_ARRAY_FOR(mod->compiled->deviated_by, u) {
504 ly_set_add(set, mod->compiled->deviated_by[u], 0);
505 }
506 LY_ARRAY_FOR(mod->compiled->augmented_by, u) {
507 ly_set_add(set, mod->compiled->augmented_by[u], 0);
508 }
509 }
510
511 /* now write module count on 2 bytes */
512 LY_CHECK_GOTO(ret = lyb_write_number(set->count, 2, out, lybctx), cleanup);
513
514 /* and all the used models */
515 for (i = 0; i < set->count; ++i) {
516 LY_CHECK_GOTO(ret = lyb_print_model(out, set->objs[i], lybctx), cleanup);
517 }
518
519cleanup:
520 ly_set_free(set, NULL);
521 return ret;
522}
523
524/**
525 * @brief Print LYB magic number.
526 *
527 * @param[in] out Out structure.
Michal Vasko60ea6352020-06-29 13:39:39 +0200528 * @return LY_ERR value.
529 */
530static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200531lyb_print_magic_number(struct ly_out *out)
Michal Vasko60ea6352020-06-29 13:39:39 +0200532{
533 int r;
534 uint32_t magic_number;
535
536 /* 'l', 'y', 'b' - 0x6c7962 */
537 ((char *)&magic_number)[0] = 'l';
538 ((char *)&magic_number)[1] = 'y';
539 ((char *)&magic_number)[2] = 'b';
540
541 r = ly_write(out, (char *)&magic_number, 3);
542 if (r < 3) {
543 return LY_ESYS;
544 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200545
546 return LY_SUCCESS;
547}
548
549/**
550 * @brief Print LYB header.
551 *
552 * @param[in] out Out structure.
Michal Vasko60ea6352020-06-29 13:39:39 +0200553 * @return LY_ERR value.
554 */
555static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200556lyb_print_header(struct ly_out *out)
Michal Vasko60ea6352020-06-29 13:39:39 +0200557{
558 int r;
559 uint8_t byte = 0;
560
561 /* version, future flags */
562 byte |= LYB_VERSION_NUM;
563
564 r = ly_write(out, (char *)&byte, 1);
565 if (r < 1) {
566 return LY_ESYS;
567 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200568
569 return LY_SUCCESS;
570}
571
572/**
573 * @brief Print opaque prefixes.
574 *
575 * @param[in] out Out structure.
576 * @param[in] prefs Prefixes to print.
577 * @param[in] lybctx LYB context.
578 * @return LY_ERR value.
579 */
580static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200581lyb_print_opaq_prefixes(struct ly_out *out, const struct ly_prefix *prefs, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200582{
583 uint8_t count;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200584 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +0200585
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200586 if (prefs && (LY_ARRAY_COUNT(prefs) > UINT8_MAX)) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200587 LOGERR(lybctx->ctx, LY_EINT, "Maximum supported number of prefixes is %u.", UINT8_MAX);
588 return LY_EINT;
589 }
590
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200591 count = prefs ? LY_ARRAY_COUNT(prefs) : 0;
Michal Vasko60ea6352020-06-29 13:39:39 +0200592
593 /* write number of prefixes on 1 byte */
594 LY_CHECK_RET(lyb_write(out, &count, 1, lybctx));
595
596 /* write all the prefixes */
597 LY_ARRAY_FOR(prefs, u) {
598 /* prefix */
Radek Krejci1798aae2020-07-14 13:26:06 +0200599 LY_CHECK_RET(lyb_write_string(prefs[u].id, 0, 1, out, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200600
601 /* namespace */
Radek Krejci1798aae2020-07-14 13:26:06 +0200602 LY_CHECK_RET(lyb_write_string(prefs[u].module_name, 0, 1, out, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200603 }
604
605 return LY_SUCCESS;
606}
607
608/**
609 * @brief Print opaque node.
610 *
611 * @param[in] opaq Node to print.
612 * @param[in] out Out structure.
613 * @param[in] lybctx LYB context.
614 * @return LY_ERR value.
615 */
616static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200617lyb_print_opaq(struct lyd_node_opaq *opaq, struct ly_out *out, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200618{
619 /* prefix */
Radek Krejci1798aae2020-07-14 13:26:06 +0200620 LY_CHECK_RET(lyb_write_string(opaq->prefix.id, 0, 1, out, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200621
Radek Krejci1798aae2020-07-14 13:26:06 +0200622 /* module reference */
623 LY_CHECK_RET(lyb_write_string(opaq->prefix.module_name, 0, 1, out, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200624
625 /* name */
626 LY_CHECK_RET(lyb_write_string(opaq->name, 0, 1, out, lybctx));
627
628 /* value prefixes */
629 LY_CHECK_RET(lyb_print_opaq_prefixes(out, opaq->val_prefs, lybctx));
630
631 /* format */
632 LY_CHECK_RET(lyb_write_number(opaq->format, 1, out, lybctx));
633
634 /* value */
635 LY_CHECK_RET(lyb_write_string(opaq->value, 0, 0, out, lybctx));
636
637 return LY_SUCCESS;
638}
639
640/**
641 * @brief Print anydata node.
642 *
643 * @param[in] anydata Node to print.
644 * @param[in] out Out structure.
645 * @param[in] lybctx LYB context.
646 * @return LY_ERR value.
647 */
648static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200649lyb_print_anydata(struct lyd_node_any *anydata, struct ly_out *out, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200650{
651 LY_ERR ret = LY_SUCCESS;
652 LYD_ANYDATA_VALUETYPE value_type;
653 int len;
654 char *buf = NULL;
655 const char *str;
656 struct ly_out *out2 = NULL;
657
658 if (anydata->value_type == LYD_ANYDATA_DATATREE) {
659 /* will be printed as a nested LYB data tree */
660 value_type = LYD_ANYDATA_LYB;
661 } else {
662 value_type = anydata->value_type;
663 }
664
665 /* first byte is type */
666 LY_CHECK_GOTO(ret = lyb_write(out, (uint8_t *)&value_type, sizeof value_type, lybctx), cleanup);
667
668 if (anydata->value_type == LYD_ANYDATA_DATATREE) {
669 /* print LYB data tree to memory */
670 LY_CHECK_GOTO(ret = ly_out_new_memory(&buf, 0, &out2), cleanup);
Radek Krejci7931b192020-06-25 17:05:03 +0200671 LY_CHECK_GOTO(ret = lyb_print_data(out2, anydata->value.tree, LYD_PRINT_WITHSIBLINGS), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200672
673 len = lyd_lyb_data_length(buf);
674 assert(len != -1);
675 str = buf;
676 } else if (anydata->value_type == LYD_ANYDATA_LYB) {
677 len = lyd_lyb_data_length(anydata->value.mem);
678 assert(len != -1);
679 str = anydata->value.mem;
680 } else {
681 len = strlen(anydata->value.str);
682 str = anydata->value.str;
683 }
684
685 /* followed by the content */
686 LY_CHECK_GOTO(ret = lyb_write_string(str, (size_t)len, 0, out, lybctx), cleanup);
687
688cleanup:
689 ly_out_free(out2, NULL, 1);
690 return ret;
691}
692
693/**
694 * @brief Print term node.
695 *
696 * @param[in] term Node to print.
697 * @param[in] out Out structure.
698 * @param[in] lybctx LYB context.
699 * @return LY_ERR value.
700 */
701static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200702lyb_print_term(struct lyd_node_term *term, struct ly_out *out, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200703{
704 LY_ERR ret;
705 int dynamic;
706 const char *str;
707
708 /* get value */
709 str = lyd_value2str(term, &dynamic);
710
711 /* print it */
712 ret = lyb_write_string(str, 0, 0, out, lybctx);
713
714 if (dynamic) {
715 free((char *)str);
716 }
717 return ret;
718}
719
720/**
721 * @brief Print YANG node metadata.
722 *
723 * @param[in] out Out structure.
724 * @param[in] node Data node whose metadata to print.
725 * @param[in] lybctx LYB context.
726 * @return LY_ERR value.
727 */
728static LY_ERR
729lyb_print_metadata(struct ly_out *out, const struct lyd_node *node, struct lyd_lyb_ctx *lybctx)
730{
731 LY_ERR ret;
732 int dynamic;
733 uint8_t count = 0;
734 const struct lys_module *wd_mod = NULL;
735 struct lyd_meta *iter;
736 const char *str;
737
738 /* with-defaults */
739 if (node->schema->nodetype & LYD_NODE_TERM) {
Radek Krejci7931b192020-06-25 17:05:03 +0200740 if (((node->flags & LYD_DEFAULT) && (lybctx->print_options & (LYD_PRINT_WD_ALL_TAG | LYD_PRINT_WD_IMPL_TAG))) ||
741 ((lybctx->print_options & LYD_PRINT_WD_ALL_TAG) && ly_is_default(node))) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200742 /* we have implicit OR explicit default node, print attribute only if context include with-defaults schema */
743 wd_mod = ly_ctx_get_module_latest(node->schema->module->ctx, "ietf-netconf-with-defaults");
744 }
745 }
746
747 /* count metadata */
748 if (wd_mod) {
749 ++count;
750 }
751 for (iter = node->meta; iter; iter = iter->next) {
752 if (count == UINT8_MAX) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200753 LOGERR(lybctx->lybctx->ctx, LY_EINT, "Maximum supported number of data node metadata is %u.", UINT8_MAX);
Michal Vasko60ea6352020-06-29 13:39:39 +0200754 return LY_EINT;
755 }
756 ++count;
757 }
758
759 /* write number of metadata on 1 byte */
Radek Krejci1798aae2020-07-14 13:26:06 +0200760 LY_CHECK_RET(lyb_write(out, &count, 1, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200761
762 if (wd_mod) {
763 /* write the "default" metadata */
Radek Krejci1798aae2020-07-14 13:26:06 +0200764 LY_CHECK_RET(lyb_write_start_subtree(out, lybctx->lybctx));
765 LY_CHECK_RET(lyb_print_model(out, wd_mod, lybctx->lybctx));
766 LY_CHECK_RET(lyb_write_string("default", 0, 1, out, lybctx->lybctx));
767 LY_CHECK_RET(lyb_write_string("true", 0, 0, out, lybctx->lybctx));
768 LY_CHECK_RET(lyb_write_stop_subtree(out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200769 }
770
771 /* write all the node metadata */
772 LY_LIST_FOR(node->meta, iter) {
773 /* each metadata is a subtree */
Radek Krejci1798aae2020-07-14 13:26:06 +0200774 LY_CHECK_RET(lyb_write_start_subtree(out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200775
776 /* model */
Radek Krejci1798aae2020-07-14 13:26:06 +0200777 LY_CHECK_RET(lyb_print_model(out, iter->annotation->module, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200778
779 /* annotation name with length */
Radek Krejci1798aae2020-07-14 13:26:06 +0200780 LY_CHECK_RET(lyb_write_string(iter->name, 0, 1, out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200781
782 /* get the value */
783 str = lyd_meta2str(iter, &dynamic);
784
785 /* metadata value */
Radek Krejci1798aae2020-07-14 13:26:06 +0200786 ret = lyb_write_string(str, 0, 0, out, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200787 if (dynamic) {
788 free((char *)str);
789 }
790 LY_CHECK_RET(ret);
791
792 /* finish metadata subtree */
Radek Krejci1798aae2020-07-14 13:26:06 +0200793 LY_CHECK_RET(lyb_write_stop_subtree(out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200794 }
795
796 return LY_SUCCESS;
797}
798
799/**
800 * @brief Print opaque node attributes.
801 *
802 * @param[in] out Out structure.
803 * @param[in] node Opaque node whose attributes to print.
804 * @param[in] lybctx LYB context.
805 * @return LY_ERR value.
806 */
807static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200808lyb_print_attributes(struct ly_out *out, const struct lyd_node_opaq *node, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200809{
810 uint8_t count = 0;
Radek Krejci5536d282020-08-04 23:27:44 +0200811 struct lyd_attr *iter;
Michal Vasko60ea6352020-06-29 13:39:39 +0200812
813 for (iter = node->attr; iter; iter = iter->next) {
814 if (count == UINT8_MAX) {
815 LOGERR(lybctx->ctx, LY_EINT, "Maximum supported number of data node attributes is %u.", UINT8_MAX);
816 return LY_EINT;
817 }
818 ++count;
819 }
820
821 /* write number of attributes on 1 byte */
822 LY_CHECK_RET(lyb_write(out, &count, 1, lybctx));
823
824 /* write all the attributes */
825 LY_LIST_FOR(node->attr, iter) {
826 /* each attribute is a subtree */
827 LY_CHECK_RET(lyb_write_start_subtree(out, lybctx));
828
829 /* prefix */
Radek Krejci1798aae2020-07-14 13:26:06 +0200830 LY_CHECK_RET(lyb_write_string(iter->prefix.id, 0, 1, out, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200831
832 /* namespace */
Radek Krejci1798aae2020-07-14 13:26:06 +0200833 LY_CHECK_RET(lyb_write_string(iter->prefix.module_name, 0, 1, out, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200834
835 /* name */
836 LY_CHECK_RET(lyb_write_string(iter->name, 0, 1, out, lybctx));
837
838 /* value prefixes */
839 LY_CHECK_RET(lyb_print_opaq_prefixes(out, iter->val_prefs, lybctx));
840
841 /* format */
842 LY_CHECK_RET(lyb_write_number(iter->format, 1, out, lybctx));
843
844 /* value */
845 LY_CHECK_RET(lyb_write_string(iter->value, 0, 0, out, lybctx));
846
847 /* finish attribute subtree */
848 LY_CHECK_RET(lyb_write_stop_subtree(out, lybctx));
849 }
850
851 return LY_SUCCESS;
852}
853
854/**
855 * @brief Print schema node hash.
856 *
857 * @param[in] out Out structure.
858 * @param[in] schema Schema node whose hash to print.
859 * @param[in,out] sibling_ht Cached hash table for these siblings, created if NULL.
860 * @param[in] lybctx LYB context.
861 * @return LY_ERR value.
862 */
863static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200864lyb_print_schema_hash(struct ly_out *out, struct lysc_node *schema, struct hash_table **sibling_ht, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200865{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200866 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +0200867 uint32_t i;
868 LYB_HASH hash;
869 struct lyd_lyb_sib_ht *sib_ht;
870 struct lysc_node *first_sibling;
871
872 if (!schema) {
873 /* opaque node, write empty hash */
874 hash = 0;
875 LY_CHECK_RET(lyb_write(out, &hash, sizeof hash, lybctx));
876 return LY_SUCCESS;
877 }
878
879 /* create whole sibling HT if not already created and saved */
880 if (!*sibling_ht) {
881 /* get first schema data sibling (or input/output) */
882 first_sibling = (struct lysc_node *)lys_getnext(NULL, lysc_data_parent(schema), schema->module->compiled, 0);
883 LY_ARRAY_FOR(lybctx->sib_hts, u) {
884 if (lybctx->sib_hts[u].first_sibling == first_sibling) {
885 /* we have already created a hash table for these siblings */
886 *sibling_ht = lybctx->sib_hts[u].ht;
887 break;
888 }
889 }
890
891 if (!*sibling_ht) {
892 /* we must create sibling hash table */
893 LY_CHECK_RET(lyb_hash_siblings(first_sibling, sibling_ht));
894
895 /* and save it */
896 LY_ARRAY_NEW_RET(lybctx->ctx, lybctx->sib_hts, sib_ht, LY_EMEM);
897
898 sib_ht->first_sibling = first_sibling;
899 sib_ht->ht = *sibling_ht;
900 }
901 }
902
903 /* get our hash */
904 LY_CHECK_RET(lyb_hash_find(*sibling_ht, schema, &hash));
905
906 /* write the hash */
907 LY_CHECK_RET(lyb_write(out, &hash, sizeof hash, lybctx));
908
909 if (hash & LYB_HASH_COLLISION_ID) {
910 /* no collision for this hash, we are done */
911 return LY_SUCCESS;
912 }
913
914 /* written hash was a collision, write also all the preceding hashes */
915 for (i = 0; !(hash & (LYB_HASH_COLLISION_ID >> i)); ++i);
916
917 for (; i; --i) {
918 hash = lyb_hash(schema, i - 1);
919 if (!hash) {
920 return LY_EINT;
921 }
922 assert(hash & (LYB_HASH_COLLISION_ID >> (i - 1)));
923
924 LY_CHECK_RET(lyb_write(out, &hash, sizeof hash, lybctx));
925 }
926
927 return LY_SUCCESS;
928}
929
930/**
931 * @brief Print data subtree.
932 *
933 * @param[in] out Out structure.
934 * @param[in] node Root node of the subtree to print.
935 * @param[in,out] sibling_ht Cached hash table for these data siblings, created if NULL.
936 * @param[in] lybctx LYB context.
937 * @return LY_ERR value.
938 */
939static LY_ERR
940lyb_print_subtree(struct ly_out *out, const struct lyd_node *node, struct hash_table **sibling_ht, struct lyd_lyb_ctx *lybctx)
941{
942 struct hash_table *child_ht = NULL;
943
944 /* register a new subtree */
Radek Krejci1798aae2020-07-14 13:26:06 +0200945 LY_CHECK_RET(lyb_write_start_subtree(out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200946
947 /* write model info first */
948 if (!node->schema && !((struct lyd_node_opaq *)node)->parent) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200949 LY_CHECK_RET(lyb_print_model(out, NULL, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200950 } else if (node->schema && !lysc_data_parent(node->schema)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200951 LY_CHECK_RET(lyb_print_model(out, node->schema->module, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200952 }
953
954 /* write schema hash */
Radek Krejci1798aae2020-07-14 13:26:06 +0200955 LY_CHECK_RET(lyb_print_schema_hash(out, (struct lysc_node *)node->schema, sibling_ht, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200956
957 /* write any metadata/attributes */
958 if (node->schema) {
959 LY_CHECK_RET(lyb_print_metadata(out, node, lybctx));
960 } else {
Radek Krejci1798aae2020-07-14 13:26:06 +0200961 LY_CHECK_RET(lyb_print_attributes(out, (struct lyd_node_opaq *)node, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200962 }
963
964 /* write node content */
965 if (!node->schema) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200966 LY_CHECK_RET(lyb_print_opaq((struct lyd_node_opaq *)node, out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200967 } else if (node->schema->nodetype & LYD_NODE_INNER) {
968 /* nothing to write */
969 } else if (node->schema->nodetype & LYD_NODE_TERM) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200970 LY_CHECK_RET(lyb_print_term((struct lyd_node_term *)node, out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200971 } else if (node->schema->nodetype & LYD_NODE_ANY) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200972 LY_CHECK_RET(lyb_print_anydata((struct lyd_node_any *)node, out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200973 } else {
Radek Krejci1798aae2020-07-14 13:26:06 +0200974 LOGINT_RET(lybctx->lybctx->ctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200975 }
976
977 /* recursively write all the descendants */
978 LY_LIST_FOR(lyd_node_children(node, 0), node) {
979 LY_CHECK_RET(lyb_print_subtree(out, node, &child_ht, lybctx));
980 }
981
982 /* finish this subtree */
Radek Krejci1798aae2020-07-14 13:26:06 +0200983 LY_CHECK_RET(lyb_write_stop_subtree(out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200984
985 return LY_SUCCESS;
986}
987
988LY_ERR
989lyb_print_data(struct ly_out *out, const struct lyd_node *root, int options)
990{
991 LY_ERR ret = LY_SUCCESS;
992 uint8_t zero = 0;
Michal Vasko60ea6352020-06-29 13:39:39 +0200993 struct hash_table *top_sibling_ht = NULL;
994 const struct lys_module *prev_mod = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +0200995 struct lyd_lyb_ctx *lybctx;
Michal Vasko30667352020-08-13 09:06:14 +0200996 const struct ly_ctx *ctx = root ? LYD_NODE_CTX(root) : NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200997
Radek Krejci1798aae2020-07-14 13:26:06 +0200998 lybctx = calloc(1, sizeof *lybctx);
999 LY_CHECK_ERR_RET(!lybctx, LOGMEM(ctx), LY_EMEM);
1000 lybctx->lybctx = calloc(1, sizeof *lybctx->lybctx);
1001 LY_CHECK_ERR_RET(!lybctx, LOGMEM(ctx), LY_EMEM);
1002
1003 lybctx->print_options = options;
Michal Vasko60ea6352020-06-29 13:39:39 +02001004 if (root) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001005 lybctx->lybctx->ctx = ctx;
Michal Vasko60ea6352020-06-29 13:39:39 +02001006
1007 if (root->schema && lysc_data_parent(root->schema)) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001008 LOGERR(lybctx->lybctx->ctx, LY_EINVAL, "LYB printer supports only printing top-level nodes.");
Michal Vasko9acaf492020-08-13 09:05:58 +02001009 ret = LY_EINVAL;
1010 goto cleanup;
Michal Vasko60ea6352020-06-29 13:39:39 +02001011 }
1012 }
1013
1014 /* LYB magic number */
Michal Vasko63f3d842020-07-08 10:10:14 +02001015 LY_CHECK_GOTO(ret = lyb_print_magic_number(out), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001016
1017 /* LYB header */
Michal Vasko63f3d842020-07-08 10:10:14 +02001018 LY_CHECK_GOTO(ret = lyb_print_header(out), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001019
1020 /* all used models */
Radek Krejci1798aae2020-07-14 13:26:06 +02001021 LY_CHECK_GOTO(ret = lyb_print_data_models(out, root, lybctx->lybctx), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001022
1023 LY_LIST_FOR(root, root) {
1024 /* do not reuse sibling hash tables from different modules */
1025 if (!root->schema || (root->schema->module != prev_mod)) {
1026 top_sibling_ht = NULL;
1027 prev_mod = root->schema ? root->schema->module : NULL;
1028 }
1029
Radek Krejci1798aae2020-07-14 13:26:06 +02001030 LY_CHECK_GOTO(ret = lyb_print_subtree(out, root, &top_sibling_ht, lybctx), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001031
Radek Krejci7931b192020-06-25 17:05:03 +02001032 if (!(options & LYD_PRINT_WITHSIBLINGS)) {
Michal Vasko60ea6352020-06-29 13:39:39 +02001033 break;
1034 }
1035 }
1036
1037 /* ending zero byte */
Radek Krejci1798aae2020-07-14 13:26:06 +02001038 LY_CHECK_GOTO(ret = lyb_write(out, &zero, sizeof zero, lybctx->lybctx), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001039
1040cleanup:
Radek Krejci1798aae2020-07-14 13:26:06 +02001041 lyd_lyb_ctx_free((struct lyd_ctx *)lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001042 return ret;
1043}