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