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