/* * Determine if the user is a member of 'identifier' * Returns one of: * 0 User does not match identifier * 1 identifier matches everybody * 2 User is in the group that is identifier * 3 User is identifer */ static int mymemberof(const struct auth_state *auth_state, const char *identifier) { int i; unsigned idhash = strhash(identifier); static unsigned anyonehash = 0; anyonehash = !anyonehash ? strhash("anyone") : anyonehash; if (!auth_state) { /* special case anonymous */ if (!strcmp(identifier, "anyone")) return 1; else if (!strcmp(identifier, "anonymous")) return 3; /* "anonymous" is not a member of any group */ else return 0; } /* is 'identifier' "anyone"? */ if (idhash == anyonehash && !strcmp(identifier, "anyone")) return 1; /* is 'identifier' me? */ if (idhash == auth_state->userid.hash && !strcmp(identifier, auth_state->userid.id)) return 3; /* is it a group i'm a member of ? */ for (i=0; i < auth_state->ngroups; i++) if (idhash == auth_state->groups[i].hash && !strcmp(identifier, auth_state->groups[i].id)) return 2; return 0; }
int dblhashRemove(dblhash_ *h, char *key) { unsigned int code; record_ *recs; unsigned int off, ind, size; ReturnErrIf(h == NULL); ReturnErrIf(key == NULL); code = strhash(key); recs = h->records; size = sizes[h->size_index]; ind = code % size; off = 0; while (recs[ind].hash) { if ((code == recs[ind].hash) && (recs[ind].key != NULL)) { if(!strcmp(key, recs[ind].key)) { /* Do not erase hash, so probes for collisions succeed */ if((recs[ind].freeMem != 'n') && (recs[ind].key != NULL)) { free(recs[ind].key); } recs[ind].key = NULL; recs[ind].value = HUGE_VAL; h->records_count--; return 0; } } ind = (code + (int)pow(++off, 2)) % size; } ReturnErr("Couldn't find %s", key); }
int dblhashFindPtr(dblhash_ *h, char *key, double **value) { record_ *recs; unsigned int off, ind, size; unsigned int code; ReturnErrIf(h == NULL); ReturnErrIf(key == NULL); ReturnErrIf(value == NULL); code = strhash(key); recs = h->records; size = sizes[h->size_index]; ind = code % size; off = 0; /* search on hash which remains even if a record has been removed, * so hash_remove() does not need to move any collision records */ while (recs[ind].hash) { if ((code == recs[ind].hash) && (recs[ind].key != NULL)) { if(!strcmp(key, recs[ind].key)) { *value = &recs[ind].value; return 0; } } ind = (code + (int)pow(++off,2)) % size; } /* Couldn't find the key */ *value = NULL; return 0; }
/* **************************************************************** * Insere os símbolos, enquanto não há colisão * **************************************************************** */ int insert_names (int sz) { SYM **hp; SYM *sp; NAME *np; /* * Inicialmente, zera as tabelas. */ memset (hashtb, 0, sz * sizeof (SYM *)); memset (symtb, 0, sizeof (symtb)); /* * Insere os nomes das funções. */ for (sp = symtb, np = nametb; np->n_name != NOSTR; np++, sp++) { strscpy (sp->s_name, np->n_name, sizeof (sp->s_name)); hp = &hashtb[strhash (sp->s_name, strlen (sp->s_name), sz)]; if (*hp != NOSYMP) return (-1); /* colisão */ sp->s_type = np->n_type; sp->s_union = np->n_id; /*** sp->s_hnext = NOSYMP; ***/ *hp = sp; } return (0); } /* end insert_names */
int namemap(char *str, int len) /*;namemap*/ { /* Namemap: Give the symbol number corresponding to a given string, putting * the string into the structure if not already there. If the string is * already known to be in the table, then it is not necessary to give the * length. */ int nmhash; struct namelistmap *node; int nnode; #ifdef PDEBUG printf("namemap %s\n", str); #endif nmhash = strhash(str) % NAMEMAPSIZE; for (node = strtonumtable[nmhash]; node != (struct namelistmap *)0; node = node->nextmap) { if (!strcmp(node->name, str)) { #ifdef PDEBUG printf("namemap returns %d\n", node->num); #endif return(node->num); } } nnode = newnode(str, len, nmhash); #ifdef PDEBUG printf("namemap returns (new) %d\n", nnode); #endif return nnode; }
static int newnode(char *str, int len, int nmhash) /*;newnode*/ { /* Newnode: Allocate storage for a new node; set the fields; insert in * namelist and namemap. The symbol number is returned. Nmhash is either * the hashvalue for the given string if already known, or -1 indicating * not yet known. */ struct namelistmap *node; int row, links; /* Allocate and set fields of node */ node = (struct namelistmap *)malloc(sizeof(struct namelistmap)); node->num = ++symcount; node->name = malloc((unsigned)(len + 1)); strcpy(node->name, str); /* Insert in namelist */ nlisthash(symcount, &row, &links); node->nextlist = numtostrtable[row]->nextlist; numtostrtable[row]->nextlist = node; numtostrtable[row] = node; /* Insert in namemap */ if (nmhash == -1) nmhash = strhash(node->name) % NAMEMAPSIZE; node->nextmap = strtonumtable[nmhash]; strtonumtable[nmhash] = node; return(symcount); }
PrimitiveType get_primitive_type (const char* name, PrimitiveType default_type) { if (!name) return default_type; struct Map { stl::hash_key<const char*> name_hash; PrimitiveType type; }; static Map map [] = { {"line_list", PrimitiveType_LineList}, {"line_strip", PrimitiveType_LineStrip}, {"triangle_list", PrimitiveType_TriangleList}, {"triangle_strip", PrimitiveType_TriangleStrip}, {"triangle_fan", PrimitiveType_TriangleFan}, }; static const size_t map_size = sizeof (map) / sizeof (*map); const Map* map_iter = map; size_t hash = strhash (name); for (size_t i=0; i<map_size; i++, map_iter++) if (map_iter->name_hash.get_hash () == hash) return map_iter->type; return default_type; }
struct subprocess_entry *subprocess_find_entry(struct hashmap *hashmap, const char *cmd) { struct subprocess_entry key; hashmap_entry_init(&key, strhash(cmd)); key.cmd = cmd; return hashmap_get(hashmap, &key, NULL); }
JsObject *CreateString(char *string){ size_t length = strlen(string); size_t var_size = length; JsStringObject *obj = (JsStringObject*)JsString_Type.tp_new(var_size); memcpy(obj->ob_sval, string, var_size); obj->ob_sval[length] = 0; obj->ob_hash = strhash(string); return (JsObject*)obj; }
/* * Helper to populate an RAM file table entry. */ static void ramfile_set(ramfile_t *file, const char *name, u8 *addr, u32 size) { file->hash = name ? strhash(name) : 0; file->addr = addr; file->size = size; D_PRINTF("%s: name=%s hash=%08x addr=%08x size=%i\n", __FUNCTION__, name, file->hash, (u32)file->addr, file->size); }
/* * hAddChannel * Adds a channel's name in the proper hash linked list, can't fail. * chptr must have a non-null name or expect a coredump. * As before the name is taken from chptr->name, we do hash its entire * lenght since this proved to be statistically faster */ int hAddChannel(struct Channel *chptr) { HASHREGS hashv = strhash(chptr->chname); chptr->hnext = channelTable[hashv]; channelTable[hashv] = chptr; return 0; }
/* * hAddClient * Adds a client's name in the proper hash linked list, can't fail, * cptr must have a non-null name or expect a coredump, the name is * infact taken from cptr->name */ int hAddClient(struct Client *cptr) { HASHREGS hashv = strhash(cli_name(cptr)); cli_hnext(cptr) = clientTable[hashv]; clientTable[hashv] = cptr; return 0; }
/* * * db_hash_registro * */ unsigned int db_hash_registro(const char *clave, int len) { return (unsigned int)(strhash(clave) & (len-1)); /* RyDeN -- Restamos 1 a la longitud de len ya que el numero ha de ser potencia de 2, y por lo tanto quedaría un número binario tipo '1000000000', por lo que restando queda '111111111', para poder hacer el & binario, además de que el 0 también cuenta :P */ }
static SExp exec_intern(const char* name) { size_t l = strlen(name); Symbol* p = smalloc(sizeof(*p) + l, TRUE); p->typeinfo = &TSymbol; p->hash = strhash(name); strncpy(p->name, name, l+1); ht_add_direct(&s_symbol_table, p->name, p); return ptr2s(p); }
/* * Record the use of a CONFIG_* word. */ static void use_config(const char *m, int slen) { unsigned int hash = strhash(m, slen); if (is_defined_config(m, slen, hash)) return; define_config(m, slen, hash); print_config(m, slen); }
int main (int argc, char **argv) { for (int argi = 0; argi < argc; ++argi) { char *str = argv[argi]; size_t hashcode = strhash (str); printf ("%20lu = strhash (\"%s\")\n", hashcode, str); } printf ("%20lu = 0xFFFFFFFFLU\n", 0xFFFFFFFFLU); printf ("%20lu = 0x%016lXLU\n", (size_t)-1L, (size_t)-1L); return EXIT_SUCCESS; }
static struct submodule_hash_entry *alloc_submodule_hash_entry( const char *submodule, struct ref_store *refs) { struct submodule_hash_entry *entry; FLEX_ALLOC_STR(entry, submodule, submodule); hashmap_entry_init(entry, strhash(submodule)); entry->refs = refs; return entry; }
/* * hChangeClient * Removes the old name of a client from a linked list and adds * the new one to another linked list, there is a slight chanche * that this is useless if the two hashes are the same but it still * would need to move the name to the top of the list. * As always it's responsibility of the caller to check that * both newname and cptr->name are valid names (not "" or NULL). * Typically, to change the nick of an already hashed client: * if (!BadPtr(newname) && ClearTheNameSomeHow(newname)) { * hChangeClient(cptr, newname); * strcpy(cptr->name, newname); * }; * There isn't an equivalent function for channels since they * don't change name. */ int hChangeClient(struct Client *cptr, const char *newname) { HASHREGS newhash = strhash(newname); assert(0 != cptr); hRemClient(cptr); cli_hnext(cptr) = clientTable[newhash]; clientTable[newhash] = cptr; return 0; }
static void changed_files(struct hashmap *result, const char *index_path, const char *workdir) { struct child_process update_index = CHILD_PROCESS_INIT; struct child_process diff_files = CHILD_PROCESS_INIT; struct strbuf index_env = STRBUF_INIT, buf = STRBUF_INIT; const char *git_dir = absolute_path(get_git_dir()), *env[] = { NULL, NULL }; FILE *fp; strbuf_addf(&index_env, "GIT_INDEX_FILE=%s", index_path); env[0] = index_env.buf; argv_array_pushl(&update_index.args, "--git-dir", git_dir, "--work-tree", workdir, "update-index", "--really-refresh", "-q", "--unmerged", NULL); update_index.no_stdin = 1; update_index.no_stdout = 1; update_index.no_stderr = 1; update_index.git_cmd = 1; update_index.use_shell = 0; update_index.clean_on_exit = 1; update_index.dir = workdir; update_index.env = env; /* Ignore any errors of update-index */ run_command(&update_index); argv_array_pushl(&diff_files.args, "--git-dir", git_dir, "--work-tree", workdir, "diff-files", "--name-only", "-z", NULL); diff_files.no_stdin = 1; diff_files.git_cmd = 1; diff_files.use_shell = 0; diff_files.clean_on_exit = 1; diff_files.out = -1; diff_files.dir = workdir; diff_files.env = env; if (start_command(&diff_files)) die("could not obtain raw diff"); fp = xfdopen(diff_files.out, "r"); while (!strbuf_getline_nul(&buf, fp)) { struct path_entry *entry; FLEX_ALLOC_STR(entry, path, buf.buf); hashmap_entry_init(entry, strhash(buf.buf)); hashmap_add(result, entry); } fclose(fp); if (finish_command(&diff_files)) die("diff-files did not exit properly"); strbuf_release(&index_env); strbuf_release(&buf); }
std::string create_filename(const std::string &prefix, const std::string &servicePath, const std::string &key) { int len = servicePath.length() + prefix.length() + 19; char file[len]; // NOTE: This requires sizeof(size_t) == 8 and a correct implementation of std::hash. std::hash<std::string> strhash; size_t hash = strhash(key); snprintf(file, len, "%s/%s-%.16zx", servicePath.c_str(), prefix.c_str(), hash); return file; }
/* * isNickJuped() * Tells if a nick is juped (nonzero returned) or not (zero) */ int isNickJuped(const char *nick) { int pos; if (nick && *nick) { for (pos = strhash(nick); (pos &= JUPEHASHMASK), jupeTable[pos][0]; pos++) { if (0 == ircd_strcmp(nick, jupeTable[pos])) return 1; } } return 0; /* A bogus pointer is NOT a juped nick, right ? :) */ }
/* * Return the ref_store instance for the specified submodule. If that * ref_store hasn't been initialized yet, return NULL. */ static struct ref_store *lookup_submodule_ref_store(const char *submodule) { struct submodule_hash_entry *entry; if (!submodule_ref_stores.tablesize) /* It's initialized on demand in register_ref_store(). */ return NULL; entry = hashmap_get_from_hash(&submodule_ref_stores, strhash(submodule), submodule); return entry ? entry->refs : NULL; }
int name_map(char *str) /*;name_map*/ { int nmhash; struct namelistmap *node; nmhash = strhash(str) % NAMEMAPSIZE; for (node = strtonumtable[nmhash]; node != (struct namelistmap *)0; node = node->nextmap) { if (!strcmp(node->name, str)) return(1); } return(0); }
static void find_exact_matches(struct string_list *a, struct string_list *b) { struct hashmap map; int i; hashmap_init(&map, (hashmap_cmp_fn)patch_util_cmp, NULL, 0); /* First, add the patches of a to a hash map */ for (i = 0; i < a->nr; i++) { struct patch_util *util = a->items[i].util; util->i = i; util->patch = a->items[i].string; util->diff = util->patch + util->diff_offset; hashmap_entry_init(util, strhash(util->diff)); hashmap_add(&map, util); } /* Now try to find exact matches in b */ for (i = 0; i < b->nr; i++) { struct patch_util *util = b->items[i].util, *other; util->i = i; util->patch = b->items[i].string; util->diff = util->patch + util->diff_offset; hashmap_entry_init(util, strhash(util->diff)); other = hashmap_remove(&map, util, NULL); if (other) { if (other->matching >= 0) BUG("already assigned!"); other->matching = i; util->matching = other->i; } } hashmap_free(&map, 0); }
int main() { int i = -42; bool b = true; std::string str = "erich"; std::hash<std::string> strhash; std::hash<bool> boolhash; std::hash<int> inthash; std::cout << (size_t)i << ":" << inthash(i)<<std::endl; std::cout << str << ":" << strhash(str)<<std::endl; std::cout << b << ":" << boolhash(b)<<std::endl; }
static msg_get_size* create(uint32_t a_id, const std::string& a_filename, uint64_t a_src_size, int a_src_fd, mode_t a_mode, const Alloc& a = Alloc()) { size_t size = sizeof(msg_get_size) + a_filename.size() + 1; msg_get_size* p = reinterpret_cast<msg_get_size*>(Alloc(a).allocate(size)); uint32_t name_hash = strhash(a_filename); new (p) msg_get_size(size, a_id, name_hash); p->m_mode = a_mode; p->m_src_fd = a_src_fd; p->m_src_size = a_src_size; strcpy(p->m_name, a_filename.c_str()); return p; }
/* * Produce an auth_state structure for the given identifier */ static struct auth_state *mynewstate(const char *identifier) { struct auth_state *output = NULL; if(canonuser_id && (!strcmp(identifier, canonuser_id) || !strcmp(identifier, canonuser_cache->userid.id))) { /* It's the currently cached user, return the previous result */ free(canonuser_id); canonuser_id = NULL; output = canonuser_cache; canonuser_cache = NULL; return output; } /* * If anyone or anonymous, just pass through. Otherwise, try to load the * groups the user is in */ if(strcmp(identifier, "anyone") && strcmp(identifier, "anonymous")) { if(ptload(identifier, &output) < 0) { syslog(LOG_ERR, "ptload failed for %s", identifier); /* Allowing this to go through is a problem if negative group access is * used significantly. Allowing this to go through is a feature when * the ptserver is having problems and the user wants to get to his * inbox. * * note that even on a failure, output should either be NULL or a * correct (enough) value. */ } } if (output == NULL) { output = (struct auth_state *)xzmalloc(sizeof(struct auth_state)); strlcpy(output->userid.id, identifier, sizeof(output->userid.id)); output->userid.hash = strhash(identifier); syslog(LOG_DEBUG, "creating empty auth_state for %s", identifier); } else { syslog(LOG_DEBUG, "using ptloaded value of: %s", output->userid.id); } return output; }
static void do_whowas(struct Client *source_p, const int parc, char *parv[]) { int cur = 0; int max = -1; const dlink_node *node = NULL; if (parc > 2 && !EmptyString(parv[2])) max = atoi(parv[2]); if (!MyConnect(source_p) && (max <= 0 || max > WHOWAS_MAX_REPLIES)) max = WHOWAS_MAX_REPLIES; DLINK_FOREACH(node, whowas_get_hash(strhash(parv[1]))->head) { const struct Whowas *whowas = node->data; if (!irccmp(parv[1], whowas->name)) { sendto_one_numeric(source_p, &me, RPL_WHOWASUSER, whowas->name, whowas->username, whowas->hostname, whowas->realname); if (HasUMode(source_p, UMODE_OPER)) sendto_one_numeric(source_p, &me, RPL_WHOISACTUALLY, whowas->name, whowas->username, whowas->hostname, whowas->sockhost); if (strcmp(whowas->account, "*")) sendto_one_numeric(source_p, &me, RPL_WHOISACCOUNT, whowas->name, whowas->account, "was"); if ((whowas->shide || ConfigServerHide.hide_servers) && !HasUMode(source_p, UMODE_OPER)) sendto_one_numeric(source_p, &me, RPL_WHOISSERVER, whowas->name, ConfigServerInfo.network_name, date_ctime(whowas->logoff)); else sendto_one_numeric(source_p, &me, RPL_WHOISSERVER, whowas->name, whowas->servername, date_ctime(whowas->logoff)); ++cur; } if (max > 0 && cur >= max) break; } if (!cur) sendto_one_numeric(source_p, &me, ERR_WASNOSUCHNICK, parv[1]); sendto_one_numeric(source_p, &me, RPL_ENDOFWHOWAS, parv[1]); }
qbyte wtp_signature ( const char *filename) /* Name of executable program */ { static char signature [30]; /* Signature is XXXXXXXX-XXXXXXXX-XXXXXXXX (hash-size-time) */ /* then passed through 32-bit CRC calculation. */ sprintf (signature, "%08lX-%08lX-%08lX", strhash (filename), get_file_size (filename), get_file_time (filename)); return (calculate_crc ((byte *) signature, strlen (signature))); }
static void add_left_or_right(struct hashmap *map, const char *path, const char *content, int is_right) { struct pair_entry *e, *existing; FLEX_ALLOC_STR(e, path, path); hashmap_entry_init(e, strhash(path)); existing = hashmap_get(map, e, NULL); if (existing) { free(e); e = existing; } else { e->left[0] = e->right[0] = '\0'; hashmap_add(map, e); } strlcpy(is_right ? e->right : e->left, content, PATH_MAX); }