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