1 /****************************************************************************** 2 * 3 * Copyright (C) 2014 Google, Inc. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at: 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 ******************************************************************************/ 18 19 #include <string.h> 20 21 #include "osi/hash_functions.h" 22 hash_function_naive(const void * key)23hash_index_t hash_function_naive(const void *key) 24 { 25 return (hash_index_t)key; 26 } 27 hash_function_integer(const void * key)28hash_index_t hash_function_integer(const void *key) 29 { 30 return ((hash_index_t)key) * 2654435761; 31 } 32 hash_function_pointer(const void * key)33hash_index_t hash_function_pointer(const void *key) 34 { 35 return ((hash_index_t)key) * 2654435761; 36 } 37 hash_function_string(const void * key)38hash_index_t hash_function_string(const void *key) 39 { 40 hash_index_t hash = 5381; 41 const char *name = (const char *)key; 42 size_t string_len = strlen(name); 43 for (size_t i = 0; i < string_len; ++i) { 44 hash = ((hash << 5) + hash ) + name[i]; 45 } 46 return hash; 47 } 48 hash_function_blob(const unsigned char * s,unsigned int len,hash_key_t h)49void hash_function_blob(const unsigned char *s, unsigned int len, hash_key_t h) 50 { 51 size_t j; 52 53 while (len--) { 54 j = sizeof(hash_key_t)-1; 55 56 while (j) { 57 h[j] = ((h[j] << 7) | (h[j-1] >> 1)) + h[j]; 58 --j; 59 } 60 61 h[0] = (h[0] << 7) + h[0] + *s++; 62 } 63 } 64