ext/ezmlm/hash/hash.c
author Mahlon E. Smith <mahlon@laika.com>
Fri, 12 May 2017 16:17:41 -0700
changeset 16 e135ccae6783
child 17 23c7f5c8ee39
permissions -rw-r--r--
Migrate hashing functions to C.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
     1
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
     2
#include "hash.h"
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
     3
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
     4
/*
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
     5
 * I originally attemped to just convert surf.c to pure Ruby, but I
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
     6
 * confess a lack of understanding surrounding the char casts from
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
     7
 * unsigned ints, etc, and screwing up a hash algo doesn't do anyone
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
     8
 * any good, least of all, me.  In other words, I don't have to fully
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
     9
 * understand DJB code to trust in it. :-)
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    10
 *
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    11
 * The following is copied verbatim from the ezmlm-idx source, version
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    12
 * 7.2.2.  See: subhash.c, surf.c, and surfpcs.c.
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    13
 *
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    14
*/
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    15
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    16
void surf(unsigned int out[8],const unsigned int in[12],const unsigned int seed[32])
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    17
{
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    18
  unsigned int t[12]; unsigned int x; unsigned int sum = 0;
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    19
  int r; int i; int loop;
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    20
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    21
  for (i = 0;i < 12;++i) t[i] = in[i] ^ seed[12 + i];
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    22
  for (i = 0;i < 8;++i) out[i] = seed[24 + i];
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    23
  x = t[11];
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    24
  for (loop = 0;loop < 2;++loop) {
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    25
    for (r = 0;r < 16;++r) {
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    26
      sum += 0x9e3779b9;
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    27
      MUSH(0,5) MUSH(1,7) MUSH(2,9) MUSH(3,13)
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    28
      MUSH(4,5) MUSH(5,7) MUSH(6,9) MUSH(7,13)
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    29
      MUSH(8,5) MUSH(9,7) MUSH(10,9) MUSH(11,13)
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    30
    }
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    31
    for (i = 0;i < 8;++i) out[i] ^= t[i + 4];
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    32
  }
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    33
}
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    34
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    35
void surfpcs_init(surfpcs *s,const unsigned int k[32])
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    36
{
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    37
  int i;
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    38
  for (i = 0;i < 32;++i) s->seed[i] = k[i];
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    39
  for (i = 0;i < 8;++i) s->sum[i] = 0;
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    40
  for (i = 0;i < 12;++i) s->in[i] = 0;
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    41
  s->todo = 0;
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    42
}
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    43
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    44
void surfpcs_add(surfpcs *s,const char *x,unsigned int n)
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    45
{
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    46
  int i;
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    47
  while (n--) {
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    48
    data[end[s->todo++]] = *x++;
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    49
    if (s->todo == 32) {
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    50
      s->todo = 0;
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    51
      if (!++s->in[8])
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    52
        if (!++s->in[9])
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    53
          if (!++s->in[10])
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    54
            ++s->in[11];
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    55
      surf(s->out,s->in,s->seed);
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    56
      for (i = 0;i < 8;++i)
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    57
	s->sum[i] += s->out[i];
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    58
    }
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    59
  }
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    60
}
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    61
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    62
void surfpcs_addlc(surfpcs *s,const char *x,unsigned int n)
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    63
/* modified from surfpcs_add by case-independence and skipping ' ' & '\t' */
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    64
{
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    65
  unsigned char ch;
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    66
  int i;
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    67
  while (n--) {
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    68
    ch = *x++;
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    69
    if (ch == ' ' || ch == '\t') continue;
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    70
    if (ch >= 'A' && ch <= 'Z')
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    71
      ch -= 'a' - 'A';
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    72
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    73
    data[end[s->todo++]] = ch;
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    74
    if (s->todo == 32) {
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    75
      s->todo = 0;
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    76
      if (!++s->in[8])
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    77
        if (!++s->in[9])
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    78
          if (!++s->in[10])
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    79
            ++s->in[11];
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    80
      surf(s->out,s->in,s->seed);
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    81
      for (i = 0;i < 8;++i)
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    82
	  s->sum[i] += s->out[i];
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    83
    }
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    84
  }
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    85
}
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    86
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    87
void surfpcs_out(surfpcs *s,unsigned char h[32])
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    88
{
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    89
  int i;
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    90
  surfpcs_add(s,".",1);
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    91
  while (s->todo) surfpcs_add(s,"",1);
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    92
  for (i = 0;i < 8;++i) s->in[i] = s->sum[i];
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    93
  for (;i < 12;++i) s->in[i] = 0;
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    94
  surf(s->out,s->in,s->seed);
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    95
  for (i = 0;i < 32;++i) h[i] = outdata[end[i]];
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    96
}
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    97
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    98
void makehash(const char *indata,unsigned int inlen,char *hash)
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    99
	/* makes hash[COOKIE=20] from stralloc *indata, ignoring case and */
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   100
	/* SPACE/TAB */
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   101
{
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   102
  unsigned char h[32];
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   103
  surfpcs s;
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   104
  unsigned int seed[32];
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   105
  int i;
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   106
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   107
  for (i = 0;i < 32;++i) seed[i] = 0;
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   108
  surfpcs_init(&s,seed);
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   109
  surfpcs_addlc(&s,indata,inlen);
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   110
  surfpcs_out(&s,h);
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   111
  for (i = 0;i < 20;++i)
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   112
    hash[i] = 'a' + (h[i] & 15);
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   113
}
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   114
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   115
unsigned int subhashb(const char *s,long len)
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   116
{
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   117
  unsigned long h;
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   118
  h = 5381;
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   119
  while (len-- > 0)
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   120
    h = (h + (h << 5)) ^ (unsigned int)*s++;
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   121
  return h % 53;
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   122
}
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   123
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   124
unsigned int subhashs(const char *s)
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   125
{
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   126
  return subhashb(s,strlen(s));
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   127
}
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   128
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   129
/* end copy of ezmlm-idx source */
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   130
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   131
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   132
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   133
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   134
/*
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   135
 * call­seq:
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   136
 *   Ezmlm::Hash.address( email ) -> String
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   137
 *
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   138
 * Call the Surf hashing function on an +email+ address, returning
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   139
 * the hashed string.  This is specific to how ezmlm is seeding
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   140
 * the hash, and parsing email addresses from messages (prefixed with
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   141
 * the '<' character.)
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   142
 *
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   143
 */
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   144
VALUE
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   145
address( VALUE klass, VALUE email ) {
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   146
	char hash[20];
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   147
	char *input;
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   148
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   149
	Check_Type( email, T_STRING );
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   150
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   151
	email = rb_str_plus( rb_str_new2("<"), email);
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   152
	input = StringValueCStr( email );
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   153
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   154
	makehash( input, strlen(input), hash );
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   155
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   156
	return rb_str_new2( hash );
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   157
}
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   158
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   159
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   160
/*
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   161
 * call­seq:
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   162
 *   Ezmlm::Hash.subscriber( address ) -> String
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   163
 *
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   164
 * Call the subscriber hashing function on an email +address+, returning
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   165
 * the index character referring to the file containing subscriber presence.
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   166
 *
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   167
 */
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   168
VALUE
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   169
subscriber( VALUE klass, VALUE email ) {
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   170
	unsigned int prefix;
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   171
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   172
	Check_Type( email, T_STRING );
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   173
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   174
	email  = rb_str_plus( rb_str_new2("T"), email);
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   175
	prefix = subhashs( StringValueCStr(email) ) + 64;
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   176
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   177
	return rb_sprintf( "%c", (char)prefix );
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   178
}
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   179
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   180
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   181
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   182
void
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   183
Init_hash()
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   184
{
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   185
	rb_mEzmlm  = rb_define_module( "Ezmlm" );
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   186
	rb_cEZHash = rb_define_class_under( rb_mEzmlm, "Hash", rb_cObject );
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   187
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   188
	rb_define_module_function( rb_cEZHash, "address", address, 1 );
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   189
	rb_define_module_function( rb_cEZHash, "subscriber", subscriber, 1 );
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   190
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   191
	return;
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   192
}
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   193