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. |
| 340 | * @param[in] sparent Schema parent node. |
| 341 | * @param[out] meta Parsed metadata. |
| 342 | * @return LY_ERR value. |
| 343 | */ |
| 344 | static LY_ERR |
| 345 | lyb_parse_metadata(struct lyd_lyb_ctx *lybctx, const struct lysc_node *sparent, struct lyd_meta **meta) |
| 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, |
| 384 | ly_strlen(meta_value), &dynamic, 0, LY_PREF_JSON, NULL, sparent); |
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); |
| 506 | LY_CHECK_GOTO(ret, cleanup); |
| 507 | dynamic = 1; |
| 508 | |
| 509 | /* attr2 is always changed to the created attribute */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 510 | ret = lyd_create_attr(NULL, &attr2, lybctx->ctx, name, strlen(name), value, ly_strlen(value), &dynamic, 0, format, |
| 511 | 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; |
| 520 | val_prefs = NULL; |
| 521 | assert(!dynamic); |
| 522 | value = NULL; |
| 523 | |
| 524 | if (!*attr) { |
| 525 | *attr = attr2; |
| 526 | } |
| 527 | |
| 528 | ret = lyb_read_stop_subtree(lybctx); |
| 529 | LY_CHECK_GOTO(ret, cleanup); |
| 530 | } |
| 531 | |
| 532 | cleanup: |
| 533 | free(prefix); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 534 | free(module_name); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 535 | free(name); |
| 536 | if (dynamic) { |
| 537 | free(value); |
| 538 | } |
| 539 | ly_free_val_prefs(lybctx->ctx, val_prefs); |
| 540 | if (ret) { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame^] | 541 | lyd_free_attr_siblings(lybctx->ctx, *attr); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 542 | *attr = NULL; |
| 543 | } |
| 544 | return ret; |
| 545 | } |
| 546 | |
| 547 | /** |
| 548 | * @brief Check whether a schema node matches a hash(es). |
| 549 | * |
| 550 | * @param[in] sibling Schema node to check. |
| 551 | * @param[in] hash Hash array to check. |
| 552 | * @param[in] hash_count Number of hashes in @p hash. |
| 553 | * @return non-zero if matches, |
| 554 | * @return 0 if not. |
| 555 | */ |
| 556 | static int |
| 557 | lyb_is_schema_hash_match(struct lysc_node *sibling, LYB_HASH *hash, uint8_t hash_count) |
| 558 | { |
| 559 | LYB_HASH sibling_hash; |
| 560 | uint8_t i; |
| 561 | |
| 562 | /* compare all the hashes starting from collision ID 0 */ |
| 563 | for (i = 0; i < hash_count; ++i) { |
| 564 | sibling_hash = lyb_hash(sibling, i); |
| 565 | if (sibling_hash != hash[i]) { |
| 566 | return 0; |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | return 1; |
| 571 | } |
| 572 | |
| 573 | /** |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 574 | * @brief Parse schema node hash. |
| 575 | * |
| 576 | * @param[in] lybctx LYB context. |
| 577 | * @param[in] sparent Schema parent, must be set if @p mod is not. |
| 578 | * @param[in] mod Module of the top-level node, must be set if @p sparent is not. |
| 579 | * @param[out] snode Parsed found schema node, may be NULL if opaque. |
| 580 | * @return LY_ERR value. |
| 581 | */ |
| 582 | static LY_ERR |
| 583 | lyb_parse_schema_hash(struct lyd_lyb_ctx *lybctx, const struct lysc_node *sparent, const struct lys_module *mod, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 584 | const struct lysc_node **snode) |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 585 | { |
| 586 | LY_ERR ret; |
| 587 | uint8_t i, j; |
| 588 | const struct lysc_node *sibling; |
| 589 | LYB_HASH hash[LYB_HASH_BITS - 1]; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 590 | uint32_t getnext_opts; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 591 | |
| 592 | *snode = NULL; |
| 593 | /* leave if-feature check for validation */ |
| 594 | getnext_opts = LYS_GETNEXT_NOSTATECHECK | (lybctx->int_opts & LYD_INTOPT_REPLY ? LYS_GETNEXT_OUTPUT : 0); |
| 595 | |
| 596 | /* read the first hash */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 597 | lyb_read(&hash[0], sizeof *hash, lybctx->lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 598 | |
| 599 | if (!hash[0]) { |
| 600 | /* opaque node */ |
| 601 | return LY_SUCCESS; |
| 602 | } |
| 603 | |
| 604 | /* based on the first hash read all the other ones, if any */ |
| 605 | for (i = 0; !(hash[0] & (LYB_HASH_COLLISION_ID >> i)); ++i) { |
| 606 | if (i > LYB_HASH_BITS) { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 607 | LOGINT_RET(lybctx->lybctx->ctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 608 | } |
| 609 | } |
| 610 | |
| 611 | /* move the first hash on its accurate position */ |
| 612 | hash[i] = hash[0]; |
| 613 | |
| 614 | /* read the rest of hashes */ |
| 615 | for (j = i; j; --j) { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 616 | lyb_read(&hash[j - 1], sizeof *hash, lybctx->lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 617 | |
| 618 | /* correct collision ID */ |
| 619 | assert(hash[j - 1] & (LYB_HASH_COLLISION_ID >> (j - 1))); |
| 620 | /* preceded with zeros */ |
| 621 | assert(!(hash[j - 1] & (LYB_HASH_MASK << (LYB_HASH_BITS - (j - 1))))); |
| 622 | } |
| 623 | |
| 624 | /* find our node with matching hashes */ |
| 625 | sibling = NULL; |
| 626 | while ((sibling = lys_getnext(sibling, sparent, mod ? mod->compiled : NULL, getnext_opts))) { |
| 627 | /* skip schema nodes from models not present during printing */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 628 | if (lyb_has_schema_model(sibling, lybctx->lybctx->models) |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 629 | && lyb_is_schema_hash_match((struct lysc_node *)sibling, hash, i + 1)) { |
| 630 | /* match found */ |
| 631 | break; |
| 632 | } |
| 633 | } |
| 634 | |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 635 | if (!sibling && (lybctx->parse_options & LYD_PARSE_STRICT)) { |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 636 | if (mod) { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 637 | 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] | 638 | " from \"%s\".", mod->name); |
| 639 | } else { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 640 | 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] | 641 | " of \"%s\".", sparent->name); |
| 642 | } |
| 643 | return LY_EVALID; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 644 | } 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] | 645 | return ret; |
| 646 | } |
| 647 | |
| 648 | *snode = sibling; |
| 649 | return LY_SUCCESS; |
| 650 | } |
| 651 | |
| 652 | /** |
| 653 | * @brief Read until the end of the current subtree. |
| 654 | * |
| 655 | * @param[in] lybctx LYB context. |
| 656 | */ |
| 657 | static void |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 658 | lyb_skip_subtree(struct lylyb_ctx *lybctx) |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 659 | { |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 660 | do { |
| 661 | /* first skip any meta information inside */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 662 | 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] | 663 | |
| 664 | /* then read data */ |
| 665 | lyb_read(NULL, LYB_LAST_SUBTREE(lybctx).written, lybctx); |
| 666 | } while (LYB_LAST_SUBTREE(lybctx).written); |
| 667 | } |
| 668 | |
| 669 | /** |
| 670 | * @brief Parse LYB subtree. |
| 671 | * |
| 672 | * @param[in] lybctx LYB context. |
| 673 | * @param[in] parent Data parent of the subtree, must be set if @p first is not. |
| 674 | * @param[in,out] first First top-level sibling, must be set if @p parent is not. |
| 675 | * @return LY_ERR value. |
| 676 | */ |
| 677 | static LY_ERR |
| 678 | lyb_parse_subtree_r(struct lyd_lyb_ctx *lybctx, struct lyd_node_inner *parent, struct lyd_node **first) |
| 679 | { |
| 680 | LY_ERR ret = LY_SUCCESS; |
| 681 | struct lyd_node *node = NULL, *tree; |
| 682 | const struct lys_module *mod; |
| 683 | const struct lysc_node *snode = NULL; |
| 684 | struct lyd_meta *meta = NULL, *m; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 685 | struct lyd_attr *attr = NULL, *a; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 686 | struct ly_prefix *val_prefs = NULL; |
| 687 | LYD_ANYDATA_VALUETYPE value_type; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 688 | char *value = NULL, *name = NULL, *prefix = NULL, *module_key = NULL; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 689 | ly_bool dynamic = 0; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 690 | LYD_FORMAT format = 0; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 691 | uint32_t prev_lo; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 692 | const struct ly_ctx *ctx = lybctx->lybctx->ctx; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 693 | |
| 694 | /* register a new subtree */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 695 | LY_CHECK_GOTO(ret = lyb_read_start_subtree(lybctx->lybctx), cleanup); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 696 | |
| 697 | if (!parent) { |
| 698 | /* top-level, read module name */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 699 | ret = lyb_parse_model(lybctx->lybctx, lybctx->parse_options, &mod); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 700 | LY_CHECK_GOTO(ret, cleanup); |
| 701 | |
| 702 | /* read hash, find the schema node starting from mod */ |
| 703 | ret = lyb_parse_schema_hash(lybctx, NULL, mod, &snode); |
| 704 | LY_CHECK_GOTO(ret, cleanup); |
| 705 | } else { |
| 706 | /* read hash, find the schema node starting from parent schema */ |
| 707 | ret = lyb_parse_schema_hash(lybctx, parent->schema, NULL, &snode); |
| 708 | LY_CHECK_GOTO(ret, cleanup); |
| 709 | } |
| 710 | |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 711 | if (!snode && !(lybctx->parse_options & LYD_PARSE_OPAQ)) { |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 712 | /* unknown data, skip them */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 713 | lyb_skip_subtree(lybctx->lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 714 | goto stop_subtree; |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 715 | } |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 716 | |
| 717 | /* create metadata/attributes */ |
| 718 | if (snode) { |
| 719 | ret = lyb_parse_metadata(lybctx, snode, &meta); |
| 720 | LY_CHECK_GOTO(ret, cleanup); |
| 721 | } else { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 722 | ret = lyb_parse_attributes(lybctx->lybctx, &attr); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 723 | LY_CHECK_GOTO(ret, cleanup); |
| 724 | } |
| 725 | |
| 726 | if (!snode) { |
| 727 | /* parse prefix */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 728 | ret = lyb_read_string(&prefix, 1, lybctx->lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 729 | LY_CHECK_GOTO(ret, cleanup); |
| 730 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 731 | /* parse module key */ |
| 732 | ret = lyb_read_string(&module_key, 1, lybctx->lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 733 | LY_CHECK_GOTO(ret, cleanup); |
| 734 | |
| 735 | /* parse name */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 736 | ret = lyb_read_string(&name, 1, lybctx->lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 737 | LY_CHECK_GOTO(ret, cleanup); |
| 738 | |
| 739 | /* parse value prefixes */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 740 | ret = lyb_parse_opaq_prefixes(lybctx->lybctx, &val_prefs); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 741 | LY_CHECK_GOTO(ret, cleanup); |
| 742 | |
| 743 | /* parse format */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 744 | lyb_read((uint8_t *)&format, 1, lybctx->lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 745 | |
| 746 | /* parse value */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 747 | ret = lyb_read_string(&value, 0, lybctx->lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 748 | LY_CHECK_GOTO(ret, cleanup); |
| 749 | dynamic = 1; |
| 750 | |
| 751 | /* create node */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 752 | ret = lyd_create_opaq(ctx, name, strlen(name), value, strlen(value), &dynamic, 0, format, val_prefs, prefix, |
| 753 | ly_strlen(prefix), module_key, ly_strlen(module_key), &node); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 754 | LY_CHECK_GOTO(ret, cleanup); |
| 755 | |
| 756 | /* process children */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 757 | while (LYB_LAST_SUBTREE(lybctx->lybctx).written) { |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 758 | ret = lyb_parse_subtree_r(lybctx, (struct lyd_node_inner *)node, NULL); |
| 759 | LY_CHECK_GOTO(ret, cleanup); |
| 760 | } |
| 761 | } else if (snode->nodetype & LYD_NODE_TERM) { |
| 762 | /* parse value */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 763 | ret = lyb_read_string(&value, 0, lybctx->lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 764 | LY_CHECK_GOTO(ret, cleanup); |
| 765 | dynamic = 1; |
| 766 | |
| 767 | /* create node */ |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 768 | ret = lyd_parser_create_term((struct lyd_ctx *)lybctx, snode, value, ly_strlen(value), &dynamic, 0, |
| 769 | LY_PREF_JSON, NULL, &node); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 770 | if (dynamic) { |
| 771 | free(value); |
| 772 | dynamic = 0; |
| 773 | } |
| 774 | value = NULL; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 775 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 776 | } else if (snode->nodetype & LYD_NODE_INNER) { |
| 777 | /* create node */ |
| 778 | ret = lyd_create_inner(snode, &node); |
| 779 | LY_CHECK_GOTO(ret, cleanup); |
| 780 | |
| 781 | /* process children */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 782 | while (LYB_LAST_SUBTREE(lybctx->lybctx).written) { |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 783 | ret = lyb_parse_subtree_r(lybctx, (struct lyd_node_inner *)node, NULL); |
| 784 | LY_CHECK_GOTO(ret, cleanup); |
| 785 | } |
| 786 | |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 787 | if (!(lybctx->parse_options & LYD_PARSE_ONLY)) { |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 788 | /* new node validation, autodelete CANNOT occur, all nodes are new */ |
Michal Vasko | 8104fd4 | 2020-07-13 11:09:51 +0200 | [diff] [blame] | 789 | ret = lyd_validate_new(lyd_node_children_p(node), snode, NULL, NULL); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 790 | LY_CHECK_GOTO(ret, cleanup); |
| 791 | |
| 792 | /* add any missing default children */ |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 793 | ret = lyd_new_implicit_r(node, lyd_node_children_p(node), NULL, NULL, &lybctx->unres_node_type, |
| 794 | &lybctx->when_check, (lybctx->validate_options & LYD_VALIDATE_NO_STATE) |
| 795 | ? LYD_IMPLICIT_NO_STATE : 0, NULL); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 796 | LY_CHECK_GOTO(ret, cleanup); |
| 797 | } |
| 798 | |
Michal Vasko | 751cb4d | 2020-07-14 12:25:28 +0200 | [diff] [blame] | 799 | if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) { |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 800 | /* rememeber the RPC/action/notification */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 801 | lybctx->op_node = node; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 802 | } |
| 803 | } else if (snode->nodetype & LYD_NODE_ANY) { |
| 804 | /* parse value type */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 805 | lyb_read((uint8_t *)&value_type, sizeof value_type, lybctx->lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 806 | if (value_type == LYD_ANYDATA_DATATREE) { |
| 807 | /* invalid situation */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 808 | LOGINT(ctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 809 | goto cleanup; |
| 810 | } |
| 811 | |
| 812 | /* read anydata content */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 813 | ret = lyb_read_string(&value, 0, lybctx->lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 814 | LY_CHECK_GOTO(ret, cleanup); |
| 815 | dynamic = 1; |
| 816 | |
| 817 | if (value_type == LYD_ANYDATA_LYB) { |
| 818 | /* turn logging off */ |
| 819 | prev_lo = ly_log_options(0); |
| 820 | |
| 821 | /* try to parse LYB into a data tree */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 822 | 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] | 823 | /* successfully parsed */ |
| 824 | free(value); |
| 825 | value = (char *)tree; |
| 826 | value_type = LYD_ANYDATA_DATATREE; |
| 827 | } |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 828 | |
| 829 | /* turn logging on again */ |
| 830 | ly_log_options(prev_lo); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 831 | } |
| 832 | |
| 833 | /* create node */ |
| 834 | ret = lyd_create_any(snode, value, value_type, &node); |
| 835 | LY_CHECK_GOTO(ret, cleanup); |
| 836 | |
| 837 | dynamic = 0; |
| 838 | value = NULL; |
| 839 | } |
| 840 | assert(node); |
| 841 | |
| 842 | /* add/correct flags */ |
| 843 | if (snode) { |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 844 | 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] | 845 | } |
| 846 | |
| 847 | /* add metadata/attributes */ |
| 848 | if (snode) { |
| 849 | LY_LIST_FOR(meta, m) { |
| 850 | m->parent = node; |
| 851 | } |
| 852 | node->meta = meta; |
| 853 | meta = NULL; |
| 854 | } else { |
| 855 | assert(!node->schema); |
| 856 | LY_LIST_FOR(attr, a) { |
| 857 | a->parent = (struct lyd_node_opaq *)node; |
| 858 | } |
| 859 | ((struct lyd_node_opaq *)node)->attr = attr; |
| 860 | attr = NULL; |
| 861 | } |
| 862 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 863 | /* insert, keep first pointer correct */ |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 864 | lyd_insert_node((struct lyd_node *)parent, first, node); |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 865 | while (!parent && (*first)->prev->next) { |
| 866 | *first = (*first)->prev; |
| 867 | } |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 868 | node = NULL; |
| 869 | |
| 870 | stop_subtree: |
| 871 | /* end the subtree */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 872 | ret = lyb_read_stop_subtree(lybctx->lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 873 | LY_CHECK_GOTO(ret, cleanup); |
| 874 | |
| 875 | cleanup: |
| 876 | free(prefix); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 877 | free(module_key); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 878 | free(name); |
| 879 | if (dynamic) { |
| 880 | free(value); |
| 881 | } |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 882 | ly_free_val_prefs(ctx, val_prefs); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 883 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 884 | lyd_free_meta_siblings(meta); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame^] | 885 | lyd_free_attr_siblings(ctx, attr); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 886 | lyd_free_tree(node); |
| 887 | return ret; |
| 888 | } |
| 889 | |
| 890 | /** |
| 891 | * @brief Parse used YANG data models. |
| 892 | * |
| 893 | * @param[in] lybctx LYB context. |
| 894 | * @return LY_ERR value. |
| 895 | */ |
| 896 | static LY_ERR |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 897 | lyb_parse_data_models(struct lylyb_ctx *lybctx, uint32_t parse_options) |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 898 | { |
| 899 | LY_ERR ret; |
| 900 | uint32_t count; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 901 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 902 | |
| 903 | /* read model count */ |
| 904 | lyb_read_number(&count, sizeof count, 2, lybctx); |
| 905 | |
| 906 | if (count) { |
| 907 | LY_ARRAY_CREATE_RET(lybctx->ctx, lybctx->models, count, LY_EMEM); |
| 908 | |
| 909 | /* read modules */ |
| 910 | for (u = 0; u < count; ++u) { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 911 | ret = lyb_parse_model(lybctx, parse_options, &lybctx->models[u]); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 912 | LY_CHECK_RET(ret); |
| 913 | LY_ARRAY_INCREMENT(lybctx->models); |
| 914 | } |
| 915 | } |
| 916 | |
| 917 | return LY_SUCCESS; |
| 918 | } |
| 919 | |
| 920 | /** |
| 921 | * @brief Parse LYB magic number. |
| 922 | * |
| 923 | * @param[in] lybctx LYB context. |
| 924 | * @return LY_ERR value. |
| 925 | */ |
| 926 | static LY_ERR |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 927 | lyb_parse_magic_number(struct lylyb_ctx *lybctx) |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 928 | { |
| 929 | char magic_byte = 0; |
| 930 | |
| 931 | lyb_read((uint8_t *)&magic_byte, 1, lybctx); |
| 932 | if (magic_byte != 'l') { |
| 933 | LOGERR(lybctx->ctx, LY_EINVAL, "Invalid first magic number byte \"0x%02x\".", magic_byte); |
| 934 | return LY_EINVAL; |
| 935 | } |
| 936 | |
| 937 | lyb_read((uint8_t *)&magic_byte, 1, lybctx); |
| 938 | if (magic_byte != 'y') { |
| 939 | LOGERR(lybctx->ctx, LY_EINVAL, "Invalid second magic number byte \"0x%02x\".", magic_byte); |
| 940 | return LY_EINVAL; |
| 941 | } |
| 942 | |
| 943 | lyb_read((uint8_t *)&magic_byte, 1, lybctx); |
| 944 | if (magic_byte != 'b') { |
| 945 | LOGERR(lybctx->ctx, LY_EINVAL, "Invalid third magic number byte \"0x%02x\".", magic_byte); |
| 946 | return LY_EINVAL; |
| 947 | } |
| 948 | |
| 949 | return LY_SUCCESS; |
| 950 | } |
| 951 | |
| 952 | /** |
| 953 | * @brief Parse LYB header. |
| 954 | * |
| 955 | * @param[in] lybctx LYB context. |
| 956 | * @return LY_ERR value. |
| 957 | */ |
| 958 | static LY_ERR |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 959 | lyb_parse_header(struct lylyb_ctx *lybctx) |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 960 | { |
| 961 | uint8_t byte = 0; |
| 962 | |
| 963 | /* version, future flags */ |
| 964 | lyb_read((uint8_t *)&byte, sizeof byte, lybctx); |
| 965 | |
| 966 | if ((byte & LYB_VERSION_MASK) != LYB_VERSION_NUM) { |
| 967 | LOGERR(lybctx->ctx, LY_EINVAL, "Invalid LYB format version \"0x%02x\", expected \"0x%02x\".", |
| 968 | byte & LYB_VERSION_MASK, LYB_VERSION_NUM); |
| 969 | return LY_EINVAL; |
| 970 | } |
| 971 | |
| 972 | return LY_SUCCESS; |
| 973 | } |
| 974 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 975 | /* |
| 976 | * @param[in] ctx libyang context for logging |
| 977 | * @param[in] parent Parent node where to connect the parsed data, required for reply where the reply data are connected with the request operation |
| 978 | * @param[in] in Input structure. |
| 979 | * @param[in] parse_options Options for parser, see @ref dataparseroptions. |
| 980 | * @param[in] validate_options Options for the validation phase, see @ref datavalidationoptions. |
| 981 | * @param[in] data_type Internal data parser flag to distnguish type of the data to parse (RPC/Reply/Notification/regular data]. |
| 982 | * @param[out] tree_p Parsed data tree. Note that NULL can be a valid result. |
| 983 | * @param[out] op_p Optional pointer to the actual operation. Useful for action and inner notifications. |
| 984 | * @param[out] lydctx_p Data parser context to finish validation. |
| 985 | */ |
| 986 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 987 | 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, |
| 988 | 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] | 989 | { |
| 990 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 991 | struct lyd_lyb_ctx *lybctx; |
| 992 | struct lyd_node *tree = NULL; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 993 | |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 994 | assert(!(parse_options & ~LYD_PARSE_OPTS_MASK)); |
| 995 | assert(!(validate_options & ~LYD_VALIDATE_OPTS_MASK)); |
| 996 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 997 | lybctx = calloc(1, sizeof *lybctx); |
| 998 | LY_CHECK_ERR_RET(!lybctx, LOGMEM(ctx), LY_EMEM); |
| 999 | lybctx->lybctx = calloc(1, sizeof *lybctx->lybctx); |
Michal Vasko | 9acaf49 | 2020-08-13 09:05:58 +0200 | [diff] [blame] | 1000 | LY_CHECK_ERR_GOTO(!lybctx->lybctx, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1001 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1002 | lybctx->lybctx->in = in; |
| 1003 | lybctx->lybctx->ctx = ctx; |
| 1004 | lybctx->parse_options = parse_options; |
| 1005 | lybctx->validate_options = validate_options; |
| 1006 | lybctx->int_opts = data_type; |
| 1007 | lybctx->free = lyd_lyb_ctx_free; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1008 | |
| 1009 | /* read magic number */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1010 | ret = lyb_parse_magic_number(lybctx->lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1011 | LY_CHECK_GOTO(ret, cleanup); |
| 1012 | |
| 1013 | /* read header */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1014 | ret = lyb_parse_header(lybctx->lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1015 | LY_CHECK_GOTO(ret, cleanup); |
| 1016 | |
| 1017 | /* read used models */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1018 | ret = lyb_parse_data_models(lybctx->lybctx, lybctx->parse_options); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1019 | LY_CHECK_GOTO(ret, cleanup); |
| 1020 | |
| 1021 | /* read subtree(s) */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1022 | while (lybctx->lybctx->in->current[0]) { |
| 1023 | ret = lyb_parse_subtree_r(lybctx, parent ? *parent : NULL, &tree); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1024 | LY_CHECK_GOTO(ret, cleanup); |
| 1025 | } |
| 1026 | |
| 1027 | /* read the last zero, parsing finished */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1028 | ly_in_skip(lybctx->lybctx->in, 1); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1029 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1030 | if (data_type == LYD_INTOPT_RPC) { |
| 1031 | /* make sure we have parsed some operation */ |
| 1032 | if (!lybctx->op_node) { |
| 1033 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_DATA, "Missing the \"rpc\"/\"action\" node."); |
| 1034 | ret = LY_EVALID; |
| 1035 | goto cleanup; |
| 1036 | } |
| 1037 | |
| 1038 | if (op_p) { |
| 1039 | *op_p = lybctx->op_node; |
| 1040 | } |
| 1041 | assert(tree); |
| 1042 | } else if (data_type == LYD_INTOPT_REPLY) { |
| 1043 | struct lyd_node_inner *iter; |
| 1044 | |
| 1045 | assert(parent); |
| 1046 | |
| 1047 | if (op_p) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1048 | *op_p = (struct lyd_node *)(*parent); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1049 | } |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 1050 | for (iter = *parent; iter->parent; iter = iter->parent) {} |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1051 | tree = (struct lyd_node *)iter; |
| 1052 | *parent = NULL; |
| 1053 | |
| 1054 | } else if (data_type == LYD_INTOPT_NOTIF) { |
| 1055 | /* make sure we have parsed some notification */ |
| 1056 | if (!lybctx->op_node) { |
| 1057 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_DATA, "Missing the \"notification\" node."); |
| 1058 | ret = LY_EVALID; |
| 1059 | goto cleanup; |
| 1060 | } |
| 1061 | |
| 1062 | if (op_p) { |
| 1063 | *op_p = lybctx->op_node; |
| 1064 | } |
| 1065 | assert(tree); |
| 1066 | } |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1067 | |
| 1068 | cleanup: |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1069 | if (ret || !lydctx_p) { |
| 1070 | lyd_lyb_ctx_free((struct lyd_ctx *)lybctx); |
| 1071 | if (ret) { |
| 1072 | lyd_free_all(tree); |
| 1073 | } |
| 1074 | } else { |
| 1075 | *lydctx_p = (struct lyd_ctx *)lybctx; |
| 1076 | if (tree_p) { |
| 1077 | *tree_p = tree; |
| 1078 | } |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1079 | } |
| 1080 | return ret; |
| 1081 | } |
| 1082 | |
| 1083 | LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1084 | 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] | 1085 | struct lyd_node **tree_p, struct lyd_ctx **lydctx_p) |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1086 | { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1087 | 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] | 1088 | } |
| 1089 | |
| 1090 | LY_ERR |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1091 | 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] | 1092 | { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1093 | 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] | 1094 | } |
| 1095 | |
| 1096 | LY_ERR |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1097 | 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] | 1098 | { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1099 | return lyd_parse_lyb_(ctx, NULL, in, LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, LYD_INTOPT_NOTIF, tree_p, ntf_p, NULL); |
| 1100 | } |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1101 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1102 | LY_ERR |
| 1103 | lyd_parse_lyb_reply(const struct lyd_node *request, struct ly_in *in, struct lyd_node **tree_p, struct lyd_node **op_p) |
| 1104 | { |
| 1105 | LY_ERR ret; |
| 1106 | struct lyd_node *req_op, *rep_op = NULL; |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1107 | const struct ly_ctx *ctx = LYD_CTX(request); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1108 | |
| 1109 | /* find request OP */ |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1110 | LYD_TREE_DFS_BEGIN((struct lyd_node *)request, req_op) { |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1111 | if (req_op->schema->nodetype & (LYS_RPC | LYS_ACTION)) { |
| 1112 | break; |
| 1113 | } |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1114 | LYD_TREE_DFS_END(request, req_op); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1115 | } |
| 1116 | if (!(req_op->schema->nodetype & (LYS_RPC | LYS_ACTION))) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1117 | 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] | 1118 | return LY_EINVAL; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1119 | } |
| 1120 | |
| 1121 | /* duplicate request OP with parents */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1122 | 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] | 1123 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1124 | 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] | 1125 | |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1126 | lyd_free_all(rep_op); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1127 | |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1128 | return ret; |
| 1129 | } |
| 1130 | |
| 1131 | API int |
| 1132 | lyd_lyb_data_length(const char *data) |
| 1133 | { |
| 1134 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1135 | struct lylyb_ctx *lybctx; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1136 | int count, i; |
| 1137 | size_t len; |
| 1138 | uint8_t buf[LYB_SIZE_MAX]; |
| 1139 | |
| 1140 | if (!data) { |
| 1141 | return -1; |
| 1142 | } |
| 1143 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1144 | lybctx = calloc(1, sizeof *lybctx); |
| 1145 | LY_CHECK_ERR_RET(!lybctx, LOGMEM(NULL), LY_EMEM); |
| 1146 | ret = ly_in_new_memory(data, &lybctx->in); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1147 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1148 | |
| 1149 | /* read magic number */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1150 | ret = lyb_parse_magic_number(lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1151 | LY_CHECK_GOTO(ret, cleanup); |
| 1152 | |
| 1153 | /* read header */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1154 | ret = lyb_parse_header(lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1155 | LY_CHECK_GOTO(ret, cleanup); |
| 1156 | |
| 1157 | /* read model count */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1158 | lyb_read_number(&count, sizeof count, 2, lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1159 | |
| 1160 | /* read all models */ |
| 1161 | for (i = 0; i < count; ++i) { |
| 1162 | /* module name length */ |
| 1163 | len = 0; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1164 | lyb_read_number(&len, sizeof len, 2, lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1165 | |
| 1166 | /* model name */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1167 | lyb_read(buf, len, lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1168 | |
| 1169 | /* revision */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1170 | lyb_read(buf, 2, lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1171 | } |
| 1172 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1173 | while (lybctx->in->current[0]) { |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1174 | /* register a new subtree */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1175 | ret = lyb_read_start_subtree(lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1176 | LY_CHECK_GOTO(ret, cleanup); |
| 1177 | |
| 1178 | /* skip it */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1179 | lyb_skip_subtree(lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1180 | |
| 1181 | /* subtree finished */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1182 | ret = lyb_read_stop_subtree(lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1183 | LY_CHECK_GOTO(ret, cleanup); |
| 1184 | } |
| 1185 | |
| 1186 | /* read the last zero, parsing finished */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1187 | ly_in_skip(lybctx->in, 1); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1188 | |
| 1189 | cleanup: |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1190 | count = lybctx->in->current - lybctx->in->start; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1191 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1192 | ly_in_free(lybctx->in, 0); |
| 1193 | lylyb_ctx_free(lybctx); |
| 1194 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1195 | return ret ? -1 : count; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1196 | } |