diff --git a/src/demo/demo.c b/src/demo/demo.c index 244b3a5a0..75af0e6e4 100755 --- a/src/demo/demo.c +++ b/src/demo/demo.c @@ -1,6 +1,6 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * includes - */ + */ #include "demo.h" /* ////////////////////////////////////////////////////////////////////////////////////// @@ -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) @@ -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) @@ -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; diff --git a/src/demo/demo.h b/src/demo/demo.h index b25a6c4f3..64212ee4d 100755 --- a/src/demo/demo.h +++ b/src/demo/demo.h @@ -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); diff --git a/src/demo/utils/fnv32.c b/src/demo/utils/fnv32.c new file mode 100755 index 000000000..787e5be19 --- /dev/null +++ b/src/demo/utils/fnv32.c @@ -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; +} diff --git a/src/tbox/utils/fnv32.c b/src/tbox/utils/fnv32.c new file mode 100755 index 000000000..cd2b2c473 --- /dev/null +++ b/src/tbox/utils/fnv32.c @@ -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 http://www.gnu.org/licenses/ + * + * 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; +} diff --git a/src/tbox/utils/fnv32.h b/src/tbox/utils/fnv32.h new file mode 100755 index 000000000..c5ca47a03 --- /dev/null +++ b/src/tbox/utils/fnv32.h @@ -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 http://www.gnu.org/licenses/ + * + * 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 diff --git a/src/tbox/utils/utils.h b/src/tbox/utils/utils.h index 8be786da1..9b3393fdf 100755 --- a/src/tbox/utils/utils.h +++ b/src/tbox/utils/utils.h @@ -42,5 +42,6 @@ #include "adler32.h" #include "singleton.h" #include "lock_profiler.h" +#include "fnv32.h" #endif