Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +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) 2018 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 <assert.h> |
| 16 | #include <errno.h> |
| 17 | #include <stdlib.h> |
| 18 | #include <string.h> |
Michal Vasko | e581e98 | 2018-08-21 12:26:55 +0200 | [diff] [blame] | 19 | #include <inttypes.h> |
Michal Vasko | 6b69e4b | 2018-10-17 12:07:27 +0200 | [diff] [blame] | 20 | #ifdef __APPLE__ |
Michal Vasko | b27fb15 | 2018-10-17 12:54:47 +0200 | [diff] [blame] | 21 | # include <libkern/OSByteOrder.h> |
| 22 | |
| 23 | # define le16toh(x) OSSwapLittleToHostInt16(x) |
| 24 | # define le32toh(x) OSSwapLittleToHostInt32(x) |
| 25 | # define le64toh(x) OSSwapLittleToHostInt64(x) |
Michal Vasko | 6b69e4b | 2018-10-17 12:07:27 +0200 | [diff] [blame] | 26 | #else |
Michal Vasko | b27fb15 | 2018-10-17 12:54:47 +0200 | [diff] [blame] | 27 | # include <endian.h> |
Michal Vasko | 6b69e4b | 2018-10-17 12:07:27 +0200 | [diff] [blame] | 28 | #endif |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 29 | |
| 30 | #include "libyang.h" |
| 31 | #include "common.h" |
| 32 | #include "context.h" |
| 33 | #include "parser.h" |
| 34 | #include "tree_internal.h" |
| 35 | |
| 36 | #define LYB_HAVE_READ_GOTO(r, d, go) if (r < 0) goto go; d += r; |
| 37 | #define LYB_HAVE_READ_RETURN(r, d, ret) if (r < 0) return ret; d += r; |
| 38 | |
| 39 | static int |
| 40 | lyb_read(const char *data, uint8_t *buf, size_t count, struct lyb_state *lybs) |
| 41 | { |
| 42 | int ret = 0, i, empty_chunk_i; |
| 43 | size_t to_read; |
Graeme Campbell | 856a40e | 2018-10-05 14:35:37 +1300 | [diff] [blame] | 44 | uint8_t meta_buf[LYB_META_BYTES]; |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 45 | |
| 46 | assert(data && lybs); |
| 47 | |
| 48 | while (1) { |
| 49 | /* check for fully-read (empty) data chunks */ |
| 50 | to_read = count; |
| 51 | empty_chunk_i = -1; |
| 52 | for (i = 0; i < lybs->used; ++i) { |
| 53 | /* we want the innermost chunks resolved first, so replace previous empty chunks, |
| 54 | * also ignore chunks that are completely finished, there is nothing for us to do */ |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 55 | if ((lybs->written[i] <= to_read) && lybs->position[i]) { |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 56 | /* empty chunk, do not read more */ |
| 57 | to_read = lybs->written[i]; |
| 58 | empty_chunk_i = i; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | if ((empty_chunk_i == -1) && !count) { |
| 63 | break; |
| 64 | } |
| 65 | |
| 66 | /* we are actually reading some data, not just finishing another chunk */ |
| 67 | if (to_read) { |
Michal Vasko | 228431e | 2018-07-10 15:47:11 +0200 | [diff] [blame] | 68 | if (buf) { |
| 69 | memcpy(buf, data + ret, to_read); |
| 70 | } |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 71 | |
| 72 | for (i = 0; i < lybs->used; ++i) { |
| 73 | /* decrease all written counters */ |
| 74 | lybs->written[i] -= to_read; |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 75 | assert(lybs->written[i] <= LYB_SIZE_MAX); |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 76 | } |
| 77 | /* decrease count/buf */ |
| 78 | count -= to_read; |
Michal Vasko | 228431e | 2018-07-10 15:47:11 +0200 | [diff] [blame] | 79 | if (buf) { |
| 80 | buf += to_read; |
| 81 | } |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 82 | |
| 83 | ret += to_read; |
| 84 | } |
| 85 | |
| 86 | if (empty_chunk_i > -1) { |
Michal Vasko | a985252 | 2018-08-08 16:24:45 +0200 | [diff] [blame] | 87 | /* read the next chunk meta information */ |
Graeme Campbell | 856a40e | 2018-10-05 14:35:37 +1300 | [diff] [blame] | 88 | memcpy(meta_buf, data + ret, LYB_META_BYTES); |
| 89 | lybs->written[empty_chunk_i] = meta_buf[0]; |
| 90 | lybs->inner_chunks[empty_chunk_i] = meta_buf[1]; |
Michal Vasko | a985252 | 2018-08-08 16:24:45 +0200 | [diff] [blame] | 91 | |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 92 | /* remember whether there is a following chunk or not */ |
| 93 | lybs->position[empty_chunk_i] = (lybs->written[empty_chunk_i] == LYB_SIZE_MAX ? 1 : 0); |
| 94 | |
Michal Vasko | a985252 | 2018-08-08 16:24:45 +0200 | [diff] [blame] | 95 | ret += LYB_META_BYTES; |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | |
| 99 | return ret; |
| 100 | } |
| 101 | |
| 102 | static int |
Michal Vasko | 1440eb5 | 2018-08-14 13:42:14 +0200 | [diff] [blame] | 103 | lyb_read_number(uint64_t *num, size_t bytes, const char *data, struct lyb_state *lybs) |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 104 | { |
Michal Vasko | 1440eb5 | 2018-08-14 13:42:14 +0200 | [diff] [blame] | 105 | int r, ret = 0; |
| 106 | size_t i; |
Michal Vasko | 228431e | 2018-07-10 15:47:11 +0200 | [diff] [blame] | 107 | uint8_t byte; |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 108 | |
Michal Vasko | 1440eb5 | 2018-08-14 13:42:14 +0200 | [diff] [blame] | 109 | for (i = 0; i < bytes; ++i) { |
Michal Vasko | 228431e | 2018-07-10 15:47:11 +0200 | [diff] [blame] | 110 | ret += (r = lyb_read(data, &byte, 1, lybs)); |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 111 | LYB_HAVE_READ_RETURN(r, data, -1); |
Michal Vasko | 228431e | 2018-07-10 15:47:11 +0200 | [diff] [blame] | 112 | |
| 113 | *(((uint8_t *)num) + i) = byte; |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | return ret; |
| 117 | } |
| 118 | |
| 119 | static int |
Michal Vasko | 1440eb5 | 2018-08-14 13:42:14 +0200 | [diff] [blame] | 120 | lyb_read_enum(uint64_t *enum_idx, uint32_t count, const char *data, struct lyb_state *lybs) |
| 121 | { |
Graeme Campbell | 856a40e | 2018-10-05 14:35:37 +1300 | [diff] [blame] | 122 | int ret = 0; |
Michal Vasko | 1440eb5 | 2018-08-14 13:42:14 +0200 | [diff] [blame] | 123 | size_t bytes; |
Graeme Campbell | 856a40e | 2018-10-05 14:35:37 +1300 | [diff] [blame] | 124 | uint64_t tmp_enum = 0; |
Michal Vasko | 1440eb5 | 2018-08-14 13:42:14 +0200 | [diff] [blame] | 125 | |
Michal Vasko | 8142689 | 2018-11-21 11:57:49 +0100 | [diff] [blame] | 126 | if (count < (1 << 8)) { |
Michal Vasko | 1440eb5 | 2018-08-14 13:42:14 +0200 | [diff] [blame] | 127 | bytes = 1; |
Michal Vasko | 8142689 | 2018-11-21 11:57:49 +0100 | [diff] [blame] | 128 | } else if (count < (1 << 16)) { |
Michal Vasko | 1440eb5 | 2018-08-14 13:42:14 +0200 | [diff] [blame] | 129 | bytes = 2; |
Michal Vasko | 8142689 | 2018-11-21 11:57:49 +0100 | [diff] [blame] | 130 | } else if (count < (1 << 24)) { |
Michal Vasko | 1440eb5 | 2018-08-14 13:42:14 +0200 | [diff] [blame] | 131 | bytes = 3; |
| 132 | } else { |
| 133 | bytes = 4; |
| 134 | } |
| 135 | |
Graeme Campbell | 856a40e | 2018-10-05 14:35:37 +1300 | [diff] [blame] | 136 | /* The enum is always read into a uint64_t buffer */ |
| 137 | ret = lyb_read_number(&tmp_enum, bytes, data, lybs); |
| 138 | *enum_idx = le64toh(tmp_enum); |
| 139 | |
| 140 | return ret; |
Michal Vasko | 1440eb5 | 2018-08-14 13:42:14 +0200 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | static int |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 144 | lyb_read_string(const char *data, char **str, int with_length, struct lyb_state *lybs) |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 145 | { |
Michal Vasko | 0d73b8c | 2018-08-08 16:25:43 +0200 | [diff] [blame] | 146 | int next_chunk = 0, r, ret = 0; |
| 147 | size_t len = 0, cur_len; |
Graeme Campbell | 856a40e | 2018-10-05 14:35:37 +1300 | [diff] [blame] | 148 | uint8_t len_buf[2]; |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 149 | |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 150 | if (with_length) { |
Graeme Campbell | 856a40e | 2018-10-05 14:35:37 +1300 | [diff] [blame] | 151 | ret += (r = lyb_read(data, len_buf, 2, lybs)); |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 152 | LYB_HAVE_READ_GOTO(r, data, error); |
Graeme Campbell | 856a40e | 2018-10-05 14:35:37 +1300 | [diff] [blame] | 153 | len = len_buf[0] | (len_buf[1] << 8); |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 154 | } else { |
| 155 | /* read until the end of this subtree */ |
| 156 | len = lybs->written[lybs->used - 1]; |
Michal Vasko | 0d73b8c | 2018-08-08 16:25:43 +0200 | [diff] [blame] | 157 | if (lybs->position[lybs->used - 1]) { |
| 158 | next_chunk = 1; |
| 159 | } |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 160 | } |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 161 | |
| 162 | *str = malloc((len + 1) * sizeof **str); |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 163 | LY_CHECK_ERR_RETURN(!*str, LOGMEM(NULL), -1); |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 164 | |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 165 | ret += (r = lyb_read(data, (uint8_t *)*str, len, lybs)); |
Michal Vasko | ed5fc58 | 2018-08-09 11:34:44 +0200 | [diff] [blame] | 166 | LYB_HAVE_READ_GOTO(r, data, error); |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 167 | |
Michal Vasko | 0d73b8c | 2018-08-08 16:25:43 +0200 | [diff] [blame] | 168 | while (next_chunk) { |
| 169 | cur_len = lybs->written[lybs->used - 1]; |
| 170 | if (lybs->position[lybs->used - 1]) { |
| 171 | next_chunk = 1; |
| 172 | } else { |
| 173 | next_chunk = 0; |
| 174 | } |
| 175 | |
| 176 | *str = ly_realloc(*str, (len + cur_len + 1) * sizeof **str); |
| 177 | LY_CHECK_ERR_RETURN(!*str, LOGMEM(NULL), -1); |
| 178 | |
| 179 | ret += (r = lyb_read(data, ((uint8_t *)*str) + len, cur_len, lybs)); |
Michal Vasko | ed5fc58 | 2018-08-09 11:34:44 +0200 | [diff] [blame] | 180 | LYB_HAVE_READ_GOTO(r, data, error); |
Michal Vasko | 0d73b8c | 2018-08-08 16:25:43 +0200 | [diff] [blame] | 181 | |
| 182 | len += cur_len; |
| 183 | } |
| 184 | |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 185 | ((char *)*str)[len] = '\0'; |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 186 | return ret; |
| 187 | |
| 188 | error: |
| 189 | free((char *)*str); |
| 190 | *str = NULL; |
| 191 | return -1; |
| 192 | } |
| 193 | |
| 194 | static void |
| 195 | lyb_read_stop_subtree(struct lyb_state *lybs) |
| 196 | { |
| 197 | if (lybs->written[lybs->used - 1]) { |
| 198 | LOGINT(NULL); |
| 199 | } |
| 200 | |
| 201 | --lybs->used; |
| 202 | } |
| 203 | |
| 204 | static int |
| 205 | lyb_read_start_subtree(const char *data, struct lyb_state *lybs) |
| 206 | { |
Graeme Campbell | 856a40e | 2018-10-05 14:35:37 +1300 | [diff] [blame] | 207 | uint8_t meta_buf[LYB_META_BYTES]; |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 208 | |
| 209 | if (lybs->used == lybs->size) { |
| 210 | lybs->size += LYB_STATE_STEP; |
| 211 | lybs->written = ly_realloc(lybs->written, lybs->size * sizeof *lybs->written); |
| 212 | lybs->position = ly_realloc(lybs->position, lybs->size * sizeof *lybs->position); |
Michal Vasko | a985252 | 2018-08-08 16:24:45 +0200 | [diff] [blame] | 213 | lybs->inner_chunks = ly_realloc(lybs->inner_chunks, lybs->size * sizeof *lybs->inner_chunks); |
| 214 | LY_CHECK_ERR_RETURN(!lybs->written || !lybs->position || !lybs->inner_chunks, LOGMEM(NULL), -1); |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 215 | } |
| 216 | |
Graeme Campbell | 856a40e | 2018-10-05 14:35:37 +1300 | [diff] [blame] | 217 | memcpy(meta_buf, data, LYB_META_BYTES); |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 218 | |
| 219 | ++lybs->used; |
Graeme Campbell | 856a40e | 2018-10-05 14:35:37 +1300 | [diff] [blame] | 220 | lybs->written[lybs->used - 1] = meta_buf[0]; |
| 221 | lybs->inner_chunks[lybs->used - 1] = meta_buf[LYB_SIZE_BYTES]; |
Michal Vasko | a985252 | 2018-08-08 16:24:45 +0200 | [diff] [blame] | 222 | lybs->position[lybs->used - 1] = (lybs->written[lybs->used - 1] == LYB_SIZE_MAX ? 1 : 0); |
| 223 | |
| 224 | return LYB_META_BYTES; |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | static int |
| 228 | lyb_parse_model(struct ly_ctx *ctx, const char *data, const struct lys_module **mod, struct lyb_state *lybs) |
| 229 | { |
| 230 | int r, ret = 0; |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 231 | char *mod_name = NULL, mod_rev[11]; |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 232 | uint16_t rev = 0; |
Graeme Campbell | 856a40e | 2018-10-05 14:35:37 +1300 | [diff] [blame] | 233 | uint8_t tmp_buf[2]; |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 234 | |
| 235 | /* model name */ |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 236 | ret += (r = lyb_read_string(data, &mod_name, 1, lybs)); |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 237 | LYB_HAVE_READ_GOTO(r, data, error); |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 238 | |
| 239 | /* revision */ |
Graeme Campbell | 856a40e | 2018-10-05 14:35:37 +1300 | [diff] [blame] | 240 | ret += (r = lyb_read(data, tmp_buf, sizeof tmp_buf, lybs)); |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 241 | LYB_HAVE_READ_GOTO(r, data, error); |
Graeme Campbell | 856a40e | 2018-10-05 14:35:37 +1300 | [diff] [blame] | 242 | rev = tmp_buf[0] | (tmp_buf[1] << 8); |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 243 | |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 244 | if (rev) { |
| 245 | sprintf(mod_rev, "%04u-%02u-%02u", ((rev & 0xFE00) >> 9) + 2000, (rev & 0x01E0) >> 5, (rev & 0x001F)); |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 246 | *mod = ly_ctx_get_module(ctx, mod_name, mod_rev, 0); |
| 247 | } else { |
| 248 | *mod = ly_ctx_get_module(ctx, mod_name, NULL, 0); |
| 249 | } |
Michal Vasko | 18c3b6d | 2018-07-24 16:08:57 +0200 | [diff] [blame] | 250 | if (ctx->data_clb) { |
| 251 | if (!*mod) { |
| 252 | *mod = ctx->data_clb(ctx, mod_name, NULL, 0, ctx->data_clb_data); |
| 253 | } else if (!(*mod)->implemented) { |
| 254 | *mod = ctx->data_clb(ctx, mod_name, (*mod)->ns, LY_MODCLB_NOT_IMPLEMENTED, ctx->data_clb_data); |
| 255 | } |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 256 | } |
| 257 | |
Michal Vasko | 60b02ff | 2018-09-25 11:01:18 +0200 | [diff] [blame] | 258 | if (!*mod) { |
| 259 | LOGERR(ctx, LY_EINVAL, "Invalid context for LYB data parsing, missing module \"%s%s%s\".", |
| 260 | mod_name, rev ? "@" : "", rev ? mod_rev : ""); |
| 261 | goto error; |
| 262 | } else if (!(*mod)->implemented) { |
| 263 | LOGERR(ctx, LY_EINVAL, "Invalid context for LYB data parsing, module \"%s%s%s\" not implemented.", |
| 264 | mod_name, rev ? "@" : "", rev ? mod_rev : ""); |
| 265 | goto error; |
| 266 | } |
| 267 | |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 268 | free(mod_name); |
| 269 | return ret; |
| 270 | |
| 271 | error: |
| 272 | free(mod_name); |
| 273 | return -1; |
| 274 | } |
| 275 | |
| 276 | static struct lyd_node * |
| 277 | lyb_new_node(const struct lys_node *schema) |
| 278 | { |
| 279 | struct lyd_node *node; |
| 280 | |
| 281 | switch (schema->nodetype) { |
| 282 | case LYS_CONTAINER: |
| 283 | case LYS_LIST: |
| 284 | case LYS_NOTIF: |
| 285 | case LYS_RPC: |
| 286 | case LYS_ACTION: |
| 287 | node = calloc(sizeof(struct lyd_node), 1); |
| 288 | break; |
| 289 | case LYS_LEAF: |
| 290 | case LYS_LEAFLIST: |
| 291 | node = calloc(sizeof(struct lyd_node_leaf_list), 1); |
Michal Vasko | b740023 | 2018-07-11 15:35:57 +0200 | [diff] [blame] | 292 | |
| 293 | if (((struct lys_node_leaf *)schema)->type.base == LY_TYPE_LEAFREF) { |
| 294 | node->validity |= LYD_VAL_LEAFREF; |
| 295 | } |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 296 | break; |
| 297 | case LYS_ANYDATA: |
| 298 | case LYS_ANYXML: |
| 299 | node = calloc(sizeof(struct lyd_node_anydata), 1); |
| 300 | break; |
| 301 | default: |
| 302 | return NULL; |
| 303 | } |
| 304 | LY_CHECK_ERR_RETURN(!node, LOGMEM(schema->module->ctx), NULL); |
| 305 | |
| 306 | /* fill basic info */ |
| 307 | node->schema = (struct lys_node *)schema; |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 308 | if (resolve_applies_when(schema, 0, NULL)) { |
Michal Vasko | 7b2a582 | 2018-12-18 13:13:03 +0100 | [diff] [blame] | 309 | /* this data are considered trusted so if this node exists, it means its when must have been true */ |
| 310 | node->when_status = LYD_WHEN | LYD_WHEN_TRUE; |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 311 | } |
| 312 | node->prev = node; |
| 313 | |
| 314 | return node; |
| 315 | } |
| 316 | |
| 317 | static int |
| 318 | lyb_parse_anydata(struct lyd_node *node, const char *data, struct lyb_state *lybs) |
| 319 | { |
| 320 | int r, ret = 0; |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 321 | char *str = NULL; |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 322 | struct lyd_node_anydata *any = (struct lyd_node_anydata *)node; |
| 323 | |
| 324 | /* read value type */ |
| 325 | ret += (r = lyb_read(data, (uint8_t *)&any->value_type, sizeof any->value_type, lybs)); |
| 326 | LYB_HAVE_READ_RETURN(r, data, -1); |
| 327 | |
| 328 | /* read anydata content */ |
| 329 | if (any->value_type == LYD_ANYDATA_DATATREE) { |
Michal Vasko | 3390170 | 2018-08-17 10:29:09 +0200 | [diff] [blame] | 330 | /* invalid situation */ |
| 331 | LOGINT(node->schema->module->ctx); |
| 332 | return -1; |
Michal Vasko | dcaf722 | 2018-08-08 16:27:00 +0200 | [diff] [blame] | 333 | } else if (any->value_type == LYD_ANYDATA_LYB) { |
| 334 | ret += (r = lyb_read_string(data, &any->value.mem, 0, lybs)); |
| 335 | LYB_HAVE_READ_RETURN(r, data, -1); |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 336 | } else { |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 337 | ret += (r = lyb_read_string(data, &str, 0, lybs)); |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 338 | LYB_HAVE_READ_RETURN(r, data, -1); |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 339 | |
| 340 | /* add to dictionary */ |
| 341 | any->value.str = lydict_insert_zc(node->schema->module->ctx, str); |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | return ret; |
| 345 | } |
| 346 | |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 347 | /* generally, fill lyd_val value union */ |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 348 | static int |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 349 | lyb_parse_val_1(struct ly_ctx *ctx, struct lys_type *type, LY_DATA_TYPE value_type, uint8_t value_flags, const char *data, |
| 350 | const char **value_str, lyd_val *value, struct lyb_state *lybs) |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 351 | { |
Michal Vasko | 228431e | 2018-07-10 15:47:11 +0200 | [diff] [blame] | 352 | int r, ret; |
| 353 | size_t i; |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 354 | char *str = NULL; |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 355 | uint8_t byte; |
Michal Vasko | 228431e | 2018-07-10 15:47:11 +0200 | [diff] [blame] | 356 | uint64_t num; |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 357 | |
Michal Vasko | 42132c5 | 2018-07-24 16:09:21 +0200 | [diff] [blame] | 358 | if (value_flags & LY_VALUE_USER) { |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 359 | /* just read value_str */ |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 360 | ret = lyb_read_string(data, &str, 0, lybs); |
| 361 | if (ret > -1) { |
| 362 | *value_str = lydict_insert_zc(ctx, str); |
| 363 | } |
| 364 | return ret; |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 365 | } |
| 366 | |
Michal Vasko | 6fb5789 | 2018-08-23 10:21:21 +0200 | [diff] [blame] | 367 | /* find the correct structure, go through leafrefs and typedefs */ |
| 368 | switch (value_type) { |
| 369 | case LY_TYPE_ENUM: |
| 370 | for (; type->base == LY_TYPE_LEAFREF; type = &type->info.lref.target->type); |
| 371 | for (; !type->info.enums.count; type = &type->der->type); |
| 372 | break; |
| 373 | case LY_TYPE_BITS: |
| 374 | for (; type->base == LY_TYPE_LEAFREF; type = &type->info.lref.target->type); |
| 375 | for (; !type->info.bits.count; type = &type->der->type); |
| 376 | break; |
| 377 | default: |
| 378 | break; |
| 379 | } |
| 380 | |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 381 | switch (value_type) { |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 382 | case LY_TYPE_INST: |
| 383 | case LY_TYPE_IDENT: |
| 384 | case LY_TYPE_UNION: |
| 385 | /* we do not actually fill value now, but value_str */ |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 386 | ret = lyb_read_string(data, &str, 0, lybs); |
| 387 | if (ret > -1) { |
| 388 | *value_str = lydict_insert_zc(ctx, str); |
| 389 | } |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 390 | break; |
| 391 | case LY_TYPE_BINARY: |
| 392 | case LY_TYPE_STRING: |
| 393 | case LY_TYPE_UNKNOWN: |
| 394 | /* read string */ |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 395 | ret = lyb_read_string(data, &str, 0, lybs); |
| 396 | if (ret > -1) { |
| 397 | value->string = lydict_insert_zc(ctx, str); |
| 398 | } |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 399 | break; |
| 400 | case LY_TYPE_BITS: |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 401 | value->bit = calloc(type->info.bits.count, sizeof *value->bit); |
| 402 | LY_CHECK_ERR_RETURN(!value->bit, LOGMEM(ctx), -1); |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 403 | |
| 404 | /* read values */ |
Michal Vasko | 228431e | 2018-07-10 15:47:11 +0200 | [diff] [blame] | 405 | ret = 0; |
| 406 | for (i = 0; i < type->info.bits.count; ++i) { |
| 407 | if (i % 8 == 0) { |
| 408 | /* read another byte */ |
Andrew Langefeld | 1f3e4f5 | 2018-08-24 22:30:37 -0500 | [diff] [blame] | 409 | ret += (r = lyb_read(data + ret, &byte, sizeof byte, lybs)); |
Michal Vasko | 228431e | 2018-07-10 15:47:11 +0200 | [diff] [blame] | 410 | if (r < 0) { |
| 411 | return -1; |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | if (byte & (0x01 << (i % 8))) { |
| 416 | /* bit is set */ |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 417 | value->bit[i] = &type->info.bits.bit[i]; |
Michal Vasko | 228431e | 2018-07-10 15:47:11 +0200 | [diff] [blame] | 418 | } |
| 419 | } |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 420 | break; |
| 421 | case LY_TYPE_BOOL: |
| 422 | /* read byte */ |
| 423 | ret = lyb_read(data, &byte, sizeof byte, lybs); |
| 424 | if ((ret > 0) && byte) { |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 425 | value->bln = 1; |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 426 | } |
| 427 | break; |
| 428 | case LY_TYPE_EMPTY: |
| 429 | /* nothing to read */ |
| 430 | ret = 0; |
| 431 | break; |
| 432 | case LY_TYPE_ENUM: |
Michal Vasko | 228431e | 2018-07-10 15:47:11 +0200 | [diff] [blame] | 433 | num = 0; |
Michal Vasko | 1440eb5 | 2018-08-14 13:42:14 +0200 | [diff] [blame] | 434 | ret = lyb_read_enum(&num, type->info.enums.count, data, lybs); |
Michal Vasko | b740023 | 2018-07-11 15:35:57 +0200 | [diff] [blame] | 435 | if (ret > 0) { |
| 436 | assert(num < type->info.enums.count); |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 437 | value->enm = &type->info.enums.enm[num]; |
Michal Vasko | 228431e | 2018-07-10 15:47:11 +0200 | [diff] [blame] | 438 | } |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 439 | break; |
| 440 | case LY_TYPE_INT8: |
| 441 | case LY_TYPE_UINT8: |
Michal Vasko | 1440eb5 | 2018-08-14 13:42:14 +0200 | [diff] [blame] | 442 | ret = lyb_read_number((uint64_t *)&value->uint8, 1, data, lybs); |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 443 | break; |
| 444 | case LY_TYPE_INT16: |
| 445 | case LY_TYPE_UINT16: |
Michal Vasko | 1440eb5 | 2018-08-14 13:42:14 +0200 | [diff] [blame] | 446 | ret = lyb_read_number((uint64_t *)&value->uint16, 2, data, lybs); |
Graeme Campbell | 856a40e | 2018-10-05 14:35:37 +1300 | [diff] [blame] | 447 | value->uint16 = le16toh(value->uint16); |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 448 | break; |
| 449 | case LY_TYPE_INT32: |
| 450 | case LY_TYPE_UINT32: |
Michal Vasko | 1440eb5 | 2018-08-14 13:42:14 +0200 | [diff] [blame] | 451 | ret = lyb_read_number((uint64_t *)&value->uint32, 4, data, lybs); |
Graeme Campbell | 856a40e | 2018-10-05 14:35:37 +1300 | [diff] [blame] | 452 | value->uint32 = le32toh(value->uint32); |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 453 | break; |
| 454 | case LY_TYPE_DEC64: |
| 455 | case LY_TYPE_INT64: |
| 456 | case LY_TYPE_UINT64: |
Michal Vasko | 1440eb5 | 2018-08-14 13:42:14 +0200 | [diff] [blame] | 457 | ret = lyb_read_number((uint64_t *)&value->uint64, 8, data, lybs); |
Graeme Campbell | 856a40e | 2018-10-05 14:35:37 +1300 | [diff] [blame] | 458 | value->uint64 = le64toh(value->uint64); |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 459 | break; |
| 460 | default: |
| 461 | return -1; |
| 462 | } |
| 463 | |
| 464 | return ret; |
| 465 | } |
| 466 | |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 467 | /* generally, fill value_str */ |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 468 | static int |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 469 | lyb_parse_val_2(struct lys_type *type, struct lyd_node_leaf_list *leaf, struct lyd_attr *attr, struct unres_data *unres) |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 470 | { |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 471 | struct ly_ctx *ctx; |
| 472 | struct lys_module *mod; |
Michal Vasko | 6fb5789 | 2018-08-23 10:21:21 +0200 | [diff] [blame] | 473 | struct lys_type *rtype; |
Michal Vasko | 228431e | 2018-07-10 15:47:11 +0200 | [diff] [blame] | 474 | char num_str[22], *str; |
Michal Vasko | 27e5e68 | 2018-07-23 14:22:57 +0200 | [diff] [blame] | 475 | int64_t frac; |
Michal Vasko | 228431e | 2018-07-10 15:47:11 +0200 | [diff] [blame] | 476 | uint32_t i, str_len; |
Michal Vasko | b0c3d9e | 2018-07-23 14:35:17 +0200 | [diff] [blame] | 477 | uint8_t *value_flags, dig; |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 478 | const char **value_str; |
| 479 | LY_DATA_TYPE value_type; |
| 480 | lyd_val *value; |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 481 | |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 482 | if (leaf) { |
| 483 | ctx = leaf->schema->module->ctx; |
| 484 | mod = lys_node_module(leaf->schema); |
| 485 | |
| 486 | value = &leaf->value; |
| 487 | value_str = &leaf->value_str; |
| 488 | value_flags = &leaf->value_flags; |
| 489 | value_type = leaf->value_type; |
| 490 | } else { |
| 491 | ctx = attr->annotation->module->ctx; |
| 492 | mod = lys_main_module(attr->annotation->module); |
| 493 | |
| 494 | value = &attr->value; |
| 495 | value_str = &attr->value_str; |
| 496 | value_flags = &attr->value_flags; |
| 497 | value_type = attr->value_type; |
| 498 | } |
| 499 | |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 500 | if (*value_flags & LY_VALUE_USER) { |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 501 | /* unfortunately, we need to also fill the value properly, so just parse it again */ |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 502 | *value_flags &= ~LY_VALUE_USER; |
| 503 | if (!lyp_parse_value(type, value_str, NULL, leaf, attr, NULL, 1, (leaf ? leaf->dflt : 0), 1)) { |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 504 | return -1; |
| 505 | } |
| 506 | |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 507 | if (!(*value_flags & LY_VALUE_USER)) { |
| 508 | LOGWRN(ctx, "Value \"%s\" was stored as a user type, but it is not in the current context.", value_str); |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 509 | } |
| 510 | return 0; |
| 511 | } |
| 512 | |
Michal Vasko | b740023 | 2018-07-11 15:35:57 +0200 | [diff] [blame] | 513 | /* we are parsing leafref/ptr union stored as the target type, |
| 514 | * so we first parse it into string and then resolve the leafref/ptr union */ |
Michal Vasko | 42132c5 | 2018-07-24 16:09:21 +0200 | [diff] [blame] | 515 | if (!(*value_flags & LY_VALUE_UNRES) && ((type->base == LY_TYPE_LEAFREF) |
| 516 | || (type->base == LY_TYPE_INST) || ((type->base == LY_TYPE_UNION) && type->info.uni.has_ptr_type))) { |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 517 | if ((value_type == LY_TYPE_INST) || (value_type == LY_TYPE_IDENT) || (value_type == LY_TYPE_UNION)) { |
Michal Vasko | b740023 | 2018-07-11 15:35:57 +0200 | [diff] [blame] | 518 | /* we already have a string */ |
| 519 | goto parse_reference; |
| 520 | } |
| 521 | } |
| 522 | |
Michal Vasko | 6fb5789 | 2018-08-23 10:21:21 +0200 | [diff] [blame] | 523 | /* find the correct structure, go through leafrefs and typedefs */ |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 524 | switch (value_type) { |
Michal Vasko | 5860852 | 2018-08-22 13:04:37 +0200 | [diff] [blame] | 525 | case LY_TYPE_BITS: |
Michal Vasko | 6fb5789 | 2018-08-23 10:21:21 +0200 | [diff] [blame] | 526 | for (rtype = type; rtype->base == LY_TYPE_LEAFREF; rtype = &rtype->info.lref.target->type); |
| 527 | for (; !rtype->info.bits.count; rtype = &rtype->der->type); |
| 528 | break; |
Michal Vasko | 5860852 | 2018-08-22 13:04:37 +0200 | [diff] [blame] | 529 | case LY_TYPE_DEC64: |
Michal Vasko | 6fb5789 | 2018-08-23 10:21:21 +0200 | [diff] [blame] | 530 | for (rtype = type; rtype->base == LY_TYPE_LEAFREF; rtype = &type->info.lref.target->type); |
Michal Vasko | 5860852 | 2018-08-22 13:04:37 +0200 | [diff] [blame] | 531 | break; |
| 532 | default: |
| 533 | break; |
| 534 | } |
| 535 | |
| 536 | switch (value_type) { |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 537 | case LY_TYPE_IDENT: |
| 538 | /* fill the identity pointer now */ |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 539 | value->ident = resolve_identref(type, *value_str, (struct lyd_node *)leaf, mod, (leaf ? leaf->dflt : 0)); |
| 540 | if (!value->ident) { |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 541 | return -1; |
| 542 | } |
| 543 | break; |
Michal Vasko | 5667036 | 2018-08-08 16:27:21 +0200 | [diff] [blame] | 544 | case LY_TYPE_INST: |
| 545 | /* unresolved instance-identifier, keep value NULL */ |
| 546 | value->instance = NULL; |
| 547 | break; |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 548 | case LY_TYPE_BINARY: |
| 549 | case LY_TYPE_STRING: |
| 550 | case LY_TYPE_UNKNOWN: |
| 551 | /* just re-assign it */ |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 552 | *value_str = value->string; |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 553 | break; |
| 554 | case LY_TYPE_BITS: |
Michal Vasko | 228431e | 2018-07-10 15:47:11 +0200 | [diff] [blame] | 555 | /* print the set bits */ |
| 556 | str = malloc(1); |
| 557 | LY_CHECK_ERR_RETURN(!str, LOGMEM(ctx), -1); |
| 558 | str[0] = '\0'; |
Michal Vasko | b740023 | 2018-07-11 15:35:57 +0200 | [diff] [blame] | 559 | str_len = 0; |
Michal Vasko | 6fb5789 | 2018-08-23 10:21:21 +0200 | [diff] [blame] | 560 | for (i = 0; i < rtype->info.bits.count; ++i) { |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 561 | if (value->bit[i]) { |
| 562 | str = ly_realloc(str, str_len + strlen(value->bit[i]->name) + (str_len ? 1 : 0) + 1); |
Michal Vasko | 228431e | 2018-07-10 15:47:11 +0200 | [diff] [blame] | 563 | LY_CHECK_ERR_RETURN(!str, LOGMEM(ctx), -1); |
| 564 | |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 565 | str_len += sprintf(str + str_len, "%s%s", str_len ? " " : "", value->bit[i]->name); |
Michal Vasko | 228431e | 2018-07-10 15:47:11 +0200 | [diff] [blame] | 566 | } |
| 567 | } |
| 568 | |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 569 | *value_str = lydict_insert_zc(ctx, str); |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 570 | break; |
| 571 | case LY_TYPE_BOOL: |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 572 | *value_str = lydict_insert(ctx, (value->bln ? "true" : "false"), 0); |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 573 | break; |
| 574 | case LY_TYPE_EMPTY: |
Michal Vasko | 27e5e68 | 2018-07-23 14:22:57 +0200 | [diff] [blame] | 575 | *value_str = lydict_insert(ctx, "", 0); |
| 576 | break; |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 577 | case LY_TYPE_UNION: |
Michal Vasko | 27e5e68 | 2018-07-23 14:22:57 +0200 | [diff] [blame] | 578 | if (attr) { |
| 579 | /* we do not support union type attribute */ |
| 580 | LOGINT(ctx); |
| 581 | return -1; |
| 582 | } |
| 583 | |
| 584 | if (resolve_union(leaf, type, 1, 2, NULL)) { |
| 585 | return -1; |
| 586 | } |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 587 | break; |
| 588 | case LY_TYPE_ENUM: |
Michal Vasko | 228431e | 2018-07-10 15:47:11 +0200 | [diff] [blame] | 589 | /* print the value */ |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 590 | *value_str = lydict_insert(ctx, value->enm->name, 0); |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 591 | break; |
| 592 | case LY_TYPE_INT8: |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 593 | sprintf(num_str, "%d", value->int8); |
| 594 | *value_str = lydict_insert(ctx, num_str, 0); |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 595 | break; |
| 596 | case LY_TYPE_UINT8: |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 597 | sprintf(num_str, "%u", value->uint8); |
| 598 | *value_str = lydict_insert(ctx, num_str, 0); |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 599 | break; |
| 600 | case LY_TYPE_INT16: |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 601 | sprintf(num_str, "%d", value->int16); |
| 602 | *value_str = lydict_insert(ctx, num_str, 0); |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 603 | break; |
| 604 | case LY_TYPE_UINT16: |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 605 | sprintf(num_str, "%u", value->uint16); |
| 606 | *value_str = lydict_insert(ctx, num_str, 0); |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 607 | break; |
| 608 | case LY_TYPE_INT32: |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 609 | sprintf(num_str, "%d", value->int32); |
| 610 | *value_str = lydict_insert(ctx, num_str, 0); |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 611 | break; |
| 612 | case LY_TYPE_UINT32: |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 613 | sprintf(num_str, "%u", value->uint32); |
| 614 | *value_str = lydict_insert(ctx, num_str, 0); |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 615 | break; |
| 616 | case LY_TYPE_INT64: |
Michal Vasko | e581e98 | 2018-08-21 12:26:55 +0200 | [diff] [blame] | 617 | sprintf(num_str, "%"PRId64, value->int64); |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 618 | *value_str = lydict_insert(ctx, num_str, 0); |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 619 | break; |
| 620 | case LY_TYPE_UINT64: |
Michal Vasko | e581e98 | 2018-08-21 12:26:55 +0200 | [diff] [blame] | 621 | sprintf(num_str, "%"PRIu64, value->uint64); |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 622 | *value_str = lydict_insert(ctx, num_str, 0); |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 623 | break; |
| 624 | case LY_TYPE_DEC64: |
Michal Vasko | 6fb5789 | 2018-08-23 10:21:21 +0200 | [diff] [blame] | 625 | frac = value->dec64 % rtype->info.dec64.div; |
| 626 | dig = rtype->info.dec64.dig; |
Michal Vasko | b0c3d9e | 2018-07-23 14:35:17 +0200 | [diff] [blame] | 627 | /* remove trailing zeros */ |
| 628 | while ((dig > 1) && !(frac % 10)) { |
| 629 | frac /= 10; |
| 630 | --dig; |
| 631 | } |
| 632 | |
Michal Vasko | 6fb5789 | 2018-08-23 10:21:21 +0200 | [diff] [blame] | 633 | sprintf(num_str, "%"PRId64".%.*"PRId64, value->dec64 / (int64_t)rtype->info.dec64.div, dig, frac); |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 634 | *value_str = lydict_insert(ctx, num_str, 0); |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 635 | break; |
| 636 | default: |
| 637 | return -1; |
| 638 | } |
| 639 | |
Michal Vasko | c2a2524 | 2018-12-19 08:52:28 +0100 | [diff] [blame^] | 640 | if (*value_flags & LY_VALUE_UNRES) { |
| 641 | /* just remove the flag, it should stay unresolved */ |
| 642 | leaf->validity &= ~LYD_VAL_LEAFREF; |
| 643 | } else if ((type->base == LY_TYPE_LEAFREF) || (type->base == LY_TYPE_INST) |
| 644 | || ((type->base == LY_TYPE_UNION) && type->info.uni.has_ptr_type)) { |
Michal Vasko | b740023 | 2018-07-11 15:35:57 +0200 | [diff] [blame] | 645 | parse_reference: |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 646 | assert(*value_str); |
Michal Vasko | b740023 | 2018-07-11 15:35:57 +0200 | [diff] [blame] | 647 | |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 648 | if (attr) { |
| 649 | /* we do not support reference types of attributes */ |
| 650 | LOGINT(ctx); |
| 651 | return -1; |
| 652 | } |
| 653 | |
| 654 | if (type->base == LY_TYPE_INST) { |
| 655 | if (unres_data_add(unres, (struct lyd_node *)leaf, UNRES_INSTID)) { |
| 656 | return -1; |
| 657 | } |
| 658 | } else if (type->base == LY_TYPE_LEAFREF) { |
| 659 | if (unres_data_add(unres, (struct lyd_node *)leaf, UNRES_LEAFREF)) { |
Michal Vasko | b740023 | 2018-07-11 15:35:57 +0200 | [diff] [blame] | 660 | return -1; |
| 661 | } |
| 662 | } else { |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 663 | if (unres_data_add(unres, (struct lyd_node *)leaf, UNRES_UNION)) { |
Michal Vasko | b740023 | 2018-07-11 15:35:57 +0200 | [diff] [blame] | 664 | return -1; |
| 665 | } |
| 666 | } |
| 667 | } |
| 668 | |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 669 | return 0; |
| 670 | } |
| 671 | |
| 672 | static int |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 673 | lyb_parse_value(struct lys_type *type, struct lyd_node_leaf_list *leaf, struct lyd_attr *attr, const char *data, |
| 674 | struct unres_data *unres, struct lyb_state *lybs) |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 675 | { |
| 676 | int r, ret = 0; |
| 677 | uint8_t start_byte; |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 678 | |
| 679 | struct ly_ctx *ctx; |
| 680 | const char **value_str; |
| 681 | lyd_val *value; |
| 682 | LY_DATA_TYPE *value_type; |
| 683 | uint8_t *value_flags; |
| 684 | |
| 685 | assert((leaf || attr) && (!leaf || !attr)); |
| 686 | |
| 687 | if (leaf) { |
| 688 | ctx = leaf->schema->module->ctx; |
| 689 | value_str = &leaf->value_str; |
| 690 | value = &leaf->value; |
| 691 | value_type = &leaf->value_type; |
| 692 | value_flags = &leaf->value_flags; |
| 693 | } else { |
| 694 | ctx = attr->annotation->module->ctx; |
| 695 | value_str = &attr->value_str; |
| 696 | value = &attr->value; |
| 697 | value_type = &attr->value_type; |
| 698 | value_flags = &attr->value_flags; |
| 699 | } |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 700 | |
| 701 | /* read value type and flags on the first byte */ |
| 702 | ret += (r = lyb_read(data, &start_byte, sizeof start_byte, lybs)); |
| 703 | LYB_HAVE_READ_RETURN(r, data, -1); |
| 704 | |
| 705 | /* fill value type, flags */ |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 706 | *value_type = start_byte & 0x1F; |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 707 | if (start_byte & 0x80) { |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 708 | assert(leaf); |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 709 | leaf->dflt = 1; |
| 710 | } |
| 711 | if (start_byte & 0x40) { |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 712 | *value_flags |= LY_VALUE_USER; |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 713 | } |
| 714 | if (start_byte & 0x20) { |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 715 | *value_flags |= LY_VALUE_UNRES; |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 716 | } |
| 717 | |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 718 | ret += (r = lyb_parse_val_1(ctx, type, *value_type, *value_flags, data, value_str, value, lybs)); |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 719 | LYB_HAVE_READ_RETURN(r, data, -1); |
| 720 | |
Michal Vasko | 27e5e68 | 2018-07-23 14:22:57 +0200 | [diff] [blame] | 721 | /* union is handled specially */ |
| 722 | if (type->base == LY_TYPE_UNION) { |
| 723 | assert(*value_type == LY_TYPE_STRING); |
| 724 | |
| 725 | *value_str = value->string; |
| 726 | value->string = NULL; |
| 727 | *value_type = LY_TYPE_UNION; |
| 728 | } |
| 729 | |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 730 | ret += (r = lyb_parse_val_2(type, leaf, attr, unres)); |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 731 | LYB_HAVE_READ_RETURN(r, data, -1); |
| 732 | |
| 733 | return ret; |
| 734 | } |
| 735 | |
| 736 | static int |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 737 | lyb_parse_attr_name(const struct lys_module *mod, const char *data, struct lys_ext_instance_complex **ext, int options, |
| 738 | struct lyb_state *lybs) |
| 739 | { |
| 740 | int r, ret = 0, pos, i, j, k; |
| 741 | const struct lys_submodule *submod = NULL; |
| 742 | char *attr_name = NULL; |
| 743 | |
| 744 | /* attr name */ |
| 745 | ret += (r = lyb_read_string(data, &attr_name, 1, lybs)); |
| 746 | LYB_HAVE_READ_RETURN(r, data, -1); |
| 747 | |
| 748 | /* search module */ |
| 749 | pos = -1; |
| 750 | for (i = 0, j = 0; i < mod->ext_size; i = i + j + 1) { |
| 751 | j = lys_ext_instance_presence(&mod->ctx->models.list[0]->extensions[0], &mod->ext[i], mod->ext_size - i); |
| 752 | if (j == -1) { |
| 753 | break; |
| 754 | } |
| 755 | if (ly_strequal(mod->ext[i + j]->arg_value, attr_name, 0)) { |
| 756 | pos = i + j; |
| 757 | break; |
| 758 | } |
| 759 | } |
| 760 | |
| 761 | /* try submodules */ |
| 762 | if (pos == -1) { |
| 763 | for (k = 0; k < mod->inc_size; ++k) { |
| 764 | submod = mod->inc[k].submodule; |
| 765 | for (i = 0, j = 0; i < submod->ext_size; i = i + j + 1) { |
| 766 | j = lys_ext_instance_presence(&mod->ctx->models.list[0]->extensions[0], &submod->ext[i], submod->ext_size - i); |
| 767 | if (j == -1) { |
| 768 | break; |
| 769 | } |
| 770 | if (ly_strequal(submod->ext[i + j]->arg_value, attr_name, 0)) { |
| 771 | pos = i + j; |
| 772 | break; |
| 773 | } |
| 774 | } |
| 775 | } |
| 776 | } |
| 777 | |
| 778 | if (pos == -1) { |
| 779 | *ext = NULL; |
| 780 | } else { |
| 781 | *ext = submod ? (struct lys_ext_instance_complex *)submod->ext[pos] : (struct lys_ext_instance_complex *)mod->ext[pos]; |
| 782 | } |
| 783 | |
| 784 | if (!*ext && (options & LYD_OPT_STRICT)) { |
| 785 | LOGVAL(mod->ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Failed to find annotation \"%s\" in \"%s\".", attr_name, mod->name); |
| 786 | free(attr_name); |
| 787 | return -1; |
| 788 | } |
| 789 | |
| 790 | free(attr_name); |
| 791 | return ret; |
| 792 | } |
| 793 | |
| 794 | static int |
| 795 | lyb_parse_attributes(struct lyd_node *node, const char *data, int options, struct unres_data *unres, struct lyb_state *lybs) |
| 796 | { |
| 797 | int r, ret = 0; |
| 798 | uint8_t i, count = 0; |
| 799 | const struct lys_module *mod; |
Michal Vasko | 3cb7d17 | 2018-08-17 10:54:40 +0200 | [diff] [blame] | 800 | struct lys_type **type; |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 801 | struct lyd_attr *attr = NULL; |
| 802 | struct lys_ext_instance_complex *ext; |
| 803 | struct ly_ctx *ctx = node->schema->module->ctx; |
| 804 | |
| 805 | /* read number of attributes stored */ |
| 806 | ret += (r = lyb_read(data, &count, 1, lybs)); |
| 807 | LYB_HAVE_READ_GOTO(r, data, error); |
| 808 | |
| 809 | /* read attributes */ |
| 810 | for (i = 0; i < count; ++i) { |
| 811 | ret += (r = lyb_read_start_subtree(data, lybs)); |
| 812 | LYB_HAVE_READ_GOTO(r, data, error); |
| 813 | |
| 814 | /* find model */ |
| 815 | ret += (r = lyb_parse_model(ctx, data, &mod, lybs)); |
| 816 | LYB_HAVE_READ_GOTO(r, data, error); |
| 817 | |
Michal Vasko | 5383066 | 2018-07-24 16:09:49 +0200 | [diff] [blame] | 818 | if (mod) { |
| 819 | /* annotation name */ |
| 820 | ret += (r = lyb_parse_attr_name(mod, data, &ext, options, lybs)); |
| 821 | LYB_HAVE_READ_GOTO(r, data, error); |
| 822 | } |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 823 | |
Michal Vasko | 5383066 | 2018-07-24 16:09:49 +0200 | [diff] [blame] | 824 | if (!mod || !ext) { |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 825 | /* unknown attribute, skip it */ |
| 826 | do { |
| 827 | ret += (r = lyb_read(data, NULL, lybs->written[lybs->used - 1], lybs)); |
Michal Vasko | 27e5e68 | 2018-07-23 14:22:57 +0200 | [diff] [blame] | 828 | LYB_HAVE_READ_GOTO(r, data, error); |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 829 | } while (lybs->written[lybs->used - 1]); |
| 830 | goto stop_subtree; |
| 831 | } |
| 832 | |
| 833 | /* allocate new attribute */ |
| 834 | if (!attr) { |
| 835 | assert(!node->attr); |
| 836 | |
| 837 | attr = calloc(1, sizeof *attr); |
| 838 | LY_CHECK_ERR_GOTO(!attr, LOGMEM(ctx), error); |
| 839 | |
| 840 | node->attr = attr; |
| 841 | } else { |
| 842 | attr->next = calloc(1, sizeof *attr); |
| 843 | LY_CHECK_ERR_GOTO(!attr->next, LOGMEM(ctx), error); |
| 844 | |
| 845 | attr = attr->next; |
| 846 | } |
| 847 | |
| 848 | /* attribute annotation */ |
| 849 | attr->annotation = ext; |
| 850 | |
| 851 | /* attribute name */ |
| 852 | attr->name = lydict_insert(ctx, attr->annotation->arg_value, 0); |
| 853 | |
| 854 | /* get the type */ |
Michal Vasko | 3cb7d17 | 2018-08-17 10:54:40 +0200 | [diff] [blame] | 855 | type = (struct lys_type **)lys_ext_complex_get_substmt(LY_STMT_TYPE, attr->annotation, NULL); |
| 856 | if (!type || !(*type)) { |
| 857 | goto error; |
| 858 | } |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 859 | |
| 860 | /* attribute value */ |
Michal Vasko | 3cb7d17 | 2018-08-17 10:54:40 +0200 | [diff] [blame] | 861 | ret += (r = lyb_parse_value(*type, NULL, attr, data, unres, lybs)); |
Michal Vasko | 27e5e68 | 2018-07-23 14:22:57 +0200 | [diff] [blame] | 862 | LYB_HAVE_READ_GOTO(r, data, error); |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 863 | |
| 864 | stop_subtree: |
| 865 | lyb_read_stop_subtree(lybs); |
| 866 | } |
| 867 | |
| 868 | return ret; |
| 869 | |
| 870 | error: |
| 871 | lyd_free_attr(ctx, node, node->attr, 1); |
| 872 | return -1; |
| 873 | } |
| 874 | |
| 875 | static int |
Michal Vasko | b740023 | 2018-07-11 15:35:57 +0200 | [diff] [blame] | 876 | lyb_is_schema_hash_match(struct lys_node *sibling, LYB_HASH *hash, uint8_t hash_count) |
Michal Vasko | 228431e | 2018-07-10 15:47:11 +0200 | [diff] [blame] | 877 | { |
| 878 | LYB_HASH sibling_hash; |
Michal Vasko | b740023 | 2018-07-11 15:35:57 +0200 | [diff] [blame] | 879 | uint8_t i; |
Michal Vasko | 228431e | 2018-07-10 15:47:11 +0200 | [diff] [blame] | 880 | |
Michal Vasko | b740023 | 2018-07-11 15:35:57 +0200 | [diff] [blame] | 881 | /* compare all the hashes starting from collision ID 0 */ |
| 882 | for (i = 0; i < hash_count; ++i) { |
| 883 | sibling_hash = lyb_hash(sibling, i); |
| 884 | if (sibling_hash != hash[i]) { |
| 885 | return 0; |
| 886 | } |
| 887 | } |
Michal Vasko | 228431e | 2018-07-10 15:47:11 +0200 | [diff] [blame] | 888 | |
Michal Vasko | b740023 | 2018-07-11 15:35:57 +0200 | [diff] [blame] | 889 | return 1; |
Michal Vasko | 228431e | 2018-07-10 15:47:11 +0200 | [diff] [blame] | 890 | } |
| 891 | |
| 892 | static int |
| 893 | lyb_parse_schema_hash(const struct lys_node *sparent, const struct lys_module *mod, const char *data, const char *yang_data_name, |
| 894 | int options, struct lys_node **snode, struct lyb_state *lybs) |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 895 | { |
| 896 | int r, ret = 0; |
Michal Vasko | b740023 | 2018-07-11 15:35:57 +0200 | [diff] [blame] | 897 | uint8_t i, j; |
| 898 | struct lys_node *sibling; |
| 899 | LYB_HASH hash[LYB_HASH_BITS - 1]; |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 900 | struct ly_ctx *ctx; |
| 901 | |
| 902 | assert((sparent || mod) && (!sparent || !mod)); |
| 903 | ctx = (sparent ? sparent->module->ctx : mod->ctx); |
| 904 | |
Michal Vasko | b740023 | 2018-07-11 15:35:57 +0200 | [diff] [blame] | 905 | /* read the first hash */ |
| 906 | ret += (r = lyb_read(data, &hash[0], sizeof *hash, lybs)); |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 907 | LYB_HAVE_READ_RETURN(r, data, -1); |
Michal Vasko | b740023 | 2018-07-11 15:35:57 +0200 | [diff] [blame] | 908 | |
| 909 | /* based on the first hash read all the other ones, if any */ |
Michal Vasko | 01fc23a | 2018-11-21 11:58:26 +0100 | [diff] [blame] | 910 | for (i = 0; !(hash[0] & (LYB_HASH_COLLISION_ID >> i)); ++i) { |
| 911 | if (i > LYB_HASH_BITS) { |
| 912 | return -1; |
| 913 | } |
| 914 | } |
Michal Vasko | b740023 | 2018-07-11 15:35:57 +0200 | [diff] [blame] | 915 | |
| 916 | /* move the first hash on its accurate position */ |
| 917 | hash[i] = hash[0]; |
| 918 | |
| 919 | /* read the rest of hashes */ |
| 920 | for (j = i; j; --j) { |
| 921 | ret += (r = lyb_read(data, &hash[j - 1], sizeof *hash, lybs)); |
| 922 | LYB_HAVE_READ_RETURN(r, data, -1); |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 923 | |
Michal Vasko | 365849c | 2018-08-07 11:31:30 +0200 | [diff] [blame] | 924 | /* correct collision ID */ |
| 925 | assert(hash[j - 1] & (LYB_HASH_COLLISION_ID >> (j - 1))); |
| 926 | /* preceded with zeros */ |
| 927 | assert(!(hash[j - 1] & (LYB_HASH_MASK << (LYB_HASH_BITS - (j - 1))))); |
Michal Vasko | 228431e | 2018-07-10 15:47:11 +0200 | [diff] [blame] | 928 | } |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 929 | |
Michal Vasko | e3880e6 | 2018-07-11 16:18:34 +0200 | [diff] [blame] | 930 | /* handle yang data templates */ |
| 931 | if ((options & LYD_OPT_DATA_TEMPLATE) && yang_data_name && mod) { |
| 932 | sparent = lyp_get_yang_data_template(mod, yang_data_name, strlen(yang_data_name)); |
| 933 | if (!sparent) { |
| 934 | sibling = NULL; |
| 935 | goto finish; |
| 936 | } |
| 937 | } |
| 938 | |
| 939 | /* handle RPC/action input/output */ |
Michal Vasko | b740023 | 2018-07-11 15:35:57 +0200 | [diff] [blame] | 940 | if (sparent && (sparent->nodetype & (LYS_RPC | LYS_ACTION))) { |
| 941 | sibling = NULL; |
| 942 | while ((sibling = (struct lys_node *)lys_getnext(sibling, sparent, NULL, LYS_GETNEXT_WITHINOUT))) { |
| 943 | if ((sibling->nodetype == LYS_INPUT) && (options & LYD_OPT_RPC)) { |
| 944 | break; |
| 945 | } |
| 946 | if ((sibling->nodetype == LYS_OUTPUT) && (options & LYD_OPT_RPCREPLY)) { |
| 947 | break; |
| 948 | } |
| 949 | } |
| 950 | if (!sibling) { |
| 951 | /* fail */ |
| 952 | goto finish; |
| 953 | } |
| 954 | |
| 955 | /* use only input/output children nodes */ |
| 956 | sparent = sibling; |
| 957 | } |
| 958 | |
| 959 | /* find our node with matching hashes */ |
| 960 | sibling = NULL; |
Michal Vasko | 228431e | 2018-07-10 15:47:11 +0200 | [diff] [blame] | 961 | while ((sibling = (struct lys_node *)lys_getnext(sibling, sparent, mod, 0))) { |
| 962 | /* skip schema nodes from models not present during printing */ |
Michal Vasko | b740023 | 2018-07-11 15:35:57 +0200 | [diff] [blame] | 963 | if (lyb_has_schema_model(sibling, lybs->models, lybs->mod_count) && lyb_is_schema_hash_match(sibling, hash, i + 1)) { |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 964 | /* match found */ |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 965 | break; |
| 966 | } |
| 967 | } |
| 968 | |
Michal Vasko | b740023 | 2018-07-11 15:35:57 +0200 | [diff] [blame] | 969 | finish: |
Michal Vasko | 228431e | 2018-07-10 15:47:11 +0200 | [diff] [blame] | 970 | *snode = sibling; |
| 971 | if (!sibling && (options & LYD_OPT_STRICT)) { |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 972 | if (mod) { |
| 973 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Failed to find matching hash for a top-level node from \"%s\".", mod->name); |
| 974 | } else { |
| 975 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, sparent, "Failed to find matching hash for a child of \"%s\".", sparent->name); |
| 976 | } |
| 977 | return -1; |
| 978 | } |
| 979 | |
| 980 | return ret; |
| 981 | } |
| 982 | |
| 983 | static int |
| 984 | lyb_parse_subtree(struct ly_ctx *ctx, const char *data, struct lyd_node *parent, struct lyd_node **first_sibling, |
Michal Vasko | 228431e | 2018-07-10 15:47:11 +0200 | [diff] [blame] | 985 | const char *yang_data_name, int options, struct unres_data *unres, struct lyb_state *lybs) |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 986 | { |
| 987 | int r, ret = 0; |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 988 | struct lyd_node *node = NULL, *iter; |
| 989 | const struct lys_module *mod; |
Michal Vasko | e3880e6 | 2018-07-11 16:18:34 +0200 | [diff] [blame] | 990 | struct lys_node *snode; |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 991 | |
| 992 | assert((parent && !first_sibling) || (!parent && first_sibling)); |
| 993 | |
| 994 | /* register a new subtree */ |
| 995 | ret += (r = lyb_read_start_subtree(data, lybs)); |
| 996 | LYB_HAVE_READ_GOTO(r, data, error); |
| 997 | |
| 998 | if (!parent) { |
| 999 | /* top-level, read module name */ |
| 1000 | ret += (r = lyb_parse_model(ctx, data, &mod, lybs)); |
| 1001 | LYB_HAVE_READ_GOTO(r, data, error); |
| 1002 | |
Michal Vasko | 5383066 | 2018-07-24 16:09:49 +0200 | [diff] [blame] | 1003 | if (mod) { |
| 1004 | /* read hash, find the schema node starting from mod, possibly yang_data_name */ |
| 1005 | r = lyb_parse_schema_hash(NULL, mod, data, yang_data_name, options, &snode, lybs); |
| 1006 | } |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 1007 | } else { |
Michal Vasko | 5383066 | 2018-07-24 16:09:49 +0200 | [diff] [blame] | 1008 | mod = lyd_node_module(parent); |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 1009 | |
Michal Vasko | e3880e6 | 2018-07-11 16:18:34 +0200 | [diff] [blame] | 1010 | /* read hash, find the schema node starting from parent schema */ |
| 1011 | r = lyb_parse_schema_hash(parent->schema, NULL, data, NULL, options, &snode, lybs); |
| 1012 | } |
| 1013 | ret += r; |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 1014 | LYB_HAVE_READ_GOTO(r, data, error); |
| 1015 | |
Michal Vasko | 5383066 | 2018-07-24 16:09:49 +0200 | [diff] [blame] | 1016 | if (!mod || !snode) { |
Michal Vasko | 997071e | 2018-07-10 15:56:14 +0200 | [diff] [blame] | 1017 | /* unknown data subtree, skip it whole */ |
| 1018 | do { |
| 1019 | ret += (r = lyb_read(data, NULL, lybs->written[lybs->used - 1], lybs)); |
Michal Vasko | a985252 | 2018-08-08 16:24:45 +0200 | [diff] [blame] | 1020 | /* also skip the meta information inside */ |
| 1021 | ret += (r = lyb_read(data, NULL, lybs->inner_chunks[lybs->used - 1] * LYB_META_BYTES, lybs)); |
Michal Vasko | 997071e | 2018-07-10 15:56:14 +0200 | [diff] [blame] | 1022 | } while (lybs->written[lybs->used - 1]); |
Michal Vasko | 228431e | 2018-07-10 15:47:11 +0200 | [diff] [blame] | 1023 | goto stop_subtree; |
| 1024 | } |
| 1025 | |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 1026 | /* |
| 1027 | * read the node |
| 1028 | */ |
| 1029 | node = lyb_new_node(snode); |
| 1030 | if (!node) { |
| 1031 | goto error; |
| 1032 | } |
| 1033 | |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 1034 | ret += (r = lyb_parse_attributes(node, data, options, unres, lybs)); |
| 1035 | LYB_HAVE_READ_GOTO(r, data, error); |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 1036 | |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 1037 | /* read node content */ |
| 1038 | switch (snode->nodetype) { |
| 1039 | case LYS_CONTAINER: |
| 1040 | case LYS_LIST: |
Michal Vasko | b740023 | 2018-07-11 15:35:57 +0200 | [diff] [blame] | 1041 | case LYS_NOTIF: |
| 1042 | case LYS_RPC: |
| 1043 | case LYS_ACTION: |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 1044 | /* nothing to read */ |
| 1045 | break; |
| 1046 | case LYS_LEAF: |
| 1047 | case LYS_LEAFLIST: |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 1048 | ret += (r = lyb_parse_value(&((struct lys_node_leaf *)node->schema)->type, (struct lyd_node_leaf_list *)node, |
| 1049 | NULL, data, unres, lybs)); |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 1050 | LYB_HAVE_READ_GOTO(r, data, error); |
| 1051 | break; |
| 1052 | case LYS_ANYXML: |
| 1053 | case LYS_ANYDATA: |
| 1054 | ret += (r = lyb_parse_anydata(node, data, lybs)); |
| 1055 | LYB_HAVE_READ_GOTO(r, data, error); |
| 1056 | break; |
| 1057 | default: |
| 1058 | goto error; |
| 1059 | } |
| 1060 | |
| 1061 | /* insert into data tree, manually */ |
| 1062 | if (parent) { |
| 1063 | if (!parent->child) { |
| 1064 | /* only child */ |
| 1065 | parent->child = node; |
| 1066 | } else { |
| 1067 | /* last child */ |
| 1068 | parent->child->prev->next = node; |
| 1069 | node->prev = parent->child->prev; |
| 1070 | parent->child->prev = node; |
| 1071 | } |
| 1072 | node->parent = parent; |
| 1073 | } else if (*first_sibling) { |
| 1074 | /* last sibling */ |
| 1075 | (*first_sibling)->prev->next = node; |
| 1076 | node->prev = (*first_sibling)->prev; |
| 1077 | (*first_sibling)->prev = node; |
| 1078 | } else { |
| 1079 | /* only sibling */ |
| 1080 | *first_sibling = node; |
| 1081 | } |
| 1082 | |
| 1083 | /* read all descendants */ |
| 1084 | while (lybs->written[lybs->used - 1]) { |
Michal Vasko | 228431e | 2018-07-10 15:47:11 +0200 | [diff] [blame] | 1085 | ret += (r = lyb_parse_subtree(ctx, data, node, NULL, NULL, options, unres, lybs)); |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 1086 | LYB_HAVE_READ_GOTO(r, data, error); |
| 1087 | } |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 1088 | |
| 1089 | /* make containers default if should be */ |
Michal Vasko | ddd545b | 2018-08-09 11:34:58 +0200 | [diff] [blame] | 1090 | if ((node->schema->nodetype == LYS_CONTAINER) && !((struct lys_node_container *)node->schema)->presence) { |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 1091 | LY_TREE_FOR(node->child, iter) { |
| 1092 | if (!iter->dflt) { |
| 1093 | break; |
| 1094 | } |
| 1095 | } |
| 1096 | |
| 1097 | if (!iter) { |
| 1098 | node->dflt = 1; |
| 1099 | } |
| 1100 | } |
| 1101 | |
Michal Vasko | fe43d04 | 2018-07-24 09:43:46 +0200 | [diff] [blame] | 1102 | #ifdef LY_ENABLED_CACHE |
Michal Vasko | 4968b9c | 2018-08-14 13:43:52 +0200 | [diff] [blame] | 1103 | /* calculate the hash and insert it into parent (list with keys is handled when its keys are inserted) */ |
| 1104 | if ((node->schema->nodetype != LYS_LIST) || !((struct lys_node_list *)node->schema)->keys_size) { |
Michal Vasko | fe43d04 | 2018-07-24 09:43:46 +0200 | [diff] [blame] | 1105 | lyd_hash(node); |
| 1106 | lyd_insert_hash(node); |
| 1107 | } |
| 1108 | #endif |
| 1109 | |
Michal Vasko | 228431e | 2018-07-10 15:47:11 +0200 | [diff] [blame] | 1110 | stop_subtree: |
| 1111 | /* end the subtree */ |
| 1112 | lyb_read_stop_subtree(lybs); |
| 1113 | |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 1114 | return ret; |
| 1115 | |
| 1116 | error: |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 1117 | lyd_free(node); |
Michal Vasko | b740023 | 2018-07-11 15:35:57 +0200 | [diff] [blame] | 1118 | if (first_sibling && (*first_sibling == node)) { |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 1119 | *first_sibling = NULL; |
| 1120 | } |
| 1121 | return -1; |
| 1122 | } |
| 1123 | |
| 1124 | static int |
Michal Vasko | 228431e | 2018-07-10 15:47:11 +0200 | [diff] [blame] | 1125 | lyb_parse_data_models(struct ly_ctx *ctx, const char *data, struct lyb_state *lybs) |
| 1126 | { |
| 1127 | int i, r, ret = 0; |
Graeme Campbell | 856a40e | 2018-10-05 14:35:37 +1300 | [diff] [blame] | 1128 | uint8_t mod_count_buf[2]; |
Michal Vasko | 228431e | 2018-07-10 15:47:11 +0200 | [diff] [blame] | 1129 | |
| 1130 | /* read model count */ |
Graeme Campbell | 856a40e | 2018-10-05 14:35:37 +1300 | [diff] [blame] | 1131 | ret += (r = lyb_read(data, mod_count_buf, 2, lybs)); |
Michal Vasko | 228431e | 2018-07-10 15:47:11 +0200 | [diff] [blame] | 1132 | LYB_HAVE_READ_RETURN(r, data, -1); |
Graeme Campbell | 856a40e | 2018-10-05 14:35:37 +1300 | [diff] [blame] | 1133 | lybs->mod_count = mod_count_buf[0] | (mod_count_buf[1] << 8); |
Michal Vasko | 228431e | 2018-07-10 15:47:11 +0200 | [diff] [blame] | 1134 | |
| 1135 | lybs->models = malloc(lybs->mod_count * sizeof *lybs->models); |
| 1136 | LY_CHECK_ERR_RETURN(!lybs->models, LOGMEM(NULL), -1); |
| 1137 | |
| 1138 | /* read modules */ |
| 1139 | for (i = 0; i < lybs->mod_count; ++i) { |
| 1140 | ret += (r = lyb_parse_model(ctx, data, &lybs->models[i], lybs)); |
| 1141 | LYB_HAVE_READ_RETURN(r, data, -1); |
| 1142 | } |
| 1143 | |
| 1144 | return ret; |
| 1145 | } |
| 1146 | |
| 1147 | static int |
Michal Vasko | 5c44504 | 2018-08-28 16:27:45 +0200 | [diff] [blame] | 1148 | lyb_parse_magic_number(const char *data, struct lyb_state *lybs) |
| 1149 | { |
Michal Vasko | 7d15c3a | 2018-11-22 15:27:46 +0100 | [diff] [blame] | 1150 | int r, ret = 0; |
| 1151 | char magic_byte = 0; |
Michal Vasko | 5c44504 | 2018-08-28 16:27:45 +0200 | [diff] [blame] | 1152 | |
Michal Vasko | 7d15c3a | 2018-11-22 15:27:46 +0100 | [diff] [blame] | 1153 | ret += (r = lyb_read(data, (uint8_t *)&magic_byte, 1, lybs)); |
| 1154 | LYB_HAVE_READ_RETURN(r, data, -1); |
| 1155 | if (magic_byte != 'l') { |
| 1156 | LOGERR(NULL, LY_EINVAL, "Invalid first magic number byte \"0x%02x\".", magic_byte); |
| 1157 | return -1; |
| 1158 | } |
Michal Vasko | 5c44504 | 2018-08-28 16:27:45 +0200 | [diff] [blame] | 1159 | |
Michal Vasko | 7d15c3a | 2018-11-22 15:27:46 +0100 | [diff] [blame] | 1160 | ret += (r = lyb_read(data, (uint8_t *)&magic_byte, 1, lybs)); |
| 1161 | LYB_HAVE_READ_RETURN(r, data, -1); |
| 1162 | if (magic_byte != 'y') { |
| 1163 | LOGERR(NULL, LY_EINVAL, "Invalid second magic number byte \"0x%02x\".", magic_byte); |
| 1164 | return -1; |
| 1165 | } |
| 1166 | |
| 1167 | ret += (r = lyb_read(data, (uint8_t *)&magic_byte, 1, lybs)); |
| 1168 | LYB_HAVE_READ_RETURN(r, data, -1); |
| 1169 | if (magic_byte != 'b') { |
| 1170 | LOGERR(NULL, LY_EINVAL, "Invalid third magic number byte \"0x%02x\".", magic_byte); |
Michal Vasko | 5c44504 | 2018-08-28 16:27:45 +0200 | [diff] [blame] | 1171 | return -1; |
| 1172 | } |
| 1173 | |
| 1174 | return ret; |
| 1175 | } |
| 1176 | |
| 1177 | static int |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 1178 | lyb_parse_header(const char *data, struct lyb_state *lybs) |
| 1179 | { |
| 1180 | int ret = 0; |
| 1181 | uint8_t byte = 0; |
| 1182 | |
Michal Vasko | 228431e | 2018-07-10 15:47:11 +0200 | [diff] [blame] | 1183 | /* TODO version, any flags? */ |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 1184 | ret += lyb_read(data, (uint8_t *)&byte, sizeof byte, lybs); |
| 1185 | |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 1186 | return ret; |
| 1187 | } |
| 1188 | |
| 1189 | struct lyd_node * |
Michal Vasko | 228431e | 2018-07-10 15:47:11 +0200 | [diff] [blame] | 1190 | lyd_parse_lyb(struct ly_ctx *ctx, const char *data, int options, const struct lyd_node *data_tree, |
| 1191 | const char *yang_data_name, int *parsed) |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 1192 | { |
Michal Vasko | beeb1bf | 2018-08-17 10:29:29 +0200 | [diff] [blame] | 1193 | int r = 0, ret = 0; |
Michal Vasko | 228431e | 2018-07-10 15:47:11 +0200 | [diff] [blame] | 1194 | struct lyd_node *node = NULL, *next, *act_notif = NULL; |
| 1195 | struct unres_data *unres = NULL; |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 1196 | struct lyb_state lybs; |
| 1197 | |
| 1198 | if (!ctx || !data) { |
| 1199 | LOGARG; |
| 1200 | return NULL; |
| 1201 | } |
| 1202 | |
| 1203 | lybs.written = malloc(LYB_STATE_STEP * sizeof *lybs.written); |
| 1204 | lybs.position = malloc(LYB_STATE_STEP * sizeof *lybs.position); |
Michal Vasko | a985252 | 2018-08-08 16:24:45 +0200 | [diff] [blame] | 1205 | lybs.inner_chunks = malloc(LYB_STATE_STEP * sizeof *lybs.inner_chunks); |
| 1206 | LY_CHECK_ERR_GOTO(!lybs.written || !lybs.position || !lybs.inner_chunks, LOGMEM(ctx), finish); |
Michal Vasko | 4968b9c | 2018-08-14 13:43:52 +0200 | [diff] [blame] | 1207 | lybs.used = 0; |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 1208 | lybs.size = LYB_STATE_STEP; |
Michal Vasko | 4968b9c | 2018-08-14 13:43:52 +0200 | [diff] [blame] | 1209 | lybs.models = NULL; |
| 1210 | lybs.mod_count = 0; |
Michal Vasko | 228431e | 2018-07-10 15:47:11 +0200 | [diff] [blame] | 1211 | |
| 1212 | unres = calloc(1, sizeof *unres); |
| 1213 | LY_CHECK_ERR_GOTO(!unres, LOGMEM(ctx), finish); |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 1214 | |
Michal Vasko | 5c44504 | 2018-08-28 16:27:45 +0200 | [diff] [blame] | 1215 | /* read magic number */ |
| 1216 | ret += (r = lyb_parse_magic_number(data, &lybs)); |
| 1217 | LYB_HAVE_READ_GOTO(r, data, finish); |
| 1218 | |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 1219 | /* read header */ |
| 1220 | ret += (r = lyb_parse_header(data, &lybs)); |
| 1221 | LYB_HAVE_READ_GOTO(r, data, finish); |
| 1222 | |
Michal Vasko | 228431e | 2018-07-10 15:47:11 +0200 | [diff] [blame] | 1223 | /* read used models */ |
| 1224 | ret += (r = lyb_parse_data_models(ctx, data, &lybs)); |
| 1225 | LYB_HAVE_READ_GOTO(r, data, finish); |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 1226 | |
| 1227 | /* read subtree(s) */ |
| 1228 | while (data[0]) { |
Michal Vasko | 228431e | 2018-07-10 15:47:11 +0200 | [diff] [blame] | 1229 | ret += (r = lyb_parse_subtree(ctx, data, NULL, &node, yang_data_name, options, unres, &lybs)); |
| 1230 | if (r < 0) { |
| 1231 | lyd_free_withsiblings(node); |
| 1232 | node = NULL; |
| 1233 | goto finish; |
| 1234 | } |
| 1235 | data += r; |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 1236 | } |
| 1237 | |
| 1238 | /* read the last zero, parsing finished */ |
| 1239 | ++ret; |
| 1240 | r = ret; |
| 1241 | |
Michal Vasko | 228431e | 2018-07-10 15:47:11 +0200 | [diff] [blame] | 1242 | if (options & LYD_OPT_DATA_ADD_YANGLIB) { |
| 1243 | if (lyd_merge(node, ly_ctx_info(ctx), LYD_OPT_DESTRUCT | LYD_OPT_EXPLICIT)) { |
| 1244 | LOGERR(ctx, LY_EINT, "Adding ietf-yang-library data failed."); |
| 1245 | lyd_free_withsiblings(node); |
| 1246 | node = NULL; |
| 1247 | goto finish; |
| 1248 | } |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 1249 | } |
Michal Vasko | 228431e | 2018-07-10 15:47:11 +0200 | [diff] [blame] | 1250 | |
| 1251 | /* resolve any unresolved instance-identifiers */ |
| 1252 | if (unres->count) { |
| 1253 | if (options & (LYD_OPT_RPC | LYD_OPT_RPCREPLY | LYD_OPT_NOTIF)) { |
| 1254 | LY_TREE_DFS_BEGIN(node, next, act_notif) { |
| 1255 | if (act_notif->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) { |
| 1256 | break; |
| 1257 | } |
| 1258 | LY_TREE_DFS_END(node, next, act_notif); |
| 1259 | } |
| 1260 | } |
Michal Vasko | 993af1e | 2018-12-10 12:05:17 +0100 | [diff] [blame] | 1261 | if (lyd_defaults_add_unres(&node, options, ctx, NULL, 0, data_tree, act_notif, unres, 0)) { |
Michal Vasko | 228431e | 2018-07-10 15:47:11 +0200 | [diff] [blame] | 1262 | lyd_free_withsiblings(node); |
| 1263 | node = NULL; |
| 1264 | goto finish; |
| 1265 | } |
| 1266 | } |
| 1267 | |
| 1268 | finish: |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 1269 | free(lybs.written); |
| 1270 | free(lybs.position); |
Michal Vasko | a985252 | 2018-08-08 16:24:45 +0200 | [diff] [blame] | 1271 | free(lybs.inner_chunks); |
Michal Vasko | 228431e | 2018-07-10 15:47:11 +0200 | [diff] [blame] | 1272 | free(lybs.models); |
| 1273 | if (unres) { |
| 1274 | free(unres->node); |
| 1275 | free(unres->type); |
| 1276 | free(unres); |
| 1277 | } |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 1278 | |
| 1279 | if (parsed) { |
| 1280 | *parsed = r; |
| 1281 | } |
| 1282 | return node; |
| 1283 | } |