token.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  1. /*
  2. * Routines used by the file-transfer code.
  3. *
  4. * Copyright (C) 1996 Andrew Tridgell
  5. * Copyright (C) 1996 Paul Mackerras
  6. * Copyright (C) 2003-2009 Wayne Davison
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, visit the http://fsf.org website.
  20. */
  21. #include "rsync.h"
  22. #include "ifuncs.h"
  23. #include "zlib/zlib.h"
  24. extern int do_compression;
  25. extern int module_id;
  26. extern int def_compress_level;
  27. extern char *skip_compress;
  28. static int compression_level, per_file_default_level;
  29. struct suffix_tree {
  30. struct suffix_tree *sibling;
  31. struct suffix_tree *child;
  32. char letter, word_end;
  33. };
  34. static char *match_list;
  35. static struct suffix_tree *suftree;
  36. static void add_suffix(struct suffix_tree **prior, char ltr, const char *str)
  37. {
  38. struct suffix_tree *node, *newnode;
  39. if (ltr == '[') {
  40. const char *after = strchr(str, ']');
  41. /* Treat "[foo" and "[]" as having a literal '['. */
  42. if (after && after++ != str+1) {
  43. while ((ltr = *str++) != ']')
  44. add_suffix(prior, ltr, after);
  45. return;
  46. }
  47. }
  48. for (node = *prior; node; prior = &node->sibling, node = node->sibling) {
  49. if (node->letter == ltr) {
  50. if (*str)
  51. add_suffix(&node->child, *str, str+1);
  52. else
  53. node->word_end = 1;
  54. return;
  55. }
  56. if (node->letter > ltr)
  57. break;
  58. }
  59. if (!(newnode = new(struct suffix_tree)))
  60. out_of_memory("add_suffix");
  61. newnode->sibling = node;
  62. newnode->child = NULL;
  63. newnode->letter = ltr;
  64. *prior = newnode;
  65. if (*str) {
  66. add_suffix(&newnode->child, *str, str+1);
  67. newnode->word_end = 0;
  68. } else
  69. newnode->word_end = 1;
  70. }
  71. static void add_nocompress_suffixes(const char *str)
  72. {
  73. char *buf, *t;
  74. const char *f = str;
  75. if (!(buf = new_array(char, strlen(f) + 1)))
  76. out_of_memory("add_nocompress_suffixes");
  77. while (*f) {
  78. if (*f == '/') {
  79. f++;
  80. continue;
  81. }
  82. t = buf;
  83. do {
  84. if (isUpper(f))
  85. *t++ = toLower(f);
  86. else
  87. *t++ = *f;
  88. } while (*++f != '/' && *f);
  89. *t++ = '\0';
  90. add_suffix(&suftree, *buf, buf+1);
  91. }
  92. free(buf);
  93. }
  94. static void init_set_compression(void)
  95. {
  96. const char *f;
  97. char *t, *start;
  98. if (skip_compress)
  99. add_nocompress_suffixes(skip_compress);
  100. /* A non-daemon transfer skips the default suffix list if the
  101. * user specified --skip-compress. */
  102. if (skip_compress && module_id < 0)
  103. f = "";
  104. else
  105. f = lp_dont_compress(module_id);
  106. if (!(match_list = t = new_array(char, strlen(f) + 2)))
  107. out_of_memory("set_compression");
  108. per_file_default_level = def_compress_level;
  109. while (*f) {
  110. if (*f == ' ') {
  111. f++;
  112. continue;
  113. }
  114. start = t;
  115. do {
  116. if (isUpper(f))
  117. *t++ = toLower(f);
  118. else
  119. *t++ = *f;
  120. } while (*++f != ' ' && *f);
  121. *t++ = '\0';
  122. if (t - start == 1+1 && *start == '*') {
  123. /* Optimize a match-string of "*". */
  124. *match_list = '\0';
  125. suftree = NULL;
  126. per_file_default_level = 0;
  127. break;
  128. }
  129. /* Move *.foo items into the stuffix tree. */
  130. if (*start == '*' && start[1] == '.' && start[2]
  131. && !strpbrk(start+2, ".?*")) {
  132. add_suffix(&suftree, start[2], start+3);
  133. t = start;
  134. }
  135. }
  136. *t++ = '\0';
  137. }
  138. /* determine the compression level based on a wildcard filename list */
  139. void set_compression(const char *fname)
  140. {
  141. const struct suffix_tree *node;
  142. const char *s;
  143. char ltr;
  144. if (!do_compression)
  145. return;
  146. if (!match_list)
  147. init_set_compression();
  148. compression_level = per_file_default_level;
  149. if (!*match_list && !suftree)
  150. return;
  151. if ((s = strrchr(fname, '/')) != NULL)
  152. fname = s + 1;
  153. for (s = match_list; *s; s += strlen(s) + 1) {
  154. if (iwildmatch(s, fname)) {
  155. compression_level = 0;
  156. return;
  157. }
  158. }
  159. if (!(node = suftree) || !(s = strrchr(fname, '.'))
  160. || s == fname || !(ltr = *++s))
  161. return;
  162. while (1) {
  163. if (isUpper(&ltr))
  164. ltr = toLower(&ltr);
  165. while (node->letter != ltr) {
  166. if (node->letter > ltr)
  167. return;
  168. if (!(node = node->sibling))
  169. return;
  170. }
  171. if ((ltr = *++s) == '\0') {
  172. if (node->word_end)
  173. compression_level = 0;
  174. return;
  175. }
  176. if (!(node = node->child))
  177. return;
  178. }
  179. }
  180. /* non-compressing recv token */
  181. static int32 simple_recv_token(int f, char **data)
  182. {
  183. static int32 residue;
  184. static char *buf;
  185. int32 n;
  186. if (!buf) {
  187. buf = new_array(char, CHUNK_SIZE);
  188. if (!buf)
  189. out_of_memory("simple_recv_token");
  190. }
  191. if (residue == 0) {
  192. int32 i = read_int(f);
  193. if (i <= 0)
  194. return i;
  195. residue = i;
  196. }
  197. *data = buf;
  198. n = MIN(CHUNK_SIZE,residue);
  199. residue -= n;
  200. read_buf(f,buf,n);
  201. return n;
  202. }
  203. /* non-compressing send token */
  204. static void simple_send_token(int f, int32 token, struct map_struct *buf,
  205. OFF_T offset, int32 n)
  206. {
  207. if (n > 0) {
  208. int32 len = 0;
  209. while (len < n) {
  210. int32 n1 = MIN(CHUNK_SIZE, n-len);
  211. write_int(f, n1);
  212. write_buf(f, map_ptr(buf, offset+len, n1), n1);
  213. len += n1;
  214. }
  215. }
  216. /* a -2 token means to send data only and no token */
  217. if (token != -2)
  218. write_int(f, -(token+1));
  219. }
  220. /* Flag bytes in compressed stream are encoded as follows: */
  221. #define END_FLAG 0 /* that's all folks */
  222. #define TOKEN_LONG 0x20 /* followed by 32-bit token number */
  223. #define TOKENRUN_LONG 0x21 /* ditto with 16-bit run count */
  224. #define DEFLATED_DATA 0x40 /* + 6-bit high len, then low len byte */
  225. #define TOKEN_REL 0x80 /* + 6-bit relative token number */
  226. #define TOKENRUN_REL 0xc0 /* ditto with 16-bit run count */
  227. #define MAX_DATA_COUNT 16383 /* fit 14 bit count into 2 bytes with flags */
  228. /* zlib.h says that if we want to be able to compress something in a single
  229. * call, avail_out must be at least 0.1% larger than avail_in plus 12 bytes.
  230. * We'll add in 0.1%+16, just to be safe (and we'll avoid floating point,
  231. * to ensure that this is a compile-time value). */
  232. #define AVAIL_OUT_SIZE(avail_in_size) ((avail_in_size)*1001/1000+16)
  233. /* For coding runs of tokens */
  234. static int32 last_token = -1;
  235. static int32 run_start;
  236. static int32 last_run_end;
  237. /* Deflation state */
  238. static z_stream tx_strm;
  239. /* Output buffer */
  240. static char *obuf;
  241. /* We want obuf to be able to hold both MAX_DATA_COUNT+2 bytes as well as
  242. * AVAIL_OUT_SIZE(CHUNK_SIZE) bytes, so make sure that it's large enough. */
  243. #if MAX_DATA_COUNT+2 > AVAIL_OUT_SIZE(CHUNK_SIZE)
  244. #define OBUF_SIZE (MAX_DATA_COUNT+2)
  245. #else
  246. #define OBUF_SIZE AVAIL_OUT_SIZE(CHUNK_SIZE)
  247. #endif
  248. /* Send a deflated token */
  249. static void
  250. send_deflated_token(int f, int32 token, struct map_struct *buf, OFF_T offset,
  251. int32 nb, int32 toklen)
  252. {
  253. int32 n, r;
  254. static int init_done, flush_pending;
  255. if (last_token == -1) {
  256. /* initialization */
  257. if (!init_done) {
  258. tx_strm.next_in = NULL;
  259. tx_strm.zalloc = NULL;
  260. tx_strm.zfree = NULL;
  261. if (deflateInit2(&tx_strm, compression_level,
  262. Z_DEFLATED, -15, 8,
  263. Z_DEFAULT_STRATEGY) != Z_OK) {
  264. rprintf(FERROR, "compression init failed\n");
  265. exit_cleanup(RERR_STREAMIO);
  266. }
  267. if ((obuf = new_array(char, OBUF_SIZE)) == NULL)
  268. out_of_memory("send_deflated_token");
  269. init_done = 1;
  270. } else
  271. deflateReset(&tx_strm);
  272. last_run_end = 0;
  273. run_start = token;
  274. flush_pending = 0;
  275. } else if (last_token == -2) {
  276. run_start = token;
  277. } else if (nb != 0 || token != last_token + 1
  278. || token >= run_start + 65536) {
  279. /* output previous run */
  280. r = run_start - last_run_end;
  281. n = last_token - run_start;
  282. if (r >= 0 && r <= 63) {
  283. write_byte(f, (n==0? TOKEN_REL: TOKENRUN_REL) + r);
  284. } else {
  285. write_byte(f, (n==0? TOKEN_LONG: TOKENRUN_LONG));
  286. write_int(f, run_start);
  287. }
  288. if (n != 0) {
  289. write_byte(f, n);
  290. write_byte(f, n >> 8);
  291. }
  292. last_run_end = last_token;
  293. run_start = token;
  294. }
  295. last_token = token;
  296. if (nb != 0 || flush_pending) {
  297. /* deflate the data starting at offset */
  298. int flush = Z_NO_FLUSH;
  299. tx_strm.avail_in = 0;
  300. tx_strm.avail_out = 0;
  301. do {
  302. if (tx_strm.avail_in == 0 && nb != 0) {
  303. /* give it some more input */
  304. n = MIN(nb, CHUNK_SIZE);
  305. tx_strm.next_in = (Bytef *)
  306. map_ptr(buf, offset, n);
  307. tx_strm.avail_in = n;
  308. nb -= n;
  309. offset += n;
  310. }
  311. if (tx_strm.avail_out == 0) {
  312. tx_strm.next_out = (Bytef *)(obuf + 2);
  313. tx_strm.avail_out = MAX_DATA_COUNT;
  314. if (flush != Z_NO_FLUSH) {
  315. /*
  316. * We left the last 4 bytes in the
  317. * buffer, in case they are the
  318. * last 4. Move them to the front.
  319. */
  320. memcpy(tx_strm.next_out,
  321. obuf+MAX_DATA_COUNT-2, 4);
  322. tx_strm.next_out += 4;
  323. tx_strm.avail_out -= 4;
  324. }
  325. }
  326. if (nb == 0 && token != -2)
  327. flush = Z_SYNC_FLUSH;
  328. r = deflate(&tx_strm, flush);
  329. if (r != Z_OK) {
  330. rprintf(FERROR, "deflate returned %d\n", r);
  331. exit_cleanup(RERR_STREAMIO);
  332. }
  333. if (nb == 0 || tx_strm.avail_out == 0) {
  334. n = MAX_DATA_COUNT - tx_strm.avail_out;
  335. if (flush != Z_NO_FLUSH) {
  336. /*
  337. * We have to trim off the last 4
  338. * bytes of output when flushing
  339. * (they are just 0, 0, ff, ff).
  340. */
  341. n -= 4;
  342. }
  343. if (n > 0) {
  344. obuf[0] = DEFLATED_DATA + (n >> 8);
  345. obuf[1] = n;
  346. write_buf(f, obuf, n+2);
  347. }
  348. }
  349. } while (nb != 0 || tx_strm.avail_out == 0);
  350. flush_pending = token == -2;
  351. }
  352. if (token == -1) {
  353. /* end of file - clean up */
  354. write_byte(f, END_FLAG);
  355. } else if (token != -2) {
  356. /* Add the data in the current block to the compressor's
  357. * history and hash table. */
  358. do {
  359. /* Break up long sections in the same way that
  360. * see_deflate_token() does. */
  361. int32 n1 = toklen > 0xffff ? 0xffff : toklen;
  362. toklen -= n1;
  363. tx_strm.next_in = (Bytef *)map_ptr(buf, offset, n1);
  364. tx_strm.avail_in = n1;
  365. tx_strm.next_out = (Bytef *) obuf;
  366. tx_strm.avail_out = AVAIL_OUT_SIZE(CHUNK_SIZE);
  367. r = deflate(&tx_strm, Z_INSERT_ONLY);
  368. if (r != Z_OK || tx_strm.avail_in != 0) {
  369. rprintf(FERROR, "deflate on token returned %d (%d bytes left)\n",
  370. r, tx_strm.avail_in);
  371. exit_cleanup(RERR_STREAMIO);
  372. }
  373. } while (toklen > 0);
  374. }
  375. }
  376. /* tells us what the receiver is in the middle of doing */
  377. static enum { r_init, r_idle, r_running, r_inflating, r_inflated } recv_state;
  378. /* for inflating stuff */
  379. static z_stream rx_strm;
  380. static char *cbuf;
  381. static char *dbuf;
  382. /* for decoding runs of tokens */
  383. static int32 rx_token;
  384. static int32 rx_run;
  385. /* Receive a deflated token and inflate it */
  386. static int32 recv_deflated_token(int f, char **data)
  387. {
  388. static int init_done;
  389. static int32 saved_flag;
  390. int32 n, flag;
  391. int r;
  392. for (;;) {
  393. switch (recv_state) {
  394. case r_init:
  395. if (!init_done) {
  396. rx_strm.next_out = NULL;
  397. rx_strm.zalloc = NULL;
  398. rx_strm.zfree = NULL;
  399. if (inflateInit2(&rx_strm, -15) != Z_OK) {
  400. rprintf(FERROR, "inflate init failed\n");
  401. exit_cleanup(RERR_STREAMIO);
  402. }
  403. if (!(cbuf = new_array(char, MAX_DATA_COUNT))
  404. || !(dbuf = new_array(char, AVAIL_OUT_SIZE(CHUNK_SIZE))))
  405. out_of_memory("recv_deflated_token");
  406. init_done = 1;
  407. } else {
  408. inflateReset(&rx_strm);
  409. }
  410. recv_state = r_idle;
  411. rx_token = 0;
  412. break;
  413. case r_idle:
  414. case r_inflated:
  415. if (saved_flag) {
  416. flag = saved_flag & 0xff;
  417. saved_flag = 0;
  418. } else
  419. flag = read_byte(f);
  420. if ((flag & 0xC0) == DEFLATED_DATA) {
  421. n = ((flag & 0x3f) << 8) + read_byte(f);
  422. read_buf(f, cbuf, n);
  423. rx_strm.next_in = (Bytef *)cbuf;
  424. rx_strm.avail_in = n;
  425. recv_state = r_inflating;
  426. break;
  427. }
  428. if (recv_state == r_inflated) {
  429. /* check previous inflated stuff ended correctly */
  430. rx_strm.avail_in = 0;
  431. rx_strm.next_out = (Bytef *)dbuf;
  432. rx_strm.avail_out = AVAIL_OUT_SIZE(CHUNK_SIZE);
  433. r = inflate(&rx_strm, Z_SYNC_FLUSH);
  434. n = AVAIL_OUT_SIZE(CHUNK_SIZE) - rx_strm.avail_out;
  435. /*
  436. * Z_BUF_ERROR just means no progress was
  437. * made, i.e. the decompressor didn't have
  438. * any pending output for us.
  439. */
  440. if (r != Z_OK && r != Z_BUF_ERROR) {
  441. rprintf(FERROR, "inflate flush returned %d (%d bytes)\n",
  442. r, n);
  443. exit_cleanup(RERR_STREAMIO);
  444. }
  445. if (n != 0 && r != Z_BUF_ERROR) {
  446. /* have to return some more data and
  447. save the flag for later. */
  448. saved_flag = flag + 0x10000;
  449. *data = dbuf;
  450. return n;
  451. }
  452. /*
  453. * At this point the decompressor should
  454. * be expecting to see the 0, 0, ff, ff bytes.
  455. */
  456. if (!inflateSyncPoint(&rx_strm)) {
  457. rprintf(FERROR, "decompressor lost sync!\n");
  458. exit_cleanup(RERR_STREAMIO);
  459. }
  460. rx_strm.avail_in = 4;
  461. rx_strm.next_in = (Bytef *)cbuf;
  462. cbuf[0] = cbuf[1] = 0;
  463. cbuf[2] = cbuf[3] = 0xff;
  464. inflate(&rx_strm, Z_SYNC_FLUSH);
  465. recv_state = r_idle;
  466. }
  467. if (flag == END_FLAG) {
  468. /* that's all folks */
  469. recv_state = r_init;
  470. return 0;
  471. }
  472. /* here we have a token of some kind */
  473. if (flag & TOKEN_REL) {
  474. rx_token += flag & 0x3f;
  475. flag >>= 6;
  476. } else
  477. rx_token = read_int(f);
  478. if (flag & 1) {
  479. rx_run = read_byte(f);
  480. rx_run += read_byte(f) << 8;
  481. recv_state = r_running;
  482. }
  483. return -1 - rx_token;
  484. case r_inflating:
  485. rx_strm.next_out = (Bytef *)dbuf;
  486. rx_strm.avail_out = AVAIL_OUT_SIZE(CHUNK_SIZE);
  487. r = inflate(&rx_strm, Z_NO_FLUSH);
  488. n = AVAIL_OUT_SIZE(CHUNK_SIZE) - rx_strm.avail_out;
  489. if (r != Z_OK) {
  490. rprintf(FERROR, "inflate returned %d (%d bytes)\n", r, n);
  491. exit_cleanup(RERR_STREAMIO);
  492. }
  493. if (rx_strm.avail_in == 0)
  494. recv_state = r_inflated;
  495. if (n != 0) {
  496. *data = dbuf;
  497. return n;
  498. }
  499. break;
  500. case r_running:
  501. ++rx_token;
  502. if (--rx_run == 0)
  503. recv_state = r_idle;
  504. return -1 - rx_token;
  505. }
  506. }
  507. }
  508. /*
  509. * put the data corresponding to a token that we've just returned
  510. * from recv_deflated_token into the decompressor's history buffer.
  511. */
  512. static void see_deflate_token(char *buf, int32 len)
  513. {
  514. int r;
  515. int32 blklen;
  516. unsigned char hdr[5];
  517. rx_strm.avail_in = 0;
  518. blklen = 0;
  519. hdr[0] = 0;
  520. do {
  521. if (rx_strm.avail_in == 0 && len != 0) {
  522. if (blklen == 0) {
  523. /* Give it a fake stored-block header. */
  524. rx_strm.next_in = (Bytef *)hdr;
  525. rx_strm.avail_in = 5;
  526. blklen = len;
  527. if (blklen > 0xffff)
  528. blklen = 0xffff;
  529. hdr[1] = blklen;
  530. hdr[2] = blklen >> 8;
  531. hdr[3] = ~hdr[1];
  532. hdr[4] = ~hdr[2];
  533. } else {
  534. rx_strm.next_in = (Bytef *)buf;
  535. rx_strm.avail_in = blklen;
  536. len -= blklen;
  537. blklen = 0;
  538. }
  539. }
  540. rx_strm.next_out = (Bytef *)dbuf;
  541. rx_strm.avail_out = AVAIL_OUT_SIZE(CHUNK_SIZE);
  542. r = inflate(&rx_strm, Z_SYNC_FLUSH);
  543. if (r != Z_OK && r != Z_BUF_ERROR) {
  544. rprintf(FERROR, "inflate (token) returned %d\n", r);
  545. exit_cleanup(RERR_STREAMIO);
  546. }
  547. } while (len || rx_strm.avail_out == 0);
  548. }
  549. /**
  550. * Transmit a verbatim buffer of length @p n followed by a token.
  551. * If token == -1 then we have reached EOF
  552. * If n == 0 then don't send a buffer
  553. */
  554. void send_token(int f, int32 token, struct map_struct *buf, OFF_T offset,
  555. int32 n, int32 toklen)
  556. {
  557. if (!do_compression)
  558. simple_send_token(f, token, buf, offset, n);
  559. else
  560. send_deflated_token(f, token, buf, offset, n, toklen);
  561. }
  562. /*
  563. * receive a token or buffer from the other end. If the reurn value is >0 then
  564. * it is a data buffer of that length, and *data will point at the data.
  565. * if the return value is -i then it represents token i-1
  566. * if the return value is 0 then the end has been reached
  567. */
  568. int32 recv_token(int f, char **data)
  569. {
  570. int tok;
  571. if (!do_compression) {
  572. tok = simple_recv_token(f,data);
  573. } else {
  574. tok = recv_deflated_token(f, data);
  575. }
  576. return tok;
  577. }
  578. /*
  579. * look at the data corresponding to a token, if necessary
  580. */
  581. void see_token(char *data, int32 toklen)
  582. {
  583. if (do_compression)
  584. see_deflate_token(data, toklen);
  585. }