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