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