<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Vlad Ioan Topan</title>
	<atom:link href="http://vtopan.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://vtopan.wordpress.com</link>
	<description>My playground</description>
	<lastBuildDate>Sat, 31 Dec 2011 16:58:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='vtopan.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Vlad Ioan Topan</title>
		<link>http://vtopan.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://vtopan.wordpress.com/osd.xml" title="Vlad Ioan Topan" />
	<atom:link rel='hub' href='http://vtopan.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Funky Python &#8211; code snippets</title>
		<link>http://vtopan.wordpress.com/2011/03/17/funky-python-code-snippets/</link>
		<comments>http://vtopan.wordpress.com/2011/03/17/funky-python-code-snippets/#comments</comments>
		<pubDate>Thu, 17 Mar 2011 00:37:15 +0000</pubDate>
		<dc:creator>vtopan</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[Snippets]]></category>

		<guid isPermaLink="false">http://vtopan.wordpress.com/?p=181</guid>
		<description><![CDATA[Python is a great programming language for a number of reasons, but one of it&#8217;s best features is the fact that, as per Python Zen item #13, &#8220;There should be one&#8211; and preferably only one &#8211;obvious way to do it.&#8221; Working with the beast for a number of years, however, does expose one to some [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vtopan.wordpress.com&amp;blog=6592892&amp;post=181&amp;subd=vtopan&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><strong>Python</strong> is a great programming language for a number of reasons, but one of it&#8217;s best features is the fact that, as per <a href="http://www.python.org/dev/peps/pep-0020/">Python Zen</a> item #13, &#8220;There should be one&#8211; and preferably only one &#8211;obvious way to do it.&#8221; Working with the beast for a number of years, however, does expose one to some less pythonic and somewhat quirky design points (maybe even a few gotchas); here are some of them.</p>
<h2>Python quirks &amp; gotchas</h2>
<h3>1. Boolean values in numeric contexts evaluate to 0/1</h3>
<p>It&#8217;s very intuitive (especially when coming to Python from C) for 0 values to evaluate to False in boolean contexts and non-zero values to True. Having False evaluate to 0 and True to 1 in numeric contexts is however less intuitive (and somewhat useless):<br />
<pre class="brush: python;">&gt;&gt;&gt; a = [1, 2, 3]
&gt;&gt;&gt; a[True], a[False]
(2, 1)
&gt;&gt;&gt; True + True # this one was rather unexpected...
2</pre></p>
<h3>2. The default argument values are evaluated at the point of function definition in the defining scope</h3>
<p>This is probably one of the most frequent gotchas out there:<br />
<pre class="brush: python;">&gt;&gt;&gt; def a(b=[]):
...     b.append(3)
...     print b
&gt;&gt;&gt; a()
[3]
&gt;&gt;&gt; a()
[3, 3]</pre><br />
The proper way to do this is to set b&#8217;s default value to <em>None</em> in the declaration and set it to [] inside the function body if it&#8217;s set to <em>None</em>:<br />
<pre class="brush: python;">&gt;&gt;&gt; def a(b=None):
...     if b is None: b = []        
...     b.append(3)
</pre></p>
<h3>3. *Everything* is an object</h3>
<p>Although it&#8217;s a fact which may escape even the best of programmers at the beginning, Python is actually object oriented to the core. Despite the fact that it allows you to write procedural (and even functional<em>ish</em>) code, <strong>everything</strong> is an object. Functions are objects, data types are objects etc. This:<br />
<pre class="brush: python;">69.bit_length()</pre><br />
doesn&#8217;t work because the period is parsed as part of the numeric token (think of 69.j or 69.e1); this however:<br />
<pre class="brush: python;">(69).bit_length()</pre><br />
works. Being objects, functions can also have attributes:<br />
<pre class="brush: python;">&gt;&gt;&gt; def a():
...    print a.x
&gt;&gt;&gt; a.x = 3
&gt;&gt;&gt; a()
3</pre><br />
This comes in handy e.g. for giving a decorated function the same internal name (for introspection purposes) as the original function:<br />
<pre class="brush: python;">def print_call_decorator(fun):
    def replacement(*args, **kwargs):
        res = fun(*args, **kwargs)
        print r'Call %s.%s =&gt; %s' % (inspect.getmodule(fun).__name__, fun.__name__, res)
        return res
    replacement.__name__ = fun.__name__
    return replacement</pre></p>
<h3>4. Generators, sets &amp; dictionaries also have comprehensions (called &#8220;displays&#8221;)</h3>
<p>As you probably know, list comprehensions are a great way to generate a new list from another iterable. But it goes further&#8230; Generators, sets and dicts also have something similar, called <a href="http://docs.python.org/reference/expressions.html#displays-for-sets-and-dictionaries">displays</a>. The basic syntax is this:<br />
<code>generator = (value for ... in ...)<br />
dict = {key:value for ... in ...}<br />
set = {value for ... in ...}</code><br />
E.g.:<br />
<pre class="brush: python;">&gt;&gt;&gt; a = ['a', 'b', 'c']
&gt;&gt;&gt; d = {x:a.index(x) for x in a}
&gt;&gt;&gt; d
{'a': 0, 'c': 2, 'b': 1}
&gt;&gt;&gt; d_rev = {d[x]:x for x in d}
&gt;&gt;&gt; d_rev
{0: 'a', 1: 'b', 2: 'c'}</pre><br />
This makes reversing a dictionary for example much cleaner.<br />
What makes displays even more fun are the <a href="http://docs.python.org/reference/expressions.html#list-displays">*list* displays</a>, which are essentially list comprehensions but with unlimited depth; using them to flatten a list of lists would look something like this:<br />
<pre class="brush: python;">flat = [x for y in lst for x in y]</pre><br />
The x/y order in the example is not a mistake; that&#8217;s actually the proper order.</p>
<h3>5. GIL: Python threads <em>aren&#8217;t</em></h3>
<p>Not on multi-processor machines, at least. Yes, there is a threading module (aptly named), but due to the <a href="http://en.wikipedia.org/wiki/Global_Interpreter_Lock">Global Interpreter Lock</a>, threads of the same (Python) process can&#8217;t actually run at the same time. This becomes more of an issue when deploying native-Python servers, as they don&#8217;t get any benefit from the number of cores installed on the machine (drastically limiting the number of open sockets a Python process can handle at the same time as opposed to a native one written in C).</p>
<h3>6. <strong>for</strong> has an <strong>else</strong> clause</h3>
<p>&#8230;and so do the <em>try&#8230;except/finally</em> and <em>while</em> constructs. In all cases, the <em>else</em> branch is executed if all went well (<em>break</em> wasn&#8217;t called to stop cycles / no exception occurred). And while the <em>else</em> branch may be useful to perform when you want something to happen only if the cycle construct wasn&#8217;t &#8220;broken&#8221; (the classic example is handling the fact that the cycle hasn&#8217;t found the value it was looking for), <em>try</em> doesn&#8217;t really need an <em>else</em> clause, as the following are equivalent and the latter seems at least to me more readable:</p>
<ul>
<li>with <em>else</em>:<br />
<pre class="brush: python;">try:
    this_may_crash()
except:
    handle_it()
else:
    call_me_if_it_didnt_crash()</pre>
</li>
<li>without <em>else</em>:<br />
<pre class="brush: python;">try:
    this_may_crash()
    call_me_if_it_didnt_crash()
except:
    handle_it()</pre>
</li>
</ul>
<h3>7. Tuple assignment order</h3>
<p>In tuple assignments, the left-values are eval&#8217;ed &amp; assigned in order:<br />
<pre class="brush: python;">&gt;&gt;&gt; a = [1, 2, 3]
&gt;&gt;&gt; i, a[i] = 1, 5 # i is set to 1 *before* a[i] is evaluated
&gt;&gt;&gt; a
[1, 5, 3]</pre><br />
This happens because tuple assignments are equivalent to assigning the unpacked pairs in order; the second line above is therefore equivalent to:<br />
<pre class="brush: python;">&gt;&gt;&gt; i = 1
&gt;&gt;&gt; a[i] = 5</pre></p>
<h3>8. Scope juggling</h3>
<p>Inside a function, variables are resolved in the global scope if no direct variable assignment appears in the<br />
function, but are local otherwise (making Python clairvoyant, as it is able to tell that something is going to happen later on inside the function, i.e. a variable will be set). Note that attributes and sequence/dict values can still be set, just not the &#8220;whole&#8221; variable&#8230;<br />
<pre class="brush: python;">a1 = [1, 2, 3]
a2 = [1, 2, 3]
b = 3
c = 4
def fun():
    global b
    print c # crashes, because c is resolved to the local one (which is not set at this point)
    print b # works, because the global directive above forces b to be resolved to the global value
    a1[0] = 4 # works, because a1 is not directly set anywhere inside the function
    a2[0] = 5 # crashes, because a2 is later on directly set
    c = 10
    b = 11
    a2 = 'something else'</pre></p>
<h2>Bonus facts</h2>
<p>As a bonus for making it through to the end, here are some lesser known / less frequently pondered upon facts about Python:</p>
<ol>
<li>a reference to the <em>current</em> list comprehension (from the inside) can be obtained with:<br />
<pre class="brush: python;">locals()['_[1]'].__self__</pre>
</li>
<li>list comprehensions can contain any number of <em>for</em>&#8230;<em>in</em> levels (this is actually documented). Flattening lists:<br />
flat = [x for y in lst for x in y]
</li>
<li><em>range() </em>actually builds a list, which can be slow and memory-consuming for large values; use <em>xrange()</em></li>
<li>modules have a <em>dict</em> attribute .__dict__ with all global symbols</li>
<li>the <em>sys.path</em> list can be tampered with before some imports to selectively import modules from dynamically-generated paths</li>
<li>flushing file I/O must be followed by an os.fsync(&#8230;) to actually work:<br />
<pre class="brush: python;">f.flush()
os.fsync(f.fileno())</pre>
</li>
<li>after instantiation, object methods have the read-only attributes <em>.im_self</em> and <em>.im_func</em> set to the current object&#8217;s class and the implementing function respectively</li>
<li>some_set.discard(x) removes x from the set only if present (without raising an exception otherwise)</li>
<li>when computing actual indexes for sequences, negative indexes get added with the sequence length; if the result is still negative, <em>IndexError</em> is raised (so [1, 2, 3][-2] is 2 and [1, 2, 3][-4] raises <em>IndexError</em>)</li>
<li>strings have the <em>.center(width[, fillchar])</em> method, which padds them centered with <em>fillchar</em> (defaults to space) to the length given in <em>width</em></li>
<li>inequality tests can be chained: 1 &lt; x &lt; 2 works</li>
<li>the minimum number of bits required to represent an integer (or long) can be obtained with the integer&#8217;s <em>.bit_length()</em> method</li>
</ol>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/vtopan.wordpress.com/181/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/vtopan.wordpress.com/181/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/vtopan.wordpress.com/181/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/vtopan.wordpress.com/181/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/vtopan.wordpress.com/181/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/vtopan.wordpress.com/181/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/vtopan.wordpress.com/181/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/vtopan.wordpress.com/181/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/vtopan.wordpress.com/181/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/vtopan.wordpress.com/181/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/vtopan.wordpress.com/181/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/vtopan.wordpress.com/181/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/vtopan.wordpress.com/181/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/vtopan.wordpress.com/181/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vtopan.wordpress.com&amp;blog=6592892&amp;post=181&amp;subd=vtopan&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://vtopan.wordpress.com/2011/03/17/funky-python-code-snippets/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/539dcced66dd05d6ba7009aca8abf164?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">vtopan</media:title>
		</media:content>
	</item>
		<item>
		<title>Switching to Linux (1)</title>
		<link>http://vtopan.wordpress.com/2010/11/11/switching-to-linux-1/</link>
		<comments>http://vtopan.wordpress.com/2010/11/11/switching-to-linux-1/#comments</comments>
		<pubDate>Thu, 11 Nov 2010 02:56:55 +0000</pubDate>
		<dc:creator>vtopan</dc:creator>
				<category><![CDATA[free software]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[papers]]></category>
		<category><![CDATA[system]]></category>

		<guid isPermaLink="false">http://vtopan.wordpress.com/?p=165</guid>
		<description><![CDATA[Since my first Linux &#8220;experience&#8221; (which happened some eight years ago during a CS lab at UTCN), every couple of years I spend somewhere between a few days to several weeks trying to switch over to &#8220;the other side&#8221;. The reasons behind these (as of yet futile) attempts revolve mostly around the concept of &#8220;freedom&#8221;, [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vtopan.wordpress.com&amp;blog=6592892&amp;post=165&amp;subd=vtopan&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Since my first Linux &#8220;experience&#8221; (which happened some eight years ago during a CS lab at UTCN), every couple of years I spend somewhere between a few days to several weeks trying to switch over to &#8220;the other side&#8221;. The reasons behind these (as of yet futile) attempts revolve mostly around the concept of &#8220;freedom&#8221;, and are beyond the scope of this writing. What I find of greater interest is the evolution of Linux-based OSes, and in particular of their target audience, which shifts more and more from hardcore enthusiasts willing to spend countless hours setting up a new machine toward average computer users (even the ones of the &#8220;point-and-clicky&#8221; variety). </p>
<h3>The &#8220;sparse&#8221; look</h3>
<p>My first Linux (Mandriva) was text-only; <a href="http://en.wikipedia.org/wiki/X_Window_System">X-Window</a> was in an &#8220;almost-working&#8221; state (crashing often, and even more often not being able to start at all) on most Linux machines I touched back then. Some time later most distributions had GUIs, but all the relevant work was still done beyond the scenes by console programs, which is still the case today. The <a href="http://en.wikipedia.org/wiki/File:Gldt1009.svg">myriad of &#8220;flavors&#8221;</a> (and window managers) has made it practically impossible to write even remotely portable GUI interfaces for Linux, so graphic interfaces get &#8220;strapped&#8221; (pardon the low-brow hint; it truly feels like <em>le mot juste</em>) on console programs. </p>
<p>And that&#8217;s precisely how most Linux GUIs look and feel like: painful. Most of the window space is simply wasted: text is larger than necessary (for most people) and controls are separated by vast amounts of empty space, giving the interfaces a very &#8220;sparse&#8221; look. And deeper on the causality chain of problems is the fact that there simply isn&#8217;t that much exposed functionality in most Linux GUIs. Although to a much lesser extent than it was the case years ago, you still have to drill down way beyond the graphical interface in order to accomplish most non-trivial tasks. And then there&#8217;s responsiveness. After being spoiled by native graphical interfaces (some optimized to the point of writing machine assembly-level code) with excellent responsiveness (as it is more often than not the case on Windows), the sensible lag experienced on most interactions with Linux GUIs tends to annoy me in a very subtle manner. <span style="font-size:90%;">Then there&#8217;s Java and Java-based GUIs, which make Linux GUIs feel lightning fast, but I won&#8217;t go there.</span></p>
<h3>Along came Ubuntu</h3>
<p>My most recent attempt at Linux started a couple of months ago, but was interrupted by even-more-work-than-usual at the job, and would have been completely forgotten and abandoned if not for a Linux-vs-Windows themed conversation with my coworkers. I complained about most of the things that annoyed me about Linux (no decent music player is near the top of my list), and after getting many answers along the main theme of &#8220;software X has come a long way since <em>then</em>&#8220;, I decided to actually give it (yet) another chance. </p>
<p>My previous experience with Ubuntu (8.04 I think) had been almost pleasant, by far more so than any other previously-tried flavor (most notable being Mandriva back when it was called Mandrake and Red Hat at home and CentOS at work), so Ubuntu 10.10 felt like the way to go. After some research regarding &#8220;the most popular Linux&#8221;, <a href="http://www.linuxmint.com/">Linux Mint</a> popped up as a tempting Ubuntu/Debian-based alternative. The sheer volume of documentation / user-assistance available for the vanilla Ubuntu convinced me to stick with it, and so far it has been the right decision: as I&#8217;ve become accustomed when setting up a Linux OS, I&#8217;ve had problems requiring &#8220;workarounds&#8221; from the first day.</p>
<h3>The good</h3>
<p>In spite of the minor technical &#8220;misadventures&#8221; during setup, the Ubuntu 10.10 GUI finally feels mature (and <em>almost</em> responsive *enough*). The themes look good, the fonts are readable even at smaller sizes etc. And then there&#8217;s the repositories: thanks to my recently-acquired 100Mbit Internet connection, in a few hours after the installation I was already playing a pretty good-looking FPS (Assault Cube) and enjoying it. I&#8217;m not much of a gamer, but on one hand I was curious how far free games have come along, and on the other I had a lot of blood-spill-requiring-frustration left over from working out what should have been minor kinks and turned into major research themes.</p>
<p>I actually managed to set up both my PPP Internet connection and a VPN to my workplace without much hassle, which is a notable first. The VPN actually works better than on Windows because I have a convenient checkbox option to only route traffic going towards the VPN server&#8217;s network through it (as opposed to manually deleting the 0.0.0.0 route through the VPN server on Windows, because some clever bloke figured I *must* want *all* my Internet traffic to be routed through a gateway which only knows private addresses).</p>
<h3>The bad</h3>
<p>The NTFS driver (ntfs-3g). It&#8217;s not bad <em>per se</em>, in fact it also has &#8220;come a long way&#8221; and when it works, it works fine. But in one instance it *chose* not to work for me, which I found very frustrating and annoying. My problem (and it seems to be a rather common one) is that on a recently-acquired USB hard-disk Windows <em>appears</em> to have messed up either the partition table or the NTFS filesystem; the problem is that it only <em>appears</em> that way to the ntfs-3g driver. Which is not to say that it&#8217;s wrong (from what I could gather, the size of the filesystem is set to a larger value than there actually is room on the disk, the difference being of a few sectors = a few KB); it&#8217;s just that Windows doesn&#8217;t seem to mind and reads from/writes to the disk without problems. I imagine that if I were to write to those last few KB on the disk the data would be lost, but at least <em>I can access the data on the disk</em>, which ntfs-3g won&#8217;t allow, because it wouldn&#8217;t mount the disk even in read-only mode. Adding insult to injury, the &#8220;Tuxera CTO&#8221; (an otherwise friendly and helpful person) suggests (<a href="http://www.tuxera.com/forum/viewtopic.php?f=3&amp;t=1125">here</a>) that the only solution to ignore the warning is to &#8220;change the sourcecode&#8221;. Booting back into Windows, backing up the data and reformatting the drive to a smaller size fixed the problem, but it shouldn&#8217;t have been necessary, and the &#8220;I know what&#8217;s right for you better than you &#8217;cause I&#8217;m the pro&#8221; attitude was somewhat disappointing.</p>
<p>Another problem is the lack of a decent file manager. After using all the &#8220;commanders&#8221; (Norton Commander, then the amazing Dos Navigator and nowadays Total Commander), I&#8217;m used to having a software which can handle all file-related operations (and I do a lot of them for my day job) easily and efficiently. <a href="http://www.ghisler.com/">TC</a>, which I wholeheartedly recommend on Windows, handles everything just fine. On Linux, so far I haven&#8217;t even been able to find a (GUI) file manager with an actual &#8220;brief&#8221; view mode; all of them insist on giving me a long line of information about each file, whether I actually need it or not, and waste about two thirds of the available screen space in the process. All the features offered by TC, not to mention the plethora of plugins available for it, are still far, far way. And since we&#8217;ve hit the sensitive point of software equivalents for Linux, here&#8217;s what I&#8217;ve managed to find so far.</p>
<h3>Software alternatives for Linux</h3>
<h4>File manager</h4>
<p>As mentioned above, I&#8217;m profoundly dissatisfied with what I&#8217;ve found so far. <a href="http://www.midnight-commander.org/">MC</a> is a must, but lacks many features. <a href="http://doublecmd.sourceforge.net/">Double Commander</a> seems be the best contender, and is built to be similar to TC (going as far as plugin interchangeability, if only there were any ELF plugins for TC&#8230;), which is a plus.</p>
<h4>Music player</h4>
<p>After finding a decent music player (i.e. one which is stable and has a compact interface like WinAMP and the other *AMPs on Windows) has been a seemingly impossible feat for years, along came <a href="http://audacious-media-player.org/screenshots">Audacious</a>, and all became well.</p>
<h4>Image viewer</h4>
<p>If good file managers and music players are hard to come by in Linux, image viewers are far more challenging. Neither one seems to grasp the basic concept of viewing a folder of images; all of them insist on &#8220;organizing my collection of photos&#8221; (&#8217;cause it&#8217;s trendy to index collections of stuff), and offer either very cumbersome methods of simply browsing image folders, or simply no way at all (except for, of course, indexing/organising the folders into a collection). The excellent XnView image viewer for Windows has a multi-platform version aptly-called <a href="http://newsgroup.xnview.com/viewforum.php?f=60">XnView MP</a>, with the downside that development is favored for the Windows version and Linux builds don&#8217;t come for each version.</p>
<h4>Other</h4>
<p>I&#8217;m still looking into options for a <strong>development IDE</strong> (for C and Python in particular), with no luck as of yet.</p>
<p>As far as <strong>web browsing</strong> is concerned, all the relevant alternatives for Windows (Opera, Firefox and Chrome) are present on Linux, and from the order of the above enumeration my browser of choice should be obvious enough.</p>
<p>For an <strong>office suite</strong> I use OpenOffice on Windows, which is also available on most platforms.</p>
<p>I strongly recommend <strong>Guake</strong> as a terminal and <strong>Gnome Do</strong> as a generic application/document opening method.</p>
<p>[To be continued]</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/vtopan.wordpress.com/165/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/vtopan.wordpress.com/165/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/vtopan.wordpress.com/165/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/vtopan.wordpress.com/165/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/vtopan.wordpress.com/165/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/vtopan.wordpress.com/165/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/vtopan.wordpress.com/165/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/vtopan.wordpress.com/165/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/vtopan.wordpress.com/165/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/vtopan.wordpress.com/165/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/vtopan.wordpress.com/165/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/vtopan.wordpress.com/165/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/vtopan.wordpress.com/165/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/vtopan.wordpress.com/165/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vtopan.wordpress.com&amp;blog=6592892&amp;post=165&amp;subd=vtopan&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://vtopan.wordpress.com/2010/11/11/switching-to-linux-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/539dcced66dd05d6ba7009aca8abf164?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">vtopan</media:title>
		</media:content>
	</item>
		<item>
		<title>Mafia Wars tips, tricks &amp; fixing problems</title>
		<link>http://vtopan.wordpress.com/2010/01/25/mafia-wars-tips-tricks/</link>
		<comments>http://vtopan.wordpress.com/2010/01/25/mafia-wars-tips-tricks/#comments</comments>
		<pubDate>Mon, 25 Jan 2010 19:33:09 +0000</pubDate>
		<dc:creator>vtopan</dc:creator>
				<category><![CDATA[games]]></category>
		<category><![CDATA[Bangkok]]></category>
		<category><![CDATA[bookmarklets]]></category>
		<category><![CDATA[facebook]]></category>
		<category><![CDATA[Mafia Wars]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[tricks]]></category>

		<guid isPermaLink="false">http://vtopan.wordpress.com/?p=143</guid>
		<description><![CDATA[Mafia wars I, like many others, have fallen victim to Zynga&#8217;s Mafia Wars, the &#8220;game&#8221; that looks like just-another-text-based-massively-multiplayer-something-something not worth clicking into, and yet becomes highly addictive if given the chance (in fact, it&#8217;s how I got to have a Facebook account and the only actual use I&#8217;ve found for the afore-mentioned account). This [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vtopan.wordpress.com&amp;blog=6592892&amp;post=143&amp;subd=vtopan&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<h3>Mafia wars</h3>
<p>I, like many others, have fallen victim to Zynga&#8217;s <strong>Mafia Wars</strong>, the &#8220;game&#8221; that looks like just-another-text-based-massively-multiplayer-something-something not worth clicking into, and yet becomes highly addictive if given the chance (in fact, it&#8217;s how I got to have a Facebook account and the only actual use I&#8217;ve found for the afore-mentioned account). This is not a rant on it&#8217;s impressive success however, but a few useful things I&#8217;ve learned about the game.</p>
<h3>Job help / boss defeat links</h3>
<p>I got to writing this text because one of my Bangkok boss defeat links didn&#8217;t publish. And since that would&#8217;ve brought me ~500K &#8211; 1 million &#8220;Baht&#8221;, I *had* to figure out how to fix it. Since it might actually be of use to other MW-victims, here goes the info.</p>
<h4>Your Facebook ID</h4>
<p>You need your Facebook ID; hover over a link to your name/homepage anywhere on Facebook and get it from the link. The link should look something like: http://www.facebook.com/profile.php?id=<strong>your-facebook-ID</strong>&amp;ref=nf</p>
<h4>Bangkok &#8211; boss</h4>
<p></p>
<ul>
<li><b>Brawler</b> (1st) episode:
<p>http://apps.facebook.com/inthemafia/track.php?next_controller=story&#038;next_action=claim_boss_bonus&#038;zy_track=newsfeed&#038;from=<b>your-facebook-ID</b>&#038;cityId=4&#038;jobId=37&#038;ztrack_category=bangkok_boss_defeated&#038;friend=<b>your-facebook-ID</b>&#038;next_params=%7B%22target_id%22%3A%22<b>your-facebook-ID</b>%22%2C+%22job_city%22%3A%224%22%2C+%22job_id%22%3A%2218%22%7D&#038;ref=nf</li>
<li><b>Criminal</b> (2nd) episode:
<p>http://apps.facebook.com/inthemafia/track.php?next_controller=story&#038;next_action=claim_boss_bonus&#038;zy_track=newsfeed&#038;from=<b>your-facebook-ID</b>&#038;cityId=4&#038;jobId=37&#038;ztrack_category=bangkok_boss_defeated&#038;friend=<b>your-facebook-ID</b>&#038;next_params=%7B%22target_id%22%3A%22<b>your-facebook-ID</b>%22%2C+%22job_city%22%3A%224%22%2C+%22job_id%22%3A%2237%22%7D&#038;ref=nf</p>
</li>
</ul>
<p>The Bangkok boss defeat links provide the clickers with B$1000 and 6 XP, and the publisher with $500.000-5.000.000 Baht, so they&#8217;re a great thing.</p>
<h3>Cool tools</h3>
<p>I&#8217;ve found some very useful tools for speeding up routine operations (collecting bonuses, sending gifts etc.) in Mafia Wars made by MW-enthusiasts. I&#8217;m not sure they respect Zynga&#8217;s TOS (actually I have a strong hunch they don&#8217;t), so use with care (and measure, to keep the game fun for everyone):</p>
<ul>
<li><a href="http://www.spockholm.com/mafia/bookmarklets.php">Spockholm Mafia Wars Tools</a> (news and talk page <a href="http://www.facebook.com/mafiatools">here</a>, beta versions <a href="http://www.spockholm.com/mafia/testing.php">here</a>)</li>
<li><a href="http://joshmiller.com/JMWTools.html">Josh Miller&#8217;s Bookmarklets</a></li>
</ul>
<p>Some of the bookmarklets help you choose what items to buy to improve your attack/defense, find ongoing wars in which you might help or advise you on whom to promote to your Top Mafia to maximize the bonuses.</p>
<h3>Misc hints</h3>
<ul>
<li>the Zynga toolbar (yes, like everyone else, I <b>hate</b> toolbars) is safe to install and provides an extra &#8220;mini energy pack&#8221; of 25% of your energy every 8 hours (it also gives an item of less use). To actually get the bonus you have to start MW by clicking the &#8220;Play Now&#8221; button on the toolbar when the last text on the toolbar says &#8220;Ready&#8221; in green (instead of a countdown timer in red). For some reason, I also have to login fresh (delete cookies) to Facebook every time for it to work.<br />
<strong>Note:</strong> some methods have been published to get the mini pack without the toolbar; the loopholes have been patched, and if new ones will be found, they will be patched as well. Since it&#8217;s not that much of a hassle, you&#8217;re better off just installing the toolbar.</li>
<li>in Bangkok, the final Boss job gives a payout between $500.000-5.000.000 &#8220;Baht&#8221;, which can really help in starting your businesses, so make sure you publish it and enough friends help you</li>
<li>if you&#8217;re like me and only use your Facebook account for playing MW, you can find plenty of friends to add to your mafia in the comments on the <a href="http://www.facebook.com/MafiaWarsFans">MafiaWarsFans</a> fan page; following that page can also keep you informed on game news and might get you extra Godfather points, offered there on random occasions by Zynga</li>
</ul>
<h3>Later edit</h3>
<p>After Mafia Wars I played Travian, and then Lord of Ultima (which has amazing graphics for a browser game), and then I simply decided my time was pointlessly wasted on them, so I stopped. You can too! <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/vtopan.wordpress.com/143/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/vtopan.wordpress.com/143/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/vtopan.wordpress.com/143/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/vtopan.wordpress.com/143/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/vtopan.wordpress.com/143/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/vtopan.wordpress.com/143/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/vtopan.wordpress.com/143/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/vtopan.wordpress.com/143/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/vtopan.wordpress.com/143/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/vtopan.wordpress.com/143/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/vtopan.wordpress.com/143/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/vtopan.wordpress.com/143/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/vtopan.wordpress.com/143/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/vtopan.wordpress.com/143/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vtopan.wordpress.com&amp;blog=6592892&amp;post=143&amp;subd=vtopan&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://vtopan.wordpress.com/2010/01/25/mafia-wars-tips-tricks/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/539dcced66dd05d6ba7009aca8abf164?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">vtopan</media:title>
		</media:content>
	</item>
		<item>
		<title>Recovering data from a dead Windows (NTFS) disk using Linux</title>
		<link>http://vtopan.wordpress.com/2009/11/15/recovering-data-from-a-dead-windows-ntfs-disk-using-linux/</link>
		<comments>http://vtopan.wordpress.com/2009/11/15/recovering-data-from-a-dead-windows-ntfs-disk-using-linux/#comments</comments>
		<pubDate>Sun, 15 Nov 2009 23:51:11 +0000</pubDate>
		<dc:creator>vtopan</dc:creator>
				<category><![CDATA[free software]]></category>
		<category><![CDATA[system]]></category>
		<category><![CDATA[binary images]]></category>
		<category><![CDATA[data recovery]]></category>
		<category><![CDATA[data rescue]]></category>
		<category><![CDATA[ddrescue]]></category>
		<category><![CDATA[efs]]></category>
		<category><![CDATA[knoppix]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[mount]]></category>
		<category><![CDATA[ntfs]]></category>
		<category><![CDATA[recovery]]></category>
		<category><![CDATA[Windows]]></category>

		<guid isPermaLink="false">http://vtopan.wordpress.com/?p=117</guid>
		<description><![CDATA[At some point in your IT-enthusiast life you must&#8217;ve had at least one dead HDD, off of which Windows wouldn&#8217;t boot anymore. Up until a while ago, particularly if the partitions were formatted with NTFS, the situation was pretty much hopeless. Nowadays, with very-much-improved NTFS support under Linux (and rather tolerant to faults compared to [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vtopan.wordpress.com&amp;blog=6592892&amp;post=117&amp;subd=vtopan&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>At some point in your IT-enthusiast life you must&#8217;ve had at least one dead HDD, off of which Windows wouldn&#8217;t boot anymore. Up until a while ago, particularly if the partitions were formatted with NTFS, the situation was pretty much hopeless. Nowadays, with very-much-improved NTFS support under Linux (and rather tolerant to faults compared to its native counterpart under Windows), it isn&#8217;t always so. If the HDD is in a &#8220;coma&#8221; (i.e. <em>almost</em> dead, but not still &#8220;sort of&#8221; kicking), booting off a Linux live CD might still help recover (some of) the data. Basic steps:</p>
<ol>
<li>Get a Linux live CD distribution which has good built in NTFS support (most of them have basic support by now) and ddrescue</li>
<li>Boot off the live CD and use <a href="http://www.gnu.org/software/ddrescue/ddrescue.html">ddrescue</a> to get a binary image of each partition <strong>or</strong> mount the partition(s) and copy the files to a safe place</li>
<li>[If using the dd(rescue) approach] mount the images as drives under Windows and copy the files <strong>or</strong> be brave and mount the partition in a VM and try to actually boot it, at least as far as a command prompt (safe mode) <strong>or</strong> use a backup/partitioning tool to write the images to another disk</li>
</ol>
<p>If you&#8217;re not paranoid about security (by nature or by job description), i.e. you don&#8217;t use EFS for your most sensitive data, you&#8217;re pretty much off the hook. If you&#8217;ve made the punishable-by-huge-amounts-of-pain mistake of using EFS and your disk crashed, as is my case, hope is as dimmed as the foresight of the folks who designed NTFS and used more than the actual user password to encrypt the data. As it turns out, to decrypt the files you <a href="http://technet.microsoft.com/en-us/library/bb457065.aspx">need a certificate</a> which can only be generated on the machine which encrypted the files, which is </p>
<h3>Linux live CDs with NTFS support</h3>
<p>I&#8217;ve tried <a href="">SystemRescueCd</a>, <a href="http://trinityhome.org/">Trinity Rescue Kit</a>, <a href="http://www.tux.org/pub/people/kent-robotti/looplinux/rip/">RIP Linux</a> and plain vanilla <a href="http://www.knopper.net/knoppix/index.html">Knoppix</a>, and <strong>Trinity Rescue Kit</strong> appears to be the best: it has <a href="http://www.linux-ntfs.org/doku.php">ntfstools / Linux-NTFS</a> installed, and it didn&#8217;t hang on boot because of the failing HDD (other distros did). As a sidenote, I haven&#8217;t managed to boot the GUI (X) of any of the distros, as my laptop monitor/graphics card seems to be uncooperative with the standard drivers/VESA mode, but apart from the visual partition manager, everything works fine from the console anyway.</p>
<p>When choosing a distro, the main points to check are if it has the <a href="http://www.ntfs-3g.org/">ntfs-3g</a> driver (as recent a version as possible, as it keeps getting better at a fast pace) and the <a href="http://www.linux-ntfs.org/doku.php">ntfstools / Linux-NTFS</a> suite I mentioned earlier, especially if you&#8217;ve used EFS to encrypt your data (in which case the only viable solution appears to be ntfsdecrypt from that suite, which needs the certificate with which the files were encrypted, which in it&#8217;s turn needs you to boot the (dead) machine, but it appears to be the only way to get the data back).</p>
<h3>Using dd/ddrescue to recover (NTFS) partitions</h3>
<h4>dd / ddrescue</h4>
<p>The tool to move binary data from one place to another under Linux is dd. It also has a data-recovery-oriented cousin called ddrescue, which basically does the same thing, but is more fault-tolerant.<br />
Basic dd usage:<br />
<pre class="brush: bash;">dd if=/source of=/destination</pre><br />
<strong>if</strong> stands for input file and <strong>of</strong> for output file, and neither of them has to be an actual file (in the Windows sense); in the above example, <em>/dev/sda1</em> is the first partition on the <strong>sda</strong> disk.<br />
To back up just the <a href="http://en.wikipedia.org/wiki/Master_boot_record">MBR</a> of the disk (the first 512 bytes) use:<br />
<pre class="brush: bash;">dd if=/dev/sda of=/mnt/sdb1/saved/mbr.bin bs=512 count=1</pre><br />
This assumes that source disk is sda and that sdb1 is the partition to which you want to back up the data, so in your particular case they may need to be changed. See the next section if you&#8217;re not sure which disk is mapped to which name.<br />
<strong>ddrescue</strong> uses fixed-position input (first) and output (second) arguments:<br />
<pre class="brush: bash;">ddrescue -v /source /destination</pre><br />
The -v option makes <strong>ddrescue</strong> verbose (i.e. periodically print progress).<br />
<strong>Note:</strong> by default, <strong>dd</strong> prints no progress/info until it&#8217;s job is finished. To check up on it&#8217;s progress, open another console (the terminals are mapped to Alt+N shortcuts in Linux, N &gt;= 1, usually up to 4) and send it the <em>USR1</em> signal. To do that, first you need to find it&#8217;s PID using <strong>ps</strong>:<br />
<pre class="brush: bash;">ps -A|grep dd</pre><br />
Then, assuming the PID of the <strong>dd</strong> process is 3456, use <a href="http://linux.die.net/man/1/kill">kill</a>:<br />
<pre class="brush: bash;">kill -USR1 3456</pre><br />
That won&#8217;t actually <strong>kill</strong> the process, in spite of it&#8217;s name; it will just send it the USR1 signal, which makes <strong>dd</strong> print it&#8217;s current status (switch to the dd terminal to see it). The command&#8217;s name (&#8220;kill&#8221;) comes from it&#8217;s most frequent usage, which is to send a process the KILL signal (i.e. &#8220;kill&#8221; it).</p>
<h4>Linux drive mapping</h4>
<p>Linux maps your disks under <strong>/dev</strong> with names following the (&#8220;regex-like&#8221;) pattern [hs]d[abcd]. An <strong>h</strong> prefix means an (older) IDE disk, meanwhile an <strong>s</strong> prefix means a serial disk (usually an internal SATA or external USB disk). The individual partitions follow the disk naming + a digit to designate the partition number. So, for example, if you have a SATA disk with two partitions, the disk would be <em>/dev/sda</em>, the first partition would be <em>/dev/sda1</em> and the second partition <em>/dev/sda2</em>.<br />
To see the available disks/partitions, use <strong>ls</strong> (the Linux equivalent of <strong>dir</strong>):<br />
<pre class="brush: bash;">ls /dev/sd*
ls /dev/hd*</pre><br />
To get extended disk info, use <a href="http://linux.die.net/man/8/hdparm">hdparm</a>:<br />
<pre class="brush: bash;">hdparm -I /dev/sda</pre><br />
The disks (actually the partitions) found under <em>/dev</em> need to be <strong>mount</strong>ed before the files on them can be read/written; up until that point they are just huge blobs of binary data.</p>
<p><strong>Note:</strong> for the rest of this writing, for simplicity&#8217;s sake, I&#8217;ll assume that <strong>sda</strong> is the broken disk and it has wto partitions, and that the recovered files/image go to <strong>sdb</strong>.</p>
<p>There are two ways to mount NTFS partitions: either using the default NTFS driver which comes with <strong>mount</strong> (ignores many problems, doesn&#8217;t care if Windows was improperly stopped &amp; the drive was left &#8220;unclean&#8221;, read-only mode by default) or the ntfs-3g driver (more sensitive, read-write by default). Use the plain mount for the broken disk and the ntfs-3g version for the drives to which you need read-write access.<br />
First off, you need to make appropriate folders for the partitions to be mounted under; standard practice is to do it under the /mnt folder. e.g.:<br />
<pre class="brush: bash;">mkdir /mnt/sda1
mkdir /mnt/sda2
mkdir /mnt/sdb1</pre><br />
Note that the /mnt folder may not exist, in which case it must be created first: <code>mkdir /mnt</code><br />
Next, mount partitions from the broken disk (read-only):<br />
<pre class="brush: bash;">mount /dev/sda1 /mnt/sda1
mount /dev/sda2 /mnt/sda2</pre><br />
The syntax of the mount command is straight-forward: <code>mount /what /where</code>; /what is the device, /where is the mount point in the filesystem. It takes other arguments, such as -t <em>type</em> to set the filesystem type, but NTFS is (nowadays) recognized automatically. The naming convention for the mount points is at your choice (you could mount the thing on something like <em>/my/broken/disk/partition/number/1</em>), but sticking to the &#8220;standard&#8221; <em>/mnt</em> path and using the original device&#8217;s name (or the partition letter if you&#8217;re more accustomed to that and a lazy typist, such as <em>/mnt/c</em>) is easier, and the help you find on the net will make more sense.<br />
Last step in the mount process: mounting the destination disk in read-write mode (default for ntfs-3g):<br />
<pre class="brush: bash;">ntfs-3g /dev/sdb1 /mnt/sdb1</pre><br />
<strong>or</strong><br />
<pre class="brush: bash;">mount -t ntfs-3g /dev/sdb1 /mnt/sdb1</pre><br />
The syntax is similar to the mount command; to check if the distro you chose has the <strong>ntfs-3g</strong> command built in, simply try to run it. If it doesn&#8217;t, choose another distro.</p>
<h4>Copying the data</h4>
<p>Run either dd or ddrescue (the latter is preferred if the disk is only partially readable):<br />
<pre class="brush: bash;">dd if=/dev/sda1 of=/mnt/sdb1/saved/part1.bin</pre><br />
<strong>or</strong><br />
<pre class="brush: bash;">ddrescue -n /dev/sdb1 /mnt/sdb1/saved/part1.bin</pre><br />
<strong>WARNING:</strong> pay attention <strong>not to pass as the destination to dd/ddrescue entire disks</strong> unless you actually want their contents overwritten (which will be the case when you restore the saved image to a new disk); be sure to add a file name otherwise. The -n option prevents ddrescue from retrying error areas, which is usually what you want. If you have a disk which does yield data after enough retries, don&#8217;t use it.</p>
<h3>Mounting the (NTFS) partition(s) from Linux/Windows</h3>
<p>You can mount the newly backed-up partitions from Linux using the loop feature:<br />
<pre class="brush: bash;">mount -o loop /mnt/sdb1/saved/part1.bin /mnt/part1</pre></p>
<p>The partition can also be mounted directly from Windows using the <a href="http://www.ltr-data.se/opencode.html#ImDisk">ImDisk Virtual Disk Driver</a> (free) or using some rather expensive commercial tools (google for alternatives).</p>
<h3>Backing up/restoring partitions/whole disks</h3>
<p>Alternatively, you can use the <strong>dd</strong> command to copy the entire disk and write the image to a fresh (identical) disk. Writing an image to a partition/disk using dd simply requires passing the disk as the <strong>of</strong> argument:<br />
Restoring a partition:<br />
<pre class="brush: bash;">dd if=/mnt/sdb1/saved/part1.bin of=/dev/sdc1</pre><br />
Restoring an entire disk:<br />
<pre class="brush: bash;">dd if=/mnt/sdb1/saved/whole-disk.bin of=/dev/sdc</pre><br />
<strong>WARNING</strong>: be careful when overwriting raw partition/disk contents; choose other recovery methods unless you understand exactly what you&#8217;re doing.</p>
<h3>Recovering files from raw data/deleted files: data carving</h3>
<p>If the partition table/NTFS structure is broken and you can&#8217;t mount the partitions but you can read the binary data, you can use <a href="http://www.cgsecurity.org/wiki/TestDisk">TestDisk</a> to recover some of the files (the ones with a specific structure, such as images and music, are more likely to be found as opposed to, say, plain text files). This is basically the same thing that file recovery programs (such as <a href="http://www.piriform.com/recuva">Recuva</a>) do on the unused space of a disk to recover deleted files.</p>
<h3>Recovering EFS encrypted files</h3>
<p>As I&#8217;ve mentioned in the opening paragraph, to recover EFS encrypted files, even under Linux, you need a recovery certificate. If you don&#8217;t have one, EFS file recovery software might help, but I&#8217;ve had little luck using them. I know of no open source/free software which does that, so you&#8217;ll probably have to use commercial software such as <a href="http://www.elcomsoft.com/aefsdr.html">Advanced EFS Data Recovery</a> from ElcomSoft (demo version available). The link called &#8220;encrypted file system recovery&#8221; from the following section details the process of manually extracting the required information for EFS recovery.</p>
<h3>Further reading</h3>
<ul>
<li><a href="https://help.ubuntu.com/community/DataRecovery#Extract%20individual%20files%20from%20recovered%20image">Ubuntu &#8211; DataRecovery</a></li>
<li><a href="http://technet.microsoft.com/en-us/library/bb457065.aspx">Encrypting File System in Windows XP and Windows Server 2003</a></li>
<li><a href="http://www.beginningtoseethelight.org/efsrecovery/">encrypted file system recovery</a></li>
<li><a href="http://www.linux-ntfs.org/doku.php?id=ntfsdecrypt">ntfsdecrypt</a></li>
</ul>
<h3>Moral of the story</h3>
<ul>
<li><strong>ALWAYS BACK UP YOUR IMPORTANT DATA</strong>. Seriously. Now. Go get some storage space (USB flash drive, external hard disk, even DVDs if you make a new one often enough, as they tend not to last very long) and copy your data on it. GO!</li>
<li>Don&#8217;t use EFS under NTFS. Use an alternative encryption solution, e.g. <a href="http://www.truecrypt.org/">TrueCrypt</a>. There are portable (i.e. works-from-flash-drive) editions of most encryption tools should the need arise, and they are reliable (I&#8217;ve used TrueCrypt without problems for quite a while now).</li>
<li>If you MUST use EFS, create a recovery certificate using <code>CIPHER /R:filename</code> (details <a href="http://technet.microsoft.com/en-us/library/bb457065.aspx">here</a>) and store it in a safe place.</li>
</ul>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/vtopan.wordpress.com/117/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/vtopan.wordpress.com/117/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/vtopan.wordpress.com/117/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/vtopan.wordpress.com/117/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/vtopan.wordpress.com/117/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/vtopan.wordpress.com/117/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/vtopan.wordpress.com/117/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/vtopan.wordpress.com/117/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/vtopan.wordpress.com/117/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/vtopan.wordpress.com/117/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/vtopan.wordpress.com/117/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/vtopan.wordpress.com/117/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/vtopan.wordpress.com/117/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/vtopan.wordpress.com/117/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vtopan.wordpress.com&amp;blog=6592892&amp;post=117&amp;subd=vtopan&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://vtopan.wordpress.com/2009/11/15/recovering-data-from-a-dead-windows-ntfs-disk-using-linux/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/539dcced66dd05d6ba7009aca8abf164?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">vtopan</media:title>
		</media:content>
	</item>
		<item>
		<title>Karaoke songs &amp; free player</title>
		<link>http://vtopan.wordpress.com/2009/10/06/karaoke-songs-andfree-player/</link>
		<comments>http://vtopan.wordpress.com/2009/10/06/karaoke-songs-andfree-player/#comments</comments>
		<pubDate>Tue, 06 Oct 2009 00:51:22 +0000</pubDate>
		<dc:creator>vtopan</dc:creator>
				<category><![CDATA[karaoke]]></category>
		<category><![CDATA[Music]]></category>
		<category><![CDATA[download]]></category>
		<category><![CDATA[free]]></category>
		<category><![CDATA[kar]]></category>
		<category><![CDATA[player]]></category>
		<category><![CDATA[songs]]></category>

		<guid isPermaLink="false">http://vtopan.wordpress.com/?p=105</guid>
		<description><![CDATA[Karaoke is one of my more enjoyable means of killing time (and music itself, some might argue, as I&#8217;m a rather untalented singer). A while ago I&#8217;ve found a free karaoke player (far from perfect, as it BSODs my new laptop, but pretty usable otherwise), which together with some fun .kar songs I&#8217;ve gathered over [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vtopan.wordpress.com&amp;blog=6592892&amp;post=105&amp;subd=vtopan&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Karaoke is one of my more enjoyable means of killing time (and music itself, some might argue, as I&#8217;m a rather untalented singer). A while ago I&#8217;ve found a free karaoke player (far from perfect, as it BSODs my new laptop, but pretty usable otherwise), which together with some fun .kar songs I&#8217;ve gathered over time make for a ~20 MB pack of (potentially) great fun.</p>
<h3>The player &#8211; Karafun</h3>
<p>You can get a player called Karafun <a href="http://www.karafun.com/karaokeplayer/">here</a> (if you know of any other, preferably better, free karaoke player, lemme know). You <strong>don&#8217;t</strong> have to buy the songs from that site too; there are plenty of free karaoke (.kar) songs on the web. It&#8217;s also <strong>portable</strong>, meaning you can copy the installed folder (<em>c:\Program Files\KaraFun</em> by default) on a USB stick and carry it around.</p>
<h3>The songs (.kar files)</h3>
<p>The most commonly used song files for karaoke are <strong>.kar</strong> files, and there&#8217;s plenty of them online. The .kar format, for the inquisitive(ly?)-natured, is a MIDI (instrumental music) file with synchronized lyrics strapped on. This is relevant because some of the files you&#8217;ll find won&#8217;t have the .kar extension, but a <strong>.mid</strong> extension; they most likely will still work (the lyrics could be missing from such files, so try playing them to make sure they&#8217;re there).</p>
<p>Google for &#8220;free kar songs&#8221; or something similar and you&#8217;ll find many searchable databases of such songs. To find the .kar file of a particular song, google for it&#8217;s name + kar (e.g.: &#8220;sinatra my way kar&#8221;). In <strong>Opera</strong>, clicking links pointing to .kar files will play them instead of prompting to download them, so simply right click &amp; &#8220;Save Linked Content As&#8221; to save them.</p>
<p>I&#8217;ve found a nice and well-organized collection on <a href="http://www.karaokebash.com/">karaokebash.com</a>, and a good .kar search engine on <a href="http://www.vanbasco.com/midisearch.html">vanbasco.com</a>.</p>
<h3>Personal selection</h3>
<p>As I&#8217;ve already mentioned, I&#8217;ve put together a collection of .kar songs, split in two &#8220;tiers&#8221;: &#8220;the most frequently played&#8221; and &#8220;other good karaoke songs&#8221;, plus a huge folder called &#8220;unsorted&#8221; which occasionally feeds the two categories above when I have enough time to plow through the pile.</p>
<p>Songs I (and my friends) most often sing along to (<strong>download</strong> link the whole pack from <a href="http://www.easy-share.com/1908027696/karaoke-io.zip">easy-share.com</a> or <a href="http://www.wikiupload.com/download_page.php?id=176512">wikiupload.com</a>):<br />
<strong>ABBA &#8211; Chiquitita</strong>.kar, <strong>ABBA &#8211; Fernando</strong>.kar, <strong>ABBA &#8211; Thank You For The Music</strong>.kar, <strong>Ace of Base &#8211; All That She Wants</strong>.kar, <strong>Aerosmith &#8211; Cryin&#8217;</strong>.kar, <strong>Afroman &#8211; Cause I Got High</strong>.kar, <strong>Andrea Bocelli &#8211; Con te partiro</strong>.kar, <strong>Andrea Bocelli &#8211; Vivo per lei</strong>.kar, <strong>Animals &#8211; House Of The Rising Sun</strong>.kar, <strong>Aqua &#8211; Barbie Girl</strong>.kar, <strong>Bangles &#8211; Manic monday</strong>.kar, <strong>Beach Boys &#8211; Kokomo</strong>.kar, <strong>Beatles &#8211; Hey Jude</strong>.kar, <strong>Beatles &#8211; Yesterday</strong>.kar, <strong>Bee Gees &#8211; Tragedy</strong>.kar, <strong>Berlin &#8211; Take My Breath Away</strong>.kar, <strong>Billy Joel &#8211; We Didn&#8217;t Start the Fire</strong>.kar, <strong>Blondie &#8211; Heart of Glass</strong>.kar, <strong>Bobby McFerrin &#8211; Don&#8217;t Worry, Be Happy</strong>.kar, <strong>Bon Jovi &#8211; Always</strong>.kar, <strong>Bon Jovi &#8211; Bed of Roses</strong>.kar, <strong>Bon Jovi &#8211; Blaze Of Glory</strong>.kar, <strong>Bon Jovi &#8211; Livin&#8217; On A Prayer</strong>.kar, <strong>Bon Jovi &#8211; This Aint a Love Song</strong>.kar, <strong>Bonnie Tyler &#8211; It&#8217;s a Heartache</strong>.kar, <strong>Bonnie Tyler &#8211; Sometimes when we touch</strong>.kar, <strong>Bonnie Tyler &#8211; Total Eclipse Of The Heart</strong>.kar, <strong>Boyzone &#8211; Father and Son</strong>.kar, <strong>Boyzone &#8211; Words</strong>.kar, <strong>Brian May &#8211; Too much love will kill you</strong>.kar, <strong>Bryan Adams &#8211; Cloud number 9</strong>.kar, <strong>Bryan Adams &#8211; Everything I do, I do it for you</strong>.kar, <strong>Celine Dion &#8211; Beauty And The Beast</strong>.kar, <strong>Coolio &#8211; Gangstas Paradise</strong>.kar, <strong>Cranberries &#8211; Zombie</strong>.kar, <strong>Crash Test Dummies &#8211; Mmm</strong>.kar, <strong>Dean Martin &#8211; Sway</strong>.kar, <strong>Dean Martin &#8211; That&#8217;s Amore</strong>.kar, <strong>Disney&#8217;s Jungle Book (Terry Gilkyson) &#8211; Bare Necessities</strong>.kar, <strong>Don McLean &#8211; American Pie</strong>.kar, <strong>Eagles &#8211; Hotel California</strong>.kar, <strong>Eddy Grant &#8211; Gimme Hope Joanna</strong>.kar, <strong>Edith Piaf &#8211; Ne me quitte pas</strong>.kar, <strong>Elvis Presley &#8211; Are You Lonesome Tonight</strong>.kar, <strong>Elvis Presley &#8211; Can&#8217;t Help Falling In Love</strong>.kar, <strong>Elvis Presley &#8211; Love Me Tender</strong>.kar, <strong>Elvis Presley &#8211; Suspicious Minds</strong>.kar, <strong>Eric Clapton &#8211; Tears In Heaven</strong>.kar, <strong>Extreme &#8211; More Than Words</strong>.kar, <strong>Frank Sinatra &#8211; My Way</strong>.kar, <strong>Frank Sinatra &#8211; Something Stupid</strong>.kar, <strong>Frank Sinatra &#8211; Strangers In The Night</strong>.kar, <strong>G. Capurro &#8211; O sole mio</strong>.kar, <strong>Gerry &amp; The Pacemakers &#8211; You&#8217;ll Never Walk Alone</strong>.kar, <strong>Gerry Rafferty &#8211; Baker Street</strong>.kar, <strong>Gloria Gaynor &#8211; I Will Survive</strong>.kar, <strong>Gordon Lightfoot &#8211; If You Could Read My Mind</strong>.kar, <strong>Guns&#8217;n'Roses &#8211; Sweet Child Of Mine</strong>.kar, <strong>Harry Belafonte &#8211; Banana Boat Song (Day-O)</strong>.kar, <strong>Jim Croce &#8211; Operator</strong>.kar, <strong>Jimmy Buffett &#8211; Margaritaville</strong>.kar, <strong>Joe Cocker &#8211; You Are So Beautiful</strong>.kar, <strong>Joe Dassin &#8211; Et si tu n&#8217;existais pas</strong>.kar, <strong>Julio Iglesias &#8211; A veces si, a veces no</strong>.kar, <strong>Led Zeppelin &#8211; Stairway To Heaven</strong>.kar, <strong>Louis Armstrong &#8211; What A Wonderful World</strong>.kar, <strong>Lucio Dalla &#8211; Caruso</strong>.kar, <strong>Mariah Carey &#8211; Without you</strong>.kar, <strong>Meat Loaf &#8211; I&#8217;d Do Anything For Love</strong>.kar, <strong>Monty Python &#8211; Always Look On The Bright Side Of Life</strong>.kar, <strong>Mungo Jerry &#8211; In The Summertime</strong>.kar, <strong>Pet Shop Boys &#8211; Go West</strong>.kar, <strong>Phil Collins &#8211; Groovy kind of love</strong>.kar, <strong>Queen &#8211; Bohemian Rhapsody</strong>.kar, <strong>Queen &#8211; Love Of My Life</strong>.kar, <strong>Queen &#8211; Too Much Love Will Kill You</strong>.kar, <strong>Ray Charles &#8211; Hit the road Jack</strong>.kar, <strong>Richard Marx &#8211; Hazard</strong>.kar, <strong>Robbie Williams &#8211; Supreme</strong>.kar, <strong>Rod Stewart &#8211; Sailing</strong>.kar, <strong>Rolling Stones &#8211; Satisfaction</strong>.kar, <strong>Scorpions &#8211; Wind of change</strong>.kar, <strong>Smokie &#8211; Alice</strong>.kar, <strong>Soul Asylum &#8211; Runaway train</strong>.kar, <strong>Status Quo &#8211; In the army now</strong>.kar, <strong>Sting &#8211; Englishman in New York</strong>.kar, <strong>Supertramp &#8211; The Logical Song</strong>.kar, <strong>Suzanne Vega &#8211; Tom&#8217;s Diner</strong>.kar, <strong>The Carpenters &#8211; Close To You</strong>.kar, <strong>The Righteous Brothers &#8211; You&#8217;ve Lost That Lovin Feelin</strong>.kar, <strong>Tina Turner &#8211; The best</strong>.kar, <strong>Tom Jones &#8211; Delilah</strong>.kar, <strong>Tony Braxton &#8211; Unbreak My Heart</strong>.kar, <strong>Ugly Kid Joe &#8211; Cats In The Cradle</strong>.kar, <strong>Westlife &#8211; Seasons in the sun</strong>.kar, <strong>Whitesnake &#8211; Here I go again</strong>.kar, <strong>Whitney Houston &#8211; I will always love you</strong>.kar.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/vtopan.wordpress.com/105/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/vtopan.wordpress.com/105/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/vtopan.wordpress.com/105/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/vtopan.wordpress.com/105/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/vtopan.wordpress.com/105/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/vtopan.wordpress.com/105/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/vtopan.wordpress.com/105/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/vtopan.wordpress.com/105/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/vtopan.wordpress.com/105/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/vtopan.wordpress.com/105/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/vtopan.wordpress.com/105/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/vtopan.wordpress.com/105/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/vtopan.wordpress.com/105/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/vtopan.wordpress.com/105/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vtopan.wordpress.com&amp;blog=6592892&amp;post=105&amp;subd=vtopan&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://vtopan.wordpress.com/2009/10/06/karaoke-songs-andfree-player/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/539dcced66dd05d6ba7009aca8abf164?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">vtopan</media:title>
		</media:content>
	</item>
		<item>
		<title>Listing the modules (DLLs) of the current process without API functions</title>
		<link>http://vtopan.wordpress.com/2009/05/27/listing-the-modules-dlls-of-the-current-process-without-api-functions/</link>
		<comments>http://vtopan.wordpress.com/2009/05/27/listing-the-modules-dlls-of-the-current-process-without-api-functions/#comments</comments>
		<pubDate>Wed, 27 May 2009 23:22:51 +0000</pubDate>
		<dc:creator>vtopan</dc:creator>
				<category><![CDATA[assembler]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[Snippets]]></category>
		<category><![CDATA[system programming]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[PEB]]></category>
		<category><![CDATA[PEB_LDR_DATA]]></category>
		<category><![CDATA[TIB]]></category>
		<category><![CDATA[Windows]]></category>

		<guid isPermaLink="false">http://vtopan.wordpress.com/?p=97</guid>
		<description><![CDATA[There is a simple way to walk the list of loaded modules (DLLs) of the current Windows process without calling any API functions. It also works with other processes, but it obviously involves reading the respective process' memory space, which in turn involves using the aptly-named ReadProcessMemory function, defeating the whole purpose of not using APIs.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vtopan.wordpress.com&amp;blog=6592892&amp;post=97&amp;subd=vtopan&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>There is a simple way to walk the list of loaded modules (DLLs) of the current Windows process without calling any API functions. It also works with other processes, but it obviously involves reading the respective process&#8217; memory space, which in turn involves using the aptly-named ReadProcessMemory function, defeating the whole purpose of not using APIs.</p>
<h3>The PEB and the TIB</h3>
<p>The <strong>PEB</strong> (Process Enviroment Block), briefly documented <a href="http://undocumented.ntinternals.net/UserMode/Undocumented%20Functions/NT%20Objects/Process/PEB.html">here</a> (the <a href="http://msdn.microsoft.com/en-us/library/aa813706(VS.85).aspx">page</a> which passes as &#8220;documentation&#8221; on MSDN just&#8230; isn&#8217;t) is a structure (stored for each process in it&#8217;s own memory space) which holds various process parameters used by the OS such as the PID, a flag set if the process is being debugged, some localization information etc. The PEB is pointed to by the <a href="http://en.wikipedia.org/wiki/Win32_Thread_Information_Block"><strong>TIB</strong></a> (Thread Information Block), which is always located at FS:[0] (if that sounds like black magic, either pick up a book on assembly language and/or x86 processor architecture, or ignore that piece of information and use the code below to get to it).</p>
<p>One of the PEB entries is a list of, well, the loaded modules of the process. Actually, a PEB member points to a structure called <em>LoaderData</em> (it&#8217;s type being <a href="http://undocumented.ntinternals.net/UserMode/Structures/PEB_LDR_DATA.html">PEB_LDR_DATA</a>), again <a href="http://msdn.microsoft.com/en-us/library/aa813708(VS.85).aspx">&#8220;almost documented&#8221;</a> by MS, which points to the list we&#8217;re interested in, looking something like this:<br />
<pre class="brush: cpp;">typedef struct LDR_DATA_ENTRY {
  LIST_ENTRY              InMemoryOrderModuleList;
  PVOID                   BaseAddress;
  PVOID                   EntryPoint;
  ULONG                   SizeOfImage;
  UNICODE_STRING          FullDllName;
  UNICODE_STRING          BaseDllName;
  ULONG                   Flags;
  SHORT                   LoadCount;
  SHORT                   TlsIndex;
  LIST_ENTRY              HashTableEntry;
  ULONG                   TimeDateStamp;
  } LDR_DATA_ENTRY, *PLDR_DATA_ENTRY;</pre><br />
It&#8217;s a linked list (the <em>LIST_ENTRY</em> structure contains the forward pointer called <em>Flink</em>) which contains serveral useful pieces of information about each module, among which it&#8217;s image base (<em>BaseAddress</em>), virtual address of the entrypoint (<em>EntryPoint</em>) (NOT the RVA you find in the PE header, but the actual computed VA) and DLL name with (<em>FullDllName</em>) and without (<em>BaseDllName</em>) the path, as <em>UNICODE_STRING</em>s. The list is circularly linked and has a sentinel element with the <em>BaseAddress</em> member set to 0.</p>
<p>The list entries should be of the <em>LDR_DATA_TABLE_ENTRY</em> data type described by MS <a href="http://msdn.microsoft.com/en-us/library/aa813708(VS.85).aspx">here</a>, but the actual structure found in-memory doesn&#8217;t have the first member (<em>BYTE Reserved1[2]</em>). An alternative (and more complete) definition of the structure can be found <a href="http://undocumented.ntinternals.net/UserMode/Structures/LDR_MODULE.html">here</a>, but it has three <em>LIST_ENTRY</em> members at the beginning instead of just one.</p>
<h3>Getting a pointer to the list</h3>
<p>We retrieve the pointer to the list from the PEB using inline assembly from the C source code:<br />
<pre class="brush: cpp;">__declspec(naked)
PLDR_DATA_ENTRY firstLdrDataEntry() {
   __asm {
      mov eax, fs:[0x30]  // PEB
      mov eax, [eax+0x0C] // PEB_LDR_DATA
      mov eax, [eax+0x1C] // InInitializationOrderModuleList
      retn
      }
   }</pre><br />
There are actually three lists of modules, sorted by three different criterias; the one we&#8217;re using is sorted by the order in which the modules were loaded.</p>
<h3>Example</h3>
<p>Using the structure defined above and the <em>firstLdrDataEntry</em> function, it becomes trivial to walk the list of loaded modules:</p>
<p><pre class="brush: cpp;">void main() {
   PLDR_DATA_ENTRY cursor;
   cursor = firstLdrDataEntry();
   while (cursor-&gt;BaseAddress) {
      printf( &quot;Module [%S] loaded at [%p] with entrypoint at [%p]\n&quot;,
              cursor-&gt;BaseDllName.Buffer, cursor-&gt;BaseAddress,
              cursor-&gt;EntryPoint);
      cursor = (PLDR_DATA_ENTRY)cursor-&gt;InMemoryOrderModuleList.Flink;
      }
   }</pre></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/vtopan.wordpress.com/97/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/vtopan.wordpress.com/97/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/vtopan.wordpress.com/97/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/vtopan.wordpress.com/97/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/vtopan.wordpress.com/97/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/vtopan.wordpress.com/97/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/vtopan.wordpress.com/97/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/vtopan.wordpress.com/97/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/vtopan.wordpress.com/97/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/vtopan.wordpress.com/97/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/vtopan.wordpress.com/97/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/vtopan.wordpress.com/97/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/vtopan.wordpress.com/97/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/vtopan.wordpress.com/97/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vtopan.wordpress.com&amp;blog=6592892&amp;post=97&amp;subd=vtopan&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://vtopan.wordpress.com/2009/05/27/listing-the-modules-dlls-of-the-current-process-without-api-functions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/539dcced66dd05d6ba7009aca8abf164?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">vtopan</media:title>
		</media:content>
	</item>
		<item>
		<title>Using NtDeleteFile from Delphi</title>
		<link>http://vtopan.wordpress.com/2009/05/26/using-ntdeletefile-from-delphi/</link>
		<comments>http://vtopan.wordpress.com/2009/05/26/using-ntdeletefile-from-delphi/#comments</comments>
		<pubDate>Tue, 26 May 2009 15:43:39 +0000</pubDate>
		<dc:creator>vtopan</dc:creator>
				<category><![CDATA[Delphi]]></category>
		<category><![CDATA[Snippets]]></category>
		<category><![CDATA[native API]]></category>
		<category><![CDATA[NtDeleteFile]]></category>

		<guid isPermaLink="false">http://vtopan.wordpress.com/?p=87</guid>
		<description><![CDATA[Using the native APIs RtlDosPathNameToNtPathName_U to convert a DOS path to a native (NT) path and NtDeleteFile to remove a file from Delphi.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vtopan.wordpress.com&amp;blog=6592892&amp;post=87&amp;subd=vtopan&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<h3>The NtDeleteFile API</h3>
<p>The native API <strong>NtDeleteFile</strong> performs the same task as the user-mode API <strong>DeleteFile</strong>, but interrestingly enough, the user-mode API does not call the native API to perform it&#8217;s task. As explained <a href="http://undocumented.ntinternals.net/UserMode/Undocumented%20Functions/NT%20Objects/File/NtDeleteFile.html">here</a>, normally files are deleted through calls to <strong>NtSetInformationFile</strong>. The main difference in behavior comes from the fact that <strong>NtDeleteFile</strong> does not wait for handles on the file to close before deleting it (note that if the file is &#8220;open for normal I/O or as a memory-mapped file&#8221;, it still can&#8217;t be deleted, so only read-only handles will be ignored).</p>
<h3>Data structures</h3>
<p>The required structures (not defined in Delphi) are <a href="http://vtopan.wordpress.com/programming/undocumentedpoorly-documented-structures-in-delphi/#UNICODE_STRING">UNICODE_STRING</a> and <a href="http://vtopan.wordpress.com/programming/undocumentedpoorly-documented-structures-in-delphi/#OBJECT_ATTRIBUTES">OBJECT_ATTRIBUTES</a>.</p>
<h3>Steps</h3>
<p>The steps to remove the file are:</p>
<ul>
<li>convert the &#8220;DOS&#8221; path name to an &#8220;NT&#8221; path name (basically prepend &#8220;\??\&#8221; to whatever the plain path is) using <i>RtlDosPathNameToNtPathName_U</i>;</li>
<li>fill in the <em>OBJECT_ATTRIBUTES</em> structure;</li>
<li>call <em>NtDeleteFile</em>.</li>
</ul>
<h3>Importing the APIs</h3>
<p>Using the afore-linked-to <a href="http://vtopan.wordpress.com/programming/undocumentedpoorly-documented-structures-in-delphi/">type definitions</a>, we still need to import the native APIs:</p>
<p><pre class="brush: delphi;">function NtDeleteFile(ObjectAttributes:POBJECT_ATTRIBUTES):DWORD; stdcall; external 'ntdll.dll';
function RtlDosPathNameToNtPathName_U(DosName:PWChar; var NtName:UNICODE_STRING; DosFilePath:PPChar; NtFilePath:PUNICODE_STRING):BOOL; stdcall; external 'ntdll.dll';</pre></p>
<p>Do note that this statically links the imported functions, making the whole application unable to load if the undocumented APIs are not present on the system (at least for Windows 2000 and XP they should be).</p>
<h3>Converting DOS paths to native paths using RtlDosPathNameToNtPathName_U</h3>
<p>Converting the DOS path name to a native path is done by the <em>RtlDosPathNameToNtPathName_U</em> API, which takes in a PWChar argument containing the DOS path and a pre-allocated UNICODE_STRING structure of MAX_PATH WideChars and returns True if it has successfully converted the path.</p>
<h3>Source code</h3>
<p>The code looks something like this:<br />
<pre class="brush: delphi;">function _DeleteFile(fileName:string):DWORD;
var
   oa:OBJECT_ATTRIBUTES;
   ws1, ws2:WideString;
   us:UNICODE_STRING;
begin
   Result := $C0000001; // STATUS_UNSUCCESSFUL, &quot;generic&quot; error 
   ws1 := fileName; // automatic String -&gt; WideString conversion
   SetLength(ws2, MAX_PATH);
   us.Length := MAX_PATH;
   us.MaximumLength := MAX_PATH;
   us.Buffer := @ws2[1];
   if not RtlDosPathNameToNtPathName_U(@ws1[1], us, nil, nil)
      then Exit;
   oa.Length := SizeOf(OBJECT_ATTRIBUTES);
   oa.RootDirectory := 0;
   oa.ObjectName := @us;
   oa.Attributes := $40; // case insensitive
   oa.SecurityDescriptor := nil;
   oa.SecurityQualityOfService := nil;
   Result := NtDeleteFile(@oa); // pass on the NTSTATUS
end;</pre></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/vtopan.wordpress.com/87/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/vtopan.wordpress.com/87/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/vtopan.wordpress.com/87/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/vtopan.wordpress.com/87/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/vtopan.wordpress.com/87/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/vtopan.wordpress.com/87/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/vtopan.wordpress.com/87/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/vtopan.wordpress.com/87/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/vtopan.wordpress.com/87/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/vtopan.wordpress.com/87/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/vtopan.wordpress.com/87/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/vtopan.wordpress.com/87/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/vtopan.wordpress.com/87/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/vtopan.wordpress.com/87/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vtopan.wordpress.com&amp;blog=6592892&amp;post=87&amp;subd=vtopan&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://vtopan.wordpress.com/2009/05/26/using-ntdeletefile-from-delphi/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/539dcced66dd05d6ba7009aca8abf164?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">vtopan</media:title>
		</media:content>
	</item>
		<item>
		<title>How to check if a thread/process is suspended (get thread state)</title>
		<link>http://vtopan.wordpress.com/2009/04/15/how-to-find-out-if-a-threadprocess-is-suspended-get-thread-state/</link>
		<comments>http://vtopan.wordpress.com/2009/04/15/how-to-find-out-if-a-threadprocess-is-suspended-get-thread-state/#comments</comments>
		<pubDate>Wed, 15 Apr 2009 01:07:18 +0000</pubDate>
		<dc:creator>vtopan</dc:creator>
				<category><![CDATA[Delphi]]></category>
		<category><![CDATA[Snippets]]></category>

		<guid isPermaLink="false">http://vtopan.wordpress.com/?p=51</guid>
		<description><![CDATA[The basic steps to get to a thread's status (and other) information knowing it's process ID and thread ID are outlined with relevant examples, the purpose being to find out if the thread is suspended.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vtopan.wordpress.com&amp;blog=6592892&amp;post=51&amp;subd=vtopan&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>The basic steps to get to a thread&#8217;s status information is the following (knowing of course both the process ID (hence forth PID) and the thread ID (TID)):</p>
<ol>
<li>call <a href="http://msdn.microsoft.com/en-us/library/ms724509(VS.85).aspx">NtQuerySystemInformation</a> with <em>SystemInformation</em> set to <em>SystemProcessInformation</em> (5)</li>
<li>iterate over the array of <em>SYSTEM_PROCESS_INFORMATION</em> structures (the structure contents is (wrongfully) explained <a href="http://undocumented.ntinternals.net/UserMode/Undocumented%20Functions/System%20Information/Structures/SYSTEM_PROCESS_INFORMATION.html">here</a>; correct version <a href="http://vtopan.wordpress.com/undocumentedpoorly-documented-structures-in-delphi/">here</a>) to find your PID (ProcessId member) of interest</li>
<li>iterate over the array of <em>SYSTEM_THREAD</em> structures (detailed below) to find the desired TID (<em>UniqueThread</em> member) and check the <em>State</em> and <em>WaitReason</em> members; both must be set to 5 if the thread is suspended, any other values otherwise</li>
</ol>
<p>As it&#8217;s probably obvious to most people keen on system-level programming, a process is suspended when all it&#8217;s threads are suspended, so all of them must be checked for the suspended status.</p>
<h3>Step one: calling NtQuerySystemInformation</h3>
<p>The required structures are defined <a href="http://vtopan.wordpress.com/undocumentedpoorly-documented-structures-in-delphi/">here</a> (for Delphi). The function isn&#8217;t defined in any headers, so we must declare it&#8217;s prototype ourselves:<br />
<pre class="brush: delphi;">function NtQuerySystemInformation(SystemInformationClass:DWORD; SystemInformation:pointer; SystemInformationLength:DWORD; ReturnLength:PDWORD):cardinal; stdcall; external 'ntdll';</pre></p>
<p>Example usage:<br />
<pre class="brush: delphi;">var 
   spi:PSYSTEM_PROCESS_INFORMATION;
   size:DWORD;
begin
if (NtQuerySystemInformation(5, nil, 0, @size) = STATUS_INFO_LENGTH_MISMATCH) // SystemProcessInformation
   and (size &gt; 0)
   then begin
        GetMem(spi, size);
        if NtQuerySystemInformation(5, spi, size, @size) = 0
           then begin
                [...] // do something with spi
                end
           else HandleError; // failed listing processes!
        FreeMem(spi);
        end
    else HandleError; // failed listing processes!
end;</pre><br />
HandleError is a fictional function (which you&#8217;ll most likely skip, &#8217;cause you&#8217;re in a hurry to get things done, right? <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  ).</p>
<h3>Step two: iterating the process list</h3>
<p>The structure only <em>looks like</em> a linked list item; the <em>NextEntryOffset</em> member is an actual offset from the beginning of the current structure to the beginning of the next one. This is needed because of the variable size of the structure (given by the variable number of threads for each process). We need an extra <em>crt</em>:<em>PSYSTEM_PROCESS_INFORMATION</em> variable to walk the pseudo-linked list because we must keep the original <em>psi</em> pointer to free it&#8217;s memory.<br />
The outline of the code which iterates the processes looking for a PID (given the <em>spi</em>:<em>PSYSTEM_PROCESS_INFORMATION</em> pointer from above) would look like this:<br />
<pre class="brush: delphi;">var 
    crt:PSYSTEM_PROCESS_INFORMATION; 
[...]
    crt := spi;
    repeat
        if crt^.ProcessID = PID
           then begin
                [...] // do something with crt^
                break;
                end;
        crt := Pointer(DWORD(crt) + crt^.NextEntryOffset);
    until crt^.NextEntryOffset = 0;</pre></p>
<h3>Step three: find the appropriate thread</h3>
<p>Given the <em>ThreadInfo</em> array in the structure located at the previous step, we iterate through it and test the <em>State</em> and <em>WaitReason</em> members for the item matching our TID:<br />
<pre class="brush: delphi;">var
    j:integer;
[...]
    for j := 0 to crt^.NumberOfThreads-1 do
        begin
        if crt^.ThreadInfo[j].UniqueThread = TID
           then begin
                if crt^.ThreadInfo[j].WaitReason = 5
                   then [...] // the thread is suspended
                   else [...]; // the thread is not suspended
                break; 
                end;
        end;</pre><br />
The <em>State</em> member must also be set to 5 (&#8220;waiting&#8221;), but if the <em>WaitReason</em> is non-null, the <em>State</em> must be 5 (and vice-versa), so there&#8217;s little point in checking it explicitly.</p>
<h3>Additional info: thread starting address, priority etc.</h3>
<p>If you&#8217;ve paid any attention while reading the structures, you might have noticed additional interesting information about threads and processes, such as the creation time, image path, priority, handle count and memory and I/O usage/history for processes (this is how <a href="http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx">Process Explorer</a> gets, for example, the WorkingSet/PeakWorkingSet and ReadBytes/WriteBytes/OtherBytes information) and starting address, priority/base priority and various timing information for threads. The starting address is particularly interesting, because the <a href="http://msdn.microsoft.com/en-us/library/ms684283(VS.85).aspx">NtQueryInformationThread</a> API with <em>ThreadInformationClass</em> set to <em>ThreadQuerySetWin32StartAddress</em> (9) only works (on Windows pre-Vista) &#8220;before the thread starts running&#8221; (quoted from MSDN), which seems to me rather pointless in the first place. </p>
<p>The <em>NtQuerySystemInformation</em> API is also a useful replacement for the <em>CreateToolhelp32Snapshot</em> suite, yielding more information about processes and threads.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/vtopan.wordpress.com/51/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/vtopan.wordpress.com/51/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/vtopan.wordpress.com/51/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/vtopan.wordpress.com/51/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/vtopan.wordpress.com/51/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/vtopan.wordpress.com/51/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/vtopan.wordpress.com/51/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/vtopan.wordpress.com/51/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/vtopan.wordpress.com/51/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/vtopan.wordpress.com/51/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/vtopan.wordpress.com/51/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/vtopan.wordpress.com/51/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/vtopan.wordpress.com/51/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/vtopan.wordpress.com/51/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vtopan.wordpress.com&amp;blog=6592892&amp;post=51&amp;subd=vtopan&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://vtopan.wordpress.com/2009/04/15/how-to-find-out-if-a-threadprocess-is-suspended-get-thread-state/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/539dcced66dd05d6ba7009aca8abf164?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">vtopan</media:title>
		</media:content>
	</item>
		<item>
		<title>Top ten striptease songs</title>
		<link>http://vtopan.wordpress.com/2009/04/06/top-ten-striptease-songs/</link>
		<comments>http://vtopan.wordpress.com/2009/04/06/top-ten-striptease-songs/#comments</comments>
		<pubDate>Mon, 06 Apr 2009 01:36:08 +0000</pubDate>
		<dc:creator>vtopan</dc:creator>
				<category><![CDATA[Music]]></category>
		<category><![CDATA[Tops]]></category>

		<guid isPermaLink="false">http://vtopan.wordpress.com/?p=39</guid>
		<description><![CDATA[Some while ago, when the opportunity arose to put them to good use, I realised I had rather few striptease songs sorted out. The very next day (my memories are rather foggy given circumstances, but it most likely was the next day, so we&#8217;ll work with that assumption) I set on a quest for the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vtopan.wordpress.com&amp;blog=6592892&amp;post=39&amp;subd=vtopan&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Some while ago, when the opportunity arose to put them to good use, I realised I had rather few striptease songs sorted out. The very next day (my memories are rather foggy given circumstances, but it most likely was the next day, so we&#8217;ll work with that assumption) I set on a quest for the web&#8217;s favorite striptease songs. The results, however,  were somewhat disappointing; mostly redundant and not as insightful as I had hoped. But plowing through them I also found some good (enough) ones, and managed to collect sufficient songs to tire out even some of the most dedicated strippers out there (not that that would be the purpose of the whole thing).</p>
<p>Stumbling over the sorted-out-music folder and running into them again, I decided to share them with the world, if only to spare other don quixotesque music fans out there from the pain of having to go thorough endless pages of search results only to find (mostly) the folowing songs listed over and over.</p>
<h3>Top ten strip songs</h3>
<p><strong>Note</strong>: the order is mostly a matter of personal taste &amp; current state of mind. The purpose is to provide you with a list of good striptease songs, rather than to make statements about the songs&#8217; quality when compared to one another, as I find judging taste to be pointless.</p>
<p>#10. <strong>Kylie Minogue &#8211; <em>Chocolate</em></strong> [<a href="http://www.google.com/search?q=Kylie+Minogue+Chocolate&amp;tbs=vid:1">watch</a>]<br />
Not much surprise in Kylie showing up in this list, now is there? </p>
<p>#9. <strong>Tina Turner &#8211; <em>Private Dancer</em></strong> [<a href="http://www.google.com/search?q=Tina+Turner+Private+Dancer&amp;tbs=vid:1">watch</a>]<br />
Striptease 101.</p>
<p>#8. <strong>Pussycat Dolls &amp; Busta Rhymes &#8211; <em>Don&#8217;t Cha</em></strong> [<a href="http://www.youtube.com/watch?v=l3Y6zMyN84s">watch</a>]<br />
Excellent rythm; rap &amp; hip hop fans will appreciate it.</p>
<p>#7. <strong>Def Leppard &#8211; <em>Pour Some Sugar on Me</em></strong> [<a href="http://www.google.com/search?q=Def+Leppard+Pour+Some+Sugar+on+Me&amp;tbs=vid:1">watch</a>]<br />
Rock.</p>
<p>#6. <strong>Right Said Fred &#8211; <em>I&#8217;m Too Sexy</em></strong> [<a href="http://www.youtube.com/watch?v=39YUXIKrOFk">watch</a>]<br />
Funny rather than sexy, but still a good song to drop clothes to.</p>
<p>#5. <strong>Joe Cocker &#8211; <em>You Can Leave Your Hat On</em></strong> [<a href="http://www.google.com/search?q=Joe+Cocker+Can+Leave+Your+Hat&amp;tbs=vid:1">watch</a>]<br />
Synonymous with &#8220;striptease song&#8221;; would be higher in the top if it weren&#8217;t so damn everywhere.</p>
<p>#4. <strong>Alannah Myles &#8211; <em>Black Velvet</em></strong> [<a href="http://www.google.com/search?q=Alannah+Myles+Black+Velvet&amp;tbs=vid:1">watch</a>]<br />
Another classic, slower &amp; more sensual than most in this top.</p>
<p>#3. <strong>Prince &#8211; <em>Cream</em></strong> [<a href="http://www.google.com/search?q=Prince+Cream&amp;tbs=vid:1">watch</a>]<br />
I&#8217;m not that much into Prince, but this song is by far among the best strip songs I&#8217;ve heard so far.</p>
<p>#2. <strong>Patricia Kaas &#8211; <em>Mademoiselle chante le blues</em></strong> [<a href="http://www.google.com/search?q=Patricia+Kaas+Mademoiselle+chante+blues&amp;tbs=vid:1">watch</a>]<br />
French strip song. &#8217;nuff said.</p>
<p>#1. <strong>En Vogue &#8211; <em>Beat of Love</em></strong> [<a href="http://www.youtube.com/watch?v=q1w0-0uhGZY">listen</a>]<br />
The bass drum rythm on this song is simply amazing, earning it a #1.</p>
<h3>Bonus song</h3>
<p>Instrumental music is excellent for striptease, and one of the best ones at that is the <strong>theme song from <em>A Shot in the Dark</em></strong> (1964; the second Pink Panther movie, featuring Peter Sellers &#8211; a &#8220;dark&#8221; comedy which you may also enjoy) by Henry Mancini. If you need further proof, check out Ursula Martinez&#8217; <em>Hanky Panky</em> (strip) show. The sax, as on <em>Mademoiselle chante le blues</em>, is excellent. On the instrumental topic, half funny-half sexy is also <strong><em>The Stripper</em></strong> from <strong>Joe Loss</strong>.</p>
<h3>Runners up</h3>
<p>As with most tops, some runners up got left out. In alphabetical order:</p>
<ul>
<li>Ella Fitzgerald &#8211; <em>I Just Wanna Make Love to You</em></li>
<li>Firefox &#8211; <em>Sex Shooter</em></li>
<li>Ginuwine &#8211; <em>Pony</em></li>
<li>Lovage &#8211; <em>Sex (I&#8217;m A)</em></li>
<li>Paula Cole &#8211; <em>Feelin&#8217; Love</em></li>
<li>Queen &#8211; <em>Fat Bottomed Girls</em></li>
<li>Sam Brown &#8211; <em>Stop</em></li>
<li>Santana &amp; Rob Thomas &#8211; <em>Smooth</em></li>
<li>Shaggy &#8211; <em>Hey Sexy Lady</em></li>
<li>The Sugababes &#8211; <em>Push the Button</em></li>
<li>Tom Jones &#8211; <em>Sex Bomb</em></li>
<li>Tom Jones &#8211; <em>You Don&#8217;t Have to Be Rich</em></li>
<li>Touch &amp; Go &#8211; <em>Tango in Harlem</em></li>
<li>Zuchero &#8211; <em>Baila Sexy Thing</em></li>
</ul>
<p>You can sample most of them on <del datetime="2010-11-15T15:05:56+00:00"><a href="http://www.imeem.com/">imeem.com</a></del> [later edit: imeem is now defunct; changed links to point to Google video searches], on <a href="http://www.youtube.com/">youtube</a> or simply googling/yahoo(ing?) for them.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/vtopan.wordpress.com/39/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/vtopan.wordpress.com/39/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/vtopan.wordpress.com/39/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/vtopan.wordpress.com/39/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/vtopan.wordpress.com/39/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/vtopan.wordpress.com/39/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/vtopan.wordpress.com/39/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/vtopan.wordpress.com/39/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/vtopan.wordpress.com/39/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/vtopan.wordpress.com/39/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/vtopan.wordpress.com/39/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/vtopan.wordpress.com/39/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/vtopan.wordpress.com/39/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/vtopan.wordpress.com/39/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vtopan.wordpress.com&amp;blog=6592892&amp;post=39&amp;subd=vtopan&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://vtopan.wordpress.com/2009/04/06/top-ten-striptease-songs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/539dcced66dd05d6ba7009aca8abf164?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">vtopan</media:title>
		</media:content>
	</item>
		<item>
		<title>Translating headers: struct / record field alignment in C / Delphi</title>
		<link>http://vtopan.wordpress.com/2009/03/10/translating-headers-struct-record-field-alignment-in-c-delphi/</link>
		<comments>http://vtopan.wordpress.com/2009/03/10/translating-headers-struct-record-field-alignment-in-c-delphi/#comments</comments>
		<pubDate>Tue, 10 Mar 2009 23:37:03 +0000</pubDate>
		<dc:creator>vtopan</dc:creator>
				<category><![CDATA[Delphi]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[datatypes]]></category>
		<category><![CDATA[header translation]]></category>
		<category><![CDATA[record]]></category>
		<category><![CDATA[struct]]></category>

		<guid isPermaLink="false">http://vtopan.wordpress.com/?p=30</guid>
		<description><![CDATA[If you&#8217;ve ever taken a shot at porting headers from C to Delphi, you may have noticed the different way in which the struct/record fields are aligned, but most likely you havent. And if you had the bad luck of stumbling over a struct that gets aligned differently when ported to Delphi, even though the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vtopan.wordpress.com&amp;blog=6592892&amp;post=30&amp;subd=vtopan&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;ve ever taken a shot at porting headers from C to Delphi, you may have noticed the different way in which the struct/record fields are aligned, but most likely you havent. And if you had the bad luck of stumbling over a struct that gets aligned differently when ported to Delphi, even though the data types were translated correctly, you&#8217;re probably interested in what happens behind the scenes and how to fix it. Well, here it goes.</p>
<h3>Why variable addresses are aligned in memory</h3>
<p>As explained in depth <a href='http://en.wikipedia.org/wiki/Packed'>here</a>, because of it&#8217;s internal structure, the time required by the CPU to access data from the RAM depends on the alignment of the address it requests (the &#8220;aligned&#8221; memory addresses being accessed faster). <i>Aligned</i> means that the address should be a multiple of the CPU&#8217;s <i>memory word</i> (not to be confused with the WORD data type in C/Delphi, which is always 2 bytes), which is the least amount of memory the CPU can get from RAM at one time. It&#8217;s also the size of the general purpose registers on the CPU, and it represents the &#8220;chunk&#8221; of data the CPU is most &#8220;comfortable&#8221; working with. On x86 architectures it&#8217;s 32 bits, which means 4 bytes (on 64 bit architectures it&#8217;s&#8230; well, 64 bits = 8 bytes). So, the memory addresses should be multiples of 4 on x86 systems for faster access.</p>
<p>Nitpicking corner: why is the WORD type 2 bytes long then, if the <i>memory word</i> on x86 is 4 bytes? Well, it&#8217;s not actually *all* the x86 architectures that have a 4 byte memory word, just the 386-and-higher ones (the aptly-named 32-bit architectures). But back when dinosaurs still roamed the Earth and &#8220;640K of memory were enough for anybody&#8221; (to paraphrase <a href='http://www.wired.com/politics/law/news/1997/01/1484'>(?)</a> one of the foremost computer visionaries), the computer word was just 16 bits, or 2 bytes, and that&#8217;s when the WORD data type was coined (without very much foresight, truth be told).</p>
<h3>Struct field alignment in C</h3>
<p>According to this <a href='http://en.wikipedia.org/wiki/Packed#Typical_alignment_of_C_structs_on_x86'>paragraph</a> on Wikipedia, struct member fields are aligned based on their size by padding them with, basically, their size in bytes. So, assuming we declare the structure:</p>
<p><code>typedef struct {<br />
  BYTE a;<br />
  WORD b;<br />
  BYTE c;<br />
  DWORD d;<br />
  } foo;</code></p>
<p>you might be tempted to assume it will take 8 bytes of memory. But, due to the afore-mentioned alignment, <i>b</i> will be aligned on a 2-byte boundary, and <i>c</i> on a 4-byte boundary, so that structure will actually be equivalent to this one:</p>
<p><code>typedef struct {<br />
  BYTE a;<br />
  BYTE padding_for_b[1]; // 1-byte padding to align .b<br />
  WORD b;<br />
  BYTE c;<br />
  BYTE padding_for_d[3]; // 3-byte padding to align .d<br />
  DWORD d;<br />
  } foo;</code></p>
<p>and take precisely 12 bytes. You can force C&#8217;s hand not to align the members using the <i>#pragma pack(1)</i> directive, but (a) the code will be slower and (b) you can&#8217;t do that to standard structures in the Windows API for obvious reasons.</p>
<h3>Record field alignment in Delphi</h3>
<p>As explained in the &#8220;align fields compiler directive&#8221; help entry, in Delphi the record field alignment is not based on the field&#8217;s data type size, but on a constant specified by the <i>ALIGN</i> (<i>A</i>) directive (which can also be set for the entire project from <i>Project-&gt;Options-&gt;Compiler-&gt;Record field alignment</i>.<br />
The default is <i>{$ALIGN 8}</i> (short: <i>{$A8}</i>), which means alignment to an 8-byte boundary. However, that&#8217;s how things work only in theory. In practice, for some mysterious reason, Delphi does exactly what C does: unless the alignment is set to 1 (eg. by <i>{$A1}</i>), it aligns record fields based on their size.</p>
<h3>The difference</h3>
<p>But if Delphi behaves just like C, why do things sometimes still crash? They do because of one exception to the rule: enumerations are treated differently. In C, they are 4 bytes in size by default (this can be changed using the <i>#pragma enum</i> directive). In Delphi, the enumeration size is controlled by the <i>$Z</i> directive, and by default it&#8217;s set to 1. Therefore, in order to get the proper translation of API structures from C to Delphi, you should use the {$Z4} directive.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/vtopan.wordpress.com/30/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/vtopan.wordpress.com/30/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/vtopan.wordpress.com/30/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/vtopan.wordpress.com/30/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/vtopan.wordpress.com/30/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/vtopan.wordpress.com/30/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/vtopan.wordpress.com/30/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/vtopan.wordpress.com/30/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/vtopan.wordpress.com/30/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/vtopan.wordpress.com/30/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/vtopan.wordpress.com/30/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/vtopan.wordpress.com/30/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/vtopan.wordpress.com/30/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/vtopan.wordpress.com/30/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vtopan.wordpress.com&amp;blog=6592892&amp;post=30&amp;subd=vtopan&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://vtopan.wordpress.com/2009/03/10/translating-headers-struct-record-field-alignment-in-c-delphi/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/539dcced66dd05d6ba7009aca8abf164?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">vtopan</media:title>
		</media:content>
	</item>
	</channel>
</rss>
