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