f0309b0d2d6f18dfd718d1b65c2213432f996eb1
[sim/e2-interface.git] / e2sim / asn1c / NativeInteger.c
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     NativeInteger_free,
24 #if !defined(ASN_DISABLE_PRINT_SUPPORT)
25     NativeInteger_print,
26 #else
27     0,
28 #endif  /* !defined(ASN_DISABLE_PRINT_SUPPORT) */
29     NativeInteger_compare,
30 #if !defined(ASN_DISABLE_BER_SUPPORT)
31     NativeInteger_decode_ber,
32     NativeInteger_encode_der,
33 #else
34     0,
35     0,
36 #endif  /* !defined(ASN_DISABLE_BER_SUPPORT) */
37 #if !defined(ASN_DISABLE_XER_SUPPORT)
38     NativeInteger_decode_xer,
39     NativeInteger_encode_xer,
40 #else
41     0,
42     0,
43 #endif  /* !defined(ASN_DISABLE_XER_SUPPORT) */
44 #if !defined(ASN_DISABLE_JER_SUPPORT)
45     NativeInteger_encode_jer,
46 #else
47     0,
48 #endif  /* !defined(ASN_DISABLE_JER_SUPPORT) */
49 #if !defined(ASN_DISABLE_OER_SUPPORT)
50     NativeInteger_decode_oer,  /* OER decoder */
51     NativeInteger_encode_oer,  /* Canonical OER encoder */
52 #else
53     0,
54     0,
55 #endif  /* !defined(ASN_DISABLE_OER_SUPPORT) */
56 #if !defined(ASN_DISABLE_UPER_SUPPORT)
57     NativeInteger_decode_uper,  /* Unaligned PER decoder */
58     NativeInteger_encode_uper,  /* Unaligned PER encoder */
59 #else
60     0,
61     0,
62 #endif  /* !defined(ASN_DISABLE_UPER_SUPPORT) */
63 #if !defined(ASN_DISABLE_APER_SUPPORT)
64     NativeInteger_decode_aper,  /* Aligned PER decoder */
65     NativeInteger_encode_aper,  /* Aligned PER encoder */
66 #else
67     0,
68     0,
69 #endif  /* !defined(ASN_DISABLE_APER_SUPPORT) */
70 #if !defined(ASN_DISABLE_RFILL_SUPPORT)
71     NativeInteger_random_fill,
72 #else
73     0,
74 #endif  /* !defined(ASN_DISABLE_RFILL_SUPPORT) */
75     0  /* Use generic outmost tag fetcher */
76 };
77 asn_TYPE_descriptor_t asn_DEF_NativeInteger = {
78     "INTEGER",  /* The ASN.1 type is still INTEGER */
79     "INTEGER",
80     &asn_OP_NativeInteger,
81     asn_DEF_NativeInteger_tags,
82     sizeof(asn_DEF_NativeInteger_tags) / sizeof(asn_DEF_NativeInteger_tags[0]),
83     asn_DEF_NativeInteger_tags,  /* Same as above */
84     sizeof(asn_DEF_NativeInteger_tags) / sizeof(asn_DEF_NativeInteger_tags[0]),
85     {
86 #if !defined(ASN_DISABLE_OER_SUPPORT)
87         0,
88 #endif  /* !defined(ASN_DISABLE_OER_SUPPORT) */
89 #if !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT)
90         0,
91 #endif  /* !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT) */
92         asn_generic_no_constraint
93     },
94     0, 0,  /* No members */
95     0  /* No specifics */
96 };
97
98 void
99 NativeInteger_free(const asn_TYPE_descriptor_t *td, void *ptr,
100                    enum asn_struct_free_method method) {
101     if(!td || !ptr)
102                 return;
103
104         ASN_DEBUG("Freeing %s as INTEGER (%d, %p, Native)",
105                 td->name, method, ptr);
106
107     switch(method) {
108     case ASFM_FREE_EVERYTHING:
109         FREEMEM(ptr);
110         break;
111     case ASFM_FREE_UNDERLYING:
112         break;
113     case ASFM_FREE_UNDERLYING_AND_RESET:
114         memset(ptr, 0, sizeof(long));
115         break;
116     }
117 }
118
119 int
120 NativeInteger_compare(const asn_TYPE_descriptor_t *td, const void *aptr, const void *bptr) {
121     (void)td;
122
123     if(aptr && bptr) {
124         const asn_INTEGER_specifics_t *specs =
125             (const asn_INTEGER_specifics_t *)td->specifics;
126         if(specs && specs->field_unsigned) {
127             const unsigned long *a = aptr;
128             const unsigned long *b = bptr;
129             if(*a < *b) {
130                 return -1;
131             } else if(*a > *b) {
132                 return 1;
133             } else {
134                 return 0;
135             }
136         } else {
137             const long *a = aptr;
138             const long *b = bptr;
139             if(*a < *b) {
140                 return -1;
141             } else if(*a > *b) {
142                 return 1;
143             } else {
144                 return 0;
145             }
146         }
147     } else if(!aptr) {
148         return -1;
149     } else {
150         return 1;
151     }
152 }