Friday, February 25, 2005

Part 6: Efficiently Representing Sets - Continued...

Great article. Gives some very valuable information on implementing sets with a finite universe.


It doesn't suit my requirements - I need to represent sets where the universal set might be infinite - like, say the set of strings.


The trouble starts with trying to implement the set complement operation - you can't obviously list an infinite set, so the trick is to represent it as a cofinite set. I haven't really read up on cofinite sets, but the layman definition goes as


'A set whose complement is finite'


Well that's it. So keep a flag with the data structure to indicate a set complement.


The ElementOf operation is trivial to implement. Subset is trickier and so's union, intersection and difference.


I just completed a bare bones implementation using a hashmap. All the tests are working (got 20 of them). You can find it here if you're interested.


Part 6: Efficiently Representing Sets - Continued...

Great article. Gives some very valuable information on implementing sets with a finite universe.

It doesn't suit my requirements - I need to represent sets where the universal set might be infinite - like, say the set of strings.

The trouble starts with trying to implement the set complement operation - you can't obviously list an infinite set, so the trick is to represent it as a cofinite set. I haven't really read up on cofinite sets, but the layman definition goes as

'A set whose complement is finite'

Well that's it. So keep a flag with the data structure to indicate a set complement.

The ElementOf operation is trivial to implement. Subset is trickier and so's union, intersection and difference.

I just completed a bare bones implementation using a hashmap. All the tests are working (got 20 of them). You can find it here if you're interested.

Thursday, February 24, 2005

Visual C# Developer Center: Part 6: Efficiently Representing Sets

And now I need to roll out my own version of a set datastructure library. C# has hashmaps - they come close but aren't really sets. You can use them to implement sets, but that's about it. Also, I want my sets to conform to one of the standard collection interfaces so that they can interoperate with code. And I REALLY REALLY need set intersection, set union and set difference!

I've been looking on the net for some guidelines to implement the same - implementing a data structure is never easy so its better to be cautious to get it right. Just found this article which seems to fit the bill.

Visual C# Developer Center: Part 6: Efficiently Representing Sets

Hope this helps. Will post more on the article later.

CAPICOM - Accessing CryptoAPI from VB6

I've this need to calculate a file fingerprint in VB6. So the need to access CrptoAPI in vb6. Didnt want to do win32 calls from VB and thankfully MS has a COM layer over cryptoAPI called CAPICOM. Useful stuff...as the MSDN site says


"Summary: CAPICOM is a new security technology from Microsoft that allows Microsoft Visual Basic, Visual Basic Script, ASP, and C++ programmers to easily incorporate digital signing and encryption into their application. (5 printed pages)"


Okay, now you can calculate SHA1, SHA-256 and SHA512 using VB. Trouble was that you cannot use the Filesystem object as it doesnt read binary files.


Here's a small sample that does a SHA1 for binary files in VB6.




Const BUFFER_SIZE AsString= 4096
Dim sha As CAPICOM.HashedData
Set sha =New CAPICOM.HashedData

f =FreeFile
Open "somebinaryfile.xyz"For Binary As f

Dim totalbytes AsLong
totalbytes =LOF(f)
Dim currentPos AsLong
currentPos = 0

While currentPos + BUFFER_SIZE <= totalbytes
Get f, , buffer
currentPos = currentPos + BUFFER_SIZE
sha.Hash buffer
Wend

Dim chunk() AsByte
Dim chunkSize AsLong
chunkSize = totalbytes - currentPos
If (chunkSize > 0) Then
ReDim chunk(1 To chunkSize)
Get f, , chunk
sha.Hash chunk
EndIf

Close f
txtHash.Text = sha.Value


ASPAlliance.com : ASP and ASP.NET Tutorials : The LogonUser API

ASPAlliance.com : ASP and ASP.NET Tutorials : The LogonUser API

Finally found a way to allow ASPNET user access to the LogonUser api. Guess what, WinXP and above, no specific permissions are required! Cool.

Visual C# Developer Center: Part 6: Efficiently Representing Sets

And now I need to roll out my own version of a set datastructure library. C# has hashmaps - they come close but aren't really sets. You can use them to implement sets, but that's about it. Also, I want my sets to conform to one of the standard collection interfaces so that they can interoperate with code. And I REALLY REALLY need set intersection, set union and set difference!


I've been looking on the net for some guidelines to implement the same - implementing a data structure is never easy so its better to be cautious to get it right. Just found this article which seems to fit the bill.


