blob: 3e905a8b971babae81e9afc3b8dd728f68b36d05 [file] [log] [blame]
Lev Walkin59b176e2005-11-26 11:25:14 +00001/*
Lev Walkin20696a42017-10-17 21:27:33 -07002 * Copyright (c) 2005-2017 Lev Walkin <vlm@lionet.info>. All rights reserved.
Lev Walkin59b176e2005-11-26 11:25:14 +00003 * 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 */
Lev Walkin6cd0d562017-08-25 11:57:01 -07009#include <asn_bit_data.h>
Lev Walkin59b176e2005-11-26 11:25:14 +000010
11#ifdef __cplusplus
12extern "C" {
13#endif
14
15/*
Lev Walkinc46b7cb2006-08-18 02:27:55 +000016 * Pre-computed PER constraints.
17 */
Lev Walkin494fb702017-08-07 20:07:00 -070018typedef struct asn_per_constraint_s {
Lev Walkinc46b7cb2006-08-18 02:27:55 +000019 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;
Lev Walkin494fb702017-08-07 20:07:00 -070030typedef struct asn_per_constraints_s {
31 asn_per_constraint_t value;
32 asn_per_constraint_t size;
Lev Walkin725883b2006-10-09 12:07:58 +000033 int (*value2code)(unsigned int value);
34 int (*code2value)(unsigned int code);
Lev Walkinc46b7cb2006-08-18 02:27:55 +000035} asn_per_constraints_t;
36
Lev Walkin6cd0d562017-08-25 11:57:01 -070037/* 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)
Lev Walkinc46b7cb2006-08-18 02:27:55 +000043
44/*
Lev Walkin9d1b45f2017-10-01 17:04:48 -070045 * X.691 (08/2015) #11.9 "General rules for encoding a length determinant"
Lev Walkinc46b7cb2006-08-18 02:27:55 +000046 * Get the length "n" from the Unaligned PER stream.
47 */
Lev Walkin9d1b45f2017-10-01 17:04:48 -070048ssize_t uper_get_length(asn_per_data_t *pd, int effective_bound_bits,
49 size_t lower_bound, int *repeat);
Lev Walkinc46b7cb2006-08-18 02:27:55 +000050
51/*
Lev Walkin5b78e1c2007-06-24 06:26:47 +000052 * Get the normally small length "n".
53 */
54ssize_t uper_get_nslength(asn_per_data_t *pd);
55
56/*
Lev Walkinc46b7cb2006-08-18 02:27:55 +000057 * Get the normally small non-negative whole number.
58 */
59ssize_t uper_get_nsnnwn(asn_per_data_t *pd);
60
Lev Walkin6c527842014-02-09 04:34:54 -080061/* X.691-2008/11, #11.5.6 */
62int uper_get_constrained_whole_number(asn_per_data_t *pd, unsigned long *v, int nbits);
63
Lev Walkin11944f12007-06-29 02:28:10 +000064
Lev Walkin6cd0d562017-08-25 11:57:01 -070065/* Temporary compatibility layer. Will get removed. */
66typedef struct asn_bit_outp_s asn_per_outp_t;
67#define per_put_few_bits(out, bits, obits) asn_put_few_bits(out, bits, obits)
68#define per_put_many_bits(out, src, nbits) asn_put_many_bits(out, src, nbits)
69#define per_put_aligned_flush(out) asn_put_aligned_flush(out)
Lev Walkin3fd85d82017-07-24 00:30:06 +040070
Lev Walkincc6a76b2017-10-07 16:23:16 -070071
72/*
73 * Rebase the given value as an offset into the range specified by the
74 * lower bound (lb) and upper bound (ub).
75 * RETURN VALUES:
76 * -1: Conversion failed due to range problems.
77 * 0: Conversion was successful.
78 */
79int per_long_range_rebase(long v, long lb, long ub, unsigned long *output);
80/* The inverse operation: restores the value by the offset and its bounds. */
81int per_long_range_unrebase(unsigned long inp, long lb, long ub, long *outp);
82
Lev Walkin6c527842014-02-09 04:34:54 -080083/* X.691-2008/11, #11.5 */
Lev Walkin6c527842014-02-09 04:34:54 -080084int uper_put_constrained_whole_number_u(asn_per_outp_t *po, unsigned long v, int nbits);
85
Lev Walkin59b176e2005-11-26 11:25:14 +000086/*
Lev Walkin9d1b45f2017-10-01 17:04:48 -070087 * X.691 (08/2015) #11.9 "General rules for encoding a length determinant"
Lev Walkin5d947a82017-10-03 01:04:03 -070088 * Put the length "whole_length" to the Unaligned PER stream.
89 * If (opt_need_eom) is given, it will be set to 1 if final 0-length is needed.
90 * In that case, invoke uper_put_length(po, 0, 0) after encoding the last block.
Lev Walkin523de9e2006-08-18 01:34:18 +000091 * This function returns the number of units which may be flushed
92 * in the next units saving iteration.
93 */
Lev Walkin5d947a82017-10-03 01:04:03 -070094ssize_t uper_put_length(asn_per_outp_t *po, size_t whole_length,
95 int *opt_need_eom);
Lev Walkin523de9e2006-08-18 01:34:18 +000096
97/*
Lev Walkin62258e22007-06-23 23:50:25 +000098 * Put the normally small length "n" to the Unaligned PER stream.
99 * Returns 0 or -1.
100 */
101int uper_put_nslength(asn_per_outp_t *po, size_t length);
102
103/*
Lev Walkin523de9e2006-08-18 01:34:18 +0000104 * Put the normally small non-negative whole number.
105 */
106int uper_put_nsnnwn(asn_per_outp_t *po, int n);
107
Lev Walkin59b176e2005-11-26 11:25:14 +0000108#ifdef __cplusplus
109}
110#endif
111
112#endif /* _PER_SUPPORT_H_ */