Sunday, November 16, 2008

C++: Anonymous namespaces

The C++ standard now says that the use of the static keyword to declare a file-scope varible as non-externally linkable is deprecated in favour of declaring it inside an anonymous namespace, that is, a namespace with no name. Such variables are guaranteed to be local to that file and can be named concisely without fear of clashes or polluting the global namespace.

That is until you want to start doing unity builds. Unity building is the pre-build process of taking all the .cpp files in a C++ project and #includeing them into a single .cpp file, and then only compiling that single file. Apparently (and surprisingly) this compiles many, many times faster than using seperate files (perhaps because of reduced disc accesses?) at least it does within Visual Studio 2005.

The main point is that you should always to endeavour to make all your variable names unique as you never know when that scope could change.

Reclaimed

I've just reclaimed this blog. It's been ages since I've posted and so much has happened. It is, this very day, my thirty-second birthday, which means I'm only twenty in hexadecimal.

Wednesday, August 09, 2006

Ruby : Cryptography - Substitution Cypher

Here's a Ruby class that can perform an encryption based on a alphabetical subsititution cypher and decrypt the resulting cyphertext using the same key. The key is provided as any alphabetical phrase. Symbols and case in the phrase are ignored and the order of each unique letter in the phrase is used to construct a new alphabet. Any remaining letters not in the phrase are added to the end of the new alphabet in ascending order.



Once the key is set the encryption algorithm simply subsitutes the letters in the input string for the ones in the new alphabet. Decryption is the reverse operation. There are so many permutations of the 26 letter Roman alphabet that the cyphertext is hard to crack using brute force techniques. It is easier to crack using letter and word frequency analysis methods but that's beyond the scope of this blog.



Check out the link that inspired this work.




class SubstitutionCypher

attr_reader :key, :cypherbet

def initialize

# most common 2, 3 and 4 letter words could be used to
verify english
# plain text on a brute force attack

