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