#include #include #include #include static unsigned int bhash(const char *str) { unsigned int hash = 5381; const unsigned char *p = (const unsigned char *) str; int c; while ((c = *p++)) hash = (hash << 5) + hash + c; return hash; } static unsigned int jhash(const char *str) { unsigned int hash = 0x9e3779b9; const unsigned char *p = (const unsigned char *) str; while (*p) { hash += *p++; hash += (hash << 10); hash ^= (hash >> 6); } hash += (hash << 3); hash ^= (hash >> 11); hash += (hash << 15); return hash; } #include static unsigned int mhash(const char *str) { unsigned char md5[40]; MD5((const unsigned char *)str, strlen(str), md5); return md5[0] | (md5[1] << 8) | (md5[2] << 16) | (md5[3] << 24); } int main(int argc, char *argv[]) { assert(argc == 3); unsigned int (*hashfunc)(const char *str); switch (*argv[1]) { case 'b': hashfunc = bhash; break; case 'j': hashfunc = jhash; break; case 'm': hashfunc = mhash; break; default: assert("hashfunc" == NULL); break; } int bpp = atoi(argv[2]); assert(bpp > 0 && bpp <= 32); char *line = NULL; size_t alloc_size = 0; ssize_t len; while ((len = getline(&line, &alloc_size, stdin)) >= 0) { if (len > 0 && line[len-1] == '\n') line[--len] = '\0'; if (len == 0) continue; unsigned int hash = hashfunc(line); if (bpp < 32) hash &= (1 << bpp) - 1; printf("%s\t%08x\n", line, hash); } return 0; }