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