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