Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file parser_lyb.c |
| 3 | * @author Michal Vasko <mvasko@cesnet.cz> |
| 4 | * @brief LYB data parser for libyang |
| 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> |
Radek Krejci | ad97c5f | 2020-06-30 09:19:28 +0200 | [diff] [blame] | 18 | #include <stdint.h> |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
| 22 | |
| 23 | #include "common.h" |
| 24 | #include "compat.h" |
Radek Krejci | ad97c5f | 2020-06-30 09:19:28 +0200 | [diff] [blame] | 25 | #include "context.h" |
| 26 | #include "dict.h" |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 27 | #include "hash_table.h" |
| 28 | #include "in.h" |
Michal Vasko | afac782 | 2020-10-20 14:22:26 +0200 | [diff] [blame] | 29 | #include "in_internal.h" |
Radek Krejci | ad97c5f | 2020-06-30 09:19:28 +0200 | [diff] [blame] | 30 | #include "log.h" |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 31 | #include "parser_data.h" |
| 32 | #include "parser_internal.h" |
Radek Krejci | 7711410 | 2021-03-10 15:21:57 +0100 | [diff] [blame] | 33 | #include "set.h" |
Radek Krejci | ad97c5f | 2020-06-30 09:19:28 +0200 | [diff] [blame] | 34 | #include "tree.h" |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 35 | #include "tree_data.h" |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 36 | #include "tree_data_internal.h" |
Radek Krejci | 859a15a | 2021-03-05 20:56:59 +0100 | [diff] [blame] | 37 | #include "tree_edit.h" |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 38 | #include "tree_schema.h" |
| 39 | #include "validation.h" |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 40 | #include "xml.h" |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 41 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 42 | void |
| 43 | lylyb_ctx_free(struct lylyb_ctx *ctx) |
| 44 | { |
| 45 | LY_ARRAY_COUNT_TYPE u; |
| 46 | |
| 47 | LY_ARRAY_FREE(ctx->subtrees); |
| 48 | LY_ARRAY_FREE(ctx->models); |
| 49 | |
| 50 | LY_ARRAY_FOR(ctx->sib_hts, u) { |
| 51 | lyht_free(ctx->sib_hts[u].ht); |
| 52 | } |
| 53 | LY_ARRAY_FREE(ctx->sib_hts); |
| 54 | |
| 55 | free(ctx); |
| 56 | } |
| 57 | |
| 58 | void |
| 59 | lyd_lyb_ctx_free(struct lyd_ctx *lydctx) |
| 60 | { |
| 61 | struct lyd_lyb_ctx *ctx = (struct lyd_lyb_ctx *)lydctx; |
| 62 | |
| 63 | lyd_ctx_free(lydctx); |
| 64 | lylyb_ctx_free(ctx->lybctx); |
| 65 | free(ctx); |
| 66 | } |
| 67 | |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 68 | /** |
| 69 | * @brief Read YANG data from LYB input. Metadata are handled transparently and not returned. |
| 70 | * |
| 71 | * @param[in] buf Destination buffer. |
| 72 | * @param[in] count Number of bytes to read. |
| 73 | * @param[in] lybctx LYB context. |
| 74 | */ |
| 75 | static void |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 76 | lyb_read(uint8_t *buf, size_t count, struct lylyb_ctx *lybctx) |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 77 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 78 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 79 | struct lyd_lyb_subtree *empty; |
| 80 | size_t to_read; |
| 81 | uint8_t meta_buf[LYB_META_BYTES]; |
| 82 | |
| 83 | assert(lybctx); |
| 84 | |
| 85 | while (1) { |
| 86 | /* check for fully-read (empty) data chunks */ |
| 87 | to_read = count; |
| 88 | empty = NULL; |
| 89 | LY_ARRAY_FOR(lybctx->subtrees, u) { |
| 90 | /* we want the innermost chunks resolved first, so replace previous empty chunks, |
| 91 | * also ignore chunks that are completely finished, there is nothing for us to do */ |
| 92 | if ((lybctx->subtrees[u].written <= to_read) && lybctx->subtrees[u].position) { |
| 93 | /* empty chunk, do not read more */ |
| 94 | to_read = lybctx->subtrees[u].written; |
| 95 | empty = &lybctx->subtrees[u]; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | if (!empty && !count) { |
| 100 | break; |
| 101 | } |
| 102 | |
| 103 | /* we are actually reading some data, not just finishing another chunk */ |
| 104 | if (to_read) { |
| 105 | if (buf) { |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 106 | ly_in_read(lybctx->in, buf, to_read); |
| 107 | } else { |
| 108 | ly_in_skip(lybctx->in, to_read); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | LY_ARRAY_FOR(lybctx->subtrees, u) { |
| 112 | /* decrease all written counters */ |
| 113 | lybctx->subtrees[u].written -= to_read; |
| 114 | assert(lybctx->subtrees[u].written <= LYB_SIZE_MAX); |
| 115 | } |
| 116 | /* decrease count/buf */ |
| 117 | count -= to_read; |
| 118 | if (buf) { |
| 119 | buf += to_read; |
| 120 | } |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | if (empty) { |
| 124 | /* read the next chunk meta information */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 125 | ly_in_read(lybctx->in, meta_buf, LYB_META_BYTES); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 126 | empty->written = meta_buf[0]; |
| 127 | empty->inner_chunks = meta_buf[1]; |
| 128 | |
| 129 | /* remember whether there is a following chunk or not */ |
| 130 | empty->position = (empty->written == LYB_SIZE_MAX ? 1 : 0); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 131 | } |
| 132 | } |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | /** |
| 136 | * @brief Read a number. |
| 137 | * |
| 138 | * @param[in] num Destination buffer. |
| 139 | * @param[in] num_size Size of @p num. |
| 140 | * @param[in] bytes Number of bytes to read. |
| 141 | * @param[in] lybctx LYB context. |
| 142 | */ |
| 143 | static void |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 144 | lyb_read_number(void *num, size_t num_size, size_t bytes, struct lylyb_ctx *lybctx) |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 145 | { |
| 146 | uint64_t buf = 0; |
| 147 | |
| 148 | lyb_read((uint8_t *)&buf, bytes, lybctx); |
| 149 | |
| 150 | /* correct byte order */ |
| 151 | buf = le64toh(buf); |
| 152 | |
| 153 | switch (num_size) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 154 | case sizeof(uint8_t): |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 155 | *((uint8_t *)num) = buf; |
| 156 | break; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 157 | case sizeof(uint16_t): |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 158 | *((uint16_t *)num) = buf; |
| 159 | break; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 160 | case sizeof(uint32_t): |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 161 | *((uint32_t *)num) = buf; |
| 162 | break; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 163 | case sizeof(uint64_t): |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 164 | *((uint64_t *)num) = buf; |
| 165 | break; |
| 166 | default: |
| 167 | LOGINT(lybctx->ctx); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * @brief Read a string. |
| 173 | * |
| 174 | * @param[in] str Destination buffer, is allocated. |
| 175 | * @param[in] with_length Whether the string is preceded with its length or it ends at the end of this subtree. |
| 176 | * @param[in] lybctx LYB context. |
| 177 | * @return LY_ERR value. |
| 178 | */ |
| 179 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 180 | lyb_read_string(char **str, ly_bool with_length, struct lylyb_ctx *lybctx) |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 181 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 182 | ly_bool next_chunk = 0; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 183 | size_t len = 0, cur_len; |
| 184 | |
| 185 | *str = NULL; |
| 186 | |
| 187 | if (with_length) { |
| 188 | lyb_read_number(&len, sizeof len, 2, lybctx); |
| 189 | } else { |
| 190 | /* read until the end of this subtree */ |
| 191 | len = LYB_LAST_SUBTREE(lybctx).written; |
| 192 | if (LYB_LAST_SUBTREE(lybctx).position) { |
| 193 | next_chunk = 1; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | *str = malloc((len + 1) * sizeof **str); |
| 198 | LY_CHECK_ERR_RET(!*str, LOGMEM(lybctx->ctx), LY_EMEM); |
| 199 | |
| 200 | lyb_read((uint8_t *)*str, len, lybctx); |
| 201 | |
| 202 | while (next_chunk) { |
| 203 | cur_len = LYB_LAST_SUBTREE(lybctx).written; |
| 204 | if (LYB_LAST_SUBTREE(lybctx).position) { |
| 205 | next_chunk = 1; |
| 206 | } else { |
| 207 | next_chunk = 0; |
| 208 | } |
| 209 | |
| 210 | *str = ly_realloc(*str, (len + cur_len + 1) * sizeof **str); |
| 211 | LY_CHECK_ERR_RET(!*str, LOGMEM(lybctx->ctx), LY_EMEM); |
| 212 | |
| 213 | lyb_read(((uint8_t *)*str) + len, cur_len, lybctx); |
| 214 | |
| 215 | len += cur_len; |
| 216 | } |
| 217 | |
Michal Vasko | cebbae5 | 2021-05-31 11:11:36 +0200 | [diff] [blame] | 218 | (*str)[len] = '\0'; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 219 | return LY_SUCCESS; |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * @brief Stop the current subtree - change LYB context state. |
| 224 | * |
| 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_read_stop_subtree(struct lylyb_ctx *lybctx) |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 230 | { |
| 231 | if (LYB_LAST_SUBTREE(lybctx).written) { |
| 232 | LOGINT_RET(lybctx->ctx); |
| 233 | } |
| 234 | |
| 235 | LY_ARRAY_DECREMENT(lybctx->subtrees); |
| 236 | return LY_SUCCESS; |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * @brief Start a new subtree - change LYB context state but also read the expected metadata. |
| 241 | * |
| 242 | * @param[in] lybctx LYB context. |
| 243 | * @return LY_ERR value. |
| 244 | */ |
| 245 | static LY_ERR |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 246 | lyb_read_start_subtree(struct lylyb_ctx *lybctx) |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 247 | { |
| 248 | uint8_t meta_buf[LYB_META_BYTES]; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 249 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 250 | |
Radek Krejci | c7d13e3 | 2020-12-09 12:32:24 +0100 | [diff] [blame] | 251 | u = LY_ARRAY_COUNT(lybctx->subtrees); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 252 | if (u == lybctx->subtree_size) { |
| 253 | LY_ARRAY_CREATE_RET(lybctx->ctx, lybctx->subtrees, u + LYB_SUBTREE_STEP, LY_EMEM); |
| 254 | lybctx->subtree_size = u + LYB_SUBTREE_STEP; |
| 255 | } |
| 256 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 257 | LY_CHECK_RET(ly_in_read(lybctx->in, meta_buf, LYB_META_BYTES)); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 258 | |
| 259 | LY_ARRAY_INCREMENT(lybctx->subtrees); |
| 260 | LYB_LAST_SUBTREE(lybctx).written = meta_buf[0]; |
| 261 | LYB_LAST_SUBTREE(lybctx).inner_chunks = meta_buf[LYB_SIZE_BYTES]; |
| 262 | LYB_LAST_SUBTREE(lybctx).position = (LYB_LAST_SUBTREE(lybctx).written == LYB_SIZE_MAX ? 1 : 0); |
| 263 | |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 264 | return LY_SUCCESS; |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * @brief Parse YANG model info. |
| 269 | * |
| 270 | * @param[in] lybctx LYB context. |
| 271 | * @param[out] mod Parsed module. |
| 272 | * @return LY_ERR value. |
| 273 | */ |
| 274 | static LY_ERR |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 275 | lyb_parse_model(struct lylyb_ctx *lybctx, uint32_t parse_options, const struct lys_module **mod) |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 276 | { |
| 277 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 278 | char *mod_name = NULL, mod_rev[LY_REV_SIZE]; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 279 | uint16_t rev; |
| 280 | |
| 281 | /* model name */ |
| 282 | ret = lyb_read_string(&mod_name, 1, lybctx); |
| 283 | LY_CHECK_GOTO(ret, cleanup); |
| 284 | |
| 285 | /* revision */ |
| 286 | lyb_read_number(&rev, sizeof rev, 2, lybctx); |
| 287 | |
| 288 | if (!mod_name[0]) { |
| 289 | /* opaq node, no module */ |
| 290 | *mod = NULL; |
| 291 | goto cleanup; |
| 292 | } |
| 293 | |
| 294 | if (rev) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 295 | sprintf(mod_rev, "%04u-%02u-%02u", ((rev & LYB_REV_YEAR_MASK) >> LYB_REV_YEAR_SHIFT) + LYB_REV_YEAR_OFFSET, |
| 296 | (rev & LYB_REV_MONTH_MASK) >> LYB_REV_MONTH_SHIFT, rev & LYB_REV_DAY_MASK); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 297 | *mod = ly_ctx_get_module(lybctx->ctx, mod_name, mod_rev); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 298 | if ((parse_options & LYD_PARSE_LYB_MOD_UPDATE) && !(*mod)) { |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 299 | /* try to use an updated module */ |
| 300 | *mod = ly_ctx_get_module_implemented(lybctx->ctx, mod_name); |
| 301 | if (*mod && (!(*mod)->revision || (strcmp((*mod)->revision, mod_rev) < 0))) { |
| 302 | /* not an implemented module in a newer revision */ |
| 303 | *mod = NULL; |
| 304 | } |
| 305 | } |
| 306 | } else { |
| 307 | *mod = ly_ctx_get_module_latest(lybctx->ctx, mod_name); |
| 308 | } |
| 309 | /* TODO data_clb supported? |
| 310 | if (lybctx->ctx->data_clb) { |
| 311 | if (!*mod) { |
| 312 | *mod = lybctx->ctx->data_clb(lybctx->ctx, mod_name, NULL, 0, lybctx->ctx->data_clb_data); |
| 313 | } else if (!(*mod)->implemented) { |
| 314 | *mod = lybctx->ctx->data_clb(lybctx->ctx, mod_name, (*mod)->ns, LY_MODCLB_NOT_IMPLEMENTED, lybctx->ctx->data_clb_data); |
| 315 | } |
| 316 | }*/ |
| 317 | |
| 318 | if (!*mod || !(*mod)->implemented) { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 319 | if (parse_options & LYD_PARSE_STRICT) { |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 320 | if (!*mod) { |
| 321 | LOGERR(lybctx->ctx, LY_EINVAL, "Invalid context for LYB data parsing, missing module \"%s%s%s\".", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 322 | mod_name, rev ? "@" : "", rev ? mod_rev : ""); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 323 | } else if (!(*mod)->implemented) { |
| 324 | LOGERR(lybctx->ctx, LY_EINVAL, "Invalid context for LYB data parsing, module \"%s%s%s\" not implemented.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 325 | mod_name, rev ? "@" : "", rev ? mod_rev : ""); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 326 | } |
| 327 | ret = LY_EINVAL; |
| 328 | goto cleanup; |
| 329 | } |
| 330 | |
| 331 | } |
| 332 | |
Michal Vasko | 85d9edc | 2021-04-22 09:15:05 +0200 | [diff] [blame] | 333 | if (*mod) { |
| 334 | /* fill cached hashes, if not already */ |
| 335 | lyb_cache_module_hash(*mod); |
| 336 | } |
Michal Vasko | 11f76c8 | 2021-04-15 14:36:14 +0200 | [diff] [blame] | 337 | |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 338 | cleanup: |
| 339 | free(mod_name); |
| 340 | return ret; |
| 341 | } |
| 342 | |
| 343 | /** |
| 344 | * @brief Parse YANG node metadata. |
| 345 | * |
| 346 | * @param[in] lybctx LYB context. |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 347 | * @param[out] meta Parsed metadata. |
| 348 | * @return LY_ERR value. |
| 349 | */ |
| 350 | static LY_ERR |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 351 | lyb_parse_metadata(struct lyd_lyb_ctx *lybctx, struct lyd_meta **meta) |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 352 | { |
| 353 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 354 | ly_bool dynamic; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 355 | uint8_t i, count = 0; |
Michal Vasko | 1e5d561 | 2020-07-03 13:29:26 +0200 | [diff] [blame] | 356 | char *meta_name = NULL, *meta_value; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 357 | const struct lys_module *mod; |
| 358 | |
| 359 | /* read number of attributes stored */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 360 | lyb_read(&count, 1, lybctx->lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 361 | |
| 362 | /* read attributes */ |
| 363 | for (i = 0; i < count; ++i) { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 364 | ret = lyb_read_start_subtree(lybctx->lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 365 | LY_CHECK_GOTO(ret, cleanup); |
| 366 | |
| 367 | /* find model */ |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 368 | ret = lyb_parse_model(lybctx->lybctx, lybctx->parse_opts, &mod); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 369 | LY_CHECK_GOTO(ret, cleanup); |
| 370 | |
| 371 | if (!mod) { |
| 372 | /* skip it */ |
| 373 | do { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 374 | lyb_read(NULL, LYB_LAST_SUBTREE(lybctx->lybctx).written, lybctx->lybctx); |
| 375 | } while (LYB_LAST_SUBTREE(lybctx->lybctx).written); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 376 | goto stop_subtree; |
| 377 | } |
| 378 | |
| 379 | /* meta name */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 380 | ret = lyb_read_string(&meta_name, 1, lybctx->lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 381 | LY_CHECK_GOTO(ret, cleanup); |
| 382 | |
| 383 | /* meta value */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 384 | ret = lyb_read_string(&meta_value, 0, lybctx->lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 385 | LY_CHECK_GOTO(ret, cleanup); |
| 386 | dynamic = 1; |
| 387 | |
| 388 | /* create metadata */ |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 389 | ret = lyd_parser_create_meta((struct lyd_ctx *)lybctx, NULL, meta, mod, meta_name, strlen(meta_name), meta_value, |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 390 | ly_strlen(meta_value), &dynamic, LY_VALUE_JSON, NULL, LYD_HINT_DATA); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 391 | |
| 392 | /* free strings */ |
| 393 | free(meta_name); |
| 394 | meta_name = NULL; |
| 395 | if (dynamic) { |
| 396 | free(meta_value); |
| 397 | dynamic = 0; |
| 398 | } |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 399 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 400 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 401 | |
| 402 | stop_subtree: |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 403 | ret = lyb_read_stop_subtree(lybctx->lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 404 | LY_CHECK_GOTO(ret, cleanup); |
| 405 | } |
| 406 | |
| 407 | cleanup: |
| 408 | free(meta_name); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 409 | if (ret) { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 410 | lyd_free_meta_siblings(*meta); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 411 | *meta = NULL; |
| 412 | } |
| 413 | return ret; |
| 414 | } |
| 415 | |
| 416 | /** |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 417 | * @brief Parse format-specific prefix data. |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 418 | * |
| 419 | * @param[in] lybctx LYB context. |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 420 | * @param[in] format Prefix data format. |
| 421 | * @param[out] prefix_data Parsed prefix data. |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 422 | * @return LY_ERR value. |
| 423 | */ |
| 424 | static LY_ERR |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 425 | lyb_parse_prefix_data(struct lylyb_ctx *lybctx, LY_VALUE_FORMAT format, void **prefix_data) |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 426 | { |
| 427 | LY_ERR ret = LY_SUCCESS; |
| 428 | uint8_t count, i; |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 429 | struct ly_set *set = NULL; |
| 430 | struct lyxml_ns *ns = NULL; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 431 | |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 432 | switch (format) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 433 | case LY_VALUE_XML: |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 434 | /* read count */ |
| 435 | lyb_read(&count, 1, lybctx); |
| 436 | if (!count) { |
| 437 | return LY_SUCCESS; |
| 438 | } |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 439 | |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 440 | /* read all NS elements */ |
| 441 | LY_CHECK_GOTO(ret = ly_set_new(&set), cleanup); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 442 | |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 443 | for (i = 0; i < count; ++i) { |
| 444 | ns = calloc(1, sizeof *ns); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 445 | |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 446 | /* prefix */ |
| 447 | LY_CHECK_GOTO(ret = lyb_read_string(&ns->prefix, 1, lybctx), cleanup); |
| 448 | |
| 449 | /* namespace */ |
| 450 | LY_CHECK_GOTO(ret = lyb_read_string(&ns->uri, 1, lybctx), cleanup); |
| 451 | |
| 452 | LY_CHECK_GOTO(ret = ly_set_add(set, ns, 1, NULL), cleanup); |
| 453 | ns = NULL; |
| 454 | } |
| 455 | |
| 456 | *prefix_data = set; |
| 457 | break; |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 458 | case LY_VALUE_JSON: |
Radek Krejci | f994364 | 2021-04-26 10:18:21 +0200 | [diff] [blame] | 459 | case LY_VALUE_LYB: |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 460 | /* nothing stored */ |
| 461 | break; |
| 462 | default: |
| 463 | LOGINT(lybctx->ctx); |
| 464 | ret = LY_EINT; |
| 465 | break; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | cleanup: |
| 469 | if (ret) { |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 470 | ly_free_prefix_data(format, set); |
| 471 | if (ns) { |
| 472 | free(ns->prefix); |
| 473 | free(ns->uri); |
| 474 | free(ns); |
| 475 | } |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 476 | } |
| 477 | return ret; |
| 478 | } |
| 479 | |
| 480 | /** |
| 481 | * @brief Parse opaque attributes. |
| 482 | * |
| 483 | * @param[in] lybctx LYB context. |
| 484 | * @param[out] attr Parsed attributes. |
| 485 | * @return LY_ERR value. |
| 486 | */ |
| 487 | static LY_ERR |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 488 | lyb_parse_attributes(struct lylyb_ctx *lybctx, struct lyd_attr **attr) |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 489 | { |
| 490 | LY_ERR ret = LY_SUCCESS; |
| 491 | uint8_t count, i; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 492 | struct lyd_attr *attr2; |
| 493 | char *prefix = NULL, *module_name = NULL, *name = NULL, *value = NULL; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 494 | ly_bool dynamic = 0; |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 495 | LY_VALUE_FORMAT format = 0; |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 496 | void *val_prefix_data = NULL; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 497 | |
| 498 | /* read count */ |
| 499 | lyb_read(&count, 1, lybctx); |
| 500 | |
| 501 | /* read attributes */ |
| 502 | for (i = 0; i < count; ++i) { |
| 503 | ret = lyb_read_start_subtree(lybctx); |
| 504 | LY_CHECK_GOTO(ret, cleanup); |
| 505 | |
Michal Vasko | 0fdcd24 | 2020-11-11 19:12:30 +0100 | [diff] [blame] | 506 | /* prefix, may be empty */ |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 507 | ret = lyb_read_string(&prefix, 1, lybctx); |
| 508 | LY_CHECK_GOTO(ret, cleanup); |
| 509 | if (!prefix[0]) { |
| 510 | free(prefix); |
| 511 | prefix = NULL; |
| 512 | } |
| 513 | |
| 514 | /* namespace, may be empty */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 515 | ret = lyb_read_string(&module_name, 1, lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 516 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 517 | if (!module_name[0]) { |
| 518 | free(module_name); |
| 519 | module_name = NULL; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | /* name */ |
| 523 | ret = lyb_read_string(&name, 1, lybctx); |
| 524 | LY_CHECK_GOTO(ret, cleanup); |
| 525 | |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 526 | /* format */ |
| 527 | lyb_read((uint8_t *)&format, 1, lybctx); |
| 528 | |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 529 | /* value prefixes */ |
| 530 | ret = lyb_parse_prefix_data(lybctx, format, &val_prefix_data); |
| 531 | LY_CHECK_GOTO(ret, cleanup); |
| 532 | |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 533 | /* value */ |
| 534 | ret = lyb_read_string(&value, 0, lybctx); |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 535 | LY_CHECK_ERR_GOTO(ret, ly_free_prefix_data(format, val_prefix_data), cleanup); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 536 | dynamic = 1; |
| 537 | |
| 538 | /* attr2 is always changed to the created attribute */ |
Michal Vasko | 501af03 | 2020-11-11 20:27:44 +0100 | [diff] [blame] | 539 | ret = lyd_create_attr(NULL, &attr2, lybctx->ctx, name, strlen(name), prefix, ly_strlen(prefix), module_name, |
| 540 | ly_strlen(module_name), value, ly_strlen(value), &dynamic, format, val_prefix_data, 0); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 541 | LY_CHECK_GOTO(ret, cleanup); |
| 542 | |
| 543 | free(prefix); |
| 544 | prefix = NULL; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 545 | free(module_name); |
| 546 | module_name = NULL; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 547 | free(name); |
| 548 | name = NULL; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 549 | assert(!dynamic); |
| 550 | value = NULL; |
| 551 | |
| 552 | if (!*attr) { |
| 553 | *attr = attr2; |
| 554 | } |
| 555 | |
| 556 | ret = lyb_read_stop_subtree(lybctx); |
| 557 | LY_CHECK_GOTO(ret, cleanup); |
| 558 | } |
| 559 | |
| 560 | cleanup: |
| 561 | free(prefix); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 562 | free(module_name); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 563 | free(name); |
| 564 | if (dynamic) { |
| 565 | free(value); |
| 566 | } |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 567 | if (ret) { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 568 | lyd_free_attr_siblings(lybctx->ctx, *attr); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 569 | *attr = NULL; |
| 570 | } |
| 571 | return ret; |
| 572 | } |
| 573 | |
| 574 | /** |
| 575 | * @brief Check whether a schema node matches a hash(es). |
| 576 | * |
| 577 | * @param[in] sibling Schema node to check. |
| 578 | * @param[in] hash Hash array to check. |
| 579 | * @param[in] hash_count Number of hashes in @p hash. |
| 580 | * @return non-zero if matches, |
| 581 | * @return 0 if not. |
| 582 | */ |
| 583 | static int |
| 584 | lyb_is_schema_hash_match(struct lysc_node *sibling, LYB_HASH *hash, uint8_t hash_count) |
| 585 | { |
| 586 | LYB_HASH sibling_hash; |
| 587 | uint8_t i; |
| 588 | |
| 589 | /* compare all the hashes starting from collision ID 0 */ |
| 590 | for (i = 0; i < hash_count; ++i) { |
Michal Vasko | 11f76c8 | 2021-04-15 14:36:14 +0200 | [diff] [blame] | 591 | sibling_hash = lyb_get_hash(sibling, i); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 592 | if (sibling_hash != hash[i]) { |
| 593 | return 0; |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | return 1; |
| 598 | } |
| 599 | |
| 600 | /** |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 601 | * @brief Parse schema node hash. |
| 602 | * |
| 603 | * @param[in] lybctx LYB context. |
| 604 | * @param[in] sparent Schema parent, must be set if @p mod is not. |
| 605 | * @param[in] mod Module of the top-level node, must be set if @p sparent is not. |
| 606 | * @param[out] snode Parsed found schema node, may be NULL if opaque. |
| 607 | * @return LY_ERR value. |
| 608 | */ |
| 609 | static LY_ERR |
| 610 | lyb_parse_schema_hash(struct lyd_lyb_ctx *lybctx, const struct lysc_node *sparent, const struct lys_module *mod, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 611 | const struct lysc_node **snode) |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 612 | { |
| 613 | LY_ERR ret; |
| 614 | uint8_t i, j; |
| 615 | const struct lysc_node *sibling; |
| 616 | LYB_HASH hash[LYB_HASH_BITS - 1]; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 617 | uint32_t getnext_opts; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 618 | |
| 619 | *snode = NULL; |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 620 | getnext_opts = lybctx->int_opts & LYD_INTOPT_REPLY ? LYS_GETNEXT_OUTPUT : 0; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 621 | |
| 622 | /* read the first hash */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 623 | lyb_read(&hash[0], sizeof *hash, lybctx->lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 624 | |
| 625 | if (!hash[0]) { |
| 626 | /* opaque node */ |
| 627 | return LY_SUCCESS; |
| 628 | } |
| 629 | |
| 630 | /* based on the first hash read all the other ones, if any */ |
| 631 | for (i = 0; !(hash[0] & (LYB_HASH_COLLISION_ID >> i)); ++i) { |
| 632 | if (i > LYB_HASH_BITS) { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 633 | LOGINT_RET(lybctx->lybctx->ctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 634 | } |
| 635 | } |
| 636 | |
| 637 | /* move the first hash on its accurate position */ |
| 638 | hash[i] = hash[0]; |
| 639 | |
| 640 | /* read the rest of hashes */ |
| 641 | for (j = i; j; --j) { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 642 | lyb_read(&hash[j - 1], sizeof *hash, lybctx->lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 643 | |
| 644 | /* correct collision ID */ |
| 645 | assert(hash[j - 1] & (LYB_HASH_COLLISION_ID >> (j - 1))); |
| 646 | /* preceded with zeros */ |
| 647 | assert(!(hash[j - 1] & (LYB_HASH_MASK << (LYB_HASH_BITS - (j - 1))))); |
| 648 | } |
| 649 | |
| 650 | /* find our node with matching hashes */ |
| 651 | sibling = NULL; |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 652 | while (1) { |
| 653 | if (!sparent && lybctx->ext) { |
| 654 | sibling = lys_getnext_ext(sibling, sparent, lybctx->ext, getnext_opts); |
| 655 | } else { |
| 656 | sibling = lys_getnext(sibling, sparent, mod ? mod->compiled : NULL, getnext_opts); |
| 657 | } |
| 658 | if (!sibling) { |
| 659 | break; |
| 660 | } |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 661 | /* skip schema nodes from models not present during printing */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 662 | if (lyb_has_schema_model(sibling, lybctx->lybctx->models) && |
| 663 | lyb_is_schema_hash_match((struct lysc_node *)sibling, hash, i + 1)) { |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 664 | /* match found */ |
| 665 | break; |
| 666 | } |
| 667 | } |
| 668 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 669 | if (!sibling && (lybctx->parse_opts & LYD_PARSE_STRICT)) { |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 670 | if (lybctx->ext) { |
| 671 | LOGVAL(lybctx->lybctx->ctx, LYVE_REFERENCE, "Failed to find matching hash for a node from \"%s\" extension instance node.", |
| 672 | lybctx->ext->def->name); |
| 673 | } else if (mod) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 674 | LOGVAL(lybctx->lybctx->ctx, LYVE_REFERENCE, "Failed to find matching hash for a top-level node" |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 675 | " from \"%s\".", mod->name); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 676 | } else { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 677 | LOGVAL(lybctx->lybctx->ctx, LYVE_REFERENCE, "Failed to find matching hash for a child node" |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 678 | " of \"%s\".", sparent->name); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 679 | } |
| 680 | return LY_EVALID; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 681 | } else if (sibling && (ret = lyd_parser_check_schema((struct lyd_ctx *)lybctx, sibling))) { |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 682 | return ret; |
| 683 | } |
| 684 | |
| 685 | *snode = sibling; |
| 686 | return LY_SUCCESS; |
| 687 | } |
| 688 | |
| 689 | /** |
| 690 | * @brief Read until the end of the current subtree. |
| 691 | * |
| 692 | * @param[in] lybctx LYB context. |
| 693 | */ |
| 694 | static void |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 695 | lyb_skip_subtree(struct lylyb_ctx *lybctx) |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 696 | { |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 697 | do { |
| 698 | /* first skip any meta information inside */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 699 | ly_in_skip(lybctx->in, LYB_LAST_SUBTREE(lybctx).inner_chunks * LYB_META_BYTES); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 700 | |
| 701 | /* then read data */ |
| 702 | lyb_read(NULL, LYB_LAST_SUBTREE(lybctx).written, lybctx); |
| 703 | } while (LYB_LAST_SUBTREE(lybctx).written); |
| 704 | } |
| 705 | |
| 706 | /** |
| 707 | * @brief Parse LYB subtree. |
| 708 | * |
| 709 | * @param[in] lybctx LYB context. |
| 710 | * @param[in] parent Data parent of the subtree, must be set if @p first is not. |
| 711 | * @param[in,out] first First top-level sibling, must be set if @p parent is not. |
| 712 | * @return LY_ERR value. |
| 713 | */ |
| 714 | static LY_ERR |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 715 | lyb_parse_subtree_r(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, struct lyd_node **first_p, struct ly_set *parsed) |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 716 | { |
| 717 | LY_ERR ret = LY_SUCCESS; |
| 718 | struct lyd_node *node = NULL, *tree; |
| 719 | const struct lys_module *mod; |
| 720 | const struct lysc_node *snode = NULL; |
| 721 | struct lyd_meta *meta = NULL, *m; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 722 | struct lyd_attr *attr = NULL, *a; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 723 | LYD_ANYDATA_VALUETYPE value_type; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 724 | char *value = NULL, *name = NULL, *prefix = NULL, *module_key = NULL; |
Michal Vasko | 9afe3df | 2021-02-05 16:33:50 +0100 | [diff] [blame] | 725 | const char *val_dict; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 726 | ly_bool dynamic = 0; |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 727 | LY_VALUE_FORMAT format = 0; |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 728 | void *val_prefix_data = NULL; |
Michal Vasko | c5e866a | 2020-11-04 17:09:26 +0100 | [diff] [blame] | 729 | uint32_t prev_lo, flags; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 730 | const struct ly_ctx *ctx = lybctx->lybctx->ctx; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 731 | |
| 732 | /* register a new subtree */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 733 | LY_CHECK_GOTO(ret = lyb_read_start_subtree(lybctx->lybctx), cleanup); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 734 | |
| 735 | if (!parent) { |
| 736 | /* top-level, read module name */ |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 737 | ret = lyb_parse_model(lybctx->lybctx, lybctx->parse_opts, &mod); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 738 | LY_CHECK_GOTO(ret, cleanup); |
| 739 | |
| 740 | /* read hash, find the schema node starting from mod */ |
| 741 | ret = lyb_parse_schema_hash(lybctx, NULL, mod, &snode); |
| 742 | LY_CHECK_GOTO(ret, cleanup); |
| 743 | } else { |
| 744 | /* read hash, find the schema node starting from parent schema */ |
| 745 | ret = lyb_parse_schema_hash(lybctx, parent->schema, NULL, &snode); |
| 746 | LY_CHECK_GOTO(ret, cleanup); |
| 747 | } |
| 748 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 749 | if (!snode && !(lybctx->parse_opts & LYD_PARSE_OPAQ)) { |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 750 | /* unknown data, skip them */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 751 | lyb_skip_subtree(lybctx->lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 752 | goto stop_subtree; |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 753 | } |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 754 | |
| 755 | /* create metadata/attributes */ |
| 756 | if (snode) { |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 757 | ret = lyb_parse_metadata(lybctx, &meta); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 758 | LY_CHECK_GOTO(ret, cleanup); |
| 759 | } else { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 760 | ret = lyb_parse_attributes(lybctx->lybctx, &attr); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 761 | LY_CHECK_GOTO(ret, cleanup); |
| 762 | } |
| 763 | |
Michal Vasko | c5e866a | 2020-11-04 17:09:26 +0100 | [diff] [blame] | 764 | /* read flags */ |
| 765 | lyb_read_number(&flags, sizeof flags, sizeof flags, lybctx->lybctx); |
| 766 | |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 767 | if (!snode) { |
| 768 | /* parse prefix */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 769 | ret = lyb_read_string(&prefix, 1, lybctx->lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 770 | LY_CHECK_GOTO(ret, cleanup); |
| 771 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 772 | /* parse module key */ |
| 773 | ret = lyb_read_string(&module_key, 1, lybctx->lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 774 | LY_CHECK_GOTO(ret, cleanup); |
| 775 | |
| 776 | /* parse name */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 777 | ret = lyb_read_string(&name, 1, lybctx->lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 778 | LY_CHECK_GOTO(ret, cleanup); |
| 779 | |
Michal Vasko | ffc92bf | 2021-06-21 08:15:14 +0200 | [diff] [blame] | 780 | /* parse value */ |
| 781 | ret = lyb_read_string(&value, 1, lybctx->lybctx); |
| 782 | LY_CHECK_ERR_GOTO(ret, ly_free_prefix_data(format, val_prefix_data), cleanup); |
| 783 | dynamic = 1; |
| 784 | |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 785 | /* parse format */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 786 | lyb_read((uint8_t *)&format, 1, lybctx->lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 787 | |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 788 | /* parse value prefixes */ |
| 789 | ret = lyb_parse_prefix_data(lybctx->lybctx, format, &val_prefix_data); |
| 790 | LY_CHECK_GOTO(ret, cleanup); |
| 791 | |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 792 | /* create node */ |
Michal Vasko | 501af03 | 2020-11-11 20:27:44 +0100 | [diff] [blame] | 793 | ret = lyd_create_opaq(ctx, name, strlen(name), prefix, ly_strlen(prefix), module_key, ly_strlen(module_key), |
| 794 | value, strlen(value), &dynamic, format, val_prefix_data, 0, &node); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 795 | LY_CHECK_GOTO(ret, cleanup); |
| 796 | |
| 797 | /* process children */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 798 | while (LYB_LAST_SUBTREE(lybctx->lybctx).written) { |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 799 | ret = lyb_parse_subtree_r(lybctx, node, NULL, NULL); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 800 | LY_CHECK_GOTO(ret, cleanup); |
| 801 | } |
| 802 | } else if (snode->nodetype & LYD_NODE_TERM) { |
| 803 | /* parse value */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 804 | ret = lyb_read_string(&value, 0, lybctx->lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 805 | LY_CHECK_GOTO(ret, cleanup); |
| 806 | dynamic = 1; |
| 807 | |
| 808 | /* create node */ |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 809 | ret = lyd_parser_create_term((struct lyd_ctx *)lybctx, snode, value, ly_strlen(value), &dynamic, LY_VALUE_JSON, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 810 | NULL, LYD_HINT_DATA, &node); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 811 | if (dynamic) { |
| 812 | free(value); |
| 813 | dynamic = 0; |
| 814 | } |
| 815 | value = NULL; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 816 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 817 | } else if (snode->nodetype & LYD_NODE_INNER) { |
| 818 | /* create node */ |
| 819 | ret = lyd_create_inner(snode, &node); |
| 820 | LY_CHECK_GOTO(ret, cleanup); |
| 821 | |
| 822 | /* process children */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 823 | while (LYB_LAST_SUBTREE(lybctx->lybctx).written) { |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 824 | ret = lyb_parse_subtree_r(lybctx, node, NULL, NULL); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 825 | LY_CHECK_GOTO(ret, cleanup); |
| 826 | } |
| 827 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 828 | if (!(lybctx->parse_opts & LYD_PARSE_ONLY)) { |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 829 | /* new node validation, autodelete CANNOT occur, all nodes are new */ |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 830 | ret = lyd_validate_new(lyd_node_child_p(node), snode, NULL, NULL); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 831 | LY_CHECK_GOTO(ret, cleanup); |
| 832 | |
| 833 | /* add any missing default children */ |
Radek Krejci | 4f2e3e5 | 2021-03-30 14:20:28 +0200 | [diff] [blame] | 834 | ret = lyd_new_implicit_r(node, lyd_node_child_p(node), NULL, NULL, &lybctx->node_when, &lybctx->node_exts, |
Michal Vasko | c43c8ab | 2021-03-05 13:32:44 +0100 | [diff] [blame] | 835 | &lybctx->node_types, (lybctx->val_opts & LYD_VALIDATE_NO_STATE) ? LYD_IMPLICIT_NO_STATE : 0, NULL); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 836 | LY_CHECK_GOTO(ret, cleanup); |
| 837 | } |
| 838 | |
Michal Vasko | 751cb4d | 2020-07-14 12:25:28 +0200 | [diff] [blame] | 839 | if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) { |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 840 | /* rememeber the RPC/action/notification */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 841 | lybctx->op_node = node; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 842 | } |
| 843 | } else if (snode->nodetype & LYD_NODE_ANY) { |
| 844 | /* parse value type */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 845 | lyb_read((uint8_t *)&value_type, sizeof value_type, lybctx->lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 846 | if (value_type == LYD_ANYDATA_DATATREE) { |
| 847 | /* invalid situation */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 848 | LOGINT(ctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 849 | goto cleanup; |
| 850 | } |
| 851 | |
| 852 | /* read anydata content */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 853 | ret = lyb_read_string(&value, 0, lybctx->lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 854 | LY_CHECK_GOTO(ret, cleanup); |
| 855 | dynamic = 1; |
| 856 | |
| 857 | if (value_type == LYD_ANYDATA_LYB) { |
| 858 | /* turn logging off */ |
| 859 | prev_lo = ly_log_options(0); |
| 860 | |
| 861 | /* try to parse LYB into a data tree */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 862 | if (lyd_parse_data_mem(ctx, value, LYD_LYB, LYD_PARSE_ONLY | LYD_PARSE_OPAQ | LYD_PARSE_STRICT, 0, &tree) == LY_SUCCESS) { |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 863 | /* successfully parsed */ |
| 864 | free(value); |
| 865 | value = (char *)tree; |
| 866 | value_type = LYD_ANYDATA_DATATREE; |
| 867 | } |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 868 | |
| 869 | /* turn logging on again */ |
| 870 | ly_log_options(prev_lo); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 871 | } |
| 872 | |
Michal Vasko | 9afe3df | 2021-02-05 16:33:50 +0100 | [diff] [blame] | 873 | /* create the node */ |
| 874 | switch (value_type) { |
| 875 | case LYD_ANYDATA_LYB: |
| 876 | case LYD_ANYDATA_DATATREE: |
| 877 | /* use the value directly */ |
| 878 | ret = lyd_create_any(snode, value, value_type, 1, &node); |
| 879 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 880 | |
Michal Vasko | 9afe3df | 2021-02-05 16:33:50 +0100 | [diff] [blame] | 881 | dynamic = 0; |
| 882 | value = NULL; |
| 883 | break; |
| 884 | case LYD_ANYDATA_STRING: |
| 885 | case LYD_ANYDATA_XML: |
| 886 | case LYD_ANYDATA_JSON: |
| 887 | /* value is expected to be in the dictionary */ |
| 888 | ret = lydict_insert_zc(ctx, value, &val_dict); |
| 889 | LY_CHECK_GOTO(ret, cleanup); |
| 890 | dynamic = 0; |
| 891 | value = NULL; |
| 892 | |
| 893 | /* use the value in the dictionary */ |
| 894 | ret = lyd_create_any(snode, val_dict, value_type, 1, &node); |
| 895 | if (ret) { |
| 896 | lydict_remove(ctx, val_dict); |
| 897 | goto cleanup; |
| 898 | } |
| 899 | break; |
| 900 | } |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 901 | } |
| 902 | assert(node); |
| 903 | |
Michal Vasko | c5e866a | 2020-11-04 17:09:26 +0100 | [diff] [blame] | 904 | /* set flags */ |
| 905 | node->flags = flags; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 906 | |
| 907 | /* add metadata/attributes */ |
| 908 | if (snode) { |
| 909 | LY_LIST_FOR(meta, m) { |
| 910 | m->parent = node; |
| 911 | } |
| 912 | node->meta = meta; |
| 913 | meta = NULL; |
| 914 | } else { |
| 915 | assert(!node->schema); |
| 916 | LY_LIST_FOR(attr, a) { |
| 917 | a->parent = (struct lyd_node_opaq *)node; |
| 918 | } |
| 919 | ((struct lyd_node_opaq *)node)->attr = attr; |
| 920 | attr = NULL; |
| 921 | } |
| 922 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 923 | /* insert, keep first pointer correct */ |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 924 | lyd_insert_node(parent, first_p, node); |
| 925 | while (!parent && (*first_p)->prev->next) { |
| 926 | *first_p = (*first_p)->prev; |
| 927 | } |
| 928 | |
| 929 | /* rememeber a successfully parsed node */ |
| 930 | if (parsed) { |
| 931 | ly_set_add(parsed, node, 1, NULL); |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 932 | } |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 933 | node = NULL; |
| 934 | |
| 935 | stop_subtree: |
| 936 | /* end the subtree */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 937 | ret = lyb_read_stop_subtree(lybctx->lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 938 | LY_CHECK_GOTO(ret, cleanup); |
| 939 | |
| 940 | cleanup: |
| 941 | free(prefix); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 942 | free(module_key); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 943 | free(name); |
| 944 | if (dynamic) { |
| 945 | free(value); |
| 946 | } |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 947 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 948 | lyd_free_meta_siblings(meta); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 949 | lyd_free_attr_siblings(ctx, attr); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 950 | lyd_free_tree(node); |
| 951 | return ret; |
| 952 | } |
| 953 | |
| 954 | /** |
| 955 | * @brief Parse used YANG data models. |
| 956 | * |
| 957 | * @param[in] lybctx LYB context. |
| 958 | * @return LY_ERR value. |
| 959 | */ |
| 960 | static LY_ERR |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 961 | lyb_parse_data_models(struct lylyb_ctx *lybctx, uint32_t parse_options) |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 962 | { |
| 963 | LY_ERR ret; |
| 964 | uint32_t count; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 965 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 966 | |
| 967 | /* read model count */ |
| 968 | lyb_read_number(&count, sizeof count, 2, lybctx); |
| 969 | |
| 970 | if (count) { |
| 971 | LY_ARRAY_CREATE_RET(lybctx->ctx, lybctx->models, count, LY_EMEM); |
| 972 | |
| 973 | /* read modules */ |
| 974 | for (u = 0; u < count; ++u) { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 975 | ret = lyb_parse_model(lybctx, parse_options, &lybctx->models[u]); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 976 | LY_CHECK_RET(ret); |
| 977 | LY_ARRAY_INCREMENT(lybctx->models); |
| 978 | } |
| 979 | } |
| 980 | |
| 981 | return LY_SUCCESS; |
| 982 | } |
| 983 | |
| 984 | /** |
| 985 | * @brief Parse LYB magic number. |
| 986 | * |
| 987 | * @param[in] lybctx LYB context. |
| 988 | * @return LY_ERR value. |
| 989 | */ |
| 990 | static LY_ERR |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 991 | lyb_parse_magic_number(struct lylyb_ctx *lybctx) |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 992 | { |
| 993 | char magic_byte = 0; |
| 994 | |
| 995 | lyb_read((uint8_t *)&magic_byte, 1, lybctx); |
| 996 | if (magic_byte != 'l') { |
| 997 | LOGERR(lybctx->ctx, LY_EINVAL, "Invalid first magic number byte \"0x%02x\".", magic_byte); |
| 998 | return LY_EINVAL; |
| 999 | } |
| 1000 | |
| 1001 | lyb_read((uint8_t *)&magic_byte, 1, lybctx); |
| 1002 | if (magic_byte != 'y') { |
| 1003 | LOGERR(lybctx->ctx, LY_EINVAL, "Invalid second magic number byte \"0x%02x\".", magic_byte); |
| 1004 | return LY_EINVAL; |
| 1005 | } |
| 1006 | |
| 1007 | lyb_read((uint8_t *)&magic_byte, 1, lybctx); |
| 1008 | if (magic_byte != 'b') { |
| 1009 | LOGERR(lybctx->ctx, LY_EINVAL, "Invalid third magic number byte \"0x%02x\".", magic_byte); |
| 1010 | return LY_EINVAL; |
| 1011 | } |
| 1012 | |
| 1013 | return LY_SUCCESS; |
| 1014 | } |
| 1015 | |
| 1016 | /** |
| 1017 | * @brief Parse LYB header. |
| 1018 | * |
| 1019 | * @param[in] lybctx LYB context. |
| 1020 | * @return LY_ERR value. |
| 1021 | */ |
| 1022 | static LY_ERR |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1023 | lyb_parse_header(struct lylyb_ctx *lybctx) |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1024 | { |
| 1025 | uint8_t byte = 0; |
| 1026 | |
| 1027 | /* version, future flags */ |
| 1028 | lyb_read((uint8_t *)&byte, sizeof byte, lybctx); |
| 1029 | |
| 1030 | if ((byte & LYB_VERSION_MASK) != LYB_VERSION_NUM) { |
| 1031 | LOGERR(lybctx->ctx, LY_EINVAL, "Invalid LYB format version \"0x%02x\", expected \"0x%02x\".", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1032 | byte & LYB_VERSION_MASK, LYB_VERSION_NUM); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1033 | return LY_EINVAL; |
| 1034 | } |
| 1035 | |
| 1036 | return LY_SUCCESS; |
| 1037 | } |
| 1038 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1039 | LY_ERR |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 1040 | lyd_parse_lyb(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent, |
| 1041 | struct lyd_node **first_p, struct ly_in *in, uint32_t parse_opts, uint32_t val_opts, enum lyd_type data_type, |
| 1042 | struct ly_set *parsed, struct lyd_ctx **lydctx_p) |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1043 | { |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1044 | LY_ERR rc = LY_SUCCESS; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1045 | struct lyd_lyb_ctx *lybctx; |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1046 | uint32_t int_opts; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1047 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1048 | assert(!(parse_opts & ~LYD_PARSE_OPTS_MASK)); |
| 1049 | assert(!(val_opts & ~LYD_VALIDATE_OPTS_MASK)); |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 1050 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1051 | lybctx = calloc(1, sizeof *lybctx); |
| 1052 | LY_CHECK_ERR_RET(!lybctx, LOGMEM(ctx), LY_EMEM); |
| 1053 | lybctx->lybctx = calloc(1, sizeof *lybctx->lybctx); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1054 | LY_CHECK_ERR_GOTO(!lybctx->lybctx, LOGMEM(ctx); rc = LY_EMEM, cleanup); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1055 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1056 | lybctx->lybctx->in = in; |
| 1057 | lybctx->lybctx->ctx = ctx; |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1058 | lybctx->parse_opts = parse_opts; |
| 1059 | lybctx->val_opts = val_opts; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1060 | lybctx->free = lyd_lyb_ctx_free; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1061 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1062 | switch (data_type) { |
Michal Vasko | 1e4c68e | 2021-02-18 15:03:01 +0100 | [diff] [blame] | 1063 | case LYD_TYPE_DATA_YANG: |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1064 | int_opts = LYD_INTOPT_WITH_SIBLINGS; |
| 1065 | break; |
Michal Vasko | 1e4c68e | 2021-02-18 15:03:01 +0100 | [diff] [blame] | 1066 | case LYD_TYPE_RPC_YANG: |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1067 | int_opts = LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NO_SIBLINGS; |
| 1068 | break; |
Michal Vasko | 1e4c68e | 2021-02-18 15:03:01 +0100 | [diff] [blame] | 1069 | case LYD_TYPE_NOTIF_YANG: |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1070 | int_opts = LYD_INTOPT_NOTIF | LYD_INTOPT_NO_SIBLINGS; |
| 1071 | break; |
Michal Vasko | 1e4c68e | 2021-02-18 15:03:01 +0100 | [diff] [blame] | 1072 | case LYD_TYPE_REPLY_YANG: |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1073 | int_opts = LYD_INTOPT_REPLY | LYD_INTOPT_NO_SIBLINGS; |
| 1074 | break; |
| 1075 | default: |
| 1076 | LOGINT(ctx); |
| 1077 | rc = LY_EINT; |
| 1078 | goto cleanup; |
| 1079 | } |
| 1080 | lybctx->int_opts = int_opts; |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 1081 | lybctx->ext = ext; |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1082 | |
| 1083 | /* find the operation node if it exists already */ |
| 1084 | LY_CHECK_GOTO(rc = lyd_parser_find_operation(parent, int_opts, &lybctx->op_node), cleanup); |
| 1085 | |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1086 | /* read magic number */ |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1087 | rc = lyb_parse_magic_number(lybctx->lybctx); |
| 1088 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1089 | |
| 1090 | /* read header */ |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1091 | rc = lyb_parse_header(lybctx->lybctx); |
| 1092 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1093 | |
| 1094 | /* read used models */ |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1095 | rc = lyb_parse_data_models(lybctx->lybctx, lybctx->parse_opts); |
| 1096 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1097 | |
| 1098 | /* read subtree(s) */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1099 | while (lybctx->lybctx->in->current[0]) { |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1100 | rc = lyb_parse_subtree_r(lybctx, parent, first_p, parsed); |
| 1101 | LY_CHECK_GOTO(rc, cleanup); |
| 1102 | |
| 1103 | if (!(int_opts & LYD_INTOPT_WITH_SIBLINGS)) { |
| 1104 | break; |
| 1105 | } |
| 1106 | } |
| 1107 | |
| 1108 | if ((int_opts & LYD_INTOPT_NO_SIBLINGS) && lybctx->lybctx->in->current[0]) { |
| 1109 | LOGVAL(ctx, LYVE_SYNTAX, "Unexpected sibling node."); |
| 1110 | rc = LY_EVALID; |
| 1111 | goto cleanup; |
| 1112 | } |
| 1113 | if ((int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NOTIF | LYD_INTOPT_REPLY)) && !lybctx->op_node) { |
| 1114 | LOGVAL(ctx, LYVE_DATA, "Missing the operation node."); |
| 1115 | rc = LY_EVALID; |
| 1116 | goto cleanup; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1117 | } |
| 1118 | |
| 1119 | /* read the last zero, parsing finished */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1120 | ly_in_skip(lybctx->lybctx->in, 1); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1121 | |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1122 | cleanup: |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1123 | /* there should be no unres stored if validation should be skipped */ |
| 1124 | assert(!(parse_opts & LYD_PARSE_ONLY) || (!lybctx->node_types.count && !lybctx->meta_types.count && |
| 1125 | !lybctx->node_when.count)); |
| 1126 | |
| 1127 | if (rc) { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1128 | lyd_lyb_ctx_free((struct lyd_ctx *)lybctx); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1129 | } else { |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1130 | *lydctx_p = (struct lyd_ctx *)lybctx; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1131 | } |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1132 | return rc; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1133 | } |
| 1134 | |
| 1135 | API int |
| 1136 | lyd_lyb_data_length(const char *data) |
| 1137 | { |
| 1138 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1139 | struct lylyb_ctx *lybctx; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1140 | int count, i; |
| 1141 | size_t len; |
| 1142 | uint8_t buf[LYB_SIZE_MAX]; |
| 1143 | |
| 1144 | if (!data) { |
| 1145 | return -1; |
| 1146 | } |
| 1147 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1148 | lybctx = calloc(1, sizeof *lybctx); |
| 1149 | LY_CHECK_ERR_RET(!lybctx, LOGMEM(NULL), LY_EMEM); |
| 1150 | ret = ly_in_new_memory(data, &lybctx->in); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1151 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1152 | |
| 1153 | /* read magic number */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1154 | ret = lyb_parse_magic_number(lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1155 | LY_CHECK_GOTO(ret, cleanup); |
| 1156 | |
| 1157 | /* read header */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1158 | ret = lyb_parse_header(lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1159 | LY_CHECK_GOTO(ret, cleanup); |
| 1160 | |
| 1161 | /* read model count */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1162 | lyb_read_number(&count, sizeof count, 2, lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1163 | |
| 1164 | /* read all models */ |
| 1165 | for (i = 0; i < count; ++i) { |
| 1166 | /* module name length */ |
| 1167 | len = 0; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1168 | lyb_read_number(&len, sizeof len, 2, lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1169 | |
| 1170 | /* model name */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1171 | lyb_read(buf, len, lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1172 | |
| 1173 | /* revision */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1174 | lyb_read(buf, 2, lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1175 | } |
| 1176 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1177 | while (lybctx->in->current[0]) { |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1178 | /* register a new subtree */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1179 | ret = lyb_read_start_subtree(lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1180 | LY_CHECK_GOTO(ret, cleanup); |
| 1181 | |
| 1182 | /* skip it */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1183 | lyb_skip_subtree(lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1184 | |
| 1185 | /* subtree finished */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1186 | ret = lyb_read_stop_subtree(lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1187 | LY_CHECK_GOTO(ret, cleanup); |
| 1188 | } |
| 1189 | |
| 1190 | /* read the last zero, parsing finished */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1191 | ly_in_skip(lybctx->in, 1); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1192 | |
| 1193 | cleanup: |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1194 | count = lybctx->in->current - lybctx->in->start; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1195 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1196 | ly_in_free(lybctx->in, 0); |
| 1197 | lylyb_ctx_free(lybctx); |
| 1198 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1199 | return ret ? -1 : count; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1200 | } |