Source code of Windows XP (NT5)
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.

421 lines
11 KiB

  1. /* Copyright (C) Boris Nikolaus, Germany, 1996-1997. All rights reserved. */
  2. /* Copyright (C) Microsoft Corporation, 1997-1998. All rights reserved. */
  3. #include "precomp.h"
  4. static const ASN1uint8_t
  5. c_aBitMask[] = {
  6. 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff
  7. };
  8. static const ASN1uint8_t
  9. c_aBitMask2[] = {
  10. 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff
  11. };
  12. static const ASN1uint8_t
  13. c_aBitMask4[] = {
  14. 0xff, 0x7f, 0x3f, 0x1f, 0x0f, 0x07, 0x03, 0x01, 0x00
  15. };
  16. static const ASN1int32_t
  17. c_aBitMask5[] = {
  18. (ASN1int32_t)0xffffffff, (ASN1int32_t)0xfffffffe,
  19. (ASN1int32_t)0xfffffffc, (ASN1int32_t)0xfffffff8,
  20. (ASN1int32_t)0xfffffff0, (ASN1int32_t)0xffffffe0,
  21. (ASN1int32_t)0xffffffc0, (ASN1int32_t)0xffffff80,
  22. (ASN1int32_t)0xffffff00, (ASN1int32_t)0xfffffe00,
  23. (ASN1int32_t)0xfffffc00, (ASN1int32_t)0xfffff800,
  24. (ASN1int32_t)0xfffff000, (ASN1int32_t)0xffffe000,
  25. (ASN1int32_t)0xffffc000, (ASN1int32_t)0xffff8000,
  26. (ASN1int32_t)0xffff0000, (ASN1int32_t)0xfffe0000,
  27. (ASN1int32_t)0xfffc0000, (ASN1int32_t)0xfff80000,
  28. (ASN1int32_t)0xfff00000, (ASN1int32_t)0xffe00000,
  29. (ASN1int32_t)0xffc00000, (ASN1int32_t)0xff800000,
  30. (ASN1int32_t)0xff000000, (ASN1int32_t)0xfe000000,
  31. (ASN1int32_t)0xfc000000, (ASN1int32_t)0xf8000000,
  32. (ASN1int32_t)0xf0000000, (ASN1int32_t)0xe0000000,
  33. (ASN1int32_t)0xc0000000, (ASN1int32_t)0x80000000,
  34. (ASN1int32_t)0x00000000
  35. };
  36. static const ASN1uint8_t
  37. c_aBitCount[] = {
  38. 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
  39. 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
  40. 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
  41. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  42. 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
  43. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  44. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  45. 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
  46. 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
  47. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  48. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  49. 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
  50. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  51. 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
  52. 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
  53. 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8
  54. };
  55. /* copy nbits bits from src/srcbit into dst/dstbit;
  56. src points to first octet containing bits to be copied
  57. srcbit names the first bit within the first octet to be copied (0=msb, 7=lsb)
  58. dst points to first octet to copy into
  59. dstbit names the first bit within the first octet to copy into (0=msb, 7=lsb)
  60. nbits is the number of bits to copy;
  61. assumes that bits of broken octet at dst/dstbit are cleared;
  62. bits of last octet behind dst/dstbit+nbits-1 will be cleared
  63. */
  64. void ASN1bitcpy(ASN1octet_t *dst, ASN1uint32_t dstbit, ASN1octet_t *src, ASN1uint32_t srcbit, ASN1uint32_t nbits)
  65. {
  66. ASN1uint32_t xsrcbit, xdstbit;
  67. if (!nbits)
  68. return;
  69. if (dstbit >= 8) {
  70. dst += dstbit / 8;
  71. dstbit &= 7;
  72. }
  73. if (srcbit >= 8) {
  74. src += srcbit / 8;
  75. srcbit &= 7;
  76. }
  77. /* check if we have to fill broken first octet */
  78. if (dstbit) {
  79. xdstbit = 8 - dstbit;
  80. /* enough bits to fill up broken octet? */
  81. if (nbits >= xdstbit) {
  82. if (srcbit < dstbit) {
  83. *dst++ |= (*src >> (dstbit - srcbit)) & c_aBitMask[xdstbit];
  84. nbits -= xdstbit;
  85. srcbit += xdstbit;
  86. dstbit = 0;
  87. } else if (srcbit == dstbit) {
  88. *dst++ |= *src++ & c_aBitMask[xdstbit];
  89. nbits -= xdstbit;
  90. srcbit = 0;
  91. dstbit = 0;
  92. } else {
  93. *dst++ |= ((*src & c_aBitMask[8 - srcbit]) << (srcbit - dstbit)) |
  94. (src[1] >> (8 - (srcbit - dstbit)));
  95. nbits -= xdstbit;
  96. src++;
  97. srcbit -= dstbit;
  98. dstbit = 0;
  99. }
  100. /* less bits to fill than needed to fill up the broken octet */
  101. } else {
  102. if (srcbit <= dstbit) {
  103. *dst |= ((*src >> (8 - srcbit - nbits)) & c_aBitMask[nbits]) <<
  104. (xdstbit - nbits);
  105. } else {
  106. *dst++ |= ((*src & c_aBitMask[8 - srcbit]) << (srcbit - dstbit)) |
  107. ((src[1] >> (16 - srcbit - nbits)) << (xdstbit - nbits));
  108. }
  109. return;
  110. }
  111. }
  112. /* fill up complete octets */
  113. if (nbits >= 8) {
  114. if (!srcbit) {
  115. CopyMemory(dst, src, nbits / 8);
  116. dst += nbits / 8;
  117. src += nbits / 8;
  118. nbits &= 7;
  119. } else {
  120. xsrcbit = 8 - srcbit;
  121. do {
  122. *dst++ = (*src << srcbit) | (src[1] >> (xsrcbit));
  123. src++;
  124. nbits -= 8;
  125. } while (nbits >= 8);
  126. }
  127. }
  128. /* fill bits into last octet */
  129. if (nbits)
  130. {
  131. *dst = (*src << srcbit) & c_aBitMask2[nbits];
  132. // lonchanc: made the following fix for the case that
  133. // src bits across byte boundary.
  134. if (srcbit + nbits > 8)
  135. {
  136. xsrcbit = nbits - (8 - srcbit);
  137. src++;
  138. *dst |= ((*src & c_aBitMask2[xsrcbit]) >> (8 - srcbit));
  139. }
  140. }
  141. }
  142. /* clear nbits bits at dst/dstbit;
  143. bits of last octet behind dst/dstbit+nbits-1 will be cleared
  144. */
  145. void ASN1bitclr(ASN1octet_t *dst, ASN1uint32_t dstbit, ASN1uint32_t nbits)
  146. {
  147. ASN1uint32_t xdstbit;
  148. if (!nbits)
  149. return;
  150. if (dstbit >= 8) {
  151. dst += dstbit / 8;
  152. dstbit &= 7;
  153. }
  154. /* clear broken ASN1octet first */
  155. if (dstbit) {
  156. xdstbit = 8 - dstbit;
  157. *dst &= c_aBitMask2[xdstbit];
  158. if (xdstbit < nbits) {
  159. dst++;
  160. nbits -= xdstbit;
  161. } else {
  162. return;
  163. }
  164. }
  165. /* clear remaining bits */
  166. ZeroMemory(dst, (nbits + 7) / 8);
  167. }
  168. /* clear nbits bits at dst/dstbit;
  169. bits of last octet behind dst/dstbit+nbits-1 will be cleared
  170. */
  171. void ASN1bitset(ASN1octet_t *dst, ASN1uint32_t dstbit, ASN1uint32_t nbits)
  172. {
  173. ASN1uint32_t xdstbit;
  174. if (!nbits)
  175. return;
  176. if (dstbit >= 8) {
  177. dst += dstbit / 8;
  178. dstbit &= 7;
  179. }
  180. /* set broken ASN1octet first */
  181. if (dstbit) {
  182. xdstbit = 8 - dstbit;
  183. if (xdstbit < nbits) {
  184. *dst |= c_aBitMask4[xdstbit];
  185. dst++;
  186. nbits -= xdstbit;
  187. } else {
  188. *dst |= c_aBitMask4[nbits] << (xdstbit - nbits);
  189. return;
  190. }
  191. }
  192. /* set complete octets */
  193. if (nbits >= 8) {
  194. memset(dst, 0xff, nbits / 8);
  195. dst += nbits / 8;
  196. nbits &= 7;
  197. }
  198. /* set remaining bits */
  199. if (nbits)
  200. *dst |= c_aBitMask4[nbits] << (8 - nbits);
  201. }
  202. /* write nbits bits of val at dst/dstbit;
  203. assumes that bits of broken octet at dst/dstbit are cleared;
  204. bits of last octet behind dst/dstbit+nbits-1 will be cleared
  205. */
  206. void ASN1bitput(ASN1octet_t *dst, ASN1uint32_t dstbit, ASN1uint32_t val, ASN1uint32_t nbits)
  207. {
  208. ASN1uint32_t xdstbit;
  209. if (!nbits)
  210. return;
  211. if (dstbit >= 8) {
  212. dst += dstbit / 8;
  213. dstbit &= 7;
  214. }
  215. xdstbit = 8 - dstbit;
  216. /* fill up broken octet first */
  217. if (dstbit) {
  218. if (xdstbit <= nbits) {
  219. *dst++ |= val >> (nbits -= xdstbit);
  220. } else {
  221. *dst |= (val & c_aBitMask[nbits]) << (xdstbit - nbits);
  222. return;
  223. }
  224. }
  225. /* copy complete octets */
  226. while (nbits >= 8)
  227. *dst++ = (ASN1octet_t) (val >> (nbits -= 8));
  228. /* copy left bits */
  229. if (nbits)
  230. *dst = (ASN1octet_t) ((val & c_aBitMask[nbits]) << (8 - nbits));
  231. }
  232. /* read nbits bits of val at src/srcbit */
  233. // lonchanc: the return value is independent of big or little endian
  234. // because we use shift left within a long integer.
  235. ASN1uint32_t ASN1bitgetu(ASN1octet_t *src, ASN1uint32_t srcbit, ASN1uint32_t nbits)
  236. {
  237. ASN1uint32_t xsrcbit;
  238. ASN1uint32_t ret;
  239. if (!nbits)
  240. return 0;
  241. if (srcbit >= 8) {
  242. src += srcbit / 8;
  243. srcbit &= 7;
  244. }
  245. xsrcbit = 8 - srcbit;
  246. ret = 0;
  247. /* get bits from broken octet first */
  248. if (srcbit) {
  249. if (xsrcbit <= nbits) {
  250. ret = (*src++ & c_aBitMask[xsrcbit]) << (nbits -= xsrcbit);
  251. } else {
  252. return (*src >> (xsrcbit - nbits)) & c_aBitMask[nbits];
  253. }
  254. }
  255. /* get complete octets */
  256. while (nbits >= 8)
  257. ret |= *src++ << (nbits -= 8);
  258. /* get left bits */
  259. if (nbits)
  260. ret |= ((*src) >> (8 - nbits)) & c_aBitMask[nbits];
  261. return ret;
  262. }
  263. /* read nbits bits of val at src/srcbit */
  264. ASN1int32_t ASN1bitget(ASN1octet_t *src, ASN1uint32_t srcbit, ASN1uint32_t nbits)
  265. {
  266. ASN1uint32_t xsrcbit;
  267. ASN1int32_t ret;
  268. if (!nbits)
  269. return 0;
  270. if (srcbit >= 8) {
  271. src += srcbit / 8;
  272. srcbit &= 7;
  273. }
  274. xsrcbit = 8 - srcbit;
  275. if (*src & (0x80 >> srcbit))
  276. ret = c_aBitMask5[nbits];
  277. else
  278. ret = 0;
  279. /* get bits from broken octet first */
  280. if (srcbit) {
  281. if (xsrcbit <= nbits) {
  282. ret = *src++ << (nbits -= xsrcbit);
  283. } else {
  284. return (*src >> (xsrcbit - nbits)) & c_aBitMask[nbits];
  285. }
  286. }
  287. /* get complete octets */
  288. while (nbits >= 8)
  289. ret |= *src++ << (nbits -= 8);
  290. /* get left bits */
  291. if (nbits)
  292. ret |= ((*src) >> (8 - nbits)) & c_aBitMask[nbits];
  293. return ret;
  294. }
  295. /* get number of set bits in nbits bits at src/srcbit */
  296. ASN1uint32_t ASN1bitcount(ASN1octet_t *src, ASN1uint32_t srcbit, ASN1uint32_t nbits)
  297. {
  298. ASN1uint32_t xsrcbit;
  299. ASN1uint32_t ret;
  300. if (!nbits)
  301. return 0;
  302. if (srcbit >= 8) {
  303. src += srcbit / 8;
  304. srcbit &= 7;
  305. }
  306. xsrcbit = 8 - srcbit;
  307. /* count bits from broken octet first */
  308. if (srcbit) {
  309. if (xsrcbit <= nbits) {
  310. ret = c_aBitCount[*src++ & c_aBitMask4[srcbit]];
  311. nbits -= xsrcbit;
  312. } else {
  313. return c_aBitCount[(*src >> (xsrcbit - nbits)) & c_aBitMask[nbits]];
  314. }
  315. } else {
  316. ret = 0;
  317. }
  318. /* count bits in complete octets */
  319. while (nbits >= 8)
  320. {
  321. ret += c_aBitCount[*src++];
  322. nbits -= 8;
  323. }
  324. /* count left bits */
  325. if (nbits)
  326. ret += c_aBitCount[(*src) & c_aBitMask2[nbits]];
  327. return ret;
  328. }
  329. /* write noctets of val at dst */
  330. void ASN1octetput(ASN1octet_t *dst, ASN1uint32_t val, ASN1uint32_t noctets)
  331. {
  332. switch (noctets) {
  333. case 4:
  334. *dst++ = (ASN1octet_t)(val >> 24);
  335. /*FALLTHROUGH*/
  336. case 3:
  337. *dst++ = (ASN1octet_t)(val >> 16);
  338. /*FALLTHROUGH*/
  339. case 2:
  340. *dst++ = (ASN1octet_t)(val >> 8);
  341. /*FALLTHROUGH*/
  342. case 1:
  343. *dst++ = (ASN1octet_t)(val);
  344. break;
  345. default:
  346. break;
  347. MyAssert(0);
  348. /*NOTREACHED*/
  349. }
  350. }
  351. /* read noctets of val at dst */
  352. ASN1uint32_t ASN1octetget(ASN1octet_t *src, ASN1uint32_t noctets)
  353. {
  354. switch (noctets) {
  355. case 4:
  356. return (*src << 24) | (src[1] << 16) | (src[2] << 8) | src[3];
  357. case 3:
  358. return (*src << 16) | (src[1] << 8) | src[2];
  359. case 2:
  360. return (*src << 8) | src[1];
  361. case 1:
  362. return *src;
  363. default:
  364. MyAssert(0);
  365. return(0);
  366. /*NOTREACHED*/
  367. }
  368. }