1 /*****************************************************************************
3 # Copyright 2019 AT&T Intellectual Property *
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 *
9 # http://www.apache.org/licenses/LICENSE-2.0 *
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. *
17 ******************************************************************************/
20 * Copyright (c) 2005-2017 Lev Walkin <vlm@lionet.info>. All rights reserved.
21 * Redistribution and modifications are permitted subject to BSD license.
26 #include <asn_system.h> /* Platform-specific types */
33 * This structure describes a position inside an incoming PER bit stream.
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 *);
45 * Create a contiguous non-refillable bit data structure.
46 * Can be freed by FREEMEM().
48 asn_bit_data_t *asn_bit_data_new_contiguous(const void *data, size_t size_bits);
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.
55 int32_t asn_get_few_bits(asn_bit_data_t *, int get_nbits);
57 /* Undo the immediately preceeding "get_few_bits" operation */
58 void asn_get_undo(asn_bit_data_t *, int get_nbits);
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.
65 int asn_get_many_bits(asn_bit_data_t *, uint8_t *dst, int right_align,
68 /* Non-thread-safe debugging function, don't use it */
69 char *asn_bit_data_string(asn_bit_data_t *);
72 * This structure supports forming bit output.
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) */
84 /* Output a small number of bits (<= 31) */
85 int asn_put_few_bits(asn_bit_outp_t *, uint32_t bits, int obits);
87 /* Output a large number of bits */
88 int asn_put_many_bits(asn_bit_outp_t *, const uint8_t *src, int put_nbits);
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.
95 int asn_put_aligned_flush(asn_bit_outp_t *);
101 #endif /* ASN_BIT_DATA */