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