Visual C# Developer Center: Part 6: Efficiently Representing Sets


Hope this helps. Will post more on the article later.

CAPICOM - Accessing CryptoAPI from VB6

I've this need to calculate a file fingerprint in VB6. So the need to access CrptoAPI in vb6. Didnt want to do win32 calls from VB and thankfully MS has a COM layer over cryptoAPI called CAPICOM. Useful stuff...as the MSDN site says


"Summary: CAPICOM is a new security technology from Microsoft that allows Microsoft Visual Basic, Visual Basic Script, ASP, and C++ programmers to easily incorporate digital signing and encryption into their application. (5 printed pages)"


Okay, now you can calculate SHA1, SHA-256 and SHA512 using VB. Trouble was that you cannot use the Filesystem object as it doesnt read binary files.


Here's a small sample that does a SHA1 for binary files in VB6.




Const BUFFER_SIZE AsString= 4096
Dim sha As CAPICOM.HashedData
Set sha =New CAPICOM.HashedData

f =FreeFile
Open "somebinaryfile.xyz"For Binary As f

Dim totalbytes AsLong
totalbytes =LOF(f)
Dim currentPos AsLong
currentPos = 0

While currentPos + BUFFER_SIZE <= totalbytes
Get f, , buffer
currentPos = currentPos + BUFFER_SIZE
sha.Hash buffer
Wend

Dim chunk() AsByte
Dim chunkSize AsLong
chunkSize = totalbytes - currentPos
If (chunkSize > 0) Then
ReDim chunk(1 To chunkSize)
Get f, , chunk
sha.Hash chunk
EndIf

Close f
txtHash.Text = sha.Value


ASPAlliance.com : ASP and ASP.NET Tutorials : The LogonUser API

ASPAlliance.com : ASP and ASP.NET Tutorials : The LogonUser API


Finally found a way to allow ASPNET user access to the LogonUser api. Guess what, WinXP and above, no specific permissions are required! Cool.

Wednesday, February 23, 2005

WSE2SP2 - Installing test certificates and the Mystery of Missing 'Other People' Tab

I was trying out WSE2.0 SP2 Hands on lab on security and hit this really strange problem.

I needed to install the sample certificates supplied with WSE, typically located in C:\Program Files\Microsoft WSE\v2.0\Samples\Sample Test Certificates.

As given in the Hands on lab manual, I installed the Server Certificate.pfx (server's private key) to the LocalMachine\Personal store and the Client Certificate.pfx (client's private key) to the CurrentUser\Personal store. The trouble started when I wanted to install the Server's public key to the Current User's\Other People store as directed. There wasn't an Other people Tab available in the mmc snap in. There is a workaround given in the Hands on lab manual -

Note: if you don't have an Other People store under Current User, open Internet Explorer, select Tools, Internet Options, Content, and press the Certificates button. You should see an Other People tab in the certificates dialog. You can import the certificate here through this interface or you can return to mmc and refresh the Current User tree and Other People should now show up.


Trouble was, it didnt work for me. I never got the tab in MMC as the note says and nor was I able to import the certificate to the correct location.


After a bit of googling, turns out that you can use the certmgr.exe's command line. It accepts a '-s' argument used to specify the store - and the value required for 'Other People' store is "AddressBook"
So the command becomes

certmgr -add "Server Public.cer" -s AddressBook

and voila - works like a charm. You can then refresh MMC console and see that the certificate has indeed been imported successfully.

WSE2SP2 - Installing test certificates and the Mystery of Missing 'Other People' Tab

I was trying out WSE2.0 SP2 Hands on lab on security and hit this really strange problem.

I needed to install the sample certificates supplied with WSE, typically located in C:\Program Files\Microsoft WSE\v2.0\Samples\Sample Test Certificates.

As given in the Hands on lab manual, I installed the Server Certificate.pfx (server's private key) to the LocalMachine\Personal store and the Client Certificate.pfx (client's private key) to the CurrentUser\Personal store. The trouble started when I wanted to install the Server's public key to the Current User's\Other People store as directed. There wasn't an Other people Tab available in the mmc snap in. There is a workaround given in the Hands on lab manual -

Note: if you don't have an Other People store under Current User, open Internet Explorer, select Tools, Internet Options, Content, and press the Certificates button. You should see an Other People tab in the certificates dialog. You can import the certificate here through this interface or you can return to mmc and refresh the Current User tree and Other People should now show up.


