+struct suffix_tree {
+ struct suffix_tree *sibling;
+ struct suffix_tree *child;
+ char letter, word_end;
+};
+
+static char *match_list;
+static struct suffix_tree *suftree;
+
+static void add_suffix(struct suffix_tree **prior, char ltr, const char *str)
+{
+ struct suffix_tree *node, *newnode;
+
+ if (ltr == '[') {
+ const char *after = strchr(str, ']');
+ /* Treat "[foo" and "[]" as having a literal '['. */
+ if (after && after++ != str+1) {
+ while ((ltr = *str++) != ']')
+ add_suffix(prior, ltr, after);
+ return;
+ }
+ }
+
+ for (node = *prior; node; prior = &node->sibling, node = node->sibling) {
+ if (node->letter == ltr) {
+ if (*str)
+ add_suffix(&node->child, *str, str+1);
+ else
+ node->word_end = 1;
+ return;
+ }
+ if (node->letter > ltr)
+ break;
+ }
+ if (!(newnode = new(struct suffix_tree)))
+ out_of_memory("add_suffix");
+ newnode->sibling = node;
+ newnode->child = NULL;
+ newnode->letter = ltr;
+ *prior = newnode;
+ if (*str) {
+ add_suffix(&newnode->child, *str, str+1);
+ newnode->word_end = 0;
+ } else
+ newnode->word_end = 1;
+}
+
+static void add_nocompress_suffixes(const char *str)
+{
+ char *buf, *t;
+ const char *f = str;
+
+ if (!(buf = new_array(char, strlen(f) + 1)))
+ out_of_memory("add_nocompress_suffixes");
+
+ while (*f) {
+ if (*f == '/') {
+ f++;
+ continue;
+ }
+
+ t = buf;
+ do {
+ if (isUpper(f))
+ *t++ = toLower(f);
+ else
+ *t++ = *f;
+ } while (*++f != '/' && *f);
+ *t++ = '\0';
+
+ add_suffix(&suftree, *buf, buf+1);
+ }
+
+ free(buf);
+}
+
+static void init_set_compression(void)
+{
+ const char *f;
+ char *t, *start;
+
+ if (skip_compress)
+ add_nocompress_suffixes(skip_compress);
+
+ /* A non-daemon transfer skips the default suffix list if the
+ * user specified --skip-compress. */
+ if (skip_compress && module_id < 0)
+ f = "";
+ else
+ f = lp_dont_compress(module_id);
+
+ if (!(match_list = t = new_array(char, strlen(f) + 2)))
+ out_of_memory("set_compression");
+
+ per_file_default_level = def_compress_level;
+
+ while (*f) {
+ if (*f == ' ') {
+ f++;
+ continue;
+ }
+
+ start = t;
+ do {
+ if (isUpper(f))
+ *t++ = toLower(f);
+ else
+ *t++ = *f;
+ } while (*++f != ' ' && *f);
+ *t++ = '\0';
+
+ if (t - start == 1+1 && *start == '*') {
+ /* Optimize a match-string of "*". */
+ *match_list = '\0';
+ suftree = NULL;
+ per_file_default_level = 0;
+ break;
+ }
+
+ /* Move *.foo items into the stuffix tree. */
+ if (*start == '*' && start[1] == '.' && start[2]
+ && !strpbrk(start+2, ".?*")) {
+ add_suffix(&suftree, start[2], start+3);
+ t = start;
+ }
+ }
+ *t++ = '\0';
+}