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