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