blob: c22bc7263acb386335958994ac9aef6ba5d03026 [file] [log] [blame]
Lev Walkin59b176e2005-11-26 11:25:14 +00001/*
Lev Walkinc46b7cb2006-08-18 02:27:55 +00002 * Copyright (c) 2005, 2006 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 */
9
10#ifdef __cplusplus
11extern "C" {
12#endif
13
14/*
Lev Walkinc46b7cb2006-08-18 02:27:55 +000015 * Pre-computed PER constraints.
16 */
17typedef struct asn_per_constraint_s {
18 enum asn_per_constraint_flags {
19 APC_UNCONSTRAINED = 0x0, /* No PER visible constraints */
20 APC_SEMI_CONSTRAINED = 0x1, /* Constrained at "lb" */
21 APC_CONSTRAINED = 0x2, /* Fully constrained */
22 APC_EXTENSIBLE = 0x4 /* May have extension */
23 } flags;
24 int range_bits; /* Full number of bits in the range */
25 int effective_bits; /* Effective bits */
26 long lower_bound; /* "lb" value */
27 long upper_bound; /* "ub" value */
28} asn_per_constraint_t;
29typedef struct asn_per_constraints_s {
30 asn_per_constraint_t value;
31 asn_per_constraint_t size;
Lev Walkin725883b2006-10-09 12:07:58 +000032 int (*value2code)(unsigned int value);
33 int (*code2value)(unsigned int code);
Lev Walkinc46b7cb2006-08-18 02:27:55 +000034} asn_per_constraints_t;
35
36/*
Lev Walkin523de9e2006-08-18 01:34:18 +000037 * This structure describes a position inside an incoming PER bit stream.
Lev Walkin59b176e2005-11-26 11:25:14 +000038 */
39typedef struct asn_per_data_s {
Lev Walkin1d9e8dd2005-12-07 05:46:03 +000040 const uint8_t *buffer; /* Pointer to the octet stream */
41 size_t nboff; /* Bit offset to the meaningful bit */
42 size_t nbits; /* Number of bits in the stream */
Lev Walkin5b78e1c2007-06-24 06:26:47 +000043 int (*refill)(struct asn_per_data_s *);
44 void *refill_key;
Lev Walkin59b176e2005-11-26 11:25:14 +000045} asn_per_data_t;
46
47/*
Lev Walkinc46b7cb2006-08-18 02:27:55 +000048 * Extract a small number of bits (<= 31) from the specified PER data pointer.
49 * This function returns -1 if the specified number of bits could not be
50 * extracted due to EOD or other conditions.
51 */
52int32_t per_get_few_bits(asn_per_data_t *per_data, int get_nbits);
53
54/*
55 * Extract a large number of bits from the specified PER data pointer.
56 * This function returns -1 if the specified number of bits could not be
57 * extracted due to EOD or other conditions.
58 */
59int per_get_many_bits(asn_per_data_t *pd, uint8_t *dst, int right_align,
60 int get_nbits);
61
62/*
63 * Get the length "n" from the Unaligned PER stream.
64 */
65ssize_t uper_get_length(asn_per_data_t *pd,
66 int effective_bound_bits,
67 int *repeat);
68
69/*
Lev Walkin5b78e1c2007-06-24 06:26:47 +000070 * Get the normally small length "n".
71 */
72ssize_t uper_get_nslength(asn_per_data_t *pd);
73
74/*
Lev Walkinc46b7cb2006-08-18 02:27:55 +000075 * Get the normally small non-negative whole number.
76 */
77ssize_t uper_get_nsnnwn(asn_per_data_t *pd);
78
79/*
Lev Walkin523de9e2006-08-18 01:34:18 +000080 * This structure supports forming PER output.
81 */
82typedef struct asn_per_outp_s {
83 uint8_t *buffer; /* Pointer into the (tmpspace) */
84 size_t nboff; /* Bit offset to the meaningful bit */
85 size_t nbits; /* Number of bits left in (tmpspace) */
86 uint8_t tmpspace[32]; /* Preliminary storage to hold data */
87 int (*outper)(const void *data, size_t size, void *op_key);
Lev Walkinbc691772006-09-17 11:02:53 +000088 void *op_key; /* Key for (outper) data callback */
89 size_t flushed_bytes; /* Bytes already flushed through (outper) */
Lev Walkin523de9e2006-08-18 01:34:18 +000090} asn_per_outp_t;
91
Lev Walkin523de9e2006-08-18 01:34:18 +000092/* Output a small number of bits (<= 31) */
93int per_put_few_bits(asn_per_outp_t *per_data, uint32_t bits, int obits);
94
Lev Walkin523de9e2006-08-18 01:34:18 +000095/* Output a large number of bits */
96int per_put_many_bits(asn_per_outp_t *po, const uint8_t *src, int put_nbits);
97
Lev Walkin59b176e2005-11-26 11:25:14 +000098/*
Lev Walkin523de9e2006-08-18 01:34:18 +000099 * Put the length "n" to the Unaligned PER stream.
100 * This function returns the number of units which may be flushed
101 * in the next units saving iteration.
102 */
103ssize_t uper_put_length(asn_per_outp_t *po, size_t whole_length);
104
105/*
Lev Walkin62258e22007-06-23 23:50:25 +0000106 * Put the normally small length "n" to the Unaligned PER stream.
107 * Returns 0 or -1.
108 */
109int uper_put_nslength(asn_per_outp_t *po, size_t length);
110
111/*
Lev Walkin523de9e2006-08-18 01:34:18 +0000112 * Put the normally small non-negative whole number.
113 */
114int uper_put_nsnnwn(asn_per_outp_t *po, int n);
115
Lev Walkin59b176e2005-11-26 11:25:14 +0000116#ifdef __cplusplus
117}
118#endif
119
120#endif /* _PER_SUPPORT_H_ */