<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

  <title>Articles Feed (Atom)</title>
  <link href="http://unindented.org/articles/atom/index.xml" rel="self" />
  <link href="http://unindented.org/" />
  <updated>2009-12-16T00:14:15+01:00</updated>
  <author>
    <name>Daniel Perez Alvarez</name>
    <email>daniel@unindented.org</email>
  </author>
  <id>http://unindented.org/</id>

  <entry>
    <id>tag:unindented.org,2009-11-22:1258846077</id>
    <title>Xcode Project Template for Cocos2D for iPhone</title>
    <updated>2009-11-22T00:27:57+01:00</updated>
    <link href="http://unindented.org/articles/2009/11/xcode-project-template-for-cocos2d-for-iphone/" rel="alternate" />
    <summary>How to create your own Xcode template for a Cocos2D for iPhone project.</summary>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml"><p>The latest versions of <a href="http://www.cocos2d-iphone.org/">Cocos2D for iPhone</a> include Xcode project templates to get you running quickly. You can install them by executing the <em>install_template.sh</em> script, which places said templates in the <em>/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Project Templates/Application</em> folder.</p>

<p>Maybe these templates don't quite cover your needs, so we will learn how to create our own. You can also take a look at my <a href="http://github.com/unindented/cocos2d-iphone-template">Xcode project template for Cocos2D v0.8.2</a>, in case it suits you.</p>

<h3>Create the template</h3>

<p>First, <a href="http://www.cocos2d-iphone.org/download">download the latest version of Cocos2D</a> and uncompress it to <em>/Developer/Library</em>. Now launch Xcode, open its <em>Preferences</em> window, and create a new source entry that points to the folder containing Cocos2D:</p>

<p><img src="http://unindented.org/articles/2009/11/xcode-project-template-for-cocos2d-for-iphone/image-01.png" alt="Source entry for Cocos2D" /></p>

<p>If you go to <em>/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Project Templates/Application</em>, you will see all the default iPhone project templates. The most basic template is the one called <em>Window-based Application</em>. If you open its folder, you will see it contains two others: <em>Window-based Application</em> and <em>Window-based Core Data Application</em>. We are interested in the first one. Copy it, rename it to something like <em>Cocos2D Application</em>, and place it in <em>/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Project Templates/Application</em>. You should have something like this:</p>

<p><img src="http://unindented.org/articles/2009/11/xcode-project-template-for-cocos2d-for-iphone/image-02.png" alt="Applications folder" /></p>

<p><img src="http://unindented.org/articles/2009/11/xcode-project-template-for-cocos2d-for-iphone/image-03.png" alt="Cocos2D template" /></p>

<p>Open your <em>Cocos2D Application</em> project in Xcode by double clicking the file named <em>___PROJECTNAME___.xcodeproj</em>. The project will look like this:</p>

<p><img src="http://unindented.org/articles/2009/11/xcode-project-template-for-cocos2d-for-iphone/image-04.png" alt="Cocos2D project in Xcode" /></p>

<p>Delete the nib file <em>MainWindow.xib</em>, as we don't need it for our project. Also delete the entry <em>Main nib file base name</em> from our plist file <em>___PROJECTNAMEASIDENTIFIER___-Info.plist</em>. While you are at it, you can add the entries <em>Status bar is initially hidden</em> and <em>Initial interface orientation</em>, in case you want to hide the status bar and change the interface orientation to landscape, respectively:</p>

<p><img src="http://unindented.org/articles/2009/11/xcode-project-template-for-cocos2d-for-iphone/image-05.png" alt="Editing the plist file" /></p>

<p>Right click on the <em>Frameworks</em> group and select <em>Add > Existing Frameworks...</em>. We need at least <em>OpenGLES.framework</em> and <em>QuartzCore.framework</em>, but if you are going to need any others, include them too.</p>

<p><img src="http://unindented.org/articles/2009/11/xcode-project-template-for-cocos2d-for-iphone/image-06.png" alt="Adding required frameworks" /></p>

<p>Now let's add some Cocos2D to our template. There are many ways to do this, but I think the best one is to add a reference to its Xcode project, and include the libraries we require. To keep things organized, create a new group named <em>Support</em>, and inside it another one named <em>Cocos2D</em>. Right click on this last group and select <em>Add > Existing Files...</em>. Navigate to <em>/Developer/Library</em>, open the Cocos2D folder, and select the file <em>cocos2d-iphone.xcodeproj</em>.</p>

<p>In the dialog that appears, uncheck <em>Copy items into destination group's folder</em>, and select as reference type <em>Relative to Cocos2D</em> (or whatever you named your source entry):</p>

<p><img src="http://unindented.org/articles/2009/11/xcode-project-template-for-cocos2d-for-iphone/image-07.png" alt="Adding a reference to the Cocos2D project" /></p>

<p>If you click on the newly added <em>cocos2d-iphone.xcodeproj</em>, Xcode will list all the files contained in the Cocos2D project. Scroll down until you see the static library <em>libcocos2d.a</em>, and tick its checkbox to link your application against it:</p>

<p><img src="http://unindented.org/articles/2009/11/xcode-project-template-for-cocos2d-for-iphone/image-08.png" alt="Linking against the Cocos2D library" /></p>

<p>Now double click on the target <em>___PROJECTNAME___</em>, select the <em>General</em> tab, and add Cocos2D as a dependency, so that it is built before our project:</p>

<p><img src="http://unindented.org/articles/2009/11/xcode-project-template-for-cocos2d-for-iphone/image-09.png" alt="Adding Cocos2D as a dependency" /></p>

<p>Switch to the <em>Build</em> tab, find the entry <em>User Header Search Paths</em>, and add <em>$(COCOS2D_SRC)</em>:</p>

<p><img src="http://unindented.org/articles/2009/11/xcode-project-template-for-cocos2d-for-iphone/image-10.png" alt="Adding Cocos2D to the user search path" /></p>

<p><img src="http://unindented.org/articles/2009/11/xcode-project-template-for-cocos2d-for-iphone/image-11.png" alt="Modifying build options" /></p>

<p>Finally, modify <em>main.m</em> to specify our application delegate:</p>

