Blog

A catalogue of my discoveries in software development and related subjects, that I think might be of use or interest to everyone else, or to me when I forget what I did!

Blogger extensions and integration using ASP.NET

October 27, 2008

After recently starting this blog using my Blogger account and its FTP publishing feature I wanted a more seamless integration with my existing site for the index page of the blog. In other words, I wanted to show the latest blogs as part of my existing site using the masterpage I have in ASP.NET. To achive this goal, I have set Blogger to publish to /blog on my domain, with a proprietary filename and .html extension. I have then put a Default.aspx page in the /blog directory, which uses the site master page. This default page basically opens the .html file server side and writes the stripped out relavent HTML content to a placeholder in the ASP.NET page. I have included some of the Blogger specific meta/link tags in the .aspx page head, to enable the RSS/Atom feeds from the page and to enable the edit post controls etc. to work. As its ASP.NET you can also add extra features to the page. I have added a drop down list of the categories (Blogger labels) to use as a quick link to each category. Blogger.com creates a .html page for each of your labels containing links to the relevant posts, these files are stored under the 'labels' folder, which can be simply enumerated on the server. Code:
if not IsPostback then

   try

    Dim file As String = "blog-content.html"

    Response.Cache.SetLastModified(IO.File.GetLastWriteTime(Server.MapPath(file)))

    dim content as string=IO.File.ReadAllText(Server.MapPath(file))
    content = content.Substring(content.IndexOf("<body>") + "<body>".Length, content.LastIndexOf("</body>") - content.IndexOf("<body>") - "<body>".Length)
    content = content.Remove(content.IndexOf("<iframe"), content.IndexOf("</iframe>") + "</iframe>".Length - content.IndexOf("<iframe"))

    lblContent.Text=content

    'load the labels
    dim labelFiles() as string=IO.Directory.GetFiles(Server.MapPath("labels"))

    ddlCats.Items.Clear
    ddlCats.Items.Add(New ListItem("--Jump To Category--",""))  

    for each filename as string in labelFiles

     dim url as string=filename.substring(filename.lastindexof("\")+1,filename.length-filename.lastindexof("\")-1)

     ddlCats.Items.Add(New ListItem(url.replace(".html",""),url))

    next

   catch ex as exception

    lblContent.Text="Error: " & ex.Message

   end try
My site only uses this method for the index page, but you could extend this idea even further to display all blog pages within your custom ASP.NET / masterpage site, by putting the /blog directory under a custom HttpHandler (if your server allows this) and rewriting the URL to your ASP.NET page. Also I would suggest using regular expressions for filtering the HTML if your server supports it. -- Edit: 25/11/2008 I have now improved the integration by using the HTML generated by my masterpage as the template on Blogger.com, so all the pages look like you are on my website. This means I could refine the above process of finding the correct content for each placeholder, (with the addition of a seperate placeholder for the sidebar) as the divs that contain the data have specific class names. I have also created two more aspx pages, "index by date" and "index by category" - the code behind for these simply enumerate the "archives"/"labels" folders on the server, (the same as my drop down box above). Additionally, these pages open each .html file to parse the content for all of the post titles and permalinks to create the list of links for each date/label. -- Edit: 18/10/2010 Since my blog no longer uses Blogger due to FTP publishing being cancelled this code is now redundant.
Permalink: Blogger extensions and integration using ASP.NET

Blog Launch

October 23, 2008

I've started this blog on my site to catalogue anything I think might be of use or interest to everyone else, or to me when I forget what I did!

This will most likely be software development related stuff, since this is what I do day in day out. This will mainly be ASP.NET/VB.NET/C# stuff depending on what language I am using at the time but will also have some other stuff thrown in for good measure such as SQL, C++, Assembly, reverse engineering, my bio and more..

I will be back dating a lot of my previous work, as it comes to mind that it's worth blogging, so it might appear that I have been posting since before the launch, I just prefer to keep things chronological!

Anyway hopefully it might prove useful!
Permalink: Blog Launch

Programmatically Setting the Default Button of a Textbox

October 09, 2008

In ASP.NET, setting the default button of a textbox is usually a simple task as you simply wrap the button and textbox in an asp:panel and set the 'defaultbutton' attribute on the panel. Sometimes however, it is not possible to put the button and textbox in a panel together, for example if the textbox is part of a databound template (e.g. gridview, repeater etc.) If you look at the HTML code that the asp:panel method generates, it basically attaches a javascript event handler to the 'onkeypress' of the textbox. I have wrapped this functionality, so that you can programmatically set the default button of any textbox control on your page, to any button on your page. Code:
Public Shared Sub SetDefaultButton(ByVal textbox As System.Web.UI.WebControls.TextBox, ByVal button As System.Web.UI.WebControls.IButtonControl)
 If TypeOf button Is System.Web.UI.WebControls.WebControl Then
 textbox.Attributes("onkeypress") = "javascript:if (event.keyCode == 13){ document.getElementById('" & CType(button, System.Web.UI.WebControls.WebControl).ClientID & "').click();event.cancelBubble = true;if (event.stopPropagation) event.stopPropagation();return false;}"
 End If

End Sub
Permalink: Programmatically Setting the Default Button of a Textbox

Using .NET TripleDES Crypto Provider

August 28, 2008

I recently wrote a wrapper class implementing the .NET System.Security.Cryptography.TripleDESCryptoServiceProvider to quickly allow you to encrypt a plaintext string and return the Base64 encoded string of the encrypted bytes created by the TripleDES cipher (and decrypt that string back to plaintext!). This provides a very simple way to encrypt and decrypt your data without the need to store it as a binary image. The VB.NET code is shown below:
Public Class TripleDES
        Private _k, _iv As Byte()

        Public Sub New(ByVal key As Byte(), ByVal iv As Byte())
            _k = key
            _iv = iv
        End Sub

        Public Property Key() As Byte()
            Get
                Return _k
            End Get
            Set(ByVal value As Byte())
                _k = value
            End Set
        End Property

        Public Property IV() As Byte()
            Get
                Return _iv
            End Get
            Set(ByVal value As Byte())
                _iv = value
            End Set
        End Property

        Public Function Encrypt(ByVal plaintext As String) As String
            Dim cipher As New System.Security.Cryptography.TripleDESCryptoServiceProvider()
            cipher.BlockSize = 64

            Dim cipherText As String
            Using cipherTextStream As New IO.MemoryStream()
                Using cryptoStream As New System.Security.Cryptography.CryptoStream(cipherTextStream, cipher.CreateEncryptor(_k, _iv), System.Security.Cryptography.CryptoStreamMode.Write)
                    Dim bytes As Byte() = Text.Encoding.UTF8.GetBytes(plaintext)
                    cryptoStream.Write(bytes, 0, bytes.Length)
                    cryptoStream.FlushFinalBlock()

                    cipherText = Convert.ToBase64String(cipherTextStream.ToArray())
                End Using
            End Using

            Return cipherText

        End Function

        Public Function Decrypt(ByVal ciphertext As String) As String
            Dim cipher As New System.Security.Cryptography.TripleDESCryptoServiceProvider()
            cipher.BlockSize = 64

            Dim plaintext As String
            Using plaintextStream As New IO.MemoryStream()
                Using cryptoStream As New System.Security.Cryptography.CryptoStream(plaintextStream, cipher.CreateDecryptor(_k, _iv), System.Security.Cryptography.CryptoStreamMode.Write)
                    Dim bytes As Byte() = Convert.FromBase64String(ciphertext)
                    cryptoStream.Write(bytes, 0, bytes.Length)
                    cryptoStream.FlushFinalBlock()

                    plaintext = Text.Encoding.UTF8.GetString(plaintextStream.ToArray())
                End Using
            End Using

            Return plaintext

        End Function
    End Class
To use it, simply instanciate it with your Key and Initialization Vector. Make sure your K and IV are the same when decrypting data you encrypted with them!
Permalink: Using .NET TripleDES Crypto Provider

Simple Password Generator

August 28, 2008

To generate random passwords I have written a simple class in VB.NET, which accepts a string containing valid password characters and then generates passwords of n length using the specified characters.
Public Class PasswordGenerator
        Private _pwChars As String

        Public Sub New()
            MyClass.New("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
        End Sub

        Public Sub New(ByVal validChars As String)
            _pwChars = validChars
        End Sub

        Public Function GeneratePassword(ByVal length As Integer) As String
            Dim password As String = ""
            Dim rndNum As New Random()

            For i As Integer = 1 To length
                password &amp;= _pwChars.Substring(rndNum.Next(0, _pwChars.Length - 1), 1)
            Next

            Return password
        End Function

    End Class
Permalink: Simple Password Generator

Bio - Web Marketing Group

August 07, 2008

I am now working for an online marketing and search engine optimization specialist WMG (web marketing group) as a bespoke web applications developer. Again I am primarily working with ASP.NET and MS SQL.

WMG Logo
Permalink: Bio - Web Marketing Group

Thread.Join Deadlock with Invoke

September 07, 2007

If you are trying to Make Thread-Safe Calls to Windows Forms Controls then you can run into deadlock problems when calling 'Invoke' from your thread, if your main thread is waiting for a Thread.Join on the calling thread. I posted a workaround for this on the Microsoft site, which uses an extra thread to make the call to Invoke method, to avoid the deadlock. VB.NET example:
Private Sub SetText(ByVal str_text As String)
        If Me.textbox1.InvokeRequired = True Then
                'call an asyncronous invoke, by calling it then forcing a DoEvents
                Dim asyncInvokeThread As New Threading.Thread(New Threading.ParameterizedThreadStart(AddressOf AsyncInvoke))
                asyncInvokeThread.IsBackground = True
                asyncInvokeThread.Start(str_text)

                Application.DoEvents()
        Else
                me.textbox1.text=str_text
        End If
End Sub
Private Sub AsyncInvoke(ByVal obj_text As Object)
        Dim str_text As String = CStr(obj_text)
        Dim d As New SetTextCallback(AddressOf SetText)
        Me.Invoke(d, New Object() {str_message})
End Sub
Permalink: Thread.Join Deadlock with Invoke

Bio - Kinetic

March 01, 2007

I then started working for a worldwide advertising media group, Kinetic, where I worked primarily on in-house systems. The development was mainly in ASP.NET and MS-SQL, although it involved the incorporation of many other technologies, including writing client side controls, Microsoft SharePoint and the research and development of any other potential useful technology.

Kinetic Logo
Permalink: Bio - Kinetic

x86 Assembly Base64 Encoder

September 19, 2006

I joined in a "competition" with some of the experts in the assembly forums over at experts-exchange to write the fastest assembly code Base64 encoder, following the guidelines of the RFC3548. Several versions emerged, some using lookup tables to precompute results, which faired well on CPUs which large caches. My version was more about opcode optimization and using bitwise arithmetic over decimal arithmetic, which achieved best results on CPUs that have better pipelining. Code:
  void ToBase64( BYTE* pSrc, char* pszOutBuf, int len )
{
      char* chr_table="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

       __asm{
                  mov ecx, len
                  mov esi, pSrc                              //;bytes from source
                  mov edi, chr_table
                  push ebp
                  mov ebp, pszOutBuf

src_byteLoop:

                  xor eax, eax

                  //;read 3 bytes
                  mov ah, byte ptr[esi]
                  mov al, byte ptr[esi+1]
                  shl eax, 16
                  mov ah, byte ptr[esi+2]

                  //;manipulate in edx bitset1
                  mov edx, eax
                  shl eax, 6                                    //;done first 6 bits

                  shr edx, 26            
                  mov bl, byte ptr [edi+edx]            //;put char in buffer
                  mov byte ptr[ebp], bl
                  inc ebp                                          //;next buf

                  //;manipulate in edx bitset2
                  mov edx, eax
                  shl eax, 6                                    //;done first 6 bits

                  shr edx, 26
                  mov bl, byte ptr [edi+edx]            //;put char in buffer
                  mov byte ptr[ebp], bl
                  inc ebp                                          //;next buf

                  //;manipulate in edx bitset3
                  mov edx, eax
                  shl eax, 6                                    //;done first 6 bits

                  shr edx, 26
                  mov bl, byte ptr [edi+edx]            //;put char in buffer
                  mov byte ptr[ebp], bl
                  inc ebp                                          //;next buf

                  //;manipulate in edx bitset4
                  mov edx, eax
                  shl eax, 6                                    //;done first 6 bits

                  shr edx, 26
                  mov bl, byte ptr [edi+edx]            //;put char in buffer
                  mov byte ptr[ebp], bl
                  inc ebp                                          //;next buf

                  //;done these bytes
                  add esi, 3
                  sub ecx, 3

                  cmp ecx, 3
                  jge src_byteLoop                        //;still got src bytes

                  xor eax, eax                              //;set to zero (pad count)
                  cmp ecx, 0
                  jz finished

                        //;need to pad out some extra bytes

                        //;read in 3 bytes regardless of junk data following pSrc - already zero from above)
                        mov ah, byte ptr[esi]
                        mov al, byte ptr[esi+1]
                        shl eax, 16
                        mov ah, byte ptr[esi+2]

                        sub ecx, 3                                    //;bytes just read
                        neg ecx                                          //;+ve inverse
                        mov edx, ecx                              //;save how many bytes need padding

                        //;as per the RFC, any padded bytes should be 0s
                        mov esi, 0xFFFFFF
                        lea ecx, dword ptr[ecx*8+8]            //;calculate bitmask to shift
                        shl esi, cl
                        and eax, esi                              //;mask out the junk bytes

                        mov ecx, edx                              //;restore pad count

                        //;manipulate in edx byte 1
                        mov edx, eax
                        shl eax, 6                                    //;done first 6 bits                        

                        shr edx, 26
                        mov bl, byte ptr [edi+edx]            //;put char in buffer
                        mov byte ptr[ebp], bl
                        inc ebp                                          //;next buf

                        //;manipulate in edx byte 2
                        mov edx, eax
                        shl eax, 6                                    //;done first 6 bits                        

                        shr edx, 26
                        mov bl, byte ptr [edi+edx]            //;put char in buffer
                        mov byte ptr[ebp], bl
                        inc ebp                                          //;next buf

                        //;manipulate in edx byte 3
                        mov edx, eax
                        shl eax, 6                                    //;done first 6 bits                        

                        shr edx, 26
                        mov bl, byte ptr [edi+edx]            //;put char in buffer
                        mov byte ptr[ebp], bl
                        inc ebp                                          //;next buf

                        //;manipulate in edx byte 3
                        mov edx, eax
                        shl eax, 6                                    //;done first 6 bits                        

                        shr edx, 26
                        mov bl, byte ptr [edi+edx]                  //;put char in buffer
                        mov byte ptr[ebp], bl
                        inc ebp                                          //;next buf

                        mov eax, ecx                              //;'return' pad count

finished:
                  test eax, eax
                  jz end
                  //;some bytes were padding, put them as =

                        sub ebp, eax                        //;move ptr back for num bytes to pad
padChars:
                        mov byte ptr[ebp], 0x3d            //;=
                        inc ebp
                        dec eax

                        jnz padChars

end:
                  pop ebp
        }      
}
There were several key points to my optimization technique:
  • Firstly unrolling of loops, which basically means eliminating any kind of loop structures in the code, in favour of manually typing the code to perform the operations on the data. In terms of asm optimizations this reduces branching and shifting EIP too often.
  • Another key point, was minimizing the load/store operations performed. To keep these down, you can use indirect addressing to get to the byte in memory you are interested in.e.g. mov edx, base add edx, edi mov bl, byte ptr[edx] becomes mov bl, [edx+edi]
  • As Base64 encoding requires you to work at bit level using 3 bytes at a time, you can speed up the conversion of the bit sets by using the binary arithmetic operations of SHL and SHR (shift left/right) to progressively take the most significant bits of one DWORD and operate on them with a lower significance by shifting them.For example, if EAX contains a DWORD and you copy that the EDX. You can get rid of the left most 6 bits of EAX by shifting left 6. You can deal with those 6 bits in EDX, by shifting EDX right 26 times. This is much quicker than shifting out one bit at a time.
My final throughput on the official test machine was 196.92 MB/s, which is almost double the throughput of the CryptoAPI function, which came in at 104.05 MB/s
Permalink: x86 Assembly Base64 Encoder

Bio - Adtec Software

September 01, 2005

After leaving University I found my first job in software development at Adtec Software, where I helped to develop software and database solutions, mainly for Debt Collection Agencies. During my experience there I continued to develop my skills, learning new applications, learning real life development techniques and dealing directly with clients. It gave me the opportunity to put my skills into practice and to learn more about how development of software and database applications is carried out in industry. I was also given the opportunity to study Microsoft professional courses in VB .NET.

Adtec Logo
Permalink: Bio - Adtec Software