Saturday, October 8, 2011

FAR3 Color Tuner

Although FAR3 haven't been officially released, you already can get some builds here.

For those who doesn't know it's free (BSD-like license) console file manager for Windows, like Total Commander. Besides of the freeness it has a big amount of plugins for almost every business. Earlier all they could be get from the plugring but now it seems that not all authors use it. You can also find plugins on the forum but you probably have to know Russian :).

One of the main (at least for me) feature of the 3.0 version is truecolor support (with ConEmu ONLY. I use "Maximus5" version).

FAR3 Color Tuner program provides quick and easy color control. It can tune standard colors and file highlight colors:

You can load, save, export and import colors.

All colors are being converted to truecolor.

Attached config have the following colors:

Download FAR3 Color Tuner v0.1 with sqlite library (341 KB)

Download FAR3 Color Tuner v0.1 Sources (29 KB)

P.S. The program is written on .NET 4, you can get it here.

UPDATE: FAR3 changed its config format since build 2461. User FAR3 Color Tuner v0.2 with this and later builds.

Download FAR3 Color Tuner v0.2 with sqlite library (343 KB)

Download FAR3 Color Tuner v0.2 Sources (29 KB)

Preventing of Submiting JQuery.UI Dialogs on Return

Look at the usual JQuery.UI dialog example. It works the same way as the official site example - you can press Enter in the fields and nothing will happen. But check out this one. It submits the form (dialog).

You may think that the workaround is to add a hidden input field:

<input type="hidden" />

but it doesn't work. Adding the normal text input hidden by css will help:

<input type="text" style="display: none;" />

Another way is to remove the <form> tag, but it isn't suitable if you have many fields and want use serialize.

Saturday, March 19, 2011

reCAPTCHA Recognition

A few days ago I was required to pass reCAPTCHA for many times. And I noticed that it's being accepted even if it's wrong. So, I decided to examine how accurately it works.

The result was sad :).

While Chad Houck and Jason Lee broke the captcha on DEF CON 18 it has another vulnerability.

I tested percentage of wrong accepting and some of them are shown below.

Distance Accepted? Right / Wrong Image
1 Y gromor prolog
gromor prolok
1 Y dentl anthony
dentl anghony
2 Y any etiation
anq etiotion
2 Y 121 cipansi
125 cipafsi
2 Y bilii three
biliy threee
3 Y meolo scc
meelo scoo
3 Y flowered crocc
flaweted croqc

You can see that accuracy is not very high. Codes with up to three errors were accepted almost all the time. Codes with four errors were accepted very rarely. Also I didn't notice that letter matching matters.

I haven't yet need to break Google's reCAPTCHA but I have already known one of its weakness. You can use it too - don't waste much your attention recognizing the words, like I did before I got this :).

Wednesday, February 16, 2011

GIMP Easy Icon Creation

It's not often I need to make icons but it happens sometimes. Having no favorite tool for creation I tried GIMP. I was surprised how easy it can create icon with several images.

First of all create image with biggest size you need. I chose 64x64. You might also want to create a layer with background to see the result better. I usually use white or dark-blue depending on foreground image lightness.

Then duplicate the layer with icon and select "Scale Layer.." from layer's right-click menu. Resize it to next size you need, ex. 32x32. Note, that unlike in Photoshop you can have layers with different size. By default you'll see current layer's border.

Let's create the last layer 16x16 and colorize layers with different colors. Also delete background before saving icon (leave only layers with icon images).

Now save document as .ico file.

Now you have icon with all the images. On the following screenshot explorer shows different images in file list window (16x16 icon) and preview window (smaller sized 64x64 icon).

Saturday, February 5, 2011

HtmlAgilityPack and XPath Peculiarity

Parsing several HTML pages I noticed that HtmlAgilityPack doesn't consider that its node has relative path for XPath. The following code illustrates this:

var html = @"
<div class="
    <p>header <span>paragraph 1-1</span></p>
    <p>header <span>paragraph 1-2</span></p>
<div class="
    <p>content <span>paragraph 2-1</span></p>
    <p>content <span>paragraph 2-2</span></p>

var doc = new HtmlDocument();

var node = doc.DocumentNode.SelectSingleNode("div[1]/p[1]");

Console.WriteLine("\r\n1st <p> in 1st <div>:");

Console.WriteLine("\r\nCount of <span> (//):");

Console.WriteLine("\r\nCount of <span> (.//):");

It produces the output:

1st <p> in 1st <div>:
<p>header <span>paragraph 1-1</span></p>

Count of <span> (//):

Count of <span> (.//):
1 says that "//" selects nodes "from the current node". So does it mean that HtmlAgilityPack works wrong?

Learning XPath on I had no doubt. But W3C specification says that it's alright:

//para selects all the para descendants of the document root and thus selects all para elements in the same document as the context node
.//para selects the para element descendants of the context node

All I wanna say is that you must be cautious to the information you got, even if it from the popular site with a good reputation (like w3schools is). "Trust no one", like Horde says :).

Wednesday, January 26, 2011

LINQ to SQL and Memory Leaks

This happened when I parsed several gigabytes of HTML pages. I've noticed that program's process ate my memory tragically - over 1 GB in 5-10 minutes.

The first thing I thought was memory leaks in HtmlAgilityPack, that I heard being updated not very often. But removing it was not successful. After several minutes of removing all components I figured out that the reason is LINQ to SQL. My code like the following:

var dcTableOne = new TableOneDataContext(_connectionString);
while (true)
    var row = new TableOne() { Status = 0, Name = stringToSave };

Besides of that I had notice that Entity Framework v4 has memory leaks too. The only method that woks fine was direct SQL executing - via SqlCommand.ExecuteNonQuery or via the same but over LINQ to SQL:

dcTableOne.ExecuteCommand("INSERT INTO TableOne(Status,Name) VALUES({0},{1})", 0, stringToSave);

After minutes of googling the reason was found. LINQ to SQL (and EF) has the feature, that tracks all objects that passed through it. And what I needed to do is turn it off. But it's caused my code to fail on inserts. So, I wend in further searches. They said me that DataContext object was designed for short lifetime and then has to be used in such way:

while (true)
    using (var dc = new TableOneDataContext(_connectionString))
        var row = new TableOne() { Status = 0, Name = stringToSave };

That fixed my problems, but after a moment of working on my project I got similar problem: I required to read big array of data from database, and it couldn't be solved with the method mentioned above by the technical reason - I had to read all data sequentially. Turning tracking off was the right decision:

dcTableOne.ObjectTrackingEnabled = false;
foreach (var elem in dcTableOne.TableOnes)
    // working with elem