cwebs 0.2
WebSocket wire protocol.
code/mt19937.c
Go to the documentation of this file.
00001 // Copyright (c) 2011-2012, Andre Caron (andre.l.caron@gmail.com)
00002 // All rights reserved.
00003 // 
00004 // Redistribution and use in source and binary forms, with or without
00005 // modification, are permitted provided that the following conditions are
00006 // met:
00007 // 
00008 //   Redistributions of source code must retain the above copyright
00009 //   notice, this list of conditions and the following disclaimer.
00010 // 
00011 //   Redistributions in binary form must reproduce the above copyright
00012 //   notice, this list of conditions and the following disclaimer in the
00013 //   documentation and/or other materials provided with the distribution.
00014 // 
00015 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00016 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00017 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00018 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00019 // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00020 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00021 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00022 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00023 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00024 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00025 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 
00034 #include "mt19937.h"
00035 
00036 static void _init ( uint32_t MT[] )
00037 {
00038     size_t i;
00039     for ( i = 1; (i < 624); ++i ) {
00040         MT[i] = (uint32_t)(0x6c078965 * (MT[i-1]^(MT[i-1]>>30)) + i);
00041     }
00042 }
00043 
00044 static uint32_t _next ( const uint32_t MT[], size_t i )
00045 {
00046     uint32_t y = MT[i];
00047     y ^= (y >> 11);
00048     y ^= (y <<  7) & 0x9d2c5680;
00049     y ^= (y << 15) & 0xefc60000;
00050     y ^= (y >> 18);
00051     return (y);
00052 }
00053 
00054 static void _mash ( uint32_t MT[] )
00055 {
00056     size_t i;
00057     size_t y;
00058     for ( i = 0; (i < 624); ++i )
00059     {
00060         y = (MT[i] >> 31) |
00061             (MT[(i+1)%624]&(1<<31));
00062         MT[i] = MT[(i+397)%624] ^ (y >> 1);
00063         if ((y % 2) != 0) {
00064             MT[i] = MT[i] ^ 0x9908b0df;
00065         }
00066     }
00067 }
00068 
00069 static void _grab ( mt19937_prng * generator, uint8_t * data, size_t size )
00070 {
00071     uint32_t next;
00072     while ( size > 0 )
00073     {
00074         next = mt19937_prng_next(generator);
00075         if ( size > 0 ) {
00076             --size, *data++ = next&0xff, next >>= 8;
00077         }
00078         if ( size > 0 ) {
00079             --size, *data++ = next&0xff, next >>= 8;
00080         }
00081         if ( size > 0 ) {
00082             --size, *data++ = next&0xff, next >>= 8;
00083         }
00084         if ( size > 0 ) {
00085             --size, *data++ = next&0xff, next >>= 8;
00086         }
00087     }
00088 }
00089 
00090 void mt19937_prng_init ( mt19937_prng * generator, uint32_t seed )
00091 {
00092     generator->base = 0;
00093     generator->data[0] = seed;
00094     _init(generator->data);
00095 }
00096 
00097 uint32_t mt19937_prng_next ( mt19937_prng * generator )
00098 {
00099     uint32_t next;
00100     if (generator->base == 0) {
00101         _mash(generator->data);
00102     }
00103     next = _next(generator->data, generator->base);
00104     ++generator->base, generator->base %= 624;
00105     return (next);
00106 }
00107 
00108 void mt19937_prng_grab ( mt19937_prng * generator, void * data, size_t size )
00109 {
00110     _grab(generator, (uint8_t*)data, size);
00111 }
 All Classes Files Functions