blob: 23079c94f29e254a7a2b7375ac046c3e35b811f0 [file] [log] [blame]
Peter Szilagyifbc56f92019-07-23 19:29:46 +00001/*
2 * Copyright (c) 2005-2017 Lev Walkin <vlm@lionet.info>. All rights reserved.
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#include <asn_bit_data.h>
10
11#ifdef __cplusplus
12extern "C" {
13#endif
14
15/*
16 * Pre-computed PER constraints.
17 */
18typedef 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;
30typedef 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;
36
37/* Temporary compatibility layer. Will get removed. */
38typedef 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)
43
44/*
45 * X.691 (08/2015) #11.9 "General rules for encoding a length determinant"
46 * Get the length "n" from the Unaligned PER stream.
47 */
48ssize_t uper_get_length(asn_per_data_t *pd, int effective_bound_bits,
49 size_t lower_bound, int *repeat);
50
51ssize_t aper_get_length(asn_per_data_t *pd, int range,
52 int effective_bound_bits, int *repeat);
53
54/*
55 * Get the normally small length "n".
56 */
57ssize_t uper_get_nslength(asn_per_data_t *pd);
58ssize_t aper_get_nslength(asn_per_data_t *pd);
59
60/*
61 * Get the normally small non-negative whole number.
62 */
63ssize_t uper_get_nsnnwn(asn_per_data_t *pd);
64ssize_t aper_get_nsnnwn(asn_per_data_t *pd, int range);
65
66/* X.691-2008/11, #11.5.6 */
67int uper_get_constrained_whole_number(asn_per_data_t *pd, unsigned long *v, int nbits);
68
69
70/* Temporary compatibility layer. Will get removed. */
71typedef 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)
75
76
77/*
78 * Rebase the given value as an offset into the range specified by the
79 * lower bound (lb) and upper bound (ub).
80 * RETURN VALUES:
81 * -1: Conversion failed due to range problems.
82 * 0: Conversion was successful.
83 */
84int 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. */
86int per_long_range_unrebase(unsigned long inp, long lb, long ub, long *outp);
87
88/* X.691-2008/11, #11.5 */
89int uper_put_constrained_whole_number_u(asn_per_outp_t *po, unsigned long v, int nbits);
90
91/*
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.
98 */
99ssize_t uper_put_length(asn_per_outp_t *po, size_t whole_length,
100 int *opt_need_eom);
101
102ssize_t aper_put_length(asn_per_outp_t *po, int range, size_t length);
103
104/* Align the current bit position to octet bundary */
105int aper_put_align(asn_per_outp_t *po);
106int32_t aper_get_align(asn_per_data_t *pd);
107
108/*
109 * Put the normally small length "n" to the Unaligned PER stream.
110 * Returns 0 or -1.
111 */
112int uper_put_nslength(asn_per_outp_t *po, size_t length);
113
114int aper_put_nslength(asn_per_outp_t *po, size_t length);
115
116/*
117 * Put the normally small non-negative whole number.
118 */
119int uper_put_nsnnwn(asn_per_outp_t *po, int n);
120
121int aper_put_nsnnwn(asn_per_outp_t *po, int range, int number);
122
123#ifdef __cplusplus
124}
125#endif
126
127#endif /* _PER_SUPPORT_H_ */