3 * Copyright (c) 2005-2017 Lev Walkin <vlm@lionet.info>. All rights reserved.
4 * Redistribution and modifications are permitted subject to BSD license.
9 #include <asn_system.h> /* Platform-specific types */
16 * This structure describes a position inside an incoming PER bit stream.
18 typedef struct asn_bit_data_s {
19 const uint8_t *buffer; /* Pointer to the octet stream */
20 size_t nboff; /* Bit offset to the meaningful bit */
21 size_t nbits; /* Number of bits in the stream */
22 size_t moved; /* Number of bits moved through this bit stream */
23 int (*refill)(struct asn_bit_data_s *);
28 * Create a contiguous non-refillable bit data structure.
29 * Can be freed by FREEMEM().
31 asn_bit_data_t *asn_bit_data_new_contiguous(const void *data, size_t size_bits);
34 * Extract a small number of bits (<= 31) from the specified PER data pointer.
35 * This function returns -1 if the specified number of bits could not be
36 * extracted due to EOD or other conditions.
38 int32_t asn_get_few_bits(asn_bit_data_t *, int get_nbits);
40 /* Undo the immediately preceeding "get_few_bits" operation */
41 void asn_get_undo(asn_bit_data_t *, int get_nbits);
44 * Extract a large number of bits from the specified PER data pointer.
45 * This function returns -1 if the specified number of bits could not be
46 * extracted due to EOD or other conditions.
48 int asn_get_many_bits(asn_bit_data_t *, uint8_t *dst, int right_align,
51 /* Non-thread-safe debugging function, don't use it */
52 char *asn_bit_data_string(asn_bit_data_t *);
55 * This structure supports forming bit output.
57 typedef struct asn_bit_outp_s {
58 uint8_t *buffer; /* Pointer into the (tmpspace) */
59 size_t nboff; /* Bit offset to the meaningful bit */
60 size_t nbits; /* Number of bits left in (tmpspace) */
61 uint8_t tmpspace[32]; /* Preliminary storage to hold data */
62 int (*output)(const void *data, size_t size, void *op_key);
63 void *op_key; /* Key for (output) data callback */
64 size_t flushed_bytes; /* Bytes already flushed through (output) */
67 /* Output a small number of bits (<= 31) */
68 int asn_put_few_bits(asn_bit_outp_t *, uint32_t bits, int obits);
70 /* Output a large number of bits */
71 int asn_put_many_bits(asn_bit_outp_t *, const uint8_t *src, int put_nbits);
74 * Flush whole bytes (0 or more) through (outper) member.
75 * The least significant bits which are not used are guaranteed to be set to 0.
76 * Returns -1 if callback returns -1. Otherwise, 0.
78 int asn_put_aligned_flush(asn_bit_outp_t *);
84 #endif /* ASN_BIT_DATA */