void dump() { // dump2(); if (! root) return; #ifdef SORT_IGNORE_CASE // if print in alphabet order ignore case, then need to re-sort the tree rebuild_tree(true); #endif print_node(root); #ifdef SORT_IGNORE_CASE rebuild_tree(false); #endif }
static size_t lha_pm2_decoder_read(void *data, uint8_t *buf) { LHAPM2Decoder *decoder = data; size_t result; int code; // On first pass through, build initial lookup trees. if (decoder->tree_state == PM2_REBUILD_UNBUILT) { // First bit in stream is discarded? read_bit(&decoder->bit_stream_reader); rebuild_tree(decoder); } result = 0; code = read_from_tree(&decoder->bit_stream_reader, decoder->code_tree); if (code < 0) { return 0; } if (code < 8) { read_single_byte(decoder, (unsigned int) code, buf, &result); } else { copy_from_history(decoder, (unsigned int) code - 8, buf, &result); } return result; }
static void output_byte(LHAPM2Decoder *decoder, uint8_t *buf, size_t *buf_len, uint8_t b) { // Add to history ring buffer. decoder->ringbuf[decoder->ringbuf_pos] = b; decoder->ringbuf_pos = (decoder->ringbuf_pos + 1) % RING_BUFFER_SIZE; // Add to output buffer. buf[*buf_len] = b; ++*buf_len; // Update history chain. update_history(decoder, b); // Count down until it is time to perform a rebuild of the // lookup trees. --decoder->tree_rebuild_remaining; if (decoder->tree_rebuild_remaining == 0) { rebuild_tree(decoder); } }
//print all possible trees void output_all_possible_trees(int* seq, int n, int num1, int num0, vector<codeWords>& store, bool firstCode) { if((num1 + num0) == 2*n) { seq[2*n] = 0; Node *root = rebuild_tree(seq, 2*n+1); //displayTree(root, 0); //show(seq, n); codeWords temp = codeWordsFromTree(root, n, firstCode); //store.push_back(temp); addCodeWords(store, temp); return; } if(num1 >= num0 && num1 < n) { seq[num1+num0] = 1; output_all_possible_trees(seq, n, num1+1, num0, store, firstCode); } if(num0 < num1 && num1 <=n) { seq[num1+num0] = 0; output_all_possible_trees(seq, n, num1, num0+1, store, firstCode); } }
static void rebuild_tree(t_btree *n, t_btree *i, t_btree **r, t_comparator cmp) { t_btree *left; t_btree *right; if (n) { left = n->left; right = n->right; n->left = 0; n->right = 0; if (n != i) ft_btree_insert(r, n, cmp); rebuild_tree(left, i, r, cmp); rebuild_tree(right, i, r, cmp); } }
void put(char* key, char* value) { NODE *node = NULL; if (root) node = fetch(key, root); if (node) { // update the value strncpy(node->value, value, STR_MAX_SIZE); #ifdef SET_IS_USE promote(node); #endif } else { if (empty_count) { // root == NULL case is covered here also node = create_node(key, value); empty_count--; #ifdef SET_IS_USE promote(node); #endif #ifdef SORT_IGNORE_CASE if (root) add_leaf(node, root, false); else rebuild_tree(false); // only for the first element #else if (root) add_leaf(node, root); else rebuild_tree(); // only for the first element #endif } else { node = rear.prev; strncpy(node->key, key, STR_MAX_SIZE); strncpy(node->value, value, STR_MAX_SIZE); #ifdef SET_IS_USE promote(node); #endif #ifdef SORT_IGNORE_CASE rebuild_tree(false); #else rebuild_tree(); #endif } } }
t_btree *ft_btree_remove(t_btree **r, void *n, size_t s, t_comparator cmp) { t_btree *removed; t_btree *new_tree; new_tree = 0; removed = ft_btree_find(*r, n, s, cmp); rebuild_tree(*r, removed, &new_tree, cmp); *r = new_tree; return (removed); }
void dec_bound(size_t b) { empty_count -= (bound - b); if (empty_count < 0) { // Need to delete empty_count nodes from the end NODE *p = &head; size_t i = 0; while (i < b) { i++; p = p->next; } NODE* q = p->next; p->next = &rear; rear.prev->next = NULL; rear.prev = p; // delete the nodes from q and rebuild the sorting tree remove_nodes(q); rebuild_tree(false); empty_count = 0; } bound = b; }
int main(int argc, char *argv[]) { Boolean use_default_sfx = TRUE; Boolean show_basename_only = FALSE; char lsdir[MaxPathSize]; char sfx[MaxPathSize]; char *lsdirp = NULL; int ch; setprogname(argv[0]); if (argc < 2) usage(); while ((ch = getopt(argc, argv, Options)) != -1) switch (ch) { case 'C': config_file = optarg; break; case 'K': pkgdb_set_dir(optarg, 3); break; case 'S': sfx[0] = 0x0; use_default_sfx = FALSE; break; case 'V': show_version(); /* NOTREACHED */ case 'b': show_basename_only = TRUE; break; case 'd': (void) strlcpy(lsdir, optarg, sizeof(lsdir)); lsdirp = lsdir; break; case 'q': quiet = 1; break; case 's': (void) strlcpy(sfx, optarg, sizeof(sfx)); use_default_sfx = FALSE; break; case 'v': ++verbose; break; default: usage(); /* NOTREACHED */ } argc -= optind; argv += optind; if (argc <= 0) { usage(); } /* * config-var is reading the config file implicitly, * so skip it here. */ if (strcasecmp(argv[0], "config-var") != 0) pkg_install_config(); if (use_default_sfx) (void) strlcpy(sfx, DEFAULT_SFX, sizeof(sfx)); if (strcasecmp(argv[0], "pmatch") == 0) { char *pattern, *pkg; argv++; /* "pmatch" */ if (argv[0] == NULL || argv[1] == NULL) { usage(); } pattern = argv[0]; pkg = argv[1]; if (pkg_match(pattern, pkg)) { return 0; } else { return 1; } } else if (strcasecmp(argv[0], "rebuild") == 0) { rebuild(); printf("Done.\n"); } else if (strcasecmp(argv[0], "rebuild-tree") == 0) { rebuild_tree(); printf("Done.\n"); } else if (strcasecmp(argv[0], "check") == 0) { argv++; /* "check" */ check(argv); if (!quiet) { printf("Done.\n"); } } else if (strcasecmp(argv[0], "lsall") == 0) { argv++; /* "lsall" */ while (*argv != NULL) { /* args specified */ int rc; const char *basep, *dir; dir = lsdirp ? lsdirp : dirname_of(*argv); basep = basename_of(*argv); if (show_basename_only) rc = match_local_files(dir, use_default_sfx, 1, basep, lsbasepattern, NULL); else rc = match_local_files(dir, use_default_sfx, 1, basep, lspattern, __UNCONST(dir)); if (rc == -1) errx(EXIT_FAILURE, "Error from match_local_files(\"%s\", \"%s\", ...)", dir, basep); argv++; } } else if (strcasecmp(argv[0], "lsbest") == 0) { argv++; /* "lsbest" */ while (*argv != NULL) { /* args specified */ const char *basep, *dir; char *p; dir = lsdirp ? lsdirp : dirname_of(*argv); basep = basename_of(*argv); p = find_best_matching_file(dir, basep, use_default_sfx, 1); if (p) { if (show_basename_only) printf("%s\n", p); else printf("%s/%s\n", dir, p); free(p); } argv++; } } else if (strcasecmp(argv[0], "list") == 0 || strcasecmp(argv[0], "dump") == 0) { pkgdb_dump(); } else if (strcasecmp(argv[0], "add") == 0) { struct pkgdb_count count; count.files = 0; count.directories = 0; count.packages = 0; for (++argv; *argv != NULL; ++argv) add_pkg(*argv, &count); } else if (strcasecmp(argv[0], "set") == 0) { argv++; /* "set" */ set_unset_variable(argv, FALSE); } else if (strcasecmp(argv[0], "unset") == 0) { argv++; /* "unset" */ set_unset_variable(argv, TRUE); } else if (strcasecmp(argv[0], "config-var") == 0) { argv++; if (argv == NULL || argv[1] != NULL) errx(EXIT_FAILURE, "config-var takes exactly one argument"); pkg_install_show_variable(argv[0]); } else if (strcasecmp(argv[0], "check-license") == 0) { if (argv[1] == NULL) errx(EXIT_FAILURE, "check-license takes exactly one argument"); load_license_lists(); switch (acceptable_pkg_license(argv[1])) { case 0: puts("no"); return 0; case 1: puts("yes"); return 0; case -1: errx(EXIT_FAILURE, "invalid license condition"); } } else if (strcasecmp(argv[0], "check-single-license") == 0) { if (argv[1] == NULL) errx(EXIT_FAILURE, "check-license takes exactly one argument"); load_license_lists(); switch (acceptable_license(argv[1])) { case 0: puts("no"); return 0; case 1: puts("yes"); return 0; case -1: errx(EXIT_FAILURE, "invalid license"); } } #ifndef BOOTSTRAP else if (strcasecmp(argv[0], "findbest") == 0) { struct url *url; char *output; int rc; process_pkg_path(); rc = 0; for (++argv; *argv != NULL; ++argv) { url = find_best_package(NULL, *argv, 1); if (url == NULL) { rc = 1; continue; } output = fetchStringifyURL(url); puts(output); fetchFreeURL(url); free(output); } return rc; } else if (strcasecmp(argv[0], "fetch-pkg-vulnerabilities") == 0) { fetch_pkg_vulnerabilities(--argc, ++argv); } else if (strcasecmp(argv[0], "check-pkg-vulnerabilities") == 0) { check_pkg_vulnerabilities(--argc, ++argv); } else if (strcasecmp(argv[0], "audit") == 0) { audit_pkgdb(--argc, ++argv); } else if (strcasecmp(argv[0], "audit-pkg") == 0) { audit_pkg(--argc, ++argv); } else if (strcasecmp(argv[0], "audit-batch") == 0) { audit_batch(--argc, ++argv); } else if (strcasecmp(argv[0], "audit-history") == 0) { audit_history(--argc, ++argv); } else if (strcasecmp(argv[0], "check-signature") == 0) { struct archive *pkg; int rc; rc = 0; for (--argc, ++argv; argc > 0; --argc, ++argv) { char *archive_name; pkg = open_archive(*argv, &archive_name); if (pkg == NULL) { warnx("%s could not be opened", *argv); continue; } if (pkg_full_signature_check(archive_name, &pkg)) rc = 1; free(archive_name); if (!pkg) archive_read_finish(pkg); } return rc; } else if (strcasecmp(argv[0], "x509-sign-package") == 0) { #ifdef HAVE_SSL --argc; ++argv; if (argc != 4) errx(EXIT_FAILURE, "x509-sign-package takes exactly four arguments"); pkg_sign_x509(argv[0], argv[1], argv[2], argv[3]); #else errx(EXIT_FAILURE, "OpenSSL support is not included"); #endif } else if (strcasecmp(argv[0], "gpg-sign-package") == 0) { --argc; ++argv; if (argc != 2) errx(EXIT_FAILURE, "gpg-sign-package takes exactly two arguments"); pkg_sign_gpg(argv[0], argv[1]); } #endif else { usage(); } return 0; }