/* file delta coding according to chunk algorithms */ int file_delta(char *src_filename, char *chunk_filename, char *delta_filename, int chunk_algo) { int fd_src, fd_chunk, fd_delta; int rwsize; hashtable *htab_md5 = NULL; hashtable *htab_csum = NULL; chunk_file_header chunk_file_hdr; chunk_block_entry chunk_bentry; delta_file_header delta_file_hdr; int i, ret = 0; /* open files */ fd_src = open(src_filename, O_RDONLY); if (fd_src == -1) return -1; fd_chunk = open(chunk_filename, O_RDONLY); if (fd_chunk == -1) { ret = -1; goto _FILE_DELTA_EXIT; } fd_delta = open(delta_filename, O_WRONLY|O_CREAT|O_TRUNC, 0644); if (fd_delta == -1) { ret = -1; goto _FILE_DELTA_EXIT; } /* build hashtable from chunk file */ htab_md5 = create_hashtable(HASHTABLE_BUCKET_SZ); htab_csum = create_hashtable(HASHTABLE_BUCKET_SZ); if (htab_md5 == NULL || htab_csum == NULL) { ret = -1; goto _FILE_DELTA_EXIT; } rwsize = read(fd_chunk, &chunk_file_hdr, CHUNK_FILE_HEADER_SZ); if (rwsize == -1 || rwsize != CHUNK_FILE_HEADER_SZ) { ret = -1; goto _FILE_DELTA_EXIT; } for(i = 0; i < chunk_file_hdr.block_nr; i++) { rwsize = read(fd_chunk, &chunk_bentry, CHUNK_BLOCK_ENTRY_SZ); if (rwsize == -1 || rwsize != CHUNK_BLOCK_ENTRY_SZ) { ret = -1; goto _FILE_DELTA_EXIT; } hash_checkin(htab_md5, chunk_bentry.md5, chunk_bentry); hash_checkin(htab_csum, chunk_bentry.csum, chunk_bentry); } /* pre-write delte file header */ delta_file_hdr.block_nr = 0; rwsize = write(fd_delta, &delta_file_hdr, DELTA_FILE_HEADER_SZ); if (rwsize == -1 || rwsize != DELTA_FILE_HEADER_SZ) { ret = -1; goto _FILE_DELTA_EXIT; } /* generate delta file according to chunk algorithms */ switch(chunk_algo) { case CHUNK_FSP: ret = file_delta_fsp(htab_md5, htab_csum, fd_src, fd_delta, &delta_file_hdr); break; case CHUNK_CDC: ret = file_delta_cdc(htab_md5, htab_csum, fd_src, fd_delta, &delta_file_hdr); break; case CHUNK_SBC: ret = file_delta_sbc(htab_md5, htab_csum, fd_src, fd_delta, &delta_file_hdr); break; } /* write delta file header */ if (ret == 0) { if (-1 == lseek(fd_delta, 0, SEEK_SET)) { ret = -1; goto _FILE_DELTA_EXIT; } rwsize = write(fd_delta, &delta_file_hdr, DELTA_FILE_HEADER_SZ); if (rwsize == -1 || rwsize != DELTA_FILE_HEADER_SZ) ret = -1; } _FILE_DELTA_EXIT: close(fd_src); close(fd_chunk); close(fd_delta); unlink(chunk_filename); if (htab_md5 != NULL) hash_free(htab_md5); if (htab_csum != NULL) hash_free(htab_csum); return ret; }
static void parse_listener_prefix(config_t *c, config_line_t *l, lwan_t *lwan) { lwan_url_map_t url_map = {0}; struct hash *hash = hash_str_new(free, free); lwan_handler_t *handler = NULL; void *callback = NULL; char *prefix = strdupa(l->line.value); void *data = NULL; while (config_read_line(c, l)) { switch (l->type) { case CONFIG_LINE_TYPE_LINE: if (!strcmp(l->line.key, "handler")) { handler = find_symbol(l->line.value); if (!handler) { config_error(c, "Could not find handler \"%s\"", l->line.value); goto out; } } else if (!strcmp(l->line.key, "callback")) { callback = find_symbol(l->line.value); if (!callback) { config_error(c, "Could not find callback \"%s\"", l->line.value); goto out; } } else { hash_add(hash, strdup(l->line.key), strdup(l->line.value)); } break; case CONFIG_LINE_TYPE_SECTION: if (!strcmp(l->section.name, "authorization")) { parse_listener_prefix_authorization(c, l, &url_map); } else { config_error(c, "Unknown section type: \"%s\"", l->section.name); goto out; } break; case CONFIG_LINE_TYPE_SECTION_END: goto add_map; } } config_error(c, "Expecting section end while parsing prefix"); goto out; add_map: if (handler == callback && !callback) { config_error(c, "Missing callback or handler"); goto out; } if (handler && callback) { config_error(c, "Callback and handler are mutually exclusive"); goto out; } if (callback) { url_map.callback = callback; url_map.flags |= HANDLER_PARSE_MASK; url_map.data = data; url_map.handler = NULL; } else if (handler && handler->init_from_hash && handler->handle) { url_map.data = handler->init_from_hash(hash); url_map.callback = handler->handle; url_map.flags |= handler->flags; url_map.handler = handler; } else { config_error(c, "Invalid handler"); goto out; } add_url_map(lwan->url_map_trie, prefix, &url_map); out: hash_free(hash); }
void ensemble_config_init_FIELD( ensemble_config_type * ensemble_config , const config_content_type * config , ecl_grid_type * grid) { if (config_content_has_item(config , FIELD_KEY)) { const config_content_item_type * item = config_content_get_item( config , FIELD_KEY ); int i; for (i=0; i < config_content_item_get_size( item ); i++) { const config_content_node_type * node = config_content_item_iget_node( item , i ); const char * key = config_content_node_iget( node , 0 ); const char * var_type_string = config_content_node_iget( node , 1 ); enkf_config_node_type * config_node; { hash_type * options = hash_alloc(); int truncation = TRUNCATE_NONE; double value_min = -1; double value_max = -1; config_content_node_init_opt_hash( node , options , 2 ); if (hash_has_key( options , MIN_KEY)) { truncation |= TRUNCATE_MIN; value_min = atof(hash_get( options , MIN_KEY)); } if (hash_has_key( options , MAX_KEY)) { truncation |= TRUNCATE_MAX; value_max = atof(hash_get( options , MAX_KEY)); } if (strcmp(var_type_string , DYNAMIC_KEY) == 0) { config_node = ensemble_config_add_field( ensemble_config , key , grid , false); enkf_config_node_update_state_field( config_node , truncation , value_min , value_max ); } else if (strcmp(var_type_string , PARAMETER_KEY) == 0) { const char * ecl_file = config_content_node_iget( node , 2 ); const char * init_file_fmt = hash_safe_get( options , INIT_FILES_KEY ); const char * init_transform = hash_safe_get( options , INIT_TRANSFORM_KEY ); const char * output_transform = hash_safe_get( options , OUTPUT_TRANSFORM_KEY ); const char * min_std_file = hash_safe_get( options , MIN_STD_KEY ); const char * forward_string = hash_safe_get( options , FORWARD_INIT_KEY ); bool forward_init = false; if (forward_string) { if (!util_sscanf_bool( forward_string , &forward_init)) fprintf(stderr,"** Warning: parsing %s as bool failed - using FALSE \n",forward_string); } config_node = ensemble_config_add_field( ensemble_config , key , grid , forward_init); enkf_config_node_update_parameter_field( config_node, ecl_file , init_file_fmt , min_std_file , truncation , value_min , value_max , init_transform , output_transform ); } else if (strcmp(var_type_string , GENERAL_KEY) == 0) { /* General - not really interesting .. */ const char * ecl_file = config_content_node_iget( node , 2 ); const char * enkf_infile = config_content_node_iget( node , 3 ); const char * init_file_fmt = hash_safe_get( options , INIT_FILES_KEY ); const char * init_transform = hash_safe_get( options , INIT_TRANSFORM_KEY ); const char * output_transform = hash_safe_get( options , OUTPUT_TRANSFORM_KEY ); const char * input_transform = hash_safe_get( options , INPUT_TRANSFORM_KEY ); const char * min_std_file = hash_safe_get( options , MIN_STD_KEY ); const char * forward_string = hash_safe_get( options , FORWARD_INIT_KEY ); bool forward_init = false; if (forward_string) { if (!util_sscanf_bool( forward_string , &forward_init)) fprintf(stderr,"** Warning: parsing %s as bool failed - using FALSE \n",forward_string); } config_node = ensemble_config_add_field( ensemble_config , key , grid , forward_init); enkf_config_node_update_general_field( config_node, ecl_file , enkf_infile , init_file_fmt , min_std_file , truncation , value_min , value_max , init_transform , input_transform , output_transform); } else util_abort("%s: field type: %s is not recognized\n",__func__ , var_type_string); hash_free( options ); } } } }
void free_client(void* c, size_t l) { client_t* client = c; hash_free(&client->recv_table); free(c); }
void ext_joblist_free(ext_joblist_type * joblist) { hash_free(joblist->jobs); free(joblist); }
void list_test(void) { openvpn_thread_init(); { struct gc_arena gc = gc_new(); struct hash *hash = hash_init(10000, get_random(), word_hash_function, word_compare_function); struct hash *nhash = hash_init(256, get_random(), word_hash_function, word_compare_function); printf("hash_init n_buckets=%d mask=0x%08x\n", hash->n_buckets, hash->mask); /* parse words from stdin */ while (true) { char buf[256]; char wordbuf[256]; int wbi; int bi; char c; if (!fgets(buf, sizeof(buf), stdin)) { break; } bi = wbi = 0; do { c = buf[bi++]; if (isalnum(c) || c == '_') { ASSERT(wbi < (int) sizeof(wordbuf)); wordbuf[wbi++] = c; } else { if (wbi) { struct word *w; ASSERT(wbi < (int) sizeof(wordbuf)); wordbuf[wbi++] = '\0'; /* word is parsed from stdin */ /* does it already exist in table? */ w = (struct word *) hash_lookup(hash, wordbuf); if (w) { /* yes, increment count */ ++w->n; } else { /* no, make a new object */ ALLOC_OBJ_GC(w, struct word, &gc); w->word = string_alloc(wordbuf, &gc); w->n = 1; ASSERT(hash_add(hash, w->word, w, false)); ASSERT(hash_add(nhash, w->word, (void *) ((random() & 0x0F) + 1), false)); } } wbi = 0; } } while (c); } #if 1 /* remove some words from the table */ { rmhash(hash, "true"); rmhash(hash, "false"); } #endif /* output contents of hash table */ { int base; int inc = 0; int count = 0; for (base = 0; base < hash_n_buckets(hash); base += inc) { struct hash_iterator hi; struct hash_element *he; inc = (get_random() % 3) + 1; hash_iterator_init_range(hash, &hi, true, base, base + inc); while ((he = hash_iterator_next(&hi))) { struct word *w = (struct word *) he->value; printf("%6d '%s'\n", w->n, w->word); ++count; } hash_iterator_free(&hi); } ASSERT(count == hash_n_elements(hash)); } #if 1 /* test hash_remove_by_value function */ { int i; for (i = 1; i <= 16; ++i) { printf("[%d] ***********************************\n", i); print_nhash(nhash); hash_remove_by_value(nhash, (void *) i, true); } printf("FINAL **************************\n"); print_nhash(nhash); } #endif hash_free(hash); hash_free(nhash); gc_free(&gc); } openvpn_thread_cleanup(); }
/* RAH 4.16.01 This code has several leaks that must be fixed */ dict2pid_t *dict2pid_build (mdef_t *mdef, dict_t *dict) { dict2pid_t *dict2pid; s3ssid_t *internal, **ldiph, **rdiph, *single; int32 pronlen; hash_table_t *hs, *hp; glist_t g; gnode_t *gn; s3senid_t *sen; hash_entry_t *he; int32 *cslen; int32 i, j, b, l, r, w, n, p; E_INFO("Building PID tables for dictionary\n"); dict2pid = (dict2pid_t *) ckd_calloc (1, sizeof(dict2pid_t)); dict2pid->internal = (s3ssid_t **) ckd_calloc (dict_size(dict), sizeof(s3ssid_t *)); dict2pid->ldiph_lc = (s3ssid_t ***) ckd_calloc_3d (mdef->n_ciphone, mdef->n_ciphone, mdef->n_ciphone, sizeof(s3ssid_t)); dict2pid->single_lc = (s3ssid_t **) ckd_calloc_2d (mdef->n_ciphone, mdef->n_ciphone, sizeof(s3ssid_t)); dict2pid->n_comstate = 0; dict2pid->n_comsseq = 0; hs = hash_new (mdef->n_ciphone * mdef->n_ciphone * mdef->n_emit_state, HASH_CASE_YES); hp = hash_new (mdef->n_ciphone * mdef->n_ciphone, HASH_CASE_YES); for (w = 0, n = 0; w < dict_size(dict); w++) { pronlen = dict_pronlen(dict, w); if (pronlen < 0) E_FATAL("Pronunciation-length(%s)= %d\n", dict_wordstr(dict, w), pronlen); n += pronlen; } internal = (s3ssid_t *) ckd_calloc (n, sizeof(s3ssid_t)); /* Temporary */ ldiph = (s3ssid_t **) ckd_calloc_2d (mdef->n_ciphone, mdef->n_ciphone, sizeof(s3ssid_t)); rdiph = (s3ssid_t **) ckd_calloc_2d (mdef->n_ciphone, mdef->n_ciphone, sizeof(s3ssid_t)); single = (s3ssid_t *) ckd_calloc (mdef->n_ciphone, sizeof(s3ssid_t)); for (b = 0; b < mdef->n_ciphone; b++) { for (l = 0; l < mdef->n_ciphone; l++) { for (r = 0; r < mdef->n_ciphone; r++) dict2pid->ldiph_lc[b][r][l] = BAD_S3SSID; dict2pid->single_lc[b][l] = BAD_S3SSID; ldiph[b][l] = BAD_S3SSID; rdiph[b][l] = BAD_S3SSID; } single[b] = BAD_S3SSID; } for (w = 0; w < dict_size(dict); w++) { dict2pid->internal[w] = internal; pronlen = dict_pronlen(dict,w); if (pronlen >= 2) { b = dict_pron(dict, w, 0); r = dict_pron(dict, w, 1); if (NOT_S3SSID(ldiph[b][r])) { g = ldiph_comsseq(mdef, b, r); ldiph[b][r] = ssidlist2comsseq (g, mdef, dict2pid, hs, hp); glist_free (g); for (l = 0; l < mdef_n_ciphone(mdef); l++) { p = mdef_phone_id_nearest (mdef, (s3cipid_t)b, (s3cipid_t)l, (s3cipid_t)r, WORD_POSN_BEGIN); dict2pid->ldiph_lc[b][r][l] = mdef_pid2ssid(mdef, p); } } internal[0] = ldiph[b][r]; for (i = 1; i < pronlen-1; i++) { l = b; b = r; r = dict_pron(dict, w, i+1); p = mdef_phone_id_nearest(mdef, (s3cipid_t)b, (s3cipid_t)l, (s3cipid_t)r, WORD_POSN_INTERNAL); internal[i] = mdef_pid2ssid(mdef, p); } l = b; b = r; if (NOT_S3SSID(rdiph[b][l])) { g = rdiph_comsseq(mdef, b, l); rdiph[b][l] = ssidlist2comsseq (g, mdef, dict2pid, hs, hp); glist_free (g); } internal[pronlen-1] = rdiph[b][l]; } else if (pronlen == 1) { b = dict_pron(dict, w, 0); if (NOT_S3SSID(single[b])) { g = single_comsseq(mdef, b); single[b] = ssidlist2comsseq (g, mdef, dict2pid, hs, hp); glist_free (g); for (l = 0; l < mdef_n_ciphone(mdef); l++) { g = single_lc_comsseq(mdef, b, l); dict2pid->single_lc[b][l] = ssidlist2comsseq (g, mdef, dict2pid, hs, hp); glist_free (g); } } internal[0] = single[b]; } internal += pronlen; } ckd_free_2d ((void **) ldiph); ckd_free_2d ((void **) rdiph); ckd_free ((void *) single); /* Allocate space for composite state table */ cslen = (int32 *) ckd_calloc (dict2pid->n_comstate, sizeof(int32)); g = hash_tolist(hs, &n); assert (n == dict2pid->n_comstate); n = 0; for (gn = g; gn; gn = gnode_next(gn)) { he = (hash_entry_t *) gnode_ptr (gn); sen = (s3senid_t *) hash_entry_key(he); for (i = 0; IS_S3SENID(sen[i]); i++); cslen[hash_entry_val(he)] = i+1; /* +1 for terminating sentinel */ n += (i+1); } dict2pid->comstate = (s3senid_t **) ckd_calloc (dict2pid->n_comstate, sizeof(s3senid_t *)); sen = (s3senid_t *) ckd_calloc (n, sizeof(s3senid_t)); for (i = 0; i < dict2pid->n_comstate; i++) { dict2pid->comstate[i] = sen; sen += cslen[i]; } /* Build composite state table from hash table hs */ for (gn = g; gn; gn = gnode_next(gn)) { he = (hash_entry_t *) gnode_ptr (gn); sen = (s3senid_t *) hash_entry_key(he); i = hash_entry_val(he); for (j = 0; j < cslen[i]; j++) dict2pid->comstate[i][j] = sen[j]; assert (sen[j-1] == BAD_S3SENID); ckd_free ((void *)sen); } ckd_free (cslen); glist_free (g); hash_free (hs); /* Allocate space for composite sseq table */ dict2pid->comsseq = (s3senid_t **) ckd_calloc (dict2pid->n_comsseq, sizeof(s3senid_t *)); g = hash_tolist (hp, &n); assert (n == dict2pid->n_comsseq); /* Build composite sseq table */ for (gn = g; gn; gn = gnode_next(gn)) { he = (hash_entry_t *) gnode_ptr (gn); i = hash_entry_val(he); dict2pid->comsseq[i] = (s3senid_t *) hash_entry_key(he); } glist_free (g); hash_free (hp); /* Weight for each composite state */ dict2pid->comwt = (int32 *) ckd_calloc (dict2pid->n_comstate, sizeof(int32)); for (i = 0; i < dict2pid->n_comstate; i++) { sen = dict2pid->comstate[i]; for (j = 0; IS_S3SENID(sen[j]); j++); #if 0 /* if comstate i has N states, its weight= (1/N^2) (Major Hack!!) */ dict2pid->comwt[i] = - (logs3 ((float64)j) << 1); #else /* if comstate i has N states, its weight= 1/N */ dict2pid->comwt[i] = - logs3 ((float64)j); #endif } E_INFO("%d composite states; %d composite sseq\n", dict2pid->n_comstate, dict2pid->n_comsseq); return dict2pid; }
static void parse_listener_prefix(config_t *c, config_line_t *l, lwan_t *lwan, const lwan_module_t *module) { lwan_url_map_t url_map = {0}; struct hash *hash = hash_str_new(free, free); void *handler = NULL; char *prefix = strdupa(l->line.value); while (config_read_line(c, l)) { switch (l->type) { case CONFIG_LINE_TYPE_LINE: if (!strcmp(l->line.key, "module")) { if (module) { config_error(c, "Module already specified"); goto out; } module = lwan_module_find(lwan, l->line.value); if (!module) { config_error(c, "Could not find module \"%s\"", l->line.value); goto out; } } else if (!strcmp(l->line.key, "handler")) { handler = find_handler_symbol(l->line.value); if (!handler) { config_error(c, "Could not find handler \"%s\"", l->line.value); goto out; } } else { hash_add(hash, strdup(l->line.key), strdup(l->line.value)); } break; case CONFIG_LINE_TYPE_SECTION: if (!strcmp(l->section.name, "authorization")) { parse_listener_prefix_authorization(c, l, &url_map); } else { config_error(c, "Unknown section type: \"%s\"", l->section.name); goto out; } break; case CONFIG_LINE_TYPE_SECTION_END: goto add_map; } } config_error(c, "Expecting section end while parsing prefix"); goto out; add_map: if (module == handler && !handler) { config_error(c, "Missing module or handler"); goto out; } if (module && handler) { config_error(c, "Handler and module are mutually exclusive"); goto out; } if (handler) { url_map.handler = handler; url_map.flags |= HANDLER_PARSE_MASK; url_map.data = hash; url_map.module = NULL; hash = NULL; } else if (module && module->init_from_hash && module->handle) { url_map.data = module->init_from_hash(hash); url_map.handler = module->handle; url_map.flags |= module->flags; url_map.module = module; } else { config_error(c, "Invalid handler"); goto out; } add_url_map(&lwan->url_map_trie, prefix, &url_map); out: hash_free(hash); }
struct iked_hash * hash_new(uint8_t type, uint16_t id) { struct iked_hash *hash; const EVP_MD *md = NULL; HMAC_CTX *ctx = NULL; int length = 0, fixedkey = 0, trunc = 0; switch (type) { case IKEV2_XFORMTYPE_PRF: switch (id) { case IKEV2_XFORMPRF_HMAC_MD5: md = EVP_md5(); length = MD5_DIGEST_LENGTH; break; case IKEV2_XFORMPRF_HMAC_SHA1: md = EVP_sha1(); length = SHA_DIGEST_LENGTH; break; case IKEV2_XFORMPRF_HMAC_SHA2_256: md = EVP_sha256(); length = SHA256_DIGEST_LENGTH; break; case IKEV2_XFORMPRF_HMAC_SHA2_384: md = EVP_sha384(); length = SHA384_DIGEST_LENGTH; break; case IKEV2_XFORMPRF_HMAC_SHA2_512: md = EVP_sha512(); length = SHA512_DIGEST_LENGTH; break; case IKEV2_XFORMPRF_AES128_XCBC: fixedkey = 128 / 8; length = fixedkey; /* FALLTHROUGH */ case IKEV2_XFORMPRF_HMAC_TIGER: case IKEV2_XFORMPRF_AES128_CMAC: default: log_debug("%s: prf %s not supported", __func__, print_map(id, ikev2_xformprf_map)); break; } break; case IKEV2_XFORMTYPE_INTEGR: switch (id) { case IKEV2_XFORMAUTH_HMAC_MD5_96: md = EVP_md5(); length = MD5_DIGEST_LENGTH; trunc = 12; break; case IKEV2_XFORMAUTH_HMAC_SHA1_96: md = EVP_sha1(); length = SHA_DIGEST_LENGTH; trunc = 12; break; case IKEV2_XFORMAUTH_HMAC_SHA2_256_128: md = EVP_sha256(); length = SHA256_DIGEST_LENGTH; trunc = 16; break; case IKEV2_XFORMAUTH_HMAC_SHA2_384_192: md = EVP_sha384(); length = SHA384_DIGEST_LENGTH; trunc = 24; break; case IKEV2_XFORMAUTH_HMAC_SHA2_512_256: md = EVP_sha512(); length = SHA512_DIGEST_LENGTH; trunc = 32; break; case IKEV2_XFORMAUTH_NONE: case IKEV2_XFORMAUTH_DES_MAC: case IKEV2_XFORMAUTH_KPDK_MD5: case IKEV2_XFORMAUTH_AES_XCBC_96: case IKEV2_XFORMAUTH_HMAC_MD5_128: case IKEV2_XFORMAUTH_HMAC_SHA1_160: case IKEV2_XFORMAUTH_AES_CMAC_96: case IKEV2_XFORMAUTH_AES_128_GMAC: case IKEV2_XFORMAUTH_AES_192_GMAC: case IKEV2_XFORMAUTH_AES_256_GMAC: default: log_debug("%s: auth %s not supported", __func__, print_map(id, ikev2_xformauth_map)); break; } break; default: log_debug("%s: hash type %s not supported", __func__, print_map(id, ikev2_xformtype_map)); break; } if (md == NULL) return (NULL); if ((hash = calloc(1, sizeof(*hash))) == NULL) { log_debug("%s: alloc hash", __func__); return (NULL); } hash->hash_type = type; hash->hash_id = id; hash->hash_priv = md; hash->hash_ctx = NULL; hash->hash_trunc = trunc; hash->hash_length = length; hash->hash_fixedkey = fixedkey; if ((ctx = calloc(1, sizeof(*ctx))) == NULL) { log_debug("%s: alloc hash ctx", __func__); hash_free(hash); return (NULL); } HMAC_CTX_init(ctx); hash->hash_ctx = ctx; return (hash); }
extern void forget_all (void) { hash_free (src_to_dest); }
void load_labels_term() { hash_free(label_table,NULL); label_table = NULL; }
void local_ministep_free(local_ministep_type * ministep) { free(ministep->name); hash_free( ministep->datasets ); local_obsdata_free(ministep->observations); free( ministep ); }
char * canonicalize_filename_mode (const char *name, canonicalize_mode_t can_mode) { char *rname, *dest, *extra_buf = NULL; char const *start; char const *end; char const *rname_limit; size_t extra_len = 0; Hash_table *ht = NULL; int saved_errno; int can_flags = can_mode & ~CAN_MODE_MASK; bool logical = can_flags & CAN_NOLINKS; size_t prefix_len; can_mode &= CAN_MODE_MASK; if (MULTIPLE_BITS_SET (can_mode)) { errno = EINVAL; return NULL; } if (name == NULL) { errno = EINVAL; return NULL; } if (name[0] == '\0') { errno = ENOENT; return NULL; } /* This is always zero for Posix hosts, but can be 2 for MS-Windows and MS-DOS X:/foo/bar file names. */ prefix_len = FILE_SYSTEM_PREFIX_LEN (name); if (!IS_ABSOLUTE_FILE_NAME (name)) { rname = xgetcwd (); if (!rname) return NULL; dest = strchr (rname, '\0'); if (dest - rname < PATH_MAX) { char *p = xrealloc (rname, PATH_MAX); dest = p + (dest - rname); rname = p; rname_limit = rname + PATH_MAX; } else { rname_limit = dest; } start = name; prefix_len = FILE_SYSTEM_PREFIX_LEN (rname); } else { rname = xmalloc (PATH_MAX); rname_limit = rname + PATH_MAX; dest = rname; if (prefix_len) { memcpy (rname, name, prefix_len); dest += prefix_len; } *dest++ = '/'; if (DOUBLE_SLASH_IS_DISTINCT_ROOT) { if (ISSLASH (name[1]) && !ISSLASH (name[2]) && !prefix_len) *dest++ = '/'; *dest = '\0'; } start = name + prefix_len; } for ( ; *start; start = end) { /* Skip sequence of multiple file name separators. */ while (ISSLASH (*start)) ++start; /* Find end of component. */ for (end = start; *end && !ISSLASH (*end); ++end) /* Nothing. */; if (end - start == 0) break; else if (end - start == 1 && start[0] == '.') /* nothing */; else if (end - start == 2 && start[0] == '.' && start[1] == '.') { /* Back up to previous component, ignore if at root already. */ if (dest > rname + prefix_len + 1) for (--dest; dest > rname && !ISSLASH (dest[-1]); --dest) continue; if (DOUBLE_SLASH_IS_DISTINCT_ROOT && dest == rname + 1 && !prefix_len && ISSLASH (*dest) && !ISSLASH (dest[1])) dest++; } else { struct stat st; if (!ISSLASH (dest[-1])) *dest++ = '/'; if (dest + (end - start) >= rname_limit) { ptrdiff_t dest_offset = dest - rname; size_t new_size = rname_limit - rname; if (end - start + 1 > PATH_MAX) new_size += end - start + 1; else new_size += PATH_MAX; rname = xrealloc (rname, new_size); rname_limit = rname + new_size; dest = rname + dest_offset; } dest = memcpy (dest, start, end - start); dest += end - start; *dest = '\0'; if (logical && (can_mode == CAN_MISSING)) { /* Avoid the stat in this case as it's inconsequential. i.e. we're neither resolving symlinks or testing component existence. */ st.st_mode = 0; } else if ((logical ? stat (rname, &st) : lstat (rname, &st)) != 0) { saved_errno = errno; if (can_mode == CAN_EXISTING) goto error; if (can_mode == CAN_ALL_BUT_LAST) { if (end[strspn (end, SLASHES)] || saved_errno != ENOENT) goto error; continue; } st.st_mode = 0; } if (S_ISLNK (st.st_mode)) { char *buf; size_t n, len; /* Detect loops. We cannot use the cycle-check module here, since it's actually possible to encounter the same symlink more than once in a given traversal. However, encountering the same symlink,NAME pair twice does indicate a loop. */ if (seen_triple (&ht, name, &st)) { if (can_mode == CAN_MISSING) continue; saved_errno = ELOOP; goto error; } buf = areadlink_with_size (rname, st.st_size); if (!buf) { if (can_mode == CAN_MISSING && errno != ENOMEM) continue; saved_errno = errno; goto error; } n = strlen (buf); len = strlen (end); if (!extra_len) { extra_len = ((n + len + 1) > PATH_MAX) ? (n + len + 1) : PATH_MAX; extra_buf = xmalloc (extra_len); } else if ((n + len + 1) > extra_len) { extra_len = n + len + 1; extra_buf = xrealloc (extra_buf, extra_len); } /* Careful here, end may be a pointer into extra_buf... */ memmove (&extra_buf[n], end, len + 1); name = end = memcpy (extra_buf, buf, n); if (IS_ABSOLUTE_FILE_NAME (buf)) { size_t pfxlen = FILE_SYSTEM_PREFIX_LEN (buf); if (pfxlen) memcpy (rname, buf, pfxlen); dest = rname + pfxlen; *dest++ = '/'; /* It's an absolute symlink */ if (DOUBLE_SLASH_IS_DISTINCT_ROOT) { if (ISSLASH (buf[1]) && !ISSLASH (buf[2]) && !pfxlen) *dest++ = '/'; *dest = '\0'; } /* Install the new prefix to be in effect hereafter. */ prefix_len = pfxlen; } else { /* Back up to previous component, ignore if at root already: */ if (dest > rname + prefix_len + 1) for (--dest; dest > rname && !ISSLASH (dest[-1]); --dest) continue; if (DOUBLE_SLASH_IS_DISTINCT_ROOT && dest == rname + 1 && ISSLASH (*dest) && !ISSLASH (dest[1]) && !prefix_len) dest++; } free (buf); } else { if (!S_ISDIR (st.st_mode) && *end && (can_mode != CAN_MISSING)) { saved_errno = ENOTDIR; goto error; } } } } if (dest > rname + prefix_len + 1 && ISSLASH (dest[-1])) --dest; if (DOUBLE_SLASH_IS_DISTINCT_ROOT && dest == rname + 1 && !prefix_len && ISSLASH (*dest) && !ISSLASH (dest[1])) dest++; *dest = '\0'; if (rname_limit != dest + 1) rname = xrealloc (rname, dest - rname + 1); free (extra_buf); if (ht) hash_free (ht); return rname; error: free (extra_buf); free (rname); if (ht) hash_free (ht); errno = saved_errno; return NULL; }
void set_free(set_type * set) { hash_free(set->key_hash); free(set); }
static void file_map_free( file_map_type * file_map ) { hash_free( file_map->kw_index ); stringlist_free( file_map->distinct_kw ); vector_free( file_map->kw_list ); free( file_map ); }
int main (int argc, char **argv) { unsigned int i; unsigned int k; unsigned int table_size[] = {1, 2, 3, 4, 5, 23, 53}; Hash_table *ht; Hash_tuning tuning; hash_reset_tuning (&tuning); tuning.shrink_threshold = 0.3; tuning.shrink_factor = 0.707; tuning.growth_threshold = 1.5; tuning.growth_factor = 2.0; tuning.is_n_buckets = true; if (1 < argc) { unsigned int seed; if (get_seed (argv[1], &seed) != 0) { fprintf (stderr, "invalid seed: %s\n", argv[1]); exit (EXIT_FAILURE); } srand (seed); } for (i = 0; i < ARRAY_CARDINALITY (table_size); i++) { size_t sz = table_size[i]; ht = hash_initialize (sz, NULL, hash_pjw, hash_compare_strings, NULL); ASSERT (ht); insert_new (ht, "a"); { char *str1 = strdup ("a"); char *str2; ASSERT (str1); str2 = hash_insert (ht, str1); ASSERT (str1 != str2); ASSERT (STREQ (str1, str2)); free (str1); } insert_new (ht, "b"); insert_new (ht, "c"); i = 0; ASSERT (hash_do_for_each (ht, walk, &i) == 3); ASSERT (i == 7); { void *buf[5] = { NULL }; ASSERT (hash_get_entries (ht, NULL, 0) == 0); ASSERT (hash_get_entries (ht, buf, 5) == 3); ASSERT (STREQ (buf[0], "a") || STREQ (buf[0], "b") || STREQ (buf[0], "c")); } ASSERT (hash_delete (ht, "a")); ASSERT (hash_delete (ht, "a") == NULL); ASSERT (hash_delete (ht, "b")); ASSERT (hash_delete (ht, "c")); ASSERT (hash_rehash (ht, 47)); ASSERT (hash_rehash (ht, 467)); /* Free an empty table. */ hash_clear (ht); hash_free (ht); ht = hash_initialize (sz, NULL, hash_pjw, hash_compare_strings, NULL); ASSERT (ht); insert_new (ht, "z"); insert_new (ht, "y"); insert_new (ht, "x"); insert_new (ht, "w"); insert_new (ht, "v"); insert_new (ht, "u"); hash_clear (ht); ASSERT (hash_get_n_entries (ht) == 0); hash_free (ht); /* Test pointer hashing. */ ht = hash_initialize (sz, NULL, NULL, NULL, NULL); ASSERT (ht); { char *str = strdup ("a"); ASSERT (str); insert_new (ht, "a"); insert_new (ht, str); ASSERT (hash_lookup (ht, str) == str); free (str); } hash_free (ht); } hash_reset_tuning (&tuning); tuning.shrink_threshold = 0.3; tuning.shrink_factor = 0.707; tuning.growth_threshold = 1.5; tuning.growth_factor = 2.0; tuning.is_n_buckets = true; /* Invalid tuning. */ ht = hash_initialize (4651, &tuning, hash_pjw, hash_compare_strings, hash_freer); ASSERT (!ht); /* Alternate tuning. */ tuning.growth_threshold = 0.89; /* Run with default tuning, then with custom tuning settings. */ for (k = 0; k < 2; k++) { Hash_tuning const *tune = (k == 0 ? NULL : &tuning); /* Now, each entry is malloc'd. */ ht = hash_initialize (4651, tune, hash_pjw, hash_compare_strings, hash_freer); ASSERT (ht); for (i = 0; i < 10000; i++) { unsigned int op = rand () % 10; switch (op) { case 0: case 1: case 2: case 3: case 4: case 5: { char buf[50]; char const *p = uinttostr (i, buf); char *p_dup = strdup (p); ASSERT (p_dup); insert_new (ht, p_dup); } break; case 6: { size_t n = hash_get_n_entries (ht); ASSERT (hash_rehash (ht, n + rand () % 20)); } break; case 7: { size_t n = hash_get_n_entries (ht); size_t delta = rand () % 20; if (delta < n) ASSERT (hash_rehash (ht, n - delta)); } break; case 8: case 9: { /* Delete a random entry. */ size_t n = hash_get_n_entries (ht); if (n) { size_t kk = rand () % n; void const *p; void *v; for (p = hash_get_first (ht); kk; --kk, p = hash_get_next (ht, p)) { /* empty */ } ASSERT (p); v = hash_delete (ht, p); ASSERT (v); free (v); } break; } } ASSERT (hash_table_ok (ht)); } hash_free (ht); } return 0; }
void lem_term() { hash_free(lemtab,NULL); lemtab = NULL; }
static void lwan_module_shutdown(lwan_t *l) { hash_free(l->module_registry); }
static void sched_block_free(sched_block_type * block) { vector_free( block->kw_list ); hash_free( block->kw_hash ); free(block); }