Archive for the ‘Development’ Category

iPhone icons for your site

April 27th, 2008 by Sjan Evardsson

Adding a shortcut to a favorite site on an iPhone (or iPod Touch) is as easy as tapping the “+” sign at the bottom of the Safari browser on the phone and selecting “Add to Homepage” - but the icon is not so appealing. In fact, the iPhone defaults to a tiny, cropped screenshot of the site unless it finds a 57×57 pixel png file in the site root. This is similar to the concept of the favicon.ico for web browsers. The file needs to be named apple-touch-icon.png.

The iPhone (or iPod Touch) will round the corners and overlay the glass look to make it blend in to the overall look and feel of the “Springboard” (their name for the desktop on these devices.)

Mine looks like this (before the modifications which take place on the device itself):

And this is what it looks like on the iPhone:

Problems arising from PHP type casting in ==

March 8th, 2008 by Sjan Evardsson

While trying to work through the issues I mentioned in the last post I started doing some serious digging and testing. Here is what I have found.

PHP seems to use == for determining equivalance when performing array_search, in_array and switch, while using either === or strcmp when doing array_key_exists.

The result of this is that array_search and in_array will return improper results when used on an array with mixed string and integer values. (Another thing I found, that may or may not be related, is that array keys will be cast from strings to integers when those strings are valid integer values.)

array_search() with mixed types

  1. $one = array (
  2.   ‘abc’,
  3.   ‘abc1′,
  4.   111,
  5.   ‘111b’,
  6.   2,
  7.   ‘2xyz’,
  8.   ‘123a’,
  9.   123
  10. );
  11. $two = $one;
  12. for ($i = 0; $i < count($one); $i++) {
  13.   $xkey = array_search($one[$i], $two);
  14.   if(strcmp(strval($one[$i]), strval($two[$xkey])) != 0) {
  15.     // This should NEVER be reached, but it is, often!
  16.     $eq = ‘FALSE’;
  17.   } else {
  18.     $eq = ‘true’;
  19.   }
  20. }
Row $one $two Correct? Found Notes
0 abc abc true 0 abc == abc : array_search($one[0], $two) where $one[0] = string(3) “abc”
1 abc1 abc1 true 1 abc1 == abc1 : array_search($one[1], $two) where $one[1] = string(4) “abc1″
2 111 111 true 2 111 == 111 : array_search($one[2], $two) where $one[2] = int(111)
3 111b 111b FALSE 2 111b == 111 : array_search($one[3], $two) where $one[3] = string(4) “111b”
4 2 2 true 4 2 == 2 : array_search($one[4], $two) where $one[4] = int(2)
5 2xyz 2xyz FALSE 4 2xyz == 2 : array_search($one[5], $two) where $one[5] = string(4) “2xyz”
6 123a 123a true 6 123a == 123a : array_search($one[6], $two) where $one[6] = string(4) “123a”
7 123 123 FALSE 6 123 == 123a : array_search($one[7], $two) where $one[7] = int(123)

array_search() with all strings

  1. $one = array (
  2.   ‘abc’,
  3.   ‘abc1′,
  4.   ‘111′,
  5.   ‘111b’,
  6.   ‘2′,
  7.   ‘2xyz’,
  8.   ‘123a’,
  9.   ‘123′
  10. );
  11. $two = $one;
  12. for ($i = 0; $i < count($one); $i++) {
  13.   $xkey = array_search($one[$i], $two);
  14.   if(strcmp(strval($one[$i]), strval($two[$xkey])) != 0) {
  15.     // This should NEVER be reached, and with all strings it isn’t.
  16.     $eq = ‘FALSE’;
  17.   } else {
  18.     $eq = ‘true’;
  19.   }
  20. }
Row $one $two Correct? Found Notes
0 abc abc true 0 abc == abc : array_search($one[0], $two) where $one[0] = string(3) “abc”
1 abc1 abc1 true 1 abc1 == abc1 : array_search($one[1], $two) where $one[1] = string(4) “abc1″
2 111 111 true 2 111 == 111 : array_search($one[2], $two) where $one[2] = string(3) “111″
3 111b 111b true 3 111b == 111b : array_search($one[3], $two) where $one[3] = string(4) “111b”
4 2 2 true 4 2 == 2 : array_search($one[4], $two) where $one[4] = string(1) “2″
5 2xyz 2xyz true 5 2xyz == 2xyz : array_search($one[5], $two) where $one[5] = string(4) “2xyz”
6 123a 123a true 6 123a == 123a : array_search($one[6], $two) where $one[6] = string(4) “123a”
7 123 123 true 7 123 == 123 : array_search($one[7], $two) where $one[7] = string(3) “123″

