1 /*
2  * hashkey.h -- definition of hash key type and helper functions
3  *
4  * Copyright (C) 2010-2011 Olaf Bergmann <bergmann@tzi.org>
5  *
6  * This file is part of the CoAP library libcoap. Please see README for terms
7  * of use.
8  */
9 
10 /**
11  * @file hashkey.h
12  * @brief definition of hash key type and helper functions
13  */
14 
15 #ifndef _COAP_HASHKEY_H_
16 #define _COAP_HASHKEY_H_
17 
18 #include "str.h"
19 
20 typedef unsigned char coap_key_t[4];
21 
22 #ifndef coap_hash
23 /**
24  * Calculates a fast hash over the given string @p s of length @p len and stores
25  * the result into @p h. Depending on the exact implementation, this function
26  * cannot be used as one-way function to check message integrity or simlar.
27  *
28  * @param s   The string used for hash calculation.
29  * @param len The length of @p s.
30  * @param h   The result buffer to store the calculated hash key.
31  */
32 void coap_hash_impl(const unsigned char *s, unsigned int len, coap_key_t h);
33 
34 #define coap_hash(String,Length,Result) \
35   coap_hash_impl((String),(Length),(Result))
36 
37 /* This is used to control the pre-set hash-keys for resources. */
38 #define __COAP_DEFAULT_HASH
39 #else
40 #undef __COAP_DEFAULT_HASH
41 #endif /* coap_hash */
42 
43 /**
44  * Calls coap_hash() with given @c str object as parameter.
45  *
46  * @param Str Must contain a pointer to a coap string object.
47  * @param H   A coap_key_t object to store the result.
48  *
49  * @hideinitializer
50  */
51 #define coap_str_hash(Str,H) {               \
52     assert(Str);                             \
53     memset((H), 0, sizeof(coap_key_t));      \
54     coap_hash((Str)->s, (Str)->length, (H)); \
55   }
56 
57 #endif /* _COAP_HASHKEY_H_ */
58