Trouble was, it didnt work for me. I never got the tab in MMC as the note says and nor was I able to import the certificate to the correct location.


After a bit of googling, turns out that you can use the certmgr.exe's command line. It accepts a '-s' argument used to specify the store - and the value required for 'Other People' store is "AddressBook"
So the command becomes

certmgr -add "Server Public.cer" -s AddressBook

and voila - works like a charm. You can then refresh MMC console and see that the certificate has indeed been imported successfully.

Thursday, February 17, 2005

Back to work....

Am back today after a long time. Been down with viral fever and doing nothing more than warming the bed with my backside :(. Really depressing!


Anyway am back today - but feeling quite tired.


This thing's thrown a spanner in the works in more than one thing - was supposed to go to the finale of the Aero India show here and couldn't make it - thanks to the viral! Was hoping to snap some nice pics there - esp. of the Surya Kirans' acrobatic show and post them on the blog...


Â


Back to work....

Am back today after a long time. Been down with viral fever and doing nothing more than warming the bed with my backside :(. Really depressing!


Anyway am back today - but feeling quite tired.


This thing's thrown a spanner in the works in more than one thing - was supposed to go to the finale of the Aero India show here and couldn't make it - thanks to the viral! Was hoping to snap some nice pics there - esp. of the Surya Kirans' acrobatic show and post them on the blog...


Â


Saturday, January 29, 2005

&nbsp; in XSLT - MSXML3, MSXML4

I had this problem last week - needed to generate . My method was to use an entity declaration



<?xml version="1.0"?>
<!DOCTYPE xsl:stylesheet [
<!ENTITY nbsp "&#160;"> <!-- white space in XSL -->
]>


Sadly, this doesn't work with MSXML3 or MSXML4. The generated HTML has a space ' ' character rather than

After some googling, found the answer and a workaround on the xsl list

Editing XSLT

Am back on XML/XSL now. I'm in need of a good XSL editor - good being defined as

1. Support intellisense
2. Have a decent XPATH visualizer
3. Debugging (optional)
4. FREE :))
5. Editor with regexp support for search/replace (We all write XHTML. But if you need to start of with an existing HTML page, 99 times out of hundered, its not going to be XHTML)

I've been using Marrowsoft's Xselerator for the past week. The XSL editor is great, other than for a couple of problems
1. Long lines - tends to get the editor confused.
2. Search/replace - Search/Replace dialogs are hard to handle.
3. 15 day trial version - so I'm soon going to be left without an editor.

Plus points are
1. Good XPath visualizer
2. Support for stylesheet params

Sadly, VS.NET lacks intellisense for XSLT files. However, there's a workaround found on Fesersoft's site

It works quite well. Plus you get VS.NET's regexp search and replace. I'm using XSLDebugger for Xpath visualization. It's a free download, though no longer supported. I guess my troubles will start when I have xsl stylesheet parameters where I'll have to live with default parameters values.

in XSLT - MSXML3, MSXML4

I had this problem last week - needed to generate . My method was to use an entity declaration






<?xml version="1.0"?>

<!DOCTYPE xsl:stylesheet [

<!ENTITY nbsp " "> <!-- white space in XSL -->

]>





Sadly, this doesn't work with MSXML3 or MSXML4. The generated HTML has a space ' ' character rather than


After some googling, found the answer and a workaround on the xsl list


Editing XSLT

Am back on XML/XSL now. I'm in need of a good XSL editor - good being defined as


1. Support intellisense

2. Have a decent XPATH visualizer

3. Debugging (optional)

4. FREE :))

5. Editor with regexp support for search/replace (We all write XHTML. But if you need to start of with an existing HTML page, 99 times out of hundered, its not going to be XHTML)


I've been using Marrowsoft's Xselerator for the past week. The XSL editor is great, other than for a couple of problems

1. Long lines - tends to get the editor confused.

2. Search/replace - Search/Replace dialogs are hard to handle.

3. 15 day trial version - so I'm soon going to be left without an editor.


Plus points are

1. Good XPath visualizer

2. Support for stylesheet params


Sadly, VS.NET lacks intellisense for XSLT files. However, there's a workaround found on Fesersoft's site


It works quite well. Plus you get VS.NET's regexp search and replace. I'm using XSLDebugger for Xpath visualization. It's a free download, though no longer supported. I guess my troubles will start when I have xsl stylesheet parameters where I'll have to live with default parameters values.

Thursday, October 28, 2004

The Code Project - Advanced Unit Testing, Part I - Overview - C# Programming

