/src/open5gs/lib/asn1c/common/INTEGER_rfill.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2017 Lev Walkin <vlm@lionet.info>. |
3 | | * All rights reserved. |
4 | | * Redistribution and modifications are permitted subject to BSD license. |
5 | | */ |
6 | | #include <asn_internal.h> |
7 | | #include <INTEGER.h> |
8 | | |
9 | | asn_random_fill_result_t |
10 | | INTEGER_random_fill(const asn_TYPE_descriptor_t *td, void **sptr, |
11 | | const asn_encoding_constraints_t *constraints, |
12 | 0 | size_t max_length) { |
13 | 0 | const asn_INTEGER_specifics_t *specs = |
14 | 0 | (const asn_INTEGER_specifics_t *)td->specifics; |
15 | 0 | asn_random_fill_result_t result_ok = {ARFILL_OK, 1}; |
16 | 0 | asn_random_fill_result_t result_failed = {ARFILL_FAILED, 0}; |
17 | 0 | asn_random_fill_result_t result_skipped = {ARFILL_SKIPPED, 0}; |
18 | 0 | INTEGER_t *st = *sptr; |
19 | 0 | const asn_INTEGER_enum_map_t *emap; |
20 | 0 | size_t emap_len; |
21 | 0 | intmax_t value; |
22 | 0 | int find_inside_map; |
23 | |
|
24 | 0 | if(max_length == 0) return result_skipped; |
25 | | |
26 | 0 | if(st == NULL) { |
27 | 0 | st = (INTEGER_t *)CALLOC(1, sizeof(*st)); |
28 | 0 | if(st == NULL) { |
29 | 0 | return result_failed; |
30 | 0 | } |
31 | 0 | } |
32 | | |
33 | 0 | if(specs) { |
34 | 0 | emap = specs->value2enum; |
35 | 0 | emap_len = specs->map_count; |
36 | 0 | if(specs->strict_enumeration) { |
37 | 0 | find_inside_map = emap_len > 0; |
38 | 0 | } else { |
39 | 0 | find_inside_map = emap_len ? asn_random_between(0, 1) : 0; |
40 | 0 | } |
41 | 0 | } else { |
42 | 0 | emap = 0; |
43 | 0 | emap_len = 0; |
44 | 0 | find_inside_map = 0; |
45 | 0 | } |
46 | |
|
47 | 0 | if(find_inside_map) { |
48 | 0 | assert(emap_len > 0); |
49 | 0 | value = emap[asn_random_between(0, emap_len - 1)].nat_value; |
50 | 0 | } else { |
51 | 0 | static const long variants[] = { |
52 | 0 | -65536, -65535, -65534, -32769, -32768, -32767, -16385, -16384, |
53 | 0 | -16383, -257, -256, -255, -254, -129, -128, -127, |
54 | 0 | -126, -1, 0, 1, 126, 127, 128, 129, |
55 | 0 | 254, 255, 256, 257, 16383, 16384, 16385, 32767, |
56 | 0 | 32768, 32769, 65534, 65535, 65536, 65537}; |
57 | 0 | if(specs && specs->field_unsigned) { |
58 | 0 | assert(variants[18] == 0); |
59 | 0 | value = variants[asn_random_between( |
60 | 0 | 18, sizeof(variants) / sizeof(variants[0]) - 1)]; |
61 | 0 | } else { |
62 | 0 | value = variants[asn_random_between( |
63 | 0 | 0, sizeof(variants) / sizeof(variants[0]) - 1)]; |
64 | 0 | } |
65 | |
|
66 | 0 | if(!constraints) constraints = &td->encoding_constraints; |
67 | 0 | #if !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT) |
68 | 0 | const asn_per_constraints_t *ct; |
69 | |
|
70 | 0 | ct = constraints ? constraints->per_constraints : 0; |
71 | 0 | if(ct && (ct->value.flags & APC_CONSTRAINED)) { |
72 | 0 | if(value < ct->value.lower_bound || value > ct->value.upper_bound) { |
73 | 0 | value = asn_random_between(ct->value.lower_bound, |
74 | 0 | ct->value.upper_bound); |
75 | 0 | } |
76 | 0 | } |
77 | 0 | #endif /* !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT) */ |
78 | 0 | } |
79 | |
|
80 | 0 | if(asn_imax2INTEGER(st, value)) { |
81 | 0 | if(st == *sptr) { |
82 | 0 | ASN_STRUCT_RESET(*td, st); |
83 | 0 | } else { |
84 | 0 | ASN_STRUCT_FREE(*td, st); |
85 | 0 | } |
86 | 0 | return result_failed; |
87 | 0 | } else { |
88 | 0 | *sptr = st; |
89 | 0 | result_ok.length = st->size; |
90 | 0 | return result_ok; |
91 | 0 | } |
92 | 0 | } |