sshzlib.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253
  1. /*
  2. * Zlib (RFC1950 / RFC1951) compression for PuTTY.
  3. *
  4. * There will no doubt be criticism of my decision to reimplement
  5. * Zlib compression from scratch instead of using the existing zlib
  6. * code. People will cry `reinventing the wheel'; they'll claim
  7. * that the `fundamental basis of OSS' is code reuse; they'll want
  8. * to see a really good reason for me having chosen not to use the
  9. * existing code.
  10. *
  11. * Well, here are my reasons. Firstly, I don't want to link the
  12. * whole of zlib into the PuTTY binary; PuTTY is justifiably proud
  13. * of its small size and I think zlib contains a lot of unnecessary
  14. * baggage for the kind of compression that SSH requires.
  15. *
  16. * Secondly, I also don't like the alternative of using zlib.dll.
  17. * Another thing PuTTY is justifiably proud of is its ease of
  18. * installation, and the last thing I want to do is to start
  19. * mandating DLLs. Not only that, but there are two _kinds_ of
  20. * zlib.dll kicking around, one with C calling conventions on the
  21. * exported functions and another with WINAPI conventions, and
  22. * there would be a significant danger of getting the wrong one.
  23. *
  24. * Thirdly, there seems to be a difference of opinion on the IETF
  25. * secsh mailing list about the correct way to round off a
  26. * compressed packet and start the next. In particular, there's
  27. * some talk of switching to a mechanism zlib isn't currently
  28. * capable of supporting (see below for an explanation). Given that
  29. * sort of uncertainty, I thought it might be better to have code
  30. * that will support even the zlib-incompatible worst case.
  31. *
  32. * Fourthly, it's a _second implementation_. Second implementations
  33. * are fundamentally a Good Thing in standardisation efforts. The
  34. * difference of opinion mentioned above has arisen _precisely_
  35. * because there has been only one zlib implementation and
  36. * everybody has used it. I don't intend that this should happen
  37. * again.
  38. */
  39. #include <stdlib.h>
  40. #include <string.h>
  41. #include <assert.h>
  42. #include "defs.h"
  43. #include "ssh.h"
  44. /* ----------------------------------------------------------------------
  45. * Basic LZ77 code. This bit is designed modularly, so it could be
  46. * ripped out and used in a different LZ77 compressor. Go to it,
  47. * and good luck :-)
  48. */
  49. struct LZ77InternalContext;
  50. struct LZ77Context {
  51. struct LZ77InternalContext *ictx;
  52. void *userdata;
  53. void (*literal) (struct LZ77Context * ctx, unsigned char c);
  54. void (*match) (struct LZ77Context * ctx, int distance, int len);
  55. };
  56. /*
  57. * Initialise the private fields of an LZ77Context. It's up to the
  58. * user to initialise the public fields.
  59. */
  60. static int lz77_init(struct LZ77Context *ctx);
  61. /*
  62. * Supply data to be compressed. Will update the private fields of
  63. * the LZ77Context, and will call literal() and match() to output.
  64. * If `compress' is false, it will never emit a match, but will
  65. * instead call literal() for everything.
  66. */
  67. static void lz77_compress(struct LZ77Context *ctx,
  68. const unsigned char *data, int len);
  69. /*
  70. * Modifiable parameters.
  71. */
  72. #define WINSIZE 32768 /* window size. Must be power of 2! */
  73. #define HASHMAX 2039 /* one more than max hash value */
  74. #define MAXMATCH 32 /* how many matches we track */
  75. #define HASHCHARS 3 /* how many chars make a hash */
  76. /*
  77. * This compressor takes a less slapdash approach than the
  78. * gzip/zlib one. Rather than allowing our hash chains to fall into
  79. * disuse near the far end, we keep them doubly linked so we can
  80. * _find_ the far end, and then every time we add a new byte to the
  81. * window (thus rolling round by one and removing the previous
  82. * byte), we can carefully remove the hash chain entry.
  83. */
  84. #define INVALID -1 /* invalid hash _and_ invalid offset */
  85. struct WindowEntry {
  86. short next, prev; /* array indices within the window */
  87. short hashval;
  88. };
  89. struct HashEntry {
  90. short first; /* window index of first in chain */
  91. };
  92. struct Match {
  93. int distance, len;
  94. };
  95. struct LZ77InternalContext {
  96. struct WindowEntry win[WINSIZE];
  97. unsigned char data[WINSIZE];
  98. int winpos;
  99. struct HashEntry hashtab[HASHMAX];
  100. unsigned char pending[HASHCHARS];
  101. int npending;
  102. };
  103. static int lz77_hash(const unsigned char *data)
  104. {
  105. return (257 * data[0] + 263 * data[1] + 269 * data[2]) % HASHMAX;
  106. }
  107. static int lz77_init(struct LZ77Context *ctx)
  108. {
  109. struct LZ77InternalContext *st;
  110. int i;
  111. st = snew(struct LZ77InternalContext);
  112. if (!st)
  113. return 0;
  114. ctx->ictx = st;
  115. for (i = 0; i < WINSIZE; i++)
  116. st->win[i].next = st->win[i].prev = st->win[i].hashval = INVALID;
  117. for (i = 0; i < HASHMAX; i++)
  118. st->hashtab[i].first = INVALID;
  119. st->winpos = 0;
  120. st->npending = 0;
  121. return 1;
  122. }
  123. static void lz77_advance(struct LZ77InternalContext *st,
  124. unsigned char c, int hash)
  125. {
  126. int off;
  127. /*
  128. * Remove the hash entry at winpos from the tail of its chain,
  129. * or empty the chain if it's the only thing on the chain.
  130. */
  131. if (st->win[st->winpos].prev != INVALID) {
  132. st->win[st->win[st->winpos].prev].next = INVALID;
  133. } else if (st->win[st->winpos].hashval != INVALID) {
  134. st->hashtab[st->win[st->winpos].hashval].first = INVALID;
  135. }
  136. /*
  137. * Create a new entry at winpos and add it to the head of its
  138. * hash chain.
  139. */
  140. st->win[st->winpos].hashval = hash;
  141. st->win[st->winpos].prev = INVALID;
  142. off = st->win[st->winpos].next = st->hashtab[hash].first;
  143. st->hashtab[hash].first = st->winpos;
  144. if (off != INVALID)
  145. st->win[off].prev = st->winpos;
  146. st->data[st->winpos] = c;
  147. /*
  148. * Advance the window pointer.
  149. */
  150. st->winpos = (st->winpos + 1) & (WINSIZE - 1);
  151. }
  152. #define CHARAT(k) ( (k)<0 ? st->data[(st->winpos+k)&(WINSIZE-1)] : data[k] )
  153. static void lz77_compress(struct LZ77Context *ctx,
  154. const unsigned char *data, int len)
  155. {
  156. struct LZ77InternalContext *st = ctx->ictx;
  157. int i, distance, off, nmatch, matchlen, advance;
  158. struct Match defermatch, matches[MAXMATCH];
  159. int deferchr;
  160. assert(st->npending <= HASHCHARS);
  161. /*
  162. * Add any pending characters from last time to the window. (We
  163. * might not be able to.)
  164. *
  165. * This leaves st->pending empty in the usual case (when len >=
  166. * HASHCHARS); otherwise it leaves st->pending empty enough that
  167. * adding all the remaining 'len' characters will not push it past
  168. * HASHCHARS in size.
  169. */
  170. for (i = 0; i < st->npending; i++) {
  171. unsigned char foo[HASHCHARS];
  172. int j;
  173. if (len + st->npending - i < HASHCHARS) {
  174. /* Update the pending array. */
  175. for (j = i; j < st->npending; j++)
  176. st->pending[j - i] = st->pending[j];
  177. break;
  178. }
  179. for (j = 0; j < HASHCHARS; j++)
  180. foo[j] = (i + j < st->npending ? st->pending[i + j] :
  181. data[i + j - st->npending]);
  182. lz77_advance(st, foo[0], lz77_hash(foo));
  183. }
  184. st->npending -= i;
  185. defermatch.distance = 0; /* appease compiler */
  186. defermatch.len = 0;
  187. deferchr = '\0';
  188. while (len > 0) {
  189. if (len >= HASHCHARS) {
  190. /*
  191. * Hash the next few characters.
  192. */
  193. int hash = lz77_hash(data);
  194. /*
  195. * Look the hash up in the corresponding hash chain and see
  196. * what we can find.
  197. */
  198. nmatch = 0;
  199. for (off = st->hashtab[hash].first;
  200. off != INVALID; off = st->win[off].next) {
  201. /* distance = 1 if off == st->winpos-1 */
  202. /* distance = WINSIZE if off == st->winpos */
  203. distance =
  204. WINSIZE - (off + WINSIZE - st->winpos) % WINSIZE;
  205. for (i = 0; i < HASHCHARS; i++)
  206. if (CHARAT(i) != CHARAT(i - distance))
  207. break;
  208. if (i == HASHCHARS) {
  209. matches[nmatch].distance = distance;
  210. matches[nmatch].len = 3;
  211. if (++nmatch >= MAXMATCH)
  212. break;
  213. }
  214. }
  215. } else {
  216. nmatch = 0;
  217. }
  218. if (nmatch > 0) {
  219. /*
  220. * We've now filled up matches[] with nmatch potential
  221. * matches. Follow them down to find the longest. (We
  222. * assume here that it's always worth favouring a
  223. * longer match over a shorter one.)
  224. */
  225. matchlen = HASHCHARS;
  226. while (matchlen < len) {
  227. int j;
  228. for (i = j = 0; i < nmatch; i++) {
  229. if (CHARAT(matchlen) ==
  230. CHARAT(matchlen - matches[i].distance)) {
  231. matches[j++] = matches[i];
  232. }
  233. }
  234. if (j == 0)
  235. break;
  236. matchlen++;
  237. nmatch = j;
  238. }
  239. /*
  240. * We've now got all the longest matches. We favour the
  241. * shorter distances, which means we go with matches[0].
  242. * So see if we want to defer it or throw it away.
  243. */
  244. matches[0].len = matchlen;
  245. if (defermatch.len > 0) {
  246. if (matches[0].len > defermatch.len + 1) {
  247. /* We have a better match. Emit the deferred char,
  248. * and defer this match. */
  249. ctx->literal(ctx, (unsigned char) deferchr);
  250. defermatch = matches[0];
  251. deferchr = data[0];
  252. advance = 1;
  253. } else {
  254. /* We don't have a better match. Do the deferred one. */
  255. ctx->match(ctx, defermatch.distance, defermatch.len);
  256. advance = defermatch.len - 1;
  257. defermatch.len = 0;
  258. }
  259. } else {
  260. /* There was no deferred match. Defer this one. */
  261. defermatch = matches[0];
  262. deferchr = data[0];
  263. advance = 1;
  264. }
  265. } else {
  266. /*
  267. * We found no matches. Emit the deferred match, if
  268. * any; otherwise emit a literal.
  269. */
  270. if (defermatch.len > 0) {
  271. ctx->match(ctx, defermatch.distance, defermatch.len);
  272. advance = defermatch.len - 1;
  273. defermatch.len = 0;
  274. } else {
  275. ctx->literal(ctx, data[0]);
  276. advance = 1;
  277. }
  278. }
  279. /*
  280. * Now advance the position by `advance' characters,
  281. * keeping the window and hash chains consistent.
  282. */
  283. while (advance > 0) {
  284. if (len >= HASHCHARS) {
  285. lz77_advance(st, *data, lz77_hash(data));
  286. } else {
  287. assert(st->npending < HASHCHARS);
  288. st->pending[st->npending++] = *data;
  289. }
  290. data++;
  291. len--;
  292. advance--;
  293. }
  294. }
  295. }
  296. /* ----------------------------------------------------------------------
  297. * Zlib compression. We always use the static Huffman tree option.
  298. * Mostly this is because it's hard to scan a block in advance to
  299. * work out better trees; dynamic trees are great when you're
  300. * compressing a large file under no significant time constraint,
  301. * but when you're compressing little bits in real time, things get
  302. * hairier.
  303. *
  304. * I suppose it's possible that I could compute Huffman trees based
  305. * on the frequencies in the _previous_ block, as a sort of
  306. * heuristic, but I'm not confident that the gain would balance out
  307. * having to transmit the trees.
  308. */
  309. struct Outbuf {
  310. strbuf *outbuf;
  311. unsigned long outbits;
  312. int noutbits;
  313. bool firstblock;
  314. };
  315. static void outbits(struct Outbuf *out, unsigned long bits, int nbits)
  316. {
  317. assert(out->noutbits + nbits <= 32);
  318. out->outbits |= bits << out->noutbits;
  319. out->noutbits += nbits;
  320. while (out->noutbits >= 8) {
  321. put_byte(out->outbuf, out->outbits & 0xFF);
  322. out->outbits >>= 8;
  323. out->noutbits -= 8;
  324. }
  325. }
  326. static const unsigned char mirrorbytes[256] = {
  327. 0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0,
  328. 0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0,
  329. 0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8,
  330. 0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8,
  331. 0x04, 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, 0xe4,
  332. 0x14, 0x94, 0x54, 0xd4, 0x34, 0xb4, 0x74, 0xf4,
  333. 0x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c, 0xec,
  334. 0x1c, 0x9c, 0x5c, 0xdc, 0x3c, 0xbc, 0x7c, 0xfc,
  335. 0x02, 0x82, 0x42, 0xc2, 0x22, 0xa2, 0x62, 0xe2,
  336. 0x12, 0x92, 0x52, 0xd2, 0x32, 0xb2, 0x72, 0xf2,
  337. 0x0a, 0x8a, 0x4a, 0xca, 0x2a, 0xaa, 0x6a, 0xea,
  338. 0x1a, 0x9a, 0x5a, 0xda, 0x3a, 0xba, 0x7a, 0xfa,
  339. 0x06, 0x86, 0x46, 0xc6, 0x26, 0xa6, 0x66, 0xe6,
  340. 0x16, 0x96, 0x56, 0xd6, 0x36, 0xb6, 0x76, 0xf6,
  341. 0x0e, 0x8e, 0x4e, 0xce, 0x2e, 0xae, 0x6e, 0xee,
  342. 0x1e, 0x9e, 0x5e, 0xde, 0x3e, 0xbe, 0x7e, 0xfe,
  343. 0x01, 0x81, 0x41, 0xc1, 0x21, 0xa1, 0x61, 0xe1,
  344. 0x11, 0x91, 0x51, 0xd1, 0x31, 0xb1, 0x71, 0xf1,
  345. 0x09, 0x89, 0x49, 0xc9, 0x29, 0xa9, 0x69, 0xe9,
  346. 0x19, 0x99, 0x59, 0xd9, 0x39, 0xb9, 0x79, 0xf9,
  347. 0x05, 0x85, 0x45, 0xc5, 0x25, 0xa5, 0x65, 0xe5,
  348. 0x15, 0x95, 0x55, 0xd5, 0x35, 0xb5, 0x75, 0xf5,
  349. 0x0d, 0x8d, 0x4d, 0xcd, 0x2d, 0xad, 0x6d, 0xed,
  350. 0x1d, 0x9d, 0x5d, 0xdd, 0x3d, 0xbd, 0x7d, 0xfd,
  351. 0x03, 0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0xe3,
  352. 0x13, 0x93, 0x53, 0xd3, 0x33, 0xb3, 0x73, 0xf3,
  353. 0x0b, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb,
  354. 0x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, 0x7b, 0xfb,
  355. 0x07, 0x87, 0x47, 0xc7, 0x27, 0xa7, 0x67, 0xe7,
  356. 0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7,
  357. 0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef,
  358. 0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff,
  359. };
  360. typedef struct {
  361. short code, extrabits;
  362. int min, max;
  363. } coderecord;
  364. static const coderecord lencodes[] = {
  365. {257, 0, 3, 3},
  366. {258, 0, 4, 4},
  367. {259, 0, 5, 5},
  368. {260, 0, 6, 6},
  369. {261, 0, 7, 7},
  370. {262, 0, 8, 8},
  371. {263, 0, 9, 9},
  372. {264, 0, 10, 10},
  373. {265, 1, 11, 12},
  374. {266, 1, 13, 14},
  375. {267, 1, 15, 16},
  376. {268, 1, 17, 18},
  377. {269, 2, 19, 22},
  378. {270, 2, 23, 26},
  379. {271, 2, 27, 30},
  380. {272, 2, 31, 34},
  381. {273, 3, 35, 42},
  382. {274, 3, 43, 50},
  383. {275, 3, 51, 58},
  384. {276, 3, 59, 66},
  385. {277, 4, 67, 82},
  386. {278, 4, 83, 98},
  387. {279, 4, 99, 114},
  388. {280, 4, 115, 130},
  389. {281, 5, 131, 162},
  390. {282, 5, 163, 194},
  391. {283, 5, 195, 226},
  392. {284, 5, 227, 257},
  393. {285, 0, 258, 258},
  394. };
  395. static const coderecord distcodes[] = {
  396. {0, 0, 1, 1},
  397. {1, 0, 2, 2},
  398. {2, 0, 3, 3},
  399. {3, 0, 4, 4},
  400. {4, 1, 5, 6},
  401. {5, 1, 7, 8},
  402. {6, 2, 9, 12},
  403. {7, 2, 13, 16},
  404. {8, 3, 17, 24},
  405. {9, 3, 25, 32},
  406. {10, 4, 33, 48},
  407. {11, 4, 49, 64},
  408. {12, 5, 65, 96},
  409. {13, 5, 97, 128},
  410. {14, 6, 129, 192},
  411. {15, 6, 193, 256},
  412. {16, 7, 257, 384},
  413. {17, 7, 385, 512},
  414. {18, 8, 513, 768},
  415. {19, 8, 769, 1024},
  416. {20, 9, 1025, 1536},
  417. {21, 9, 1537, 2048},
  418. {22, 10, 2049, 3072},
  419. {23, 10, 3073, 4096},
  420. {24, 11, 4097, 6144},
  421. {25, 11, 6145, 8192},
  422. {26, 12, 8193, 12288},
  423. {27, 12, 12289, 16384},
  424. {28, 13, 16385, 24576},
  425. {29, 13, 24577, 32768},
  426. };
  427. static void zlib_literal(struct LZ77Context *ectx, unsigned char c)
  428. {
  429. struct Outbuf *out = (struct Outbuf *) ectx->userdata;
  430. if (c <= 143) {
  431. /* 0 through 143 are 8 bits long starting at 00110000. */
  432. outbits(out, mirrorbytes[0x30 + c], 8);
  433. } else {
  434. /* 144 through 255 are 9 bits long starting at 110010000. */
  435. outbits(out, 1 + 2 * mirrorbytes[0x90 - 144 + c], 9);
  436. }
  437. }
  438. static void zlib_match(struct LZ77Context *ectx, int distance, int len)
  439. {
  440. const coderecord *d, *l;
  441. int i, j, k;
  442. struct Outbuf *out = (struct Outbuf *) ectx->userdata;
  443. while (len > 0) {
  444. int thislen;
  445. /*
  446. * We can transmit matches of lengths 3 through 258
  447. * inclusive. So if len exceeds 258, we must transmit in
  448. * several steps, with 258 or less in each step.
  449. *
  450. * Specifically: if len >= 261, we can transmit 258 and be
  451. * sure of having at least 3 left for the next step. And if
  452. * len <= 258, we can just transmit len. But if len == 259
  453. * or 260, we must transmit len-3.
  454. */
  455. thislen = (len > 260 ? 258 : len <= 258 ? len : len - 3);
  456. len -= thislen;
  457. /*
  458. * Binary-search to find which length code we're
  459. * transmitting.
  460. */
  461. i = -1;
  462. j = lenof(lencodes);
  463. while (1) {
  464. assert(j - i >= 2);
  465. k = (j + i) / 2;
  466. if (thislen < lencodes[k].min)
  467. j = k;
  468. else if (thislen > lencodes[k].max)
  469. i = k;
  470. else {
  471. l = &lencodes[k];
  472. break; /* found it! */
  473. }
  474. }
  475. /*
  476. * Transmit the length code. 256-279 are seven bits
  477. * starting at 0000000; 280-287 are eight bits starting at
  478. * 11000000.
  479. */
  480. if (l->code <= 279) {
  481. outbits(out, mirrorbytes[(l->code - 256) * 2], 7);
  482. } else {
  483. outbits(out, mirrorbytes[0xc0 - 280 + l->code], 8);
  484. }
  485. /*
  486. * Transmit the extra bits.
  487. */
  488. if (l->extrabits)
  489. outbits(out, thislen - l->min, l->extrabits);
  490. /*
  491. * Binary-search to find which distance code we're
  492. * transmitting.
  493. */
  494. i = -1;
  495. j = lenof(distcodes);
  496. while (1) {
  497. assert(j - i >= 2);
  498. k = (j + i) / 2;
  499. if (distance < distcodes[k].min)
  500. j = k;
  501. else if (distance > distcodes[k].max)
  502. i = k;
  503. else {
  504. d = &distcodes[k];
  505. break; /* found it! */
  506. }
  507. }
  508. /*
  509. * Transmit the distance code. Five bits starting at 00000.
  510. */
  511. outbits(out, mirrorbytes[d->code * 8], 5);
  512. /*
  513. * Transmit the extra bits.
  514. */
  515. if (d->extrabits)
  516. outbits(out, distance - d->min, d->extrabits);
  517. }
  518. }
  519. struct ssh_zlib_compressor {
  520. struct LZ77Context ectx;
  521. ssh_compressor sc;
  522. };
  523. ssh_compressor *zlib_compress_init(void)
  524. {
  525. struct Outbuf *out;
  526. struct ssh_zlib_compressor *comp = snew(struct ssh_zlib_compressor);
  527. lz77_init(&comp->ectx);
  528. comp->sc.vt = &ssh_zlib;
  529. comp->ectx.literal = zlib_literal;
  530. comp->ectx.match = zlib_match;
  531. out = snew(struct Outbuf);
  532. out->outbuf = NULL;
  533. out->outbits = out->noutbits = 0;
  534. out->firstblock = true;
  535. comp->ectx.userdata = out;
  536. return &comp->sc;
  537. }
  538. void zlib_compress_cleanup(ssh_compressor *sc)
  539. {
  540. struct ssh_zlib_compressor *comp =
  541. container_of(sc, struct ssh_zlib_compressor, sc);
  542. struct Outbuf *out = (struct Outbuf *)comp->ectx.userdata;
  543. if (out->outbuf)
  544. strbuf_free(out->outbuf);
  545. sfree(out);
  546. sfree(comp->ectx.ictx);
  547. sfree(comp);
  548. }
  549. void zlib_compress_block(ssh_compressor *sc,
  550. const unsigned char *block, int len,
  551. unsigned char **outblock, int *outlen,
  552. int minlen)
  553. {
  554. struct ssh_zlib_compressor *comp =
  555. container_of(sc, struct ssh_zlib_compressor, sc);
  556. struct Outbuf *out = (struct Outbuf *) comp->ectx.userdata;
  557. bool in_block;
  558. assert(!out->outbuf);
  559. out->outbuf = strbuf_new_nm();
  560. /*
  561. * If this is the first block, output the Zlib (RFC1950) header
  562. * bytes 78 9C. (Deflate compression, 32K window size, default
  563. * algorithm.)
  564. */
  565. if (out->firstblock) {
  566. outbits(out, 0x9C78, 16);
  567. out->firstblock = false;
  568. in_block = false;
  569. } else
  570. in_block = true;
  571. if (!in_block) {
  572. /*
  573. * Start a Deflate (RFC1951) fixed-trees block. We
  574. * transmit a zero bit (BFINAL=0), followed by a zero
  575. * bit and a one bit (BTYPE=01). Of course these are in
  576. * the wrong order (01 0).
  577. */
  578. outbits(out, 2, 3);
  579. }
  580. /*
  581. * Do the compression.
  582. */
  583. lz77_compress(&comp->ectx, block, len);
  584. /*
  585. * End the block (by transmitting code 256, which is
  586. * 0000000 in fixed-tree mode), and transmit some empty
  587. * blocks to ensure we have emitted the byte containing the
  588. * last piece of genuine data. There are three ways we can
  589. * do this:
  590. *
  591. * - Minimal flush. Output end-of-block and then open a
  592. * new static block. This takes 9 bits, which is
  593. * guaranteed to flush out the last genuine code in the
  594. * closed block; but allegedly zlib can't handle it.
  595. *
  596. * - Zlib partial flush. Output EOB, open and close an
  597. * empty static block, and _then_ open the new block.
  598. * This is the best zlib can handle.
  599. *
  600. * - Zlib sync flush. Output EOB, then an empty
  601. * _uncompressed_ block (000, then sync to byte
  602. * boundary, then send bytes 00 00 FF FF). Then open the
  603. * new block.
  604. *
  605. * For the moment, we will use Zlib partial flush.
  606. */
  607. outbits(out, 0, 7); /* close block */
  608. outbits(out, 2, 3 + 7); /* empty static block */
  609. outbits(out, 2, 3); /* open new block */
  610. /*
  611. * If we've been asked to pad out the compressed data until it's
  612. * at least a given length, do so by emitting further empty static
  613. * blocks.
  614. */
  615. while (out->outbuf->len < minlen) {
  616. outbits(out, 0, 7); /* close block */
  617. outbits(out, 2, 3); /* open new static block */
  618. }
  619. *outlen = out->outbuf->len;
  620. *outblock = (unsigned char *)strbuf_to_str(out->outbuf);
  621. out->outbuf = NULL;
  622. }
  623. /* ----------------------------------------------------------------------
  624. * Zlib decompression. Of course, even though our compressor always
  625. * uses static trees, our _decompressor_ has to be capable of
  626. * handling dynamic trees if it sees them.
  627. */
  628. /*
  629. * The way we work the Huffman decode is to have a table lookup on
  630. * the first N bits of the input stream (in the order they arrive,
  631. * of course, i.e. the first bit of the Huffman code is in bit 0).
  632. * Each table entry lists the number of bits to consume, plus
  633. * either an output code or a pointer to a secondary table.
  634. */
  635. struct zlib_table;
  636. struct zlib_tableentry;
  637. struct zlib_tableentry {
  638. unsigned char nbits;
  639. short code;
  640. struct zlib_table *nexttable;
  641. };
  642. struct zlib_table {
  643. int mask; /* mask applied to input bit stream */
  644. struct zlib_tableentry *table;
  645. };
  646. #define MAXCODELEN 16
  647. #define MAXSYMS 288
  648. /*
  649. * Build a single-level decode table for elements
  650. * [minlength,maxlength) of the provided code/length tables, and
  651. * recurse to build subtables.
  652. */
  653. static struct zlib_table *zlib_mkonetab(int *codes, unsigned char *lengths,
  654. int nsyms,
  655. int pfx, int pfxbits, int bits)
  656. {
  657. struct zlib_table *tab = snew(struct zlib_table);
  658. int pfxmask = (1 << pfxbits) - 1;
  659. int nbits, i, j, code;
  660. tab->table = snewn(1 << bits, struct zlib_tableentry);
  661. tab->mask = (1 << bits) - 1;
  662. for (code = 0; code <= tab->mask; code++) {
  663. tab->table[code].code = -1;
  664. tab->table[code].nbits = 0;
  665. tab->table[code].nexttable = NULL;
  666. }
  667. for (i = 0; i < nsyms; i++) {
  668. if (lengths[i] <= pfxbits || (codes[i] & pfxmask) != pfx)
  669. continue;
  670. code = (codes[i] >> pfxbits) & tab->mask;
  671. for (j = code; j <= tab->mask; j += 1 << (lengths[i] - pfxbits)) {
  672. tab->table[j].code = i;
  673. nbits = lengths[i] - pfxbits;
  674. if (tab->table[j].nbits < nbits)
  675. tab->table[j].nbits = nbits;
  676. }
  677. }
  678. for (code = 0; code <= tab->mask; code++) {
  679. if (tab->table[code].nbits <= bits)
  680. continue;
  681. /* Generate a subtable. */
  682. tab->table[code].code = -1;
  683. nbits = tab->table[code].nbits - bits;
  684. if (nbits > 7)
  685. nbits = 7;
  686. tab->table[code].nbits = bits;
  687. tab->table[code].nexttable = zlib_mkonetab(codes, lengths, nsyms,
  688. pfx | (code << pfxbits),
  689. pfxbits + bits, nbits);
  690. }
  691. return tab;
  692. }
  693. /*
  694. * Build a decode table, given a set of Huffman tree lengths.
  695. */
  696. static struct zlib_table *zlib_mktable(unsigned char *lengths,
  697. int nlengths)
  698. {
  699. int count[MAXCODELEN], startcode[MAXCODELEN], codes[MAXSYMS];
  700. int code, maxlen;
  701. int i, j;
  702. /* Count the codes of each length. */
  703. maxlen = 0;
  704. for (i = 1; i < MAXCODELEN; i++)
  705. count[i] = 0;
  706. for (i = 0; i < nlengths; i++) {
  707. count[lengths[i]]++;
  708. if (maxlen < lengths[i])
  709. maxlen = lengths[i];
  710. }
  711. /* Determine the starting code for each length block. */
  712. code = 0;
  713. for (i = 1; i < MAXCODELEN; i++) {
  714. startcode[i] = code;
  715. code += count[i];
  716. code <<= 1;
  717. }
  718. /* Determine the code for each symbol. Mirrored, of course. */
  719. for (i = 0; i < nlengths; i++) {
  720. code = startcode[lengths[i]]++;
  721. codes[i] = 0;
  722. for (j = 0; j < lengths[i]; j++) {
  723. codes[i] = (codes[i] << 1) | (code & 1);
  724. code >>= 1;
  725. }
  726. }
  727. /*
  728. * Now we have the complete list of Huffman codes. Build a
  729. * table.
  730. */
  731. return zlib_mkonetab(codes, lengths, nlengths, 0, 0,
  732. maxlen < 9 ? maxlen : 9);
  733. }
  734. static int zlib_freetable(struct zlib_table **ztab)
  735. {
  736. struct zlib_table *tab;
  737. int code;
  738. if (ztab == NULL)
  739. return -1;
  740. if (*ztab == NULL)
  741. return 0;
  742. tab = *ztab;
  743. for (code = 0; code <= tab->mask; code++)
  744. if (tab->table[code].nexttable != NULL)
  745. zlib_freetable(&tab->table[code].nexttable);
  746. sfree(tab->table);
  747. tab->table = NULL;
  748. sfree(tab);
  749. *ztab = NULL;
  750. return (0);
  751. }
  752. struct zlib_decompress_ctx {
  753. struct zlib_table *staticlentable, *staticdisttable;
  754. struct zlib_table *currlentable, *currdisttable, *lenlentable;
  755. enum {
  756. START, OUTSIDEBLK,
  757. TREES_HDR, TREES_LENLEN, TREES_LEN, TREES_LENREP,
  758. INBLK, GOTLENSYM, GOTLEN, GOTDISTSYM,
  759. UNCOMP_LEN, UNCOMP_NLEN, UNCOMP_DATA
  760. } state;
  761. int sym, hlit, hdist, hclen, lenptr, lenextrabits, lenaddon, len,
  762. lenrep;
  763. int uncomplen;
  764. unsigned char lenlen[19];
  765. /*
  766. * Array that accumulates the code lengths sent in the header of a
  767. * dynamic-Huffman-tree block.
  768. *
  769. * There are 286 actual symbols in the literal/length alphabet
  770. * (256 literals plus 20 length categories), and 30 symbols in the
  771. * distance alphabet. However, the block header transmits the
  772. * number of code lengths for the former alphabet as a 5-bit value
  773. * HLIT to be added to 257, and the latter as a 5-bit value HDIST
  774. * to be added to 1. This means that the number of _code lengths_
  775. * can go as high as 288 for the symbol alphabet and 32 for the
  776. * distance alphabet - each of those values being 2 more than the
  777. * maximum number of actual symbols.
  778. *
  779. * It's tempting to rule that sending out-of-range HLIT or HDIST
  780. * is therefore just illegal, and to fault it when we initially
  781. * receive that header. But instead I've chosen to permit the
  782. * Huffman-code definition to include code length entries for
  783. * those unused symbols; if a header of that form is transmitted,
  784. * then the effect will be that in the main body of the block,
  785. * some bit sequence(s) will generate an illegal symbol number,
  786. * and _that_ will be faulted as a decoding error.
  787. *
  788. * Rationale: this can already happen! The standard Huffman code
  789. * used in a _static_ block for the literal/length alphabet is
  790. * defined in such a way that it includes codes for symbols 287
  791. * and 288, which are then never actually sent in the body of the
  792. * block. And I think that if the standard static tree definition
  793. * is willing to include Huffman codes that don't correspond to a
  794. * symbol, then it's an excessive restriction on dynamic tables
  795. * not to permit them to do the same. In particular, it would be
  796. * strange for a dynamic block not to be able to exactly mimic
  797. * either or both of the Huffman codes used by a static block for
  798. * the corresponding alphabet.
  799. *
  800. * So we place no constraint on HLIT or HDIST during code
  801. * construction, and we make this array large enough to include
  802. * the maximum number of code lengths that can possibly arise as a
  803. * result. It's only trying to _use_ the junk Huffman codes after
  804. * table construction is completed that will provoke a decode
  805. * error.
  806. */
  807. unsigned char lengths[288 + 32];
  808. unsigned long bits;
  809. int nbits;
  810. unsigned char window[WINSIZE];
  811. int winpos;
  812. strbuf *outblk;
  813. ssh_decompressor dc;
  814. };
  815. ssh_decompressor *zlib_decompress_init(void)
  816. {
  817. struct zlib_decompress_ctx *dctx = snew(struct zlib_decompress_ctx);
  818. unsigned char lengths[288];
  819. memset(lengths, 8, 144);
  820. memset(lengths + 144, 9, 256 - 144);
  821. memset(lengths + 256, 7, 280 - 256);
  822. memset(lengths + 280, 8, 288 - 280);
  823. dctx->staticlentable = zlib_mktable(lengths, 288);
  824. memset(lengths, 5, 32);
  825. dctx->staticdisttable = zlib_mktable(lengths, 32);
  826. dctx->state = START; /* even before header */
  827. dctx->currlentable = dctx->currdisttable = dctx->lenlentable = NULL;
  828. dctx->bits = 0;
  829. dctx->nbits = 0;
  830. dctx->winpos = 0;
  831. dctx->outblk = NULL;
  832. dctx->dc.vt = &ssh_zlib;
  833. return &dctx->dc;
  834. }
  835. void zlib_decompress_cleanup(ssh_decompressor *dc)
  836. {
  837. struct zlib_decompress_ctx *dctx =
  838. container_of(dc, struct zlib_decompress_ctx, dc);
  839. if (dctx->currlentable && dctx->currlentable != dctx->staticlentable)
  840. zlib_freetable(&dctx->currlentable);
  841. if (dctx->currdisttable && dctx->currdisttable != dctx->staticdisttable)
  842. zlib_freetable(&dctx->currdisttable);
  843. if (dctx->lenlentable)
  844. zlib_freetable(&dctx->lenlentable);
  845. zlib_freetable(&dctx->staticlentable);
  846. zlib_freetable(&dctx->staticdisttable);
  847. if (dctx->outblk)
  848. strbuf_free(dctx->outblk);
  849. sfree(dctx);
  850. }
  851. static int zlib_huflookup(unsigned long *bitsp, int *nbitsp,
  852. struct zlib_table *tab)
  853. {
  854. unsigned long bits = *bitsp;
  855. int nbits = *nbitsp;
  856. while (1) {
  857. struct zlib_tableentry *ent;
  858. ent = &tab->table[bits & tab->mask];
  859. if (ent->nbits > nbits)
  860. return -1; /* not enough data */
  861. bits >>= ent->nbits;
  862. nbits -= ent->nbits;
  863. if (ent->code == -1)
  864. tab = ent->nexttable;
  865. else {
  866. *bitsp = bits;
  867. *nbitsp = nbits;
  868. return ent->code;
  869. }
  870. if (!tab) {
  871. /*
  872. * There was a missing entry in the table, presumably
  873. * due to an invalid Huffman table description, and the
  874. * subsequent data has attempted to use the missing
  875. * entry. Return a decoding failure.
  876. */
  877. return -2;
  878. }
  879. }
  880. }
  881. static void zlib_emit_char(struct zlib_decompress_ctx *dctx, int c)
  882. {
  883. dctx->window[dctx->winpos] = c;
  884. dctx->winpos = (dctx->winpos + 1) & (WINSIZE - 1);
  885. put_byte(dctx->outblk, c);
  886. }
  887. #define EATBITS(n) ( dctx->nbits -= (n), dctx->bits >>= (n) )
  888. bool zlib_decompress_block(ssh_decompressor *dc,
  889. const unsigned char *block, int len,
  890. unsigned char **outblock, int *outlen)
  891. {
  892. struct zlib_decompress_ctx *dctx =
  893. container_of(dc, struct zlib_decompress_ctx, dc);
  894. const coderecord *rec;
  895. int code, blktype, rep, dist, nlen, header;
  896. static const unsigned char lenlenmap[] = {
  897. 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
  898. };
  899. assert(!dctx->outblk);
  900. dctx->outblk = strbuf_new_nm();
  901. while (len > 0 || dctx->nbits > 0) {
  902. while (dctx->nbits < 24 && len > 0) {
  903. dctx->bits |= (*block++) << dctx->nbits;
  904. dctx->nbits += 8;
  905. len--;
  906. }
  907. switch (dctx->state) {
  908. case START:
  909. /* Expect 16-bit zlib header. */
  910. if (dctx->nbits < 16)
  911. goto finished; /* done all we can */
  912. /*
  913. * The header is stored as a big-endian 16-bit integer,
  914. * in contrast to the general little-endian policy in
  915. * the rest of the format :-(
  916. */
  917. header = (((dctx->bits & 0xFF00) >> 8) |
  918. ((dctx->bits & 0x00FF) << 8));
  919. EATBITS(16);
  920. /*
  921. * Check the header:
  922. *
  923. * - bits 8-11 should be 1000 (Deflate/RFC1951)
  924. * - bits 12-15 should be at most 0111 (window size)
  925. * - bit 5 should be zero (no dictionary present)
  926. * - we don't care about bits 6-7 (compression rate)
  927. * - bits 0-4 should be set up to make the whole thing
  928. * a multiple of 31 (checksum).
  929. */
  930. if ((header & 0x0F00) != 0x0800 ||
  931. (header & 0xF000) > 0x7000 ||
  932. (header & 0x0020) != 0x0000 ||
  933. (header % 31) != 0)
  934. goto decode_error;
  935. dctx->state = OUTSIDEBLK;
  936. break;
  937. case OUTSIDEBLK:
  938. /* Expect 3-bit block header. */
  939. if (dctx->nbits < 3)
  940. goto finished; /* done all we can */
  941. EATBITS(1);
  942. blktype = dctx->bits & 3;
  943. EATBITS(2);
  944. if (blktype == 0) {
  945. int to_eat = dctx->nbits & 7;
  946. dctx->state = UNCOMP_LEN;
  947. EATBITS(to_eat); /* align to byte boundary */
  948. } else if (blktype == 1) {
  949. dctx->currlentable = dctx->staticlentable;
  950. dctx->currdisttable = dctx->staticdisttable;
  951. dctx->state = INBLK;
  952. } else if (blktype == 2) {
  953. dctx->state = TREES_HDR;
  954. }
  955. break;
  956. case TREES_HDR:
  957. /*
  958. * Dynamic block header. Five bits of HLIT, five of
  959. * HDIST, four of HCLEN.
  960. */
  961. if (dctx->nbits < 5 + 5 + 4)
  962. goto finished; /* done all we can */
  963. dctx->hlit = 257 + (dctx->bits & 31);
  964. EATBITS(5);
  965. dctx->hdist = 1 + (dctx->bits & 31);
  966. EATBITS(5);
  967. dctx->hclen = 4 + (dctx->bits & 15);
  968. EATBITS(4);
  969. dctx->lenptr = 0;
  970. dctx->state = TREES_LENLEN;
  971. memset(dctx->lenlen, 0, sizeof(dctx->lenlen));
  972. break;
  973. case TREES_LENLEN:
  974. if (dctx->nbits < 3)
  975. goto finished;
  976. while (dctx->lenptr < dctx->hclen && dctx->nbits >= 3) {
  977. dctx->lenlen[lenlenmap[dctx->lenptr++]] =
  978. (unsigned char) (dctx->bits & 7);
  979. EATBITS(3);
  980. }
  981. if (dctx->lenptr == dctx->hclen) {
  982. dctx->lenlentable = zlib_mktable(dctx->lenlen, 19);
  983. dctx->state = TREES_LEN;
  984. dctx->lenptr = 0;
  985. }
  986. break;
  987. case TREES_LEN:
  988. if (dctx->lenptr >= dctx->hlit + dctx->hdist) {
  989. dctx->currlentable = zlib_mktable(dctx->lengths, dctx->hlit);
  990. dctx->currdisttable = zlib_mktable(dctx->lengths + dctx->hlit,
  991. dctx->hdist);
  992. zlib_freetable(&dctx->lenlentable);
  993. dctx->lenlentable = NULL;
  994. dctx->state = INBLK;
  995. break;
  996. }
  997. code =
  998. zlib_huflookup(&dctx->bits, &dctx->nbits, dctx->lenlentable);
  999. if (code == -1)
  1000. goto finished;
  1001. if (code == -2)
  1002. goto decode_error;
  1003. if (code < 16)
  1004. dctx->lengths[dctx->lenptr++] = code;
  1005. else {
  1006. dctx->lenextrabits = (code == 16 ? 2 : code == 17 ? 3 : 7);
  1007. dctx->lenaddon = (code == 18 ? 11 : 3);
  1008. dctx->lenrep = (code == 16 && dctx->lenptr > 0 ?
  1009. dctx->lengths[dctx->lenptr - 1] : 0);
  1010. dctx->state = TREES_LENREP;
  1011. }
  1012. break;
  1013. case TREES_LENREP:
  1014. if (dctx->nbits < dctx->lenextrabits)
  1015. goto finished;
  1016. rep =
  1017. dctx->lenaddon +
  1018. (dctx->bits & ((1 << dctx->lenextrabits) - 1));
  1019. EATBITS(dctx->lenextrabits);
  1020. while (rep > 0 && dctx->lenptr < dctx->hlit + dctx->hdist) {
  1021. dctx->lengths[dctx->lenptr] = dctx->lenrep;
  1022. dctx->lenptr++;
  1023. rep--;
  1024. }
  1025. dctx->state = TREES_LEN;
  1026. break;
  1027. case INBLK:
  1028. code =
  1029. zlib_huflookup(&dctx->bits, &dctx->nbits, dctx->currlentable);
  1030. if (code == -1)
  1031. goto finished;
  1032. if (code == -2)
  1033. goto decode_error;
  1034. if (code < 256)
  1035. zlib_emit_char(dctx, code);
  1036. else if (code == 256) {
  1037. dctx->state = OUTSIDEBLK;
  1038. if (dctx->currlentable != dctx->staticlentable) {
  1039. zlib_freetable(&dctx->currlentable);
  1040. dctx->currlentable = NULL;
  1041. }
  1042. if (dctx->currdisttable != dctx->staticdisttable) {
  1043. zlib_freetable(&dctx->currdisttable);
  1044. dctx->currdisttable = NULL;
  1045. }
  1046. } else if (code < 286) {
  1047. dctx->state = GOTLENSYM;
  1048. dctx->sym = code;
  1049. } else {
  1050. /* literal/length symbols 286 and 287 are invalid */
  1051. goto decode_error;
  1052. }
  1053. break;
  1054. case GOTLENSYM:
  1055. rec = &lencodes[dctx->sym - 257];
  1056. if (dctx->nbits < rec->extrabits)
  1057. goto finished;
  1058. dctx->len =
  1059. rec->min + (dctx->bits & ((1 << rec->extrabits) - 1));
  1060. EATBITS(rec->extrabits);
  1061. dctx->state = GOTLEN;
  1062. break;
  1063. case GOTLEN:
  1064. code =
  1065. zlib_huflookup(&dctx->bits, &dctx->nbits,
  1066. dctx->currdisttable);
  1067. if (code == -1)
  1068. goto finished;
  1069. if (code == -2)
  1070. goto decode_error;
  1071. if (code >= 30) /* dist symbols 30 and 31 are invalid */
  1072. goto decode_error;
  1073. dctx->state = GOTDISTSYM;
  1074. dctx->sym = code;
  1075. break;
  1076. case GOTDISTSYM:
  1077. rec = &distcodes[dctx->sym];
  1078. if (dctx->nbits < rec->extrabits)
  1079. goto finished;
  1080. dist = rec->min + (dctx->bits & ((1 << rec->extrabits) - 1));
  1081. EATBITS(rec->extrabits);
  1082. dctx->state = INBLK;
  1083. while (dctx->len--)
  1084. zlib_emit_char(dctx, dctx->window[(dctx->winpos - dist) &
  1085. (WINSIZE - 1)]);
  1086. break;
  1087. case UNCOMP_LEN:
  1088. /*
  1089. * Uncompressed block. We expect to see a 16-bit LEN.
  1090. */
  1091. if (dctx->nbits < 16)
  1092. goto finished;
  1093. dctx->uncomplen = dctx->bits & 0xFFFF;
  1094. EATBITS(16);
  1095. dctx->state = UNCOMP_NLEN;
  1096. break;
  1097. case UNCOMP_NLEN:
  1098. /*
  1099. * Uncompressed block. We expect to see a 16-bit NLEN,
  1100. * which should be the one's complement of the previous
  1101. * LEN.
  1102. */
  1103. if (dctx->nbits < 16)
  1104. goto finished;
  1105. nlen = dctx->bits & 0xFFFF;
  1106. EATBITS(16);
  1107. if (dctx->uncomplen != (nlen ^ 0xFFFF))
  1108. goto decode_error;
  1109. if (dctx->uncomplen == 0)
  1110. dctx->state = OUTSIDEBLK; /* block is empty */
  1111. else
  1112. dctx->state = UNCOMP_DATA;
  1113. break;
  1114. case UNCOMP_DATA:
  1115. if (dctx->nbits < 8)
  1116. goto finished;
  1117. zlib_emit_char(dctx, dctx->bits & 0xFF);
  1118. EATBITS(8);
  1119. if (--dctx->uncomplen == 0)
  1120. dctx->state = OUTSIDEBLK; /* end of uncompressed block */
  1121. break;
  1122. }
  1123. }
  1124. finished:
  1125. *outlen = dctx->outblk->len;
  1126. *outblock = (unsigned char *)strbuf_to_str(dctx->outblk);
  1127. dctx->outblk = NULL;
  1128. return true;
  1129. decode_error:
  1130. *outblock = NULL;
  1131. *outlen = 0;
  1132. return false;
  1133. }
  1134. const ssh_compression_alg ssh_zlib = {
  1135. "zlib",
  1136. "[email protected]", /* delayed version */
  1137. zlib_compress_init,
  1138. zlib_compress_cleanup,
  1139. zlib_compress_block,
  1140. zlib_decompress_init,
  1141. zlib_decompress_cleanup,
  1142. zlib_decompress_block,
  1143. "zlib (RFC1950)"
  1144. };