@@common_english_words =
["of","to","in","it","is","be","as","at","so","we","he","by","o
r","on","do","if","me","my","up","an","go","no","us","am","the"
,"and","for","are","but","not","you","all","any","can","had","h
er","was","one","our","out","day","get","has","him","his","how"
,"man","new","now","old","see","two","way","who","boy","did","i
ts","let","put","say","she","too","use","that","with","have","t
his","will","your","from","they","know","want","been","good","m
uch","some","time","very","when","come","here","just","like","l
ong","make","many","more","only","over","such","take","than","t
hem","well","were"];

@key = ""
@@alphabet =
"abcdefghijklmnopqrstuvwxyz".splitStripUniqDown
@cypherbet = @@alphabet
@cypherhash = makehash(@cypherbet, @@alphabet)
@plainhash = makehash(@@alphabet, @cypherbet)

end

def key=(key)

@key = key
@cypherbet = key.splitStripUniqDown
@cypherbet.delete_if { |c| not @@alphabet.include?(c)
}
@@alphabet.each { |c| @cypherbet.push(c) unless
@cypherbet.include?(c) }
@cypherhash = makehash(@cypherbet, @@alphabet)
@plainhash = makehash(@@alphabet, @cypherbet)

end

def decode(cyphertext)

substitute(cyphertext, @cypherbet, @cypherhash)

end

def encode(plaintext)

substitute(plaintext, @@alphabet, @plainhash)

end


def crack(cyphertext)

i = 0

@@alphabet.each_permutation do |alphabet|

@cypherbet = alphabet
@cypherhash = makehash(@cypherbet, @@alphabet)
@plainhash = makehash(@@alphabet, @cypherbet)
plaintext = decode(cyphertext)
check(plaintext)
i = i + 1
puts i.to_s+" : "+plaintext if i % 1000 == 0

end

i

end

def check(plaintext)

wordcount = 0

plaintext.each(" ") do |word|

if @@common_english_words.include?(word.strip)

wordcount = wordcount + 1

end

end

if wordcount > 1

puts "Found "+wordcount.to_s+" English Words in :
"+plaintext

end

end

private

def substitute(fromtext, frombet, tohash)

totext = ""

lfromtext = fromtext.downcase

lfromtext.unpack("a" * lfromtext.length).each do |c|

cc = c
cc = tohash[c] if frombet.include?(c)
totext = totext + cc

end

totext

end

def makehash(alphabet0, alphabet1)

hash = {}
i = 0
alphabet0.each do |c|
hash[c] = alphabet1[i]
i = i + 1
end
hash

end

end



This class uses the following String extension:




class String

def splitStripUniqDown
lkey = self.downcase
lkey.unpack("a" * lkey.length).uniq.delete_if { |c|
c==" " }
end

end



Here's a usage example




cypher = SubstitutionCypher.new

cypher.key = "Barry John Northern"
puts cypher.key
puts cypher.cypherbet.join

cypher.key = "The Quick Brown Fox Jumped Over The Lazy Dogs"
puts cypher.key
puts cypher.cypherbet.join

cypher.key = "attack"
puts cypher.key
puts cypher.cypherbet.join
s = cypher.encode("this is the plain text")
puts s
p = cypher.decode(s)
puts p
cypher.check(p)

#cypher.crack(s)



As you can see, the final call to "crack" is commented out. It will usually take a very long time to find something and then only if the rearranged letters are few and limited to the start of the alphabet.


Ruby : Permutations

I've been interested in the field of cryptography recently after reading all of Dan Brown's excellent books, he wrote 3 crackers before the Da Vinci Code and I highly recommend them, especially Angels & Demons and Deception Point. Anyway, a brute force attack on a code simply iterates through every possible permutation of a key and decyphers the ciphertext until the resulting plaintext is intelligible. The two downsides of a brute force attack are firstly it can take many, many iterations before the correct key is found (which requires a lot of processing power) and secondly that matching the plaintext against an english dictionary every time round the loop is quite expensive. This makes for a slow algorithm. So, I guess Ruby wouldn't be the language of choice to implement something like this. However, Ruby is nice and expressive and good for learning the basic without getting bogged down with low level optimisations.

To this end I've developed an extension to the standard Array class which simply allows you to iterate through every unique permutation of an array. It is based on a Python algorithm I googled for but it's nice to see it more elegantly expressed in Ruby...


class Array

def each_permutation(&blockproc)

a = []
self.each do |c|
a.push(c)
end
n = a.length
p = Array.new(n+1,0)
i = 1

blockproc.call(a)

while i < n do

if p[i] < i

j = 0
j = p[i] if (i % 2) == 1
t = a[j]
a[j] = a[i]
a[i] = t

p[i] = p[i] + 1
i = 1

blockproc.call(a)

else

p[i] = 0
i = i + 1

end

end

end

end




["a","b","c"].each_permutation { |arr| puts arr.join }

abc
bac
cab
acb
bca
cba



Ruby's String class has a built in "crypt" method which applies a one-way cryptographic hash but I think it's nice to develop something new. I'll post more about the simple transposition encryption/decryption and crack algorithms that I developed soon.

Thursday, August 03, 2006

Ruby : Tetris Screenshot



My lastest version of Tetris and information about it can be found here

Tuesday, July 25, 2006

Ruby : Tetris

If you've heard what Ruby on Rails has done for Web Development you may be interested in Shattered. It's like Ruby on Rails for games development. It's only at version 0.3.2 at the moment, early days but it looks promising already. I'm investigating it with a view of using it as a rapid prototyping tool for more avant-garde game designs.

Everything you need to know can be found on the Shattered Weblog which has some very useful links in the top right hand corner.

I've written a simple tetris game using it to limber up :) I'm currently in the process of writing a little tutorial on the Shattered Wiki to help people out. I found it quite hard to get going at first due to the lack of examples and documentation. Saying that though, if you're prepared to look through the Shattered sources and reference documentation it's not too hard to find out what's going on and it shows some very nice likke Ruby tricks as well.

Check it out

Wednesday, July 05, 2006

Ruby : Pretty Code

I've been through many beginners tutorials for Ruby and I must say I really like this language. I think it's going to be very useful. The Pragmatic Programmers - Dave Thomas and Andy Hunt - give some good advice to learning a new language ... they say you must ask the question, "How will I know when I'm done?". I decided that my first "bout" of learning would be concluded when I had written my own program to prettify the text files I write for this blog! Usually I have to search and replace certain characters, manually add html tags for line breaks and code snippets, ensure that any preformatted line doesn't exceed 62 characters ... in short, a bit of a chore.



So here's the Ruby blog prettifier ... and of course, this blog post is the first to have been run through it! :)




class String

def subAfter str, ext
self[0..self.index(str)-1]+"."+ext
end

end

$column = 62
filename = ARGV[0].to_s

puts "Processing "+filename+"...\n"

File.open(filename, "r") do |file|

outFile = File.new(filename.subAfter(".","htm"), "w")

code = false;

file.each_line do |line|

if line.chomp == "@@@"

if code
outFile.puts "</pre></tt></blockquote>"
else
outFile.puts "<blockquote><pre><tt>"
end

code = !code;

else

if code

long = line
line = ""

while long.length > $column

space = long[0..$column].rindex(" ")

if space == nil
space = $column
end

line += long[0..space]+"\n"
long = long[space+1..-1]

end

line += long


line.gsub!(/</,"<")
line.gsub!(/>/,">")
else
line.gsub!(/\n\r|\n|\r/, "<br>")
end

outFile.puts line

end

end

outFile.close()

# This is a really long comment which I've added to show you
how the code prettifier neatly looks for the last space before
col 80 and splits the line :)
#_This_is_another_really_long_comment_which_I've_added_to_show_
you_how_the_code_prettifier_simply_cuts_the_line_if_there_are_n
o_spaces!

