Posted on December 7th, 2010 by Brendan Dahl
Trapeze uses many new features available in HTML5 including: canvas, web workers, drag/drop files, and some of the new file API’s. The new canvas element is the biggest piece that enables all the 2D drawing commands that a PDF needs to render correctly. Web workers are used to parse the PDF files in a another thread so the browser doesn’t lock up. Web workers also draw the PDF to what I call a FauxCanvas which basically just stores all the operations to the a fake canvas object and then sends them back to the main thread to apply them to the real canvas when its ready. Hopefully, in the future this will not be needed if an off screen canvas is made available to web workers. The drag and drop file opening was added just to see how it works.
Some of the limitations:
- No ability to open a PDF from a URL. This is because cross site AJAX requests are not allowed by the browser. To get around this a proxy can be used, but I don’t currently have the funds to pay for the bandwidth a proxy pdf server would require.
- Only tested in Firefox and Chrome.
- Missing support for portions of the PDF spec (full color space support, pdf functions, patterns, certain image types, …).
The project source is available at http://code.google.com/p/trapeze-reader/
Posted on August 13th, 2010 by Brendan Dahl
After learning that many lotteries now use software and hardware random number generators(RNG) and reading a story about Daniel Corriveau, I decided it would be fun to explore trying to figure the state of a RNG and predict values.
One of the most commonly used RNG algorithms today is the Mersenee Twister(MT) algorithm. On the wikipedia article on MT it mentions after observing 624 generated numbers it is possible to predict all future iterates. At first glance I didn’t see how this was possible because of the algorithm’s tempering function, but after reading comments on wikipedia the tempering function is bijective(reversible). If you’re looking for a good puzzle to solve, try writing your own reverse tempering function. The tempering function is defined as:
k = mt[state->mti];
k ^= (k >> 11); // Temper shift U
k ^= (k << 7) & 0x9d2c5680UL; // Temper shift S & TemperingMaskB
k ^= (k << 15) & 0xefc60000UL; // Temper shift L & TemperingMaskC
k ^= (k >> 18); // Temper shift T
My solution is broken up in to the different parts and is definitely not optimized but it works.
private const UInt32 TemperingMaskB = 0x9d2c5680;
private const UInt32 TemperingMaskC = 0xefc60000;
static UInt32 undoTemper(UInt32 y)
y = undoTemperShiftL(y);
y = undoTemperShiftT(y);
y = undoTemperShiftS(y);
y = undoTemperShiftU(y);
static UInt32 undoTemperShiftL(UInt32 y)
UInt32 last14 = y >> 18;
UInt32 final = y ^ last14;
static UInt32 undoTemperShiftT(UInt32 y)
UInt32 first17 = y << 15;
UInt32 final = y ^ (first17 & TemperingMaskC);
static UInt32 undoTemperShiftS(UInt32 y)
* This one also sucked to figure out, but now I think i could write
* a general one. This basically waterfalls and keeps restoring original
* bits then shifts the values down and xors again to restore more bits
* and keeps on doing it.
UInt32 a = y << 7;
UInt32 b = y ^ (a & TemperingMaskB);
UInt32 c = b << 7;
UInt32 d = y ^ (c & TemperingMaskB); // now we have 14 of the original
UInt32 e = d << 7;
UInt32 f = y ^ (e & TemperingMaskB); // now we have 21 of the original
UInt32 g = f << 7;
UInt32 h = y ^ (g & TemperingMaskB); // now we have 28 of the original
UInt32 i = h << 7; // now we have the original xor
UInt32 final = y ^ (i & TemperingMaskB);
static UInt32 undoTemperShiftU(UInt32 y)
* This was confusing to figure out.
* We know the first 11 bits are un-altered becuase they were
* xored with 0's. We shift those 11 bits to the right and xor that with the
* original which gives us the first 22 bits(b) of what it orginally was. Now that we have the
* first 22 bits so we can shift that to the right 11 bits which gives us
* what the number was orginally xored with. So then we just xor y with that and
* our number is restored!
UInt32 a = y >> 11;
UInt32 b = y ^ a;
UInt32 c = b >> 11;
UInt32 final = y ^ c;
So then after observing 624 values and using the reverse temper function it is possible fill in the 624 internal state values of the mersenne twister algorithm and predict all the future values. Unfortunately this doesn’t help in many ways for practical purposes, since you need all 624 values unaltered. Take for instance the lottery. First, you don’t actually know the random number. Second, most likely the random number was used to shuffle the numbers in a certain way and only a portion of the random number was used. Third, if the algorithm gets reseeded each time you don’t know your 624 consecutive values. Also, some of the newer versions of the MT generate two values and combine them to create a better random double.
Posted on October 20th, 2009 by Brendan Dahl
ERROR 1126 (HY000): Can't open shared library
If you receive the error from MySQL while installing a plugin or a user defined function(UDF) there can be several causes:
- You mistyped your dll file name, double check the name
- MySQL can’t find the dll file in the plugin folder
mysql> show variables like 'plugin_dir';
- And make sure that path matches the path where your dll is stored.
- A dependent dll can’t be found
- Download Dependency Walker from http://www.dependencywalker.com/
- Open your dll on the machine you’re installing the plugin. It should tell you if there was a missing dependency
In my case it turned out the be #3.
Dependency walker said I was missing MSVCR90D.dll, which I learned from Stack Overflow is actually the debug build of MSVCR90 (Notice: the “D” on the end). So I went back to Visual Studio Express 2008 and made sure my configuration was set to Release and rebuilt the build and copied it over and everything worked.
Posted on October 17th, 2009 by Brendan Dahl
If you want to execute a native SQL query and don’t care about the result, just grab the ADO.NET database connection from the NHibernate session and use that to run the query.
Here’s the code:
// Get the NHibernate Session (this line will change depending how you do it)
NHibernate.ISession session = SessionManager.GetInstance();
// Create a SQL Command
System.Data.IDbCommand command = session.Connection.CreateCommand();
// Set the query you're going to run
command.CommandText = "INSERT INTO Users ('Peter', 'Piper')";
// Run the query