1 /* GStreamer
2  * Copyright (C) 2007 Thomas Vander Stichele <thomas at apestaart dot org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19 
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23 
24 #include <glib.h>
25 
26 #include "fnv1hash.h"
27 
28 /* This file implements FNV-1 hashing used in the Ogg payload encoders
29  * to generate the 24-bit ident value based on the header pages.
30  * See http://isthe.com/chongo/tech/comp/fnv/
31  */
32 
33 #define MASK_24 (((guint32) 1 << 24) -1)
34 
35 #define FNV1_HASH_32_INIT ((guint32) 0x811C9DC5L)
36 //2166136261L)
37 #define FNV1_HASH_32_PRIME 16777619
38 
39 guint32
fnv1_hash_32_new(void)40 fnv1_hash_32_new (void)
41 {
42   return FNV1_HASH_32_INIT;
43 }
44 
45 guint32
fnv1_hash_32_update(guint32 hash,const guchar * data,guint length)46 fnv1_hash_32_update (guint32 hash, const guchar * data, guint length)
47 {
48   guint i;
49   const guchar *p = data;
50 
51   for (i = 0; i < length; ++i, ++p) {
52     hash *= FNV1_HASH_32_PRIME;
53     hash ^= *p;
54   }
55 
56   return hash;
57 }
58 
59 guint32
fnv1_hash_32_to_24(guint32 hash)60 fnv1_hash_32_to_24 (guint32 hash)
61 {
62   return (hash >> 24) ^ (hash & MASK_24);
63 }
64