Skip to content

Commit

Permalink
utils: implement fnv32 hash
Browse files Browse the repository at this point in the history
  • Loading branch information
alexyer committed Jan 29, 2016
1 parent d94b7c6 commit 0e2e8d6
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/demo/demo.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
*/
#include "demo.h"

/* //////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -30,7 +30,7 @@ typedef struct __tb_demo_t
*/

// the demos
static tb_demo_t g_demo[] =
static tb_demo_t g_demo[] =
{
// libc
TB_DEMO_MAIN_ITEM(libc_time)
Expand Down Expand Up @@ -92,6 +92,7 @@ static tb_demo_t g_demo[] =
, TB_DEMO_MAIN_ITEM(utils_base32)
, TB_DEMO_MAIN_ITEM(utils_base64)
, TB_DEMO_MAIN_ITEM(utils_adler32)
, TB_DEMO_MAIN_ITEM(utils_fnv32)

// other
, TB_DEMO_MAIN_ITEM(other_test)
Expand Down Expand Up @@ -219,7 +220,7 @@ tb_int_t main(tb_int_t argc, tb_char_t** argv)
if (!tb_init(tb_null, tb_null)) return 0;
#endif

// init
// init
tb_int_t ok = 0;
tb_char_t const* name = tb_null;

Expand Down
1 change: 1 addition & 0 deletions src/demo/demo.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ TB_DEMO_MAIN_DECL(utils_option);
TB_DEMO_MAIN_DECL(utils_base32);
TB_DEMO_MAIN_DECL(utils_base64);
TB_DEMO_MAIN_DECL(utils_adler32);
TB_DEMO_MAIN_DECL(utils_fnv32);

// other
TB_DEMO_MAIN_DECL(other_test);
Expand Down
17 changes: 17 additions & 0 deletions src/demo/utils/fnv32.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "../demo.h"

/* //////////////////////////////////////////////////////////////////////////////////////
* main
*/
tb_int_t tb_demo_utils_fnv32_main(tb_int_t argc, tb_char_t** argv)
{
// data
tb_byte_t* data = (tb_byte_t*)argv[1];

// trace
tb_printf("[fnv32]: %x\n", tb_fnv32_encode(data));
return 0;
}
52 changes: 52 additions & 0 deletions src/tbox/utils/fnv32.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*!The Treasure Box Library
*
* TBox is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* TBox is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with TBox;
* If not, see <a href="http://www.gnu.org/licenses/"> http://www.gnu.org/licenses/</a>
*
* Copyright (C) 2016, Olexander Yermakov All rights reserved.
*
* @author alexyer
* @file fnv32.c
* @ingroup utils
*
*/

/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "fnv32.h"

/* //////////////////////////////////////////////////////////////////////////////////////
* macros
*/

// FNV prime
#define FNV_32_PRIME (0x01000193)
#define FNV0 (0x811c9dc5)

/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
tb_uint32_t tb_fnv32_encode(tb_byte_t const* data)
{
tb_uint32_t hval = FNV0;

while (*data)
{
hval *= FNV_32_PRIME;
hval ^= (unsigned int)*data++;
}

return hval;
}
54 changes: 54 additions & 0 deletions src/tbox/utils/fnv32.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*!The Treasure Box Library
*
* TBox is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* TBox is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with TBox;
* If not, see <a href="http://www.gnu.org/licenses/"> http://www.gnu.org/licenses/</a>
*
* Copyright (C) 2016, Olexander Yermakov All rights reserved.
*
* @author alexyer
* @file fnv32.h
* @ingroup utils
*
*/
#ifndef TB_UTILS_FNV32_H
#define TB_UTILS_FNV32_H

/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"

/* //////////////////////////////////////////////////////////////////////////////////////
* extern
*/
__tb_extern_c_enter__

/* //////////////////////////////////////////////////////////////////////////////////////
* interfaces
*/

/*! encode fnv32
*
* @param data the data
*
* @return the fnv32 value
*/
tb_uint32_t tb_fnv32_encode(tb_byte_t const* data);

/* //////////////////////////////////////////////////////////////////////////////////////
* extern
*/
__tb_extern_c_leave__

#endif
1 change: 1 addition & 0 deletions src/tbox/utils/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,6 @@
#include "adler32.h"
#include "singleton.h"
#include "lock_profiler.h"
#include "fnv32.h"

#endif

0 comments on commit 0e2e8d6

Please sign in to comment.