diff -r 000000000000 -r ca70ae20a155 src/tools/py2sis/ensymble/decodemif.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/tools/py2sis/ensymble/decodemif.py Tue Feb 16 10:07:05 2010 +0530 @@ -0,0 +1,229 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +############################################################################## +# decodemif.py - Decodes a Symbian OS v9.x multi-image file (MIF) +# Copyright 2006, 2007 Jussi Ylänen +# +# This program is part of Ensymble developer utilities for Symbian OS(TM). +# +# Ensymble is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# Ensymble is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ensymble; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +# +# +# Version history +# --------------- +# +# v0.03 2006-09-22 +# Replaced every possible range(...) with xrange(...) for efficiency +# +# v0.02 2006-08-22 +# Added file type recognition (SVG, binary SVG or other) +# +# v0.01 2006-08-14 +# Added support for strange index entries (length 0) +# +# v0.00 2006-08-13 +# Work started +############################################################################## + +VERSION = "v0.02 2006-09-22" + +import sys +import os +import struct +import getopt +import random +import tempfile + +# Parameters +MAXMIFFILESIZE = 1024 * 1024 # Arbitrary maximum size of MIF file + +tempdir = None +dumpcounter = 0 + +def mkdtemp(template): + ''' + Create a unique temporary directory. + + tempfile.mkdtemp() was introduced in Python v2.3. This is for + backward compatibility. + ''' + + # Cross-platform way to determine a suitable location for temporary files. + systemp = tempfile.gettempdir() + + if not template.endswith("XXXXXX"): + raise ValueError("invalid template for mkdtemp(): %s" % template) + + for n in xrange(10000): + randchars = [] + for m in xrange(6): + randchars.append(random.choice("abcdefghijklmnopqrstuvwxyz")) + + tempdir = os.path.join(systemp, template[: -6]) + "".join(randchars) + + try: + os.mkdir(tempdir, 0700) + return tempdir + except OSError: + pass + +def dumpdata(data): + '''Dumps data to a file in a temporary directory.''' + + global tempdir, dumpcounter + + if tempdir == None: + # Create temporary directory for dumped files. + tempdir = mkdtemp("decodemif-XXXXXX") + dumpcounter = 0 + + # Determine file type. + if data[0:5] == " len(mifdata): + raise ValueError("%s: index %d out of range" % + (miffilename, n)) + + index.append((a, b)) + + n = len(index) + print "%s: %s %s inside" % (miffilename, n or "no", + ((n == 1) and "file") or "files") + + # Extract contents. + for i in index: + offset = i[0] + length = i[1] + if mifdata[offset:offset + 4] != "C##4": + raise ValueError("%s: invalid file header %d" % + (miffilename, n)) + + print "0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x" % ( + tuple(struct.unpack("