check-PER-INTEGER.c 6.2 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <stdio.h>
#include <assert.h>

#define	EMIT_ASN_DEBUG	1
#include <INTEGER.h>
#include <INTEGER.c>
#include <per_support.c>
#include <per_support.h>

static int FailOut(const void *data, size_t size, void *op_key) {
	assert(!"UNREACHABLE");
	return 0;
}

static void normalize(asn_per_outp_t *po) {
	if(po->nboff >= 8) {
		po->buffer += (po->nboff >> 3);
		po->nbits  -= (po->nboff & ~0x07);
		po->nboff  &= 0x07;
	}
}

static void
check_per_encode_constrained(int lineno, int unsigned_, long value, long lbound, unsigned long ubound, int bit_range) {
	INTEGER_t st;
	INTEGER_t *reconstructed_st = 0;
	asn_INTEGER_specifics_t specs;
	asn_per_constraints_t cts;
	asn_enc_rval_t enc_rval;
	asn_dec_rval_t dec_rval;
	asn_per_outp_t po;
	asn_per_data_t pd;

Lev Walkin's avatar
Lev Walkin committed
34
35
36
37
38
39
	if(unsigned_)
		printf("%d: Recoding %s %lu [%ld..%lu]\n", lineno,
		  unsigned_ ? "unsigned" : "signed", value, lbound, ubound);
	else
		printf("%d: Recoding %s %ld [%ld..%lu]\n", lineno,
		  unsigned_ ? "unsigned" : "signed", value, lbound, ubound);
40
41
42
43
44
45
46
47
48
49
50
51
52

	memset(&st, 0, sizeof(st));
	memset(&po, 0, sizeof(po));
	memset(&pd, 0, sizeof(pd));
	memset(&cts, 0, sizeof(cts));
	memset(&specs, 0, sizeof(specs));

	cts.value.flags = APC_CONSTRAINED;
	cts.value.range_bits = bit_range;
	cts.value.effective_bits = bit_range;
	cts.value.lower_bound = lbound;
	cts.value.upper_bound = ubound;

Lev Walkin's avatar
Lev Walkin committed
53
54
55
56
	if(unsigned_)
		asn_ulong2INTEGER(&st, (unsigned long)value);
	else
		asn_long2INTEGER(&st, value);
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72

	po.buffer = po.tmpspace;
	po.nboff = 0;
	po.nbits = 8 * sizeof(po.tmpspace);
	po.outper = FailOut;

	specs.field_width = sizeof(long);
	specs.field_unsigned = unsigned_;

	asn_DEF_INTEGER.specifics = &specs;
	enc_rval = INTEGER_encode_uper(&asn_DEF_INTEGER, &cts, &st, &po);
	assert(enc_rval.encoded == 0);

	normalize(&po);

	assert(po.buffer == &po.tmpspace[bit_range / 8]);
Lev Walkin's avatar
Lev Walkin committed
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92

	if(unsigned_) {
		unsigned long recovered_value =
			  ((uint32_t)po.tmpspace[0] << 24)
			| ((uint32_t)po.tmpspace[1] << 16)
			| ((uint32_t)po.tmpspace[2] << 8)
			| ((uint32_t)po.tmpspace[3] << 0);
		recovered_value >>= (32 - bit_range);
		recovered_value += cts.value.lower_bound;
		assert(recovered_value == (unsigned long)value);
	} else {
		long recovered_value =
			  ((uint32_t)po.tmpspace[0] << 24)
			| ((uint32_t)po.tmpspace[1] << 16)
			| ((uint32_t)po.tmpspace[2] << 8)
			| ((uint32_t)po.tmpspace[3] << 0);
		recovered_value = (unsigned long)recovered_value >> (32 - bit_range);
		recovered_value += cts.value.lower_bound;
		assert((long)recovered_value == value);
	}
93
94
95
96
97
98
99
100
101
102
	assert(po.nboff == ((bit_range == 32) ? 0 : (8 - (32 - bit_range))));
	assert(po.nbits ==  8 * (sizeof(po.tmpspace) - (po.buffer-po.tmpspace)));
	assert(po.flushed_bytes == 0);

	pd.buffer = po.tmpspace;
	pd.nboff = 0;
	pd.nbits = 8 * (po.buffer - po.tmpspace) + po.nboff;
	pd.moved = 0;
	dec_rval = INTEGER_decode_uper(0, &asn_DEF_INTEGER, &cts,
					(void **)&reconstructed_st, &pd);
Lev Walkin's avatar
Lev Walkin committed
103
104
105
106
107
108
109
110
111
112
	assert(dec_rval.code == RC_OK);
	if(unsigned_) {
		unsigned long reconstructed_value = 0;
		asn_INTEGER2ulong(reconstructed_st, &reconstructed_value);
		assert(reconstructed_value == (unsigned long)value);
	} else {
		long reconstructed_value = 0;
		asn_INTEGER2long(reconstructed_st, &reconstructed_value);
		assert(reconstructed_value == value);
	}
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
}

#define	CHECK(u, v, l, r, b)	\
	check_per_encode_constrained(__LINE__, u, v, l, r, b)

