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