ext/ezmlm/hash/hash.c
author Mahlon E. Smith <mahlon@martini.nu>
Thu, 03 Oct 2019 11:44:44 -0700
changeset 28 99fdbf4a5f37
parent 23 eedb2586dea4
permissions -rw-r--r--
Be explicit with sticky removal, so two simultaneous processes don't create a race where sticky is left enabled.
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
23
eedb2586dea4 Tag C functions as static to avoid name collisions.
Mahlon E. Smith <mahlon@laika.com>
parents: 17
diff changeset
    16
static
16
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    17
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
    18
{
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    19
  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
    20
  int r; int i; int loop;
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    21
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    22
  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
    23
  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
    24
  x = t[11];
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    25
  for (loop = 0;loop < 2;++loop) {
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    26
    for (r = 0;r < 16;++r) {
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    27
      sum += 0x9e3779b9;
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    28
      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
    29
      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
    30
      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
    31
    }
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    32
    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
    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
23
eedb2586dea4 Tag C functions as static to avoid name collisions.
Mahlon E. Smith <mahlon@laika.com>
parents: 17
diff changeset
    36
static
16
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    37
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
    38
{
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    39
  int i;
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    40
  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
    41
  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
    42
  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
    43
  s->todo = 0;
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    44
}
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    45
23
eedb2586dea4 Tag C functions as static to avoid name collisions.
Mahlon E. Smith <mahlon@laika.com>
parents: 17
diff changeset
    46
static
16
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    47
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
    48
{
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    49
  int i;
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    50
  while (n--) {
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    51
    data[end[s->todo++]] = *x++;
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    52
    if (s->todo == 32) {
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    53
      s->todo = 0;
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    54
      if (!++s->in[8])
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    55
        if (!++s->in[9])
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    56
          if (!++s->in[10])
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    57
            ++s->in[11];
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    58
      surf(s->out,s->in,s->seed);
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    59
      for (i = 0;i < 8;++i)
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    60
	s->sum[i] += s->out[i];
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
  }
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    63
}
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    64
23
eedb2586dea4 Tag C functions as static to avoid name collisions.
Mahlon E. Smith <mahlon@laika.com>
parents: 17
diff changeset
    65
static
16
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    66
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
    67
/* 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
    68
{
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    69
  unsigned char ch;
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    70
  int i;
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    71
  while (n--) {
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    72
    ch = *x++;
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    73
    if (ch == ' ' || ch == '\t') continue;
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    74
    if (ch >= 'A' && ch <= 'Z')
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    75
      ch -= 'a' - 'A';
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    76
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    77
    data[end[s->todo++]] = ch;
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    78
    if (s->todo == 32) {
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    79
      s->todo = 0;
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    80
      if (!++s->in[8])
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    81
        if (!++s->in[9])
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    82
          if (!++s->in[10])
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    83
            ++s->in[11];
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    84
      surf(s->out,s->in,s->seed);
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    85
      for (i = 0;i < 8;++i)
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    86
	  s->sum[i] += s->out[i];
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    87
    }
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
}
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    90
23
eedb2586dea4 Tag C functions as static to avoid name collisions.
Mahlon E. Smith <mahlon@laika.com>
parents: 17
diff changeset
    91
static
16
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    92
void surfpcs_out(surfpcs *s,unsigned char h[32])
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    93
{
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    94
  int i;
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    95
  surfpcs_add(s,".",1);
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    96
  while (s->todo) surfpcs_add(s,"",1);
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    97
  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
    98
  for (;i < 12;++i) s->in[i] = 0;
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    99
  surf(s->out,s->in,s->seed);
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   100
  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
   101
}
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   102
23
eedb2586dea4 Tag C functions as static to avoid name collisions.
Mahlon E. Smith <mahlon@laika.com>
parents: 17
diff changeset
   103
static
16
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   104
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
   105
	/* 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
   106
	/* SPACE/TAB */
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   107
{
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   108
  unsigned char h[32];
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   109
  surfpcs s;
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   110
  unsigned int seed[32];
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   111
  int i;
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   112
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   113
  for (i = 0;i < 32;++i) seed[i] = 0;
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   114
  surfpcs_init(&s,seed);
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   115
  surfpcs_addlc(&s,indata,inlen);
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   116
  surfpcs_out(&s,h);
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   117
  for (i = 0;i < 20;++i)
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   118
    hash[i] = 'a' + (h[i] & 15);
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   119
}
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   120
23
eedb2586dea4 Tag C functions as static to avoid name collisions.
Mahlon E. Smith <mahlon@laika.com>
parents: 17
diff changeset
   121
static
16
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   122
unsigned int subhashb(const char *s,long len)
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 long h;
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   125
  h = 5381;
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   126
  while (len-- > 0)
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   127
    h = (h + (h << 5)) ^ (unsigned int)*s++;
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   128
  return h % 53;
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   129
}
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   130
23
eedb2586dea4 Tag C functions as static to avoid name collisions.
Mahlon E. Smith <mahlon@laika.com>
parents: 17
diff changeset
   131
static
16
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   132
unsigned int subhashs(const char *s)
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
  return subhashb(s,strlen(s));
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   135
}
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   136
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   137
/* end copy of ezmlm-idx source */
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   138
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   139
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   140
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   141
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   142
/*
23
eedb2586dea4 Tag C functions as static to avoid name collisions.
Mahlon E. Smith <mahlon@laika.com>
parents: 17
diff changeset
   143
 * call-seq:
16
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   144
 *   Ezmlm::Hash.address( email ) -> String
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   145
 *
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   146
 * 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
   147
 * 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
   148
 * 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
   149
 * the '<' character.)
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
 */
23
eedb2586dea4 Tag C functions as static to avoid name collisions.
Mahlon E. Smith <mahlon@laika.com>
parents: 17
diff changeset
   152
static VALUE
16
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   153
address( VALUE klass, VALUE email ) {
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   154
	char hash[20];
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   155
	char *input;
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   156
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   157
	Check_Type( email, T_STRING );
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   158
17
23c7f5c8ee39 Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 16
diff changeset
   159
	email = rb_str_plus( rb_str_new2("<"), email );
16
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   160
	input = StringValueCStr( email );
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   161
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   162
	makehash( input, strlen(input), hash );
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   163
17
23c7f5c8ee39 Multiple changes.
Mahlon E. Smith <mahlon@laika.com>
parents: 16
diff changeset
   164
	return rb_str_new( hash, 20 );
16
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   165
}
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
/*
23
eedb2586dea4 Tag C functions as static to avoid name collisions.
Mahlon E. Smith <mahlon@laika.com>
parents: 17
diff changeset
   169
 * call-seq:
16
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   170
 *   Ezmlm::Hash.subscriber( address ) -> String
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
 * 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
   173
 * 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
   174
 *
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   175
 */
23
eedb2586dea4 Tag C functions as static to avoid name collisions.
Mahlon E. Smith <mahlon@laika.com>
parents: 17
diff changeset
   176
static VALUE
16
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   177
subscriber( VALUE klass, VALUE email ) {
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   178
	unsigned int prefix;
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
	Check_Type( email, T_STRING );
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
	email  = rb_str_plus( rb_str_new2("T"), email);
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   183
	prefix = subhashs( StringValueCStr(email) ) + 64;
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
	return rb_sprintf( "%c", (char)prefix );
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   186
}
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
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   189
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   190
void
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   191
Init_hash()
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
	rb_mEzmlm  = rb_define_module( "Ezmlm" );
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   194
	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
   195
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   196
	rb_define_module_function( rb_cEZHash, "address", address, 1 );
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   197
	rb_define_module_function( rb_cEZHash, "subscriber", subscriber, 1 );
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   198
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   199
	return;
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   200
}
e135ccae6783 Migrate hashing functions to C.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   201