1/* calc-xxh: calculate XXH32 hashes for name and value strings */
2
3#include <inttypes.h>
4#include <stdint.h>
5#include <stdio.h>
6#include <stdlib.h>
7#include <string.h>
8
9#include XXH_HEADER_NAME
10
11int
12main (int argc, char **argv)
13{
14    uint32_t seed, name_hash, nameval_hash;
15    size_t name_len, value_len;
16    const char *name, *value;
17    XXH32_state_t hash_state;
18    int mode;
19
20    if (argc != 5)
21    {
22        fprintf(stderr,
23"Usage: %s mode seed name value\n"
24"\n"
25"   mode is one of:\n"
26"       0   Single seed, update\n"
27"       1   Use name hash as seed for calculating nameval hash\n"
28"\n"
29"   seed is the initial seed\n"
30"\n", argv[0]);
31        return 0;
32    }
33
34    mode = atoi(argv[1]);
35    if (!(mode == 0 || mode == 1))
36    {
37        fprintf(stderr, "mode `%s' is invalid\n", argv[1]);
38        return 1;
39    }
40
41    seed = atoi(argv[2]);
42    name = argv[3];
43    value = argv[4];
44    name_len = strlen(name);
45    value_len = strlen(value);
46
47    switch (mode)
48    {
49    case 0:
50        XXH32_reset(&hash_state, seed);
51        XXH32_update(&hash_state, name, name_len);
52        name_hash = XXH32_digest(&hash_state);
53        XXH32_update(&hash_state, value, value_len);
54        nameval_hash = XXH32_digest(&hash_state);
55        break;
56    default:
57        name_hash = XXH32(name, name_len, seed);
58        nameval_hash = XXH32(value, value_len, name_hash);
59        break;
60    }
61
62    printf(
63"In mode %d, name: `%s', value: `%s' hash to:\n"
64"name hash 0x%08"PRIX32"\n"
65"nameval hash 0x%08"PRIX32"\n"
66        , mode, name, value, name_hash, nameval_hash);
67
68    return 0;
69}
70