Came across this article on some advances to NUnit that I found missing! Great stuff, but I guess I'll have to wait until this gets into NUnit.

Very useful article - explores points where unit testing doesn't work well enough (atleast the normal set of tools available).

The Code Project - Advanced Unit Testing, Part I - Overview - C# Programming

The Code Project - Advanced Unit Testing, Part I - Overview - C# Programming

Came across this article on some advances to NUnit that I found missing! Great stuff, but I guess I'll have to wait until this gets into NUnit.


Very useful article - explores points where unit testing doesn't work well enough (atleast the normal set of tools available).


The Code Project - Advanced Unit Testing, Part I - Overview - C# Programming

Wednesday, October 27, 2004

Email message attachments in Perl

Pretty useful stuff. Just used this to send out personalized invitation emails to all my friends.
Email message attachments in Perl

Additional note: If you're using a MS Exchange server like me, most likely, you will not be able to send out email outside your organization. To work around this, you need to use the sign in to your SMTP server using the auth method in Net::SMTP. Give your domain username (just the username) and the password in the call to auth.

I noticed that if I included the full email id in the call to auth, the call did not fail but only the emails within the org. domain went through. I haven't really dug into this, cos I got it working with the username soon after.

Nice logs in .NET

Been off the blog for sometime now. Been a bit busy with work and also with preparations for marriage (on Nov 7th) so haven't got much time in between to post.

Here's something interesting (and hopefully useful).

I've always been a fan of Log4Net (http://logging.apache.org/log4net). However, on this occasion, I had to use something that was custom built (using open source doesn't go well with certain customers). So decided to look into System.Diagnostics. The overall idea seems to be pretty simple - configure a set of trace listeners, send your trace requests to a static Trace class. The Trace class will forward the calls to each of the listeners in sequence. There's also a mechanism for multi-level logging - from nothing on to most verbose (None, Errors, Warnings, Information and Verbose). This is somewhat like the levels in log4net, but surprisingly, the Trace interface expects you to check for the switch each time you want to make a call. For example

Traceswitch ts = new Traceswitch("myswitch");
.
.
.
Trace.WriteLineIf(ts.TraceInfo, "this message will be logged if ts.level <= TraceLevel.TraceInfo);

Frankly, its too much work for me. Also, it is inefficient as the parameters will be built to a temporary object even if the trace level results in a no-op.

It sounds pretty basic - and believe me, it is. It just logs the string that it's passed. Also, there's no way to have different listeners that can each log at a different level - something like I want my file log to be at verbose level but want an event log listener that sends errors to the event log.

So, here's something that I whipped up that gets over (some of) these limitations.

My requirements were
1. Include a timestamp
2. Include caller information
3. Include an easier API for app programmers (me)

For the first, the easiest thing to do is to subclass TextWriterTraceListener and override the Write and the WriteLine methods.

The second and third require a wee bit more thought. One solution is to replace Trace class with another class that provides a more convenient interface. I called mine TraceExt with the following methods

WriteInformation
WriteError
WriteWarning
WriteVerbose

Each method has two overloaded versions - one that takes a plain string and another that takes a format string and object array as in string.Format.

Another point worth mentioning is that the TraceExt is not a static class. Rather it's expected that the class that uses TraceExt will keep a static member.

The TraceExt ctor takes the name of the trace switch to use. It instantiates the trace switch and then on, the calls to the WriteXXX are forwarded to Trace.WriteLine if the switch has the necessary level. A sample use of the TraceExt follows:

public class MainClass {
private static TraceExt log = new TraceExt("myswitch");

public static int Main(string args[]) {
Trace.listeners.add(new TextWriterTraceListener("c:\some.log");
log.WriteEntry();
log.WriteInformation("This is some string");
log.WriteExit();
}
}



Each of the WriteXXXX methods are implemented as follows:

public void Information(string format, params object[] args)
{
if (ts.TraceInfo)
{
WriteLine(format, args);
}
}


The WriteLine method is used to include other information in the logs like the caller, the method name etc. A little bit of reflection is used to get such information.


StackTrace trace = new StackTrace();
StackFrame frame = trace.GetFrame(0);
string callingMethod = frame.GetMethod().Name;
string callingClass = frame.GetMethod().DeclaringType.Name;
string callingNS = frame.GetMethod().DeclaringType.Namespace;


Sadly, just found out that I can't upload the solution file zip. If you're interested, comment on the article and leave your email id.