2 * Copyright (c) 2005-2017 Lev Walkin <vlm@lionet.info>. All rights reserved.
3 * Redistribution and modifications are permitted subject to BSD license.
5 #ifndef _PER_SUPPORT_H_
6 #define _PER_SUPPORT_H_
8 #include <asn_system.h> /* Platform-specific types */
9 #include <asn_bit_data.h>
16 * Pre-computed PER constraints.
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 */
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;
33 int (*value2code)(unsigned int value);
34 int (*code2value)(unsigned int code);
35 } asn_per_constraints_t;
37 /* Temporary compatibility layer. Will get removed. */
38 typedef struct asn_bit_data_s asn_per_data_t;
39 #define per_get_few_bits(data, bits) asn_get_few_bits(data, bits)
40 #define per_get_undo(data, bits) asn_get_undo(data, bits)
41 #define per_get_many_bits(data, dst, align, bits) \
42 asn_get_many_bits(data, dst, align, bits)
45 * X.691 (08/2015) #11.9 "General rules for encoding a length determinant"
46 * Get the length "n" from the Unaligned PER stream.
48 ssize_t uper_get_length(asn_per_data_t *pd, int effective_bound_bits,
49 size_t lower_bound, int *repeat);
51 ssize_t aper_get_length(asn_per_data_t *pd, int range,
52 int effective_bound_bits, int *repeat);
55 * Get the normally small length "n".
57 ssize_t uper_get_nslength(asn_per_data_t *pd);
58 ssize_t aper_get_nslength(asn_per_data_t *pd);
61 * Get the normally small non-negative whole number.
63 ssize_t uper_get_nsnnwn(asn_per_data_t *pd);
64 ssize_t aper_get_nsnnwn(asn_per_data_t *pd, int range);
66 /* X.691-2008/11, #11.5.6 */
67 int uper_get_constrained_whole_number(asn_per_data_t *pd, unsigned long *v, int nbits);
70 /* Temporary compatibility layer. Will get removed. */
71 typedef struct asn_bit_outp_s asn_per_outp_t;
72 #define per_put_few_bits(out, bits, obits) asn_put_few_bits(out, bits, obits)
73 #define per_put_many_bits(out, src, nbits) asn_put_many_bits(out, src, nbits)
74 #define per_put_aligned_flush(out) asn_put_aligned_flush(out)
78 * Rebase the given value as an offset into the range specified by the
79 * lower bound (lb) and upper bound (ub).
81 * -1: Conversion failed due to range problems.
82 * 0: Conversion was successful.
84 int per_long_range_rebase(long v, long lb, long ub, unsigned long *output);
85 /* The inverse operation: restores the value by the offset and its bounds. */
86 int per_long_range_unrebase(unsigned long inp, long lb, long ub, long *outp);
88 /* X.691-2008/11, #11.5 */
89 int uper_put_constrained_whole_number_u(asn_per_outp_t *po, unsigned long v, int nbits);
92 * X.691 (08/2015) #11.9 "General rules for encoding a length determinant"
93 * Put the length "whole_length" to the Unaligned PER stream.
94 * If (opt_need_eom) is given, it will be set to 1 if final 0-length is needed.
95 * In that case, invoke uper_put_length(po, 0, 0) after encoding the last block.
96 * This function returns the number of units which may be flushed
97 * in the next units saving iteration.
99 ssize_t uper_put_length(asn_per_outp_t *po, size_t whole_length,
102 ssize_t aper_put_length(asn_per_outp_t *po, int range, size_t length);
104 /* Align the current bit position to octet bundary */
105 int aper_put_align(asn_per_outp_t *po);
106 int32_t aper_get_align(asn_per_data_t *pd);
109 * Put the normally small length "n" to the Unaligned PER stream.
112 int uper_put_nslength(asn_per_outp_t *po, size_t length);
114 int aper_put_nslength(asn_per_outp_t *po, size_t length);
117 * Put the normally small non-negative whole number.
119 int uper_put_nsnnwn(asn_per_outp_t *po, int n);
121 int aper_put_nsnnwn(asn_per_outp_t *po, int range, int number);
127 #endif /* _PER_SUPPORT_H_ */