Monday, February 27, 2017

Numbers Memory Trade Off

I have a small obsession about human memory. I’ve watched many YouTube videos about savants with profound memory and the things they can do. Videos such as “The Boy With The Incredible Brain” and “The Real Rain Man”. These people who can remember anything with perfect accuracy are fascinating.

My obsession pushed me to learn about memory techniques. I found out that even though, my memory is nothing to be proud of, using a few tricks I was able to memorize the order of a deck of 52 play cards! At my peak I was able to memorize two full decks in less than 15 minutes. I believe, that almost anyone who practices that can achieve the same results in a short period of time.

In one TED talk, Daniel Tammet, who is a savant, talked about synesthesia. He described synesthesia as one of the key abilities for outstanding memory and many other astonishing creative abilities. Synesthesia, is a cross between senses, for instance, perceiving numbers as colors or sounds. I instantly saw that as an invite to try LSD, as it’s known to be one of the side effects of the drug ;)

Daniel Tammet, also claims that everyone has synesthesia of some level, and that it’s also an acquired ability to some level.
On my very little free time, I’ve started working on an Android app for helping “stimulating” synesthesia. The idea is simply a big size Simon game. Simon is a kind of a memory game. In that game a player is given a series of tones and lights and requires to repeat the series. Usually this game has four colors. My version of the game has ten colors, each one corresponds to a number from Zero to Nine. In my game the series of tones is not random, and it corresponds to string of numbers the user wish to memorize. Currently I use 50 digits of Pi, but I will add an option to enter arbitrary number, or choose phone number from the contact list.
If anyone has more interesting links about these kind of memory techniques, or any unbelievable savant stories, please share it down in the comments.
If anyone is interested in the Android app, or wants to help please do it on GitHub on the following link:
Warning, this is my very first Android app, I’m more used to see Android apps in IDA...


Monday, October 3, 2016

More QRCodes

I’ve updated the library from the last post to support colored QRCodes with all kinds of outputs.
To try it one can either use my fork at: (In Red)
Or the pull request at the original project: (In Green)
Here is the output of
PNG format

TTY using basic ANSI colors:

ASCII using extended RGB escape codes:

Sunday, September 11, 2016

Business Card

When I just started freelancing, I wanted to design my own business card. It had to have QR code, of course. At first I tried one of these online QR code generators and got something like this:

That contains all the Information I want to share about myself. Unfortunately, this is a very dense QR code, and therefore it’s harder to scan properly with a smartphone, especially when printed on a card. It seemed like no matter how much Information I removed from it, it was still the dense kind of QR code. So I started reading about the structure of QR codes to understand what are the limits of the nice smartphone friendly type.
Apparently there are classes of QR codes called versions, where “Ver1” is the most friendly, but contains the least amount of data, while the one above is of version 7. Therefore, I was aiming for version 5 at most.
During my research (when I say research I mean reading the Wikipedia page for QR code), I came across this picture that explains the structure of QR codes:

Source: Wikipedia
After I saw that sample, I knew exactly how my Business card should look like. My skill is “Reverse Engineering”, I take things apart to understand how they work. The Picture reflected that very well. I wanted to deconstruct the QR into elements of data on the card. The idea I had in mind was something like:

The colors of a box reflect the data it encodes. Of course, this is just a sample I made by hand, in which the colors have very little to do with the data.

My plan was to:

  • Find a Python library for encoding QR codes
  • Find a way to encode the information in the version 5 QR code
  • Write a new feature for the library to add colors to the data

I started with the Python QR code library from:

But it seemed like it wasn’t optimized enough, this is what I got the first time I used it:

Which is a level 8 QR code, while for the same data I already got a level 7 QR code. At first I assumed it had to do with the amount of error correction used in the encoding, but investigating further, I found that both of them were set for M (Medium) error correction mode.
By investigating the code I found that I can get a better result by using the library a bit differently, but still it wasn't the most optimized encoding.
QR code has 4 different kinds of data encodings:

  • Numbers only (0-9), for which every 3 characters are encoded by 10 bits
  • Number + capital alphabet + “ $%*+-./:”, in which every 2 characters are encoded by 11 bits
  • 8 bit char for each every character takes 8 bits
  • Kanji for Chinese characters encoding

