Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

975 lines
27 KiB

  1. /* Copyright (C) Boris Nikolaus, Germany, 1996-1997. All rights reserved. */
  2. /* Copyright (C) Microsoft Corporation, 1997-1998. All rights reserved. */
  3. // lonchanc: we seem to have a significant amount of memory leak
  4. // while dealing with real number and unlimited integers.
  5. // we definitely want to re-visit all the following routines carefully
  6. // in the future.
  7. // moreover, we need to make sure all the memory allocation and free
  8. // are either using encoding and decoding memory manager or kernel one.
  9. // need to make sure we do not mix them together.
  10. #include "precomp.h"
  11. /* builtin intx values */
  12. ASN1octet_t ASN1intx_0_[] = { 0 };
  13. ASN1intx_t ASN1intx_0 = { 1, ASN1intx_0_ };
  14. ASN1octet_t ASN1intx_1_[] = { 1 };
  15. ASN1intx_t ASN1intx_1 = { 1, ASN1intx_1_ };
  16. ASN1octet_t ASN1intx_2_[] = { 2 };
  17. ASN1intx_t ASN1intx_2 = { 1, ASN1intx_2_ };
  18. ASN1octet_t ASN1intx_16_[] = { 16 };
  19. ASN1intx_t ASN1intx_16 = { 1, ASN1intx_16_ };
  20. ASN1octet_t ASN1intx_256_[] = { 1, 0 };
  21. ASN1intx_t ASN1intx_256 = { 2, ASN1intx_256_ };
  22. ASN1octet_t ASN1intx_64K_[] = { 1, 0, 0 };
  23. ASN1intx_t ASN1intx_64K = { 3, ASN1intx_64K_ };
  24. ASN1octet_t ASN1intx_1G_[] = { 64, 0, 0, 0 };
  25. ASN1intx_t ASN1intx_1G = { 4, ASN1intx_1G_ };
  26. /* add two intx values */
  27. void ASN1intx_add(ASN1intx_t *dst, ASN1intx_t *arg1, ASN1intx_t *arg2)
  28. {
  29. ASN1octet_t *v;
  30. int l;
  31. int s1, s2;
  32. int o1, o2;
  33. int i;
  34. int c;
  35. int w;
  36. /* get signs */
  37. s1 = arg1->value[0] > 0x7f ? 0xff : 0x00;
  38. s2 = arg2->value[0] > 0x7f ? 0xff : 0x00;
  39. /* result length will be <= l */
  40. l = arg1->length > arg2->length ? arg1->length + 1 : arg2->length + 1;
  41. /* offset into values */
  42. o1 = l - arg1->length;
  43. o2 = l - arg2->length;
  44. /* allocate result */
  45. v = (ASN1octet_t *)MemAlloc(l, UNKNOWN_MODULE);
  46. if (v)
  47. {
  48. /* clear carry bit */
  49. c = 0;
  50. /* add octet by octet */
  51. for (i = l - 1; i >= 0; i--) {
  52. w = (i >= o1 ? arg1->value[i - o1] : s1) + (i >= o2 ? arg2->value[i - o2] : s2) + c;
  53. v[i] = (ASN1octet_t)w;
  54. c = w > 0xff;
  55. }
  56. /* octets which may shall dropped */
  57. w = v[0] > 0x7f ? 0xff : 0x00;
  58. /* count octets that shall be dropped */
  59. for (i = 0; i < l - 1; i++) {
  60. if (v[i] != w)
  61. break;
  62. }
  63. if ((v[i] ^ w) & 0x80)
  64. i--;
  65. /* allocate and copy result */
  66. dst->length = l - i;
  67. dst->value = (ASN1octet_t *)MemAlloc(l - i, UNKNOWN_MODULE);
  68. if (dst->value)
  69. {
  70. CopyMemory(dst->value, v + i, l - i);
  71. }
  72. MemFree(v);
  73. }
  74. }
  75. /* substract two intx values */
  76. void ASN1intx_sub(ASN1intx_t *dst, ASN1intx_t *arg1, ASN1intx_t *arg2)
  77. {
  78. ASN1octet_t *v;
  79. int l;
  80. int s1, s2;
  81. int o1, o2;
  82. int i;
  83. int c;
  84. int w;
  85. /* get signs */
  86. s1 = arg1->value[0] > 0x7f ? 0xff : 0x00;
  87. s2 = arg2->value[0] > 0x7f ? 0xff : 0x00;
  88. /* result length will be <= l */
  89. l = arg1->length > arg2->length ? arg1->length + 1 : arg2->length + 1;
  90. /* offset into values */
  91. o1 = l - arg1->length;
  92. o2 = l - arg2->length;
  93. /* allocate result */
  94. v = (ASN1octet_t *)MemAlloc(l, UNKNOWN_MODULE);
  95. if (v)
  96. {
  97. /* clear borrow bit */
  98. c = 0;
  99. /* substract octet by octet */
  100. for (i = l - 1; i >= 0; i--) {
  101. w = (i >= o1 ? arg1->value[i - o1] : s1) - (i >= o2 ? arg2->value[i - o2] : s2) - c;
  102. v[i] = (ASN1octet_t)w;
  103. c = w < 0;
  104. }
  105. /* octets which may shall dropped */
  106. w = v[0] > 0x7f ? 0xff : 0x00;
  107. /* count octets that shall be dropped */
  108. for (i = 0; i < l - 1; i++) {
  109. if (v[i] != w)
  110. break;
  111. }
  112. if ((v[i] ^ w) & 0x80)
  113. i--;
  114. // lonchanc: do we forget to free dst->value???
  115. // in case that dst and arg1 are identical. for instance,
  116. // ASN1BEREncReal() calls ASN1intx_sub(&exponent, &exponent, &help);
  117. /* allocate and copy result */
  118. dst->length = l - i;
  119. dst->value = (ASN1octet_t *)MemAlloc(l - i, UNKNOWN_MODULE);
  120. if (dst->value)
  121. {
  122. CopyMemory(dst->value, v + i, l - i);
  123. }
  124. MemFree(v);
  125. }
  126. }
  127. /* add one octet to an intx */
  128. #ifdef ENABLE_ALL
  129. void ASN1intx_addoctet(ASN1intx_t *dst, ASN1intx_t *arg1, ASN1octet_t arg2)
  130. {
  131. ASN1octet_t *v;
  132. int l;
  133. int i;
  134. int c;
  135. int w;
  136. /* result length will be <= l */
  137. l = arg1->length + 1;
  138. /* allocate result */
  139. v = (ASN1octet_t *)MemAlloc(l, UNKNOWN_MODULE);
  140. if (v)
  141. {
  142. /* add octet by octet */
  143. c = arg2;
  144. for (i = l - 2; i >= 0; i--) {
  145. w = arg1->value[i] + c;
  146. v[i + 1] = (ASN1octet_t)w;
  147. c = (w > 0xff);
  148. }
  149. v[0] = arg1->value[0] > 0x7f ? (ASN1octet_t)(0xff + c) : (ASN1octet_t)c;
  150. /* octets which may shall dropped */
  151. w = v[0] > 0x7f ? 0xff : 0x00;
  152. /* count octets that shall be dropped */
  153. for (i = 0; i < l - 1; i++) {
  154. if (v[i] != w)
  155. break;
  156. }
  157. if ((v[i] ^ w) & 0x80)
  158. i--;
  159. /* allocate and copy result */
  160. dst->length = l - i;
  161. dst->value = (ASN1octet_t *)MemAlloc(l - i, UNKNOWN_MODULE);
  162. if (dst->value)
  163. {
  164. CopyMemory(dst->value, v + i, l - i);
  165. }
  166. MemFree(v);
  167. }
  168. }
  169. #endif // ENABLE_ALL
  170. /* substract one octet to an intx */
  171. #ifdef ENABLE_ALL
  172. void ASN1intx_suboctet(ASN1intx_t *dst, ASN1intx_t *arg1, ASN1octet_t arg2)
  173. {
  174. ASN1octet_t *v;
  175. int l;
  176. int i;
  177. int c;
  178. int w;
  179. /* result length will be <= l */
  180. l = arg1->length + 1;
  181. /* allocate result */
  182. v = (ASN1octet_t *)MemAlloc(l, UNKNOWN_MODULE);
  183. if (v)
  184. {
  185. /* substract octet by octet */
  186. c = arg2;
  187. for (i = l - 2; i >= 0; i--) {
  188. w = arg1->value[i] - c;
  189. v[i + 1] = (ASN1octet_t)w;
  190. c = (w < 0);
  191. }
  192. v[0] = arg1->value[0] > 0x7f ? (ASN1octet_t)(0xff - c) : (ASN1octet_t)c;
  193. /* octets which may shall dropped */
  194. w = v[0] > 0x7f ? 0xff : 0x00;
  195. /* count octets that shall be dropped */
  196. for (i = 0; i < l - 1; i++) {
  197. if (v[i] != w)
  198. break;
  199. }
  200. if ((v[i] ^ w) & 0x80)
  201. i--;
  202. /* allocate and copy result */
  203. dst->length = l - i;
  204. dst->value = (ASN1octet_t *)MemAlloc(l - i, UNKNOWN_MODULE);
  205. if (dst->value)
  206. {
  207. CopyMemory(dst->value, v + i, l - i);
  208. }
  209. MemFree(v);
  210. }
  211. }
  212. #endif // ENABLE_ALL
  213. /* multiply intx by an octet */
  214. void ASN1intx_muloctet(ASN1intx_t *dst, ASN1intx_t *arg1, ASN1octet_t arg2)
  215. {
  216. ASN1octet_t *v;
  217. int l;
  218. int c;
  219. int i;
  220. int w;
  221. /* result length will be <= l */
  222. l = arg1->length + 1;
  223. /* allocate result */
  224. v = (ASN1octet_t *)MemAlloc(l, UNKNOWN_MODULE);
  225. if (v)
  226. {
  227. /* multiply octet by octet */
  228. c = 0;
  229. for (i = l - 2; i >= 0; i--) {
  230. w = arg1->value[i] * arg2 + c;
  231. v[i + 1] = (ASN1octet_t)w;
  232. c = w >> 8;
  233. }
  234. v[0] = (ASN1octet_t)(arg1->value[0] > 0x7f ? 0xff * arg2 + c : c);
  235. /* octets which may shall dropped */
  236. w = v[0] > 0x7f ? 0xff : 0x00;
  237. /* count octets that shall be dropped */
  238. for (i = 0; i < l - 1; i++) {
  239. if (v[i] != w)
  240. break;
  241. }
  242. if ((v[i] ^ w) & 0x80)
  243. i--;
  244. /* allocate and copy result */
  245. dst->length = l - i;
  246. dst->value = (ASN1octet_t *)MemAlloc(l - i, UNKNOWN_MODULE);
  247. if (dst->value)
  248. {
  249. CopyMemory(dst->value, v + i, l - i);
  250. }
  251. MemFree(v);
  252. }
  253. }
  254. /* increment an intx */
  255. #ifdef ENABLE_ALL
  256. void ASN1intx_inc(ASN1intx_t *val)
  257. {
  258. ASN1octet_t *v;
  259. int l;
  260. int i;
  261. int w;
  262. /* result length will be <= l */
  263. l = val->length + 1;
  264. /* allocate result */
  265. v = (ASN1octet_t *)MemAlloc(l, UNKNOWN_MODULE);
  266. if (v)
  267. {
  268. /* copy value */
  269. CopyMemory(v + 1, val->value, l - 1);
  270. MemFree(val->value);
  271. v[0] = v[1] > 0x7f ? 0xff : 0x00;
  272. /* increment value */
  273. for (i = l - 1; i >= 0; i--) {
  274. if (++v[i])
  275. break;
  276. }
  277. /* octets which may shall dropped */
  278. w = v[0] > 0x7f ? 0xff : 0x00;
  279. /* count octets that shall be dropped */
  280. for (i = 0; i < l - 1; i++) {
  281. if (v[i] != w)
  282. break;
  283. }
  284. if ((v[i] ^ w) & 0x80)
  285. i--;
  286. /* allocate and copy result */
  287. val->length = l - i;
  288. val->value = (ASN1octet_t *)MemAlloc(l - i, UNKNOWN_MODULE);
  289. if (val->value)
  290. {
  291. CopyMemory(val->value, v + i, l - i);
  292. }
  293. MemFree(v);
  294. }
  295. }
  296. #endif // ENABLE_ALL
  297. /* decrement an intx */
  298. #ifdef ENABLE_ALL
  299. void ASN1intx_dec(ASN1intx_t *val)
  300. {
  301. ASN1octet_t *v;
  302. int l;
  303. int i;
  304. int w;
  305. /* result length will be <= l */
  306. l = val->length + 1;
  307. /* allocate result */
  308. v = (ASN1octet_t *)MemAlloc(l, UNKNOWN_MODULE);
  309. if (v)
  310. {
  311. /* copy value */
  312. CopyMemory(v + 1, val->value, l - 1);
  313. MemFree(val->value);
  314. v[0] = v[1] > 0x7f ? 0xff : 0x00;
  315. /* decrement value */
  316. for (i = l - 1; i >= 0; i--) {
  317. if (v[i]--)
  318. break;
  319. }
  320. /* octets which may shall dropped */
  321. w = v[0] > 0x7f ? 0xff : 0x00;
  322. /* count octets that shall be dropped */
  323. for (i = 0; i < l - 1; i++) {
  324. if (v[i] != w)
  325. break;
  326. }
  327. if ((v[i] ^ w) & 0x80)
  328. i--;
  329. /* allocate and copy result */
  330. val->length = l - i;
  331. val->value = (ASN1octet_t *)MemAlloc(l - i, UNKNOWN_MODULE);
  332. if (val->value)
  333. {
  334. CopyMemory(val->value, v + i, l - i);
  335. }
  336. MemFree(v);
  337. }
  338. }
  339. #endif // ENABLE_ALL
  340. /* negate an intx value */
  341. #ifdef ENABLE_ALL
  342. void ASN1intx_neg(ASN1intx_t *dst, ASN1intx_t *arg)
  343. {
  344. ASN1uint32_t i;
  345. /* duplicate value */
  346. if (ASN1intx_dup(dst, arg))
  347. {
  348. /* ones complement */
  349. for (i = 0; i < dst->length; i++)
  350. dst->value[i] = ~dst->value[i];
  351. /* and increment */
  352. ASN1intx_inc(dst);
  353. }
  354. }
  355. #endif // ENABLE_ALL
  356. /* returns floor(log2(arg - 1)) */
  357. #ifdef ENABLE_ALL
  358. ASN1uint32_t ASN1intx_log2(ASN1intx_t *arg)
  359. {
  360. ASN1uint32_t i;
  361. ASN1intx_t v;
  362. ASN1uint32_t n;
  363. if (ASN1intx_dup(&v, arg))
  364. {
  365. ASN1intx_dec(&v);
  366. if (v.value[0] > 0x7f) {
  367. ASN1intx_free(&v);
  368. return 0;
  369. }
  370. for (i = 0; i < v.length; i++) {
  371. if (v.value[i])
  372. break;
  373. }
  374. if (i >= v.length) {
  375. n = 0;
  376. } else if (v.value[i] > 0x7f) {
  377. n = 8 * (v.length - i - 1) + 8;
  378. } else if (v.value[i] > 0x3f) {
  379. n = 8 * (v.length - i - 1) + 7;
  380. } else if (v.value[i] > 0x1f) {
  381. n = 8 * (v.length - i - 1) + 6;
  382. } else if (v.value[i] > 0x0f) {
  383. n = 8 * (v.length - i - 1) + 5;
  384. } else if (v.value[i] > 0x07) {
  385. n = 8 * (v.length - i - 1) + 4;
  386. } else if (v.value[i] > 0x03) {
  387. n = 8 * (v.length - i - 1) + 3;
  388. } else if (v.value[i] > 0x01) {
  389. n = 8 * (v.length - i - 1) + 2;
  390. } else {
  391. n = 8 * (v.length - i - 1) + 1;
  392. }
  393. ASN1intx_free(&v);
  394. return n;
  395. }
  396. return 0;
  397. }
  398. #endif // ENABLE_ALL
  399. /* returns floor(log2(arg - 1)) */
  400. #ifdef ENABLE_ALL
  401. ASN1uint32_t ASN1uint32_log2(ASN1uint32_t arg)
  402. {
  403. ASN1uint32_t i;
  404. arg--;
  405. for (i = 32; i != 0; i--) {
  406. if (arg & (1 << (i - 1)))
  407. break;
  408. }
  409. return i;
  410. }
  411. #endif // ENABLE_ALL
  412. /* returns floor(log256(arg - 1)) */
  413. #ifdef ENABLE_ALL
  414. ASN1uint32_t ASN1intx_log256(ASN1intx_t *arg)
  415. {
  416. ASN1uint32_t i;
  417. ASN1intx_t v;
  418. if (ASN1intx_dup(&v, arg))
  419. {
  420. ASN1intx_dec(&v);
  421. if (v.value[0] > 0x7f) {
  422. ASN1intx_free(&v);
  423. return 0;
  424. }
  425. for (i = 0; i < v.length; i++) {
  426. if (v.value[i])
  427. break;
  428. }
  429. ASN1intx_free(&v);
  430. return v.length - i;
  431. }
  432. return 0;
  433. }
  434. #endif // ENABLE_ALL
  435. /* returns floor(log256(arg - 1)) */
  436. #ifdef ENABLE_ALL
  437. ASN1uint32_t ASN1uint32_log256(ASN1uint32_t arg)
  438. {
  439. if (arg > 0x10000) {
  440. if (arg > 0x1000000)
  441. return 4;
  442. return 3;
  443. }
  444. if (arg > 0x100)
  445. return 2;
  446. if (arg > 1)
  447. return 1;
  448. return 0;
  449. }
  450. #endif // ENABLE_ALL
  451. /* compare two intx values; return 0 iff equal */
  452. #ifdef ENABLE_ALL
  453. ASN1int32_t ASN1intx_cmp(ASN1intx_t *arg1, ASN1intx_t *arg2)
  454. {
  455. int s1, s2;
  456. int o1, o2;
  457. int l;
  458. int i;
  459. int d;
  460. s1 = arg1->value[0] > 0x7f ? 0xff : 0x00;
  461. s2 = arg2->value[0] > 0x7f ? 0xff : 0x00;
  462. if (s1 != s2)
  463. return s1 == 0xff ? -1 : 1;
  464. l = arg1->length > arg2->length ? arg1->length : arg2->length;
  465. o1 = l - arg1->length;
  466. o2 = l - arg2->length;
  467. for (i = 0; i < l; i++) {
  468. d = (i >= o1 ? arg1->value[i - o1] : s1) - (i >= o2 ? arg2->value[i - o2] : s2);
  469. if (d)
  470. return d;
  471. }
  472. return 0;
  473. }
  474. #endif // ENABLE_ALL
  475. /* create an intx value from an uint32 value */
  476. #ifdef ENABLE_ALL
  477. void ASN1intx_setuint32(ASN1intx_t *dst, ASN1uint32_t val)
  478. {
  479. ASN1octet_t o[5], *v = o;
  480. int n = 5;
  481. v[0] = 0;
  482. v[1] = (ASN1octet_t)(val >> 24);
  483. v[2] = (ASN1octet_t)(val >> 16);
  484. v[3] = (ASN1octet_t)(val >> 8);
  485. v[4] = (ASN1octet_t)(val);
  486. while (n > 1 && !*v && v[1] <= 0x7f) {
  487. n--;
  488. v++;
  489. }
  490. dst->length = n;
  491. dst->value = (ASN1octet_t *)MemAlloc(n, UNKNOWN_MODULE);
  492. if (dst->value)
  493. {
  494. CopyMemory(dst->value, v, n);
  495. }
  496. }
  497. #endif // ENABLE_ALL
  498. /* create an intx value from an int32 value */
  499. #ifdef ENABLE_ALL
  500. void ASN1intx_setint32(ASN1intx_t *dst, ASN1int32_t val)
  501. {
  502. ASN1octet_t o[5], *v = o;
  503. int n = 5;
  504. v[0] = (ASN1octet_t)(val < 0 ? 0xff : 0x00);
  505. v[1] = (ASN1octet_t)(val >> 24);
  506. v[2] = (ASN1octet_t)(val >> 16);
  507. v[3] = (ASN1octet_t)(val >> 8);
  508. v[4] = (ASN1octet_t)(val);
  509. while (n > 1 && ((!*v && v[1] <= 0x7f) || (*v == 0xff && v[1] > 0x7f))) {
  510. n--;
  511. v++;
  512. }
  513. dst->length = n;
  514. dst->value = (ASN1octet_t *)MemAlloc(n, UNKNOWN_MODULE);
  515. if (dst->value)
  516. {
  517. CopyMemory(dst->value, v, n);
  518. }
  519. }
  520. #endif // ENABLE_ALL
  521. /* copy constructor */
  522. ASN1int32_t ASN1intx_dup(ASN1intx_t *dst, ASN1intx_t *val)
  523. {
  524. dst->length = val->length;
  525. dst->value = (ASN1octet_t *)MemAlloc(val->length, UNKNOWN_MODULE);
  526. if (dst->value)
  527. {
  528. CopyMemory(dst->value, val->value, val->length);
  529. return 1;
  530. }
  531. // fail to allocate memory
  532. dst->length = 0;
  533. return 0;
  534. }
  535. /* free an intx value */
  536. void ASN1intx_free(ASN1intx_t *val)
  537. {
  538. if (val)
  539. {
  540. MemFree(val->value);
  541. }
  542. }
  543. #ifdef HAS_SIXTYFOUR_BITS
  544. /* convert an intx value to a uint64 value */
  545. #ifdef ENABLE_ALL
  546. ASN1uint64_t ASN1intx2uint64(ASN1intx_t *val)
  547. {
  548. switch (val->length) {
  549. case 1:
  550. return (ASN1uint64_t)val->value[val->length - 1];
  551. case 2:
  552. return (ASN1uint64_t)(val->value[val->length - 1] |
  553. ((ASN1uint32_t)val->value[val->length - 2] << 8));
  554. case 3:
  555. return (ASN1uint64_t)(val->value[val->length - 1] |
  556. ((ASN1uint32_t)val->value[val->length - 2] << 8) |
  557. ((ASN1uint32_t)val->value[val->length - 3] << 16));
  558. case 4:
  559. return (ASN1uint64_t)(val->value[val->length - 1] |
  560. ((ASN1uint32_t)val->value[val->length - 2] << 8) |
  561. ((ASN1uint32_t)val->value[val->length - 3] << 16) |
  562. ((ASN1uint32_t)val->value[val->length - 4] << 24));
  563. case 5:
  564. return (ASN1uint64_t)(val->value[val->length - 1] |
  565. ((ASN1uint64_t)val->value[val->length - 2] << 8) |
  566. ((ASN1uint64_t)val->value[val->length - 3] << 16) |
  567. ((ASN1uint64_t)val->value[val->length - 4] << 24) |
  568. ((ASN1uint64_t)val->value[val->length - 5] << 32));
  569. case 6:
  570. return (ASN1uint64_t)(val->value[val->length - 1] |
  571. ((ASN1uint64_t)val->value[val->length - 2] << 8) |
  572. ((ASN1uint64_t)val->value[val->length - 3] << 16) |
  573. ((ASN1uint64_t)val->value[val->length - 4] << 24) |
  574. ((ASN1uint64_t)val->value[val->length - 5] << 32) |
  575. ((ASN1uint64_t)val->value[val->length - 6] << 40));
  576. case 7:
  577. return (ASN1uint64_t)(val->value[val->length - 1] |
  578. ((ASN1uint64_t)val->value[val->length - 2] << 8) |
  579. ((ASN1uint64_t)val->value[val->length - 3] << 16) |
  580. ((ASN1uint64_t)val->value[val->length - 4] << 24) |
  581. ((ASN1uint64_t)val->value[val->length - 5] << 32) |
  582. ((ASN1uint64_t)val->value[val->length - 6] << 40) |
  583. ((ASN1uint64_t)val->value[val->length - 7] << 48));
  584. default:
  585. return (ASN1uint64_t)(val->value[val->length - 1] |
  586. ((ASN1uint64_t)val->value[val->length - 2] << 8) |
  587. ((ASN1uint64_t)val->value[val->length - 3] << 16) |
  588. ((ASN1uint64_t)val->value[val->length - 4] << 24) |
  589. ((ASN1uint64_t)val->value[val->length - 5] << 32) |
  590. ((ASN1uint64_t)val->value[val->length - 6] << 40) |
  591. ((ASN1uint64_t)val->value[val->length - 7] << 48) |
  592. ((ASN1uint64_t)val->value[val->length - 8] << 56));
  593. }
  594. }
  595. #endif // ENABLE_ALL
  596. #endif
  597. /* check if intx value is a uint64 value */
  598. #ifdef ENABLE_ALL
  599. int ASN1intxisuint64(ASN1intx_t *val)
  600. {
  601. if (val->value[0] > 0x7f)
  602. return 0;
  603. return ASN1intx_uoctets(val) <= 8;
  604. }
  605. #endif // ENABLE_ALL
  606. #ifdef HAS_SIXTYFOUR_BITS
  607. /* convert an intx value to a int64 value */
  608. #ifdef ENABLE_ALL
  609. ASN1int64_t ASN1intx2int64(ASN1intx_t *val)
  610. {
  611. switch (val->length) {
  612. case 1:
  613. return (ASN1int64_t)(ASN1int8_t)val->value[val->length - 1];
  614. case 2:
  615. return (ASN1int64_t)(ASN1int16_t)(val->value[val->length - 1] |
  616. ((ASN1uint32_t)val->value[val->length - 2] << 8));
  617. case 3:
  618. return (ASN1int64_t)(ASN1int32_t)(val->value[val->length - 1] |
  619. ((ASN1uint32_t)val->value[val->length - 2] << 8) |
  620. ((ASN1uint32_t)val->value[val->length - 3] << 16) |
  621. ((ASN1uint32_t)val->value[val->length - 3] > 0x7f ?
  622. 0xffffffffff000000LL : 0));
  623. case 4:
  624. return (ASN1int64_t)(ASN1int32_t)(val->value[val->length - 1] |
  625. ((ASN1uint32_t)val->value[val->length - 2] << 8) |
  626. ((ASN1uint32_t)val->value[val->length - 3] << 16) |
  627. ((ASN1uint32_t)val->value[val->length - 4] << 24));
  628. case 5:
  629. return (ASN1int64_t)(val->value[val->length - 1] |
  630. ((ASN1uint64_t)val->value[val->length - 2] << 8) |
  631. ((ASN1uint64_t)val->value[val->length - 3] << 16) |
  632. ((ASN1uint64_t)val->value[val->length - 4] << 24) |
  633. ((ASN1uint64_t)val->value[val->length - 5] << 32) |
  634. ((ASN1uint64_t)val->value[val->length - 5] > 0x7f ?
  635. 0xffffff0000000000LL : 0));
  636. case 6:
  637. return (ASN1int64_t)(val->value[val->length - 1] |
  638. ((ASN1uint64_t)val->value[val->length - 2] << 8) |
  639. ((ASN1uint64_t)val->value[val->length - 3] << 16) |
  640. ((ASN1uint64_t)val->value[val->length - 4] << 24) |
  641. ((ASN1uint64_t)val->value[val->length - 5] << 32) |
  642. ((ASN1uint64_t)val->value[val->length - 6] << 40) |
  643. ((ASN1uint64_t)val->value[val->length - 6] > 0x7f ?
  644. 0xffff000000000000LL : 0));
  645. case 7:
  646. return (ASN1int64_t)((ASN1uint64_t)val->value[val->length - 1] |
  647. ((ASN1uint64_t)val->value[val->length - 2] << 8) |
  648. ((ASN1uint64_t)val->value[val->length - 3] << 16) |
  649. ((ASN1uint64_t)val->value[val->length - 4] << 24) |
  650. ((ASN1uint64_t)val->value[val->length - 5] << 32) |
  651. ((ASN1uint64_t)val->value[val->length - 6] << 40) |
  652. ((ASN1uint64_t)val->value[val->length - 7] << 48) |
  653. ((ASN1uint64_t)val->value[val->length - 7] > 0x7f ?
  654. 0xff00000000000000LL : 0));
  655. default:
  656. return (ASN1int64_t)((ASN1uint64_t)val->value[val->length - 1] |
  657. ((ASN1uint64_t)val->value[val->length - 2] << 8) |
  658. ((ASN1uint64_t)val->value[val->length - 3] << 16) |
  659. ((ASN1uint64_t)val->value[val->length - 4] << 24) |
  660. ((ASN1uint64_t)val->value[val->length - 5] << 32) |
  661. ((ASN1uint64_t)val->value[val->length - 6] << 40) |
  662. ((ASN1uint64_t)val->value[val->length - 7] << 48) |
  663. ((ASN1uint64_t)val->value[val->length - 8] << 56));
  664. }
  665. }
  666. #endif // USE_ASN1intx2int64
  667. #endif
  668. /* check if intx value is an int64 value */
  669. #ifdef USE_ASN1intxisint64
  670. int
  671. ASN1intxisint64(ASN1intx_t *val)
  672. {
  673. return ASN1intx_octets(val) <= 8;
  674. }
  675. #endif // USE_ASN1intxisint64
  676. /* convert intx value to uint32 value */
  677. #ifdef ENABLE_ALL
  678. ASN1uint32_t ASN1intx2uint32(ASN1intx_t *val)
  679. {
  680. switch (val->length) {
  681. case 1:
  682. return (ASN1uint32_t)val->value[val->length - 1];
  683. case 2:
  684. return (ASN1uint32_t)(val->value[val->length - 1] |
  685. ((ASN1uint32_t)val->value[val->length - 2] << 8));
  686. case 3:
  687. return (ASN1uint32_t)(val->value[val->length - 1] |
  688. ((ASN1uint32_t)val->value[val->length - 2] << 8) |
  689. ((ASN1uint32_t)val->value[val->length - 3] << 16));
  690. default:
  691. return (ASN1uint32_t)(val->value[val->length - 1] |
  692. ((ASN1uint32_t)val->value[val->length - 2] << 8) |
  693. ((ASN1uint32_t)val->value[val->length - 3] << 16) |
  694. ((ASN1uint32_t)val->value[val->length - 4] << 24));
  695. }
  696. }
  697. #endif // ENABLE_ALL
  698. /* check if intx value is an uint32 value */
  699. #ifdef ENABLE_ALL
  700. int ASN1intxisuint32(ASN1intx_t *val)
  701. {
  702. if (val->value[0] > 0x7f)
  703. return 0;
  704. return ASN1intx_uoctets(val) <= 4;
  705. }
  706. #endif // ENABLE_ALL
  707. /* convert intx value to int32 value */
  708. ASN1int32_t ASN1intx2int32(ASN1intx_t *val)
  709. {
  710. switch (val->length) {
  711. case 1:
  712. return (ASN1int32_t)(ASN1int8_t)val->value[val->length - 1];
  713. case 2:
  714. return (ASN1int32_t)(ASN1int16_t)(val->value[val->length - 1] |
  715. ((ASN1uint32_t)val->value[val->length - 2] << 8));
  716. case 3:
  717. return (ASN1int32_t)(val->value[val->length - 1] |
  718. ((ASN1uint32_t)val->value[val->length - 2] << 8) |
  719. ((ASN1uint32_t)val->value[val->length - 3] << 16) |
  720. ((ASN1uint32_t)val->value[val->length - 3] > 0x7f ?
  721. 0xff000000 : 0));
  722. default:
  723. return (ASN1int32_t)(val->value[val->length - 1] |
  724. ((ASN1uint32_t)val->value[val->length - 2] << 8) |
  725. ((ASN1uint32_t)val->value[val->length - 3] << 16) |
  726. ((ASN1uint32_t)val->value[val->length - 4] << 24));
  727. }
  728. }
  729. /* check if intx value is an int32 value */
  730. #ifdef ENABLE_ALL
  731. int ASN1intxisint32(ASN1intx_t *val)
  732. {
  733. return ASN1intx_octets(val) <= 4;
  734. }
  735. #endif // ENABLE_ALL
  736. /* convert intx value to uint16 value */
  737. #ifdef ENABLE_ALL
  738. ASN1uint16_t ASN1intx2uint16(ASN1intx_t *val)
  739. {
  740. if (val->length == 1)
  741. return (ASN1uint16_t)val->value[val->length - 1];
  742. return (ASN1uint16_t)(val->value[val->length - 1] |
  743. ((ASN1uint32_t)val->value[val->length - 2] << 8));
  744. }
  745. #endif // ENABLE_ALL
  746. /* check if intx value is an uint16 value */
  747. #ifdef ENABLE_ALL
  748. int ASN1intxisuint16(ASN1intx_t *val)
  749. {
  750. if (val->value[0] > 0x7f)
  751. return 0;
  752. return ASN1intx_uoctets(val) <= 2;
  753. }
  754. #endif // ENABLE_ALL
  755. /* convert intx value to int16 value */
  756. #ifdef ENABLE_ALL
  757. ASN1int16_t ASN1intx2int16(ASN1intx_t *val)
  758. {
  759. if (val->length == 1)
  760. return (ASN1int16_t)(ASN1int8_t)val->value[val->length - 1];
  761. return (ASN1int16_t)(val->value[val->length - 1] |
  762. ((ASN1uint32_t)val->value[val->length - 2] << 8));
  763. }
  764. #endif // ENABLE_ALL
  765. /* check if intx value is an int16 value */
  766. #ifdef ENABLE_ALL
  767. int ASN1intxisint16(ASN1intx_t *val)
  768. {
  769. return ASN1intx_octets(val) <= 2;
  770. }
  771. #endif // ENABLE_ALL
  772. /* convert intx value to uint8 value */
  773. #ifdef ENABLE_ALL
  774. ASN1uint8_t ASN1intx2uint8(ASN1intx_t *val)
  775. {
  776. return (ASN1uint8_t)val->value[val->length - 1];
  777. }
  778. #endif // ENABLE_ALL
  779. /* check if intx value is an uint8 value */
  780. #ifdef ENABLE_ALL
  781. int ASN1intxisuint8(ASN1intx_t *val)
  782. {
  783. if (val->value[0] > 0x7f)
  784. return 0;
  785. return ASN1intx_uoctets(val) <= 1;
  786. }
  787. #endif // ENABLE_ALL
  788. /* convert intx value to int8 value */
  789. #ifdef ENABLE_ALL
  790. ASN1int8_t ASN1intx2int8(ASN1intx_t *val)
  791. {
  792. return (ASN1int8_t)val->value[val->length - 1];
  793. }
  794. #endif // ENABLE_ALL
  795. /* check if intx value is an int8 value */
  796. #ifdef ENABLE_ALL
  797. int ASN1intxisint8(ASN1intx_t *val)
  798. {
  799. return ASN1intx_octets(val) <= 1;
  800. }
  801. #endif // ENABLE_ALL
  802. /* count octets for a signed encoding of an intx value */
  803. #ifdef ENABLE_ALL
  804. ASN1uint32_t ASN1intx_octets(ASN1intx_t *val)
  805. {
  806. ASN1uint32_t i;
  807. ASN1uint32_t s;
  808. s = val->value[0] > 0x7f ? 0xff : 0x00;
  809. for (i = 0; i < val->length; i++) {
  810. if (val->value[i] != s)
  811. break;
  812. }
  813. if (i && ((val->value[i] ^ s) & 0x80))
  814. i--;
  815. return val->length - i;
  816. }
  817. #endif // ENABLE_ALL
  818. /* count octets for unsigned encoding of an unsigned intx value */
  819. ASN1uint32_t ASN1intx_uoctets(ASN1intx_t *val)
  820. {
  821. ASN1uint32_t i;
  822. for (i = 0; i < val->length; i++) {
  823. if (val->value[i])
  824. break;
  825. }
  826. return val->length - i;
  827. }
  828. /* count octets for signed encoding of an uint32 value */
  829. #ifdef ENABLE_ALL
  830. ASN1uint32_t ASN1uint32_octets(ASN1uint32_t val)
  831. {
  832. if (val >= 0x8000) {
  833. if (val >= 0x800000) {
  834. return ((val >= 0x80000000) ? 5 : 4);
  835. }
  836. return 3;
  837. }
  838. return ((val >= 0x80) ? 2 : 1);
  839. }
  840. #endif // ENABLE_ALL
  841. /* count octets for unsigned encoding of an uint32 value */
  842. ASN1uint32_t ASN1uint32_uoctets(ASN1uint32_t val)
  843. {
  844. if (val >= 0x10000) {
  845. return ((val >= 0x1000000) ? 4 : 3);
  846. }
  847. return ((val >= 0x100) ? 2 : 1);
  848. }
  849. /* count octets for signed encoding of an int32 value */
  850. ASN1uint32_t ASN1int32_octets(ASN1int32_t val)
  851. {
  852. if (val >= 0) {
  853. if (val >= 0x8000) {
  854. return ((val >= 0x800000) ? 4 : 3);
  855. }
  856. return ((val >= 0x80) ? 2 : 1);
  857. }
  858. if (val < -0x8000) {
  859. return ((val < -0x800000) ? 4 : 3);
  860. }
  861. return ((val < -0x80) ? 2 : 1);
  862. }
  863. /* convert an intx value into a double */
  864. #ifdef ENABLE_ALL
  865. double ASN1intx2double(ASN1intx_t *val)
  866. {
  867. double ret;
  868. ASN1uint32_t i;
  869. if (val->value[0] > 0x7f)
  870. ret = (double)(val->value[0] - 0x100);
  871. else
  872. ret = (double)val->value[0];
  873. for (i = 1; i < val->length; i++) {
  874. ret = ret * 256.0 + (double)val->value[i];
  875. }
  876. return ret;
  877. }
  878. #endif // ENABLE_ALL