blob: 0e4e189e66e4f4103daf65af012f075dd743536a [file] [log] [blame]
Lev Walkin59b176e2005-11-26 11:25:14 +00001/*
Lev Walkin6c527842014-02-09 04:34:54 -08002 * Copyright (c) 2005-2014 Lev Walkin <vlm@lionet.info>.
Lev Walkin9218bc12007-06-27 04:09:37 +00003 * All rights reserved.
Lev Walkin59b176e2005-11-26 11:25:14 +00004 * 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
12extern "C" {
13#endif
14
15/*
Lev Walkinc46b7cb2006-08-18 02:27:55 +000016 * Pre-computed PER constraints.
17 */
Wim Lewis18c2ec92014-07-29 11:30:10 -070018typedef const 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;
Wim Lewis18c2ec92014-07-29 11:30:10 -070030typedef const struct asn_per_constraints_s {
31 struct asn_per_constraint_s value;
32 struct asn_per_constraint_s 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
37/*
Lev Walkin523de9e2006-08-18 01:34:18 +000038 * This structure describes a position inside an incoming PER bit stream.
Lev Walkin59b176e2005-11-26 11:25:14 +000039 */
40typedef struct asn_per_data_s {
Lev Walkind00657f2007-06-26 02:51:10 +000041 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 Walkin59b176e2005-11-26 11:25:14 +000047} asn_per_data_t;
48
49/*
Lev Walkinc46b7cb2006-08-18 02:27:55 +000050 * 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 */
54int32_t per_get_few_bits(asn_per_data_t *per_data, int get_nbits);
55
Lev Walkind00657f2007-06-26 02:51:10 +000056/* Undo the immediately preceeding "get_few_bits" operation */
57void per_get_undo(asn_per_data_t *per_data, int get_nbits);
58
Lev Walkinc46b7cb2006-08-18 02:27:55 +000059/*
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 */
64int 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 */
70ssize_t uper_get_length(asn_per_data_t *pd,
71 int effective_bound_bits,
72 int *repeat);
73
74/*
Lev Walkin5b78e1c2007-06-24 06:26:47 +000075 * Get the normally small length "n".
76 */
77ssize_t uper_get_nslength(asn_per_data_t *pd);
78
79/*
Lev Walkinc46b7cb2006-08-18 02:27:55 +000080 * Get the normally small non-negative whole number.
81 */
82ssize_t uper_get_nsnnwn(asn_per_data_t *pd);
83
Lev Walkin6c527842014-02-09 04:34:54 -080084/* X.691-2008/11, #11.5.6 */
85int uper_get_constrained_whole_number(asn_per_data_t *pd, unsigned long *v, int nbits);
86
Lev Walkin11944f12007-06-29 02:28:10 +000087/* Non-thread-safe debugging function, don't use it */
88char *per_data_string(asn_per_data_t *pd);
89
Lev Walkinc46b7cb2006-08-18 02:27:55 +000090/*
Lev Walkin523de9e2006-08-18 01:34:18 +000091 * This structure supports forming PER output.
92 */
93typedef struct asn_per_outp_s {
94 uint8_t *buffer; /* Pointer into the (tmpspace) */
95 size_t nboff; /* Bit offset to the meaningful bit */
96 size_t nbits; /* Number of bits left in (tmpspace) */
97 uint8_t tmpspace[32]; /* Preliminary storage to hold data */
98 int (*outper)(const void *data, size_t size, void *op_key);
Lev Walkinbc691772006-09-17 11:02:53 +000099 void *op_key; /* Key for (outper) data callback */
100 size_t flushed_bytes; /* Bytes already flushed through (outper) */
Lev Walkin523de9e2006-08-18 01:34:18 +0000101} asn_per_outp_t;
102
Lev Walkin523de9e2006-08-18 01:34:18 +0000103/* Output a small number of bits (<= 31) */
104int per_put_few_bits(asn_per_outp_t *per_data, uint32_t bits, int obits);
105
Lev Walkin523de9e2006-08-18 01:34:18 +0000106/* Output a large number of bits */
107int per_put_many_bits(asn_per_outp_t *po, const uint8_t *src, int put_nbits);
108
Lev Walkin3fd85d82017-07-24 00:30:06 +0400109/*
110 * Flush whole bytes (0 or more) through (outper) member.
111 * The least significant bits which are not used are guaranteed to be set to 0.
112 * Returns -1 if callback returns -1. Otherwise, 0.
113 */
114int per_put_aligned_flush(asn_per_outp_t *po);
115
Lev Walkin6c527842014-02-09 04:34:54 -0800116/* X.691-2008/11, #11.5 */
117int uper_put_constrained_whole_number_s(asn_per_outp_t *po, long v, int nbits);
118int uper_put_constrained_whole_number_u(asn_per_outp_t *po, unsigned long v, int nbits);
119
Lev Walkin59b176e2005-11-26 11:25:14 +0000120/*
Lev Walkin523de9e2006-08-18 01:34:18 +0000121 * Put the length "n" to the Unaligned PER stream.
122 * This function returns the number of units which may be flushed
123 * in the next units saving iteration.
124 */
125ssize_t uper_put_length(asn_per_outp_t *po, size_t whole_length);
126
127/*
Lev Walkin62258e22007-06-23 23:50:25 +0000128 * Put the normally small length "n" to the Unaligned PER stream.
129 * Returns 0 or -1.
130 */
131int uper_put_nslength(asn_per_outp_t *po, size_t length);
132
133/*
Lev Walkin523de9e2006-08-18 01:34:18 +0000134 * Put the normally small non-negative whole number.
135 */
136int uper_put_nsnnwn(asn_per_outp_t *po, int n);
137
Lev Walkin59b176e2005-11-26 11:25:14 +0000138#ifdef __cplusplus
139}
140#endif
141
142#endif /* _PER_SUPPORT_H_ */