Commit | Line | Data |
---|---|---|
d44e3c4f | 1 | /****************************************************************************** |
2 | * Copyright (c) 2000-2016 Ericsson Telecom AB | |
3 | * All rights reserved. This program and the accompanying materials | |
4 | * are made available under the terms of the Eclipse Public License v1.0 | |
5 | * which accompanies this distribution, and is available at | |
6 | * http://www.eclipse.org/legal/epl-v10.html | |
7 | * | |
8 | * Contributors: | |
9 | * Balasko, Jeno | |
10 | * Bibo, Zoltan | |
11 | * Forstner, Matyas | |
12 | * Gecse, Roland | |
13 | * Koppany, Csaba | |
14 | * Kovacs, Ferenc | |
15 | * Raduly, Csaba | |
16 | * Szabo, Janos Zoltan – initial implementation | |
17 | * | |
18 | ******************************************************************************/ | |
970ed795 EL |
19 | #include "../common/dbgnew.hh" |
20 | #include "Int.hh" | |
21 | #include "string.hh" | |
22 | #include "error.h" | |
23 | #include "Setting.hh" | |
24 | ||
25 | #include <openssl/crypto.h> | |
26 | ||
27 | #include <stdlib.h> | |
28 | #include <stdio.h> | |
29 | #include <errno.h> | |
30 | #include <math.h> | |
31 | ||
32 | // We cannot check without using a "./configure" script or such if we have | |
33 | // llabs() or not. Define our own function instead. | |
34 | inline long long ll_abs(long long x) { return ((x >= 0) ? (x) : (-x)); } | |
35 | ||
36 | namespace Common { | |
37 | ||
38 | string Int2string(const Int& i) | |
39 | { | |
40 | char *s = NULL; | |
41 | s = mprintf("%lld", i); | |
42 | string str(s); | |
43 | Free(s); | |
44 | return str; | |
45 | } | |
46 | ||
47 | Int string2Int(const char *s, const Location& loc) | |
48 | { | |
49 | errno = 0; | |
50 | Int i = strtoll(s, NULL, 10); | |
51 | switch (errno) { | |
52 | case ERANGE: { | |
53 | if (loc.get_filename() != NULL) { | |
54 | loc.error("Overflow when converting `%s' to integer value: %s", s, | |
55 | strerror(errno)); | |
56 | } else { | |
57 | FATAL_ERROR("Overflow when converting `%s' to integer value: %s", s, | |
58 | strerror(errno)); | |
59 | } | |
60 | break; } | |
61 | case 0: | |
62 | break; | |
63 | default: | |
64 | FATAL_ERROR("Unexpected error when converting `%s' to integer: %s", s, | |
65 | strerror(errno)); | |
66 | } | |
67 | return i; | |
68 | } | |
69 | ||
70 | int_val_t::int_val_t() : native_flag(true) | |
71 | { | |
72 | val.openssl = NULL; | |
73 | } | |
74 | ||
75 | int_val_t::int_val_t(const int_val_t& v) | |
76 | { | |
77 | native_flag = v.is_native(); | |
78 | if (native_flag) val.native = v.get_val(); | |
79 | else val.openssl = BN_dup(v.get_val_openssl()); | |
80 | } | |
81 | ||
82 | int_val_t::int_val_t(const char *s, const Location& loc) | |
83 | { | |
84 | BIGNUM *n = NULL; | |
85 | if (!BN_dec2bn(&n, *s == '+' ? s + 1 : s)) | |
86 | loc.error("Unexpected error when converting `%s' to integer", s); | |
87 | if (BN_num_bits(n) > (int)sizeof(long long) * 8 - 1) { | |
88 | native_flag = false; | |
89 | val.openssl = n; | |
90 | } else { | |
91 | native_flag = true; | |
92 | val.native = string2Int(s, loc); | |
93 | BN_free(n); | |
94 | } | |
95 | } | |
96 | ||
97 | int_val_t::~int_val_t() | |
98 | { | |
99 | if (!native_flag) BN_free(val.openssl); | |
100 | } | |
101 | ||
102 | string int_val_t::t_str() const | |
103 | { | |
104 | char *tmp = NULL; | |
105 | if (native_flag) { | |
106 | tmp = mprintf("%lld", val.native); | |
107 | string s(tmp); | |
108 | Free(tmp); | |
109 | return s; | |
110 | } else { | |
111 | if (!(tmp = BN_bn2dec(val.openssl))) | |
112 | FATAL_ERROR("int_val_t::t_str()"); | |
113 | string s(tmp); | |
114 | OPENSSL_free(tmp); | |
115 | return s; | |
116 | } | |
117 | } | |
118 | ||
119 | BIGNUM *int_val_t::to_openssl() const | |
120 | { | |
121 | BIGNUM *big = NULL; | |
122 | if (native_flag) { | |
123 | string str = Int2string(val.native); | |
124 | if (!BN_dec2bn(&big, str.c_str())) FATAL_ERROR("int_val_t::to_openssl()"); | |
125 | } else { | |
126 | big = BN_dup(val.openssl); | |
127 | if (!big) FATAL_ERROR("int_val_t::to_openssl()"); | |
128 | } | |
129 | return big; | |
130 | } | |
131 | ||
132 | bool int_val_t::operator==(Int right) const | |
133 | { | |
134 | if (!native_flag) return false; | |
135 | return val.native == right; | |
136 | } | |
137 | ||
138 | bool int_val_t::operator==(const int_val_t& v) const | |
139 | { | |
140 | if (native_flag != v.is_native()) return false; | |
141 | if (native_flag) return val.native == v.get_val(); | |
142 | return !BN_cmp(val.openssl, v.get_val_openssl()); | |
143 | } | |
144 | ||
145 | bool int_val_t::operator<(const int_val_t& v) const | |
146 | { | |
147 | if (native_flag) { | |
148 | if (v.is_native()) { | |
149 | return val.native < v.get_val(); | |
150 | } else { | |
151 | BIGNUM *this_big = to_openssl(); | |
152 | if (!this_big) FATAL_ERROR("int_val_t::operator<(int_val_t& v)"); | |
153 | int this_equ = BN_cmp(this_big, v.get_val_openssl()); | |
154 | BN_free(this_big); | |
155 | return this_equ == -1; | |
156 | } | |
157 | } else { | |
158 | if (v.is_native()) { | |
159 | BIGNUM *v_big = v.to_openssl(); | |
160 | if (!v_big) FATAL_ERROR("int_val_t::operator<(int_val_t& v)"); | |
161 | int v_equ = BN_cmp(val.openssl, v_big); | |
162 | BN_free(v_big); | |
163 | return v_equ == -1; | |
164 | } else { | |
165 | return BN_cmp(val.openssl, v.val.openssl) == -1; | |
166 | } | |
167 | } | |
168 | } | |
169 | ||
170 | int_val_t int_val_t::operator-() const | |
171 | { | |
172 | if (native_flag) { | |
173 | if (val.native == LLONG_MIN) { | |
174 | BIGNUM *result = int_val_t(LLONG_MIN).to_openssl(); | |
175 | BN_set_negative(result, 0); | |
176 | return int_val_t(result); | |
177 | } else { | |
178 | return int_val_t(-val.native); | |
179 | } | |
180 | } else { | |
181 | BIGNUM *llong_max_plus_one = int_val_t(LLONG_MIN).to_openssl(); | |
182 | BN_set_negative(llong_max_plus_one, 0); | |
183 | int cmp = BN_cmp(val.openssl, llong_max_plus_one); | |
184 | BN_free(llong_max_plus_one); | |
185 | if (cmp == 0) { | |
186 | return int_val_t(LLONG_MIN); | |
187 | } else { | |
188 | BIGNUM *result = BN_dup(val.openssl); | |
189 | BN_set_negative(result, !BN_is_negative(result)); | |
190 | return int_val_t(result); | |
191 | } | |
192 | } | |
193 | } | |
194 | ||
195 | int_val_t int_val_t::operator+(const int_val_t& right) const | |
196 | { | |
197 | // a + b = a add b | |
198 | // a + -b = a sub b | |
199 | // -a + b = b sub a | |
200 | // -a + -b = -(a add b) | |
201 | // Use only inline functions and BN_* directly. Call out for operator- in | |
202 | // the beginning. | |
203 | bool a_neg = is_negative(); | |
204 | bool b_neg = right.is_negative(); | |
205 | bool r_neg = a_neg && b_neg; | |
206 | if (!a_neg && b_neg) return operator-(-right); | |
207 | if (a_neg && !b_neg) return right.operator-(-(*this)); | |
208 | if (native_flag) { | |
209 | long long result_; | |
210 | if (right.is_native()) { | |
211 | unsigned long long result = val.native + right.get_val(); | |
212 | result_ = val.native + right.get_val(); | |
213 | if (static_cast<long long>(result) != result_ || | |
214 | (!r_neg && result_ < 0) || (r_neg && result_ > 0)) { | |
215 | // We can safely assume that the sum of two non-negative long long | |
216 | // values fit in an unsigned long long. limits.h says: | |
217 | // # ifndef ULLONG_MAX | |
218 | // # define ULLONG_MAX (LLONG_MAX * 2ULL + 1) | |
219 | // # endif | |
220 | // This is the most complicated case. We cannot be sure that | |
221 | // sizeof(BN_ULONG) == sizeof(long long). First convert the long long | |
222 | // to string and feed BN_dec2bn. | |
223 | BIGNUM *left_ = to_openssl(); | |
224 | BIGNUM *right_ = right.to_openssl(); | |
225 | BN_add(left_, left_, right_); | |
226 | BN_free(right_); | |
227 | return int_val_t(left_); | |
228 | } else { | |
229 | return int_val_t(result_); | |
230 | } | |
231 | } else { | |
232 | // long long (>= 0) + BIGNUM == BIGNUM. | |
233 | BIGNUM *result = BN_new(); | |
234 | BIGNUM *left_ = to_openssl(); | |
235 | BN_add(result, left_, right.get_val_openssl()); | |
236 | return int_val_t(result); | |
237 | } | |
238 | } else { | |
239 | // BIGNUM + long long (>= 0) == BIGNUM. | |
240 | BIGNUM *result = BN_new(); | |
241 | BIGNUM *right_; | |
242 | right_ = right.is_native() ? right.to_openssl() : right.get_val_openssl(); | |
243 | BN_add(result, val.openssl, right_); | |
244 | if (right.is_native()) | |
245 | BN_free(right_); | |
246 | return int_val_t(result); | |
247 | } | |
248 | } | |
249 | ||
250 | int_val_t int_val_t::operator-(const int_val_t& right) const | |
251 | { | |
252 | // a - b = a sub b | |
253 | // -a - b = -(a add b) | |
254 | // a - -b = a add b | |
255 | // -a - -b = -a add b = b sub a | |
256 | bool a_neg = is_negative(); | |
257 | bool b_neg = right.is_negative(); | |
258 | if (!a_neg && b_neg) return operator+(-right); | |
259 | if (a_neg && !b_neg) return -right.operator+(-(*this)); | |
260 | if (native_flag) { | |
261 | if (right.is_native()) { | |
262 | // Since both operands are non-negative the most negative result of a | |
263 | // subtraction can be -LLONG_MAX and according to limits.h: | |
264 | // # ifndef LLONG_MIN | |
265 | // # define LLONG_MIN (-LLONG_MAX-1) | |
266 | // # endif | |
267 | return int_val_t(val.native - right.get_val()); | |
268 | } else { | |
269 | BIGNUM *left_bn = to_openssl(); | |
270 | BN_sub(left_bn, left_bn, right.get_val_openssl()); | |
271 | // The result can be small enough to fit in long long. The same is true | |
272 | // for division. Back conversion is a really costly operation using | |
273 | // strings all the time. TODO Improve it. | |
274 | if (BN_num_bits(left_bn) <= (int)sizeof(long long) * 8 - 1) { | |
275 | char *result_str = BN_bn2dec(left_bn); | |
276 | Int result_ll = string2Int(result_str, Location()); | |
277 | OPENSSL_free(result_str); | |
278 | BN_free(left_bn); | |
279 | return int_val_t(result_ll); | |
280 | } else { | |
281 | return int_val_t(left_bn); | |
282 | } | |
283 | } | |
284 | } else { | |
285 | BIGNUM *result = BN_new(); | |
286 | BIGNUM *right_bn; | |
287 | right_bn = right.is_native() ? right.to_openssl() : | |
288 | right.get_val_openssl(); | |
289 | BN_sub(result, val.openssl, right_bn); | |
290 | if (right.is_native()) BN_free(right_bn); | |
291 | if (BN_num_bits(result) <= (int)sizeof(long long) * 8 - 1) { | |
292 | char *result_str = BN_bn2dec(result); | |
293 | Int result_ll = string2Int(result_str, Location()); | |
294 | OPENSSL_free(result_str); | |
295 | return int_val_t(result_ll); | |
296 | } else { | |
297 | return int_val_t(result); | |
298 | } | |
299 | } | |
300 | } | |
301 | ||
302 | int_val_t int_val_t::operator*(const int_val_t& right) const | |
303 | { | |
304 | if ((native_flag && val.native == 0LL) || | |
305 | (right.native_flag && right.val.native == 0LL)) | |
306 | return int_val_t(0LL); | |
307 | if (native_flag) { | |
308 | if (right.native_flag) { | |
309 | // 2^15 is used as a simple heuristic. | |
310 | // TODO: Improve. | |
311 | if (ll_abs(val.native) < 32768LL && ll_abs(right.val.native) < 32768LL) { | |
312 | return int_val_t(val.native * right.val.native); | |
313 | } else { | |
314 | BIGNUM *left_bn = to_openssl(); | |
315 | BIGNUM *right_bn = right.to_openssl(); | |
316 | BN_CTX *ctx = BN_CTX_new(); | |
970ed795 EL |
317 | BN_mul(left_bn, left_bn, right_bn, ctx); |
318 | BN_CTX_free(ctx); | |
319 | BN_free(right_bn); | |
320 | if (BN_num_bits(left_bn) < (int)sizeof(long long) * 8) { | |
321 | BN_free(left_bn); | |
322 | return int_val_t(val.native * right.val.native); | |
323 | } else { | |
324 | return int_val_t(left_bn); | |
325 | } | |
326 | } | |
327 | } else { | |
328 | BIGNUM *this_bn = to_openssl(); | |
329 | BN_CTX *ctx = BN_CTX_new(); | |
970ed795 EL |
330 | BN_mul(this_bn, this_bn, right.get_val_openssl(), ctx); |
331 | BN_CTX_free(ctx); | |
332 | return int_val_t(this_bn); | |
333 | } | |
334 | } else { | |
335 | BIGNUM *result = BN_new(); | |
336 | BIGNUM *right_bn; | |
337 | BN_CTX *ctx = BN_CTX_new(); | |
970ed795 EL |
338 | right_bn = right.native_flag ? right.to_openssl() |
339 | : right.get_val_openssl(); | |
340 | BN_mul(result, val.openssl, right_bn, ctx); | |
341 | BN_CTX_free(ctx); | |
342 | if (right.native_flag) BN_free(right_bn); | |
343 | return int_val_t(result); | |
344 | } | |
345 | } | |
346 | ||
347 | int_val_t int_val_t::operator/(const int_val_t& right) const | |
348 | { | |
349 | if (native_flag && val.native == 0LL) | |
350 | return int_val_t(0LL); | |
351 | if (right.is_native() && right.get_val() == 0LL) | |
352 | FATAL_ERROR("Division by zero after semantic check"); | |
353 | if (native_flag) { | |
354 | if (right.native_flag) { | |
355 | return int_val_t(val.native / right.get_val()); | |
356 | } else { | |
357 | BIGNUM *left_bn = to_openssl(); | |
358 | BN_CTX *ctx = BN_CTX_new(); | |
970ed795 EL |
359 | BN_div(left_bn, NULL, left_bn, right.get_val_openssl(), ctx); |
360 | BN_CTX_free(ctx); | |
361 | if (BN_num_bits(left_bn) <= (int)sizeof(long long) * 8 - 1) { | |
362 | char *result_str = BN_bn2dec(left_bn); | |
363 | Int result_ll = string2Int(result_str, Location()); | |
364 | OPENSSL_free(result_str); | |
365 | BN_free(left_bn); | |
366 | return int_val_t(result_ll); | |
367 | } else { | |
368 | return int_val_t(left_bn); | |
369 | } | |
370 | } | |
371 | } else { | |
372 | BIGNUM *result = BN_new(); | |
373 | BIGNUM *right_bn; | |
374 | BN_CTX *ctx = BN_CTX_new(); | |
970ed795 EL |
375 | right_bn = right.is_native() ? right.to_openssl() : |
376 | right.get_val_openssl(); | |
377 | BN_div(result, NULL, val.openssl, right_bn, ctx); | |
378 | BN_CTX_free(ctx); | |
379 | if (BN_num_bits(result) <= (int)sizeof(long long) * 8 - 1) { | |
380 | char *result_str = BN_bn2dec(result); | |
381 | Int result_ll = string2Int(result_str, Location()); | |
382 | OPENSSL_free(result_str); | |
383 | return int_val_t(result_ll); | |
384 | } else { | |
385 | if (right.is_native()) | |
386 | BN_free(right_bn); | |
387 | return int_val_t(result); | |
388 | } | |
389 | } | |
390 | } | |
391 | ||
392 | int_val_t int_val_t::operator&(Int right) const | |
393 | { | |
394 | // TODO Right can be int_val_t. Now it works only if right fits in | |
395 | // BN_ULONG. If it's not true right must be converted to BIGNUM and the | |
396 | // bits should be set with BN_is_bit_set/BN_set_bit. | |
397 | BN_ULONG right_bn_ulong = (BN_ULONG)right; | |
398 | if (right != (long long)right_bn_ulong) | |
399 | FATAL_ERROR("Bitmask is too big"); | |
400 | if (native_flag) { | |
401 | return int_val_t(val.native & right); | |
402 | } else { | |
403 | BIGNUM *tmp = BN_dup(val.openssl); | |
404 | BN_mask_bits(tmp, sizeof(BN_ULONG) * 8); | |
405 | BN_ULONG word = BN_get_word(tmp); | |
406 | BN_free(tmp); | |
407 | return int_val_t(word & right_bn_ulong); | |
408 | } | |
409 | } | |
410 | ||
411 | int_val_t int_val_t::operator>>(Int right) const | |
412 | { | |
413 | if (native_flag) { | |
414 | // Shifting right (or left) with a number greater or equal to the bits of | |
415 | // the type of the left operand has an undefined behaviour. | |
416 | // http://bytes.com/groups/c/495137-right-shift-weird-result-why | |
417 | Int shifted_value = right >= static_cast<Int>(sizeof(Int) * 8) ? 0 : | |
418 | val.native >> right; | |
419 | return int_val_t(shifted_value); | |
420 | } else { | |
421 | BIGNUM *result = BN_new(); | |
422 | BN_rshift(result, val.openssl, right); | |
423 | if (BN_num_bits(result) < (int)sizeof(long long) * 8 - 1) { | |
424 | char *result_str = BN_bn2dec(result); | |
425 | Int result_ll = string2Int(result_str, Location()); | |
426 | OPENSSL_free(result_str); | |
427 | BN_free(result); | |
428 | return int_val_t(result_ll); | |
429 | } else { | |
430 | return int_val_t(result); | |
431 | } | |
432 | } | |
433 | } | |
434 | ||
435 | const Int& int_val_t::get_val() const | |
436 | { | |
437 | if (!native_flag) FATAL_ERROR("Invalid conversion of a large integer value"); | |
438 | return val.native; | |
439 | } | |
440 | ||
441 | BIGNUM *int_val_t::get_val_openssl() const | |
442 | { | |
443 | if (native_flag) FATAL_ERROR("Invalid conversion of a large integer value"); | |
444 | return val.openssl; | |
445 | } | |
446 | ||
447 | Real int_val_t::to_real() const | |
448 | { | |
449 | if (native_flag) { | |
450 | return (double)val.native; | |
451 | } else { | |
452 | char *result_str = BN_bn2dec(val.openssl); | |
453 | Real result = 0.0; | |
454 | // Use fixed-point notation. The mantissa is usually at most 52-bits. | |
455 | // Bigger integer values will be rounded. | |
456 | if (sscanf(result_str, "%lf", &result) != 1) | |
457 | FATAL_ERROR("Conversion of integer value `%s' to float failed", | |
458 | result_str); // No deallocation, it'll crash anyway... | |
459 | OPENSSL_free(result_str); | |
460 | return result; | |
461 | } | |
462 | } | |
463 | ||
464 | int_val_t& int_val_t::operator=(const int_val_t& right) | |
465 | { | |
466 | if (!native_flag) BN_free(val.openssl); | |
467 | native_flag = right.native_flag; | |
468 | if (native_flag) val.native = right.get_val(); | |
469 | else val.openssl = BN_dup(right.get_val_openssl()); | |
470 | return *this; | |
471 | } | |
472 | ||
473 | int_val_t& int_val_t::operator<<=(Int right) | |
474 | { | |
475 | // It makes no sense to support negative operands. GCC returns constant "0" | |
476 | // with "warning: left shift count is negative" for these shifts. | |
477 | // BN_set_word is not enough since sizeof(BN_ULONG) != sizeof(long long). | |
478 | // In TTCN-3 <<= right means >>= -right. | |
479 | if (right < 0) FATAL_ERROR("The second operand of bitwise shift operators " | |
480 | "cannot be negative"); | |
481 | if (right == 0) return *this; | |
482 | if (native_flag) { | |
483 | BIGNUM *result = BN_new(); | |
484 | BN_dec2bn(&result, Int2string(val.native).c_str()); | |
485 | BN_lshift(result, result, right); | |
486 | if (BN_num_bits(result) > (int)sizeof(long long) * 8 - 1) { | |
487 | val.openssl = result; | |
488 | native_flag = false; | |
489 | } else { | |
490 | val.native <<= right; | |
491 | BN_free(result); | |
492 | } | |
493 | } else { | |
494 | BN_lshift(val.openssl, val.openssl, right); | |
495 | } | |
496 | return *this; | |
497 | } | |
498 | ||
499 | int_val_t& int_val_t::operator>>=(Int right) | |
500 | { | |
501 | if (right < 0) FATAL_ERROR("The second operand of bitwise shift operators " | |
502 | "cannot be negative"); | |
503 | if (right == 0) return *this; | |
504 | if (native_flag) { | |
505 | val.native >>= right; | |
506 | } else { | |
507 | BN_rshift(val.openssl, val.openssl, right); | |
508 | if (BN_num_bits(val.openssl) <= (int)sizeof(long long) * 8 - 1) { | |
509 | char *result_str = BN_bn2dec(val.openssl); | |
510 | Int result_ll = string2Int(result_str, Location()); | |
511 | OPENSSL_free(result_str); | |
512 | native_flag = true; | |
513 | BN_free(val.openssl); | |
514 | val.native = result_ll; | |
515 | } | |
516 | } | |
517 | return *this; | |
518 | } | |
519 | ||
520 | int_val_t& int_val_t::operator+=(Int right) | |
521 | { | |
522 | // Unfortunately we have to check the sign of the "right" operand and | |
523 | // perform addition or subtraction accordingly. | |
524 | if (right == 0) return *this; | |
525 | bool neg = right < 0; | |
526 | if (native_flag) { | |
527 | BIGNUM *result = BN_new(); | |
528 | BN_set_word(result, (BN_ULONG)val.native); | |
529 | if (neg) BN_sub_word(result, (BN_ULONG)right); | |
530 | else BN_add_word(result, (BN_ULONG)right); | |
531 | if (BN_num_bits(result) > (int)sizeof(long long) * 8 - 1) { | |
532 | val.openssl = result; | |
533 | native_flag = false; | |
534 | } else { | |
535 | val.native += right; | |
536 | BN_free(result); | |
537 | } | |
538 | } else { | |
539 | if (neg) BN_sub_word(val.openssl, (BN_ULONG)right); | |
540 | else BN_add_word(val.openssl, (BN_ULONG)right); | |
541 | if (BN_num_bits(val.openssl) <= (int)sizeof(long long) * 8 - 1) { | |
542 | // TODO BN_ULONG != long long. | |
543 | BN_ULONG tmp = BN_get_word(val.openssl); | |
544 | if (BN_is_negative(val.openssl)) tmp *= -1; | |
545 | BN_free(val.openssl); | |
546 | val.native = tmp; | |
547 | native_flag = true; | |
548 | } | |
549 | } | |
550 | return *this; | |
551 | } | |
552 | ||
553 | } // Common |