Contiki 2.5
hash_xxfast.c
Go to the documentation of this file.
1 /**
2  * \addtogroup hash
3  * @{
4  */
5 
6 /**
7  * \defgroup hash_xxfast Hashing implementation using the xxFAST algorithm
8  *
9  * @{
10  */
11 
12 /**
13  * \file
14  * \brief Implementation of the hashing interface based on the xxFAST algorithm
15  * \author Yann Collet
16  * \author Wolf-Bastian Poettner <poettner@ibr.cs.tu-bs.de>
17  */
18 
19 /*
20  xxHash - Fast Hash algorithm
21  Copyright (C) 2012, Yann Collet.
22  BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
23 
24  Redistribution and use in source and binary forms, with or without
25  modification, are permitted provided that the following conditions are
26  met:
27 
28  * Redistributions of source code must retain the above copyright
29  notice, this list of conditions and the following disclaimer.
30  * Redistributions in binary form must reproduce the above
31  copyright notice, this list of conditions and the following disclaimer
32  in the documentation and/or other materials provided with the
33  distribution.
34 
35  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
46 
47  You can contact the author at :
48  - xxHash source repository : http://code.google.com/p/xxhash/
49 */
50 
51 #include <stdio.h>
52 #include <string.h> // memcpy
53 
54 #include "hash.h"
55 
56 #define _rotl(x,r) ((x << r) | (x >> (32 - r)))
57 
58 #define PRIME1 2654435761U
59 #define PRIME2 2246822519U
60 #define PRIME3 3266489917U
61 #define PRIME4 668265263U
62 #define PRIME5 0x165667b1
63 
64 void hash_xxfast_init() {
65 }
66 
67 uint32_t hash_xxfast_buffer(uint8_t * buffer, uint16_t length)
68 {
69  uint8_t * p = buffer;
70  uint8_t * bEnd = p + length;
71  uint8_t * limit = bEnd - 4;
72  uint32_t idx = PRIME1;
73  uint32_t crc = PRIME5;
74 
75  while (p<limit)
76  {
77  crc += ((*(uint32_t *)p) + idx++);
78  crc += _rotl(crc, 17) * PRIME4;
79  crc *= PRIME1;
80  p+=4;
81  }
82 
83  while (p<bEnd)
84  {
85  crc += ((*p) + idx++);
86  crc *= PRIME1;
87  p++;
88  }
89 
90  crc += length;
91 
92  crc ^= crc >> 15;
93  crc *= PRIME2;
94  crc ^= crc >> 13;
95  crc *= PRIME3;
96  crc ^= crc >> 16;
97 
98  return crc;
99 }
100 
101 uint32_t hash_xxfast_convenience(uint32_t one, uint32_t two, uint32_t three, uint32_t four, uint32_t five)
102 {
103  uint8_t buffer[20];
104 
105  memcpy(buffer + 0, &one, sizeof(uint32_t));
106  memcpy(buffer + 4, &two, sizeof(uint32_t));
107  memcpy(buffer + 8, &three, sizeof(uint32_t));
108  memcpy(buffer + 12, &four, sizeof(uint32_t));
109  memcpy(buffer + 16, &five, sizeof(uint32_t));
110 
111  return hash_xxfast_buffer(buffer, 16);
112 }
113 
114 uint32_t hash_xxfast_convenience_ptr(uint32_t * one, uint32_t * two, uint32_t * three, uint32_t * four, uint32_t * five)
115 {
116  uint8_t buffer[20];
117 
118  memcpy(buffer + 0, one, sizeof(uint32_t));
119  memcpy(buffer + 4, two, sizeof(uint32_t));
120  memcpy(buffer + 8, three, sizeof(uint32_t));
121  memcpy(buffer + 12, four, sizeof(uint32_t));
122  memcpy(buffer + 16, five, sizeof(uint32_t));
123 
124  return hash_xxfast_buffer(buffer, 16);
125 }
126 
127 const struct hash_driver hash_xxfast = {
128  "xxFAST",
129  hash_xxfast_init,
130  hash_xxfast_convenience,
131  hash_xxfast_convenience_ptr,
132  hash_xxfast_buffer,
133 };
134 
135 /** @} */
136 /** @} */