in_array() and array_key_exists()

  1. $array = array(‘111′=>‘111′, ‘11b’=>‘11b’, ‘222b’=>‘222b’,‘2×22′=>‘2×22′);
  2. $keys = array_keys($array);
  3. $searches = array(‘111b’,222,11,‘222b’,2);
  4. foreach ($searches as $search) {
  5.   $ia = (in_array($search, $array))?‘true’:‘false’;
  6.   $ake = (array_key_exists($search, $array))?‘true’:‘false’;
  7.   if ($search === ‘222b’) {
  8.     // This is the only place where either should return true
  9.     $iaf = ($ia == ‘true’)?" class=\"$true\"":" class=\"$false\"";
  10.     $akef = ($ake == ‘true’)?" class=\"$true\"":" class=\"$false\"";
  11.     $notes = "** Both should be true **";
  12.   } else {
  13.     $iaf = ($ia == ‘false’)?" class=\"$true\"":" class=\"$false\"";
  14.     $akef = ($ake == ‘false’)?" class=\"$true\"":" class=\"$false\"";
  15.     $notes = "Both should be false";
  16.   }
  17. }

Notice how the array keys are cast to type int in both the original array and in array_keys.

$array $keys
array(4) {
  [111]=>
  string(3) "111"
  ["11b"]=>
  string(3) "11b"
  ["222b"]=>
  string(4) "222b"
  ["2x22"]=>
  string(4) "2x22"
}
array(4) {
  [0]=>
  int(111)
  [1]=>
  string(3) "11b"
  [2]=>
  string(4) "222b"
  [3]=>
  string(4) "2x22"
}
Search Item in_array array_key_exists Notes
111b false false Both should be false
222 true false Both should be false
11 true false Both should be false
222b true true ** Both should be true **
2 true false Both should be false

So, it appears that array_key_exists() uses either or === strcmp() while in_array() uses ==

NOTE: Calling array_key_exists() with a string value ‘111′ will return true for an item with a key of int 111. This is not the same behavior as ===, but is the same behavior as strcmp() which must be what is used internally for array_key_exists().

The difference between the 3 operations is clear:

$a $b $a == $b $a === $b strcmp(strval($a),strval($b))
int(123) string(4) “123b” bool(true) bool(false) int(-1)

Another area where this becomes an issue is in switch statements. Take the following, for example:

switch()

  1. $array = array(111,‘222b’);
  2. foreach($array as $val)
  3. {
  4.   $row = ($row == ‘row’)?‘offset_row’:‘row’;
  5.   $false = ($false == ‘false’)?‘offset_false’:‘false’;
  6.   $true = ($true == ‘true’)?‘offset_true’:‘true’;
  7.   switch($val)
  8.   {
  9.     case ‘111b’: // this displays
  10.       $match = ‘111b’;
  11.       $f = " class=\"$false\"";
  12.       $notes = "Incorrect: should have fallen through to next case";
  13.       break;
  14.     case 111: // never makes it here even tho this is correct
  15.       $match = 111;
  16.       $f = " class=\"$true\"";
  17.       $notes = "** Correct **";
  18.       break;
  19.     case 222: // this displays
  20.       $match = 222;
  21.       $f = " class=\"$false\"";
  22.       $notes = "Incorrect: should have fallen through to next case";
  23.       break;
  24.     case ‘222b’: // never makes it here even tho this is correct
  25.       $match = ‘222b’;
  26.       $f = " class=\"$true\"";
  27.       $notes = "** Correct **";
  28.       break;
  29.     default:
  30.       $match = ‘no match’;
  31.       $f = " class=\"$false\"";
  32.       $notes = "Incorrect: should have matched";
  33.       break;
  34.   }
  35. }
Search Item Match Notes
111 111b Incorrect: should have fallen through to next case
222b 222 Incorrect: should have fallen through to next case

PHP array_search implicit cast of search term

March 8th, 2008 by Sjan Evardsson

There is an error in the values that array_search returns when searching on an array that has a mix of numeric values (123) and alpha-numeric mixed strings that start with the same and follow with alpha characters (’123a’).

