vfs: check userland buffers before reading them.
[haiku.git] / headers / private / shared / HashString.h
blob26a0ab138f8163d2aca39c5a8125e611cd663b92
1 /*
2 * Copyright 2004-2007, Ingo Weinhold, bonefish@users.sf.net. All rights reserved.
3 * Distributed under the terms of the MIT License.
4 */
5 #ifndef HASH_STRING_H
6 #define HASH_STRING_H
8 #include <SupportDefs.h>
10 // string_hash
12 // from the Dragon Book: a slightly modified hashpjw()
13 static inline
14 uint32
15 string_hash(const char *name)
17 uint32 h = 0;
18 if (name) {
19 for (; *name; name++) {
20 uint32 g = h & 0xf0000000;
21 if (g)
22 h ^= g >> 24;
23 h = (h << 4) + *name;
26 return h;
29 #ifdef __cplusplus
31 namespace BPrivate {
33 // HashString
34 class HashString {
35 public:
36 HashString();
37 HashString(const HashString &string);
38 HashString(const char *string, int32 length = -1);
39 ~HashString();
41 bool SetTo(const char *string, int32 maxLength = -1);
42 void Unset();
44 void Truncate(int32 newLength);
46 const char *GetString() const;
47 int32 GetLength() const { return fLength; }
49 uint32 GetHashCode() const { return string_hash(GetString()); }
51 HashString &operator=(const HashString &string);
52 bool operator==(const HashString &string) const;
53 bool operator!=(const HashString &string) const { return !(*this == string); }
55 private:
56 bool _SetTo(const char *string, int32 length);
58 private:
59 int32 fLength;
60 char *fString;
63 } // namespace BPrivate
65 using BPrivate::HashString;
67 #endif // __cplusplus
69 #endif // HASH_STRING_H