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) { |
| 152 | case 1: |
| 153 | *((uint8_t *)num) = buf; |
| 154 | break; |
| 155 | case 2: |
| 156 | *((uint16_t *)num) = buf; |
| 157 | break; |
| 158 | case 4: |
| 159 | *((uint32_t *)num) = buf; |
| 160 | break; |
| 161 | case 8: |
| 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 | |
| 249 | if (!lybctx->subtrees) { |
Radek Krejci | f6d14cb | 2020-07-02 16:11:45 +0200 | [diff] [blame] | 250 | assert(lybctx->subtree_size == 0); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 251 | u = 0; |
| 252 | } else { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 253 | u = LY_ARRAY_COUNT(lybctx->subtrees); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 254 | } |
| 255 | if (u == lybctx->subtree_size) { |
| 256 | LY_ARRAY_CREATE_RET(lybctx->ctx, lybctx->subtrees, u + LYB_SUBTREE_STEP, LY_EMEM); |
| 257 | lybctx->subtree_size = u + LYB_SUBTREE_STEP; |
| 258 | } |
| 259 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 260 | 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] | 261 | |
| 262 | LY_ARRAY_INCREMENT(lybctx->subtrees); |
| 263 | LYB_LAST_SUBTREE(lybctx).written = meta_buf[0]; |
| 264 | LYB_LAST_SUBTREE(lybctx).inner_chunks = meta_buf[LYB_SIZE_BYTES]; |
| 265 | LYB_LAST_SUBTREE(lybctx).position = (LYB_LAST_SUBTREE(lybctx).written == LYB_SIZE_MAX ? 1 : 0); |
| 266 | |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 267 | return LY_SUCCESS; |
| 268 | } |
| 269 | |
| 270 | /** |
| 271 | * @brief Parse YANG model info. |
| 272 | * |
| 273 | * @param[in] lybctx LYB context. |
| 274 | * @param[out] mod Parsed module. |
| 275 | * @return LY_ERR value. |
| 276 | */ |
| 277 | static LY_ERR |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 278 | 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] | 279 | { |
| 280 | LY_ERR ret = LY_SUCCESS; |
| 281 | char *mod_name = NULL, mod_rev[11]; |
| 282 | uint16_t rev; |
| 283 | |
| 284 | /* model name */ |
| 285 | ret = lyb_read_string(&mod_name, 1, lybctx); |
| 286 | LY_CHECK_GOTO(ret, cleanup); |
| 287 | |
| 288 | /* revision */ |
| 289 | lyb_read_number(&rev, sizeof rev, 2, lybctx); |
| 290 | |
| 291 | if (!mod_name[0]) { |
| 292 | /* opaq node, no module */ |
| 293 | *mod = NULL; |
| 294 | goto cleanup; |
| 295 | } |
| 296 | |
| 297 | if (rev) { |
| 298 | sprintf(mod_rev, "%04u-%02u-%02u", ((rev & 0xFE00) >> 9) + 2000, (rev & 0x01E0) >> 5, rev & 0x001Fu); |
| 299 | *mod = ly_ctx_get_module(lybctx->ctx, mod_name, mod_rev); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 300 | if ((parse_options & LYD_PARSE_LYB_MOD_UPDATE) && !(*mod)) { |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 301 | /* try to use an updated module */ |
| 302 | *mod = ly_ctx_get_module_implemented(lybctx->ctx, mod_name); |
| 303 | if (*mod && (!(*mod)->revision || (strcmp((*mod)->revision, mod_rev) < 0))) { |
| 304 | /* not an implemented module in a newer revision */ |
| 305 | *mod = NULL; |
| 306 | } |
| 307 | } |
| 308 | } else { |
| 309 | *mod = ly_ctx_get_module_latest(lybctx->ctx, mod_name); |
| 310 | } |
| 311 | /* TODO data_clb supported? |
| 312 | if (lybctx->ctx->data_clb) { |
| 313 | if (!*mod) { |
| 314 | *mod = lybctx->ctx->data_clb(lybctx->ctx, mod_name, NULL, 0, lybctx->ctx->data_clb_data); |
| 315 | } else if (!(*mod)->implemented) { |
| 316 | *mod = lybctx->ctx->data_clb(lybctx->ctx, mod_name, (*mod)->ns, LY_MODCLB_NOT_IMPLEMENTED, lybctx->ctx->data_clb_data); |
| 317 | } |
| 318 | }*/ |
| 319 | |
| 320 | if (!*mod || !(*mod)->implemented) { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 321 | if (parse_options & LYD_PARSE_STRICT) { |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 322 | if (!*mod) { |
| 323 | 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] | 324 | mod_name, rev ? "@" : "", rev ? mod_rev : ""); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 325 | } else if (!(*mod)->implemented) { |
| 326 | 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] | 327 | mod_name, rev ? "@" : "", rev ? mod_rev : ""); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 328 | } |
| 329 | ret = LY_EINVAL; |
| 330 | goto cleanup; |
| 331 | } |
| 332 | |
| 333 | } |
| 334 | |
| 335 | cleanup: |
| 336 | free(mod_name); |
| 337 | return ret; |
| 338 | } |
| 339 | |
| 340 | /** |
| 341 | * @brief Parse YANG node metadata. |
| 342 | * |
| 343 | * @param[in] lybctx LYB context. |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 344 | * @param[out] meta Parsed metadata. |
| 345 | * @return LY_ERR value. |
| 346 | */ |
| 347 | static LY_ERR |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 348 | lyb_parse_metadata(struct lyd_lyb_ctx *lybctx, struct lyd_meta **meta) |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 349 | { |
| 350 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 351 | ly_bool dynamic; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 352 | uint8_t i, count = 0; |
Michal Vasko | 1e5d561 | 2020-07-03 13:29:26 +0200 | [diff] [blame] | 353 | char *meta_name = NULL, *meta_value; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 354 | const struct lys_module *mod; |
| 355 | |
| 356 | /* read number of attributes stored */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 357 | lyb_read(&count, 1, lybctx->lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 358 | |
| 359 | /* read attributes */ |
| 360 | for (i = 0; i < count; ++i) { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 361 | ret = lyb_read_start_subtree(lybctx->lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 362 | LY_CHECK_GOTO(ret, cleanup); |
| 363 | |
| 364 | /* find model */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 365 | ret = lyb_parse_model(lybctx->lybctx, lybctx->parse_options, &mod); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 366 | LY_CHECK_GOTO(ret, cleanup); |
| 367 | |
| 368 | if (!mod) { |
| 369 | /* skip it */ |
| 370 | do { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 371 | lyb_read(NULL, LYB_LAST_SUBTREE(lybctx->lybctx).written, lybctx->lybctx); |
| 372 | } while (LYB_LAST_SUBTREE(lybctx->lybctx).written); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 373 | goto stop_subtree; |
| 374 | } |
| 375 | |
| 376 | /* meta name */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 377 | ret = lyb_read_string(&meta_name, 1, lybctx->lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 378 | LY_CHECK_GOTO(ret, cleanup); |
| 379 | |
| 380 | /* meta value */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 381 | ret = lyb_read_string(&meta_value, 0, lybctx->lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 382 | LY_CHECK_GOTO(ret, cleanup); |
| 383 | dynamic = 1; |
| 384 | |
| 385 | /* create metadata */ |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 386 | 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] | 387 | ly_strlen(meta_value), &dynamic, LY_PREF_JSON, NULL, LYD_HINT_DATA); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 388 | |
| 389 | /* free strings */ |
| 390 | free(meta_name); |
| 391 | meta_name = NULL; |
| 392 | if (dynamic) { |
| 393 | free(meta_value); |
| 394 | dynamic = 0; |
| 395 | } |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 396 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 397 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 398 | |
| 399 | stop_subtree: |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 400 | ret = lyb_read_stop_subtree(lybctx->lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 401 | LY_CHECK_GOTO(ret, cleanup); |
| 402 | } |
| 403 | |
| 404 | cleanup: |
| 405 | free(meta_name); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 406 | if (ret) { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 407 | lyd_free_meta_siblings(*meta); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 408 | *meta = NULL; |
| 409 | } |
| 410 | return ret; |
| 411 | } |
| 412 | |
| 413 | /** |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame^] | 414 | * @brief Parse format-specific prefix data. |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 415 | * |
| 416 | * @param[in] lybctx LYB context. |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame^] | 417 | * @param[in] format Prefix data format. |
| 418 | * @param[out] prefix_data Parsed prefix data. |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 419 | * @return LY_ERR value. |
| 420 | */ |
| 421 | static LY_ERR |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame^] | 422 | 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] | 423 | { |
| 424 | LY_ERR ret = LY_SUCCESS; |
| 425 | uint8_t count, i; |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame^] | 426 | struct ly_set *set = NULL; |
| 427 | struct lyxml_ns *ns = NULL; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 428 | |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame^] | 429 | switch (format) { |
| 430 | case LY_PREF_XML: |
| 431 | /* read count */ |
| 432 | lyb_read(&count, 1, lybctx); |
| 433 | if (!count) { |
| 434 | return LY_SUCCESS; |
| 435 | } |
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 | /* read all NS elements */ |
| 438 | LY_CHECK_GOTO(ret = ly_set_new(&set), cleanup); |
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 | for (i = 0; i < count; ++i) { |
| 441 | ns = calloc(1, sizeof *ns); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 442 | |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame^] | 443 | /* prefix */ |
| 444 | LY_CHECK_GOTO(ret = lyb_read_string(&ns->prefix, 1, lybctx), cleanup); |
| 445 | |
| 446 | /* namespace */ |
| 447 | LY_CHECK_GOTO(ret = lyb_read_string(&ns->uri, 1, lybctx), cleanup); |
| 448 | |
| 449 | LY_CHECK_GOTO(ret = ly_set_add(set, ns, 1, NULL), cleanup); |
| 450 | ns = NULL; |
| 451 | } |
| 452 | |
| 453 | *prefix_data = set; |
| 454 | break; |
| 455 | case LY_PREF_JSON: |
| 456 | /* nothing stored */ |
| 457 | break; |
| 458 | default: |
| 459 | LOGINT(lybctx->ctx); |
| 460 | ret = LY_EINT; |
| 461 | break; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | cleanup: |
| 465 | if (ret) { |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame^] | 466 | ly_free_prefix_data(format, set); |
| 467 | if (ns) { |
| 468 | free(ns->prefix); |
| 469 | free(ns->uri); |
| 470 | free(ns); |
| 471 | } |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 472 | } |
| 473 | return ret; |
| 474 | } |
| 475 | |
| 476 | /** |
| 477 | * @brief Parse opaque attributes. |
| 478 | * |
| 479 | * @param[in] lybctx LYB context. |
| 480 | * @param[out] attr Parsed attributes. |
| 481 | * @return LY_ERR value. |
| 482 | */ |
| 483 | static LY_ERR |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 484 | lyb_parse_attributes(struct lylyb_ctx *lybctx, struct lyd_attr **attr) |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 485 | { |
| 486 | LY_ERR ret = LY_SUCCESS; |
| 487 | uint8_t count, i; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 488 | struct lyd_attr *attr2; |
| 489 | char *prefix = NULL, *module_name = NULL, *name = NULL, *value = NULL; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 490 | ly_bool dynamic = 0; |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame^] | 491 | LY_PREFIX_FORMAT format = 0; |
| 492 | void *val_prefix_data = NULL; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 493 | |
| 494 | /* read count */ |
| 495 | lyb_read(&count, 1, lybctx); |
| 496 | |
| 497 | /* read attributes */ |
| 498 | for (i = 0; i < count; ++i) { |
| 499 | ret = lyb_read_start_subtree(lybctx); |
| 500 | LY_CHECK_GOTO(ret, cleanup); |
| 501 | |
| 502 | /* prefix, may be emtpy */ |
| 503 | ret = lyb_read_string(&prefix, 1, lybctx); |
| 504 | LY_CHECK_GOTO(ret, cleanup); |
| 505 | if (!prefix[0]) { |
| 506 | free(prefix); |
| 507 | prefix = NULL; |
| 508 | } |
| 509 | |
| 510 | /* namespace, may be empty */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 511 | ret = lyb_read_string(&module_name, 1, lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 512 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 513 | if (!module_name[0]) { |
| 514 | free(module_name); |
| 515 | module_name = NULL; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | /* name */ |
| 519 | ret = lyb_read_string(&name, 1, lybctx); |
| 520 | LY_CHECK_GOTO(ret, cleanup); |
| 521 | |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 522 | /* format */ |
| 523 | lyb_read((uint8_t *)&format, 1, lybctx); |
| 524 | |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame^] | 525 | /* value prefixes */ |
| 526 | ret = lyb_parse_prefix_data(lybctx, format, &val_prefix_data); |
| 527 | LY_CHECK_GOTO(ret, cleanup); |
| 528 | |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 529 | /* value */ |
| 530 | ret = lyb_read_string(&value, 0, lybctx); |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame^] | 531 | 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] | 532 | dynamic = 1; |
| 533 | |
| 534 | /* attr2 is always changed to the created attribute */ |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 535 | ret = lyd_create_attr(NULL, &attr2, lybctx->ctx, name, strlen(name), value, ly_strlen(value), &dynamic, format, |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame^] | 536 | 0, val_prefix_data, prefix, ly_strlen(prefix), module_name, ly_strlen(module_name)); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 537 | LY_CHECK_GOTO(ret, cleanup); |
| 538 | |
| 539 | free(prefix); |
| 540 | prefix = NULL; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 541 | free(module_name); |
| 542 | module_name = NULL; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 543 | free(name); |
| 544 | name = NULL; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 545 | assert(!dynamic); |
| 546 | value = NULL; |
| 547 | |
| 548 | if (!*attr) { |
| 549 | *attr = attr2; |
| 550 | } |
| 551 | |
| 552 | ret = lyb_read_stop_subtree(lybctx); |
| 553 | LY_CHECK_GOTO(ret, cleanup); |
| 554 | } |
| 555 | |
| 556 | cleanup: |
| 557 | free(prefix); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 558 | free(module_name); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 559 | free(name); |
| 560 | if (dynamic) { |
| 561 | free(value); |
| 562 | } |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 563 | if (ret) { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 564 | lyd_free_attr_siblings(lybctx->ctx, *attr); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 565 | *attr = NULL; |
| 566 | } |
| 567 | return ret; |
| 568 | } |
| 569 | |
| 570 | /** |
| 571 | * @brief Check whether a schema node matches a hash(es). |
| 572 | * |
| 573 | * @param[in] sibling Schema node to check. |
| 574 | * @param[in] hash Hash array to check. |
| 575 | * @param[in] hash_count Number of hashes in @p hash. |
| 576 | * @return non-zero if matches, |
| 577 | * @return 0 if not. |
| 578 | */ |
| 579 | static int |
| 580 | lyb_is_schema_hash_match(struct lysc_node *sibling, LYB_HASH *hash, uint8_t hash_count) |
| 581 | { |
| 582 | LYB_HASH sibling_hash; |
| 583 | uint8_t i; |
| 584 | |
| 585 | /* compare all the hashes starting from collision ID 0 */ |
| 586 | for (i = 0; i < hash_count; ++i) { |
| 587 | sibling_hash = lyb_hash(sibling, i); |
| 588 | if (sibling_hash != hash[i]) { |
| 589 | return 0; |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | return 1; |
| 594 | } |
| 595 | |
| 596 | /** |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 597 | * @brief Parse schema node hash. |
| 598 | * |
| 599 | * @param[in] lybctx LYB context. |
| 600 | * @param[in] sparent Schema parent, must be set if @p mod is not. |
| 601 | * @param[in] mod Module of the top-level node, must be set if @p sparent is not. |
| 602 | * @param[out] snode Parsed found schema node, may be NULL if opaque. |
| 603 | * @return LY_ERR value. |
| 604 | */ |
| 605 | static LY_ERR |
| 606 | 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] | 607 | const struct lysc_node **snode) |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 608 | { |
| 609 | LY_ERR ret; |
| 610 | uint8_t i, j; |
| 611 | const struct lysc_node *sibling; |
| 612 | LYB_HASH hash[LYB_HASH_BITS - 1]; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 613 | uint32_t getnext_opts; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 614 | |
| 615 | *snode = NULL; |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 616 | getnext_opts = lybctx->int_opts & LYD_INTOPT_REPLY ? LYS_GETNEXT_OUTPUT : 0; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 617 | |
| 618 | /* read the first hash */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 619 | lyb_read(&hash[0], sizeof *hash, lybctx->lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 620 | |
| 621 | if (!hash[0]) { |
| 622 | /* opaque node */ |
| 623 | return LY_SUCCESS; |
| 624 | } |
| 625 | |
| 626 | /* based on the first hash read all the other ones, if any */ |
| 627 | for (i = 0; !(hash[0] & (LYB_HASH_COLLISION_ID >> i)); ++i) { |
| 628 | if (i > LYB_HASH_BITS) { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 629 | LOGINT_RET(lybctx->lybctx->ctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 630 | } |
| 631 | } |
| 632 | |
| 633 | /* move the first hash on its accurate position */ |
| 634 | hash[i] = hash[0]; |
| 635 | |
| 636 | /* read the rest of hashes */ |
| 637 | for (j = i; j; --j) { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 638 | lyb_read(&hash[j - 1], sizeof *hash, lybctx->lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 639 | |
| 640 | /* correct collision ID */ |
| 641 | assert(hash[j - 1] & (LYB_HASH_COLLISION_ID >> (j - 1))); |
| 642 | /* preceded with zeros */ |
| 643 | assert(!(hash[j - 1] & (LYB_HASH_MASK << (LYB_HASH_BITS - (j - 1))))); |
| 644 | } |
| 645 | |
| 646 | /* find our node with matching hashes */ |
| 647 | sibling = NULL; |
| 648 | while ((sibling = lys_getnext(sibling, sparent, mod ? mod->compiled : NULL, getnext_opts))) { |
| 649 | /* skip schema nodes from models not present during printing */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 650 | if (lyb_has_schema_model(sibling, lybctx->lybctx->models) && |
| 651 | lyb_is_schema_hash_match((struct lysc_node *)sibling, hash, i + 1)) { |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 652 | /* match found */ |
| 653 | break; |
| 654 | } |
| 655 | } |
| 656 | |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 657 | if (!sibling && (lybctx->parse_options & LYD_PARSE_STRICT)) { |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 658 | if (mod) { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 659 | LOGVAL(lybctx->lybctx->ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "Failed to find matching hash for a top-level node" |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 660 | " from \"%s\".", mod->name); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 661 | } else { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 662 | LOGVAL(lybctx->lybctx->ctx, LY_VLOG_LYSC, sparent, LYVE_REFERENCE, "Failed to find matching hash for a child node" |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 663 | " of \"%s\".", sparent->name); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 664 | } |
| 665 | return LY_EVALID; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 666 | } 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] | 667 | return ret; |
| 668 | } |
| 669 | |
| 670 | *snode = sibling; |
| 671 | return LY_SUCCESS; |
| 672 | } |
| 673 | |
| 674 | /** |
| 675 | * @brief Read until the end of the current subtree. |
| 676 | * |
| 677 | * @param[in] lybctx LYB context. |
| 678 | */ |
| 679 | static void |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 680 | lyb_skip_subtree(struct lylyb_ctx *lybctx) |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 681 | { |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 682 | do { |
| 683 | /* first skip any meta information inside */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 684 | 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] | 685 | |
| 686 | /* then read data */ |
| 687 | lyb_read(NULL, LYB_LAST_SUBTREE(lybctx).written, lybctx); |
| 688 | } while (LYB_LAST_SUBTREE(lybctx).written); |
| 689 | } |
| 690 | |
| 691 | /** |
| 692 | * @brief Parse LYB subtree. |
| 693 | * |
| 694 | * @param[in] lybctx LYB context. |
| 695 | * @param[in] parent Data parent of the subtree, must be set if @p first is not. |
| 696 | * @param[in,out] first First top-level sibling, must be set if @p parent is not. |
| 697 | * @return LY_ERR value. |
| 698 | */ |
| 699 | static LY_ERR |
| 700 | lyb_parse_subtree_r(struct lyd_lyb_ctx *lybctx, struct lyd_node_inner *parent, struct lyd_node **first) |
| 701 | { |
| 702 | LY_ERR ret = LY_SUCCESS; |
| 703 | struct lyd_node *node = NULL, *tree; |
| 704 | const struct lys_module *mod; |
| 705 | const struct lysc_node *snode = NULL; |
| 706 | struct lyd_meta *meta = NULL, *m; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 707 | struct lyd_attr *attr = NULL, *a; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 708 | LYD_ANYDATA_VALUETYPE value_type; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 709 | char *value = NULL, *name = NULL, *prefix = NULL, *module_key = NULL; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 710 | ly_bool dynamic = 0; |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame^] | 711 | LY_PREFIX_FORMAT format = 0; |
| 712 | void *val_prefix_data = NULL; |
Michal Vasko | c5e866a | 2020-11-04 17:09:26 +0100 | [diff] [blame] | 713 | uint32_t prev_lo, flags; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 714 | const struct ly_ctx *ctx = lybctx->lybctx->ctx; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 715 | |
| 716 | /* register a new subtree */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 717 | LY_CHECK_GOTO(ret = lyb_read_start_subtree(lybctx->lybctx), cleanup); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 718 | |
| 719 | if (!parent) { |
| 720 | /* top-level, read module name */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 721 | ret = lyb_parse_model(lybctx->lybctx, lybctx->parse_options, &mod); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 722 | LY_CHECK_GOTO(ret, cleanup); |
| 723 | |
| 724 | /* read hash, find the schema node starting from mod */ |
| 725 | ret = lyb_parse_schema_hash(lybctx, NULL, mod, &snode); |
| 726 | LY_CHECK_GOTO(ret, cleanup); |
| 727 | } else { |
| 728 | /* read hash, find the schema node starting from parent schema */ |
| 729 | ret = lyb_parse_schema_hash(lybctx, parent->schema, NULL, &snode); |
| 730 | LY_CHECK_GOTO(ret, cleanup); |
| 731 | } |
| 732 | |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 733 | if (!snode && !(lybctx->parse_options & LYD_PARSE_OPAQ)) { |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 734 | /* unknown data, skip them */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 735 | lyb_skip_subtree(lybctx->lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 736 | goto stop_subtree; |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 737 | } |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 738 | |
| 739 | /* create metadata/attributes */ |
| 740 | if (snode) { |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 741 | ret = lyb_parse_metadata(lybctx, &meta); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 742 | LY_CHECK_GOTO(ret, cleanup); |
| 743 | } else { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 744 | ret = lyb_parse_attributes(lybctx->lybctx, &attr); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 745 | LY_CHECK_GOTO(ret, cleanup); |
| 746 | } |
| 747 | |
Michal Vasko | c5e866a | 2020-11-04 17:09:26 +0100 | [diff] [blame] | 748 | /* read flags */ |
| 749 | lyb_read_number(&flags, sizeof flags, sizeof flags, lybctx->lybctx); |
| 750 | |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 751 | if (!snode) { |
| 752 | /* parse prefix */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 753 | ret = lyb_read_string(&prefix, 1, lybctx->lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 754 | LY_CHECK_GOTO(ret, cleanup); |
| 755 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 756 | /* parse module key */ |
| 757 | ret = lyb_read_string(&module_key, 1, lybctx->lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 758 | LY_CHECK_GOTO(ret, cleanup); |
| 759 | |
| 760 | /* parse name */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 761 | ret = lyb_read_string(&name, 1, lybctx->lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 762 | LY_CHECK_GOTO(ret, cleanup); |
| 763 | |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 764 | /* parse format */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 765 | lyb_read((uint8_t *)&format, 1, lybctx->lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 766 | |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame^] | 767 | /* parse value prefixes */ |
| 768 | ret = lyb_parse_prefix_data(lybctx->lybctx, format, &val_prefix_data); |
| 769 | LY_CHECK_GOTO(ret, cleanup); |
| 770 | |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 771 | /* parse value */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 772 | ret = lyb_read_string(&value, 0, lybctx->lybctx); |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame^] | 773 | 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] | 774 | dynamic = 1; |
| 775 | |
| 776 | /* create node */ |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame^] | 777 | ret = lyd_create_opaq(ctx, name, strlen(name), value, strlen(value), &dynamic, format, 0, val_prefix_data, |
| 778 | prefix, ly_strlen(prefix), module_key, ly_strlen(module_key), &node); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 779 | LY_CHECK_GOTO(ret, cleanup); |
| 780 | |
| 781 | /* process children */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 782 | while (LYB_LAST_SUBTREE(lybctx->lybctx).written) { |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 783 | ret = lyb_parse_subtree_r(lybctx, (struct lyd_node_inner *)node, NULL); |
| 784 | LY_CHECK_GOTO(ret, cleanup); |
| 785 | } |
| 786 | } else if (snode->nodetype & LYD_NODE_TERM) { |
| 787 | /* parse value */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 788 | ret = lyb_read_string(&value, 0, lybctx->lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 789 | LY_CHECK_GOTO(ret, cleanup); |
| 790 | dynamic = 1; |
| 791 | |
| 792 | /* create node */ |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 793 | 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] | 794 | NULL, LYD_HINT_DATA, &node); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 795 | if (dynamic) { |
| 796 | free(value); |
| 797 | dynamic = 0; |
| 798 | } |
| 799 | value = NULL; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 800 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 801 | } else if (snode->nodetype & LYD_NODE_INNER) { |
| 802 | /* create node */ |
| 803 | ret = lyd_create_inner(snode, &node); |
| 804 | LY_CHECK_GOTO(ret, cleanup); |
| 805 | |
| 806 | /* process children */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 807 | while (LYB_LAST_SUBTREE(lybctx->lybctx).written) { |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 808 | ret = lyb_parse_subtree_r(lybctx, (struct lyd_node_inner *)node, NULL); |
| 809 | LY_CHECK_GOTO(ret, cleanup); |
| 810 | } |
| 811 | |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 812 | if (!(lybctx->parse_options & LYD_PARSE_ONLY)) { |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 813 | /* new node validation, autodelete CANNOT occur, all nodes are new */ |
Michal Vasko | 8104fd4 | 2020-07-13 11:09:51 +0200 | [diff] [blame] | 814 | ret = lyd_validate_new(lyd_node_children_p(node), snode, NULL, NULL); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 815 | LY_CHECK_GOTO(ret, cleanup); |
| 816 | |
| 817 | /* add any missing default children */ |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 818 | ret = lyd_new_implicit_r(node, lyd_node_children_p(node), NULL, NULL, &lybctx->unres_node_type, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 819 | &lybctx->when_check, (lybctx->validate_options & LYD_VALIDATE_NO_STATE) ? |
| 820 | LYD_IMPLICIT_NO_STATE : 0, NULL); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 821 | LY_CHECK_GOTO(ret, cleanup); |
| 822 | } |
| 823 | |
Michal Vasko | 751cb4d | 2020-07-14 12:25:28 +0200 | [diff] [blame] | 824 | if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) { |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 825 | /* rememeber the RPC/action/notification */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 826 | lybctx->op_node = node; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 827 | } |
| 828 | } else if (snode->nodetype & LYD_NODE_ANY) { |
| 829 | /* parse value type */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 830 | lyb_read((uint8_t *)&value_type, sizeof value_type, lybctx->lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 831 | if (value_type == LYD_ANYDATA_DATATREE) { |
| 832 | /* invalid situation */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 833 | LOGINT(ctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 834 | goto cleanup; |
| 835 | } |
| 836 | |
| 837 | /* read anydata content */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 838 | ret = lyb_read_string(&value, 0, lybctx->lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 839 | LY_CHECK_GOTO(ret, cleanup); |
| 840 | dynamic = 1; |
| 841 | |
| 842 | if (value_type == LYD_ANYDATA_LYB) { |
| 843 | /* turn logging off */ |
| 844 | prev_lo = ly_log_options(0); |
| 845 | |
| 846 | /* try to parse LYB into a data tree */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 847 | 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] | 848 | /* successfully parsed */ |
| 849 | free(value); |
| 850 | value = (char *)tree; |
| 851 | value_type = LYD_ANYDATA_DATATREE; |
| 852 | } |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 853 | |
| 854 | /* turn logging on again */ |
| 855 | ly_log_options(prev_lo); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 856 | } |
| 857 | |
| 858 | /* create node */ |
| 859 | ret = lyd_create_any(snode, value, value_type, &node); |
| 860 | LY_CHECK_GOTO(ret, cleanup); |
| 861 | |
| 862 | dynamic = 0; |
| 863 | value = NULL; |
| 864 | } |
| 865 | assert(node); |
| 866 | |
Michal Vasko | c5e866a | 2020-11-04 17:09:26 +0100 | [diff] [blame] | 867 | /* set flags */ |
| 868 | node->flags = flags; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 869 | |
| 870 | /* add metadata/attributes */ |
| 871 | if (snode) { |
| 872 | LY_LIST_FOR(meta, m) { |
| 873 | m->parent = node; |
| 874 | } |
| 875 | node->meta = meta; |
| 876 | meta = NULL; |
| 877 | } else { |
| 878 | assert(!node->schema); |
| 879 | LY_LIST_FOR(attr, a) { |
| 880 | a->parent = (struct lyd_node_opaq *)node; |
| 881 | } |
| 882 | ((struct lyd_node_opaq *)node)->attr = attr; |
| 883 | attr = NULL; |
| 884 | } |
| 885 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 886 | /* insert, keep first pointer correct */ |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 887 | lyd_insert_node((struct lyd_node *)parent, first, node); |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 888 | while (!parent && (*first)->prev->next) { |
| 889 | *first = (*first)->prev; |
| 890 | } |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 891 | node = NULL; |
| 892 | |
| 893 | stop_subtree: |
| 894 | /* end the subtree */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 895 | ret = lyb_read_stop_subtree(lybctx->lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 896 | LY_CHECK_GOTO(ret, cleanup); |
| 897 | |
| 898 | cleanup: |
| 899 | free(prefix); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 900 | free(module_key); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 901 | free(name); |
| 902 | if (dynamic) { |
| 903 | free(value); |
| 904 | } |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 905 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 906 | lyd_free_meta_siblings(meta); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 907 | lyd_free_attr_siblings(ctx, attr); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 908 | lyd_free_tree(node); |
| 909 | return ret; |
| 910 | } |
| 911 | |
| 912 | /** |
| 913 | * @brief Parse used YANG data models. |
| 914 | * |
| 915 | * @param[in] lybctx LYB context. |
| 916 | * @return LY_ERR value. |
| 917 | */ |
| 918 | static LY_ERR |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 919 | lyb_parse_data_models(struct lylyb_ctx *lybctx, uint32_t parse_options) |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 920 | { |
| 921 | LY_ERR ret; |
| 922 | uint32_t count; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 923 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 924 | |
| 925 | /* read model count */ |
| 926 | lyb_read_number(&count, sizeof count, 2, lybctx); |
| 927 | |
| 928 | if (count) { |
| 929 | LY_ARRAY_CREATE_RET(lybctx->ctx, lybctx->models, count, LY_EMEM); |
| 930 | |
| 931 | /* read modules */ |
| 932 | for (u = 0; u < count; ++u) { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 933 | ret = lyb_parse_model(lybctx, parse_options, &lybctx->models[u]); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 934 | LY_CHECK_RET(ret); |
| 935 | LY_ARRAY_INCREMENT(lybctx->models); |
| 936 | } |
| 937 | } |
| 938 | |
| 939 | return LY_SUCCESS; |
| 940 | } |
| 941 | |
| 942 | /** |
| 943 | * @brief Parse LYB magic number. |
| 944 | * |
| 945 | * @param[in] lybctx LYB context. |
| 946 | * @return LY_ERR value. |
| 947 | */ |
| 948 | static LY_ERR |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 949 | lyb_parse_magic_number(struct lylyb_ctx *lybctx) |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 950 | { |
| 951 | char magic_byte = 0; |
| 952 | |
| 953 | lyb_read((uint8_t *)&magic_byte, 1, lybctx); |
| 954 | if (magic_byte != 'l') { |
| 955 | LOGERR(lybctx->ctx, LY_EINVAL, "Invalid first magic number byte \"0x%02x\".", magic_byte); |
| 956 | return LY_EINVAL; |
| 957 | } |
| 958 | |
| 959 | lyb_read((uint8_t *)&magic_byte, 1, lybctx); |
| 960 | if (magic_byte != 'y') { |
| 961 | LOGERR(lybctx->ctx, LY_EINVAL, "Invalid second magic number byte \"0x%02x\".", magic_byte); |
| 962 | return LY_EINVAL; |
| 963 | } |
| 964 | |
| 965 | lyb_read((uint8_t *)&magic_byte, 1, lybctx); |
| 966 | if (magic_byte != 'b') { |
| 967 | LOGERR(lybctx->ctx, LY_EINVAL, "Invalid third magic number byte \"0x%02x\".", magic_byte); |
| 968 | return LY_EINVAL; |
| 969 | } |
| 970 | |
| 971 | return LY_SUCCESS; |
| 972 | } |
| 973 | |
| 974 | /** |
| 975 | * @brief Parse LYB header. |
| 976 | * |
| 977 | * @param[in] lybctx LYB context. |
| 978 | * @return LY_ERR value. |
| 979 | */ |
| 980 | static LY_ERR |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 981 | lyb_parse_header(struct lylyb_ctx *lybctx) |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 982 | { |
| 983 | uint8_t byte = 0; |
| 984 | |
| 985 | /* version, future flags */ |
| 986 | lyb_read((uint8_t *)&byte, sizeof byte, lybctx); |
| 987 | |
| 988 | if ((byte & LYB_VERSION_MASK) != LYB_VERSION_NUM) { |
| 989 | 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] | 990 | byte & LYB_VERSION_MASK, LYB_VERSION_NUM); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 991 | return LY_EINVAL; |
| 992 | } |
| 993 | |
| 994 | return LY_SUCCESS; |
| 995 | } |
| 996 | |
Michal Vasko | ee38a5d | 2020-11-09 21:02:18 +0100 | [diff] [blame] | 997 | /** |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 998 | * @param[in] ctx libyang context for logging |
Michal Vasko | ee38a5d | 2020-11-09 21:02:18 +0100 | [diff] [blame] | 999 | * @param[in] parent Parent node where to connect the parsed data, required for reply where the reply data are connected |
| 1000 | * with the request operation |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1001 | * @param[in] in Input structure. |
| 1002 | * @param[in] parse_options Options for parser, see @ref dataparseroptions. |
| 1003 | * @param[in] validate_options Options for the validation phase, see @ref datavalidationoptions. |
| 1004 | * @param[in] data_type Internal data parser flag to distnguish type of the data to parse (RPC/Reply/Notification/regular data]. |
| 1005 | * @param[out] tree_p Parsed data tree. Note that NULL can be a valid result. |
| 1006 | * @param[out] op_p Optional pointer to the actual operation. Useful for action and inner notifications. |
| 1007 | * @param[out] lydctx_p Data parser context to finish validation. |
| 1008 | */ |
| 1009 | static LY_ERR |
Michal Vasko | ee38a5d | 2020-11-09 21:02:18 +0100 | [diff] [blame] | 1010 | lyd_parse_lyb_(const struct ly_ctx *ctx, struct lyd_node_inner **parent, struct ly_in *in, uint32_t parse_options, |
| 1011 | uint32_t validate_options, uint32_t data_type, struct lyd_node **tree_p, struct lyd_node **op_p, |
| 1012 | struct lyd_ctx **lydctx_p) |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1013 | { |
| 1014 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1015 | struct lyd_lyb_ctx *lybctx; |
| 1016 | struct lyd_node *tree = NULL; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1017 | |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 1018 | assert(!(parse_options & ~LYD_PARSE_OPTS_MASK)); |
| 1019 | assert(!(validate_options & ~LYD_VALIDATE_OPTS_MASK)); |
| 1020 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1021 | lybctx = calloc(1, sizeof *lybctx); |
| 1022 | LY_CHECK_ERR_RET(!lybctx, LOGMEM(ctx), LY_EMEM); |
| 1023 | lybctx->lybctx = calloc(1, sizeof *lybctx->lybctx); |
Michal Vasko | 9acaf49 | 2020-08-13 09:05:58 +0200 | [diff] [blame] | 1024 | LY_CHECK_ERR_GOTO(!lybctx->lybctx, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1025 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1026 | lybctx->lybctx->in = in; |
| 1027 | lybctx->lybctx->ctx = ctx; |
| 1028 | lybctx->parse_options = parse_options; |
| 1029 | lybctx->validate_options = validate_options; |
| 1030 | lybctx->int_opts = data_type; |
| 1031 | lybctx->free = lyd_lyb_ctx_free; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1032 | |
| 1033 | /* read magic number */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1034 | ret = lyb_parse_magic_number(lybctx->lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1035 | LY_CHECK_GOTO(ret, cleanup); |
| 1036 | |
| 1037 | /* read header */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1038 | ret = lyb_parse_header(lybctx->lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1039 | LY_CHECK_GOTO(ret, cleanup); |
| 1040 | |
| 1041 | /* read used models */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1042 | ret = lyb_parse_data_models(lybctx->lybctx, lybctx->parse_options); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1043 | LY_CHECK_GOTO(ret, cleanup); |
| 1044 | |
| 1045 | /* read subtree(s) */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1046 | while (lybctx->lybctx->in->current[0]) { |
| 1047 | ret = lyb_parse_subtree_r(lybctx, parent ? *parent : NULL, &tree); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1048 | LY_CHECK_GOTO(ret, cleanup); |
| 1049 | } |
| 1050 | |
| 1051 | /* read the last zero, parsing finished */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1052 | ly_in_skip(lybctx->lybctx->in, 1); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1053 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1054 | if (data_type == LYD_INTOPT_RPC) { |
| 1055 | /* make sure we have parsed some operation */ |
| 1056 | if (!lybctx->op_node) { |
| 1057 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_DATA, "Missing the \"rpc\"/\"action\" node."); |
| 1058 | ret = LY_EVALID; |
| 1059 | goto cleanup; |
| 1060 | } |
| 1061 | |
| 1062 | if (op_p) { |
| 1063 | *op_p = lybctx->op_node; |
| 1064 | } |
| 1065 | assert(tree); |
| 1066 | } else if (data_type == LYD_INTOPT_REPLY) { |
| 1067 | struct lyd_node_inner *iter; |
| 1068 | |
| 1069 | assert(parent); |
| 1070 | |
| 1071 | if (op_p) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1072 | *op_p = (struct lyd_node *)(*parent); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1073 | } |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 1074 | for (iter = *parent; iter->parent; iter = iter->parent) {} |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1075 | tree = (struct lyd_node *)iter; |
| 1076 | *parent = NULL; |
| 1077 | |
| 1078 | } else if (data_type == LYD_INTOPT_NOTIF) { |
| 1079 | /* make sure we have parsed some notification */ |
| 1080 | if (!lybctx->op_node) { |
| 1081 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_DATA, "Missing the \"notification\" node."); |
| 1082 | ret = LY_EVALID; |
| 1083 | goto cleanup; |
| 1084 | } |
| 1085 | |
| 1086 | if (op_p) { |
| 1087 | *op_p = lybctx->op_node; |
| 1088 | } |
| 1089 | assert(tree); |
| 1090 | } |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1091 | |
| 1092 | cleanup: |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1093 | if (ret || !lydctx_p) { |
| 1094 | lyd_lyb_ctx_free((struct lyd_ctx *)lybctx); |
| 1095 | if (ret) { |
| 1096 | lyd_free_all(tree); |
| 1097 | } |
| 1098 | } else { |
| 1099 | *lydctx_p = (struct lyd_ctx *)lybctx; |
| 1100 | if (tree_p) { |
| 1101 | *tree_p = tree; |
| 1102 | } |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1103 | } |
| 1104 | return ret; |
| 1105 | } |
| 1106 | |
| 1107 | LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1108 | lyd_parse_lyb_data(const struct ly_ctx *ctx, struct ly_in *in, uint32_t parse_options, uint32_t validate_options, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1109 | struct lyd_node **tree_p, struct lyd_ctx **lydctx_p) |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1110 | { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1111 | return lyd_parse_lyb_(ctx, NULL, in, parse_options, validate_options, 0, tree_p, NULL, lydctx_p); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1112 | } |
| 1113 | |
| 1114 | LY_ERR |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1115 | lyd_parse_lyb_rpc(const struct ly_ctx *ctx, struct ly_in *in, struct lyd_node **tree_p, struct lyd_node **op_p) |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1116 | { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1117 | return lyd_parse_lyb_(ctx, NULL, in, LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, LYD_INTOPT_RPC, tree_p, op_p, NULL); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1118 | } |
| 1119 | |
| 1120 | LY_ERR |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1121 | lyd_parse_lyb_notif(const struct ly_ctx *ctx, struct ly_in *in, struct lyd_node **tree_p, struct lyd_node **ntf_p) |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1122 | { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1123 | return lyd_parse_lyb_(ctx, NULL, in, LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, LYD_INTOPT_NOTIF, tree_p, ntf_p, NULL); |
| 1124 | } |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1125 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1126 | LY_ERR |
| 1127 | lyd_parse_lyb_reply(const struct lyd_node *request, struct ly_in *in, struct lyd_node **tree_p, struct lyd_node **op_p) |
| 1128 | { |
| 1129 | LY_ERR ret; |
| 1130 | struct lyd_node *req_op, *rep_op = NULL; |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1131 | const struct ly_ctx *ctx = LYD_CTX(request); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1132 | |
| 1133 | /* find request OP */ |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1134 | LYD_TREE_DFS_BEGIN((struct lyd_node *)request, req_op) { |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1135 | if (req_op->schema->nodetype & (LYS_RPC | LYS_ACTION)) { |
| 1136 | break; |
| 1137 | } |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1138 | LYD_TREE_DFS_END(request, req_op); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1139 | } |
| 1140 | if (!(req_op->schema->nodetype & (LYS_RPC | LYS_ACTION))) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1141 | LOGERR(LYD_CTX(request), LY_EINVAL, "No RPC/action in the request found."); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1142 | return LY_EINVAL; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1143 | } |
| 1144 | |
| 1145 | /* duplicate request OP with parents */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1146 | LY_CHECK_RET(lyd_dup_single(req_op, NULL, LYD_DUP_WITH_PARENTS, &rep_op)); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1147 | |
Michal Vasko | ee38a5d | 2020-11-09 21:02:18 +0100 | [diff] [blame] | 1148 | ret = lyd_parse_lyb_(ctx, (struct lyd_node_inner **)&rep_op, in, LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, |
| 1149 | LYD_INTOPT_REPLY, tree_p, op_p, NULL); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1150 | |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1151 | lyd_free_all(rep_op); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1152 | |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1153 | return ret; |
| 1154 | } |
| 1155 | |
| 1156 | API int |
| 1157 | lyd_lyb_data_length(const char *data) |
| 1158 | { |
| 1159 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1160 | struct lylyb_ctx *lybctx; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1161 | int count, i; |
| 1162 | size_t len; |
| 1163 | uint8_t buf[LYB_SIZE_MAX]; |
| 1164 | |
| 1165 | if (!data) { |
| 1166 | return -1; |
| 1167 | } |
| 1168 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1169 | lybctx = calloc(1, sizeof *lybctx); |
| 1170 | LY_CHECK_ERR_RET(!lybctx, LOGMEM(NULL), LY_EMEM); |
| 1171 | ret = ly_in_new_memory(data, &lybctx->in); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1172 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1173 | |
| 1174 | /* read magic number */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1175 | ret = lyb_parse_magic_number(lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1176 | LY_CHECK_GOTO(ret, cleanup); |
| 1177 | |
| 1178 | /* read header */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1179 | ret = lyb_parse_header(lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1180 | LY_CHECK_GOTO(ret, cleanup); |
| 1181 | |
| 1182 | /* read model count */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1183 | lyb_read_number(&count, sizeof count, 2, lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1184 | |
| 1185 | /* read all models */ |
| 1186 | for (i = 0; i < count; ++i) { |
| 1187 | /* module name length */ |
| 1188 | len = 0; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1189 | lyb_read_number(&len, sizeof len, 2, lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1190 | |
| 1191 | /* model name */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1192 | lyb_read(buf, len, lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1193 | |
| 1194 | /* revision */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1195 | lyb_read(buf, 2, lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1196 | } |
| 1197 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1198 | while (lybctx->in->current[0]) { |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1199 | /* register a new subtree */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1200 | ret = lyb_read_start_subtree(lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1201 | LY_CHECK_GOTO(ret, cleanup); |
| 1202 | |
| 1203 | /* skip it */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1204 | lyb_skip_subtree(lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1205 | |
| 1206 | /* subtree finished */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1207 | ret = lyb_read_stop_subtree(lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1208 | LY_CHECK_GOTO(ret, cleanup); |
| 1209 | } |
| 1210 | |
| 1211 | /* read the last zero, parsing finished */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1212 | ly_in_skip(lybctx->in, 1); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1213 | |
| 1214 | cleanup: |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1215 | count = lybctx->in->current - lybctx->in->start; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1216 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1217 | ly_in_free(lybctx->in, 0); |
| 1218 | lylyb_ctx_free(lybctx); |
| 1219 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1220 | return ret ? -1 : count; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1221 | } |