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