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.

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