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