Friday, March 28, 2008

How to hack iphone locales

You may already seen out there some projects that translates the iPhone user interface to many different languages. Locale strings are stored in a totally different way though. Things like "Monday" and the Portuguese equivalent "Segunda-Feira" are stored in a ICU (International Components for Unicode) data file.

For 12 mondays I had to stand looking at "Segunda-Feira" being printed out of the bounds of iCal icon. Until today. I tried fgrep'ng the whole iPhone for the string "segunda" with no luck. Then I tried converting all the plists from binaries to XML and fgrep'ng again and still no luck. Doing some research I found that apple uses ICU on iPhone, which drove me to find our target: /usr/share/icu/icudt34l.dat. This is a 8.5MB file bundled with all the locales (timezones, region formats, etc.).

To start playing with it, the first thing we will need is obviously the file itself:
cd /tmp
ssh root@iPhone cp /usr/share/icu/icudt34l.dat /usr/share/icu/icudt34l.dat.bak
scp root@iPhone:/usr/share/icu/icudt34l.dat .
Now we need the ICU version 3.4 tools. Again, I don't like macports or fink so I had a bad time trying to compile it myself (remember I am on a Mac OS X Leopard). You have now the cake recipe:
cd /usr/src
wget ftp://ftp.software.ibm.com/software/globalization/icu/3.4.1/icu-3.4.1.tgz
tar -zxf icu-3.4.1.tgz
mv icu icu-3.4.1
cd icu-3.4.1/source
For some weird reason the configure script for darwin is broken, so I had to fix that with the following patch:
--- config/mh-darwin 2004-05-18 18:54:24.000000000 -0300
+++ config/mh-darwin.new 2008-03-27 23:48:45.000000000 -0300
@@ -58,6 +58,7 @@
@echo "generating dependency information for $<" @$(GEN_DEPS.c) $< > /dev/null
@mv $@ $@~
- @echo -n "$@ " > $@
@cat < $@~ >> $@
@-rm $@~

@@ -65,6 +66,7 @@
@echo "generating dependency information for $<" @$(GEN_DEPS.cc) $< >/dev/null
@mv $@ $@~
- @echo -n "$@ " > $@
@cat < $@~ >> $@
@-rm $@~
Now we can run configure:
./runConfigureICU MacOSX --disable-samples --disable-draft --disable-extras --disable-tracing --disable-tests
and make it:
make
sudo make install
I couldn't manage to get file from bin/ installed on my system, but anyway I wasn't really interested on going deeply on that. Heck, I want to get rid of that "Segunda-Feira" thing !

Lets extract our package now:
cd /tmp
/usr/src/icu-3.4.1/source/bin/decmn icudt34l.dat --pkgdata > out.lst
cd icudt34l
boom ! we got a lot of files... and look who is laying around:
-rw-r--r-- 1 igor wheel 51K Mar 28 00:14 pt.res
-rw-r--r-- 1 igor wheel 496B Mar 27 23:06 pt_BR.res
-rw-r--r-- 1 igor wheel 3.7K Mar 27 23:06 pt_PT.res
The file we want is pt.res, but its a binary file. I couldn't manage to convert it back to something readable but again I am more interested in the quickest solution possible. Just download the pt.txt here. Near to the end you will find the calendar day names. Edit accordingly. Important: remember to use UTF-8.

Now its time to generate our new resource:
/usr/src/icu-3.4.1/source/bin/genrb pt.txt
and re-package:
/usr/src/icu-3.4.1/source/bin/gencmn -v -n icudt34l 0 < ../out.lst
At this point you cross your fingers and hope this works... worked for me :)
scp icudt34l.dat root@iPhone:/usr/share/icu
reboot and you are all set.

If you can read Portuguese you might find this entry interesting as well. It describes the similar process of updating Mac OS X timezone. It makes use of `icupkg` though, that is not available on ICU version 3.4.

Wednesday, March 26, 2008

GTK Python bindings for Mac OS X 10.5 Leopard

Following my previous post we are now ready to compile and install the python bindings in order to have UMIT ready to go.

