ZBar bar code reader . http://zbar.sourceforge.net/ ZBar is licensed under the GNU LGPL 2.1 to enable development of both open source and commercial projects.

Dependents:   GR-PEACH_Camera_in_barcode levkov_ov7670

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers isaac.c Source File

isaac.c

00001 /*Written by Timothy B. Terriberry (tterribe@xiph.org) 1999-2009 public domain.
00002   Based on the public domain implementation by Robert J. Jenkins Jr.*/
00003 #include <float.h>
00004 #include <math.h>
00005 #include <string.h>
00006 #include "isaac.h"
00007 
00008 
00009 
00010 #define ISAAC_MASK        (0xFFFFFFFFU)
00011 
00012 
00013 
00014 static void isaac_update(isaac_ctx *_ctx){
00015   unsigned *m;
00016   unsigned *r;
00017   unsigned  a;
00018   unsigned  b;
00019   unsigned  x;
00020   unsigned  y;
00021   int       i;
00022   m=_ctx->m;
00023   r=_ctx->r;
00024   a=_ctx->a;
00025   b=_ctx->b+(++_ctx->c)&ISAAC_MASK;
00026   for(i=0;i<ISAAC_SZ/2;i++){
00027     x=m[i];
00028     a=(a^a<<13)+m[i+ISAAC_SZ/2]&ISAAC_MASK;
00029     m[i]=y=m[(x&ISAAC_SZ-1<<2)>>2]+a+b&ISAAC_MASK;
00030     r[i]=b=m[y>>ISAAC_SZ_LOG+2&ISAAC_SZ-1]+x&ISAAC_MASK;
00031     x=m[++i];
00032     a=(a^a>>6)+m[i+ISAAC_SZ/2]&ISAAC_MASK;
00033     m[i]=y=m[(x&ISAAC_SZ-1<<2)>>2]+a+b&ISAAC_MASK;
00034     r[i]=b=m[y>>ISAAC_SZ_LOG+2&ISAAC_SZ-1]+x&ISAAC_MASK;
00035     x=m[++i];
00036     a=(a^a<<2)+m[i+ISAAC_SZ/2]&ISAAC_MASK;
00037     m[i]=y=m[(x&ISAAC_SZ-1<<2)>>2]+a+b&ISAAC_MASK;
00038     r[i]=b=m[y>>ISAAC_SZ_LOG+2&ISAAC_SZ-1]+x&ISAAC_MASK;
00039     x=m[++i];
00040     a=(a^a>>16)+m[i+ISAAC_SZ/2]&ISAAC_MASK;
00041     m[i]=y=m[(x&ISAAC_SZ-1<<2)>>2]+a+b&ISAAC_MASK;
00042     r[i]=b=m[y>>ISAAC_SZ_LOG+2&ISAAC_SZ-1]+x&ISAAC_MASK;
00043   }
00044   for(i=ISAAC_SZ/2;i<ISAAC_SZ;i++){
00045     x=m[i];
00046     a=(a^a<<13)+m[i-ISAAC_SZ/2]&ISAAC_MASK;
00047     m[i]=y=m[(x&ISAAC_SZ-1<<2)>>2]+a+b&ISAAC_MASK;
00048     r[i]=b=m[y>>ISAAC_SZ_LOG+2&ISAAC_SZ-1]+x&ISAAC_MASK;
00049     x=m[++i];
00050     a=(a^a>>6)+m[i-ISAAC_SZ/2]&ISAAC_MASK;
00051     m[i]=y=m[(x&ISAAC_SZ-1<<2)>>2]+a+b&ISAAC_MASK;
00052     r[i]=b=m[y>>ISAAC_SZ_LOG+2&ISAAC_SZ-1]+x&ISAAC_MASK;
00053     x=m[++i];
00054     a=(a^a<<2)+m[i-ISAAC_SZ/2]&ISAAC_MASK;
00055     m[i]=y=m[(x&ISAAC_SZ-1<<2)>>2]+a+b&ISAAC_MASK;
00056     r[i]=b=m[y>>ISAAC_SZ_LOG+2&ISAAC_SZ-1]+x&ISAAC_MASK;
00057     x=m[++i];
00058     a=(a^a>>16)+m[i-ISAAC_SZ/2]&ISAAC_MASK;
00059     m[i]=y=m[(x&ISAAC_SZ-1<<2)>>2]+a+b&ISAAC_MASK;
00060     r[i]=b=m[y>>ISAAC_SZ_LOG+2&ISAAC_SZ-1]+x&ISAAC_MASK;
00061   }
00062   _ctx->b=b;
00063   _ctx->a=a;
00064   _ctx->n=ISAAC_SZ;
00065 }
00066 
00067 static void isaac_mix(unsigned _x[8]){
00068   static const unsigned char SHIFT[8]={11,2,8,16,10,4,8,9};
00069   int i;
00070   for(i=0;i<8;i++){
00071     _x[i]^=_x[i+1&7]<<SHIFT[i];
00072     _x[i+3&7]+=_x[i];
00073     _x[i+1&7]+=_x[i+2&7];
00074     i++;
00075     _x[i]^=_x[i+1&7]>>SHIFT[i];
00076     _x[i+3&7]+=_x[i];
00077     _x[i+1&7]+=_x[i+2&7];
00078   }
00079 }
00080 
00081 
00082 void isaac_init(isaac_ctx *_ctx,const void *_seed,int _nseed){
00083   const unsigned char *seed;
00084   unsigned            *m;
00085   unsigned            *r;
00086   unsigned             x[8];
00087   int                  i;
00088   int                  j;
00089   _ctx->a=_ctx->b=_ctx->c=0;
00090   m=_ctx->m;
00091   r=_ctx->r;
00092   x[0]=x[1]=x[2]=x[3]=x[4]=x[5]=x[6]=x[7]=0x9E3779B9;
00093   for(i=0;i<4;i++)isaac_mix(x);
00094   if(_nseed>ISAAC_SEED_SZ_MAX)_nseed=ISAAC_SEED_SZ_MAX;
00095   seed=(const unsigned char *)_seed;
00096   for(i=0;i<_nseed>>2;i++){
00097     r[i]=seed[i<<2|3]<<24|seed[i<<2|2]<<16|seed[i<<2|1]<<8|seed[i<<2];
00098   }
00099   if(_nseed&3){
00100     r[i]=seed[i<<2];
00101     for(j=1;j<(_nseed&3);j++)r[i]+=seed[i<<2|j]<<(j<<3);
00102     i++;
00103   }
00104   memset(r+i,0,(ISAAC_SZ-i)*sizeof(*r));
00105   for(i=0;i<ISAAC_SZ;i+=8){
00106     for(j=0;j<8;j++)x[j]+=r[i+j];
00107     isaac_mix(x);
00108     memcpy(m+i,x,sizeof(x));
00109   }
00110   for(i=0;i<ISAAC_SZ;i+=8){
00111     for(j=0;j<8;j++)x[j]+=m[i+j];
00112     isaac_mix(x);
00113     memcpy(m+i,x,sizeof(x));
00114   }
00115   isaac_update(_ctx);
00116 }
00117 
00118 unsigned isaac_next_uint32(isaac_ctx *_ctx){
00119   if(!_ctx->n)isaac_update(_ctx);
00120   return _ctx->r[--_ctx->n];
00121 }
00122 
00123 /*Returns a uniform random integer less than the given maximum value.
00124   _n: The upper bound on the range of numbers returned (not inclusive).
00125       This must be strictly less than 2**32.
00126   Return: An integer uniformly distributed between 0 (inclusive) and _n
00127            (exclusive).*/
00128 unsigned isaac_next_uint(isaac_ctx *_ctx,unsigned _n){
00129   unsigned r;
00130   unsigned v;
00131   unsigned d;
00132   do{
00133     r=isaac_next_uint32(_ctx);
00134     v=r%_n;
00135     d=r-v;
00136   }
00137   while((d+_n-1&ISAAC_MASK)<d);
00138   return v;
00139 }
00140