Moving in e2sim originally from it/test/simulators
[sim/e2-interface.git] / e2sim / ASN1c / asn_bit_data.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 ASN_BIT_DATA
24 #define ASN_BIT_DATA
25
26 #include <asn_system.h>         /* Platform-specific types */
27
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31
32 /*
33  * This structure describes a position inside an incoming PER bit stream.
34  */
35 typedef struct asn_bit_data_s {
36   const uint8_t *buffer;  /* Pointer to the octet stream */
37          size_t  nboff;   /* Bit offset to the meaningful bit */
38          size_t  nbits;   /* Number of bits in the stream */
39          size_t  moved;   /* Number of bits moved through this bit stream */
40   int (*refill)(struct asn_bit_data_s *);
41   void *refill_key;
42 } asn_bit_data_t;
43
44 /*
45  * Create a contiguous non-refillable bit data structure.
46  * Can be freed by FREEMEM().
47  */
48 asn_bit_data_t *asn_bit_data_new_contiguous(const void *data, size_t size_bits);
49
50 /*
51  * Extract a small number of bits (<= 31) from the specified PER data pointer.
52  * This function returns -1 if the specified number of bits could not be
53  * extracted due to EOD or other conditions.
54  */
55 int32_t asn_get_few_bits(asn_bit_data_t *, int get_nbits);
56
57 /* Undo the immediately preceeding "get_few_bits" operation */
58 void asn_get_undo(asn_bit_data_t *, int get_nbits);
59
60 /*
61  * Extract a large number of bits from the specified PER data pointer.
62  * This function returns -1 if the specified number of bits could not be
63  * extracted due to EOD or other conditions.
64  */
65 int asn_get_many_bits(asn_bit_data_t *, uint8_t *dst, int right_align,
66                         int get_nbits);
67
68 /* Non-thread-safe debugging function, don't use it */
69 char *asn_bit_data_string(asn_bit_data_t *);
70
71 /*
72  * This structure supports forming bit output.
73  */
74 typedef struct asn_bit_outp_s {
75         uint8_t *buffer;        /* Pointer into the (tmpspace) */
76         size_t nboff;           /* Bit offset to the meaningful bit */
77         size_t nbits;           /* Number of bits left in (tmpspace) */
78         uint8_t tmpspace[32];   /* Preliminary storage to hold data */
79         int (*output)(const void *data, size_t size, void *op_key);
80         void *op_key;           /* Key for (output) data callback */
81         size_t flushed_bytes;   /* Bytes already flushed through (output) */
82 } asn_bit_outp_t;
83
84 /* Output a small number of bits (<= 31) */
85 int asn_put_few_bits(asn_bit_outp_t *, uint32_t bits, int obits);
86
87 /* Output a large number of bits */
88 int asn_put_many_bits(asn_bit_outp_t *, const uint8_t *src, int put_nbits);
89
90 /*
91  * Flush whole bytes (0 or more) through (outper) member.
92  * The least significant bits which are not used are guaranteed to be set to 0.
93  * Returns -1 if callback returns -1. Otherwise, 0.
94  */
95 int asn_put_aligned_flush(asn_bit_outp_t *);
96
97 #ifdef __cplusplus
98 }
99 #endif
100
101 #endif  /* ASN_BIT_DATA */