blob: 750522b4d24503f937a81a791d60bf0e033d41df [file] [log] [blame]
Lev Walkin6cd0d562017-08-25 11:57:01 -07001/*
2 * Copyright (c) 2005-2017 Lev Walkin <vlm@lionet.info>.
3 * All rights reserved.
4 * Redistribution and modifications are permitted subject to BSD license.
5 */
6#ifndef ASN_BIT_DATA
7#define ASN_BIT_DATA
8
9#include <asn_system.h> /* Platform-specific types */
10
11#ifdef __cplusplus
12extern "C" {
13#endif
14
15/*
16 * This structure describes a position inside an incoming PER bit stream.
17 */
18typedef 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 *);
24 void *refill_key;
25} asn_bit_data_t;
26
27/*
28 * Extract a small number of bits (<= 31) from the specified PER data pointer.
29 * This function returns -1 if the specified number of bits could not be
30 * extracted due to EOD or other conditions.
31 */
32int32_t asn_get_few_bits(asn_bit_data_t *, int get_nbits);
33
34/* Undo the immediately preceeding "get_few_bits" operation */
35void asn_get_undo(asn_bit_data_t *, int get_nbits);
36
37/*
38 * Extract a large number of bits from the specified PER data pointer.
39 * This function returns -1 if the specified number of bits could not be
40 * extracted due to EOD or other conditions.
41 */
42int asn_get_many_bits(asn_bit_data_t *, uint8_t *dst, int right_align,
43 int get_nbits);
44
45/* Non-thread-safe debugging function, don't use it */
46char *asn_bit_data_string(asn_bit_data_t *);
47
48/*
49 * This structure supports forming bit output.
50 */
51typedef struct asn_bit_outp_s {
52 uint8_t *buffer; /* Pointer into the (tmpspace) */
53 size_t nboff; /* Bit offset to the meaningful bit */
54 size_t nbits; /* Number of bits left in (tmpspace) */
55 uint8_t tmpspace[32]; /* Preliminary storage to hold data */
56 int (*output)(const void *data, size_t size, void *op_key);
57 void *op_key; /* Key for (output) data callback */
58 size_t flushed_bytes; /* Bytes already flushed through (output) */
59} asn_bit_outp_t;
60
61/* Output a small number of bits (<= 31) */
62int asn_put_few_bits(asn_bit_outp_t *, uint32_t bits, int obits);
63
64/* Output a large number of bits */
65int asn_put_many_bits(asn_bit_outp_t *, const uint8_t *src, int put_nbits);
66
67/*
68 * Flush whole bytes (0 or more) through (outper) member.
69 * The least significant bits which are not used are guaranteed to be set to 0.
70 * Returns -1 if callback returns -1. Otherwise, 0.
71 */
72int asn_put_aligned_flush(asn_bit_outp_t *);
73
74#ifdef __cplusplus
75}
76#endif
77
78#endif /* ASN_BIT_DATA */