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