/* omflibut.c (emx+gcc) */

/* Utility functions for dealing with OMFLIBs.

Copyright (c) 1993 Eberhard Mattes

This file is part of the emx OMFLIB library.  This library is free
software; you can redistribute it and/or modify it under the terms of
the GNU Library General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your
option) any later version.  This library is distributed in the hope
that it will be useful, but WITHOUT ANY WARRANTY; without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.  See the GNU Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free Software
Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

As a special exception, programs written by Eberhard Mattes can use
this library without restrictions unless changed by someone else. */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "omflib0.h"
#include "omflib.h"

int omflib_set_error (char *error)
{
  strcpy (error, strerror (errno));
  return (-1);
}


int omflib_close (struct omflib *p, char *error)
{
  int i;

  fclose (p->f);
  if (p->dict != NULL)
    free (p->dict);
  if (p->mod_tab != NULL)
    {
      for (i = 0; i < p->mod_count; ++i)
        free (p->mod_tab[i].name);
      free (p->mod_tab);
    }
  if (p->pub_tab != NULL)
    {
      for (i = 0; i < p->pub_count; ++i)
        free (p->pub_tab[i].name);
      free (p->pub_tab);
    }
  free (p);
  return (0);
}


#define ROL2(x) (((unsigned)(x) << 2) | ((unsigned)(x) >> 14))
#define ROR2(x) (((unsigned)(x) >> 2) | ((unsigned)(x) << 14))

void omflib_hash (struct omflib *p, const byte *name)
{
  int i, len;
  word block_index, bucket_index, block_index_delta, bucket_index_delta;
  byte c;

  len = name[0];
  block_index = 0;
  block_index_delta = 0;
  bucket_index = 0;
  bucket_index_delta = 0;
  for (i = 0; i < len; ++i)
    {
      c = name[i] | 0x20;
      block_index = ROL2 (block_index) ^ c;
      bucket_index_delta = ROR2 (bucket_index_delta) ^ c;
      c = name[len-i] | 0x20;
      bucket_index = ROR2 (bucket_index) ^ c;
      block_index_delta = ROL2 (block_index_delta) ^ c;
    }
  p->block_index = block_index % p->dict_blocks;
  p->block_index_delta = block_index_delta % p->dict_blocks;
  if (p->block_index_delta == 0)
    p->block_index_delta = 1;
  p->bucket_index = bucket_index % 37;
  p->bucket_index_delta = bucket_index_delta % 37;
  if (p->bucket_index_delta == 0)
    p->bucket_index_delta = 1;
}


void omflib_module_name (char *dst, const char *src)
{
  const char *base, *np;
  char *s;

  base = src;
  for (np = src; *np != 0; ++np)
    if (*np == ':' || *np == '\\' || *np == '/')
      base = np + 1;
  _strncpy (dst, base, 256);
  s = _getext (dst);
  if (s != NULL) *s = 0;
}
