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