end

Tuesday, July 04, 2006

Ruby : to_eng Integer class extension

Here's the "English Number" method implemented in a more useful form; as an extension to the Integer class. It now works with negative numbers to. The usage is simple.




puts 0.to_eng(0) # arg is 0 for British, 1 for American
puts 12.to_eng(1)
puts -54.to_eng(1)
puts (10**600).to_eng(0)






# extend the Integer class with a "to english" method

class Integer

def to_eng scale

number = self.abs

if number == 0
return 'zero'
end

# ... code removed see previous post

if write > 0

exps = write.to_eng(scale)

# ...

end
# ...

if self<0
numString = "Minus " + numString
end

# Now we just return "numString"...
numString

end

end

Ruby : English Numbers

Well, it's been almost a month since my last post and what a busy month it's been! I started to write an article about a wrapper I had written for C++ iostreams that automatically closes them on scope exit until I found out that they do that anyway!! Talk about redundant levels of abstraction!

I've also found myself over-using templates recently, I mean they're fun to use but they tend to obfusticate code and generate hard-to-read compiler errors. I had this realisation after discovering Ruby - one of the easiest, most concise and powerful "scripting" languages I've ever had the pleasure to use. Check it out www.rubycentral.com. I've been following a great article called Learn To Program (Ruby), a precursor to the book by the same name.

On Chapter 8 there's a nifty English Number Program example that takes any positive integer and returns the english name of that number. It's quite limited and only works properly between 0-999. The author sets an exersize to make it work for thousands and millions and asks, how high can you go? My answer? Is that British Scale or American Scale? Either? Ok, well, in either case I can go as high as 10^600, which is the largest number-word below googolplex which is too large to include anyway :)


Here's my first Ruby program (based on the original example), enjoy ... for an example of conciseness see the code I wrote to sort the array of name/exponent pairs by exponent ...



def englishNumber number, scale
if number < 0 # No negative numbers.
return 'Please enter a number that isn\'t negative.'
end
if number == 0
return 'zero'
end

# check scale:

if scale<0 or scale>1
return 'scale out of range'
end

# valid scales:
# 0) British (long scale)
# 1) American (short scale)

numString = '' # This is the string we will return.

onesPlace = ['one', 'two', 'three', 'four',
'five', 'six', 'seven', 'eight', 'nine']
tensPlace = ['ten', 'twenty', 'thirty', 'forty', 'fifty',
'sixty', 'seventy', 'eighty', 'ninety']
teenagers = ['eleven', 'twelve', 'thirteen', 'fourteen',
'fifteen', 'sixteen', 'seventeen', 'eighteen',
'nineteen']

# short scale words

expTens = [['b'],['tr'],['quadr'],['quint'],['sext'],
['sept'],['oct'],['non'],['dec'],['undec'],
['duodec'],['tredec'],['quattuordec'],['quindec'],
['sexdec'],['septendec'],
['octodec'],['novemdec'],['vigint']]

# add short scale exponents and append "illion"'s!

exp = 9

expTens.each do |expTen|

expTen[0] = expTen[0] + 'illion';
expTen.push(exp)
exp = exp + 3

end

if scale == 0 # British (long scale)
# not using uncommon "milliard" (10**9)

# convert exponents to long scale

expTens.each do |expTen|

expTen[1] = (expTen[1] - 3) * 2

end

end

# add words and exponents common to both scales

expTens = [ ['hundred', 2], ['thousand', 3], ['million', 6] ]
+ expTens
expTens = expTens + [ ['googol', 100], ['centillion', 600] ]

# rational.rb says googolplex i.e. 10**(10**100) => Infinity

# unfortunatly now after the possible conversion to British
# long scale the expTens array is not in order. A googol's
# exponent is 100 which means it should be between
# sexdecillion and septendecillion.

# let's simply sort the array every time in case other such
# ordering errors occur

expTens.sort! { |x, y| x[1]<=>y[1] } # how easy was that! :)

left = number

# handle hundreds and above

expTens.reverse.each do |expTen|

value = 10**expTen[1]
write = left/value
left = left - write*value

if write > 0

exps = englishNumber(write, scale)
numString = numString + exps + ' ' + expTen[0]

if left > 0

if left < 100
numString = numString + ' and '
elsif
numString = numString + ', '
end

