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