ps2sdk  1.1
A collection of Open Source libraries used for developing applications on Sony's PlayStation 2® (PS2).
lookupa.c File Reference
#include "standard.h"
#include "lookupa.h"
+ Include dependency graph for lookupa.c:

Go to the source code of this file.

Macros

#define mix(a, b, c)
 
#define mixc(a, b, c, d, e, f, g, h)
 

Functions

ub4 lookup (ub1 *k, ub4 length, ub4 level)
 
void checksum (ub1 *k, ub4 len, ub4 *state)
 

Macro Definition Documentation

◆ mix

#define mix (   a,
  b,
 
)
Value:
{ \
a -= b; a -= c; a ^= (c>>13); \
b -= c; b -= a; b ^= (a<<8); \
c -= a; c -= b; c ^= (b>>13); \
a -= b; a -= c; a ^= (c>>12); \
b -= c; b -= a; b ^= (a<<16); \
c -= a; c -= b; c ^= (b>>5); \
a -= b; a -= c; a ^= (c>>3); \
b -= c; b -= a; b ^= (a<<10); \
c -= a; c -= b; c ^= (b>>15); \
}

Definition at line 41 of file lookupa.c.

◆ mixc

#define mixc (   a,
  b,
  c,
  d,
  e,
  f,
  g,
 
)
Value:
{ \
a^=b<<11; d+=a; b+=c; \
b^=c>>2; e+=b; c+=d; \
c^=d<<8; f+=c; d+=e; \
d^=e>>16; g+=d; e+=f; \
e^=f<<10; h+=e; f+=g; \
f^=g>>4; a+=f; g+=h; \
g^=h<<8; b+=g; h+=a; \
h^=a>>9; c+=h; a+=b; \
}

Definition at line 136 of file lookupa.c.

Function Documentation

◆ checksum()

void checksum ( ub1 k,
ub4  len,
ub4 state 
)

Definition at line 170 of file lookupa.c.

174 {
175  register ub4 a,b,c,d,e,f,g,h,length;
176 
177  /* Use the length and level; add in the golden ratio. */
178  length = len;
179  a=state[0]; b=state[1]; c=state[2]; d=state[3];
180  e=state[4]; f=state[5]; g=state[6]; h=state[7];
181 
182  /*---------------------------------------- handle most of the key */
183  while (len >= 32)
184  {
185  a += (k[0] +(k[1]<<8) +(k[2]<<16) +(k[3]<<24));
186  b += (k[4] +(k[5]<<8) +(k[6]<<16) +(k[7]<<24));
187  c += (k[8] +(k[9]<<8) +(k[10]<<16)+(k[11]<<24));
188  d += (k[12]+(k[13]<<8)+(k[14]<<16)+(k[15]<<24));
189  e += (k[16]+(k[17]<<8)+(k[18]<<16)+(k[19]<<24));
190  f += (k[20]+(k[21]<<8)+(k[22]<<16)+(k[23]<<24));
191  g += (k[24]+(k[25]<<8)+(k[26]<<16)+(k[27]<<24));
192  h += (k[28]+(k[29]<<8)+(k[30]<<16)+(k[31]<<24));
193  mixc(a,b,c,d,e,f,g,h);
194  mixc(a,b,c,d,e,f,g,h);
195  mixc(a,b,c,d,e,f,g,h);
196  mixc(a,b,c,d,e,f,g,h);
197  k += 32; len -= 32;
198  }
199 
200  /*------------------------------------- handle the last 31 bytes */
201  h += length;
202  switch(len)
203  {
204  case 31: h+=(k[30]<<24);
205  case 30: h+=(k[29]<<16);
206  case 29: h+=(k[28]<<8);
207  case 28: g+=(k[27]<<24);
208  case 27: g+=(k[26]<<16);
209  case 26: g+=(k[25]<<8);
210  case 25: g+=k[24];
211  case 24: f+=(k[23]<<24);
212  case 23: f+=(k[22]<<16);
213  case 22: f+=(k[21]<<8);
214  case 21: f+=k[20];
215  case 20: e+=(k[19]<<24);
216  case 19: e+=(k[18]<<16);
217  case 18: e+=(k[17]<<8);
218  case 17: e+=k[16];
219  case 16: d+=(k[15]<<24);
220  case 15: d+=(k[14]<<16);
221  case 14: d+=(k[13]<<8);
222  case 13: d+=k[12];
223  case 12: c+=(k[11]<<24);
224  case 11: c+=(k[10]<<16);
225  case 10: c+=(k[9]<<8);
226  case 9 : c+=k[8];
227  case 8 : b+=(k[7]<<24);
228  case 7 : b+=(k[6]<<16);
229  case 6 : b+=(k[5]<<8);
230  case 5 : b+=k[4];
231  case 4 : a+=(k[3]<<24);
232  case 3 : a+=(k[2]<<16);
233  case 2 : a+=(k[1]<<8);
234  case 1 : a+=k[0];
235  }
236  mixc(a,b,c,d,e,f,g,h);
237  mixc(a,b,c,d,e,f,g,h);
238  mixc(a,b,c,d,e,f,g,h);
239  mixc(a,b,c,d,e,f,g,h);
240 
241  /*-------------------------------------------- report the result */
242  state[0]=a; state[1]=b; state[2]=c; state[3]=d;
243  state[4]=e; state[5]=f; state[6]=g; state[7]=h;
244 }
#define mixc(a, b, c, d, e, f, g, h)
Definition: lookupa.c:136
u32 ub4
Definition: standard.h:32

References mixc.

◆ lookup()

ub4 lookup ( ub1 k,
ub4  length,
ub4  level 
)

Definition at line 82 of file lookupa.c.

86 {
87  register ub4 a,b,c,len;
88 
89  /* Set up the internal state */
90  len = length;
91  a = b = 0x9e3779b9; /* the golden ratio; an arbitrary value */
92  c = level; /* the previous hash value */
93 
94  /*---------------------------------------- handle most of the key */
95  while (len >= 12)
96  {
97  a += (k[0] +((ub4)k[1]<<8) +((ub4)k[2]<<16) +((ub4)k[3]<<24));
98  b += (k[4] +((ub4)k[5]<<8) +((ub4)k[6]<<16) +((ub4)k[7]<<24));
99  c += (k[8] +((ub4)k[9]<<8) +((ub4)k[10]<<16)+((ub4)k[11]<<24));
100  mix(a,b,c);
101  k += 12; len -= 12;
102  }
103 
104  /*------------------------------------- handle the last 11 bytes */
105  c += length;
106  switch(len) /* all the case statements fall through */
107  {
108  case 11: c+=((ub4)k[10]<<24);
109  case 10: c+=((ub4)k[9]<<16);
110  case 9 : c+=((ub4)k[8]<<8);
111  /* the first byte of c is reserved for the length */
112  case 8 : b+=((ub4)k[7]<<24);
113  case 7 : b+=((ub4)k[6]<<16);
114  case 6 : b+=((ub4)k[5]<<8);
115  case 5 : b+=k[4];
116  case 4 : a+=((ub4)k[3]<<24);
117  case 3 : a+=((ub4)k[2]<<16);
118  case 2 : a+=((ub4)k[1]<<8);
119  case 1 : a+=k[0];
120  /* case 0: nothing left to add */
121  }
122  mix(a,b,c);
123  /*-------------------------------------------- report the result */
124  return c;
125 }
#define mix(a, b, c)
Definition: lookupa.c:41

References mix.