/src/open5gs/lib/asn1c/common/NativeInteger.c
Line | Count | Source |
1 | | /*- |
2 | | * Copyright (c) 2004, 2005, 2006 Lev Walkin <vlm@lionet.info>. |
3 | | * All rights reserved. |
4 | | * Redistribution and modifications are permitted subject to BSD license. |
5 | | */ |
6 | | /* |
7 | | * Read the NativeInteger.h for the explanation wrt. differences between |
8 | | * INTEGER and NativeInteger. |
9 | | * Basically, both are decoders and encoders of ASN.1 INTEGER type, but this |
10 | | * implementation deals with the standard (machine-specific) representation |
11 | | * of them instead of using the platform-independent buffer. |
12 | | */ |
13 | | #include <asn_internal.h> |
14 | | #include <NativeInteger.h> |
15 | | |
16 | | /* |
17 | | * NativeInteger basic type description. |
18 | | */ |
19 | | static const ber_tlv_tag_t asn_DEF_NativeInteger_tags[] = { |
20 | | (ASN_TAG_CLASS_UNIVERSAL | (2 << 2)) |
21 | | }; |
22 | | asn_TYPE_operation_t asn_OP_NativeInteger = { |
23 | | .kind = ASN_KIND_PRIMITIVE, |
24 | | NativeInteger_free, |
25 | | #if !defined(ASN_DISABLE_PRINT_SUPPORT) |
26 | | NativeInteger_print, |
27 | | #else |
28 | | 0, |
29 | | #endif /* !defined(ASN_DISABLE_PRINT_SUPPORT) */ |
30 | | NativeInteger_compare, |
31 | | NativeInteger_copy, |
32 | | #if !defined(ASN_DISABLE_BER_SUPPORT) |
33 | | NativeInteger_decode_ber, |
34 | | NativeInteger_encode_der, |
35 | | #else |
36 | | 0, |
37 | | 0, |
38 | | #endif /* !defined(ASN_DISABLE_BER_SUPPORT) */ |
39 | | #if !defined(ASN_DISABLE_XER_SUPPORT) |
40 | | NativeInteger_decode_xer, |
41 | | NativeInteger_encode_xer, |
42 | | #else |
43 | | 0, |
44 | | 0, |
45 | | #endif /* !defined(ASN_DISABLE_XER_SUPPORT) */ |
46 | | #if !defined(ASN_DISABLE_JER_SUPPORT) |
47 | | NativeInteger_decode_jer, |
48 | | NativeInteger_encode_jer, |
49 | | #else |
50 | | 0, |
51 | | 0, |
52 | | #endif /* !defined(ASN_DISABLE_JER_SUPPORT) */ |
53 | | #if !defined(ASN_DISABLE_OER_SUPPORT) |
54 | | NativeInteger_decode_oer, /* OER decoder */ |
55 | | NativeInteger_encode_oer, /* Canonical OER encoder */ |
56 | | #else |
57 | | 0, |
58 | | 0, |
59 | | #endif /* !defined(ASN_DISABLE_OER_SUPPORT) */ |
60 | | #if !defined(ASN_DISABLE_UPER_SUPPORT) |
61 | | NativeInteger_decode_uper, /* Unaligned PER decoder */ |
62 | | NativeInteger_encode_uper, /* Unaligned PER encoder */ |
63 | | #else |
64 | | 0, |
65 | | 0, |
66 | | #endif /* !defined(ASN_DISABLE_UPER_SUPPORT) */ |
67 | | #if !defined(ASN_DISABLE_APER_SUPPORT) |
68 | | NativeInteger_decode_aper, /* Aligned PER decoder */ |
69 | | NativeInteger_encode_aper, /* Aligned PER encoder */ |
70 | | #else |
71 | | 0, |
72 | | 0, |
73 | | #endif /* !defined(ASN_DISABLE_APER_SUPPORT) */ |
74 | | #if !defined(ASN_DISABLE_RFILL_SUPPORT) |
75 | | NativeInteger_random_fill, |
76 | | #else |
77 | | 0, |
78 | | #endif /* !defined(ASN_DISABLE_RFILL_SUPPORT) */ |
79 | | 0 /* Use generic outmost tag fetcher */, |
80 | | #if !defined(ASN_DISABLE_CBOR_SUPPORT) |
81 | | NativeInteger_decode_cbor, |
82 | | NativeInteger_encode_cbor, |
83 | | #else |
84 | | 0, |
85 | | 0, |
86 | | #endif /* !defined(ASN_DISABLE_CBOR_SUPPORT) */ |
87 | | }; |
88 | | asn_TYPE_descriptor_t asn_DEF_NativeInteger = { |
89 | | "INTEGER", /* The ASN.1 type is still INTEGER */ |
90 | | "INTEGER", |
91 | | &asn_OP_NativeInteger, |
92 | | asn_DEF_NativeInteger_tags, |
93 | | sizeof(asn_DEF_NativeInteger_tags) / sizeof(asn_DEF_NativeInteger_tags[0]), |
94 | | asn_DEF_NativeInteger_tags, /* Same as above */ |
95 | | sizeof(asn_DEF_NativeInteger_tags) / sizeof(asn_DEF_NativeInteger_tags[0]), |
96 | | { |
97 | | #if !defined(ASN_DISABLE_OER_SUPPORT) |
98 | | 0, |
99 | | #endif /* !defined(ASN_DISABLE_OER_SUPPORT) */ |
100 | | #if !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT) |
101 | | 0, |
102 | | #endif /* !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT) */ |
103 | | #if !defined(ASN_DISABLE_JER_SUPPORT) |
104 | | 0, |
105 | | #endif /* !defined(ASN_DISABLE_JER_SUPPORT) */ |
106 | | asn_generic_no_constraint |
107 | | }, |
108 | | 0, 0, /* No members */ |
109 | | 0 /* No specifics */ |
110 | | }; |
111 | | |
112 | | void |
113 | | NativeInteger_free(const asn_TYPE_descriptor_t *td, void *ptr, |
114 | 305k | enum asn_struct_free_method method) { |
115 | 305k | if(!td || !ptr) |
116 | 0 | return; |
117 | | |
118 | 305k | ASN_DEBUG("Freeing %s as INTEGER (%d, %p, Native)", |
119 | 305k | td->name, method, ptr); |
120 | | |
121 | 305k | switch(method) { |
122 | 8.66k | case ASFM_FREE_EVERYTHING: |
123 | 8.66k | FREEMEM(ptr); |
124 | 8.66k | break; |
125 | 296k | case ASFM_FREE_UNDERLYING: |
126 | 296k | break; |
127 | 0 | case ASFM_FREE_UNDERLYING_AND_RESET: |
128 | 0 | memset(ptr, 0, sizeof(long)); |
129 | 0 | break; |
130 | 305k | } |
131 | 305k | } |
132 | | |
133 | | int |
134 | 1.24M | NativeInteger_compare(const asn_TYPE_descriptor_t *td, const void *aptr, const void *bptr) { |
135 | 1.24M | (void)td; |
136 | | |
137 | 1.24M | if(aptr && bptr) { |
138 | 1.24M | const asn_INTEGER_specifics_t *specs = |
139 | 1.24M | (const asn_INTEGER_specifics_t *)td->specifics; |
140 | 1.24M | if(specs && specs->field_unsigned) { |
141 | 0 | const unsigned long *a = aptr; |
142 | 0 | const unsigned long *b = bptr; |
143 | 0 | if(*a < *b) { |
144 | 0 | return -1; |
145 | 0 | } else if(*a > *b) { |
146 | 0 | return 1; |
147 | 0 | } else { |
148 | 0 | return 0; |
149 | 0 | } |
150 | 1.24M | } else { |
151 | 1.24M | const long *a = aptr; |
152 | 1.24M | const long *b = bptr; |
153 | 1.24M | if(*a < *b) { |
154 | 495k | return -1; |
155 | 749k | } else if(*a > *b) { |
156 | 646k | return 1; |
157 | 646k | } else { |
158 | 103k | return 0; |
159 | 103k | } |
160 | 1.24M | } |
161 | 1.24M | } else if(!aptr) { |
162 | 0 | return -1; |
163 | 0 | } else { |
164 | 0 | return 1; |
165 | 0 | } |
166 | 1.24M | } |
167 | | |
168 | | int |
169 | 0 | NativeInteger_copy(const asn_TYPE_descriptor_t *td, void **aptr, const void *bptr) { |
170 | 0 | unsigned long *a = *aptr; |
171 | 0 | const unsigned long *b = bptr; |
172 | |
|
173 | 0 | (void)td; |
174 | | |
175 | | /* Check if source has data */ |
176 | 0 | if(!b) { |
177 | | /* Clear destination */ |
178 | 0 | if(a) { |
179 | 0 | FREEMEM(a); |
180 | 0 | *aptr = 0; |
181 | 0 | } |
182 | 0 | return 0; |
183 | 0 | } |
184 | | |
185 | 0 | if(!a) { |
186 | 0 | a = *aptr = MALLOC(sizeof(*a)); |
187 | 0 | if(!a) return -1; |
188 | 0 | } |
189 | | |
190 | 0 | *a = *b; |
191 | |
|
192 | 0 | return 0; |
193 | 0 | } |