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 | |
| 21 | #include "set.h" |
| 22 | #include "tree.h" |
| 23 | |
| 24 | struct hash_table; |
| 25 | struct ly_ctx; |
| 26 | struct lyd_node; |
| 27 | struct lysc_node; |
| 28 | |
| 29 | /** |
| 30 | * @brief Internal structure for LYB parser/printer. |
| 31 | */ |
| 32 | struct lyd_lyb_ctx { |
| 33 | struct lyd_lyb_subtree { |
| 34 | size_t written; |
| 35 | size_t position; |
| 36 | uint8_t inner_chunks; |
| 37 | } *subtrees; |
| 38 | LY_ARRAY_SIZE_TYPE subtree_size; |
| 39 | |
| 40 | size_t byte_count; /**< printed/parsed bytes */ |
| 41 | const struct ly_ctx *ctx; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame^] | 42 | union { |
| 43 | struct { |
| 44 | int parse_options; |
| 45 | int validate_options; |
| 46 | }; |
| 47 | int print_options; |
| 48 | }; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 49 | |
| 50 | /* LYB parser only */ |
| 51 | const char *data; |
| 52 | int int_opts; |
| 53 | const struct lys_module **models; |
| 54 | struct ly_set unres_node_type; |
| 55 | struct ly_set unres_meta_type; |
| 56 | struct ly_set when_check; |
| 57 | struct lyd_node *op_ntf; |
| 58 | |
| 59 | /* LYB printer only */ |
| 60 | struct lyd_lyb_sib_ht { |
| 61 | struct lysc_node *first_sibling; |
| 62 | struct hash_table *ht; |
| 63 | } *sib_hts; |
| 64 | }; |
| 65 | |
| 66 | /** |
| 67 | * LYB format |
| 68 | * |
| 69 | * Unlike XML or JSON, it is binary format so most data are represented in similar way but in binary. |
| 70 | * Some notable differences: |
| 71 | * |
| 72 | * - schema nodes are identified based on their hash instead of their string name. In case of collisions |
| 73 | * an array of hashes is created with each next hash one bit shorter until a unique sequence of all these |
| 74 | * hashes is found and then all of them are stored. |
| 75 | * |
| 76 | * - tree structure is represented as individual strictly bounded subtrees. Each subtree begins |
| 77 | * with its metadata, which consist of 1) the whole subtree length in bytes and 2) number |
| 78 | * of included metadata chunks of nested subtrees. |
| 79 | * |
| 80 | * - since length of a subtree is not known before it is printed, holes are first written and |
| 81 | * after the subtree is printed, they are filled with actual valid metadata. As a consequence, |
| 82 | * LYB data cannot be directly printed into streams! |
| 83 | * |
| 84 | * - data are preceded with information about all the used modules. It is needed because of |
| 85 | * possible augments and deviations which must be known beforehand, otherwise schema hashes |
| 86 | * could be matched to the wrong nodes. |
| 87 | */ |
| 88 | |
| 89 | /* just a shortcut */ |
| 90 | #define LYB_LAST_SUBTREE(lybctx) lybctx->subtrees[LY_ARRAY_SIZE(lybctx->subtrees) - 1] |
| 91 | |
| 92 | /* struct lyd_lyb_subtree allocation step */ |
| 93 | #define LYB_SUBTREE_STEP 4 |
| 94 | |
| 95 | /* current LYB format version */ |
| 96 | #define LYB_VERSION_NUM 0x10 |
| 97 | |
| 98 | /* LYB format version mask of the header byte */ |
| 99 | #define LYB_VERSION_MASK 0x10 |
| 100 | |
| 101 | /** |
| 102 | * LYB schema hash constants |
| 103 | * |
| 104 | * Hash is divided to collision ID and hash itself. |
| 105 | * |
| 106 | * First bits are collision ID until 1 is found. The rest is truncated 32b hash. |
| 107 | * 1xxx xxxx - collision ID 0 (no collisions) |
| 108 | * 01xx xxxx - collision ID 1 (collision ID 0 hash collided) |
| 109 | * 001x xxxx - collision ID 2 ... |
| 110 | */ |
| 111 | |
| 112 | /* Number of bits the whole hash will take (including hash collision ID) */ |
| 113 | #define LYB_HASH_BITS 8 |
| 114 | |
| 115 | /* Masking 32b hash (collision ID 0) */ |
| 116 | #define LYB_HASH_MASK 0x7f |
| 117 | |
| 118 | /* Type for storing the whole hash (used only internally, publicly defined directly) */ |
| 119 | #define LYB_HASH uint8_t |
| 120 | |
| 121 | /* Need to move this first >> collision number (from 0) to get collision ID hash part */ |
| 122 | #define LYB_HASH_COLLISION_ID 0x80 |
| 123 | |
| 124 | /* How many bytes are reserved for one data chunk SIZE (8B is maximum) */ |
| 125 | #define LYB_SIZE_BYTES 1 |
| 126 | |
| 127 | /* Maximum size that will be written into LYB_SIZE_BYTES (must be large enough) */ |
| 128 | #define LYB_SIZE_MAX UINT8_MAX |
| 129 | |
| 130 | /* How many bytes are reserved for one data chunk inner chunk count */ |
| 131 | #define LYB_INCHUNK_BYTES 1 |
| 132 | |
| 133 | /* Maximum size that will be written into LYB_INCHUNK_BYTES (must be large enough) */ |
| 134 | #define LYB_INCHUNK_MAX UINT8_MAX |
| 135 | |
| 136 | /* Just a helper macro */ |
| 137 | #define LYB_META_BYTES (LYB_INCHUNK_BYTES + LYB_SIZE_BYTES) |
| 138 | |
| 139 | /* Type large enough for all meta data */ |
| 140 | #define LYB_META uint16_t |
| 141 | |
| 142 | #endif /* LY_LYB_H_ */ |