Blame gl/hash-pjw-bare.c

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