end

end

end

# handle teens

write = left/10 # How many tens left to write out?
left = left - write*10 # Subtract off those tens.

if write > 0
if ((write == 1) and (left > 0))
# Since we can't write "tenty-two" instead of "twelve",
# we have to make a special exception for these.
numString = numString + teenagers[left-1]
# The "-1" is because teenagers[3] is 'fourteen',
# not 'thirteen'.

# Since we took care of the digit in the ones place
# already, we have nothing left to write.
left = 0
else
numString = numString + tensPlace[write-1]
# The "-1" is because tensPlace[3] is 'forty',
# not 'thirty'.
end

if left > 0
# So we don't write 'sixtyfour'...
numString = numString + '-'
end
end

# handle ones

write = left # How many ones left to write out?
left = 0 # Subtract off those ones.

if write > 0
numString = numString + onesPlace[write-1]
# The "-1" is because onesPlace[3] is 'four', not 'three'.
end

# Now we just return "numString"...
numString
end

def formatEnglishNumber value, scale
value.to_s+" = "+englishNumber( value, scale)
end

def formatEnglishNumberExp exp, scale
"10**"+exp.to_s+" = "+englishNumber(10**exp, scale)
end

scale = -1;
while scale<0 or scale>1
puts "Use 0) British Long Scale 1) American Short Scale?"
scale = gets.chomp.to_i
end

puts "Some Numbers:"
puts
puts formatEnglishNumber( 0, scale)
puts formatEnglishNumber( 9, scale)
puts formatEnglishNumber( 10, scale)
puts formatEnglishNumber( 11, scale)
puts formatEnglishNumber( 17, scale)
puts formatEnglishNumber( 32, scale)
puts formatEnglishNumber( 88, scale)
puts formatEnglishNumber( 99, scale)
puts formatEnglishNumber(100, scale)
puts formatEnglishNumber(101, scale)
puts formatEnglishNumber(234, scale)
puts formatEnglishNumber(1000, scale)
puts formatEnglishNumber(3211, scale)
puts formatEnglishNumber(10000, scale)
puts formatEnglishNumber(100000, scale)
puts formatEnglishNumber(999999, scale)

puts
puts "Let's test the scale: "
puts

scalemax = 64;
if scale == 0
scalemax = 121
end

scalemax.times do |exp|
puts formatEnglishNumberExp(exp, scale)
end

puts
puts "Some bigger numbers!"
puts
puts formatEnglishNumber(100098765432134, scale)
puts formatEnglishNumber(2348723948732948723, scale)
puts formatEnglishNumber(2342342324100098765432134, scale)
puts formatEnglishNumber(124523598054213453230980329480, scale)
puts formatEnglishNumberExp(100, scale)
puts formatEnglishNumberExp(600, scale)
# googolplex is too big!!
#puts formatEnglishNumberExp(10**100, scale)

puts
puts "Press Enter To Quit"
gets

Tuesday, June 06, 2006

C# : Localization and Web Services

I've recently moved house, what hard work that is! We're pretty much back to normal now so hopefully I will be blogging about some more coding topics that I've found interesting. I've recently been developing a small WinForms application in C#. It needed to be localized for a number of languages and I was pleasantly surprised at how easy this was.

If you set the "Localizable" property of your main form or custom control to true you can then set the language. Any changes made for each selected language (the pull-down contains all that are available through the OS) are writting to a seperate .resx file for that language, so for example, Form1.resx is the default language UI and Form1.fr.resx is the French version. Subsequent language resx's only contain the changes from the default file. The beauty of the entire UI resource file being customizable and not just a string table is that you can change the layout of controls to accomodate longer strings etc.

One annoying thing I did encounter was that if you edit the .resx file by hand in the VS .NET editor (you'll have to Show All Files in the Solution Explorer first) then any additions you make (i.e. for Dialog Messages etc.) will be overwritten by the IDE if you make changes in that.


It's also very, very easy to consume a Web Service in VS .NET. It's simply a matter of adding a "Web Reference" to the project in the Solution Explorer, you enter the URL and it is automatically wrapped in a namespace/class for the language you are developing in. It can then be simply instantiated and used. I actually wrote a COM object in pure C++ that uses a web service and is in turn used by a C# application and through script!


Cross-language development is fun when things are decoupled like this but I find that developing software using Managed C++ with a combination of pure C++ APIs and Managed Types can be a bit of a headache. The Garbage Collector obviously doesn't let managed types own un-managed ones etc. so you have to do your own memory management on them. When all's said and done, if I'm using .NET I'd rather program in C# .. a language specifically designed and fit for purpose rather than Managed C++ which has been (very ingeniously) hacked to work for the .NET runtime.