varnish-cache/lib/libvmod_directors/shard_hash.c
1
/*-
2
 * Copyright 2009-2013 UPLEX - Nils Goroll Systemoptimierung
3
 * All rights reserved.
4
 *
5
 * Authors: Nils Goroll <nils.goroll@uplex.de>
6
 *          Geoffrey Simmons <geoff.simmons@uplex.de>
7
 *          Julian Wiesener <jw@uplex.de>
8
 *
9
 * Redistribution and use in source and binary forms, with or without
10
 * modification, are permitted provided that the following conditions
11
 * are met:
12
 * 1. Redistributions of source code must retain the above copyright
13
 *    notice, this list of conditions and the following disclaimer.
14
 * 2. Redistributions in binary form must reproduce the above copyright
15
 *    notice, this list of conditions and the following disclaimer in the
16
 *    documentation and/or other materials provided with the distribution.
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
22
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28
 * SUCH DAMAGE.
29
 */
30
31
#include "config.h"
32
33
#include <string.h>
34
35
#include "cache/cache.h"
36
37
#include "vsha256.h"
38
#include "vend.h"
39
40
#include "shard_parse_vcc_enums.h"
41
#include "shard_hash.h"
42
43
/*
44
 * XXX use the crc32 from libvgz, but declare it here to avoid an include
45
 * dependency nightmare (at least for now)
46
 */
47
48
unsigned long crc32(unsigned long, const unsigned char *buf, unsigned len);
49
50
static uint32_t v_matchproto_(hash_func)
51 18
shard_hash_crc32(VCL_STRING s)
52
{
53
        uint32_t crc;
54 18
        crc = crc32(~0U, (const unsigned char *)s, strlen(s));
55 18
        crc ^= ~0U;
56 18
        return (crc);
57
}
58
59
static uint32_t v_matchproto_(hash_func)
60 833
shard_hash_sha256(VCL_STRING s)
61
{
62
        struct VSHA256Context sha256;
63
        union {
64
                unsigned char digest[32];
65
                uint32_t uint32_digest[8];
66
        } sha256_digest;
67
        uint32_t r;
68
69 833
        VSHA256_Init(&sha256);
70 833
        VSHA256_Update(&sha256, s, strlen(s));
71 833
        VSHA256_Final(sha256_digest.digest, &sha256);
72
73
        /*
74
         * use low 32 bits only
75
         * XXX: Are these the best bits to pick?
76
         */
77 833
        vle32enc(&r, sha256_digest.uint32_digest[7]);
78 833
        return (r);
79
}
80
81
static uint32_t v_matchproto_(hash_func)
82 4
shard_hash_rs(VCL_STRING s)
83
{
84 4
        uint32_t res = 0;
85
        /* hash function from Robert Sedgwicks 'Algorithms in C' book */
86 4
        const uint32_t b    = 378551;
87 4
        uint32_t a          = 63689;
88
89 44
        while (*s) {
90 36
                res = res * a + (*s++);
91 36
                a *= b;
92
        }
93
94 4
        return (res);
95
}
96
97
static uint32_t v_matchproto_(hash_func)
98 0
_shard_hash_invalid(VCL_STRING s)
99
{
100
        (void)s;
101 0
        WRONG("invalid hash fp _ALG_E_ENVALID");
102
        NEEDLESS(return(0));
103
}
104
105
const hash_func shard_hash_f[_ALG_E_MAX] = {
106
        [_ALG_E_INVALID] = _shard_hash_invalid,
107
        [CRC32]  = shard_hash_crc32,
108
        [SHA256]         = shard_hash_sha256,
109
        [RS]             = shard_hash_rs
110
};