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.

1906 lines
61 KiB

  1. //************************************************************************
  2. // Microsoft Corporation
  3. // Copyright(c) Microsoft Corp., 1990-1992
  4. //
  5. //
  6. // Revision history:
  7. // 5/5/94 Created gurdeep
  8. //
  9. //************************************************************************
  10. //#define COMP_12K
  11. #include "wan.h"
  12. #define __FILE_SIG__ COMPRESS_FILESIG
  13. //#define DEBUG
  14. CONST
  15. unsigned long lookup_array1[256] = {
  16. 0, 10276755, 20553510, 30830265,
  17. 41107020, 51383775, 61660530, 71937285,
  18. 82214040, 92490795, 102767550, 113044305,
  19. 123321060, 133597815, 143874570, 154151325,
  20. 164428080, 174704835, 184981590, 195258345,
  21. 205535100, 215811855, 226088610, 236365365,
  22. 246642120, 256918875, 267195630, 277472385,
  23. 287749140, 298025895, 308302650, 318579405,
  24. 328856160, 339132915, 349409670, 359686425,
  25. 369963180, 380239935, 390516690, 400793445,
  26. 411070200, 421346955, 431623710, 441900465,
  27. 452177220, 462453975, 472730730, 483007485,
  28. 493284240, 503560995, 513837750, 524114505,
  29. 534391260, 544668015, 554944770, 565221525,
  30. 575498280, 585775035, 596051790, 606328545,
  31. 616605300, 626882055, 637158810, 647435565,
  32. 657712320, 667989075, 678265830, 688542585,
  33. 698819340, 709096095, 719372850, 729649605,
  34. 739926360, 750203115, 760479870, 770756625,
  35. 781033380, 791310135, 801586890, 811863645,
  36. 822140400, 832417155, 842693910, 852970665,
  37. 863247420, 873524175, 883800930, 894077685,
  38. 904354440, 914631195, 924907950, 935184705,
  39. 945461460, 955738215, 966014970, 976291725,
  40. 986568480, 996845235, 1007121990, 1017398745,
  41. 1027675500, 1037952255, 1048229010, 1058505765,
  42. 1068782520, 1079059275, 1089336030, 1099612785,
  43. 1109889540, 1120166295, 1130443050, 1140719805,
  44. 1150996560, 1161273315, 1171550070, 1181826825,
  45. 1192103580, 1202380335, 1212657090, 1222933845,
  46. 1233210600, 1243487355, 1253764110, 1264040865,
  47. 1274317620, 1284594375, 1294871130, 1305147885,
  48. 1315424640, 1325701395, 1335978150, 1346254905,
  49. 1356531660, 1366808415, 1377085170, 1387361925,
  50. 1397638680, 1407915435, 1418192190, 1428468945,
  51. 1438745700, 1449022455, 1459299210, 1469575965,
  52. 1479852720, 1490129475, 1500406230, 1510682985,
  53. 1520959740, 1531236495, 1541513250, 1551790005,
  54. 1562066760, 1572343515, 1582620270, 1592897025,
  55. 1603173780, 1613450535, 1623727290, 1634004045,
  56. 1644280800, 1654557555, 1664834310, 1675111065,
  57. 1685387820, 1695664575, 1705941330, 1716218085,
  58. 1726494840, 1736771595, 1747048350, 1757325105,
  59. 1767601860, 1777878615, 1788155370, 1798432125,
  60. 1808708880, 1818985635, 1829262390, 1839539145,
  61. 1849815900, 1860092655, 1870369410, 1880646165,
  62. 1890922920, 1901199675, 1911476430, 1921753185,
  63. 1932029940, 1942306695, 1952583450, 1962860205,
  64. 1973136960, 1983413715, 1993690470, 2003967225,
  65. 2014243980, 2024520735, 2034797490, 2045074245,
  66. 2055351000, 2065627755, 2075904510, 2086181265,
  67. 2096458020, 2106734775, 2117011530, 2127288285,
  68. 2137565040, 2147841795, 2158118550, 2168395305,
  69. 2178672060, 2188948815, 2199225570, 2209502325,
  70. 2219779080, 2230055835, 2240332590, 2250609345,
  71. 2260886100, 2271162855, 2281439610, 2291716365,
  72. 2301993120, 2312269875, 2322546630, 2332823385,
  73. 2343100140, 2353376895, 2363653650, 2373930405,
  74. 2384207160, 2394483915, 2404760670, 2415037425,
  75. 2425314180, 2435590935, 2445867690, 2456144445,
  76. 2466421200, 2476697955, 2486974710, 2497251465,
  77. 2507528220, 2517804975, 2528081730, 2538358485,
  78. 2548635240, 2558911995, 2569188750, 2579465505,
  79. 2589742260, 2600019015, 2610295770, 2620572525
  80. };
  81. /*
  82. for i = 0 to 255,
  83. lookup_array2[i] = lookup_array1[i] << 8;
  84. */
  85. CONST
  86. unsigned long lookup_array2[256] = {
  87. 0, 2630849280, 966731264, 3597580544,
  88. 1933462528, 269344512, 2900193792, 1236075776,
  89. 3866925056, 2202807040, 538689024, 3169538304,
  90. 1505420288, 4136269568, 2472151552, 808033536,
  91. 3438882816, 1774764800, 110646784, 2741496064,
  92. 1077378048, 3708227328, 2044109312, 379991296,
  93. 3010840576, 1346722560, 3977571840, 2313453824,
  94. 649335808, 3280185088, 1616067072, 4246916352,
  95. 2582798336, 918680320, 3549529600, 1885411584,
  96. 221293568, 2852142848, 1188024832, 3818874112,
  97. 2154756096, 490638080, 3121487360, 1457369344,
  98. 4088218624, 2424100608, 759982592, 3390831872,
  99. 1726713856, 62595840, 2693445120, 1029327104,
  100. 3660176384, 1996058368, 331940352, 2962789632,
  101. 1298671616, 3929520896, 2265402880, 601284864,
  102. 3232134144, 1568016128, 4198865408, 2534747392,
  103. 870629376, 3501478656, 1837360640, 173242624,
  104. 2804091904, 1139973888, 3770823168, 2106705152,
  105. 442587136, 3073436416, 1409318400, 4040167680,
  106. 2376049664, 711931648, 3342780928, 1678662912,
  107. 14544896, 2645394176, 981276160, 3612125440,
  108. 1948007424, 283889408, 2914738688, 1250620672,
  109. 3881469952, 2217351936, 553233920, 3184083200,
  110. 1519965184, 4150814464, 2486696448, 822578432,
  111. 3453427712, 1789309696, 125191680, 2756040960,
  112. 1091922944, 3722772224, 2058654208, 394536192,
  113. 3025385472, 1361267456, 3992116736, 2327998720,
  114. 663880704, 3294729984, 1630611968, 4261461248,
  115. 2597343232, 933225216, 3564074496, 1899956480,
  116. 235838464, 2866687744, 1202569728, 3833419008,
  117. 2169300992, 505182976, 3136032256, 1471914240,
  118. 4102763520, 2438645504, 774527488, 3405376768,
  119. 1741258752, 77140736, 2707990016, 1043872000,
  120. 3674721280, 2010603264, 346485248, 2977334528,
  121. 1313216512, 3944065792, 2279947776, 615829760,
  122. 3246679040, 1582561024, 4213410304, 2549292288,
  123. 885174272, 3516023552, 1851905536, 187787520,
  124. 2818636800, 1154518784, 3785368064, 2121250048,
  125. 457132032, 3087981312, 1423863296, 4054712576,
  126. 2390594560, 726476544, 3357325824, 1693207808,
  127. 29089792, 2659939072, 995821056, 3626670336,
  128. 1962552320, 298434304, 2929283584, 1265165568,
  129. 3896014848, 2231896832, 567778816, 3198628096,
  130. 1534510080, 4165359360, 2501241344, 837123328,
  131. 3467972608, 1803854592, 139736576, 2770585856,
  132. 1106467840, 3737317120, 2073199104, 409081088,
  133. 3039930368, 1375812352, 4006661632, 2342543616,
  134. 678425600, 3309274880, 1645156864, 4276006144,
  135. 2611888128, 947770112, 3578619392, 1914501376,
  136. 250383360, 2881232640, 1217114624, 3847963904,
  137. 2183845888, 519727872, 3150577152, 1486459136,
  138. 4117308416, 2453190400, 789072384, 3419921664,
  139. 1755803648, 91685632, 2722534912, 1058416896,
  140. 3689266176, 2025148160, 361030144, 2991879424,
  141. 1327761408, 3958610688, 2294492672, 630374656,
  142. 3261223936, 1597105920, 4227955200, 2563837184,
  143. 899719168, 3530568448, 1866450432, 202332416,
  144. 2833181696, 1169063680, 3799912960, 2135794944,
  145. 471676928, 3102526208, 1438408192, 4069257472,
  146. 2405139456, 741021440, 3371870720, 1707752704,
  147. 43634688, 2674483968, 1010365952, 3641215232,
  148. 1977097216, 312979200, 2943828480, 1279710464,
  149. 3910559744, 2246441728, 582323712, 3213172992,
  150. 1549054976, 4179904256, 2515786240, 851668224
  151. };
  152. /*
  153. for i = 0 to 255,
  154. lookup_array3[i] = lookup_array1[i] << 16;
  155. */
  156. CONST
  157. unsigned long lookup_array3[256] = {
  158. 0, 3482517504, 2670067712, 1857617920,
  159. 1045168128, 232718336, 3715235840, 2902786048,
  160. 2090336256, 1277886464, 465436672, 3947954176,
  161. 3135504384, 2323054592, 1510604800, 698155008,
  162. 4180672512, 3368222720, 2555772928, 1743323136,
  163. 930873344, 118423552, 3600941056, 2788491264,
  164. 1976041472, 1163591680, 351141888, 3833659392,
  165. 3021209600, 2208759808, 1396310016, 583860224,
  166. 4066377728, 3253927936, 2441478144, 1629028352,
  167. 816578560, 4128768, 3486646272, 2674196480,
  168. 1861746688, 1049296896, 236847104, 3719364608,
  169. 2906914816, 2094465024, 1282015232, 469565440,
  170. 3952082944, 3139633152, 2327183360, 1514733568,
  171. 702283776, 4184801280, 3372351488, 2559901696,
  172. 1747451904, 935002112, 122552320, 3605069824,
  173. 2792620032, 1980170240, 1167720448, 355270656,
  174. 3837788160, 3025338368, 2212888576, 1400438784,
  175. 587988992, 4070506496, 3258056704, 2445606912,
  176. 1633157120, 820707328, 8257536, 3490775040,
  177. 2678325248, 1865875456, 1053425664, 240975872,
  178. 3723493376, 2911043584, 2098593792, 1286144000,
  179. 473694208, 3956211712, 3143761920, 2331312128,
  180. 1518862336, 706412544, 4188930048, 3376480256,
  181. 2564030464, 1751580672, 939130880, 126681088,
  182. 3609198592, 2796748800, 1984299008, 1171849216,
  183. 359399424, 3841916928, 3029467136, 2217017344,
  184. 1404567552, 592117760, 4074635264, 3262185472,
  185. 2449735680, 1637285888, 824836096, 12386304,
  186. 3494903808, 2682454016, 1870004224, 1057554432,
  187. 245104640, 3727622144, 2915172352, 2102722560,
  188. 1290272768, 477822976, 3960340480, 3147890688,
  189. 2335440896, 1522991104, 710541312, 4193058816,
  190. 3380609024, 2568159232, 1755709440, 943259648,
  191. 130809856, 3613327360, 2800877568, 1988427776,
  192. 1175977984, 363528192, 3846045696, 3033595904,
  193. 2221146112, 1408696320, 596246528, 4078764032,
  194. 3266314240, 2453864448, 1641414656, 828964864,
  195. 16515072, 3499032576, 2686582784, 1874132992,
  196. 1061683200, 249233408, 3731750912, 2919301120,
  197. 2106851328, 1294401536, 481951744, 3964469248,
  198. 3152019456, 2339569664, 1527119872, 714670080,
  199. 4197187584, 3384737792, 2572288000, 1759838208,
  200. 947388416, 134938624, 3617456128, 2805006336,
  201. 1992556544, 1180106752, 367656960, 3850174464,
  202. 3037724672, 2225274880, 1412825088, 600375296,
  203. 4082892800, 3270443008, 2457993216, 1645543424,
  204. 833093632, 20643840, 3503161344, 2690711552,
  205. 1878261760, 1065811968, 253362176, 3735879680,
  206. 2923429888, 2110980096, 1298530304, 486080512,
  207. 3968598016, 3156148224, 2343698432, 1531248640,
  208. 718798848, 4201316352, 3388866560, 2576416768,
  209. 1763966976, 951517184, 139067392, 3621584896,
  210. 2809135104, 1996685312, 1184235520, 371785728,
  211. 3854303232, 3041853440, 2229403648, 1416953856,
  212. 604504064, 4087021568, 3274571776, 2462121984,
  213. 1649672192, 837222400, 24772608, 3507290112,
  214. 2694840320, 1882390528, 1069940736, 257490944,
  215. 3740008448, 2927558656, 2115108864, 1302659072,
  216. 490209280, 3972726784, 3160276992, 2347827200,
  217. 1535377408, 722927616, 4205445120, 3392995328,
  218. 2580545536, 1768095744, 955645952, 143196160,
  219. 3625713664, 2813263872, 2000814080, 1188364288,
  220. 375914496, 3858432000, 3045982208, 2233532416,
  221. 1421082624, 608632832, 4091150336, 3278700544
  222. };
  223. /*
  224. The key for the multiplicative hash function consists of 3 unsigned
  225. characters. They are composed (logically) by concatenating them i.e.
  226. the composed key = 2^16*c2 + 2^8*c2 + c3 and fits in 24 bits. The
  227. composed key is not actually computed here as we use the components
  228. to directly compute the hash function.
  229. The multiplicative hash function consists of taking the higher order
  230. 12 bits (2^12 = 4096) of the lower order 24 bits of the product
  231. key * Multiplier where
  232. Multiplier = floor(A * pow(2.0, (double) w));
  233. double A = 0.6125423371; (chosen according to Knuth)
  234. w = 24 (the key's width in bits)
  235. The algorithm for this is in Cormen/Leiserson/Rivest.
  236. To do the multplication efficiently, the product c*Multiplier is
  237. precomputed and stored in lookup_array1 (for all 256 possible c's).
  238. lookup_array2 and lookup_array3 contain the same data as lookup_array1
  239. but shifted left 8 and 16 bits respectively.
  240. MultHash1 is the mult hashing function. MultHash0 contains an older
  241. (slower but less space-efficient) version of the same function.
  242. */
  243. #define MULTHASH1(c1,c2,c3) \
  244. ((lookup_array1[c1]+ \
  245. lookup_array2[c2]+ \
  246. lookup_array3[c3] ) & 0x00fff000) >> 12
  247. /*
  248. USHORT xorlookup1 [256] = {
  249. 0x110, 0x120, 0x130, 0x140, 0x150, 0x160, 0x170, 0x180, // 0-7
  250. 0x190, 0x1a0, 0x1b0, 0x1c0, 0x1d0, 0x1e0, 0x1f0, 0x100, // 8-15
  251. 0x210, 0x220, 0x230, 0x240, 0x250, 0x260, 0x270, 0x280, // 16-23
  252. 0x290, 0x2a0, 0x2b0, 0x2c0, 0x2d0, 0x2e0, 0x2f0, 0x200, // 24-31
  253. 0x310, 0x320, 0x330, 0x340, 0x350, 0x360, 0x370, 0x380, // 32-39
  254. 0x390, 0x3a0, 0x3b0, 0x3c0, 0x3d0, 0x3e0, 0x3f0, 0x300, // 40-47
  255. 0x410, 0x420, 0x430, 0x440, 0x450, 0x460, 0x470, 0x480, // 48-55
  256. 0x490, 0x4a0, 0x4b0, 0x4c0, 0x4d0, 0x4e0, 0x4f0, 0x400, // 56-63
  257. 0x510, 0x520, 0x530, 0x540, 0x550, 0x560, 0x570, 0x580, // 64-71
  258. 0x590, 0x5a0, 0x5b0, 0x5c0, 0x5d0, 0x5e0, 0x5f0, 0x500, // 72-79
  259. 0x610, 0x620, 0x630, 0x640, 0x650, 0x660, 0x670, 0x680, // 80-87
  260. 0x690, 0x6a0, 0x6b0, 0x6c0, 0x6d0, 0x6e0, 0x6f0, 0x600, // 88-95
  261. 0x710, 0x720, 0x730, 0x740, 0x750, 0x760, 0x770, 0x780, // 96-103
  262. 0x790, 0x7a0, 0x7b0, 0x7c0, 0x7d0, 0x7e0, 0x7f0, 0x700, // 104-111
  263. 0x810, 0x820, 0x830, 0x840, 0x850, 0x860, 0x870, 0x880, // 112-119
  264. 0x890, 0x8a0, 0x8b0, 0x8c0, 0x8d0, 0x8e0, 0x8f0, 0x800, // 120-127
  265. 0x910, 0x920, 0x930, 0x940, 0x950, 0x960, 0x970, 0x980, // 128-135
  266. 0x990, 0x9a0, 0x9b0, 0x9c0, 0x9d0, 0x9e0, 0x9f0, 0x900, // 136-143
  267. 0xa10, 0xa20, 0xa30, 0xa40, 0xa50, 0xa60, 0xa70, 0xa80, // 144-151
  268. 0xa90, 0xaa0, 0xab0, 0xac0, 0xad0, 0xae0, 0xaf0, 0xa00, // 152-159
  269. 0xb10, 0xb20, 0xb30, 0xb40, 0xb50, 0xb60, 0xb70, 0xb80, // 160-167
  270. 0xb90, 0xba0, 0xbb0, 0xbc0, 0xbd0, 0xbe0, 0xbf0, 0xb00, // 168-175
  271. 0xc10, 0xc20, 0xc30, 0xc40, 0xc50, 0xc60, 0xc70, 0xc80, // 176-183
  272. 0xc90, 0xca0, 0xcb0, 0xcc0, 0xcd0, 0xce0, 0xcf0, 0xc00, // 184-191
  273. 0xd10, 0xd20, 0xd30, 0xd40, 0xd50, 0xd60, 0xd70, 0xd80, // 192-199
  274. 0xd90, 0xda0, 0xdb0, 0xdc0, 0xdd0, 0xde0, 0xdf0, 0xd00, // 200-207
  275. 0xe10, 0xe20, 0xe30, 0xe40, 0xe50, 0xe60, 0xe70, 0xe80, // 208-215
  276. 0xe90, 0xea0, 0xeb0, 0xec0, 0xed0, 0xee0, 0xef0, 0xe00, // 216-223
  277. 0xf10, 0xf20, 0xf30, 0xf40, 0xf50, 0xf60, 0xf70, 0xf80, // 224-231
  278. 0xf90, 0xfa0, 0xfb0, 0xfc0, 0xfd0, 0xfe0, 0xff0, 0xf00, // 232-239
  279. 0x010, 0x020, 0x030, 0x040, 0x050, 0x060, 0x070, 0x080, // 240-247
  280. 0x090, 0x0a0, 0x0b0, 0x0c0, 0x0d0, 0x0e0, 0x0f0, 0x000 }; // 248-255
  281. USHORT xorlookup2 [256] = {
  282. 0x101, 0x201, 0x301, 0x401, 0x501, 0x601, 0x701, 0x801, // 0-7
  283. 0x901, 0xa01, 0xb01, 0xc01, 0xd01, 0xe01, 0xf01, 0x001, // 8-15
  284. 0x102, 0x202, 0x302, 0x402, 0x502, 0x602, 0x702, 0x802, // 16-23
  285. 0x902, 0xa02, 0xb02, 0xc02, 0xd02, 0xe02, 0xf02, 0x002, // 24-31
  286. 0x103, 0x203, 0x303, 0x403, 0x503, 0x603, 0x703, 0x803, // 32-39
  287. 0x903, 0xa03, 0xb03, 0xc03, 0xd03, 0xe03, 0xf03, 0x003, // 40-47
  288. 0x104, 0x204, 0x304, 0x404, 0x504, 0x604, 0x704, 0x804, // 48-55
  289. 0x904, 0xa04, 0xb04, 0xc04, 0xd04, 0xe04, 0xf04, 0x004, // 56-63
  290. 0x105, 0x205, 0x305, 0x405, 0x505, 0x605, 0x705, 0x805, // 64-71
  291. 0x905, 0xa05, 0xb05, 0xc05, 0xd05, 0xe05, 0xf05, 0x005, // 72-79
  292. 0x106, 0x206, 0x306, 0x406, 0x506, 0x606, 0x706, 0x806, // 80-87
  293. 0x906, 0xa06, 0xb06, 0xc06, 0xd06, 0xe06, 0xf06, 0x006, // 88-95
  294. 0x107, 0x207, 0x307, 0x407, 0x507, 0x607, 0x707, 0x807, // 96-103
  295. 0x907, 0xa07, 0xb07, 0xc07, 0xd07, 0xe07, 0xf07, 0x007, // 104-111
  296. 0x108, 0x208, 0x308, 0x408, 0x508, 0x608, 0x708, 0x808, // 112-119
  297. 0x908, 0xa08, 0xb08, 0xc08, 0xd08, 0xe08, 0xf08, 0x008, // 120-127
  298. 0x109, 0x209, 0x309, 0x409, 0x509, 0x609, 0x709, 0x809, // 128-135
  299. 0x909, 0xa09, 0xb09, 0xc09, 0xd09, 0xe09, 0xf09, 0x009, // 136-143
  300. 0x10a, 0x20a, 0x30a, 0x40a, 0x50a, 0x60a, 0x70a, 0x80a, // 144-151
  301. 0x90a, 0xa0a, 0xb0a, 0xc0a, 0xd0a, 0xe0a, 0xf0a, 0x00a, // 152-159
  302. 0x10b, 0x20b, 0x30b, 0x40b, 0x50b, 0x60b, 0x70b, 0x80b, // 160-167
  303. 0x90b, 0xa0b, 0xb0b, 0xc0b, 0xd0b, 0xe0b, 0xf0b, 0x00b, // 168-175
  304. 0x10c, 0x20c, 0x30c, 0x40c, 0x50c, 0x60c, 0x70c, 0x80c, // 176-183
  305. 0x90c, 0xa0c, 0xb0c, 0xc0c, 0xd0c, 0xe0c, 0xf0c, 0x00c, // 184-191
  306. 0x10d, 0x20d, 0x30d, 0x40d, 0x50d, 0x60d, 0x70d, 0x80d, // 192-199
  307. 0x90d, 0xa0d, 0xb0d, 0xc0d, 0xd0d, 0xe0d, 0xf0d, 0x00d, // 200-207
  308. 0x10e, 0x20e, 0x30e, 0x40e, 0x50e, 0x60e, 0x70e, 0x80e, // 208-215
  309. 0x90e, 0xa0e, 0xb0e, 0xc0e, 0xd0e, 0xe0e, 0xf0e, 0x00e, // 216-223
  310. 0x10f, 0x20f, 0x30f, 0x40f, 0x50f, 0x60f, 0x70f, 0x80f, // 224-231
  311. 0x90f, 0xa0f, 0xb0f, 0xc0f, 0xd0f, 0xe0f, 0xf0f, 0x00f, // 232-239
  312. 0x000, 0x200, 0x300, 0x400, 0x500, 0x600, 0x700, 0x800, // 240-247
  313. 0x900, 0xa00, 0xb00, 0xc00, 0xd00, 0xe00, 0xf00, 0x100 }; // 248-255
  314. */
  315. /* Bitptrs point to the current byte. The current bit (i.e. next bit to be
  316. * stored) is masked off by the bit entry. When this reaches zero, it is
  317. * reset to 0x80 and the next byte is set up. The bytes are filled MSBit
  318. * first. */
  319. /* Starts and sets the first byte to zero for the bitptr. */
  320. #define bitptr_init(s) pbyte = s; byte=0; bit = 16;
  321. /* Sets up the byte part of the bitptr so that it is pointing to the byte after
  322. * the byte which had the last bit put into it. */
  323. #define bitptr_end() if (bit != 16) *pbyte++=(UCHAR)(byte >> 8);
  324. /* Goes to the next bit, and byte if necessary. */
  325. #define bitptr_next() \
  326. if (bit < 10) { \
  327. *pbyte++=(UCHAR)(byte >> 8); \
  328. byte <<= 8; \
  329. bit = 16; \
  330. } else \
  331. bit-- ;
  332. /*
  333. #define bitptr_next() \
  334. bit--; \
  335. if (bit < 9) { \
  336. *pbyte++=(UCHAR)(byte >> 8); \
  337. byte <<= 8; \
  338. bit = 16; \
  339. }
  340. */
  341. /* Advances to the next bit, and byte if necessary, readjusting the bit. */
  342. #define bitptr_advance() \
  343. if (bit < 9) { \
  344. *pbyte++=(UCHAR)(byte >> 8); \
  345. bit+=8; \
  346. byte <<= 8; \
  347. }
  348. /* BIT I/O FUNCTIONS *********************************************************/
  349. /* These routines output most-significant-bit-first and the input will return
  350. * them MSB first, too. */
  351. /* Outputs a one bit in the bit stream. */
  352. #define out_bit_1() bit--; byte |= (1 << bit); bitptr_advance();
  353. #define out_bit_0() bitptr_next();
  354. /* TestBit; output 1 if that bit is set */
  355. //#define tb(b,w,n) if ((w) & (n)) *pbyte |= bit; bitptr_next(b);
  356. #define out_bits_2(w) bit-=2; byte|=(w << bit); bitptr_advance();
  357. #define out_bits_3(w) bit-=3; byte|=(w << bit); bitptr_advance();
  358. #define out_bits_4(w) bit-=4; byte|=(w << bit); bitptr_advance();
  359. #define out_bits_5(w) bit-=5; byte|=(w << bit); bitptr_advance();
  360. #define out_bits_6(w) bit-=6; byte|=(w << bit); bitptr_advance();
  361. #define out_bits_7(w) bit-=7; byte|=(w << bit); bitptr_advance();
  362. // #define out_bits_8(w) bit-=8; byte|=(w << bit); bit+=8; *pbyte++=(UCHAR)(byte >> 8); byte <<= 8;
  363. #define out_bits_8(w) byte|=(w << (bit-8)); *pbyte++=(UCHAR)(byte >> 8); byte <<= 8;
  364. /*
  365. #define out_bits_9(w) \
  366. if (bit > 9) { \
  367. bit-=9; byte|=(w << bit); \
  368. *pbyte++=(UCHAR)(byte >> 8);\
  369. bit+=8; \
  370. byte <<= 8; \
  371. } else { \
  372. bit=16; byte |= w; \
  373. *pbyte++=(UCHAR)(byte >> 8); *pbyte++=(UCHAR)(byte); byte=0; \
  374. }
  375. */
  376. #define out_bits_9(w) \
  377. if (bit > 9) { \
  378. byte|=(w << (bit-9)); \
  379. *pbyte++=(UCHAR)(byte >> 8);\
  380. bit--; \
  381. byte <<= 8; \
  382. } else { \
  383. bit=16; byte |= w; \
  384. *pbyte++=(UCHAR)(byte >> 8); *pbyte++=(UCHAR)(byte); byte=0; \
  385. }
  386. #define out_bits_10(w) \
  387. if (bit > 10) { \
  388. bit-=10; byte |= (w << bit); *pbyte++ = (UCHAR)(byte >> 8); bit+=8; byte <<=8; \
  389. } else { \
  390. out_bits_2((w >> 8)); \
  391. out_bits_8((w & 0xFF)); \
  392. }
  393. //
  394. // Weird effect - if out_bits_9 used instead of out_bits_8,
  395. // it's faster! if (bit == 11) is faster than if (bit != 11).
  396. //
  397. #define out_bits_11(w) \
  398. if (bit > 11) { \
  399. bit-=11; byte |= (w << bit); *pbyte++ = (UCHAR)(byte >> 8); bit+=8; byte <<=8; \
  400. } else { \
  401. if (bit == 11) { \
  402. bit=16; byte |= w; \
  403. *pbyte++=(UCHAR)(byte >> 8); *pbyte++=(UCHAR)(byte); byte=0; \
  404. } else { \
  405. bit=11-bit; \
  406. byte|=(w >> bit); \
  407. *pbyte++=(UCHAR)(byte >> 8); *pbyte++=(UCHAR)(byte); \
  408. bit=16-bit; \
  409. byte=(w << bit); \
  410. } \
  411. }
  412. #define out_bits_12(w) \
  413. if (bit > 12) { \
  414. bit-=12; byte |= (w << bit); *pbyte++ = (UCHAR)(byte >> 8); bit+=8; byte <<=8; \
  415. } else { \
  416. out_bits_4((w >> 8)); \
  417. out_bits_8((w & 0xFF)); \
  418. }
  419. #define out_bits_13(w) \
  420. if (bit > 13) { \
  421. bit-=13; byte |= (w << bit); *pbyte++ = (UCHAR)(byte >> 8); bit+=8; byte <<=8; \
  422. } else { \
  423. out_bits_5((w >> 8)); \
  424. out_bits_8((w & 0xFF)); \
  425. }
  426. #define out_bits_14(w) \
  427. if (bit > 14) { \
  428. bit-=14; byte |= (w << bit); *pbyte++ = (UCHAR)(byte >> 8); bit+=8; byte <<=8; \
  429. } else { \
  430. out_bits_6((w >> 8)); \
  431. out_bits_8((w & 0xFF)); \
  432. }
  433. #define out_reserve_4() \
  434. bit-=4; bitptr_advance();
  435. /* Starts the given bit pointer */
  436. #define inbit_start(s) pbyte = s; bit = 16; byte=(*pbyte << 8) + *(pbyte+1); pbyte++;
  437. #define inbit_end() if (bit != 16) pbyte++;
  438. #define in_bit_next() if (bit < 9) { \
  439. bit=16; \
  440. byte <<=8; \
  441. byte |= *(++pbyte); \
  442. }
  443. #define in_bit_advance() if (bit < 9) { \
  444. bit+=8; \
  445. byte <<=8; \
  446. byte |= *(++pbyte); \
  447. }
  448. /* Returns non-zero in bitset if the next bit in the stream is a 1. */
  449. #define in_bit() bit--; bitset = (byte >> bit) & 1; in_bit_next()
  450. #define in_bits_2(w) bit-=2; w = (byte >> bit) & 0x03;\
  451. in_bit_advance();
  452. #define in_bits_3(w) bit-=3; w = (byte >> bit) & 0x07;\
  453. in_bit_advance();
  454. #define in_bits_4(w) bit-=4; w = (byte >> bit) & 0x0F;\
  455. in_bit_advance();
  456. #define in_bits_5(w) bit-=5; w = (byte >> bit) & 0x1F;\
  457. in_bit_advance();
  458. #define in_bits_6(w) bit-=6; w = (byte >> bit) & 0x3F;\
  459. in_bit_advance();
  460. #define in_bits_7(w) bit-=7; w = (byte >> bit) & 0x7F;\
  461. in_bit_advance();
  462. #define in_bits_8(w) bit-=8; w = (byte >> bit) & 0xFF;\
  463. bit+=8; byte <<=8; byte |= *(++pbyte);
  464. #define in_bits_9(w) bit-=9; w = (byte >> bit) & 0x1FF; \
  465. bit+=8; byte <<=8; byte |= *(++pbyte); \
  466. in_bit_advance();
  467. #define in_bits_10(w) if (bit > 10) { \
  468. bit-=10; w = (byte >> bit) & 0x3FF; \
  469. bit+=8; byte <<=8; byte |= *(++pbyte); \
  470. } else { \
  471. in_bits_2(bitset); \
  472. in_bits_8(w); \
  473. w= w + (bitset << 8); \
  474. }
  475. #define in_bits_11(w) if (bit > 11) { \
  476. bit-=11; w = (byte >> bit) & 0x7FF; \
  477. bit+=8; byte <<=8; byte |= *(++pbyte); \
  478. } else { \
  479. in_bits_3(bitset); \
  480. in_bits_8(w); \
  481. w= w + (bitset << 8); \
  482. }
  483. #define in_bits_12(w) if (bit > 12) { \
  484. bit-=12; w = (byte >> bit) & 0xFFF; \
  485. bit+=8; byte <<=8; byte |= *(++pbyte); \
  486. } else { \
  487. in_bits_4(bitset); \
  488. in_bits_8(w); \
  489. w= w + (bitset << 8); \
  490. }
  491. #define in_bits_13(w)\
  492. if (bit > 13) { \
  493. bit-=13; w = (byte >> bit) & 0x1FFF; \
  494. bit+=8; byte <<=8; byte |= *(++pbyte); \
  495. } else { \
  496. in_bits_5(bitset); \
  497. in_bits_8(w); \
  498. w=w + (bitset << 8); \
  499. }
  500. #define in_bits_14(w)\
  501. if (bit > 14) { \
  502. bit-=14; w = (byte >> bit) & 0x3FFF; \
  503. bit+=8; byte <<=8; byte |= *(++pbyte); \
  504. } else { \
  505. in_bits_6(bitset); \
  506. in_bits_8(w); \
  507. w=w + (bitset << 8); \
  508. }
  509. UCHAR SHApad1[40] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  510. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  511. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  512. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  513. UCHAR SHApad2[40] = {0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2,
  514. 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2,
  515. 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2,
  516. 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2};
  517. PUCHAR ClntSSrvR = "On the client side, this is the send key; on the server side, it is the receive key.";
  518. PUCHAR ClntRSrvS = "On the client side, this is the receive key; on the server side, it is the send key.";
  519. #define ECP_STRING_LEN strlen(ClntSSrvR)
  520. #ifdef DEBUG
  521. char
  522. ChPrint(UCHAR b)
  523. {
  524. if (isprint(b))
  525. return (char)b;
  526. else
  527. return '.';
  528. }
  529. #endif
  530. //* compress()
  531. //
  532. // Function: Main compression function.
  533. //
  534. // Parameters:
  535. // IN CurrentBuffer -> points to NDIS_WAN_PACKET with data to compress
  536. // OUT CompOutBuffer -> points to NDIS_WAN_PACKET to compress data to
  537. // IN CurrentLength -> points to Length of data to compress
  538. // IN context -> connection compress context
  539. //
  540. // Returns: Nothing
  541. //
  542. // WARNING: CODE IS HIGHLY OPTIMIZED FOR TIME.
  543. //
  544. //
  545. UCHAR
  546. compress (UCHAR *CurrentBuffer, UCHAR *CompOutBuffer, ULONG *CurrentLength, SendContext *context)
  547. {
  548. int copylen ;
  549. int bit;
  550. int byte;
  551. int backptr ;
  552. int cbMatch;
  553. int hashvalue ;
  554. int lookup1 ;
  555. UCHAR *matchptr ;
  556. UCHAR *pbyte;
  557. UCHAR *historyptr, *historybaseptr ;
  558. UCHAR *currentptr ;
  559. UCHAR *endptr ;
  560. UCHAR hashchar1;
  561. UCHAR hashchar2;
  562. UCHAR hashchar3;
  563. int literal ;
  564. UCHAR status=0; // return flags
  565. PUCHAR currentbuf ;
  566. USHORT usBufferLength = (USHORT)*CurrentLength;
  567. register BOOLEAN bHistoryless;
  568. // Will this packet fit at the end of the history buffer?
  569. //
  570. if (((context->CurrentIndex + *CurrentLength) >= (HISTORY_MAX - 1 )) ||
  571. (context->CurrentIndex == 0)) {
  572. context->CurrentIndex = 0; // Index into the history
  573. status |= PACKET_AT_FRONT;
  574. }
  575. if (0 == (context->BundleFlags & DO_HISTORY_LESS)) {
  576. historybaseptr = context->History;
  577. bHistoryless = FALSE;
  578. } else {
  579. ASSERT(0 == context->CurrentIndex);
  580. ASSERT(0 == context->ValidHistory);
  581. historybaseptr = CurrentBuffer;
  582. bHistoryless = TRUE;
  583. }
  584. //
  585. // we no longer need to save the non compressed data - tonybe 01-12-95
  586. //
  587. // RtlMoveMemory(context->CompressBuffer, CurrentBuffer, *CurrentLength) ;
  588. // Start out the bit pointing output
  589. //
  590. bitptr_init(CompOutBuffer);
  591. //
  592. // We are now compressing into an output buffer - tonybe 01-12-95
  593. //
  594. // bitptr_init(CurrentBuffer);
  595. historyptr = historybaseptr + context->CurrentIndex;
  596. currentptr = CurrentBuffer;
  597. //
  598. // we are now compressing from the currentbuffer - tonybe 01-12-95
  599. //
  600. // currentptr = context->CompressBuffer ;
  601. endptr = currentptr + *CurrentLength - 1;
  602. while (currentptr < (endptr-2)) {
  603. hashchar1 = *currentptr++ ;
  604. if(bHistoryless)
  605. {
  606. ASSERT(*historyptr == hashchar1);
  607. historyptr++;
  608. }
  609. else
  610. {
  611. *historyptr++ = hashchar1;
  612. }
  613. hashchar2 = *currentptr ;
  614. hashchar3 = *(currentptr+1) ;
  615. // "fast" hash function
  616. // hashvalue = (int)hashchar1 ^ xorlookup1[hashchar2] ^ xorlookup2[hashchar3];
  617. hashvalue = MULTHASH1(hashchar1, hashchar2, hashchar3) ;
  618. matchptr = historybaseptr +
  619. ((context->HashTable[hashvalue] >= context->HashOffset) ?
  620. (context->HashTable[hashvalue] - context->HashOffset) : 0);
  621. if (matchptr != (historyptr - 1))
  622. context->HashTable[hashvalue] = (USHORT)(historyptr - historybaseptr) + context->HashOffset;
  623. if (context->ValidHistory < historyptr)
  624. context->ValidHistory = historyptr;
  625. if (matchptr != historybaseptr && matchptr != (historyptr - 1) &&
  626. matchptr != historyptr && (matchptr+1) <= context->ValidHistory &&
  627. *(matchptr-1) == hashchar1 && *matchptr == hashchar2 &&
  628. *(matchptr+1) == hashchar3) {
  629. backptr = ((int)(historyptr - matchptr)) & (HISTORY_SIZE - 1) ;
  630. if(bHistoryless)
  631. {
  632. ASSERT(*historyptr == hashchar2);
  633. ASSERT(*(historyptr + 1) == hashchar3);
  634. historyptr +=2;
  635. }
  636. else
  637. {
  638. *historyptr++ = hashchar2 ; // copy the other 2 chars
  639. *historyptr++ = hashchar3 ; // copy the other 2 chars
  640. }
  641. currentptr +=2 ;
  642. cbMatch = 3 ; // length of match
  643. matchptr +=2 ; // we have already matched 3
  644. while ((currentptr < endptr) && (matchptr <= context->ValidHistory) && (*matchptr == *currentptr)) {
  645. matchptr++ ;
  646. if(bHistoryless)
  647. {
  648. ASSERT(*historyptr == *currentptr);
  649. historyptr++;
  650. currentptr++ ;
  651. }
  652. else
  653. {
  654. *historyptr++ = *currentptr++ ;
  655. }
  656. cbMatch++ ;
  657. }
  658. // First output the backpointer
  659. //
  660. if (backptr >= 320) {
  661. backptr -= 320 ;
  662. out_bits_8((0xc000 + backptr) >> 8) ; // 110 + 13 bits
  663. out_bits_8((backptr)) ;
  664. } else if (backptr < 64) { // 1111 + 6 bits
  665. backptr += 0x3c0 ;
  666. out_bits_10(backptr);
  667. } else {
  668. backptr += (0xE00 - 64); // 1110 + 8 bits
  669. out_bits_12(backptr);
  670. }
  671. // output the length of the match encoding
  672. //
  673. switch (cbMatch) {
  674. case 3:
  675. out_bit_0(); // length of 3 - most common
  676. break;
  677. case 4:
  678. out_bits_4(8);
  679. break;
  680. case 5:
  681. out_bits_4(9);
  682. break;
  683. case 6:
  684. out_bits_4(10);
  685. break;
  686. case 7:
  687. out_bits_4(11);
  688. break;
  689. case 8:
  690. out_bits_6(48);
  691. break;
  692. case 9:
  693. out_bits_6(49);
  694. break;
  695. case 10:
  696. out_bits_6(50);
  697. break;
  698. case 11:
  699. out_bits_6(51);
  700. break;
  701. case 12:
  702. out_bits_6(52);
  703. break;
  704. case 13:
  705. out_bits_6(53);
  706. break;
  707. case 14:
  708. out_bits_6(54);
  709. break;
  710. case 15:
  711. out_bits_6(55);
  712. break;
  713. case 16:
  714. out_bits_8(0xe0);
  715. break;
  716. case 17:
  717. out_bits_8(0xe1);
  718. break;
  719. case 18:
  720. out_bits_8(0xe2);
  721. break;
  722. case 19:
  723. out_bits_8(0xe3);
  724. break;
  725. case 20:
  726. out_bits_8(0xe4);
  727. break;
  728. case 21:
  729. out_bits_8(0xe5);
  730. break;
  731. case 22:
  732. out_bits_8(0xe6);
  733. break;
  734. case 23:
  735. out_bits_8(0xe7);
  736. break;
  737. case 24:
  738. out_bits_8(0xe8);
  739. break;
  740. case 25:
  741. out_bits_8(0xe9);
  742. break;
  743. case 26:
  744. out_bits_8(0xea);
  745. break;
  746. case 27:
  747. out_bits_8(0xeb);
  748. break;
  749. case 28:
  750. out_bits_8(0xec);
  751. break;
  752. case 29:
  753. out_bits_8(0xed);
  754. break;
  755. case 30:
  756. out_bits_8(0xee);
  757. break;
  758. case 31:
  759. out_bits_8(0xef);
  760. break;
  761. default:
  762. if (cbMatch < 64) {
  763. out_bits_4(0xF) ;
  764. cbMatch -= 32 ;
  765. out_bits_6(cbMatch) ;
  766. }
  767. else if (cbMatch < 128) {
  768. out_bits_5(0x1F) ;
  769. cbMatch -= 64 ;
  770. out_bits_7(cbMatch) ;
  771. }
  772. else if (cbMatch < 256) {
  773. out_bits_6(0x3F) ;
  774. cbMatch -= 128 ;
  775. out_bits_8(cbMatch) ;
  776. }
  777. else if (cbMatch < 512) {
  778. out_bits_7(0x7F) ;
  779. cbMatch -= 256 ;
  780. out_bits_9(cbMatch) ;
  781. }
  782. else if (cbMatch < 1024) {
  783. out_bits_8(0xFF) ;
  784. cbMatch -= 512 ;
  785. out_bits_10(cbMatch) ;
  786. }
  787. else if (cbMatch < 2048) {
  788. out_bits_9(0x1FF) ;
  789. cbMatch -= 1024 ;
  790. out_bits_11(cbMatch) ;
  791. }
  792. else if (cbMatch < 4096) {
  793. out_bits_10(0x3FF) ;
  794. cbMatch -= 2048 ;
  795. out_bits_12(cbMatch) ;
  796. }
  797. else if (cbMatch < 8192) {
  798. out_bits_11(0x7FF) ;
  799. cbMatch -= 4096 ;
  800. out_bits_13(cbMatch) ;
  801. }
  802. else { // 8192 and greater
  803. out_bits_12(0xFFF) ;
  804. cbMatch -= 8192 ;
  805. out_bits_14(cbMatch) ;
  806. }
  807. break ;
  808. }
  809. } else { // encode a literal
  810. // temp=literallookup[context->History[i-1]] ;
  811. literal= hashchar1 ;
  812. if (literal & 0x80) {
  813. literal += 0x80;
  814. out_bits_9(literal) ;
  815. } else {
  816. out_bits_8(literal) ;
  817. }
  818. }
  819. } // while
  820. // get any remaining chars as literals
  821. while (currentptr <= endptr) {
  822. // temp=literallookup[context->History[i-1]] ;
  823. literal=*currentptr ;
  824. if (literal & 0x80) {
  825. literal += 0x80;
  826. out_bits_9(literal) ;
  827. } else {
  828. out_bits_8(literal) ;
  829. }
  830. if(bHistoryless)
  831. {
  832. ASSERT(*historyptr == *currentptr);
  833. historyptr ++;
  834. currentptr ++;
  835. }
  836. else
  837. {
  838. *historyptr++ = *currentptr++ ;
  839. }
  840. }
  841. bitptr_end() ;
  842. // Check if we had expansion instead of compression
  843. //
  844. if ((ULONG)(pbyte - CompOutBuffer) > *CurrentLength) { // expansion.
  845. //
  846. // We don't need to do this copy since we can just signal the outside world
  847. // that compression did not take place and the valid data is still in the
  848. // current buffer
  849. //
  850. // RtlMoveMemory(CompOutBuffer, CurrentBuffer, *CurrentLength) ;
  851. context->HashOffset = 0;
  852. if (!(context->BundleFlags & DO_HISTORY_LESS)) {
  853. memset (context->History, 0, HISTORY_SIZE + 1) ;
  854. }
  855. memset (context->HashTable, 0, sizeof(context->HashTable)) ;
  856. #ifdef COMP_12K
  857. status = 0 ;
  858. #else
  859. status = PACKET_FLUSHED;
  860. #endif
  861. context->CurrentIndex = HISTORY_SIZE+1 ; // this forces a start over next time
  862. } else { // compression successful
  863. *CurrentLength = (ULONG)(pbyte - CompOutBuffer);
  864. //
  865. // the compressed data is now in CompOutBuffer - tonybe 01-12-95
  866. //
  867. // *CurrentLength = pbyte - CurrentBuffer ;
  868. status |= PACKET_COMPRESSED ;
  869. context->CurrentIndex = (int)(historyptr - historybaseptr) ;
  870. if (context->BundleFlags & DO_HISTORY_LESS) {
  871. context->HashOffset += usBufferLength;
  872. }
  873. }
  874. return(status);
  875. }
  876. //* initsendcontext()
  877. //
  878. // Function: Initialize SendContext block
  879. //
  880. // Parameters: IN context -> connection compress context
  881. //
  882. // Returns: Nothing
  883. //
  884. //*
  885. void
  886. initsendcontext (SendContext *context)
  887. {
  888. context->CurrentIndex = 0; // Index into the history
  889. context->ValidHistory = 0 ; // reset valid history
  890. if (((context->HashOffset > MAX_HASH_OFFSET) &&
  891. (context->BundleFlags & DO_HISTORY_LESS)) ||
  892. !(context->BundleFlags & DO_HISTORY_LESS)) {
  893. context->HashOffset = 0;
  894. memset (context->HashTable, 0, sizeof(context->HashTable));
  895. }
  896. if(!(context->BundleFlags & DO_HISTORY_LESS)) {
  897. memset (context->History, 0, HISTORY_SIZE + 1) ;
  898. }
  899. }
  900. //* initrecvcontext()
  901. //
  902. // Function: Initialize RecvContext block
  903. //
  904. // Parameters: IN context -> connection decompress context
  905. //
  906. // Returns: Nothing
  907. //
  908. //*
  909. void
  910. initrecvcontext (RecvContext *context)
  911. {
  912. context->CurrentPtr = context->History ;
  913. #if DBG
  914. context->DebugFence = DEBUG_FENCE_VALUE;
  915. #endif
  916. if(!(context->BundleFlags & DO_HISTORY_LESS)) {
  917. memset (context->History, 0, HISTORY_SIZE + 1) ;
  918. }
  919. }
  920. //* decompress()
  921. //
  922. // Function: de-compression function.
  923. //
  924. // Parameters: IN inbuf -> points to data to be uncompressed
  925. // IN inlen -> length of data
  926. // IN start -> flag indicating whether to start with a clean history buffer
  927. // OUT output-> decompressed data
  928. // OUT outlen-> lenght of decompressed data
  929. // IN context -> connection decompress context
  930. //
  931. // Returns: TRUE if decompress was successful
  932. // FALSE if it wasnt
  933. //
  934. // WARNING: CODE IS HIGHLY OPTIMIZED FOR TIME.
  935. //
  936. //*
  937. int
  938. decompress(
  939. UCHAR *inbuf,
  940. int inlen,
  941. int start,
  942. UCHAR **output,
  943. int *outlen,
  944. RecvContext *context)
  945. {
  946. UCHAR *inend; // When we know we're done decompressing
  947. UCHAR *outstart; // Remember where in dbuf we started
  948. UCHAR *current;
  949. int backptr; // Back pointer for copy items
  950. int length; // Where to copy from in dbuf
  951. UCHAR *s1, *s2;
  952. int bitset;
  953. int bit;
  954. int byte;
  955. UCHAR *pbyte;
  956. UCHAR *historyend = context->History + context->HistorySize;
  957. inend = inbuf + inlen ;
  958. if ((context->BundleFlags & DO_HISTORY_LESS) && start == 0) {
  959. return FALSE;
  960. }
  961. //
  962. // Start out looking at the first bit
  963. //
  964. inbit_start(inbuf);
  965. if (start) // start over clean?
  966. context->CurrentPtr = current = context->History ;
  967. else
  968. current = context->CurrentPtr ;
  969. //
  970. // Save our starting position
  971. //
  972. outstart = current;
  973. //
  974. // Decompress until we run out of input
  975. //
  976. while (pbyte < inend) {
  977. //
  978. // Jump on what to do with these three bits.
  979. //
  980. in_bits_3(length);
  981. switch (length) {
  982. case 0:
  983. in_bits_5(length) ;
  984. goto LITERAL ;
  985. case 1:
  986. in_bits_5(length) ;
  987. length += 32 ;
  988. goto LITERAL ;
  989. case 2:
  990. in_bits_5(length) ;
  991. length += 64 ;
  992. goto LITERAL ;
  993. case 3:
  994. in_bits_5(length) ;
  995. length += 96 ;
  996. goto LITERAL ;
  997. case 4:
  998. in_bits_6(length) ;
  999. length +=128 ;
  1000. goto LITERAL ;
  1001. case 5:
  1002. in_bits_6(length) ;
  1003. length +=192 ;
  1004. goto LITERAL ;
  1005. case 6:
  1006. in_bits_13 (backptr) ; // 110 - 14 bit offset
  1007. backptr+=320 ;
  1008. break ;
  1009. case 7:
  1010. in_bit() ;
  1011. if (bitset) {
  1012. in_bits_6(backptr) ;
  1013. } else {
  1014. in_bits_8(backptr) ;
  1015. backptr+=64 ;
  1016. }
  1017. break ;
  1018. }
  1019. //
  1020. // If we reach here, it's a copy item
  1021. //
  1022. //
  1023. // Now get the length
  1024. //
  1025. in_bit() ; // 1st length bit
  1026. if (!bitset) {
  1027. length = 3 ;
  1028. goto DONE ;
  1029. }
  1030. in_bit() ; // 2nd length bit
  1031. if (!bitset) {
  1032. in_bits_2 (length) ;
  1033. length += 4 ;
  1034. goto DONE ;
  1035. }
  1036. in_bit() ; // 3rd length bit
  1037. if (!bitset) {
  1038. in_bits_3 (length) ;
  1039. length += 8 ;
  1040. goto DONE ;
  1041. }
  1042. in_bit() ; // 4th length bit
  1043. if (!bitset) {
  1044. in_bits_4 (length) ;
  1045. length += 16 ;
  1046. goto DONE ;
  1047. }
  1048. in_bit() ; // 5th length bit
  1049. if (!bitset) {
  1050. in_bits_5 (length) ;
  1051. length += 32 ;
  1052. goto DONE ;
  1053. }
  1054. in_bit() ; // 6th length bit
  1055. if (!bitset) {
  1056. in_bits_6 (length) ;
  1057. length += 64 ;
  1058. goto DONE ;
  1059. }
  1060. in_bit() ; // 7th length bit
  1061. if (!bitset) {
  1062. in_bits_7 (length) ;
  1063. length += 128 ;
  1064. goto DONE ;
  1065. }
  1066. in_bit() ; // 8th length bit
  1067. if (!bitset) {
  1068. in_bits_8 (length) ;
  1069. length += 256 ;
  1070. goto DONE ;
  1071. }
  1072. in_bit() ; // 9th length bit
  1073. if (!bitset) {
  1074. in_bits_9 (length) ;
  1075. length += 512 ;
  1076. goto DONE ;
  1077. }
  1078. in_bit() ; // 10th length bit
  1079. if (!bitset) {
  1080. in_bits_10 (length) ;
  1081. length += 1024 ;
  1082. goto DONE ;
  1083. }
  1084. //
  1085. // length cannot be greater than max packets size which is 1500
  1086. //
  1087. #if DBG
  1088. DbgPrint("NDISWAN: RAS Decompressor problem1: Possible data corruption\n");
  1089. #endif
  1090. return FALSE ;
  1091. DONE:
  1092. //
  1093. // Turn the backptr into an index location
  1094. //
  1095. #ifdef COMP_12K
  1096. s2 = current - backptr ;
  1097. #else
  1098. if (context->BundleFlags & DO_HISTORY_LESS) {
  1099. s2 = current - backptr ;
  1100. if(s2 < context->History)
  1101. {
  1102. return FALSE;
  1103. }
  1104. }
  1105. else {
  1106. s2 = context->History + (((current - context->History) - backptr) & (HISTORY_SIZE -1)) ;
  1107. }
  1108. #endif
  1109. s1 = current;
  1110. current += length;
  1111. // if we are past the end of the history this is a bad sign: abort decompression
  1112. //
  1113. if (current >= historyend || (s2 + length) >= historyend) {
  1114. #if DBG
  1115. DbgPrint("NDISWAN: RAS Decompressor problem2: Possible data corruption\n");
  1116. #endif
  1117. return FALSE ;
  1118. }
  1119. // loop unrolled to handle lenght>backptr case
  1120. //
  1121. *s1=*s2;
  1122. *(s1+1)=*(s2+1);
  1123. s1+=2;
  1124. s2+=2;
  1125. length-=2;
  1126. //
  1127. // copy all the bytes
  1128. //
  1129. while (length) {
  1130. *s1++=*s2++;
  1131. length--;
  1132. }
  1133. //
  1134. // We have another copy item, and no literals
  1135. //
  1136. continue;
  1137. LITERAL:
  1138. //
  1139. // We have a literal
  1140. //
  1141. if (current >= historyend) {
  1142. #if DBG
  1143. DbgPrint("NDISWAN: RAS Decompressor problem3: Possible data corruption\n");
  1144. #endif
  1145. return FALSE ;
  1146. }
  1147. //*current++ = literallookup[length];
  1148. *current++ = (UCHAR)length;
  1149. } // while loop
  1150. // End case:
  1151. //
  1152. if (current >= historyend) {
  1153. #if DBG
  1154. DbgPrint("NDISWAN: RAS Decompressor problem4: Possible data corruption\n");
  1155. #endif
  1156. return FALSE ;
  1157. }
  1158. if ((bit == 16) && (pbyte == inend)) {
  1159. *current++ = *(pbyte -1) ;
  1160. }
  1161. #if DBG
  1162. if (context->DebugFence != DEBUG_FENCE_VALUE) {
  1163. DbgPrint("Decompression Error!\n");
  1164. DbgPrint("context %p, current %p, outstart %p\n", context, current, outstart);
  1165. DbgPrint("inbuf %p, inlength %d, start %p\n", inbuf, inlen, start);
  1166. DbgBreakPoint();
  1167. }
  1168. #endif
  1169. *outlen = (int)(current - outstart) ; // the length of decompressed data
  1170. *output = context->CurrentPtr ;
  1171. context->CurrentPtr = current ;
  1172. return TRUE ;
  1173. }
  1174. //
  1175. // This function uses the 16 byte user session key and the 8 byte
  1176. // challenge to create an intial 16 byte encryption session key.
  1177. //
  1178. VOID
  1179. GetStartKeyFromSHA(
  1180. PCRYPTO_INFO CryptoInfo,
  1181. PUCHAR Challenge
  1182. )
  1183. {
  1184. UCHAR Digest[A_SHA_DIGEST_LEN];
  1185. UCHAR SessionKeyChallenge[MAX_USERSESSIONKEY_SIZE + MAX_CHALLENGE_SIZE];
  1186. NdisZeroMemory(Digest, A_SHA_DIGEST_LEN);
  1187. //
  1188. // Copy the start session key
  1189. //
  1190. NdisMoveMemory(SessionKeyChallenge,
  1191. CryptoInfo->StartKey,
  1192. MAX_USERSESSIONKEY_SIZE);
  1193. //
  1194. // Append the challenge
  1195. //
  1196. NdisMoveMemory((PUCHAR)(SessionKeyChallenge + MAX_USERSESSIONKEY_SIZE),
  1197. Challenge,
  1198. MAX_CHALLENGE_SIZE);
  1199. //
  1200. // SHAInit(context)
  1201. // SHAUpdate(context, sessionkey, sessionkeylength)
  1202. // SHAUpdate(context, sessionkeychallenge, sessionkeylength + challengelength)
  1203. // SHAFinal(context, digest)
  1204. //
  1205. // Start key is the first 16 bytes of the digest.
  1206. //
  1207. A_SHAInit(CryptoInfo->Context);
  1208. A_SHAUpdate(CryptoInfo->Context,
  1209. CryptoInfo->StartKey,
  1210. MAX_USERSESSIONKEY_SIZE);
  1211. A_SHAUpdate(CryptoInfo->Context,
  1212. SessionKeyChallenge,
  1213. MAX_USERSESSIONKEY_SIZE + MAX_CHALLENGE_SIZE);
  1214. A_SHAFinal(CryptoInfo->Context,
  1215. Digest);
  1216. NdisMoveMemory(CryptoInfo->StartKey,
  1217. Digest,
  1218. CryptoInfo->SessionKeyLength);
  1219. NdisMoveMemory(CryptoInfo->SessionKey,
  1220. Digest,
  1221. CryptoInfo->SessionKeyLength);
  1222. }
  1223. VOID
  1224. GetNewKeyFromSHA(
  1225. PCRYPTO_INFO CryptoInfo
  1226. )
  1227. {
  1228. UCHAR Digest[A_SHA_DIGEST_LEN];
  1229. NdisZeroMemory(Digest, A_SHA_DIGEST_LEN);
  1230. A_SHAInit(CryptoInfo->Context);
  1231. A_SHAUpdate(CryptoInfo->Context,
  1232. CryptoInfo->StartKey,
  1233. CryptoInfo->SessionKeyLength);
  1234. A_SHAUpdate(CryptoInfo->Context,
  1235. SHApad1,
  1236. 40);
  1237. A_SHAUpdate(CryptoInfo->Context,
  1238. CryptoInfo->SessionKey,
  1239. CryptoInfo->SessionKeyLength);
  1240. A_SHAUpdate(CryptoInfo->Context,
  1241. SHApad2,
  1242. 40);
  1243. A_SHAFinal(CryptoInfo->Context,
  1244. Digest);
  1245. NdisMoveMemory(CryptoInfo->SessionKey,
  1246. Digest,
  1247. CryptoInfo->SessionKeyLength);
  1248. }
  1249. VOID
  1250. GetMasterKey(
  1251. PCRYPTO_INFO CryptoInfo,
  1252. PUCHAR NTResponse
  1253. )
  1254. {
  1255. UCHAR Digest[A_SHA_DIGEST_LEN];
  1256. PVOID Context;
  1257. NdisZeroMemory(Digest, A_SHA_DIGEST_LEN);
  1258. Context = CryptoInfo->Context;
  1259. A_SHAInit(Context);
  1260. #ifdef DEBUG_CCP
  1261. {
  1262. PUCHAR Key;
  1263. Key = CryptoInfo->StartKey;
  1264. DbgPrint("GMK-UserSessionKey: %.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x\n",
  1265. Key[0],Key[1],Key[2],Key[3],
  1266. Key[4],Key[5],Key[6],Key[7],
  1267. Key[8],Key[9],Key[10],Key[11],
  1268. Key[12],Key[13],Key[14],Key[15]);
  1269. Key = NTResponse;
  1270. DbgPrint("GMK-NTResponse: %.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x\n",
  1271. Key[0],Key[1],Key[2],Key[3],
  1272. Key[4],Key[5],Key[6],Key[7],
  1273. Key[8],Key[9],Key[10],Key[11],
  1274. Key[12],Key[13],Key[14],Key[15]);
  1275. DbgPrint(" %.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x\n",
  1276. Key[16],Key[17],Key[18],Key[19],
  1277. Key[20],Key[21],Key[22],Key[23]);
  1278. }
  1279. #endif
  1280. A_SHAUpdate(Context,
  1281. CryptoInfo->StartKey,
  1282. MAX_USERSESSIONKEY_SIZE);
  1283. A_SHAUpdate(Context,
  1284. NTResponse,
  1285. MAX_NT_RESPONSE);
  1286. A_SHAUpdate(Context,
  1287. "This is the MPPE Master Key",
  1288. 27);
  1289. A_SHAFinal(Context, Digest);
  1290. NdisMoveMemory(CryptoInfo->StartKey,
  1291. Digest,
  1292. MAX_USERSESSIONKEY_SIZE);
  1293. #ifdef DEBUG_CCP
  1294. {
  1295. PUCHAR Key;
  1296. Key = CryptoInfo->StartKey;
  1297. DbgPrint("MasterKey: %.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x\n",
  1298. Key[0],Key[1],Key[2],Key[3],
  1299. Key[4],Key[5],Key[6],Key[7],
  1300. Key[8],Key[9],Key[10],Key[11],
  1301. Key[12],Key[13],Key[14],Key[15]);
  1302. }
  1303. #endif
  1304. }
  1305. VOID
  1306. GetAsymetricStartKey(
  1307. PCRYPTO_INFO CryptoInfo,
  1308. BOOLEAN IsSend
  1309. )
  1310. {
  1311. UCHAR Digest[A_SHA_DIGEST_LEN];
  1312. PVOID Context;
  1313. PUCHAR s;
  1314. NdisZeroMemory(Digest, A_SHA_DIGEST_LEN);
  1315. Context = CryptoInfo->Context;
  1316. if (IsSend) {
  1317. if (CryptoInfo->Flags & CRYPTO_IS_SERVER) {
  1318. s = ClntRSrvS;
  1319. } else {
  1320. s = ClntSSrvR;
  1321. }
  1322. } else {
  1323. if (CryptoInfo->Flags & CRYPTO_IS_SERVER) {
  1324. s = ClntSSrvR;
  1325. } else {
  1326. s = ClntRSrvS;
  1327. }
  1328. }
  1329. #ifdef DEBUG_CCP
  1330. {
  1331. PUCHAR Key;
  1332. Key = CryptoInfo->StartKey;
  1333. DbgPrint("GASK-StartKey: %.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x\n",
  1334. Key[0],Key[1],Key[2],Key[3],
  1335. Key[4],Key[5],Key[6],Key[7],
  1336. Key[8],Key[9],Key[10],Key[11],
  1337. Key[12],Key[13],Key[14],Key[15]);
  1338. DbgPrint("GASK-String: %s\n", s);
  1339. }
  1340. #endif
  1341. A_SHAInit(Context);
  1342. A_SHAUpdate(Context,
  1343. CryptoInfo->StartKey,
  1344. MAX_USERSESSIONKEY_SIZE);
  1345. A_SHAUpdate(Context,SHApad1,40);
  1346. A_SHAUpdate(Context,s,strlen(s));
  1347. A_SHAUpdate(Context,SHApad2,40);
  1348. A_SHAFinal(Context,Digest);
  1349. NdisMoveMemory(CryptoInfo->StartKey,
  1350. Digest,
  1351. CryptoInfo->SessionKeyLength);
  1352. NdisMoveMemory(CryptoInfo->SessionKey,
  1353. Digest,
  1354. CryptoInfo->SessionKeyLength);
  1355. #ifdef DEBUG_CCP
  1356. {
  1357. PUCHAR Key;
  1358. Key = CryptoInfo->StartKey;
  1359. DbgPrint("%s %s AsymetricKey: %.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x\n",
  1360. (CryptoInfo->Flags & CRYPTO_IS_SERVER) ? "Server" : "Client",
  1361. (IsSend) ? "Send" : "Recv",
  1362. Key[0],Key[1],Key[2],Key[3],
  1363. Key[4],Key[5],Key[6],Key[7],
  1364. Key[8],Key[9],Key[10],Key[11],
  1365. Key[12],Key[13],Key[14],Key[15]);
  1366. }
  1367. #endif
  1368. }
  1369. /* Copyright (C) RSA Data Security, Inc. created 1993. This is an
  1370. unpublished work protected as such under copyright law. This work
  1371. contains proprietary, confidential, and trade secret information of
  1372. RSA Data Security, Inc. Use, disclosure or reproduction without the
  1373. express written authorization of RSA Data Security, Inc. is
  1374. prohibited.
  1375. */
  1376. /* SHA initialization. Begins an SHA operation, writing a new context.
  1377. */
  1378. void A_SHAInitCommon (context)
  1379. A_SHA_COMM_CTX *context;
  1380. {
  1381. context->count[0] = context->count[1] = 0;
  1382. /* Load magic initialization constants.
  1383. */
  1384. context->state[0] = 0x67452301;
  1385. context->state[1] = 0xefcdab89;
  1386. context->state[2] = 0x98badcfe;
  1387. context->state[3] = 0x10325476;
  1388. context->state[4] = 0xc3d2e1f0;
  1389. }
  1390. /* SHA block update operation. Continues an SHA message-digest
  1391. operation, processing another message block, and updating the
  1392. context.
  1393. */
  1394. void A_SHAUpdateCommon (context, partIn, partInLen, Transform)
  1395. A_SHA_COMM_CTX *context;
  1396. unsigned char *partIn;
  1397. ULONG partInLen;
  1398. A_SHA_TRANSFORM *Transform;
  1399. {
  1400. unsigned int bufferLen;
  1401. /* Compute length of buffer */
  1402. bufferLen = (unsigned int)(context->count[1] & 0x3f);
  1403. /* Update number of bytes */
  1404. if ((context->count[1] += partInLen) < partInLen)
  1405. context->count[0]++;
  1406. /* If previous input in buffer, buffer new input and transform if
  1407. possible.
  1408. */
  1409. if (bufferLen > 0 && bufferLen + partInLen >= 64) {
  1410. NdisMoveMemory(context->buffer+bufferLen, partIn, 64-bufferLen);
  1411. partIn += (64-bufferLen);
  1412. partInLen -= (64-bufferLen);
  1413. (*Transform) (context->state, context->buffer);
  1414. bufferLen = 0;
  1415. }
  1416. /* Transform directly from input.
  1417. */
  1418. while (partInLen >= 64) {
  1419. (*Transform) (context->state, partIn);
  1420. partIn += 64;
  1421. partInLen -= 64;
  1422. }
  1423. /* Buffer remaining input */
  1424. NdisMoveMemory((context->buffer+bufferLen), partIn, partInLen);
  1425. }
  1426. /* SHA finalization. Ends an SHA message-digest operation, writing
  1427. the message digest and zeroizing the context.
  1428. */
  1429. void A_SHAFinalCommon (context, digest, Transform)
  1430. A_SHA_COMM_CTX *context;
  1431. unsigned char digest[A_SHA_DIGEST_LEN];
  1432. A_SHA_TRANSFORM *Transform;
  1433. {
  1434. ULONG bitCount[2];
  1435. unsigned char pad[72];
  1436. unsigned int padLen;
  1437. /* Compute padding: 80 00 00 ... 00 00 <bit count>
  1438. */
  1439. padLen = 64 - (unsigned int)(context->count[1] & 0x3f);
  1440. if (padLen <= 8)
  1441. padLen += 64;
  1442. pad[0] = 0x80;
  1443. NdisZeroMemory(pad+1, padLen-7);
  1444. bitCount[0] = (context->count[0] << 3) | (context->count[1] >> 29);
  1445. bitCount[1] = context->count[1] << 3;
  1446. ByteReverse ((UNALIGNED ULONG*)(pad+padLen-8), bitCount, 2);
  1447. /* Digest padding */
  1448. A_SHAUpdateCommon (context, pad, padLen, Transform);
  1449. /* Store digest */
  1450. ByteReverse ((UNALIGNED ULONG*)digest, context->state, 5);
  1451. /* Restart the context */
  1452. A_SHAInitCommon (context);
  1453. }
  1454. void A_SHAInit (A_SHA_CTX *context)
  1455. {
  1456. A_SHAInitCommon (&context->commonContext);
  1457. }
  1458. void A_SHAUpdate (context, partIn, partInLen)
  1459. A_SHA_CTX *context;
  1460. unsigned char *partIn;
  1461. unsigned int partInLen;
  1462. {
  1463. A_SHAUpdateCommon (&context->commonContext, partIn, partInLen, SHATransform);
  1464. }
  1465. void A_SHAFinal (context, digest)
  1466. A_SHA_CTX *context;
  1467. unsigned char digest[A_SHA_DIGEST_LEN];
  1468. {
  1469. A_SHAFinalCommon (&context->commonContext, digest, SHATransform);
  1470. }
  1471. void SHATransform (state, block)
  1472. ULONG state[5];
  1473. unsigned char block[64];
  1474. {
  1475. ULONG a = state[0], b = state[1], c = state[2], d = state[3],
  1476. e = state[4], x[80];
  1477. ByteReverse (x, (ULONG*)block, 16);
  1478. SHAExpand (x);
  1479. /* Round 1 */
  1480. FF (a, b, c, d, e, x[ 0]);
  1481. FF (e, a, b, c, d, x[ 1]);
  1482. FF (d, e, a, b, c, x[ 2]);
  1483. FF (c, d, e, a, b, x[ 3]);
  1484. FF (b, c, d, e, a, x[ 4]);
  1485. FF (a, b, c, d, e, x[ 5]);
  1486. FF (e, a, b, c, d, x[ 6]);
  1487. FF (d, e, a, b, c, x[ 7]);
  1488. FF (c, d, e, a, b, x[ 8]);
  1489. FF (b, c, d, e, a, x[ 9]);
  1490. FF (a, b, c, d, e, x[10]);
  1491. FF (e, a, b, c, d, x[11]);
  1492. FF (d, e, a, b, c, x[12]);
  1493. FF (c, d, e, a, b, x[13]);
  1494. FF (b, c, d, e, a, x[14]);
  1495. FF (a, b, c, d, e, x[15]);
  1496. FF (e, a, b, c, d, x[16]);
  1497. FF (d, e, a, b, c, x[17]);
  1498. FF (c, d, e, a, b, x[18]);
  1499. FF (b, c, d, e, a, x[19]);
  1500. /* Round 2 */
  1501. GG (a, b, c, d, e, x[20]);
  1502. GG (e, a, b, c, d, x[21]);
  1503. GG (d, e, a, b, c, x[22]);
  1504. GG (c, d, e, a, b, x[23]);
  1505. GG (b, c, d, e, a, x[24]);
  1506. GG (a, b, c, d, e, x[25]);
  1507. GG (e, a, b, c, d, x[26]);
  1508. GG (d, e, a, b, c, x[27]);
  1509. GG (c, d, e, a, b, x[28]);
  1510. GG (b, c, d, e, a, x[29]);
  1511. GG (a, b, c, d, e, x[30]);
  1512. GG (e, a, b, c, d, x[31]);
  1513. GG (d, e, a, b, c, x[32]);
  1514. GG (c, d, e, a, b, x[33]);
  1515. GG (b, c, d, e, a, x[34]);
  1516. GG (a, b, c, d, e, x[35]);
  1517. GG (e, a, b, c, d, x[36]);
  1518. GG (d, e, a, b, c, x[37]);
  1519. GG (c, d, e, a, b, x[38]);
  1520. GG (b, c, d, e, a, x[39]);
  1521. /* Round 3 */
  1522. HH (a, b, c, d, e, x[40]);
  1523. HH (e, a, b, c, d, x[41]);
  1524. HH (d, e, a, b, c, x[42]);
  1525. HH (c, d, e, a, b, x[43]);
  1526. HH (b, c, d, e, a, x[44]);
  1527. HH (a, b, c, d, e, x[45]);
  1528. HH (e, a, b, c, d, x[46]);
  1529. HH (d, e, a, b, c, x[47]);
  1530. HH (c, d, e, a, b, x[48]);
  1531. HH (b, c, d, e, a, x[49]);
  1532. HH (a, b, c, d, e, x[50]);
  1533. HH (e, a, b, c, d, x[51]);
  1534. HH (d, e, a, b, c, x[52]);
  1535. HH (c, d, e, a, b, x[53]);
  1536. HH (b, c, d, e, a, x[54]);
  1537. HH (a, b, c, d, e, x[55]);
  1538. HH (e, a, b, c, d, x[56]);
  1539. HH (d, e, a, b, c, x[57]);
  1540. HH (c, d, e, a, b, x[58]);
  1541. HH (b, c, d, e, a, x[59]);
  1542. /* Round 4 */
  1543. II (a, b, c, d, e, x[60]);
  1544. II (e, a, b, c, d, x[61]);
  1545. II (d, e, a, b, c, x[62]);
  1546. II (c, d, e, a, b, x[63]);
  1547. II (b, c, d, e, a, x[64]);
  1548. II (a, b, c, d, e, x[65]);
  1549. II (e, a, b, c, d, x[66]);
  1550. II (d, e, a, b, c, x[67]);
  1551. II (c, d, e, a, b, x[68]);
  1552. II (b, c, d, e, a, x[69]);
  1553. II (a, b, c, d, e, x[70]);
  1554. II (e, a, b, c, d, x[71]);
  1555. II (d, e, a, b, c, x[72]);
  1556. II (c, d, e, a, b, x[73]);
  1557. II (b, c, d, e, a, x[74]);
  1558. II (a, b, c, d, e, x[75]);
  1559. II (e, a, b, c, d, x[76]);
  1560. II (d, e, a, b, c, x[77]);
  1561. II (c, d, e, a, b, x[78]);
  1562. II (b, c, d, e, a, x[79]);
  1563. state[0] += a;
  1564. state[1] += b;
  1565. state[2] += c;
  1566. state[3] += d;
  1567. state[4] += e;
  1568. /* Zeroize potentially sensitive information.
  1569. */
  1570. NdisZeroMemory((void *)x, sizeof (x));
  1571. }
  1572. /* Expands x[0..15] into x[16..79], according to the recurrence
  1573. x[i] = x[i-3] ^ x[i-8] ^ x[i-14] ^ x[i-16].
  1574. */
  1575. void SHAExpand (x)
  1576. ULONG x[80];
  1577. {
  1578. unsigned int i;
  1579. ULONG tmp;
  1580. for (i = 16; i < 80; i++)
  1581. {
  1582. tmp = x[i-3] ^ x[i-8] ^ x[i-14] ^ x[i-16];
  1583. x[i] = (tmp << 1) | (tmp >> 31);
  1584. }
  1585. }
  1586. VOID
  1587. ByteReverse(
  1588. UNALIGNED ULONG *Out,
  1589. ULONG *In,
  1590. ULONG Count
  1591. )
  1592. {
  1593. ULONG i;
  1594. ULONG Value;
  1595. for (i = 0; i < Count; i++) {
  1596. Value = (ULONG)(In[i] << 16) | (In[i] >> 16);
  1597. Out[i] = ((Value & 0xFF00FF00L) >> 8) | ((Value & 0x00FF00FFL) << 8);
  1598. }
  1599. }