The problem is that moving from one mode to another cost about 15 bits. For example: to encode the string “a111a” you can use either:

  • 15 to set to mode 3
  • Encode “a”
  • 15 bits to move to mode 1
  • Encode “111” with 10 bits
  • Move back to mode 3 = 15 more bits
  • Encode “a”

In total: 15 + 8 + 15 + 10 + 15 + 8 = 63 bits
Another option is to:

  • Move to mode 3 for 15
  • Encode everything “a111a” with 8 * 5 bits

In total: 15 + 40 = 55bits
Therefore, it’s hard to know when it’s really worth changing encoding mode.
To solve the problem with 100% certainty of the best encoding, I used a flow graph. In that graph I made a node for “start” and edge to each encoding mode that can encode the next character. For the data In the example above the graph would look something like:

Once I created the graph, searching the shortest path from “start” to “end” using “Dijkstra” gave me the very best encoding for my data.

This work brought me back the level 7 QR code, which is still not the level I was aiming. Meanwhile, I found out that UPPER CASE letters are encoded much better than lower case. So by playing with the data a little I got:

This is a level 6 QR code, just one more level to go ;)
The last encoding improvement I got by reducing error correction from M to L (Low). Here it is:

This is a level 5 QR code, big success!

All that I was left to do is take care of the colors. To adjust the colors I simply attached an RGB value to every data piece that I added, and percolated it throw the different functions. For the error correction I used two different modes, one uses a different color (see example below) and one that reflects the data colors in a vague way. Here is the result:

FN:Assaf Nativ
  • Probs
  • Timing pattern
  • Error Correction

One more fun twist: Squares -> Circles:

Cheer, Assaf

P.S. The definition of the QR Code standard is ISO/IEC 18004:2015, which cost about 300 USD. If you liked this post, any help in getting the PDF would be very appreciated.

Saturday, May 9, 2015

Future Predictions FAIL

Note: sorry for my extra bad grammar in this post, I’m not sure which tenses I should use when talking about past writings that describe future that is now the past but wasn't then as it was in the future… well you get it...
Every once in a while I come across an article about how the future might look like. Usually these kind of writings turns out to be a total fail when the future date comes. I adopted a habit of collecting these articles in a special folder. Each article is saved with the name of read_me_in_year_YYYY.pdf where YYYY is the year which the current paper is trying to foresee. I’m not allowing myself to read any of these until the year YYYY arrives.
Recently I had the pleasure of opening the first one that I saved in 2008 that tried to predict computers technology for 2015. I found an online copy of that article here:
The article is trying its best in 15 technologies, and I think it’s quite safe to say it failed in about 12 of them, but I’ll say it failed all the 15.
  1. The memristor. The idea of memory of the size of persistent memory that we currently have but that works in the speed of RAM that we currently have, is not new, and if it does turn true in the future it will have to change the entire way operating systems are written. This idea is a lot of fun to play with, and one can actually try it himself with a special hardware such as this But as awesome as it is, we are still going to have to wait for this to become real, if it ever will.
  2. 32-Core CPU. Especially not for home users. FAIL. They failed to see the Intel Itanium fail on the desktop. It seems like we are stuck at the 8 cores border for quite a while.
  3. End of Stand-Alone Graphics Boards. FAIL. Just a week ago I bought a new graphics adapter for my PC to support my awesome three monitor setup.
  4. USB 3.0, well, ok. But they made much a bigger deal of it, than what it really is. They also predicted it would have a different connector.
  5. Wireless Power Transmission. FAIL. They win the FAIL flag here just because we don’t have light bulbs that work on wireless power.
  6. Windows 64bit, yes it’s here. And again, they get a FAIL because quoting “Microsoft will have to jettison 32-bit altogether”. Nevermind what the word “jettison” means, Windows 10 is coming out soon and guess what, it’s going to have a 32bit version. They also tried to predict that in 2025 we will have 128bit OS, which I think frankly is just silly.
  7. Windows 7. Not much of a prediction there. They were aiming at 2010 two year prediction is not that exciting because things are already being set and built.
  8. Google desktop. Not much of a prediction here either.
  9. Gesture-Based remote control. I think it’s now safe to call this dream a fail. And the same goes for voice recognition TV, do you know anyone who speaks to his TV and it actually answers?
  10. Tru2Way TV. FAIL, quoting from Wikipedia: “As of July 2010, Panasonic, the sole device manufacturer, producing Tru2way compatible televisions, has stated that they will no longer sell Tru2way compatible televisions. Thus, at this point there are no television sets with built-in Tru2Way compatibility being sold.”
  11. No DRM from the big companies… Lol, FAIL
  12. Use any phone on any wireless network. FAIL. Something much better might happen with 4G.
  13. Your fingers do even more walking. Or how multi touch screen seemed such a neat technology just 7 years ago. I must give them a PASS on this one. Multi-touch screens are everywhere now, and we kinda’ take them for granted. I think they failed from the other side on this one. They predicted that about 800 million touch-screens would have been sold in 2013, but just with smartphones it came closer to a billion.
  14. Cell phones are the new paper. Not more than what it was like in 2008. FAIL
  15. Where you at. I’m not sure what they are trying to describe here. The idea is so vague. It sounds a little like 4square, but it’s not. Anyhow, FAIL.
Recently Gizmodo made a post about how and why we so often fail in predicting future technology. I find their post very relevant to this one, so here is a link:


Saturday, January 31, 2015

IMHO: Why C++ is broken

Recently I had the privilege to be part of a team of very strong programmers. Everyone in this small team has many years of experience in writing code for systems that are critical in performance, stability, maintenance.
A part of the product that I was in charge of, had to store information in a database. To store the information in the DB I had to serialize it, and for that I used Tomer Filiba serializing template for C++ called construct++. From the construct++ I see a small crack in the language, the implementation of the serializing is brilliant, but the code itself is ugly in a way that only C++ (and maybe Perl) can make you write. Code that is so ugly that only Bjarne Stroustrup can love. This is not Tomer's fault, it's 100% C++ to blame. But if one don’t find this kind of syntax to be discouraging from using the language, take a look at the lambda functions of C++11. For example:
auto func = [] () { cout << “Hello world”; };
You put it into a template and you got yourself every type of brackets in one line of code… Ugh...
But ugly syntax is nothing to be afraid off. A simple example of where the language is really broken could be derived from using both Templates and Forward declarations in the same code.

  1. Using templates. Note that Template definition must be done completely from the header file.
  2. Forward declaration. Two classes are using each others methods, for example DB of cars and owners data, if a car class needs to ask a person class about weight, and person needs to ask about the size of the car. In that case, the implementation has to be done in the CPP file, while the each header will have a forward declaration of the other class.

If the two classes are both templates and referring each other, they can’t be in the header file nor can they be in the CPP file . One might say this is a bad design, but in my opinion it’s possible to find a good case where this kind of design is required.
Anyhow, the entire C++ implementation of templates has a wrong attitude for the problem. Templates came to introduce simple compile time code generator to the language. Templates is a poor implementation of a code generator for C++, that is just a bit aware of the code. This is a perfect example of how code generator should never be implemented. Not only is this code generator unreadable, it also imposes new restrictions on your code, which makes none code generated code to be less effective.
Of Course I’m not the first person to complain about C++. Most notable is Linus here: which I can’t agree more with.
As for the bad syntax:


Friday, September 26, 2014

How to Terminate a Terminator

Today we celebrate 30 years from the release of The Terminator. The story of a machine which was sent from the future to kill John Connor before he was even born.
Ever since I watched that movie, I was damn afraid that one day the machine will over rise and kill us all. Arnold is quite a scary dude.
What a tough motherfucker.

Recently I decided to cope with my nightmares, and watch the entire series all over again. But this time I’m doing it not to be scared, but to gather information about future robotics to be ready to fight it when the day will come. As a software guy, I decided to focus on what makes these cyborgs tick. And as far as it goes to the first module they sent back in time, a Motorola 6502 is the main CPU.

In the picture above, we can see the very first picture taken from the eye of the Terminator. We see it got some kind of compass (Just like the iPhone), and it compiles some code.
Wait. What?!
Yes, it compiles some code, from source. It's even got remarks to help us understand the code. It defines some constants and then moves to the next operation.

Now it’s done with compiling, so it checks the checksum, to see that there are no errors or malwares in the binary (On the right). On the left side, we can see a binary dump of the code, what brings
up the inescapable conclusion that this CPU got 16bit address space, so bill gates was right after all, 64k should be enough for anyone.

If anyone wants to go through the process I went by he would probably need the instruction set for this CPU, so here is a link to it
Bottom line, the code seems like some kind of checksum calculator subroutine (I hate this name, why not call it a function, subroutine sounds so old).
Next we get a more readable code, with many comments, lucky for us, this cyborg left us lots of debugging information.
We now know the address in memory of much of the code, what would become handy if we would like to exploit this cyborg.
More code on the bottom, and some data table on the left.
At this point of the movie we see the HID of the cyborg for the first time. We see that it’s got many options to choose from, and it takes it a long time to move around the menu and choose the desired answer. What a UI nightmare. Besides, we see that it uses some kind of ASCII art to display stuff on the right top corner, in higher resolution than before, therefore it’s very hard to read it.
Some OCR app, very cool. From now on, I use CAPTCHs for any private detail that might uncover my true location. I thought that the M6502 can’t do much, but then again, these cyborgs must be very good programmers.
Again code checksum, I think the dog has scared the cyborg, so it went into catatonic mode of rechecking everything.
Same code as before, This must be some kind of code that the cyborg must execute before it can work a gun. We defiantly need to find flaws here.
I’m starting to think that the entire cyborg is made out of two subroutines that just checks for errors on each other. Here we find a new kind of application that is not made of any ASCII-art, and might be scanning for something. It’s hard to see since the resolution is very bad here.
Look at this, a car reversing application. It works very fast and it gives results in no time. It uses many English letters and glowing pictures of what you are supposed to push. We are going to have to put some anti-debugging stuff on all the cars from now on.
That's about it for the terminator in this movie, we still got some pictures from future technology such as choppers.
Again lots of Motorola 6502 code, written in the most wired format I have ever seen.

