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