Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file lyb.h |
| 3 | * @author Michal Vasko <mvasko@cesnet.cz> |
| 4 | * @brief Header for LYB format printer & parser |
| 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 | #ifndef LY_LYB_H_ |
| 16 | #define LY_LYB_H_ |
| 17 | |
| 18 | #include <stddef.h> |
| 19 | #include <stdint.h> |
| 20 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 21 | #include "parser_internal.h" |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 22 | #include "set.h" |
| 23 | #include "tree.h" |
| 24 | |
| 25 | struct hash_table; |
| 26 | struct ly_ctx; |
| 27 | struct lyd_node; |
| 28 | struct lysc_node; |
| 29 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 30 | struct lylyb_ctx { |
| 31 | const struct ly_ctx *ctx; |
| 32 | uint64_t line; /* current line */ |
| 33 | struct ly_in *in; /* input structure */ |
| 34 | |
| 35 | const struct lys_module **models; |
| 36 | |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 37 | struct lyd_lyb_subtree { |
| 38 | size_t written; |
| 39 | size_t position; |
| 40 | uint8_t inner_chunks; |
| 41 | } *subtrees; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 42 | LY_ARRAY_COUNT_TYPE subtree_size; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 43 | |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 44 | /* LYB printer only */ |
| 45 | struct lyd_lyb_sib_ht { |
| 46 | struct lysc_node *first_sibling; |
| 47 | struct hash_table *ht; |
| 48 | } *sib_hts; |
| 49 | }; |
| 50 | |
| 51 | /** |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 52 | * @brief Internal structure for LYB parser/printer. |
| 53 | * |
| 54 | * Note that the structure maps to the lyd_ctx which is common for all the data parsers |
| 55 | */ |
| 56 | struct lyd_lyb_ctx { |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 57 | const struct lysc_ext_instance *ext; /**< extension instance possibly changing document root context of the data being parsed */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 58 | union { |
| 59 | struct { |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 60 | uint32_t parse_opts; /**< various @ref dataparseroptions. */ |
| 61 | uint32_t val_opts; /**< various @ref datavalidationoptions. */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 62 | }; |
| 63 | uint32_t print_options; |
| 64 | }; |
| 65 | uint32_t int_opts; /**< internal data parser options */ |
| 66 | uint32_t path_len; /**< used bytes in the path buffer */ |
| 67 | char path[LYD_PARSER_BUFSIZE]; /**< buffer for the generated path */ |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 68 | struct ly_set node_when; /**< set of nodes with "when" conditions */ |
Radek Krejci | 4f2e3e5 | 2021-03-30 14:20:28 +0200 | [diff] [blame] | 69 | struct ly_set node_exts; /**< set of nodes and extensions connected with a plugin providing own validation callback */ |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 70 | struct ly_set node_types; /**< set of nodes validated with LY_EINCOMPLETE result */ |
| 71 | struct ly_set meta_types; /**< set of metadata validated with LY_EINCOMPLETE result */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 72 | struct lyd_node *op_node; /**< if an RPC/action/notification is being parsed, store the pointer to it */ |
| 73 | |
| 74 | /* callbacks */ |
| 75 | lyd_ctx_free_clb free; /* destructor */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 76 | |
| 77 | struct lylyb_ctx *lybctx; /* lyb format context */ |
| 78 | }; |
| 79 | |
| 80 | /** |
| 81 | * @brief Destructor for the lylyb_ctx structure |
| 82 | */ |
| 83 | void lyd_lyb_ctx_free(struct lyd_ctx *lydctx); |
| 84 | |
| 85 | /** |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 86 | * LYB format |
| 87 | * |
| 88 | * Unlike XML or JSON, it is binary format so most data are represented in similar way but in binary. |
| 89 | * Some notable differences: |
| 90 | * |
| 91 | * - schema nodes are identified based on their hash instead of their string name. In case of collisions |
| 92 | * an array of hashes is created with each next hash one bit shorter until a unique sequence of all these |
| 93 | * hashes is found and then all of them are stored. |
| 94 | * |
| 95 | * - tree structure is represented as individual strictly bounded subtrees. Each subtree begins |
| 96 | * with its metadata, which consist of 1) the whole subtree length in bytes and 2) number |
| 97 | * of included metadata chunks of nested subtrees. |
| 98 | * |
| 99 | * - since length of a subtree is not known before it is printed, holes are first written and |
| 100 | * after the subtree is printed, they are filled with actual valid metadata. As a consequence, |
| 101 | * LYB data cannot be directly printed into streams! |
| 102 | * |
| 103 | * - data are preceded with information about all the used modules. It is needed because of |
| 104 | * possible augments and deviations which must be known beforehand, otherwise schema hashes |
| 105 | * could be matched to the wrong nodes. |
| 106 | */ |
| 107 | |
| 108 | /* just a shortcut */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 109 | #define LYB_LAST_SUBTREE(lybctx) lybctx->subtrees[LY_ARRAY_COUNT(lybctx->subtrees) - 1] |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 110 | |
| 111 | /* struct lyd_lyb_subtree allocation step */ |
| 112 | #define LYB_SUBTREE_STEP 4 |
| 113 | |
| 114 | /* current LYB format version */ |
| 115 | #define LYB_VERSION_NUM 0x10 |
| 116 | |
| 117 | /* LYB format version mask of the header byte */ |
| 118 | #define LYB_VERSION_MASK 0x10 |
| 119 | |
| 120 | /** |
| 121 | * LYB schema hash constants |
| 122 | * |
| 123 | * Hash is divided to collision ID and hash itself. |
| 124 | * |
Michal Vasko | 11f76c8 | 2021-04-15 14:36:14 +0200 | [diff] [blame] | 125 | * @anchor collisionid |
| 126 | * |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 127 | * First bits are collision ID until 1 is found. The rest is truncated 32b hash. |
| 128 | * 1xxx xxxx - collision ID 0 (no collisions) |
| 129 | * 01xx xxxx - collision ID 1 (collision ID 0 hash collided) |
| 130 | * 001x xxxx - collision ID 2 ... |
Michal Vasko | 11f76c8 | 2021-04-15 14:36:14 +0200 | [diff] [blame] | 131 | * |
| 132 | * When finding a match for a unique schema (siblings) hash (sequence of hashes with increasing collision ID), the highest |
| 133 | * collision ID can be read from the last hash (LYB parser). |
| 134 | * |
| 135 | * To learn what is the highest collision ID of a hash that must be included in a unique schema (siblings) hash, |
| 136 | * collisions with all the preceding sibling schema hashes must be checked (LYB printer). |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 137 | */ |
| 138 | |
| 139 | /* Number of bits the whole hash will take (including hash collision ID) */ |
| 140 | #define LYB_HASH_BITS 8 |
| 141 | |
| 142 | /* Masking 32b hash (collision ID 0) */ |
| 143 | #define LYB_HASH_MASK 0x7f |
| 144 | |
| 145 | /* Type for storing the whole hash (used only internally, publicly defined directly) */ |
| 146 | #define LYB_HASH uint8_t |
| 147 | |
| 148 | /* Need to move this first >> collision number (from 0) to get collision ID hash part */ |
| 149 | #define LYB_HASH_COLLISION_ID 0x80 |
| 150 | |
| 151 | /* How many bytes are reserved for one data chunk SIZE (8B is maximum) */ |
| 152 | #define LYB_SIZE_BYTES 1 |
| 153 | |
| 154 | /* Maximum size that will be written into LYB_SIZE_BYTES (must be large enough) */ |
| 155 | #define LYB_SIZE_MAX UINT8_MAX |
| 156 | |
| 157 | /* How many bytes are reserved for one data chunk inner chunk count */ |
| 158 | #define LYB_INCHUNK_BYTES 1 |
| 159 | |
| 160 | /* Maximum size that will be written into LYB_INCHUNK_BYTES (must be large enough) */ |
| 161 | #define LYB_INCHUNK_MAX UINT8_MAX |
| 162 | |
| 163 | /* Just a helper macro */ |
| 164 | #define LYB_META_BYTES (LYB_INCHUNK_BYTES + LYB_SIZE_BYTES) |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 165 | #define LYB_BYTE_MASK 0xff |
| 166 | |
| 167 | /* model revision as XXXX XXXX XXXX XXXX (2B) (year is offset from 2000) |
| 168 | * YYYY YYYM MMMD DDDD */ |
| 169 | #define LYB_REV_YEAR_OFFSET 2000 |
| 170 | #define LYB_REV_YEAR_MASK 0xfe00U |
| 171 | #define LYB_REV_YEAR_SHIFT 9 |
| 172 | #define LYB_REV_MONTH_MASK 0x01E0U |
| 173 | #define LYB_REV_MONTH_SHIFT 5 |
| 174 | #define LYB_REV_DAY_MASK 0x001fU |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 175 | |
| 176 | /* Type large enough for all meta data */ |
| 177 | #define LYB_META uint16_t |
| 178 | |
Michal Vasko | 11f76c8 | 2021-04-15 14:36:14 +0200 | [diff] [blame] | 179 | /** |
| 180 | * @brief Get single hash for a schema node to be used for LYB data. Read from cache, if possible. |
| 181 | * |
| 182 | * @param[in] node Node to hash. |
| 183 | * @param[in] collision_id Collision ID of the hash to generate, see @ref collisionid. |
| 184 | * @return Generated hash. |
| 185 | */ |
| 186 | LYB_HASH lyb_get_hash(const struct lysc_node *node, uint8_t collision_id); |
| 187 | |
| 188 | /** |
| 189 | * @brief Fill the hash cache of all the schema nodes of a module. |
| 190 | * |
| 191 | * @param[in] mod Module to process. |
| 192 | */ |
| 193 | void lyb_cache_module_hash(const struct lys_module *mod); |
| 194 | |
| 195 | /** |
| 196 | * @brief Check whether a node's module is in a module array. |
| 197 | * |
| 198 | * @param[in] node Node to check. |
| 199 | * @param[in] models Modules in a sized array. |
| 200 | * @return Boolean value whether @p node's module was found in the given @p models array. |
| 201 | */ |
| 202 | ly_bool lyb_has_schema_model(const struct lysc_node *node, const struct lys_module **models); |
| 203 | |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 204 | #endif /* LY_LYB_H_ */ |