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