{"id":1751,"date":"2024-11-22T19:42:26","date_gmt":"2024-11-22T11:42:26","guid":{"rendered":"https:\/\/www.madbull.site\/?p=1751"},"modified":"2024-11-23T10:21:32","modified_gmt":"2024-11-23T02:21:32","slug":"xxhash-%e5%bf%ab%e9%80%9f%e8%ae%a1%e7%ae%97%e5%93%88%e5%b8%8c%e7%9a%84c%e8%af%ad%e8%a8%80%e5%ba%93","status":"publish","type":"post","link":"https:\/\/www.madbull.site\/?p=1751","title":{"rendered":"xxhash&#8211;\u5feb\u901f\u8ba1\u7b97\u54c8\u5e0c\u7684C\u8bed\u8a00\u5e93"},"content":{"rendered":"\n<p class=\"has-large-font-size\"><strong>\u7b80\u4ecb<\/strong><\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>\u5206\u4eab\u4e00\u4e2a\u5feb\u901f\u8ba1\u7b97HASH\u7684\u5f00\u6e90C\u8bed\u8a00\u5e93&#8211;xxhash<\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>\u6e90\u4ee3\u7801\u5730\u5740\uff1a<a href=\"https:\/\/github.com\/Cyan4973\/xxHash\">https:\/\/github.com\/Cyan4973\/xxHash<\/a><\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>xxHash \u662f\u4e00\u79cd\u6781\u5feb\u7684\u54c8\u5e0c\u7b97\u6cd5\uff0c\u5b83\u7684\u9650\u5236\u5728\u4e8e RAM \u7684\u901f\u5ea6\u3002 \u4ee3\u7801\u5177\u6709\u9ad8\u5ea6\u53ef\u79fb\u690d\u6027\uff0c\u5e76\u5728\u6240\u6709\u5e73\u53f0\u4e0a\u751f\u6210\u76f8\u540c\u7684\u54c8\u5e0c\u503c\uff08\u5c0f\u7aef\/\u5927\u7aef\uff09\u3002 \u8be5\u5e93\u5305\u62ec\u4ee5\u4e0b\u7b97\u6cd5\uff1a<\/p>\n\n\n\n<div style=\"margin-top:0;margin-bottom:0;height:13px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<ul class=\"wp-block-list\">\n<li>XXH32 \uff1a\u4f7f\u7528 32bits \u8ba1\u7b97\uff0c\u751f\u6210 32bits \u54c8\u5e0c<\/li>\n\n\n\n<li>XXH64 \uff1a\u4f7f\u7528 64bits \u8ba1\u7b97\uff0c\u751f\u6210 64bits \u54c8\u5e0c<\/li>\n\n\n\n<li>XXH3 \uff08\u4ece <code>v0.8.0<\/code> \u5f00\u59cb\uff09\uff1a\u4f7f\u7528\u77e2\u91cf\u5316\u7b97\u672f\u751f\u6210 64bits \u6216 128bits \u54c8\u5e0c\u3002 128 \u4f4d\u79f0\u4e3a XXH128\u3002<\/li>\n<\/ul>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"has-large-font-size\"><strong>\u7f16\u8bd1\u5b89\u88c5<\/strong><\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>\u4e0b\u8f7d\uff1a<code>git clone https:\/\/github.com\/Cyan4973\/xxHash.git<\/code><\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"672\" height=\"162\" src=\"https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/11\/\u56fe\u7247-27.png\" alt=\"\" class=\"wp-image-1752\" srcset=\"https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/11\/\u56fe\u7247-27.png 672w, https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/11\/\u56fe\u7247-27-300x72.png 300w\" sizes=\"auto, (max-width: 672px) 100vw, 672px\" \/><\/figure>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>\u7f16\u8bd1\u5b89\u88c5\uff1a<code>make -j 6 &amp;&amp; make install<\/code><\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--10);margin-bottom:var(--wp--preset--spacing--10);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1015\" height=\"430\" src=\"https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/11\/\u56fe\u7247-28.png\" alt=\"\" class=\"wp-image-1753\" srcset=\"https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/11\/\u56fe\u7247-28.png 1015w, https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/11\/\u56fe\u7247-28-300x127.png 300w, https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/11\/\u56fe\u7247-28-768x325.png 768w\" sizes=\"auto, (max-width: 1015px) 100vw, 1015px\" \/><\/figure>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"has-large-font-size\"><strong>\u793a\u4f8b<\/strong><\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>\u7ed9\u51fa\u7684\u793a\u4f8b\u662f\u63d0\u4f9b\u4e86 <code>XXH32_hash_t <\/code>\u548c <code>XXH64_hash_t <\/code>\u7c7b\u578b\u7684hash\u8ba1\u7b97\u4ee5\u53ca <code>XXH3 <\/code>\u4e2d <code>XXH64_hash_t <\/code>\u548c <code>XXH128_hash_t <\/code>\u7c7b\u578b\u7684 hash \u8ba1\u7b97\u3002<\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>\u8bfb\u53d6\u4e00\u4e2a\u7ed9\u5b9a\u7684\u6587\u4ef6\uff0c\u7136\u540e\u628a\u6587\u4ef6\u5185\u5bb9\u9010\u6b65\u8ba1\u7b97\u51fa\u5bf9\u5e94\u7684 hash \u503c\u3002<\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>\u6d4b\u8bd5\u4ee3\u7801txxh.c<\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--10);margin-bottom:var(--wp--preset--spacing--10);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ gcc -Wall -o test txxh.c -lxxhash\n\/\/ .\/test \/etc\/service\n\n#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n#include &lt;unistd.h&gt;\n#include &lt;xxhash.h&gt;\n\n\nXXH32_hash_t xxh32_compute_file_hash(const char *file_path) {\n    FILE *file = fopen(file_path, \"rb\");\n    if (file == NULL) {\n        perror(\"Failed to open file\");\n        exit(EXIT_FAILURE);\n    }\n\n    \/\/ \u521b\u5efa state\n    XXH32_state_t * state = XXH32_createState() ;\n    \/\/ \u91cd\u7f6e state\n    XXH32_reset (state, 0) ;\n\n    char buffer&#91;4096];\n    size_t bytes_read;\n    while ((bytes_read = fread(buffer, 1, sizeof(buffer), file)) &gt; 0) {\n        \/\/ \u8bfb\u6587\u4ef6\u9010\u6b65\u8ba1\u7b97\n        XXH32_update(state, buffer, bytes_read);\n    }\n\n    fclose(file);\n\n    \/\/ \u83b7\u53d6hash\u6570\u5b57\n    XXH32_hash_t xxh32 = XXH32_digest(state);\n    \/\/ \u91ca\u653e state\n    XXH32_freeState(state) ;\n\n    return xxh32 ;\n}\n\nXXH64_hash_t xxh64_compute_file_hash(const char *file_path) {\n    FILE *file = fopen(file_path, \"rb\");\n    if (file == NULL) {\n        perror(\"Failed to open file\");\n        exit(EXIT_FAILURE);\n    }\n\n    \/\/ \u521b\u5efa state\n    XXH64_state_t * state = XXH64_createState() ;\n    \/\/ \u91cd\u7f6e state\n    XXH64_reset (state, 0) ;\n\n    char buffer&#91;4096];\n    size_t bytes_read;\n    while ((bytes_read = fread(buffer, 1, sizeof(buffer), file)) &gt; 0) {\n        \/\/ \u8bfb\u6587\u4ef6\u9010\u6b65\u8ba1\u7b97\n        XXH64_update(state, buffer, bytes_read);\n    }\n\n    fclose(file);\n\n    \/\/ \u83b7\u53d6hash\u6570\u5b57\n    XXH64_hash_t xxh64 = XXH64_digest(state);\n    \/\/ \u91ca\u653e state\n    XXH64_freeState(state) ;\n\n    return xxh64 ;\n}\n\nXXH64_hash_t xxh3_64_compute_file_hash(const char *file_path) {\n    FILE *file = fopen(file_path, \"rb\");\n    if (file == NULL) {\n        perror(\"Failed to open file\");\n        exit(EXIT_FAILURE);\n    }\n\n    \/\/ \u521b\u5efa state\n    XXH3_state_t * state = XXH3_createState() ;\n    \/\/ \u91cd\u7f6e state\n    XXH3_64bits_reset(state) ;\n\n    char buffer&#91;4096];\n    size_t bytes_read;\n\n    while ((bytes_read = fread(buffer, 1, sizeof(buffer), file)) &gt; 0) {\n        \/\/ \u8bfb\u6587\u4ef6\u9010\u6b65\u8ba1\u7b97\n        XXH3_64bits_update(state, buffer, bytes_read);\n    }\n    fclose(file);\n\n\n    \/\/ \u83b7\u53d6hash\u6570\u5b57\n    XXH64_hash_t xxh64 = XXH3_64bits_digest(state) ;\n    \/\/ \u91ca\u653e state\n    XXH3_freeState(state) ;\n\n    return xxh64 ;\n}\n\nXXH128_hash_t xxh3_128_compute_file_hash(const char *file_path) {\n    FILE *file = fopen(file_path, \"rb\");\n    if (file == NULL) {\n        perror(\"Failed to open file\");\n        exit(EXIT_FAILURE);\n    }\n\n    \/\/ \u521b\u5efa state\n    XXH3_state_t * state = XXH3_createState() ;\n    \/\/ \u91cd\u7f6e state\n    XXH3_128bits_reset(state) ;\n\n    char buffer&#91;4096];\n    size_t bytes_read;\n\n    while ((bytes_read = fread(buffer, 1, sizeof(buffer), file)) &gt; 0) {\n        \/\/ \u8bfb\u6587\u4ef6\u9010\u6b65\u8ba1\u7b97\n        XXH3_128bits_update(state, buffer, bytes_read);\n    }\n    fclose(file);\n\n\n    \/\/ \u83b7\u53d6hash\u6570\u5b57\n    XXH128_hash_t xxh128 = XXH3_128bits_digest(state) ;\n    \/\/ \u91ca\u653e state\n    XXH3_freeState(state) ;\n\n    return xxh128 ;\n}\n\n\nint main(int argc, char *argv&#91;]) {\n    if (argc != 2) {\n        fprintf(stderr, \"Usage: %s &lt;file_path&gt;\\n\", argv&#91;0]);\n        exit(EXIT_FAILURE);\n    }\n\n    const char *file_path = argv&#91;1];\n\n    XXH32_hash_t hash32 = xxh32_compute_file_hash(file_path) ;\n    printf(\"32-bit hash of file '%s': %X\\n\", file_path, hash32);\n\n    XXH64_hash_t hash64 = xxh64_compute_file_hash(file_path);\n    printf(\"64-bit hash of file '%s': %lX\\n\", file_path, hash64);\n\n    XXH64_hash_t hash64_2 = xxh3_64_compute_file_hash(file_path) ;\n    printf(\"h3-64-bit hash of file '%s': %lX\\n\", file_path, hash64_2) ;\n\n    XXH128_hash_t hash128 = xxh3_128_compute_file_hash(file_path);\n    printf(\"h3-128-bit hash of file '%s': %lX %lX\\n\", file_path, hash128.high64, hash128.low64);\n\n    return 0 ;\n}<\/code><\/pre>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"has-large-font-size\"><strong>\u7f16\u8bd1\u6d4b\u8bd5<\/strong><\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--10);margin-bottom:var(--wp--preset--spacing--10);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"683\" height=\"470\" src=\"https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/11\/\u56fe\u7247-29.png\" alt=\"\" class=\"wp-image-1754\" srcset=\"https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/11\/\u56fe\u7247-29.png 683w, https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/11\/\u56fe\u7247-29-300x206.png 300w\" sizes=\"auto, (max-width: 683px) 100vw, 683px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\u5206\u4eab\u4e00\u4e2a\u5feb\u901f\u8ba1\u7b97HASH\u7684\u5f00\u6e90C\u8bed\u8a00\u5e93\u2013xxhash<\/p>\n<p>\u6e90\u4ee3\u7801\u5730\u5740\uff1ahttps:\/\/github.com\/Cyan4973\/xxHash<\/p>\n<p>xxHash \u662f\u4e00\u79cd\u6781\u5feb\u7684\u54c8\u5e0c\u7b97\u6cd5\uff0c\u5b83\u7684\u9650\u5236\u5728\u4e8e RAM \u7684\u901f\u5ea6\u3002 \u4ee3\u7801\u5177\u6709\u9ad8\u5ea6\u53ef\u79fb\u690d\u6027\uff0c\u5e76\u5728\u6240\u6709\u5e73\u53f0\u4e0a\u751f\u6210\u76f8\u540c\u7684\u54c8\u5e0c\u503c\uff08\u5c0f\u7aef\/\u5927\u7aef\uff09\u3002 \u8be5\u5e93\u5305\u62ec\u4ee5\u4e0b\u7b97\u6cd5\uff1a<\/p>\n<p>XXH32 \uff1a\u4f7f\u7528 32bits \u7b97\u672f\u751f\u6210 32bits \u54c8\u5e0c<br \/>\nXXH64 \uff1a\u4f7f\u7528 64bits \u7b97\u672f\u751f\u6210 64bits \u54c8\u5e0c<br \/>\nXXH3 \uff08\u4ece v0.8.0 \u5f00\u59cb\uff09\uff1a\u4f7f\u7528\u77e2\u91cf\u5316\u7b97\u672f\u751f\u6210 64bits \u6216 128bits \u54c8\u5e0c\u3002 128 \u4f4d\u79f0\u4e3a XXH128\u3002<\/p>\n","protected":false},"author":1,"featured_media":559,"comment_status":"open","ping_status":"open","sticky":false,"template":"single-with-sidebar","format":"standard","meta":{"footnotes":""},"categories":[156,154],"tags":[581,582,583,579,580,578],"class_list":["post-1751","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-c","category-154","tag-xxh128_hash_t","tag-xxh3","tag-xxh3_createstate","tag-xxh32_hash_t","tag-xxh64_hash_t","tag-xxhash"],"_links":{"self":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts\/1751","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1751"}],"version-history":[{"count":4,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts\/1751\/revisions"}],"predecessor-version":[{"id":1758,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts\/1751\/revisions\/1758"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/media\/559"}],"wp:attachment":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1751"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1751"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1751"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}