Friday, June 9, 2017

WCF Exception type

Framework 4.0

As you know, when you throw an exception in a WCF endpoint, it is always returned to the calling code as a System.Exception. I would like to have the calling code understand whether the exception is deliberately thrown by my code (ie the account doesn't have enough funds to cover the purchase) or is thrown by something external to my logic (ie the sql command timed out).

I would normally use the exception type to do this, perhaps my making all the deliberate exceptions ApplicationExceptions. Unfortunately the exception I throw always gets converted to a System.Exception by the time it reaches the calling code. It's great that an exception thrown in the WCF endpoint causes an exception to be thrown in the calling code, but it would be even better if the exception type were the same.

Although I have not found a way to do exactly this, I have found a simple way to discover the type of the original exception.

If you look at the exception returned you can see a [Detail] property which has a [Type] property that is a string containing the full type of the original exception ie "System.ApplicationException". However you will soon find that both [Detail] and [Type] are not accessible. 

Here is a line of code that gets the original exception type anyway.

Dim exBase As String = ex.GetType().GetProperty("Detail").GetValue(ex, Nothing).Type

Tuesday, June 6, 2017

Getting Manifest info from an apk

I am beta-testing my BrevetRider app so I put a button on my BrevetManager.com website to allow beta-testers to side-load the apk before I deploy it to GooglePlay. I've figured out the whole Archive/Sign/Deploy process but I wanted to display the apk size, date, and version. I don't want to have to remember to update the version on the button every time I update the version on the apk so I wanted a way to get the version number out of the apk. It turned out to be a LOT more difficult than I expected.

The apk is a zip file. You have to unzip it, find AndroidManifest.xml, which is itself compressed in a proprietary format. It looks more like the metadata section of a jpg file than XML. There are several items of information that can be recovered so I return them all in a dictionary.

I got most of this code from https://stackoverflow.com/questions/2097813/how-to-parse-the-androidmanifest-xml-file-inside-an-apk-package.

Start a new console project in Visual Studio 2017 and call it GetApkInfoHost. Add a new class called GetApkInfo.



We will start by adding the SharpZipLib package so we can unzip the apk file. Browse to Tools -> Nuget Package Manager -> Manage NuGet Packages for Solution...

Then browse to SharpZipLib and install it.


Now we are ready to write GetApkInfo as a static class with a single public method that takes an apk path and returns a dictionary of property names and values. Single value properties will be returned as a string and multi-value properties will be returned as a list of strings.

Here is the code for the GetApkInfo class. It is not even close to being self-explanatory so it's fortunate it is well commented. Basically we unzip the apk and look for the AndroidManifest.xml file. When we find it we pass it to decompressXML which parses the binary format and pulls out some information which we put into a dictionary. If we find the same key more than once we convert the string value into a List<String> and keep appending to it.

using System;
using System.Collections.Generic;
using System.IO;

namespace GetApkVersion
{
    static class GetApkInfo
    {
        public static Dictionary<string, object> GetInfo(string ApkPath)
        {
            ICSharpCode.SharpZipLib.Zip.ZipInputStream zip = new ICSharpCode.SharpZipLib.Zip.ZipInputStream(File.OpenRead(ApkPath));
            var filestream = new FileStream(ApkPath, FileMode.Open, FileAccess.Read);
            ICSharpCode.SharpZipLib.Zip.ZipFile zipfile = new ICSharpCode.SharpZipLib.Zip.ZipFile(filestream);
            ICSharpCode.SharpZipLib.Zip.ZipEntry item;

            while ((item = zip.GetNextEntry()) != null)
            {
                if (item.Name == "AndroidManifest.xml")
                {
                    byte[] bytes = new byte[50 * 1024];
                    Stream strm = zipfile.GetInputStream(item);
                    int size = strm.Read(bytes, 0, bytes.Length);
                    return decompressXML(bytes);
                }
            }

            return null;
        }

        private static int endDocTag = 0x00100101;
        private static int startTag = 0x00100102;
        private static int endTag = 0x00100103;
        private static Dictionary<String, object> decompressXML(byte[] xml)
        {
            Dictionary<string, object> d = new Dictionary<string, object>();
            // Compressed XML file/bytes starts with 24x bytes of data,
            // 9 32 bit words in little endian order (LSB first):
            //   0th word is 03 00 08 00
            //   3rd word SEEMS TO BE:  Offset at then of StringTable
            //   4th word is: Number of strings in string table
            // WARNING: Sometime I indiscriminently display or refer to word in 
            //   little endian storage format, or in integer format (ie MSB first).
            int numbStrings = LEW(xml, 4 * 4);

            // StringIndexTable starts at offset 24x, an array of 32 bit LE offsets
            // of the length/string data in the StringTable.
            int sitOff = 0x24;  // Offset of start of StringIndexTable

            // StringTable, each string is represented with a 16 bit little endian 
            // character count, followed by that number of 16 bit (LE) (Unicode) chars.
            int stOff = sitOff + numbStrings * 4;  // StringTable follows StrIndexTable

            // XMLTags, The XML tag tree starts after some unknown content after the
            // StringTable.  There is some unknown data after the StringTable, scan
            // forward from this point to the flag for the start of an XML start tag.
            int xmlTagOff = LEW(xml, 3 * 4);  // Start from the offset in the 3rd word.
                                              // Scan forward until we find the bytes: 0x02011000(x00100102 in normal int)
            for (int ii = xmlTagOff; ii < xml.Length - 4; ii += 4)
            {
                if (LEW(xml, ii) == startTag)
                {
                    xmlTagOff = ii; break;
                }
            } // end of hack, scanning for start of first start tag

            // XML tags and attributes:
            // Every XML start and end tag consists of 6 32 bit words:
            //   0th word: 02011000 for startTag and 03011000 for endTag 
            //   1st word: a flag?, like 38000000
            //   2nd word: Line of where this tag appeared in the original source file
            //   3rd word: FFFFFFFF ??
            //   4th word: StringIndex of NameSpace name, or FFFFFFFF for default NS
            //   5th word: StringIndex of Element Name
            //   (Note: 01011000 in 0th word means end of XML document, endDocTag)

            // Start tags (not end tags) contain 3 more words:
            //   6th word: 14001400 meaning?? 
            //   7th word: Number of Attributes that follow this tag(follow word 8th)
            //   8th word: 00000000 meaning??

            // Attributes consist of 5 words: 
            //   0th word: StringIndex of Attribute Name's Namespace, or FFFFFFFF
            //   1st word: StringIndex of Attribute Name
            //   2nd word: StringIndex of Attribute Value, or FFFFFFF if ResourceId used
            //   3rd word: Flags?
            //   4th word: str ind of attr value again, or ResourceId of value

            // TMP, dump string table to tr for debugging
            //tr.addSelect("strings", null);
            //for (int ii=0; ii<numbStrings; ii++) {
            //  // Length of string starts at StringTable plus offset in StrIndTable
            //  String str = compXmlString(xml, sitOff, stOff, ii);
            //  tr.add(String.valueOf(ii), str);
            //}
            //tr.parent();

            // Step through the XML tree element tags and attributes
            int off = xmlTagOff;
            int indent = 0;
            int startTagLineNo = -2;
            while (off < xml.Length)
            {
                int tag0 = LEW(xml, off);
                //int tag1 = LEW(xml, off+1*4);
                int lineNo = LEW(xml, off + 2 * 4);
                //int tag3 = LEW(xml, off+3*4);
                int nameNsSi = LEW(xml, off + 4 * 4);
                int nameSi = LEW(xml, off + 5 * 4);

                if (tag0 == startTag)
                { // XML START TAG
                    int tag6 = LEW(xml, off + 6 * 4);  // Expected to be 14001400
                    int numbAttrs = LEW(xml, off + 7 * 4);  // Number of Attributes to follow
                                                            //int tag8 = LEW(xml, off+8*4);  // Expected to be 00000000
                    off += 9 * 4;  // Skip over 6+3 words of startTag data
                    String name = compXmlString(xml, sitOff, stOff, nameSi);
                    //tr.addSelect(name, null);
                    startTagLineNo = lineNo;

                    // Look for the Attributes

                    for (int ii = 0; ii < numbAttrs; ii++)
                    {
                        int attrNameNsSi = LEW(xml, off);  // AttrName Namespace Str Ind, or FFFFFFFF
                        int attrNameSi = LEW(xml, off + 1 * 4);  // AttrName String Index
                        int attrValueSi = LEW(xml, off + 2 * 4); // AttrValue Str Ind, or FFFFFFFF
                        int attrFlags = LEW(xml, off + 3 * 4);
                        int attrResId = LEW(xml, off + 4 * 4);  // AttrValue ResourceId or dup AttrValue StrInd
                        off += 5 * 4;  // Skip over the 5 words of an attribute

                        String attrName = compXmlString(xml, sitOff, stOff, attrNameSi);
                        String attrValue = attrValueSi != -1
                          ? compXmlString(xml, sitOff, stOff, attrValueSi)
                          : /*"resourceID 0x" + */attrResId.ToString();
                        if (d.ContainsKey(attrName))
                        {
                            if (d[attrName].GetType() == typeof(string))
                            {   // Promote to list
                                String value = d[attrName].ToString();
                                d[attrName] = new List<String> { value };
                            }
                            ((List<string>)d[attrName]).Add(attrValue);
                        }
                        else
                            d.Add(attrName, attrValue);
                        //tr.add(attrName, attrValue);
                    }

                }
                else if (tag0 == endTag)
                { // XML END TAG
                    indent--;
                    off += 6 * 4;  // Skip over 6 words of endTag data

                }
                else if (tag0 == endDocTag)
                {  // END OF XML DOC TAG
                    break;

                }
                else
                {
                    throw new Exception("  Unrecognized tag code '" + tag0.ToString("X")
                      + "' at offset " + off);
                }
            } // end of while loop scanning tags and attributes of XML tree


            return d;
        } // end of decompressXML


        private static String compXmlString(byte[] xml, int sitOff, int stOff, int strInd)
        {
            if (strInd < 0) return null;
            int strOff = stOff + LEW(xml, sitOff + strInd * 4);
            return compXmlStringAt(xml, strOff);
        }


        // compXmlStringAt -- Return the string stored in StringTable format at
        // offset strOff.  This offset points to the 16 bit string length, which 
        // is followed by that number of 16 bit (Unicode) chars.
        private static String compXmlStringAt(byte[] arr, int strOff)
        {
            int strLen = arr[strOff + 1] << 8 & 0xff00 | arr[strOff] & 0xff;
            byte[] chars = new byte[strLen];
            for (int ii = 0; ii < strLen; ii++)
            {
                chars[ii] = arr[strOff + 2 + ii * 2];
            }


            return System.Text.Encoding.UTF8.GetString(chars);  // Hack, just use 8 byte chars
        } // end of compXmlStringAt


        // LEW -- Return value of a Little Endian 32 bit word from the byte array
        //   at offset off.
        private static int LEW(byte[] arr, int off)
        {
            return (int)(((uint)arr[off + 3] << 24 & 0xff000000) | ((uint)arr[off + 2] << 16 & 0xff0000) | ((uint)arr[off + 1] << 8 & 0xff00) | ((uint)arr[off] & 0xFF));
        } // end of LEW

    }
}

Now we can write the console code that makes the call and displays the results. Replace the apkpath with one of your own.

Change Program.cs to look like this.

using System;
using System.Collections.Generic;

namespace GetApkVersion
{
    class Program
    {
        static void Main(string[] args)
        {
            string apkpath = @"C:\Users\Admin\Documents\apk\Splash.Android-Signed.apk";
            Dictionary<string, object> version = GetApkInfo.GetInfo(apkpath);
            foreach (KeyValuePair<string, object> kv in version)
            {
                if (kv.Value.GetType() == typeof(List<String>))
                    Console.WriteLine(kv.Key + "=" + String.Join(",", ((List<String>)kv.Value).ToArray()));
                else
                    Console.WriteLine(kv.Key + "=" + kv.Value.ToString());
            }
            Console.ReadKey();
        }
    }
}

Here are the results.