blob: bf06f056724053f33bc3f1b3d91a30df1f62ca58 [file] [log] [blame]
vlm337167e2005-11-26 11:25:14 +00001/*
vlm1fcf7592006-08-18 02:27:55 +00002 * Copyright (c) 2005, 2006 Lev Walkin <vlm@lionet.info>. All rights reserved.
vlm337167e2005-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/*
vlm1fcf7592006-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;
vlm86380d32006-10-09 12:07:58 +000032 int (*value2code)(unsigned int value);
33 int (*code2value)(unsigned int code);
vlm1fcf7592006-08-18 02:27:55 +000034} asn_per_constraints_t;
35
36/*
vlm18dd82c2006-08-18 01:34:18 +000037 * This structure describes a position inside an incoming PER bit stream.
vlm337167e2005-11-26 11:25:14 +000038 */
39typedef struct asn_per_data_s {
vlm26b48ca2007-06-26 02:51:10 +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 */
43 size_t moved; /* Number of bits moved through this bit stream */
44 int (*refill)(struct asn_per_data_s *);
45 void *refill_key;
vlm337167e2005-11-26 11:25:14 +000046} asn_per_data_t;
47
48/*
vlm1fcf7592006-08-18 02:27:55 +000049 * Extract a small number of bits (<= 31) from the specified PER data pointer.
50 * This function returns -1 if the specified number of bits could not be
51 * extracted due to EOD or other conditions.
52 */
53int32_t per_get_few_bits(asn_per_data_t *per_data, int get_nbits);
54
vlm26b48ca2007-06-26 02:51:10 +000055/* Undo the immediately preceeding "get_few_bits" operation */
56void per_get_undo(asn_per_data_t *per_data, int get_nbits);
57
vlm1fcf7592006-08-18 02:27:55 +000058/*
59 * Extract a large number of bits from the specified PER data pointer.
60 * This function returns -1 if the specified number of bits could not be
61 * extracted due to EOD or other conditions.
62 */
63int per_get_many_bits(asn_per_data_t *pd, uint8_t *dst, int right_align,
64 int get_nbits);
65
66/*
67 * Get the length "n" from the Unaligned PER stream.
68 */
69ssize_t uper_get_length(asn_per_data_t *pd,
70 int effective_bound_bits,
71 int *repeat);
72
73/*
vlm02cd28b2007-06-24 06:26:47 +000074 * Get the normally small length "n".
75 */
76ssize_t uper_get_nslength(asn_per_data_t *pd);
77
78/*
vlm1fcf7592006-08-18 02:27:55 +000079 * Get the normally small non-negative whole number.
80 */
81ssize_t uper_get_nsnnwn(asn_per_data_t *pd);
82
83/*
vlm18dd82c2006-08-18 01:34:18 +000084 * This structure supports forming PER output.
85 */
86typedef struct asn_per_outp_s {
87 uint8_t *buffer; /* Pointer into the (tmpspace) */
88 size_t nboff; /* Bit offset to the meaningful bit */
89 size_t nbits; /* Number of bits left in (tmpspace) */
90 uint8_t tmpspace[32]; /* Preliminary storage to hold data */
91 int (*outper)(const void *data, size_t size, void *op_key);
vlme7e9bd92006-09-17 11:02:53 +000092 void *op_key; /* Key for (outper) data callback */
93 size_t flushed_bytes; /* Bytes already flushed through (outper) */
vlm18dd82c2006-08-18 01:34:18 +000094} asn_per_outp_t;
95
vlm18dd82c2006-08-18 01:34:18 +000096/* Output a small number of bits (<= 31) */
97int per_put_few_bits(asn_per_outp_t *per_data, uint32_t bits, int obits);
98
vlm18dd82c2006-08-18 01:34:18 +000099/* Output a large number of bits */
100int per_put_many_bits(asn_per_outp_t *po, const uint8_t *src, int put_nbits);
101
vlm337167e2005-11-26 11:25:14 +0000102/*
vlm18dd82c2006-08-18 01:34:18 +0000103 * Put the length "n" to the Unaligned PER stream.
104 * This function returns the number of units which may be flushed
105 * in the next units saving iteration.
106 */
107ssize_t uper_put_length(asn_per_outp_t *po, size_t whole_length);
108
109/*
vlm252357f2007-06-23 23:50:25 +0000110 * Put the normally small length "n" to the Unaligned PER stream.
111 * Returns 0 or -1.
112 */
113int uper_put_nslength(asn_per_outp_t *po, size_t length);
114
115/*
vlm18dd82c2006-08-18 01:34:18 +0000116 * Put the normally small non-negative whole number.
117 */
118int uper_put_nsnnwn(asn_per_outp_t *po, int n);
119
vlm337167e2005-11-26 11:25:14 +0000120#ifdef __cplusplus
121}
122#endif
123
124#endif /* _PER_SUPPORT_H_ */