Contiki 2.5
hash_xor.c
Go to the documentation of this file.
1 /**
2  * \addtogroup hash
3  * @{
4  */
5 
6 /**
7  * \defgroup hash_xor 'Hashing' implementation using the XOR function
8  *
9  * @{
10  */
11 
12 /**
13  * \file
14  * \brief Implementation of the hashing interface based on the XOR function
15  * \author Wolf-Bastian Poettner <poettner@ibr.cs.tu-bs.de>
16  */
17 
18 #include <stdio.h>
19 #include <string.h> // memcpy
20 
21 #include "hash.h"
22 
23 void hash_xor_init() {
24 }
25 
26 uint32_t hash_xor_buffer(uint8_t * buffer, uint16_t length)
27 {
28  int i;
29  int limit = (length / 4) * 4;
30  uint32_t hash = 0;
31 
32  for( i=0; i < limit; i = i + 4) {
33  uint32_t * value = (uint32_t* ) &buffer[i];
34  hash ^= *value;
35  }
36 
37  if( limit < length ) {
38  uint32_t remainder = 0;
39  memcpy(&remainder, &buffer[limit], length - limit);
40  hash ^= remainder;
41  }
42 
43  return hash;
44 }
45 
46 uint32_t hash_xor_convenience(uint32_t one, uint32_t two, uint32_t three, uint32_t four, uint32_t five)
47 {
48  uint8_t buffer[20];
49 
50  memcpy(buffer + 0, &one, sizeof(uint32_t));
51  memcpy(buffer + 4, &two, sizeof(uint32_t));
52  memcpy(buffer + 8, &three, sizeof(uint32_t));
53  memcpy(buffer + 12, &four, sizeof(uint32_t));
54  memcpy(buffer + 16, &five, sizeof(uint32_t));
55 
56  return hash_xor_buffer(buffer, 16);
57 }
58 
59 uint32_t hash_xor_convenience_ptr(uint32_t * one, uint32_t * two, uint32_t * three, uint32_t * four, uint32_t * five)
60 {
61  uint8_t buffer[20];
62 
63  memcpy(buffer + 0, one, sizeof(uint32_t));
64  memcpy(buffer + 4, two, sizeof(uint32_t));
65  memcpy(buffer + 8, three, sizeof(uint32_t));
66  memcpy(buffer + 12, four, sizeof(uint32_t));
67  memcpy(buffer + 16, five, sizeof(uint32_t));
68 
69  return hash_xor_buffer(buffer, 16);
70 }
71 
72 const struct hash_driver hash_xor = {
73  "XOR",
74  hash_xor_init,
75  hash_xor_convenience,
76  hash_xor_convenience_ptr,
77  hash_xor_buffer,
78 };
79 
80 /** @} */
81 /** @} */