dirhash.c revision 1acb01b4e28067a37c4f41e0a76df9f429fb417e
1/* 2 * dirhash.c -- Calculate the hash of a directory entry 3 * 4 * Copyright (c) 2001 Daniel Phillips 5 * 6 * Copyright (c) 2002 Theodore Ts'o. 7 * 8 * %Begin-Header% 9 * This file may be redistributed under the terms of the GNU Public 10 * License. 11 * %End-Header% 12 */ 13 14#include <stdio.h> 15 16#include "ext2_fs.h" 17#include "ext2fs.h" 18 19static ext2_dirhash_t dx_hack_hash (const char *name, int len) 20{ 21 __u32 hash0 = 0x12a3fe2d, hash1 = 0x37abe8f9; 22 while (len--) { 23 __u32 hash = hash1 + (hash0 ^ (*name++ * 7152373)); 24 25 if (hash & 0x80000000) hash -= 0x7fffffff; 26 hash1 = hash0; 27 hash0 = hash; 28 } 29 return (hash0 << 1); 30} 31 32/* 33 * Returns the hash of a filename. If len is 0 and name is NULL, then 34 * this function can be used to test whether or not a hash version is 35 * supported. 36 */ 37errcode_t ext2fs_dirhash(int version, const char *name, int len, 38 ext2_dirhash_t *ret_hash) 39{ 40 __u32 hash; 41 42 if (version == 0) 43 hash = dx_hack_hash(name, len); 44 else { 45 *ret_hash = 0; 46 return EXT2_ET_DIRHASH_UNSUPP; 47 } 48 *ret_hash = hash; 49 return 0; 50 51} 52