<div class="highlight"><pre><span class="kt">int</span> <span class="nf">main</span><span class="p">(</span><span class="kt">int</span> <span class="n">argc</span><span class="p">,</span> <span class="kt">char</span><span class="o">*</span> <span class="n">argv</span><span class="p">[])</span>
<span class="p">{</span>
    <span class="n">NSAutoreleasePool</span><span class="o">*</span> <span class="n">pool</span> <span class="o">=</span> <span class="p">[[</span><span class="n">NSAutoreleasePool</span> <span class="n">alloc</span><span class="p">]</span> <span class="n">init</span><span class="p">];</span>
    <span class="kt">int</span> <span class="n">retVal</span> <span class="o">=</span> <span class="n">UIApplicationMain</span><span class="p">(</span><span class="n">argc</span><span class="p">,</span> <span class="n">argv</span><span class="p">,</span> <span class="nb">nil</span><span class="p">,</span> <span class="s">@&quot;___PROJECTNAMEASIDENTIFIER___AppDelegate&quot;</span><span class="p">);</span>
    <span class="p">[</span><span class="n">pool</span> <span class="n">release</span><span class="p">];</span>
    <span class="k">return</span> <span class="n">retVal</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>


<p>All that's left to do is write your template code in <em>___PROJECTNAMEASIDENTIFIER___AppDelegate.h</em> and <em>___PROJECTNAMEASIDENTIFIER___AppDelegate.m</em>.</p>

<h3>Change the description and icon</h3>

<p>If you create a new project in Xcode, you will see our new template:</p>

<p><img src="http://unindented.org/articles/2009/11/xcode-project-template-for-cocos2d-for-iphone/image-12.png" alt="Our Cocos2D template" /></p>

<p>However, the description and icon are the same as those of the <em>Window-based Application</em> (obviously). Let's solve that. From the Finder, right click on the file <em>___PROJECTNAME___.xcodeproj</em>, and select <em>Show Package Contents</em>:</p>

<p><img src="http://unindented.org/articles/2009/11/xcode-project-template-for-cocos2d-for-iphone/image-13.png" alt="Contents of the xcodeproj file" /></p>

<p>Edit <em>TemplateInfo.plist</em> to add your description:</p>

<div class="highlight"><pre><span class="nt">&lt;plist</span> <span class="na">version=</span><span class="s">&quot;1.0&quot;</span><span class="nt">&gt;</span>
<span class="nt">&lt;dict&gt;</span>
    <span class="nt">&lt;key&gt;</span>Description<span class="nt">&lt;/key&gt;</span>
    <span class="nt">&lt;string&gt;</span>This template provides a starting point for a Cocos2D application.<span class="nt">&lt;/string&gt;</span>
<span class="nt">&lt;/dict&gt;</span>
<span class="nt">&lt;/plist&gt;</span>
</pre></div>


<p>To change the icon, we just have to replace the file <em>TemplateIcon.icns</em> with our own. To create a icns file, launch the app <em>Icon Composer</em> found in <em>/Developer/Applications/Utilities</em>:</p>

<p><img src="http://unindented.org/articles/2009/11/xcode-project-template-for-cocos2d-for-iphone/image-14.png" alt="The Icon Composer application" /></p>

<p>Just drag and drop the image you want to use for your icon in the different boxes. I used an image that comes with Cocos2D, found in the <em>Resources</em> folder:</p>

<p><img src="http://unindented.org/articles/2009/11/xcode-project-template-for-cocos2d-for-iphone/image-15.png" alt="Our new icon" /></p>

<p>Save your creation with the name <em>TemplateIcon.icns</em>, and replace the original. Try to create a new project from Xcode, and this time it will show up with our icon and description:</p>

<p><img src="http://unindented.org/articles/2009/11/xcode-project-template-for-cocos2d-for-iphone/image-16.png" alt="Our Cocos2D template, with its own icon and description" /></p>

<p>That's it, we are done! I hope to see your amazing templates uploaded to GitHub.</p>
</div>
    </content>
    <category term="Cocos2D" />
    <category term="iPhone" />
    <category term="Xcode" />
  </entry>
  <entry>
    <id>tag:unindented.org,2009-10-01:1254425687</id>
    <title>GitHub Ribbon Using CSS Transforms</title>
    <updated>2009-10-01T21:34:47+02:00</updated>
    <link href="http://unindented.org/articles/2009/10/github-ribbon-using-css-transforms/" rel="alternate" />
    <summary>How to replicate the GitHub ribbon using CSS transforms.</summary>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml"><p>For those of you who don't know, <a href="http://github.com/">GitHub</a> is a web-based hosting service for projects that employ the <a href="http://git-scm.com/">Git</a> version control system. You can use GitHub to host your public and private projects, collaborate on them in a distributed way, and expand your social coding network.</p>

<p>If you want to show the world your love for GitHub, they put up some <a href="http://github.com/blog/273-github-ribbons">ribbons</a> that you can overlay on your site. Unfortunately they are just PNG images, so if you want to make any modification, e.g. change the background color, you will have to download the provided PSD file and edit it with your tool of choice.</p>

<p>Using some of the features found in modern browsers, we will turn those images into HTML and CSS, so that we can style them as we please.</p>

<p>Check out the <a href="http://unindented.org/articles/2009/10/github-ribbon-using-css-transforms/demo/">demo</a>!</p>

<h3>Superstylin'</h3>

<p>First of all, let's see the HTML we will use for the ribbon. An extra <code>div</code> tag surrounding the link to our GitHub profile is all we need:</p>

<div class="highlight"><pre><span class="nt">&lt;div</span> <span class="na">class=</span><span class="s">&quot;ribbon&quot;</span><span class="nt">&gt;</span>
  <span class="nt">&lt;a</span> <span class="na">href=</span><span class="s">&quot;#&quot;</span> <span class="na">rel=</span><span class="s">&quot;me&quot;</span><span class="nt">&gt;</span>Fork me on GitHub<span class="nt">&lt;/a&gt;</span>
<span class="nt">&lt;/div&gt;</span>
</pre></div>


<p>Now let's try to replicate the look of the ribbon with some basic CSS:</p>

<div class="highlight"><pre><span class="nc">.ribbon</span> <span class="p">{</span>
  <span class="k">background-color</span><span class="o">:</span> <span class="m">#a00</span><span class="p">;</span>
  <span class="k">overflow</span><span class="o">:</span> <span class="k">hidden</span><span class="p">;</span>
<span class="p">}</span>
<span class="nc">.ribbon</span> <span class="nt">a</span> <span class="p">{</span>
  <span class="k">border</span><span class="o">:</span> <span class="m">1px</span> <span class="k">solid</span> <span class="m">#faa</span><span class="p">;</span>
  <span class="k">color</span><span class="o">:</span> <span class="m">#fff</span><span class="p">;</span>
  <span class="k">display</span><span class="o">:</span> <span class="k">block</span><span class="p">;</span>
  <span class="k">font</span><span class="o">:</span> <span class="k">bold</span> <span class="m">81.25%</span> <span class="s1">&#39;Helvetiva Neue&#39;</span><span class="o">,</span> <span class="n">Helvetica</span><span class="o">,</span> <span class="n">Arial</span><span class="o">,</span> <span class="k">sans-serif</span><span class="p">;</span>
  <span class="k">margin</span><span class="o">:</span> <span class="m">0.05em</span> <span class="m">0</span><span class="p">;</span>
  <span class="k">padding</span><span class="o">:</span> <span class="m">0.5em</span> <span class="m">3.5em</span><span class="p">;</span>
  <span class="k">text-align</span><span class="o">:</span> <span class="k">center</span><span class="p">;</span>
  <span class="k">text-decoration</span><span class="o">:</span> <span class="k">none</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>


<p>The only rule that may catch your eye is that <code>overflow: hidden</code>. Under some circumstances the text would span two lines, and this seemed to solve it.</p>

<p>Also, the font used for the original GitHub ribbon is <a href="http://www.fontriver.com/font/collegiate/">Collegiate</a>. I went with Helvetica instead, but feel free to experiment with <a href="https://developer.mozilla.org/en/CSS/@font-face"><code>@font-face</code></a> if you want to stay faithful to the original design.</p>

<p>Anyway, here is what we've got:</p>

<p><img src="http://unindented.org/articles/2009/10/github-ribbon-using-css-transforms/image-01.png" alt="Ribbon with basic styles" /></p>

<p>To achieve the rotation effect, we will use the <code>transform</code> property. It is still in <a href="http://www.w3.org/TR/css3-2d-transforms/">draft stage</a>, so we will have to fall back on browser-specific declarations (<a href="https://developer.mozilla.org/en/CSS/-moz-transform"><code>-moz-transform</code></a> for Firefox 3.5+, <a href="http://developer.apple.com/mac/library/documentation/AppleApplications/Reference/SafariCSSRef/Articles/StandardCSSProperties.html"><code>-webkit-transform</code></a> for Safari 3.1+):</p>

<div class="highlight"><pre><span class="nc">.ribbon</span> <span class="p">{</span>
  <span class="o">...</span>
  <span class="k">position</span><span class="o">:</span> <span class="k">absolute</span><span class="p">;</span>
  <span class="k">left</span><span class="o">:</span> <span class="m">-3em</span><span class="p">;</span>
  <span class="k">top</span><span class="o">:</span> <span class="m">2.5em</span><span class="p">;</span>
  <span class="o">-</span><span class="n">moz</span><span class="o">-</span><span class="n">transform</span><span class="o">:</span> <span class="n">rotate</span><span class="p">(</span><span class="m">-45</span><span class="n">deg</span><span class="p">);</span>
  <span class="o">-</span><span class="n">webkit</span><span class="o">-</span><span class="n">transform</span><span class="o">:</span> <span class="n">rotate</span><span class="p">(</span><span class="m">-45</span><span class="n">deg</span><span class="p">);</span>
<span class="p">}</span>
</pre></div>


<p>I'm applying a counterclockwise rotation of 45 degrees, and using the <code>top</code> and <code>left</code> properties to place the ribbon in the upper left corner of my page. Tweak those values according to your needs. We should be looking at something similar to this:</p>

<p><img src="http://unindented.org/articles/2009/10/github-ribbon-using-css-transforms/image-02.png" alt="Ribbon with rotation" /></p>

<p>Let's give the ribbon the final touch using the <code>text-shadow</code> and <code>box-shadow</code> properties. The latter is in <a href="http://www.w3.org/TR/css3-background/">draft stage</a>, so again we have to fall back on browser-specific declarations (<a href="https://developer.mozilla.org/en/CSS/-moz-box-shadow"><code>-moz-box-shadow</code></a> for Firefox 3.5+, <a href="http://developer.apple.com/mac/library/documentation/AppleApplications/Reference/SafariCSSRef/Articles/StandardCSSProperties.html"><code>-webkit-box-shadow</code></a> for Safari 3.0+):</p>

<div class="highlight"><pre><span class="nc">.ribbon</span> <span class="p">{</span>
  <span class="o">...</span>
  <span class="o">-</span><span class="n">moz</span><span class="o">-</span><span class="n">box</span><span class="o">-</span><span class="n">shadow</span><span class="o">:</span> <span class="m">0</span> <span class="m">0</span> <span class="m">1em</span> <span class="m">#888</span><span class="p">;</span>
  <span class="o">-</span><span class="n">webkit</span><span class="o">-</span><span class="n">box</span><span class="o">-</span><span class="n">shadow</span><span class="o">:</span> <span class="m">0</span> <span class="m">0</span> <span class="m">1em</span> <span class="m">#888</span><span class="p">;</span>
<span class="p">}</span>
<span class="nc">.ribbon</span> <span class="nt">a</span> <span class="p">{</span>
  <span class="o">...</span>
  <span class="k">text-shadow</span><span class="o">:</span> <span class="m">0</span> <span class="m">0</span> <span class="m">0.5em</span> <span class="m">#444</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>


<p>If you take a look at the documentation for those two properties, you will see I'm using shadows with no offset and a slight blur. The result:</p>

<p><img src="http://unindented.org/articles/2009/10/github-ribbon-using-css-transforms/image-03.png" alt="Ribbon with final touches" /></p>

<p>It's not identical to the original, but I think it's a good enough approximation.</p>

<p>To sum up, this is all the CSS we used to build the ribbon:</p>

<div class="highlight"><pre><span class="nc">.ribbon</span> <span class="p">{</span>
  <span class="k">background-color</span><span class="o">:</span> <span class="m">#a00</span><span class="p">;</span>
  <span class="k">overflow</span><span class="o">:</span> <span class="k">hidden</span><span class="p">;</span>
  <span class="c">/* top left corner */</span>
  <span class="k">position</span><span class="o">:</span> <span class="k">absolute</span><span class="p">;</span>
  <span class="k">left</span><span class="o">:</span> <span class="m">-3em</span><span class="p">;</span>
  <span class="k">top</span><span class="o">:</span> <span class="m">2.5em</span><span class="p">;</span>
  <span class="c">/* 45 deg ccw rotation */</span>
  <span class="o">-</span><span class="n">moz</span><span class="o">-</span><span class="n">transform</span><span class="o">:</span> <span class="n">rotate</span><span class="p">(</span><span class="m">-45</span><span class="n">deg</span><span class="p">);</span>
  <span class="o">-</span><span class="n">webkit</span><span class="o">-</span><span class="n">transform</span><span class="o">:</span> <span class="n">rotate</span><span class="p">(</span><span class="m">-45</span><span class="n">deg</span><span class="p">);</span>
  <span class="c">/* shadow */</span>
  <span class="o">-</span><span class="n">moz</span><span class="o">-</span><span class="n">box</span><span class="o">-</span><span class="n">shadow</span><span class="o">:</span> <span class="m">0</span> <span class="m">0</span> <span class="m">1em</span> <span class="m">#888</span><span class="p">;</span>
  <span class="o">-</span><span class="n">webkit</span><span class="o">-</span><span class="n">box</span><span class="o">-</span><span class="n">shadow</span><span class="o">:</span> <span class="m">0</span> <span class="m">0</span> <span class="m">1em</span> <span class="m">#888</span><span class="p">;</span>
<span class="p">}</span>
<span class="nc">.ribbon</span> <span class="nt">a</span> <span class="p">{</span>
  <span class="k">border</span><span class="o">:</span> <span class="m">1px</span> <span class="k">solid</span> <span class="m">#faa</span><span class="p">;</span>
  <span class="k">color</span><span class="o">:</span> <span class="m">#fff</span><span class="p">;</span>
  <span class="k">display</span><span class="o">:</span> <span class="k">block</span><span class="p">;</span>
  <span class="k">font</span><span class="o">:</span> <span class="k">bold</span> <span class="m">81.25%</span> <span class="s1">&#39;Helvetiva Neue&#39;</span><span class="o">,</span> <span class="n">Helvetica</span><span class="o">,</span> <span class="n">Arial</span><span class="o">,</span> <span class="k">sans-serif</span><span class="p">;</span>
  <span class="k">margin</span><span class="o">:</span> <span class="m">0.05em</span> <span class="m">0</span> <span class="m">0.075em</span> <span class="m">0</span><span class="p">;</span>
  <span class="k">padding</span><span class="o">:</span> <span class="m">0.5em</span> <span class="m">3.5em</span><span class="p">;</span>
  <span class="k">text-align</span><span class="o">:</span> <span class="k">center</span><span class="p">;</span>
  <span class="k">text-decoration</span><span class="o">:</span> <span class="k">none</span><span class="p">;</span>
  <span class="c">/* shadow */</span>
  <span class="k">text-shadow</span><span class="o">:</span> <span class="m">0</span> <span class="m">0</span> <span class="m">0.5em</span> <span class="m">#444</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>


<h3>Browser support</h3>

<p>As you can imagine, our ribbon will not be displayed properly by the majority of browsers:</p>

<table summary="Browser support">
  <thead>
    <tr>
      <td> </td>
      <th scope="col">Firefox</th>
      <th scope="col">Safari</th>
      <th scope="col">Chrome</th>
      <th scope="col">Opera</th>
      <th scope="col">IE</th>
    </tr>
  </thead>
  <tfoot>
    <tr>
      <td> </td>
      <th scope="col">3.5+</th>
      <th scope="col">3.1+</th>
      <th scope="col">Yes</th>
      <th scope="col">No</th>
      <th scope="col">No</th>
    </tr>
  </tfoot>
  <tbody>
    <tr>
      <th scope="row">-moz-transform</th>
      <td>3.5+</td>
      <td>No</td>
      <td>No</td>
      <td>No</td>
      <td>No</td>
    </tr>
    <tr>
      <th scope="row">-webkit-transform</th>
      <td>No</td>
      <td>3.1+</td>
      <td>Yes</td>
      <td>No</td>
      <td>No</td>
    </tr>
    <tr>
      <th scope="row">-moz-box-shadow</th>
      <td>3.5+</td>
      <td>No</td>
      <td>No</td>
      <td>No</td>
      <td>No</td>
    </tr>
    <tr>
      <th scope="row">-webkit-box-shadow</th>
      <td>No</td>
      <td>3.0+</td>
      <td>Yes</td>
      <td>No</td>
      <td>No</td>
    </tr>
    <tr>
      <th scope="row">text-shadow</th>
      <td>3.0+</td>
      <td>Yes</td>
      <td>Yes</td>
      <td>9.5+</td>
      <td>No</td>
    </tr>
  </tbody>
</table>


<p>Nevertheless, it's a useful technique with many compelling benefits. If you are only targeting modern browsers, go for it!</p>
</div>
    </content>
    <category term="CSS" />
  </entry>
  <entry>
    <id>tag:unindented.org,2009-09-19:1253380758</id>
    <title>Brute-Forcing Your Web Application</title>
    <updated>2009-09-19T19:19:18+02:00</updated>
    <link href="http://unindented.org/articles/2009/09/brute-forcing-your-web-application/" rel="alternate" />
    <summary>How to test your password-protected web application for a brute-force attack.</summary>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml"><p>In many Ruby on Rails tutorials (specially those aimed at beginners), you will often see user authentication handled like this:</p>

<div class="highlight"><pre><span class="k">class</span> <span class="nc">Admin</span><span class="o">::</span><span class="no">PostsController</span> <span class="o">&lt;</span> <span class="no">ApplicationController</span>
  <span class="n">before_filter</span> <span class="ss">:authenticate</span>

  <span class="kp">protected</span>

  <span class="k">def</span> <span class="nf">authenticate</span>
    <span class="n">authenticate_or_request_with_http_basic</span> <span class="k">do</span> <span class="o">|</span><span class="nb">name</span><span class="p">,</span> <span class="n">pass</span><span class="o">|</span>
      <span class="nb">name</span> <span class="o">==</span> <span class="s1">&#39;admin&#39;</span> <span class="o">&amp;&amp;</span> <span class="n">pass</span> <span class="o">==</span> <span class="s1">&#39;snafu&#39;</span>
    <span class="k">end</span>
  <span class="k">end</span>
<span class="k">end</span>
</pre></div>


<p>This may be acceptable for a quick prototype, but in a production application it spells disaster. With the right tools, we could launch an attack and find out the password in a matter of hours.</p>

<h3>Dictionary attack</h3>

<p><a href="http://www.thc.org/">Hydra</a> is a dictionary-based remote password cracker, i.e., it uses a word list to try to find out the username and password.</p>

<p>Let's suppose the attacker knows (or guesses) that the administrator username is <em>admin</em>. He would just have to specify the word list to use and the URL of your application, and go grab some coffee:</p>

<div class="highlight"><pre>C:\&gt;hydra -l admin -P english.dic -s 3000 127.0.0.1 http-get /posts/new
Hydra v5.4 (c) 2006 by van Hauser / THC - use allowed only for legal purposes.
Hydra (http://www.thc.org) starting at 2009-09-19 17:55:40
[DATA] 16 tasks, 1 servers, 20864 login tries (l:1/p:20864), ~1304 tries per task
[DATA] attacking service http-get on port 3000
[STATUS] 1384.00 tries/min, 1384 tries in 00:01h, 19480 todo in 00:15h
[STATUS] 1394.67 tries/min, 4184 tries in 00:03h, 16680 todo in 00:12h
[STATUS] 1395.86 tries/min, 9771 tries in 00:07h, 11093 todo in 00:08h
[STATUS] 1406.50 tries/min, 16878 tries in 00:12h, 3986 todo in 00:03h
[3000][www] host: 127.0.0.1   login: admin   password: snafu
[STATUS] attack finished for 127.0.0.1 (waiting for childs to finish)
Hydra (http://www.thc.org) finished at 2009-09-19 18:08:10
</pre></div>


<p>Don't think that you are safe because your password isn't a word picked from the dictionary. An attacker can tune his word list in innumerable ways.</p>

<h3>Search attack</h3>

<p><a href="http://www.hoobie.net/brutus/">Brutus</a> is a bit more flexible than Hydra, as it allows various types of attacks. We will use it to perform a search attack, i.e., it will try all possible combinations of a given character set and a given password length range:</p>

<p><img src="http://unindented.org/articles/2009/09/brute-forcing-your-web-application/image-01.png" alt="Brutus in action" /></p>

<p>It's slower than a dictionary attack, because the search space is much bigger, but, with the right character set and length range, the attacker will eventually get what he wants.</p>

<h3>Protecting yourself</h3>

<p>Try to avoid <code>authenticate_or_request_with_http_basic</code> in your Rails apps. There are better alternatives, like <a href="http://github.com/technoweenie/restful-authentication">Restful Authentication</a>.</p>

<p>If you have to stick with basic authentication, try to increase the time it takes the attacker to guess your password by:</p>

<ul>
<li>Picking unusual administrator usernames</li>
<li>Increasing the length of your passwords</li>
<li>Increasing the complexity of your passwords (e.g. by including spaces, punctuation characters, etc.)</li>
<li>Imposing a delay between failed authentication attempts</li>
<li>Locking the account after a number of failed authentication attempts</li>
</ul>


<p>Remember that a brute-force attack will always succeed, it's just a question of time!</p>
</div>
    </content>
    <category term="Hacking" />
    <category term="Rails" />
  </entry>
  <entry>
    <id>tag:unindented.org,2007-04-29:1177881391</id>
    <title>Tooltips Hidden Behind the Taskbar</title>
    <updated>2007-04-29T23:16:31+02:00</updated>
    <link href="http://unindented.org/articles/2007/04/tooltips-hidden-behind-the-taskbar/" rel="alternate" />
    <summary>Fix tooltips getting hidden behind the taskbar in Windows.</summary>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml"><p>There's a really annoying bug in multiple versions of Windows where sometimes the system tray tooltips and popup notifications will show up behind the taskbar, or behind other windows:</p>

<p><img src="http://unindented.org/articles/2007/04/tooltips-hidden-behind-the-taskbar/image-01.png" alt="Tooltip hidden behind the taskbar" /></p>

<p>It's documented in <a href="http://support.microsoft.com/kb/912650">Microsoft's Knowledge Base</a>, but I find the proposed solutions a bit lacking:</p>

<blockquote><p>To resolve this issue, use one of the following methods:</p>

<ul>
<li>Log off and then log back on to the current account.</li>
<li>Restart your computer.</li>
</ul>
</blockquote>

<p>I'm constantly getting bitten by this bug, so I developed a little program to fix it. What it does is apply the style <code>HWND_TOPMOST</code> to all windows that have the class name <code>tooltips_class32</code>, solving the problem (temporarily):</p>

<div class="highlight"><pre><span class="nf">SetTopmost</span> <span class="nv">PROC</span> <span class="nv">hWnd</span> <span class="p">:</span><span class="nv">HWND</span>

    <span class="c1">; we just need to apply HWND_TOPMOST, the window procedure for the tooltip</span>
    <span class="c1">; automatically sets the size, position, and visibility of the control</span>
    <span class="nf">INVOKE</span> <span class="nv">SetWindowPos</span><span class="p">,</span> <span class="nv">hWnd</span><span class="p">,</span> <span class="nv">HWND_TOPMOST</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="nv">SWP_NOMOVE</span> <span class="nv">OR</span> <span class="nv">SWP_NOSIZE</span> <span class="nv">OR</span> <span class="nv">SWP_NOACTIVATE</span>

    <span class="nf">ret</span>

<span class="nf">SetTopmost</span> <span class="nv">ENDP</span>
</pre></div>


<p>Maybe I could modify it so that it runs in the background, sending a message every few minutes...</p>

<p>Well, for now this is what you have: <a href="http://unindented.org/articles/2007/04/tooltips-hidden-behind-the-taskbar/tooltips.zip">tooltips.zip</a> (source included, as always).</p>
</div>
    </content>
    <category term="Windows" />
    <category term="Win32" />
  </entry>
  <entry>
    <id>tag:unindented.org,2006-02-25:1140905761</id>
    <title>Fading Windows</title>
    <updated>2006-02-25T23:16:01+01:00</updated>
    <link href="http://unindented.org/articles/2006/06/fading-windows/" rel="alternate" />
    <summary>How to apply a fade effect to your windows.</summary>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml"><p>Here I show two ways to apply a cool fade effect to your windows: one using the Win32 API function <a href="http://msdn.microsoft.com/en-us/library/ms632669"><code>AnimateWindow</code></a>, and the other using <a href="http://msdn.microsoft.com/en-us/library/ms633540"><code>SetLayeredWindowAttributes</code></a>.</p>

<h3>AnimateWindow</h3>

<p>You just have to specify the handle to the window, how long it takes to play the animation, and the type of animation. For example, you could do a fade-in when opening and a fade-out when closing the window like this:</p>

<div class="highlight"><pre><span class="nf">.IF</span> <span class="nv">uMsg</span> <span class="err">==</span> <span class="nv">WM_INITDIALOG</span>

    <span class="c1">; fade-in</span>
    <span class="nf">INVOKE</span> <span class="nv">AnimateWindow</span><span class="p">,</span> <span class="nv">hWnd</span><span class="p">,</span> <span class="mi">500</span><span class="p">,</span> <span class="nv">AW_BLEND</span>

<span class="nf">.ELSEIF</span> <span class="nv">uMsg</span> <span class="err">==</span> <span class="nv">WM_CLOSE</span>

    <span class="c1">; fade-out</span>
    <span class="nf">INVOKE</span> <span class="nv">AnimateWindow</span><span class="p">,</span> <span class="nv">hWnd</span><span class="p">,</span> <span class="mi">500</span><span class="p">,</span> <span class="nv">AW_BLEND</span> <span class="nv">OR</span> <span class="nv">AW_HIDE</span>
    <span class="c1">; close</span>
    <span class="nf">INVOKE</span> <span class="nv">EndDialog</span><span class="p">,</span> <span class="nv">hWnd</span><span class="p">,</span> <span class="nv">NULL</span>

<span class="nf">.ENDIF</span>
</pre></div>


<p>Pros:</p>

<ul>
<li>Very easy to do.</li>
<li>In addition to <code>AW_BLEND</code>, it has other effects.</li>
</ul>


<p>Cons:</p>

<ul>
<li>Some controls aren't rendered correctly during fade-in.</li>
<li>You can't specify the final transparency of the window.</li>
</ul>


<p>Example in MASM32: <a href="http://unindented.org/articles/2006/06/fading-windows/fade-1.zip">fade-1.zip</a>.</p>

<h3>SetLayeredWindowAttributes</h3>

<p>This method takes a bit more work, here's how I did the fade-in and fade-out:</p>

<div class="highlight"><pre><span class="nf">.IF</span> <span class="nv">uMsg</span> <span class="err">==</span> <span class="nv">WM_INITDIALOG</span>

    <span class="c1">; find address of SetWindowAttributes</span>
    <span class="nf">INVOKE</span> <span class="nv">LoadLibrary</span><span class="p">,</span> <span class="nv">ADDR</span> <span class="nv">sDllName</span>
    <span class="nf">.IF</span> <span class="nb">eax</span> <span class="err">!=</span> <span class="nv">NULL</span>
        <span class="nf">push</span> <span class="nb">eax</span> <span class="c1">; for FreeLibrary</span>
        <span class="nf">INVOKE</span> <span class="nv">GetProcAddress</span><span class="p">,</span> <span class="nb">eax</span><span class="p">,</span> <span class="nv">ADDR</span> <span class="nv">sApiName</span>
        <span class="nf">mov</span> <span class="nv">SetLayeredWindowAttribs</span><span class="p">,</span> <span class="nb">eax</span>
        <span class="nf">.IF</span> <span class="nv">SetLayeredWindowAttribs</span> <span class="err">!=</span> <span class="nv">NULL</span>
            <span class="c1">; make the window invisible</span>
            <span class="nf">push</span> <span class="nv">LWA_ALPHA</span>
            <span class="nf">push</span> <span class="mi">0</span>
            <span class="nf">push</span> <span class="mi">0</span>
            <span class="nf">push</span> <span class="nv">hWnd</span>
            <span class="nf">call</span> <span class="p">[</span><span class="nv">SetLayeredWindowAttribs</span><span class="p">]</span>
            <span class="c1">; show it</span>
            <span class="nf">INVOKE</span> <span class="nv">ShowWindow</span><span class="p">,</span> <span class="nv">hWnd</span><span class="p">,</span> <span class="nv">SW_SHOW</span>
            <span class="c1">; redraw it</span>
            <span class="nf">INVOKE</span> <span class="nv">RedrawWindow</span><span class="p">,</span> <span class="nv">hWnd</span><span class="p">,</span> <span class="nv">NULL</span><span class="p">,</span> <span class="nv">NULL</span><span class="p">,</span> <span class="nv">RDW_UPDATENOW</span>
        <span class="nf">.ENDIF</span>
        <span class="nf">call</span> <span class="nv">FreeLibrary</span>
    <span class="nf">.ENDIF</span>

    <span class="c1">; fade-in</span>
    <span class="nf">.IF</span> <span class="nv">SetLayeredWindowAttribs</span> <span class="err">!=</span> <span class="nv">NULL</span>
        <span class="nf">push</span> <span class="nb">esi</span>
        <span class="nf">mov</span> <span class="nb">esi</span><span class="p">,</span> <span class="mi">0</span>
        <span class="nf">.WHILE</span> <span class="nb">esi</span> <span class="o">&lt;</span> <span class="mi">255</span>
            <span class="nf">inc</span> <span class="nb">esi</span>
            <span class="nf">push</span> <span class="nv">LWA_ALPHA</span>
            <span class="nf">push</span> <span class="nb">esi</span>
            <span class="nf">push</span> <span class="mi">0</span>
            <span class="nf">push</span> <span class="nv">hWnd</span>
            <span class="nf">call</span> <span class="p">[</span><span class="nv">SetLayeredWindowAttribs</span><span class="p">]</span>
            <span class="nf">INVOKE</span> <span class="nv">Sleep</span><span class="p">,</span> <span class="mi">1</span>
        <span class="nf">.ENDW</span>
        <span class="nf">pop</span> <span class="nb">esi</span>
    <span class="nf">.ENDIF</span>

<span class="nf">.ELSEIF</span> <span class="nv">uMsg</span> <span class="err">==</span> <span class="nv">WM_CLOSE</span>

    <span class="c1">; fade-out</span>
    <span class="nf">.IF</span> <span class="nv">SetLayeredWindowAttribs</span> <span class="err">!=</span> <span class="nv">NULL</span>
        <span class="nf">push</span> <span class="nb">esi</span>
        <span class="nf">mov</span> <span class="nb">esi</span><span class="p">,</span> <span class="mi">255</span>
        <span class="nf">.WHILE</span> <span class="nb">esi</span> <span class="o">&gt;</span> <span class="mi">0</span>
            <span class="nf">dec</span> <span class="nb">esi</span>
            <span class="nf">push</span> <span class="nv">LWA_ALPHA</span>
            <span class="nf">push</span> <span class="nb">esi</span>
            <span class="nf">push</span> <span class="mi">0</span>
            <span class="nf">push</span> <span class="nv">hWnd</span>
            <span class="nf">call</span> <span class="p">[</span><span class="nv">SetLayeredWindowAttribs</span><span class="p">]</span>
            <span class="nf">INVOKE</span> <span class="nv">Sleep</span><span class="p">,</span> <span class="mi">1</span>
        <span class="nf">.ENDW</span>
        <span class="nf">pop</span> <span class="nb">esi</span>
    <span class="nf">.ENDIF</span>
    <span class="c1">; close</span>
    <span class="nf">INVOKE</span> <span class="nv">EndDialog</span><span class="p">,</span> <span class="nv">hWnd</span><span class="p">,</span> <span class="nv">NULL</span>

<span class="nf">.ENDIF</span>
</pre></div>


<p>Pros:</p>

<ul>
<li>Renders all controls correctly during fade-in (<code>AnimateWindow</code> doesn't, AFAIK).</li>
<li>You can specify the final transparency of the window.</li>
</ul>


<p>Cons:</p>

<ul>
<li>Not as easy as <code>AnimateWindow</code>.</li>
<li><code>AnimateWindow</code> has other types of effects.</li>
</ul>


<p>Example in MASM32: <a href="http://unindented.org/articles/2006/06/fading-windows/fade-2.zip">fade-2.zip</a>.</p>
</div>
    </content>
    <category term="Windows" />
    <category term="Win32" />
  </entry>
  <entry>
    <id>tag:unindented.org,2005-03-06:1110146904</id>
    <title>Detecting VMware</title>
    <updated>2005-03-06T23:08:24+01:00</updated>
    <link href="http://unindented.org/articles/2005/03/detecting-vmware/" rel="alternate" />
    <summary>How to detect if your program is running inside a VMware virtual machine.</summary>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml"><p>To detect if your program is running inside a VMware virtual machine, all you need is four assembly instructions to communicate with it's backdoor I/O port:</p>

<div class="highlight"><pre><span class="nf">mov</span> <span class="nb">eax</span><span class="p">,</span> <span class="mh">564D5868h</span>
<span class="nf">mov</span> <span class="nb">ecx</span><span class="p">,</span> <span class="mh">0Ah</span>
<span class="nf">mov</span> <span class="nb">edx</span><span class="p">,</span> <span class="mh">5658h</span>
<span class="nf">in</span> <span class="nb">eax</span><span class="p">,</span> <span class="nb">dx</span>
</pre></div>


<p>A description of this port, specific to VMware virtual machines, can be found at <a href="http://chitchat.at.infoseek.co.jp/vmware/">VM Back</a>. Take into account that if you try to do this on a non-VMware system, a global protection fault will occur.</p>

<p>I wrote examples for Windows in MASM32 (<a href="http://unindented.org/articles/2005/03/detecting-vmware/vmware-asm.zip">vmware-asm.zip</a>) and C (<a href="http://unindented.org/articles/2005/03/detecting-vmware/vmware-c.zip">vmware-c.zip</a>), as a starting point for future experiments.</p>
</div>
    </content>
    <category term="Cracking" />
    <category term="Virtualization" />
  </entry>
  <entry>
    <id>tag:unindented.org,2005-02-14:1108419989</id>
    <title>Creating a Non-Rectangular Window</title>
    <updated>2005-02-14T23:26:29+01:00</updated>
    <link href="http://unindented.org/articles/2005/02/creating-a-non-rectangular-window/" rel="alternate" />
    <summary>How to create non-rectangular windows from a bitmap file.</summary>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml"><p>You've probably come across many applications that use non-rectangular windows. Windows Media Player and Winamp instantly come to mind:</p>

<p><img src="http://unindented.org/articles/2005/02/creating-a-non-rectangular-window/image-01.png" alt="Windows Media Player skin" /></p>

<p>It's quite easy to create non-rectangular windows from a bitmap file in your Win32 applications, using functions such as <a href="http://msdn.microsoft.com/en-us/library/ms536639"><code>CreateRectRgn</code></a> and <a href="http://msdn.microsoft.com/en-us/library/ms536688"><code>CombineRgn</code></a>.</p>

<h3>With a little help</h3>

<p>I've created a helper function to simplify the process. Just include the bitmap you want to use in your resource file:</p>

<div class="highlight"><pre><span class="cp">#define IDB_BMRGN 201</span>

<span class="n">IDB_BMPRGN</span> <span class="n">BITMAP</span> <span class="n">DISCARDABLE</span> <span class="s">&quot;region.bmp&quot;</span>
</pre></div>


<p>Then, in the initialization part of your dialog, call my <code>CreateBmpRgn</code> function, specifying the handle of the window, the bitmap, and the color your want to be transparent (in the form <code>00BBGGRRh</code>):</p>

<div class="highlight"><pre><span class="c1">; load image</span>
<span class="nf">INVOKE</span> <span class="nv">LoadImage</span><span class="p">,</span> <span class="nv">hInstance</span><span class="p">,</span> <span class="nv">IDB_BMPRGN</span><span class="p">,</span> <span class="nv">IMAGE_BITMAP</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="nv">LR_DEFAULTCOLOR</span>
<span class="nf">mov</span> <span class="nv">hBitmap</span><span class="p">,</span> <span class="nb">eax</span>
<span class="c1">; create region using magenta as the transparent color</span>
<span class="nf">INVOKE</span> <span class="nv">CreateBmpRgn</span><span class="p">,</span> <span class="nv">hWnd</span><span class="p">,</span> <span class="nv">hBitmap</span><span class="p">,</span> <span class="mh">0FF00FFh</span>
<span class="c1">; set region</span>
<span class="nf">INVOKE</span> <span class="nv">SetWindowRgn</span><span class="p">,</span> <span class="nv">hWnd</span><span class="p">,</span> <span class="nb">eax</span><span class="p">,</span> <span class="nv">TRUE</span>
</pre></div>


<p>You can check out the results in this example application written in MASM32: <a href="http://unindented.org/articles/2005/02/creating-a-non-rectangular-window/non-rectangular.zip">non-rectangular.zip</a> (press <em>Esc</em> to close it).</p>

<h3>The real thing</h3>

<p>The code for the function is a bit more complex:</p>

<div class="highlight"><pre><span class="nf">CreateBmpRgn</span> <span class="nv">PROC</span> <span class="nv">hWnd</span> <span class="p">:</span><span class="nv">HWND</span><span class="p">,</span> <span class="nv">hBmRgn</span> <span class="p">:</span><span class="nv">HBITMAP</span><span class="p">,</span> <span class="nb">cl</span><span class="nv">rTrans</span> <span class="p">:</span><span class="nv">COLORREF</span>

    <span class="nf">LOCAL</span> <span class="nv">hdcDest</span> <span class="p">:</span><span class="nv">HDC</span>
    <span class="nf">LOCAL</span> <span class="nv">hdcSrc</span>  <span class="p">:</span><span class="nv">HDC</span>
    <span class="nf">LOCAL</span> <span class="nv">bmInfo</span>  <span class="p">:</span><span class="nv">BITMAP</span>

    <span class="nf">LOCAL</span> <span class="nv">hRgn</span>    <span class="p">:</span><span class="nv">HRGN</span>
    <span class="nf">LOCAL</span> <span class="nv">hTmpRgn</span> <span class="p">:</span><span class="nv">HRGN</span>
    <span class="nf">LOCAL</span> <span class="nv">x</span>       <span class="p">:</span><span class="kt">DWORD</span>
    <span class="nf">LOCAL</span> <span class="nv">y</span>       <span class="p">:</span><span class="kt">DWORD</span>
    <span class="nf">LOCAL</span> <span class="nv">begin</span>   <span class="p">:</span><span class="kt">DWORD</span>

    <span class="c1">; destination device context</span>
    <span class="nf">INVOKE</span> <span class="nv">GetDC</span><span class="p">,</span> <span class="nv">hWnd</span>
    <span class="nf">mov</span> <span class="nv">hdcDest</span><span class="p">,</span> <span class="nb">eax</span>
    <span class="c1">; source device context</span>
    <span class="nf">INVOKE</span> <span class="nv">CreateCompatibleDC</span><span class="p">,</span> <span class="nv">hdcDest</span>
    <span class="nf">mov</span> <span class="nv">hdcSrc</span><span class="p">,</span> <span class="nb">eax</span>
    <span class="c1">; get bitmap info</span>
    <span class="nf">INVOKE</span> <span class="nv">GetObject</span><span class="p">,</span> <span class="nv">hBmRgn</span><span class="p">,</span> <span class="nb">SI</span><span class="nv">ZEOF</span> <span class="nv">BITMAP</span><span class="p">,</span> <span class="nv">ADDR</span> <span class="nv">bmInfo</span>
    <span class="c1">; select bitmap into device context</span>
    <span class="nf">INVOKE</span> <span class="nv">SelectObject</span><span class="p">,</span> <span class="nv">hdcSrc</span><span class="p">,</span> <span class="nv">hBmRgn</span>

    <span class="c1">; create empty region</span>
    <span class="nf">INVOKE</span> <span class="nv">CreateRectRgn</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span>
    <span class="nf">mov</span> <span class="nv">hRgn</span><span class="p">,</span> <span class="nb">eax</span>

    <span class="nf">mov</span> <span class="nv">x</span><span class="p">,</span> <span class="mi">0</span>
    <span class="nf">mov</span> <span class="nv">y</span><span class="p">,</span> <span class="mi">0</span>

    <span class="c1">; pixel rows</span>
    <span class="nf">.WHILE</span> <span class="nv">TRUE</span>
        <span class="nf">mov</span> <span class="nb">eax</span><span class="p">,</span> <span class="nv">y</span>
        <span class="nf">.BREAK</span> <span class="nv">.IF</span> <span class="nb">eax</span> <span class="err">==</span> <span class="nv">bmInfo.bmHeight</span>

        <span class="c1">; pixel columns</span>
        <span class="nf">.WHILE</span> <span class="nv">TRUE</span>
            <span class="nf">mov</span> <span class="nb">eax</span><span class="p">,</span> <span class="nv">x</span>
            <span class="nf">.BREAK</span> <span class="nv">.IF</span> <span class="nb">eax</span> <span class="err">==</span> <span class="nv">bmInfo.bmWidth</span>

            <span class="c1">; skip first transparent pixels of the row</span>
            <span class="nf">.WHILE</span> <span class="nv">TRUE</span>
                <span class="nf">mov</span> <span class="nb">eax</span><span class="p">,</span> <span class="nv">x</span>
                <span class="nf">.BREAK</span> <span class="nv">.IF</span> <span class="nb">eax</span> <span class="err">==</span> <span class="nv">bmInfo.bmWidth</span>
                <span class="nf">INVOKE</span> <span class="nv">GetPixel</span><span class="p">,</span> <span class="nv">hdcSrc</span><span class="p">,</span> <span class="nv">x</span><span class="p">,</span> <span class="nv">y</span>
                <span class="nf">.BREAK</span> <span class="nv">.IF</span> <span class="nb">eax</span> <span class="err">!=</span> <span class="nb">cl</span><span class="nv">rTrans</span>

                <span class="nf">inc</span> <span class="nv">x</span>
            <span class="nf">.ENDW</span>

            <span class="c1">; remember this pixel</span>
            <span class="nf">push</span> <span class="nv">x</span>
            <span class="nf">pop</span> <span class="nv">begin</span>

            <span class="c1">; find first non-transparent pixel</span>
            <span class="nf">.WHILE</span> <span class="nv">TRUE</span>
                <span class="nf">mov</span> <span class="nb">eax</span><span class="p">,</span> <span class="nv">x</span>
                <span class="nf">.BREAK</span> <span class="nv">.IF</span> <span class="nb">eax</span> <span class="err">==</span> <span class="nv">bmInfo.bmWidth</span>
                <span class="nf">INVOKE</span> <span class="nv">GetPixel</span><span class="p">,</span> <span class="nv">hdcSrc</span><span class="p">,</span> <span class="nv">x</span><span class="p">,</span> <span class="nv">y</span>
                <span class="nf">.BREAK</span> <span class="nv">.IF</span> <span class="nb">eax</span> <span class="err">==</span> <span class="nb">cl</span><span class="nv">rTrans</span>

                <span class="nf">inc</span> <span class="nv">x</span>
            <span class="nf">.ENDW</span>

            <span class="c1">; create temp region</span>
            <span class="nf">mov</span> <span class="nb">eax</span><span class="p">,</span> <span class="nv">y</span>
            <span class="nf">inc</span> <span class="nb">eax</span>
            <span class="nf">INVOKE</span> <span class="nv">CreateRectRgn</span><span class="p">,</span> <span class="nv">begin</span><span class="p">,</span> <span class="nv">y</span><span class="p">,</span> <span class="nv">x</span><span class="p">,</span> <span class="nb">eax</span>
            <span class="nf">mov</span> <span class="nv">hTmpRgn</span><span class="p">,</span> <span class="nb">eax</span>
            <span class="c1">; combine it with final region</span>
            <span class="nf">INVOKE</span> <span class="nv">CombineRgn</span><span class="p">,</span> <span class="nv">hRgn</span><span class="p">,</span> <span class="nv">hRgn</span><span class="p">,</span> <span class="nv">hTmpRgn</span><span class="p">,</span> <span class="nv">RGN_OR</span>
            <span class="c1">; delete temp region</span>
            <span class="nf">INVOKE</span> <span class="nv">DeleteObject</span><span class="p">,</span> <span class="nv">hTmpRgn</span>
        <span class="nf">.ENDW</span>

        <span class="nf">mov</span> <span class="nv">x</span><span class="p">,</span> <span class="mi">0</span>
        <span class="nf">inc</span> <span class="nv">y</span>
    <span class="nf">.ENDW</span>

    <span class="c1">; release and delete device contexts</span>
    <span class="nf">INVOKE</span> <span class="nv">ReleaseDC</span><span class="p">,</span> <span class="nv">hWnd</span><span class="p">,</span> <span class="nv">hdcDest</span>
    <span class="nf">INVOKE</span> <span class="nv">DeleteDC</span><span class="p">,</span> <span class="nv">hdcSrc</span>

    <span class="nf">mov</span> <span class="nb">eax</span><span class="p">,</span> <span class="nv">hRgn</span>
    <span class="nf">ret</span>

<span class="nf">CreateBmpRgn</span> <span class="nv">ENDP</span>
</pre></div>


<p>What I'm doing is reading the image pixel by pixel, identifying which ones should be transparent, creating temporary rectangular regions, and combining them all to form the final region.</p>
</div>
    </content>
    <category term="Windows" />
    <category term="Win32" />
  </entry>
  <entry>
    <id>tag:unindented.org,2005-02-14:1108416460</id>
    <title>Opening and Closing the CD Tray</title>
    <updated>2005-02-14T22:27:40+01:00</updated>
    <link href="http://unindented.org/articles/2005/02/opening-and-closing-the-cd-tray/" rel="alternate" />
    <summary>How to open and close the CD tray from your Win32 application.</summary>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml"><p>Opening and closing the CD tray from your Win32 application is really easy. You just need to include <em>winmm.lib</em> in your code:</p>

<div class="highlight"><pre><span class="nf">INCLUDE</span> <span class="nv">C</span><span class="p">:</span><span class="err">\</span><span class="nv">masm32</span><span class="err">\</span><span class="nv">include</span><span class="err">\</span><span class="nv">winmm.inc</span>
<span class="nf">INCLUDELIB</span> <span class="nv">C</span><span class="p">:</span><span class="err">\</span><span class="nv">masm32</span><span class="err">\</span><span class="nv">lib</span><span class="err">\</span><span class="nv">winmm.lib</span>
</pre></div>


<p>And then call <code>mciSendString</code> with the command you want to send:</p>

<div class="highlight"><pre><span class="nf">.DATA</span>
<span class="nf">sMciOpenTray</span>  <span class="nv">DB</span> <span class="s">&quot;set cdaudio door open wait&quot;</span><span class="p">,</span> <span class="mi">0</span>
<span class="nf">sMciCloseTray</span> <span class="nv">DB</span> <span class="s">&quot;set cdaudio door closed wait&quot;</span><span class="p">,</span> <span class="mi">0</span>

<span class="nf">.CODE</span>
<span class="c1">; open the CD tray</span>
<span class="nf">INVOKE</span> <span class="nv">mciSendString</span><span class="p">,</span> <span class="nv">ADDR</span> <span class="nv">sMciOpenTray</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span>
<span class="c1">; close the CD tray</span>
<span class="nf">INVOKE</span> <span class="nv">mciSendString</span><span class="p">,</span> <span class="nv">ADDR</span> <span class="nv">sMciCloseTray</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span>
</pre></div>


<p>You can see all the possible commands in the MSDN reference for <a href="http://msdn.microsoft.com/en-us/library/ms709492"><code>mciSendString</code></a>.</p>

<p>Here is an example app written in MASM32: <a href="http://unindented.org/articles/2005/02/opening-and-closing-the-cd-tray/cd-tray.zip">cd-tray.zip</a> (sources included, of course).</p>
</div>
    </content>
    <category term="Windows" />
    <category term="Win32" />
  </entry>
  <entry>
    <id>tag:unindented.org,2005-02-12:1108245323</id>
    <title>Using Windows XP Visual Styles</title>
    <updated>2005-02-12T22:55:23+01:00</updated>
    <link href="http://unindented.org/articles/2005/02/using-windows-xp-visual-styles/" rel="alternate" />
    <summary>How to use the Windows XP visual style in your Win32 applications.</summary>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml"><p>If you want your Win32 app to use the Windows XP visual style, all the information you need is in the MSDN article <a href="http://msdn.microsoft.com/en-us/library/ms997646">Using Windows XP Visual Styles</a>. In case it isn't clear enough, here's what I did for my MASM32 application.</p>

<h3>Create a manifest file</h3>

<p>Create a new text file with the name you want (in my case, <em>manifest.xml</em>) and paste the following XML code:</p>

<div class="highlight"><pre><span class="cp">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;yes&quot; ?&gt;</span>
<span class="nt">&lt;assembly</span> <span class="na">xmlns=</span><span class="s">&quot;urn:schemas-microsoft-com:asm.v1&quot;</span> <span class="na">manifestVersion=</span><span class="s">&quot;1.0&quot;</span><span class="nt">&gt;</span>
  <span class="nt">&lt;assemblyIdentity</span>
    <span class="na">processorArchitecture=</span><span class="s">&quot;X86&quot;</span>
    <span class="na">type=</span><span class="s">&quot;win32&quot;</span>
    <span class="na">name=</span><span class="s">&quot;NameOfYourApp&quot;</span>
    <span class="na">version=</span><span class="s">&quot;1.0.0.0&quot;</span> <span class="nt">/&gt;</span>
  <span class="nt">&lt;description&gt;</span>Description of your app.<span class="nt">&lt;/description&gt;</span>
  <span class="nt">&lt;dependency&gt;</span>
    <span class="nt">&lt;dependentAssembly&gt;</span>
      <span class="nt">&lt;assemblyIdentity</span>
        <span class="na">processorArchitecture=</span><span class="s">&quot;X86&quot;</span>
        <span class="na">type=</span><span class="s">&quot;win32&quot;</span>
        <span class="na">name=</span><span class="s">&quot;Microsoft.Windows.Common-Controls&quot;</span>
        <span class="na">version=</span><span class="s">&quot;6.0.0.0&quot;</span>
        <span class="na">publicKeyToken=</span><span class="s">&quot;6595b64144ccf1df&quot;</span>
        <span class="na">language=</span><span class="s">&quot;*&quot;</span> <span class="nt">/&gt;</span>
    <span class="nt">&lt;/dependentAssembly&gt;</span>
  <span class="nt">&lt;/dependency&gt;</span>
<span class="nt">&lt;/assembly&gt;</span>
</pre></div>


<p>Replace <em>NameOfYourApp</em> and <em>1.0.0.0</em> with the name and version of your app. Also change the description to something more meaningful.</p>

<h3>Include the manifest file</h3>

<p>Just add these lines to your application's resource file:</p>

<div class="highlight"><pre><span class="cp">#define CREATEPROCESS_MANIFEST_RESOURCE_ID 1</span>
<span class="cp">#define RT_MANIFEST 24</span>

<span class="n">CREATEPROCESS_MANIFEST_RESOURCE_ID</span> <span class="n">RT_MANIFEST</span> <span class="n">DISCARDABLE</span> <span class="s">&quot;manifest.xml&quot;</span>
</pre></div>


<h3>Make a few changes</h3>

<p>Include <em>comctl32.lib</em> in your app:</p>

<div class="highlight"><pre><span class="nf">INCLUDE</span> <span class="nv">C</span><span class="p">:</span><span class="err">\</span><span class="nv">masm32</span><span class="err">\</span><span class="nv">include</span><span class="err">\</span><span class="nv">comctl32.inc</span>
<span class="nf">INCLUDELIB</span> <span class="nv">C</span><span class="p">:</span><span class="err">\</span><span class="nv">masm32</span><span class="err">\</span><span class="nv">lib</span><span class="err">\</span><span class="nv">comctl32.lib</span>
</pre></div>


<p>Call <code>InitCommonControls</code> anywhere in the code, just to put a reference to <em>comctl32.dll</em> so that it gets loaded. For example:</p>

<div class="highlight"><pre><span class="nf">INVOKE</span> <span class="nv">GetModuleHandle</span><span class="p">,</span> <span class="nv">NULL</span>
<span class="nf">mov</span> <span class="nv">hInstance</span><span class="p">,</span> <span class="nb">eax</span>
<span class="nf">INVOKE</span> <span class="nb">Dial</span><span class="nv">ogBoxParam</span><span class="p">,</span> <span class="nv">hInstance</span><span class="p">,</span> <span class="nv">IDD_MAINDLG</span><span class="p">,</span> <span class="nv">NULL</span><span class="p">,</span> <span class="nv">ADDR</span> <span class="nb">Dial</span><span class="nv">ogProc</span><span class="p">,</span> <span class="nv">NULL</span>
<span class="nf">INVOKE</span> <span class="nv">ExitProcess</span><span class="p">,</span> <span class="nb">eax</span>

<span class="nf">INVOKE</span> <span class="nv">InitCommonControls</span>
</pre></div>


<p>It'll never get executed, but it doesn't matter.</p>

<h3>The result</h3>

<p><img src="http://unindented.org/articles/2005/02/using-windows-xp-visual-styles/image-01.png" alt="Classic style" /></p>

<p><img src="http://unindented.org/articles/2005/02/using-windows-xp-visual-styles/image-02.png" alt="XP style" /></p>

<p>Quite easy, wasn't it?</p>

<p>You can download my example app to see it for yourself: <a href="http://unindented.org/articles/2005/02/using-windows-xp-visual-styles/xp-stylin.zip">xp-stylin.zip</a> (sources included).</p>
</div>
    </content>
    <category term="Windows" />
    <category term="Win32" />
  </entry>
  <entry>
    <id>tag:unindented.org,2005-02-10:1108072018</id>
    <title>Color in a Console Application</title>
    <updated>2005-02-10T22:46:58+01:00</updated>
    <link href="http://unindented.org/articles/2005/02/color-in-a-console-application/" rel="alternate" />
    <summary>How to print a message in your Windows console application using a color other than the default.</summary>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml"><p>If you want to print a message in your Windows console application using a color other than the default, you just need to call the Win32 API function <a href="http://msdn.microsoft.com/en-us/library/ms686047"><code>SetConsoleTextAttribute</code></a>, specifying the handle of the console and the color.</p>

<p>The color is composed of 2 bytes. The first one is the background color, and the second one the foreground color, so you can get all the combinations by looping from hexadecimal value <code>00</code> (black background and black foreground) to <code>FF</code> (white background and white foreground).</p>

<p>This simple program prints the first 16 color combinations:</p>

<div class="highlight"><pre><span class="cp">#include &lt;stdio.h&gt;</span>
<span class="cp">#include &lt;windows.h&gt;</span>

<span class="kt">int</span> <span class="nf">main</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span>
<span class="p">{</span>
  <span class="n">HANDLE</span> <span class="n">hConsole</span><span class="p">;</span>
  <span class="kt">int</span> <span class="n">k</span><span class="p">;</span>

  <span class="n">hConsole</span> <span class="o">=</span> <span class="n">GetStdHandle</span><span class="p">(</span><span class="n">STD_OUTPUT_HANDLE</span><span class="p">);</span>
  <span class="k">for</span> <span class="p">(</span><span class="n">k</span> <span class="o">=</span> <span class="mh">0x00</span><span class="p">;</span> <span class="n">k</span> <span class="o">&lt;=</span> <span class="mh">0x0F</span><span class="p">;</span> <span class="n">k</span> <span class="o">+=</span> <span class="mh">0x01</span><span class="p">)</span>
  <span class="p">{</span>
    <span class="c1">// change color</span>
    <span class="n">SetConsoleTextAttribute</span><span class="p">(</span><span class="n">hConsole</span><span class="p">,</span> <span class="n">k</span><span class="p">);</span>
    <span class="n">printf</span><span class="p">(</span><span class="s">&quot;%2d Colored text in a Windows console</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span> <span class="n">k</span><span class="p">);</span>
  <span class="p">}</span>
  <span class="c1">// restore to normal color</span>
  <span class="n">SetConsoleTextAttribute</span><span class="p">(</span><span class="n">hConsole</span><span class="p">,</span> <span class="mh">0x07</span><span class="p">);</span>

  <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>


<p><img src="http://unindented.org/articles/2005/02/color-in-a-console-application/image-01.png" alt="Colored text inside a Windows console" /></p>
</div>
    </content>
    <category term="Console" />
    <category term="Windows" />
    <category term="Win32" />
  </entry>

</feed>
