sshzlib.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206
  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. unsigned char lengths[286 + 32];
  766. unsigned long bits;
  767. int nbits;
  768. unsigned char window[WINSIZE];
  769. int winpos;
  770. strbuf *outblk;
  771. ssh_decompressor dc;
  772. };
  773. ssh_decompressor *zlib_decompress_init(void)
  774. {
  775. struct zlib_decompress_ctx *dctx = snew(struct zlib_decompress_ctx);
  776. unsigned char lengths[288];
  777. memset(lengths, 8, 144);
  778. memset(lengths + 144, 9, 256 - 144);
  779. memset(lengths + 256, 7, 280 - 256);
  780. memset(lengths + 280, 8, 288 - 280);
  781. dctx->staticlentable = zlib_mktable(lengths, 288);
  782. memset(lengths, 5, 32);
  783. dctx->staticdisttable = zlib_mktable(lengths, 32);
  784. dctx->state = START; /* even before header */
  785. dctx->currlentable = dctx->currdisttable = dctx->lenlentable = NULL;
  786. dctx->bits = 0;
  787. dctx->nbits = 0;
  788. dctx->winpos = 0;
  789. dctx->outblk = NULL;
  790. dctx->dc.vt = &ssh_zlib;
  791. return &dctx->dc;
  792. }
  793. void zlib_decompress_cleanup(ssh_decompressor *dc)
  794. {
  795. struct zlib_decompress_ctx *dctx =
  796. container_of(dc, struct zlib_decompress_ctx, dc);
  797. if (dctx->currlentable && dctx->currlentable != dctx->staticlentable)
  798. zlib_freetable(&dctx->currlentable);
  799. if (dctx->currdisttable && dctx->currdisttable != dctx->staticdisttable)
  800. zlib_freetable(&dctx->currdisttable);
  801. if (dctx->lenlentable)
  802. zlib_freetable(&dctx->lenlentable);
  803. zlib_freetable(&dctx->staticlentable);
  804. zlib_freetable(&dctx->staticdisttable);
  805. if (dctx->outblk)
  806. strbuf_free(dctx->outblk);
  807. sfree(dctx);
  808. }
  809. static int zlib_huflookup(unsigned long *bitsp, int *nbitsp,
  810. struct zlib_table *tab)
  811. {
  812. unsigned long bits = *bitsp;
  813. int nbits = *nbitsp;
  814. while (1) {
  815. struct zlib_tableentry *ent;
  816. ent = &tab->table[bits & tab->mask];
  817. if (ent->nbits > nbits)
  818. return -1; /* not enough data */
  819. bits >>= ent->nbits;
  820. nbits -= ent->nbits;
  821. if (ent->code == -1)
  822. tab = ent->nexttable;
  823. else {
  824. *bitsp = bits;
  825. *nbitsp = nbits;
  826. return ent->code;
  827. }
  828. if (!tab) {
  829. /*
  830. * There was a missing entry in the table, presumably
  831. * due to an invalid Huffman table description, and the
  832. * subsequent data has attempted to use the missing
  833. * entry. Return a decoding failure.
  834. */
  835. return -2;
  836. }
  837. }
  838. }
  839. static void zlib_emit_char(struct zlib_decompress_ctx *dctx, int c)
  840. {
  841. dctx->window[dctx->winpos] = c;
  842. dctx->winpos = (dctx->winpos + 1) & (WINSIZE - 1);
  843. put_byte(dctx->outblk, c);
  844. }
  845. #define EATBITS(n) ( dctx->nbits -= (n), dctx->bits >>= (n) )
  846. bool zlib_decompress_block(ssh_decompressor *dc,
  847. const unsigned char *block, int len,
  848. unsigned char **outblock, int *outlen)
  849. {
  850. struct zlib_decompress_ctx *dctx =
  851. container_of(dc, struct zlib_decompress_ctx, dc);
  852. const coderecord *rec;
  853. int code, blktype, rep, dist, nlen, header;
  854. static const unsigned char lenlenmap[] = {
  855. 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
  856. };
  857. assert(!dctx->outblk);
  858. dctx->outblk = strbuf_new_nm();
  859. while (len > 0 || dctx->nbits > 0) {
  860. while (dctx->nbits < 24 && len > 0) {
  861. dctx->bits |= (*block++) << dctx->nbits;
  862. dctx->nbits += 8;
  863. len--;
  864. }
  865. switch (dctx->state) {
  866. case START:
  867. /* Expect 16-bit zlib header. */
  868. if (dctx->nbits < 16)
  869. goto finished; /* done all we can */
  870. /*
  871. * The header is stored as a big-endian 16-bit integer,
  872. * in contrast to the general little-endian policy in
  873. * the rest of the format :-(
  874. */
  875. header = (((dctx->bits & 0xFF00) >> 8) |
  876. ((dctx->bits & 0x00FF) << 8));
  877. EATBITS(16);
  878. /*
  879. * Check the header:
  880. *
  881. * - bits 8-11 should be 1000 (Deflate/RFC1951)
  882. * - bits 12-15 should be at most 0111 (window size)
  883. * - bit 5 should be zero (no dictionary present)
  884. * - we don't care about bits 6-7 (compression rate)
  885. * - bits 0-4 should be set up to make the whole thing
  886. * a multiple of 31 (checksum).
  887. */
  888. if ((header & 0x0F00) != 0x0800 ||
  889. (header & 0xF000) > 0x7000 ||
  890. (header & 0x0020) != 0x0000 ||
  891. (header % 31) != 0)
  892. goto decode_error;
  893. dctx->state = OUTSIDEBLK;
  894. break;
  895. case OUTSIDEBLK:
  896. /* Expect 3-bit block header. */
  897. if (dctx->nbits < 3)
  898. goto finished; /* done all we can */
  899. EATBITS(1);
  900. blktype = dctx->bits & 3;
  901. EATBITS(2);
  902. if (blktype == 0) {
  903. int to_eat = dctx->nbits & 7;
  904. dctx->state = UNCOMP_LEN;
  905. EATBITS(to_eat); /* align to byte boundary */
  906. } else if (blktype == 1) {
  907. dctx->currlentable = dctx->staticlentable;
  908. dctx->currdisttable = dctx->staticdisttable;
  909. dctx->state = INBLK;
  910. } else if (blktype == 2) {
  911. dctx->state = TREES_HDR;
  912. }
  913. break;
  914. case TREES_HDR:
  915. /*
  916. * Dynamic block header. Five bits of HLIT, five of
  917. * HDIST, four of HCLEN.
  918. */
  919. if (dctx->nbits < 5 + 5 + 4)
  920. goto finished; /* done all we can */
  921. dctx->hlit = 257 + (dctx->bits & 31);
  922. EATBITS(5);
  923. dctx->hdist = 1 + (dctx->bits & 31);
  924. EATBITS(5);
  925. dctx->hclen = 4 + (dctx->bits & 15);
  926. EATBITS(4);
  927. dctx->lenptr = 0;
  928. dctx->state = TREES_LENLEN;
  929. memset(dctx->lenlen, 0, sizeof(dctx->lenlen));
  930. break;
  931. case TREES_LENLEN:
  932. if (dctx->nbits < 3)
  933. goto finished;
  934. while (dctx->lenptr < dctx->hclen && dctx->nbits >= 3) {
  935. dctx->lenlen[lenlenmap[dctx->lenptr++]] =
  936. (unsigned char) (dctx->bits & 7);
  937. EATBITS(3);
  938. }
  939. if (dctx->lenptr == dctx->hclen) {
  940. dctx->lenlentable = zlib_mktable(dctx->lenlen, 19);
  941. dctx->state = TREES_LEN;
  942. dctx->lenptr = 0;
  943. }
  944. break;
  945. case TREES_LEN:
  946. if (dctx->lenptr >= dctx->hlit + dctx->hdist) {
  947. dctx->currlentable = zlib_mktable(dctx->lengths, dctx->hlit);
  948. dctx->currdisttable = zlib_mktable(dctx->lengths + dctx->hlit,
  949. dctx->hdist);
  950. zlib_freetable(&dctx->lenlentable);
  951. dctx->lenlentable = NULL;
  952. dctx->state = INBLK;
  953. break;
  954. }
  955. code =
  956. zlib_huflookup(&dctx->bits, &dctx->nbits, dctx->lenlentable);
  957. if (code == -1)
  958. goto finished;
  959. if (code == -2)
  960. goto decode_error;
  961. if (code < 16)
  962. dctx->lengths[dctx->lenptr++] = code;
  963. else {
  964. dctx->lenextrabits = (code == 16 ? 2 : code == 17 ? 3 : 7);
  965. dctx->lenaddon = (code == 18 ? 11 : 3);
  966. dctx->lenrep = (code == 16 && dctx->lenptr > 0 ?
  967. dctx->lengths[dctx->lenptr - 1] : 0);
  968. dctx->state = TREES_LENREP;
  969. }
  970. break;
  971. case TREES_LENREP:
  972. if (dctx->nbits < dctx->lenextrabits)
  973. goto finished;
  974. rep =
  975. dctx->lenaddon +
  976. (dctx->bits & ((1 << dctx->lenextrabits) - 1));
  977. EATBITS(dctx->lenextrabits);
  978. while (rep > 0 && dctx->lenptr < dctx->hlit + dctx->hdist) {
  979. dctx->lengths[dctx->lenptr] = dctx->lenrep;
  980. dctx->lenptr++;
  981. rep--;
  982. }
  983. dctx->state = TREES_LEN;
  984. break;
  985. case INBLK:
  986. code =
  987. zlib_huflookup(&dctx->bits, &dctx->nbits, dctx->currlentable);
  988. if (code == -1)
  989. goto finished;
  990. if (code == -2)
  991. goto decode_error;
  992. if (code < 256)
  993. zlib_emit_char(dctx, code);
  994. else if (code == 256) {
  995. dctx->state = OUTSIDEBLK;
  996. if (dctx->currlentable != dctx->staticlentable) {
  997. zlib_freetable(&dctx->currlentable);
  998. dctx->currlentable = NULL;
  999. }
  1000. if (dctx->currdisttable != dctx->staticdisttable) {
  1001. zlib_freetable(&dctx->currdisttable);
  1002. dctx->currdisttable = NULL;
  1003. }
  1004. } else if (code < 286) { /* static tree can give >285; ignore */
  1005. dctx->state = GOTLENSYM;
  1006. dctx->sym = code;
  1007. }
  1008. break;
  1009. case GOTLENSYM:
  1010. rec = &lencodes[dctx->sym - 257];
  1011. if (dctx->nbits < rec->extrabits)
  1012. goto finished;
  1013. dctx->len =
  1014. rec->min + (dctx->bits & ((1 << rec->extrabits) - 1));
  1015. EATBITS(rec->extrabits);
  1016. dctx->state = GOTLEN;
  1017. break;
  1018. case GOTLEN:
  1019. code =
  1020. zlib_huflookup(&dctx->bits, &dctx->nbits,
  1021. dctx->currdisttable);
  1022. if (code == -1)
  1023. goto finished;
  1024. if (code == -2)
  1025. goto decode_error;
  1026. if (code >= 30) /* dist symbols 30 and 31 are invalid */
  1027. goto decode_error;
  1028. dctx->state = GOTDISTSYM;
  1029. dctx->sym = code;
  1030. break;
  1031. case GOTDISTSYM:
  1032. rec = &distcodes[dctx->sym];
  1033. if (dctx->nbits < rec->extrabits)
  1034. goto finished;
  1035. dist = rec->min + (dctx->bits & ((1 << rec->extrabits) - 1));
  1036. EATBITS(rec->extrabits);
  1037. dctx->state = INBLK;
  1038. while (dctx->len--)
  1039. zlib_emit_char(dctx, dctx->window[(dctx->winpos - dist) &
  1040. (WINSIZE - 1)]);
  1041. break;
  1042. case UNCOMP_LEN:
  1043. /*
  1044. * Uncompressed block. We expect to see a 16-bit LEN.
  1045. */
  1046. if (dctx->nbits < 16)
  1047. goto finished;
  1048. dctx->uncomplen = dctx->bits & 0xFFFF;
  1049. EATBITS(16);
  1050. dctx->state = UNCOMP_NLEN;
  1051. break;
  1052. case UNCOMP_NLEN:
  1053. /*
  1054. * Uncompressed block. We expect to see a 16-bit NLEN,
  1055. * which should be the one's complement of the previous
  1056. * LEN.
  1057. */
  1058. if (dctx->nbits < 16)
  1059. goto finished;
  1060. nlen = dctx->bits & 0xFFFF;
  1061. EATBITS(16);
  1062. if (dctx->uncomplen != (nlen ^ 0xFFFF))
  1063. goto decode_error;
  1064. if (dctx->uncomplen == 0)
  1065. dctx->state = OUTSIDEBLK; /* block is empty */
  1066. else
  1067. dctx->state = UNCOMP_DATA;
  1068. break;
  1069. case UNCOMP_DATA:
  1070. if (dctx->nbits < 8)
  1071. goto finished;
  1072. zlib_emit_char(dctx, dctx->bits & 0xFF);
  1073. EATBITS(8);
  1074. if (--dctx->uncomplen == 0)
  1075. dctx->state = OUTSIDEBLK; /* end of uncompressed block */
  1076. break;
  1077. }
  1078. }
  1079. finished:
  1080. *outlen = dctx->outblk->len;
  1081. *outblock = (unsigned char *)strbuf_to_str(dctx->outblk);
  1082. dctx->outblk = NULL;
  1083. return true;
  1084. decode_error:
  1085. *outblock = NULL;
  1086. *outlen = 0;
  1087. return false;
  1088. }
  1089. const ssh_compression_alg ssh_zlib = {
  1090. "zlib",
  1091. "[email protected]", /* delayed version */
  1092. zlib_compress_init,
  1093. zlib_compress_cleanup,
  1094. zlib_compress_block,
  1095. zlib_decompress_init,
  1096. zlib_decompress_cleanup,
  1097. zlib_decompress_block,
  1098. "zlib (RFC1950)"
  1099. };