Created
February 27, 2011 21:58
-
-
Save xxx/846581 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string.h> | |
#include <stdint.h> | |
#include <stdlib.h> | |
#include <limits.h> | |
#include "hashcode.h" | |
int ipow(int base, int exp) { | |
int result = 1; | |
while (exp) { | |
if (exp & 1) | |
result *= base; | |
exp >>= 1; | |
base *= base; | |
} | |
return result; | |
} | |
int java_hash_code(const char *str) { | |
int sum = 0; | |
int len = strlen(str); | |
int i = len; | |
while(--i > -1) { | |
sum += str[i] * ipow(31, (len - (i + 1))); | |
} | |
return sum; | |
} | |
// http://blogs.sun.com/darcy/entry/string_unhashing?intcmp=2223 | |
char *java_unhash_code(int target) { | |
// leaky leaky | |
char *result = calloc(100, sizeof(char)); | |
if (target < 0) { | |
// "polygenelubricants" is known to hash to INT_MIN | |
strcat(result, "polygenelubricants"); | |
if (target == INT_MIN) | |
return result; | |
target &= INT_MAX; | |
} | |
unhash2(result, target); | |
return result; | |
} | |
// assumes target is positive | |
void unhash2(char *partial, int target) { | |
int div = target / 31; | |
int rem = target % 31; | |
int len = strlen(partial); | |
if (div <= CHAR_MAX) { | |
if (div != 0) { | |
partial[len] = div; | |
partial[++len] = '\0'; | |
} | |
partial[len] = rem; | |
partial[len + 1] = '\0'; | |
} else { | |
unhash2(partial, div); | |
len = strlen(partial); | |
partial[len] = rem; | |
partial[len + 1] = '\0'; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef _HASHCODE_H_ | |
#define _HASHCODE_H_ | |
int ipow(int base, int exp); | |
int java_hash_code(const char *str); | |
char *java_unhash_code(int target); | |
void unhash2(char *partial, int target); | |
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SHELL=/bin/sh | |
CC=clang | |
SOURCES=hashcode.c | |
OBJECTS=hashcode.o | |
LIBFILE=libhashcode.so | |
LIBVERSION=0 | |
SHARED=$(LIBFILE).0.0.1 | |
LINKS=$(LIBFILE) $(LIBFILE).$(LIBVERSION) | |
CC_OBJECTS_OPTS=-c -fPIC -O3 -Wall -Werror -pedantic-errors -std=c99 | |
CC_SHARED_OPTS=-shared -Wl,-soname,$(LIBFILE).$(LIBVERSION) | |
INSTALL_DIR=../../lib/ext | |
LINK_CMD=ln -fs | |
.PHONY: all install clean | |
all: $(SOURCES) $(OBJECTS) $(SHARED) | |
install: $(SHARED) | |
mkdir -p $(INSTALL_DIR) | |
install $(SHARED) $(INSTALL_DIR) | |
$(LINK_CMD) $(INSTALL_DIR)/$(SHARED) $(INSTALL_DIR)/$(LIBFILE).$(LIBVERSION) | |
$(LINK_CMD) $(INSTALL_DIR)/$(SHARED) $(INSTALL_DIR)/$(LIBFILE) | |
$(SHARED): $(OBJECTS) | |
$(CC) $(CC_SHARED_OPTS) -o $@ $(OBJECTS) | |
$(OBJECTS): $(SOURCES) | |
$(CC) $(CC_OBJECTS_OPTS) $(SOURCES) -o $@ | |
clean: | |
rm -f *.o *.so* core *.core |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment