Tuesday, August 12, 2014

Where does 116444736000000000 come from?

While dealing with time conversions functions of various languages / technologies, e.g. Windows API or Java, you might come across the magic constant 116444736000000000. You can also find it in several code snippets on the web - however it never nearly explained where it comes from... This I wanna change, here and now!

A time value is simply a counter of ticks since an defined start of time, called epoch. Not only the "epoch date" differs between various systems, but also the counter resolution, that is how often it is updated.

Windows uses in most functions the FILETIME structure, which represents the actual time as the number of 100-nanosecond intervals since January 1, 1601 (UTC).

Other platforms like Java/Android or some Unix functions represent a time value as the number of seconds elapsed since 00:00:00 January 1, 1970.

To convert between both worlds, you need an offset factor and that is... 116444736000000000. This value used as a FILETIME value, meaning 100ns intervals since 01-01-1601, represents exactly the data 01-01-1970!

Proof?
We know that 1ms = 1000000 ns = 10000 * 100 ns.
So 116444736000000000*1/100 ns = 11644473600000 ms = 11644473600 s.
Between 01-01-1601 and 01-01-1970, there are exactly 134774 days.
134774 * 24 (hours per day) * 3600 (seconds per hour) = 11644473600 s.

Tuesday, April 01, 2014

How many walls/borders has a nxn grid?

While implementing an algorithm for a hobby project, I came across the question how many walls (or borders) has a nxn grid? Here I present the solutions of my line of thought: the first one is quite complicated, but was also the first which came into my mind. After convinced of the correctness of this one, a much easier idea occured... :-)

Lets start: A 1x1 grid has 4 walls which is easy to see:
 _
|_| = 1 cells --> 4 walls

Lets have a look at a 2x2 grid:
 _ _
|_|_|
|_|_| = 2 cells --> 12 walls (feel free to count it :-)

So my idea to get the equation of a nxn grid is based on following image / procedure:

   1 2 ... n
   _ _     _
1 |_|_|...|_|
2 |_|_|_|_|_|   
   . _|   .
   _ _|   .
n |_|_|... _|
  
1. Start with the (orange) cell in the topleft corner which has 4 walls: 4
2. Lets move in the top row further to the right (cyan). For each of the remaining n-1 cells in this row, there are 3 walls per cell left: (n-1) * 3
3. The same applies if we go down in the first column step by step until the red cell. So for each cell there are also 3 walls left: (n-1) * 3
4.  Then there is (n-1) x (n-1) grid left. However if we go there row by row and column by column, it turns out that for each cell only two more walls come along (the wall at the bottom and at the right), because the left and top wall were then already taken into account. So (n-1) * (n-1) * 2 walls are left.

So in total we have come up with following equation for an nxn grid:
x = 4 + (n-1)*3 + (n-1)*3 + (n-1)*(n-1)*2
   = 4 + 3n - 3 + 3n - 3 + (n^2 -2n +1)*2
   = 4 + 6n - 6 + 2n^2 - 4n + 2
   = 2n^2 + 2n
   = 2n (n+1)

Manually checking of the cases n=3 and n=4 turned out to be correct, so I guess it's ok.

However, afterwards a more simple solution came into my mind by just looking at the vertical walls only and at the horizontal walls separately:

   1 2 ... n
   _ _     _
1 |_|_|   |_|
2 |_|_|   |_|
  .
   _ _     _
n |_|_|...|_| 

1. If just looking at the vertical walls (red), we see that there are n rows, each having n+1 walls: n * (n+1) walls in total.
2. The same applies for the horizontal walls (cyan): n columns * (n+1) walls.

This is in total:
y = n * (n+1) + n * (n+1)
  = n^2 + n * n^2 + n
  = 2n^2 + 2n
  = 2n (n+1)
  = x

That's it :-)

Monday, February 10, 2014

My Book Word Edition + Twonky Server: How to access the root folder of the NAS

I own a Western Digital 'My Book World Edition' (that's a simple NAS) for some years now, mostly as backup and central data access point.
After having purchased a smart TV, finally I was motivated to activate the TwonkyMedia server to stream media via DNLA.
Everthing went fine, however only the media files in the \public folder of the NAS were indexed by Twonky and I did not manage to get access to the whole NAS via the Twonky browser. Pretty bad as I was used to my private well-known folder structure and not willing to change it.
I did not manage to fix the problem for some time, and for some reason I did not find anything useful on the net, until I came across this topic on WD community forum:

http://community.wd.com/t5/Other-Network-Drives/USB-Media-Share-TwonkyMedia-Server/td-p/28117

Here the main information with the single command to adjust the start location, thanks to user garyg:
This is because the shares start at /DataVolume/Public.  If you change the default start location to /shares, or maybe it's /Shares, you will be able to access anything which has been defined as a share on the MyBook.

The command to fix the USB sharing problem is:
         http://<nas>:9000/rpc/set_option?contentbase=/shares

where <nas> is the name or IP address of the My Book.  This command is entered into your web browser's address bar.  The browser will respond with a page that says "/shares".

Thursday, November 15, 2012

My alternative to HTML frames

Maybe some of you have noticed that I have updated my homepage. Well, the content remained more or less the same but the layout was finetuned.
Furthermore, the previous pages were created using a WYSIWYG tool which resulted in pretty ugly, overbloated HTML. Of course it was also hard to maintain and lacked of HTML validity.

The new pages were manually written (hm okay.. copy-pasted). The biggest difference is the fact that they are not frame-based anymore but solely CSS-based (I avoided to use tables for solely layout purposes as much as I could).
But when searching the net for alternatives to frames, my plan turned out to be more difficult than expected. Using I-Frames was no choice. PHP is not supported by my webspace and I was not willing to pay more just for some 'include' directives.
On the other hand, each change of the main layout (header at the top, meu at the left border) would have meant to manually adapt each html file... this is unacceptable.

So what to do?
Actually I came out with my own solution:
1. I started by coding a main layout template file which contains the general layout without any content:



2. This template was then used as base for every html page of my site. Well, so far no advantage. But in each file I put some markers to identify which parts of the html source belongs to the template and which parts contain the actual new content. These markers are just pre-defined HTML comments like:


here comes may cool individual HTML content



3. Then I programmed a fancy tool which compares all files belonging to my site to the latest template. The markers help to identify the regions which belong to the template file and which are individual content. When the template changes, my tool can update the page files in a way that only the template regions are updated but the actual content is not touched. Here a screenshot of my tool:


A single button click will then update all sites :-)

What do you think of this solution?
Well, I must admit it took some time to code it and get it correctly working, but it was fun to write it and it works like a charm.
.. I love my own-invented solution!

Saturday, December 03, 2011

Reason for homogeneous (4D) coordinates in computer graphics

Homogeneous coordinates are used computer graphics - you can read this statement in every 3D computer graphics related book or article. If you ever asked yourself why this is the case, then you are at the right place...
The reason for this is to handle rotation, scaling and translation in a common way. While you can handle rotation and scaling using 3x3 matrices and vectors with 3 components, translation cannot be processed - unfortunately this is normally not further explained although it is quite easy to see:
Suppose we have a point p with coordinate (x, y, z) and we want to translate it by a distance defined by the translation vector t=(tx, ty, tz).
Of course, what you need to do to get new position p' of point p after translation is to add t to p:
p' = p + t, which means
p'.x = p.x + tx
p'.y = p.y + ty
p'.z = p.z + tz
If you try to create a 3x3 matrix M to perform this operation by p' = M * p, you get into trouble. What we require is:

x'   x + tx   |a b c|   |x|
y' = y + ty = |d e f| * |y|
z'   z + tz   |g h i|   |z|

x + tx = ax + by + cz (1)
y + ty = dx + ey + fz
z + tz = gx + hy + iz

Looking at equation (1) we directly see that a = 1 and we are left with tx = by + cz. But the translation in x-direction must be indepent of y and z coordinates, so b and c must be zero.

x + tx = 1*x + 0*y + c*z = x   => tx = 0
So only the primitive translation by zero is possible. The same applies for the equations for y and z.

Using homogeneous coordinates, that is to add a forth component w = 1 to each point, we can derive a 4x4 matrix for translation without any problems.


x + tx   |a b c d|   |x|
y + ty = |e f g h| * |y|
z + tz   |i j k l|   |z|
1        |m n o p|   |1|

we get e.g. for x-coordinate:

x + tx = ax + by + cz + d, 
so a = 1 and d = tx which leads us to the final translation matrix:

    |1 0 0 tx|
M = |0 1 0 ty|
    |0 0 1 tz|
    |0 0 0 1 |

That's it :-)




Sunday, January 09, 2011

Browsing old reversing sites - the good old times

I just browsed through an old backup folder called "Reversing favourites" ... man this contains really OLD urls of reversing sites: groups, tutorials and so on!
Of course, I clicked several of them, full of hope, just to get disappointed that ca. 90% of the urls do not exist anymore. But some of the linked site still exist, but of course not updated for several years - just lying dead on some server.
I engrossed the thoughts what really cool sites existed at that time, now approximately 10 years ago. Here are some that are still online but out-of-date - maybe some of you want also fall in some nostalgy.
So here are my three top sites that are still online available:

  • Immortal Descendants - this is one of my all-time favourite reversing sites. The tutorials and the code snippets were outstanding at that time, at least for me. I linked a working archived version of the site, the original URL was http://www.immortaldescendants.org.
  • BIW Reversing - not that old but still no update in the last two years
  • Crackstore - one of the first reversing sites I have ever visited
Let's see, next time I post the URLS of the top site of nowadays... stay tuned!

Friday, September 24, 2010

Injecting a dll to explorer.exe under windows 7 - FAILED

Today I tried to get an old self-coded tool to work under windows 7 64bit... I'm talking about my Desktop Symbol Utility I coded for and under windows XP.
In fact, I really do not require it because I managed to deactivate the dropshadow of the icon texts on the desktop while keeping the transpareny under windows 7 - without the help of an external tool! :-) Wow, finally Microsoft got it working... after how many years?

Howsoever, I found this tool accidentally again on my harddrive while searching for something else and tried to run it under my windows 7. As I had already expected, injecting the dll into explorer process did not work. So I extracted the code and had a look into it. I expected some typical error as it was programmed under a 32bit system and maybe the is some incompatible/wrong use of data types? Well, unfortunately not. It turned out that the CreateRemoteThread() call failed. My first assumption that the way the process ID is obtained is no longer valid under win7 was wrong - a simple compare with the PID in Process Explorer showed it. Also the other functions (VirtualAlloc etc.) succeeded.
Maybe it something to do with protected processes? (-> Link to article from Microsoft).
Well, I don't know... so if there is someone out there who got this tool working under Win7-64, please drop me some lines how you managed it - would be really great!