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