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