That's about it with this version of the Terminator.
Next we have to deal with two mother fuckers of the next movie titled The Terminator - Judgment Day. One of the cyborgs is of the same module (T-800), we will soon see that it’s got a much different interface. The other is of a newer model (T-1000), made out of some kind of liquid metal. It's a shame that we don’t get to see any view from the eyes of the new cyborg, so we are going to have some troubles trying to kill it. Lets hope the future won't use this one against us. All of the pictures from this movie are of the T-800 point of view.
It scans to find a proper motor vehicle. No code here, we don’t know what kind of CPU it is running on. But we see it’s got a much better interface than the old model. The resolution is a bit better, and the font is much more human friendly. Although, it's still uses some ASCII-art at the table on the left (the stars that separate the title from the data), and why is it all written in English, Isn’t it supposed to be using some kind of binary language, or is this a debug log used by the developers.
The checksums are still there on the left.
Lots of useless data. I wonder if we can overflow the WGHT details with some fat guy, could be an interesting entry point.
Nice enhancement. And we got some code on the left. If it’s a machine code, then it’s RISC CPU and it’s 24 bits in size, not M6502 as far as I know.
Playing arcades, the 80’s were so much fun.
The end indeed.
Good driving application, would take the beats the Google Car, that's for sure. I wonder what the IMAGE LEVEL data stand for.
Next is a scene that is found on the DVD extra features only, in which we get to see a full reboot of the cyborg, lots of useful information.
The code is a bit strange... It's neither hex encoded nor ASCII.
Bingo, now we know the version of the firmware, and we know that it’s possible to upgrade it, maybe to make a nice nanny or a hairdresser out of this terminator. Cyberdyne Systems, lets google that, to see if we have anything to worry about, or anywhere to get a firmware update from.
O no! Apparently there is a company called Cyberdyne Systems,, and they even got a web site. I will drop them an email later to ask for the source code of the Series 800 model T01.
Next is some combat & tactics apps, so be extra careful with that.
That’s a huge potential damage 534053 543596 876 874798 4745757 44
And the alternative power program:
Information about the PCB and internals. It seems like a one layer old type of PCB, not very advanced.
Diagnostics screen, or debugger.
Many analog interferences, must be old CRT technology. Very strange for future technology...
Shuts down just like an old TV set.
Many years later, we got a new movie with again, two cyborgs, but this time we get to see the Point of view of both of them. We will start by examining yet another version of the T-800 Terminator.
It's getting older, but still a good looking by any standard.
The resolution got better. Font is about the same, the information moved to the left side of the display, defiantly a newer version of the interface.
Audio Waveform, must be a new module they got into the code, because this is the first time I see it. The analysis got much more data in it this time, but it seems more like monkey punching on the keyboard data than a machine code.
What's with the small magnifier (bottom left)? It's very good in finding clothes that fit well. I need this machine for the next time I go on shopping.
Not the checksum again!
And what’s with the Z-Buffer, is it like, calculating the distance from the object, is it tries to render some 3d graphics, is it part of the GPU?

Now we are really doomed, finally, after 3 movies they got a color screen! Where did these colors come from all of a sudden?
A big improvement on the UI, everything is set in boxes, and no more silly ASCII-art.
First, create the dialog box to display some information, it takes time...
Then the data appear. Well, the worst it can do is to kill 999 people, let's hope it would spend it on people that I don't like.
A terminator when damaged, starts printing *s and some random parts of the English dictionary. It also gets blurred vision of the data, and many analog interferences. Dude, check the cable, it seems not to be connected.
I told you not to launch all the applications at once, it makes your your system to faulty.
When a terminator is badly damaged, it starts seeing colors, just like a friend of mine who took some nasty stuff back in Goa India.
Next is the other cyborg which must be the most advanced machine we saw in the entire series.
I give up, take me and do your worst!
It’s got everything one might want with his cyborg a bluish awesome display and a modem-fax.
Was that before or after the Facebook?
It’s hard to read, but the title of the picture on the left says it’s running on auto-pilot, while in fact it’s driving a vehicle.
It’s much better than going on a killing spring of all the people with the same name, as it was done on the first movie.
Double checking.
It has this window that looks like a file icon on the left side, and it seems to be open all the time. Could that be a blind spot for the cyborg.
This cyborg got an endless count of features, it got all the apps found in the App Store and the Play Store combined.
Cool 3d rendering of a car on the right.

Too cool to be true.

As for the next movie (that's the 4th one), well, it’s very hard to gather information from this one, because most of the displays looks a little like this:
Or this even this:
There is a small fight going on with a the T-800 in this movie, so we do get a short glance at its new display here:
They finally left the M6502 behind.
They did it again, they changed the UI completely, just when you get used to something, they pull a new version and you have to learn all the shortcuts all over again. The message box seems a little bit similar to the old one, probably some code reusing.

That's about as far as the movies go, but I'm not going to sit back and let them all kill me while there is some much more to learn. Yes, The Sarah Conner Chronicles, which give out many new insights to what might come to hunt us down.
Summer Glau, I love you! Definitely the most beautiful and talented Terminator of them all.
With most enhanced GUI, User interface, Fonts and killing apps.
Termo vision,
And Kendal Safe breaking application.
She is so pretty, I had to put another picture.

Hope this Information would help you survive the next apocalypse which is coming to us on 29/8/1997, wait, what?! Nevermind this information you can forget all about it now...