we start with pygobject which is cursed by Murphy's law. At the first attempt I got make stopped by an error message, that could be fixed by telling which python version we would like to use (one more time thanks to macports).
wget http://ftp.gnome.org/pub/GNOME/sources/pygobject/2.14/pygobject-2.14.1.tar.bz2
tar -jxf pygobject-2.14.1.tar.bz2
cd pygobject-2.14.1
./configure --prefix=/usr/local --disable-glibtest CFLAGS="-arch i686" PYTHON=/usr/bin/python2.5
make
sudo make install
next pycairo:
wget http://cairographics.org/releases/pycairo-1.4.12.tar.gz
tar -zxf pycairo-1.4.12.tar.gz
./configure --prefix=/usr/local CFLAGS="-arch i686" PYTHON=/usr/bin/python2.5
make
sudo make install
and finally pygtk:
wget http://ftp.gnome.org/pub/GNOME/sources/pygtk/2.12/pygtk-2.12.1.tar.bz2
tar -jxf pygtk-2.12.1.tar.bz2
cd pygtk-2.12.1
./configure --prefix=/usr/local CFLAGS="-arch i686" PYTHON=/usr/bin/python2.5
make
sudo make install
that's the end of our saga. You can now enjoy UMIT on your Mac OS :)

Thursday, March 6, 2008

Compiling GTK+ on Mac OS X 10.5 Leopard

UMIT is an awesome graphical frontend for nmap. It works out of the box, that is, unzip and go. But, it requires GTK+2, which is a totally different history...

Compiling GTK+ on Mac OS X can be a time consuming task, specially if you don't want to use tools like Fink or MacPorts. Obviously I don't keep backup of my libs so after my last HDD crash (less than a year brand new MacBook, thanks to MacSaber) I had to redo all the GTK saga again. This time I am going to record the whole process to the history, so I can drastically reduce the efforts on future attempts.

Yes, GTK has a million dependencies that have a million dependencies themselves leaving us with a gazillion libraries to take care of. While I was going forward and back across the dependencies I wrote down what depends on what. That's what I came up with:
  • gtk
    • glib
      • pkg_config
      • gettext
        • expat
      • libpng
    • atk
    • pango
      • cairo
        • pixman
        • freetype
        • fontconfig
    • libtiff
      • libjpeg
Easy, huh ? No its not what it looks like. It took me a whole day and a half to fix every little issue that kept me from going ahead during the process. For each library, there is a special trick you have to learn.

For the architecture I decided to go with i686 after gettext complained about x86_64. Yes, I had to go back and re-compile everything that was already done until that step.

I like to install everything on /usr/local so I know where my stuff is laying around. Also, I tried to find the latest version of each package in the time this blog entry was written. Some stuff is hosted on more than one "official" place and not always it is updated everywhere. So I could fail on finding the state of the art for everything here but I am probably close to that.

I am not going to report any error message or to detail any problem that I faced during the process. Instead I will go direct to the point.

GLIB

we start with pkg_config:
wget http://pkgconfig.freedesktop.org/releases/pkg-config-0.23.tar.gz
tar -zxf pkg-config-0.23.tar.gz
cd pkg-config-0.23
./configure --prefix=/usr/local CFLAGS="-arch i686"
make
sudo make install
then we move to gettext, that requires expat:
wget http://ufpr.dl.sourceforge.net/sourceforge/expat/expat-2.0.1.tar.gz
tar -zxf expat-2.0.1.tar.gz
cd expat-2.0.1
./configure --prefix=/usr/local CFLAGS="-arch i686"
make
sudo make install
before going to gettext we need to hack emacs as found here:
sudo mv /usr/bin/emacs-i386 /usr/bin/emacs-i386.backup
sudo /usr/libexec/dumpemacs -d
finally gettext:
wget ftp://ftp.unicamp.br/pub/gnu/gettext/gettext-0.17.tar.gz
tar -zxf gettext-0.17.tar.gz
cd gettext-0.17
./configure --prefix=/usr/local CFLAGS="-arch i686"
make
sudo make install
and the last item on glib, libpng:
wget http://ufpr.dl.sourceforge.net/sourceforge/libpng/libpng-1.2.25.tar.bz2
tar -jxf libpng-1.2.25.tar.bz2
cd libpng-1.2.25
./configure --prefix=/usr/local CFLAGS="-arch i686"
make
sudo make install
now glib itself, that is by far more problematic than the libs above:
wget http://ftp.gnome.org/pub/gnome/sources/glib/2.14/glib-2.14.6.tar.bz2
tar -jxf glib-2.14.6.tar.bz2
cd glib-2.14.6
at this point we have to patch glib/gutils.h as seen here:
--- gutils.h 2008-02-07 03:24:59.000000000 -0200
+++ /usr/src/glib-2.14.6/glib/gutils.h 2008-03-06 16:43:34.000000000 -0300
@@ -108,6 +108,8 @@
#ifdef G_IMPLEMENT_INLINES
# define G_INLINE_FUNC
# undef G_CAN_INLINE
+#elif defined (__APPLE__)
+# define G_INLINE_FUNC static inline
#elif defined (__GNUC__)
# if __GNUC_PREREQ (4,2) && defined (__STDC_VERSION__) \
&& __STDC_VERSION__ >= 199901L
also the C compiler need some special flags. the SSE instructions have to be enabled with -msse2 which is mandatory here. I copied the other flags from the portfile at MacPorts. Finally:
./configure --prefix=/usr/local --enable-static CFLAGS="-arch i686 -msse2 -funroll-loops -fstrict-aliasing -finline-functions" LDFLAGS="-bind_at_load"
make
sudo make install