int
main() {
  int unsigned_;
  for(unsigned_ = 0; unsigned_ < 2; unsigned_++) {
	int u = unsigned_;

	/* Encode a signed 0x8babab into a range constrained by 0..2^29-1 */
	CHECK(u, 0x8babab, 0, 536870911UL, 29);
	CHECK(u, 0x8babab, 0, 1073741823UL, 30);
	CHECK(u, 0x8babab, 0, 2147483647UL, 31);

	CHECK(u, 0x8babab, 10, 536870901UL, 29);
	CHECK(u, 0x8babab, 10, 1073741803UL, 30);
	CHECK(u, 0x8babab, 10, 2147483607UL, 31);

	CHECK(0, 0x8babab, -10, 536870901UL, 29);
	CHECK(0, 0x8babab, -10, 1073741803UL, 30);
	CHECK(0, 0x8babab, -10, 2147483607UL, 31);

	CHECK(u, 11, 10, 536870901UL, 29);
	CHECK(u, 11, 10, 1073741803UL, 30);
	CHECK(u, 11, 10, 2147483607UL, 31);

	CHECK(0, 1, -10, 536870901UL, 29);
	CHECK(0, 1, -10, 1073741803UL, 30);
	CHECK(0, 1, -10, 2147483607UL, 31);

	CHECK(u, 10, 10, 536870901UL, 29);
	CHECK(u, 10, 10, 1073741803UL, 30);
	CHECK(u, 10, 10, 2147483607UL, 31);

	CHECK(0, 0, -10, 536870901UL, 29);
	CHECK(0, 0, -10, 1073741803UL, 30);
	CHECK(0, 0, -10, 2147483607UL, 31);

	CHECK(0, -1, -10, 536870901UL, 29);
	CHECK(0, -1, -10, 1073741803UL, 30);
	CHECK(0, -1, -10, 2147483607UL, 31);

	CHECK(0, -10, -10, 536870901UL, 29);
	CHECK(0, -10, -10, 1073741803UL, 30);
	CHECK(0, -10, -10, 2147483607UL, 31);

	CHECK(u, 536870901UL, 10, 536870901UL, 29);
	CHECK(u, 1073741803UL, 10, 1073741803UL, 30);
	CHECK(u, 2147483607UL, 10, 2147483607UL, 31);

	CHECK(0, 536870901UL, -10, 536870901UL, 29);
	CHECK(0, 1073741803UL, -10, 1073741803UL, 30);
	CHECK(0, 2147483607UL, -10, 2147483607UL, 31);

169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
	CHECK(0, -2147483648, -2147483648, 2147483647, 32);
	CHECK(0, -10, -2147483648, 2147483647, 32);
	CHECK(0,  -1, -2147483648, 2147483647, 32);
	CHECK(0,   0, -2147483648, 2147483647, 32);
	CHECK(0,   1, -2147483648, 2147483647, 32);
	CHECK(0,  10, -2147483648, 2147483647, 32);
	CHECK(0,  2147483647, -2147483648, 2147483647, 32);

	CHECK(1,  0, 0, 4294967295UL, 32);
	CHECK(1,  1, 0, 4294967295UL, 32);
	CHECK(1, 10, 0, 4294967295UL, 32);
	CHECK(1, 2000000000, 0, 4294967295UL, 32);
	CHECK(1, 2147483647, 0, 4294967295UL, 32);
	CHECK(1, 2147483648, 0, 4294967295UL, 32);
	CHECK(1, 4000000000, 0, 4294967295UL, 32);
	CHECK(1, 4294967295UL, 0, 4294967295UL, 32);

	CHECK(1, 10, 10, 4294967285UL, 32);
	CHECK(1, 11, 10, 4294967285UL, 32);

	if(sizeof(long) > sizeof(uint32_t)) {
		CHECK(0,   0, -10, 4294967285UL, 32);
		CHECK(0,   1, -10, 4294967285UL, 32);
		CHECK(0,  -1, -10, 4294967285UL, 32);
		CHECK(0, -10, -10, 4294967285UL, 32);
		CHECK(0, -10, -10, 4294967285UL, 32);
		CHECK(0, 0x8babab, -10, 4294967285UL, 32);
Lev Walkin's avatar
Lev Walkin committed
196
197
198
199

		CHECK(u, 0x8babab, 0, 4294967295UL, 32);
		CHECK(u, 11, 10, 4294967205UL, 32);
		CHECK(u, 10, 10, 4294967205UL, 32);
200
		CHECK(u, 4294967205UL, 10, 4294967285UL, 32);
Lev Walkin's avatar
Lev Walkin committed
201

202
		CHECK(0, 4294967205UL, -10, 4294967285UL, 32);
Lev Walkin's avatar
Lev Walkin committed
203
204
205
206
207
208
209
		CHECK(u, 4294967295UL, 1, 4294967295UL, 32);

		CHECK(u, 2000000000, 0, 4294967295UL, 32);
		CHECK(u, 2147483647, 0, 4294967295UL, 32);
		CHECK(u, 2147483648, 0, 4294967295UL, 32);
		CHECK(u, 4000000000, 0, 4294967295UL, 32);
	}
210
211
212
213
 }

  return 0;
}