vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 1 | /* |
vlm | 1fcf759 | 2006-08-18 02:27:55 +0000 | [diff] [blame] | 2 | * Copyright (c) 2005, 2006 Lev Walkin <vlm@lionet.info>. All rights reserved. |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 3 | * Redistribution and modifications are permitted subject to BSD license. |
| 4 | */ |
| 5 | #ifndef _PER_SUPPORT_H_ |
| 6 | #define _PER_SUPPORT_H_ |
| 7 | |
| 8 | #include <asn_system.h> /* Platform-specific types */ |
| 9 | |
| 10 | #ifdef __cplusplus |
| 11 | extern "C" { |
| 12 | #endif |
| 13 | |
| 14 | /* |
vlm | 1fcf759 | 2006-08-18 02:27:55 +0000 | [diff] [blame] | 15 | * Pre-computed PER constraints. |
| 16 | */ |
| 17 | typedef struct asn_per_constraint_s { |
| 18 | enum asn_per_constraint_flags { |
| 19 | APC_UNCONSTRAINED = 0x0, /* No PER visible constraints */ |
| 20 | APC_SEMI_CONSTRAINED = 0x1, /* Constrained at "lb" */ |
| 21 | APC_CONSTRAINED = 0x2, /* Fully constrained */ |
| 22 | APC_EXTENSIBLE = 0x4 /* May have extension */ |
| 23 | } flags; |
| 24 | int range_bits; /* Full number of bits in the range */ |
| 25 | int effective_bits; /* Effective bits */ |
| 26 | long lower_bound; /* "lb" value */ |
| 27 | long upper_bound; /* "ub" value */ |
| 28 | } asn_per_constraint_t; |
| 29 | typedef struct asn_per_constraints_s { |
| 30 | asn_per_constraint_t value; |
| 31 | asn_per_constraint_t size; |
vlm | 86380d3 | 2006-10-09 12:07:58 +0000 | [diff] [blame] | 32 | int (*value2code)(unsigned int value); |
| 33 | int (*code2value)(unsigned int code); |
vlm | 1fcf759 | 2006-08-18 02:27:55 +0000 | [diff] [blame] | 34 | } asn_per_constraints_t; |
| 35 | |
| 36 | /* |
vlm | 18dd82c | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 37 | * This structure describes a position inside an incoming PER bit stream. |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 38 | */ |
| 39 | typedef struct asn_per_data_s { |
vlm | 4d2ca12 | 2005-12-07 05:46:03 +0000 | [diff] [blame] | 40 | const uint8_t *buffer; /* Pointer to the octet stream */ |
| 41 | size_t nboff; /* Bit offset to the meaningful bit */ |
| 42 | size_t nbits; /* Number of bits in the stream */ |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 43 | } asn_per_data_t; |
| 44 | |
| 45 | /* |
vlm | 1fcf759 | 2006-08-18 02:27:55 +0000 | [diff] [blame] | 46 | * Extract a small number of bits (<= 31) from the specified PER data pointer. |
| 47 | * This function returns -1 if the specified number of bits could not be |
| 48 | * extracted due to EOD or other conditions. |
| 49 | */ |
| 50 | int32_t per_get_few_bits(asn_per_data_t *per_data, int get_nbits); |
| 51 | |
| 52 | /* |
| 53 | * Extract a large number of bits from the specified PER data pointer. |
| 54 | * This function returns -1 if the specified number of bits could not be |
| 55 | * extracted due to EOD or other conditions. |
| 56 | */ |
| 57 | int per_get_many_bits(asn_per_data_t *pd, uint8_t *dst, int right_align, |
| 58 | int get_nbits); |
| 59 | |
| 60 | /* |
| 61 | * Get the length "n" from the Unaligned PER stream. |
| 62 | */ |
| 63 | ssize_t uper_get_length(asn_per_data_t *pd, |
| 64 | int effective_bound_bits, |
| 65 | int *repeat); |
| 66 | |
| 67 | /* |
| 68 | * Get the normally small non-negative whole number. |
| 69 | */ |
| 70 | ssize_t uper_get_nsnnwn(asn_per_data_t *pd); |
| 71 | |
| 72 | /* |
vlm | 18dd82c | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 73 | * This structure supports forming PER output. |
| 74 | */ |
| 75 | typedef struct asn_per_outp_s { |
| 76 | uint8_t *buffer; /* Pointer into the (tmpspace) */ |
| 77 | size_t nboff; /* Bit offset to the meaningful bit */ |
| 78 | size_t nbits; /* Number of bits left in (tmpspace) */ |
| 79 | uint8_t tmpspace[32]; /* Preliminary storage to hold data */ |
| 80 | int (*outper)(const void *data, size_t size, void *op_key); |
vlm | e7e9bd9 | 2006-09-17 11:02:53 +0000 | [diff] [blame] | 81 | void *op_key; /* Key for (outper) data callback */ |
| 82 | size_t flushed_bytes; /* Bytes already flushed through (outper) */ |
vlm | 18dd82c | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 83 | } asn_per_outp_t; |
| 84 | |
vlm | 18dd82c | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 85 | /* Output a small number of bits (<= 31) */ |
| 86 | int per_put_few_bits(asn_per_outp_t *per_data, uint32_t bits, int obits); |
| 87 | |
vlm | 18dd82c | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 88 | /* Output a large number of bits */ |
| 89 | int per_put_many_bits(asn_per_outp_t *po, const uint8_t *src, int put_nbits); |
| 90 | |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 91 | /* |
vlm | 18dd82c | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 92 | * Put the length "n" to the Unaligned PER stream. |
| 93 | * This function returns the number of units which may be flushed |
| 94 | * in the next units saving iteration. |
| 95 | */ |
| 96 | ssize_t uper_put_length(asn_per_outp_t *po, size_t whole_length); |
| 97 | |
| 98 | /* |
vlm | 18dd82c | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 99 | * Put the normally small non-negative whole number. |
| 100 | */ |
| 101 | int uper_put_nsnnwn(asn_per_outp_t *po, int n); |
| 102 | |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 103 | #ifdef __cplusplus |
| 104 | } |
| 105 | #endif |
| 106 | |
| 107 | #endif /* _PER_SUPPORT_H_ */ |