sshzlib.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209
  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. unsigned char *outbuf;
  311. int outlen, outsize;
  312. unsigned long outbits;
  313. int noutbits;
  314. bool firstblock;
  315. };
  316. static void outbits(struct Outbuf *out, unsigned long bits, int nbits)
  317. {
  318. assert(out->noutbits + nbits <= 32);
  319. out->outbits |= bits << out->noutbits;
  320. out->noutbits += nbits;
  321. while (out->noutbits >= 8) {
  322. if (out->outlen >= out->outsize) {
  323. out->outsize = out->outlen + 64;
  324. out->outbuf = sresize(out->outbuf, out->outsize, unsigned char);
  325. }
  326. out->outbuf[out->outlen++] = (unsigned char) (out->outbits & 0xFF);
  327. out->outbits >>= 8;
  328. out->noutbits -= 8;
  329. }
  330. }
  331. static const unsigned char mirrorbytes[256] = {
  332. 0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0,
  333. 0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0,
  334. 0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8,
  335. 0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8,
  336. 0x04, 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, 0xe4,
  337. 0x14, 0x94, 0x54, 0xd4, 0x34, 0xb4, 0x74, 0xf4,
  338. 0x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c, 0xec,
  339. 0x1c, 0x9c, 0x5c, 0xdc, 0x3c, 0xbc, 0x7c, 0xfc,
  340. 0x02, 0x82, 0x42, 0xc2, 0x22, 0xa2, 0x62, 0xe2,
  341. 0x12, 0x92, 0x52, 0xd2, 0x32, 0xb2, 0x72, 0xf2,
  342. 0x0a, 0x8a, 0x4a, 0xca, 0x2a, 0xaa, 0x6a, 0xea,
  343. 0x1a, 0x9a, 0x5a, 0xda, 0x3a, 0xba, 0x7a, 0xfa,
  344. 0x06, 0x86, 0x46, 0xc6, 0x26, 0xa6, 0x66, 0xe6,
  345. 0x16, 0x96, 0x56, 0xd6, 0x36, 0xb6, 0x76, 0xf6,
  346. 0x0e, 0x8e, 0x4e, 0xce, 0x2e, 0xae, 0x6e, 0xee,
  347. 0x1e, 0x9e, 0x5e, 0xde, 0x3e, 0xbe, 0x7e, 0xfe,
  348. 0x01, 0x81, 0x41, 0xc1, 0x21, 0xa1, 0x61, 0xe1,
  349. 0x11, 0x91, 0x51, 0xd1, 0x31, 0xb1, 0x71, 0xf1,
  350. 0x09, 0x89, 0x49, 0xc9, 0x29, 0xa9, 0x69, 0xe9,
  351. 0x19, 0x99, 0x59, 0xd9, 0x39, 0xb9, 0x79, 0xf9,
  352. 0x05, 0x85, 0x45, 0xc5, 0x25, 0xa5, 0x65, 0xe5,
  353. 0x15, 0x95, 0x55, 0xd5, 0x35, 0xb5, 0x75, 0xf5,
  354. 0x0d, 0x8d, 0x4d, 0xcd, 0x2d, 0xad, 0x6d, 0xed,
  355. 0x1d, 0x9d, 0x5d, 0xdd, 0x3d, 0xbd, 0x7d, 0xfd,
  356. 0x03, 0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0xe3,
  357. 0x13, 0x93, 0x53, 0xd3, 0x33, 0xb3, 0x73, 0xf3,
  358. 0x0b, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb,
  359. 0x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, 0x7b, 0xfb,
  360. 0x07, 0x87, 0x47, 0xc7, 0x27, 0xa7, 0x67, 0xe7,
  361. 0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7,
  362. 0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef,
  363. 0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff,
  364. };
  365. typedef struct {
  366. short code, extrabits;
  367. int min, max;
  368. } coderecord;
  369. static const coderecord lencodes[] = {
  370. {257, 0, 3, 3},
  371. {258, 0, 4, 4},
  372. {259, 0, 5, 5},
  373. {260, 0, 6, 6},
  374. {261, 0, 7, 7},
  375. {262, 0, 8, 8},
  376. {263, 0, 9, 9},
  377. {264, 0, 10, 10},
  378. {265, 1, 11, 12},
  379. {266, 1, 13, 14},
  380. {267, 1, 15, 16},
  381. {268, 1, 17, 18},
  382. {269, 2, 19, 22},
  383. {270, 2, 23, 26},
  384. {271, 2, 27, 30},
  385. {272, 2, 31, 34},
  386. {273, 3, 35, 42},
  387. {274, 3, 43, 50},
  388. {275, 3, 51, 58},
  389. {276, 3, 59, 66},
  390. {277, 4, 67, 82},
  391. {278, 4, 83, 98},
  392. {279, 4, 99, 114},
  393. {280, 4, 115, 130},
  394. {281, 5, 131, 162},
  395. {282, 5, 163, 194},
  396. {283, 5, 195, 226},
  397. {284, 5, 227, 257},
  398. {285, 0, 258, 258},
  399. };
  400. static const coderecord distcodes[] = {
  401. {0, 0, 1, 1},
  402. {1, 0, 2, 2},
  403. {2, 0, 3, 3},
  404. {3, 0, 4, 4},
  405. {4, 1, 5, 6},
  406. {5, 1, 7, 8},
  407. {6, 2, 9, 12},
  408. {7, 2, 13, 16},
  409. {8, 3, 17, 24},
  410. {9, 3, 25, 32},
  411. {10, 4, 33, 48},
  412. {11, 4, 49, 64},
  413. {12, 5, 65, 96},
  414. {13, 5, 97, 128},
  415. {14, 6, 129, 192},
  416. {15, 6, 193, 256},
  417. {16, 7, 257, 384},
  418. {17, 7, 385, 512},
  419. {18, 8, 513, 768},
  420. {19, 8, 769, 1024},
  421. {20, 9, 1025, 1536},
  422. {21, 9, 1537, 2048},
  423. {22, 10, 2049, 3072},
  424. {23, 10, 3073, 4096},
  425. {24, 11, 4097, 6144},
  426. {25, 11, 6145, 8192},
  427. {26, 12, 8193, 12288},
  428. {27, 12, 12289, 16384},
  429. {28, 13, 16385, 24576},
  430. {29, 13, 24577, 32768},
  431. };
  432. static void zlib_literal(struct LZ77Context *ectx, unsigned char c)
  433. {
  434. struct Outbuf *out = (struct Outbuf *) ectx->userdata;
  435. if (c <= 143) {
  436. /* 0 through 143 are 8 bits long starting at 00110000. */
  437. outbits(out, mirrorbytes[0x30 + c], 8);
  438. } else {
  439. /* 144 through 255 are 9 bits long starting at 110010000. */
  440. outbits(out, 1 + 2 * mirrorbytes[0x90 - 144 + c], 9);
  441. }
  442. }
  443. static void zlib_match(struct LZ77Context *ectx, int distance, int len)
  444. {
  445. const coderecord *d, *l;
  446. int i, j, k;
  447. struct Outbuf *out = (struct Outbuf *) ectx->userdata;
  448. while (len > 0) {
  449. int thislen;
  450. /*
  451. * We can transmit matches of lengths 3 through 258
  452. * inclusive. So if len exceeds 258, we must transmit in
  453. * several steps, with 258 or less in each step.
  454. *
  455. * Specifically: if len >= 261, we can transmit 258 and be
  456. * sure of having at least 3 left for the next step. And if
  457. * len <= 258, we can just transmit len. But if len == 259
  458. * or 260, we must transmit len-3.
  459. */
  460. thislen = (len > 260 ? 258 : len <= 258 ? len : len - 3);
  461. len -= thislen;
  462. /*
  463. * Binary-search to find which length code we're
  464. * transmitting.
  465. */
  466. i = -1;
  467. j = sizeof(lencodes) / sizeof(*lencodes);
  468. while (1) {
  469. assert(j - i >= 2);
  470. k = (j + i) / 2;
  471. if (thislen < lencodes[k].min)
  472. j = k;
  473. else if (thislen > lencodes[k].max)
  474. i = k;
  475. else {
  476. l = &lencodes[k];
  477. break; /* found it! */
  478. }
  479. }
  480. /*
  481. * Transmit the length code. 256-279 are seven bits
  482. * starting at 0000000; 280-287 are eight bits starting at
  483. * 11000000.
  484. */
  485. if (l->code <= 279) {
  486. outbits(out, mirrorbytes[(l->code - 256) * 2], 7);
  487. } else {
  488. outbits(out, mirrorbytes[0xc0 - 280 + l->code], 8);
  489. }
  490. /*
  491. * Transmit the extra bits.
  492. */
  493. if (l->extrabits)
  494. outbits(out, thislen - l->min, l->extrabits);
  495. /*
  496. * Binary-search to find which distance code we're
  497. * transmitting.
  498. */
  499. i = -1;
  500. j = sizeof(distcodes) / sizeof(*distcodes);
  501. while (1) {
  502. assert(j - i >= 2);
  503. k = (j + i) / 2;
  504. if (distance < distcodes[k].min)
  505. j = k;
  506. else if (distance > distcodes[k].max)
  507. i = k;
  508. else {
  509. d = &distcodes[k];
  510. break; /* found it! */
  511. }
  512. }
  513. /*
  514. * Transmit the distance code. Five bits starting at 00000.
  515. */
  516. outbits(out, mirrorbytes[d->code * 8], 5);
  517. /*
  518. * Transmit the extra bits.
  519. */
  520. if (d->extrabits)
  521. outbits(out, distance - d->min, d->extrabits);
  522. }
  523. }
  524. struct ssh_zlib_compressor {
  525. struct LZ77Context ectx;
  526. ssh_compressor sc;
  527. };
  528. ssh_compressor *zlib_compress_init(void)
  529. {
  530. struct Outbuf *out;
  531. struct ssh_zlib_compressor *comp = snew(struct ssh_zlib_compressor);
  532. lz77_init(&comp->ectx);
  533. comp->sc.vt = &ssh_zlib;
  534. comp->ectx.literal = zlib_literal;
  535. comp->ectx.match = zlib_match;
  536. out = snew(struct Outbuf);
  537. out->outbits = out->noutbits = 0;
  538. out->firstblock = true;
  539. comp->ectx.userdata = out;
  540. return &comp->sc;
  541. }
  542. void zlib_compress_cleanup(ssh_compressor *sc)
  543. {
  544. struct ssh_zlib_compressor *comp =
  545. container_of(sc, struct ssh_zlib_compressor, sc);
  546. sfree(comp->ectx.userdata);
  547. sfree(comp->ectx.ictx);
  548. sfree(comp);
  549. }
  550. void zlib_compress_block(ssh_compressor *sc,
  551. const unsigned char *block, int len,
  552. unsigned char **outblock, int *outlen,
  553. int minlen)
  554. {
  555. struct ssh_zlib_compressor *comp =
  556. container_of(sc, struct ssh_zlib_compressor, sc);
  557. struct Outbuf *out = (struct Outbuf *) comp->ectx.userdata;
  558. bool in_block;
  559. out->outbuf = NULL;
  560. out->outlen = out->outsize = 0;
  561. /*
  562. * If this is the first block, output the Zlib (RFC1950) header
  563. * bytes 78 9C. (Deflate compression, 32K window size, default
  564. * algorithm.)
  565. */
  566. if (out->firstblock) {
  567. outbits(out, 0x9C78, 16);
  568. out->firstblock = false;
  569. in_block = false;
  570. } else
  571. in_block = true;
  572. if (!in_block) {
  573. /*
  574. * Start a Deflate (RFC1951) fixed-trees block. We
  575. * transmit a zero bit (BFINAL=0), followed by a zero
  576. * bit and a one bit (BTYPE=01). Of course these are in
  577. * the wrong order (01 0).
  578. */
  579. outbits(out, 2, 3);
  580. }
  581. /*
  582. * Do the compression.
  583. */
  584. lz77_compress(&comp->ectx, block, len);
  585. /*
  586. * End the block (by transmitting code 256, which is
  587. * 0000000 in fixed-tree mode), and transmit some empty
  588. * blocks to ensure we have emitted the byte containing the
  589. * last piece of genuine data. There are three ways we can
  590. * do this:
  591. *
  592. * - Minimal flush. Output end-of-block and then open a
  593. * new static block. This takes 9 bits, which is
  594. * guaranteed to flush out the last genuine code in the
  595. * closed block; but allegedly zlib can't handle it.
  596. *
  597. * - Zlib partial flush. Output EOB, open and close an
  598. * empty static block, and _then_ open the new block.
  599. * This is the best zlib can handle.
  600. *
  601. * - Zlib sync flush. Output EOB, then an empty
  602. * _uncompressed_ block (000, then sync to byte
  603. * boundary, then send bytes 00 00 FF FF). Then open the
  604. * new block.
  605. *
  606. * For the moment, we will use Zlib partial flush.
  607. */
  608. outbits(out, 0, 7); /* close block */
  609. outbits(out, 2, 3 + 7); /* empty static block */
  610. outbits(out, 2, 3); /* open new block */
  611. /*
  612. * If we've been asked to pad out the compressed data until it's
  613. * at least a given length, do so by emitting further empty static
  614. * blocks.
  615. */
  616. while (out->outlen < minlen) {
  617. outbits(out, 0, 7); /* close block */
  618. outbits(out, 2, 3); /* open new static block */
  619. }
  620. *outblock = out->outbuf;
  621. *outlen = out->outlen;
  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. unsigned char *outblk;
  771. int outlen, outsize;
  772. ssh_decompressor dc;
  773. };
  774. ssh_decompressor *zlib_decompress_init(void)
  775. {
  776. struct zlib_decompress_ctx *dctx = snew(struct zlib_decompress_ctx);
  777. unsigned char lengths[288];
  778. memset(lengths, 8, 144);
  779. memset(lengths + 144, 9, 256 - 144);
  780. memset(lengths + 256, 7, 280 - 256);
  781. memset(lengths + 280, 8, 288 - 280);
  782. dctx->staticlentable = zlib_mktable(lengths, 288);
  783. memset(lengths, 5, 32);
  784. dctx->staticdisttable = zlib_mktable(lengths, 32);
  785. dctx->state = START; /* even before header */
  786. dctx->currlentable = dctx->currdisttable = dctx->lenlentable = NULL;
  787. dctx->bits = 0;
  788. dctx->nbits = 0;
  789. dctx->winpos = 0;
  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. sfree(dctx);
  806. }
  807. static int zlib_huflookup(unsigned long *bitsp, int *nbitsp,
  808. struct zlib_table *tab)
  809. {
  810. unsigned long bits = *bitsp;
  811. int nbits = *nbitsp;
  812. while (1) {
  813. struct zlib_tableentry *ent;
  814. ent = &tab->table[bits & tab->mask];
  815. if (ent->nbits > nbits)
  816. return -1; /* not enough data */
  817. bits >>= ent->nbits;
  818. nbits -= ent->nbits;
  819. if (ent->code == -1)
  820. tab = ent->nexttable;
  821. else {
  822. *bitsp = bits;
  823. *nbitsp = nbits;
  824. return ent->code;
  825. }
  826. if (!tab) {
  827. /*
  828. * There was a missing entry in the table, presumably
  829. * due to an invalid Huffman table description, and the
  830. * subsequent data has attempted to use the missing
  831. * entry. Return a decoding failure.
  832. */
  833. return -2;
  834. }
  835. }
  836. }
  837. static void zlib_emit_char(struct zlib_decompress_ctx *dctx, int c)
  838. {
  839. dctx->window[dctx->winpos] = c;
  840. dctx->winpos = (dctx->winpos + 1) & (WINSIZE - 1);
  841. if (dctx->outlen >= dctx->outsize) {
  842. dctx->outsize = dctx->outlen + 512;
  843. dctx->outblk = sresize(dctx->outblk, dctx->outsize, unsigned char);
  844. }
  845. dctx->outblk[dctx->outlen++] = c;
  846. }
  847. #define EATBITS(n) ( dctx->nbits -= (n), dctx->bits >>= (n) )
  848. bool zlib_decompress_block(ssh_decompressor *dc,
  849. const unsigned char *block, int len,
  850. unsigned char **outblock, int *outlen)
  851. {
  852. struct zlib_decompress_ctx *dctx =
  853. container_of(dc, struct zlib_decompress_ctx, dc);
  854. const coderecord *rec;
  855. int code, blktype, rep, dist, nlen, header;
  856. static const unsigned char lenlenmap[] = {
  857. 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
  858. };
  859. dctx->outblk = snewn(256, unsigned char);
  860. dctx->outsize = 256;
  861. dctx->outlen = 0;
  862. while (len > 0 || dctx->nbits > 0) {
  863. while (dctx->nbits < 24 && len > 0) {
  864. dctx->bits |= (*block++) << dctx->nbits;
  865. dctx->nbits += 8;
  866. len--;
  867. }
  868. switch (dctx->state) {
  869. case START:
  870. /* Expect 16-bit zlib header. */
  871. if (dctx->nbits < 16)
  872. goto finished; /* done all we can */
  873. /*
  874. * The header is stored as a big-endian 16-bit integer,
  875. * in contrast to the general little-endian policy in
  876. * the rest of the format :-(
  877. */
  878. header = (((dctx->bits & 0xFF00) >> 8) |
  879. ((dctx->bits & 0x00FF) << 8));
  880. EATBITS(16);
  881. /*
  882. * Check the header:
  883. *
  884. * - bits 8-11 should be 1000 (Deflate/RFC1951)
  885. * - bits 12-15 should be at most 0111 (window size)
  886. * - bit 5 should be zero (no dictionary present)
  887. * - we don't care about bits 6-7 (compression rate)
  888. * - bits 0-4 should be set up to make the whole thing
  889. * a multiple of 31 (checksum).
  890. */
  891. if ((header & 0x0F00) != 0x0800 ||
  892. (header & 0xF000) > 0x7000 ||
  893. (header & 0x0020) != 0x0000 ||
  894. (header % 31) != 0)
  895. goto decode_error;
  896. dctx->state = OUTSIDEBLK;
  897. break;
  898. case OUTSIDEBLK:
  899. /* Expect 3-bit block header. */
  900. if (dctx->nbits < 3)
  901. goto finished; /* done all we can */
  902. EATBITS(1);
  903. blktype = dctx->bits & 3;
  904. EATBITS(2);
  905. if (blktype == 0) {
  906. int to_eat = dctx->nbits & 7;
  907. dctx->state = UNCOMP_LEN;
  908. EATBITS(to_eat); /* align to byte boundary */
  909. } else if (blktype == 1) {
  910. dctx->currlentable = dctx->staticlentable;
  911. dctx->currdisttable = dctx->staticdisttable;
  912. dctx->state = INBLK;
  913. } else if (blktype == 2) {
  914. dctx->state = TREES_HDR;
  915. }
  916. break;
  917. case TREES_HDR:
  918. /*
  919. * Dynamic block header. Five bits of HLIT, five of
  920. * HDIST, four of HCLEN.
  921. */
  922. if (dctx->nbits < 5 + 5 + 4)
  923. goto finished; /* done all we can */
  924. dctx->hlit = 257 + (dctx->bits & 31);
  925. EATBITS(5);
  926. dctx->hdist = 1 + (dctx->bits & 31);
  927. EATBITS(5);
  928. dctx->hclen = 4 + (dctx->bits & 15);
  929. EATBITS(4);
  930. dctx->lenptr = 0;
  931. dctx->state = TREES_LENLEN;
  932. memset(dctx->lenlen, 0, sizeof(dctx->lenlen));
  933. break;
  934. case TREES_LENLEN:
  935. if (dctx->nbits < 3)
  936. goto finished;
  937. while (dctx->lenptr < dctx->hclen && dctx->nbits >= 3) {
  938. dctx->lenlen[lenlenmap[dctx->lenptr++]] =
  939. (unsigned char) (dctx->bits & 7);
  940. EATBITS(3);
  941. }
  942. if (dctx->lenptr == dctx->hclen) {
  943. dctx->lenlentable = zlib_mktable(dctx->lenlen, 19);
  944. dctx->state = TREES_LEN;
  945. dctx->lenptr = 0;
  946. }
  947. break;
  948. case TREES_LEN:
  949. if (dctx->lenptr >= dctx->hlit + dctx->hdist) {
  950. dctx->currlentable = zlib_mktable(dctx->lengths, dctx->hlit);
  951. dctx->currdisttable = zlib_mktable(dctx->lengths + dctx->hlit,
  952. dctx->hdist);
  953. zlib_freetable(&dctx->lenlentable);
  954. dctx->lenlentable = NULL;
  955. dctx->state = INBLK;
  956. break;
  957. }
  958. code =
  959. zlib_huflookup(&dctx->bits, &dctx->nbits, dctx->lenlentable);
  960. if (code == -1)
  961. goto finished;
  962. if (code == -2)
  963. goto decode_error;
  964. if (code < 16)
  965. dctx->lengths[dctx->lenptr++] = code;
  966. else {
  967. dctx->lenextrabits = (code == 16 ? 2 : code == 17 ? 3 : 7);
  968. dctx->lenaddon = (code == 18 ? 11 : 3);
  969. dctx->lenrep = (code == 16 && dctx->lenptr > 0 ?
  970. dctx->lengths[dctx->lenptr - 1] : 0);
  971. dctx->state = TREES_LENREP;
  972. }
  973. break;
  974. case TREES_LENREP:
  975. if (dctx->nbits < dctx->lenextrabits)
  976. goto finished;
  977. rep =
  978. dctx->lenaddon +
  979. (dctx->bits & ((1 << dctx->lenextrabits) - 1));
  980. EATBITS(dctx->lenextrabits);
  981. while (rep > 0 && dctx->lenptr < dctx->hlit + dctx->hdist) {
  982. dctx->lengths[dctx->lenptr] = dctx->lenrep;
  983. dctx->lenptr++;
  984. rep--;
  985. }
  986. dctx->state = TREES_LEN;
  987. break;
  988. case INBLK:
  989. code =
  990. zlib_huflookup(&dctx->bits, &dctx->nbits, dctx->currlentable);
  991. if (code == -1)
  992. goto finished;
  993. if (code == -2)
  994. goto decode_error;
  995. if (code < 256)
  996. zlib_emit_char(dctx, code);
  997. else if (code == 256) {
  998. dctx->state = OUTSIDEBLK;
  999. if (dctx->currlentable != dctx->staticlentable) {
  1000. zlib_freetable(&dctx->currlentable);
  1001. dctx->currlentable = NULL;
  1002. }
  1003. if (dctx->currdisttable != dctx->staticdisttable) {
  1004. zlib_freetable(&dctx->currdisttable);
  1005. dctx->currdisttable = NULL;
  1006. }
  1007. } else if (code < 286) { /* static tree can give >285; ignore */
  1008. dctx->state = GOTLENSYM;
  1009. dctx->sym = code;
  1010. }
  1011. break;
  1012. case GOTLENSYM:
  1013. rec = &lencodes[dctx->sym - 257];
  1014. if (dctx->nbits < rec->extrabits)
  1015. goto finished;
  1016. dctx->len =
  1017. rec->min + (dctx->bits & ((1 << rec->extrabits) - 1));
  1018. EATBITS(rec->extrabits);
  1019. dctx->state = GOTLEN;
  1020. break;
  1021. case GOTLEN:
  1022. code =
  1023. zlib_huflookup(&dctx->bits, &dctx->nbits,
  1024. dctx->currdisttable);
  1025. if (code == -1)
  1026. goto finished;
  1027. if (code == -2)
  1028. goto decode_error;
  1029. if (code >= 30) /* dist symbols 30 and 31 are invalid */
  1030. goto decode_error;
  1031. dctx->state = GOTDISTSYM;
  1032. dctx->sym = code;
  1033. break;
  1034. case GOTDISTSYM:
  1035. rec = &distcodes[dctx->sym];
  1036. if (dctx->nbits < rec->extrabits)
  1037. goto finished;
  1038. dist = rec->min + (dctx->bits & ((1 << rec->extrabits) - 1));
  1039. EATBITS(rec->extrabits);
  1040. dctx->state = INBLK;
  1041. while (dctx->len--)
  1042. zlib_emit_char(dctx, dctx->window[(dctx->winpos - dist) &
  1043. (WINSIZE - 1)]);
  1044. break;
  1045. case UNCOMP_LEN:
  1046. /*
  1047. * Uncompressed block. We expect to see a 16-bit LEN.
  1048. */
  1049. if (dctx->nbits < 16)
  1050. goto finished;
  1051. dctx->uncomplen = dctx->bits & 0xFFFF;
  1052. EATBITS(16);
  1053. dctx->state = UNCOMP_NLEN;
  1054. break;
  1055. case UNCOMP_NLEN:
  1056. /*
  1057. * Uncompressed block. We expect to see a 16-bit NLEN,
  1058. * which should be the one's complement of the previous
  1059. * LEN.
  1060. */
  1061. if (dctx->nbits < 16)
  1062. goto finished;
  1063. nlen = dctx->bits & 0xFFFF;
  1064. EATBITS(16);
  1065. if (dctx->uncomplen != (nlen ^ 0xFFFF))
  1066. goto decode_error;
  1067. if (dctx->uncomplen == 0)
  1068. dctx->state = OUTSIDEBLK; /* block is empty */
  1069. else
  1070. dctx->state = UNCOMP_DATA;
  1071. break;
  1072. case UNCOMP_DATA:
  1073. if (dctx->nbits < 8)
  1074. goto finished;
  1075. zlib_emit_char(dctx, dctx->bits & 0xFF);
  1076. EATBITS(8);
  1077. if (--dctx->uncomplen == 0)
  1078. dctx->state = OUTSIDEBLK; /* end of uncompressed block */
  1079. break;
  1080. }
  1081. }
  1082. finished:
  1083. *outblock = dctx->outblk;
  1084. *outlen = dctx->outlen;
  1085. return true;
  1086. decode_error:
  1087. sfree(dctx->outblk);
  1088. *outblock = dctx->outblk = NULL;
  1089. *outlen = 0;
  1090. return false;
  1091. }
  1092. const struct ssh_compression_alg ssh_zlib = {
  1093. "zlib",
  1094. "[email protected]", /* delayed version */
  1095. zlib_compress_init,
  1096. zlib_compress_cleanup,
  1097. zlib_compress_block,
  1098. zlib_decompress_init,
  1099. zlib_decompress_cleanup,
  1100. zlib_decompress_block,
  1101. "zlib (RFC1950)"
  1102. };