/* Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <b.h>

static b_error_t b_hash_new_cache (b_t * data, unsigned char *what,
				   b_uint64 len, unsigned char *where,
				   char *status);
static b_error_t b_hash_destroy_single (b_hash_t ** cache_root,
					b_uint64 * numb, b_hash_t * cache);

b_error_t
b_hash_new (b_t * data, unsigned char *what, b_uint64 len,
	    unsigned char *where)
{
  char status;
  b_hash_t *hash;
  b_error_t error;

  B_LOG4 ("b_hash_new: %p %p %d %p", data, what, (int) len, where);

  if (!what || !where)
    return B_ERROR_DATA;

  len = len > 0 ? len : strlen ((char *) what);

  if ((error = b_hash_new_cache (data, what, len, where, &status)) != B_OK)
    return error;

  if (status)
    return B_OK;

  b_MD5 (what, len, where);

  if (!(hash = (b_hash_t *) calloc (1, sizeof (b_hash_t))))
    return B_ERROR_MEMORY;

  hash->what = b_string_alloc (what, len);
  hash->len = len;

  b_hash_copy (hash->hash, where);

  if (data->hashes_cache_items)
    {
      hash->next = data->hashes_cache;
      data->hashes_cache->prev = hash;
    }

  data->hashes_cache = hash;
  data->hashes_cache_items++;

  if (data->hashes_cache_items >= data->max_cache)
    {
      hash = data->hashes_cache;

      while (hash->next)
	hash = hash->next;

      b_hash_destroy_single (&data->hashes_cache, &data->hashes_cache_items,
			     hash);
    }

  return B_OK;
}

static b_error_t
b_hash_new_cache (b_t * data, unsigned char *what, b_uint64 len,
		  unsigned char *where, char *status)
{
  b_hash_t *h;

  B_LOG5 ("b_hash_new_cache: %p %p %d %p %p", data, what, (int) len, where,
	  status);

  for (h = data->hashes_cache; h; h = h->next)
    {
      if (len == h->len && !memcmp (what, h->what, (size_t) len))
	{
	  b_hash_copy (where, h->hash);
	  *status = 1;
	  return B_OK;
	}
    }

  *status = 0;
  return B_OK;
}

b_error_t
b_hash_copy (unsigned char *dst, unsigned char *src)
{
  if (!dst || !src)
    return B_ERROR_DATA;

  B_LOG2 ("b_hash_copy: %p %p", dst, src);

  memcpy (dst, src, B_HASH_SIZE);
  return B_OK;
}

int
b_hash_compare (unsigned char *a, unsigned char *b)
{
  B_LOG2 ("b_hash_compare: %p %p", a, b);

  return memcmp (a, b, B_HASH_SIZE);
}

int
b_hash_empty (unsigned char *a)
{
  int i;

  B_LOG1 ("b_hash_empty: %p", a);

  if (!a)
    return 0;

  for (i = 0; i < B_HASH_SIZE; i++)
    if (a[i])
      return 1;

  return 0;
}

b_error_t
b_hash_zero (unsigned char *a)
{
  B_LOG1 ("b_hash_zero: %p", a);

  if (!a)
    return B_ERROR_DATA;

  memset (a, 0, B_HASH_SIZE);
  return B_OK;
}

b_error_t
b_hash_print (FILE * file, unsigned char *hash)
{
  int i;

  B_LOG2 ("b_hash_print: %p %p", file, hash);

  if (!file || !hash)
    return B_ERROR_DATA;

  for (i = 0; i < B_HASH_SIZE; i++)
    fprintf (file, "%.2X", hash[i]);
  fprintf (file, "\n");

  return B_OK;
}

/* Destroy the hashes cache list: */
b_error_t
b_hash_destroy (b_hash_t ** list, b_uint64 * len)
{
  B_LOG2 ("b_hash_destroy: %p %p", list, len);

  if (!list || !*list)
    return B_ERROR_DATA;

  while (*list)
    b_hash_destroy_single (list, len, *list);

  return B_OK;
}

/* This function destroies a hashes cache item: */
static b_error_t
b_hash_destroy_single (b_hash_t ** cache_root, b_uint64 * numb,
		       b_hash_t * cache)
{
  B_LOG3 ("b_hash_destroy_single: %p %p %p", cache_root, numb, cache);

  if (!cache)
    return B_ERROR_DATA;

  if (cache == *cache_root)
    *cache_root = cache->next;

  if (cache->prev)
    cache->prev->next = cache->next;

  if (cache->next)
    cache->next->prev = cache->prev;

  if (cache->what)
    free (cache->what);

  free (cache);

  if (numb)
    (*numb)--;

  return B_OK;
}

/* EOF */
