Moving in e2sim originally from it/test/simulators
[sim/e2-interface.git] / e2sim / ASN1c / per_support.h
1 /*****************************************************************************
2 #                                                                            *
3 # Copyright 2019 AT&T Intellectual Property                                  *
4 #                                                                            *
5 # Licensed under the Apache License, Version 2.0 (the "License");            *
6 # you may not use this file except in compliance with the License.           *
7 # You may obtain a copy of the License at                                    *
8 #                                                                            *
9 #      http://www.apache.org/licenses/LICENSE-2.0                            *
10 #                                                                            *
11 # Unless required by applicable law or agreed to in writing, software        *
12 # distributed under the License is distributed on an "AS IS" BASIS,          *
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   *
14 # See the License for the specific language governing permissions and        *
15 # limitations under the License.                                             *
16 #                                                                            *
17 ******************************************************************************/
18
19 /*
20  * Copyright (c) 2005-2017 Lev Walkin <vlm@lionet.info>. All rights reserved.
21  * Redistribution and modifications are permitted subject to BSD license.
22  */
23 #ifndef _PER_SUPPORT_H_
24 #define _PER_SUPPORT_H_
25
26 #include <asn_system.h>         /* Platform-specific types */
27 #include <asn_bit_data.h>
28
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32
33 /*
34  * Pre-computed PER constraints.
35  */
36 typedef struct asn_per_constraint_s {
37         enum asn_per_constraint_flags {
38                 APC_UNCONSTRAINED       = 0x0,  /* No PER visible constraints */
39                 APC_SEMI_CONSTRAINED    = 0x1,  /* Constrained at "lb" */
40                 APC_CONSTRAINED         = 0x2,  /* Fully constrained */
41                 APC_EXTENSIBLE          = 0x4   /* May have extension */
42         } flags;
43         int  range_bits;                /* Full number of bits in the range */
44         int  effective_bits;            /* Effective bits */
45         long lower_bound;               /* "lb" value */
46         long upper_bound;               /* "ub" value */
47 } asn_per_constraint_t;
48 typedef struct asn_per_constraints_s {
49         asn_per_constraint_t value;
50         asn_per_constraint_t size;
51         int (*value2code)(unsigned int value);
52         int (*code2value)(unsigned int code);
53 } asn_per_constraints_t;
54
55 /* Temporary compatibility layer. Will get removed. */
56 typedef struct asn_bit_data_s asn_per_data_t;
57 #define per_get_few_bits(data, bits)   asn_get_few_bits(data, bits)
58 #define per_get_undo(data, bits)   asn_get_undo(data, bits)
59 #define per_get_many_bits(data, dst, align, bits) \
60     asn_get_many_bits(data, dst, align, bits)
61
62 /*
63  * X.691 (08/2015) #11.9 "General rules for encoding a length determinant"
64  * Get the length "n" from the Unaligned PER stream.
65  */
66 ssize_t uper_get_length(asn_per_data_t *pd, int effective_bound_bits,
67                         size_t lower_bound, int *repeat);
68
69 ssize_t aper_get_length(asn_per_data_t *pd, int range,
70                         int effective_bound_bits, int *repeat);
71
72 /*
73  * Get the normally small length "n".
74  */
75 ssize_t uper_get_nslength(asn_per_data_t *pd);
76 ssize_t aper_get_nslength(asn_per_data_t *pd);
77
78 /*
79  * Get the normally small non-negative whole number.
80  */
81 ssize_t uper_get_nsnnwn(asn_per_data_t *pd);
82 ssize_t aper_get_nsnnwn(asn_per_data_t *pd, int range);
83
84 /* X.691-2008/11, #11.5.6 */
85 int uper_get_constrained_whole_number(asn_per_data_t *pd, unsigned long *v, int nbits);
86
87
88 /* Temporary compatibility layer. Will get removed. */
89 typedef struct asn_bit_outp_s asn_per_outp_t;
90 #define per_put_few_bits(out, bits, obits) asn_put_few_bits(out, bits, obits)
91 #define per_put_many_bits(out, src, nbits) asn_put_many_bits(out, src, nbits)
92 #define per_put_aligned_flush(out) asn_put_aligned_flush(out)
93
94
95 /*
96  * Rebase the given value as an offset into the range specified by the
97  * lower bound (lb) and upper bound (ub).
98  * RETURN VALUES:
99  *  -1: Conversion failed due to range problems.
100  *   0: Conversion was successful.
101  */
102 int per_long_range_rebase(long v, long lb, long ub, unsigned long *output);
103 /* The inverse operation: restores the value by the offset and its bounds. */
104 int per_long_range_unrebase(unsigned long inp, long lb, long ub, long *outp);
105
106 /* X.691-2008/11, #11.5 */
107 int uper_put_constrained_whole_number_u(asn_per_outp_t *po, unsigned long v, int nbits);
108
109 /*
110  * X.691 (08/2015) #11.9 "General rules for encoding a length determinant"
111  * Put the length "whole_length" to the Unaligned PER stream.
112  * If (opt_need_eom) is given, it will be set to 1 if final 0-length is needed.
113  * In that case, invoke uper_put_length(po, 0, 0) after encoding the last block.
114  * This function returns the number of units which may be flushed
115  * in the next units saving iteration.
116  */
117 ssize_t uper_put_length(asn_per_outp_t *po, size_t whole_length,
118                         int *opt_need_eom);
119
120 ssize_t aper_put_length(asn_per_outp_t *po, int range, size_t length);
121
122 /* Align the current bit position to octet bundary */
123 int aper_put_align(asn_per_outp_t *po);
124 int32_t aper_get_align(asn_per_data_t *pd);
125
126 /*
127  * Put the normally small length "n" to the Unaligned PER stream.
128  * Returns 0 or -1.
129  */
130 int uper_put_nslength(asn_per_outp_t *po, size_t length);
131
132 int aper_put_nslength(asn_per_outp_t *po, size_t length);
133
134 /*
135  * Put the normally small non-negative whole number.
136  */
137 int uper_put_nsnnwn(asn_per_outp_t *po, int n);
138
139 int aper_put_nsnnwn(asn_per_outp_t *po, int range, int number);
140
141 #ifdef __cplusplus
142 }
143 #endif
144
145 #endif  /* _PER_SUPPORT_H_ */