The results are actually kind of bizarre, but explainable by a bug in PHP’s equivalence test. When testing for equivalence (using ==) PHP determines that 123 == ‘123xyz’. PHP casts the string to an integer when doing the comparison (so ‘123xyz’ becomes 123). This is documented in bugs.php.net (http://bugs.php.net/bug.php?id=23110) - but this leads to problems: both switch and array_search use == for comparison.

So, using:

  1. $one = array (
  2.   ‘abc’,
  3.   ‘abc1′,
  4.   111,
  5.   ‘111b’,
  6.   2,
  7.   ‘2xyz’,
  8.   ‘123a’,
  9.   123
  10. );
  11. $two = $one;
  12.  
  13. foreach($one as $val)
  14. {
  15.   $key = array_search($val, $two);
  16.   if ($key !== false) {
  17.     echo "$val == {$two[$key]} \n";
  18.     if (strcmp(strval($val), strval($two[$key])) == 0) {
  19.       echo "strcmp returns true";
  20.     } else {
  21.       echo "strcmp returns false";
  22.     }
  23.   } else {
  24.     echo "$val not found \n";
  25.   }
  26. }

results in:

abc == abc -- strcmp returns true
abc1 == abc1 -- strcmp returns true
111 == 111 -- strcmp returns true
111b == 111 -- strcmp returns false
2 == 2 -- strcmp returns true
2xyz == 2 -- strcmp returns false
123a == 123a -- strcmp returns true
123 == 123a -- strcmp returns false

This becomes a real problem when you can’t be sure that the values in an array are all of the same type. However, if you are sure that all the values in the array are of type string then array_search works flawlessly.

I am still unsure how to work around this, however, I think having a version of array_search that doesn’t do an implicit cast on the search value would be of great use.

New Class of Exploits: Dangling Pointers

July 23rd, 2007 by Sjan Evardsson

While dangling pointers are a common coding error (especially in C++) there has previously been no way known to exploit them. In fact, they were generally considered a quality control issue rather than a security issue. That is all set to change. According to an article today from SearchSecurity Jonathan Afek and Adi Sharabani of Watchfire Inc have uncovered a way to exploit generic dangling pointers to run shell code on a server in much the same fashion as buffer overflows. According to Danny Allen (also of Watchfire) this technique can be used on any application with dangling pointers.

Afek will be giving a presentation on the technique in August at the Black Hat Briefings in Las Vegas.

Technorati Tags: ,

One to watch?

May 1st, 2007 by Sjan Evardsson

Sun is proposing an alternative to AJAX, called Project Flair, which is set for early release later this year. In an InfoWorld article, Sun engineer and principal investor Dan Ingalls describes it as being more like the old style of of desktop application programming (using a JavaScript programming kernel) that adds collaboration and web access.

How this actually ends up performing is anyone’s guess, but I’ll be keeping an eye out for it.

Technorati Tags: , ,

New(?) sorting algorithm

February 25th, 2007 by Sjan Evardsson

George Papadopoulos has released BitFast - a linked list sorting algorithm with examples written in C and C++. He claims sort performance 10 times faster than the MergeSort Algorithm. (But where is the Big O notation?)

You can see the project site, which has a download link for the C and C++ source code. The explanation is fairly clear, although it seems a little sparse to me. The source code lacks in comments, and has been written as proof of concept only, but it will provide the experienced C or C++ developer with a better understanding of what he is doing.

Mostly, it looks like he is applying the Radix Sort Algorithm to linked lists of integers or floats. This does nothing for strings, and the proof-of-concept code is only set up to handle 32 bit number values only. Perhaps the only difference between Radix and BitFast, is that Papadopoulos claims that BitFast is an in-place algorithm and an online algorithm.

Technorati Tags: , ,

Rosetta Code: A Call for Editors

January 25th, 2007 by Sjan Evardsson

Rosetta Code has put out a call for editors. The idea is ingenious in its simplicity: a Wiki that shows how to do the same tasks in a wide variety of languages. For anyone who learns best by example this is a no-brainer.

Stop by, create an account and help out!

Technorati Tags:

Music from chaos

July 15th, 2006 by Sjan Evardsson

A conversation with a coworker the other day got me thinking about ways to make our “noodling” a reality. We were trying to come up with a way to generate random music (defined by octave, step, and duration), while maintaining harmonic relevance. (In other words, we want to create music, not noise.)

We decided we should start with the following propositions:

  • limiting the tones to a pentatonic scale
  • limiting the octave range to that audible to humans
  • limiting the duration of tones to a maximum of one or two whole notes

Beyond that, the discussion turned to how to generate the random values. Since I have been reading about the history of Nonlinear Dynamics (chaos) lately, my first thought was, of course, to generate the values for octave, step and duration by using a set of non-linear equations. So, of course, the first choice would have to be the Lorenz “butterfly” equations .

So, the plan is to calculate the values, convert them to MIDI values and write them out to a file using Python. Since the outcome of the equations relies on the starting conditions (Sensitive Dependence on Initial Conditions - also called the “Butterfly Effect” ) we thought that we could use starting values such as the current Temperature, Barometric Pressure and Humidity, or perhaps Date, Time and processs number, or counting Buicks, Chevys and Fords in the parking lot. Pretty much any 3 starting numbers.

Smart Testing

May 30th, 2006 by Sjan Evardsson

Scott Sehlhorst ()has written a concise article on how to do smart software testing. While I have worked with non-technical people who wanted “full-coverage” testing of all builds, and have also worked with non-technical people who wanted to skip testing and just go live and “fix it in the field,” I have not previously had the numbers to say “yes, this has been tested and we are 99% confident that the application is 99% bug free.” Without having the numbers and formulae at hand the best I could previously say was “I am pretty confident that it is mostly bug-free.” Well, I know that the 99/99 numbers sound a lot more confident than that, and I am less likely to spend a week running tests to get as close to full coverage as possible now that I have this bit in my tool-belt.

When to upgrade?

March 27th, 2006 by Sjan Evardsson

I have a minor (or possibly major) problem with my favorite Java IDE, . It seems that even though version 5.5 Q-build has been promoted to Build 1, I still have issues with using version 4.

I was much more comfortable with the workings of version 3 and earlier, where setting up projects and working within projects seemed much simpler. I still find myself frustrated with version 4 and the need to explicitly import the libraries I routinely use. With version 3 and earlier I imported those libraries generically for all projects. I still haven’t figured out how to do that in version 4 or 5. So now that the world is moving on what am I to do? I guess I will just have to upgrade and bite the bullet.