forked from CESNET/libyang
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema_compile_node.c
More file actions
4390 lines (3944 loc) · 163 KB
/
schema_compile_node.c
File metadata and controls
4390 lines (3944 loc) · 163 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file schema_compile_node.c
* @author Radek Krejci <rkrejci@cesnet.cz>
* @author Michal Vasko <mvasko@cesnet.cz>
* @brief Schema compilation of common nodes.
*
* Copyright (c) 2015 - 2023 CESNET, z.s.p.o.
*
* This source code is licensed under BSD 3-Clause License (the "License").
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*/
#define _GNU_SOURCE /* asprintf, strdup */
#include "schema_compile_node.h"
#include <assert.h>
#include <ctype.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "compat.h"
#include "dict.h"
#include "log.h"
#include "ly_common.h"
#include "plugins.h"
#include "plugins_internal.h"
#include "plugins_types.h"
#include "schema_compile.h"
#include "schema_compile_amend.h"
#include "schema_features.h"
#include "set.h"
#include "tree.h"
#include "tree_data.h"
#include "tree_edit.h"
#include "tree_schema.h"
#include "tree_schema_internal.h"
#include "xpath.h"
/**
* @brief Item for storing typedef chain item.
*/
struct lys_type_item {
const struct lysp_tpdf *tpdf;
struct lysp_node *node;
};
/**
* @brief Add a node with a when to unres.
*
* @param[in] ctx Compile context.
* @param[in] when Specific compiled when to check.
* @param[in] node Compiled node with when(s).
* @return LY_ERR value.
*/
static LY_ERR
lysc_unres_when_add(struct lysc_ctx *ctx, struct lysc_when *when, struct lysc_node *node)
{
LY_ERR rc = LY_SUCCESS;
struct lysc_unres_when *w = NULL;
/* do not check when(s) in a grouping or in disabled data (high risk of false-positives) */
if (ctx->compile_opts & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED)) {
goto cleanup;
}
/* add new unres when */
w = calloc(1, sizeof *w);
LY_CHECK_ERR_GOTO(!w, LOGMEM(ctx->ctx); rc = LY_EMEM, cleanup);
w->node = node;
w->when = when;
/* add into the unres set */
LY_CHECK_ERR_GOTO(ly_set_add(&ctx->unres->whens, w, 1, NULL), LOGMEM(ctx->ctx); rc = LY_EMEM, cleanup);
w = NULL;
cleanup:
free(w);
return rc;
}
/**
* @brief Add a node with must(s) to unres.
*
* @param[in] ctx Compile context.
* @param[in] node Compiled node with must(s).
* @param[in] pnode Parsed ndoe with must(s).
* @return LY_ERR value.
*/
static LY_ERR
lysc_unres_must_add(struct lysc_ctx *ctx, struct lysc_node *node, struct lysp_node *pnode)
{
struct lysc_unres_must *m = NULL;
LY_ARRAY_COUNT_TYPE u;
struct lysc_must *musts;
struct lysp_restr *pmusts;
LY_ERR ret;
/* do not check must(s) in a grouping or in disabled data (high risk of false-positives) */
if (ctx->compile_opts & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED)) {
return LY_SUCCESS;
}
musts = lysc_node_musts(node);
pmusts = lysp_node_musts(pnode);
assert(LY_ARRAY_COUNT(musts) == LY_ARRAY_COUNT(pmusts));
if (!musts) {
/* no must */
return LY_SUCCESS;
}
/* add new unres must */
m = calloc(1, sizeof *m);
LY_CHECK_ERR_GOTO(!m, ret = LY_EMEM, error);
m->node = node;
/* add must local modules */
LY_ARRAY_CREATE_GOTO(ctx->ctx, m->local_mods, LY_ARRAY_COUNT(pmusts), ret, error);
LY_ARRAY_FOR(pmusts, u) {
m->local_mods[u] = pmusts[u].arg.mod;
LY_ARRAY_INCREMENT(m->local_mods);
}
/* store ext */
m->ext = ctx->ext;
LY_CHECK_ERR_GOTO(ly_set_add(&ctx->unres->musts, m, 1, NULL), ret = LY_EMEM, error);
return LY_SUCCESS;
error:
if (m) {
LY_ARRAY_FREE(m->local_mods);
free(m);
}
LOGMEM(ctx->ctx);
return ret;
}
static LY_ERR
lysc_unres_leafref_add(struct lysc_ctx *ctx, struct lysc_node_leaf *leaf, const struct lysp_module *local_mod)
{
struct lysc_unres_leafref *l = NULL;
struct ly_set *leafrefs_set;
LY_ARRAY_COUNT_TYPE u;
int is_lref = 0;
if (ctx->compile_opts & LYS_COMPILE_GROUPING) {
/* do not check leafrefs in groupings */
return LY_SUCCESS;
}
/* use special set for disabled leafrefs */
leafrefs_set = ctx->compile_opts & LYS_COMPILE_DISABLED ? &ctx->unres->disabled_leafrefs : &ctx->unres->leafrefs;
if (leaf->type->basetype == LY_TYPE_LEAFREF) {
/* leafref */
is_lref = 1;
} else if (leaf->type->basetype == LY_TYPE_UNION) {
/* union with leafrefs */
LY_ARRAY_FOR(((struct lysc_type_union *)leaf->type)->types, u) {
if (((struct lysc_type_union *)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
is_lref = 1;
break;
}
}
}
if (is_lref) {
/* add new unresolved leafref node */
l = calloc(1, sizeof *l);
LY_CHECK_ERR_RET(!l, LOGMEM(ctx->ctx), LY_EMEM);
l->node = &leaf->node;
l->local_mod = local_mod;
l->ext = ctx->ext;
LY_CHECK_ERR_RET(ly_set_add(leafrefs_set, l, 1, NULL), free(l); LOGMEM(ctx->ctx), LY_EMEM);
}
return LY_SUCCESS;
}
/**
* @brief Add/replace a leaf default value in unres.
* Can also be used for a single leaf-list default value.
*
* @param[in] ctx Compile context.
* @param[in] leaf Leaf with the default value.
* @param[in] dflt Default value to use.
* @return LY_ERR value.
*/
static LY_ERR
lysc_unres_leaf_dflt_add(struct lysc_ctx *ctx, struct lysc_node_leaf *leaf, struct lysp_qname *dflt)
{
struct lysc_unres_dflt *r = NULL;
uint32_t i;
if (ctx->compile_opts & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING)) {
return LY_SUCCESS;
}
for (i = 0; i < ctx->unres->dflts.count; ++i) {
if (((struct lysc_unres_dflt *)ctx->unres->dflts.objs[i])->leaf == leaf) {
/* just replace the default */
r = ctx->unres->dflts.objs[i];
lysp_qname_free(ctx->ctx, r->dflt);
free(r->dflt);
break;
}
}
if (!r) {
/* add new unres item */
r = calloc(1, sizeof *r);
LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM);
r->leaf = leaf;
LY_CHECK_RET(ly_set_add(&ctx->unres->dflts, r, 1, NULL));
}
r->dflt = malloc(sizeof *r->dflt);
LY_CHECK_GOTO(!r->dflt, error);
LY_CHECK_GOTO(lysp_qname_dup(ctx->ctx, dflt, r->dflt), error);
return LY_SUCCESS;
error:
free(r->dflt);
LOGMEM(ctx->ctx);
return LY_EMEM;
}
/**
* @brief Add/replace a leaf-list default value(s) in unres.
*
* @param[in] ctx Compile context.
* @param[in] llist Leaf-list with the default value.
* @param[in] dflts Sized array of the default values.
* @return LY_ERR value.
*/
static LY_ERR
lysc_unres_llist_dflts_add(struct lysc_ctx *ctx, struct lysc_node_leaflist *llist, struct lysp_qname *dflts)
{
struct lysc_unres_dflt *r = NULL;
uint32_t i;
if (ctx->compile_opts & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING)) {
return LY_SUCCESS;
}
for (i = 0; i < ctx->unres->dflts.count; ++i) {
if (((struct lysc_unres_dflt *)ctx->unres->dflts.objs[i])->llist == llist) {
/* just replace the defaults */
r = ctx->unres->dflts.objs[i];
lysp_qname_free(ctx->ctx, r->dflt);
free(r->dflt);
r->dflt = NULL;
FREE_ARRAY(ctx->ctx, r->dflts, lysp_qname_free);
r->dflts = NULL;
break;
}
}
if (!r) {
r = calloc(1, sizeof *r);
LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM);
r->llist = llist;
LY_CHECK_RET(ly_set_add(&ctx->unres->dflts, r, 1, NULL));
}
DUP_ARRAY(ctx->ctx, dflts, r->dflts, lysp_qname_dup);
return LY_SUCCESS;
}
/**
* @brief Add a bits/enumeration type to unres.
*
* @param[in] ctx Compile context.
* @param[in] leaf Leaf of type bits/enumeration whose disabled items to free.
* @return LY_ERR value.
*/
static LY_ERR
lysc_unres_bitenum_add(struct lysc_ctx *ctx, struct lysc_node_leaf *leaf)
{
if (ctx->compile_opts & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING)) {
/* skip groupings and redundant for disabled nodes */
return LY_SUCCESS;
}
LY_CHECK_RET(ly_set_add(&ctx->unres->disabled_bitenums, leaf, 1, NULL));
return LY_SUCCESS;
}
/**
* @brief Duplicate the compiled pattern structure.
*
* Instead of duplicating memory, the reference counter in the @p orig is increased.
*
* @param[in] orig The pattern structure to duplicate.
* @return The duplicated structure to use.
*/
static struct lysc_pattern *
lysc_pattern_dup(struct lysc_pattern *orig)
{
++orig->refcount;
return orig;
}
/**
* @brief Duplicate the array of compiled patterns.
*
* The sized array itself is duplicated, but the pattern structures are just shadowed by increasing their reference counter.
*
* @param[in] ctx Libyang context for logging.
* @param[in] orig The patterns sized array to duplicate.
* @return New sized array as a copy of @p orig.
* @return NULL in case of memory allocation error.
*/
static struct lysc_pattern **
lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig)
{
struct lysc_pattern **dup = NULL;
LY_ARRAY_COUNT_TYPE u;
assert(orig);
LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_COUNT(orig), NULL);
LY_ARRAY_FOR(orig, u) {
dup[u] = lysc_pattern_dup(orig[u]);
LY_ARRAY_INCREMENT(dup);
}
return dup;
}
/**
* @brief Duplicate compiled range structure.
*
* @param[in] ctx Compile context.
* @param[in] orig The range structure to be duplicated.
* @param[in] tpdf_chain Chain of the used typedefs, traversed backwards.
* @param[in] tpdf_chain_last Index of the last (backwards) typedef in @p tpdf_chain to use.
* @return New compiled range structure as a copy of @p orig.
* @return NULL in case of memory allocation error.
*/
static struct lysc_range *
lysc_range_dup(struct lysc_ctx *ctx, const struct lysc_range *orig, struct ly_set *tpdf_chain, uint32_t tpdf_chain_last)
{
struct lysc_range *dup;
LY_ERR ret;
struct lys_type_item *tpdf_item;
uint32_t i;
assert(orig);
dup = calloc(1, sizeof *dup);
LY_CHECK_ERR_RET(!dup, LOGMEM(ctx->ctx), NULL);
if (orig->parts) {
LY_ARRAY_CREATE_GOTO(ctx->ctx, dup->parts, LY_ARRAY_COUNT(orig->parts), ret, error);
(*((LY_ARRAY_COUNT_TYPE *)(dup->parts) - 1)) = LY_ARRAY_COUNT(orig->parts);
memcpy(dup->parts, orig->parts, LY_ARRAY_COUNT(dup->parts) * sizeof *dup->parts);
}
DUP_STRING_GOTO(ctx->ctx, orig->eapptag, dup->eapptag, ret, error);
DUP_STRING_GOTO(ctx->ctx, orig->emsg, dup->emsg, ret, error);
/* collect all range extensions */
if (tpdf_chain->count > tpdf_chain_last) {
i = tpdf_chain->count;
do {
--i;
tpdf_item = tpdf_chain->objs[i];
if (!tpdf_item->tpdf->type.range) {
continue;
}
COMPILE_EXTS_GOTO(ctx, tpdf_item->tpdf->type.range->exts, dup->exts, dup, ret, error);
} while (i > tpdf_chain_last);
}
return dup;
error:
if (dup) {
lysc_range_free(ctx->ctx, dup);
free(dup);
}
return NULL;
}
/**
* @brief Print status into a string.
*
* @param[in] flags Flags with the status to print.
* @return String status.
*/
static const char *
lys_status2str(uint16_t flags)
{
flags &= LYS_STATUS_MASK;
switch (flags) {
case 0:
case LYS_STATUS_CURR:
return "current";
case LYS_STATUS_DEPRC:
return "deprecated";
case LYS_STATUS_OBSLT:
return "obsolete";
default:
LOGINT(NULL);
return NULL;
}
}
/**
* @brief Compile status information of the given statement.
*
* To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes
* has the status correctly set during the compilation.
*
* @param[in] ctx Compile context
* @param[in] parsed_flags Parsed statement flags.
* @param[in] inherited_flags Parsed inherited flags from a schema-only statement (augment, uses, ext instance, ...).
* @param[in] parent_flags Compiled parent node flags.
* @param[in] parent_name Name of the parent node, for logging.
* @param[in] stmt_name Statement name, for logging.
* @param[in,out] stmt_flags Statement flags with the correct status set.
* @return LY_ERR value.
*/
static LY_ERR
lys_compile_status(struct lysc_ctx *ctx, uint16_t parsed_flags, uint16_t inherited_flags, uint16_t parent_flags,
const char *parent_name, const char *stmt_name, uint16_t *stmt_flags)
{
/* normalize to status-only */
parsed_flags &= LYS_STATUS_MASK;
inherited_flags &= LYS_STATUS_MASK;
parent_flags &= LYS_STATUS_MASK;
/* check for conflicts */
if (parent_flags && parsed_flags && (parent_flags > parsed_flags)) {
LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Status \"%s\" of \"%s\" is in conflict with \"%s\" status of parent \"%s\".",
lys_status2str(parsed_flags), stmt_name, lys_status2str(parent_flags), parent_name);
return LY_EVALID;
} else if (inherited_flags && parsed_flags && (inherited_flags > parsed_flags)) {
LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Inherited schema-only status \"%s\" is in conflict with \"%s\" status of \"%s\".",
lys_status2str(inherited_flags), lys_status2str(parsed_flags), stmt_name);
return LY_EVALID;
} else if (parent_flags && inherited_flags && (parent_flags > inherited_flags)) {
LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Status \"%s\" of parent \"%s\" is in conflict with inherited schema-only status \"%s\".",
lys_status2str(parent_flags), parent_name, lys_status2str(inherited_flags));
return LY_EVALID;
}
/* clear */
(*stmt_flags) &= ~LYS_STATUS_MASK;
if (parsed_flags) {
/* explicit status */
(*stmt_flags) |= parsed_flags;
} else if (inherited_flags) {
/* inherited status from a schema-only statement */
(*stmt_flags) |= inherited_flags;
} else if (parent_flags) {
/* inherited status from a parent node */
(*stmt_flags) |= parent_flags;
} else {
/* default status */
(*stmt_flags) |= LYS_STATUS_CURR;
}
return LY_SUCCESS;
}
/**
* @brief Compile information from the when statement
*
* @param[in] ctx Compile context.
* @param[in] when_p Parsed when structure.
* @param[in] inherited_flags Inherited flags from a schema-only statement.
* @param[in] parent Parent node, if any.
* @param[in] ctx_node Context node for the when statement.
* @param[out] when Pointer where to store pointer to the created compiled when structure.
* @return LY_ERR value.
*/
static LY_ERR
lys_compile_when_(struct lysc_ctx *ctx, const struct lysp_when *when_p, uint16_t inherited_flags,
const struct lysc_node *parent, const struct lysc_node *ctx_node, struct lysc_when **when)
{
LY_ERR ret = LY_SUCCESS;
LY_VALUE_FORMAT format;
*when = calloc(1, sizeof **when);
LY_CHECK_ERR_RET(!(*when), LOGMEM(ctx->ctx), LY_EMEM);
(*when)->refcount = 1;
LY_CHECK_RET(lyxp_expr_parse(ctx->ctx, when_p->cond, 0, 1, &(*when)->cond));
LY_CHECK_RET(lyplg_type_prefix_data_new(ctx->ctx, when_p->cond, strlen(when_p->cond),
LY_VALUE_SCHEMA, ctx->pmod, &format, (void **)&(*when)->prefixes));
(*when)->context = (struct lysc_node *)ctx_node;
DUP_STRING_GOTO(ctx->ctx, when_p->dsc, (*when)->dsc, ret, done);
DUP_STRING_GOTO(ctx->ctx, when_p->ref, (*when)->ref, ret, done);
COMPILE_EXTS_GOTO(ctx, when_p->exts, (*when)->exts, (*when), ret, done);
LY_CHECK_RET(lys_compile_status(ctx, 0, inherited_flags, parent ? parent->flags : 0, parent ? parent->name : NULL,
"when", &(*when)->flags));
done:
return ret;
}
LY_ERR
lys_compile_when(struct lysc_ctx *ctx, const struct lysp_when *when_p, uint16_t inherited_flags, const struct lysc_node *parent,
const struct lysc_node *ctx_node, struct lysc_node *node, struct lysc_when **when_c)
{
LY_ERR rc = LY_SUCCESS;
struct lysc_when **new_when, ***node_when, *ptr;
assert(when_p && (node || when_c));
if (node) {
/* get the when array */
node_when = lysc_node_when_p(node);
/* create new when pointer */
LY_ARRAY_NEW_GOTO(ctx->ctx, *node_when, new_when, rc, cleanup);
} else {
/* individual when */
new_when = &ptr;
*new_when = calloc(1, sizeof **new_when);
LY_CHECK_ERR_GOTO(!*new_when, LOGMEM(ctx->ctx); rc = LY_EMEM, cleanup);
}
if (!when_c || !(*when_c)) {
/* compile when */
LY_CHECK_GOTO(rc = lys_compile_when_(ctx, when_p, inherited_flags, parent, ctx_node, new_when), cleanup);
/* remember the compiled when for sharing */
if (when_c) {
*when_c = *new_when;
}
} else {
/* use the previously compiled when */
++(*when_c)->refcount;
*new_when = *when_c;
}
if (node) {
/* add when to unres if there is a node for evaluation (not for extension instances) */
LY_CHECK_GOTO(rc = lysc_unres_when_add(ctx, *new_when, node), cleanup);
}
cleanup:
return rc;
}
LY_ERR
lys_compile_must(struct lysc_ctx *ctx, const struct lysp_restr *must_p, struct lysc_must *must)
{
LY_ERR ret = LY_SUCCESS;
LY_VALUE_FORMAT format;
LY_CHECK_RET(lyxp_expr_parse(ctx->ctx, must_p->arg.str, 0, 1, &must->cond));
LY_CHECK_RET(lyplg_type_prefix_data_new(ctx->ctx, must_p->arg.str, strlen(must_p->arg.str),
LY_VALUE_SCHEMA, must_p->arg.mod, &format, (void **)&must->prefixes));
DUP_STRING_GOTO(ctx->ctx, must_p->eapptag, must->eapptag, ret, done);
DUP_STRING_GOTO(ctx->ctx, must_p->emsg, must->emsg, ret, done);
DUP_STRING_GOTO(ctx->ctx, must_p->dsc, must->dsc, ret, done);
DUP_STRING_GOTO(ctx->ctx, must_p->ref, must->ref, ret, done);
COMPILE_EXTS_GOTO(ctx, must_p->exts, must->exts, must, ret, done);
done:
return ret;
}
/**
* @brief Validate and normalize numeric value from a range definition.
* @param[in] ctx Compile context.
* @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to
* allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into
* valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from
* the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100.
* @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64.
* @param[in] value String value of the range boundary.
* @param[out] len Number of the processed bytes from the value. Processing stops on the first character which is not part of the number boundary.
* @param[out] valcopy NULL-terminated string with the numeric value to parse and store.
* @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value).
*/
static LY_ERR
range_part_check_value_syntax(struct lysc_ctx *ctx, LY_DATA_TYPE basetype, uint8_t frdigits, const char *value,
size_t *len, char **valcopy)
{
size_t fraction = 0, size;
*len = 0;
assert(value);
/* parse value */
if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) {
return LY_EVALID;
}
if ((value[*len] == '-') || (value[*len] == '+')) {
++(*len);
}
while (isdigit(value[*len])) {
++(*len);
}
if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) {
if (basetype == LY_TYPE_DEC64) {
goto decimal;
} else {
*valcopy = strndup(value, *len);
return LY_SUCCESS;
}
}
fraction = *len;
++(*len);
while (isdigit(value[*len])) {
++(*len);
}
if (basetype == LY_TYPE_DEC64) {
decimal:
assert(frdigits);
if (fraction && (*len - 1 - fraction > frdigits)) {
LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
"Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.",
(int)(*len), value, frdigits);
return LY_EINVAL;
}
if (fraction) {
size = (*len) + (frdigits - ((*len) - 1 - fraction));
} else {
size = (*len) + frdigits + 1;
}
*valcopy = malloc(size * sizeof **valcopy);
LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM);
(*valcopy)[size - 1] = '\0';
if (fraction) {
memcpy(&(*valcopy)[0], &value[0], fraction);
memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction));
memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction));
} else {
memcpy(&(*valcopy)[0], &value[0], *len);
memset(&(*valcopy)[*len], '0', frdigits);
}
}
return LY_SUCCESS;
}
/**
* @brief Check that values in range are in ascendant order.
* @param[in] unsigned_value Flag to note that we are working with unsigned values.
* @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous,
* max can be also equal.
* @param[in] value Current value to check.
* @param[in] prev_value The last seen value.
* @return LY_SUCCESS or LY_EEXIST for invalid order.
*/
static LY_ERR
range_part_check_ascendancy(ly_bool unsigned_value, ly_bool max, int64_t value, int64_t prev_value)
{
if (unsigned_value) {
if ((max && ((uint64_t)prev_value > (uint64_t)value)) || (!max && ((uint64_t)prev_value >= (uint64_t)value))) {
return LY_EEXIST;
}
} else {
if ((max && (prev_value > value)) || (!max && (prev_value >= value))) {
return LY_EEXIST;
}
}
return LY_SUCCESS;
}
/**
* @brief Set min/max value of the range part.
* @param[in] ctx Compile context.
* @param[in] part Range part structure to fill.
* @param[in] max Flag to distinguish if storing min or max value.
* @param[in] prev The last seen value to check that all values in range are specified in ascendant order.
* @param[in] basetype Type of the value to get know implicit min/max values and other checking rules.
* @param[in] first Flag for the first value of the range to avoid ascendancy order.
* @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
* @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
* @param[in] base_range Range from the type from which the current type is derived (if not built-in) to get type's min and max values.
* @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set.
* @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number),
* LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the
* frdigits value), LY_EMEM.
*/
static LY_ERR
range_part_minmax(struct lysc_ctx *ctx, struct lysc_range_part *part, ly_bool max, int64_t prev, LY_DATA_TYPE basetype,
ly_bool first, ly_bool length_restr, uint8_t frdigits, struct lysc_range *base_range, const char **value)
{
LY_ERR ret = LY_SUCCESS;
char *valcopy = NULL;
size_t len = 0;
if (value) {
ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy);
LY_CHECK_GOTO(ret, finalize);
}
if (!valcopy && base_range) {
if (max) {
part->max_64 = base_range->parts[LY_ARRAY_COUNT(base_range->parts) - 1].max_64;
} else {
part->min_64 = base_range->parts[0].min_64;
}
if (!first) {
ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev);
}
goto finalize;
}
switch (basetype) {
case LY_TYPE_INT8: /* range */
if (valcopy) {
ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-128), INT64_C(127), LY_BASE_DEC, max ? &part->max_64 : &part->min_64);
} else if (max) {
part->max_64 = INT64_C(127);
} else {
part->min_64 = INT64_C(-128);
}
if (!ret && !first) {
ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
}
break;
case LY_TYPE_INT16: /* range */
if (valcopy) {
ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-32768), INT64_C(32767), LY_BASE_DEC,
max ? &part->max_64 : &part->min_64);
} else if (max) {
part->max_64 = INT64_C(32767);
} else {
part->min_64 = INT64_C(-32768);
}
if (!ret && !first) {
ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
}
break;
case LY_TYPE_INT32: /* range */
if (valcopy) {
ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-2147483648), INT64_C(2147483647), LY_BASE_DEC,
max ? &part->max_64 : &part->min_64);
} else if (max) {
part->max_64 = INT64_C(2147483647);
} else {
part->min_64 = INT64_C(-2147483648);
}
if (!ret && !first) {
ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
}
break;
case LY_TYPE_INT64: /* range */
case LY_TYPE_DEC64: /* range */
if (valcopy) {
ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807),
LY_BASE_DEC, max ? &part->max_64 : &part->min_64);
} else if (max) {
part->max_64 = INT64_C(9223372036854775807);
} else {
part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1);
}
if (!ret && !first) {
ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
}
break;
case LY_TYPE_UINT8: /* range */
if (valcopy) {
ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(255), LY_BASE_DEC, max ? &part->max_u64 : &part->min_u64);
} else if (max) {
part->max_u64 = UINT64_C(255);
} else {
part->min_u64 = UINT64_C(0);
}
if (!ret && !first) {
ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
}
break;
case LY_TYPE_UINT16: /* range */
if (valcopy) {
ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(65535), LY_BASE_DEC, max ? &part->max_u64 : &part->min_u64);
} else if (max) {
part->max_u64 = UINT64_C(65535);
} else {
part->min_u64 = UINT64_C(0);
}
if (!ret && !first) {
ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
}
break;
case LY_TYPE_UINT32: /* range */
if (valcopy) {
ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(4294967295), LY_BASE_DEC,
max ? &part->max_u64 : &part->min_u64);
} else if (max) {
part->max_u64 = UINT64_C(4294967295);
} else {
part->min_u64 = UINT64_C(0);
}
if (!ret && !first) {
ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
}
break;
case LY_TYPE_UINT64: /* range */
case LY_TYPE_STRING: /* length */
case LY_TYPE_BINARY: /* length */
if (valcopy) {
ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(18446744073709551615), LY_BASE_DEC,
max ? &part->max_u64 : &part->min_u64);
} else if (max) {
part->max_u64 = UINT64_C(18446744073709551615);
} else {
part->min_u64 = UINT64_C(0);
}
if (!ret && !first) {
ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
}
break;
default:
LOGINT(ctx->ctx);
ret = LY_EINT;
}
finalize:
if (ret == LY_EDENIED) {
LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
"Invalid %s restriction - value \"%s\" does not fit the type limitations.",
length_restr ? "length" : "range", valcopy ? valcopy : *value);
} else if (ret == LY_EVALID) {
LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
"Invalid %s restriction - invalid value \"%s\".",
length_restr ? "length" : "range", valcopy ? valcopy : *value);
} else if (ret == LY_EEXIST) {
LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
"Invalid %s restriction - values are not in ascending order (%s).",
length_restr ? "length" : "range",
(valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min");
} else if (!ret && value) {
*value = *value + len;
}
free(valcopy);
return ret;
}
LY_ERR
lys_compile_type_range(struct lysc_ctx *ctx, const struct lysp_restr *range_p, LY_DATA_TYPE basetype, ly_bool length_restr,
uint8_t frdigits, struct lysc_range *base_range, struct lysc_range **range)
{
LY_ERR ret = LY_SUCCESS;
const char *expr;
struct lysc_range_part *parts = NULL, *part;
ly_bool range_expected = 0, uns;
LY_ARRAY_COUNT_TYPE parts_done = 0, u, v;
assert(range);
assert(range_p);
expr = range_p->arg.str;
while (1) {
if (isspace(*expr)) {
++expr;
} else if (*expr == '\0') {
if (range_expected) {
LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
"Invalid %s restriction - unexpected end of the expression after \"..\" (%s).",
length_restr ? "length" : "range", range_p->arg.str);
ret = LY_EVALID;
goto cleanup;
} else if (!parts || (parts_done == LY_ARRAY_COUNT(parts))) {
LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
"Invalid %s restriction - unexpected end of the expression (%s).",
length_restr ? "length" : "range", range_p->arg.str);
ret = LY_EVALID;
goto cleanup;
}
parts_done++;
break;
} else if (!strncmp(expr, "min", ly_strlen_const("min"))) {
if (parts) {
/* min cannot be used elsewhere than in the first part */
LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
"Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range",
(int)(expr - range_p->arg.str), range_p->arg.str);
ret = LY_EVALID;
goto cleanup;
}
expr += ly_strlen_const("min");
LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
LY_CHECK_GOTO(ret = range_part_minmax(ctx, part, 0, 0, basetype, 1, length_restr, frdigits, base_range, NULL), cleanup);
part->max_64 = part->min_64;
} else if (*expr == '|') {
if (!parts || range_expected) {
LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
"Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr);
ret = LY_EVALID;
goto cleanup;
}
expr++;
parts_done++;
/* process next part of the expression */
} else if (!strncmp(expr, "..", 2)) {
expr += 2;
while (isspace(*expr)) {
expr++;
}
if (!parts || (LY_ARRAY_COUNT(parts) == parts_done)) {
LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
"Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range");
ret = LY_EVALID;
goto cleanup;
}
/* continue expecting the upper boundary */
range_expected = 1;
} else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) {
/* number */
if (range_expected) {
part = &parts[LY_ARRAY_COUNT(parts) - 1];
LY_CHECK_GOTO(ret = range_part_minmax(ctx, part, 1, part->min_64, basetype, 0, length_restr, frdigits, NULL, &expr), cleanup);
range_expected = 0;
} else {
LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
LY_CHECK_GOTO(ret = range_part_minmax(ctx, part, 0, parts_done ? parts[LY_ARRAY_COUNT(parts) - 2].max_64 : 0,
basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup);
part->max_64 = part->min_64;
}
/* continue with possible another expression part */
} else if (!strncmp(expr, "max", ly_strlen_const("max"))) {
expr += ly_strlen_const("max");
while (isspace(*expr)) {
expr++;
}
if (*expr != '\0') {
LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).",
length_restr ? "length" : "range", expr);
ret = LY_EVALID;
goto cleanup;
}
if (range_expected) {
part = &parts[LY_ARRAY_COUNT(parts) - 1];
LY_CHECK_GOTO(ret = range_part_minmax(ctx, part, 1, part->min_64, basetype, 0, length_restr, frdigits, base_range, NULL), cleanup);
range_expected = 0;
} else {
LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
LY_CHECK_GOTO(ret = range_part_minmax(ctx, part, 1, parts_done ? parts[LY_ARRAY_COUNT(parts) - 2].max_64 : 0,
basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup);
part->min_64 = part->max_64;
}
} else {
LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).",
length_restr ? "length" : "range", expr);
ret = LY_EVALID;
goto cleanup;
}
}
/* check with the previous range/length restriction */
if (base_range) {
switch (basetype) {
case LY_TYPE_BINARY:
case LY_TYPE_UINT8:
case LY_TYPE_UINT16:
case LY_TYPE_UINT32:
case LY_TYPE_UINT64:
case LY_TYPE_STRING:
uns = 1;
break;
case LY_TYPE_DEC64:
case LY_TYPE_INT8:
case LY_TYPE_INT16:
case LY_TYPE_INT32:
case LY_TYPE_INT64:
uns = 0;
break;
default:
LOGINT(ctx->ctx);
ret = LY_EINT;
goto cleanup;
}
for (u = v = 0; u < parts_done && v < LY_ARRAY_COUNT(base_range->parts); ++u) {
if ((uns && (parts[u].min_u64 < base_range->parts[v].min_u64)) || (!uns && (parts[u].min_64 < base_range->parts[v].min_64))) {
goto baseerror;
}
/* current lower bound is not lower than the base */
if (base_range->parts[v].min_64 == base_range->parts[v].max_64) {
/* base has single value */
if (base_range->parts[v].min_64 == parts[u].min_64) {
/* both lower bounds are the same */