Blame gl/hash-pjw-bare.c

Packit Service 4684c1
/* hash-pjw-bare.c -- compute a hash value from a provided buffer.
Packit Service 4684c1
Packit Service 4684c1
   Copyright (C) 2012-2020 Free Software Foundation, Inc.
Packit Service 4684c1
Packit Service 4684c1
   This program is free software: you can redistribute it and/or modify it
Packit Service 4684c1
   under the terms of the GNU Lesser General Public License as published
Packit Service 4684c1
   by the Free Software Foundation; either version 2.1 of the License, or
Packit Service 4684c1
   (at your option) any later version.
Packit Service 4684c1
Packit Service 4684c1
   This program is distributed in the hope that it will be useful,
Packit Service 4684c1
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 4684c1
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 4684c1
   Lesser General Public License for more details.
Packit Service 4684c1
Packit Service 4684c1
   You should have received a copy of the GNU Lesser General Public License
Packit Service 4684c1
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
Packit Service 4684c1
Packit Service 4684c1
#include <config.h>
Packit Service 4684c1
Packit Service 4684c1
#include "hash-pjw-bare.h"
Packit Service 4684c1
Packit Service 4684c1
#include <limits.h>
Packit Service 4684c1
Packit Service 4684c1
#define SIZE_BITS (sizeof (size_t) * CHAR_BIT)
Packit Service 4684c1
Packit Service 4684c1
/* Return a hash of the N bytes of X using the method described by
Packit Service 4684c1
   Bruno Haible in https://www.haible.de/bruno/hashfunc.html.
Packit Service 4684c1
   Note that while many hash functions reduce their result via modulo
Packit Service 4684c1
   to a 0..table_size-1 range, this function does not do that.  */
Packit Service 4684c1
Packit Service 4684c1
size_t
Packit Service 4684c1
hash_pjw_bare (const void *x, size_t n)
Packit Service 4684c1
{
Packit Service 4684c1
  const unsigned char *s = x;
Packit Service 4684c1
  size_t h = 0;
Packit Service 4684c1
  unsigned i;
Packit Service 4684c1
Packit Service 4684c1
  for (i = 0; i < n; i++)
Packit Service 4684c1
    h = s[i] + ((h << 9) | (h >> (SIZE_BITS - 9)));
Packit Service 4684c1
Packit Service 4684c1
  return h;
Packit Service 4684c1
}