blob: 0cb070dd797542d50c25099338daa181cf2b2ac0 [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 "context.h"
27#include "hash_table.h"
Michal Vasko60ea6352020-06-29 13:39:39 +020028#include "log.h"
Radek Krejci7931b192020-06-25 17:05:03 +020029#include "parser_data.h"
Michal Vasko60ea6352020-06-29 13:39:39 +020030#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
Radek Krejci1798aae2020-07-14 13:26:06 +0200225lyb_write(struct ly_out *out, const uint8_t *buf, size_t count, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200226{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200227 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +0200228 struct lyd_lyb_subtree *full, *iter;
Michal Vasko5233e962020-08-14 14:26:20 +0200229 size_t to_write;
Michal Vasko60ea6352020-06-29 13:39:39 +0200230 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) {
Michal Vasko5233e962020-08-14 14:26:20 +0200251 LY_CHECK_RET(ly_write_(out, (char *)buf, to_write));
Michal Vasko60ea6352020-06-29 13:39:39 +0200252
253 LY_ARRAY_FOR(lybctx->subtrees, u) {
254 /* increase all written counters */
Michal Vasko5233e962020-08-14 14:26:20 +0200255 lybctx->subtrees[u].written += to_write;
Michal Vasko60ea6352020-06-29 13:39:39 +0200256 assert(lybctx->subtrees[u].written <= LYB_SIZE_MAX);
257 }
258 /* decrease count/buf */
Michal Vasko5233e962020-08-14 14:26:20 +0200259 count -= to_write;
260 buf += to_write;
Michal Vasko60ea6352020-06-29 13:39:39 +0200261 }
262
263 if (full) {
264 /* write the meta information (inner chunk count and chunk size) */
265 meta_buf[0] = full->written & 0xFF;
266 meta_buf[1] = full->inner_chunks & 0xFF;
Michal Vasko5233e962020-08-14 14:26:20 +0200267 LY_CHECK_RET(ly_write_skipped(out, full->position, (char *)meta_buf, LYB_META_BYTES));
Michal Vasko60ea6352020-06-29 13:39:39 +0200268
269 /* zero written and inner chunks */
270 full->written = 0;
271 full->inner_chunks = 0;
272
273 /* skip space for another chunk size */
Michal Vasko5233e962020-08-14 14:26:20 +0200274 LY_CHECK_RET(ly_write_skip(out, LYB_META_BYTES, &full->position));
Michal Vasko60ea6352020-06-29 13:39:39 +0200275
276 /* increase inner chunk count */
277 for (iter = &lybctx->subtrees[0]; iter != full; ++iter) {
278 if (iter->inner_chunks == LYB_INCHUNK_MAX) {
279 LOGINT(lybctx->ctx);
280 return LY_EINT;
281 }
282 ++iter->inner_chunks;
283 }
284 }
285 }
286
287 return LY_SUCCESS;
288}
289
290/**
291 * @brief Stop the current subtree - write its final metadata.
292 *
293 * @param[in] out Out structure.
294 * @param[in] lybctx LYB context.
295 * @return LY_ERR value.
296 */
297static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200298lyb_write_stop_subtree(struct ly_out *out, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200299{
Michal Vasko60ea6352020-06-29 13:39:39 +0200300 uint8_t meta_buf[LYB_META_BYTES];
301
302 /* write the meta chunk information */
303 meta_buf[0] = LYB_LAST_SUBTREE(lybctx).written & 0xFF;
304 meta_buf[1] = LYB_LAST_SUBTREE(lybctx).inner_chunks & 0xFF;
Michal Vasko5233e962020-08-14 14:26:20 +0200305 LY_CHECK_RET(ly_write_skipped(out, LYB_LAST_SUBTREE(lybctx).position, (char *)&meta_buf, LYB_META_BYTES));
Michal Vasko60ea6352020-06-29 13:39:39 +0200306
307 LY_ARRAY_DECREMENT(lybctx->subtrees);
308 return LY_SUCCESS;
309}
310
311/**
312 * @brief Start a new subtree - skip bytes for its metadata.
313 *
314 * @param[in] out Out structure.
315 * @param[in] lybctx LYB context.
316 * @return LY_ERR value.
317 */
318static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200319lyb_write_start_subtree(struct ly_out *out, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200320{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200321 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +0200322
323 if (!lybctx->subtrees) {
Radek Krejcif6d14cb2020-07-02 16:11:45 +0200324 assert(lybctx->subtree_size == 0);
Michal Vasko60ea6352020-06-29 13:39:39 +0200325 u = 0;
326 } else {
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200327 u = LY_ARRAY_COUNT(lybctx->subtrees);
Michal Vasko60ea6352020-06-29 13:39:39 +0200328 }
329 if (u == lybctx->subtree_size) {
330 LY_ARRAY_CREATE_RET(lybctx->ctx, lybctx->subtrees, u + LYB_SUBTREE_STEP, LY_EMEM);
331 lybctx->subtree_size = u + LYB_SUBTREE_STEP;
332 }
333
334 LY_ARRAY_INCREMENT(lybctx->subtrees);
335 LYB_LAST_SUBTREE(lybctx).written = 0;
336 LYB_LAST_SUBTREE(lybctx).inner_chunks = 0;
337
338 /* another inner chunk */
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200339 for (u = 0; u < LY_ARRAY_COUNT(lybctx->subtrees) - 1; ++u) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200340 if (lybctx->subtrees[u].inner_chunks == LYB_INCHUNK_MAX) {
341 LOGINT(lybctx->ctx);
Michal Vasko5233e962020-08-14 14:26:20 +0200342 return LY_EINT;
Michal Vasko60ea6352020-06-29 13:39:39 +0200343 }
344 ++lybctx->subtrees[u].inner_chunks;
345 }
346
Michal Vasko5233e962020-08-14 14:26:20 +0200347 LY_CHECK_RET(ly_write_skip(out, LYB_META_BYTES, &LYB_LAST_SUBTREE(lybctx).position));
Michal Vasko60ea6352020-06-29 13:39:39 +0200348
349 return LY_SUCCESS;
350}
351
352/**
353 * @brief Write a number.
354 *
355 * @param[in] num Number to write.
356 * @param[in] bytes Actual accessible bytes of @p num.
357 * @param[in] out Out structure.
358 * @param[in] lybctx LYB context.
359 * @return LY_ERR value.
360 */
361static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200362lyb_write_number(uint64_t num, size_t bytes, struct ly_out *out, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200363{
364 /* correct byte order */
365 num = htole64(num);
366
367 return lyb_write(out, (uint8_t *)&num, bytes, lybctx);
368}
369
370/**
371 * @brief Write a string.
372 *
373 * @param[in] str String to write.
374 * @param[in] str_len Length of @p str.
375 * @param[in] with_length Whether to precede the string with its length.
376 * @param[in] out Out structure.
377 * @param[in] lybctx LYB context.
378 * @return LY_ERR value.
379 */
380static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200381lyb_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 +0200382{
Michal Vasko60ea6352020-06-29 13:39:39 +0200383 if (!str) {
384 str = "";
Michal Vasko30667352020-08-13 09:06:14 +0200385 LY_CHECK_ERR_RET(str_len, LOGINT(lybctx->ctx), LY_EINT);
Michal Vasko60ea6352020-06-29 13:39:39 +0200386 }
387 if (!str_len) {
388 str_len = strlen(str);
389 }
390
391 if (with_length) {
392 /* print length on 2 bytes */
393 if (str_len > UINT16_MAX) {
394 LOGINT(lybctx->ctx);
395 return LY_EINT;
396 }
397 LY_CHECK_RET(lyb_write_number(str_len, 2, out, lybctx));
398 }
399
Michal Vasko63f3d842020-07-08 10:10:14 +0200400 LY_CHECK_RET(lyb_write(out, (const uint8_t *)str, str_len, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200401
402 return LY_SUCCESS;
403}
404
405/**
406 * @brief Print YANG module info.
407 *
408 * @param[in] out Out structure.
409 * @param[in] mod Module to print.
410 * @param[in] lybctx LYB context.
411 * @return LY_ERR value.
412 */
413static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200414lyb_print_model(struct ly_out *out, const struct lys_module *mod, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200415{
416 int r;
417 uint16_t revision;
418
419 /* model name length and model name */
420 if (mod) {
421 LY_CHECK_RET(lyb_write_string(mod->name, 0, 1, out, lybctx));
422 } else {
423 LY_CHECK_RET(lyb_write_string("", 0, 1, out, lybctx));
424 }
425
426 /* model revision as XXXX XXXX XXXX XXXX (2B) (year is offset from 2000)
427 * YYYY YYYM MMMD DDDD */
428 revision = 0;
429 if (mod && mod->revision) {
430 r = atoi(mod->revision);
431 r -= 2000;
432 r <<= 9;
433
434 revision |= r;
435
436 r = atoi(mod->revision + 5);
437 r <<= 5;
438
439 revision |= r;
440
441 r = atoi(mod->revision + 8);
442
443 revision |= r;
444 }
445 LY_CHECK_RET(lyb_write_number(revision, sizeof revision, out, lybctx));
446
447 return LY_SUCCESS;
448}
449
450/**
451 * @brief Print all used YANG modules.
452 *
453 * @param[in] out Out structure.
454 * @param[in] root Data root.
455 * @param[in] lybctx LYB context.
456 * @return LY_ERR value.
457 */
458static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200459lyb_print_data_models(struct ly_out *out, const struct lyd_node *root, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200460{
461 struct ly_set *set;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200462 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +0200463 LY_ERR ret = LY_SUCCESS;
464 struct lys_module *mod;
465 const struct lyd_node *node;
466 uint32_t i;
467
468 set = ly_set_new();
469 LY_CHECK_RET(!set, LY_EMEM);
470
471 /* collect all data node modules */
472 LY_LIST_FOR(root, node) {
473 if (!node->schema) {
474 continue;
475 }
476
477 mod = node->schema->module;
478 ly_set_add(set, mod, 0);
479
480 /* add also their modules deviating or augmenting them */
481 LY_ARRAY_FOR(mod->compiled->deviated_by, u) {
482 ly_set_add(set, mod->compiled->deviated_by[u], 0);
483 }
484 LY_ARRAY_FOR(mod->compiled->augmented_by, u) {
485 ly_set_add(set, mod->compiled->augmented_by[u], 0);
486 }
487 }
488
489 /* now write module count on 2 bytes */
490 LY_CHECK_GOTO(ret = lyb_write_number(set->count, 2, out, lybctx), cleanup);
491
492 /* and all the used models */
493 for (i = 0; i < set->count; ++i) {
494 LY_CHECK_GOTO(ret = lyb_print_model(out, set->objs[i], lybctx), cleanup);
495 }
496
497cleanup:
498 ly_set_free(set, NULL);
499 return ret;
500}
501
502/**
503 * @brief Print LYB magic number.
504 *
505 * @param[in] out Out structure.
Michal Vasko60ea6352020-06-29 13:39:39 +0200506 * @return LY_ERR value.
507 */
508static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200509lyb_print_magic_number(struct ly_out *out)
Michal Vasko60ea6352020-06-29 13:39:39 +0200510{
Michal Vasko60ea6352020-06-29 13:39:39 +0200511 uint32_t magic_number;
512
513 /* 'l', 'y', 'b' - 0x6c7962 */
514 ((char *)&magic_number)[0] = 'l';
515 ((char *)&magic_number)[1] = 'y';
516 ((char *)&magic_number)[2] = 'b';
517
Michal Vasko5233e962020-08-14 14:26:20 +0200518 LY_CHECK_RET(ly_write_(out, (char *)&magic_number, 3));
Michal Vasko60ea6352020-06-29 13:39:39 +0200519
520 return LY_SUCCESS;
521}
522
523/**
524 * @brief Print LYB header.
525 *
526 * @param[in] out Out structure.
Michal Vasko60ea6352020-06-29 13:39:39 +0200527 * @return LY_ERR value.
528 */
529static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200530lyb_print_header(struct ly_out *out)
Michal Vasko60ea6352020-06-29 13:39:39 +0200531{
Michal Vasko60ea6352020-06-29 13:39:39 +0200532 uint8_t byte = 0;
533
534 /* version, future flags */
535 byte |= LYB_VERSION_NUM;
536
Michal Vasko5233e962020-08-14 14:26:20 +0200537 LY_CHECK_RET(ly_write_(out, (char *)&byte, 1));
Michal Vasko60ea6352020-06-29 13:39:39 +0200538
539 return LY_SUCCESS;
540}
541
542/**
543 * @brief Print opaque prefixes.
544 *
545 * @param[in] out Out structure.
546 * @param[in] prefs Prefixes to print.
547 * @param[in] lybctx LYB context.
548 * @return LY_ERR value.
549 */
550static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200551lyb_print_opaq_prefixes(struct ly_out *out, const struct ly_prefix *prefs, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200552{
553 uint8_t count;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200554 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +0200555
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200556 if (prefs && (LY_ARRAY_COUNT(prefs) > UINT8_MAX)) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200557 LOGERR(lybctx->ctx, LY_EINT, "Maximum supported number of prefixes is %u.", UINT8_MAX);
558 return LY_EINT;
559 }
560
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200561 count = prefs ? LY_ARRAY_COUNT(prefs) : 0;
Michal Vasko60ea6352020-06-29 13:39:39 +0200562
563 /* write number of prefixes on 1 byte */
564 LY_CHECK_RET(lyb_write(out, &count, 1, lybctx));
565
566 /* write all the prefixes */
567 LY_ARRAY_FOR(prefs, u) {
568 /* prefix */
Radek Krejci1798aae2020-07-14 13:26:06 +0200569 LY_CHECK_RET(lyb_write_string(prefs[u].id, 0, 1, out, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200570
571 /* namespace */
Radek Krejci1798aae2020-07-14 13:26:06 +0200572 LY_CHECK_RET(lyb_write_string(prefs[u].module_name, 0, 1, out, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200573 }
574
575 return LY_SUCCESS;
576}
577
578/**
579 * @brief Print opaque node.
580 *
581 * @param[in] opaq Node to print.
582 * @param[in] out Out structure.
583 * @param[in] lybctx LYB context.
584 * @return LY_ERR value.
585 */
586static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200587lyb_print_opaq(struct lyd_node_opaq *opaq, struct ly_out *out, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200588{
589 /* prefix */
Radek Krejci1798aae2020-07-14 13:26:06 +0200590 LY_CHECK_RET(lyb_write_string(opaq->prefix.id, 0, 1, out, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200591
Radek Krejci1798aae2020-07-14 13:26:06 +0200592 /* module reference */
593 LY_CHECK_RET(lyb_write_string(opaq->prefix.module_name, 0, 1, out, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200594
595 /* name */
596 LY_CHECK_RET(lyb_write_string(opaq->name, 0, 1, out, lybctx));
597
598 /* value prefixes */
599 LY_CHECK_RET(lyb_print_opaq_prefixes(out, opaq->val_prefs, lybctx));
600
601 /* format */
602 LY_CHECK_RET(lyb_write_number(opaq->format, 1, out, lybctx));
603
604 /* value */
605 LY_CHECK_RET(lyb_write_string(opaq->value, 0, 0, out, lybctx));
606
607 return LY_SUCCESS;
608}
609
610/**
611 * @brief Print anydata node.
612 *
613 * @param[in] anydata Node to print.
614 * @param[in] out Out structure.
615 * @param[in] lybctx LYB context.
616 * @return LY_ERR value.
617 */
618static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200619lyb_print_anydata(struct lyd_node_any *anydata, struct ly_out *out, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200620{
621 LY_ERR ret = LY_SUCCESS;
622 LYD_ANYDATA_VALUETYPE value_type;
623 int len;
624 char *buf = NULL;
625 const char *str;
626 struct ly_out *out2 = NULL;
627
628 if (anydata->value_type == LYD_ANYDATA_DATATREE) {
629 /* will be printed as a nested LYB data tree */
630 value_type = LYD_ANYDATA_LYB;
631 } else {
632 value_type = anydata->value_type;
633 }
634
635 /* first byte is type */
636 LY_CHECK_GOTO(ret = lyb_write(out, (uint8_t *)&value_type, sizeof value_type, lybctx), cleanup);
637
638 if (anydata->value_type == LYD_ANYDATA_DATATREE) {
639 /* print LYB data tree to memory */
640 LY_CHECK_GOTO(ret = ly_out_new_memory(&buf, 0, &out2), cleanup);
Radek Krejci7931b192020-06-25 17:05:03 +0200641 LY_CHECK_GOTO(ret = lyb_print_data(out2, anydata->value.tree, LYD_PRINT_WITHSIBLINGS), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200642
643 len = lyd_lyb_data_length(buf);
644 assert(len != -1);
645 str = buf;
646 } else if (anydata->value_type == LYD_ANYDATA_LYB) {
647 len = lyd_lyb_data_length(anydata->value.mem);
648 assert(len != -1);
649 str = anydata->value.mem;
650 } else {
651 len = strlen(anydata->value.str);
652 str = anydata->value.str;
653 }
654
655 /* followed by the content */
656 LY_CHECK_GOTO(ret = lyb_write_string(str, (size_t)len, 0, out, lybctx), cleanup);
657
658cleanup:
659 ly_out_free(out2, NULL, 1);
660 return ret;
661}
662
663/**
664 * @brief Print term node.
665 *
666 * @param[in] term Node to print.
667 * @param[in] out Out structure.
668 * @param[in] lybctx LYB context.
669 * @return LY_ERR value.
670 */
671static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200672lyb_print_term(struct lyd_node_term *term, struct ly_out *out, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200673{
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200674 /* print the value */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200675 return lyb_write_string(LYD_CANON_VALUE(term), 0, 0, out, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200676}
677
678/**
679 * @brief Print YANG node metadata.
680 *
681 * @param[in] out Out structure.
682 * @param[in] node Data node whose metadata to print.
683 * @param[in] lybctx LYB context.
684 * @return LY_ERR value.
685 */
686static LY_ERR
687lyb_print_metadata(struct ly_out *out, const struct lyd_node *node, struct lyd_lyb_ctx *lybctx)
688{
Michal Vasko60ea6352020-06-29 13:39:39 +0200689 uint8_t count = 0;
690 const struct lys_module *wd_mod = NULL;
691 struct lyd_meta *iter;
Michal Vasko60ea6352020-06-29 13:39:39 +0200692
693 /* with-defaults */
694 if (node->schema->nodetype & LYD_NODE_TERM) {
Radek Krejci7931b192020-06-25 17:05:03 +0200695 if (((node->flags & LYD_DEFAULT) && (lybctx->print_options & (LYD_PRINT_WD_ALL_TAG | LYD_PRINT_WD_IMPL_TAG))) ||
696 ((lybctx->print_options & LYD_PRINT_WD_ALL_TAG) && ly_is_default(node))) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200697 /* we have implicit OR explicit default node, print attribute only if context include with-defaults schema */
698 wd_mod = ly_ctx_get_module_latest(node->schema->module->ctx, "ietf-netconf-with-defaults");
699 }
700 }
701
702 /* count metadata */
703 if (wd_mod) {
704 ++count;
705 }
706 for (iter = node->meta; iter; iter = iter->next) {
707 if (count == UINT8_MAX) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200708 LOGERR(lybctx->lybctx->ctx, LY_EINT, "Maximum supported number of data node metadata is %u.", UINT8_MAX);
Michal Vasko60ea6352020-06-29 13:39:39 +0200709 return LY_EINT;
710 }
711 ++count;
712 }
713
714 /* write number of metadata on 1 byte */
Radek Krejci1798aae2020-07-14 13:26:06 +0200715 LY_CHECK_RET(lyb_write(out, &count, 1, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200716
717 if (wd_mod) {
718 /* write the "default" metadata */
Radek Krejci1798aae2020-07-14 13:26:06 +0200719 LY_CHECK_RET(lyb_write_start_subtree(out, lybctx->lybctx));
720 LY_CHECK_RET(lyb_print_model(out, wd_mod, lybctx->lybctx));
721 LY_CHECK_RET(lyb_write_string("default", 0, 1, out, lybctx->lybctx));
722 LY_CHECK_RET(lyb_write_string("true", 0, 0, out, lybctx->lybctx));
723 LY_CHECK_RET(lyb_write_stop_subtree(out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200724 }
725
726 /* write all the node metadata */
727 LY_LIST_FOR(node->meta, iter) {
728 /* each metadata is a subtree */
Radek Krejci1798aae2020-07-14 13:26:06 +0200729 LY_CHECK_RET(lyb_write_start_subtree(out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200730
731 /* model */
Radek Krejci1798aae2020-07-14 13:26:06 +0200732 LY_CHECK_RET(lyb_print_model(out, iter->annotation->module, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200733
734 /* annotation name with length */
Radek Krejci1798aae2020-07-14 13:26:06 +0200735 LY_CHECK_RET(lyb_write_string(iter->name, 0, 1, out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200736
Michal Vasko60ea6352020-06-29 13:39:39 +0200737 /* metadata value */
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200738 LY_CHECK_RET(lyb_write_string(iter->value.canonical, 0, 0, out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200739
740 /* finish metadata subtree */
Radek Krejci1798aae2020-07-14 13:26:06 +0200741 LY_CHECK_RET(lyb_write_stop_subtree(out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200742 }
743
744 return LY_SUCCESS;
745}
746
747/**
748 * @brief Print opaque node attributes.
749 *
750 * @param[in] out Out structure.
751 * @param[in] node Opaque node whose attributes to print.
752 * @param[in] lybctx LYB context.
753 * @return LY_ERR value.
754 */
755static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200756lyb_print_attributes(struct ly_out *out, const struct lyd_node_opaq *node, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200757{
758 uint8_t count = 0;
Radek Krejci5536d282020-08-04 23:27:44 +0200759 struct lyd_attr *iter;
Michal Vasko60ea6352020-06-29 13:39:39 +0200760
761 for (iter = node->attr; iter; iter = iter->next) {
762 if (count == UINT8_MAX) {
763 LOGERR(lybctx->ctx, LY_EINT, "Maximum supported number of data node attributes is %u.", UINT8_MAX);
764 return LY_EINT;
765 }
766 ++count;
767 }
768
769 /* write number of attributes on 1 byte */
770 LY_CHECK_RET(lyb_write(out, &count, 1, lybctx));
771
772 /* write all the attributes */
773 LY_LIST_FOR(node->attr, iter) {
774 /* each attribute is a subtree */
775 LY_CHECK_RET(lyb_write_start_subtree(out, lybctx));
776
777 /* prefix */
Radek Krejci1798aae2020-07-14 13:26:06 +0200778 LY_CHECK_RET(lyb_write_string(iter->prefix.id, 0, 1, out, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200779
780 /* namespace */
Radek Krejci1798aae2020-07-14 13:26:06 +0200781 LY_CHECK_RET(lyb_write_string(iter->prefix.module_name, 0, 1, out, lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200782
783 /* name */
784 LY_CHECK_RET(lyb_write_string(iter->name, 0, 1, out, lybctx));
785
786 /* value prefixes */
787 LY_CHECK_RET(lyb_print_opaq_prefixes(out, iter->val_prefs, lybctx));
788
789 /* format */
790 LY_CHECK_RET(lyb_write_number(iter->format, 1, out, lybctx));
791
792 /* value */
793 LY_CHECK_RET(lyb_write_string(iter->value, 0, 0, out, lybctx));
794
795 /* finish attribute subtree */
796 LY_CHECK_RET(lyb_write_stop_subtree(out, lybctx));
797 }
798
799 return LY_SUCCESS;
800}
801
802/**
803 * @brief Print schema node hash.
804 *
805 * @param[in] out Out structure.
806 * @param[in] schema Schema node whose hash to print.
807 * @param[in,out] sibling_ht Cached hash table for these siblings, created if NULL.
808 * @param[in] lybctx LYB context.
809 * @return LY_ERR value.
810 */
811static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200812lyb_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 +0200813{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200814 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +0200815 uint32_t i;
816 LYB_HASH hash;
817 struct lyd_lyb_sib_ht *sib_ht;
818 struct lysc_node *first_sibling;
819
820 if (!schema) {
821 /* opaque node, write empty hash */
822 hash = 0;
823 LY_CHECK_RET(lyb_write(out, &hash, sizeof hash, lybctx));
824 return LY_SUCCESS;
825 }
826
827 /* create whole sibling HT if not already created and saved */
828 if (!*sibling_ht) {
829 /* get first schema data sibling (or input/output) */
830 first_sibling = (struct lysc_node *)lys_getnext(NULL, lysc_data_parent(schema), schema->module->compiled, 0);
831 LY_ARRAY_FOR(lybctx->sib_hts, u) {
832 if (lybctx->sib_hts[u].first_sibling == first_sibling) {
833 /* we have already created a hash table for these siblings */
834 *sibling_ht = lybctx->sib_hts[u].ht;
835 break;
836 }
837 }
838
839 if (!*sibling_ht) {
840 /* we must create sibling hash table */
841 LY_CHECK_RET(lyb_hash_siblings(first_sibling, sibling_ht));
842
843 /* and save it */
844 LY_ARRAY_NEW_RET(lybctx->ctx, lybctx->sib_hts, sib_ht, LY_EMEM);
845
846 sib_ht->first_sibling = first_sibling;
847 sib_ht->ht = *sibling_ht;
848 }
849 }
850
851 /* get our hash */
852 LY_CHECK_RET(lyb_hash_find(*sibling_ht, schema, &hash));
853
854 /* write the hash */
855 LY_CHECK_RET(lyb_write(out, &hash, sizeof hash, lybctx));
856
857 if (hash & LYB_HASH_COLLISION_ID) {
858 /* no collision for this hash, we are done */
859 return LY_SUCCESS;
860 }
861
862 /* written hash was a collision, write also all the preceding hashes */
863 for (i = 0; !(hash & (LYB_HASH_COLLISION_ID >> i)); ++i);
864
865 for (; i; --i) {
866 hash = lyb_hash(schema, i - 1);
867 if (!hash) {
868 return LY_EINT;
869 }
870 assert(hash & (LYB_HASH_COLLISION_ID >> (i - 1)));
871
872 LY_CHECK_RET(lyb_write(out, &hash, sizeof hash, lybctx));
873 }
874
875 return LY_SUCCESS;
876}
877
878/**
879 * @brief Print data subtree.
880 *
881 * @param[in] out Out structure.
882 * @param[in] node Root node of the subtree to print.
883 * @param[in,out] sibling_ht Cached hash table for these data siblings, created if NULL.
884 * @param[in] lybctx LYB context.
885 * @return LY_ERR value.
886 */
887static LY_ERR
888lyb_print_subtree(struct ly_out *out, const struct lyd_node *node, struct hash_table **sibling_ht, struct lyd_lyb_ctx *lybctx)
889{
890 struct hash_table *child_ht = NULL;
891
892 /* register a new subtree */
Radek Krejci1798aae2020-07-14 13:26:06 +0200893 LY_CHECK_RET(lyb_write_start_subtree(out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200894
895 /* write model info first */
896 if (!node->schema && !((struct lyd_node_opaq *)node)->parent) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200897 LY_CHECK_RET(lyb_print_model(out, NULL, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200898 } else if (node->schema && !lysc_data_parent(node->schema)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200899 LY_CHECK_RET(lyb_print_model(out, node->schema->module, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200900 }
901
902 /* write schema hash */
Radek Krejci1798aae2020-07-14 13:26:06 +0200903 LY_CHECK_RET(lyb_print_schema_hash(out, (struct lysc_node *)node->schema, sibling_ht, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200904
905 /* write any metadata/attributes */
906 if (node->schema) {
907 LY_CHECK_RET(lyb_print_metadata(out, node, lybctx));
908 } else {
Radek Krejci1798aae2020-07-14 13:26:06 +0200909 LY_CHECK_RET(lyb_print_attributes(out, (struct lyd_node_opaq *)node, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200910 }
911
912 /* write node content */
913 if (!node->schema) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200914 LY_CHECK_RET(lyb_print_opaq((struct lyd_node_opaq *)node, out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200915 } else if (node->schema->nodetype & LYD_NODE_INNER) {
916 /* nothing to write */
917 } else if (node->schema->nodetype & LYD_NODE_TERM) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200918 LY_CHECK_RET(lyb_print_term((struct lyd_node_term *)node, out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200919 } else if (node->schema->nodetype & LYD_NODE_ANY) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200920 LY_CHECK_RET(lyb_print_anydata((struct lyd_node_any *)node, out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200921 } else {
Radek Krejci1798aae2020-07-14 13:26:06 +0200922 LOGINT_RET(lybctx->lybctx->ctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200923 }
924
925 /* recursively write all the descendants */
926 LY_LIST_FOR(lyd_node_children(node, 0), node) {
927 LY_CHECK_RET(lyb_print_subtree(out, node, &child_ht, lybctx));
928 }
929
930 /* finish this subtree */
Radek Krejci1798aae2020-07-14 13:26:06 +0200931 LY_CHECK_RET(lyb_write_stop_subtree(out, lybctx->lybctx));
Michal Vasko60ea6352020-06-29 13:39:39 +0200932
933 return LY_SUCCESS;
934}
935
936LY_ERR
937lyb_print_data(struct ly_out *out, const struct lyd_node *root, int options)
938{
939 LY_ERR ret = LY_SUCCESS;
940 uint8_t zero = 0;
Michal Vasko60ea6352020-06-29 13:39:39 +0200941 struct hash_table *top_sibling_ht = NULL;
942 const struct lys_module *prev_mod = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +0200943 struct lyd_lyb_ctx *lybctx;
Michal Vaskob7be7a82020-08-20 09:09:04 +0200944 const struct ly_ctx *ctx = root ? LYD_CTX(root) : NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200945
Radek Krejci1798aae2020-07-14 13:26:06 +0200946 lybctx = calloc(1, sizeof *lybctx);
947 LY_CHECK_ERR_RET(!lybctx, LOGMEM(ctx), LY_EMEM);
948 lybctx->lybctx = calloc(1, sizeof *lybctx->lybctx);
949 LY_CHECK_ERR_RET(!lybctx, LOGMEM(ctx), LY_EMEM);
950
951 lybctx->print_options = options;
Michal Vasko60ea6352020-06-29 13:39:39 +0200952 if (root) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200953 lybctx->lybctx->ctx = ctx;
Michal Vasko60ea6352020-06-29 13:39:39 +0200954
955 if (root->schema && lysc_data_parent(root->schema)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200956 LOGERR(lybctx->lybctx->ctx, LY_EINVAL, "LYB printer supports only printing top-level nodes.");
Michal Vasko9acaf492020-08-13 09:05:58 +0200957 ret = LY_EINVAL;
958 goto cleanup;
Michal Vasko60ea6352020-06-29 13:39:39 +0200959 }
960 }
961
962 /* LYB magic number */
Michal Vasko63f3d842020-07-08 10:10:14 +0200963 LY_CHECK_GOTO(ret = lyb_print_magic_number(out), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200964
965 /* LYB header */
Michal Vasko63f3d842020-07-08 10:10:14 +0200966 LY_CHECK_GOTO(ret = lyb_print_header(out), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200967
968 /* all used models */
Radek Krejci1798aae2020-07-14 13:26:06 +0200969 LY_CHECK_GOTO(ret = lyb_print_data_models(out, root, lybctx->lybctx), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200970
971 LY_LIST_FOR(root, root) {
972 /* do not reuse sibling hash tables from different modules */
973 if (!root->schema || (root->schema->module != prev_mod)) {
974 top_sibling_ht = NULL;
975 prev_mod = root->schema ? root->schema->module : NULL;
976 }
977
Radek Krejci1798aae2020-07-14 13:26:06 +0200978 LY_CHECK_GOTO(ret = lyb_print_subtree(out, root, &top_sibling_ht, lybctx), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200979
Radek Krejci7931b192020-06-25 17:05:03 +0200980 if (!(options & LYD_PRINT_WITHSIBLINGS)) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200981 break;
982 }
983 }
984
985 /* ending zero byte */
Radek Krejci1798aae2020-07-14 13:26:06 +0200986 LY_CHECK_GOTO(ret = lyb_write(out, &zero, sizeof zero, lybctx->lybctx), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200987
988cleanup:
Radek Krejci1798aae2020-07-14 13:26:06 +0200989 lyd_lyb_ctx_free((struct lyd_ctx *)lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200990 return ret;
991}