GTK

the next is atk:
wget http://ftp.gnome.org/pub/gnome/sources/atk/1.21/atk-1.21.92.tar.bz2
tar -jxf atk-1.21.92.tar.bz2
cd atk-1.21.92
./configure --prefix=/usr/local CFLAGS="-arch i686"
make
sudo make install
and following cairo deps, pixman:
wget http://cairographics.org/releases/pixman-0.9.6.tar.gz
tar -zxf pixman-0.9.6.tar.gz
cd pixman-0.9.6
./configure --prefix=/usr/local CFLAGS="-arch i686"
make
sudo make install
freetype:
wget http://download.savannah.gnu.org/releases/freetype/freetype-2.3.5.tar.bz2
tar -jxf freetype-2.3.5.tar.bz2
cd freetype-2.3.5
./configure --prefix=/usr/local CFLAGS="-arch i686 -msse2"
make
sudo make install
fontconfig:
wget http://fontconfig.org/release/fontconfig-2.4.92.tar.gz
tar -zxf fontconfig-2.4.92.tar.gz
cd fontconfig-2.4.92
./configure --prefix=/usr/local CFLAGS="-arch i686"
make
sudo make install
and finally cairo. note we need again the SSE instructions plus we have to specify the location of the X development libraries. If you don't have it, refer to apple support on how to install Xcode tools and X11.
wget http://cairographics.org/releases/cairo-1.4.14.tar.gz
tar -zxf cairo-1.4.14.tar.gz
cd cairo-1.4.14
./configure --prefix=/usr/local --enable-xlib --x-includes=/usr/X11/include --x-libraries=/usr/X11/lib CFLAGS="-arch i686 -msse2"
make
sudo make install
now that we have cairo done, we can do pango:
wget http://ftp.gnome.org/pub/GNOME/sources/pango/1.19/pango-1.19.4.tar.bz2
tar -jxf pango-1.19.4.tar.bz2
cd pango-1.19.4
./configure --prefix=/usr/local CFLAGS="-arch i686 -msse2"
make
sudo make install
libjpeg has a special rule hidden inside the makefile, that you don't want to miss:
wget http://www.ijg.org/files/jpegsrc.v6b.tar.gz
tar -zxf jpegsrc.v6b.tar.gz
cd jpeg-6b
./configure --prefix=/usr/local CFLAGS="-arch i686"
make
sudo make install
sudo make install-lib
and the last one, libtiff:
wget ftp://ftp.remotesensing.org/pub/libtiff/tiff-3.8.2.tar.gz
tar -zxf tiff-3.8.2.tar.gz
cd tiff-3.8.2
./configure --prefix=/usr/local --with-apple-opengl-framework CFLAGS="-arch i686"
make
sudo make install
we are now all ready to the big guy:
wget http://ftp.gnome.org/pub/gnome/sources/gtk+/2.12/gtk+-2.12.8.tar.bz2
tar -jxf gtk+-2.12.8.tar.bz2
cd gtk+-2.12.8
./configure --prefix=/usr/local --x-includes=/usr/X11/include --x-libraries=/usr/X11/lib CFLAGS="-arch i686 -msse2"
make
sudo make install
and voilá ! it took me many hours to get here, I hope you can do it in minutes ;)

We are not ready for UMIT yet. We need the python bindings still. see you in the next adventure.

update: thanks to Gregory and Antoine for pointing out the typos